raft_log/api/types.rs
1//! Core type definitions for the Raft-log implementation.
2//!
3//! This module defines the `Types` trait which serves as a type system for the
4//! entire Raft-log implementation. It allows users to customize core data types
5//! such as log id, log payload, vote, callback, and user data.
6
7use std::fmt::Debug;
8use std::marker::PhantomData;
9
10use chunked_wal::Callback;
11use codeq::Codec;
12
13use crate::RaftLogAction;
14use crate::WalTypes;
15use crate::raft_log::state_machine::raft_log_state::RaftLogState;
16
17/// The `Types` trait defines the core type parameters used throughout the
18/// Raft-log implementation.
19///
20/// This trait provides an abstraction layer for the log implementation by
21/// defining the core types used throughout the system. Implementations can
22/// customize the concrete types for: log id, log payload, vote, callback, and
23/// user data.
24pub trait Types
25where Self: Debug + Default + PartialEq + Eq + Clone + 'static
26{
27 /// Unique identifier for log entries. Usually it contains the term and log
28 /// index.
29 type LogId: Debug + Clone + Ord + Eq + Codec + Send + Sync + 'static;
30
31 /// The actual data/command stored in log entries.
32 type LogPayload: Debug + Clone + Codec + Send + Sync + 'static;
33
34 /// Representation of vote in leader election.
35 ///
36 /// A vote typically contains:
37 /// - The election term number
38 /// - The candidate node ID
39 /// - A commitment flag indicating whether a quorum of nodes has granted
40 /// this vote
41 ///
42 /// The commitment flag is set to true when a majority of nodes in the
43 /// cluster have voted for this candidate in this term.
44 ///
45 /// In some raft implementation the vote is called `hard state`
46 type Vote: Debug + Clone + PartialOrd + Eq + Codec + Send + Sync + 'static;
47
48 /// Callback handlers for notification of an IO operation.
49 type Callback: Callback;
50
51 /// Custom data that can be attached to Raft-log.
52 ///
53 /// This data is not used by the Raft-log implementation, but it can be used
54 /// by the user. For example, an application could attach a
55 /// configuration or the node info in this field.
56 type UserData: Debug + Clone + Eq + Codec + Send + Sync + 'static;
57
58 /// Get the log index from the log id.
59 fn log_index(log_id: &Self::LogId) -> u64;
60
61 /// Get the payload size from the log payload.
62 ///
63 /// The returned size does not have to be accurate. It is only used to
64 /// estimate the space occupied in the cache.
65 fn payload_size(payload: &Self::LogPayload) -> u64;
66
67 /// Get the next log index from the log id.
68 fn next_log_index(log_id: Option<&Self::LogId>) -> u64 {
69 match log_id {
70 Some(log_id) => Self::log_index(log_id) + 1,
71 None => 0,
72 }
73 }
74}
75
76#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
77pub struct RaftWalTypes<T>(PhantomData<T>);
78
79impl<T> WalTypes for RaftWalTypes<T>
80where T: Types
81{
82 type Action = RaftLogAction<T>;
83 type Checkpoint = RaftLogState<T>;
84 type Callback = T::Callback;
85}