oxirs-cluster 0.2.4

Raft-backed distributed dataset for high availability and horizontal scaling
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
//! # Data Replication
//!
//! High-level data replication management for distributed RDF storage.
//! Works with Raft consensus to ensure consistent replication.

use crate::raft::OxirsNodeId;
use std::collections::HashMap;
use std::time::{Duration, SystemTime};
use tokio::time::sleep;

/// Replication strategy for the cluster
#[derive(Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub enum ReplicationStrategy {
    /// Synchronous replication - wait for all replicas
    Synchronous,
    /// Asynchronous replication - fire and forget
    Asynchronous,
    /// Semi-synchronous - wait for minimum replicas
    SemiSynchronous { min_replicas: usize },
    /// Raft consensus - use Raft for replication
    RaftConsensus,
}

impl Default for ReplicationStrategy {
    fn default() -> Self {
        Self::RaftConsensus
    }
}

/// Information about a replica node
#[derive(Debug, Clone)]
pub struct ReplicaInfo {
    /// Unique node identifier
    pub node_id: OxirsNodeId,
    /// Address of the replica
    pub address: String,
    /// Last successfully applied log index
    pub last_applied_index: u64,
    /// Whether the replica is currently healthy
    pub is_healthy: bool,
    /// Last time we successfully communicated with this replica
    pub last_contact: SystemTime,
    /// Current replication lag in log entries
    pub replication_lag: u64,
    /// Network latency to this replica
    pub latency: Duration,
}

impl ReplicaInfo {
    /// Create a new replica info
    pub fn new(node_id: OxirsNodeId, address: String) -> Self {
        Self {
            node_id,
            address,
            last_applied_index: 0,
            is_healthy: true,
            last_contact: SystemTime::now(),
            replication_lag: 0,
            latency: Duration::from_millis(0),
        }
    }

    /// Check if replica is stale based on contact time
    pub fn is_stale(&self, threshold: Duration) -> bool {
        self.last_contact.elapsed().unwrap_or(Duration::MAX) > threshold
    }

    /// Update health status and contact time
    pub fn update_health(&mut self, is_healthy: bool) {
        self.is_healthy = is_healthy;
        if is_healthy {
            self.last_contact = SystemTime::now();
        }
    }
}

/// Replication statistics
#[derive(Debug, Clone, Default)]
pub struct ReplicationStats {
    pub total_replicas: usize,
    pub healthy_replicas: usize,
    pub average_lag: f64,
    pub max_lag: u64,
    pub min_lag: u64,
    pub average_latency: Duration,
    pub replication_throughput: f64, // operations per second
}

/// Replication manager for distributed RDF data
#[derive(Debug)]
pub struct ReplicationManager {
    strategy: ReplicationStrategy,
    replicas: HashMap<OxirsNodeId, ReplicaInfo>,
    local_node_id: OxirsNodeId,
    stats: ReplicationStats,
}

impl ReplicationManager {
    /// Create a new replication manager
    pub fn new(strategy: ReplicationStrategy, local_node_id: OxirsNodeId) -> Self {
        Self {
            strategy,
            replicas: HashMap::new(),
            local_node_id,
            stats: ReplicationStats::default(),
        }
    }

    /// Create a new replication manager with Raft consensus (default)
    pub fn with_raft_consensus(local_node_id: OxirsNodeId) -> Self {
        Self::new(ReplicationStrategy::RaftConsensus, local_node_id)
    }

    /// Add a replica to the replication set
    pub fn add_replica(&mut self, node_id: OxirsNodeId, address: String) -> bool {
        if node_id == self.local_node_id {
            tracing::warn!("Cannot add local node as replica");
            return false;
        }

        let replica_info = ReplicaInfo::new(node_id, address.clone());
        let is_new = !self.replicas.contains_key(&node_id);

        self.replicas.insert(node_id, replica_info);

        if is_new {
            tracing::info!("Added replica {} at {}", node_id, address);
            self.update_stats();
        }

        is_new
    }

    /// Remove a replica from the replication set
    pub fn remove_replica(&mut self, node_id: OxirsNodeId) -> bool {
        if let Some(replica) = self.replicas.remove(&node_id) {
            tracing::info!("Removed replica {} at {}", node_id, replica.address);
            self.update_stats();
            true
        } else {
            false
        }
    }

    /// Get all replicas
    pub fn get_replicas(&self) -> &HashMap<OxirsNodeId, ReplicaInfo> {
        &self.replicas
    }

    /// Get healthy replicas only
    pub fn get_healthy_replicas(&self) -> Vec<&ReplicaInfo> {
        self.replicas
            .values()
            .filter(|replica| replica.is_healthy)
            .collect()
    }

    /// Get replica by node ID
    pub fn get_replica(&self, node_id: OxirsNodeId) -> Option<&ReplicaInfo> {
        self.replicas.get(&node_id)
    }

    /// Update replica health status
    pub fn update_replica_health(&mut self, node_id: OxirsNodeId, is_healthy: bool) -> bool {
        if let Some(replica) = self.replicas.get_mut(&node_id) {
            let was_healthy = replica.is_healthy;
            replica.update_health(is_healthy);

            if was_healthy != is_healthy {
                tracing::info!(
                    "Replica {} health changed: {} -> {}",
                    node_id,
                    was_healthy,
                    is_healthy
                );
                self.update_stats();
            }

            true
        } else {
            false
        }
    }

    /// Update replica lag information
    pub fn update_replica_lag(
        &mut self,
        node_id: OxirsNodeId,
        applied_index: u64,
        current_index: u64,
    ) {
        if let Some(replica) = self.replicas.get_mut(&node_id) {
            replica.last_applied_index = applied_index;
            replica.replication_lag = current_index.saturating_sub(applied_index);
            self.update_stats();
        }
    }

    /// Check all replicas and mark stale ones as unhealthy
    pub async fn health_check(&mut self, stale_threshold: Duration) {
        let mut changed = false;

        for replica in self.replicas.values_mut() {
            let was_healthy = replica.is_healthy;

            if replica.is_stale(stale_threshold) {
                replica.is_healthy = false;
            }

            if was_healthy != replica.is_healthy {
                changed = true;
                tracing::warn!(
                    "Replica {} marked as unhealthy due to staleness",
                    replica.node_id
                );
            }
        }

        if changed {
            self.update_stats();
        }
    }

    /// Get the current replication strategy
    pub fn get_strategy(&self) -> &ReplicationStrategy {
        &self.strategy
    }

    /// Change the replication strategy
    pub fn set_strategy(&mut self, strategy: ReplicationStrategy) {
        if self.strategy != strategy {
            tracing::info!(
                "Changing replication strategy from {:?} to {:?}",
                self.strategy,
                strategy
            );
            self.strategy = strategy;
        }
    }

    /// Get replication statistics
    pub fn get_stats(&self) -> &ReplicationStats {
        &self.stats
    }

    /// Check if replication requirements are met
    pub fn is_replication_healthy(&self) -> bool {
        let healthy_count = self.get_healthy_replicas().len();

        match &self.strategy {
            ReplicationStrategy::Synchronous => healthy_count == self.replicas.len(),
            ReplicationStrategy::Asynchronous => true, // Always considered healthy
            ReplicationStrategy::SemiSynchronous { min_replicas } => healthy_count >= *min_replicas,
            ReplicationStrategy::RaftConsensus => {
                // For Raft, we need a majority of nodes to be healthy
                let total_nodes = self.replicas.len() + 1; // +1 for local node
                let majority = (total_nodes / 2) + 1;
                healthy_count + 1 >= majority // +1 for local node
            }
        }
    }

    /// Get required replica count for the current strategy
    pub fn required_replica_count(&self) -> usize {
        match &self.strategy {
            ReplicationStrategy::Synchronous => self.replicas.len(),
            ReplicationStrategy::Asynchronous => 0,
            ReplicationStrategy::SemiSynchronous { min_replicas } => *min_replicas,
            ReplicationStrategy::RaftConsensus => {
                let total_nodes = self.replicas.len() + 1;
                (total_nodes / 2) + 1 - 1 // -1 because local node is not a replica
            }
        }
    }

    /// Update internal statistics
    fn update_stats(&mut self) {
        let healthy_replicas_count = self.replicas.values().filter(|r| r.is_healthy).count();
        let healthy_lags: Vec<u64> = self
            .replicas
            .values()
            .filter(|r| r.is_healthy)
            .map(|r| r.replication_lag)
            .collect();
        let healthy_latencies: Vec<Duration> = self
            .replicas
            .values()
            .filter(|r| r.is_healthy)
            .map(|r| r.latency)
            .collect();

        self.stats.total_replicas = self.replicas.len();
        self.stats.healthy_replicas = healthy_replicas_count;

        if !healthy_lags.is_empty() {
            let total_lag: u64 = healthy_lags.iter().sum();
            self.stats.average_lag = total_lag as f64 / healthy_lags.len() as f64;
            self.stats.max_lag = healthy_lags.iter().copied().max().unwrap_or(0);
            self.stats.min_lag = healthy_lags.iter().copied().min().unwrap_or(0);

            let total_latency: Duration = healthy_latencies.iter().sum();
            self.stats.average_latency = total_latency / healthy_latencies.len() as u32;
        } else {
            self.stats.average_lag = 0.0;
            self.stats.max_lag = 0;
            self.stats.min_lag = 0;
            self.stats.average_latency = Duration::from_millis(0);
        }
    }

    /// Run periodic maintenance tasks
    pub async fn run_maintenance(&mut self) {
        const HEALTH_CHECK_INTERVAL: Duration = Duration::from_secs(30);
        const STALE_THRESHOLD: Duration = Duration::from_secs(60);

        loop {
            sleep(HEALTH_CHECK_INTERVAL).await;

            self.health_check(STALE_THRESHOLD).await;

            // Log stats periodically
            if self.stats.total_replicas > 0 {
                tracing::debug!(
                    "Replication stats: {}/{} healthy, avg lag: {:.1}, max lag: {}",
                    self.stats.healthy_replicas,
                    self.stats.total_replicas,
                    self.stats.average_lag,
                    self.stats.max_lag
                );
            }
        }
    }
}

/// Replication-related errors
#[derive(Debug, thiserror::Error)]
pub enum ReplicationError {
    #[error("Insufficient replicas: need {required}, have {available}")]
    InsufficientReplicas { required: usize, available: usize },

    #[error("Replica {node_id} is unhealthy")]
    UnhealthyReplica { node_id: OxirsNodeId },

    #[error("Replication timeout after {timeout:?}")]
    Timeout { timeout: Duration },

    #[error("Network error: {message}")]
    Network { message: String },

    #[error("Serialization error: {message}")]
    Serialization { message: String },
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_replication_strategy_default() {
        let strategy = ReplicationStrategy::default();
        assert_eq!(strategy, ReplicationStrategy::RaftConsensus);
    }

    #[test]
    fn test_replica_info_creation() {
        let replica = ReplicaInfo::new(1, "127.0.0.1:8080".to_string());

        assert_eq!(replica.node_id, 1);
        assert_eq!(replica.address, "127.0.0.1:8080");
        assert_eq!(replica.last_applied_index, 0);
        assert!(replica.is_healthy);
        assert_eq!(replica.replication_lag, 0);
        assert_eq!(replica.latency, Duration::from_millis(0));
    }

    #[test]
    fn test_replica_info_staleness() {
        let replica = ReplicaInfo::new(1, "127.0.0.1:8080".to_string());

        // Fresh replica should not be stale
        assert!(!replica.is_stale(Duration::from_secs(10)));

        // Wait a tiny bit to ensure elapsed time passes the threshold
        std::thread::sleep(Duration::from_micros(1));

        // Simulate old replica by checking against very short threshold
        assert!(replica.is_stale(Duration::from_nanos(1)));
    }

    #[test]
    fn test_replica_info_update_health() {
        let mut replica = ReplicaInfo::new(1, "127.0.0.1:8080".to_string());

        // Update to unhealthy
        replica.update_health(false);
        assert!(!replica.is_healthy);

        // Update to healthy
        replica.update_health(true);
        assert!(replica.is_healthy);
    }

    #[test]
    fn test_replication_manager_creation() {
        let manager = ReplicationManager::new(ReplicationStrategy::Synchronous, 1);

        assert_eq!(manager.strategy, ReplicationStrategy::Synchronous);
        assert_eq!(manager.local_node_id, 1);
        assert!(manager.replicas.is_empty());
        assert_eq!(manager.stats.total_replicas, 0);
    }

    #[test]
    fn test_replication_manager_with_raft_consensus() {
        let manager = ReplicationManager::with_raft_consensus(1);

        assert_eq!(manager.strategy, ReplicationStrategy::RaftConsensus);
        assert_eq!(manager.local_node_id, 1);
    }

    #[test]
    fn test_replication_manager_add_replica() {
        let mut manager = ReplicationManager::new(ReplicationStrategy::Synchronous, 1);

        // Add replica
        assert!(manager.add_replica(2, "127.0.0.1:8081".to_string()));
        assert_eq!(manager.replicas.len(), 1);
        assert!(manager.replicas.contains_key(&2));

        // Adding same replica again should return false
        assert!(!manager.add_replica(2, "127.0.0.1:8081".to_string()));
        assert_eq!(manager.replicas.len(), 1);

        // Cannot add local node as replica
        assert!(!manager.add_replica(1, "127.0.0.1:8080".to_string()));
        assert_eq!(manager.replicas.len(), 1);
    }

    #[test]
    fn test_replication_manager_remove_replica() {
        let mut manager = ReplicationManager::new(ReplicationStrategy::Synchronous, 1);

        manager.add_replica(2, "127.0.0.1:8081".to_string());
        manager.add_replica(3, "127.0.0.1:8082".to_string());
        assert_eq!(manager.replicas.len(), 2);

        // Remove replica
        assert!(manager.remove_replica(2));
        assert_eq!(manager.replicas.len(), 1);
        assert!(!manager.replicas.contains_key(&2));

        // Removing non-existent replica should return false
        assert!(!manager.remove_replica(4));
        assert_eq!(manager.replicas.len(), 1);
    }

    #[test]
    fn test_replication_manager_get_healthy_replicas() {
        let mut manager = ReplicationManager::new(ReplicationStrategy::Synchronous, 1);

        manager.add_replica(2, "127.0.0.1:8081".to_string());
        manager.add_replica(3, "127.0.0.1:8082".to_string());

        // Mark one replica as unhealthy
        manager.update_replica_health(3, false);

        let healthy_replicas = manager.get_healthy_replicas();
        assert_eq!(healthy_replicas.len(), 1);
        assert_eq!(healthy_replicas[0].node_id, 2);
    }

    #[test]
    fn test_replication_manager_update_replica_health() {
        let mut manager = ReplicationManager::new(ReplicationStrategy::Synchronous, 1);

        manager.add_replica(2, "127.0.0.1:8081".to_string());

        // Update health status
        assert!(manager.update_replica_health(2, false));
        let replica = manager.get_replica(2).unwrap();
        assert!(!replica.is_healthy);

        // Update non-existent replica should return false
        assert!(!manager.update_replica_health(3, true));
    }

    #[test]
    fn test_replication_manager_update_replica_lag() {
        let mut manager = ReplicationManager::new(ReplicationStrategy::Synchronous, 1);

        manager.add_replica(2, "127.0.0.1:8081".to_string());

        // Update lag information
        manager.update_replica_lag(2, 50, 100);
        let replica = manager.get_replica(2).unwrap();
        assert_eq!(replica.last_applied_index, 50);
        assert_eq!(replica.replication_lag, 50);
    }

    #[tokio::test]
    async fn test_replication_manager_health_check() {
        let mut manager = ReplicationManager::new(ReplicationStrategy::Synchronous, 1);

        manager.add_replica(2, "127.0.0.1:8081".to_string());
        manager.add_replica(3, "127.0.0.1:8082".to_string());

        // Both replicas should be healthy initially
        assert_eq!(manager.get_healthy_replicas().len(), 2);

        // Run health check with very short threshold - should mark all as unhealthy
        manager.health_check(Duration::from_nanos(1)).await;
        assert_eq!(manager.get_healthy_replicas().len(), 0);
    }

    #[test]
    fn test_replication_manager_strategy_change() {
        let mut manager = ReplicationManager::new(ReplicationStrategy::Synchronous, 1);

        assert_eq!(manager.get_strategy(), &ReplicationStrategy::Synchronous);

        manager.set_strategy(ReplicationStrategy::Asynchronous);
        assert_eq!(manager.get_strategy(), &ReplicationStrategy::Asynchronous);
    }

    #[test]
    fn test_replication_manager_health_status() {
        let mut manager =
            ReplicationManager::new(ReplicationStrategy::SemiSynchronous { min_replicas: 2 }, 1);

        // Add replicas
        manager.add_replica(2, "127.0.0.1:8081".to_string());
        manager.add_replica(3, "127.0.0.1:8082".to_string());
        manager.add_replica(4, "127.0.0.1:8083".to_string());

        // All healthy - should meet requirements
        assert!(manager.is_replication_healthy());

        // Mark one as unhealthy - should still meet requirements (2 out of 3)
        manager.update_replica_health(4, false);
        assert!(manager.is_replication_healthy());

        // Mark another as unhealthy - should not meet requirements (1 out of 3)
        manager.update_replica_health(3, false);
        assert!(!manager.is_replication_healthy());
    }

    #[test]
    fn test_replication_manager_required_replica_count() {
        let mut manager = ReplicationManager::new(ReplicationStrategy::Synchronous, 1);
        manager.add_replica(2, "127.0.0.1:8081".to_string());
        manager.add_replica(3, "127.0.0.1:8082".to_string());

        // Synchronous requires all replicas
        assert_eq!(manager.required_replica_count(), 2);

        // Asynchronous requires no replicas
        manager.set_strategy(ReplicationStrategy::Asynchronous);
        assert_eq!(manager.required_replica_count(), 0);

        // Semi-synchronous requires minimum specified
        manager.set_strategy(ReplicationStrategy::SemiSynchronous { min_replicas: 1 });
        assert_eq!(manager.required_replica_count(), 1);

        // Raft consensus requires majority
        manager.set_strategy(ReplicationStrategy::RaftConsensus);
        // With 2 replicas + 1 local = 3 total, majority is 2, so we need 1 replica (since local is always available)
        assert_eq!(manager.required_replica_count(), 1);
    }

    #[test]
    fn test_replication_manager_raft_consensus_health() {
        let mut manager = ReplicationManager::new(ReplicationStrategy::RaftConsensus, 1);

        // Single node cluster (1 local + 0 replicas = 1 total) - should be healthy
        assert!(manager.is_replication_healthy());

        // Add replicas to form 3-node cluster
        manager.add_replica(2, "127.0.0.1:8081".to_string());
        manager.add_replica(3, "127.0.0.1:8082".to_string());

        // All healthy (3 total, majority = 2, local + 2 replicas = 3) - should be healthy
        assert!(manager.is_replication_healthy());

        // Mark one replica as unhealthy (local + 1 replica = 2, still majority) - should be healthy
        manager.update_replica_health(3, false);
        assert!(manager.is_replication_healthy());

        // Mark both replicas as unhealthy (only local = 1, not majority) - should not be healthy
        manager.update_replica_health(2, false);
        assert!(!manager.is_replication_healthy());
    }

    #[test]
    fn test_replication_stats_default() {
        let stats = ReplicationStats::default();
        assert_eq!(stats.total_replicas, 0);
        assert_eq!(stats.healthy_replicas, 0);
        assert_eq!(stats.average_lag, 0.0);
        assert_eq!(stats.max_lag, 0);
        assert_eq!(stats.min_lag, 0);
        assert_eq!(stats.average_latency, Duration::from_millis(0));
        assert_eq!(stats.replication_throughput, 0.0);
    }

    #[test]
    fn test_replication_error_display() {
        let err = ReplicationError::InsufficientReplicas {
            required: 3,
            available: 1,
        };
        assert!(err
            .to_string()
            .contains("Insufficient replicas: need 3, have 1"));

        let err = ReplicationError::UnhealthyReplica { node_id: 42 };
        assert!(err.to_string().contains("Replica 42 is unhealthy"));

        let err = ReplicationError::Timeout {
            timeout: Duration::from_secs(5),
        };
        assert!(err.to_string().contains("Replication timeout after 5s"));

        let err = ReplicationError::Network {
            message: "connection failed".to_string(),
        };
        assert!(err.to_string().contains("Network error: connection failed"));

        let err = ReplicationError::Serialization {
            message: "json error".to_string(),
        };
        assert!(err.to_string().contains("Serialization error: json error"));
    }
}