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

chore: improve tracing handling #144

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

Conversation

jeluard
Copy link
Contributor

@jeluard jeluard commented Mar 13, 2025

WIP

Clarifies tracing usage by advocating to leverage smaller functions and instrument.
Remove parent from spans as they should be inherited; new threads loose it so it has to be restored at the gasket stage boundaries.

Summary by CodeRabbit

  • New Features
    • Introduced updated block processing and state management to deliver improved system stability and observability.
    • Added new methods for managing rewards and state within the ledger.
    • Enhanced logging capabilities in the roll_back method for better traceability.
  • Refactor
    • Streamlined internal interfaces by removing redundant parameters and consolidating processing logic, resulting in smoother operations.
    • Improved method signatures to enhance usability without consuming instances.
  • Chores
    • Added refined methods for reward adjustments, state updates, and metric resets, enhancing overall system performance and monitoring.

Copy link

coderabbitai bot commented Mar 13, 2025

Walkthrough

This pull request removes redundant span propagation parameters and refines method signatures across multiple modules. In the consensus code, the parent_span parameter is removed, and spans are now captured using Span::current(). Ledger methods have been updated to borrow rather than take ownership, and new functions manage volatile state and database operations. In the pipeline and sync modules, block processing now flows through a new forward method with improved tracing instrumentation, streamlining rollback events and overall parameter management.

Changes

File(s) Change Summary
crates/amaru-consensus/src/consensus/header_validation.rs
crates/amaru-consensus/src/consensus/wiring.rs
Removed parent_span/Span parameters from method signatures and enum variants; updated tracing calls to use Span::current() and simplified span management.
crates/amaru-ledger/src/rewards.rs
crates/amaru-ledger/src/state/transaction.rs
Changed ownership semantics (using references instead of moving self) and removed unnecessary parent parameters from logging and function signatures.
crates/amaru-ledger/src/state.rs Introduced handle_volatile_state to encapsulate volatile ledger state management; updated the forward method by removing span parameters.
crates/amaru-ledger/src/store.rs
crates/amaru-stores/src/rocksdb/mod.rs
Added new methods: tick_pools, apply_rewards, adjust_pots, reset_fees, and reset_blocks_count to modularise database operations with enhanced tracing.
crates/amaru/src/pipeline.rs
crates/amaru/src/sync/pull.rs
simulation/amaru-sim/src/bin/amaru-sim/sync.rs
Introduced a new forward method in Stage and updated roll_forward/rollback_to methods with improved instrumentation; removed span parameters from rollback events.

Sequence Diagram(s)

sequenceDiagram
    participant Client
    participant Stage
    participant State
    participant LedgerStore

    Client->>Stage: roll_forward(point, raw_block)
    Stage->>Stage: forward(point, block, block_header_hash)
    Note right of Stage: Process block & validate state
    Stage->>State: forward(slot, point, block)
    State->>State: handle_volatile_state(now_stable, slot, current_epoch)
    State-->>Stage: BlockValidationResult
    Stage-->>Client: BlockValidationResult
Loading
sequenceDiagram
    participant Peer
    participant Consensus

    Peer->>Consensus: forward_block(peer, header)
    Consensus->>Consensus: Capture current span (Span::current())
    Consensus-->>Peer: ValidateBlockEvent::Validated
Loading

Possibly related PRs

Suggested reviewers

  • abailly
  • KtorZ

Poem

In our code, the spanner's been tossed,
No more extra spans to get lost.
Blocks now forward with a jaunty air,
Ledger ticks with nary a care.
Cheers to clean and brilliant code—
A ripper update on the motherload!
☕🐧


📜 Recent review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 851ed74 and 2b4cad4.

📒 Files selected for processing (11)
  • crates/amaru-consensus/src/consensus/header_validation.rs (4 hunks)
  • crates/amaru-consensus/src/consensus/wiring.rs (2 hunks)
  • crates/amaru-ledger/src/rewards.rs (1 hunks)
  • crates/amaru-ledger/src/state.rs (4 hunks)
  • crates/amaru-ledger/src/state/transaction.rs (1 hunks)
  • crates/amaru-ledger/src/store.rs (2 hunks)
  • crates/amaru-stores/src/rocksdb/mod.rs (4 hunks)
  • crates/amaru/src/pipeline.rs (3 hunks)
  • crates/amaru/src/sync/pull.rs (1 hunks)
  • examples/shared/src/lib.rs (1 hunks)
  • simulation/amaru-sim/src/bin/amaru-sim/sync.rs (0 hunks)
💤 Files with no reviewable changes (1)
  • simulation/amaru-sim/src/bin/amaru-sim/sync.rs
🚧 Files skipped from review as they are similar to previous changes (8)
  • examples/shared/src/lib.rs
  • crates/amaru-consensus/src/consensus/wiring.rs
  • crates/amaru-ledger/src/rewards.rs
  • crates/amaru-consensus/src/consensus/header_validation.rs
  • crates/amaru-ledger/src/state.rs
  • crates/amaru-ledger/src/state/transaction.rs
  • crates/amaru/src/sync/pull.rs
  • crates/amaru/src/pipeline.rs

Note

🎁 Summarized by CodeRabbit Free

Your organization has reached its limit of developer seats under the Pro Plan. For new users, CodeRabbit will generate a high-level summary and a walkthrough for each pull request. For a comprehensive line-by-line review, please add seats to your subscription by visiting https://app.coderabbit.ai/login.If you believe this is a mistake and have available seats, please assign one to the pull request author through the subscription management page using the link above.

🪧 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

@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 (1)
crates/amaru-stores/src/rocksdb/mod.rs (1)

167-177: Well-structured pot adjustment method

The adjust_pots method provides a clean interface for adjusting pots based on the rewards summary. The instrumentation includes helpful fields that will make tracing much more informative.

I notice that this method currently only resets fees to zero. You might want to use the rewards_summary parameter more extensively in the future.

Consider updating this method to actually use the rewards_summary parameter for adjusting other aspects of the pots, as the name suggests:

fn adjust_pots(&self, rewards_summary: &RewardsSummary) -> Result<(), StoreError> {
    self.with_pots(|mut row| {
+       let pots = row.borrow_mut();
+       pots.treasury += rewards_summary.delta_treasury();
+       pots.reserves += rewards_summary.delta_reserves();
-       row.borrow_mut().fees = 0;
+       pots.fees = 0;
    })
}
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between c32e7da and 856df98.

📒 Files selected for processing (10)
  • crates/amaru-consensus/src/consensus/header_validation.rs (4 hunks)
  • crates/amaru-consensus/src/consensus/wiring.rs (2 hunks)
  • crates/amaru-ledger/src/rewards.rs (1 hunks)
  • crates/amaru-ledger/src/state.rs (4 hunks)
  • crates/amaru-ledger/src/state/transaction.rs (1 hunks)
  • crates/amaru-ledger/src/store.rs (2 hunks)
  • crates/amaru-stores/src/rocksdb/mod.rs (4 hunks)
  • crates/amaru/src/pipeline.rs (3 hunks)
  • crates/amaru/src/sync/pull.rs (1 hunks)
  • simulation/amaru-sim/src/bin/amaru-sim/sync.rs (0 hunks)
💤 Files with no reviewable changes (1)
  • simulation/amaru-sim/src/bin/amaru-sim/sync.rs
⏰ Context from checks skipped due to timeout of 90000ms (2)
  • GitHub Check: Build on windows-latest with target x86_64-pc-windows-msvc
  • GitHub Check: Snapshots (preprod, 10.1.4)
🔇 Additional comments (29)
crates/amaru-ledger/src/store.rs (2)

25-25: Good addition of tracing import.

Adding the instrument and Level imports from tracing is proper preparation for the new instrumented method.


165-172: Brilliant implementation of tick_pools!

This new method is a cracking addition that follows the established patterns in the Store trait. The instrumentation with tracing will provide excellent visibility into pool ticking operations without drowning logs in details (thanks to skip_all).

The implementation efficiently leverages the existing with_pools method and applies the tick operation for a specified epoch to each pool - keeping things DRY as a bone.

crates/amaru/src/sync/pull.rs (2)

118-127: Smashing improvement to tracing instrumentation!

This change is spot on! Replacing the manual span creation with the declarative #[instrument] attribute makes the code much cleaner. The fields are well chosen to provide relevant context for debugging - including point, tip and peer information.

Using skip_all to avoid logging the full method arguments while explicitly defining the fields you want is a neat trick that keeps the logs tidy.


133-133: Correctly simplified Rollback event creation.

Good job removing the span parameter from the Rollback event creation, which aligns with the changes to the PullEvent enum elsewhere in the codebase. This makes the code more consistent and easier to follow.

crates/amaru-ledger/src/rewards.rs (1)

793-796: Sensible ownership model improvement.

Crikey, this is much better! Changing the method signature from self to &self means the RewardsSummary instance isn't consumed when calling unclaimed_rewards(). This allows for multiple calls to the method or further use of the instance afterwards.

The corresponding change from into_iter() to iter() properly reflects this by borrowing rather than consuming the accounts collection. This is a textbook example of good Rust ownership practices.

crates/amaru-consensus/src/consensus/wiring.rs (4)

2-3: Good imports cleanup and constants addition.

The imports have been properly organized, and adding the EVENT_TARGET constant is a bonzer improvement for consistency throughout the module.

Also applies to: 5-5, 9-10


14-14: Proper simplification of the Rollback event variant.

Removing the Span parameter from the Rollback variant is a sensible change. No need to pass spans around like hot potatoes when they're not needed! This streamlines the event payload and aligns with your other tracing improvements.


39-52: Excellent tracing enhancement for RollForward handling.

This is a proper job! Adding the trace span with detailed fields provides much better observability for roll-forward events. The commentary explains clearly what's happening with the span, and setting the parent span ensures the tracing context is maintained.

Including the slot and hash in the span fields is brilliant for troubleshooting, as these are key identifiers when tracking block progress through the system.


53-56: Correctly updated Rollback handler call.

You've properly updated the handle_roll_back call to match the new method signature without the span parameter. This maintains consistency with the changes to the PullEvent enum and overall tracing approach.

crates/amaru-ledger/src/state.rs (3)

182-271: Nice extraction of volatile state handling logic!

This is a brilliant refactoring that moves the volatile state handling logic into its own method. It makes the forward method much cleaner while properly encapsulating this complex logic. Good use of span instrumentation as well.

A proper job of modularization, this is! The code is now as tidy as a well-organized pub after closing time.


277-278: Good simplification of method signature

Removing the span parameter streamlines the API and is consistent with the pattern of using Span::current() elsewhere. This is a nice improvement to reduce parameter passing.


295-295: Clean delegation to the new method

Nice replacement of the inline logic with a call to the new handle_volatile_state method. This is a textbook example of the "Extract Method" refactoring pattern.

crates/amaru-ledger/src/state/transaction.rs (3)

36-43: Simplified function signature by removing parent span parameter

Removing the parent parameter from apply function is a good call. No need to pass spans around when you can access the current span through the tracing system. This makes the API cleaner and less prone to misuse.

Like removing the unnecessary middleman at a farmers' market - direct from producer to consumer!


80-90: Streamlined apply_certificate calls

The parent parameter has been removed from the apply_certificate calls, consistent with the function signature change. This kind of consistent refactoring across the codebase is exactly what we want to see.


236-237: Removed parent parameter from trace logging

Consistently removed the parent parameter from all trace logging statements. This is a thorough cleanup that ensures consistency throughout the file. Good attention to detail!

Also applies to: 240-241, 250-251, 255-256, 262-263, 267-268, 278-279, 288-289, 302-303

crates/amaru/src/pipeline.rs (3)

57-69: Great extraction of forward logic into dedicated method!

This new forward method is a cracking piece of work! It encapsulates the block processing logic with proper instrumentation and clean error handling. The use of #[instrument] with detailed fields will make debugging much easier.

The pattern of returning the current span with the result is elegant - you're killing two birds with one stone here.


81-81: Clean delegation to forward method

The roll_forward method now simply delegates to the new forward method, making it much cleaner and focused. This is as tidy as a freshly made bed in a 5-star hotel!


84-84: Added instrumentation to rollback_to method

Nice addition of the #[instrument] attribute to the rollback_to method. This brings it in line with the new forward method and will make tracing consistent across both operations. Good attention to symmetry in the API.

crates/amaru-stores/src/rocksdb/mod.rs (4)

154-165: Good extraction of reward application logic

This new apply_rewards method nicely encapsulates the logic for applying rewards by modifying the treasury and fees. The instrumentation will make it easier to trace this operation during debugging.

Clean, focused, and well-instrumented - just the ticket!


179-187: Simple and focused fee reset method

Nice addition of this focused method. It's small but important to have these operations as separate, well-named methods rather than scattered inline.


189-203: Good encapsulation of block count reset logic

This reset_blocks_count method provides a nice abstraction for resetting the block count. The comment about potentially needing a more efficient way to drop a "table" is valuable and flags a possible future optimization.

Good job including this note - it's like leaving a tip for the next developer who has to work with this code. Very considerate!


415-437: Clean use of extracted methods in next_snapshot

The refactoring of next_snapshot to use the newly created methods makes the code much more readable and maintainable. This is a textbook example of good refactoring - breaking down a complex method into well-named, single-responsibility methods.

This change has made the method as readable as a well-written novel - you know exactly what's happening at each step!

crates/amaru-consensus/src/consensus/header_validation.rs (7)

31-31: Nice addition to the tracing imports

Adding Span to the imports is a good move here - it'll come in handy with the Span::current() usage later on. Clean and tidy!


120-120: Good use of Span::current() instead of parameter passing

Love this change! Using Span::current() rather than manually passing a span around is much cleaner. It's like letting the postman deliver mail without having to tell him the address for every house - the context is already there.


132-133: Parameter reduction looks spot on

Removing the parent_span parameter from this forward_block call keeps things consistent with your new approach. Less parameters means less faff for everyone!


183-183: Clean parameter removal

Another spot where you've properly removed the parent_span parameter. Top job on being thorough!


189-190: Parameter cleanup continues

Good stuff! The parent_span has been properly removed from the switch_to_fork call as well, maintaining consistency across the codebase.


199-199: Brilliant addition of instrumentation

Adding the #[instrument] attribute to handle_roll_back is a cracking improvement! The skip(self) is a nice touch too - no need to faff about trying to format that self reference.


221-222: Final parameter cleanup looks good

You've done a proper job of removing the parent_span parameter from this last switch_to_fork call. The changes look consistent throughout the file - you've dotted all the i's and crossed all the t's!

@jeluard jeluard force-pushed the jeluard/better-tracing branch 3 times, most recently from f50e0eb to 0d4a19e Compare March 13, 2025 18:04
@jeluard jeluard marked this pull request as draft March 13, 2025 18:45
@jeluard jeluard force-pushed the jeluard/better-tracing branch from 0d4a19e to 851ed74 Compare March 13, 2025 18:53
@jeluard jeluard marked this pull request as ready for review March 13, 2025 19:20
@jeluard jeluard force-pushed the jeluard/better-tracing branch from 851ed74 to 2b4cad4 Compare March 13, 2025 19:53
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