Skip to main content

Crate zlayer_consensus

Crate zlayer_consensus 

Source
Expand description

§zlayer-consensus

A shared Raft consensus library built on openraft 0.9. Provides pluggable storage backends and network transports that can be parameterized with any application’s state machine types.

Used by both ZLayer (container orchestration) and Zatabase (database replication).

§Quick Start

use zlayer_consensus::{ConsensusNodeBuilder, ConsensusConfig};
use zlayer_consensus::storage::mem_store::{MemLogStore, MemStateMachine};
use zlayer_consensus::network::http_client::HttpNetwork;

// 1. Define your TypeConfig
openraft::declare_raft_types!(
    pub MyTypeConfig:
        D = MyRequest,
        R = MyResponse,
);

// 2. Create storage
let log_store = MemLogStore::<MyTypeConfig>::new();
let sm = MemStateMachine::<MyTypeConfig, MyState, _>::new(|state, req| {
    // apply request to state, return response
});

// 3. Create network
let network = HttpNetwork::<MyTypeConfig>::new();

// 4. Build the node
let node = ConsensusNodeBuilder::new(1, "127.0.0.1:9000".into())
    .with_config(ConsensusConfig::default())
    .build_with(log_store, sm, network)
    .await?;

// 5. Bootstrap (first node only)
node.bootstrap().await?;

// 6. Propose writes
let response = node.propose(MyRequest::Set("key".into(), "value".into())).await?;

§Feature Flags

FlagDefaultDescription
mem-storeyesIn-memory BTreeMap-based storage (testing/dev)
redb-storenoCrash-safe persistent storage via redb

Re-exports§

pub use config::ConsensusConfig;
pub use error::ConsensusError;
pub use error::Result;
pub use node::ConsensusNode;
pub use node::ConsensusNodeBuilder;
pub use types::NodeId;
pub use openraft;

Modules§

config
Consensus configuration with production-ready defaults.
error
Error types for the consensus layer.
network
Network implementations for Raft RPC communication.
node
High-level ConsensusNode wrapper around openraft::Raft.
storage
Storage implementations for the Raft log and state machine.
types
Core type aliases and re-exports for the consensus layer.

Structs§

BasicNode
An implementation of trait Node that contains minimal node information.