@@ -17,6 +17,7 @@ package ddl
17
17
import (
18
18
"bytes"
19
19
"testing"
20
+ "time"
20
21
21
22
"github.com/pingcap/tidb/pkg/ddl/ingest"
22
23
distsqlctx "github.com/pingcap/tidb/pkg/distsql/context"
@@ -27,6 +28,8 @@ import (
27
28
"github.com/pingcap/tidb/pkg/parser/mysql"
28
29
"github.com/pingcap/tidb/pkg/privilege"
29
30
"github.com/pingcap/tidb/pkg/sessionctx"
31
+ "github.com/pingcap/tidb/pkg/sessionctx/variable"
32
+ "github.com/pingcap/tidb/pkg/table"
30
33
"github.com/pingcap/tidb/pkg/types"
31
34
contextutil "github.com/pingcap/tidb/pkg/util/context"
32
35
"github.com/pingcap/tidb/pkg/util/deeptest"
@@ -224,13 +227,25 @@ func assertStaticExprContextEqual(t *testing.T, sctx sessionctx.Context, exprCtx
224
227
)
225
228
}
226
229
230
+ // newMockReorgSessCtx creates a mock session context for reorg test.
231
+ // In old implementations, DDL is using `mock.Context` to construct the contexts used in DDL reorg.
232
+ // After refactoring, we just need it to do test the new implementation is compatible with the old one.
233
+ func newMockReorgSessCtx (store kv.Storage ) sessionctx.Context {
234
+ c := mock .NewContext ()
235
+ c .Store = store
236
+ c .GetSessionVars ().SetStatusFlag (mysql .ServerStatusAutocommit , false )
237
+ tz := * time .UTC
238
+ c .ResetSessionAndStmtTimeZone (& tz )
239
+ return c
240
+ }
241
+
227
242
// TestReorgExprContext is used in refactor stage to make sure the newReorgExprCtx() is
228
- // compatible with newReorgSessCtx (nil).GetExprCtx() to make it safe to replace `mock.Context` usage.
243
+ // compatible with newMockReorgSessCtx (nil).GetExprCtx() to make it safe to replace `mock.Context` usage.
229
244
// After refactor, the TestReorgExprContext can be removed.
230
245
func TestReorgExprContext (t * testing.T ) {
231
246
// test default expr context
232
247
store := & mockStorage {client : & mock.Client {}}
233
- sctx := newReorgSessCtx (store )
248
+ sctx := newMockReorgSessCtx (store )
234
249
defaultCtx := newReorgExprCtx ()
235
250
// should use an empty static warn handler by default
236
251
evalCtx := defaultCtx .GetStaticEvalCtx ()
@@ -255,7 +270,7 @@ func TestReorgExprContext(t *testing.T) {
255
270
ResourceGroupName : "rg2" ,
256
271
},
257
272
} {
258
- sctx = newReorgSessCtx (store )
273
+ sctx = newMockReorgSessCtx (store )
259
274
require .NoError (t , initSessCtx (sctx , & reorg ))
260
275
ctx , err := newReorgExprCtxWithReorgMeta (& reorg , sctx .GetSessionVars ().StmtCtx .WarnHandler )
261
276
require .NoError (t , err )
@@ -278,6 +293,68 @@ func TestReorgExprContext(t *testing.T) {
278
293
}
279
294
}
280
295
296
+ func TestReorgTableMutateContext (t * testing.T ) {
297
+ originalRowFmt := variable .GetDDLReorgRowFormat ()
298
+ defer variable .SetDDLReorgRowFormat (originalRowFmt )
299
+
300
+ exprCtx := contextstatic .NewStaticExprContext ()
301
+
302
+ assertTblCtxMatchSessionCtx := func (ctx table.MutateContext , sctx sessionctx.Context ) {
303
+ sctxTblCtx := sctx .GetTableCtx ()
304
+ require .Equal (t , uint64 (0 ), ctx .ConnectionID ())
305
+ require .Equal (t , sctxTblCtx .ConnectionID (), ctx .ConnectionID ())
306
+
307
+ require .False (t , ctx .InRestrictedSQL ())
308
+ require .Equal (t , sctxTblCtx .InRestrictedSQL (), ctx .InRestrictedSQL ())
309
+
310
+ require .Equal (t , variable .AssertionLevelOff , ctx .TxnAssertionLevel ())
311
+ require .Equal (t , sctxTblCtx .TxnAssertionLevel (), ctx .TxnAssertionLevel ())
312
+
313
+ require .Equal (t , variable .GetDDLReorgRowFormat () != variable .DefTiDBRowFormatV1 , ctx .GetRowEncodingConfig ().IsRowLevelChecksumEnabled )
314
+ require .Equal (t , variable .GetDDLReorgRowFormat () != variable .DefTiDBRowFormatV1 , ctx .GetRowEncodingConfig ().RowEncoder .Enable )
315
+ require .Equal (t , sctxTblCtx .GetRowEncodingConfig (), ctx .GetRowEncodingConfig ())
316
+
317
+ require .NotNil (t , ctx .GetMutateBuffers ())
318
+ require .Equal (t , sctxTblCtx .GetMutateBuffers (), ctx .GetMutateBuffers ())
319
+
320
+ require .Equal (t , variable .DefTiDBShardAllocateStep , ctx .GetRowIDShardGenerator ().GetShardStep ())
321
+ sctx .GetSessionVars ().TxnCtx .StartTS = 123 // make sure GetRowIDShardGenerator() pass assert
322
+ require .Equal (t , sctxTblCtx .GetRowIDShardGenerator ().GetShardStep (), ctx .GetRowIDShardGenerator ().GetShardStep ())
323
+ require .GreaterOrEqual (t , ctx .GetRowIDShardGenerator ().GetCurrentShard (1 ), int64 (0 ))
324
+
325
+ alloc1 , ok := sctxTblCtx .GetReservedRowIDAlloc ()
326
+ require .True (t , ok )
327
+ alloc2 , ok := ctx .GetReservedRowIDAlloc ()
328
+ require .True (t , ok )
329
+ require .Equal (t , alloc1 , alloc2 )
330
+ require .True (t , alloc2 .Exhausted ())
331
+
332
+ binlog , ok := ctx .GetBinlogSupport ()
333
+ require .False (t , ok )
334
+ require .Nil (t , binlog )
335
+ statistics , ok := ctx .GetStatisticsSupport ()
336
+ require .False (t , ok )
337
+ require .Nil (t , statistics )
338
+ cached , ok := ctx .GetCachedTableSupport ()
339
+ require .False (t , ok )
340
+ require .Nil (t , cached )
341
+ temp , ok := ctx .GetTemporaryTableSupport ()
342
+ require .False (t , ok )
343
+ require .Nil (t , temp )
344
+ dml , ok := ctx .GetExchangePartitionDMLSupport ()
345
+ require .False (t , ok )
346
+ require .Nil (t , dml )
347
+ }
348
+
349
+ // test when the row format is v1
350
+ variable .SetDDLReorgRowFormat (variable .DefTiDBRowFormatV1 )
351
+ sctx := newMockReorgSessCtx (& mockStorage {client : & mock.Client {}})
352
+ require .NoError (t , initSessCtx (sctx , & model.DDLReorgMeta {}))
353
+ ctx := newReorgTableMutateContext (exprCtx )
354
+ require .Same (t , exprCtx , ctx .GetExprCtx ())
355
+ assertTblCtxMatchSessionCtx (ctx , sctx )
356
+ }
357
+
281
358
type mockStorage struct {
282
359
kv.Storage
283
360
client kv.Client
@@ -313,13 +390,13 @@ func assertDistSQLCtxEqual(t *testing.T, expected *distsqlctx.DistSQLContext, ac
313
390
}
314
391
315
392
// TestReorgExprContext is used in refactor stage to make sure the newDefaultReorgDistSQLCtx() is
316
- // compatible with newReorgSessCtx (nil).GetDistSQLCtx() to make it safe to replace `mock.Context` usage.
393
+ // compatible with newMockReorgSessCtx (nil).GetDistSQLCtx() to make it safe to replace `mock.Context` usage.
317
394
// After refactor, the TestReorgExprContext can be removed.
318
395
func TestReorgDistSQLCtx (t * testing.T ) {
319
396
store := & mockStorage {client : & mock.Client {}}
320
397
321
398
// test default dist sql context
322
- expected := newReorgSessCtx (store ).GetDistSQLCtx ()
399
+ expected := newMockReorgSessCtx (store ).GetDistSQLCtx ()
323
400
defaultCtx := newDefaultReorgDistSQLCtx (store .client , expected .WarnHandler )
324
401
assertDistSQLCtxEqual (t , expected , defaultCtx )
325
402
@@ -339,7 +416,7 @@ func TestReorgDistSQLCtx(t *testing.T) {
339
416
ResourceGroupName : "rg2" ,
340
417
},
341
418
} {
342
- sctx := newReorgSessCtx (store )
419
+ sctx := newMockReorgSessCtx (store )
343
420
require .NoError (t , initSessCtx (sctx , & reorg ))
344
421
expected = sctx .GetDistSQLCtx ()
345
422
ctx , err := newReorgDistSQLCtxWithReorgMeta (store .client , & reorg , expected .WarnHandler )
0 commit comments