raft-hpc-core
Shared Raft consensus infrastructure for HPC systems. This crate provides reusable components for building distributed applications using the openraft library.
This crate enables multiple applications (like Pact and Lattice) to share common Raft infrastructure while defining their own application-specific state and commands.
Features
- Log Stores: In-memory and file-backed implementations with a polymorphic variant
- gRPC Transport: TLS-enabled peer-to-peer communication using Tonic
- In-Memory Network: Channel-based network for testing without serialization overhead
- State Machine: Generic state machine with persistent snapshot support
- Backup/Restore: Export, verify, and restore Raft state as compressed tar.gz archives
Installation
Add to your Cargo.toml:
[]
= "2026.1"
Or to use the latest development version from git:
[]
= { = "https://github.com/witlox/hpc-core" }
Usage
1. Define Your Type Configuration
Each application provides its own TypeConfig via openraft::declare_raft_types!:
use declare_raft_types;
use ;
use Cursor;
declare_raft_types!;
2. Implement Your Application State
Implement StateMachineState for your state type:
use StateMachineState;
use HashMap;
3. Choose a Log Store
use ;
// In-memory (for testing or ephemeral nodes)
let mem_store = new;
// File-backed (for production)
let file_store = new?;
// Or use the polymorphic variant
let store = File;
4. Set Up the Network
For production with gRPC:
use ;
// Plain gRPC
let network = new;
network.register.await;
// With TLS
let tls = from_paths?;
let network = with_tls;
// Server side: wrap your Raft instance
let server = new;
For testing with in-memory channels:
use MemNetworkFactory;
let network = new;
network.register.await;
5. Create the State Machine
use HpcStateMachine;
use Arc;
use RwLock;
let state = new;
// In-memory snapshots only
let sm = new;
// With persistent snapshots
let sm = with_snapshot_dir?;
6. Backup and Restore
For backup support, implement BackupMetadataSource:
use ;
// Export
let metadata = export_backup.await?;
// Verify
let metadata = ?;
// Restore (stops the cluster first, then restart with restored data)
let metadata = ?;
Architecture
What's Provided (Generic)
| Component | Description |
|---|---|
MemLogStore |
In-memory log storage |
FileLogStore |
File-backed WAL with JSON entries |
LogStoreVariant |
Polymorphic wrapper for runtime selection |
GrpcNetworkFactory |
gRPC transport with optional TLS/mTLS |
MemNetworkFactory |
In-memory network for testing |
RaftTransportServer |
Tonic gRPC server handler |
HpcStateMachine |
State machine with snapshot management |
export_backup / verify_backup / restore_backup |
Backup utilities |
What You Provide (Application-Specific)
| Component | Description |
|---|---|
TypeConfig |
Your openraft type configuration |
| Command enum | Application commands (e.g., Set, Delete) |
| Response enum | Command responses |
| State struct | Application state implementing StateMachineState |
BackupMetadataSource |
Optional metadata for backups |
File Layout
When using persistent storage:
{data_dir}/raft/
├── vote.json # Persisted vote
├── committed.json # Last committed log ID
├── wal/
│ ├── 1.json # Log entry at index 1
│ ├── 2.json # Log entry at index 2
│ └── purged.json # Purge marker
└── snapshots/
├── current # Pointer to latest snapshot
└── snap-0-100.json # Snapshot at index 100