Skip to content

eventstore,eventservice: propagate event iterator errors#4985

Open
lidezhu wants to merge 14 commits intomasterfrom
ldz/optimize-event-store042902
Open

eventstore,eventservice: propagate event iterator errors#4985
lidezhu wants to merge 14 commits intomasterfrom
ldz/optimize-event-store042902

Conversation

@lidezhu
Copy link
Copy Markdown
Collaborator

@lidezhu lidezhu commented May 2, 2026

What problem does this PR solve?

Issue Number: close #5005

What is changed and how it works?

This pull request significantly improves the error handling and propagation mechanisms within the event store and event service components. By modifying iterator interfaces and implementations to return errors, the system can now reliably detect and react to failures during event iteration, preventing potential resource leaks and enhancing overall stability. The changes ensure that errors are not silently ignored but are instead properly surfaced and managed throughout the event processing pipeline.

Highlights

  • Error Propagation: Modified the GetIterator method in EventStore and its corresponding interface to return errors, ensuring that issues during iterator creation or retrieval are properly surfaced.
  • Enhanced Error Handling: Implemented robust error handling within eventStore.GetIterator for pebble.NewIter and iter.First() calls, preventing silent failures and ensuring resource cleanup.
  • Iterator Close Error Reporting: Updated the EventIterator.Close method to return any accumulated iterator errors, providing a comprehensive view of potential issues during iteration lifecycle.
  • Refactored Event Scanning Logic: Adjusted the eventScanner to correctly handle and propagate errors returned by GetIterator and Close, improving the reliability of event processing.

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

Release Notes

  • Bug Fixes
    • Enhanced error handling in event retrieval operations to improve system reliability and prevent silent failures during event processing.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 2, 2026

Review Change Stack

📝 Walkthrough

Walkthrough

The PR propagates event store iterator errors through the entire scanning pipeline. EventStore.GetIterator now returns (EventIterator, error), captures Pebble initialization and first-read failures, and traces them. EventScanner integrates this error-aware API, handling creation failures immediately and managing iterator cleanup with conditional error propagation. All tests and mocks are updated to validate the new contract.

Changes

Event Store Iterator Error Propagation

Layer / File(s) Summary
Interface Contracts
logservice/eventstore/event_store.go
EventStore.GetIterator signature changes from (EventIterator) to (EventIterator, error). EventIterator documentation updated to mention error accumulation.
Event Store Error Handling
logservice/eventstore/event_store.go
eventStore.GetIterator now captures db.NewIter and iter.First() errors, traces them, and returns (nil, error) or (nil, nil) on failure. eventStoreIter.Close() wraps errors with errors.Trace().
Scanner Integration
pkg/eventservice/event_scanner.go
eventGetter interface requires GetIterator(...) (EventIterator, error). scan() method handles iterator errors immediately, explicitly closes iterators afterward, and propagates close errors differently based on scan success.
Test Infrastructure
pkg/eventservice/event_scanner_test.go
Import eventstore package. Add stubEventGetter test double and TestEventScannerReturnsIteratorErrors covering iterator creation and close failures.
Event Store Tests
logservice/eventstore/event_store_test.go
Add requireEventIterator helper for error-checked iterator creation. Update TestEventStoreSwitchSubStat to use a closure with proper error handling and cleanup. Refactor TestEventStoreGetIteratorConcurrently to use the helper.
Service Mocks
pkg/eventservice/event_service_test.go
Update mockEventStore.GetIterator to return (EventIterator, error). Add closeErr field to mockEventIterator and change Close() to return (rowCount, closeErr) instead of (0, nil).

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Possibly related PRs

  • pingcap/ticdc#4953: Both PRs modify event_store.go's GetIterator implementation; this PR adds error-returning API and error-aware construction while the related PR adjusts iterator bounds/decoding and Pebble cache usage.

Suggested labels

lgtm, approved, size/M

Suggested reviewers

  • wk989898
  • hongyunyan
  • tenfyzhong

Poem

A scanner seeks the truth untold,
Through iterators, brave and bold.
When Pebble whispers errors clear,
No more shall failures disappear! 🐰✨

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 11.11% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The pull request title 'eventstore,eventservice: propagate event iterator errors' clearly and accurately summarizes the main change, directly addressing error propagation improvements across both components.
Description check ✅ Passed The PR description is substantially complete, including issue number, detailed explanation of changes with highlights, problem context, and addresses all major template sections, though release note section lacks detail.
Linked Issues check ✅ Passed The pull request fully addresses the linked issue #5005 by modifying EventStore.GetIterator to return errors and updating eventScanner to properly propagate and handle iterator errors [#5005].
Out of Scope Changes check ✅ Passed All changes directly relate to error propagation in event store iterator handling; no out-of-scope modifications were introduced beyond the stated objectives.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ 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/optimize-event-store042902

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 and usage tips.

@lidezhu lidezhu changed the title fix scan iterator eventstore,eventservice: propagate event iterator errors May 2, 2026
Copy link
Copy Markdown

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

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 updates the EventStore and EventScanner to properly handle and propagate errors during iterator creation and closure. Key changes include modifying the GetIterator signature to return an error, implementing more robust error checking when initializing Pebble iterators, and updating the eventScanner to handle errors from both scanning and closing iterators. Feedback focuses on potential resource leaks due to the removal of defer in the scanner and a suggestion to simplify the error handling logic when checking the first element of a Pebble iterator.

Comment thread pkg/eventservice/event_scanner.go Outdated
Comment thread logservice/eventstore/event_store.go Outdated
Base automatically changed from ldz/optimize-event-store0429 to master May 7, 2026 00:38
@ti-chi-bot ti-chi-bot Bot added do-not-merge/needs-linked-issue 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. labels May 7, 2026
# Conflicts:
#	logservice/eventstore/event_store.go
@ti-chi-bot ti-chi-bot Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/XXL Denotes a PR that changes 1000+ lines, ignoring generated files. do-not-merge/needs-linked-issue labels May 7, 2026
@lidezhu
Copy link
Copy Markdown
Collaborator Author

lidezhu commented May 7, 2026

/gemini summary

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

This pull request significantly improves the error handling and propagation mechanisms within the event store and event service components. By modifying iterator interfaces and implementations to return errors, the system can now reliably detect and react to failures during event iteration, preventing potential resource leaks and enhancing overall stability. The changes ensure that errors are not silently ignored but are instead properly surfaced and managed throughout the event processing pipeline.

Highlights

  • Error Propagation: Modified the GetIterator method in EventStore and its corresponding interface to return errors, ensuring that issues during iterator creation or retrieval are properly surfaced.
  • Enhanced Error Handling: Implemented robust error handling within eventStore.GetIterator for pebble.NewIter and iter.First() calls, preventing silent failures and ensuring resource cleanup.
  • Iterator Close Error Reporting: Updated the EventIterator.Close method to return any accumulated iterator errors, providing a comprehensive view of potential issues during iteration lifecycle.
  • Refactored Event Scanning Logic: Adjusted the eventScanner to correctly handle and propagate errors returned by GetIterator and Close, improving the reliability of event processing.
  • Comprehensive Unit Testing: Added new unit tests and updated existing ones to cover scenarios where event iterator creation or closing fails, validating the new error propagation mechanisms.
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.

Changelog
  • logservice/eventstore/event_store.go
    • Imported the github.com/pingcap/errors package for enhanced error handling.
    • Modified the EventStore.GetIterator method signature to return an (EventIterator, error) tuple.
    • Updated the documentation for EventIterator.Close to explicitly mention returning accumulated iterator errors.
    • Added error handling for db.NewIter and iter.First() calls within eventStore.GetIterator, returning errors when appropriate.
    • Ensured that errors are wrapped using errors.Trace for better context.
  • logservice/eventstore/event_store_test.go
    • Introduced a new helper function requireEventIterator to simplify testing of GetIterator calls.
    • Updated existing test cases to accommodate the new GetIterator signature and handle potential errors.
  • pkg/eventservice/event_broker.go
    • Removed an unnecessary empty line within an error logging block.
  • pkg/eventservice/event_scanner.go
    • Updated the eventGetter.GetIterator interface signature to return an (eventstore.EventIterator, error) tuple.
    • Modified the newEventScanner function to accept the updated eventGetter interface.
    • Refactored the scan method to properly handle and propagate errors returned by GetIterator and closeIterator, ensuring correct resource management.
    • Changed the closeIterator function signature to return (int64, error).
  • pkg/eventservice/event_scanner_test.go
    • Imported the github.com/pingcap/ticdc/logservice/eventstore package.
    • Added a stubEventGetter type to mock the eventGetter interface for testing purposes.
    • Implemented a new test case TestEventScannerReturnsIteratorErrors to verify error propagation from GetIterator and Close methods.
  • pkg/eventservice/event_service_test.go
    • Adjusted the mockEventStore.GetIterator method signature to return an (eventstore.EventIterator, error) tuple.
    • Modified the mockEventIterator.Close method to return (int64, error) and store a closeErr for testing error scenarios.
Activity
  • coderabbitai[bot] reported that the rate limit for reviews was exceeded.
  • ti-chi-bot[bot] indicated that the PR is not yet approved and listed required approvers.
  • lidezhu requested a Gemini summary.
  • gemini-code-assist[bot] commented on a potential resource leak due to defer removal and suggested using named return parameters.
  • gemini-code-assist[bot] suggested simplifying error handling logic for iter.First() and iter.Close(), which lidezhu confirmed as fixed.

@lidezhu
Copy link
Copy Markdown
Collaborator Author

lidezhu commented May 7, 2026

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented May 7, 2026

✅ Actions performed

Review triggered.

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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

🧹 Nitpick comments (1)
pkg/eventservice/event_scanner_test.go (1)

61-96: 💤 Low value

LGTM – both error propagation paths are correctly covered.

Minor coverage gap: the scanErr != nil && closeErr != nil branch (line 148–152 in event_scanner.go, which logs a warning and returns scanErr) is not exercised. Worth adding a test case for completeness, though it is low-risk.

🤖 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 `@pkg/eventservice/event_scanner_test.go` around lines 61 - 96, Add a test case
in TestEventScannerReturnsIteratorErrors that constructs a scanner using
newEventScanner with a stubEventGetter whose iterator returns a non-nil error
from Next (scanErr) and also returns a non-nil error from Close (closeErr) (e.g.
&mockEventIterator{nextErr: scanErr, closeErr: closeErr}), call
scanner.scan(...) with the same disp/dataRange, and assert the returned error is
scanErr, events is nil and interrupted is false to exercise the scanErr != nil
&& closeErr != nil branch in event_scanner.scan; reuse NewMockSchemaStore and
mockMounter as in the other cases.
🤖 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.

Nitpick comments:
In `@pkg/eventservice/event_scanner_test.go`:
- Around line 61-96: Add a test case in TestEventScannerReturnsIteratorErrors
that constructs a scanner using newEventScanner with a stubEventGetter whose
iterator returns a non-nil error from Next (scanErr) and also returns a non-nil
error from Close (closeErr) (e.g. &mockEventIterator{nextErr: scanErr, closeErr:
closeErr}), call scanner.scan(...) with the same disp/dataRange, and assert the
returned error is scanErr, events is nil and interrupted is false to exercise
the scanErr != nil && closeErr != nil branch in event_scanner.scan; reuse
NewMockSchemaStore and mockMounter as in the other cases.

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: 417bb8d7-f9c3-4f73-ac38-b12d2338e81d

📥 Commits

Reviewing files that changed from the base of the PR and between 29a8576 and 1d0ea08.

📒 Files selected for processing (6)
  • logservice/eventstore/event_store.go
  • logservice/eventstore/event_store_test.go
  • pkg/eventservice/event_broker.go
  • pkg/eventservice/event_scanner.go
  • pkg/eventservice/event_scanner_test.go
  • pkg/eventservice/event_service_test.go
💤 Files with no reviewable changes (1)
  • pkg/eventservice/event_broker.go

@ti-chi-bot ti-chi-bot Bot added the needs-1-more-lgtm Indicates a PR needs 1 more LGTM. label May 7, 2026
@ti-chi-bot
Copy link
Copy Markdown

ti-chi-bot Bot commented May 7, 2026

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: asddongmen

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:

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
Copy link
Copy Markdown

ti-chi-bot Bot commented May 7, 2026

[LGTM Timeline notifier]

Timeline:

  • 2026-05-07 09:28:48.987439796 +0000 UTC m=+346401.860789768: ☑️ agreed by asddongmen.

@ti-chi-bot ti-chi-bot Bot added the approved label May 7, 2026
@lidezhu
Copy link
Copy Markdown
Collaborator Author

lidezhu commented May 7, 2026

/hold

@ti-chi-bot ti-chi-bot Bot added the do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. label May 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

approved do-not-merge/hold Indicates that a PR should not merge because someone has issued a /hold command. needs-1-more-lgtm Indicates a PR needs 1 more LGTM. release-note Denotes a PR that will be considered when it comes time to generate release notes. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

eventservice: propagate event store iterator errors

2 participants