ruvector_raft/
lib.rs

1//! Raft consensus implementation for ruvector distributed metadata
2//!
3//! This crate provides a production-ready Raft consensus implementation
4//! following the Raft paper specification for managing distributed metadata
5//! in the ruvector vector database.
6
7pub mod election;
8pub mod log;
9pub mod node;
10pub mod rpc;
11pub mod state;
12
13pub use node::{RaftNode, RaftNodeConfig};
14pub use rpc::{
15    AppendEntriesRequest, AppendEntriesResponse, InstallSnapshotRequest, InstallSnapshotResponse,
16    RequestVoteRequest, RequestVoteResponse,
17};
18pub use state::{LeaderState, PersistentState, RaftState, VolatileState};
19
20use thiserror::Error;
21
22/// Result type for Raft operations
23pub type RaftResult<T> = Result<T, RaftError>;
24
25/// Errors that can occur during Raft operations
26#[derive(Debug, Error)]
27pub enum RaftError {
28    #[error("Node is not the leader")]
29    NotLeader,
30
31    #[error("No leader available")]
32    NoLeader,
33
34    #[error("Invalid term: {0}")]
35    InvalidTerm(u64),
36
37    #[error("Invalid log index: {0}")]
38    InvalidLogIndex(u64),
39
40    #[error("Serialization error: {0}")]
41    SerializationEncodeError(#[from] bincode::error::EncodeError),
42
43    #[error("Deserialization error: {0}")]
44    SerializationDecodeError(#[from] bincode::error::DecodeError),
45
46    #[error("IO error: {0}")]
47    IoError(#[from] std::io::Error),
48
49    #[error("Election timeout")]
50    ElectionTimeout,
51
52    #[error("Log inconsistency detected")]
53    LogInconsistency,
54
55    #[error("Snapshot installation failed: {0}")]
56    SnapshotFailed(String),
57
58    #[error("Configuration error: {0}")]
59    ConfigError(String),
60
61    #[error("Internal error: {0}")]
62    Internal(String),
63}
64
65/// Node identifier type
66pub type NodeId = String;
67
68/// Term number in Raft consensus
69pub type Term = u64;
70
71/// Log index in Raft log
72pub type LogIndex = u64;