d_engine_core/commit_handler/
mod.rs

1//! # Commit Handler Module
2//!
3//! The `commit_handler` module is responsible for processing newly committed
4//! log entries in a Raft cluster. It acts as a bridge between the Raft
5//! consensus protocol and the application-specific business logic. When a new
6//! log entry is committed by the Raft leader and replicated to a majority of
7//! nodes, the commit handler ensures that the entry is applied to the state
8//! machine in a consistent and thread-safe manner.
9//!
10//! ## Key Responsibilities
11//! 1. **Receiving Commits**: Listens for newly committed log entries from the Raft log.
12//! 2. **Batching**: Groups multiple log entries into batches for efficient processing.
13//! 3. **Applying to State Machine**: Passes the committed entries to the state machine for
14//!    application-specific processing.
15//! 4. **Snapshotting**: Periodically triggers snapshots to reduce the size of the Raft log and
16//!    improve recovery performance.
17//!
18//! ## Raft Protocol Integration
19//! - The commit handler adheres to the Raft protocol by ensuring that committed entries are applied
20//!   in the exact order they were committed.
21//! - It guarantees linearizability by applying entries only after they are committed and replicated
22//!   to a majority of nodes.
23//!
24//! ## Best Practices
25//! - **Batching**: Use batching to reduce the overhead of applying individual log entries.
26//! - **Concurrency**: Apply log entries concurrently (if safe) to improve throughput.
27//! - **Snapshotting**: Regularly take snapshots to prevent the Raft log from growing indefinitely.
28//! - **Error Handling**: Handle errors gracefully and ensure the state machine remains consistent
29//!   even in the face of failures.
30//!
31//! ## Customization for Business Logic
32//! - The actual implementation of how to use the committed log entries is left to the developer
33//!   (business layer). The commit handler provides a framework for processing entries, but the
34//!   business logic must define how to interpret and apply them.
35//! - Developers should implement the `StateMachine` trait to define how log entries are applied to
36//!   the application state.
37//!
38//! ## Example Workflow
39//! 1. The Raft leader commits a new log entry and replicates it to a majority of nodes.
40//! 2. The commit handler receives the committed entry and adds it to a batch.
41//! 3. Once the batch reaches a certain size or a timeout occurs, the handler applies the batch to
42//!    the state machine.
43//! 4. The state machine processes the entries and updates the application state.
44//! 5. Periodically, the commit handler triggers a snapshot(TODO: in future release) to compact the
45//!    Raft log.
46//!
47//! ## Notes
48//! - The commit handler is designed to be thread-safe and can be used in a multi-threaded or
49//!   asynchronous environment.
50//! - Developers must ensure that the state machine implementation is idempotent to handle potential
51//!   retries or re-applications of log entries.
52mod default_commit_handler;
53
54pub use default_commit_handler::*;
55#[cfg(test)]
56mod default_commit_handler_test;
57
58#[cfg(any(test, feature = "test-utils"))]
59use mockall::automock;
60use tonic::async_trait;
61
62use crate::Result;
63
64#[cfg_attr(any(test, feature = "test-utils"), automock)]
65#[async_trait]
66pub trait CommitHandler: Send + Sync + 'static {
67    async fn run(&mut self) -> Result<()>;
68}