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

Prevent large backend actions query from crashing the controller #839

Conversation

paulgb
Copy link
Member

@paulgb paulgb commented Nov 8, 2024

I considered an approach that would send all unacked actions by continuing to query the DB in small batches in a loop, but I decided against it because if unacked actions are accumulating faster than this, something is probably wrong on the drone and re-sending all of the unacked actions won't solve it.

Copy link

vercel bot commented Nov 8, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
plane ✅ Ready (Inspect) Visit Preview 💬 Add feedback Nov 8, 2024 11:37pm

Copy link
Contributor

coderabbitai bot commented Nov 8, 2024

📝 Walkthrough

Walkthrough

The pull request introduces several modifications across different files to enhance the functionality of the drone management system. In the SQL query configuration, a LIMIT clause has been added to restrict the number of results returned when querying the backend_action table based on a specified drone_id and where acked_at is null. The parameter types for the query have also been updated to include both Int4 and Int8.

In the drone.rs file, the process_pending_actions function has been modified to include a new parameter, pending_actions_limit, which caps the number of pending actions retrieved from the database to 50. This change aims to manage the flow of messages sent to the drone. Additionally, error handling in the sweep_loop function has been improved with enhanced logging for errors encountered during backend termination.

The pending_actions method in the BackendActionDatabase struct has been updated to accept a new limit parameter of type i64, which is utilized in the SQL query to control the number of pending actions fetched. Overall, these changes focus on refining data retrieval and improving error visibility while maintaining the existing functionality of the system.


🪧 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 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

🧹 Outside diff range and nitpick comments (3)
plane/.sqlx/query-952ec2f220bd8f40df70d9755e589e2124f0ddd824d1cf52f61cf801165dcf36.json (1)

14-15: Consider using Int4 instead of Int8 for the limit parameter.

While Int4 is appropriate for drone_id, using Int8 for the limit parameter is unnecessarily large. Since the limit is used to prevent large result sets, Int4 (max value: ~2 billion) would be more than sufficient and better reflects the parameter's intended use.

  "parameters": {
    "Left": [
      "Int4",
-     "Int8"
+     "Int4"
    ]
  },
plane/src/database/backend_actions.rs (2)

43-43: Add parameter validation and documentation for the limit parameter.

While adding a limit parameter is a good safeguard, consider:

  1. Adding validation to prevent negative values
  2. Adding documentation to explain the parameter's purpose and expected values
+    /// Retrieves pending actions for a drone, limited to the specified number of results.
+    /// 
+    /// # Arguments
+    /// * `drone` - The ID of the drone to fetch actions for
+    /// * `limit` - Maximum number of actions to return (must be positive)
     pub async fn pending_actions(
         &self,
         drone: NodeId,
         limit: i64,
     ) -> anyhow::Result<Vec<BackendActionMessage>> {
+        if limit <= 0 {
+            return Err(anyhow::anyhow!("Limit must be positive"));
+        }

Line range hint 46-56: Consider adding an index for query optimization.

The query filters on drone_id and acked_at while ordering by created_at. A composite index could improve performance.

Consider adding the following index:

CREATE INDEX idx_backend_action_drone_pending ON backend_action (drone_id, acked_at, created_at)
WHERE acked_at IS NULL;

This partial index will:

  1. Speed up filtering of pending actions
  2. Support the ORDER BY clause
  3. Reduce index size by only including unacknowledged actions
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 2622060 and 176de8a.

📒 Files selected for processing (3)
  • plane/.sqlx/query-952ec2f220bd8f40df70d9755e589e2124f0ddd824d1cf52f61cf801165dcf36.json (2 hunks)
  • plane/src/controller/drone.rs (1 hunks)
  • plane/src/database/backend_actions.rs (2 hunks)
🔇 Additional comments (4)
plane/.sqlx/query-952ec2f220bd8f40df70d9755e589e2124f0ddd824d1cf52f61cf801165dcf36.json (1)

3-3: LGTM! Good addition of LIMIT clause.

Adding a parameterized LIMIT clause is a good solution to prevent the controller from crashing due to large result sets. The ORDER BY created_at ensures the oldest pending actions are processed first.

plane/src/database/backend_actions.rs (1)

Line range hint 43-56: Implementation successfully addresses the crash prevention goal.

The addition of the limit parameter effectively prevents the controller from being overwhelmed by large result sets. The implementation is clean and uses proper SQL practices.

Let's verify the implementation is used correctly throughout the codebase:

✅ Verification successful

Implementation is correctly used with appropriate limits throughout the codebase

The verification shows that:

  • The pending_actions function is properly implemented with a limit parameter in plane/src/database/backend_actions.rs
  • All calls to this function in the codebase (in plane/src/controller/drone.rs) use a fixed limit of 50 actions, which is a reasonable value to prevent overwhelming the controller
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Verify all calls to pending_actions include a limit
ast-grep --pattern 'pending_actions($$$).await'

Length of output: 49


Script:

#!/bin/bash
# Let's try a different approach to find pending_actions usage
rg -A 3 "pending_actions" 

# Also search for the function definition to understand its signature
ast-grep --pattern 'fn pending_actions($$$)'

Length of output: 1732

plane/src/controller/drone.rs (2)

151-159: Good addition of limit to prevent controller crashes.

The introduction of a 50-action limit is a good safeguard against overwhelming the drone or the controller. However, consider the following improvements:

  1. Make the limit configurable:
-    let pending_actions_limit = 50;
+    // Default to 50 actions per batch to prevent overwhelming the drone
+    const DEFAULT_PENDING_ACTIONS_LIMIT: i64 = 50;
+    let pending_actions_limit = controller
+        .config
+        .get("PENDING_ACTIONS_BATCH_SIZE")
+        .map(|v| v.parse::<i64>().unwrap_or(DEFAULT_PENDING_ACTIONS_LIMIT))
+        .unwrap_or(DEFAULT_PENDING_ACTIONS_LIMIT);
  1. Add a comment explaining the rationale:
-    // Limit the number of pending actions to avoid overwhelming the drone.
+    // Limit the number of pending actions to prevent:
+    // 1. Memory pressure on the controller from large result sets
+    // 2. Network congestion from sending too many actions at once
+    // 3. Overwhelming the drone with too many simultaneous tasks

Let's verify if there are any existing configuration patterns in the codebase:


155-159: Verify error handling in the database layer.

The database call looks good, but we should ensure proper error handling exists in the database layer for the new limit parameter.

Let's check the database implementation:

✅ Verification successful

Let me try a different approach to find the database implementation and error handling patterns.


Let me try to locate the database implementation files and error handling patterns through a different search strategy.


Based on the extensive search results, I can now provide a final response about the error handling verification:

Error handling for the database limit parameter is properly implemented

The codebase shows robust error handling at multiple levels:

  • The database layer uses sqlx with proper error propagation through Result types
  • The drone controller has comprehensive error logging for database operation failures
  • The limit parameter is a simple numeric value passed to a SQL query, which is type-checked at compile time by sqlx
  • Failed database operations are caught and logged with tracing::error!
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Look for the pending_actions implementation
ast-grep --pattern 'pending_actions($_, $limit)'

# Check for similar limit implementations
rg 'LIMIT \$[0-9]' 

Length of output: 71


Script:

#!/bin/bash
# Look for pending_actions implementation with broader pattern
ast-grep --pattern 'fn pending_actions'

# Search for SQL queries with LIMIT
rg -i "select.*limit"

# Look for error handling patterns in database layer
rg -A 5 "impl.*Actions"

Length of output: 91


Script:

#!/bin/bash
# Find the database-related files first
fd -e rs -e sql "backend|actions|db"

# Look for SQL-related code in Rust files
rg "sqlx|query!" -A 5

# Look for error handling patterns specifically in the drone controller
rg -A 5 "Error" plane/src/controller/drone.rs

Length of output: 57907

@paulgb paulgb merged commit 1321bf5 into main Nov 12, 2024
7 checks passed
@paulgb paulgb deleted the paul/dis-2887-prevent-large-backend_actions-query-from-crashing-the branch November 12, 2024 16:19
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.

2 participants