Skip to content

Commit b51eb49

Browse files
authored
add a map to store all span tasks (#375)
1 parent aa7fb72 commit b51eb49

File tree

1 file changed

+39
-43
lines changed

1 file changed

+39
-43
lines changed

maintainer/replica/replication_db.go

+39-43
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@ import (
2525
// ReplicationDB is an in memory data struct that maintains the replication spans
2626
type ReplicationDB struct {
2727
changefeedID string
28-
nodeTasks map[node.ID]map[common.DispatcherID]*SpanReplication
29-
// group the spans by schema id
28+
// allTasks maintains all the span tasks
29+
allTasks map[common.DispatcherID]*SpanReplication
30+
31+
// group the tasks by the node id, schema id, and table id for fast access
32+
nodeTasks map[node.ID]map[common.DispatcherID]*SpanReplication
3033
schemaTasks map[int64]map[common.DispatcherID]*SpanReplication
31-
// group the spans by table id
32-
tableTasks map[int64]map[common.DispatcherID]*SpanReplication
34+
tableTasks map[int64]map[common.DispatcherID]*SpanReplication
3335

3436
// maps that maintained base on the span scheduling status
3537
replicating map[common.DispatcherID]*SpanReplication
@@ -48,6 +50,7 @@ func NewReplicaSetDB(changefeedID string) *ReplicationDB {
4850
schemaTasks: make(map[int64]map[common.DispatcherID]*SpanReplication),
4951
tableTasks: make(map[int64]map[common.DispatcherID]*SpanReplication),
5052

53+
allTasks: make(map[common.DispatcherID]*SpanReplication),
5154
replicating: make(map[common.DispatcherID]*SpanReplication),
5255
scheduling: make(map[common.DispatcherID]*SpanReplication),
5356
absent: make(map[common.DispatcherID]*SpanReplication),
@@ -60,22 +63,23 @@ func (db *ReplicationDB) GetTaskByID(id common.DispatcherID) *SpanReplication {
6063
db.lock.RLock()
6164
defer db.lock.RUnlock()
6265

63-
return db.getTaskByIDUnLock(id)
66+
return db.allTasks[id]
6467
}
6568

6669
// TaskSize returns the total task size in the db, it includes replicating, scheduling and absent tasks
6770
func (db *ReplicationDB) TaskSize() int {
6871
db.lock.RLock()
6972
defer db.lock.RUnlock()
7073

71-
return len(db.replicating) + len(db.absent) + len(db.scheduling)
74+
return len(db.allTasks)
7275
}
7376

7477
// TryRemoveAll removes non-scheduled tasks from the db and return the scheduled tasks
7578
func (db *ReplicationDB) TryRemoveAll() []*SpanReplication {
7679
db.lock.Lock()
7780
defer db.lock.Unlock()
7881

82+
// remove the absent task directly
7983
for _, stm := range db.absent {
8084
db.removeSpanUnLock(stm)
8185
}
@@ -249,7 +253,7 @@ func (db *ReplicationDB) GetTasksBySchemaID(schemaID int64) []*SpanReplication {
249253
if !ok {
250254
return nil
251255
}
252-
var replicaSets []*SpanReplication = make([]*SpanReplication, 0, len(sm))
256+
var replicaSets = make([]*SpanReplication, 0, len(sm))
253257
for _, v := range sm {
254258
replicaSets = append(replicaSets, v)
255259
}
@@ -283,8 +287,10 @@ func (db *ReplicationDB) ReplaceReplicaSet(olds []*SpanReplication, news []*Span
283287

284288
// first check if all the old replica set exists, if not, return false
285289
for _, old := range olds {
286-
dbItem := db.getTaskByIDUnLock(old.ID)
287-
if dbItem == nil {
290+
if _, ok := db.allTasks[old.ID]; !ok {
291+
log.Warn("old replica set not found, skip",
292+
zap.String("changefeed", db.changefeedID),
293+
zap.String("span", old.ID.String()))
288294
return false
289295
}
290296
}
@@ -306,6 +312,7 @@ func (db *ReplicationDB) AddReplicatingSpan(task *SpanReplication) {
306312
zap.String("nodeID", nodeID.String()),
307313
zap.String("span", task.ID.String()))
308314

315+
db.allTasks[task.ID] = task
309316
db.replicating[task.ID] = task
310317
db.updateNodeMap("", nodeID, task)
311318
db.addToSchemaAndTableMap(task)
@@ -411,48 +418,15 @@ func (db *ReplicationDB) BindSpanToNode(old, new node.ID, task *SpanReplication)
411418
db.updateNodeMap(old, new, task)
412419
}
413420

414-
// GetTaskByID returns the replica set by the id, it will search the replicating, scheduling and absent map
415-
func (db *ReplicationDB) getTaskByIDUnLock(id common.DispatcherID) *SpanReplication {
416-
r, ok := db.replicating[id]
417-
if ok {
418-
return r
419-
}
420-
r, ok = db.scheduling[id]
421-
if ok {
422-
return r
423-
}
424-
return db.absent[id]
425-
}
426-
427421
// addAbsentReplicaSetUnLock adds the replica set to the absent map
428422
func (db *ReplicationDB) addAbsentReplicaSetUnLock(tasks ...*SpanReplication) {
429423
for _, task := range tasks {
424+
db.allTasks[task.ID] = task
430425
db.absent[task.ID] = task
431426
db.addToSchemaAndTableMap(task)
432427
}
433428
}
434429

435-
// addToSchemaAndTableMap adds the task to the schema and table map
436-
func (db *ReplicationDB) addToSchemaAndTableMap(task *SpanReplication) {
437-
tableID := task.Span.TableID
438-
schemaID := task.GetSchemaID()
439-
// modify the schema map
440-
schemaMap, ok := db.schemaTasks[schemaID]
441-
if !ok {
442-
schemaMap = make(map[common.DispatcherID]*SpanReplication)
443-
db.schemaTasks[schemaID] = schemaMap
444-
}
445-
schemaMap[task.ID] = task
446-
447-
// modify the table map
448-
tableMap, ok := db.tableTasks[tableID]
449-
if !ok {
450-
tableMap = make(map[common.DispatcherID]*SpanReplication)
451-
db.tableTasks[tableID] = tableMap
452-
}
453-
tableMap[task.ID] = task
454-
}
455-
456430
// removeSpanUnLock removes the replica set from the db without lock
457431
func (db *ReplicationDB) removeSpanUnLock(spans ...*SpanReplication) {
458432
for _, span := range spans {
@@ -480,9 +454,31 @@ func (db *ReplicationDB) removeSpanUnLock(spans ...*SpanReplication) {
480454
if len(nodeMap) == 0 {
481455
delete(db.nodeTasks, nodeID)
482456
}
457+
delete(db.allTasks, span.ID)
483458
}
484459
}
485460

461+
// addToSchemaAndTableMap adds the task to the schema and table map
462+
func (db *ReplicationDB) addToSchemaAndTableMap(task *SpanReplication) {
463+
tableID := task.Span.TableID
464+
schemaID := task.GetSchemaID()
465+
// modify the schema map
466+
schemaMap, ok := db.schemaTasks[schemaID]
467+
if !ok {
468+
schemaMap = make(map[common.DispatcherID]*SpanReplication)
469+
db.schemaTasks[schemaID] = schemaMap
470+
}
471+
schemaMap[task.ID] = task
472+
473+
// modify the table map
474+
tableMap, ok := db.tableTasks[tableID]
475+
if !ok {
476+
tableMap = make(map[common.DispatcherID]*SpanReplication)
477+
db.tableTasks[tableID] = tableMap
478+
}
479+
tableMap[task.ID] = task
480+
}
481+
486482
// updateNodeMap updates the node map, it will remove the task from the old node and add it to the new node
487483
func (db *ReplicationDB) updateNodeMap(old, new node.ID, task *SpanReplication) {
488484
//clear from the old node

0 commit comments

Comments
 (0)