Skip to content

Commit

Permalink
log: Add upgrade progress information to the logs (#58246)
Browse files Browse the repository at this point in the history
ref #58255
  • Loading branch information
ystaticy authored Dec 20, 2024
1 parent de36787 commit 3e28938
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 0 deletions.
31 changes: 31 additions & 0 deletions pkg/session/bootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (
"fmt"
"os"
osuser "os/user"
"reflect"
"runtime"
"strconv"
"strings"
"time"
Expand Down Expand Up @@ -804,6 +806,29 @@ func bootstrap(s sessiontypes.Session) {
}
}

func getFunctionName(f func(sessiontypes.Session, int64)) (string, error) {
if f == nil {
return "", errors.New("function is nil")
}

funcPtr := reflect.ValueOf(f).Pointer()
if funcPtr == 0 {
return "", errors.New("invalid function pointer")
}

fullName := runtime.FuncForPC(funcPtr).Name()
if fullName == "" {
return "", errors.New("unable to retrieve function name")
}

parts := strings.Split(fullName, ".")
if len(parts) == 0 {
return "", errors.New("invalid function name structure")
}

return parts[len(parts)-1], nil
}

const (
// varTrue is the true value in mysql.TiDB table for boolean columns.
varTrue = "True"
Expand Down Expand Up @@ -1554,7 +1579,13 @@ func upgrade(s sessiontypes.Session) {
// It is only used in test.
addMockBootstrapVersionForTest(s)
for _, upgrade := range bootstrapVersion {
funcName, err := getFunctionName(upgrade)
terror.MustNil(err)
upgrade(s, ver)
logutil.BgLogger().Info("upgrade in progress, a version has just been completed or be skipped.",
zap.Int64("old-start-version", ver),
zap.String("in-progress-version", funcName),
zap.Int64("latest-version", currentBootstrapVersion))
}
if isNull {
upgradeToVer99After(s)
Expand Down
44 changes: 44 additions & 0 deletions pkg/session/bootstrap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2659,3 +2659,47 @@ func TestTiDBUpgradeToVer240(t *testing.T) {
require.Contains(t, string(chk.GetRow(0).GetBytes(1)), "idx_schema_table_state")
require.Contains(t, string(chk.GetRow(0).GetBytes(1)), "idx_schema_table_partition_state")
}

// testExampleAFunc is a example func for TestGetFuncName
func testExampleAFunc(s sessiontypes.Session, i int64) {}

// testExampleBFunc is a example func for TestGetFuncName
func testExampleBFunc(s sessiontypes.Session, i int64) {}

func TestGetFuncName(t *testing.T) {
// Test case 1: Pass a valid function
t.Run("Valid function", func(t *testing.T) {
name, err := getFunctionName(testExampleAFunc)
if err != nil {
t.Fatalf("Expected no error, got: %v", err)
}
if name != "testExampleAFunc" {
t.Errorf("Expected function name 'testExampleAFunc', got: %s", name)
}
})

// Test case 2: Pass another valid function
t.Run("Another valid function", func(t *testing.T) {
name, err := getFunctionName(testExampleBFunc)
if err != nil {
t.Fatalf("Expected no error, got: %v", err)
}
if name != "testExampleBFunc" {
t.Errorf("Expected function name 'testExampleBFunc', got: %s", name)
}
})

// Test case 3: Pass nil as the function
t.Run("Nil function", func(t *testing.T) {
name, err := getFunctionName(nil)
if err == nil {
t.Fatalf("Expected an error, got nil")
}
if name != "" {
t.Errorf("Expected empty function name, got: %s", name)
}
if err.Error() != "function is nil" {
t.Errorf("Expected error 'function is nil', got: %v", err)
}
})
}

0 comments on commit 3e28938

Please sign in to comment.