Expand description
§Rabia Core - State Machine Replication Protocol Implementation
Core components implementing the Rabia consensus protocol for State Machine Replication (SMR).
This crate provides the fundamental building blocks for the Rabia SMR protocol implementation, enabling fault-tolerant distributed state machines:
§SMR Protocol Components
- StateMachine Trait: Interface for implementing deterministic state machines
- Operation Types: Core types for SMR operations, batching, and results
- Consensus Messages: Protocol messages for coordinating operation ordering
- Node Management: Types like NodeId, BatchId, PhaseId for cluster coordination
- Error Handling: Comprehensive error types and recovery mechanisms
- Serialization: High-performance binary serialization for SMR operations
- Memory Management: Optimized memory pools for reduced allocations
- Validation: Operation and state validation utilities
§Implementing State Machines with the Rabia Protocol
use rabia_core::smr::StateMachine;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
#[derive(Clone, Serialize, Deserialize)]
pub struct CounterCommand {
pub operation: String, // "increment", "decrement", "get"
pub value: i64,
}
#[derive(Clone, Serialize, Deserialize)]
pub struct CounterResponse {
pub value: i64,
pub success: bool,
}
#[derive(Clone, Serialize, Deserialize)]
pub struct CounterState {
pub value: i64,
}
#[derive(Clone)]
pub struct CounterSMR {
state: CounterState,
}
#[async_trait]
impl StateMachine for CounterSMR {
type Command = CounterCommand;
type Response = CounterResponse;
type State = CounterState;
async fn apply_command(&mut self, command: Self::Command) -> Self::Response {
// Your deterministic operation logic here
// This will execute identically on all replicas
match command.operation.as_str() {
"increment" => {
self.state.value += command.value;
CounterResponse { value: self.state.value, success: true }
}
"decrement" => {
self.state.value -= command.value;
CounterResponse { value: self.state.value, success: true }
}
"get" => {
CounterResponse { value: self.state.value, success: true }
}
_ => CounterResponse { value: self.state.value, success: false }
}
}
fn get_state(&self) -> Self::State {
self.state.clone()
}
fn set_state(&mut self, state: Self::State) {
self.state = state;
}
fn serialize_state(&self) -> Vec<u8> {
bincode::serialize(&self.state).unwrap_or_default()
}
fn deserialize_state(&mut self, data: &[u8]) -> Result<(), Box<dyn std::error::Error>> {
self.state = bincode::deserialize(data)?;
Ok(())
}
}
This protocol implementation handles consensus, networking, and persistence, letting you focus on your application’s business logic.
Re-exports§
pub use error::*;
pub use types::*;
pub use validation::*;
Modules§
- batching
- error
- Error Types
- memory_
pool - messages
- network
- persistence
- serialization
- smr
- State Machine Replication (SMR) Interface
- state_
machine - types
- Core Types
- validation