Skip to content

Commit 37f61af

Browse files
committed
fix base merge
Signed-off-by: Wenqi Mou <[email protected]>
1 parent 1eee968 commit 37f61af

File tree

3 files changed

+38
-27
lines changed

3 files changed

+38
-27
lines changed

br/pkg/restore/internal/prealloc_db/db.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ func (db *DB) canReuseTableID(ti *model.TableInfo) bool {
287287
}
288288
prealloced := db.preallocedIDs.PreallocedFor(ti)
289289
if prealloced {
290-
log.Info("reusing table ID", zap.Stringer("table", ti.Name))
290+
log.Info("reusing table ID", zap.Stringer("table", ti.Name), zap.Int64("tableID", ti.ID))
291291
}
292292
return prealloced
293293
}

br/pkg/stream/table_mapping.go

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -136,37 +136,48 @@ func (tm *TableMappingManager) ProcessTableValueAndUpdateIdMapping(dbID int64, t
136136
}
137137

138138
func (tm *TableMappingManager) MergeBaseDBReplace(baseMap map[UpstreamID]*DBReplace) {
139-
// merge baseMap to DBReplaceMap
140-
// also updating globalIdMap, not going to be used in prod but convenient in test
139+
// first pass: update all global IDs
141140
for upstreamID, baseDBReplace := range baseMap {
142141
tm.globalIdMap[upstreamID] = baseDBReplace.DbID
143142

144-
if existingDBReplace, exists := tm.DBReplaceMap[upstreamID]; exists {
145-
existingDBReplace.DbID = baseDBReplace.DbID
143+
for tableUpID, baseTableReplace := range baseDBReplace.TableMap {
144+
tm.globalIdMap[tableUpID] = baseTableReplace.TableID
146145

147-
for tableUpID, baseTableReplace := range baseDBReplace.TableMap {
148-
tm.globalIdMap[tableUpID] = baseTableReplace.TableID
146+
for partUpID, basePartDownID := range baseTableReplace.PartitionMap {
147+
tm.globalIdMap[partUpID] = basePartDownID
148+
}
149+
}
150+
}
149151

150-
if existingTableReplace, tableExists := existingDBReplace.TableMap[tableUpID]; tableExists {
151-
existingTableReplace.TableID = baseTableReplace.TableID
152+
// second pass: update the DBReplaceMap
153+
// first update all existing entries using the global ID map
154+
for _, existingDBReplace := range tm.DBReplaceMap {
155+
if newID, exists := tm.globalIdMap[existingDBReplace.DbID]; exists {
156+
existingDBReplace.DbID = newID
157+
}
152158

153-
for partUpID, basePartDownID := range baseTableReplace.PartitionMap {
154-
tm.globalIdMap[partUpID] = basePartDownID
155-
existingTableReplace.PartitionMap[partUpID] = basePartDownID
156-
}
157-
} else {
158-
existingDBReplace.TableMap[tableUpID] = baseTableReplace
159-
for partUpID, basePartDownID := range baseTableReplace.PartitionMap {
160-
tm.globalIdMap[partUpID] = basePartDownID
161-
}
159+
for _, existingTableReplace := range existingDBReplace.TableMap {
160+
if newID, exists := tm.globalIdMap[existingTableReplace.TableID]; exists {
161+
existingTableReplace.TableID = newID
162+
}
163+
164+
for partUpID, partDownID := range existingTableReplace.PartitionMap {
165+
if newID, exists := tm.globalIdMap[partDownID]; exists {
166+
existingTableReplace.PartitionMap[partUpID] = newID
162167
}
163168
}
164-
} else {
169+
}
170+
}
171+
172+
// then add any new entries from the base map
173+
for upstreamID, baseDBReplace := range baseMap {
174+
if _, exists := tm.DBReplaceMap[upstreamID]; !exists {
165175
tm.DBReplaceMap[upstreamID] = baseDBReplace
176+
} else {
177+
existingDBReplace := tm.DBReplaceMap[upstreamID]
166178
for tableUpID, baseTableReplace := range baseDBReplace.TableMap {
167-
tm.globalIdMap[tableUpID] = baseTableReplace.TableID
168-
for partUpID, basePartDownID := range baseTableReplace.PartitionMap {
169-
tm.globalIdMap[partUpID] = basePartDownID
179+
if _, exists := existingDBReplace.TableMap[tableUpID]; !exists {
180+
existingDBReplace.TableMap[tableUpID] = baseTableReplace
170181
}
171182
}
172183
}
@@ -274,18 +285,18 @@ func (tm *TableMappingManager) ReplaceTemporaryIDs(
274285
return nil
275286
}
276287

277-
func (tm *TableMappingManager) FilterDBReplaceMap(filter *utils.PiTRTableTracker) {
288+
func (tm *TableMappingManager) FilterDBReplaceMap(tracker *utils.PiTRTableTracker) {
278289
// iterate through existing DBReplaceMap
279290
for dbID, dbReplace := range tm.DBReplaceMap {
280291
// remove entire database if not in filter
281-
if !filter.ContainsDB(dbID) {
292+
if !tracker.ContainsDB(dbID) {
282293
delete(tm.DBReplaceMap, dbID)
283294
continue
284295
}
285296

286297
// filter tables in this database
287298
for tableID := range dbReplace.TableMap {
288-
if !filter.ContainsTable(dbID, tableID) {
299+
if !tracker.ContainsTable(dbID, tableID) {
289300
delete(dbReplace.TableMap, tableID)
290301
}
291302
}

br/pkg/task/restore.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1462,7 +1462,7 @@ func AdjustTablesToRestoreAndCreateTableTracker(
14621462
// build tracker for pitr restore to use later
14631463
piTRTableTracker := utils.NewPiTRTableTracker()
14641464

1465-
// put all the newly created db that matches the filter during log backup into the pitr filter
1465+
// put all the newly created db that matches the filter during log backup into the pitr table tracker
14661466
newlyCreatedDBs := logBackupTableHistory.GetNewlyCreatedDBHistory()
14671467
for dbId, dbName := range newlyCreatedDBs {
14681468
if utils.MatchSchema(cfg.TableFilter, dbName) {
@@ -1562,7 +1562,6 @@ func AdjustTablesToRestoreAndCreateTableTracker(
15621562
}
15631563
}
15641564
// store the tracker into config
1565-
log.Info("pitr table tracker", zap.String("map", piTRTableTracker.String()))
15661565
cfg.PiTRTableTracker = piTRTableTracker
15671566
return
15681567
}
@@ -1571,6 +1570,7 @@ func UpdatePiTRTableTracker(cfg *RestoreConfig, tableMap map[int64]*metautil.Tab
15711570
for _, table := range tableMap {
15721571
cfg.PiTRTableTracker.AddTable(table.DB.ID, table.Info.ID)
15731572
}
1573+
log.Info("pitr table tracker", zap.String("map", cfg.PiTRTableTracker.String()))
15741574
}
15751575

15761576
// tweakLocalConfForRestore tweaks some of configs of TiDB to make the restore progress go well.

0 commit comments

Comments
 (0)