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::StandaloneServer;
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 --------------------
136/// Storage trait for implementing custom storage backends
137///
138/// Implement this trait to create your own storage engine.
139pub use d_engine_core::{StateMachine, StorageEngine};
140pub use node::LocalClientError;
141pub use node::LocalKvClient;
142pub use node::Node;
143pub use node::NodeBuilder;
144// Re-export storage implementations
145pub use storage::{
146 FileStateMachine,
147 // File-based storage
148 FileStorageEngine,
149};
150// Conditional RocksDB exports
151#[cfg(feature = "rocksdb")]
152pub use storage::{RocksDBStateMachine, RocksDBStorageEngine};
153
154// -------------------- Data Types --------------------
155
156/// Common Raft protocol types
157pub mod common {
158 // Basic types used in Raft consensus protocol
159 pub use d_engine_proto::common::Entry;
160 pub use d_engine_proto::common::EntryPayload;
161 pub use d_engine_proto::common::LogId;
162 pub use d_engine_proto::common::entry_payload;
163}
164
165/// Client protocol types
166pub mod client {
167 // Client write command types for custom business logic
168 pub use d_engine_proto::client::WriteCommand;
169 pub use d_engine_proto::client::write_command;
170}
171
172/// Server storage protocol types
173pub mod server_storage {
174 // Server storage protocol types (snapshots, replication)
175 pub use d_engine_proto::server::storage::SnapshotMetadata;
176}
177
178// ==================== Internal API (Hidden) ====================
179mod membership;
180mod network;
181mod utils;
182
183#[doc(hidden)]
184pub use d_engine_core::Raft;
185
186// ==================== Test Utilities ====================
187
188/// Test utilities for d-engine-server
189///
190/// This module is only available when running tests.
191///
192/// Contains mock implementations and test helpers.
193#[cfg(test)]
194#[doc(hidden)]
195pub(crate) mod test_utils;