Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

puller(cdc): debug frontier (#10202) #10271

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
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
21 changes: 17 additions & 4 deletions cdc/kv/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1331,7 +1331,7 @@ func (s *eventFeedSession) getStreamCancel(storeAddr string) (cancel context.Can
}

func (s *eventFeedSession) logSlowRegions(ctx context.Context) error {
ticker := time.NewTicker(10 * time.Second)
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
select {
Expand All @@ -1340,12 +1340,18 @@ func (s *eventFeedSession) logSlowRegions(ctx context.Context) error {
case <-ticker.C:
}

currTime := s.client.pdClock.CurrentTime()
attr := s.rangeLock.CollectLockedRangeAttrs(nil)
ckptTime := oracle.GetTimeFromTS(attr.SlowestRegion.CheckpointTs)
currTime := s.client.pdClock.CurrentTime()
log.Info("event feed starts to check locked regions",
zap.String("namespace", s.changefeed.Namespace),
zap.String("changefeed", s.changefeed.ID),
zap.Int64("tableID", s.tableID),
zap.String("tableName", s.tableName))

if attr.SlowestRegion.Initialized {
ckptTime := oracle.GetTimeFromTS(attr.SlowestRegion.CheckpointTs)
if currTime.Sub(ckptTime) > 2*resolveLockMinInterval {
log.Info("event feed finds a slow region",
log.Info("event feed finds a initialized slow region",
zap.String("namespace", s.changefeed.Namespace),
zap.String("changefeed", s.changefeed.ID),
zap.Int64("tableID", s.tableID),
Expand All @@ -1359,6 +1365,13 @@ func (s *eventFeedSession) logSlowRegions(ctx context.Context) error {
zap.Int64("tableID", s.tableID),
zap.String("tableName", s.tableName),
zap.Any("slowRegion", attr.SlowestRegion))
} else if currTime.Sub(ckptTime) > 10*time.Minute {
log.Info("event feed finds a uninitialized slow region",
zap.String("namespace", s.changefeed.Namespace),
zap.String("changefeed", s.changefeed.ID),
zap.Int64("tableID", s.tableID),
zap.String("tableName", s.tableName),
zap.Any("slowRegion", attr.SlowestRegion))
}
if len(attr.Holes) > 0 {
holes := make([]string, 0, len(attr.Holes))
Expand Down
15 changes: 12 additions & 3 deletions cdc/kv/shared_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -696,23 +696,26 @@ func (s *SharedClient) resolveLock(ctx context.Context) error {
}

func (s *SharedClient) logSlowRegions(ctx context.Context) error {
ticker := time.NewTicker(10 * time.Second)
ticker := time.NewTicker(30 * time.Second)
defer ticker.Stop()
for {
select {
case <-ctx.Done():
return ctx.Err()
case <-ticker.C:
}
log.Info("event feed starts to check locked regions",
zap.String("namespace", s.changefeed.Namespace),
zap.String("changefeed", s.changefeed.ID))

currTime := s.pdClock.CurrentTime()
s.totalSpans.RLock()
for subscriptionID, rt := range s.totalSpans.v {
attr := rt.rangeLock.CollectLockedRangeAttrs(nil)
ckptTime := oracle.GetTimeFromTS(attr.SlowestRegion.CheckpointTs)
if attr.SlowestRegion.Initialized {
ckptTime := oracle.GetTimeFromTS(attr.SlowestRegion.CheckpointTs)
if currTime.Sub(ckptTime) > 2*resolveLockMinInterval {
log.Info("event feed finds a slow region",
log.Info("event feed finds a initialized slow region",
zap.String("namespace", s.changefeed.Namespace),
zap.String("changefeed", s.changefeed.ID),
zap.Any("subscriptionID", subscriptionID),
Expand All @@ -724,6 +727,12 @@ func (s *SharedClient) logSlowRegions(ctx context.Context) error {
zap.String("changefeed", s.changefeed.ID),
zap.Any("subscriptionID", subscriptionID),
zap.Any("slowRegion", attr.SlowestRegion))
} else if currTime.Sub(ckptTime) > 10*time.Minute {
log.Info("event feed finds a uninitialized slow region",
zap.String("namespace", s.changefeed.Namespace),
zap.String("changefeed", s.changefeed.ID),
zap.Any("subscriptionID", subscriptionID),
zap.Any("slowRegion", attr.SlowestRegion))
}
if len(attr.Holes) > 0 {
holes := make([]string, 0, len(attr.Holes))
Expand Down
1 change: 1 addition & 0 deletions cdc/puller/frontier/frontier.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ type Frontier interface {
Forward(regionID uint64, span tablepb.Span, ts uint64)
Frontier() uint64
String() string
Entries(fn func(key []byte, ts uint64))
}

// spanFrontier tracks the minimum timestamp of a set of spans.
Expand Down
44 changes: 44 additions & 0 deletions cdc/puller/frontier/frontier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package frontier

import (
"bytes"
"math"
"math/rand"
"sort"
"testing"
Expand Down Expand Up @@ -391,3 +392,46 @@ func TestMinMaxWithRegionSplitMerge(t *testing.T) {
f.Forward(8, tablepb.Span{StartKey: []byte("d"), EndKey: []byte("e")}, 5)
require.Equal(t, uint64(5), f.Frontier())
}

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

ab := tablepb.Span{StartKey: []byte("a"), EndKey: []byte("b")}
bc := tablepb.Span{StartKey: []byte("b"), EndKey: []byte("c")}
cd := tablepb.Span{StartKey: []byte("c"), EndKey: []byte("d")}
de := tablepb.Span{StartKey: []byte("d"), EndKey: []byte("e")}
ef := tablepb.Span{StartKey: []byte("e"), EndKey: []byte("f")}
af := tablepb.Span{StartKey: []byte("a"), EndKey: []byte("f")}
f := NewFrontier(0, af)

var slowestTs uint64 = math.MaxUint64
var slowestRange tablepb.Span
getSlowestRange := func() {
slowestTs = math.MaxUint64
slowestRange = tablepb.Span{}
f.Entries(func(key []byte, ts uint64) {
if ts < slowestTs {
slowestTs = ts
slowestRange.StartKey = key
slowestRange.EndKey = nil
} else if slowestTs != math.MaxUint64 && len(slowestRange.EndKey) == 0 {
slowestRange.EndKey = key
}
})
}

getSlowestRange()
require.Equal(t, uint64(0), slowestTs)
require.Equal(t, []byte("a"), []byte(slowestRange.StartKey))
require.Equal(t, []byte("f"), []byte(slowestRange.EndKey))

f.Forward(1, ab, 100)
f.Forward(2, bc, 200)
f.Forward(3, cd, 300)
f.Forward(4, de, 400)
f.Forward(5, ef, 500)
getSlowestRange()
require.Equal(t, uint64(100), slowestTs)
require.Equal(t, []byte("a"), []byte(slowestRange.StartKey))
require.Equal(t, []byte("b"), []byte(slowestRange.EndKey))
}
33 changes: 32 additions & 1 deletion cdc/puller/puller.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package puller

import (
"context"
"math"
"sync/atomic"
"time"

Expand Down Expand Up @@ -146,6 +147,8 @@ func (p *pullerImpl) Run(ctx context.Context) error {
WithLabelValues(p.changefeed.Namespace, p.changefeed.ID, "resolved")

lastResolvedTs := p.checkpointTs
lastAdvancedTime := time.Now()
lastLogSlowRangeTime := time.Now()
g.Go(func() error {
stuckDetectorTicker := time.NewTicker(1 * time.Minute)
defer stuckDetectorTicker.Stop()
Expand Down Expand Up @@ -234,10 +237,38 @@ func (p *pullerImpl) Run(ctx context.Context) error {
zap.Duration("duration", time.Since(start)),
zap.Strings("spans", spans))
}
if !initialized || resolvedTs == lastResolvedTs {
if !initialized {
continue
}
if resolvedTs <= lastResolvedTs {
if time.Since(lastAdvancedTime) > 30*time.Second && time.Since(lastLogSlowRangeTime) > 30*time.Second {
var slowestTs uint64 = math.MaxUint64
slowestRange := tablepb.Span{}
rangeFilled := true
p.tsTracker.Entries(func(key []byte, ts uint64) {
if ts < slowestTs {
slowestTs = ts
slowestRange.StartKey = key
rangeFilled = false
} else if !rangeFilled {
slowestRange.EndKey = key
rangeFilled = true
}
})
log.Info("table puller has been stucked",
zap.String("namespace", p.changefeed.Namespace),
zap.String("changefeed", p.changefeed.ID),
zap.Int64("tableID", p.tableID),
zap.String("tableName", p.tableName),
zap.Uint64("resolvedTs", resolvedTs),
zap.Uint64("slowestRangeTs", slowestTs),
zap.Stringer("range", &slowestRange))
lastLogSlowRangeTime = time.Now()
}
continue
}
lastResolvedTs = resolvedTs
lastAdvancedTime = time.Now()
err := output(&model.RawKVEntry{CRTs: resolvedTs, OpType: model.OpTypeResolved, RegionID: e.RegionID})
if err != nil {
return errors.Trace(err)
Expand Down
Loading