Skip to main content

zlayer_consensus/
lib.rs

1//! # zlayer-consensus
2//!
3//! A shared Raft consensus library built on [openraft](https://docs.rs/openraft) 0.9.
4//! Provides pluggable storage backends and network transports that can be
5//! parameterized with any application's state machine types.
6//!
7//! Used by both **`ZLayer`** (container orchestration) and **Zatabase** (database replication).
8//!
9//! ## Quick Start
10//!
11//! ```ignore
12//! use zlayer_consensus::{ConsensusNodeBuilder, ConsensusConfig};
13//! use zlayer_consensus::storage::mem_store::{MemLogStore, MemStateMachine};
14//! use zlayer_consensus::network::http_client::HttpNetwork;
15//!
16//! // 1. Define your TypeConfig
17//! openraft::declare_raft_types!(
18//!     pub MyTypeConfig:
19//!         D = MyRequest,
20//!         R = MyResponse,
21//! );
22//!
23//! // 2. Create storage
24//! let log_store = MemLogStore::<MyTypeConfig>::new();
25//! let sm = MemStateMachine::<MyTypeConfig, MyState, _>::new(|state, req| {
26//!     // apply request to state, return response
27//! });
28//!
29//! // 3. Create network
30//! let network = HttpNetwork::<MyTypeConfig>::new();
31//!
32//! // 4. Build the node
33//! let node = ConsensusNodeBuilder::new(1, "127.0.0.1:9000".into())
34//!     .with_config(ConsensusConfig::default())
35//!     .build_with(log_store, sm, network)
36//!     .await?;
37//!
38//! // 5. Bootstrap (first node only)
39//! node.bootstrap().await?;
40//!
41//! // 6. Propose writes
42//! let response = node.propose(MyRequest::Set("key".into(), "value".into())).await?;
43//! ```
44//!
45//! ## Feature Flags
46//!
47//! | Flag | Default | Description |
48//! |------|---------|-------------|
49//! | `mem-store` | yes | In-memory BTreeMap-based storage (testing/dev) |
50//! | `redb-store` | no | Crash-safe persistent storage via redb |
51
52pub mod config;
53pub mod error;
54pub mod network;
55pub mod node;
56pub mod storage;
57pub mod types;
58
59// Re-export key items at the crate root for ergonomics.
60pub use config::ConsensusConfig;
61pub use error::{ConsensusError, Result};
62pub use node::{ConsensusNode, ConsensusNodeBuilder};
63pub use types::{BasicNode, NodeId};
64
65// Re-export openraft types that consumers will commonly need.
66pub use openraft;