Skip to content

logpuller: initialize spans only after full range coverage#5657

Merged
ti-chi-bot[bot] merged 11 commits into
masterfrom
ldz/fix-puller0715
Jul 16, 2026
Merged

logpuller: initialize spans only after full range coverage#5657
ti-chi-bot[bot] merged 11 commits into
masterfrom
ldz/fix-puller0715

Conversation

@lidezhu

@lidezhu lidezhu commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator

What problem does this PR solve?

Issue Number: close #5658

What is changed and how it works?

This pull request refines the initialization process for logpuller spans. Previously, a span could be marked as initialized prematurely. The changes implement a tracking mechanism that aggregates initialized ranges across regions, ensuring the entire span is fully covered before transitioning to an initialized state. This improves the reliability of the subscription client's initialization phase.

Highlights

  • Span Initialization Logic: Introduced a spanInitializationTracker to ensure that a subscription span is only marked as initialized after all underlying regions within the span have completed their initial scan.
  • Event Handling Update: Updated region_event_handler.go to delegate initialization tracking to the new tracker instead of immediately marking the span as initialized upon receiving a single event.
  • Test Coverage: Added comprehensive unit tests in span_initialization_tracker_test.go and updated existing tests to verify the new initialization logic.

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`.

Summary by CodeRabbit

  • Bug Fixes

    • Improved initialization tracking by recording per-region initialization and marking a subscription ready only after all key ranges finish the initial scan.
    • Refined resolved-ts emission/advancement so behavior is correct when initialization completes or hasn’t yet.
    • Added more detailed warnings when resolved timestamps advance before initialization finishes.
  • Tests

    • Added unit tests for initialization tracking, including interval merging behavior.
    • Expanded event/out-of-order and final-range initialization scenarios in existing region handler tests.

@ti-chi-bot

ti-chi-bot Bot commented Jul 15, 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 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. labels Jul 15, 2026
@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 8af5aea2-3081-442c-9123-7a20c14122e9

📥 Commits

Reviewing files that changed from the base of the PR and between 77255f6 and bd393ae.

📒 Files selected for processing (1)
  • logservice/logpuller/region_event_handler.go
🚧 Files skipped from review as they are similar to previous changes (1)
  • logservice/logpuller/region_event_handler.go

📝 Walkthrough

Walkthrough

The logpuller now tracks initialized key-range coverage across regions, marks spans initialized only after complete coverage, adjusts resolved-timestamp handling, and updates tests to use realistic locked range state.

Changes

Span initialization coverage

Layer / File(s) Summary
Initialized range coverage tracker
logservice/logpuller/span_initialization_tracker.go, logservice/logpuller/span_initialization_tracker_test.go
Adds monotonic interval tracking that clamps, merges, and completes initialized coverage for a subscribed span, with unit tests for overlap behavior.
Span initialization wiring
logservice/logpuller/span_registry.go, logservice/logpuller/region_event_handler.go, logservice/logpuller/region_event_handler_test.go
Region INITIALIZED events update span coverage, atomically mark the span initialized after full coverage, and revise resolved-timestamp handling with coverage-aware logging and tests.
Range-lock-backed test setup
logservice/logpuller/region_event_handler_test.go, logservice/logpuller/subscription_client_test.go
Tests now create locked range state through RangeLock.LockRange and use explicit region version identifiers.

Estimated code review effort: 3 (Moderate) | ~20 minutes

Possibly related PRs

  • pingcap/ticdc#4954: Both changes update resolve-lock test setup involving subscribed-span region state.

Suggested labels: lgtm, approved

Suggested reviewers: asddongmen, hongyunyan

Poem

I’m a rabbit counting ranges in line,
Merging each interval, neat and fine.
When every key has crossed the gate,
The span declares its initialized state.
Resolved timestamps now wait their turn—
And carrot-sweet tests confirm what we learn. 🐇

🚥 Pre-merge checks | ✅ 3 | ❌ 2

❌ Failed checks (2 warnings)

Check name Status Explanation Resolution
Description check ⚠️ Warning The template is only partially filled: the questions are unanswered and the release-note block still contains placeholder text instead of a real note or None. Fill in the two question answers and replace the release-note placeholder with a real note or None, and remove any unused checklist items.
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (3 passed)
Check name Status Explanation
Title check ✅ Passed The title clearly describes the main change: delaying span initialization until full range coverage.
Linked Issues check ✅ Passed The code now tracks region INITIALIZED coverage and only flips span.initialized after full span coverage, matching #5658.
Out of Scope Changes check ✅ Passed All code changes support span initialization tracking or tests for it; no unrelated changes are evident.
✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch ldz/fix-puller0715

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/M Denotes a PR that changes 30-99 lines, ignoring generated files. label Jul 15, 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 initialization logic of a subscribedSpan by moving the state transition of initialized from handleResolvedTs to tryAdvanceResolvedTs in region_event_handler.go. This ensures that the span is marked as initialized only after its resolved timestamp has been successfully advanced. Additionally, a new unit test TestSpanInitializedAfterResolvedTsAdvanced has been added to verify this behavior, and a descriptive comment was added to the initialized field in span_registry.go. There are no review comments to address, and the changes look correct and well-tested.

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.

@ti-chi-bot ti-chi-bot Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Jul 15, 2026
@ti-chi-bot ti-chi-bot Bot added size/XL Denotes a PR that changes 500-999 lines, ignoring generated files. and removed size/L Denotes a PR that changes 100-499 lines, ignoring generated files. labels Jul 15, 2026
@lidezhu lidezhu changed the title logpuller: initialize spans after resolved ts advances logpuller: initialize spans only after full range coverage Jul 15, 2026
@lidezhu lidezhu marked this pull request as ready for review July 15, 2026 10:43
@ti-chi-bot ti-chi-bot Bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Jul 15, 2026
@lidezhu

lidezhu commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator Author

/gemini summary

@gemini-code-assist

Copy link
Copy Markdown

Summary of Changes

This pull request refines the initialization process for logpuller spans. Previously, a span could be marked as initialized prematurely. The changes implement a tracking mechanism that aggregates initialized ranges across regions, ensuring the entire span is fully covered before transitioning to an initialized state. This improves the reliability of the subscription client's initialization phase.

Highlights

  • Span Initialization Logic: Introduced a spanInitializationTracker to ensure that a subscription span is only marked as initialized after all underlying regions within the span have completed their initial scan.
  • Event Handling Update: Updated region_event_handler.go to delegate initialization tracking to the new tracker instead of immediately marking the span as initialized upon receiving a single event.
  • Test Coverage: Added comprehensive unit tests in span_initialization_tracker_test.go and updated existing tests to verify the new initialization logic.
New Features

🧠 You can now enable Memory (public preview) to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Activity
  • Pull request created by lidezhu.
  • Bot skipped CI due to draft status.
  • Bot notified that the PR is not approved.
  • Author requested a summary via /gemini summary.

@lidezhu

lidezhu commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator Author

/gemini review

@lidezhu

lidezhu commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Jul 15, 2026

Copy link
Copy Markdown
Contributor
✅ Action performed

Review finished.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@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 introduces a spanInitializationTracker to track the initialization of sub-ranges within a subscribed span. Instead of marking a span as initialized immediately, it now tracks individual initialized ranges and only marks the entire span as initialized once all of its sub-ranges have completed their initial scans. The PR also updates related event handling logic, warning logs, and unit tests to support this new tracking mechanism. I have no feedback to provide.

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.

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (1)
logservice/logpuller/span_initialization_tracker_test.go (1)

40-52: 📐 Maintainability & Code Quality | 🔵 Trivial | 💤 Low value

Consider adding test cases for open-ended intervals (infinity bounds).

In TiKV and TiCDC, empty byte slices ([]byte{}) are often used in StartKey and EndKey to represent negative and positive infinity. Consider adding a test case that verifies the tracker correctly handles merging and completion when given empty bounds to ensure open-ended ranges are flawlessly supported.

🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

In `@logservice/logpuller/span_initialization_tracker_test.go` around lines 40 -
52, The TestSpanInitializationTrackerMergesOverlappingRanges test only covers
finite bounds; add cases using empty StartKey and EndKey values to represent
negative and positive infinity. Verify spanInitializationTracker.add merges
these open-ended ranges correctly and returns completion only after the total
open-ended range is covered, while preserving the existing finite-range
assertions.
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@logservice/logpuller/region_event_handler.go`:
- Around line 396-412: In the resolved-timestamp handling flow, clamp ts to
span.startTs before the outer conditional whenever ts exceeds span.startTs while
span.initialized.Load() is false. Keep the existing warning in place, then
evaluate the conditional and subsequent store/return logic using the clamped
timestamp so downstream consumers cannot observe premature progress.

---

Nitpick comments:
In `@logservice/logpuller/span_initialization_tracker_test.go`:
- Around line 40-52: The TestSpanInitializationTrackerMergesOverlappingRanges
test only covers finite bounds; add cases using empty StartKey and EndKey values
to represent negative and positive infinity. Verify
spanInitializationTracker.add merges these open-ended ranges correctly and
returns completion only after the total open-ended range is covered, while
preserving the existing finite-range assertions.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: b4317788-e2f7-44b8-9bcf-8e0ac3cfdc53

📥 Commits

Reviewing files that changed from the base of the PR and between d4d1c5e and 77255f6.

📒 Files selected for processing (6)
  • logservice/logpuller/region_event_handler.go
  • logservice/logpuller/region_event_handler_test.go
  • logservice/logpuller/span_initialization_tracker.go
  • logservice/logpuller/span_initialization_tracker_test.go
  • logservice/logpuller/span_registry.go
  • logservice/logpuller/subscription_client_test.go

Comment thread logservice/logpuller/region_event_handler.go
@lidezhu

lidezhu commented Jul 15, 2026

Copy link
Copy Markdown
Collaborator Author

/test all

@ti-chi-bot ti-chi-bot Bot added needs-1-more-lgtm Indicates a PR needs 1 more LGTM. approved labels Jul 16, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jul 16, 2026

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: asddongmen, hongyunyan

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

The pull request process is described here

Details Needs approval from an approver in each of these files:
  • OWNERS [asddongmen,hongyunyan]

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 added lgtm and removed needs-1-more-lgtm Indicates a PR needs 1 more LGTM. labels Jul 16, 2026
@ti-chi-bot

ti-chi-bot Bot commented Jul 16, 2026

Copy link
Copy Markdown

[LGTM Timeline notifier]

Timeline:

  • 2026-07-16 06:12:55.411108555 +0000 UTC m=+866961.447203611: ☑️ agreed by asddongmen.
  • 2026-07-16 06:38:00.959285987 +0000 UTC m=+868466.995381033: ☑️ agreed by hongyunyan.

@ti-chi-bot ti-chi-bot Bot merged commit dd07f60 into master Jul 16, 2026
25 checks passed
@ti-chi-bot ti-chi-bot Bot deleted the ldz/fix-puller0715 branch July 16, 2026 07:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved lgtm release-note Denotes a PR that will be considered when it comes time to generate release notes. size/XL Denotes a PR that changes 500-999 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

logpuller: span may initialize before all regions finish initial scan

3 participants