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

executor: support 'SHOW CONFIG' syntax to show configs of PD and TiKV instances #16230

Merged
merged 11 commits into from
Apr 16, 2020
5 changes: 4 additions & 1 deletion executor/memtable_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,10 @@ func (e *clusterConfigRetriever) retrieve(_ context.Context, sctx sessionctx.Con
return nil, nil
}
e.retrieved = true
return fetchClusterConfig(sctx, e.extractor.NodeTypes, e.extractor.Instances)
}

func fetchClusterConfig(sctx sessionctx.Context, nodeTypes, nodeAddrs set.StringSet) ([][]types.Datum, error) {
type result struct {
idx int
rows [][]types.Datum
Expand All @@ -159,7 +162,7 @@ func (e *clusterConfigRetriever) retrieve(_ context.Context, sctx sessionctx.Con
if err != nil {
return nil, err
}
serversInfo = filterClusterServerInfo(serversInfo, e.extractor.NodeTypes, e.extractor.Instances)
serversInfo = filterClusterServerInfo(serversInfo, nodeTypes, nodeAddrs)

var finalRows [][]types.Datum
wg := sync.WaitGroup{}
Expand Down
31 changes: 31 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ import (
"github.com/pingcap/tidb/util/format"
"github.com/pingcap/tidb/util/hack"
"github.com/pingcap/tidb/util/hint"
"github.com/pingcap/tidb/util/set"
"github.com/pingcap/tidb/util/sqlexec"
"github.com/pingcap/tidb/util/stringutil"
)
Expand Down Expand Up @@ -127,6 +128,8 @@ func (e *ShowExec) fetchAll(ctx context.Context) error {
return e.fetchShowCollation()
case ast.ShowColumns:
return e.fetchShowColumns(ctx)
case ast.ShowConfig:
return e.fetchShowClusterConfigs(ctx)
case ast.ShowCreateTable:
return e.fetchShowCreateTable()
case ast.ShowCreateSequence:
Expand Down Expand Up @@ -963,6 +966,34 @@ func (e *ShowExec) fetchShowCreateSequence() error {
return nil
}

// TestShowClusterConfigKey is the key used to store TestShowClusterConfigFunc.
var TestShowClusterConfigKey stringutil.StringerStr = "TestShowClusterConfigKey"

// TestShowClusterConfigFunc is used to test 'show config ...'.
type TestShowClusterConfigFunc func() ([][]types.Datum, error)

func (e *ShowExec) fetchShowClusterConfigs(ctx context.Context) error {
emptySet := set.NewStringSet()
var confItems [][]types.Datum
var err error
if f := e.ctx.Value(TestShowClusterConfigKey); f != nil {
confItems, err = f.(TestShowClusterConfigFunc)()
} else {
confItems, err = fetchClusterConfig(e.ctx, emptySet, emptySet)
}
if err != nil {
return err
}
for _, items := range confItems {
row := make([]interface{}, 0, 4)
for _, item := range items {
row = append(row, item.GetString())
}
e.appendRow(row)
}
return nil
}

func (e *ShowExec) fetchShowCreateTable() error {
tb, err := e.getTable()
if err != nil {
Expand Down
35 changes: 35 additions & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
plannercore "github.com/pingcap/tidb/planner/core"
"github.com/pingcap/tidb/session"
"github.com/pingcap/tidb/sessionctx"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/util/testkit"
"github.com/pingcap/tidb/util/testutil"
)
Expand Down Expand Up @@ -791,3 +792,37 @@ func (s *testSuite5) TestShowBuiltin(c *C) {
c.Assert("abs", Equals, rows[0][0].(string))
c.Assert("yearweek", Equals, rows[267][0].(string))
}

func (s *testSuite5) TestShowClusterConfig(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

var confItems [][]types.Datum
var confErr error
var confFunc executor.TestShowClusterConfigFunc = func() ([][]types.Datum, error) {
return confItems, confErr
}
tk.Se.SetValue(executor.TestShowClusterConfigKey, confFunc)
strs2Items := func(strs ...string) []types.Datum {
items := make([]types.Datum, 0, len(strs))
for _, s := range strs {
items = append(items, types.NewStringDatum(s))
}
return items
}
confItems = append(confItems, strs2Items("tidb", "127.0.0.1:1111", "log.level", "info"))
confItems = append(confItems, strs2Items("pd", "127.0.0.1:2222", "log.level", "info"))
confItems = append(confItems, strs2Items("tikv", "127.0.0.1:3333", "log.level", "info"))
tk.MustQuery("show config").Check(testkit.Rows(
"tidb 127.0.0.1:1111 log.level info",
"pd 127.0.0.1:2222 log.level info",
"tikv 127.0.0.1:3333 log.level info"))
tk.MustQuery("show config where type='tidb'").Check(testkit.Rows(
"tidb 127.0.0.1:1111 log.level info"))
tk.MustQuery("show config where type like '%ti%'").Check(testkit.Rows(
"tidb 127.0.0.1:1111 log.level info",
"tikv 127.0.0.1:3333 log.level info"))

confErr = fmt.Errorf("something unknown error")
c.Assert(tk.QueryToErr("show config"), ErrorMatches, confErr.Error())
}
2 changes: 2 additions & 0 deletions planner/core/planbuilder.go
Original file line number Diff line number Diff line change
Expand Up @@ -2920,6 +2920,8 @@ func buildShowSchema(s *ast.ShowStmt, isView bool, isSequence bool) (schema *exp
return buildTableRegionsSchema()
case ast.ShowEngines:
names = []string{"Engine", "Support", "Comment", "Transactions", "XA", "Savepoints"}
case ast.ShowConfig:
names = []string{"Type", "Instance", "Name", "Value"}
case ast.ShowDatabases:
names = []string{"Database"}
case ast.ShowOpenTables:
Expand Down