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

refactor(torii-core): upgrade model silent if not in namespace #2688

Merged
merged 4 commits into from
Nov 14, 2024

Conversation

Larkooo
Copy link
Collaborator

@Larkooo Larkooo commented Nov 14, 2024

Summary by CodeRabbit

  • Bug Fixes
    • Enhanced error handling for model retrieval in event processing, providing better visibility when models are absent.
  • Refactor
    • Cleaned up import statements and improved logging for clearer event processing flow without affecting functionality.

Copy link

coderabbitai bot commented Nov 14, 2024

Walkthrough

Ohayo, sensei! This pull request modifies the error handling logic in the process methods of multiple processors, including UpgradeEventProcessor, UpgradeModelProcessor, StoreDelRecordProcessor, StoreSetRecordProcessor, and StoreUpdateRecordProcessor. The changes introduce a match statement to handle database model retrieval errors more gracefully, allowing for silent handling of "no rows" errors while still propagating other errors. Additionally, the import of Ok from the anyhow crate has been removed for code cleanliness. The overall control flow remains unchanged.

Changes

File Path Change Summary
crates/torii/core/src/processors/upgrade_event.rs, crates/torii/core/src/processors/upgrade_model.rs Modified process methods to use a match statement for error handling during model retrieval, allowing silent handling of "no rows" errors while propagating other errors. Removed import of Ok from anyhow crate.
crates/torii/core/src/processors/store_del_record.rs, crates/torii/core/src/processors/store_set_record.rs, crates/torii/core/src/processors/store_update_record.rs Enhanced error handling in process methods to log debug messages for "no rows" errors before returning Ok(()). Updated imports to include debug logging from the tracing crate.

Possibly related PRs

Suggested reviewers

  • glihm

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

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

@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 (2)
crates/torii/core/src/processors/upgrade_model.rs (1)

59-67: Consider making the error handling more robust, sensei!

The current string-based error matching could be fragile if database error messages change. Consider creating a more robust solution.

Here's a suggested approach:

- if e.to_string().contains("no rows") {
+ // Add this helper at module level
+ fn is_not_found_error(e: &Error) -> bool {
+     // Consider using error downcast or specific error types
+     // depending on your database layer
+     e.to_string().contains("no rows")
+ }
+
+ // In the match arm
+ if is_not_found_error(&e) {
crates/torii/core/src/processors/upgrade_event.rs (1)

62-69: Consider extracting this error handling pattern, sensei! 🎋

Since this error handling pattern might be reused across processors, consider extracting it into a helper function. Also, string matching for error types can be fragile.

Here's a suggested approach:

// In a common utility module
pub(crate) fn handle_model_not_found<T>(result: Result<T, Error>) -> Result<Option<T>, Error> {
    match result {
        Ok(value) => Ok(Some(value)),
        Err(e) if e.to_string().contains("no rows") => Ok(None),
        Err(e) => Err(e),
    }
}

// In your processor
let model = match handle_model_not_found(db.model(event.selector).await)? {
    Some(m) => m,
    None => return Ok(()),
};
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 6af7e92 and 60f49d6.

📒 Files selected for processing (2)
  • crates/torii/core/src/processors/upgrade_event.rs (2 hunks)
  • crates/torii/core/src/processors/upgrade_model.rs (2 hunks)
🔇 Additional comments (3)
crates/torii/core/src/processors/upgrade_model.rs (2)

1-1: Ohayo! Import cleanup looks good, sensei!

The removal of the explicit Ok import from anyhow is a nice cleanup since it's available via the prelude.


57-67: Ohayo! The error handling approach is solid, sensei!

The new match-based error handling elegantly handles the case where models don't exist in the indexed namespace, with clear comments explaining the behavior.

crates/torii/core/src/processors/upgrade_event.rs (1)

59-70: Ohayo! Clean and well-documented error handling implementation, sensei! 🎌

The new error handling approach elegantly handles the case where models don't exist in the namespace, which aligns perfectly with the PR objective. The added comments clearly explain the rationale.

Let's verify the consistency of this pattern across other processors:

✅ Verification successful

Ohayo! The error handling pattern is perfectly consistent across processors, sensei! ✨

The verification shows that the same "no rows" error handling pattern is consistently implemented across all relevant processors:

  • store_del_record.rs
  • store_update_member.rs
  • store_set_record.rs
  • store_update_record.rs
  • upgrade_model.rs
  • upgrade_event.rs (the file under review)

All processors follow the same pattern of silently returning Ok(()) when a model is not found, which aligns perfectly with the PR objective of handling models not in the namespace.

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check if similar error handling patterns exist in other processors
# Expected: Find similar patterns in upgrade_model.rs and potentially other processors

# Search for similar error handling patterns in processor files
rg -U "match.*model.*\{.*contains.*no rows.*Ok\(\(\)\).*\}" crates/torii/core/src/processors/

# Look for any other direct model queries that might need similar handling
ast-grep --pattern 'db.model($$$).await?'

Length of output: 139


Script:

#!/bin/bash
# Let's try a broader search for model-related error handling patterns
rg -U "match.*db\.model.*\{.*Err.*\}" crates/torii/core/src/processors/

# Search for any direct model queries in processor files
rg "db\.model\(" crates/torii/core/src/processors/

# Look for similar error handling patterns with 'no rows'
rg -U "no rows" crates/torii/core/src/processors/

# Search for all processor files to understand the context better
fd ".*processor\.rs$" crates/torii/core/src/processors/

Length of output: 1659


Script:

#!/bin/bash
# Let's check the actual error handling patterns in these files
rg -U -A 5 "no rows" crates/torii/core/src/processors/store_update_record.rs crates/torii/core/src/processors/store_update_member.rs crates/torii/core/src/processors/store_set_record.rs crates/torii/core/src/processors/store_del_record.rs

# Also check upgrade_model.rs specifically since it's closely related
cat crates/torii/core/src/processors/upgrade_model.rs

Length of output: 5943

Copy link

codecov bot commented Nov 14, 2024

Codecov Report

Attention: Patch coverage is 38.88889% with 33 lines in your changes missing coverage. Please review.

Project coverage is 57.24%. Comparing base (6af7e92) to head (db93dba).
Report is 1 commits behind head on main.

Files with missing lines Patch % Lines
crates/torii/core/src/processors/upgrade_event.rs 0.00% 15 Missing ⚠️
crates/torii/core/src/processors/upgrade_model.rs 0.00% 15 Missing ⚠️
...ates/torii/core/src/processors/store_del_record.rs 87.50% 1 Missing ⚠️
...ates/torii/core/src/processors/store_set_record.rs 87.50% 1 Missing ⚠️
...s/torii/core/src/processors/store_update_record.rs 87.50% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2688      +/-   ##
==========================================
- Coverage   57.26%   57.24%   -0.03%     
==========================================
  Files         408      408              
  Lines       51754    51791      +37     
==========================================
+ Hits        29638    29648      +10     
- Misses      22116    22143      +27     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@kariy kariy enabled auto-merge (squash) November 14, 2024 09:21
Copy link

@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)
crates/torii/core/src/processors/store_del_record.rs (1)

58-66: Ohayo sensei! Consider using a custom error type for more idiomatic error handling

The error handling and debug logging implementation looks good! However, checking error messages via string matching could be fragile if the underlying error message changes.

Consider defining a custom error type or using the database-specific error type for more robust error matching:

- Err(e) if e.to_string().contains("no rows") => {
+ Err(e) if matches!(e.downcast_ref(), Some(SqlError::NoRows)) => {

This would require:

  1. Defining a custom error enum or using your SQL library's error type
  2. Using downcast_ref() for type-safe error matching

The current implementation is functional, but this change would make it more maintainable and type-safe.

crates/torii/core/src/processors/store_set_record.rs (1)

59-67: LGTM! Improved error handling with better observability.

Ohayo sensei! The enhanced error handling is a solid improvement:

  • Gracefully handles the "no rows" case with debug logging
  • Maintains existing behavior while improving observability
  • Follows Rust's idiomatic error handling patterns

Consider standardizing this pattern across other processors in the codebase for consistent error handling and logging behavior.

crates/torii/core/src/processors/store_update_record.rs (1)

Line range hint 1-100: Solid implementation approach overall!

The changes demonstrate good practices:

  • Error handling is explicit and maintainable
  • Logging levels are appropriately chosen (debug for diagnostic info, info for business events)
  • The code maintains clean Rust idioms

Consider adding metrics/counters for skipped models to help monitor the system's behavior in production.

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between 60f49d6 and db93dba.

📒 Files selected for processing (5)
  • crates/torii/core/src/processors/store_del_record.rs (2 hunks)
  • crates/torii/core/src/processors/store_set_record.rs (2 hunks)
  • crates/torii/core/src/processors/store_update_record.rs (2 hunks)
  • crates/torii/core/src/processors/upgrade_event.rs (2 hunks)
  • crates/torii/core/src/processors/upgrade_model.rs (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
  • crates/torii/core/src/processors/upgrade_event.rs
  • crates/torii/core/src/processors/upgrade_model.rs
🔇 Additional comments (4)
crates/torii/core/src/processors/store_del_record.rs (1)

7-7: Ohayo! Clean import addition for logging capabilities!

The import of both debug and info levels is well-aligned with their usage in the code.

crates/torii/core/src/processors/store_set_record.rs (1)

7-7: LGTM! Clean import addition.

Ohayo! The addition of debug from tracing is well-aligned with the new error handling improvements.

crates/torii/core/src/processors/store_update_record.rs (2)

62-70: Ohayo! Clean error handling pattern, sensei!

The match pattern for database errors is well-implemented:

  • Gracefully handles the "no rows" case with appropriate debug logging
  • Properly propagates other database errors
  • Maintains good observability with contextual logging

8-8: Good logging practices!

The debug-level logging for skipped models is appropriate and includes relevant context (selector) for troubleshooting.

Also applies to: 63-67

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