Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infoschema: fix tidb version showed in cluster_info table and update the go.mod for new sysutil (#16003) #18413

Merged
merged 6 commits into from
Jul 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion executor/infoschema_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -700,7 +700,7 @@ func (s *testInfoschemaClusterTableSuite) TestTiDBClusterInfo(c *C) {
tidbStatusAddr := fmt.Sprintf(":%d", config.GetGlobalConfig().Status.StatusPort)
row := func(cols ...string) string { return strings.Join(cols, " ") }
tk.MustQuery("select type, instance, status_address, version, git_hash from information_schema.cluster_info").Check(testkit.Rows(
row("tidb", ":4000", tidbStatusAddr, "5.7.25-TiDB-None", "None"),
row("tidb", ":4000", tidbStatusAddr, "None", "None"),
row("pd", mockAddr, mockAddr, "4.0.0-alpha", "mock-pd-githash"),
row("tikv", "store1", "", "", ""),
))
Expand Down
32 changes: 30 additions & 2 deletions infoschema/tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import (
"github.com/pingcap/parser/model"
"github.com/pingcap/parser/mysql"
"github.com/pingcap/parser/terror"
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain/infosync"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/meta/autoid"
Expand Down Expand Up @@ -1376,21 +1377,48 @@ func GetTiDBServerInfo(ctx sessionctx.Context) ([]ServerInfo, error) {
if err != nil {
return nil, errors.Trace(err)
}

var servers []ServerInfo
var isDefaultVersion bool
if len(config.GetGlobalConfig().ServerVersion) == 0 {
isDefaultVersion = true
}
for _, node := range tidbNodes {
servers = append(servers, ServerInfo{
ServerType: "tidb",
Address: fmt.Sprintf("%s:%d", node.IP, node.Port),
StatusAddr: fmt.Sprintf("%s:%d", node.IP, node.StatusPort),
Version: node.Version,
Version: FormatVersion(node.Version, isDefaultVersion),
GitHash: node.GitHash,
StartTimestamp: node.StartTimestamp,
})
}
return servers, nil
}

// FormatVersion make TiDBVersion consistent to TiKV and PD.
// The default TiDBVersion is 5.7.25-TiDB-${TiDBReleaseVersion}.
func FormatVersion(TiDBVersion string, isDefaultVersion bool) string {
var version, nodeVersion string

// The user hasn't set the config 'ServerVersion'.
if isDefaultVersion {
nodeVersion = TiDBVersion[strings.LastIndex(TiDBVersion, "TiDB-")+len("TiDB-"):]
if nodeVersion[0] == 'v' {
nodeVersion = nodeVersion[1:]
}
nodeVersions := strings.Split(nodeVersion, "-")
if len(nodeVersions) == 1 {
version = nodeVersions[0]
} else if len(nodeVersions) >= 2 {
version = fmt.Sprintf("%s-%s", nodeVersions[0], nodeVersions[1])
}
} else { // The user has already set the config 'ServerVersion',it would be a complex scene, so just use the 'ServerVersion' as version.
version = TiDBVersion
}

return version
}

// GetPDServerInfo returns all PD nodes information of cluster
func GetPDServerInfo(ctx sessionctx.Context) ([]ServerInfo, error) {
// Get PD servers info.
Expand Down
18 changes: 18 additions & 0 deletions infoschema/tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1297,3 +1297,21 @@ func (s *testTableSuite) TestPerformanceSchemaforPlanCache(c *C) {
tk.MustQuery("select plan_cache_hits, plan_in_cache from information_schema.statements_summary where digest_text='select * from t'").Check(
testkit.Rows("3 1"))
}

func (s *testTableSuite) TestFormatVersion(c *C) {
// Test for defaultVersions.
defaultVersions := []string{"5.7.25-TiDB-None", "5.7.25-TiDB-8.0.18", "5.7.25-TiDB-8.0.18-beta.1", "5.7.25-TiDB-v4.0.0-beta-446-g5268094af"}
defaultRes := []string{"None", "8.0.18", "8.0.18-beta.1", "4.0.0-beta"}
for i, v := range defaultVersions {
version := infoschema.FormatVersion(v, true)
c.Assert(version, Equals, defaultRes[i])
}

// Test for versions user set.
versions := []string{"8.0.18", "5.7.25-TiDB", "8.0.18-TiDB-4.0.0-beta.1"}
res := []string{"8.0.18", "5.7.25-TiDB", "8.0.18-TiDB-4.0.0-beta.1"}
for i, v := range versions {
version := infoschema.FormatVersion(v, false)
c.Assert(version, Equals, res[i])
}
}