1use crate::raft::OxirsNodeId;
6use serde::{Deserialize, Serialize};
7use std::collections::HashMap;
8use std::time::{Duration, SystemTime};
9
10#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
12pub struct GeoCoordinates {
13 pub latitude: f64,
14 pub longitude: f64,
15}
16#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
18pub enum CrossRegionStrategy {
19 AsyncAll,
21 SelectiveSync { target_regions: Vec<String> },
23 EventualConsistency { reconciliation_interval_ms: u64 },
25 ChainReplication { replication_chain: Vec<String> },
27}
28#[derive(Debug, Clone)]
30pub struct EventualConsistencyMetadata {
31 pub timestamp: SystemTime,
32 pub vector_clock: VectorClock,
33 pub source_region: String,
34 pub reconciliation_interval: Duration,
35}
36#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
38pub struct RegionConfig {
39 pub local_replication_factor: usize,
41 pub cross_region_replication_factor: usize,
43 pub max_regional_latency_ms: u64,
45 pub prefer_local_leader: bool,
47 pub enable_cross_region_backup: bool,
49 pub enable_relay: bool,
51 pub relay_latency_threshold_ms: f64,
53 pub enable_compression: bool,
55 pub enable_read_local: bool,
57 pub routing_strategy: RoutingStrategy,
59 pub properties: HashMap<String, String>,
61}
62#[derive(Debug)]
64pub struct RegionPerformanceMetrics {
65 pub inter_region_latencies: HashMap<(String, String), LatencyStats>,
67 pub region_throughput: HashMap<String, ThroughputStats>,
69 pub region_error_rates: HashMap<String, ErrorRateStats>,
71 pub last_updated: SystemTime,
73 pub monitoring_enabled: bool,
75}
76#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
78pub enum RoutingStrategy {
79 Direct,
81 LatencyAware,
83 BandwidthAware,
85 CostAware,
87}
88#[derive(Debug, Clone)]
90pub struct Route {
91 pub hops: Vec<String>,
93 pub total_latency: f64,
95 pub use_compression: bool,
97}
98impl Route {
99 pub fn direct(source: String, dest: String) -> Self {
101 Self {
102 hops: vec![source, dest],
103 total_latency: 0.0,
104 use_compression: true,
105 }
106 }
107}
108#[derive(Debug, Clone, Serialize, Deserialize)]
110pub struct VectorClock {
111 pub clocks: HashMap<String, u64>,
112}
113impl VectorClock {
114 pub fn new() -> Self {
116 Self {
117 clocks: HashMap::new(),
118 }
119 }
120 pub fn compare(&self, other: &VectorClock) -> VectorClockOrdering {
122 let mut self_greater = false;
123 let mut other_greater = false;
124 let all_keys: std::collections::HashSet<_> =
125 self.clocks.keys().chain(other.clocks.keys()).collect();
126 for key in all_keys {
127 let self_value = self.clocks.get(key).unwrap_or(&0);
128 let other_value = other.clocks.get(key).unwrap_or(&0);
129 if self_value > other_value {
130 self_greater = true;
131 } else if other_value > self_value {
132 other_greater = true;
133 }
134 }
135 match (self_greater, other_greater) {
136 (true, false) => VectorClockOrdering::Greater,
137 (false, true) => VectorClockOrdering::Less,
138 (false, false) => VectorClockOrdering::Equal,
139 (true, true) => VectorClockOrdering::Concurrent,
140 }
141 }
142}
143#[derive(Debug, Clone)]
145pub struct ThroughputStats {
146 pub operations_per_second: f64,
147 pub bytes_per_second: f64,
148 pub peak_ops_per_second: f64,
149 pub last_measurement: SystemTime,
150}
151#[derive(Debug, Clone, PartialEq)]
153pub enum RegionStatus {
154 Healthy,
155 Degraded,
156 Unavailable,
157}
158#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
160pub enum ConnectivityStatus {
161 Optimal,
163 Degraded { latency_ms: u64 },
165 Unstable { error_rate: f64 },
167 Disconnected,
169}
170#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
172pub enum ConflictResolutionStrategy {
173 LastWriterWins,
175 VectorClock,
177 Custom { resolution_function: String },
179 Manual,
181}
182#[derive(Debug, Default)]
184pub struct LatencyStats {
185 pub min_ms: f64,
186 pub max_ms: f64,
187 pub avg_ms: f64,
188 pub p95_ms: f64,
189 pub p99_ms: f64,
190 pub sample_count: u64,
191}
192#[derive(Debug, Clone)]
194pub struct RegionTopology {
195 pub regions: HashMap<String, Region>,
197 pub node_placements: HashMap<OxirsNodeId, NodePlacement>,
199 pub latency_matrix: HashMap<(String, String), f64>,
201 pub connectivity_status: HashMap<(String, String), ConnectivityStatus>,
203}
204#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
206pub struct AvailabilityZone {
207 pub id: String,
209 pub name: String,
211 pub region_id: String,
213}
214#[derive(Debug, Clone)]
216pub struct ReplicationPackage {
217 pub data: Vec<u8>,
218 pub metadata: EventualConsistencyMetadata,
219}
220#[derive(Debug, Clone)]
222pub struct ErrorRateStats {
223 pub total_operations: u64,
224 pub failed_operations: u64,
225 pub error_rate: f64,
226 pub last_error: Option<SystemTime>,
227}
228#[derive(Debug, Clone)]
230pub struct RegionHealth {
231 pub region_id: String,
232 pub total_nodes: usize,
233 pub healthy_nodes: usize,
234 pub throughput: ThroughputStats,
235 pub error_rate: ErrorRateStats,
236 pub status: RegionStatus,
237}
238#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
240pub struct MultiRegionReplicationStrategy {
241 pub intra_region: IntraRegionStrategy,
243 pub cross_region: CrossRegionStrategy,
245 pub conflict_resolution: ConflictResolutionStrategy,
247}
248#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
250pub enum ConsensusStrategy {
251 GlobalRaft,
253 RegionalRaft {
255 primary_region: String,
257 backup_regions: Vec<String>,
259 },
260 ByzantineConsensus {
262 byzantine_quorum: usize,
264 },
265 HybridConsensus {
267 region_preferences: HashMap<String, f64>,
269 },
270}
271#[derive(Debug, Clone, PartialEq)]
273pub enum VectorClockOrdering {
274 Less,
275 Greater,
276 Equal,
277 Concurrent,
278}
279#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
281pub struct Region {
282 pub id: String,
284 pub name: String,
286 pub coordinates: Option<GeoCoordinates>,
288 pub availability_zones: Vec<AvailabilityZone>,
290 pub config: RegionConfig,
292}
293#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
295pub struct NodePlacement {
296 pub node_id: OxirsNodeId,
298 pub region_id: String,
300 pub availability_zone_id: String,
302 pub data_center: Option<String>,
304 pub rack: Option<String>,
306}
307#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
309pub enum IntraRegionStrategy {
310 Synchronous { min_replicas: usize },
312 Asynchronous {
314 batch_size: usize,
315 batch_timeout_ms: u64,
316 },
317 Quorum { quorum_size: usize },
319}