Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
57df930
coordinator: persist maintainer epochs before ownership changes
hongyunyan Jun 17, 2026
38669c8
maintainer,dispatcher: fence stale maintainer epochs
hongyunyan Jun 17, 2026
260e153
downstreamadapter: harden table trigger takeover
hongyunyan Jun 17, 2026
0d8b952
coordinator: address resume review feedback
hongyunyan Jun 22, 2026
a4f7d30
Merge branch 'codex/pr-5182-epoch-persistence' into codex/pr-5182-sta…
hongyunyan Jun 22, 2026
1953b27
Merge branch 'codex/pr-5182-stale-maintainer-fence' into codex/pr-518…
hongyunyan Jun 22, 2026
06a6788
coordinator: refresh changefeed backend mock
hongyunyan Jun 23, 2026
7366373
Merge branch 'codex/pr-5182-epoch-persistence' into codex/pr-5182-sta…
hongyunyan Jun 23, 2026
22291bc
Merge branch 'codex/pr-5182-stale-maintainer-fence' into codex/pr-518…
hongyunyan Jun 23, 2026
cd9e0b5
pkg: use canonical errors import
hongyunyan Jun 23, 2026
f10352d
Merge branch 'codex/pr-5182-epoch-persistence' into codex/pr-5182-sta…
hongyunyan Jun 23, 2026
5b69301
Merge branch 'codex/pr-5182-stale-maintainer-fence' into codex/pr-518…
hongyunyan Jun 23, 2026
45f36e3
Merge remote-tracking branch 'upstream/master' into codex/pr-5182-sta…
hongyunyan Jun 24, 2026
0c6b72e
Merge branch 'codex/pr-5182-stale-maintainer-fence' into codex/pr-518…
hongyunyan Jun 24, 2026
95b9bd5
downstreamadapter: release fence on post bootstrap close
hongyunyan Jun 24, 2026
21bd769
downstreamadapter: fence redo control messages
hongyunyan Jun 24, 2026
92359b5
downstreamadapter: preserve owner on rejected trigger takeover
hongyunyan Jun 24, 2026
6d8ee44
downstreamadapter: cover maintainer owner admission check
hongyunyan Jun 24, 2026
b7ef383
downstreamadapter: document nil table trigger bootstrap
hongyunyan Jun 24, 2026
788ffda
downstreamadapter: clarify nil trigger bootstrap invariant
hongyunyan Jun 24, 2026
4dcaaca
downstreamadapter: simplify maintainer fence handling
hongyunyan Jun 25, 2026
97a2b51
Merge remote-tracking branch 'upstream/master' into codex/pr-5182-tri…
hongyunyan Jun 29, 2026
da9d721
dispatcherorchestrator: ignore fenced bootstrap trigger errors
hongyunyan Jun 29, 2026
8b195f4
dispatcherorchestrator: harden nil trigger bootstrap
hongyunyan Jun 29, 2026
6f6400e
dispatcherorchestrator: merge bootstrap trigger helpers
hongyunyan Jun 29, 2026
9057d1d
dispatcherorchestrator: simplify bootstrap tests
hongyunyan Jun 29, 2026
247fdf7
dispatcherorchestrator: isolate bootstrap test services
hongyunyan Jun 29, 2026
20c4abd
dispatcherorchestrator: satisfy static lint
hongyunyan Jun 29, 2026
f5dd38f
dispatchermanager: shrink redo resolved wakeup fence
hongyunyan Jun 29, 2026
fb5a943
dispatcherorchestrator: tighten bootstrap response recovery
hongyunyan Jun 29, 2026
e46e493
dispatchermanager: simplify maintainer admission checks
hongyunyan Jun 29, 2026
66db419
dispatchermanager: trim maintainer admission work
hongyunyan Jun 29, 2026
3c2766f
dispatcherorchestrator: reuse post bootstrap trigger lookup
hongyunyan Jun 29, 2026
f73bc29
dispatcherorchestrator: report missing post bootstrap trigger
hongyunyan Jun 29, 2026
0610191
dispatcherorchestrator,dispatchermanager: simplify maintainer checks
hongyunyan Jun 29, 2026
c020ba1
downstreamadapter: trim maintainer cleanup code
hongyunyan Jun 29, 2026
93e1344
downstreamadapter: reuse maintainer update path
hongyunyan Jun 30, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 52 additions & 5 deletions downstreamadapter/dispatchermanager/dispatcher_manager_info.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,27 @@ func (e *DispatcherManager) GetMaintainerID() node.ID {
func (e *DispatcherManager) TryUpdateMaintainer(from node.ID, maintainerEpoch uint64) bool {
e.meta.Lock()
defer e.meta.Unlock()
if !e.canUpdateMaintainerLocked(from, maintainerEpoch) {
return false
}
e.meta.maintainerEpoch = maintainerEpoch
e.meta.maintainerID = from
return true
}

// CanUpdateMaintainer reports whether a bootstrap request can become the
// dispatcher manager owner without mutating the current owner/epoch.
func (e *DispatcherManager) CanUpdateMaintainer(from node.ID, maintainerEpoch uint64) bool {
e.meta.Lock()
defer e.meta.Unlock()
return e.canUpdateMaintainerLocked(from, maintainerEpoch)
}

func (e *DispatcherManager) canUpdateMaintainerLocked(from node.ID, maintainerEpoch uint64) bool {
if maintainerEpoch == 0 {
if e.meta.maintainerEpoch != 0 {
return false
}
e.meta.maintainerID = from
return true
}
if e.meta.maintainerEpoch > maintainerEpoch {
Expand All @@ -72,20 +88,51 @@ func (e *DispatcherManager) TryUpdateMaintainer(from node.ID, maintainerEpoch ui
if e.meta.maintainerEpoch == maintainerEpoch && e.meta.maintainerID != "" && e.meta.maintainerID != from {
return false
}
e.meta.maintainerEpoch = maintainerEpoch
e.meta.maintainerID = from
return true
}

// maintainerRequestAdmission is a single meta-lock snapshot for request fencing
// and stale-request logs.
type maintainerRequestAdmission struct {
allowed bool
currentEpoch uint64
currentMaintainer node.ID
}

// IsMaintainerRequestAllowed reports whether a request belongs to the current
// maintainer owner/epoch view known by this dispatcher manager.
func (e *DispatcherManager) IsMaintainerRequestAllowed(from node.ID, maintainerEpoch uint64) bool {
return e.maintainerRequestAdmission(from, maintainerEpoch).allowed
}

func (e *DispatcherManager) maintainerRequestAdmission(from node.ID, maintainerEpoch uint64) maintainerRequestAdmission {
e.meta.Lock()
defer e.meta.Unlock()
admission := maintainerRequestAdmission{
currentEpoch: e.meta.maintainerEpoch,
currentMaintainer: e.meta.maintainerID,
}
admission.allowed = IsMaintainerRequestAllowedBySnapshot(
from,
maintainerEpoch,
admission.currentMaintainer,
admission.currentEpoch,
)
return admission
}

// IsMaintainerRequestAllowedBySnapshot applies maintainer admission rules to a
// caller-held owner/epoch snapshot.
func IsMaintainerRequestAllowedBySnapshot(
from node.ID,
maintainerEpoch uint64,
currentMaintainer node.ID,
currentMaintainerEpoch uint64,
) bool {
if maintainerEpoch == 0 {
return e.meta.maintainerEpoch == 0 && (e.meta.maintainerID == "" || e.meta.maintainerID == from)
return currentMaintainerEpoch == 0 && (currentMaintainer == "" || currentMaintainer == from)
}
return e.meta.maintainerEpoch == maintainerEpoch && e.meta.maintainerID == from
return currentMaintainerEpoch == maintainerEpoch && currentMaintainer == from
}

func (e *DispatcherManager) GetMaintainerEpoch() uint64 {
Expand Down
4 changes: 2 additions & 2 deletions downstreamadapter/dispatchermanager/heartbeat_collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -280,12 +280,12 @@ func (c *HeartBeatCollector) RecvMessages(_ context.Context, msg *messaging.Targ
redoMessage := msg.Message[0].(*heartbeatpb.RedoResolvedTsForwardMessage)
c.redoResolvedTsForwardMessageDynamicStream.Push(
common.NewChangefeedGIDFromPB(redoMessage.ChangefeedID),
NewRedoResolvedTsForwardMessage(redoMessage))
NewRedoResolvedTsForwardMessage(msg.From, redoMessage))
case messaging.TypeRedoMetaMessage:
redoMessage := msg.Message[0].(*heartbeatpb.RedoMetaMessage)
c.redoMetaMessageDynamicStream.Push(
common.NewChangefeedGIDFromPB(redoMessage.ChangefeedID),
NewRedoMetaMessage(redoMessage))
NewRedoMetaMessage(msg.From, redoMessage))
case messaging.TypeMergeDispatcherRequest:
mergeDispatcherRequest := msg.Message[0].(*heartbeatpb.MergeDispatcherRequest)
c.mergeDispatcherRequestDynamicStream.Push(
Expand Down
143 changes: 113 additions & 30 deletions downstreamadapter/dispatchermanager/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -277,14 +277,15 @@ func preCheckForSchedulerHandler(req SchedulerDispatcherRequest, dispatcherManag
log.Warn("scheduleDispatcherRequest has no valid operator key, skip")
return common.DispatcherID{}, false
}
if !dispatcherManager.IsMaintainerRequestAllowed(req.From, req.MaintainerEpoch) {
log.Warn("drop stale schedule dispatcher request",
zap.String("changefeedID", req.ChangefeedID.String()),
zap.String("dispatcherID", dispatcherID.String()),
zap.String("from", req.From.String()),
zap.Uint64("requestMaintainerEpoch", req.MaintainerEpoch),
zap.Uint64("currentMaintainerEpoch", dispatcherManager.GetMaintainerEpoch()),
zap.String("currentMaintainer", dispatcherManager.GetMaintainerID().String()))
if !isMaintainerControlMessageAllowed(
dispatcherManager,
"drop stale schedule dispatcher request",
"requestMaintainerEpoch",
req.ChangefeedID,
req.From,
req.MaintainerEpoch,
zap.String("dispatcherID", dispatcherID.String()),
) {
return common.DispatcherID{}, false
}
isRedo := common.IsRedoMode(req.Config.Mode)
Expand Down Expand Up @@ -593,16 +594,14 @@ func (h *HeartBeatResponseHandler) Handle(dispatcherManager *DispatcherManager,
// isHeartBeatResponseAllowed drops dispatcher heartbeats from stale maintainers
// before they can update table state or complete scheduler operators.
func isHeartBeatResponseAllowed(dispatcherManager *DispatcherManager, heartbeatResponse HeartBeatResponse) bool {
if dispatcherManager.IsMaintainerRequestAllowed(heartbeatResponse.From, heartbeatResponse.MaintainerEpoch) {
return true
}
log.Warn("drop stale heartbeat response",
zap.String("changefeedID", heartbeatResponse.ChangefeedID.String()),
zap.String("from", heartbeatResponse.From.String()),
zap.Uint64("responseMaintainerEpoch", heartbeatResponse.MaintainerEpoch),
zap.Uint64("currentMaintainerEpoch", dispatcherManager.GetMaintainerEpoch()),
zap.String("currentMaintainer", dispatcherManager.GetMaintainerID().String()))
return false
return isMaintainerControlMessageAllowed(
dispatcherManager,
"drop stale heartbeat response",
"responseMaintainerEpoch",
heartbeatResponse.ChangefeedID,
heartbeatResponse.From,
heartbeatResponse.MaintainerEpoch,
)
}

func (h *HeartBeatResponseHandler) GetSize(event HeartBeatResponse) int { return 0 }
Expand Down Expand Up @@ -692,11 +691,14 @@ func newRedoResolvedTsForwardMessageDynamicStream() dynstream.DynamicStream[int,
}

type RedoResolvedTsForwardMessage struct {
From node.ID
*heartbeatpb.RedoResolvedTsForwardMessage
}

func NewRedoResolvedTsForwardMessage(msg *heartbeatpb.RedoResolvedTsForwardMessage) RedoResolvedTsForwardMessage {
return RedoResolvedTsForwardMessage{msg}
// NewRedoResolvedTsForwardMessage carries the sender node with redo
// resolved-ts updates so stale maintainers cannot unblock redo dispatchers.
func NewRedoResolvedTsForwardMessage(from node.ID, msg *heartbeatpb.RedoResolvedTsForwardMessage) RedoResolvedTsForwardMessage {
return RedoResolvedTsForwardMessage{From: from, RedoResolvedTsForwardMessage: msg}
}

type RedoResolvedTsForwardMessageHandler struct{}
Expand All @@ -715,7 +717,15 @@ func (h *RedoResolvedTsForwardMessageHandler) Handle(dispatcherManager *Dispatch
panic("invalid message count")
}
msg := messages[0]
dispatcherManager.MaintainerFenceMu.Lock()
if !isRedoResolvedTsForwardMessageAllowed(dispatcherManager, msg) {
dispatcherManager.MaintainerFenceMu.Unlock()
return false
}
ok := dispatcherManager.SetRedoResolvedTs(msg.ResolvedTs)
// Redo resolved-ts is already atomic; wake dispatchers outside the maintainer
// fence so scheduler/bootstrap requests do not wait for a full dispatcher scan.
dispatcherManager.MaintainerFenceMu.Unlock()
if ok {
dispatcherManager.dispatcherMap.ForEach(func(_ common.DispatcherID, dispatcher *dispatcher.EventDispatcher) {
dispatcher.HandleCacheEvents()
Expand All @@ -724,6 +734,19 @@ func (h *RedoResolvedTsForwardMessageHandler) Handle(dispatcherManager *Dispatch
return false
}

// isRedoResolvedTsForwardMessageAllowed drops redo resolved-ts updates from
// stale maintainers before they can unblock cached events.
func isRedoResolvedTsForwardMessageAllowed(dispatcherManager *DispatcherManager, msg RedoResolvedTsForwardMessage) bool {
return isMaintainerControlMessageAllowed(
dispatcherManager,
"drop stale redo resolved ts forward message",
"requestMaintainerEpoch",
msg.ChangefeedID,
msg.From,
msg.MaintainerEpoch,
)
}

func (h *RedoResolvedTsForwardMessageHandler) GetSize(event RedoResolvedTsForwardMessage) int {
return 0
}
Expand Down Expand Up @@ -761,11 +784,14 @@ func newRedoMetaMessageDynamicStream() dynstream.DynamicStream[int, common.GID,
}

type RedoMetaMessage struct {
From node.ID
*heartbeatpb.RedoMetaMessage
}

func NewRedoMetaMessage(msg *heartbeatpb.RedoMetaMessage) RedoMetaMessage {
return RedoMetaMessage{msg}
// NewRedoMetaMessage carries the sender node with redo meta updates so redo
// metadata cannot be advanced by stale maintainers after ownership changes.
func NewRedoMetaMessage(from node.ID, msg *heartbeatpb.RedoMetaMessage) RedoMetaMessage {
return RedoMetaMessage{From: from, RedoMetaMessage: msg}
}

type RedoMetaMessageHandler struct{}
Expand All @@ -783,13 +809,69 @@ func (h *RedoMetaMessageHandler) Handle(dispatcherManager *DispatcherManager, me
// TODO: Support batch
panic("invalid message count")
}
msg := messages[0]
dispatcherManager.MaintainerFenceMu.Lock()
defer dispatcherManager.MaintainerFenceMu.Unlock()
if !isRedoMetaMessageAllowed(dispatcherManager, msg) {
return false
}
if dispatcherManager.GetTableTriggerRedoDispatcher() != nil {
msg := messages[0]
dispatcherManager.UpdateRedoMeta(msg.CheckpointTs, msg.ResolvedTs)
}
return false
}

// isRedoMetaMessageAllowed drops redo meta updates from stale maintainers
// before they can advance redo recovery boundaries.
func isRedoMetaMessageAllowed(dispatcherManager *DispatcherManager, msg RedoMetaMessage) bool {
return isMaintainerControlMessageAllowed(
dispatcherManager,
"drop stale redo meta message",
"requestMaintainerEpoch",
msg.ChangefeedID,
msg.From,
msg.MaintainerEpoch,
)
}

func isMaintainerControlMessageAllowed(
dispatcherManager *DispatcherManager,
logMessage string,
epochField string,
changefeedID *heartbeatpb.ChangefeedID,
from node.ID,
maintainerEpoch uint64,
extraFields ...zap.Field,
) bool {
admission := dispatcherManager.maintainerRequestAdmission(from, maintainerEpoch)
if admission.allowed {
return true
}
logStaleMaintainerControlMessage(logMessage, epochField, changefeedID, from, maintainerEpoch, admission, extraFields...)
return false
}

func logStaleMaintainerControlMessage(
logMessage string,
epochField string,
changefeedID *heartbeatpb.ChangefeedID,
from node.ID,
maintainerEpoch uint64,
admission maintainerRequestAdmission,
extraFields ...zap.Field,
) {
fields := make([]zap.Field, 0, 5+len(extraFields))
fields = append(fields,
zap.String("changefeedID", changefeedID.String()),
zap.String("from", from.String()),
zap.Uint64(epochField, maintainerEpoch),
zap.Uint64("currentMaintainerEpoch", admission.currentEpoch),
zap.String("currentMaintainer", admission.currentMaintainer.String()),
)
fields = append(fields, extraFields...)
log.Warn(logMessage, fields...)
}

func (h *RedoMetaMessageHandler) GetSize(event RedoMetaMessage) int { return 0 }
func (h *RedoMetaMessageHandler) IsPaused(event RedoMetaMessage) bool { return false }
func (h *RedoMetaMessageHandler) GetArea(_ common.GID, _ *DispatcherManager) int {
Expand Down Expand Up @@ -844,13 +926,14 @@ func (h *MergeDispatcherRequestHandler) Handle(dispatcherManager *DispatcherMana
mergeDispatcherRequest := reqs[0]
dispatcherManager.MaintainerFenceMu.Lock()
defer dispatcherManager.MaintainerFenceMu.Unlock()
if !dispatcherManager.IsMaintainerRequestAllowed(mergeDispatcherRequest.From, mergeDispatcherRequest.MaintainerEpoch) {
log.Warn("drop stale merge dispatcher request",
zap.String("changefeedID", mergeDispatcherRequest.ChangefeedID.String()),
zap.String("from", mergeDispatcherRequest.From.String()),
zap.Uint64("requestMaintainerEpoch", mergeDispatcherRequest.MaintainerEpoch),
zap.Uint64("currentMaintainerEpoch", dispatcherManager.GetMaintainerEpoch()),
zap.String("currentMaintainer", dispatcherManager.GetMaintainerID().String()))
if !isMaintainerControlMessageAllowed(
dispatcherManager,
"drop stale merge dispatcher request",
"requestMaintainerEpoch",
mergeDispatcherRequest.ChangefeedID,
mergeDispatcherRequest.From,
mergeDispatcherRequest.MaintainerEpoch,
) {
return false
}
dispatcherIDs := make([]common.DispatcherID, 0, len(mergeDispatcherRequest.DispatcherIDs))
Expand Down
72 changes: 72 additions & 0 deletions downstreamadapter/dispatchermanager/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -263,6 +263,12 @@ func TestDispatcherManagerTryUpdateMaintainerEpoch(t *testing.T) {
strictDM.meta.maintainerID = "current-maintainer"
strictDM.meta.maintainerEpoch = 2

require.False(t, strictDM.CanUpdateMaintainer("current-maintainer", 0))
require.Equal(t, node.ID("current-maintainer"), strictDM.GetMaintainerID())
require.Equal(t, uint64(2), strictDM.GetMaintainerEpoch())
require.True(t, strictDM.CanUpdateMaintainer("new-maintainer", 3))
require.Equal(t, node.ID("current-maintainer"), strictDM.GetMaintainerID())
require.Equal(t, uint64(2), strictDM.GetMaintainerEpoch())
require.False(t, strictDM.TryUpdateMaintainer("current-maintainer", 0))
require.Equal(t, node.ID("current-maintainer"), strictDM.GetMaintainerID())
require.Equal(t, uint64(2), strictDM.GetMaintainerEpoch())
Expand Down Expand Up @@ -361,6 +367,72 @@ func TestHeartBeatResponseHandlerDropsStaleMaintainerEpoch(t *testing.T) {
})
}

func TestRedoControlMessagesAllowedByMaintainerEpoch(t *testing.T) {
t.Parallel()

changefeedID := common.NewChangeFeedIDWithName("test-changefeed", "test-namespace")
strictDM := &DispatcherManager{
changefeedID: changefeedID,
}
strictDM.meta.maintainerID = "current-maintainer"
strictDM.meta.maintainerEpoch = 2

newRedoMeta := func(from node.ID, epoch uint64) RedoMetaMessage {
return NewRedoMetaMessage(from, &heartbeatpb.RedoMetaMessage{
ChangefeedID: changefeedID.ToPB(),
CheckpointTs: 100,
ResolvedTs: 200,
MaintainerEpoch: epoch,
})
}
newRedoResolved := func(from node.ID, epoch uint64) RedoResolvedTsForwardMessage {
return NewRedoResolvedTsForwardMessage(from, &heartbeatpb.RedoResolvedTsForwardMessage{
ChangefeedID: changefeedID.ToPB(),
ResolvedTs: 200,
MaintainerEpoch: epoch,
})
}

require.False(t, isRedoMetaMessageAllowed(strictDM, newRedoMeta("old-maintainer", 1)))
require.True(t, isRedoMetaMessageAllowed(strictDM, newRedoMeta("current-maintainer", 2)))
require.False(t, isRedoResolvedTsForwardMessageAllowed(strictDM, newRedoResolved("old-maintainer", 1)))
require.True(t, isRedoResolvedTsForwardMessageAllowed(strictDM, newRedoResolved("current-maintainer", 2)))

compatDM := &DispatcherManager{
changefeedID: changefeedID,
}
compatDM.meta.maintainerID = "compat-maintainer"
require.True(t, isRedoMetaMessageAllowed(compatDM, newRedoMeta("compat-maintainer", 0)))
}

func TestRedoResolvedTsForwardMessageHandlerDropsStaleMaintainerEpoch(t *testing.T) {
t.Parallel()

changefeedID := common.NewChangeFeedIDWithName("test-changefeed", "test-namespace")
dispatcherManager := &DispatcherManager{
changefeedID: changefeedID,
dispatcherMap: newDispatcherMap[*dispatcher.EventDispatcher](),
}
dispatcherManager.meta.maintainerID = "current-maintainer"
dispatcherManager.meta.maintainerEpoch = 2
dispatcherManager.redoGlobalTs.Store(50)

handler := &RedoResolvedTsForwardMessageHandler{}
newResolvedMessage := func(from node.ID, resolvedTs, epoch uint64) RedoResolvedTsForwardMessage {
return NewRedoResolvedTsForwardMessage(from, &heartbeatpb.RedoResolvedTsForwardMessage{
ChangefeedID: changefeedID.ToPB(),
ResolvedTs: resolvedTs,
MaintainerEpoch: epoch,
})
}

require.False(t, handler.Handle(dispatcherManager, newResolvedMessage("old-maintainer", 100, 1)))
require.Equal(t, uint64(50), dispatcherManager.redoGlobalTs.Load())

require.False(t, handler.Handle(dispatcherManager, newResolvedMessage("current-maintainer", 120, 2)))
require.Equal(t, uint64(120), dispatcherManager.redoGlobalTs.Load())
}

func TestDispatcherManagerIsRedoReadyRequiresPublication(t *testing.T) {
t.Parallel()

Expand Down
Loading
Loading