@@ -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.
4242type 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.
8494func (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.
140150func (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.
0 commit comments