Skip to main content

raft_log/
lib.rs

1//! Log Storage for raft:
2//! A high-performance, reliable local disk-based log storage implementation for
3//! the raft consensus protocol.
4//!
5//! ## Features
6//!
7//! - Type-safe API with generic types for log entries, vote information, and
8//!   user data
9//! - Asynchronous write operations with callback support
10//! - Efficient batch processing and disk I/O
11//! - Core raft operations support:
12//!   - Vote persistence for election safety
13//!   - Log entry append for replication
14//!   - Commit index management
15//!   - Log entry reads for state machine application
16//!
17//! ## Example
18//!
19//! See [basic usage example](https://github.com/datafuselabs/openraft/blob/main/examples/basic_usage.rs)
20//! for a complete demonstration of core functionality, including:
21//!
22//! Basic usage:
23//!
24//! ```rust
25//! # use std::io;
26//! # use std::sync::Arc;
27//! # use std::sync::mpsc::sync_channel;
28//! # use std::sync::mpsc::SyncSender;
29//! use raft_log::{RaftLog, Config, Types};
30//!
31//! use raft_log::api::raft_log_writer::RaftLogWriter;
32//!
33//! // Define your application-specific types
34//! #[derive(Debug, Clone, PartialEq, Eq, Default)]
35//! struct MyTypes;
36//! impl Types for MyTypes {
37//!     type LogId = (u64, u64);        // (term, index)
38//!     type LogPayload = String;        // Log entry data
39//!     type Vote = (u64, u64);         // (term, voted_for)
40//!     type UserData = String;         // Custom user data
41//!     type Callback = SyncSender<io::Result<()>>;
42//!
43//!     fn log_index(log_id: &Self::LogId) -> u64 {
44//!         log_id.1
45//!     }
46//!
47//!     fn payload_size(payload: &Self::LogPayload) -> u64 {
48//!         payload.len() as u64
49//!     }
50//! }
51//!
52//! // Open a RaftLog instance
53//! let temp_dir = tempfile::tempdir().unwrap();
54//! let config = Arc::new(Config::new(temp_dir.path().to_str().unwrap()));
55//! let mut raft_log = RaftLog::<MyTypes>::open(config).unwrap();
56//!
57//! // Save vote information
58//! raft_log.save_vote((1, 2)).unwrap();  // Voted for node-2 in term 1
59//!
60//! // Append log entries
61//! let entries = vec![
62//!     ((1, 1), "first entry".to_string()),
63//!     ((1, 2), "second entry".to_string()),
64//! ];
65//! raft_log.append(entries).unwrap();
66//!
67//! // Update commit index
68//! raft_log.commit((1, 2)).unwrap();
69//!
70//! // Flush changes to disk with callback
71//! let (tx, rx) = sync_channel(1);
72//! raft_log.flush(true, Some(tx)).unwrap();
73//! rx.recv().unwrap().unwrap();
74//! ```
75
76mod config;
77
78pub(crate) mod num;
79pub(crate) mod raft_log;
80pub(crate) mod testing;
81
82pub mod types;
83pub use codeq;
84
85pub mod api;
86pub mod dump_writer;
87pub mod errors;
88
89pub use api::types::RaftWalTypes;
90pub use api::types::Types;
91pub use chunked_wal::Callback;
92pub use chunked_wal::ChunkId;
93pub use chunked_wal::ChunkStat;
94pub use chunked_wal::FlushLatencyPercentiles;
95pub use chunked_wal::FlushMetrics;
96pub use chunked_wal::StateMachine;
97pub use chunked_wal::WALRecord;
98pub use chunked_wal::WalTypes;
99pub use config::Config;
100pub use raft_log::stat::Stat;
101
102pub use self::raft_log::dump::Dump;
103pub use self::raft_log::dump_api::DumpApi;
104pub use self::raft_log::dump_raft_log::DumpRaftLog;
105pub use self::raft_log::dump_raft_log::DumpRaftLogIter;
106pub use self::raft_log::raft_log::RaftLog;
107pub use self::raft_log::raft_log_action::RaftLogAction;
108pub use self::raft_log::raft_log_record::RaftLogRecord;
109pub use crate::types::Segment;
110
111#[cfg(test)]
112mod tests;