Expand description
Log Storage for raft: A high-performance, reliable local disk-based log storage implementation for the raft consensus protocol.
§Features
- Type-safe API with generic types for log entries, vote information, and user data
- Asynchronous write operations with callback support
- Efficient batch processing and disk I/O
- Core raft operations support:
- Vote persistence for election safety
- Log entry append for replication
- Commit index management
- Log entry reads for state machine application
§Example
See basic usage example for a complete demonstration of core functionality, including:
Basic usage:
use raft_log::{RaftLog, Config, Types};
use raft_log::api::raft_log_writer::RaftLogWriter;
// Define your application-specific types
#[derive(Debug, Clone, PartialEq, Eq, Default)]
struct MyTypes;
impl Types for MyTypes {
type LogId = (u64, u64); // (term, index)
type LogPayload = String; // Log entry data
type Vote = (u64, u64); // (term, voted_for)
type UserData = String; // Custom user data
type Callback = SyncSender<io::Result<()>>;
fn log_index(log_id: &Self::LogId) -> u64 {
log_id.1
}
fn payload_size(payload: &Self::LogPayload) -> u64 {
payload.len() as u64
}
}
// Open a RaftLog instance
let temp_dir = tempfile::tempdir().unwrap();
let config = Arc::new(Config {
dir: temp_dir.path().to_str().unwrap().to_string(),
..Default::default()
});
let mut raft_log = RaftLog::<MyTypes>::open(config).unwrap();
// Save vote information
raft_log.save_vote((1, 2)).unwrap(); // Voted for node-2 in term 1
// Append log entries
let entries = vec![
((1, 1), "first entry".to_string()),
((1, 2), "second entry".to_string()),
];
raft_log.append(entries).unwrap();
// Update commit index
raft_log.commit((1, 2)).unwrap();
// Flush changes to disk with callback
let (tx, rx) = sync_channel(1);
raft_log.flush(tx).unwrap();
rx.recv().unwrap().unwrap();
Re-exports§
Modules§
- api
- Core API interfaces and types for Raft-log operations.
- dump_
writer - errors
- types
Structs§
- ChunkId
- ChunkId represents a unique identifier for a chunk based on its global offset in the log.
- Chunk
Stat - Statistics about a single chunk in the Raft log
- Config
- Configuration for Raft-log.
- Dump
- A dump utility that reads WAL records from disk.
- Dump
Raft Log - A struct that contains a snapshot of RaftLog data for inspection or debugging.
- Dump
Raft LogIter - An iterator over log entries in a DumpData
- RaftLog
- RaftLog is a Write-Ahead-Log implementation for the Raft consensus protocol.
- Stat
- Statistics about a Raft log, including information about closed chunks, open chunk, and payload cache performance metrics.
Enums§
- WALRecord
- WALRecord represents different types of records that can be written to the Write-Ahead Log (WAL). Each variant corresponds to a specific operation in the Raft protocol.