From 7de47ae9c71270f7231e64336d267bf78c0c05ba Mon Sep 17 00:00:00 2001 From: Chao Wang Date: Fri, 20 Sep 2024 10:30:49 +0800 Subject: [PATCH] lightning: remove legacy code of `session` --- pkg/lightning/backend/kv/BUILD.bazel | 8 -- pkg/lightning/backend/kv/context_test.go | 85 ------------ pkg/lightning/backend/kv/session.go | 163 ----------------------- 3 files changed, 256 deletions(-) diff --git a/pkg/lightning/backend/kv/BUILD.bazel b/pkg/lightning/backend/kv/BUILD.bazel index b28c0b1339fab..34d4dc24ef32a 100644 --- a/pkg/lightning/backend/kv/BUILD.bazel +++ b/pkg/lightning/backend/kv/BUILD.bazel @@ -18,8 +18,6 @@ go_library( "//pkg/expression", "//pkg/expression/exprctx", "//pkg/expression/exprstatic", - "//pkg/expression/sessionexpr", - "//pkg/infoschema/context", "//pkg/kv", "//pkg/lightning/backend/encode", "//pkg/lightning/common", @@ -30,14 +28,11 @@ go_library( "//pkg/meta/autoid", "//pkg/meta/model", "//pkg/parser/mysql", - "//pkg/planner/planctx", - "//pkg/sessionctx", "//pkg/sessionctx/stmtctx", "//pkg/sessionctx/variable", "//pkg/table", "//pkg/table/tables", "//pkg/table/tblctx", - "//pkg/table/tblsession", "//pkg/tablecodec", "//pkg/types", "//pkg/util/chunk", @@ -47,7 +42,6 @@ go_library( "//pkg/util/mathutil", "//pkg/util/redact", "//pkg/util/timeutil", - "//pkg/util/topsql/stmtstats", "@com_github_docker_go_units//:go-units", "@com_github_pingcap_errors//:errors", "@org_uber_go_zap//:zap", @@ -74,7 +68,6 @@ go_test( "//pkg/ddl", "//pkg/errctx", "//pkg/expression/exprctx", - "//pkg/expression/exprstatic", "//pkg/kv", "//pkg/lightning/backend/encode", "//pkg/lightning/common", @@ -95,7 +88,6 @@ go_test( "//pkg/tablecodec", "//pkg/types", "//pkg/util/context", - "//pkg/util/deeptest", "//pkg/util/mock", "//pkg/util/rowcodec", "//pkg/util/timeutil", diff --git a/pkg/lightning/backend/kv/context_test.go b/pkg/lightning/backend/kv/context_test.go index 8941bb82fa222..bcd9c8aacd198 100644 --- a/pkg/lightning/backend/kv/context_test.go +++ b/pkg/lightning/backend/kv/context_test.go @@ -23,9 +23,6 @@ import ( "github.com/pingcap/errors" "github.com/pingcap/tidb/pkg/errctx" "github.com/pingcap/tidb/pkg/expression/exprctx" - "github.com/pingcap/tidb/pkg/expression/exprstatic" - "github.com/pingcap/tidb/pkg/lightning/backend/encode" - "github.com/pingcap/tidb/pkg/lightning/log" "github.com/pingcap/tidb/pkg/meta/model" "github.com/pingcap/tidb/pkg/parser/mysql" "github.com/pingcap/tidb/pkg/sessionctx/stmtctx" @@ -33,7 +30,6 @@ import ( "github.com/pingcap/tidb/pkg/table/tblctx" "github.com/pingcap/tidb/pkg/types" contextutil "github.com/pingcap/tidb/pkg/util/context" - "github.com/pingcap/tidb/pkg/util/deeptest" "github.com/pingcap/tidb/pkg/util/rowcodec" "github.com/pingcap/tidb/pkg/util/timeutil" "github.com/stretchr/testify/require" @@ -126,63 +122,10 @@ func TestLitExprContext(t *testing.T) { }, } - // We need to compare the new introduced `*litExprContext` the same behavior with the old `session`. - // After refactoring finished, we can remove the old session and this test. - compareWithLegacySession := func(t *testing.T, ctx *litExprContext, opts *encode.SessionOptions) { - if opts.SysVars == nil { - opts.SysVars = make(map[string]string) - } - if _, ok := opts.SysVars["div_precision_increment"]; !ok { - // It seems that `DefDivPrecisionIncrement` is not set as a default value in `newSession` and its - // default value is 0. - // We should set it manually to make test pass. - // The legacy code has no bug for this default value because the `DefaultImportantVariables` - // will be loaded every time to override this variable: - // https://github.com/pingcap/tidb/blob/2e457b394f09165e23fa5121fcfd89c6e8a6e835/pkg/lightning/common/common.go#L33-L42 - opts.SysVars["div_precision_increment"] = strconv.Itoa(variable.DefDivPrecisionIncrement) - } - if _, ok := opts.SysVars["block_encryption_mode"]; !ok { - // same reason with `DivPrecisionIncrement`, we need to set `block_encryption_mode` manually to pass test. - opts.SysVars["block_encryption_mode"] = variable.DefBlockEncryptionMode - } - se := newSession(opts, log.L()) - seCtx := exprstatic.MakeExprContextStatic(se.exprCtx.ExprContext) - deeptest.AssertDeepClonedEqual(t, seCtx, ctx.ExprContext, deeptest.WithIgnorePath([]string{ - "$.exprCtxState.evalCtx.id", - "$.exprCtxState.evalCtx.evalCtxState.typeCtx.loc", - "$.exprCtxState.evalCtx.evalCtxState.warnHandler", - "$.exprCtxState.evalCtx.evalCtxState.typeCtx.warnHandler", - "$.exprCtxState.evalCtx.evalCtxState.errCtx.warnHandler", - "$.exprCtxState.evalCtx.evalCtxState.currentTime", - "$.exprCtxState.evalCtx.evalCtxState.requestVerificationFn", - "$.exprCtxState.evalCtx.evalCtxState.requestDynamicVerificationFn", - "$.exprCtxState.rng", - "$.exprCtxState.planCacheTracker", - })) - currentTime, err := seCtx.GetEvalCtx().CurrentTime() - require.NoError(t, err) - seTime, err := seCtx.GetEvalCtx().CurrentTime() - require.NoError(t, err) - if opts.Timestamp == 0 { - require.InDelta(t, seTime.Unix(), currentTime.Unix(), 2) - } else { - require.Equal(t, opts.Timestamp*1000000000, currentTime.UnixNano()) - require.Equal(t, seTime.UnixNano(), currentTime.UnixNano()) - } - require.Equal(t, seCtx.GetEvalCtx().Location().String(), ctx.GetEvalCtx().Location().String()) - } - for i, c := range cases { t.Run("case-"+strconv.Itoa(i), func(t *testing.T) { ctx, err := newLitExprContext(c.sqlMode, c.sysVars, c.timestamp) require.NoError(t, err) - - compareWithLegacySession(t, ctx, &encode.SessionOptions{ - SQLMode: c.sqlMode, - SysVars: c.sysVars, - Timestamp: c.timestamp, - }) - evalCtx := ctx.GetEvalCtx() require.Equal(t, c.sqlMode, evalCtx.SQLMode()) tc, ec := evalCtx.TypeCtx(), evalCtx.ErrCtx() @@ -316,31 +259,6 @@ func TestLitTableMutateContext(t *testing.T) { require.Empty(t, tblCtx.GetColumnSize(456)) } - // We need to compare the new introduced `*litTableMutateContext` the same behavior with the old `session`. - // After refactoring finished, we can remove the old session and this test. - compareWithLegacySession := func(ctx *litTableMutateContext, vars map[string]string) { - se := newSession(&encode.SessionOptions{ - SQLMode: mysql.ModeNone, - SysVars: vars, - }, log.L()) - // make sure GetRowIDShardGenerator() internal assertion pass - se.GetSessionVars().TxnCtx = &variable.TransactionContext{} - se.GetSessionVars().TxnCtx.StartTS = 123 - seCtx := se.GetTableCtx() - require.Equal(t, seCtx.ConnectionID(), ctx.ConnectionID()) - require.Equal(t, seCtx.InRestrictedSQL(), ctx.InRestrictedSQL()) - require.Equal(t, seCtx.TxnAssertionLevel(), ctx.TxnAssertionLevel()) - require.Equal(t, seCtx.GetMutateBuffers(), ctx.GetMutateBuffers()) - require.Equal(t, seCtx.EnableMutationChecker(), ctx.EnableMutationChecker()) - require.Equal(t, seCtx.GetRowEncodingConfig(), ctx.GetRowEncodingConfig()) - require.Equal(t, seCtx.GetRowIDShardGenerator().GetShardStep(), ctx.GetRowIDShardGenerator().GetShardStep()) - seAlloc, ok := seCtx.GetReservedRowIDAlloc() - require.True(t, ok) - alloc, ok := ctx.GetReservedRowIDAlloc() - require.True(t, ok) - require.Equal(t, seAlloc, alloc) - } - // test for default tblCtx, err := newLitTableMutateContext(exprCtx, nil) require.NoError(t, err) @@ -355,7 +273,6 @@ func TestLitTableMutateContext(t *testing.T) { g := tblCtx.GetRowIDShardGenerator() require.NotNil(t, g) require.Equal(t, variable.DefTiDBShardAllocateStep, g.GetShardStep()) - compareWithLegacySession(tblCtx, nil) // test for load vars sysVars := map[string]string{ @@ -377,7 +294,6 @@ func TestLitTableMutateContext(t *testing.T) { require.NotNil(t, g) require.NotEqual(t, variable.DefTiDBShardAllocateStep, g.GetShardStep()) require.Equal(t, 1234567, g.GetShardStep()) - compareWithLegacySession(tblCtx, sysVars) // test for `RowEncodingConfig.IsRowLevelChecksumEnabled` which should be loaded from global variable. require.False(t, variable.EnableRowLevelChecksum.Load()) @@ -392,5 +308,4 @@ func TestLitTableMutateContext(t *testing.T) { IsRowLevelChecksumEnabled: true, RowEncoder: &rowcodec.Encoder{Enable: true}, }, tblCtx.GetRowEncodingConfig()) - compareWithLegacySession(tblCtx, sysVars) } diff --git a/pkg/lightning/backend/kv/session.go b/pkg/lightning/backend/kv/session.go index e13667a55f197..bda638da38609 100644 --- a/pkg/lightning/backend/kv/session.go +++ b/pkg/lightning/backend/kv/session.go @@ -19,29 +19,20 @@ package kv import ( "context" "errors" - "fmt" - "strconv" "sync" "github.com/docker/go-units" - "github.com/pingcap/tidb/pkg/errctx" "github.com/pingcap/tidb/pkg/expression/exprctx" - "github.com/pingcap/tidb/pkg/expression/sessionexpr" - infoschema "github.com/pingcap/tidb/pkg/infoschema/context" "github.com/pingcap/tidb/pkg/kv" "github.com/pingcap/tidb/pkg/lightning/backend/encode" "github.com/pingcap/tidb/pkg/lightning/common" "github.com/pingcap/tidb/pkg/lightning/log" "github.com/pingcap/tidb/pkg/lightning/manual" "github.com/pingcap/tidb/pkg/meta/model" - planctx "github.com/pingcap/tidb/pkg/planner/planctx" - "github.com/pingcap/tidb/pkg/sessionctx" "github.com/pingcap/tidb/pkg/sessionctx/variable" "github.com/pingcap/tidb/pkg/table/tblctx" - "github.com/pingcap/tidb/pkg/table/tblsession" "github.com/pingcap/tidb/pkg/types" "github.com/pingcap/tidb/pkg/util/mathutil" - "github.com/pingcap/tidb/pkg/util/topsql/stmtstats" "go.uber.org/zap" ) @@ -282,160 +273,6 @@ func (*transaction) MayFlush() error { return nil } -// sessExprContext implements the ExprContext interface -// It embedded an `ExprContext` and a `sessEvalContext` to provide no optional properties. -type sessExprContext struct { - *sessionexpr.ExprContext - evalCtx *sessEvalContext -} - -// GetEvalCtx implements the ExprContext.GetEvalCtx interface -func (ctx *sessExprContext) GetEvalCtx() exprctx.EvalContext { - return ctx.evalCtx -} - -// sessEvalContext implements the EvalContext interface -// It embedded an `EvalContext` and provide no optional properties. -type sessEvalContext struct { - exprctx.EvalContext -} - -// GetOptionalPropSet returns the optional properties provided by this context. -func (*sessEvalContext) GetOptionalPropSet() exprctx.OptionalEvalPropKeySet { - return 0 -} - -// GetOptionalPropProvider gets the optional property provider by key -func (*sessEvalContext) GetOptionalPropProvider(exprctx.OptionalEvalPropKey) (exprctx.OptionalEvalPropProvider, bool) { - return nil, false -} - -// Deprecated: `session` will be removed soon. -// session is a trimmed down Session type which only wraps our own trimmed-down -// transaction type and provides the session variables to the TiDB library -// optimized for Lightning. -// The `session` object is private to make sure it is only used by public `Session` struct to provide limited access. -// TODO: remove `session` and build related context without a mocked `sessionctx.Context` instead. -type session struct { - sessionctx.Context - planctx.EmptyPlanContextExtended - txn transaction - Vars *variable.SessionVars - exprCtx *sessExprContext - tblctx *tblsession.MutateContext - // currently, we only set `CommonAddRecordCtx` - values map[fmt.Stringer]any -} - -// Deprecated: this function will be removed soon. -// newSession creates a new trimmed down Session matching the options. -func newSession(options *encode.SessionOptions, logger log.Logger) *session { - s := &session{ - values: make(map[fmt.Stringer]any, 1), - } - sqlMode := options.SQLMode - vars := variable.NewSessionVars(s) - vars.SkipUTF8Check = true - vars.StmtCtx.InInsertStmt = true - vars.SQLMode = sqlMode - - typeFlags := vars.StmtCtx.TypeFlags(). - WithTruncateAsWarning(!sqlMode.HasStrictMode()). - WithIgnoreInvalidDateErr(sqlMode.HasAllowInvalidDatesMode()). - WithIgnoreZeroInDate(!sqlMode.HasStrictMode() || sqlMode.HasAllowInvalidDatesMode() || - !sqlMode.HasNoZeroInDateMode() || !sqlMode.HasNoZeroDateMode()) - vars.StmtCtx.SetTypeFlags(typeFlags) - - errLevels := vars.StmtCtx.ErrLevels() - errLevels[errctx.ErrGroupBadNull] = errctx.ResolveErrLevel(false, !sqlMode.HasStrictMode()) - errLevels[errctx.ErrGroupDividedByZero] = - errctx.ResolveErrLevel(!sqlMode.HasErrorForDivisionByZeroMode(), !sqlMode.HasStrictMode()) - vars.StmtCtx.SetErrLevels(errLevels) - - if options.SysVars != nil { - for k, v := range options.SysVars { - // since 6.3(current master) tidb checks whether we can set a system variable - // lc_time_names is a read-only variable for now, but might be implemented later, - // so we not remove it from defaultImportantVariables and check it in below way. - if sv := variable.GetSysVar(k); sv == nil { - logger.DPanic("unknown system var", zap.String("key", k)) - continue - } else if sv.ReadOnly { - logger.Debug("skip read-only variable", zap.String("key", k)) - continue - } - if err := vars.SetSystemVar(k, v); err != nil { - logger.DPanic("new session: failed to set system var", - log.ShortError(err), - zap.String("key", k)) - } - } - } - vars.StmtCtx.SetTimeZone(vars.Location()) - if err := vars.SetSystemVar("timestamp", strconv.FormatInt(options.Timestamp, 10)); err != nil { - logger.Warn("new session: failed to set timestamp", - log.ShortError(err)) - } - vars.TxnCtx = nil - s.Vars = vars - exprCtx := sessionexpr.NewExprContext(s) - // The exprCtx should be an expression context providing no optional properties in `EvalContext`. - // That is to make sure it only allows expressions that require basic context. - s.exprCtx = &sessExprContext{ - ExprContext: exprCtx, - evalCtx: &sessEvalContext{ - EvalContext: exprCtx.GetEvalCtx(), - }, - } - s.tblctx = tblsession.NewMutateContext(s) - s.txn.kvPairs = &Pairs{} - - return s -} - -// Txn implements the sessionctx.Context interface -func (se *session) Txn(_ bool) (kv.Transaction, error) { - return &se.txn, nil -} - -// GetSessionVars implements the sessionctx.Context interface -func (se *session) GetSessionVars() *variable.SessionVars { - return se.Vars -} - -// GetExprCtx returns the expression context of the session. -func (se *session) GetExprCtx() exprctx.ExprContext { - return se.exprCtx -} - -// GetTableCtx returns the table.MutateContext -func (se *session) GetTableCtx() tblctx.MutateContext { - return se.tblctx -} - -// SetValue saves a value associated with this context for key. -func (se *session) SetValue(key fmt.Stringer, value any) { - se.values[key] = value -} - -// Value returns the value associated with this context for key. -func (se *session) Value(key fmt.Stringer) any { - return se.values[key] -} - -// StmtAddDirtyTableOP implements the sessionctx.Context interface -func (*session) StmtAddDirtyTableOP(_ int, _ int64, _ kv.Handle) {} - -// GetInfoSchema implements the sessionctx.Context interface. -func (*session) GetInfoSchema() infoschema.MetaOnlyInfoSchema { - return nil -} - -// GetStmtStats implements the sessionctx.Context interface. -func (*session) GetStmtStats() *stmtstats.StatementStats { - return nil -} - // Session is used to provide context for lightning. type Session struct { txn transaction