Skip to content
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

Handle watchers with future sequence numbers gracefully #4773

Merged

Conversation

wdbaruni
Copy link
Member

@wdbaruni wdbaruni commented Dec 16, 2024

Summary by CodeRabbit

  • New Features

    • Enhanced logic for determining the starting iterator in the watcher, improving error handling for various iterator types.
  • Bug Fixes

    • Improved handling of scenarios where the requested sequence number is unavailable, preventing potential issues during event processing.
  • Tests

    • Expanded test cases for the watcher to cover additional iterator behaviors and error handling scenarios, enhancing overall test coverage.

Copy link

linear bot commented Dec 16, 2024

Copy link
Contributor

coderabbitai bot commented Dec 16, 2024

Walkthrough

The changes focus on enhancing the event iterator logic in the watcher package. Modifications to the determineStartingIterator method improve handling for different iterator types, specifically EventIteratorTrimHorizon and EventIteratorLatest. The updates include a direct return for the trim horizon type and refined logic for the latest event type, along with better error handling and logging for scenarios where the requested sequence number is unavailable.

Changes

File Change Summary
pkg/lib/watcher/watcher.go - Enhanced determineStartingIterator method logic
- Added direct return for EventIteratorTrimHorizon
- Improved handling of EventIteratorLatest type
- Added warning logging for sequence number mismatches
pkg/lib/watcher/watcher_test.go - Expanded test cases for iterator behavior
- Added scenarios for empty event stores
- Improved coverage for different iterator types
- Updated error handling test cases

Poem

🐰 Hop, hop, through event streams we go,
Iterators dancing, both fast and slow.
Trim the horizon, catch the latest beat,
Watching sequences make our code complete.
A rabbit's guide to event iteration's might! 🚀

Tip

CodeRabbit's docstrings feature is now available as part of our Early Access Program! Simply use the command @coderabbitai generate docstrings to have CodeRabbit automatically generate docstrings for your pull request.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d938376 and 76e0d11.

📒 Files selected for processing (1)
  • pkg/lib/watcher/watcher_test.go (5 hunks)
🔇 Additional comments (3)
pkg/lib/watcher/watcher_test.go (3)

115-118: LGTM: Well-structured test case for non-latest iterator without checkpoint.

The test case correctly verifies that the watcher continues from the specified sequence number when no checkpoint exists.


156-165: LGTM: Comprehensive test coverage for empty store scenarios.

The test cases thoroughly verify the behavior of different iterator types when the store is empty, ensuring consistent handling by starting from sequence 0.


177-195: LGTM: Excellent test coverage for handling future sequence numbers.

These test cases are crucial for ensuring the watcher gracefully handles sequence numbers that are at or beyond the latest event. They verify that:

  1. Exact matches to the latest sequence are preserved
  2. Future sequence numbers gracefully fall back to the latest sequence
  3. Both AtSequenceNumber and AfterSequenceNumber iterators handle future sequences consistently

This aligns perfectly with the PR objective of handling watchers with future sequence numbers gracefully.


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

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

Actionable comments posted: 0

🧹 Nitpick comments (2)
pkg/lib/watcher/watcher_test.go (1)

116-116: Correct the mismatched sequence number in the comment

The comment indicates storing events up to sequence number 15, but setupLatestEvent is set to ptr(uint64(10)). Please update the comment or the code to reflect the actual behavior.

pkg/lib/watcher/watcher.go (1)

111-117: Clarify log message for sequence number adjustment

When the requested sequence number exceeds the latest, the log message states: "requested sequence number is higher than latest, starting from latest instead". To enhance clarity, consider specifying that the watcher will start after the latest sequence number, effectively waiting for new events.

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 36c44b5 and d938376.

📒 Files selected for processing (2)
  • pkg/lib/watcher/watcher.go (1 hunks)
  • pkg/lib/watcher/watcher_test.go (3 hunks)
🔇 Additional comments (3)
pkg/lib/watcher/watcher_test.go (2)

185-189: Ensure consistency in test case expectations

In the test case "Sequence too high, start after latest", the initialIter is set to watcher.AtSequenceNumberIterator(20) while the setupLatestEvent is ptr(uint64(15)). The expectedIter is watcher.AfterSequenceNumberIterator(15). Verify that this is the intended behavior and that the expectedIter aligns with the logic in determineStartingIterator.


191-195: Ensure correctness in "After sequence too high" test case

Similarly, in the test case "After sequence too high, start after latest", confirm that setting expectedIter to watcher.AfterSequenceNumberIterator(15) when initialIter is watcher.AfterSequenceNumberIterator(20) and setupLatestEvent is ptr(uint64(15)) is the intended behavior.

pkg/lib/watcher/watcher.go (1)

95-118: Improved handling of initial event iterators in determineStartingIterator

The updated logic correctly handles the EventIteratorTrimHorizon by returning it directly, and adjusts the starting iterator appropriately for EventIteratorLatest and when the requested sequence number exceeds the latest. This ensures the watcher starts from the correct position according to the iterator type.

@wdbaruni wdbaruni merged commit 823e9e9 into main Dec 16, 2024
14 checks passed
@wdbaruni wdbaruni deleted the eng-453-handle-watchers-with-future-sequence-numbers-gracefully branch December 16, 2024 12:07
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant