@@ -292,3 +292,129 @@ func TestCacheWithSchemaTsZero(t *testing.T) {
292
292
checkFn (85 , 100 , true )
293
293
require .Equal (t , 16 , ic .Size ())
294
294
}
295
+
296
+ func TestCacheWithSchemaTsZero (t * testing.T ) {
297
+ ic := infoschema .NewCache (16 )
298
+ require .NotNil (t , ic )
299
+
300
+ for i := 1 ; i <= 8 ; i ++ {
301
+ ic .Insert (infoschema .MockInfoSchemaWithSchemaVer (nil , int64 (i )), uint64 (i ))
302
+ }
303
+
304
+ checkFn := func (start , end int64 , exist bool ) {
305
+ require .True (t , start <= end )
306
+ latestSchemaVersion := ic .GetLatest ().SchemaMetaVersion ()
307
+ for ts := start ; ts <= end ; ts ++ {
308
+ is := ic .GetBySnapshotTS (uint64 (ts ))
309
+ if exist {
310
+ require .NotNil (t , is , fmt .Sprintf ("ts %d" , ts ))
311
+ if ts > latestSchemaVersion {
312
+ require .Equal (t , latestSchemaVersion , is .SchemaMetaVersion (), fmt .Sprintf ("ts %d" , ts ))
313
+ } else {
314
+ require .Equal (t , ts , is .SchemaMetaVersion (), fmt .Sprintf ("ts %d" , ts ))
315
+ }
316
+ } else {
317
+ require .Nil (t , is , fmt .Sprintf ("ts %d" , ts ))
318
+ }
319
+ }
320
+ }
321
+ checkFn (1 , 8 , true )
322
+ checkFn (8 , 10 , true )
323
+
324
+ // mock for meet error There is no Write MVCC info for the schema version
325
+ ic .Insert (infoschema .MockInfoSchemaWithSchemaVer (nil , 9 ), 0 )
326
+ checkFn (1 , 7 , true )
327
+ checkFn (8 , 9 , false )
328
+ checkFn (9 , 10 , false )
329
+
330
+ for i := 10 ; i <= 16 ; i ++ {
331
+ ic .Insert (infoschema .MockInfoSchemaWithSchemaVer (nil , int64 (i )), uint64 (i ))
332
+ checkFn (1 , 7 , true )
333
+ checkFn (8 , 9 , false )
334
+ checkFn (10 , 16 , true )
335
+ }
336
+ require .Equal (t , 16 , ic .Size ())
337
+
338
+ // refill the cache
339
+ ic .Insert (infoschema .MockInfoSchemaWithSchemaVer (nil , 9 ), 9 )
340
+ checkFn (1 , 16 , true )
341
+ require .Equal (t , 16 , ic .Size ())
342
+
343
+ // Test more than capacity
344
+ ic .Insert (infoschema .MockInfoSchemaWithSchemaVer (nil , 17 ), 17 )
345
+ checkFn (1 , 1 , false )
346
+ checkFn (2 , 17 , true )
347
+ checkFn (2 , 20 , true )
348
+ require .Equal (t , 16 , ic .Size ())
349
+
350
+ // Test for there is a hole in the middle.
351
+ ic = infoschema .NewCache (16 )
352
+
353
+ // mock for restart with full load the latest version schema.
354
+ ic .Insert (infoschema .MockInfoSchemaWithSchemaVer (nil , 100 ), 100 )
355
+ checkFn (1 , 99 , false )
356
+ checkFn (100 , 100 , true )
357
+
358
+ for i := 1 ; i <= 16 ; i ++ {
359
+ ic .Insert (infoschema .MockInfoSchemaWithSchemaVer (nil , int64 (i )), uint64 (i ))
360
+ }
361
+ checkFn (1 , 1 , false )
362
+ checkFn (2 , 15 , true )
363
+ checkFn (16 , 16 , false )
364
+ checkFn (100 , 100 , true )
365
+ require .Equal (t , 16 , ic .Size ())
366
+
367
+ for i := 85 ; i < 100 ; i ++ {
368
+ ic .Insert (infoschema .MockInfoSchemaWithSchemaVer (nil , int64 (i )), uint64 (i ))
369
+ }
370
+ checkFn (1 , 84 , false )
371
+ checkFn (85 , 100 , true )
372
+ require .Equal (t , 16 , ic .Size ())
373
+
374
+ // Test cache with schema version hole, which is cause by schema version doesn't has related schema-diff.
375
+ ic = infoschema .NewCache (16 )
376
+ require .NotNil (t , ic )
377
+ for i := 1 ; i <= 8 ; i ++ {
378
+ ic .Insert (infoschema .MockInfoSchemaWithSchemaVer (nil , int64 (i )), uint64 (i ))
379
+ }
380
+ checkFn (1 , 10 , true )
381
+ // mock for schema version hole, schema-version 9 is missing.
382
+ ic .Insert (infoschema .MockInfoSchemaWithSchemaVer (nil , 10 ), 10 )
383
+ checkFn (1 , 7 , true )
384
+ // without empty schema version map, get snapshot by ts 8, 9 will both failed.
385
+ checkFn (8 , 9 , false )
386
+ checkFn (10 , 10 , true )
387
+ // add empty schema version 9.
388
+ ic .InsertEmptySchemaVersion (9 )
389
+ // after set empty schema version, get snapshot by ts 8, 9 will both success.
390
+ checkFn (1 , 8 , true )
391
+ checkFn (10 , 10 , true )
392
+ is := ic .GetBySnapshotTS (uint64 (9 ))
393
+ require .NotNil (t , is )
394
+ // since schema version 9 is empty, so get by ts 9 will get schema which version is 8.
395
+ require .Equal (t , int64 (8 ), is .SchemaMetaVersion ())
396
+ }
397
+
398
+ func TestCacheEmptySchemaVersion (t * testing.T ) {
399
+ ic := infoschema .NewCache (16 )
400
+ require .NotNil (t , ic )
401
+ require .Equal (t , 0 , len (ic .GetEmptySchemaVersions ()))
402
+ for i := 0 ; i < 16 ; i ++ {
403
+ ic .InsertEmptySchemaVersion (int64 (i ))
404
+ }
405
+ emptyVersions := ic .GetEmptySchemaVersions ()
406
+ require .Equal (t , 16 , len (emptyVersions ))
407
+ for i := 0 ; i < 16 ; i ++ {
408
+ _ , ok := emptyVersions [int64 (i )]
409
+ require .True (t , ok )
410
+ }
411
+ for i := 16 ; i < 20 ; i ++ {
412
+ ic .InsertEmptySchemaVersion (int64 (i ))
413
+ }
414
+ emptyVersions = ic .GetEmptySchemaVersions ()
415
+ require .Equal (t , 16 , len (emptyVersions ))
416
+ for i := 4 ; i < 20 ; i ++ {
417
+ _ , ok := emptyVersions [int64 (i )]
418
+ require .True (t , ok )
419
+ }
420
+ }
0 commit comments