Skip to content

Conversation

agourlay
Copy link
Member

@agourlay agourlay commented Mar 11, 2025

This PR makes sure all update operation are priced according to the content of their content.

It leverages an existing infrastructure for computing the cost of updates from #5729.

@agourlay agourlay added this to the Rate limiting milestone Mar 11, 2025
@agourlay agourlay marked this pull request as ready for review March 11, 2025 14:43
@@ -54,8 +54,7 @@ impl ShardReplicaSet {
let result = match state {
ReplicaState::Active => {
// Rate limit update operations on Active replica
// TODO(ratelimits) determine cost of update based on operation
Copy link
Member Author

Choose a reason for hiding this comment

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

used to be always 1 token per update

@@ -296,19 +295,11 @@ impl ShardReplicaSet {

if self.peer_is_active(this_peer_id) {
// Check write rate limiter before proceeding if replica active
// TODO(ratelimits) determine cost of update based on operation

self.check_write_rate_limiter_lazy(&hw_measurement_acc, || {
Copy link
Member Author

Choose a reason for hiding this comment

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

this path already had the fix in place, just needed to refactor to share it

Copy link
Contributor

coderabbitai bot commented Mar 11, 2025

📝 Walkthrough

Walkthrough

This change introduces a new method, check_operation_write_rate_limiter, within the ShardReplicaSet implementation. The new method calculates the cost of an operation dynamically by estimating the request’s cardinality and checks it against the write rate limiter. Consequently, the previous fixed-cost approach (using a cost of 1) in the update_local and update_impl methods has been replaced. Additionally, outdated comments regarding cost calculation have been removed. In the testing suite, a new test function for batch update operations has been added to verify that write rate limiting works as expected for batch updates, and an existing strict mode test has been renamed to focus on filtered update operations. These changes adjust both the core rate limiting logic and its tests.

Possibly related PRs

Suggested reviewers

  • timvisee
  • JojiiOfficial

📜 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 4b74671 and 075636c.

📒 Files selected for processing (2)
  • lib/collection/src/shards/replica_set/update.rs (4 hunks)
  • tests/openapi/test_strictmode.py (2 hunks)
🧰 Additional context used
🪛 Ruff (0.8.2)
tests/openapi/test_strictmode.py

1094-1094: Redefinition of unused collection_name from line 3

(F811)


1134-1134: Redefinition of unused collection_name from line 3

(F811)

🔇 Additional comments (6)
tests/openapi/test_strictmode.py (3)

1094-1094: Function name updated to better reflect its purpose.

The function renaming from test_strict_mode_write_rate_limiting_update_op to test_strict_mode_write_rate_limiting_filtered_update_op more accurately describes the test's focus on filtered update operations specifically, improving code clarity.

🧰 Tools
🪛 Ruff (0.8.2)

1094-1094: Redefinition of unused collection_name from line 3

(F811)


1134-1174: Well-structured test for batch update rate limiting.

This new test function thoroughly validates that write rate limiting works correctly for batch updates based on point count. The test effectively checks three important scenarios:

  1. Updates exceeding the rate limit (11 points) are rejected
  2. Updates within the rate limit (10 points) succeed
  3. Subsequent updates fail after tokens are depleted

The implementation aligns perfectly with the PR objective of ensuring operations are priced based on their content.

🧰 Tools
🪛 Ruff (0.8.2)

1134-1134: Redefinition of unused collection_name from line 3

(F811)


1094-1094:

✅ Verification successful

Function parameters redefined but not used.

The static analysis shows that the collection_name parameter is redefined but appears unused. This is likely a false positive as it's a pytest fixture that's implicitly used for test setup.

Also applies to: 1134-1134


🏁 Script executed:

#!/bin/bash
# Check how the fixture is used in similar test functions
grep -n "def test_.*collection_name" tests/openapi/test_strictmode.py | head -n2

Length of output: 191


Refined Review Comment: False Positive on Fixture Usage

After verifying the similar test functions in the file (e.g., test_patch_collection_full at line 46 and test_patch_collection_partially at line 78) that utilize the collection_name fixture—and noting that it’s implicit in the test setup—the static analysis flag is a false positive. No changes are needed here.

🧰 Tools
🪛 Ruff (0.8.2)

1094-1094: Redefinition of unused collection_name from line 3

(F811)

lib/collection/src/shards/replica_set/update.rs (3)

535-556: Well-implemented dynamic rate limiting based on operation cost.

The new check_operation_write_rate_limiter method elegantly solves the problem of pricing operations based on their content. By using the estimate_request_cardinality method to determine the operation cost dynamically, the implementation ensures that batch operations consume tokens proportional to their size.

The approach is robust with:

  • Fallback to minimum cost of 1 (maintaining backward compatibility for simple operations)
  • Error logging when cardinality estimation fails (rather than failing the operation)
  • Clean integration with the existing rate limiting system

This is a significant improvement over the previous fixed-cost approach.


57-57: Update call sites now use the dynamic cost calculation.

The replacement of direct rate limiting calls with the new method in both update_local and update_impl ensures consistent application of the dynamic pricing model throughout the codebase. All write operations will now be properly charged based on their actual impact rather than a fixed cost.

Also applies to: 298-302


13-13: Added necessary import for the new method.

The addition of Shard to the imports supports the new method implementation which requires the shard to estimate request cardinality.

✨ Finishing Touches
  • 📝 Generate Docstrings

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
🪧 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.
  • @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

@coszio coszio left a comment

Choose a reason for hiding this comment

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

Nice usage of OperationAffectArea

@agourlay agourlay requested a review from JojiiOfficial March 11, 2025 14:54
@agourlay agourlay merged commit 1a0862e into dev Mar 11, 2025
17 checks passed
@agourlay agourlay deleted the write-rate-limiting-batch-update branch March 11, 2025 15:10
@timvisee timvisee mentioned this pull request Mar 21, 2025
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.

3 participants