diff --git a/pkg/infoschema/builder.go b/pkg/infoschema/builder.go index 067ad24c04c83..445348609f802 100644 --- a/pkg/infoschema/builder.go +++ b/pkg/infoschema/builder.go @@ -531,7 +531,7 @@ func (b *Builder) applyTableUpdate(m *meta.Meta, diff *model.SchemaDiff) ([]int6 // TODO: Check how this would work with ADD/REMOVE Partitioning, // which may have AutoID not connected to tableID // TODO: can there be _tidb_rowid AutoID per partition? - oldAllocs, _ := b.is.AllocByID(oldTableID) + oldAllocs, _ := allocByID(b.is, oldTableID) allocs = filterAllocators(diff, oldAllocs) } @@ -1149,12 +1149,14 @@ func NewBuilder(r autoid.Requirement, factory func() (pools.Resource, error)) *B return &Builder{ Requirement: r, is: &infoSchema{ - schemaMap: map[string]*schemaTables{}, - policyMap: map[string]*model.PolicyInfo{}, - resourceGroupMap: map[string]*model.ResourceGroupInfo{}, - ruleBundleMap: map[int64]*placement.Bundle{}, - sortedTablesBuckets: make([]sortedTables, bucketCount), - referredForeignKeyMap: make(map[SchemaAndTableName][]*model.ReferredFKInfo), + infoSchemaMisc: infoSchemaMisc{ + policyMap: map[string]*model.PolicyInfo{}, + resourceGroupMap: map[string]*model.ResourceGroupInfo{}, + ruleBundleMap: map[int64]*placement.Bundle{}, + referredForeignKeyMap: make(map[SchemaAndTableName][]*model.ReferredFKInfo), + }, + schemaMap: map[string]*schemaTables{}, + sortedTablesBuckets: make([]sortedTables, bucketCount), }, dirtyDB: make(map[string]bool), factory: factory, diff --git a/pkg/infoschema/infoschema.go b/pkg/infoschema/infoschema.go index 5ba5652fc7398..4eb6002b7772c 100644 --- a/pkg/infoschema/infoschema.go +++ b/pkg/infoschema/infoschema.go @@ -35,22 +35,24 @@ import ( // InfoSchema is the interface used to retrieve the schema information. // It works as a in memory cache and doesn't handle any schema change. // InfoSchema is read-only, and the returned value is a copy. -// TODO: add more methods to retrieve tables and columns. type InfoSchema interface { SchemaByName(schema model.CIStr) (*model.DBInfo, bool) SchemaExists(schema model.CIStr) bool TableByName(schema, table model.CIStr) (table.Table, error) TableExists(schema, table model.CIStr) bool SchemaByID(id int64) (*model.DBInfo, bool) - PolicyByName(name model.CIStr) (*model.PolicyInfo, bool) - ResourceGroupByName(name model.CIStr) (*model.ResourceGroupInfo, bool) TableByID(id int64) (table.Table, bool) - AllocByID(id int64) (autoid.Allocators, bool) AllSchemas() []*model.DBInfo - Clone() (result []*model.DBInfo) SchemaTables(schema model.CIStr) []table.Table SchemaMetaVersion() int64 FindTableByPartitionID(partitionID int64) (table.Table, *model.DBInfo, *model.PartitionDefinition) + Misc +} + +// Misc contains the methods that are not closely related to InfoSchema. +type Misc interface { + PolicyByName(name model.CIStr) (*model.PolicyInfo, bool) + ResourceGroupByName(name model.CIStr) (*model.ResourceGroupInfo, bool) // PlacementBundleByPhysicalTableID is used to get a rule bundle. PlacementBundleByPhysicalTableID(id int64) (*placement.Bundle, bool) // AllPlacementBundles is used to get all placement bundles @@ -65,6 +67,8 @@ type InfoSchema interface { GetTableReferredForeignKeys(schema, table string) []*model.ReferredFKInfo } +var _ Misc = &infoSchemaMisc{} + type sortedTables []table.Table func (s sortedTables) searchTable(id int64) int { @@ -85,6 +89,17 @@ type schemaTables struct { const bucketCount = 512 type infoSchema struct { + infoSchemaMisc + schemaMap map[string]*schemaTables + + // sortedTablesBuckets is a slice of sortedTables, a table's bucket index is (tableID % bucketCount). + sortedTablesBuckets []sortedTables + + // schemaMetaVersion is the version of schema, and we should check version when change schema. + schemaMetaVersion int64 +} + +type infoSchemaMisc struct { // ruleBundleMap stores all placement rules ruleBundleMap map[int64]*placement.Bundle @@ -96,17 +111,9 @@ type infoSchema struct { resourceGroupMutex sync.RWMutex resourceGroupMap map[string]*model.ResourceGroupInfo - schemaMap map[string]*schemaTables - - // sortedTablesBuckets is a slice of sortedTables, a table's bucket index is (tableID % bucketCount). - sortedTablesBuckets []sortedTables - // temporaryTables stores the temporary table ids temporaryTableIDs map[int64]struct{} - // schemaMetaVersion is the version of schema, and we should check version when change schema. - schemaMetaVersion int64 - // referredForeignKeyMap records all table's ReferredFKInfo. // referredSchemaAndTableName => child SchemaAndTableAndForeignKeyName => *model.ReferredFKInfo referredForeignKeyMap map[SchemaAndTableName][]*model.ReferredFKInfo @@ -279,7 +286,8 @@ func (is *infoSchema) TableByID(id int64) (val table.Table, ok bool) { return slice[idx], true } -func (is *infoSchema) AllocByID(id int64) (autoid.Allocators, bool) { +// allocByID returns the Allocators of a table. +func allocByID(is *infoSchema, id int64) (autoid.Allocators, bool) { tbl, ok := is.TableByID(id) if !ok { return autoid.Allocators{}, false @@ -334,17 +342,10 @@ func (is *infoSchema) FindTableByPartitionID(partitionID int64) (table.Table, *m } // HasTemporaryTable returns whether information schema has temporary table -func (is *infoSchema) HasTemporaryTable() bool { +func (is *infoSchemaMisc) HasTemporaryTable() bool { return len(is.temporaryTableIDs) != 0 } -func (is *infoSchema) Clone() (result []*model.DBInfo) { - for _, v := range is.schemaMap { - result = append(result, v.dbInfo.Clone()) - } - return -} - // GetSequenceByName gets the sequence by name. func GetSequenceByName(is InfoSchema, schema, sequence model.CIStr) (util.SequenceTable, error) { tbl, err := is.TableByName(schema, sequence) @@ -403,7 +404,7 @@ func HasAutoIncrementColumn(tbInfo *model.TableInfo) (bool, string) { } // PolicyByName is used to find the policy. -func (is *infoSchema) PolicyByName(name model.CIStr) (*model.PolicyInfo, bool) { +func (is *infoSchemaMisc) PolicyByName(name model.CIStr) (*model.PolicyInfo, bool) { is.policyMutex.RLock() defer is.policyMutex.RUnlock() t, r := is.policyMap[name.L] @@ -411,7 +412,7 @@ func (is *infoSchema) PolicyByName(name model.CIStr) (*model.PolicyInfo, bool) { } // ResourceGroupByName is used to find the resource group. -func (is *infoSchema) ResourceGroupByName(name model.CIStr) (*model.ResourceGroupInfo, bool) { +func (is *infoSchemaMisc) ResourceGroupByName(name model.CIStr) (*model.ResourceGroupInfo, bool) { is.resourceGroupMutex.RLock() defer is.resourceGroupMutex.RUnlock() t, r := is.resourceGroupMap[name.L] @@ -419,7 +420,7 @@ func (is *infoSchema) ResourceGroupByName(name model.CIStr) (*model.ResourceGrou } // AllResourceGroups returns all resource groups. -func (is *infoSchema) AllResourceGroups() []*model.ResourceGroupInfo { +func (is *infoSchemaMisc) AllResourceGroups() []*model.ResourceGroupInfo { is.resourceGroupMutex.RLock() defer is.resourceGroupMutex.RUnlock() groups := make([]*model.ResourceGroupInfo, 0, len(is.resourceGroupMap)) @@ -430,7 +431,7 @@ func (is *infoSchema) AllResourceGroups() []*model.ResourceGroupInfo { } // AllPlacementPolicies returns all placement policies -func (is *infoSchema) AllPlacementPolicies() []*model.PolicyInfo { +func (is *infoSchemaMisc) AllPlacementPolicies() []*model.PolicyInfo { is.policyMutex.RLock() defer is.policyMutex.RUnlock() policies := make([]*model.PolicyInfo, 0, len(is.policyMap)) @@ -440,12 +441,12 @@ func (is *infoSchema) AllPlacementPolicies() []*model.PolicyInfo { return policies } -func (is *infoSchema) PlacementBundleByPhysicalTableID(id int64) (*placement.Bundle, bool) { +func (is *infoSchemaMisc) PlacementBundleByPhysicalTableID(id int64) (*placement.Bundle, bool) { t, r := is.ruleBundleMap[id] return t, r } -func (is *infoSchema) AllPlacementBundles() []*placement.Bundle { +func (is *infoSchemaMisc) AllPlacementBundles() []*placement.Bundle { bundles := make([]*placement.Bundle, 0, len(is.ruleBundleMap)) for _, bundle := range is.ruleBundleMap { bundles = append(bundles, bundle) @@ -453,31 +454,31 @@ func (is *infoSchema) AllPlacementBundles() []*placement.Bundle { return bundles } -func (is *infoSchema) setResourceGroup(resourceGroup *model.ResourceGroupInfo) { +func (is *infoSchemaMisc) setResourceGroup(resourceGroup *model.ResourceGroupInfo) { is.resourceGroupMutex.Lock() defer is.resourceGroupMutex.Unlock() is.resourceGroupMap[resourceGroup.Name.L] = resourceGroup } -func (is *infoSchema) deleteResourceGroup(name string) { +func (is *infoSchemaMisc) deleteResourceGroup(name string) { is.resourceGroupMutex.Lock() defer is.resourceGroupMutex.Unlock() delete(is.resourceGroupMap, name) } -func (is *infoSchema) setPolicy(policy *model.PolicyInfo) { +func (is *infoSchemaMisc) setPolicy(policy *model.PolicyInfo) { is.policyMutex.Lock() defer is.policyMutex.Unlock() is.policyMap[policy.Name.L] = policy } -func (is *infoSchema) deletePolicy(name string) { +func (is *infoSchemaMisc) deletePolicy(name string) { is.policyMutex.Lock() defer is.policyMutex.Unlock() delete(is.policyMap, name) } -func (is *infoSchema) addReferredForeignKeys(schema model.CIStr, tbInfo *model.TableInfo) { +func (is *infoSchemaMisc) addReferredForeignKeys(schema model.CIStr, tbInfo *model.TableInfo) { for _, fk := range tbInfo.ForeignKeys { if fk.Version < model.FKVersion1 { continue @@ -517,7 +518,7 @@ func (is *infoSchema) addReferredForeignKeys(schema model.CIStr, tbInfo *model.T } } -func (is *infoSchema) deleteReferredForeignKeys(schema model.CIStr, tbInfo *model.TableInfo) { +func (is *infoSchemaMisc) deleteReferredForeignKeys(schema model.CIStr, tbInfo *model.TableInfo) { for _, fk := range tbInfo.ForeignKeys { if fk.Version < model.FKVersion1 { continue @@ -539,7 +540,7 @@ func (is *infoSchema) deleteReferredForeignKeys(schema model.CIStr, tbInfo *mode } // GetTableReferredForeignKeys gets the table's ReferredFKInfo by lowercase schema and table name. -func (is *infoSchema) GetTableReferredForeignKeys(schema, table string) []*model.ReferredFKInfo { +func (is *infoSchemaMisc) GetTableReferredForeignKeys(schema, table string) []*model.ReferredFKInfo { name := SchemaAndTableName{schema: schema, table: table} return is.referredForeignKeyMap[name] } diff --git a/pkg/infoschema/infoschema_test.go b/pkg/infoschema/infoschema_test.go index 0c8bb5690181b..cd4c79c6a1a32 100644 --- a/pkg/infoschema/infoschema_test.go +++ b/pkg/infoschema/infoschema_test.go @@ -129,8 +129,6 @@ func TestBasic(t *testing.T) { schemas := is.AllSchemas() require.Len(t, schemas, 4) - schemas = is.Clone() - require.Len(t, schemas, 4) require.True(t, is.SchemaExists(dbName)) require.False(t, is.SchemaExists(noexist)) @@ -173,10 +171,6 @@ func TestBasic(t *testing.T) { require.False(t, ok) require.Nil(t, tb) - alloc, ok := is.AllocByID(tbID) - require.True(t, ok) - require.NotNil(t, alloc) - tb, err = is.TableByName(dbName, tbName) require.NoError(t, err) require.NotNil(t, tb)