-
Notifications
You must be signed in to change notification settings - Fork 50
[WIP] logpuller: improve retry backoff and range reload scheduling #4700
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
Open
lidezhu
wants to merge
2
commits into
pingcap:master
Choose a base branch
from
lidezhu:ldz/improve-puller0329
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,164 @@ | ||
| // Copyright 2026 PingCAP, Inc. | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package logpuller | ||
|
|
||
| import ( | ||
| "context" | ||
| "sort" | ||
| "sync" | ||
| "time" | ||
|
|
||
| "github.com/pingcap/log" | ||
| "github.com/pingcap/ticdc/heartbeatpb" | ||
| "github.com/pingcap/ticdc/pkg/common" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| type rangeReloadTaskSet struct { | ||
| subscribedSpan *subscribedSpan | ||
| filterLoop bool | ||
| priority TaskType | ||
| spans []heartbeatpb.TableSpan | ||
| flushScheduled bool | ||
| } | ||
|
|
||
| type rangeReloadAggregator struct { | ||
| client *subscriptionClient | ||
| debounce time.Duration | ||
| flushThreshold int | ||
|
|
||
| mu sync.Mutex | ||
| tasks map[SubscriptionID]*rangeReloadTaskSet | ||
| } | ||
|
|
||
| func newRangeReloadAggregator(client *subscriptionClient, debounce time.Duration, flushThreshold int) *rangeReloadAggregator { | ||
| if flushThreshold <= 0 { | ||
| flushThreshold = rangeReloadImmediateFlushMax | ||
| } | ||
| return &rangeReloadAggregator{ | ||
| client: client, | ||
| debounce: debounce, | ||
| flushThreshold: flushThreshold, | ||
| tasks: make(map[SubscriptionID]*rangeReloadTaskSet), | ||
| } | ||
| } | ||
|
|
||
| func (a *rangeReloadAggregator) add( | ||
| span heartbeatpb.TableSpan, | ||
| subscribedSpan *subscribedSpan, | ||
| filterLoop bool, | ||
| priority TaskType, | ||
| ) { | ||
| if subscribedSpan == nil || common.IsEmptySpan(span) { | ||
| return | ||
| } | ||
| var immediateFlush bool | ||
| a.mu.Lock() | ||
| set := a.tasks[subscribedSpan.subID] | ||
| if set == nil { | ||
| set = &rangeReloadTaskSet{ | ||
| subscribedSpan: subscribedSpan, | ||
| filterLoop: filterLoop, | ||
| priority: priority, | ||
| spans: make([]heartbeatpb.TableSpan, 0, 4), | ||
| } | ||
| a.tasks[subscribedSpan.subID] = set | ||
| } | ||
| if priority < set.priority { | ||
| set.priority = priority | ||
| } | ||
| set.spans = append(set.spans, span) | ||
| if len(set.spans) >= a.flushThreshold { | ||
| immediateFlush = true | ||
| set.flushScheduled = false | ||
| } else if !set.flushScheduled { | ||
| // Debounce reloads so a split/merge storm can be collapsed into a | ||
| // smaller batch of merged spans before hitting region metadata APIs. | ||
| set.flushScheduled = true | ||
| a.client.retryScheduler.schedule(a.debounce, func(_ context.Context) { | ||
| a.flush(subscribedSpan.subID) | ||
| }) | ||
| } | ||
| a.mu.Unlock() | ||
|
|
||
| if immediateFlush { | ||
| a.flush(subscribedSpan.subID) | ||
| } | ||
| } | ||
|
|
||
| func (a *rangeReloadAggregator) flush(subID SubscriptionID) { | ||
| var ( | ||
| set *rangeReloadTaskSet | ||
| mergedSpans []heartbeatpb.TableSpan | ||
| ) | ||
| a.mu.Lock() | ||
| set = a.tasks[subID] | ||
| if set == nil { | ||
| a.mu.Unlock() | ||
| return | ||
| } | ||
| delete(a.tasks, subID) | ||
| set.flushScheduled = false | ||
| mergedSpans = mergeTableSpans(set.spans) | ||
| a.mu.Unlock() | ||
|
|
||
| if set.subscribedSpan.stopped.Load() { | ||
| return | ||
| } | ||
| log.Debug("subscription client flush aggregated reload spans", | ||
| zap.Uint64("subscriptionID", uint64(subID)), | ||
| zap.Int("inputSpanCount", len(set.spans)), | ||
| zap.Int("mergedSpanCount", len(mergedSpans))) | ||
| for _, span := range mergedSpans { | ||
| a.client.scheduleRangeTaskAfter(rangeTask{ | ||
| span: span, | ||
| subscribedSpan: set.subscribedSpan, | ||
| filterLoop: set.filterLoop, | ||
| priority: set.priority, | ||
| }, 0) | ||
| } | ||
| } | ||
|
|
||
| func mergeTableSpans(spans []heartbeatpb.TableSpan) []heartbeatpb.TableSpan { | ||
| if len(spans) <= 1 { | ||
| return append([]heartbeatpb.TableSpan(nil), spans...) | ||
| } | ||
| copied := append([]heartbeatpb.TableSpan(nil), spans...) | ||
| sort.Slice(copied, func(i, j int) bool { | ||
| return common.StartCompare(copied[i].StartKey, copied[j].StartKey) < 0 | ||
| }) | ||
|
|
||
| merged := make([]heartbeatpb.TableSpan, 0, len(copied)) | ||
| for _, span := range copied { | ||
| if common.IsEmptySpan(span) { | ||
| continue | ||
| } | ||
| if len(merged) == 0 { | ||
| merged = append(merged, span) | ||
| continue | ||
| } | ||
| last := merged[len(merged)-1] | ||
| if common.EndCompare(last.EndKey, span.StartKey) >= 0 { | ||
| // Adjacent retry spans are merged too, because they can be resolved | ||
| // by a single BatchLoadRegionsWithKeyRange call later. | ||
| if common.EndCompare(span.EndKey, last.EndKey) > 0 { | ||
| last.EndKey = span.EndKey | ||
| } | ||
| merged[len(merged)-1] = last | ||
| continue | ||
| } | ||
| merged = append(merged, span) | ||
| } | ||
| return merged | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resetting the retry backoff on every
ResolvedTsevent is redundant and potentially expensive, asResolvedTsevents are received very frequently. The backoff state only needs to be reset when a region is successfully initialized or when a new request is sent. SinceresetRetryBackoffis already called onEvent_INITIALIZEDand afterdoSend, this call should be removed to avoid unnecessary map iterations inretryBackoffManager.