Skip to content

[WIP] log puller optimization#5498

Draft
lidezhu wants to merge 22 commits into
masterfrom
ldz/improve-log-puller052302
Draft

[WIP] log puller optimization#5498
lidezhu wants to merge 22 commits into
masterfrom
ldz/improve-log-puller052302

Conversation

@lidezhu

@lidezhu lidezhu commented Jun 24, 2026

Copy link
Copy Markdown
Collaborator

What problem does this PR solve?

Issue Number: close #xxx

What is changed and how it works?

Check List

Tests

  • Unit test
  • Integration test
  • Manual test (add detailed scripts or steps below)
  • No code

Questions

Will it cause performance regression or break compatibility?
Do you need to update user documentation, design documentation or monitoring documentation?

Release note

Please refer to [Release Notes Language Style Guide](https://pingcap.github.io/tidb-dev-guide/contribute-to-tidb/release-notes-style-guide.html) to write a quality release note.

If you don't think this PR needs a release note then fill it with `None`.

@ti-chi-bot

ti-chi-bot Bot commented Jun 24, 2026

Copy link
Copy Markdown

Skipping CI for Draft Pull Request.
If you want CI signal for your change, please convert it to an actual PR.
You can still manually trigger a test run with /test all

@ti-chi-bot ti-chi-bot Bot added release-note Denotes a PR that will be considered when it comes time to generate release notes. do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. labels Jun 24, 2026
@coderabbitai

coderabbitai Bot commented Jun 24, 2026

Copy link
Copy Markdown
Contributor

Important

Review skipped

Draft detected.

Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 884e5de7-cea7-490e-bd7e-90b739225372

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review
✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ldz/improve-log-puller052302

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@ti-chi-bot ti-chi-bot Bot added the size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. label Jun 24, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the region request scheduling and flow control in the logpuller service by introducing a state-tracked requestCache, a non-blocking controlQueue for deregistration, and a deferred task scheduling mechanism in requestedStore to prevent overloading busy stores. The review feedback highlights several critical issues, including potential memory leaks in requestCache due to stale pointers not being set to nil during slice clearing and compaction, thread-safety data races from lazy initialization of controlQueue, and a resource leak where promoted store tasks are not properly finished when a region is stopped.

Important

The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.

Comment on lines +335 to +336
c.ready = c.ready[:0]
c.readyIdx = 0

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In clear(), slicing c.ready to [:0] without setting the elements to nil first will leak memory because the underlying array of the slice still holds references to the *regionReq pointers. We should set the elements to nil before slicing to allow them to be garbage collected.

	for i := range c.ready {
		c.ready[i] = nil
	}
	c.ready = c.ready[:0]
	c.readyIdx = 0

Comment on lines 313 to 315
if removed > 0 {
c.compactReadyLocked()
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

In takeUnsentRegions(), since all queued requests are removed from c.requests, any remaining elements in c.ready are now invalid (lazily deleted). Calling c.compactReadyLocked() might return early and do nothing if c.readyIdx is small, leaving stale *regionReq pointers in the slice and causing a memory leak. We should explicitly clear c.ready and set its elements to nil.

	if removed > 0 {
		for i := range c.ready {
			c.ready[i] = nil
		}
		c.ready = c.ready[:0]
		c.readyIdx = 0
	}

Comment on lines +392 to +394
if s.controlQueue == nil {
s.controlQueue = newControlQueue()
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The lazy initialization of s.controlQueue is not thread-safe and introduces a data race because Unsubscribe (which calls enqueueDeregisterToAllStores) can be called concurrently from any goroutine. Since controlQueue is already guaranteed to be initialized in newRegionRequestWorker and in all relevant tests, this lazy initialization check is unnecessary and should be removed.

Comment on lines 781 to 783
if worker.controlQueue == nil {
worker.controlQueue = newControlQueue()
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

Similarly, this lazy initialization check of worker.controlQueue is not thread-safe and can cause a data race when Unsubscribe is called concurrently. Since controlQueue is always initialized when the worker is created, we should remove this check.

Comment on lines 720 to 724
region := regionTask.GetRegionInfo()
if region.isStopped() {
enqueued, err := s.enqueueRegionToAllStores(ctx, region)
if err != nil {
return err
}
if !enqueued {
log.Debug("enqueue stop request failed, retry later",
zap.Uint64("subscriptionID", uint64(region.subscribedSpan.subID)))
s.regionTaskQueue.Push(regionTask)
}
s.enqueueDeregisterToAllStores(region.subscribedSpan.subID, region.filterLoop)
continue
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

If region.isStopped() is true, the loop continues immediately. However, if this task was previously deferred and promoted, promotedStore will be leaked (never finished). We should ensure that the promoted task is finished and another task is promoted before continuing.

Suggested change
region := regionTask.GetRegionInfo()
if region.isStopped() {
enqueued, err := s.enqueueRegionToAllStores(ctx, region)
if err != nil {
return err
}
if !enqueued {
log.Debug("enqueue stop request failed, retry later",
zap.Uint64("subscriptionID", uint64(region.subscribedSpan.subID)))
s.regionTaskQueue.Push(regionTask)
}
s.enqueueDeregisterToAllStores(region.subscribedSpan.subID, region.filterLoop)
continue
}
region := regionTask.GetRegionInfo()
if region.isStopped() {
if promotedStore := regionTask.deferredStore.Load(); promotedStore != nil {
promotedStore.finishPromotedTask(regionTask)
promotedStore.promoteDeferredTask(s.regionTaskQueue)
}
s.enqueueDeregisterToAllStores(region.subscribedSpan.subID, region.filterLoop)
continue
}

Base automatically changed from ldz/improve-log-puller0523 to master June 25, 2026 02:54
@ti-chi-bot

ti-chi-bot Bot commented Jun 25, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by:
Once this PR has been reviewed and has the lgtm label, please assign flowbehappy for approval. For more information see the Code Review Process.
Please ensure that each of them provides their approval before proceeding.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@ti-chi-bot

ti-chi-bot Bot commented Jul 1, 2026

Copy link
Copy Markdown

[FORMAT CHECKER NOTIFICATION]

Notice: To remove the do-not-merge/needs-linked-issue label, please provide the linked issue number on one line in the PR body, for example: Issue Number: close #123 or Issue Number: ref #456.

📖 For more info, you can check the "Contribute Code" section in the development guide.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

do-not-merge/needs-linked-issue do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant