From db2776a25226072a711fc0084856f672dd797287 Mon Sep 17 00:00:00 2001 From: tiancaiamao Date: Tue, 17 Dec 2024 19:51:57 +0800 Subject: [PATCH] infoschema: change infoschem v2 btree element from tableItem to *tableItem (#58324) ref pingcap/tidb#58321 --- pkg/infoschema/builder.go | 4 +- pkg/infoschema/infoschema_v2.go | 62 ++++++++++++++-------------- pkg/infoschema/infoschema_v2_test.go | 12 +++--- 3 files changed, 39 insertions(+), 39 deletions(-) diff --git a/pkg/infoschema/builder.go b/pkg/infoschema/builder.go index cd582a72da0a1..281b628e697bd 100644 --- a/pkg/infoschema/builder.go +++ b/pkg/infoschema/builder.go @@ -965,8 +965,8 @@ func (b *Builder) createSchemaTablesForDB(di *model.DBInfo, tableFromMeta tableF tableID: id, schemaVersion: schemaVersion, } - b.infoData.byID.Set(item) - b.infoData.byName.Set(item) + b.infoData.byID.Set(&item) + b.infoData.byName.Set(&item) } } b.addDB(schemaVersion, di, schTbls) diff --git a/pkg/infoschema/infoschema_v2.go b/pkg/infoschema/infoschema_v2.go index f4c6efb79f85a..6135932e4dc27 100644 --- a/pkg/infoschema/infoschema_v2.go +++ b/pkg/infoschema/infoschema_v2.go @@ -85,13 +85,13 @@ type Data struct { // // It means as long as we can find an item in it, the item is available, even through the // schema version maybe smaller than required. - byName *btree.BTreeG[tableItem] + byName *btree.BTreeG[*tableItem] // For the TableByID API, sorted by {tableID, schemaVersion} => dbID // To reload model.TableInfo, we need both table ID and database ID for meta kv API. // It provides the tableID => databaseID mapping. // This mapping MUST be synced with byName. - byID *btree.BTreeG[tableItem] + byID *btree.BTreeG[*tableItem] // For the SchemaByName API, sorted by {dbName, schemaVersion} => model.DBInfo // Stores the full data in memory. @@ -186,8 +186,8 @@ type tableCacheKey struct { // NewData creates an infoschema V2 data struct. func NewData() *Data { ret := &Data{ - byID: btree.NewBTreeG[tableItem](compareByID), - byName: btree.NewBTreeG[tableItem](compareByName), + byID: btree.NewBTreeG[*tableItem](compareByID), + byName: btree.NewBTreeG[*tableItem](compareByName), schemaMap: btree.NewBTreeG[schemaItem](compareSchemaItem), schemaID2Name: btree.NewBTreeG[schemaIDName](compareSchemaByID), tableCache: newSieve[tableCacheKey, table.Table](1024 * 1024 * size.MB), @@ -209,8 +209,8 @@ func (isd *Data) SetCacheCapacity(capacity uint64) { } func (isd *Data) add(item tableItem, tbl table.Table) { - isd.byID.Set(item) - isd.byName.Set(item) + isd.byID.Set(&item) + isd.byName.Set(&item) isd.tableCache.Set(tableCacheKey{item.tableID, item.schemaVersion}, tbl) ti := tbl.Meta() if pi := ti.GetPartitionInfo(); pi != nil { @@ -240,8 +240,8 @@ func (isd *Data) addDB(schemaVersion int64, dbInfo *model.DBInfo) { func (isd *Data) remove(item tableItem) { item.tomb = true - isd.byID.Set(item) - isd.byName.Set(item) + isd.byID.Set(&item) + isd.byName.Set(&item) isd.tableInfoResident.Set(tableInfoItem{ dbName: item.dbName, tableID: item.tableID, @@ -271,7 +271,7 @@ func (isd *Data) resetBeforeFullLoad(schemaVersion int64) { resetPID2TIDBeforeFullLoad(isd.pid2tid, schemaVersion) } -func resetByIDBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64) { +func resetByIDBeforeFullLoad(bt *btree.BTreeG[*tableItem], schemaVersion int64) { pivot, ok := bt.Max() if !ok { return @@ -281,10 +281,10 @@ func resetByIDBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64) { if bt.Len() < batchSize { batchSize = bt.Len() } - items := make([]tableItem, 0, batchSize) + items := make([]*tableItem, 0, batchSize) items = append(items, pivot) for { - bt.Descend(pivot, func(item tableItem) bool { + bt.Descend(pivot, func(item *tableItem) bool { if pivot.tableID == item.tableID { return true // skip MVCC version } @@ -296,7 +296,7 @@ func resetByIDBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64) { break } for _, item := range items { - bt.Set(tableItem{ + bt.Set(&tableItem{ dbName: item.dbName, dbID: item.dbID, tableName: item.tableName, @@ -309,7 +309,7 @@ func resetByIDBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64) { } } -func resetByNameBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64) { +func resetByNameBeforeFullLoad(bt *btree.BTreeG[*tableItem], schemaVersion int64) { pivot, ok := bt.Max() if !ok { return @@ -319,10 +319,10 @@ func resetByNameBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64) if bt.Len() < batchSize { batchSize = bt.Len() } - items := make([]tableItem, 0, batchSize) + items := make([]*tableItem, 0, batchSize) items = append(items, pivot) for { - bt.Descend(pivot, func(item tableItem) bool { + bt.Descend(pivot, func(item *tableItem) bool { if pivot.dbName == item.dbName && pivot.tableName == item.tableName { return true // skip MVCC version } @@ -334,7 +334,7 @@ func resetByNameBeforeFullLoad(bt *btree.BTreeG[tableItem], schemaVersion int64) break } for _, item := range items { - bt.Set(tableItem{ + bt.Set(&tableItem{ dbName: item.dbName, dbID: item.dbID, tableName: item.tableName, @@ -457,7 +457,7 @@ func resetPID2TIDBeforeFullLoad(bt *btree.BTreeG[partitionItem], schemaVersion i } } -func compareByID(a, b tableItem) bool { +func compareByID(a, b *tableItem) bool { if a.tableID < b.tableID { return true } @@ -468,7 +468,7 @@ func compareByID(a, b tableItem) bool { return a.schemaVersion < b.schemaVersion } -func compareByName(a, b tableItem) bool { +func compareByName(a, b *tableItem) bool { if a.dbName.L < b.dbName.L { return true } @@ -553,12 +553,12 @@ func NewInfoSchemaV2(r autoid.Requirement, factory func() (pools.Resource, error } } -func search(bt *btree.BTreeG[tableItem], schemaVersion int64, end tableItem, matchFn func(a, b *tableItem) bool) (tableItem, bool) { +func search(bt *btree.BTreeG[*tableItem], schemaVersion int64, end tableItem, matchFn func(a, b *tableItem) bool) (*tableItem, bool) { var ok bool - var target tableItem + var target *tableItem // Iterate through the btree, find the query item whose schema version is the largest one (latest). - bt.Descend(end, func(item tableItem) bool { - if !matchFn(&end, &item) { + bt.Descend(&end, func(item *tableItem) bool { + if !matchFn(&end, item) { return false } if item.schemaVersion > schemaVersion { @@ -594,7 +594,7 @@ func (is *infoschemaV2) CloneAndUpdateTS(startTS uint64) *infoschemaV2 { return &tmp } -func (is *infoschemaV2) searchTableItemByID(tableID int64) (tableItem, bool) { +func (is *infoschemaV2) searchTableItemByID(tableID int64) (*tableItem, bool) { eq := func(a, b *tableItem) bool { return a.tableID == b.tableID } return search( is.byID, @@ -679,7 +679,7 @@ func (is *infoschemaV2) IterateAllTableItems(visit func(TableItem) bool) { return } var pivot *tableItem - is.byName.Descend(maxv, func(item tableItem) bool { + is.byName.Descend(maxv, func(item *tableItem) bool { if item.schemaVersion > is.schemaMetaVersion { // skip MVCC version, those items are not visible to the queried schema version return true @@ -688,7 +688,7 @@ func (is *infoschemaV2) IterateAllTableItems(visit func(TableItem) bool) { // skip MVCC version, this db.table has been visited already return true } - pivot = &item + pivot = item if !item.tomb { return visit(TableItem{DBName: item.dbName, TableName: item.tableName}) } @@ -743,10 +743,10 @@ type tableByNameHelper struct { end tableItem schemaVersion int64 found bool - res tableItem + res *tableItem } -func (h *tableByNameHelper) onItem(item tableItem) bool { +func (h *tableByNameHelper) onItem(item *tableItem) bool { if item.dbName.L != h.end.dbName.L || item.tableName.L != h.end.tableName.L { h.found = false return false @@ -779,7 +779,7 @@ func (is *infoschemaV2) TableByName(ctx context.Context, schema, tbl pmodel.CISt var h tableByNameHelper h.end = tableItem{dbName: schema, tableName: tbl, schemaVersion: math.MaxInt64} h.schemaVersion = is.infoSchema.schemaMetaVersion - is.byName.Descend(h.end, h.onItem) + is.byName.Descend(&h.end, h.onItem) if !h.found { return nil, ErrTableNotExists.FastGenByArgs(schema, tbl) @@ -907,8 +907,8 @@ func (is *infoschemaV2) SchemaSimpleTableInfos(ctx context.Context, schema pmode // Ascend is much more difficult than Descend. // So the data is taken out first and then dedup in Descend order. - var tableItems []tableItem - is.byName.Ascend(tableItem{dbName: schema}, func(item tableItem) bool { + var tableItems []*tableItem + is.byName.Ascend(&tableItem{dbName: schema}, func(item *tableItem) bool { if item.dbName.L != schema.L { return false } @@ -923,7 +923,7 @@ func (is *infoschemaV2) SchemaSimpleTableInfos(ctx context.Context, schema pmode tblInfos := make([]*model.TableNameInfo, 0, len(tableItems)) var curr *tableItem for i := len(tableItems) - 1; i >= 0; i-- { - item := &tableItems[i] + item := tableItems[i] if curr == nil || curr.tableName != tableItems[i].tableName { curr = item if !item.tomb { diff --git a/pkg/infoschema/infoschema_v2_test.go b/pkg/infoschema/infoschema_v2_test.go index 20d6f68d0a676..208b9727dda9f 100644 --- a/pkg/infoschema/infoschema_v2_test.go +++ b/pkg/infoschema/infoschema_v2_test.go @@ -653,10 +653,10 @@ func TestDataStructFieldsCorrectnessInSchemaChange(t *testing.T) { require.NoError(t, err) _, err = builder.ApplyDiff(meta.NewMutator(txn), &model.SchemaDiff{Type: model.ActionCreateTable, Version: 2, SchemaID: dbInfo.ID, TableID: tblInfo.ID}) require.NoError(t, err) - tblItem, ok := v2.Data.byName.Get(tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 2}) + tblItem, ok := v2.Data.byName.Get(&tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 2}) require.True(t, ok) require.Equal(t, tblItem.tableID, tblInfo.ID) - tblItem, ok = v2.Data.byID.Get(tableItem{tableID: tblInfo.ID, schemaVersion: 2}) + tblItem, ok = v2.Data.byID.Get(&tableItem{tableID: tblInfo.ID, schemaVersion: 2}) require.True(t, ok) require.Equal(t, tblItem.dbID, dbInfo.ID) tbl, ok := v2.Data.tableCache.Get(tableCacheKey{tableID: tblInfo.ID, schemaVersion: 2}) @@ -699,10 +699,10 @@ func TestDataStructFieldsCorrectnessInSchemaChange(t *testing.T) { _, err = builder.ApplyDiff(m, &model.SchemaDiff{Type: model.ActionDropTable, Version: 5, SchemaID: dbInfo.ID, TableID: tblInfo.ID}) require.NoError(t, err) // at first, the table will not be removed - tblItem, ok = v2.Data.byName.Get(tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 5}) + tblItem, ok = v2.Data.byName.Get(&tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 5}) require.True(t, ok) require.False(t, tblItem.tomb) - tblItem, ok = v2.Data.byID.Get(tableItem{tableID: tblInfo.ID, schemaVersion: 5}) + tblItem, ok = v2.Data.byID.Get(&tableItem{tableID: tblInfo.ID, schemaVersion: 5}) require.True(t, ok) require.False(t, tblItem.tomb) _, ok = v2.Data.tableCache.Get(tableCacheKey{tableID: tblInfo.ID, schemaVersion: 5}) @@ -716,10 +716,10 @@ func TestDataStructFieldsCorrectnessInSchemaChange(t *testing.T) { _, err = builder.ApplyDiff(m, &model.SchemaDiff{Type: model.ActionDropTable, Version: 5, SchemaID: dbInfo.ID, TableID: tblInfo.ID}) require.NoError(t, err) // at first, the table will not be removed - tblItem, ok = v2.Data.byName.Get(tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 5}) + tblItem, ok = v2.Data.byName.Get(&tableItem{dbName: dbInfo.Name, tableName: tblInfo.Name, schemaVersion: 5}) require.True(t, ok) require.True(t, tblItem.tomb) - tblItem, ok = v2.Data.byID.Get(tableItem{tableID: tblInfo.ID, schemaVersion: 5}) + tblItem, ok = v2.Data.byID.Get(&tableItem{tableID: tblInfo.ID, schemaVersion: 5}) require.True(t, ok) require.True(t, tblItem.tomb) _, ok = v2.Data.tableCache.Get(tableCacheKey{tableID: tblInfo.ID, schemaVersion: 5})