mod.rs

Overview

This file implements a Load Balancing Service for managing and optimizing thread workloads in a multi-threaded environment. It monitors the load on each thread, decides whether a thread should continue as is, be split into smaller units, or be collapsed (merged) with others to maintain balanced resource utilization. The service uses statistical load metrics aggregated over a configurable window and employs heuristics based on load thresholds and load disproportions.

Core functionalities include:

This file is part of the system managing thread distribution and load balancing, interfacing with the thread tracking service, metrics collection, thread tables, and block production components.


Constants

MAX_LOAD_DISPROPORTION: Load = 2

Defines the maximum allowed load disproportion between threads before triggering a split or collapse action. It must not be less than 2 to avoid infinite splitting/collapsing cycles. This constant constrains the heuristic balancing logic.


Data Structures

Proposal

pub struct Proposal {
    pub proposed_threads_table: ThreadsTable,
}

Represents a proposal for changing the threads layout, containing the proposed new threads table after a split or collapse operation.

ThreadAction

pub enum ThreadAction {
    ContinueAsIs,
    Split(Proposal),
    Collapse(Proposal),
}

Enumerates possible actions the load balancer can decide for a thread:

CheckError

pub enum CheckError {
    StatsAreNotReady,
    ThreadIsNotInTheTable,
}

Represents possible error states when checking thread load for balancing:


Main Struct: LoadBalancingService

pub struct LoadBalancingService {
    thread_load_map: HashMap<ThreadIdentifier, AggregatedLoad>,
    metrics: Option<BlockProductionMetrics>,
    window_size: usize,
    load_threshold: Load,
}

Manages load balancing logic by maintaining load statistics for each thread, deciding when to split or merge threads based on load metrics.


Implementation Details and Methods

start

pub fn start(
    metrics: Option<BlockProductionMetrics>,
    window_size: usize,
    load_threshold: usize,
) -> Self

Initializes a new LoadBalancingService instance.

Usage Example:

let service = LoadBalancingService::start(Some(metrics), 50, 100);

check

pub fn check(
    &mut self,
    block_identifier: &BlockIdentifier,
    thread_identifier: &ThreadIdentifier,
    threads_table: &ThreadsTable,
    max_table_size: usize,
) -> anyhow::Result<ThreadAction, CheckError>

Evaluates the current load state of a thread and decides what action to take: continue, split, or collapse.


handle_block_finalized

pub fn handle_block_finalized<TOptimisticState>(
    &mut self,
    block: &AckiNackiBlock,
    block_state: Arc<TOptimisticState>,
) where
    TOptimisticState: OptimisticState,

Updates thread load statistics upon block finalization.


read_load

fn read_load(&self, thread_identifier: &ThreadIdentifier) -> anyhow::Result<Load, CheckError>

Retrieves the current load value of a thread from the aggregated load map.


Trait Implementation

Subscriber for LoadBalancingService

Implements subscription to thread lifecycle events.

fn handle_start_thread(
    &mut self,
    _parent_split_block: &BlockIdentifier,
    thread_identifier: &ThreadIdentifier,
    threads_table: Option<ThreadsTable>,
)

Handles the event of a new thread starting.

fn handle_stop_thread(
    &mut self,
    _last_thread_block: &BlockIdentifier,
    thread_identifier: &ThreadIdentifier,
)

Handles the event of a thread stopping.


Interaction with Other Components


Important Implementation Details


Visual Diagram

classDiagram
class LoadBalancingService {
-thread_load_map: HashMap
-metrics: Option<BlockProductionMetrics>
-window_size: usize
-load_threshold: Load
+start()
+check()
+handle_block_finalized()
-read_load()
}
class Proposal {
+proposed_threads_table: ThreadsTable
}
class ThreadAction {
<<enum>>
+ContinueAsIs
+Split
+Collapse
}
class CheckError {
<<enum>>
+StatsAreNotReady
+ThreadIsNotInTheTable
}
LoadBalancingService ..> Proposal : uses
LoadBalancingService ..> ThreadAction : returns
LoadBalancingService ..> CheckError : returns
LoadBalancingService ..> AggregatedLoad : manages
LoadBalancingService ..> BlockProductionMetrics : optionally reports
LoadBalancingService ..> ThreadsTable : reads
LoadBalancingService ..> OptimisticState : updates load from block state
LoadBalancingService ..|> Subscriber : implements

This class diagram illustrates the main data structures and relationships within the file, highlighting the primary class LoadBalancingService, its dependencies, and key enums used for control flow.