Skip to content

Commit

Permalink
executor: fix issue of query instance from CLUSTER_SLOW_QUERY return …
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-srebot authored Nov 17, 2022
1 parent cfec3aa commit 2e9f2c8
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 9 deletions.
32 changes: 23 additions & 9 deletions executor/slow_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ type slowQueryRetriever struct {
fileLine int
checker *slowLogChecker
columnValueFactoryMap map[string]slowQueryColumnValueFactory
instanceFactory func([]types.Datum)

taskList chan slowLogTask
stats *slowQueryRuntimeStats
Expand Down Expand Up @@ -96,6 +97,13 @@ func (e *slowQueryRetriever) initialize(ctx context.Context, sctx sessionctx.Con
// initialize column value factories.
e.columnValueFactoryMap = make(map[string]slowQueryColumnValueFactory, len(e.outputCols))
for idx, col := range e.outputCols {
if col.Name.O == util.ClusterTableInstanceColumnName {
e.instanceFactory, err = getInstanceColumnValueFactory(sctx, idx)
if err != nil {
return err
}
continue
}
factory, err := getColumnValueFactoryByName(sctx, col.Name.O, idx)
if err != nil {
return err
Expand Down Expand Up @@ -227,6 +235,11 @@ func (e *slowQueryRetriever) dataForSlowLog(ctx context.Context, sctx sessionctx
if len(rows) == 0 {
continue
}
if e.instanceFactory != nil {
for i := range rows {
e.instanceFactory(rows[i])
}
}
e.lastFetchSize = calculateDatumsSize(rows)
return rows, nil
}
Expand Down Expand Up @@ -777,19 +790,20 @@ func getColumnValueFactoryByName(sctx sessionctx.Context, colName string, column
row[columnIdx] = types.NewDatum(v)
return true, nil
}, nil
case util.ClusterTableInstanceColumnName:
instanceAddr, err := infoschema.GetInstanceAddr(sctx)
if err != nil {
return nil, err
}
return func(row []types.Datum, value string, tz *time.Location, checker *slowLogChecker) (valid bool, err error) {
row[columnIdx] = types.NewStringDatum(instanceAddr)
return true, nil
}, nil
}
return nil, nil
}

func getInstanceColumnValueFactory(sctx sessionctx.Context, columnIdx int) (func(row []types.Datum), error) {
instanceAddr, err := infoschema.GetInstanceAddr(sctx)
if err != nil {
return nil, err
}
return func(row []types.Datum) {
row[columnIdx] = types.NewStringDatum(instanceAddr)
}, nil
}

func parsePlan(planString string) string {
if len(planString) <= len(variable.SlowLogPlanPrefix)+len(variable.SlowLogPlanSuffix) {
return planString
Expand Down
6 changes: 6 additions & 0 deletions infoschema/cluster_tables_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
"github.com/pingcap/tidb/config"
"github.com/pingcap/tidb/domain"
"github.com/pingcap/tidb/executor"
"github.com/pingcap/tidb/infoschema"
"github.com/pingcap/tidb/kv"
"github.com/pingcap/tidb/parser"
"github.com/pingcap/tidb/parser/auth"
Expand Down Expand Up @@ -252,6 +253,11 @@ func SubTestSelectClusterTable(s *clusterTablesSuite) func(*testing.T) {
tk.MustQuery("select * from `CLUSTER_statements_summary_history`")
require.NotNil(t, re)
require.Equal(t, 0, len(re.Rows()))

// Test for https://github.com/pingcap/tidb/issues/33974
instanceAddr, err := infoschema.GetInstanceAddr(tk.Session())
require.NoError(t, err)
tk.MustQuery("select instance from `CLUSTER_SLOW_QUERY` where time='2019-02-12 19:33:56.571953'").Check(testkit.Rows(instanceAddr))
}
}
}
Expand Down

0 comments on commit 2e9f2c8

Please sign in to comment.