raft_rust/lib.rs
1//! 独立的 Raft 共识库。
2//!
3//! # 概述
4//!
5//! 本 crate 提供由 `step()` / `tick()` 驱动的纯 Raft 共识节点。
6//! 存储与应用状态机均可插拔:
7//!
8//! * [`storage::Engine`] — 用于 Raft 日志的有序键值存储。
9//! 内置持久化后端 [`storage::BitCask`]。
10//! * [`raft::State`] — 从已提交日志顺序应用的确定性状态机。
11//! * [`net`] — 可选 TCP + bincode 传输;`raft-node` / `raft-cli` 二进制基于此。
12//!
13//! 核心 `Node` 仍由 `step()` / `tick()` 驱动;出站消息经
14//! `crossbeam::channel::Sender<raft::Envelope>` 发出。进程内部署见 `cluster` 模块,
15//! 多进程部署见 `config/node*.yaml` + `raft-node`。
16//!
17//! # 最小用法
18//!
19//! ```ignore
20//! use raft_rust::raft::{self, Log, Node, Options, State};
21//! use raft_rust::storage::BitCask;
22//! use crossbeam::channel;
23//!
24//! let (tx, rx) = channel::unbounded();
25//! let log = Log::new(Box::new(BitCask::new("data/node".into())?))?;
26//! let state: Box<dyn State> = Box::new(MyState::default());
27//! let node = Node::new(1, peers, log, state, tx, Options::default())?;
28//! // 通过 node.tick()? 与 node.step(envelope)? 驱动
29//! ```
30
31// 默认开启全部 clippy lint
32#![warn(clippy::all)]
33// Message/Envelope 等变体较大,允许 large_enum_variant
34#![allow(clippy::large_enum_variant)]
35// 允许模块与类型同名风格
36#![allow(clippy::module_inception)]
37// 复杂通道类型签名过长时允许
38#![allow(clippy::type_complexity)]
39
40// 进程内多节点编排模块
41pub mod cluster;
42// YAML 节点配置模块
43pub mod config;
44// TCP+bincode 网络传输模块
45pub mod net;
46// 统一错误类型模块
47pub mod error;
48// Raft 协议核心模块
49pub mod raft;
50// 日志持久化引擎模块
51pub mod storage;
52
53// 对外重导出 Error/Result
54pub use error::{Error, Result};
55// 对外重导出 Raft 主 API
56pub use raft::{
57 // 协议类型:信封、日志、消息、节点与请求响应
58 encode_session, Envelope, Entry, Index, Key, Log, Membership, MembershipEntry, Message, Node,
59 // 节点 ID、选项、会话与状态机相关类型
60 NodeID, Options, Request, Response, SessionState, State, Status, Term, TICK_INTERVAL,
61// 当前作用域结束
62};
63// 对外重导出演示用 KV 状态机
64pub use raft::kv;
65// 对外重导出 BitCask 与 Engine
66pub use storage::{BitCask, Engine};