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): torii-runner #2881

Merged
merged 5 commits into from
Jan 9, 2025
Merged

Conversation

Larkooo
Copy link
Collaborator

@Larkooo Larkooo commented Jan 9, 2025

Summary by CodeRabbit

  • Dependency Updates

    • Added torii-runner as a new workspace dependency
    • Simplified dependencies in the Torii binary
  • Architecture Changes

    • Introduced a new Runner class to manage application initialization and execution
    • Streamlined the main application startup process
    • Consolidated multiple setup components into a single, modular approach
  • Performance Improvements

    • Optimized application configuration and startup sequence
    • Implemented more efficient asynchronous task management

Copy link

coderabbitai bot commented Jan 9, 2025

Warning

Rate limit exceeded

@Larkooo has exceeded the limit for the number of commits or files that can be reviewed per hour. Please wait 3 minutes and 30 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 6996d8e and ff1939f.

📒 Files selected for processing (1)
  • crates/torii/runner/Cargo.toml (1 hunks)

Walkthrough

Ohayo, sensei! The pull request introduces a significant refactoring of the Torii binary, centralizing its execution logic into a new torii-runner package. The changes simplify the main application structure by moving complex initialization and setup processes into a dedicated Runner struct. This restructuring reduces dependency complexity and encapsulates the application's lifecycle management within a more modular approach.

Changes

File Change Summary
Cargo.toml Added torii-runner dependency at crates/torii/runner
bin/torii/Cargo.toml Dramatically reduced dependencies, keeping only torii-runner, tokio, anyhow, and clap
bin/torii/src/main.rs Replaced complex initialization with simple Runner instantiation and run() method call
crates/torii/runner/Cargo.toml Created new package with comprehensive dependencies and feature configurations
crates/torii/runner/src/lib.rs Introduced Runner struct with centralized application setup and execution logic

Sequence Diagram

sequenceDiagram
    participant Main as Main Application
    participant Runner as Runner
    participant SQLite as SQLite Database
    participant JSONRPC as JSON-RPC Client
    participant Executor as Transaction Executor
    participant Server as Servers

    Main->>Runner: Create with arguments
    Runner->>Runner: Validate world address
    Runner->>Runner: Setup logging
    Runner->>SQLite: Initialize connection pool
    Runner->>JSONRPC: Create client
    Runner->>Executor: Initialize
    Runner->>Server: Configure and spawn (GraphQL, gRPC, Relay)
    Runner->>Runner: Await task completion or shutdown
Loading

Possibly related PRs

Suggested reviewers

  • glihm

Finishing Touches

  • 📝 Generate Docstrings

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

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: 2

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 5d1d308 and 79caf0d.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (5)
  • Cargo.toml (1 hunks)
  • bin/torii/Cargo.toml (1 hunks)
  • bin/torii/src/main.rs (1 hunks)
  • crates/torii/runner/Cargo.toml (1 hunks)
  • crates/torii/runner/src/lib.rs (1 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (1)
  • GitHub Check: fmt
🔇 Additional comments (5)
bin/torii/src/main.rs (1)

21-23: Ohayo, sensei! Great modularization with the Runner struct

Encapsulating the application initialization into Runner enhances the codebase's modularity and maintainability.

bin/torii/Cargo.toml (1)

8-11: Ohayo, sensei! Nice reduction of dependencies

Streamlining the dependencies enhances build times and reduces complexity.

crates/torii/runner/Cargo.toml (2)

1-6: Ohayo! Package configuration looks good, sensei!

The package metadata properly inherits workspace settings while maintaining its unique identity.


60-63: Features configuration is well-organized, sensei!

The default features and their propagation to underlying crates are properly configured.

Cargo.toml (1)

128-128: Ohayo! The workspace dependency addition looks perfect, sensei!

The torii-runner is properly integrated into the workspace dependencies with the correct path.

// Set the number of threads based on CPU count
let cpu_count = std::thread::available_parallelism().unwrap().get();
let thread_count = cmp::min(cpu_count, 8);
sqlx::query(&format!("PRAGMA threads = {};", thread_count)).execute(&pool).await?;
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Sensei, consider parameterizing SQL queries to prevent injection risks

While thread_count is an internal value, using parameterized queries enhances security and follows best practices.

Apply this diff to use a parameterized query:

-sqlx::query(&format!("PRAGMA threads = {};", thread_count)).execute(&pool).await?;
+sqlx::query("PRAGMA threads = ?;").bind(thread_count).execute(&pool).await?;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
sqlx::query(&format!("PRAGMA threads = {};", thread_count)).execute(&pool).await?;
sqlx::query("PRAGMA threads = ?;").bind(thread_count).execute(&pool).await?;

futures.workspace = true
http-body = "0.4.5"
http.workspace = true
hyper-reverse-proxy = { git = "https://github.com/tarrencev/hyper-reverse-proxy" }
Copy link

Choose a reason for hiding this comment

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

💡 Codebase verification

Ohayo! Yes, please pin that git dependency to commit 4bfaf98, sensei!

The repository has no tags or releases, making it even more important to pin to a specific commit. Update the dependency to:

hyper-reverse-proxy = { git = "https://github.com/tarrencev/hyper-reverse-proxy", rev = "4bfaf98b7ae61a49c6238ee1bd38ad030e7fa7f6" }
🔗 Analysis chain

Consider pinning the git dependency to a specific commit, sensei!

Using a git dependency without a specific commit or tag for hyper-reverse-proxy could lead to reproducibility issues.

Run this script to check the dependency's commit history:

🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check the commit history of the hyper-reverse-proxy dependency
gh repo view tarrencev/hyper-reverse-proxy --json updatedAt,latestTag,defaultBranchRef \
  --jq '{
    lastUpdated: .updatedAt,
    latestTag: .latestTag.name,
    defaultBranch: .defaultBranchRef.name
  }'

Length of output: 1344


Script:

#!/bin/bash
# Check if repo exists and get basic info
echo "=== Repository Info ==="
gh repo view tarrencev/hyper-reverse-proxy --json updatedAt,pushedAt,url 2>/dev/null || echo "Repository not found"

echo -e "\n=== Latest Release Info ==="
gh repo view tarrencev/hyper-reverse-proxy --json latestRelease --jq '.latestRelease' 2>/dev/null || echo "No release info available"

echo -e "\n=== Recent Commits ==="
git ls-remote https://github.com/tarrencev/hyper-reverse-proxy.git HEAD refs/tags/* 2>/dev/null || echo "Cannot fetch git references"

Length of output: 612

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: 1

🧹 Nitpick comments (3)
crates/torii/runner/src/lib.rs (3)

83-86: Consider handling send errors in the Ctrl-C handler

The current implementation silently ignores send errors. While this might be acceptable for shutdown scenarios, logging the error could help with debugging.

-            let _ = shutdown_tx_clone.send(());
+            if let Err(e) = shutdown_tx_clone.send(()) {
+                eprintln!("Failed to send shutdown signal: {}", e);
+            }

259-266: Add documentation for metrics endpoint configuration

While the metrics setup is well-implemented, adding documentation about the metrics endpoint and available metrics would improve maintainability.

Consider adding a doc comment:

+        // Start the metrics server if metrics are enabled
+        // Exposes Prometheus metrics at http://{metrics_addr}:{metrics_port}/metrics
+        // Available metrics:
+        // - torii_process_* : Process-level metrics
+        // - Add other available metrics here
         if self.args.metrics.metrics {

277-286: Consider adding timeout handling for long-running tasks

The current implementation waits indefinitely for tasks to complete. Adding a timeout mechanism could prevent hanging in case of task deadlocks.

Consider adding a timeout:

-        tokio::select! {
+        tokio::select! {
+            _ = tokio::time::sleep(Duration::from_secs(300)) => {
+                error!(target: LOG_TARGET, "Timeout waiting for tasks to complete");
+                return Ok(());
+            }
             res = engine_handle => res??,
             res = executor_handle => res??,
             // ... other tasks ...
         };
📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 79caf0d and 6996d8e.

⛔ Files ignored due to path filters (1)
  • Cargo.lock is excluded by !**/*.lock
📒 Files selected for processing (3)
  • Cargo.toml (1 hunks)
  • bin/torii/Cargo.toml (1 hunks)
  • crates/torii/runner/src/lib.rs (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • Cargo.toml
🔇 Additional comments (3)
bin/torii/Cargo.toml (1)

7-9: Ohayo! Clean dependency management, sensei!

The reduction in dependencies and addition of workspace-level dependencies torii-cli and torii-runner aligns well with the refactoring goals, making the package structure more modular and maintainable.

crates/torii/runner/src/lib.rs (2)

120-120: Ohayo sensei! Consider parameterizing SQL queries to prevent injection risks

While thread_count is an internal value, using parameterized queries enhances security and follows best practices.


59-63: Clean error handling, sensei!

The early validation of the world address with a clear error message is a good practice.

Comment on lines +292 to +314
async fn spawn_rebuilding_graphql_server(
shutdown_tx: Sender<()>,
pool: Arc<SqlitePool>,
proxy_server: Arc<Proxy>,
) {
let mut broker = SimpleBroker::<Model>::subscribe();

loop {
let shutdown_rx = shutdown_tx.subscribe();
let (new_addr, new_server) = torii_graphql::server::new(shutdown_rx, &pool).await;

tokio::spawn(new_server);

proxy_server.set_graphql_addr(new_addr).await;

// Break the loop if there are no more events
if broker.next().await.is_none() {
break;
} else {
tokio::time::sleep(Duration::from_secs(1)).await;
}
}
}
Copy link

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Enhance error handling in GraphQL server spawning

The current implementation has two potential issues:

  1. No error handling for server creation
  2. Fixed sleep duration might not be optimal for all scenarios

Consider:

  1. Adding error handling for server creation
  2. Making the sleep duration configurable
 async fn spawn_rebuilding_graphql_server(
     shutdown_tx: Sender<()>,
     pool: Arc<SqlitePool>,
     proxy_server: Arc<Proxy>,
+    retry_delay: Option<Duration>,
 ) {
     let mut broker = SimpleBroker::<Model>::subscribe();
+    let delay = retry_delay.unwrap_or(Duration::from_secs(1));
 
     loop {
         let shutdown_rx = shutdown_tx.subscribe();
-        let (new_addr, new_server) = torii_graphql::server::new(shutdown_rx, &pool).await;
+        let server_result = torii_graphql::server::new(shutdown_rx, &pool).await;
+        match server_result {
+            Ok((new_addr, new_server)) => {
+                tokio::spawn(new_server);
+                proxy_server.set_graphql_addr(new_addr).await;
+            }
+            Err(e) => {
+                error!(target: LOG_TARGET, "Failed to create GraphQL server: {}", e);
+            }
+        }
 
-        tokio::spawn(new_server);
-
-        proxy_server.set_graphql_addr(new_addr).await;
-
         if broker.next().await.is_none() {
             break;
         } else {
-            tokio::time::sleep(Duration::from_secs(1)).await;
+            tokio::time::sleep(delay).await;
         }
     }
 }

Committable suggestion skipped: line range outside the PR's diff.

Copy link

codecov bot commented Jan 9, 2025

Codecov Report

Attention: Patch coverage is 0% with 218 lines in your changes missing coverage. Please review.

Project coverage is 55.80%. Comparing base (7036164) to head (ff1939f).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
crates/torii/runner/src/lib.rs 0.00% 215 Missing ⚠️
bin/torii/src/main.rs 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2881      +/-   ##
==========================================
+ Coverage   55.78%   55.80%   +0.01%     
==========================================
  Files         448      449       +1     
  Lines       57735    57720      -15     
==========================================
  Hits        32209    32209              
+ Misses      25526    25511      -15     

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

@Larkooo Larkooo enabled auto-merge (squash) January 9, 2025 09:20
@Larkooo Larkooo merged commit 7718099 into dojoengine:main Jan 9, 2025
13 of 15 checks passed
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