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 {
55//! dir: temp_dir.path().to_str().unwrap().to_string(),
56//! ..Default::default()
57//! });
58//! let mut raft_log = RaftLog::<MyTypes>::open(config).unwrap();
59//!
60//! // Save vote information
61//! raft_log.save_vote((1, 2)).unwrap(); // Voted for node-2 in term 1
62//!
63//! // Append log entries
64//! let entries = vec![
65//! ((1, 1), "first entry".to_string()),
66//! ((1, 2), "second entry".to_string()),
67//! ];
68//! raft_log.append(entries).unwrap();
69//!
70//! // Update commit index
71//! raft_log.commit((1, 2)).unwrap();
72//!
73//! // Flush changes to disk with callback
74//! let (tx, rx) = sync_channel(1);
75//! raft_log.flush(tx).unwrap();
76//! rx.recv().unwrap().unwrap();
77//! ```
78
79mod chunk;
80mod config;
81
82pub(crate) mod disp;
83pub(crate) mod file_lock;
84pub(crate) mod num;
85pub(crate) mod offset_reader;
86pub(crate) mod raft_log;
87pub(crate) mod testing;
88
89pub mod types;
90pub use codeq;
91
92pub mod api;
93pub mod dump_writer;
94pub mod errors;
95
96pub use api::types::Types;
97pub use chunk::chunk_id::ChunkId;
98pub use config::Config;
99pub use raft_log::stat::ChunkStat;
100pub use raft_log::stat::Stat;
101pub use raft_log::wal::callback::Callback;
102
103pub use self::raft_log::dump::Dump;
104pub use self::raft_log::dump_api::DumpApi;
105pub use self::raft_log::dump_raft_log::DumpRaftLog;
106pub use self::raft_log::dump_raft_log::DumpRaftLogIter;
107pub use self::raft_log::raft_log::RaftLog;
108pub use self::raft_log::wal::wal_record::WALRecord;
109pub use crate::types::Segment;
110
111#[cfg(test)]
112mod tests;