Skip to content

Commit

Permalink
*: fix information schema data race. (#5672) (#5676)
Browse files Browse the repository at this point in the history
  • Loading branch information
coocood authored Jan 18, 2018
1 parent e0ff268 commit 78f1023
Showing 1 changed file with 19 additions and 10 deletions.
29 changes: 19 additions & 10 deletions infoschema/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,19 +100,11 @@ func (b *Builder) ApplyDiff(m *meta.Meta, diff *model.SchemaDiff) ([]int64, erro

// copySortedTables copies sortedTables for old table and new table for later modification.
func (b *Builder) copySortedTables(oldTableID, newTableID int64) {
buckets := b.is.sortedTablesBuckets
if tableIDIsValid(oldTableID) {
bucketIdx := tableBucketIdx(oldTableID)
oldSortedTables := buckets[bucketIdx]
newSortedTables := make(sortedTables, len(oldSortedTables))
copy(newSortedTables, oldSortedTables)
buckets[bucketIdx] = newSortedTables
b.copySortedTablesBucket(tableBucketIdx(oldTableID))
}
if tableIDIsValid(newTableID) && newTableID != oldTableID {
oldSortedTables := buckets[tableBucketIdx(newTableID)]
newSortedTables := make(sortedTables, len(oldSortedTables), len(oldSortedTables)+1)
copy(newSortedTables, oldSortedTables)
buckets[tableBucketIdx(newTableID)] = newSortedTables
b.copySortedTablesBucket(tableBucketIdx(newTableID))
}
}

Expand All @@ -138,6 +130,16 @@ func (b *Builder) applyDropSchema(schemaID int64) []int64 {
return nil
}
delete(b.is.schemaMap, di.Name.L)

// Copy the sortedTables that contain the table we are going to drop.
bucketIdxMap := make(map[int]struct{})
for _, tbl := range di.Tables {
bucketIdxMap[tableBucketIdx(tbl.ID)] = struct{}{}
}
for bucketIdx := range bucketIdxMap {
b.copySortedTablesBucket(bucketIdx)
}

ids := make([]int64, 0, len(di.Tables))
for _, tbl := range di.Tables {
b.applyDropTable(di, tbl.ID)
Expand All @@ -147,6 +149,13 @@ func (b *Builder) applyDropSchema(schemaID int64) []int64 {
return ids
}

func (b *Builder) copySortedTablesBucket(bucketIdx int) {
oldSortedTables := b.is.sortedTablesBuckets[bucketIdx]
newSortedTables := make(sortedTables, len(oldSortedTables))
copy(newSortedTables, oldSortedTables)
b.is.sortedTablesBuckets[bucketIdx] = newSortedTables
}

func (b *Builder) applyCreateTable(m *meta.Meta, roDBInfo *model.DBInfo, tableID int64, alloc autoid.Allocator) error {
tblInfo, err := m.GetTable(roDBInfo.ID, tableID)
if err != nil {
Expand Down

0 comments on commit 78f1023

Please sign in to comment.