@@ -39,6 +39,7 @@ import (
39
39
)
40
40
41
41
// Controller schedules and balance tables
42
+ // there are 3 main components in the controller, scheduler, ReplicationDB and operator controller
42
43
type Controller struct {
43
44
// initialTables hold all tables that before controller bootstrapped
44
45
initialTables []commonEvent.Table
@@ -93,6 +94,37 @@ func NewController(changefeedID string,
93
94
return s
94
95
}
95
96
97
+ // HandleStatus handle the status report from the node
98
+ func (c * Controller ) HandleStatus (from node.ID , statusList []* heartbeatpb.TableSpanStatus ) {
99
+ for _ , status := range statusList {
100
+ dispatcherID := common .NewDispatcherIDFromPB (status .ID )
101
+ stm := c .GetTask (dispatcherID )
102
+ if stm == nil {
103
+ log .Warn ("no span found, ignore" ,
104
+ zap .String ("changefeed" , c .changefeedID ),
105
+ zap .String ("from" , from .String ()),
106
+ zap .Any ("status" , status ),
107
+ zap .String ("span" , dispatcherID .String ()))
108
+ if status .ComponentStatus == heartbeatpb .ComponentState_Working {
109
+ // if the span is not found, and the status is working, we need to remove it from dispatcher
110
+ _ = c .messageCenter .SendCommand (replica .NewRemoveInferiorMessage (from , c .changefeedID , status .ID ))
111
+ }
112
+ continue
113
+ }
114
+ c .operatorController .UpdateOperatorStatus (dispatcherID , from , status )
115
+ nodeID := stm .GetNodeID ()
116
+ if nodeID != from {
117
+ // todo: handle the case that the node id is mismatch
118
+ log .Warn ("node id not match" ,
119
+ zap .String ("changefeed" , c .changefeedID ),
120
+ zap .Any ("from" , from ),
121
+ zap .Stringer ("node" , nodeID ))
122
+ continue
123
+ }
124
+ stm .UpdateStatus (status )
125
+ }
126
+ }
127
+
96
128
func (c * Controller ) GetTasksBySchemaID (schemaID int64 ) []* replica.SpanReplication {
97
129
return c .replicationDB .GetTasksBySchemaID (schemaID )
98
130
}
@@ -207,25 +239,22 @@ func (c *Controller) GetTask(dispatcherID common.DispatcherID) *replica.SpanRepl
207
239
return c .replicationDB .GetTaskByID (dispatcherID )
208
240
}
209
241
210
- // RemoveAllTasks remove all tasks, todo: move these 3 methods to the operator controller
242
+ // RemoveAllTasks remove all tasks
211
243
func (c * Controller ) RemoveAllTasks () {
212
- for _ , replicaSet := range c .replicationDB .TryRemoveAll () {
213
- c .operatorController .ReplicaSetRemoved (operator .NewRemoveDispatcherOperator (c .replicationDB , replicaSet ))
214
- }
244
+ c .operatorController .RemoveAllTasks ()
215
245
}
216
246
247
+ // RemoveTasksBySchemaID remove all tasks by schema id
217
248
func (c * Controller ) RemoveTasksBySchemaID (schemaID int64 ) {
218
- for _ , replicaSet := range c .replicationDB .TryRemoveBySchemaID (schemaID ) {
219
- c .operatorController .ReplicaSetRemoved (operator .NewRemoveDispatcherOperator (c .replicationDB , replicaSet ))
220
- }
249
+ c .operatorController .RemoveTasksBySchemaID (schemaID )
221
250
}
222
251
252
+ // RemoveTasksByTableIDs remove all tasks by table id
223
253
func (c * Controller ) RemoveTasksByTableIDs (tables ... int64 ) {
224
- for _ , replicaSet := range c .replicationDB .TryRemoveByTableIDs (tables ... ) {
225
- c .operatorController .ReplicaSetRemoved (operator .NewRemoveDispatcherOperator (c .replicationDB , replicaSet ))
226
- }
254
+ c .operatorController .RemoveTasksByTableIDs (tables ... )
227
255
}
228
256
257
+ // GetTasksByTableIDs get all tasks by table id
229
258
func (c * Controller ) GetTasksByTableIDs (tableIDs ... int64 ) []* replica.SpanReplication {
230
259
return c .replicationDB .GetTasksByTableIDs (tableIDs ... )
231
260
}
@@ -236,6 +265,7 @@ func (c *Controller) UpdateSchemaID(tableID, newSchemaID int64) {
236
265
c .replicationDB .UpdateSchemaID (tableID , newSchemaID )
237
266
}
238
267
268
+ // RemoveNode is called when a node is removed
239
269
func (c * Controller ) RemoveNode (id node.ID ) {
240
270
c .operatorController .OnNodeRemoved (id )
241
271
}
@@ -245,36 +275,6 @@ func (c *Controller) ScheduleFinished() bool {
245
275
return c .replicationDB .GetAbsentSize () == 0 && c .operatorController .OperatorSize () == 0
246
276
}
247
277
248
- func (c * Controller ) HandleStatus (from node.ID , statusList []* heartbeatpb.TableSpanStatus ) {
249
- for _ , status := range statusList {
250
- dispatcherID := common .NewDispatcherIDFromPB (status .ID )
251
- stm := c .GetTask (dispatcherID )
252
- if stm == nil {
253
- log .Warn ("no span found, ignore" ,
254
- zap .String ("changefeed" , c .changefeedID ),
255
- zap .String ("from" , from .String ()),
256
- zap .Any ("status" , status ),
257
- zap .String ("span" , dispatcherID .String ()))
258
- if status .ComponentStatus == heartbeatpb .ComponentState_Working {
259
- // if the span is not found, and the status is working, we need to remove it from dispatcher
260
- _ = c .messageCenter .SendCommand (replica .NewRemoveInferiorMessage (from , c .changefeedID , status .ID ))
261
- }
262
- continue
263
- }
264
- c .operatorController .UpdateOperatorStatus (dispatcherID , from , status )
265
- nodeID := stm .GetNodeID ()
266
- if nodeID != from {
267
- // todo: handle the case that the node id is mismatch
268
- log .Warn ("node id not match" ,
269
- zap .String ("changefeed" , c .changefeedID ),
270
- zap .Any ("from" , from ),
271
- zap .Stringer ("node" , nodeID ))
272
- continue
273
- }
274
- stm .UpdateStatus (status )
275
- }
276
- }
277
-
278
278
func (c * Controller ) TaskSize () int {
279
279
return c .replicationDB .TaskSize ()
280
280
}
0 commit comments