ruvector_replication/
lib.rs

1//! Data replication and synchronization for ruvector
2//!
3//! This crate provides comprehensive replication capabilities including:
4//! - Multi-node replica management
5//! - Synchronous, asynchronous, and semi-synchronous replication modes
6//! - Conflict resolution with vector clocks and CRDTs
7//! - Change data capture and streaming
8//! - Automatic failover and split-brain prevention
9//!
10//! # Examples
11//!
12//! ```no_run
13//! use ruvector_replication::{ReplicaSet, ReplicaRole, SyncMode, SyncManager, ReplicationLog};
14//! use std::sync::Arc;
15//!
16//! fn example() -> Result<(), Box<dyn std::error::Error>> {
17//!     // Create a replica set
18//!     let mut replica_set = ReplicaSet::new("cluster-1");
19//!
20//!     // Add replicas
21//!     replica_set.add_replica("replica-1", "192.168.1.10:9001", ReplicaRole::Primary)?;
22//!     replica_set.add_replica("replica-2", "192.168.1.11:9001", ReplicaRole::Secondary)?;
23//!
24//!     // Create sync manager and configure synchronization
25//!     let log = Arc::new(ReplicationLog::new("replica-1"));
26//!     let manager = SyncManager::new(Arc::new(replica_set), log);
27//!     manager.set_sync_mode(SyncMode::SemiSync { min_replicas: 1 });
28//!     Ok(())
29//! }
30//! ```
31
32pub mod conflict;
33pub mod failover;
34pub mod replica;
35pub mod stream;
36pub mod sync;
37
38pub use conflict::{ConflictResolver, LastWriteWins, MergeFunction, VectorClock};
39pub use failover::{FailoverManager, FailoverPolicy, HealthStatus};
40pub use replica::{Replica, ReplicaRole, ReplicaSet, ReplicaStatus};
41pub use stream::{ChangeEvent, ChangeOperation, ReplicationStream};
42pub use sync::{LogEntry, ReplicationLog, SyncManager, SyncMode};
43
44use thiserror::Error;
45
46/// Result type for replication operations
47pub type Result<T> = std::result::Result<T, ReplicationError>;
48
49/// Errors that can occur during replication operations
50#[derive(Error, Debug)]
51pub enum ReplicationError {
52    #[error("Replica not found: {0}")]
53    ReplicaNotFound(String),
54
55    #[error("No primary replica available")]
56    NoPrimary,
57
58    #[error("Replication timeout: {0}")]
59    Timeout(String),
60
61    #[error("Synchronization failed: {0}")]
62    SyncFailed(String),
63
64    #[error("Conflict resolution failed: {0}")]
65    ConflictResolution(String),
66
67    #[error("Failover failed: {0}")]
68    FailoverFailed(String),
69
70    #[error("Network error: {0}")]
71    Network(String),
72
73    #[error("Quorum not met: needed {needed}, got {available}")]
74    QuorumNotMet { needed: usize, available: usize },
75
76    #[error("Split-brain detected")]
77    SplitBrain,
78
79    #[error("Invalid replica state: {0}")]
80    InvalidState(String),
81
82    #[error("Serialization encode error: {0}")]
83    SerializationEncode(#[from] bincode::error::EncodeError),
84
85    #[error("Serialization decode error: {0}")]
86    SerializationDecode(#[from] bincode::error::DecodeError),
87
88    #[error("IO error: {0}")]
89    Io(#[from] std::io::Error),
90}
91
92#[cfg(test)]
93mod tests {
94    use super::*;
95
96    #[test]
97    fn test_error_display() {
98        let err = ReplicationError::QuorumNotMet {
99            needed: 2,
100            available: 1,
101        };
102        assert_eq!(err.to_string(), "Quorum not met: needed 2, got 1");
103    }
104}