Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

infoschema: split InfoSchemaMisc from InfoSchema interface #51015

Merged
merged 6 commits into from
Feb 7, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions pkg/infoschema/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down Expand Up @@ -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,
Expand Down
71 changes: 36 additions & 35 deletions pkg/infoschema/infoschema.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,23 +34,25 @@ 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)
SchemaByTable(tableInfo *model.TableInfo) (*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
Expand All @@ -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 {
Expand All @@ -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

Expand All @@ -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
Expand Down Expand Up @@ -283,7 +290,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
Expand Down Expand Up @@ -338,17 +346,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)
Expand Down Expand Up @@ -406,23 +407,23 @@ 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]
return t, r
}

// 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]
return t, r
}

// 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))
Expand All @@ -433,7 +434,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))
Expand All @@ -443,44 +444,44 @@ 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)
}
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
Expand Down Expand Up @@ -520,7 +521,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
Expand All @@ -542,7 +543,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]
}
Expand Down
6 changes: 0 additions & 6 deletions pkg/infoschema/infoschema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,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))
Expand Down Expand Up @@ -172,10 +170,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)
Expand Down