Skip to content

Commit 6f578e0

Browse files
committed
add epoch to maintainer related code
1 parent eff6e26 commit 6f578e0

18 files changed

Lines changed: 794 additions & 227 deletions

‎coordinator/changefeed/changefeed.go‎

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ func (c *Changefeed) NewAddMaintainerMessage(server node.ID, maintainerEpoch uin
271271
}
272272

273273
func (c *Changefeed) NewRemoveMaintainerMessage(server node.ID, casCade, removed bool) *messaging.TargetMessage {
274-
return RemoveMaintainerMessage(c.GetKeyspaceID(), c.ID, server, casCade, removed)
274+
return RemoveMaintainerMessageWithEpoch(c.GetKeyspaceID(), c.ID, server, casCade, removed, c.GetMaintainerEpoch())
275275
}
276276

277277
func (c *Changefeed) NewCheckpointTsMessage(ts uint64) *messaging.TargetMessage {
@@ -284,14 +284,19 @@ func (c *Changefeed) NewCheckpointTsMessage(ts uint64) *messaging.TargetMessage
284284
}
285285

286286
func RemoveMaintainerMessage(keyspaceID uint32, id common.ChangeFeedID, server node.ID, casCade bool, removed bool) *messaging.TargetMessage {
287+
return RemoveMaintainerMessageWithEpoch(keyspaceID, id, server, casCade, removed, 0)
288+
}
289+
290+
func RemoveMaintainerMessageWithEpoch(keyspaceID uint32, id common.ChangeFeedID, server node.ID, casCade bool, removed bool, maintainerEpoch uint64) *messaging.TargetMessage {
287291
casCade = casCade || removed
288292
return messaging.NewSingleTargetMessage(server,
289293
messaging.MaintainerManagerTopic,
290294
&heartbeatpb.RemoveMaintainerRequest{
291-
Id: id.ToPB(),
292-
Cascade: casCade,
293-
Removed: removed,
294-
KeyspaceId: keyspaceID,
295+
Id: id.ToPB(),
296+
Cascade: casCade,
297+
Removed: removed,
298+
KeyspaceId: keyspaceID,
299+
MaintainerEpoch: maintainerEpoch,
295300
})
296301
}
297302

‎coordinator/changefeed/changefeed_test.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,13 +169,16 @@ func TestChangefeed_NewRemoveMaintainerMessage(t *testing.T) {
169169
SinkURI: "kafka://127.0.0.1:9092",
170170
State: config.StateNormal,
171171
Config: config.GetDefaultReplicaConfig(),
172+
Epoch: 42,
172173
}
173174
cf := NewChangefeed(cfID, info, 100, true)
174175

175176
server := node.ID("server-1")
176177
msg := cf.NewRemoveMaintainerMessage(server, true, true)
177178
require.Equal(t, server, msg.To)
178179
require.Equal(t, messaging.MaintainerManagerTopic, msg.Topic)
180+
req := msg.Message[0].(*heartbeatpb.RemoveMaintainerRequest)
181+
require.Equal(t, uint64(42), req.MaintainerEpoch)
179182
}
180183

181184
func TestChangefeed_NewCheckpointTsMessage(t *testing.T) {
@@ -199,6 +202,12 @@ func TestRemoveMaintainerMessage(t *testing.T) {
199202
msg := RemoveMaintainerMessage(common.DefaultKeyspaceID, cfID, server, true, true)
200203
require.Equal(t, server, msg.To)
201204
require.Equal(t, messaging.MaintainerManagerTopic, msg.Topic)
205+
req := msg.Message[0].(*heartbeatpb.RemoveMaintainerRequest)
206+
require.Equal(t, uint64(0), req.MaintainerEpoch)
207+
208+
msg = RemoveMaintainerMessageWithEpoch(common.DefaultKeyspaceID, cfID, server, true, true, 9)
209+
req = msg.Message[0].(*heartbeatpb.RemoveMaintainerRequest)
210+
require.Equal(t, uint64(9), req.MaintainerEpoch)
202211
}
203212

204213
func TestChangefeedGetStatusForResume(t *testing.T) {

‎coordinator/operator/operator_add.go‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@ func (m *AddMaintainerOperator) Check(from node.ID, status *heartbeatpb.Maintain
8585
}
8686

8787
func maintainerEpochMatches(expected, actual uint64) bool {
88-
// Epoch zero means one side is from an older binary or a test fixture that
89-
// does not participate in maintainer epoch fencing yet.
90-
return expected == 0 || actual == 0 || expected == actual
88+
// Expected epoch zero keeps compatibility with operators that were created
89+
// before epoch fencing was introduced.
90+
return expected == 0 || expected == actual
9191
}
9292

9393
// Schedule builds the "add maintainer" command message, or returns nil when finished/canceled.

‎coordinator/operator/operator_add_test.go‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ func TestAddMaintainerOperator_CheckRequiresMaintainerEpoch(t *testing.T) {
8181
req := op.Schedule().Message[0].(*heartbeatpb.AddMaintainerRequest)
8282
require.Equal(t, uint64(7), req.MaintainerEpoch)
8383

84+
op.Check("n1", &heartbeatpb.MaintainerStatus{
85+
State: heartbeatpb.ComponentState_Working,
86+
BootstrapDone: true,
87+
MaintainerEpoch: 0,
88+
})
89+
require.False(t, op.finished.Load())
90+
8491
op.Check("n1", &heartbeatpb.MaintainerStatus{
8592
State: heartbeatpb.ComponentState_Working,
8693
BootstrapDone: true,

‎coordinator/operator/operator_controller.go‎

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,20 @@ func (oc *Controller) StopChangefeed(_ context.Context, cfID common.ChangeFeedID
127127
oc.mu.Lock()
128128
defer oc.mu.Unlock()
129129

130+
changefeed := oc.changefeedDB.GetByID(cfID)
131+
if changefeed == nil {
132+
log.Warn("stop changefeed failed, changefeed not found",
133+
zap.String("role", oc.role),
134+
zap.Bool("removed", removed),
135+
zap.String("changefeed", cfID.Name()))
136+
if old, ok := oc.operators[cfID]; ok {
137+
return old.OP
138+
}
139+
return nil
140+
}
141+
keyspaceID := changefeed.GetKeyspaceID()
142+
maintainerEpoch := changefeed.GetMaintainerEpoch()
143+
130144
scheduledNode := oc.changefeedDB.StopByChangefeedID(cfID, removed)
131145
if scheduledNode == "" {
132146
log.Info("changefeed is not scheduled, try stop maintainer using coordinator node",
@@ -136,17 +150,14 @@ func (oc *Controller) StopChangefeed(_ context.Context, cfID common.ChangeFeedID
136150
scheduledNode = oc.selfNode.ID
137151
}
138152

139-
changefeed := oc.changefeedDB.GetByID(cfID)
140-
keyspaceID := changefeed.GetKeyspaceID()
141-
142-
return oc.pushStopChangefeedOperator(keyspaceID, cfID, scheduledNode, removed)
153+
return oc.pushStopChangefeedOperator(keyspaceID, cfID, scheduledNode, removed, maintainerEpoch)
143154
}
144155

145156
// pushStopChangefeedOperator pushes a stop changefeed operator to the controller.
146157
// it checks if the operator already exists, if exists, it will replace the old one.
147158
// if the old operator is the removing operator, it will skip this operator.
148-
func (oc *Controller) pushStopChangefeedOperator(keyspaceID uint32, cfID common.ChangeFeedID, nodeID node.ID, remove bool) operator.Operator[common.ChangeFeedID, *heartbeatpb.MaintainerStatus] {
149-
op := NewStopChangefeedOperator(keyspaceID, cfID, nodeID, oc.selfNode.ID, oc.backend, remove)
159+
func (oc *Controller) pushStopChangefeedOperator(keyspaceID uint32, cfID common.ChangeFeedID, nodeID node.ID, remove bool, maintainerEpoch uint64) operator.Operator[common.ChangeFeedID, *heartbeatpb.MaintainerStatus] {
160+
op := NewStopChangefeedOperator(keyspaceID, cfID, nodeID, oc.selfNode.ID, oc.backend, remove, maintainerEpoch)
150161
if old, ok := oc.operators[cfID]; ok {
151162
oldStop, ok := old.OP.(*StopChangefeedOperator)
152163
if ok {

‎coordinator/operator/operator_move.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,10 @@ func (m *MoveMaintainerOperator) Schedule() *messaging.TargetMessage {
8888
}
8989

9090
if m.originNodeStopped {
91-
if !m.bind {
91+
if m.maintainerEpoch == 0 {
9292
m.maintainerEpoch = m.changefeed.GetMaintainerEpoch()
93+
}
94+
if !m.bind {
9395
m.db.BindChangefeedToNode(m.origin, m.dest, m.changefeed)
9496
m.bind = true
9597
}

‎coordinator/operator/operator_move_test.go‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ func TestMoveMaintainerOperator_OnNodeRemove(t *testing.T) {
3131
ChangefeedID: cfID,
3232
Config: config.GetDefaultReplicaConfig(),
3333
SinkURI: "mysql://127.0.0.1:3306",
34+
Epoch: 7,
3435
},
3536
1, true)
3637
changefeedDB.AddReplicatingMaintainer(cf, "n1")
@@ -44,6 +45,7 @@ func TestMoveMaintainerOperator_OnNodeRemove(t *testing.T) {
4445
require.Len(t, changefeedDB.GetByNodeID("n1"), 1)
4546
req := op.Schedule().Message[0].(*heartbeatpb.AddMaintainerRequest)
4647
require.NotNil(t, req)
48+
require.Equal(t, uint64(7), req.MaintainerEpoch)
4749
require.Len(t, changefeedDB.GetByNodeID("n1"), 1)
4850

4951
op.OnNodeRemove("n1")

‎coordinator/operator/operator_stop.go‎

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type StopChangefeedOperator struct {
3434
cfID common.ChangeFeedID
3535
nodeID node.ID
3636
changefeedIsRemoved bool
37+
maintainerEpoch uint64
3738
finished atomic.Bool
3839
coordinatorNodeID node.ID
3940
backend changefeed.Backend
@@ -46,18 +47,23 @@ func NewStopChangefeedOperator(
4647
coordinatorNode node.ID,
4748
backend changefeed.Backend,
4849
removed bool,
50+
maintainerEpoch uint64,
4951
) *StopChangefeedOperator {
5052
return &StopChangefeedOperator{
5153
keyspaceID: keyspaceID,
5254
cfID: cfID,
5355
nodeID: nodeID,
5456
changefeedIsRemoved: removed,
57+
maintainerEpoch: maintainerEpoch,
5558
coordinatorNodeID: coordinatorNode,
5659
backend: backend,
5760
}
5861
}
5962

6063
func (m *StopChangefeedOperator) Check(_ node.ID, status *heartbeatpb.MaintainerStatus) {
64+
if status == nil {
65+
return
66+
}
6167
if !m.finished.Load() && status.State != heartbeatpb.ComponentState_Working {
6268
log.Info("maintainer report non-working status",
6369
zap.Stringer("maintainer", m.cfID))
@@ -66,7 +72,14 @@ func (m *StopChangefeedOperator) Check(_ node.ID, status *heartbeatpb.Maintainer
6672
}
6773

6874
func (m *StopChangefeedOperator) Schedule() *messaging.TargetMessage {
69-
return changefeed.RemoveMaintainerMessage(m.keyspaceID, m.cfID, m.nodeID, true, m.changefeedIsRemoved)
75+
return changefeed.RemoveMaintainerMessageWithEpoch(
76+
m.keyspaceID,
77+
m.cfID,
78+
m.nodeID,
79+
true,
80+
m.changefeedIsRemoved,
81+
m.maintainerEpoch,
82+
)
7083
}
7184

7285
// OnNodeRemove is called when node offline, and the maintainer must already move to absent status and will be scheduled again

‎coordinator/operator/operator_stop_test.go‎

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"github.com/golang/mock/gomock"
2020
"github.com/pingcap/ticdc/coordinator/changefeed"
2121
mock_changefeed "github.com/pingcap/ticdc/coordinator/changefeed/mock"
22+
"github.com/pingcap/ticdc/heartbeatpb"
2223
"github.com/pingcap/ticdc/pkg/common"
2324
"github.com/pingcap/ticdc/pkg/config"
2425
"github.com/pingcap/ticdc/pkg/errors"
@@ -38,7 +39,7 @@ func TestStopChangefeedOperator_OnNodeRemove(t *testing.T) {
3839

3940
ctrl := gomock.NewController(t)
4041
backend := mock_changefeed.NewMockBackend(ctrl)
41-
op := NewStopChangefeedOperator(common.DefaultKeyspaceID, cfID, "n1", "n2", backend, true)
42+
op := NewStopChangefeedOperator(common.DefaultKeyspaceID, cfID, "n1", "n2", backend, true, 0)
4243
op.OnNodeRemove("n1")
4344
require.Equal(t, "n2", op.nodeID.String())
4445
require.False(t, op.finished.Load())
@@ -54,7 +55,7 @@ func TestStopChangefeedOperator_OnTaskRemoved(t *testing.T) {
5455
},
5556
1, true)
5657
changefeedDB.AddReplicatingMaintainer(cf, "n1")
57-
op := NewStopChangefeedOperator(common.DefaultKeyspaceID, cfID, "n1", "n2", nil, true)
58+
op := NewStopChangefeedOperator(common.DefaultKeyspaceID, cfID, "n1", "n2", nil, true, 0)
5859
op.OnTaskRemoved()
5960
require.True(t, op.finished.Load())
6061
}
@@ -72,11 +73,19 @@ func TestStopChangefeedOperator_PostFinish(t *testing.T) {
7273

7374
ctrl := gomock.NewController(t)
7475
backend := mock_changefeed.NewMockBackend(ctrl)
75-
op := NewStopChangefeedOperator(common.DefaultKeyspaceID, cfID, "n1", "n2", backend, true)
76+
op := NewStopChangefeedOperator(common.DefaultKeyspaceID, cfID, "n1", "n2", backend, true, 0)
7677
backend.EXPECT().DeleteChangefeed(gomock.Any(), cfID).Return(errors.New("err"))
7778
op.PostFinish()
7879

79-
op2 := NewStopChangefeedOperator(common.DefaultKeyspaceID, cfID, "n1", "n2", backend, false)
80+
op2 := NewStopChangefeedOperator(common.DefaultKeyspaceID, cfID, "n1", "n2", backend, false, 0)
8081
backend.EXPECT().SetChangefeedProgress(gomock.Any(), cfID, config.ProgressNone).Return(errors.New("err"))
8182
op2.PostFinish()
8283
}
84+
85+
func TestStopChangefeedOperator_ScheduleMaintainerEpoch(t *testing.T) {
86+
cfID := common.NewChangeFeedIDWithName("test", common.DefaultKeyspaceName)
87+
op := NewStopChangefeedOperator(common.DefaultKeyspaceID, cfID, "n1", "n2", nil, true, 9)
88+
89+
req := op.Schedule().Message[0].(*heartbeatpb.RemoveMaintainerRequest)
90+
require.Equal(t, uint64(9), req.MaintainerEpoch)
91+
}

‎downstreamadapter/dispatchermanager/dispatcher_manager_info.go‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,15 @@ func (e *DispatcherManager) SetMaintainerID(maintainerID node.ID) {
5858
e.meta.maintainerID = maintainerID
5959
}
6060

61+
func (e *DispatcherManager) SetMaintainerIdentity(maintainerID node.ID, maintainerEpoch uint64) {
62+
e.meta.Lock()
63+
defer e.meta.Unlock()
64+
e.meta.maintainerID = maintainerID
65+
if maintainerEpoch != 0 {
66+
e.meta.maintainerEpoch = maintainerEpoch
67+
}
68+
}
69+
6170
func (e *DispatcherManager) GetMaintainerEpoch() uint64 {
6271
e.meta.Lock()
6372
defer e.meta.Unlock()

0 commit comments

Comments
 (0)