From b8515ae354a537ae1cce9ef32c4ca1afe84eea9e Mon Sep 17 00:00:00 2001 From: Weizhen Wang Date: Thu, 23 Nov 2023 13:28:11 +0800 Subject: [PATCH] session: move session interface into new package (#48792) ref pingcap/tidb#44940 --- br/pkg/gluetidb/BUILD.bazel | 1 + br/pkg/gluetidb/glue.go | 9 +- cmd/benchdb/BUILD.bazel | 1 + cmd/benchdb/main.go | 3 +- cmd/ddltest/BUILD.bazel | 1 + cmd/ddltest/ddl_test.go | 3 +- pkg/ddl/BUILD.bazel | 1 + pkg/ddl/db_change_test.go | 3 +- pkg/ddl/ddl_api_test.go | 4 +- pkg/ddl/stat_test.go | 4 +- pkg/ddl/table_modify_test.go | 4 +- pkg/ddl/testutil/BUILD.bazel | 1 + pkg/ddl/testutil/testutil.go | 3 +- pkg/executor/BUILD.bazel | 1 + pkg/executor/inspection_result_test.go | 4 +- pkg/executor/test/issuetest/BUILD.bazel | 2 +- .../test/issuetest/executor_issue_test.go | 4 +- pkg/executor/update_test.go | 4 +- pkg/planner/core/BUILD.bazel | 1 + .../core/memtable_predicate_extractor_test.go | 3 +- pkg/planner/core/tests/prepare/BUILD.bazel | 1 + .../core/tests/prepare/prepare_test.go | 5 +- pkg/server/BUILD.bazel | 1 + pkg/server/driver_tidb.go | 3 +- pkg/server/handler/tikvhandler/BUILD.bazel | 1 + .../handler/tikvhandler/tikv_handler.go | 3 +- pkg/server/rpc_server.go | 3 +- pkg/session/BUILD.bazel | 2 + pkg/session/bench_test.go | 13 +- pkg/session/bootstrap.go | 307 +++++++++--------- pkg/session/bootstrap_test.go | 7 +- pkg/session/bootstraptest/BUILD.bazel | 1 + .../bootstraptest/bootstrap_upgrade_test.go | 11 +- pkg/session/mock_bootstrap.go | 31 +- pkg/session/nontransactional.go | 19 +- pkg/session/session.go | 79 +---- pkg/session/sync_upgrade.go | 3 +- pkg/session/testutil.go | 9 +- pkg/session/tidb.go | 3 +- pkg/session/types/BUILD.bazel | 21 ++ pkg/session/types/sesson_interface.go | 91 ++++++ pkg/store/gcworker/BUILD.bazel | 1 + pkg/store/gcworker/gc_worker.go | 5 +- pkg/table/tables/BUILD.bazel | 1 + pkg/table/tables/state_remote_test.go | 4 +- pkg/telemetry/cte_test/BUILD.bazel | 1 + pkg/telemetry/cte_test/cte_test.go | 3 +- pkg/testkit/BUILD.bazel | 1 + pkg/testkit/asynctestkit.go | 5 +- pkg/testkit/mocksessionmanager.go | 3 +- pkg/testkit/testkit.go | 13 +- pkg/util/ddl-checker/BUILD.bazel | 1 + pkg/util/ddl-checker/executable_checker.go | 3 +- 53 files changed, 405 insertions(+), 307 deletions(-) create mode 100644 pkg/session/types/BUILD.bazel create mode 100644 pkg/session/types/sesson_interface.go diff --git a/br/pkg/gluetidb/BUILD.bazel b/br/pkg/gluetidb/BUILD.bazel index 979f05b4e5b48..6ff71aa916e86 100644 --- a/br/pkg/gluetidb/BUILD.bazel +++ b/br/pkg/gluetidb/BUILD.bazel @@ -18,6 +18,7 @@ go_library( "//pkg/parser/model", "//pkg/parser/mysql", "//pkg/session", + "//pkg/session/types", "//pkg/sessionctx", "@com_github_pingcap_errors//:errors", "@com_github_pingcap_log//:log", diff --git a/br/pkg/gluetidb/glue.go b/br/pkg/gluetidb/glue.go index da42b1b1d9b71..c618281b4b2b3 100644 --- a/br/pkg/gluetidb/glue.go +++ b/br/pkg/gluetidb/glue.go @@ -22,6 +22,7 @@ import ( "github.com/pingcap/tidb/pkg/parser/model" "github.com/pingcap/tidb/pkg/parser/mysql" "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx" pd "github.com/tikv/pd/client" "go.uber.org/zap" @@ -58,7 +59,7 @@ type Glue struct { } type tidbSession struct { - se session.Session + se sessiontypes.Session } // GetDomain implements glue.Glue. @@ -358,7 +359,7 @@ func (gs *tidbSession) showCreatePlacementPolicy(policy *model.PolicyInfo) strin // mockSession is used for test. type mockSession struct { - se session.Session + se sessiontypes.Session globalVars map[string]string } @@ -434,11 +435,11 @@ func (s *mockSession) GetGlobalVariable(name string) (string, error) { // MockGlue only used for test type MockGlue struct { - se session.Session + se sessiontypes.Session GlobalVars map[string]string } -func (m *MockGlue) SetSession(se session.Session) { +func (m *MockGlue) SetSession(se sessiontypes.Session) { m.se = se } diff --git a/cmd/benchdb/BUILD.bazel b/cmd/benchdb/BUILD.bazel index cb9959c92f175..6e8acbd0bd3ff 100644 --- a/cmd/benchdb/BUILD.bazel +++ b/cmd/benchdb/BUILD.bazel @@ -8,6 +8,7 @@ go_library( deps = [ "//pkg/parser/terror", "//pkg/session", + "//pkg/session/types", "//pkg/store", "//pkg/store/driver", "//pkg/util/logutil", diff --git a/cmd/benchdb/main.go b/cmd/benchdb/main.go index 3af0e74fe7cc5..abe57173805d3 100644 --- a/cmd/benchdb/main.go +++ b/cmd/benchdb/main.go @@ -26,6 +26,7 @@ import ( "github.com/pingcap/log" "github.com/pingcap/tidb/pkg/parser/terror" "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/store" "github.com/pingcap/tidb/pkg/store/driver" "github.com/pingcap/tidb/pkg/util/logutil" @@ -88,7 +89,7 @@ func main() { type benchDB struct { store tikv.Storage - session session.Session + session sessiontypes.Session } func newBenchDB() *benchDB { diff --git a/cmd/ddltest/BUILD.bazel b/cmd/ddltest/BUILD.bazel index a036bcd5bba6f..bf3fce97cf57a 100644 --- a/cmd/ddltest/BUILD.bazel +++ b/cmd/ddltest/BUILD.bazel @@ -20,6 +20,7 @@ go_test( "//pkg/parser/model", "//pkg/parser/terror", "//pkg/session", + "//pkg/session/types", "//pkg/sessionctx", "//pkg/sessionctx/variable", "//pkg/sessiontxn", diff --git a/cmd/ddltest/ddl_test.go b/cmd/ddltest/ddl_test.go index 6711cb73fd25a..6c90ebf3a2fcc 100644 --- a/cmd/ddltest/ddl_test.go +++ b/cmd/ddltest/ddl_test.go @@ -39,6 +39,7 @@ import ( "github.com/pingcap/tidb/pkg/parser/model" "github.com/pingcap/tidb/pkg/parser/terror" "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx" "github.com/pingcap/tidb/pkg/sessionctx/variable" "github.com/pingcap/tidb/pkg/sessiontxn" @@ -76,7 +77,7 @@ type server struct { type ddlSuite struct { store kv.Storage dom *domain.Domain - s session.Session + s sessiontypes.Session ctx sessionctx.Context m sync.Mutex diff --git a/pkg/ddl/BUILD.bazel b/pkg/ddl/BUILD.bazel index c244607ec0351..744be405f4dc5 100644 --- a/pkg/ddl/BUILD.bazel +++ b/pkg/ddl/BUILD.bazel @@ -276,6 +276,7 @@ go_test( "//pkg/planner/core", "//pkg/server", "//pkg/session", + "//pkg/session/types", "//pkg/sessionctx", "//pkg/sessionctx/stmtctx", "//pkg/sessionctx/variable", diff --git a/pkg/ddl/db_change_test.go b/pkg/ddl/db_change_test.go index d2a02d1960fe2..8984002bd21af 100644 --- a/pkg/ddl/db_change_test.go +++ b/pkg/ddl/db_change_test.go @@ -34,6 +34,7 @@ import ( "github.com/pingcap/tidb/pkg/parser/model" "github.com/pingcap/tidb/pkg/parser/terror" "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx" "github.com/pingcap/tidb/pkg/sessiontxn" "github.com/pingcap/tidb/pkg/testkit" @@ -289,7 +290,7 @@ func TestTwoStates(t *testing.T) { } type stateCase struct { - session session.Session + session sessiontypes.Session rawStmt ast.StmtNode stmt sqlexec.Statement expectedExecErr string diff --git a/pkg/ddl/ddl_api_test.go b/pkg/ddl/ddl_api_test.go index e6dec271e9fac..9426844e43ae4 100644 --- a/pkg/ddl/ddl_api_test.go +++ b/pkg/ddl/ddl_api_test.go @@ -23,7 +23,7 @@ import ( "github.com/pingcap/tidb/pkg/ddl" "github.com/pingcap/tidb/pkg/kv" "github.com/pingcap/tidb/pkg/parser/model" - "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/testkit" "github.com/stretchr/testify/require" ) @@ -134,7 +134,7 @@ func TestIsJobRollbackable(t *testing.T) { } } -func enQueueDDLJobs(t *testing.T, sess session.Session, txn kv.Transaction, jobType model.ActionType, start, end int) { +func enQueueDDLJobs(t *testing.T, sess sessiontypes.Session, txn kv.Transaction, jobType model.ActionType, start, end int) { for i := start; i < end; i++ { job := &model.Job{ ID: int64(i), diff --git a/pkg/ddl/stat_test.go b/pkg/ddl/stat_test.go index 291b01e79f13e..8aaac9d9d913b 100644 --- a/pkg/ddl/stat_test.go +++ b/pkg/ddl/stat_test.go @@ -31,7 +31,7 @@ import ( "github.com/pingcap/tidb/pkg/parser/model" "github.com/pingcap/tidb/pkg/parser/mysql" "github.com/pingcap/tidb/pkg/parser/terror" - "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx" "github.com/pingcap/tidb/pkg/sessiontxn" "github.com/pingcap/tidb/pkg/testkit" @@ -148,7 +148,7 @@ func TestGetDDLInfo(t *testing.T) { tk.MustExec("rollback") } -func addDDLJobs(sess session.Session, txn kv.Transaction, job *model.Job) error { +func addDDLJobs(sess sessiontypes.Session, txn kv.Transaction, job *model.Job) error { b, err := job.Encode(true) if err != nil { return err diff --git a/pkg/ddl/table_modify_test.go b/pkg/ddl/table_modify_test.go index 975c09f518a55..1b69aed2d66e1 100644 --- a/pkg/ddl/table_modify_test.go +++ b/pkg/ddl/table_modify_test.go @@ -26,7 +26,7 @@ import ( "github.com/pingcap/tidb/pkg/kv" "github.com/pingcap/tidb/pkg/parser/model" "github.com/pingcap/tidb/pkg/parser/terror" - "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessiontxn" "github.com/pingcap/tidb/pkg/testkit" "github.com/pingcap/tidb/pkg/util" @@ -127,7 +127,7 @@ func TestConcurrentLockTables(t *testing.T) { tk2.MustExec("unlock tables") } -func testParallelExecSQL(t *testing.T, store kv.Storage, dom *domain.Domain, sql1, sql2 string, se1, se2 session.Session, f func(t *testing.T, err1, err2 error)) { +func testParallelExecSQL(t *testing.T, store kv.Storage, dom *domain.Domain, sql1, sql2 string, se1, se2 sessiontypes.Session, f func(t *testing.T, err1, err2 error)) { callback := &callback.TestDDLCallback{} times := 0 callback.OnJobRunBeforeExported = func(job *model.Job) { diff --git a/pkg/ddl/testutil/BUILD.bazel b/pkg/ddl/testutil/BUILD.bazel index 12628e9c102f2..54d11c7cc4084 100644 --- a/pkg/ddl/testutil/BUILD.bazel +++ b/pkg/ddl/testutil/BUILD.bazel @@ -10,6 +10,7 @@ go_library( "//pkg/kv", "//pkg/parser/model", "//pkg/session", + "//pkg/session/types", "//pkg/sessiontxn", "//pkg/table", "//pkg/table/tables", diff --git a/pkg/ddl/testutil/testutil.go b/pkg/ddl/testutil/testutil.go index 0fe58f7c34762..1e02e6a28d7a1 100644 --- a/pkg/ddl/testutil/testutil.go +++ b/pkg/ddl/testutil/testutil.go @@ -24,6 +24,7 @@ import ( "github.com/pingcap/tidb/pkg/kv" "github.com/pingcap/tidb/pkg/parser/model" "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessiontxn" "github.com/pingcap/tidb/pkg/table" "github.com/pingcap/tidb/pkg/table/tables" @@ -68,7 +69,7 @@ func ExecMultiSQLInGoroutine(s kv.Storage, dbName string, multiSQL []string, don } // ExtractAllTableHandles extracts all handles of a given table. -func ExtractAllTableHandles(se session.Session, dbName, tbName string) ([]int64, error) { +func ExtractAllTableHandles(se sessiontypes.Session, dbName, tbName string) ([]int64, error) { dom := domain.GetDomain(se) tbl, err := dom.InfoSchema().TableByName(model.NewCIStr(dbName), model.NewCIStr(tbName)) if err != nil { diff --git a/pkg/executor/BUILD.bazel b/pkg/executor/BUILD.bazel index 76277ce2336e0..5cf987f160f5b 100644 --- a/pkg/executor/BUILD.bazel +++ b/pkg/executor/BUILD.bazel @@ -401,6 +401,7 @@ go_test( "//pkg/planner/util", "//pkg/server", "//pkg/session", + "//pkg/session/types", "//pkg/sessionctx", "//pkg/sessionctx/binloginfo", "//pkg/sessionctx/stmtctx", diff --git a/pkg/executor/inspection_result_test.go b/pkg/executor/inspection_result_test.go index 27f47dcd3dd54..29638352b75e3 100644 --- a/pkg/executor/inspection_result_test.go +++ b/pkg/executor/inspection_result_test.go @@ -28,7 +28,7 @@ import ( "github.com/pingcap/sysutil" "github.com/pingcap/tidb/pkg/infoschema" "github.com/pingcap/tidb/pkg/parser/mysql" - "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx/variable" "github.com/pingcap/tidb/pkg/testkit" "github.com/pingcap/tidb/pkg/types" @@ -179,7 +179,7 @@ func TestInspectionResult(t *testing.T) { } } -func parseTime(t *testing.T, se session.Session, str string) types.Time { +func parseTime(t *testing.T, se sessiontypes.Session, str string) types.Time { time, err := types.ParseTime(se.GetSessionVars().StmtCtx.TypeCtx(), str, mysql.TypeDatetime, types.MaxFsp) require.NoError(t, err) return time diff --git a/pkg/executor/test/issuetest/BUILD.bazel b/pkg/executor/test/issuetest/BUILD.bazel index f03a2147ddc4d..bf67149870188 100644 --- a/pkg/executor/test/issuetest/BUILD.bazel +++ b/pkg/executor/test/issuetest/BUILD.bazel @@ -17,7 +17,7 @@ go_test( "//pkg/parser/auth", "//pkg/parser/charset", "//pkg/parser/mysql", - "//pkg/session", + "//pkg/session/types", "//pkg/testkit", "//pkg/util", "//pkg/util/dbterror/exeerrors", diff --git a/pkg/executor/test/issuetest/executor_issue_test.go b/pkg/executor/test/issuetest/executor_issue_test.go index 78eeee0f0e019..74cb0556517b9 100644 --- a/pkg/executor/test/issuetest/executor_issue_test.go +++ b/pkg/executor/test/issuetest/executor_issue_test.go @@ -28,7 +28,7 @@ import ( "github.com/pingcap/tidb/pkg/parser/auth" "github.com/pingcap/tidb/pkg/parser/charset" "github.com/pingcap/tidb/pkg/parser/mysql" - "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/testkit" "github.com/pingcap/tidb/pkg/util" "github.com/pingcap/tidb/pkg/util/dbterror/exeerrors" @@ -576,7 +576,7 @@ func TestIssue42662(t *testing.T) { sm := &testkit.MockSessionManager{ PS: []*util.ProcessInfo{tk.Session().ShowProcess()}, } - sm.Conn = make(map[uint64]session.Session) + sm.Conn = make(map[uint64]sessiontypes.Session) sm.Conn[tk.Session().GetSessionVars().ConnectionID] = tk.Session() dom.ServerMemoryLimitHandle().SetSessionManager(sm) go dom.ServerMemoryLimitHandle().Run() diff --git a/pkg/executor/update_test.go b/pkg/executor/update_test.go index e6052ca91ea61..8c17c045808be 100644 --- a/pkg/executor/update_test.go +++ b/pkg/executor/update_test.go @@ -21,7 +21,7 @@ import ( "time" "github.com/pingcap/tidb/pkg/kv" - "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx/variable" "github.com/pingcap/tidb/pkg/testkit" "github.com/stretchr/testify/require" @@ -50,7 +50,7 @@ func testUpdatePKLazyCheck(t *testing.T, tk *testkit.TestKit, clusteredIndex var tk.MustExec("commit") } -func getPresumeExistsCount(t *testing.T, se session.Session) int { +func getPresumeExistsCount(t *testing.T, se sessiontypes.Session) int { txn, err := se.Txn(false) require.NoError(t, err) buf := txn.GetMemBuffer() diff --git a/pkg/planner/core/BUILD.bazel b/pkg/planner/core/BUILD.bazel index d1723b8a7b38e..0f3516a348b7b 100644 --- a/pkg/planner/core/BUILD.bazel +++ b/pkg/planner/core/BUILD.bazel @@ -250,6 +250,7 @@ go_test( "//pkg/planner/property", "//pkg/planner/util", "//pkg/session", + "//pkg/session/types", "//pkg/sessionctx", "//pkg/sessionctx/variable", "//pkg/sessiontxn", diff --git a/pkg/planner/core/memtable_predicate_extractor_test.go b/pkg/planner/core/memtable_predicate_extractor_test.go index 2c53fa8f2120b..77f1585807d2b 100644 --- a/pkg/planner/core/memtable_predicate_extractor_test.go +++ b/pkg/planner/core/memtable_predicate_extractor_test.go @@ -32,6 +32,7 @@ import ( "github.com/pingcap/tidb/pkg/planner" plannercore "github.com/pingcap/tidb/pkg/planner/core" "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/testkit" "github.com/pingcap/tidb/pkg/types" "github.com/pingcap/tidb/pkg/util/hint" @@ -39,7 +40,7 @@ import ( "github.com/stretchr/testify/require" ) -func getLogicalMemTable(t *testing.T, dom *domain.Domain, se session.Session, parser *parser.Parser, sql string) *plannercore.LogicalMemTable { +func getLogicalMemTable(t *testing.T, dom *domain.Domain, se sessiontypes.Session, parser *parser.Parser, sql string) *plannercore.LogicalMemTable { stmt, err := parser.ParseOneStmt(sql, "", "") require.NoError(t, err) diff --git a/pkg/planner/core/tests/prepare/BUILD.bazel b/pkg/planner/core/tests/prepare/BUILD.bazel index 106222cc3069d..970c173c54bf6 100644 --- a/pkg/planner/core/tests/prepare/BUILD.bazel +++ b/pkg/planner/core/tests/prepare/BUILD.bazel @@ -20,6 +20,7 @@ go_test( "//pkg/parser/auth", "//pkg/planner/core", "//pkg/session", + "//pkg/session/types", "//pkg/sessionctx/variable", "//pkg/testkit", "//pkg/testkit/testsetup", diff --git a/pkg/planner/core/tests/prepare/prepare_test.go b/pkg/planner/core/tests/prepare/prepare_test.go index 3da9d010e8974..99ace9c81672e 100644 --- a/pkg/planner/core/tests/prepare/prepare_test.go +++ b/pkg/planner/core/tests/prepare/prepare_test.go @@ -34,6 +34,7 @@ import ( "github.com/pingcap/tidb/pkg/parser/auth" "github.com/pingcap/tidb/pkg/planner/core" "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx/variable" "github.com/pingcap/tidb/pkg/testkit" "github.com/pingcap/tidb/pkg/util/hint" @@ -635,7 +636,7 @@ func TestPrepareCacheForPartition(t *testing.T) { } } -func newSession(t *testing.T, store kv.Storage, dbName string) session.Session { +func newSession(t *testing.T, store kv.Storage, dbName string) sessiontypes.Session { se, err := session.CreateSession4Test(store) require.NoError(t, err) mustExec(t, se, "create database if not exists "+dbName) @@ -643,7 +644,7 @@ func newSession(t *testing.T, store kv.Storage, dbName string) session.Session { return se } -func mustExec(t *testing.T, se session.Session, sql string) { +func mustExec(t *testing.T, se sessiontypes.Session, sql string) { _, err := se.Execute(context.Background(), sql) require.NoError(t, err) } diff --git a/pkg/server/BUILD.bazel b/pkg/server/BUILD.bazel index bdf0715b60e44..86901c1b84b5e 100644 --- a/pkg/server/BUILD.bazel +++ b/pkg/server/BUILD.bazel @@ -63,6 +63,7 @@ go_library( "//pkg/server/metrics", "//pkg/session", "//pkg/session/txninfo", + "//pkg/session/types", "//pkg/sessionctx", "//pkg/sessionctx/sessionstates", "//pkg/sessionctx/stmtctx", diff --git a/pkg/server/driver_tidb.go b/pkg/server/driver_tidb.go index 9e70f5ad18d30..c1d51b354e01a 100644 --- a/pkg/server/driver_tidb.go +++ b/pkg/server/driver_tidb.go @@ -32,6 +32,7 @@ import ( "github.com/pingcap/tidb/pkg/server/internal/column" "github.com/pingcap/tidb/pkg/server/internal/resultset" "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx" "github.com/pingcap/tidb/pkg/sessionctx/sessionstates" "github.com/pingcap/tidb/pkg/sessionctx/stmtctx" @@ -55,7 +56,7 @@ func NewTiDBDriver(store kv.Storage) *TiDBDriver { // TiDBContext implements QueryCtx. type TiDBContext struct { - session.Session + sessiontypes.Session stmts map[int]*TiDBStatement } diff --git a/pkg/server/handler/tikvhandler/BUILD.bazel b/pkg/server/handler/tikvhandler/BUILD.bazel index e4ba4a2ae109a..824f6a4053f4e 100644 --- a/pkg/server/handler/tikvhandler/BUILD.bazel +++ b/pkg/server/handler/tikvhandler/BUILD.bazel @@ -19,6 +19,7 @@ go_library( "//pkg/server/handler", "//pkg/session", "//pkg/session/txninfo", + "//pkg/session/types", "//pkg/sessionctx", "//pkg/sessionctx/binloginfo", "//pkg/sessionctx/variable", diff --git a/pkg/server/handler/tikvhandler/tikv_handler.go b/pkg/server/handler/tikvhandler/tikv_handler.go index 4f5863a62d817..394212e331ea9 100644 --- a/pkg/server/handler/tikvhandler/tikv_handler.go +++ b/pkg/server/handler/tikvhandler/tikv_handler.go @@ -46,6 +46,7 @@ import ( "github.com/pingcap/tidb/pkg/server/handler" "github.com/pingcap/tidb/pkg/session" "github.com/pingcap/tidb/pkg/session/txninfo" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx" "github.com/pingcap/tidb/pkg/sessionctx/binloginfo" "github.com/pingcap/tidb/pkg/sessionctx/variable" @@ -825,7 +826,7 @@ type SchemaTableStorage struct { } func getSchemaTablesStorageInfo(h *SchemaStorageHandler, schema *model.CIStr, table *model.CIStr) (messages []*SchemaTableStorage, err error) { - var s session.Session + var s sessiontypes.Session if s, err = session.CreateSession(h.Store); err != nil { return } diff --git a/pkg/server/rpc_server.go b/pkg/server/rpc_server.go index 64225ccaca210..231a8ecc0fcef 100644 --- a/pkg/server/rpc_server.go +++ b/pkg/server/rpc_server.go @@ -33,6 +33,7 @@ import ( "github.com/pingcap/tidb/pkg/privilege" "github.com/pingcap/tidb/pkg/privilege/privileges" "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx/variable" "github.com/pingcap/tidb/pkg/util" "github.com/pingcap/tidb/pkg/util/logutil" @@ -214,7 +215,7 @@ func (s *rpcServer) handleCopRequest(ctx context.Context, req *coprocessor.Reque return h.HandleRequest(ctx, req) } -func (s *rpcServer) createSession() (session.Session, error) { +func (s *rpcServer) createSession() (sessiontypes.Session, error) { se, err := session.CreateSessionWithDomain(s.dom.Store(), s.dom) if err != nil { return nil, err diff --git a/pkg/session/BUILD.bazel b/pkg/session/BUILD.bazel index 14b3a33123521..b4c484de4123f 100644 --- a/pkg/session/BUILD.bazel +++ b/pkg/session/BUILD.bazel @@ -53,6 +53,7 @@ go_library( "//pkg/privilege/privileges", "//pkg/session/metrics", "//pkg/session/txninfo", + "//pkg/session/types", "//pkg/sessionctx", "//pkg/sessionctx/binloginfo", "//pkg/sessionctx/sessionstates", @@ -142,6 +143,7 @@ go_test( "//pkg/meta", "//pkg/parser/ast", "//pkg/parser/auth", + "//pkg/session/types", "//pkg/sessionctx", "//pkg/sessionctx/variable", "//pkg/statistics", diff --git a/pkg/session/bench_test.go b/pkg/session/bench_test.go index 0064336dd5d3c..ec8a226c6e009 100644 --- a/pkg/session/bench_test.go +++ b/pkg/session/bench_test.go @@ -31,6 +31,7 @@ import ( "github.com/pingcap/tidb/pkg/expression" "github.com/pingcap/tidb/pkg/kv" "github.com/pingcap/tidb/pkg/parser/ast" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/store/mockstore" "github.com/pingcap/tidb/pkg/util/benchdaily" "github.com/pingcap/tidb/pkg/util/chunk" @@ -43,7 +44,7 @@ import ( var smallCount = 100 var bigCount = 10000 -func prepareBenchSession() (Session, *domain.Domain, kv.Storage) { +func prepareBenchSession() (sessiontypes.Session, *domain.Domain, kv.Storage) { config.UpdateGlobal(func(cfg *config.Config) { cfg.Instance.EnableSlowLog.Store(false) }) @@ -65,7 +66,7 @@ func prepareBenchSession() (Session, *domain.Domain, kv.Storage) { return se, domain, store } -func prepareBenchData(se Session, colType string, valueFormat string, valueCount int) { +func prepareBenchData(se sessiontypes.Session, colType string, valueFormat string, valueCount int) { mustExecute(se, "drop table if exists t") mustExecute(se, fmt.Sprintf("create table t (pk int primary key auto_increment, col %s, index idx (col))", colType)) mustExecute(se, "begin") @@ -75,7 +76,7 @@ func prepareBenchData(se Session, colType string, valueFormat string, valueCount mustExecute(se, "commit") } -func prepareNonclusteredBenchData(se Session, colType string, valueFormat string, valueCount int) { +func prepareNonclusteredBenchData(se sessiontypes.Session, colType string, valueFormat string, valueCount int) { mustExecute(se, "drop table if exists t") mustExecute(se, fmt.Sprintf("create table t (pk int primary key /*T![clustered_index] NONCLUSTERED */ auto_increment, col %s, index idx (col))", colType)) mustExecute(se, "begin") @@ -85,7 +86,7 @@ func prepareNonclusteredBenchData(se Session, colType string, valueFormat string mustExecute(se, "commit") } -func prepareSortBenchData(se Session, colType string, valueFormat string, valueCount int) { +func prepareSortBenchData(se sessiontypes.Session, colType string, valueFormat string, valueCount int) { mustExecute(se, "drop table if exists t") mustExecute(se, fmt.Sprintf("create table t (pk int primary key auto_increment, col %s)", colType)) mustExecute(se, "begin") @@ -100,7 +101,7 @@ func prepareSortBenchData(se Session, colType string, valueFormat string, valueC mustExecute(se, "commit") } -func prepareJoinBenchData(se Session, colType string, valueFormat string, valueCount int) { +func prepareJoinBenchData(se sessiontypes.Session, colType string, valueFormat string, valueCount int) { mustExecute(se, "drop table if exists t") mustExecute(se, fmt.Sprintf("create table t (pk int primary key auto_increment, col %s)", colType)) mustExecute(se, "begin") @@ -125,7 +126,7 @@ func readResult(ctx context.Context, rs sqlexec.RecordSet, count int) { rs.Close() } -func hasPlan(ctx context.Context, b *testing.B, se Session, plan string) { +func hasPlan(ctx context.Context, b *testing.B, se sessiontypes.Session, plan string) { find := false rs, err := se.Execute(ctx, "explain select * from t where col = 'hello 64'") if err != nil { diff --git a/pkg/session/bootstrap.go b/pkg/session/bootstrap.go index bc3b231aa5787..ba14a7828b1b5 100644 --- a/pkg/session/bootstrap.go +++ b/pkg/session/bootstrap.go @@ -44,6 +44,7 @@ import ( "github.com/pingcap/tidb/pkg/parser/mysql" "github.com/pingcap/tidb/pkg/parser/terror" "github.com/pingcap/tidb/pkg/planner/core" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx/variable" "github.com/pingcap/tidb/pkg/table/tables" timertable "github.com/pingcap/tidb/pkg/timer/tablestore" @@ -694,7 +695,7 @@ const ( var CreateTimers = timertable.CreateTimerTableSQL("mysql", "tidb_timers") // bootstrap initiates system DB for a store. -func bootstrap(s Session) { +func bootstrap(s sessiontypes.Session) { startTime := time.Now() err := InitMDLVariableForBootstrap(s.GetStore()) if err != nil { @@ -1045,7 +1046,7 @@ func DisableRunBootstrapSQLFileInTest() { } var ( - bootstrapVersion = []func(Session, int64){ + bootstrapVersion = []func(sessiontypes.Session, int64){ upgradeToVer2, upgradeToVer3, upgradeToVer4, @@ -1187,7 +1188,7 @@ var ( } ) -func checkBootstrapped(s Session) (bool, error) { +func checkBootstrapped(s sessiontypes.Session) (bool, error) { ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap) // Check if system db exists. _, err := s.ExecuteInternal(ctx, "USE %n", mysql.SystemDB) @@ -1215,7 +1216,7 @@ func checkBootstrapped(s Session) (bool, error) { // getTiDBVar gets variable value from mysql.tidb table. // Those variables are used by TiDB server. -func getTiDBVar(s Session, name string) (sVal string, isNull bool, e error) { +func getTiDBVar(s sessiontypes.Session, name string) (sVal string, isNull bool, e error) { ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap) rs, err := s.ExecuteInternal(ctx, `SELECT HIGH_PRIORITY VARIABLE_VALUE FROM %n.%n WHERE VARIABLE_NAME= %?`, mysql.SystemDB, @@ -1249,7 +1250,7 @@ var ( // upgrade function will do some upgrade works, when the system is bootstrapped by low version TiDB server // For example, add new system variables into mysql.global_variables table. -func upgrade(s Session) { +func upgrade(s sessiontypes.Session) { ver, err := getBootstrapVersion(s) terror.MustNil(err) if ver >= currentBootstrapVersion { @@ -1342,7 +1343,7 @@ func checkOwnerVersion(ctx context.Context, dom *domain.Domain) (bool, error) { } // upgradeToVer2 updates to version 2. -func upgradeToVer2(s Session, ver int64) { +func upgradeToVer2(s sessiontypes.Session, ver int64) { if ver >= version2 { return } @@ -1360,7 +1361,7 @@ func upgradeToVer2(s Session, ver int64) { } // upgradeToVer3 updates to version 3. -func upgradeToVer3(s Session, ver int64) { +func upgradeToVer3(s sessiontypes.Session, ver int64) { if ver >= version3 { return } @@ -1369,14 +1370,14 @@ func upgradeToVer3(s Session, ver int64) { } // upgradeToVer4 updates to version 4. -func upgradeToVer4(s Session, ver int64) { +func upgradeToVer4(s sessiontypes.Session, ver int64) { if ver >= version4 { return } mustExecute(s, CreateStatsMetaTable) } -func upgradeToVer5(s Session, ver int64) { +func upgradeToVer5(s sessiontypes.Session, ver int64) { if ver >= version5 { return } @@ -1384,7 +1385,7 @@ func upgradeToVer5(s Session, ver int64) { mustExecute(s, CreateStatsBucketsTable) } -func upgradeToVer6(s Session, ver int64) { +func upgradeToVer6(s sessiontypes.Session, ver int64) { if ver >= version6 { return } @@ -1393,7 +1394,7 @@ func upgradeToVer6(s Session, ver int64) { mustExecute(s, "UPDATE HIGH_PRIORITY mysql.user SET Super_priv='Y'") } -func upgradeToVer7(s Session, ver int64) { +func upgradeToVer7(s sessiontypes.Session, ver int64) { if ver >= version7 { return } @@ -1402,7 +1403,7 @@ func upgradeToVer7(s Session, ver int64) { mustExecute(s, "UPDATE HIGH_PRIORITY mysql.user SET Process_priv='Y'") } -func upgradeToVer8(s Session, ver int64) { +func upgradeToVer8(s sessiontypes.Session, ver int64) { if ver >= version8 { return } @@ -1414,7 +1415,7 @@ func upgradeToVer8(s Session, ver int64) { upgradeToVer7(s, ver) } -func upgradeToVer9(s Session, ver int64) { +func upgradeToVer9(s sessiontypes.Session, ver int64) { if ver >= version9 { return } @@ -1423,7 +1424,7 @@ func upgradeToVer9(s Session, ver int64) { mustExecute(s, "UPDATE HIGH_PRIORITY mysql.user SET Trigger_priv='Y'") } -func doReentrantDDL(s Session, sql string, ignorableErrs ...error) { +func doReentrantDDL(s sessiontypes.Session, sql string, ignorableErrs ...error) { ctx, cancel := context.WithTimeout(context.Background(), time.Duration(internalSQLTimeout)*time.Second) ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnBootstrap) _, err := s.ExecuteInternal(ctx, sql) @@ -1438,7 +1439,7 @@ func doReentrantDDL(s Session, sql string, ignorableErrs ...error) { } } -func upgradeToVer10(s Session, ver int64) { +func upgradeToVer10(s sessiontypes.Session, ver int64) { if ver >= version10 { return } @@ -1449,7 +1450,7 @@ func upgradeToVer10(s Session, ver int64) { doReentrantDDL(s, "ALTER TABLE mysql.stats_histograms DROP COLUMN use_count_to_estimate", dbterror.ErrCantDropFieldOrKey) } -func upgradeToVer11(s Session, ver int64) { +func upgradeToVer11(s sessiontypes.Session, ver int64) { if ver >= version11 { return } @@ -1457,7 +1458,7 @@ func upgradeToVer11(s Session, ver int64) { mustExecute(s, "UPDATE HIGH_PRIORITY mysql.user SET References_priv='Y'") } -func upgradeToVer12(s Session, ver int64) { +func upgradeToVer12(s sessiontypes.Session, ver int64) { if ver >= version12 { return } @@ -1502,7 +1503,7 @@ func upgradeToVer12(s Session, ver int64) { mustExecute(s, "COMMIT") } -func upgradeToVer13(s Session, ver int64) { +func upgradeToVer13(s sessiontypes.Session, ver int64) { if ver >= version13 { return } @@ -1522,7 +1523,7 @@ func upgradeToVer13(s Session, ver int64) { mustExecute(s, "UPDATE HIGH_PRIORITY mysql.user SET Create_view_priv='Y',Show_view_priv='Y' WHERE Create_priv='Y'") } -func upgradeToVer14(s Session, ver int64) { +func upgradeToVer14(s sessiontypes.Session, ver int64) { if ver >= version14 { return } @@ -1542,35 +1543,35 @@ func upgradeToVer14(s Session, ver int64) { } } -func upgradeToVer15(s Session, ver int64) { +func upgradeToVer15(s sessiontypes.Session, ver int64) { if ver >= version15 { return } doReentrantDDL(s, CreateGCDeleteRangeTable) } -func upgradeToVer16(s Session, ver int64) { +func upgradeToVer16(s sessiontypes.Session, ver int64) { if ver >= version16 { return } doReentrantDDL(s, "ALTER TABLE mysql.stats_histograms ADD COLUMN `cm_sketch` BLOB", infoschema.ErrColumnExists) } -func upgradeToVer17(s Session, ver int64) { +func upgradeToVer17(s sessiontypes.Session, ver int64) { if ver >= version17 { return } doReentrantDDL(s, "ALTER TABLE mysql.user MODIFY User CHAR(32)") } -func upgradeToVer18(s Session, ver int64) { +func upgradeToVer18(s sessiontypes.Session, ver int64) { if ver >= version18 { return } doReentrantDDL(s, "ALTER TABLE mysql.stats_histograms ADD COLUMN `tot_col_size` BIGINT(64) NOT NULL DEFAULT 0", infoschema.ErrColumnExists) } -func upgradeToVer19(s Session, ver int64) { +func upgradeToVer19(s sessiontypes.Session, ver int64) { if ver >= version19 { return } @@ -1579,7 +1580,7 @@ func upgradeToVer19(s Session, ver int64) { doReentrantDDL(s, "ALTER TABLE mysql.columns_priv MODIFY User CHAR(32)") } -func upgradeToVer20(s Session, ver int64) { +func upgradeToVer20(s sessiontypes.Session, ver int64) { if ver >= version20 { return } @@ -1587,7 +1588,7 @@ func upgradeToVer20(s Session, ver int64) { doReentrantDDL(s, CreateStatsFeedbackTable) } -func upgradeToVer21(s Session, ver int64) { +func upgradeToVer21(s sessiontypes.Session, ver int64) { if ver >= version21 { return } @@ -1598,14 +1599,14 @@ func upgradeToVer21(s Session, ver int64) { doReentrantDDL(s, "ALTER TABLE mysql.gc_delete_range DROP INDEX element_id", dbterror.ErrCantDropFieldOrKey) } -func upgradeToVer22(s Session, ver int64) { +func upgradeToVer22(s sessiontypes.Session, ver int64) { if ver >= version22 { return } doReentrantDDL(s, "ALTER TABLE mysql.stats_histograms ADD COLUMN `stats_ver` BIGINT(64) NOT NULL DEFAULT 0", infoschema.ErrColumnExists) } -func upgradeToVer23(s Session, ver int64) { +func upgradeToVer23(s sessiontypes.Session, ver int64) { if ver >= version23 { return } @@ -1613,7 +1614,7 @@ func upgradeToVer23(s Session, ver int64) { } // writeSystemTZ writes system timezone info into mysql.tidb -func writeSystemTZ(s Session) { +func writeSystemTZ(s sessiontypes.Session) { mustExecute(s, `INSERT HIGH_PRIORITY INTO %n.%n VALUES (%?, %?, "TiDB Global System Timezone.") ON DUPLICATE KEY UPDATE VARIABLE_VALUE= %?`, mysql.SystemDB, mysql.TiDBTable, @@ -1624,7 +1625,7 @@ func writeSystemTZ(s Session) { } // upgradeToVer24 initializes `System` timezone according to docs/design/2018-09-10-adding-tz-env.md -func upgradeToVer24(s Session, ver int64) { +func upgradeToVer24(s sessiontypes.Session, ver int64) { if ver >= version24 { return } @@ -1632,7 +1633,7 @@ func upgradeToVer24(s Session, ver int64) { } // upgradeToVer25 updates tidb_max_chunk_size to new low bound value 32 if previous value is small than 32. -func upgradeToVer25(s Session, ver int64) { +func upgradeToVer25(s sessiontypes.Session, ver int64) { if ver >= version25 { return } @@ -1641,7 +1642,7 @@ func upgradeToVer25(s Session, ver int64) { mustExecute(s, sql) } -func upgradeToVer26(s Session, ver int64) { +func upgradeToVer26(s sessiontypes.Session, ver int64) { if ver >= version26 { return } @@ -1656,21 +1657,21 @@ func upgradeToVer26(s Session, ver int64) { mustExecute(s, "UPDATE HIGH_PRIORITY mysql.user SET Create_view_priv='Y',Show_view_priv='Y' WHERE Create_priv='Y'") } -func upgradeToVer27(s Session, ver int64) { +func upgradeToVer27(s sessiontypes.Session, ver int64) { if ver >= version27 { return } doReentrantDDL(s, "ALTER TABLE mysql.stats_histograms ADD COLUMN `correlation` DOUBLE NOT NULL DEFAULT 0", infoschema.ErrColumnExists) } -func upgradeToVer28(s Session, ver int64) { +func upgradeToVer28(s sessiontypes.Session, ver int64) { if ver >= version28 { return } doReentrantDDL(s, CreateBindInfoTable) } -func upgradeToVer29(s Session, ver int64) { +func upgradeToVer29(s sessiontypes.Session, ver int64) { // upgradeToVer29 only need to be run when the current version is 28. if ver != version28 { return @@ -1680,42 +1681,42 @@ func upgradeToVer29(s Session, ver int64) { doReentrantDDL(s, "ALTER TABLE mysql.bind_info ADD INDEX sql_index (original_sql(1024),default_db(1024))", dbterror.ErrDupKeyName) } -func upgradeToVer30(s Session, ver int64) { +func upgradeToVer30(s sessiontypes.Session, ver int64) { if ver >= version30 { return } mustExecute(s, CreateStatsTopNTable) } -func upgradeToVer31(s Session, ver int64) { +func upgradeToVer31(s sessiontypes.Session, ver int64) { if ver >= version31 { return } doReentrantDDL(s, "ALTER TABLE mysql.stats_histograms ADD COLUMN `last_analyze_pos` BLOB DEFAULT NULL", infoschema.ErrColumnExists) } -func upgradeToVer32(s Session, ver int64) { +func upgradeToVer32(s sessiontypes.Session, ver int64) { if ver >= version32 { return } doReentrantDDL(s, "ALTER TABLE mysql.tables_priv MODIFY table_priv SET('Select','Insert','Update','Delete','Create','Drop','Grant', 'Index', 'Alter', 'Create View', 'Show View', 'Trigger', 'References')") } -func upgradeToVer33(s Session, ver int64) { +func upgradeToVer33(s sessiontypes.Session, ver int64) { if ver >= version33 { return } doReentrantDDL(s, CreateExprPushdownBlacklist) } -func upgradeToVer34(s Session, ver int64) { +func upgradeToVer34(s sessiontypes.Session, ver int64) { if ver >= version34 { return } doReentrantDDL(s, CreateOptRuleBlacklist) } -func upgradeToVer35(s Session, ver int64) { +func upgradeToVer35(s sessiontypes.Session, ver int64) { if ver >= version35 { return } @@ -1724,7 +1725,7 @@ func upgradeToVer35(s Session, ver int64) { mustExecute(s, sql) } -func upgradeToVer36(s Session, ver int64) { +func upgradeToVer36(s sessiontypes.Session, ver int64) { if ver >= version36 { return } @@ -1734,7 +1735,7 @@ func upgradeToVer36(s Session, ver int64) { mustExecute(s, "UPDATE HIGH_PRIORITY mysql.user SET Create_tmp_table_priv='Y',Lock_tables_priv='Y',Create_routine_priv='Y',Alter_routine_priv='Y',Event_priv='Y' WHERE Super_priv='Y'") } -func upgradeToVer37(s Session, ver int64) { +func upgradeToVer37(s sessiontypes.Session, ver int64) { if ver >= version37 { return } @@ -1744,14 +1745,14 @@ func upgradeToVer37(s Session, ver int64) { mustExecute(s, sql) } -func upgradeToVer38(s Session, ver int64) { +func upgradeToVer38(s sessiontypes.Session, ver int64) { if ver >= version38 { return } doReentrantDDL(s, CreateGlobalPrivTable) } -func writeNewCollationParameter(s Session, flag bool) { +func writeNewCollationParameter(s sessiontypes.Session, flag bool) { comment := "If the new collations are enabled. Do not edit it." b := varFalse if flag { @@ -1762,7 +1763,7 @@ func writeNewCollationParameter(s Session, flag bool) { ) } -func upgradeToVer40(s Session, ver int64) { +func upgradeToVer40(s sessiontypes.Session, ver int64) { if ver >= version40 { return } @@ -1770,7 +1771,7 @@ func upgradeToVer40(s Session, ver int64) { writeNewCollationParameter(s, false) } -func upgradeToVer41(s Session, ver int64) { +func upgradeToVer41(s sessiontypes.Session, ver int64) { if ver >= version41 { return } @@ -1779,12 +1780,12 @@ func upgradeToVer41(s Session, ver int64) { } // writeDefaultExprPushDownBlacklist writes default expr pushdown blacklist into mysql.expr_pushdown_blacklist -func writeDefaultExprPushDownBlacklist(s Session) { +func writeDefaultExprPushDownBlacklist(s sessiontypes.Session) { mustExecute(s, "INSERT HIGH_PRIORITY INTO mysql.expr_pushdown_blacklist VALUES"+ "('date_add','tiflash', 'DST(daylight saving time) does not take effect in TiFlash date_add')") } -func upgradeToVer42(s Session, ver int64) { +func upgradeToVer42(s sessiontypes.Session, ver int64) { if ver >= version42 { return } @@ -1794,7 +1795,7 @@ func upgradeToVer42(s Session, ver int64) { } // Convert statement summary global variables to non-empty values. -func writeStmtSummaryVars(s Session) { +func writeStmtSummaryVars(s sessiontypes.Session) { sql := "UPDATE %n.%n SET variable_value= %? WHERE variable_name= %? AND variable_value=''" mustExecute(s, sql, mysql.SystemDB, mysql.GlobalVariablesTable, variable.BoolToOnOff(variable.DefTiDBEnableStmtSummary), variable.TiDBEnableStmtSummary) mustExecute(s, sql, mysql.SystemDB, mysql.GlobalVariablesTable, variable.BoolToOnOff(variable.DefTiDBStmtSummaryInternalQuery), variable.TiDBStmtSummaryInternalQuery) @@ -1804,21 +1805,21 @@ func writeStmtSummaryVars(s Session) { mustExecute(s, sql, mysql.SystemDB, mysql.GlobalVariablesTable, strconv.FormatUint(uint64(variable.DefTiDBStmtSummaryMaxSQLLength), 10), variable.TiDBStmtSummaryMaxSQLLength) } -func upgradeToVer43(s Session, ver int64) { +func upgradeToVer43(s sessiontypes.Session, ver int64) { if ver >= version43 { return } writeStmtSummaryVars(s) } -func upgradeToVer44(s Session, ver int64) { +func upgradeToVer44(s sessiontypes.Session, ver int64) { if ver >= version44 { return } mustExecute(s, "DELETE FROM mysql.global_variables where variable_name = \"tidb_isolation_read_engines\"") } -func upgradeToVer45(s Session, ver int64) { +func upgradeToVer45(s sessiontypes.Session, ver int64) { if ver >= version45 { return } @@ -1828,7 +1829,7 @@ func upgradeToVer45(s Session, ver int64) { // In v3.1.1, we wrongly replace the context of upgradeToVer39 with upgradeToVer44. If we upgrade from v3.1.1 to a newer version, // upgradeToVer39 will be missed. So we redo upgradeToVer39 here to make sure the upgrading from v3.1.1 succeed. -func upgradeToVer46(s Session, ver int64) { +func upgradeToVer46(s sessiontypes.Session, ver int64) { if ver >= version46 { return } @@ -1838,28 +1839,28 @@ func upgradeToVer46(s Session, ver int64) { mustExecute(s, "UPDATE HIGH_PRIORITY mysql.user SET File_priv='Y' WHERE Super_priv='Y'") } -func upgradeToVer47(s Session, ver int64) { +func upgradeToVer47(s sessiontypes.Session, ver int64) { if ver >= version47 { return } doReentrantDDL(s, "ALTER TABLE mysql.bind_info ADD COLUMN `source` varchar(10) NOT NULL default 'unknown'", infoschema.ErrColumnExists) } -func upgradeToVer50(s Session, ver int64) { +func upgradeToVer50(s sessiontypes.Session, ver int64) { if ver >= version50 { return } doReentrantDDL(s, CreateSchemaIndexUsageTable) } -func upgradeToVer52(s Session, ver int64) { +func upgradeToVer52(s sessiontypes.Session, ver int64) { if ver >= version52 { return } doReentrantDDL(s, "ALTER TABLE mysql.stats_histograms MODIFY cm_sketch BLOB(6291456)") } -func upgradeToVer53(s Session, ver int64) { +func upgradeToVer53(s sessiontypes.Session, ver int64) { if ver >= version53 { return } @@ -1869,7 +1870,7 @@ func upgradeToVer53(s Session, ver int64) { mustExecute(s, sql) } -func upgradeToVer54(s Session, ver int64) { +func upgradeToVer54(s sessiontypes.Session, ver int64) { if ver >= version54 { return } @@ -1894,7 +1895,7 @@ func upgradeToVer54(s Session, ver int64) { // When cherry-pick upgradeToVer52 to v4.0, we wrongly name it upgradeToVer48. // If we upgrade from v4.0 to a newer version, the real upgradeToVer48 will be missed. // So we redo upgradeToVer48 here to make sure the upgrading from v4.0 succeeds. -func upgradeToVer55(s Session, ver int64) { +func upgradeToVer55(s sessiontypes.Session, ver int64) { if ver >= version55 { return } @@ -1944,33 +1945,33 @@ func upgradeToVer55(s Session, ver int64) { // When cherry-pick upgradeToVer54 to v4.0, we wrongly name it upgradeToVer49. // If we upgrade from v4.0 to a newer version, the real upgradeToVer49 will be missed. // So we redo upgradeToVer49 here to make sure the upgrading from v4.0 succeeds. -func upgradeToVer56(s Session, ver int64) { +func upgradeToVer56(s sessiontypes.Session, ver int64) { if ver >= version56 { return } doReentrantDDL(s, CreateStatsExtended) } -func upgradeToVer57(s Session, ver int64) { +func upgradeToVer57(s sessiontypes.Session, ver int64) { if ver >= version57 { return } insertBuiltinBindInfoRow(s) } -func initBindInfoTable(s Session) { +func initBindInfoTable(s sessiontypes.Session) { mustExecute(s, CreateBindInfoTable) insertBuiltinBindInfoRow(s) } -func insertBuiltinBindInfoRow(s Session) { +func insertBuiltinBindInfoRow(s sessiontypes.Session) { mustExecute(s, `INSERT HIGH_PRIORITY INTO mysql.bind_info(original_sql, bind_sql, default_db, status, create_time, update_time, charset, collation, source) VALUES (%?, %?, "mysql", %?, "0000-00-00 00:00:00", "0000-00-00 00:00:00", "", "", %?)`, bindinfo.BuiltinPseudoSQL4BindLock, bindinfo.BuiltinPseudoSQL4BindLock, bindinfo.Builtin, bindinfo.Builtin, ) } -func upgradeToVer59(s Session, ver int64) { +func upgradeToVer59(s sessiontypes.Session, ver int64) { if ver >= version59 { return } @@ -1985,7 +1986,7 @@ func upgradeToVer59(s Session, ver int64) { writeOOMAction(s) } -func upgradeToVer60(s Session, ver int64) { +func upgradeToVer60(s sessiontypes.Session, ver int64) { if ver >= version60 { return } @@ -2002,7 +2003,7 @@ type bindInfo struct { source string } -func upgradeToVer67(s Session, ver int64) { +func upgradeToVer67(s sessiontypes.Session, ver int64) { if ver >= version67 { return } @@ -2098,21 +2099,21 @@ func updateBindInfo(iter *chunk.Iterator4Chunk, p *parser.Parser, bindMap map[st } } -func writeMemoryQuotaQuery(s Session) { +func writeMemoryQuotaQuery(s sessiontypes.Session) { comment := "memory_quota_query is 32GB by default in v3.0.x, 1GB by default in v4.0.x+" mustExecute(s, `INSERT HIGH_PRIORITY INTO %n.%n VALUES (%?, %?, %?) ON DUPLICATE KEY UPDATE VARIABLE_VALUE=%?`, mysql.SystemDB, mysql.TiDBTable, tidbDefMemoryQuotaQuery, 32<<30, comment, 32<<30, ) } -func upgradeToVer62(s Session, ver int64) { +func upgradeToVer62(s sessiontypes.Session, ver int64) { if ver >= version62 { return } doReentrantDDL(s, "ALTER TABLE mysql.stats_buckets ADD COLUMN `ndv` bigint not null default 0", infoschema.ErrColumnExists) } -func upgradeToVer63(s Session, ver int64) { +func upgradeToVer63(s sessiontypes.Session, ver int64) { if ver >= version63 { return } @@ -2120,7 +2121,7 @@ func upgradeToVer63(s Session, ver int64) { mustExecute(s, "UPDATE HIGH_PRIORITY mysql.user SET Create_tablespace_priv='Y' where Super_priv='Y'") } -func upgradeToVer64(s Session, ver int64) { +func upgradeToVer64(s sessiontypes.Session, ver int64) { if ver >= version64 { return } @@ -2129,35 +2130,35 @@ func upgradeToVer64(s Session, ver int64) { mustExecute(s, "UPDATE HIGH_PRIORITY mysql.user SET Repl_slave_priv='Y',Repl_client_priv='Y' where Super_priv='Y'") } -func upgradeToVer65(s Session, ver int64) { +func upgradeToVer65(s sessiontypes.Session, ver int64) { if ver >= version65 { return } doReentrantDDL(s, CreateStatsFMSketchTable) } -func upgradeToVer66(s Session, ver int64) { +func upgradeToVer66(s sessiontypes.Session, ver int64) { if ver >= version66 { return } mustExecute(s, "set @@global.tidb_track_aggregate_memory_usage = 1") } -func upgradeToVer68(s Session, ver int64) { +func upgradeToVer68(s sessiontypes.Session, ver int64) { if ver >= version68 { return } mustExecute(s, "DELETE FROM mysql.global_variables where VARIABLE_NAME = 'tidb_enable_clustered_index' and VARIABLE_VALUE = 'OFF'") } -func upgradeToVer69(s Session, ver int64) { +func upgradeToVer69(s sessiontypes.Session, ver int64) { if ver >= version69 { return } doReentrantDDL(s, CreateGlobalGrantsTable) } -func upgradeToVer70(s Session, ver int64) { +func upgradeToVer70(s sessiontypes.Session, ver int64) { if ver >= version70 { return } @@ -2165,28 +2166,28 @@ func upgradeToVer70(s Session, ver int64) { mustExecute(s, "UPDATE HIGH_PRIORITY mysql.user SET plugin='mysql_native_password'") } -func upgradeToVer71(s Session, ver int64) { +func upgradeToVer71(s sessiontypes.Session, ver int64) { if ver >= version71 { return } mustExecute(s, "UPDATE mysql.global_variables SET VARIABLE_VALUE='OFF' WHERE VARIABLE_NAME = 'tidb_multi_statement_mode' AND VARIABLE_VALUE = 'WARN'") } -func upgradeToVer72(s Session, ver int64) { +func upgradeToVer72(s sessiontypes.Session, ver int64) { if ver >= version72 { return } doReentrantDDL(s, "ALTER TABLE mysql.stats_meta ADD COLUMN snapshot BIGINT(64) UNSIGNED NOT NULL DEFAULT 0", infoschema.ErrColumnExists) } -func upgradeToVer73(s Session, ver int64) { +func upgradeToVer73(s sessiontypes.Session, ver int64) { if ver >= version73 { return } doReentrantDDL(s, CreateCapturePlanBaselinesBlacklist) } -func upgradeToVer74(s Session, ver int64) { +func upgradeToVer74(s sessiontypes.Session, ver int64) { if ver >= version74 { return } @@ -2194,7 +2195,7 @@ func upgradeToVer74(s Session, ver int64) { mustExecute(s, fmt.Sprintf("UPDATE mysql.global_variables SET VARIABLE_VALUE='%[1]v' WHERE VARIABLE_NAME = 'tidb_stmt_summary_max_stmt_count' AND CAST(VARIABLE_VALUE AS SIGNED) = 200", variable.DefTiDBStmtSummaryMaxStmtCount)) } -func upgradeToVer75(s Session, ver int64) { +func upgradeToVer75(s sessiontypes.Session, ver int64) { if ver >= version75 { return } @@ -2205,21 +2206,21 @@ func upgradeToVer75(s Session, ver int64) { doReentrantDDL(s, "ALTER TABLE mysql.columns_priv MODIFY COLUMN Host CHAR(255)") } -func upgradeToVer76(s Session, ver int64) { +func upgradeToVer76(s sessiontypes.Session, ver int64) { if ver >= version76 { return } doReentrantDDL(s, "ALTER TABLE mysql.columns_priv MODIFY COLUMN Column_priv SET('Select','Insert','Update','References')") } -func upgradeToVer77(s Session, ver int64) { +func upgradeToVer77(s sessiontypes.Session, ver int64) { if ver >= version77 { return } doReentrantDDL(s, CreateColumnStatsUsageTable) } -func upgradeToVer78(s Session, ver int64) { +func upgradeToVer78(s sessiontypes.Session, ver int64) { if ver >= version78 { return } @@ -2228,14 +2229,14 @@ func upgradeToVer78(s Session, ver int64) { doReentrantDDL(s, "ALTER TABLE mysql.stats_histograms MODIFY last_analyze_pos LONGBLOB DEFAULT NULL") } -func upgradeToVer79(s Session, ver int64) { +func upgradeToVer79(s sessiontypes.Session, ver int64) { if ver >= version79 { return } doReentrantDDL(s, CreateTableCacheMetaTable) } -func upgradeToVer80(s Session, ver int64) { +func upgradeToVer80(s sessiontypes.Session, ver int64) { if ver >= version80 { return } @@ -2258,7 +2259,7 @@ func upgradeToVer80(s Session, ver int64) { // For users that upgrade TiDB from a pre-4.0 version, we want to disable index merge by default. // This helps minimize query plan regressions. -func upgradeToVer81(s Session, ver int64) { +func upgradeToVer81(s sessiontypes.Session, ver int64) { if ver >= version81 { return } @@ -2279,49 +2280,49 @@ func upgradeToVer81(s Session, ver int64) { mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableIndexMerge, variable.Off) } -func upgradeToVer82(s Session, ver int64) { +func upgradeToVer82(s sessiontypes.Session, ver int64) { if ver >= version82 { return } doReentrantDDL(s, CreateAnalyzeOptionsTable) } -func upgradeToVer83(s Session, ver int64) { +func upgradeToVer83(s sessiontypes.Session, ver int64) { if ver >= version83 { return } doReentrantDDL(s, CreateStatsHistory) } -func upgradeToVer84(s Session, ver int64) { +func upgradeToVer84(s sessiontypes.Session, ver int64) { if ver >= version84 { return } doReentrantDDL(s, CreateStatsMetaHistory) } -func upgradeToVer85(s Session, ver int64) { +func upgradeToVer85(s sessiontypes.Session, ver int64) { if ver >= version85 { return } mustExecute(s, fmt.Sprintf("UPDATE HIGH_PRIORITY mysql.bind_info SET status= '%s' WHERE status = '%s'", bindinfo.Enabled, bindinfo.Using)) } -func upgradeToVer86(s Session, ver int64) { +func upgradeToVer86(s sessiontypes.Session, ver int64) { if ver >= version86 { return } doReentrantDDL(s, "ALTER TABLE mysql.tables_priv MODIFY COLUMN Column_priv SET('Select','Insert','Update','References')") } -func upgradeToVer87(s Session, ver int64) { +func upgradeToVer87(s sessiontypes.Session, ver int64) { if ver >= version87 { return } doReentrantDDL(s, CreateAnalyzeJobs) } -func upgradeToVer88(s Session, ver int64) { +func upgradeToVer88(s sessiontypes.Session, ver int64) { if ver >= version88 { return } @@ -2329,7 +2330,7 @@ func upgradeToVer88(s Session, ver int64) { doReentrantDDL(s, "ALTER TABLE mysql.user CHANGE `Repl_client_priv` `Repl_client_priv` ENUM('N','Y') NOT NULL DEFAULT 'N' AFTER `Repl_slave_priv`") } -func upgradeToVer89(s Session, ver int64) { +func upgradeToVer89(s sessiontypes.Session, ver int64) { if ver >= version89 { return } @@ -2342,7 +2343,7 @@ func upgradeToVer89(s Session, ver int64) { // (not guaranteed to be the same on all servers), and writes a message // to the error log. The message is important since the behavior is weird // (changes to the config file will no longer take effect past this point). -func importConfigOption(s Session, configName, svName, valStr string) { +func importConfigOption(s sessiontypes.Session, configName, svName, valStr string) { message := fmt.Sprintf("%s is now configured by the system variable %s. One-time importing the value specified in tidb.toml file", configName, svName) logutil.BgLogger().Warn(message, zap.String("value", valStr)) // We use insert ignore, since if its a duplicate we don't want to overwrite any user-set values. @@ -2351,7 +2352,7 @@ func importConfigOption(s Session, configName, svName, valStr string) { mustExecute(s, sql) } -func upgradeToVer90(s Session, ver int64) { +func upgradeToVer90(s sessiontypes.Session, ver int64) { if ver >= version90 { return } @@ -2369,7 +2370,7 @@ func upgradeToVer90(s Session, ver int64) { importConfigOption(s, "oom-action", variable.TiDBMemOOMAction, valStr) } -func upgradeToVer91(s Session, ver int64) { +func upgradeToVer91(s sessiontypes.Session, ver int64) { if ver >= version91 { return } @@ -2383,7 +2384,7 @@ func upgradeToVer91(s Session, ver int64) { importConfigOption(s, "prepared-plan-cache.memory-guard-ratio", variable.TiDBPrepPlanCacheMemoryGuardRatio, valStr) } -func upgradeToVer93(s Session, ver int64) { +func upgradeToVer93(s sessiontypes.Session, ver int64) { if ver >= version93 { return } @@ -2391,21 +2392,21 @@ func upgradeToVer93(s Session, ver int64) { importConfigOption(s, "oom-use-tmp-storage", variable.TiDBEnableTmpStorageOnOOM, valStr) } -func upgradeToVer94(s Session, ver int64) { +func upgradeToVer94(s sessiontypes.Session, ver int64) { if ver >= version94 { return } mustExecute(s, CreateMDLView) } -func upgradeToVer95(s Session, ver int64) { +func upgradeToVer95(s sessiontypes.Session, ver int64) { if ver >= version95 { return } doReentrantDDL(s, "ALTER TABLE mysql.user ADD COLUMN IF NOT EXISTS `User_attributes` JSON") } -func upgradeToVer97(s Session, ver int64) { +func upgradeToVer97(s sessiontypes.Session, ver int64) { if ver >= version97 { return } @@ -2426,19 +2427,19 @@ func upgradeToVer97(s Session, ver int64) { mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBOptRangeMaxSize, 0) } -func upgradeToVer98(s Session, ver int64) { +func upgradeToVer98(s sessiontypes.Session, ver int64) { if ver >= version98 { return } doReentrantDDL(s, "ALTER TABLE mysql.user ADD COLUMN IF NOT EXISTS `Token_issuer` varchar(255)") } -func upgradeToVer99Before(s Session) { +func upgradeToVer99Before(s sessiontypes.Session) { mustExecute(s, "INSERT HIGH_PRIORITY IGNORE INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableMDL, 0) } -func upgradeToVer99After(s Session) { +func upgradeToVer99After(s sessiontypes.Session) { sql := fmt.Sprintf("UPDATE HIGH_PRIORITY %[1]s.%[2]s SET VARIABLE_VALUE = %[4]d WHERE VARIABLE_NAME = '%[3]s'", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableMDL, 1) mustExecute(s, sql) @@ -2449,7 +2450,7 @@ func upgradeToVer99After(s Session) { terror.MustNil(err) } -func upgradeToVer100(s Session, ver int64) { +func upgradeToVer100(s sessiontypes.Session, ver int64) { if ver >= version100 { return } @@ -2457,28 +2458,28 @@ func upgradeToVer100(s Session, ver int64) { importConfigOption(s, "performance.server-memory-quota", variable.TiDBServerMemoryLimit, valStr) } -func upgradeToVer101(s Session, ver int64) { +func upgradeToVer101(s sessiontypes.Session, ver int64) { if ver >= version101 { return } doReentrantDDL(s, CreatePlanReplayerStatusTable) } -func upgradeToVer102(s Session, ver int64) { +func upgradeToVer102(s sessiontypes.Session, ver int64) { if ver >= version102 { return } doReentrantDDL(s, CreatePlanReplayerTaskTable) } -func upgradeToVer103(s Session, ver int64) { +func upgradeToVer103(s sessiontypes.Session, ver int64) { if ver >= version103 { return } doReentrantDDL(s, CreateStatsTableLocked) } -func upgradeToVer104(s Session, ver int64) { +func upgradeToVer104(s sessiontypes.Session, ver int64) { if ver >= version104 { return } @@ -2488,7 +2489,7 @@ func upgradeToVer104(s Session, ver int64) { } // For users that upgrade TiDB from a pre-6.0 version, we want to disable tidb cost model2 by default to keep plans unchanged. -func upgradeToVer105(s Session, ver int64) { +func upgradeToVer105(s sessiontypes.Session, ver int64) { if ver >= version105 { return } @@ -2507,7 +2508,7 @@ func upgradeToVer105(s Session, ver int64) { mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBCostModelVersion, "1") } -func upgradeToVer106(s Session, ver int64) { +func upgradeToVer106(s sessiontypes.Session, ver int64) { if ver >= version106 { return } @@ -2516,7 +2517,7 @@ func upgradeToVer106(s Session, ver int64) { doReentrantDDL(s, "Alter table mysql.user add COLUMN IF NOT EXISTS `Password_reuse_time` smallint unsigned DEFAULT NULL AFTER `Password_reuse_history`") } -func upgradeToVer107(s Session, ver int64) { +func upgradeToVer107(s sessiontypes.Session, ver int64) { if ver >= version107 { return } @@ -2525,7 +2526,7 @@ func upgradeToVer107(s Session, ver int64) { doReentrantDDL(s, "ALTER TABLE mysql.user ADD COLUMN IF NOT EXISTS `Password_lifetime` SMALLINT UNSIGNED DEFAULT NULL") } -func upgradeToVer108(s Session, ver int64) { +func upgradeToVer108(s sessiontypes.Session, ver int64) { if ver >= version108 { return } @@ -2533,7 +2534,7 @@ func upgradeToVer108(s Session, ver int64) { } // For users that upgrade TiDB from a 6.2-6.4 version, we want to disable tidb gc_aware_memory_track by default. -func upgradeToVer109(s Session, ver int64) { +func upgradeToVer109(s sessiontypes.Session, ver int64) { if ver >= version109 { return } @@ -2542,7 +2543,7 @@ func upgradeToVer109(s Session, ver int64) { } // For users that upgrade TiDB from a 5.4-6.4 version, we want to enable tidb tidb_stats_load_pseudo_timeout by default. -func upgradeToVer110(s Session, ver int64) { +func upgradeToVer110(s sessiontypes.Session, ver int64) { if ver >= version110 { return } @@ -2550,14 +2551,14 @@ func upgradeToVer110(s Session, ver int64) { mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBStatsLoadPseudoTimeout, 1) } -func upgradeToVer130(s Session, ver int64) { +func upgradeToVer130(s sessiontypes.Session, ver int64) { if ver >= version130 { return } doReentrantDDL(s, "ALTER TABLE mysql.stats_meta_history ADD COLUMN IF NOT EXISTS `source` varchar(40) NOT NULL after `version`;") } -func upgradeToVer131(s Session, ver int64) { +func upgradeToVer131(s sessiontypes.Session, ver int64) { if ver >= version131 { return } @@ -2565,14 +2566,14 @@ func upgradeToVer131(s Session, ver int64) { doReentrantDDL(s, CreateTTLJobHistory) } -func upgradeToVer132(s Session, ver int64) { +func upgradeToVer132(s sessiontypes.Session, ver int64) { if ver >= version132 { return } doReentrantDDL(s, CreateMDLView) } -func upgradeToVer133(s Session, ver int64) { +func upgradeToVer133(s sessiontypes.Session, ver int64) { if ver >= version133 { return } @@ -2580,7 +2581,7 @@ func upgradeToVer133(s Session, ver int64) { mysql.SystemDB, mysql.GlobalVariablesTable, variable.DefTiDBServerMemoryLimit, variable.TiDBServerMemoryLimit, "0") } -func upgradeToVer134(s Session, ver int64) { +func upgradeToVer134(s sessiontypes.Session, ver int64) { if ver >= version134 { return } @@ -2592,7 +2593,7 @@ func upgradeToVer134(s Session, ver int64) { } // For users that upgrade TiDB from a pre-7.0 version, we want to set tidb_opt_advanced_join_hint to off by default to keep plans unchanged. -func upgradeToVer135(s Session, ver int64) { +func upgradeToVer135(s sessiontypes.Session, ver int64) { if ver >= version135 { return } @@ -2612,7 +2613,7 @@ func upgradeToVer135(s Session, ver int64) { mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBOptAdvancedJoinHint, false) } -func upgradeToVer136(s Session, ver int64) { +func upgradeToVer136(s sessiontypes.Session, ver int64) { if ver >= version136 { return } @@ -2621,26 +2622,26 @@ func upgradeToVer136(s Session, ver int64) { doReentrantDDL(s, "ALTER TABLE mysql.tidb_background_subtask ADD INDEX idx_task_key(task_key)", dbterror.ErrDupKeyName) } -func upgradeToVer137(_ Session, _ int64) { +func upgradeToVer137(_ sessiontypes.Session, _ int64) { // NOOP, we don't depend on ddl to init the default group due to backward compatible issue. } // For users that upgrade TiDB from a version below 7.0, we want to enable tidb tidb_enable_null_aware_anti_join by default. -func upgradeToVer138(s Session, ver int64) { +func upgradeToVer138(s sessiontypes.Session, ver int64) { if ver >= version138 { return } mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBOptimizerEnableNAAJ, variable.On) } -func upgradeToVer139(s Session, ver int64) { +func upgradeToVer139(s sessiontypes.Session, ver int64) { if ver >= version139 { return } mustExecute(s, CreateLoadDataJobs) } -func upgradeToVer140(s Session, ver int64) { +func upgradeToVer140(s sessiontypes.Session, ver int64) { if ver >= version140 { return } @@ -2650,7 +2651,7 @@ func upgradeToVer140(s Session, ver int64) { // upgradeToVer141 sets the value of `tidb_session_plan_cache_size` as `tidb_prepared_plan_cache_size` for compatibility, // and update tidb_load_based_replica_read_threshold from 0 to 4. -func upgradeToVer141(s Session, ver int64) { +func upgradeToVer141(s sessiontypes.Session, ver int64) { if ver >= version141 { return } @@ -2674,7 +2675,7 @@ func upgradeToVer141(s Session, ver int64) { mustExecute(s, "REPLACE HIGH_PRIORITY INTO %n.%n VALUES (%?, %?);", mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBLoadBasedReplicaReadThreshold, variable.DefTiDBLoadBasedReplicaReadThreshold.String()) } -func upgradeToVer142(s Session, ver int64) { +func upgradeToVer142(s sessiontypes.Session, ver int64) { if ver >= version142 { return } @@ -2693,7 +2694,7 @@ func upgradeToVer142(s Session, ver int64) { mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBEnableNonPreparedPlanCache, variable.Off) } -func upgradeToVer143(s Session, ver int64) { +func upgradeToVer143(s sessiontypes.Session, ver int64) { if ver >= version143 { return } @@ -2701,7 +2702,7 @@ func upgradeToVer143(s Session, ver int64) { doReentrantDDL(s, "ALTER TABLE mysql.tidb_background_subtask ADD COLUMN `error` BLOB", infoschema.ErrColumnExists) } -func upgradeToVer144(s Session, ver int64) { +func upgradeToVer144(s sessiontypes.Session, ver int64) { if ver >= version144 { return } @@ -2720,7 +2721,7 @@ func upgradeToVer144(s Session, ver int64) { mysql.SystemDB, mysql.GlobalVariablesTable, variable.TiDBPlanCacheInvalidationOnFreshStats, variable.Off) } -func upgradeToVer146(s Session, ver int64) { +func upgradeToVer146(s sessiontypes.Session, ver int64) { if ver >= version146 { return } @@ -2728,42 +2729,42 @@ func upgradeToVer146(s Session, ver int64) { doReentrantDDL(s, "ALTER TABLE mysql.stats_history ADD INDEX idx_create_time (create_time)", dbterror.ErrDupKeyName) } -func upgradeToVer167(s Session, ver int64) { +func upgradeToVer167(s sessiontypes.Session, ver int64) { if ver >= version167 { return } doReentrantDDL(s, "ALTER TABLE mysql.tidb_background_subtask ADD COLUMN `step` INT AFTER `id`", infoschema.ErrColumnExists) } -func upgradeToVer168(s Session, ver int64) { +func upgradeToVer168(s sessiontypes.Session, ver int64) { if ver >= version168 { return } mustExecute(s, CreateImportJobs) } -func upgradeToVer169(s Session, ver int64) { +func upgradeToVer169(s sessiontypes.Session, ver int64) { if ver >= version169 { return } mustExecute(s, CreateRunawayTable) } -func upgradeToVer170(s Session, ver int64) { +func upgradeToVer170(s sessiontypes.Session, ver int64) { if ver >= version170 { return } mustExecute(s, CreateTimers) } -func upgradeToVer171(s Session, ver int64) { +func upgradeToVer171(s sessiontypes.Session, ver int64) { if ver >= version171 { return } mustExecute(s, "ALTER TABLE mysql.tidb_runaway_queries CHANGE COLUMN `tidb_server` `tidb_server` varchar(512)") } -func upgradeToVer172(s Session, ver int64) { +func upgradeToVer172(s sessiontypes.Session, ver int64) { if ver >= version172 { return } @@ -2772,14 +2773,14 @@ func upgradeToVer172(s Session, ver int64) { mustExecute(s, CreateDoneRunawayWatchTable) } -func upgradeToVer173(s Session, ver int64) { +func upgradeToVer173(s sessiontypes.Session, ver int64) { if ver >= version173 { return } doReentrantDDL(s, "ALTER TABLE mysql.tidb_background_subtask ADD COLUMN `summary` JSON", infoschema.ErrColumnExists) } -func upgradeToVer174(s Session, ver int64) { +func upgradeToVer174(s sessiontypes.Session, ver int64) { if ver >= version174 { return } @@ -2794,7 +2795,7 @@ func upgradeToVer174(s Session, ver int64) { // the issue #44298 that bindings for `in (?)` can't work for `in (?, ?, ?)`. // After this update, multiple bindings may have the same `original_sql`, but it's OK, and // for safety, don't remove duplicated bindings when upgrading. -func upgradeToVer175(s Session, ver int64) { +func upgradeToVer175(s sessiontypes.Session, ver int64) { if ver >= version175 { return } @@ -2840,14 +2841,14 @@ func upgradeToVer175(s Session, ver int64) { } } -func upgradeToVer176(s Session, ver int64) { +func upgradeToVer176(s sessiontypes.Session, ver int64) { if ver >= version176 { return } mustExecute(s, CreateGlobalTaskHistory) } -func upgradeToVer177(s Session, ver int64) { +func upgradeToVer177(s sessiontypes.Session, ver int64) { if ver >= version177 { return } @@ -2860,7 +2861,7 @@ func upgradeToVer177(s Session, ver int64) { } // writeDDLTableVersion writes mDDLTableVersion into mysql.tidb -func writeDDLTableVersion(s Session) { +func writeDDLTableVersion(s sessiontypes.Session) { var err error var ddlTableVersion meta.DDLTableVersion err = kv.RunInNewTxn(kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap), s.GetStore(), true, func(ctx context.Context, txn kv.Transaction) error { @@ -2878,21 +2879,21 @@ func writeDDLTableVersion(s Session) { ) } -func upgradeToVer178(s Session, ver int64) { +func upgradeToVer178(s sessiontypes.Session, ver int64) { if ver >= version178 { return } writeDDLTableVersion(s) } -func upgradeToVer179(s Session, ver int64) { +func upgradeToVer179(s sessiontypes.Session, ver int64) { if ver >= version179 { return } doReentrantDDL(s, "ALTER TABLE mysql.global_variables MODIFY COLUMN `VARIABLE_VALUE` varchar(16383)") } -func writeOOMAction(s Session) { +func writeOOMAction(s sessiontypes.Session) { comment := "oom-action is `log` by default in v3.0.x, `cancel` by default in v4.0.11+" mustExecute(s, `INSERT HIGH_PRIORITY INTO %n.%n VALUES (%?, %?, %?) ON DUPLICATE KEY UPDATE VARIABLE_VALUE= %?`, mysql.SystemDB, mysql.TiDBTable, tidbDefOOMAction, variable.OOMActionLog, comment, variable.OOMActionLog, @@ -2900,7 +2901,7 @@ func writeOOMAction(s Session) { } // updateBootstrapVer updates bootstrap version variable in mysql.TiDB table. -func updateBootstrapVer(s Session) { +func updateBootstrapVer(s sessiontypes.Session) { // Update bootstrap version. mustExecute(s, `INSERT HIGH_PRIORITY INTO %n.%n VALUES (%?, %?, "TiDB bootstrap version.") ON DUPLICATE KEY UPDATE VARIABLE_VALUE=%?`, mysql.SystemDB, mysql.TiDBTable, tidbServerVersionVar, currentBootstrapVersion, currentBootstrapVersion, @@ -2908,7 +2909,7 @@ func updateBootstrapVer(s Session) { } // getBootstrapVersion gets bootstrap version from mysql.tidb table; -func getBootstrapVersion(s Session) (int64, error) { +func getBootstrapVersion(s sessiontypes.Session) (int64, error) { sVal, isNull, err := getTiDBVar(s, tidbServerVersionVar) if err != nil { return 0, errors.Trace(err) @@ -2920,7 +2921,7 @@ func getBootstrapVersion(s Session) (int64, error) { } // doDDLWorks executes DDL statements in bootstrap stage. -func doDDLWorks(s Session) { +func doDDLWorks(s sessiontypes.Session) { // Create a test database. mustExecute(s, "CREATE DATABASE IF NOT EXISTS test") // Create system db. @@ -3025,7 +3026,7 @@ func doDDLWorks(s Session) { // doBootstrapSQLFile executes SQL commands in a file as the last stage of bootstrap. // It is useful for setting the initial value of GLOBAL variables. -func doBootstrapSQLFile(s Session) error { +func doBootstrapSQLFile(s sessiontypes.Session) error { sqlFile := config.GetGlobalConfig().InitializeSQLFile ctx := kv.WithInternalSourceType(context.Background(), kv.InternalTxnBootstrap) if sqlFile == "" { @@ -3064,7 +3065,7 @@ func doBootstrapSQLFile(s Session) error { // doDMLWorks executes DML statements in bootstrap stage. // All the statements run in a single transaction. -func doDMLWorks(s Session) { +func doDMLWorks(s sessiontypes.Session) { mustExecute(s, "BEGIN") if config.GetGlobalConfig().Security.SecureBootstrap { // If secure bootstrap is enabled, we create a root@localhost account which can login with auth_socket. @@ -3168,7 +3169,7 @@ func doDMLWorks(s Session) { } } -func mustExecute(s Session, sql string, args ...interface{}) { +func mustExecute(s sessiontypes.Session, sql string, args ...interface{}) { ctx, cancel := context.WithTimeout(context.Background(), time.Duration(internalSQLTimeout)*time.Second) ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnBootstrap) _, err := s.ExecuteInternal(ctx, sql, args...) diff --git a/pkg/session/bootstrap_test.go b/pkg/session/bootstrap_test.go index e23b15fca0326..61e87361f9c31 100644 --- a/pkg/session/bootstrap_test.go +++ b/pkg/session/bootstrap_test.go @@ -25,6 +25,7 @@ import ( "github.com/pingcap/tidb/pkg/domain" "github.com/pingcap/tidb/pkg/meta" "github.com/pingcap/tidb/pkg/parser/auth" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx" "github.com/pingcap/tidb/pkg/sessionctx/variable" "github.com/pingcap/tidb/pkg/statistics" @@ -1590,7 +1591,7 @@ func TestTiDBUpgradeToVer140(t *testing.T) { }() ver139 := version139 - resetTo139 := func(s Session) { + resetTo139 := func(s sessiontypes.Session) { txn, err := store.Begin() require.NoError(t, err) m := meta.NewMeta(txn) @@ -2004,7 +2005,7 @@ func TestTiDBBindingInListToVer175(t *testing.T) { MustExec(t, seV174, "insert into mysql.bind_info values ('select * from `test` . `t` where `a` in ( ? )', 'SELECT /*+ use_index(`t` `c`)*/ * FROM `test`.`t` WHERE `a` IN (1)', 'test', 'enabled', '2023-09-13 14:41:38.319', '2023-09-13 14:41:36.319', 'utf8', 'utf8_general_ci', 'manual', '', '')") MustExec(t, seV174, "insert into mysql.bind_info values ('select * from `test` . `t` where `a` in ( ? ) and `b` in ( ... )', 'SELECT /*+ use_index(`t` `c`)*/ * FROM `test`.`t` WHERE `a` IN (1) AND `b` IN (1,2,3)', 'test', 'enabled', '2023-09-13 14:41:37.319', '2023-09-13 14:41:38.319', 'utf8', 'utf8_general_ci', 'manual', '', '')") - showBindings := func(s Session) (records []string) { + showBindings := func(s sessiontypes.Session) (records []string) { MustExec(t, s, "admin reload bindings") res := MustExecToRecodeSet(t, s, "show global bindings") chk := res.NewChunk(nil) @@ -2043,7 +2044,7 @@ func TestTiDBBindingInListToVer175(t *testing.T) { require.Equal(t, []string{"SELECT /*+ use_index(`t` `c`)*/ * FROM `test`.`t` WHERE `a` IN (1) AND `b` IN (1,2,3):select * from `test` . `t` where `a` in ( ... ) and `b` in ( ... )", "SELECT /*+ use_index(`t` `c`)*/ * FROM `test`.`t` WHERE `a` IN (1):select * from `test` . `t` where `a` in ( ... )"}, bindings) - planFromBinding := func(s Session, q string) { + planFromBinding := func(s sessiontypes.Session, q string) { MustExec(t, s, q) res := MustExecToRecodeSet(t, s, "select @@last_plan_from_binding") chk := res.NewChunk(nil) diff --git a/pkg/session/bootstraptest/BUILD.bazel b/pkg/session/bootstraptest/BUILD.bazel index 67477675738cf..4b25d2e72d946 100644 --- a/pkg/session/bootstraptest/BUILD.bazel +++ b/pkg/session/bootstraptest/BUILD.bazel @@ -19,6 +19,7 @@ go_test( "//pkg/parser/terror", "//pkg/server/handler", "//pkg/session", #keep + "//pkg/session/types", "//pkg/sessionctx", "//pkg/testkit", #keep "//pkg/testkit/testmain", diff --git a/pkg/session/bootstraptest/bootstrap_upgrade_test.go b/pkg/session/bootstraptest/bootstrap_upgrade_test.go index adde5a6437ec9..fbe12509b94c1 100644 --- a/pkg/session/bootstraptest/bootstrap_upgrade_test.go +++ b/pkg/session/bootstraptest/bootstrap_upgrade_test.go @@ -33,6 +33,7 @@ import ( "github.com/pingcap/tidb/pkg/parser/terror" "github.com/pingcap/tidb/pkg/server/handler" "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx" "github.com/pingcap/tidb/pkg/testkit" tidb_util "github.com/pingcap/tidb/pkg/util" @@ -443,7 +444,7 @@ func TestUpgradeVersionForPausedJob(t *testing.T) { } // checkDDLJobExecSucc is used to make sure the DDL operation is successful. -func checkDDLJobExecSucc(t *testing.T, se session.Session, jobID int64) { +func checkDDLJobExecSucc(t *testing.T, se sessiontypes.Session, jobID int64) { sql := fmt.Sprintf(" admin show ddl jobs where job_id=%d", jobID) suc := false for i := 0; i < 20; i++ { @@ -686,7 +687,7 @@ func TestUpgradeWithPauseDDL(t *testing.T) { tc := session.TestCallback{Cnt: atomicutil.NewInt32(0)} sql := "select job_meta, processing from mysql.tidb_ddl_job where job_id in (select min(job_id) from mysql.tidb_ddl_job group by schema_ids, table_ids, processing) order by processing desc, job_id" - tc.OnBootstrapBeforeExported = func(s session.Session) { + tc.OnBootstrapBeforeExported = func(s sessiontypes.Session) { rows, err := execute(context.Background(), s, sql) require.NoError(t, err) require.Len(t, rows, 0) @@ -709,7 +710,7 @@ func TestUpgradeWithPauseDDL(t *testing.T) { }() <-ch } - checkDDLJobState := func(s session.Session) { + checkDDLJobState := func(s sessiontypes.Session) { rows, err := execute(context.Background(), s, sql) require.NoError(t, err) for _, row := range rows { @@ -727,7 +728,7 @@ func TestUpgradeWithPauseDDL(t *testing.T) { } } // Before every test bootstrap(DDL operation), we add a user and a system DB's DDL operations. - tc.OnBootstrapExported = func(s session.Session) { + tc.OnBootstrapExported = func(s sessiontypes.Session) { var query1, query2 string switch tc.Cnt.Load() % 2 { case 0: @@ -745,7 +746,7 @@ func TestUpgradeWithPauseDDL(t *testing.T) { checkDDLJobState(s) } - tc.OnBootstrapAfterExported = func(s session.Session) { + tc.OnBootstrapAfterExported = func(s sessiontypes.Session) { checkDDLJobState(s) } session.TestHook = tc diff --git a/pkg/session/mock_bootstrap.go b/pkg/session/mock_bootstrap.go index 94d633c13c0b5..9a200b6518a29 100644 --- a/pkg/session/mock_bootstrap.go +++ b/pkg/session/mock_bootstrap.go @@ -22,6 +22,7 @@ import ( "flag" "time" + "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/util/logutil" atomicutil "go.uber.org/atomic" "go.uber.org/zap" @@ -75,7 +76,7 @@ var allDDLs = []string{ var mockLatestVer = currentBootstrapVersion + 1 -func mockUpgradeToVerLatest(s Session, ver int64) { +func mockUpgradeToVerLatest(s types.Session, ver int64) { logutil.BgLogger().Info("mock upgrade to ver latest", zap.Int64("old ver", ver), zap.Int64("mock latest ver", mockLatestVer)) if ver >= mockLatestVer { return @@ -114,7 +115,7 @@ func mockUpgradeToVerLatest(s Session, ver int64) { } // mockSimpleUpgradeToVerLatest mocks a simple bootstrapVersion(make the test faster). -func mockSimpleUpgradeToVerLatest(s Session, ver int64) { +func mockSimpleUpgradeToVerLatest(s types.Session, ver int64) { logutil.BgLogger().Info("mock upgrade to ver latest", zap.Int64("old ver", ver), zap.Int64("mock latest ver", mockLatestVer)) if ver >= mockLatestVer { return @@ -151,7 +152,7 @@ const ( // MockUpgradeToVerLatestKind is used to indicate the use of different mock bootstrapVersion. var MockUpgradeToVerLatestKind = defaultMockUpgradeToVerLatest -func addMockBootstrapVersionForTest(s Session) { +func addMockBootstrapVersionForTest(s types.Session) { if WithMockUpgrade == nil || !*WithMockUpgrade { return } @@ -168,51 +169,51 @@ func addMockBootstrapVersionForTest(s Session) { // Callback is used for Test. type Callback interface { // OnBootstrapBefore is called before doing bootstrap. - OnBootstrapBefore(s Session) + OnBootstrapBefore(s types.Session) // OnBootstrap is called doing bootstrap. - OnBootstrap(s Session) + OnBootstrap(s types.Session) // OnBootstrapAfter is called after doing bootstrap. - OnBootstrapAfter(s Session) + OnBootstrapAfter(s types.Session) } // BaseCallback implements Callback interfaces. type BaseCallback struct{} // OnBootstrapBefore implements Callback interface. -func (*BaseCallback) OnBootstrapBefore(Session) {} +func (*BaseCallback) OnBootstrapBefore(types.Session) {} // OnBootstrap implements Callback interface. -func (*BaseCallback) OnBootstrap(Session) {} +func (*BaseCallback) OnBootstrap(types.Session) {} // OnBootstrapAfter implements Callback interface. -func (*BaseCallback) OnBootstrapAfter(Session) {} +func (*BaseCallback) OnBootstrapAfter(types.Session) {} // TestCallback is used to customize user callback themselves. type TestCallback struct { *BaseCallback Cnt *atomicutil.Int32 - OnBootstrapBeforeExported func(s Session) - OnBootstrapExported func(s Session) - OnBootstrapAfterExported func(s Session) + OnBootstrapBeforeExported func(s types.Session) + OnBootstrapExported func(s types.Session) + OnBootstrapAfterExported func(s types.Session) } // OnBootstrapBefore mocks the same behavior with the main bootstrap hook. -func (tc *TestCallback) OnBootstrapBefore(s Session) { +func (tc *TestCallback) OnBootstrapBefore(s types.Session) { if tc.OnBootstrapBeforeExported != nil { tc.OnBootstrapBeforeExported(s) } } // OnBootstrap mocks the same behavior with the main bootstrap hook. -func (tc *TestCallback) OnBootstrap(s Session) { +func (tc *TestCallback) OnBootstrap(s types.Session) { if tc.OnBootstrapExported != nil { tc.OnBootstrapExported(s) } } // OnBootstrapAfter mocks the same behavior with the main bootstrap hook. -func (tc *TestCallback) OnBootstrapAfter(s Session) { +func (tc *TestCallback) OnBootstrapAfter(s types.Session) { if tc.OnBootstrapAfterExported != nil { tc.OnBootstrapAfterExported(s) } diff --git a/pkg/session/nontransactional.go b/pkg/session/nontransactional.go index 197643c48ebb3..5738a3d545dbf 100644 --- a/pkg/session/nontransactional.go +++ b/pkg/session/nontransactional.go @@ -32,6 +32,7 @@ import ( "github.com/pingcap/tidb/pkg/parser/opcode" "github.com/pingcap/tidb/pkg/planner/core" session_metrics "github.com/pingcap/tidb/pkg/session/metrics" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx/variable" "github.com/pingcap/tidb/pkg/table" "github.com/pingcap/tidb/pkg/types" @@ -74,7 +75,7 @@ func (j job) String(redacted bool) string { } // HandleNonTransactionalDML is the entry point for a non-transactional DML statement -func HandleNonTransactionalDML(ctx context.Context, stmt *ast.NonTransactionalDMLStmt, se Session) (sqlexec.RecordSet, error) { +func HandleNonTransactionalDML(ctx context.Context, stmt *ast.NonTransactionalDMLStmt, se sessiontypes.Session) (sqlexec.RecordSet, error) { sessVars := se.GetSessionVars() originalReadStaleness := se.GetSessionVars().ReadStaleness // NT-DML is a write operation, and should not be affected by read_staleness that is supposed to affect only SELECT. @@ -129,7 +130,7 @@ func HandleNonTransactionalDML(ctx context.Context, stmt *ast.NonTransactionalDM // // Note: this is not a comprehensive check. // We do this to help user prevent some easy mistakes, at an acceptable maintenance cost. -func checkConstraintWithShardColumn(se Session, stmt *ast.NonTransactionalDMLStmt, +func checkConstraintWithShardColumn(se sessiontypes.Session, stmt *ast.NonTransactionalDMLStmt, tableName *ast.TableName, shardColumnInfo *model.ColumnInfo, tableSources []*ast.TableSource) error { switch s := stmt.DMLStmt.(type) { case *ast.UpdateStmt: @@ -148,7 +149,7 @@ func checkConstraintWithShardColumn(se Session, stmt *ast.NonTransactionalDMLStm } // shard column should not be updated. -func checkUpdateShardColumn(se Session, assignments []*ast.Assignment, shardColumnInfo *model.ColumnInfo, +func checkUpdateShardColumn(se sessiontypes.Session, assignments []*ast.Assignment, shardColumnInfo *model.ColumnInfo, tableName *ast.TableName, tableSources []*ast.TableSource, isUpdate bool) error { // if the table has alias, the alias is used in assignments, and we should use aliased name to compare aliasedShardColumnTableName := tableName.Name.L @@ -178,7 +179,7 @@ func checkUpdateShardColumn(se Session, assignments []*ast.Assignment, shardColu return nil } -func checkConstraint(stmt *ast.NonTransactionalDMLStmt, se Session) error { +func checkConstraint(stmt *ast.NonTransactionalDMLStmt, se sessiontypes.Session) error { sessVars := se.GetSessionVars() if !(sessVars.IsAutocommit() && !sessVars.InTxn()) { return errors.Errorf("non-transactional DML can only run in auto-commit mode. auto-commit:%v, inTxn:%v", @@ -256,7 +257,7 @@ func checkReadClauses(limit *ast.Limit, order *ast.OrderByClause) error { // single-threaded worker. work on the key range [start, end] func runJobs(ctx context.Context, jobs []job, stmt *ast.NonTransactionalDMLStmt, - tableName *ast.TableName, se Session, originalCondition ast.ExprNode) ([]string, error) { + tableName *ast.TableName, se sessiontypes.Session, originalCondition ast.ExprNode) ([]string, error) { // prepare for the construction of statement var shardColumnRefer *ast.ResultField var shardColumnType types.FieldType @@ -331,7 +332,7 @@ func runJobs(ctx context.Context, jobs []job, stmt *ast.NonTransactionalDMLStmt, return splitStmts, nil } -func doOneJob(ctx context.Context, job *job, totalJobCount int, options statementBuildInfo, se Session, dryRun bool) string { +func doOneJob(ctx context.Context, job *job, totalJobCount int, options statementBuildInfo, se sessiontypes.Session, dryRun bool) string { var whereCondition ast.ExprNode if job.start.IsNull() { @@ -441,7 +442,7 @@ func doOneJob(ctx context.Context, job *job, totalJobCount int, options statemen return "" } -func buildShardJobs(ctx context.Context, stmt *ast.NonTransactionalDMLStmt, se Session, +func buildShardJobs(ctx context.Context, stmt *ast.NonTransactionalDMLStmt, se sessiontypes.Session, selectSQL string, shardColumnInfo *model.ColumnInfo, memTracker *memory.Tracker) ([]job, error) { var shardColumnCollate string if shardColumnInfo != nil { @@ -536,7 +537,7 @@ func appendNewJob(jobs []job, id int, start types.Datum, end types.Datum, size i return jobs } -func buildSelectSQL(stmt *ast.NonTransactionalDMLStmt, se Session) ( +func buildSelectSQL(stmt *ast.NonTransactionalDMLStmt, se sessiontypes.Session) ( *ast.TableName, string, *model.ColumnInfo, []*ast.TableSource, error) { // only use the first table join, ok := stmt.DMLStmt.TableRefsJoin() @@ -582,7 +583,7 @@ func buildSelectSQL(stmt *ast.NonTransactionalDMLStmt, se Session) ( return tableName, selectSQL, shardColumnInfo, tableSources, nil } -func selectShardColumn(stmt *ast.NonTransactionalDMLStmt, se Session, tableSources []*ast.TableSource, +func selectShardColumn(stmt *ast.NonTransactionalDMLStmt, se sessiontypes.Session, tableSources []*ast.TableSource, leftMostTableName *ast.TableName, leftMostTableSource *ast.TableSource) ( *model.ColumnInfo, *ast.TableName, error) { var indexed bool diff --git a/pkg/session/session.go b/pkg/session/session.go index 152a6706b76da..5fcac90030187 100644 --- a/pkg/session/session.go +++ b/pkg/session/session.go @@ -71,6 +71,7 @@ import ( "github.com/pingcap/tidb/pkg/privilege/privileges" session_metrics "github.com/pingcap/tidb/pkg/session/metrics" "github.com/pingcap/tidb/pkg/session/txninfo" + "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx" "github.com/pingcap/tidb/pkg/sessionctx/binloginfo" "github.com/pingcap/tidb/pkg/sessionctx/sessionstates" @@ -115,76 +116,18 @@ import ( "go.uber.org/zap" ) -// Session context, it is consistent with the lifecycle of a client connection. -type Session interface { - sessionctx.Context - Status() uint16 // Flag of current status, such as autocommit. - LastInsertID() uint64 // LastInsertID is the last inserted auto_increment ID. - LastMessage() string // LastMessage is the info message that may be generated by last command - AffectedRows() uint64 // Affected rows by latest executed stmt. - // Execute is deprecated, and only used by plugins. Use ExecuteStmt() instead. - Execute(context.Context, string) ([]sqlexec.RecordSet, error) // Execute a sql statement. - // ExecuteStmt executes a parsed statement. - ExecuteStmt(context.Context, ast.StmtNode) (sqlexec.RecordSet, error) - // Parse is deprecated, use ParseWithParams() instead. - Parse(ctx context.Context, sql string) ([]ast.StmtNode, error) - // ExecuteInternal is a helper around ParseWithParams() and ExecuteStmt(). It is not allowed to execute multiple statements. - ExecuteInternal(context.Context, string, ...interface{}) (sqlexec.RecordSet, error) - String() string // String is used to debug. - CommitTxn(context.Context) error - RollbackTxn(context.Context) - // PrepareStmt executes prepare statement in binary protocol. - PrepareStmt(sql string) (stmtID uint32, paramCount int, fields []*ast.ResultField, err error) - // ExecutePreparedStmt executes a prepared statement. - // Deprecated: please use ExecuteStmt, this function is left for testing only. - // TODO: remove ExecutePreparedStmt. - ExecutePreparedStmt(ctx context.Context, stmtID uint32, param []expression.Expression) (sqlexec.RecordSet, error) - DropPreparedStmt(stmtID uint32) error - // SetSessionStatesHandler sets SessionStatesHandler for type stateType. - SetSessionStatesHandler(stateType sessionstates.SessionStateType, handler sessionctx.SessionStatesHandler) - SetClientCapability(uint32) // Set client capability flags. - SetConnectionID(uint64) - SetCommandValue(byte) - SetCompressionAlgorithm(int) - SetCompressionLevel(int) - SetProcessInfo(string, time.Time, byte, uint64) - SetTLSState(*tls.ConnectionState) - SetCollation(coID int) error - SetSessionManager(util.SessionManager) - Close() - Auth(user *auth.UserIdentity, auth, salt []byte, authConn conn.AuthConn) error - AuthWithoutVerification(user *auth.UserIdentity) bool - AuthPluginForUser(user *auth.UserIdentity) (string, error) - MatchIdentity(username, remoteHost string) (*auth.UserIdentity, error) - // Return the information of the txn current running - TxnInfo() *txninfo.TxnInfo - // PrepareTxnCtx is exported for test. - PrepareTxnCtx(context.Context) error - // FieldList returns fields list of a table. - FieldList(tableName string) (fields []*ast.ResultField, err error) - SetPort(port string) - - // set cur session operations allowed when tikv disk full happens. - SetDiskFullOpt(level kvrpcpb.DiskFullOpt) - GetDiskFullOpt() kvrpcpb.DiskFullOpt - ClearDiskFullOpt() - - // SetExtensions sets the `*extension.SessionExtensions` object - SetExtensions(extensions *extension.SessionExtensions) -} - func init() { executor.CreateSession = func(ctx sessionctx.Context) (sessionctx.Context, error) { return CreateSession(ctx.GetStore()) } executor.CloseSession = func(ctx sessionctx.Context) { - if se, ok := ctx.(Session); ok { + if se, ok := ctx.(types.Session); ok { se.Close() } } } -var _ Session = (*session)(nil) +var _ types.Session = (*session)(nil) type stmtRecord struct { st sqlexec.Statement @@ -1889,7 +1832,7 @@ func (s *session) DisableSandBoxMode() { } // ParseWithParams4Test wrapper (s *session) ParseWithParams for test -func ParseWithParams4Test(ctx context.Context, s Session, +func ParseWithParams4Test(ctx context.Context, s types.Session, sql string, args ...interface{}) (ast.StmtNode, error) { return s.(*session).ParseWithParams(ctx, sql, args) } @@ -1945,7 +1888,7 @@ func (s *session) ExecRestrictedStmt(ctx context.Context, stmtNode ast.StmtNode, } // ExecRestrictedStmt4Test wrapper `(s *session) ExecRestrictedStmt` for test. -func ExecRestrictedStmt4Test(ctx context.Context, s Session, +func ExecRestrictedStmt4Test(ctx context.Context, s types.Session, stmtNode ast.StmtNode, opts ...sqlexec.OptionFuncAlias) ( []chunk.Row, []*ast.ResultField, error) { ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnOthers) @@ -3044,7 +2987,7 @@ func (s *session) SetSessionStatesHandler(stateType sessionstates.SessionStateTy } // CreateSession4Test creates a new session environment for test. -func CreateSession4Test(store kv.Storage) (Session, error) { +func CreateSession4Test(store kv.Storage) (types.Session, error) { se, err := CreateSession4TestWithOpt(store, nil) if err == nil { // Cover both chunk rpc encoding and default encoding. @@ -3064,7 +3007,7 @@ type Opt struct { } // CreateSession4TestWithOpt creates a new session environment for test. -func CreateSession4TestWithOpt(store kv.Storage, opt *Opt) (Session, error) { +func CreateSession4TestWithOpt(store kv.Storage, opt *Opt) (types.Session, error) { s, err := CreateSessionWithOpt(store, opt) if err == nil { // initialize session variables for test. @@ -3078,13 +3021,13 @@ func CreateSession4TestWithOpt(store kv.Storage, opt *Opt) (Session, error) { } // CreateSession creates a new session environment. -func CreateSession(store kv.Storage) (Session, error) { +func CreateSession(store kv.Storage) (types.Session, error) { return CreateSessionWithOpt(store, nil) } // CreateSessionWithOpt creates a new session environment with option. // Use default option if opt is nil. -func CreateSessionWithOpt(store kv.Storage, opt *Opt) (Session, error) { +func CreateSessionWithOpt(store kv.Storage, opt *Opt) (types.Session, error) { s, err := createSessionWithOpt(store, opt) if err != nil { return nil, err @@ -3560,7 +3503,7 @@ func GetDomain(store kv.Storage) (*domain.Domain, error) { // If no bootstrap and storage is remote, we must use a little lease time to // bootstrap quickly, after bootstrapped, we will reset the lease time. // TODO: Using a bootstrap tool for doing this may be better later. -func runInBootstrapSession(store kv.Storage, bootstrap func(Session)) { +func runInBootstrapSession(store kv.Storage, bootstrap func(types.Session)) { s, err := createSession(store) if err != nil { // Bootstrap fail will cause program exit. @@ -4417,7 +4360,7 @@ func (s *session) setRequestSource(ctx context.Context, stmtLabel string, stmtNo } // RemoveLockDDLJobs removes the DDL jobs which doesn't get the metadata lock from job2ver. -func RemoveLockDDLJobs(s Session, job2ver map[int64]int64, job2ids map[int64]string, printLog bool) { +func RemoveLockDDLJobs(s types.Session, job2ver map[int64]int64, job2ids map[int64]string, printLog bool) { sv := s.GetSessionVars() if sv.InRestrictedSQL { return diff --git a/pkg/session/sync_upgrade.go b/pkg/session/sync_upgrade.go index 384a505fae871..be20dfc3764c9 100644 --- a/pkg/session/sync_upgrade.go +++ b/pkg/session/sync_upgrade.go @@ -23,6 +23,7 @@ import ( "github.com/pingcap/tidb/pkg/ddl/syncer" "github.com/pingcap/tidb/pkg/domain" "github.com/pingcap/tidb/pkg/owner" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx" "github.com/pingcap/tidb/pkg/util/logutil" "go.uber.org/zap" @@ -119,7 +120,7 @@ func IsUpgradingClusterState(s sessionctx.Context) (bool, error) { return stateInfo.State == syncer.StateUpgrading, nil } -func printClusterState(s Session, ver int64) { +func printClusterState(s sessiontypes.Session, ver int64) { // After SupportUpgradeHTTPOpVer version, the upgrade by paused user DDL can be notified through the HTTP API. // We check the global state see if we are upgrading by paused the user DDL. if ver >= SupportUpgradeHTTPOpVer { diff --git a/pkg/session/testutil.go b/pkg/session/testutil.go index f73533a508454..78eca23d1a63e 100644 --- a/pkg/session/testutil.go +++ b/pkg/session/testutil.go @@ -21,6 +21,7 @@ import ( "github.com/pingcap/tidb/pkg/domain" "github.com/pingcap/tidb/pkg/expression" "github.com/pingcap/tidb/pkg/kv" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/store/mockstore" "github.com/pingcap/tidb/pkg/testkit/testenv" "github.com/pingcap/tidb/pkg/util/sqlexec" @@ -50,7 +51,7 @@ func CreateStoreAndBootstrap(t *testing.T) (kv.Storage, *domain.Domain) { var sessionKitIDGenerator atomicutil.Uint64 // CreateSessionAndSetID creates a session and set connection ID. -func CreateSessionAndSetID(t *testing.T, store kv.Storage) Session { +func CreateSessionAndSetID(t *testing.T, store kv.Storage) sessiontypes.Session { se, err := CreateSession4Test(store) se.SetConnectionID(sessionKitIDGenerator.Inc()) require.NoError(t, err) @@ -58,7 +59,7 @@ func CreateSessionAndSetID(t *testing.T, store kv.Storage) Session { } // MustExec executes a sql statement and asserts no error occurs. -func MustExec(t *testing.T, se Session, sql string, args ...interface{}) { +func MustExec(t *testing.T, se sessiontypes.Session, sql string, args ...interface{}) { rs, err := exec(se, sql, args...) require.NoError(t, err) if rs != nil { @@ -67,13 +68,13 @@ func MustExec(t *testing.T, se Session, sql string, args ...interface{}) { } // MustExecToRecodeSet executes a sql statement and asserts no error occurs. -func MustExecToRecodeSet(t *testing.T, se Session, sql string, args ...interface{}) sqlexec.RecordSet { +func MustExecToRecodeSet(t *testing.T, se sessiontypes.Session, sql string, args ...interface{}) sqlexec.RecordSet { rs, err := exec(se, sql, args...) require.NoError(t, err) return rs } -func exec(se Session, sql string, args ...interface{}) (sqlexec.RecordSet, error) { +func exec(se sessiontypes.Session, sql string, args ...interface{}) (sqlexec.RecordSet, error) { ctx := context.Background() if len(args) == 0 { rs, err := se.Execute(ctx, sql) diff --git a/pkg/session/tidb.go b/pkg/session/tidb.go index 16e3a2f423678..d4beca68160c5 100644 --- a/pkg/session/tidb.go +++ b/pkg/session/tidb.go @@ -35,6 +35,7 @@ import ( "github.com/pingcap/tidb/pkg/parser" "github.com/pingcap/tidb/pkg/parser/ast" session_metrics "github.com/pingcap/tidb/pkg/session/metrics" + "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx" "github.com/pingcap/tidb/pkg/sessionctx/variable" "github.com/pingcap/tidb/pkg/sessiontxn" @@ -376,7 +377,7 @@ func GetRows4Test(ctx context.Context, _ sessionctx.Context, rs sqlexec.RecordSe } // ResultSetToStringSlice changes the RecordSet to [][]string. -func ResultSetToStringSlice(ctx context.Context, s Session, rs sqlexec.RecordSet) ([][]string, error) { +func ResultSetToStringSlice(ctx context.Context, s types.Session, rs sqlexec.RecordSet) ([][]string, error) { rows, err := GetRows4Test(ctx, s, rs) if err != nil { return nil, err diff --git a/pkg/session/types/BUILD.bazel b/pkg/session/types/BUILD.bazel new file mode 100644 index 0000000000000..c7a5e83dcb3c4 --- /dev/null +++ b/pkg/session/types/BUILD.bazel @@ -0,0 +1,21 @@ +load("@io_bazel_rules_go//go:def.bzl", "go_library") + +go_library( + name = "types", + srcs = ["sesson_interface.go"], + importpath = "github.com/pingcap/tidb/pkg/session/types", + visibility = ["//visibility:public"], + deps = [ + "//pkg/expression", + "//pkg/extension", + "//pkg/parser/ast", + "//pkg/parser/auth", + "//pkg/privilege/conn", + "//pkg/session/txninfo", + "//pkg/sessionctx", + "//pkg/sessionctx/sessionstates", + "//pkg/util", + "//pkg/util/sqlexec", + "@com_github_pingcap_kvproto//pkg/kvrpcpb", + ], +) diff --git a/pkg/session/types/sesson_interface.go b/pkg/session/types/sesson_interface.go new file mode 100644 index 0000000000000..62b31b80709d0 --- /dev/null +++ b/pkg/session/types/sesson_interface.go @@ -0,0 +1,91 @@ +// Copyright 2023 PingCAP, Inc. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package types + +import ( + "context" + "crypto/tls" + "time" + + "github.com/pingcap/kvproto/pkg/kvrpcpb" + "github.com/pingcap/tidb/pkg/expression" + "github.com/pingcap/tidb/pkg/extension" + "github.com/pingcap/tidb/pkg/parser/ast" + "github.com/pingcap/tidb/pkg/parser/auth" + "github.com/pingcap/tidb/pkg/privilege/conn" + "github.com/pingcap/tidb/pkg/session/txninfo" + "github.com/pingcap/tidb/pkg/sessionctx" + "github.com/pingcap/tidb/pkg/sessionctx/sessionstates" + "github.com/pingcap/tidb/pkg/util" + "github.com/pingcap/tidb/pkg/util/sqlexec" +) + +// Session context, it is consistent with the lifecycle of a client connection. +type Session interface { + sessionctx.Context + Status() uint16 // Flag of current status, such as autocommit. + LastInsertID() uint64 // LastInsertID is the last inserted auto_increment ID. + LastMessage() string // LastMessage is the info message that may be generated by last command + AffectedRows() uint64 // Affected rows by latest executed stmt. + // Execute is deprecated, and only used by plugins. Use ExecuteStmt() instead. + Execute(context.Context, string) ([]sqlexec.RecordSet, error) // Execute a sql statement. + // ExecuteStmt executes a parsed statement. + ExecuteStmt(context.Context, ast.StmtNode) (sqlexec.RecordSet, error) + // Parse is deprecated, use ParseWithParams() instead. + Parse(ctx context.Context, sql string) ([]ast.StmtNode, error) + // ExecuteInternal is a helper around ParseWithParams() and ExecuteStmt(). It is not allowed to execute multiple statements. + ExecuteInternal(context.Context, string, ...interface{}) (sqlexec.RecordSet, error) + String() string // String is used to debug. + CommitTxn(context.Context) error + RollbackTxn(context.Context) + // PrepareStmt executes prepare statement in binary protocol. + PrepareStmt(sql string) (stmtID uint32, paramCount int, fields []*ast.ResultField, err error) + // ExecutePreparedStmt executes a prepared statement. + // Deprecated: please use ExecuteStmt, this function is left for testing only. + // TODO: remove ExecutePreparedStmt. + ExecutePreparedStmt(ctx context.Context, stmtID uint32, param []expression.Expression) (sqlexec.RecordSet, error) + DropPreparedStmt(stmtID uint32) error + // SetSessionStatesHandler sets SessionStatesHandler for type stateType. + SetSessionStatesHandler(stateType sessionstates.SessionStateType, handler sessionctx.SessionStatesHandler) + SetClientCapability(uint32) // Set client capability flags. + SetConnectionID(uint64) + SetCommandValue(byte) + SetCompressionAlgorithm(int) + SetCompressionLevel(int) + SetProcessInfo(string, time.Time, byte, uint64) + SetTLSState(*tls.ConnectionState) + SetCollation(coID int) error + SetSessionManager(util.SessionManager) + Close() + Auth(user *auth.UserIdentity, auth, salt []byte, authConn conn.AuthConn) error + AuthWithoutVerification(user *auth.UserIdentity) bool + AuthPluginForUser(user *auth.UserIdentity) (string, error) + MatchIdentity(username, remoteHost string) (*auth.UserIdentity, error) + // Return the information of the txn current running + TxnInfo() *txninfo.TxnInfo + // PrepareTxnCtx is exported for test. + PrepareTxnCtx(context.Context) error + // FieldList returns fields list of a table. + FieldList(tableName string) (fields []*ast.ResultField, err error) + SetPort(port string) + + // set cur session operations allowed when tikv disk full happens. + SetDiskFullOpt(level kvrpcpb.DiskFullOpt) + GetDiskFullOpt() kvrpcpb.DiskFullOpt + ClearDiskFullOpt() + + // SetExtensions sets the `*extension.SessionExtensions` object + SetExtensions(extensions *extension.SessionExtensions) +} diff --git a/pkg/store/gcworker/BUILD.bazel b/pkg/store/gcworker/BUILD.bazel index 5745182657b9f..25f5b2d2e7134 100644 --- a/pkg/store/gcworker/BUILD.bazel +++ b/pkg/store/gcworker/BUILD.bazel @@ -17,6 +17,7 @@ go_library( "//pkg/parser/terror", "//pkg/privilege", "//pkg/session", + "//pkg/session/types", "//pkg/sessionctx/variable", "//pkg/tablecodec", "//pkg/util/codec", diff --git a/pkg/store/gcworker/gc_worker.go b/pkg/store/gcworker/gc_worker.go index b090f082e6582..97ccd85bb1a92 100644 --- a/pkg/store/gcworker/gc_worker.go +++ b/pkg/store/gcworker/gc_worker.go @@ -45,6 +45,7 @@ import ( "github.com/pingcap/tidb/pkg/parser/terror" "github.com/pingcap/tidb/pkg/privilege" "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx/variable" "github.com/pingcap/tidb/pkg/tablecodec" "github.com/pingcap/tidb/pkg/util/codec" @@ -232,7 +233,7 @@ func (w *GCWorker) start(ctx context.Context, wg *sync.WaitGroup) { } } -func createSession(store kv.Storage) session.Session { +func createSession(store kv.Storage) sessiontypes.Session { for { se, err := session.CreateSession(store) if err != nil { @@ -1874,7 +1875,7 @@ func (w *GCWorker) saveValueToSysTable(key, value string) error { // GC placement rules when the partitions are removed by the GC worker. // Placement rules cannot be removed immediately after drop table / truncate table, // because the tables can be flashed back or recovered. -func (w *GCWorker) doGCPlacementRules(se session.Session, safePoint uint64, dr util.DelRangeTask, gcPlacementRuleCache map[int64]interface{}) (err error) { +func (w *GCWorker) doGCPlacementRules(se sessiontypes.Session, safePoint uint64, dr util.DelRangeTask, gcPlacementRuleCache map[int64]interface{}) (err error) { // Get the job from the job history var historyJob *model.Job failpoint.Inject("mockHistoryJobForGC", func(v failpoint.Value) { diff --git a/pkg/table/tables/BUILD.bazel b/pkg/table/tables/BUILD.bazel index b10e03f4b7d4b..2b4d0f6524dd7 100644 --- a/pkg/table/tables/BUILD.bazel +++ b/pkg/table/tables/BUILD.bazel @@ -88,6 +88,7 @@ go_test( "//pkg/parser/model", "//pkg/parser/mysql", "//pkg/session", + "//pkg/session/types", "//pkg/sessionctx", "//pkg/sessionctx/stmtctx", "//pkg/sessionctx/variable", diff --git a/pkg/table/tables/state_remote_test.go b/pkg/table/tables/state_remote_test.go index d8b6cde16ee26..898c5aba4392b 100644 --- a/pkg/table/tables/state_remote_test.go +++ b/pkg/table/tables/state_remote_test.go @@ -20,7 +20,7 @@ import ( "time" "github.com/pingcap/tidb/pkg/kv" - "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/table/tables" "github.com/pingcap/tidb/pkg/testkit" "github.com/stretchr/testify/require" @@ -28,7 +28,7 @@ import ( ) // initRow add a new record into the cached table meta lock table. -func initRow(ctx context.Context, exec session.Session, tid int) error { +func initRow(ctx context.Context, exec sessiontypes.Session, tid int) error { _, err := exec.ExecuteInternal(ctx, "insert ignore into mysql.table_cache_meta values (%?, 'NONE', 0, 0)", tid) return err } diff --git a/pkg/telemetry/cte_test/BUILD.bazel b/pkg/telemetry/cte_test/BUILD.bazel index 39590ecbd9dd8..0918caac0e410 100644 --- a/pkg/telemetry/cte_test/BUILD.bazel +++ b/pkg/telemetry/cte_test/BUILD.bazel @@ -10,6 +10,7 @@ go_test( "//pkg/domain", "//pkg/kv", "//pkg/session", + "//pkg/session/types", "//pkg/store/mockstore", "//pkg/testkit/testsetup", "@com_github_stretchr_testify//require", diff --git a/pkg/telemetry/cte_test/cte_test.go b/pkg/telemetry/cte_test/cte_test.go index 0f61a3485ac97..445223978b562 100644 --- a/pkg/telemetry/cte_test/cte_test.go +++ b/pkg/telemetry/cte_test/cte_test.go @@ -21,6 +21,7 @@ import ( "github.com/pingcap/tidb/pkg/domain" "github.com/pingcap/tidb/pkg/kv" "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/store/mockstore" "github.com/pingcap/tidb/pkg/testkit/testsetup" "github.com/stretchr/testify/require" @@ -95,7 +96,7 @@ type testSuite struct { store kv.Storage dom *domain.Domain etcdCluster *integration.ClusterV3 - se session.Session + se sessiontypes.Session close func() } diff --git a/pkg/testkit/BUILD.bazel b/pkg/testkit/BUILD.bazel index b9922d4065b45..13890b8f84a8c 100644 --- a/pkg/testkit/BUILD.bazel +++ b/pkg/testkit/BUILD.bazel @@ -26,6 +26,7 @@ go_library( "//pkg/resourcemanager", "//pkg/session", "//pkg/session/txninfo", + "//pkg/session/types", "//pkg/sessionctx", "//pkg/sessionctx/variable", "//pkg/store/driver", diff --git a/pkg/testkit/asynctestkit.go b/pkg/testkit/asynctestkit.go index 2867e4f256eb1..ad6cf72b91ef4 100644 --- a/pkg/testkit/asynctestkit.go +++ b/pkg/testkit/asynctestkit.go @@ -26,6 +26,7 @@ import ( "github.com/pingcap/tidb/pkg/expression" "github.com/pingcap/tidb/pkg/kv" "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/util/sqlexec" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" @@ -240,10 +241,10 @@ type sessionCtxKeyType struct{} var sessionKey = sessionCtxKeyType{} // TryRetrieveSession tries retrieve session from context. -func TryRetrieveSession(ctx context.Context) session.Session { +func TryRetrieveSession(ctx context.Context) sessiontypes.Session { s := ctx.Value(sessionKey) if s == nil { return nil } - return s.(session.Session) + return s.(sessiontypes.Session) } diff --git a/pkg/testkit/mocksessionmanager.go b/pkg/testkit/mocksessionmanager.go index af1aa1022660a..88223f21e51d7 100644 --- a/pkg/testkit/mocksessionmanager.go +++ b/pkg/testkit/mocksessionmanager.go @@ -24,6 +24,7 @@ import ( "github.com/pingcap/tidb/pkg/planner/core" "github.com/pingcap/tidb/pkg/session" "github.com/pingcap/tidb/pkg/session/txninfo" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx" "github.com/pingcap/tidb/pkg/util" ) @@ -35,7 +36,7 @@ type MockSessionManager struct { SerID uint64 TxnInfo []*txninfo.TxnInfo Dom *domain.Domain - Conn map[uint64]session.Session + Conn map[uint64]sessiontypes.Session mu sync.Mutex ConAttrs map[uint64]map[string]string diff --git a/pkg/testkit/testkit.go b/pkg/testkit/testkit.go index 07949e3b87b04..dca6e0de84b43 100644 --- a/pkg/testkit/testkit.go +++ b/pkg/testkit/testkit.go @@ -30,6 +30,7 @@ import ( "github.com/pingcap/tidb/pkg/parser/ast" "github.com/pingcap/tidb/pkg/parser/terror" "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/sessionctx/variable" "github.com/pingcap/tidb/pkg/testkit/testenv" "github.com/pingcap/tidb/pkg/types" @@ -53,7 +54,7 @@ type TestKit struct { assert *assert.Assertions t testing.TB store kv.Storage - session session.Session + session sessiontypes.Session alloc chunk.Allocator } @@ -77,7 +78,7 @@ func NewTestKit(t testing.TB, store kv.Storage) *TestKit { if ok { mockSm.mu.Lock() if mockSm.Conn == nil { - mockSm.Conn = make(map[uint64]session.Session) + mockSm.Conn = make(map[uint64]sessiontypes.Session) } mockSm.Conn[tk.session.GetSessionVars().ConnectionID] = tk.session mockSm.mu.Unlock() @@ -89,7 +90,7 @@ func NewTestKit(t testing.TB, store kv.Storage) *TestKit { } // NewTestKitWithSession returns a new *TestKit. -func NewTestKitWithSession(t testing.TB, store kv.Storage, se session.Session) *TestKit { +func NewTestKitWithSession(t testing.TB, store kv.Storage, se sessiontypes.Session) *TestKit { return &TestKit{ require: require.New(t), assert: assert.New(t), @@ -108,14 +109,14 @@ func (tk *TestKit) RefreshSession() { } // SetSession set the session of testkit -func (tk *TestKit) SetSession(session session.Session) { +func (tk *TestKit) SetSession(session sessiontypes.Session) { tk.session = session // enforce sysvar cache loading, ref loadCommonGlobalVariableIfNeeded tk.MustExec("select 3") } // Session return the session associated with the testkit -func (tk *TestKit) Session() session.Session { +func (tk *TestKit) Session() sessiontypes.Session { return tk.session } @@ -422,7 +423,7 @@ func (tk *TestKit) MustExecToErr(sql string, args ...interface{}) { tk.require.Error(err) } -func newSession(t testing.TB, store kv.Storage) session.Session { +func newSession(t testing.TB, store kv.Storage) sessiontypes.Session { se, err := session.CreateSession4Test(store) require.NoError(t, err) se.SetConnectionID(testKitIDGenerator.Inc()) diff --git a/pkg/util/ddl-checker/BUILD.bazel b/pkg/util/ddl-checker/BUILD.bazel index c8ee1ac53d0b2..96d5884489418 100644 --- a/pkg/util/ddl-checker/BUILD.bazel +++ b/pkg/util/ddl-checker/BUILD.bazel @@ -12,6 +12,7 @@ go_library( "//pkg/parser", "//pkg/parser/ast", "//pkg/session", + "//pkg/session/types", "//pkg/store/mockstore", "//pkg/util/dbutil", "//pkg/util/logutil", diff --git a/pkg/util/ddl-checker/executable_checker.go b/pkg/util/ddl-checker/executable_checker.go index a448c6b8fa787..15230d9bcefd7 100644 --- a/pkg/util/ddl-checker/executable_checker.go +++ b/pkg/util/ddl-checker/executable_checker.go @@ -23,6 +23,7 @@ import ( "github.com/pingcap/tidb/pkg/parser" "github.com/pingcap/tidb/pkg/parser/ast" "github.com/pingcap/tidb/pkg/session" + sessiontypes "github.com/pingcap/tidb/pkg/session/types" "github.com/pingcap/tidb/pkg/store/mockstore" "github.com/pingcap/tidb/pkg/util/logutil" "go.uber.org/atomic" @@ -30,7 +31,7 @@ import ( // ExecutableChecker is a part of TiDB to check the sql's executability type ExecutableChecker struct { - session session.Session + session sessiontypes.Session parser *parser.Parser isClosed *atomic.Bool }