Skip to main content

raft_log/api/
state_machine.rs

1//! State machine abstraction for applying records in a deterministic order.
2
3use std::fmt::Debug;
4
5use crate::ChunkId;
6use crate::types::Segment;
7
8/// A trait representing a state machine of [`WAL`] that can apply records to
9/// modify its state.
10///
11/// The Raft-log follows a Write-Ahead Log (WAL) + State Machine pattern. This
12/// trait defines the state machine component that processes records persisted
13/// in the WAL to build and maintain application state.
14///
15/// # Type Parameters
16/// * `R` - The type of records that can be applied to the state machine
17///
18/// [`WAL`]: crate::api::wal::WAL
19pub trait StateMachine<R> {
20    /// The type of error that can occur during record application
21    type Error: std::error::Error + Debug + 'static;
22
23    /// Applies a record that is already persisted in the WAL to the state
24    /// machine, potentially modifying its state.
25    ///
26    /// # Arguments
27    /// * `record` - The record to apply.
28    /// * `chunk_id` - The identifier of the chunk containing this record.
29    /// * `global_segment` - The global offset and size of the record in the log
30    ///   file.
31    fn apply(
32        &mut self,
33        record: &R,
34        chunk_id: ChunkId,
35        global_segment: Segment,
36    ) -> Result<(), Self::Error>;
37}