Skip to content

Commit

Permalink
schema cache: fix wrong timestamp in schema cache (pingcap#46326)
Browse files Browse the repository at this point in the history
  • Loading branch information
hihihuhu authored and crazycs520 committed Sep 1, 2023
1 parent 298bb31 commit d86325d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
3 changes: 3 additions & 0 deletions domain/domain.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
5 changes: 5 additions & 0 deletions infoschema/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions infoschema/cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand All @@ -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())
}

0 comments on commit d86325d

Please sign in to comment.