mod.rs
Overview
This file implements a service responsible for tracking updates to a threads table in the context of block finalization within a system managing concurrent or parallel thread execution. Specifically, it monitors the creation and destruction of threads as blocks become finalized and notifies subscribers of such events. The service assumes that blocks are finalized in the correct order and provides mechanisms to initialize thread tracking, handle block finalization, and notify subscribers about thread lifecycle events.
The main entities in this file are:
CheckpointBlockData: Holds metadata about a block relevant to thread tracking.CommandError: Enumerates possible errors that can occur during command execution.ThreadsTrackingService: The core service that tracks thread lifecycle events and notifies subscribers.Subscribertrait (re-exported fromsubscribersmodule): Defines the interface for components interested in thread lifecycle changes.
Entities and Components
CheckpointBlockData
A data structure containing information about a block and its relation to threads.
Fields
parent_block_identifier: BlockIdentifier
Identifier of the parent block.block_identifier: BlockIdentifier
Identifier of the current block.thread_identifier: ThreadIdentifier
Identifier of the thread related to this block.threads_table: ThreadsTable
The state of the threads table at this checkpoint.
CommandError
An enumeration of error types that can occur during command processing in the ThreadsTrackingService.
Variants
BlockDataMissing: Indicates missing data for a given block ID, which may be critical depending on the command.UnexpectedDependency: Indicates a block referenced a thread dependency that was not expected and might point to a bug.ParentBlockMissing: Indicates a missing parent block; could be critical.CriticalInconsistency: Indicates the service is in an inconsistent state.
ThreadsTrackingService
This struct implements the thread tracking logic.
Methods
start() -> Self
Creates and returns a new instance ofThreadsTrackingService.
Usage:let service = ThreadsTrackingService::start();init_thread<T>(&mut self, block_identifier: BlockIdentifier, threads_set: HashSet<ThreadIdentifier>, subscribers: &mut T)
Initializes thread tracking for a set of threads associated with a specific block. For each thread inthreads_set, it notifies subscribers that the thread has started (with no associated threads table snapshot).block_identifier: The identifier of the block from which threads originate.threads_set: A set of thread identifiers to initialize.subscribers: A mutable reference to an object implementing theSubscribertrait that will receive notifications.
Example:
service.init_thread(block_id, threads_set, &mut subscriber);handle_block_invalidated(&mut self, _block_identifier: BlockIdentifier) -> anyhow::Result<(), CommandError>
Handles the invalidation of a block. Currently, this function is a stub that returnsOk(())and does not perform any action._block_identifier: The identifier of the invalidated block.
Usage:
service.handle_block_invalidated(block_id)?;handle_block_finalized<T>(&mut self, block_identifier: BlockIdentifier, thread_identifier: ThreadIdentifier, threads_table_after_this_block: ThreadsTable, subscribers: &mut T) -> anyhow::Result<(), CommandError>
Processes the finalization of a block related to a specific thread. It determines which threads have started or stopped and notifies subscribers accordingly.block_identifier: The finalized block’s identifier.thread_identifier: The thread identifier associated with the finalized block.threads_table_after_this_block: Current state of the threads table after processing this block.subscribers: A mutable reference to an object implementing theSubscribertrait to receive notifications.
Behavior:Identifies new threads that appeared in the
threads_table_after_this_blockand notifies subscribers of their start.Detects if the current thread has been removed from the threads table, indicating it has stopped, and notifies subscribers accordingly.
Example:
service.handle_block_finalized(block_id, thread_id, threads_table, &mut subscriber)?;
Subscriber Trait (from subscribers module)
Defines the interface for receiving notifications about thread start and stop events.
handle_start_thread(&mut self, parent_split_block: &BlockIdentifier, thread_identifier: &ThreadIdentifier, threads_table: Option<ThreadsTable>)
Called when a new thread is detected.handle_stop_thread(&mut self, last_thread_block: &BlockIdentifier, thread_identifier: &ThreadIdentifier)
Called when a thread is detected to have stopped.
See the subscribers module for detailed implementation and usage of Subscriber.
Important Implementation Details and Algorithms
Thread Lifecycle Tracking:
The service tracks thread lifecycles by comparing the threads table state before and after block finalization. New threads are detected based on whether they "spawn" at the current block (using theis_spawning_blockmethod onThreadIdentifier), and thread termination is detected when a thread identifier no longer appears in the updated threads table.Notification Strategy:
Subscribers are notified immediately when threads start or stop, allowing other components to react to these lifecycle events (e.g., resource allocation or cleanup).Assumptions:
The service assumes blocks are finalized in the correct order. This assumption is crucial for maintaining consistency in thread lifecycle tracking.Error Handling:
TheCommandErrorenum provides detailed error cases, but the current public methods mostly returnOk(())or handle errors internally, suggesting that error handling is prepared for future expansion or integration with other system components.Testing:
The file includes tests using themockallcrate to mock theSubscribertrait for verifying interaction behavior, ensuring that subscribers receive the correct notifications on thread start and stop events.
Interaction with Other Parts of the System
BlockIdentifier,ThreadIdentifier, andThreadsTableTypes:
These are imported from thecrate::typesmodule and represent core concepts used throughout the system to identify blocks, threads, and track thread states.SubscriberTrait:
The service notifies subscribers about thread lifecycle events. Subscribers implement theSubscribertrait (defined in thesubscribersmodule), allowing them to react appropriately when threads start or stop.External Logging:
Uses thetracingcrate for diagnostic logging, helpful during debugging and monitoring.Error Handling and Result Types:
Usesanyhow::Resultfor error propagation, allowing integration with broader error handling strategies in the application.
Visual Diagram
classDiagram
class ThreadsTrackingService {
+start()
+init_thread()
+handle_block_invalidated()
+handle_block_finalized()
}
class CheckpointBlockData {
+parent_block_identifier
+block_identifier
+thread_identifier
+threads_table
}
class CommandError {
<<enum>>
+BlockDataMissing
+UnexpectedDependency
+ParentBlockMissing
+CriticalInconsistency
}
ThreadsTrackingService ..> CommandError : uses
ThreadsTrackingService ..> CheckpointBlockData : uses
ThreadsTrackingService ..> Subscriber : notifies
Subscriber <|.. subscribers::Subscriber
This diagram shows the main structs and their relationships within this file: the ThreadsTrackingService manages thread lifecycle tracking and notifies the Subscriber trait implementations; it uses CheckpointBlockData for block metadata and returns or handles CommandError variants when applicable.