Skip to content

Commit eff6e26

Browse files
committed
add epoch to the add maintainer related opeartions
1 parent 99f4859 commit eff6e26

13 files changed

Lines changed: 368 additions & 197 deletions

‎coordinator/changefeed/changefeed.go‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,17 @@ func (c *Changefeed) SetInfo(info *config.ChangeFeedInfo) {
106106
c.info.Store(info)
107107
}
108108

109+
func (c *Changefeed) GetMaintainerEpoch() uint64 {
110+
if c == nil || c.info == nil {
111+
return 0
112+
}
113+
info := c.GetInfo()
114+
if info == nil {
115+
return 0
116+
}
117+
return info.Epoch
118+
}
119+
109120
func (c *Changefeed) StartFinished() {
110121
c.backoff.StartFinished()
111122
}
@@ -246,7 +257,7 @@ func (c *Changefeed) GetLastSavedCheckPointTs() uint64 {
246257
return c.lastSavedCheckpointTs.Load()
247258
}
248259

249-
func (c *Changefeed) NewAddMaintainerMessage(server node.ID) *messaging.TargetMessage {
260+
func (c *Changefeed) NewAddMaintainerMessage(server node.ID, maintainerEpoch uint64) *messaging.TargetMessage {
250261
return messaging.NewSingleTargetMessage(server,
251262
messaging.MaintainerManagerTopic,
252263
&heartbeatpb.AddMaintainerRequest{
@@ -255,6 +266,7 @@ func (c *Changefeed) NewAddMaintainerMessage(server node.ID) *messaging.TargetMe
255266
Config: c.configBytes,
256267
IsNewChangefeed: c.isNew,
257268
KeyspaceId: c.GetKeyspaceID(),
269+
MaintainerEpoch: maintainerEpoch,
258270
})
259271
}
260272

‎coordinator/changefeed/changefeed_test.go‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,9 +156,11 @@ func TestChangefeed_NewAddMaintainerMessage(t *testing.T) {
156156
cf := NewChangefeed(cfID, info, 100, true)
157157

158158
server := node.ID("server-1")
159-
msg := cf.NewAddMaintainerMessage(server)
159+
msg := cf.NewAddMaintainerMessage(server, 42)
160160
require.Equal(t, server, msg.To)
161161
require.Equal(t, messaging.MaintainerManagerTopic, msg.Topic)
162+
req := msg.Message[0].(*heartbeatpb.AddMaintainerRequest)
163+
require.Equal(t, uint64(42), req.MaintainerEpoch)
162164
}
163165

164166
func TestChangefeed_NewRemoveMaintainerMessage(t *testing.T) {

‎coordinator/controller.go‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -621,7 +621,18 @@ func (c *Controller) finishBootstrap(ctx context.Context, runningChangefeeds map
621621
zap.String("changefeed", cfID.String()),
622622
zap.String("node", rm.nodeID.String()),
623623
zap.String("status", common.FormatMaintainerStatus(rm.status)))
624-
cf := changefeed.NewChangefeed(cfID, cfMeta.Info, rm.status.CheckpointTs, false)
624+
info := cfMeta.Info
625+
if epoch := rm.status.GetMaintainerEpoch(); epoch != 0 && info.Epoch != epoch {
626+
clonedInfo, err := info.Clone()
627+
if err != nil {
628+
log.Panic("clone changefeed info failed",
629+
zap.Stringer("changefeed", cfID),
630+
zap.Error(err))
631+
}
632+
clonedInfo.Epoch = epoch
633+
info = clonedInfo
634+
}
635+
cf := changefeed.NewChangefeed(cfID, info, rm.status.CheckpointTs, false)
625636
c.changefeedDB.AddReplicatingMaintainer(cf, rm.nodeID)
626637
delete(runningChangefeeds, cfID)
627638
}

‎coordinator/operator/operator_add.go‎

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,10 @@ const (
4040
// Note: OperatorController can call Schedule/Check/OnNodeRemove concurrently under a
4141
// shared read lock, so the operator's internal state must be concurrency-safe.
4242
type AddMaintainerOperator struct {
43-
cf *changefeed.Changefeed
44-
dest node.ID
45-
finished atomic.Bool
43+
cf *changefeed.Changefeed
44+
dest node.ID
45+
maintainerEpoch uint64
46+
finished atomic.Bool
4647
// canceled records why the operator stops scheduling.
4748
// It must be atomic because Schedule can run concurrently with OnNodeRemove/OnTaskRemoved.
4849
canceled atomic.Int32
@@ -56,9 +57,10 @@ func NewAddMaintainerOperator(
5657
dest node.ID,
5758
) *AddMaintainerOperator {
5859
return &AddMaintainerOperator{
59-
cf: cf,
60-
dest: dest,
61-
db: db,
60+
cf: cf,
61+
dest: dest,
62+
maintainerEpoch: cf.GetMaintainerEpoch(),
63+
db: db,
6264
}
6365
}
6466

@@ -73,19 +75,27 @@ func (m *AddMaintainerOperator) Check(from node.ID, status *heartbeatpb.Maintain
7375
// This avoids false positives when a removal-only maintainer reports Working.
7476
if !m.finished.Load() && from == m.dest &&
7577
status.State == heartbeatpb.ComponentState_Working &&
76-
status.BootstrapDone {
78+
status.BootstrapDone &&
79+
maintainerEpochMatches(m.maintainerEpoch, status.MaintainerEpoch) {
7780
log.Info("maintainer report working status",
78-
zap.String("changefeed", m.cf.ID.String()))
81+
zap.String("changefeed", m.cf.ID.String()),
82+
zap.Uint64("maintainerEpoch", status.MaintainerEpoch))
7983
m.finished.Store(true)
8084
}
8185
}
8286

87+
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
91+
}
92+
8393
// Schedule builds the "add maintainer" command message, or returns nil when finished/canceled.
8494
func (m *AddMaintainerOperator) Schedule() *messaging.TargetMessage {
8595
if m.finished.Load() || m.canceled.Load() != None {
8696
return nil
8797
}
88-
return m.cf.NewAddMaintainerMessage(m.dest)
98+
return m.cf.NewAddMaintainerMessage(m.dest, m.maintainerEpoch)
8999
}
90100

91101
// OnNodeRemove cancels the operator when the destination node goes offline.
@@ -138,8 +148,8 @@ func (m *AddMaintainerOperator) PostFinish() {
138148

139149
// String returns a human-readable description of the operator, for logging.
140150
func (m *AddMaintainerOperator) String() string {
141-
return fmt.Sprintf("add maintainer operator: %s, dest:%s",
142-
m.cf.ID, m.dest)
151+
return fmt.Sprintf("add maintainer operator: %s, dest:%s, epoch:%d",
152+
m.cf.ID, m.dest, m.maintainerEpoch)
143153
}
144154

145155
// Type returns the operator type used by metrics/logging.

‎coordinator/operator/operator_add_test.go‎

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ import (
1818

1919
"github.com/pingcap/ticdc/coordinator/changefeed"
2020
"github.com/pingcap/ticdc/heartbeatpb"
21+
"github.com/pingcap/ticdc/pkg/common"
22+
"github.com/pingcap/ticdc/pkg/config"
2123
"github.com/stretchr/testify/require"
2224
)
2325

@@ -66,3 +68,30 @@ func TestAddMaintainerOperator_CheckRequiresBootstrapDone(t *testing.T) {
6668
})
6769
require.True(t, op.finished.Load())
6870
}
71+
72+
func TestAddMaintainerOperator_CheckRequiresMaintainerEpoch(t *testing.T) {
73+
cfID := common.NewChangeFeedIDWithName("test", common.DefaultKeyspaceName)
74+
cf := changefeed.NewChangefeed(cfID, &config.ChangeFeedInfo{
75+
ChangefeedID: cfID,
76+
Config: config.GetDefaultReplicaConfig(),
77+
SinkURI: "mysql://127.0.0.1:3306",
78+
Epoch: 7,
79+
}, 1, true)
80+
op := NewAddMaintainerOperator(nil, cf, "n1")
81+
req := op.Schedule().Message[0].(*heartbeatpb.AddMaintainerRequest)
82+
require.Equal(t, uint64(7), req.MaintainerEpoch)
83+
84+
op.Check("n1", &heartbeatpb.MaintainerStatus{
85+
State: heartbeatpb.ComponentState_Working,
86+
BootstrapDone: true,
87+
MaintainerEpoch: 6,
88+
})
89+
require.False(t, op.finished.Load())
90+
91+
op.Check("n1", &heartbeatpb.MaintainerStatus{
92+
State: heartbeatpb.ComponentState_Working,
93+
BootstrapDone: true,
94+
MaintainerEpoch: 7,
95+
})
96+
require.True(t, op.finished.Load())
97+
}

‎coordinator/operator/operator_move.go‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ type MoveMaintainerOperator struct {
3636
originNodeStopped bool
3737
finished bool
3838
bind bool
39+
maintainerEpoch uint64
3940

4041
canceled bool
4142

@@ -68,10 +69,12 @@ func (m *MoveMaintainerOperator) Check(from node.ID, status *heartbeatpb.Maintai
6869
}
6970
if m.originNodeStopped && from == m.dest &&
7071
status.State == heartbeatpb.ComponentState_Working &&
71-
status.BootstrapDone {
72+
status.BootstrapDone &&
73+
maintainerEpochMatches(m.maintainerEpoch, status.MaintainerEpoch) {
7274
log.Info("changefeed added to dest node",
7375
zap.String("dest", m.dest.String()),
74-
zap.String("changefeed", m.changefeed.ID.String()))
76+
zap.String("changefeed", m.changefeed.ID.String()),
77+
zap.Uint64("maintainerEpoch", status.MaintainerEpoch))
7578
m.finished = true
7679
}
7780
}
@@ -86,10 +89,11 @@ func (m *MoveMaintainerOperator) Schedule() *messaging.TargetMessage {
8689

8790
if m.originNodeStopped {
8891
if !m.bind {
92+
m.maintainerEpoch = m.changefeed.GetMaintainerEpoch()
8993
m.db.BindChangefeedToNode(m.origin, m.dest, m.changefeed)
9094
m.bind = true
9195
}
92-
return m.changefeed.NewAddMaintainerMessage(m.dest)
96+
return m.changefeed.NewAddMaintainerMessage(m.dest, m.maintainerEpoch)
9397
}
9498
return m.changefeed.NewRemoveMaintainerMessage(m.origin, false, false)
9599
}

‎coordinator/operator/operator_move_test.go‎

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ func TestMoveMaintainerOperator_CheckRequiresDestBootstrapDone(t *testing.T) {
8484
ChangefeedID: cfID,
8585
Config: config.GetDefaultReplicaConfig(),
8686
SinkURI: "mysql://127.0.0.1:3306",
87+
Epoch: 7,
8788
},
8889
1, true)
8990
changefeedDB.AddReplicatingMaintainer(cf, "n1")
@@ -93,16 +94,27 @@ func TestMoveMaintainerOperator_CheckRequiresDestBootstrapDone(t *testing.T) {
9394
op.Check("n1", &heartbeatpb.MaintainerStatus{State: heartbeatpb.ComponentState_Stopped})
9495
require.True(t, op.originNodeStopped)
9596
require.False(t, op.finished)
97+
req := op.Schedule().Message[0].(*heartbeatpb.AddMaintainerRequest)
98+
require.Equal(t, uint64(7), req.MaintainerEpoch)
9699

97100
op.Check("n2", &heartbeatpb.MaintainerStatus{
98-
State: heartbeatpb.ComponentState_Working,
99-
BootstrapDone: false,
101+
State: heartbeatpb.ComponentState_Working,
102+
BootstrapDone: true,
103+
MaintainerEpoch: 6,
100104
})
101105
require.False(t, op.finished)
102106

103107
op.Check("n2", &heartbeatpb.MaintainerStatus{
104-
State: heartbeatpb.ComponentState_Working,
105-
BootstrapDone: true,
108+
State: heartbeatpb.ComponentState_Working,
109+
BootstrapDone: false,
110+
MaintainerEpoch: 7,
111+
})
112+
require.False(t, op.finished)
113+
114+
op.Check("n2", &heartbeatpb.MaintainerStatus{
115+
State: heartbeatpb.ComponentState_Working,
116+
BootstrapDone: true,
117+
MaintainerEpoch: 7,
106118
})
107119
require.True(t, op.finished)
108120
require.Nil(t, op.Schedule())

0 commit comments

Comments
 (0)