Skip to main content

d_engine_server/
lib.rs

1//! # d-engine-server
2//!
3//! Complete Raft server with gRPC and storage - batteries included
4//!
5//! ## When to use this crate directly
6//!
7//! - ✅ Embedding server in a larger Rust application
8//! - ✅ Need programmatic access to server APIs
9//! - ✅ Building custom tooling around d-engine
10//! - ✅ Already have your own client implementation
11//!
12//! ## When to use `d-engine` instead
13//!
14//! Most users should use [`d-engine`](https://crates.io/crates/d-engine):
15//!
16//! ```toml
17//! [dependencies]
18//! d-engine = { version = "0.2", features = ["server"] }
19//! ```
20//!
21//! It re-exports this crate plus optional client libraries with simpler dependency management.
22//!
23//! ## Quick Start
24//!
25//! **Embedded Mode** (zero-overhead local client):
26//!
27//! ```rust,ignore
28//! use d_engine_server::EmbeddedEngine;
29//! use std::time::Duration;
30//!
31//! #[tokio::main]
32//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
33//!     let engine = EmbeddedEngine::start_with("config.toml").await?;
34//!     engine.wait_ready(Duration::from_secs(5)).await?;
35//!
36//!     let client = engine.client();
37//!     client.put(b"key".to_vec(), b"value".to_vec()).await?;
38//!
39//!     engine.stop().await?;
40//!     Ok(())
41//! }
42//! ```
43//!
44//! **Standalone Mode** (independent server):
45//!
46//! ```rust,ignore
47//! use d_engine_server::StandaloneServer;
48//! use tokio::sync::watch;
49//!
50//! #[tokio::main]
51//! async fn main() -> Result<(), Box<dyn std::error::Error>> {
52//!     std::env::set_var("CONFIG_PATH", "config.toml");
53//!     let (_shutdown_tx, shutdown_rx) = watch::channel(());
54//!     StandaloneServer::run(shutdown_rx).await?;
55//!     Ok(())
56//! }
57//! ```
58//!
59//! ## Features
60//!
61//! This crate provides:
62//! - **gRPC Server** - Production-ready Raft RPC implementation
63//! - **Storage Backends** - File-based and RocksDB storage
64//! - **Cluster Orchestration** - Node lifecycle and membership management
65//! - **Snapshot Coordination** - Automatic log compaction
66//! - **Watch API** - Real-time state change notifications
67//!
68//! ## Custom Storage
69//!
70//! Implement the [`StateMachine`] and [`StorageEngine`] traits:
71//!
72//! ```rust,ignore
73//! use d_engine_server::{StateMachine, StorageEngine};
74//!
75//! struct MyStateMachine;
76//! impl StateMachine for MyStateMachine {
77//!     // Apply committed entries to your application state
78//! }
79//!
80//! struct MyStorageEngine;
81//! impl StorageEngine for MyStorageEngine {
82//!     // Persist Raft logs and metadata
83//! }
84//! ```
85//!
86//! ## Documentation
87//!
88//! For comprehensive guides:
89//! - [Customize Storage Engine](https://docs.rs/d-engine/latest/d_engine/docs/server_guide/customize_storage_engine/index.html)
90//! - [Customize State Machine](https://docs.rs/d-engine/latest/d_engine/docs/server_guide/customize_state_machine/index.html)
91//! - [Watch Feature Guide](https://docs.rs/d-engine/latest/d_engine/docs/server_guide/watch_feature/index.html)
92
93#![warn(missing_docs)]
94
95// ==================== Core Public API ====================
96
97/// Node lifecycle management
98///
99/// Contains [`Node`] and [`NodeBuilder`] for server setup.
100pub mod node;
101
102/// Public API layer for different deployment modes
103///
104/// Contains [`EmbeddedEngine`] and [`StandaloneServer`].
105pub mod api;
106
107// Re-export LeaderInfo from d-engine-core
108pub use d_engine_core::LeaderInfo;
109
110/// Storage layer implementations
111///
112/// Provides file-based and RocksDB storage backends.
113pub mod storage;
114
115// -------------------- Primary Entry Points --------------------
116pub use api::EmbeddedEngine;
117pub use api::StandaloneEngine;
118// -------------------- Error Types --------------------
119/// Unified error type for all d-engine operations
120pub use d_engine_core::Error;
121/// Hard state (Raft persistent state: term, voted_for, log)
122pub use d_engine_core::HardState;
123/// Log storage trait
124pub use d_engine_core::LogStore;
125/// Metadata storage trait
126pub use d_engine_core::MetaStore;
127/// Protocol buffer error type
128pub use d_engine_core::ProstError;
129/// Unified result type (equivalent to Result<T, Error>)
130pub use d_engine_core::Result;
131/// Snapshot operation error type
132pub use d_engine_core::SnapshotError;
133/// Storage-specific error type
134pub use d_engine_core::StorageError;
135// -------------------- Extension Traits --------------------
136pub use api::EmbeddedClient;
137/// Storage trait for implementing custom storage backends
138///
139/// Implement this trait to create your own storage engine.
140pub use d_engine_core::{StateMachine, StorageEngine};
141pub use node::Node;
142pub use node::NodeBuilder;
143// Re-export storage implementations
144pub use storage::{FileStateMachine, FileStorageEngine};
145// Re-export core types needed by applications
146pub use d_engine_core::ApplyResult;
147// Conditional RocksDB exports
148#[cfg(feature = "rocksdb")]
149pub use storage::{RocksDBStateMachine, RocksDBStorageEngine};
150
151// -------------------- Data Types --------------------
152
153/// Common Raft protocol types
154pub mod common {
155    // Basic types used in Raft consensus protocol
156    pub use d_engine_proto::common::Entry;
157    pub use d_engine_proto::common::EntryPayload;
158    pub use d_engine_proto::common::LogId;
159    pub use d_engine_proto::common::entry_payload;
160}
161
162/// Client protocol types
163pub mod client {
164    // Client write command types for custom business logic
165    pub use d_engine_proto::client::WriteCommand;
166    pub use d_engine_proto::client::write_command;
167}
168
169/// Server storage protocol types
170pub mod server_storage {
171    // Server storage protocol types (snapshots, replication)
172    pub use d_engine_proto::server::storage::SnapshotMetadata;
173}
174
175// ==================== Internal API (Hidden) ====================
176mod membership;
177mod network;
178mod utils;
179
180#[doc(hidden)]
181pub use d_engine_core::Raft;
182
183// ==================== Test Utilities ====================
184
185/// Test utilities for d-engine-server
186///
187/// This module is only available when running tests.
188///
189/// Contains mock implementations and test helpers.
190#[cfg(test)]
191#[doc(hidden)]
192pub(crate) mod test_utils;