-
Notifications
You must be signed in to change notification settings - Fork 61
logpuller: split subscription runtime helpers from subscription client #5603
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
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
41d1081
logpuller: split subscription runtime helpers from subscription client
lidezhu c27f91d
remove unnecessary file
lidezhu f83181f
small refactor
lidezhu cf031f8
more refactor
lidezhu 13d997f
refactor
lidezhu 8e0ba23
fix
lidezhu 12232a4
refactor
lidezhu f2167b3
add some test
lidezhu 125b52b
address comment
lidezhu 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
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,156 @@ | ||
| // 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" | ||
| "sync" | ||
| "sync/atomic" | ||
|
|
||
| "github.com/pingcap/log" | ||
| "github.com/pingcap/ticdc/pkg/metrics" | ||
| "github.com/pingcap/ticdc/utils/dynstream" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| // regionEventSink delivers region events to dynstream and owns push-side flow control. | ||
| type regionEventSink struct { | ||
| ctx context.Context | ||
| ds dynstream.DynamicStream[int, SubscriptionID, regionEvent, *subscribedSpan, *regionEventHandler] | ||
| // the following three fields are used to manage feedback from ds and notify other goroutines | ||
| mu sync.Mutex | ||
| cond *sync.Cond | ||
| paused atomic.Bool | ||
| } | ||
|
|
||
| func newRegionEventSink(ctx context.Context, failureHandler *regionFailureHandler) *regionEventSink { | ||
| sink := ®ionEventSink{ctx: ctx} | ||
|
|
||
| option := dynstream.NewOption() | ||
| // Note: it is max batch size of the kv sent from tikv(not committed rows) | ||
| option.BatchCount = 1024 | ||
| // TODO: Set `UseBuffer` to true until we refactor the `regionEventHandler.Handle` method so that it doesn't call any method of the dynamic stream. Currently, if `UseBuffer` is set to false, there will be a deadlock: | ||
| // ds.handleLoop fetch events from `ch` -> regionEventHandler.Handle -> ds.RemovePath -> send event to `ch` | ||
| option.UseBuffer = true | ||
| option.EnableMemoryControl = true | ||
| ds := dynstream.NewParallelDynamicStream( | ||
| "log-puller", | ||
| ®ionEventHandler{eventSink: sink, failureHandler: failureHandler}, | ||
| option, | ||
| ) | ||
| ds.Start() | ||
| sink.ds = ds | ||
| sink.cond = sync.NewCond(&sink.mu) | ||
| return sink | ||
| } | ||
|
|
||
| func (s *regionEventSink) AddPath(rt *subscribedSpan) { | ||
| areaSetting := dynstream.NewAreaSettingsWithMaxPendingSize(1*1024*1024*1024, dynstream.MemoryControlForPuller, "logPuller") // 1GB | ||
| if err := s.ds.AddPath(rt.subID, rt, areaSetting); err != nil { | ||
| log.Warn("subscription client add path failed", | ||
| zap.Uint64("subscriptionID", uint64(rt.subID)), | ||
| zap.Error(err)) | ||
| } | ||
| } | ||
|
|
||
| func (s *regionEventSink) RemovePath(subID SubscriptionID) error { | ||
| return s.ds.RemovePath(subID) | ||
| } | ||
|
|
||
| func (s *regionEventSink) Wake(subID SubscriptionID) { | ||
| s.ds.Wake(subID) | ||
| } | ||
|
|
||
| func (s *regionEventSink) Push(subID SubscriptionID, event regionEvent) { | ||
| // fast path | ||
| if !s.paused.Load() { | ||
| s.ds.Push(subID, event) | ||
| return | ||
| } | ||
|
|
||
| // slow path: wait until paused is false | ||
| s.mu.Lock() | ||
| for s.paused.Load() { | ||
| select { | ||
| case <-s.ctx.Done(): | ||
| s.mu.Unlock() | ||
| return | ||
| default: | ||
| s.cond.Wait() | ||
| } | ||
| } | ||
| s.mu.Unlock() | ||
| s.ds.Push(subID, event) | ||
| } | ||
|
lidezhu marked this conversation as resolved.
|
||
|
|
||
| func (s *regionEventSink) Run(ctx context.Context) error { | ||
| for { | ||
| select { | ||
| case <-ctx.Done(): | ||
| return nil | ||
| case feedback := <-s.ds.Feedback(): | ||
| switch feedback.FeedbackType { | ||
| case dynstream.PauseArea: | ||
| s.mu.Lock() | ||
| s.paused.Store(true) | ||
| s.mu.Unlock() | ||
| log.Info("subscription client pause push region event") | ||
| case dynstream.ResumeArea: | ||
| s.mu.Lock() | ||
| s.paused.Store(false) | ||
| s.cond.Broadcast() | ||
| s.mu.Unlock() | ||
| log.Info("subscription client resume push region event") | ||
|
lidezhu marked this conversation as resolved.
|
||
| case dynstream.ReleasePath, dynstream.ResumePath: | ||
| // Ignore it, because it is no need to pause and resume a path in puller. | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func (s *regionEventSink) UpdateMetrics() { | ||
| dsMetrics := s.ds.GetMetrics() | ||
| metricSubscriptionClientDSChannelSize.Set(float64(dsMetrics.EventChanSize)) | ||
| metricSubscriptionClientDSPendingQueueLen.Set(float64(dsMetrics.PendingQueueLen)) | ||
|
|
||
| if len(dsMetrics.MemoryControl.AreaMemoryMetrics) == 0 { | ||
| return | ||
| } | ||
| if len(dsMetrics.MemoryControl.AreaMemoryMetrics) != 1 { | ||
| log.Warn("subscription client should have exactly one area") | ||
| return | ||
| } | ||
|
|
||
| areaMetric := dsMetrics.MemoryControl.AreaMemoryMetrics[0] | ||
| metrics.DynamicStreamMemoryUsage.WithLabelValues( | ||
| "log-puller", | ||
| "max", | ||
| "default", | ||
| "default", | ||
| ).Set(float64(areaMetric.MaxMemory())) | ||
| metrics.DynamicStreamMemoryUsage.WithLabelValues( | ||
| "log-puller", | ||
| "used", | ||
| "default", | ||
| "default", | ||
| ).Set(float64(areaMetric.MemoryUsage())) | ||
| } | ||
|
|
||
| func (s *regionEventSink) Close() { | ||
| s.mu.Lock() | ||
| s.paused.Store(false) | ||
| s.cond.Broadcast() | ||
| s.mu.Unlock() | ||
| s.ds.Close() | ||
| } | ||
|
lidezhu marked this conversation as resolved.
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.