Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
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
11 changes: 9 additions & 2 deletions downstreamadapter/dispatcher/basic_dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -517,13 +517,20 @@ func (d *BasicDispatcher) GetLastSyncedTs() uint64 {
}

func (d *BasicDispatcher) GetCheckpointTs() uint64 {
// Event handling adds DML to tableProgress before advancing resolvedTs.
// Read resolvedTs first so an empty progress list cannot be combined with
// a newer resolvedTs that includes DML added after the progress read.
resolvedTs := d.GetResolvedTs()
checkpointTs, isEmpty := d.tableProgress.GetCheckpointTs()
failpoint.Inject("afterReadTableProgress", func() {
failpoint.Call("github.com/pingcap/ticdc/downstreamadapter/dispatcher/afterReadTableProgress", d)
})
if checkpointTs == 0 {
// This means the dispatcher has never send events to the sink,
// so we use resolvedTs as checkpointTs
checkpointTs = d.GetResolvedTs()
checkpointTs = resolvedTs
} else if isEmpty {
checkpointTs = max(checkpointTs, d.GetResolvedTs())
checkpointTs = max(checkpointTs, resolvedTs)
}

if d.addTableCheckpointBlocker != nil {
Expand Down
38 changes: 38 additions & 0 deletions downstreamadapter/dispatcher/basic_dispatcher_info_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,47 @@ package dispatcher
import (
"testing"

"github.com/pingcap/failpoint"
commonEvent "github.com/pingcap/ticdc/pkg/common/event"
"github.com/stretchr/testify/require"
)

func TestCheckpointTsDoesNotCombineOldProgressAndNewResolved(t *testing.T) {
for _, name := range []string{"noPreviousDML", "previousDMLFlushed"} {
t.Run(name, func(t *testing.T) {
d := &BasicDispatcher{tableProgress: NewTableProgress()}
d.resolvedTs.Store(100)
if name == "previousDMLFlushed" {
previous := &commonEvent.DMLEvent{StartTs: 80, CommitTs: 100}
d.tableProgress.Add(previous)
previous.PostFlush()
}
pending := &commonEvent.DMLEvent{StartTs: 90, CommitTs: 101}
injected := false
const point = "github.com/pingcap/ticdc/downstreamadapter/dispatcher/afterReadTableProgress"
require.NoError(t, failpoint.EnableCall(point, func(observed *BasicDispatcher) {
if observed != d {
return
}
injected = true
// Model an event handler enqueueing DML and then publishing resolved ts
// after the checkpoint reader has observed an empty progress list.
d.tableProgress.Add(pending)
d.resolvedTs.Store(800)
}))
t.Cleanup(func() { require.NoError(t, failpoint.Disable(point)) })
checkpoint := d.GetCheckpointTs()
if !injected {
t.Skip("requires failpoint instrumentation")
}
require.Equal(t, uint64(100), checkpoint)
require.NoError(t, failpoint.Disable(point))
pending.PostFlush()
require.Equal(t, uint64(800), d.GetCheckpointTs())
})
}
}

func TestBasicDispatcherStartTsConcurrentAccess(t *testing.T) {
d := &BasicDispatcher{}
const finalStartTs uint64 = 1000
Expand Down
65 changes: 15 additions & 50 deletions downstreamadapter/dispatchermanager/dispatcher_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,9 @@ func (e *DispatcherManager) newEventDispatchers(infos map[common.DispatcherID]di
d.Remove()
return newWritePathClosedError()
}
// Register progress and its generation before event delivery can report Working.
seq := e.dispatcherMap.Set(id, d)
d.SetSeq(seq)
if d.IsTableTriggerDispatcher() {
if util.GetOrZero(e.config.SinkConfig.SendAllBootstrapAtStart) {
d.BootstrapState = dispatcher.BootstrapNotStarted
Expand All @@ -669,11 +672,12 @@ func (e *DispatcherManager) newEventDispatchers(infos map[common.DispatcherID]di
// we don't register table trigger event dispatcher in event collector, when created.
// Table trigger event dispatcher is a special dispatcher,
// it need to wait get the initial table schema store from the maintainer, then will register to event collector to receive events.
failpoint.Inject("beforeRegisterDispatcher", func() {
failpoint.Call("github.com/pingcap/ticdc/downstreamadapter/dispatchermanager/beforeRegisterDispatcher", d)
})
appcontext.GetService[*eventcollector.EventCollector](appcontext.EventCollector).AddDispatcher(d, e.sinkQuota)
}

seq := e.dispatcherMap.Set(id, d)
d.SetSeq(seq)
e.writePathMu.Unlock()

if d.IsTableTriggerDispatcher() {
Expand Down Expand Up @@ -835,47 +839,16 @@ func (e *DispatcherManager) collectComponentStatusWhenChanged(ctx context.Contex
defer delay.Stop()
for {
statusMessage := make([]*heartbeatpb.TableSpanStatus, 0)
// why we need compare with latest watermark? for not backward the watermark?
watermark := e.latestWatermark.Get()
newWatermark := &heartbeatpb.Watermark{
CheckpointTs: watermark.CheckpointTs,
ResolvedTs: watermark.ResolvedTs,
Seq: watermark.Seq,
}
redoWatermark := e.latestRedoWatermark.Get()
newRedoWatermark := &heartbeatpb.Watermark{
CheckpointTs: redoWatermark.CheckpointTs,
ResolvedTs: redoWatermark.ResolvedTs,
Seq: redoWatermark.Seq,
}
var seq, redoSeq uint64
select {
case <-ctx.Done():
return
case tableSpanStatus := <-e.sharedInfo.GetStatusesChan():
statusMessage = append(statusMessage, tableSpanStatus.TableSpanStatus)
if common.IsDefaultMode(tableSpanStatus.Mode) {
// Note: tableSpanStatus.Seq is the seq assigned when that dispatcher was created.
// status messages can arrive out-of-order and Seq has no ordering relationship with
// per-dispatcher checkpoint/resolved ts.
//
// - Keep Watermark.Seq monotonic to avoid maintainer dropping the watermark as stale
// while still applying status updates (statuses are handled regardless of watermark Seq).
// - Still update CheckpointTs with min() regardless of Seq ordering; the periodic
// aggregateDispatcherHeartbeats() is responsible for advancing the watermark.
if newWatermark.Seq < tableSpanStatus.Seq {
newWatermark.Seq = tableSpanStatus.Seq
}
if tableSpanStatus.CheckpointTs != 0 && tableSpanStatus.CheckpointTs < newWatermark.CheckpointTs {
newWatermark.CheckpointTs = tableSpanStatus.CheckpointTs
}
seq = tableSpanStatus.Seq
} else {
// Same rule applies to redo watermark.
if newRedoWatermark.Seq < tableSpanStatus.Seq {
newRedoWatermark.Seq = tableSpanStatus.Seq
}
if tableSpanStatus.CheckpointTs != 0 && tableSpanStatus.CheckpointTs < newRedoWatermark.CheckpointTs {
newRedoWatermark.CheckpointTs = tableSpanStatus.CheckpointTs
}
redoSeq = tableSpanStatus.Seq
}
delay.Reset(10 * time.Millisecond)
loop:
Expand All @@ -884,19 +857,9 @@ func (e *DispatcherManager) collectComponentStatusWhenChanged(ctx context.Contex
case tableSpanStatus := <-e.sharedInfo.GetStatusesChan():
statusMessage = append(statusMessage, tableSpanStatus.TableSpanStatus)
if common.IsDefaultMode(tableSpanStatus.Mode) {
if newWatermark.Seq < tableSpanStatus.Seq {
newWatermark.Seq = tableSpanStatus.Seq
}
if tableSpanStatus.CheckpointTs != 0 && tableSpanStatus.CheckpointTs < newWatermark.CheckpointTs {
newWatermark.CheckpointTs = tableSpanStatus.CheckpointTs
}
seq = max(seq, tableSpanStatus.Seq)
} else {
if newRedoWatermark.Seq < tableSpanStatus.Seq {
newRedoWatermark.Seq = tableSpanStatus.Seq
}
if tableSpanStatus.CheckpointTs != 0 && tableSpanStatus.CheckpointTs < newRedoWatermark.CheckpointTs {
newRedoWatermark.CheckpointTs = tableSpanStatus.CheckpointTs
}
redoSeq = max(redoSeq, tableSpanStatus.Seq)
}
case <-delay.C:
break loop
Expand All @@ -905,8 +868,10 @@ func (e *DispatcherManager) collectComponentStatusWhenChanged(ctx context.Contex
var message heartbeatpb.HeartBeatRequest
message.ChangefeedID = e.changefeedID.ToPB()
message.Statuses = statusMessage
message.Watermark = newWatermark
message.RedoWatermark = newRedoWatermark
// Read the aggregate after collecting statuses. Its generation must cover
// all new dispatchers before their Working statuses can finish operators.
message.Watermark = getStatusWatermark(&e.latestWatermark, e.dispatcherMap, seq)
message.RedoWatermark = getStatusWatermark(&e.latestRedoWatermark, e.redoDispatcherMap, redoSeq)
e.heartbeatRequestQueue.Enqueue(&HeartBeatRequestWithTargetID{TargetID: e.GetMaintainerID(), Request: &message})
}
}
Expand Down
31 changes: 31 additions & 0 deletions downstreamadapter/dispatchermanager/dispatcher_manager_helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"math"
"time"

"github.com/pingcap/failpoint"
"github.com/pingcap/log"
"github.com/pingcap/ticdc/downstreamadapter/dispatcher"
"github.com/pingcap/ticdc/downstreamadapter/eventcollector"
Expand All @@ -28,6 +29,36 @@ import (
"go.uber.org/zap"
)

// getStatusWatermark reuses a periodic aggregate only if it covers the current
// dispatcher generation. Raising an older aggregate's Seq would hide unflushed
// events from dispatchers missing from that aggregate.
func getStatusWatermark[T dispatcher.Dispatcher](cached *Watermark, dispatchers *DispatcherMap[T], seq uint64) *heartbeatpb.Watermark {
if dispatchers != nil {
seq = max(seq, dispatchers.GetSeq())
}
watermark := cached.Get()
if watermark.Seq >= seq {
return watermark
}

// Only collect progress here; lifecycle cleanup remains with the periodic
// heartbeat task, which must not run concurrently with another cleanup pass.
watermark = heartbeatpb.NewMaxWatermark()
watermark.Seq = dispatchers.ForEach(func(_ common.DispatcherID, d T) {
status := d.GetComponentStatus()
if status == heartbeatpb.ComponentState_Preparing || status == heartbeatpb.ComponentState_MergeReady {
failpoint.Inject("afterSkipMergeDispatcher", func() {
failpoint.Call("github.com/pingcap/ticdc/downstreamadapter/dispatchermanager/afterSkipMergeDispatcher", d)
})
return
}
var info dispatcher.HeartBeatInfo
d.GetHeartBeatInfo(&info)
watermark.Update(info.Watermark)
})
return watermark
}

func getDispatcherStatus(id common.DispatcherID, dispatcherItem dispatcher.Dispatcher, needCompleteStatus bool) (*heartbeatpb.TableSpanStatus, *cleanMap, *heartbeatpb.Watermark) {
heartBeatInfo := &dispatcher.HeartBeatInfo{}
// the merged dispatcher in preparing state, don't need to join the calculation of the heartbeat
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import (
"math"
"time"

"github.com/pingcap/failpoint"
"github.com/pingcap/log"
"github.com/pingcap/ticdc/downstreamadapter/dispatcher"
"github.com/pingcap/ticdc/downstreamadapter/eventcollector"
Expand Down Expand Up @@ -220,15 +221,19 @@ func (e *DispatcherManager) newRedoDispatchers(infos map[common.DispatcherID]dis
rd.Remove()
return newWritePathClosedError()
}
// Register progress and its generation before event delivery can report Working.
redoSeq := e.redoDispatcherMap.Set(rd.GetId(), rd)
rd.SetSeq(redoSeq)
if rd.IsTableTriggerDispatcher() {
e.SetTableTriggerRedoDispatcher(rd)
} else {
e.redoSchemaIDToDispatchers.Set(schemaIds[idx], id)
failpoint.Inject("beforeRegisterDispatcher", func() {
failpoint.Call("github.com/pingcap/ticdc/downstreamadapter/dispatchermanager/beforeRegisterDispatcher", rd)
})
appcontext.GetService[*eventcollector.EventCollector](appcontext.EventCollector).AddDispatcher(rd, e.redoQuota)
}

redoSeq := e.redoDispatcherMap.Set(rd.GetId(), rd)
rd.SetSeq(redoSeq)
e.writePathMu.Unlock()

if rd.IsTableTriggerDispatcher() {
Expand Down
Loading
Loading