Skip to content

Commit 1dedadf

Browse files
lcwangchaowinoros
authored andcommitted
lightning: remove legacy code of session (pingcap#56174)
ref pingcap#53388
1 parent e9b21b6 commit 1dedadf

File tree

3 files changed

+0
-256
lines changed

3 files changed

+0
-256
lines changed

pkg/lightning/backend/kv/BUILD.bazel

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,6 @@ go_library(
1818
"//pkg/expression",
1919
"//pkg/expression/exprctx",
2020
"//pkg/expression/exprstatic",
21-
"//pkg/expression/sessionexpr",
22-
"//pkg/infoschema/context",
2321
"//pkg/kv",
2422
"//pkg/lightning/backend/encode",
2523
"//pkg/lightning/common",
@@ -30,14 +28,11 @@ go_library(
3028
"//pkg/meta/autoid",
3129
"//pkg/meta/model",
3230
"//pkg/parser/mysql",
33-
"//pkg/planner/planctx",
34-
"//pkg/sessionctx",
3531
"//pkg/sessionctx/stmtctx",
3632
"//pkg/sessionctx/variable",
3733
"//pkg/table",
3834
"//pkg/table/tables",
3935
"//pkg/table/tblctx",
40-
"//pkg/table/tblsession",
4136
"//pkg/tablecodec",
4237
"//pkg/types",
4338
"//pkg/util/chunk",
@@ -47,7 +42,6 @@ go_library(
4742
"//pkg/util/mathutil",
4843
"//pkg/util/redact",
4944
"//pkg/util/timeutil",
50-
"//pkg/util/topsql/stmtstats",
5145
"@com_github_docker_go_units//:go-units",
5246
"@com_github_pingcap_errors//:errors",
5347
"@org_uber_go_zap//:zap",
@@ -74,7 +68,6 @@ go_test(
7468
"//pkg/ddl",
7569
"//pkg/errctx",
7670
"//pkg/expression/exprctx",
77-
"//pkg/expression/exprstatic",
7871
"//pkg/kv",
7972
"//pkg/lightning/backend/encode",
8073
"//pkg/lightning/common",
@@ -95,7 +88,6 @@ go_test(
9588
"//pkg/tablecodec",
9689
"//pkg/types",
9790
"//pkg/util/context",
98-
"//pkg/util/deeptest",
9991
"//pkg/util/mock",
10092
"//pkg/util/rowcodec",
10193
"//pkg/util/timeutil",

pkg/lightning/backend/kv/context_test.go

Lines changed: 0 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,13 @@ import (
2323
"github.com/pingcap/errors"
2424
"github.com/pingcap/tidb/pkg/errctx"
2525
"github.com/pingcap/tidb/pkg/expression/exprctx"
26-
"github.com/pingcap/tidb/pkg/expression/exprstatic"
27-
"github.com/pingcap/tidb/pkg/lightning/backend/encode"
28-
"github.com/pingcap/tidb/pkg/lightning/log"
2926
"github.com/pingcap/tidb/pkg/meta/model"
3027
"github.com/pingcap/tidb/pkg/parser/mysql"
3128
"github.com/pingcap/tidb/pkg/sessionctx/stmtctx"
3229
"github.com/pingcap/tidb/pkg/sessionctx/variable"
3330
"github.com/pingcap/tidb/pkg/table/tblctx"
3431
"github.com/pingcap/tidb/pkg/types"
3532
contextutil "github.com/pingcap/tidb/pkg/util/context"
36-
"github.com/pingcap/tidb/pkg/util/deeptest"
3733
"github.com/pingcap/tidb/pkg/util/rowcodec"
3834
"github.com/pingcap/tidb/pkg/util/timeutil"
3935
"github.com/stretchr/testify/require"
@@ -126,63 +122,10 @@ func TestLitExprContext(t *testing.T) {
126122
},
127123
}
128124

129-
// We need to compare the new introduced `*litExprContext` the same behavior with the old `session`.
130-
// After refactoring finished, we can remove the old session and this test.
131-
compareWithLegacySession := func(t *testing.T, ctx *litExprContext, opts *encode.SessionOptions) {
132-
if opts.SysVars == nil {
133-
opts.SysVars = make(map[string]string)
134-
}
135-
if _, ok := opts.SysVars["div_precision_increment"]; !ok {
136-
// It seems that `DefDivPrecisionIncrement` is not set as a default value in `newSession` and its
137-
// default value is 0.
138-
// We should set it manually to make test pass.
139-
// The legacy code has no bug for this default value because the `DefaultImportantVariables`
140-
// will be loaded every time to override this variable:
141-
// https://github.com/pingcap/tidb/blob/2e457b394f09165e23fa5121fcfd89c6e8a6e835/pkg/lightning/common/common.go#L33-L42
142-
opts.SysVars["div_precision_increment"] = strconv.Itoa(variable.DefDivPrecisionIncrement)
143-
}
144-
if _, ok := opts.SysVars["block_encryption_mode"]; !ok {
145-
// same reason with `DivPrecisionIncrement`, we need to set `block_encryption_mode` manually to pass test.
146-
opts.SysVars["block_encryption_mode"] = variable.DefBlockEncryptionMode
147-
}
148-
se := newSession(opts, log.L())
149-
seCtx := exprstatic.MakeExprContextStatic(se.exprCtx.ExprContext)
150-
deeptest.AssertDeepClonedEqual(t, seCtx, ctx.ExprContext, deeptest.WithIgnorePath([]string{
151-
"$.exprCtxState.evalCtx.id",
152-
"$.exprCtxState.evalCtx.evalCtxState.typeCtx.loc",
153-
"$.exprCtxState.evalCtx.evalCtxState.warnHandler",
154-
"$.exprCtxState.evalCtx.evalCtxState.typeCtx.warnHandler",
155-
"$.exprCtxState.evalCtx.evalCtxState.errCtx.warnHandler",
156-
"$.exprCtxState.evalCtx.evalCtxState.currentTime",
157-
"$.exprCtxState.evalCtx.evalCtxState.requestVerificationFn",
158-
"$.exprCtxState.evalCtx.evalCtxState.requestDynamicVerificationFn",
159-
"$.exprCtxState.rng",
160-
"$.exprCtxState.planCacheTracker",
161-
}))
162-
currentTime, err := seCtx.GetEvalCtx().CurrentTime()
163-
require.NoError(t, err)
164-
seTime, err := seCtx.GetEvalCtx().CurrentTime()
165-
require.NoError(t, err)
166-
if opts.Timestamp == 0 {
167-
require.InDelta(t, seTime.Unix(), currentTime.Unix(), 2)
168-
} else {
169-
require.Equal(t, opts.Timestamp*1000000000, currentTime.UnixNano())
170-
require.Equal(t, seTime.UnixNano(), currentTime.UnixNano())
171-
}
172-
require.Equal(t, seCtx.GetEvalCtx().Location().String(), ctx.GetEvalCtx().Location().String())
173-
}
174-
175125
for i, c := range cases {
176126
t.Run("case-"+strconv.Itoa(i), func(t *testing.T) {
177127
ctx, err := newLitExprContext(c.sqlMode, c.sysVars, c.timestamp)
178128
require.NoError(t, err)
179-
180-
compareWithLegacySession(t, ctx, &encode.SessionOptions{
181-
SQLMode: c.sqlMode,
182-
SysVars: c.sysVars,
183-
Timestamp: c.timestamp,
184-
})
185-
186129
evalCtx := ctx.GetEvalCtx()
187130
require.Equal(t, c.sqlMode, evalCtx.SQLMode())
188131
tc, ec := evalCtx.TypeCtx(), evalCtx.ErrCtx()
@@ -314,31 +257,6 @@ func TestLitTableMutateContext(t *testing.T) {
314257
require.Empty(t, tblCtx.GetColumnSize(456))
315258
}
316259

317-
// We need to compare the new introduced `*litTableMutateContext` the same behavior with the old `session`.
318-
// After refactoring finished, we can remove the old session and this test.
319-
compareWithLegacySession := func(ctx *litTableMutateContext, vars map[string]string) {
320-
se := newSession(&encode.SessionOptions{
321-
SQLMode: mysql.ModeNone,
322-
SysVars: vars,
323-
}, log.L())
324-
// make sure GetRowIDShardGenerator() internal assertion pass
325-
se.GetSessionVars().TxnCtx = &variable.TransactionContext{}
326-
se.GetSessionVars().TxnCtx.StartTS = 123
327-
seCtx := se.GetTableCtx()
328-
require.Equal(t, seCtx.ConnectionID(), ctx.ConnectionID())
329-
require.Equal(t, seCtx.InRestrictedSQL(), ctx.InRestrictedSQL())
330-
require.Equal(t, seCtx.TxnAssertionLevel(), ctx.TxnAssertionLevel())
331-
require.Equal(t, seCtx.GetMutateBuffers(), ctx.GetMutateBuffers())
332-
require.Equal(t, seCtx.EnableMutationChecker(), ctx.EnableMutationChecker())
333-
require.Equal(t, seCtx.GetRowEncodingConfig(), ctx.GetRowEncodingConfig())
334-
require.Equal(t, seCtx.GetRowIDShardGenerator().GetShardStep(), ctx.GetRowIDShardGenerator().GetShardStep())
335-
seAlloc, ok := seCtx.GetReservedRowIDAlloc()
336-
require.True(t, ok)
337-
alloc, ok := ctx.GetReservedRowIDAlloc()
338-
require.True(t, ok)
339-
require.Equal(t, seAlloc, alloc)
340-
}
341-
342260
// test for default
343261
tblCtx, err := newLitTableMutateContext(exprCtx, nil)
344262
require.NoError(t, err)
@@ -353,7 +271,6 @@ func TestLitTableMutateContext(t *testing.T) {
353271
g := tblCtx.GetRowIDShardGenerator()
354272
require.NotNil(t, g)
355273
require.Equal(t, variable.DefTiDBShardAllocateStep, g.GetShardStep())
356-
compareWithLegacySession(tblCtx, nil)
357274

358275
// test for load vars
359276
sysVars := map[string]string{
@@ -375,7 +292,6 @@ func TestLitTableMutateContext(t *testing.T) {
375292
require.NotNil(t, g)
376293
require.NotEqual(t, variable.DefTiDBShardAllocateStep, g.GetShardStep())
377294
require.Equal(t, 1234567, g.GetShardStep())
378-
compareWithLegacySession(tblCtx, sysVars)
379295

380296
// test for `RowEncodingConfig.IsRowLevelChecksumEnabled` which should be loaded from global variable.
381297
require.False(t, variable.EnableRowLevelChecksum.Load())
@@ -390,5 +306,4 @@ func TestLitTableMutateContext(t *testing.T) {
390306
IsRowLevelChecksumEnabled: true,
391307
RowEncoder: &rowcodec.Encoder{Enable: true},
392308
}, tblCtx.GetRowEncodingConfig())
393-
compareWithLegacySession(tblCtx, sysVars)
394309
}

pkg/lightning/backend/kv/session.go

Lines changed: 0 additions & 163 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,20 @@ package kv
1919
import (
2020
"context"
2121
"errors"
22-
"fmt"
23-
"strconv"
2422
"sync"
2523

2624
"github.com/docker/go-units"
27-
"github.com/pingcap/tidb/pkg/errctx"
2825
"github.com/pingcap/tidb/pkg/expression/exprctx"
29-
"github.com/pingcap/tidb/pkg/expression/sessionexpr"
30-
infoschema "github.com/pingcap/tidb/pkg/infoschema/context"
3126
"github.com/pingcap/tidb/pkg/kv"
3227
"github.com/pingcap/tidb/pkg/lightning/backend/encode"
3328
"github.com/pingcap/tidb/pkg/lightning/common"
3429
"github.com/pingcap/tidb/pkg/lightning/log"
3530
"github.com/pingcap/tidb/pkg/lightning/manual"
3631
"github.com/pingcap/tidb/pkg/meta/model"
37-
planctx "github.com/pingcap/tidb/pkg/planner/planctx"
38-
"github.com/pingcap/tidb/pkg/sessionctx"
3932
"github.com/pingcap/tidb/pkg/sessionctx/variable"
4033
"github.com/pingcap/tidb/pkg/table/tblctx"
41-
"github.com/pingcap/tidb/pkg/table/tblsession"
4234
"github.com/pingcap/tidb/pkg/types"
4335
"github.com/pingcap/tidb/pkg/util/mathutil"
44-
"github.com/pingcap/tidb/pkg/util/topsql/stmtstats"
4536
"go.uber.org/zap"
4637
)
4738

@@ -282,160 +273,6 @@ func (*transaction) MayFlush() error {
282273
return nil
283274
}
284275

285-
// sessExprContext implements the ExprContext interface
286-
// It embedded an `ExprContext` and a `sessEvalContext` to provide no optional properties.
287-
type sessExprContext struct {
288-
*sessionexpr.ExprContext
289-
evalCtx *sessEvalContext
290-
}
291-
292-
// GetEvalCtx implements the ExprContext.GetEvalCtx interface
293-
func (ctx *sessExprContext) GetEvalCtx() exprctx.EvalContext {
294-
return ctx.evalCtx
295-
}
296-
297-
// sessEvalContext implements the EvalContext interface
298-
// It embedded an `EvalContext` and provide no optional properties.
299-
type sessEvalContext struct {
300-
exprctx.EvalContext
301-
}
302-
303-
// GetOptionalPropSet returns the optional properties provided by this context.
304-
func (*sessEvalContext) GetOptionalPropSet() exprctx.OptionalEvalPropKeySet {
305-
return 0
306-
}
307-
308-
// GetOptionalPropProvider gets the optional property provider by key
309-
func (*sessEvalContext) GetOptionalPropProvider(exprctx.OptionalEvalPropKey) (exprctx.OptionalEvalPropProvider, bool) {
310-
return nil, false
311-
}
312-
313-
// Deprecated: `session` will be removed soon.
314-
// session is a trimmed down Session type which only wraps our own trimmed-down
315-
// transaction type and provides the session variables to the TiDB library
316-
// optimized for Lightning.
317-
// The `session` object is private to make sure it is only used by public `Session` struct to provide limited access.
318-
// TODO: remove `session` and build related context without a mocked `sessionctx.Context` instead.
319-
type session struct {
320-
sessionctx.Context
321-
planctx.EmptyPlanContextExtended
322-
txn transaction
323-
Vars *variable.SessionVars
324-
exprCtx *sessExprContext
325-
tblctx *tblsession.MutateContext
326-
// currently, we only set `CommonAddRecordCtx`
327-
values map[fmt.Stringer]any
328-
}
329-
330-
// Deprecated: this function will be removed soon.
331-
// newSession creates a new trimmed down Session matching the options.
332-
func newSession(options *encode.SessionOptions, logger log.Logger) *session {
333-
s := &session{
334-
values: make(map[fmt.Stringer]any, 1),
335-
}
336-
sqlMode := options.SQLMode
337-
vars := variable.NewSessionVars(s)
338-
vars.SkipUTF8Check = true
339-
vars.StmtCtx.InInsertStmt = true
340-
vars.SQLMode = sqlMode
341-
342-
typeFlags := vars.StmtCtx.TypeFlags().
343-
WithTruncateAsWarning(!sqlMode.HasStrictMode()).
344-
WithIgnoreInvalidDateErr(sqlMode.HasAllowInvalidDatesMode()).
345-
WithIgnoreZeroInDate(!sqlMode.HasStrictMode() || sqlMode.HasAllowInvalidDatesMode() ||
346-
!sqlMode.HasNoZeroInDateMode() || !sqlMode.HasNoZeroDateMode())
347-
vars.StmtCtx.SetTypeFlags(typeFlags)
348-
349-
errLevels := vars.StmtCtx.ErrLevels()
350-
errLevels[errctx.ErrGroupBadNull] = errctx.ResolveErrLevel(false, !sqlMode.HasStrictMode())
351-
errLevels[errctx.ErrGroupDividedByZero] =
352-
errctx.ResolveErrLevel(!sqlMode.HasErrorForDivisionByZeroMode(), !sqlMode.HasStrictMode())
353-
vars.StmtCtx.SetErrLevels(errLevels)
354-
355-
if options.SysVars != nil {
356-
for k, v := range options.SysVars {
357-
// since 6.3(current master) tidb checks whether we can set a system variable
358-
// lc_time_names is a read-only variable for now, but might be implemented later,
359-
// so we not remove it from defaultImportantVariables and check it in below way.
360-
if sv := variable.GetSysVar(k); sv == nil {
361-
logger.DPanic("unknown system var", zap.String("key", k))
362-
continue
363-
} else if sv.ReadOnly {
364-
logger.Debug("skip read-only variable", zap.String("key", k))
365-
continue
366-
}
367-
if err := vars.SetSystemVar(k, v); err != nil {
368-
logger.DPanic("new session: failed to set system var",
369-
log.ShortError(err),
370-
zap.String("key", k))
371-
}
372-
}
373-
}
374-
vars.StmtCtx.SetTimeZone(vars.Location())
375-
if err := vars.SetSystemVar("timestamp", strconv.FormatInt(options.Timestamp, 10)); err != nil {
376-
logger.Warn("new session: failed to set timestamp",
377-
log.ShortError(err))
378-
}
379-
vars.TxnCtx = nil
380-
s.Vars = vars
381-
exprCtx := sessionexpr.NewExprContext(s)
382-
// The exprCtx should be an expression context providing no optional properties in `EvalContext`.
383-
// That is to make sure it only allows expressions that require basic context.
384-
s.exprCtx = &sessExprContext{
385-
ExprContext: exprCtx,
386-
evalCtx: &sessEvalContext{
387-
EvalContext: exprCtx.GetEvalCtx(),
388-
},
389-
}
390-
s.tblctx = tblsession.NewMutateContext(s)
391-
s.txn.kvPairs = &Pairs{}
392-
393-
return s
394-
}
395-
396-
// Txn implements the sessionctx.Context interface
397-
func (se *session) Txn(_ bool) (kv.Transaction, error) {
398-
return &se.txn, nil
399-
}
400-
401-
// GetSessionVars implements the sessionctx.Context interface
402-
func (se *session) GetSessionVars() *variable.SessionVars {
403-
return se.Vars
404-
}
405-
406-
// GetExprCtx returns the expression context of the session.
407-
func (se *session) GetExprCtx() exprctx.ExprContext {
408-
return se.exprCtx
409-
}
410-
411-
// GetTableCtx returns the table.MutateContext
412-
func (se *session) GetTableCtx() tblctx.MutateContext {
413-
return se.tblctx
414-
}
415-
416-
// SetValue saves a value associated with this context for key.
417-
func (se *session) SetValue(key fmt.Stringer, value any) {
418-
se.values[key] = value
419-
}
420-
421-
// Value returns the value associated with this context for key.
422-
func (se *session) Value(key fmt.Stringer) any {
423-
return se.values[key]
424-
}
425-
426-
// StmtAddDirtyTableOP implements the sessionctx.Context interface
427-
func (*session) StmtAddDirtyTableOP(_ int, _ int64, _ kv.Handle) {}
428-
429-
// GetInfoSchema implements the sessionctx.Context interface.
430-
func (*session) GetInfoSchema() infoschema.MetaOnlyInfoSchema {
431-
return nil
432-
}
433-
434-
// GetStmtStats implements the sessionctx.Context interface.
435-
func (*session) GetStmtStats() *stmtstats.StatementStats {
436-
return nil
437-
}
438-
439276
// Session is used to provide context for lightning.
440277
type Session struct {
441278
txn transaction

0 commit comments

Comments
 (0)