diff --git a/domain/domain.go b/domain/domain.go index 6559e694a82a3..f57f769509966 100644 --- a/domain/domain.go +++ b/domain/domain.go @@ -186,6 +186,9 @@ func (do *Domain) loadInfoSchema(startTS uint64) (infoschema.InfoSchema, bool, i } if is := do.infoCache.GetByVersion(neededSchemaVersion); is != nil { + // try to insert here as well to correct the schemaTs if previous is wrong + // the insert method check if schemaTs is zero + do.infoCache.Insert(is, uint64(schemaTs)) return is, true, 0, nil, nil } diff --git a/infoschema/cache.go b/infoschema/cache.go index fd114209ddbb9..eb9fbc6c4857b 100644 --- a/infoschema/cache.go +++ b/infoschema/cache.go @@ -73,6 +73,11 @@ func (h *InfoCache) GetLatest() InfoSchema { return nil } +// Len returns the size of the cache +func (h *InfoCache) Len() int { + return len(h.cache) +} + func (h *InfoCache) getSchemaByTimestampNoLock(ts uint64) (InfoSchema, bool) { logutil.BgLogger().Debug("SCHEMA CACHE get schema", zap.Uint64("timestamp", ts)) // search one by one instead of binary search, because the timestamp of a schema could be 0 diff --git a/infoschema/cache_test.go b/infoschema/cache_test.go index b05ffa531fae5..60aad8bbdd985 100644 --- a/infoschema/cache_test.go +++ b/infoschema/cache_test.go @@ -137,12 +137,14 @@ func TestGetByTimestamp(t *testing.T) { ic := infoschema.NewCache(16) require.NotNil(t, ic) require.Nil(t, ic.GetLatest()) + require.Equal(t, 0, ic.Len()) is1 := infoschema.MockInfoSchemaWithSchemaVer(nil, 1) ic.Insert(is1, 1) require.Nil(t, ic.GetBySnapshotTS(0)) require.Equal(t, is1, ic.GetBySnapshotTS(1)) require.Equal(t, is1, ic.GetBySnapshotTS(2)) + require.Equal(t, 1, ic.Len()) is3 := infoschema.MockInfoSchemaWithSchemaVer(nil, 3) ic.Insert(is3, 3) @@ -152,6 +154,7 @@ func TestGetByTimestamp(t *testing.T) { require.Nil(t, ic.GetBySnapshotTS(2)) require.Equal(t, is3, ic.GetBySnapshotTS(3)) require.Equal(t, is3, ic.GetBySnapshotTS(4)) + require.Equal(t, 2, ic.Len()) is2 := infoschema.MockInfoSchemaWithSchemaVer(nil, 2) // schema version 2 doesn't have timestamp set @@ -164,4 +167,13 @@ func TestGetByTimestamp(t *testing.T) { require.Nil(t, ic.GetBySnapshotTS(2)) require.Equal(t, is3, ic.GetBySnapshotTS(3)) require.Equal(t, is3, ic.GetBySnapshotTS(4)) + require.Equal(t, 3, ic.Len()) + + // insert is2 again with correct timestamp, to correct previous wrong timestamp + ic.Insert(is2, 2) + require.Equal(t, is3, ic.GetLatest()) + require.Equal(t, is1, ic.GetBySnapshotTS(1)) + require.Equal(t, is2, ic.GetBySnapshotTS(2)) + require.Equal(t, is3, ic.GetBySnapshotTS(3)) + require.Equal(t, 3, ic.Len()) }