Skip to content

Commit

Permalink
sessionctx: support session var tidb_last_plan_replayer_token (#37851)
Browse files Browse the repository at this point in the history
ref #37798
  • Loading branch information
Yisaer authored Sep 15, 2022
1 parent b5ab19c commit 5209ac2
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 6 deletions.
13 changes: 7 additions & 6 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,17 +152,18 @@ func TestPlanReplayer(t *testing.T) {
tk.MustExec("drop table if exists t")
tk.MustExec("create table t(a int, b int, index idx_a(a))")
tk.MustExec("alter table t set tiflash replica 1")
tk.MustExec("plan replayer dump explain select * from t where a=10")
tk.MustExec("plan replayer dump explain select /*+ read_from_storage(tiflash[t]) */ * from t")
tk.MustQuery("plan replayer dump explain select * from t where a=10")
tk.MustQuery("plan replayer dump explain select /*+ read_from_storage(tiflash[t]) */ * from t")

tk.MustExec("create table t1 (a int)")
tk.MustExec("create table t2 (a int)")
tk.MustExec("create definer=`root`@`127.0.0.1` view v1 as select * from t1")
tk.MustExec("create definer=`root`@`127.0.0.1` view v2 as select * from v1")
tk.MustExec("plan replayer dump explain with tmp as (select a from t1 group by t1.a) select * from tmp, t2 where t2.a=tmp.a;")
tk.MustExec("plan replayer dump explain select * from t1 where t1.a > (with cte1 as (select 1) select count(1) from cte1);")
tk.MustExec("plan replayer dump explain select * from v1")
tk.MustExec("plan replayer dump explain select * from v2")
tk.MustQuery("plan replayer dump explain with tmp as (select a from t1 group by t1.a) select * from tmp, t2 where t2.a=tmp.a;")
tk.MustQuery("plan replayer dump explain select * from t1 where t1.a > (with cte1 as (select 1) select count(1) from cte1);")
tk.MustQuery("plan replayer dump explain select * from v1")
tk.MustQuery("plan replayer dump explain select * from v2")
require.True(t, len(tk.Session().GetSessionVars().LastPlanReplayerToken) > 0)
}

func TestShow(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions executor/plan_replayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ func (e *PlanReplayerExec) Next(ctx context.Context, req *chunk.Chunk) error {
}
req.AppendString(0, res)
e.endFlag = true
e.ctx.GetSessionVars().LastPlanReplayerToken = res
return nil
}

Expand Down
8 changes: 8 additions & 0 deletions server/plan_replayer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,5 +136,13 @@ func prepareData4PlanReplayer(t *testing.T, client *testServerClient, statHandle
var filename string
err = rows.Scan(&filename)
require.NoError(t, err)
rows.Close()
rows = tk.MustQuery("select @@tidb_last_plan_replayer_token")
require.True(t, rows.Next(), "unexpected data")
var filename2 string
err = rows.Scan(&filename2)
require.NoError(t, err)
rows.Close()
require.Equal(t, filename, filename2)
return filename
}
3 changes: 3 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -1270,6 +1270,9 @@ type SessionVars struct {
// ranges would exceed the limit, it chooses less accurate ranges such as full range. 0 indicates that there is no
// memory limit for ranges.
RangeMaxSize int64

// LastPlanReplayerToken indicates the last plan replayer token
LastPlanReplayerToken string
}

// GetPreparedStmtByName returns the prepared statement specified by stmtName.
Expand Down
5 changes: 5 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,11 @@ var defaultSysVars = []*SysVar{
}
return string(info), nil
}},
{Scope: ScopeSession, Name: TiDBLastPlanReplayerToken, Value: "", ReadOnly: true,
GetSession: func(s *SessionVars) (string, error) {
return s.LastPlanReplayerToken, nil
},
},
/* The system variables below have INSTANCE scope */
{Scope: ScopeInstance, Name: TiDBLogFileMaxDays, Value: strconv.Itoa(config.GetGlobalConfig().Log.File.MaxDays), Type: TypeInt, MinValue: 0, MaxValue: math.MaxInt32, SetGlobal: func(s *SessionVars, val string) error {
maxAge, err := strconv.ParseInt(val, 10, 32)
Expand Down
3 changes: 3 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ const (
// TiDBLastDDLInfo is used to get the last ddl info within the current session.
TiDBLastDDLInfo = "tidb_last_ddl_info"

// TiDBLastPlanReplayerToken is used to get the last plan replayer token within the current session
TiDBLastPlanReplayerToken = "tidb_last_plan_replayer_token"

// TiDBConfig is a read-only variable that shows the config of the current server.
TiDBConfig = "tidb_config"

Expand Down

0 comments on commit 5209ac2

Please sign in to comment.