Skip to main content

amaters_cluster/
lib.rs

1//! Consensus layer for AmateRS (Ukehi - The Sacred Pledge)
2//!
3//! This crate implements Raft consensus with support for encrypted logs
4//! and distributed cluster management.
5//!
6//! ## Architecture
7//!
8//! The consensus layer consists of:
9//!
10//! - **Raft Node**: Core consensus implementation with leader election and log replication
11//! - **Log Management**: Persistent log with in-memory cache and compaction
12//! - **State Management**: Persistent and volatile state tracking
13//! - **RPC Layer**: Request/response messages for inter-node communication
14//!
15//! ## Example
16//!
17//! ```rust,ignore
18//! use amaters_cluster::{RaftNode, RaftConfig, Command};
19//!
20//! // Create a 3-node cluster
21//! let config = RaftConfig::new(1, vec![1, 2, 3]);
22//! let node = RaftNode::new(config)?;
23//!
24//! // Propose a command (as leader)
25//! let cmd = Command::from_str("SET key value");
26//! let index = node.propose(cmd)?;
27//! ```
28//!
29//! ## Consensus Flow
30//!
31//! ```text
32//!  propose(cmd) → [Leader]
33//!                     │ replicate_to_followers()
34//!                     ├──→ [Follower 1] handle_append_entries()
35//!                     └──→ [Follower 2] handle_append_entries()
36//!                               ↓
37//!                     handle_replication_response() (on leader)
38//!                               ↓
39//!                     commit_index advances → StateMachine::apply()
40//! ```
41
42pub mod alert_rules;
43pub mod cluster_command;
44pub mod cluster_topology;
45pub mod config;
46pub mod encryption;
47pub mod error;
48pub mod failover;
49pub mod heartbeat;
50pub mod key_rotation;
51pub mod log;
52pub mod merkle;
53pub mod metrics;
54pub mod migration;
55pub mod node;
56pub mod partitioner;
57pub mod persistence;
58pub mod placement;
59pub mod placement_scheduler;
60pub mod placement_state_machine;
61pub mod rpc;
62pub mod shard;
63pub mod snapshot;
64pub mod state;
65pub mod types;
66pub mod wal;
67// Re-exports for convenience
68pub use cluster_command::ClusterCommand;
69pub use cluster_topology::{
70    ClusterTopology, NodeState as ClusterNodeState, NodeStatus, TopologyCollector,
71};
72pub use encryption::{EncryptedPayload, EntryEncryptor, LogEncryptionKey, LogIntegrityVerifier};
73pub use error::{RaftError, RaftResult};
74pub use failover::{
75    AlertCallback, AlertEvent, AlertManager, FailoverConfig, FailoverController,
76    FailoverCoordinator, FailoverEvent,
77};
78
79pub use alert_rules::{
80    AlertRule, AlertSeverity, AlertSink, CollectingSink, FiredAlert, LogSink, RuleEngine,
81    default_rules, event_dedup_key,
82};
83pub use heartbeat::FailureDetector;
84pub use key_rotation::{KeyManager, KeyVersion, LEGACY_KEY_VERSION};
85pub use log::{ApplyResult, Command, LogEntry, RaftLog, SnapshotData, StateMachine};
86pub use merkle::{MerkleProof, MerkleTree};
87pub use metrics::ClusterMetrics;
88pub use migration::{Migration, MigrationStatus, MigrationTracker, compute_rebalance_plan};
89pub use node::RaftNode;
90pub use partitioner::{
91    PartitionStrategy, Partitioner, QueryPlan, QueryRouter, QueryStats, ResultMerger,
92};
93pub use persistence::{FilePersistence, MemoryPersistence, RaftPersistence};
94pub use placement::{PlacementAction, PlacementCoordinator, PlacementPlan, PlacementPolicy};
95pub use placement_scheduler::{
96    PlacementScheduler, PlacementSchedulerConfig, PlacementSchedulerHandle,
97};
98pub use placement_state_machine::PlacementStateMachine;
99pub use rpc::{
100    AppendEntriesRequest, AppendEntriesResponse, RequestVoteRequest, RequestVoteResponse,
101};
102pub use shard::{
103    KeyRange, ShardId, ShardMerge, ShardMetadata, ShardRegistry, ShardSplit, ShardState,
104    ShardTransfer,
105};
106pub use snapshot::{
107    DiskSnapshotStore, InstallSnapshotRequest, InstallSnapshotResponse, Snapshot, SnapshotConfig,
108    SnapshotManager, SnapshotMetadata, SnapshotPolicy, SnapshotReceiver, SnapshotStore,
109};
110pub use state::{CandidateState, FencingTokenState, LeaderState, PersistentState, VolatileState};
111pub use types::{
112    ClusterConfig, ConfigState, FailureEvent, FencingToken, HeartbeatConfig, LogIndex,
113    MembershipChange, NodeId, NodeState, RaftConfig, Term,
114};
115pub use wal::{CorruptionPolicy, SyncMode, WalDiagnostics, WalReader, WalWriter};
116
117/// Library version
118pub const VERSION: &str = env!("CARGO_PKG_VERSION");
119
120/// Library name
121pub const NAME: &str = env!("CARGO_PKG_NAME");