|
15 | 15 | package infoschema_test
|
16 | 16 |
|
17 | 17 | import (
|
| 18 | + "fmt" |
18 | 19 | "testing"
|
19 | 20 |
|
20 | 21 | "github.com/pingcap/tidb/infoschema"
|
@@ -211,3 +212,82 @@ func TestReSize(t *testing.T) {
|
211 | 212 | require.Nil(t, ic.GetByVersion(3))
|
212 | 213 | require.Equal(t, is4, ic.GetByVersion(4))
|
213 | 214 | }
|
| 215 | + |
| 216 | +func TestCacheWithSchemaTsZero(t *testing.T) { |
| 217 | + ic := infoschema.NewCache(16) |
| 218 | + require.NotNil(t, ic) |
| 219 | + |
| 220 | + for i := 1; i <= 8; i++ { |
| 221 | + ic.Insert(infoschema.MockInfoSchemaWithSchemaVer(nil, int64(i)), uint64(i)) |
| 222 | + } |
| 223 | + |
| 224 | + checkFn := func(start, end int64, exist bool) { |
| 225 | + require.True(t, start <= end) |
| 226 | + latestSchemaVersion := ic.GetLatest().SchemaMetaVersion() |
| 227 | + for ts := start; ts <= end; ts++ { |
| 228 | + is := ic.GetBySnapshotTS(uint64(ts)) |
| 229 | + if exist { |
| 230 | + require.NotNil(t, is, fmt.Sprintf("ts %d", ts)) |
| 231 | + if ts > latestSchemaVersion { |
| 232 | + require.Equal(t, latestSchemaVersion, is.SchemaMetaVersion(), fmt.Sprintf("ts %d", ts)) |
| 233 | + } else { |
| 234 | + require.Equal(t, ts, is.SchemaMetaVersion(), fmt.Sprintf("ts %d", ts)) |
| 235 | + } |
| 236 | + } else { |
| 237 | + require.Nil(t, is, fmt.Sprintf("ts %d", ts)) |
| 238 | + } |
| 239 | + } |
| 240 | + } |
| 241 | + checkFn(1, 8, true) |
| 242 | + checkFn(8, 10, true) |
| 243 | + |
| 244 | + // mock for meet error There is no Write MVCC info for the schema version |
| 245 | + ic.Insert(infoschema.MockInfoSchemaWithSchemaVer(nil, 9), 0) |
| 246 | + checkFn(1, 7, true) |
| 247 | + checkFn(8, 9, false) |
| 248 | + checkFn(9, 10, false) |
| 249 | + |
| 250 | + for i := 10; i <= 16; i++ { |
| 251 | + ic.Insert(infoschema.MockInfoSchemaWithSchemaVer(nil, int64(i)), uint64(i)) |
| 252 | + checkFn(1, 7, true) |
| 253 | + checkFn(8, 9, false) |
| 254 | + checkFn(10, 16, true) |
| 255 | + } |
| 256 | + require.Equal(t, 16, ic.Size()) |
| 257 | + |
| 258 | + // refill the cache |
| 259 | + ic.Insert(infoschema.MockInfoSchemaWithSchemaVer(nil, 9), 9) |
| 260 | + checkFn(1, 16, true) |
| 261 | + require.Equal(t, 16, ic.Size()) |
| 262 | + |
| 263 | + // Test more than capacity |
| 264 | + ic.Insert(infoschema.MockInfoSchemaWithSchemaVer(nil, 17), 17) |
| 265 | + checkFn(1, 1, false) |
| 266 | + checkFn(2, 17, true) |
| 267 | + checkFn(2, 20, true) |
| 268 | + require.Equal(t, 16, ic.Size()) |
| 269 | + |
| 270 | + // Test for there is a hole in the middle. |
| 271 | + ic = infoschema.NewCache(16) |
| 272 | + |
| 273 | + // mock for restart with full load the latest version schema. |
| 274 | + ic.Insert(infoschema.MockInfoSchemaWithSchemaVer(nil, 100), 100) |
| 275 | + checkFn(1, 99, false) |
| 276 | + checkFn(100, 100, true) |
| 277 | + |
| 278 | + for i := 1; i <= 16; i++ { |
| 279 | + ic.Insert(infoschema.MockInfoSchemaWithSchemaVer(nil, int64(i)), uint64(i)) |
| 280 | + } |
| 281 | + checkFn(1, 1, false) |
| 282 | + checkFn(2, 15, true) |
| 283 | + checkFn(16, 16, false) |
| 284 | + checkFn(100, 100, true) |
| 285 | + require.Equal(t, 16, ic.Size()) |
| 286 | + |
| 287 | + for i := 85; i < 100; i++ { |
| 288 | + ic.Insert(infoschema.MockInfoSchemaWithSchemaVer(nil, int64(i)), uint64(i)) |
| 289 | + } |
| 290 | + checkFn(1, 84, false) |
| 291 | + checkFn(85, 100, true) |
| 292 | + require.Equal(t, 16, ic.Size()) |
| 293 | +} |
0 commit comments