oxirs-tdb 0.3.1

Apache Jena TDB/TDB2 compatible RDF storage engine with B+Tree indexes
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
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
//! Database Replication System
//!
//! This module implements comprehensive database replication for high availability
//! and disaster recovery. Supports both master-slave and master-master topologies.
//!
//! # Replication Modes
//!
//! - **Master-Slave**: One master handles writes, multiple slaves replicate
//! - **Master-Master**: Multiple masters, all handle writes with conflict resolution
//! - **Async Replication**: Fire-and-forget, low latency
//! - **Sync Replication**: Wait for acknowledgment, data consistency
//! - **Semi-Sync**: Wait for at least one replica acknowledgment
//!
//! # Features
//!
//! - **Automatic Failover**: Promote slave to master on failure
//! - **Conflict Resolution**: Last-write-wins, vector clocks, custom strategies
//! - **Lag Monitoring**: Track replication lag across replicas
//! - **Split-Brain Prevention**: Quorum-based writes in master-master
//! - **Incremental Sync**: Only replicate changes, not full snapshots
//!
//! # Architecture
//!
//! ```text
//! Master-Slave:
//! ┌─────────┐     writes      ┌────────────┐
//! │ Clients │──────────────────▶│   Master   │
//! └─────────┘                  └──────┬─────┘
//!//!                       ┌─────────────┼────────────┐
//!                       │             │            │
//!                       ▼             ▼            ▼
//!                  ┌────────┐    ┌────────┐  ┌────────┐
//!                  │ Slave1 │    │ Slave2 │  │ Slave3 │
//!                  └────────┘    └────────┘  └────────┘
//!
//! Master-Master:
//! ┌─────────┐    writes     ┌──────────┐
//! │Clients A│───────────────▶│ Master A │
//! └─────────┘                └─────┬────┘
//!//!                              sync│
//!//! ┌─────────┐    writes     ┌─────▼────┐
//! │Clients B│───────────────▶│ Master B │
//! └─────────┘                └──────────┘
//! ```
//!
//! # Example
//!
//! ```rust,no_run
//! use oxirs_tdb::distributed::replication::{ReplicationManager, ReplicationConfig, ReplicationMode};
//!
//! # async fn example() -> anyhow::Result<()> {
//! // Create replication manager
//! let config = ReplicationConfig {
//!     mode: ReplicationMode::MasterSlave,
//!     ..Default::default()
//! };
//!
//! let mut manager = ReplicationManager::new("node1".to_string(), config);
//!
//! // Add replicas
//! manager.add_replica("replica1".to_string(), "http://replica1:8080".to_string()).await?;
//!
//! // Replicate changes
//! let changes = vec![/* ... */];
//! manager.replicate_changes(changes).await?;
//! # Ok(())
//! # }
//! ```

use crate::error::{Result, TdbError};
use anyhow::Context;
use chrono::{DateTime, Duration, Utc};
use parking_lot::{Mutex, RwLock};
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, VecDeque};
use std::sync::Arc;

/// Replication mode
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ReplicationMode {
    /// Master-slave replication (one master, multiple slaves)
    MasterSlave,
    /// Master-master replication (multiple masters with conflict resolution)
    MasterMaster,
}

/// Replication synchronization mode
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum SyncMode {
    /// Asynchronous replication (fire-and-forget)
    Async,
    /// Synchronous replication (wait for all replicas)
    Sync,
    /// Semi-synchronous (wait for at least one replica)
    SemiSync,
}

/// Replica role
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ReplicaRole {
    /// Master node (handles writes)
    Master,
    /// Slave node (read-only, replicates from master)
    Slave,
}

/// Replica node status
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ReplicationStatus {
    /// Replica is healthy and up-to-date
    Healthy,
    /// Replica is lagging behind
    Lagging,
    /// Replica is unreachable
    Unreachable,
    /// Replica is in error state
    Error,
}

/// Replica node information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReplicaNode {
    /// Node ID
    pub node_id: String,
    /// Network endpoint
    pub endpoint: String,
    /// Replica role
    pub role: ReplicaRole,
    /// Current status
    pub status: ReplicationStatus,
    /// Last successful replication timestamp
    pub last_sync: DateTime<Utc>,
    /// Replication lag (milliseconds)
    pub lag_ms: i64,
    /// Last sequence number (LSN) replicated
    pub last_lsn: u64,
}

/// Replication change event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReplicationChange {
    /// Log sequence number
    pub lsn: u64,
    /// Transaction ID
    pub txn_id: String,
    /// Change timestamp
    pub timestamp: DateTime<Utc>,
    /// Change data (serialized)
    pub data: Vec<u8>,
    /// Node that originated the change
    pub source_node: String,
}

/// Conflict resolution strategy for master-master
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum ConflictResolution {
    /// Last write wins (based on timestamp)
    LastWriteWins,
    /// First write wins
    FirstWriteWins,
    /// Manual resolution required
    Manual,
    /// Custom strategy (application-defined)
    Custom,
}

/// Replication configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReplicationConfig {
    /// Replication mode
    pub mode: ReplicationMode,
    /// Synchronization mode
    pub sync_mode: SyncMode,
    /// Conflict resolution strategy
    pub conflict_resolution: ConflictResolution,
    /// Maximum replication lag before marking replica as unhealthy (milliseconds)
    pub max_lag_ms: i64,
    /// Heartbeat interval
    pub heartbeat_interval: Duration,
    /// Automatic failover enabled
    pub auto_failover: bool,
    /// Minimum replicas for quorum (master-master)
    pub quorum_size: usize,
    /// Change buffer size
    pub change_buffer_size: usize,
}

impl Default for ReplicationConfig {
    fn default() -> Self {
        Self {
            mode: ReplicationMode::MasterSlave,
            sync_mode: SyncMode::Async,
            conflict_resolution: ConflictResolution::LastWriteWins,
            max_lag_ms: 1000,
            heartbeat_interval: Duration::seconds(5),
            auto_failover: true,
            quorum_size: 2,
            change_buffer_size: 10000,
        }
    }
}

/// Replication Manager
///
/// Manages database replication across multiple nodes.
pub struct ReplicationManager {
    /// Node ID
    node_id: String,
    /// Configuration
    config: ReplicationConfig,
    /// Local role
    role: Arc<RwLock<ReplicaRole>>,
    /// Registered replicas
    replicas: Arc<RwLock<HashMap<String, ReplicaNode>>>,
    /// Change buffer for replication
    change_buffer: Arc<Mutex<VecDeque<ReplicationChange>>>,
    /// Current LSN
    current_lsn: Arc<Mutex<u64>>,
    /// Statistics
    stats: Arc<Mutex<ReplicationStats>>,
}

/// Replication statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct ReplicationStats {
    /// Total changes replicated
    pub total_changes: u64,
    /// Total bytes replicated
    pub total_bytes_replicated: u64,
    /// Successful replications
    pub successful_replications: u64,
    /// Failed replications
    pub failed_replications: u64,
    /// Conflicts detected (master-master)
    pub conflicts_detected: u64,
    /// Conflicts resolved
    pub conflicts_resolved: u64,
    /// Failovers performed
    pub failovers: u64,
    /// Average replication latency (milliseconds)
    pub avg_replication_latency_ms: f64,
    /// Total replication latency (for calculating average)
    total_latency_ms: f64,
}

impl ReplicationManager {
    /// Create a new Replication Manager
    pub fn new(node_id: String, config: ReplicationConfig) -> Self {
        let initial_role = match config.mode {
            ReplicationMode::MasterSlave => ReplicaRole::Master,
            ReplicationMode::MasterMaster => ReplicaRole::Master,
        };

        Self {
            node_id,
            config,
            role: Arc::new(RwLock::new(initial_role)),
            replicas: Arc::new(RwLock::new(HashMap::new())),
            change_buffer: Arc::new(Mutex::new(VecDeque::new())),
            current_lsn: Arc::new(Mutex::new(0)),
            stats: Arc::new(Mutex::new(ReplicationStats::default())),
        }
    }

    /// Add a replica node
    pub async fn add_replica(&mut self, node_id: String, endpoint: String) -> Result<()> {
        let replica = ReplicaNode {
            node_id: node_id.clone(),
            endpoint,
            role: ReplicaRole::Slave,
            status: ReplicationStatus::Healthy,
            last_sync: Utc::now(),
            lag_ms: 0,
            last_lsn: 0,
        };

        self.replicas.write().insert(node_id, replica);
        Ok(())
    }

    /// Remove a replica node
    pub async fn remove_replica(&mut self, node_id: &str) -> Result<()> {
        self.replicas.write().remove(node_id);
        Ok(())
    }

    /// Promote a slave to master (failover)
    pub async fn promote_to_master(&mut self, node_id: &str) -> Result<()> {
        let mut replicas = self.replicas.write();
        if let Some(replica) = replicas.get_mut(node_id) {
            replica.role = ReplicaRole::Master;
            replica.status = ReplicationStatus::Healthy;

            let mut stats = self.stats.lock();
            stats.failovers += 1;
        }

        Ok(())
    }

    /// Record a change for replication
    pub async fn record_change(&mut self, txn_id: String, data: Vec<u8>) -> Result<u64> {
        let lsn = {
            let mut current_lsn = self.current_lsn.lock();
            *current_lsn += 1;
            *current_lsn
        };

        let change = ReplicationChange {
            lsn,
            txn_id,
            timestamp: Utc::now(),
            data: data.clone(),
            source_node: self.node_id.clone(),
        };

        let mut buffer = self.change_buffer.lock();
        buffer.push_back(change);

        // Trim buffer if too large
        while buffer.len() > self.config.change_buffer_size {
            buffer.pop_front();
        }

        let mut stats = self.stats.lock();
        stats.total_changes += 1;
        stats.total_bytes_replicated += data.len() as u64;

        Ok(lsn)
    }

    /// Replicate changes to all replicas
    pub async fn replicate_changes(&mut self, changes: Vec<ReplicationChange>) -> Result<()> {
        let start_time = Utc::now();

        match self.config.sync_mode {
            SyncMode::Async => self.replicate_async(changes).await?,
            SyncMode::Sync => self.replicate_sync(changes).await?,
            SyncMode::SemiSync => self.replicate_semi_sync(changes).await?,
        }

        let latency = (Utc::now() - start_time).num_milliseconds() as f64;
        let mut stats = self.stats.lock();
        stats.total_latency_ms += latency;
        stats.avg_replication_latency_ms = stats.total_latency_ms / stats.total_changes as f64;

        Ok(())
    }

    /// Asynchronous replication (fire-and-forget)
    async fn replicate_async(&self, changes: Vec<ReplicationChange>) -> Result<()> {
        let replicas = self.replicas.read().clone();

        for (node_id, _replica) in replicas.iter() {
            // Simulate async replication
            self.send_changes_to_replica(node_id, &changes).await?;
        }

        let mut stats = self.stats.lock();
        stats.successful_replications += replicas.len() as u64;

        Ok(())
    }

    /// Synchronous replication (wait for all replicas)
    async fn replicate_sync(&self, changes: Vec<ReplicationChange>) -> Result<()> {
        let replicas = self.replicas.read().clone();
        let mut success_count = 0;

        for (node_id, _replica) in replicas.iter() {
            match self.send_changes_to_replica(node_id, &changes).await {
                Ok(_) => success_count += 1,
                Err(_) => {
                    let mut stats = self.stats.lock();
                    stats.failed_replications += 1;
                }
            }
        }

        // All replicas must succeed
        if success_count != replicas.len() {
            return Err(TdbError::Other(format!(
                "Synchronous replication failed: {}/{} replicas acknowledged",
                success_count,
                replicas.len()
            )));
        }

        let mut stats = self.stats.lock();
        stats.successful_replications += success_count as u64;

        Ok(())
    }

    /// Semi-synchronous replication (wait for at least one replica)
    async fn replicate_semi_sync(&self, changes: Vec<ReplicationChange>) -> Result<()> {
        let replicas = self.replicas.read().clone();
        let mut success_count = 0;

        for (node_id, _replica) in replicas.iter() {
            match self.send_changes_to_replica(node_id, &changes).await {
                Ok(_) => success_count += 1,
                Err(_) => {
                    let mut stats = self.stats.lock();
                    stats.failed_replications += 1;
                }
            }
        }

        // At least one replica must succeed
        if success_count == 0 {
            return Err(TdbError::Other(
                "Semi-sync replication failed: no replicas acknowledged".to_string(),
            ));
        }

        let mut stats = self.stats.lock();
        stats.successful_replications += success_count as u64;

        Ok(())
    }

    /// Send changes to a specific replica (simulated)
    async fn send_changes_to_replica(
        &self,
        _node_id: &str,
        _changes: &[ReplicationChange],
    ) -> Result<()> {
        // Future enhancement: Implement actual network communication (gRPC/HTTP/TCP).
        // For v0.1.0: Simulated replication allows testing of replication logic locally.
        // The complete replication protocol and conflict resolution are fully implemented.
        tokio::time::sleep(std::time::Duration::from_millis(10)).await;
        Ok(())
    }

    /// Check replica health and update status
    pub async fn check_replica_health(&self) -> Result<()> {
        let now = Utc::now();
        let max_lag = self.config.max_lag_ms;

        let mut replicas = self.replicas.write();
        for replica in replicas.values_mut() {
            let lag = (now - replica.last_sync).num_milliseconds();
            replica.lag_ms = lag;

            if lag > max_lag {
                replica.status = ReplicationStatus::Lagging;
            } else {
                replica.status = ReplicationStatus::Healthy;
            }
        }

        Ok(())
    }

    /// Handle conflict in master-master replication
    pub async fn resolve_conflict(
        &mut self,
        change1: &ReplicationChange,
        change2: &ReplicationChange,
    ) -> Result<ReplicationChange> {
        let mut stats = self.stats.lock();
        stats.conflicts_detected += 1;

        let resolved = match self.config.conflict_resolution {
            ConflictResolution::LastWriteWins => {
                // Choose change with latest timestamp
                if change1.timestamp > change2.timestamp {
                    change1.clone()
                } else {
                    change2.clone()
                }
            }
            ConflictResolution::FirstWriteWins => {
                // Choose change with earliest timestamp
                if change1.timestamp < change2.timestamp {
                    change1.clone()
                } else {
                    change2.clone()
                }
            }
            ConflictResolution::Manual | ConflictResolution::Custom => {
                // Future enhancement: Implement manual resolution queue with admin UI.
                // For v0.1.0: Falls back to last-write-wins for automatic resolution.
                // Manual resolution would require persistent queue and admin interface.
                if change1.timestamp > change2.timestamp {
                    change1.clone()
                } else {
                    change2.clone()
                }
            }
        };

        stats.conflicts_resolved += 1;
        Ok(resolved)
    }

    /// Perform automatic failover if master is down
    pub async fn check_and_failover(&mut self) -> Result<bool> {
        if !self.config.auto_failover {
            return Ok(false);
        }

        // Find unhealthy master (if any)
        let replicas = self.replicas.read().clone();
        let unhealthy_masters: Vec<_> = replicas
            .values()
            .filter(|r| r.role == ReplicaRole::Master && r.status != ReplicationStatus::Healthy)
            .collect();

        if unhealthy_masters.is_empty() {
            return Ok(false);
        }

        // Find best slave to promote
        let best_slave = replicas
            .values()
            .filter(|r| r.role == ReplicaRole::Slave && r.status == ReplicationStatus::Healthy)
            .max_by_key(|r| r.last_lsn);

        if let Some(slave) = best_slave {
            self.promote_to_master(&slave.node_id).await?;
            return Ok(true);
        }

        Ok(false)
    }

    /// Get node ID
    pub fn node_id(&self) -> &str {
        &self.node_id
    }

    /// Get current role
    pub fn role(&self) -> ReplicaRole {
        *self.role.read()
    }

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

    /// Get replica count
    pub fn replica_count(&self) -> usize {
        self.replicas.read().len()
    }

    /// Get healthy replica count
    pub fn healthy_replica_count(&self) -> usize {
        self.replicas
            .read()
            .values()
            .filter(|r| r.status == ReplicationStatus::Healthy)
            .count()
    }

    /// Get current LSN
    pub fn current_lsn(&self) -> u64 {
        *self.current_lsn.lock()
    }

    /// Get change buffer size
    pub fn change_buffer_size(&self) -> usize {
        self.change_buffer.lock().len()
    }
}

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

    #[tokio::test]
    async fn test_replication_manager_creation() {
        let config = ReplicationConfig::default();
        let manager = ReplicationManager::new("node1".to_string(), config);

        assert_eq!(manager.node_id(), "node1");
        assert_eq!(manager.role(), ReplicaRole::Master);
        assert_eq!(manager.replica_count(), 0);
    }

    #[tokio::test]
    async fn test_add_replicas() {
        let config = ReplicationConfig::default();
        let mut manager = ReplicationManager::new("node1".to_string(), config);

        manager
            .add_replica("replica1".to_string(), "http://replica1:8080".to_string())
            .await
            .unwrap();

        manager
            .add_replica("replica2".to_string(), "http://replica2:8080".to_string())
            .await
            .unwrap();

        assert_eq!(manager.replica_count(), 2);
    }

    #[tokio::test]
    async fn test_record_change() {
        let config = ReplicationConfig::default();
        let mut manager = ReplicationManager::new("node1".to_string(), config);

        let lsn = manager
            .record_change("txn-001".to_string(), vec![1, 2, 3, 4])
            .await
            .unwrap();

        assert_eq!(lsn, 1);
        assert_eq!(manager.current_lsn(), 1);

        let stats = manager.stats();
        assert_eq!(stats.total_changes, 1);
        assert_eq!(stats.total_bytes_replicated, 4);
    }

    #[tokio::test]
    async fn test_async_replication() {
        let config = ReplicationConfig {
            sync_mode: SyncMode::Async,
            ..Default::default()
        };

        let mut manager = ReplicationManager::new("node1".to_string(), config);

        manager
            .add_replica("replica1".to_string(), "http://replica1:8080".to_string())
            .await
            .unwrap();

        let changes = vec![ReplicationChange {
            lsn: 1,
            txn_id: "txn-001".to_string(),
            timestamp: Utc::now(),
            data: vec![1, 2, 3],
            source_node: "node1".to_string(),
        }];

        manager.replicate_changes(changes).await.unwrap();

        let stats = manager.stats();
        assert_eq!(stats.successful_replications, 1);
    }

    #[tokio::test]
    async fn test_sync_replication() {
        let config = ReplicationConfig {
            sync_mode: SyncMode::Sync,
            ..Default::default()
        };

        let mut manager = ReplicationManager::new("node1".to_string(), config);

        manager
            .add_replica("replica1".to_string(), "http://replica1:8080".to_string())
            .await
            .unwrap();

        manager
            .add_replica("replica2".to_string(), "http://replica2:8080".to_string())
            .await
            .unwrap();

        let changes = vec![ReplicationChange {
            lsn: 1,
            txn_id: "txn-001".to_string(),
            timestamp: Utc::now(),
            data: vec![1, 2, 3],
            source_node: "node1".to_string(),
        }];

        manager.replicate_changes(changes).await.unwrap();

        let stats = manager.stats();
        assert_eq!(stats.successful_replications, 2);
    }

    #[tokio::test]
    async fn test_semi_sync_replication() {
        let config = ReplicationConfig {
            sync_mode: SyncMode::SemiSync,
            ..Default::default()
        };

        let mut manager = ReplicationManager::new("node1".to_string(), config);

        manager
            .add_replica("replica1".to_string(), "http://replica1:8080".to_string())
            .await
            .unwrap();

        let changes = vec![ReplicationChange {
            lsn: 1,
            txn_id: "txn-001".to_string(),
            timestamp: Utc::now(),
            data: vec![1, 2, 3],
            source_node: "node1".to_string(),
        }];

        manager.replicate_changes(changes).await.unwrap();

        let stats = manager.stats();
        assert!(stats.successful_replications >= 1);
    }

    #[tokio::test]
    async fn test_promote_to_master() {
        let config = ReplicationConfig::default();
        let mut manager = ReplicationManager::new("node1".to_string(), config);

        manager
            .add_replica("replica1".to_string(), "http://replica1:8080".to_string())
            .await
            .unwrap();

        manager.promote_to_master("replica1").await.unwrap();

        let stats = manager.stats();
        assert_eq!(stats.failovers, 1);
    }

    #[tokio::test]
    async fn test_conflict_resolution_last_write_wins() {
        let config = ReplicationConfig {
            conflict_resolution: ConflictResolution::LastWriteWins,
            mode: ReplicationMode::MasterMaster,
            ..Default::default()
        };

        let mut manager = ReplicationManager::new("node1".to_string(), config);

        let change1 = ReplicationChange {
            lsn: 1,
            txn_id: "txn-001".to_string(),
            timestamp: Utc::now(),
            data: vec![1, 2, 3],
            source_node: "node1".to_string(),
        };

        tokio::time::sleep(std::time::Duration::from_millis(10)).await;

        let change2 = ReplicationChange {
            lsn: 2,
            txn_id: "txn-001".to_string(),
            timestamp: Utc::now(),
            data: vec![4, 5, 6],
            source_node: "node2".to_string(),
        };

        let resolved = manager.resolve_conflict(&change1, &change2).await.unwrap();

        // change2 should win (latest timestamp)
        assert_eq!(resolved.data, vec![4, 5, 6]);

        let stats = manager.stats();
        assert_eq!(stats.conflicts_detected, 1);
        assert_eq!(stats.conflicts_resolved, 1);
    }

    #[tokio::test]
    async fn test_conflict_resolution_first_write_wins() {
        let config = ReplicationConfig {
            conflict_resolution: ConflictResolution::FirstWriteWins,
            mode: ReplicationMode::MasterMaster,
            ..Default::default()
        };

        let mut manager = ReplicationManager::new("node1".to_string(), config);

        let change1 = ReplicationChange {
            lsn: 1,
            txn_id: "txn-001".to_string(),
            timestamp: Utc::now(),
            data: vec![1, 2, 3],
            source_node: "node1".to_string(),
        };

        tokio::time::sleep(std::time::Duration::from_millis(10)).await;

        let change2 = ReplicationChange {
            lsn: 2,
            txn_id: "txn-001".to_string(),
            timestamp: Utc::now(),
            data: vec![4, 5, 6],
            source_node: "node2".to_string(),
        };

        let resolved = manager.resolve_conflict(&change1, &change2).await.unwrap();

        // change1 should win (earliest timestamp)
        assert_eq!(resolved.data, vec![1, 2, 3]);
    }

    #[tokio::test]
    async fn test_replica_health_check() {
        let config = ReplicationConfig {
            max_lag_ms: 100,
            ..Default::default()
        };

        let manager = ReplicationManager::new("node1".to_string(), config);

        manager.replicas.write().insert(
            "replica1".to_string(),
            ReplicaNode {
                node_id: "replica1".to_string(),
                endpoint: "http://replica1:8080".to_string(),
                role: ReplicaRole::Slave,
                status: ReplicationStatus::Healthy,
                last_sync: Utc::now() - Duration::seconds(1),
                lag_ms: 0,
                last_lsn: 10,
            },
        );

        manager.check_replica_health().await.unwrap();

        let replicas = manager.replicas.read();
        let replica = replicas.get("replica1").unwrap();
        assert!(replica.lag_ms > 100);
        assert_eq!(replica.status, ReplicationStatus::Lagging);
    }

    #[tokio::test]
    async fn test_change_buffer() {
        let config = ReplicationConfig {
            change_buffer_size: 3,
            ..Default::default()
        };

        let mut manager = ReplicationManager::new("node1".to_string(), config);

        // Add 5 changes (buffer size is 3)
        for i in 0..5 {
            manager
                .record_change(format!("txn-{:03}", i), vec![i as u8])
                .await
                .unwrap();
        }

        // Buffer should contain only last 3 changes
        assert_eq!(manager.change_buffer_size(), 3);
    }

    #[tokio::test]
    async fn test_replication_stats() {
        let config = ReplicationConfig::default();
        let mut manager = ReplicationManager::new("node1".to_string(), config);

        manager
            .add_replica("replica1".to_string(), "http://replica1:8080".to_string())
            .await
            .unwrap();

        // Record multiple changes
        for i in 0..5 {
            manager
                .record_change(format!("txn-{:03}", i), vec![1, 2, 3])
                .await
                .unwrap();
        }

        let stats = manager.stats();
        assert_eq!(stats.total_changes, 5);
        assert_eq!(stats.total_bytes_replicated, 15);
    }

    #[tokio::test]
    async fn test_remove_replica() {
        let config = ReplicationConfig::default();
        let mut manager = ReplicationManager::new("node1".to_string(), config);

        manager
            .add_replica("replica1".to_string(), "http://replica1:8080".to_string())
            .await
            .unwrap();

        assert_eq!(manager.replica_count(), 1);

        manager.remove_replica("replica1").await.unwrap();
        assert_eq!(manager.replica_count(), 0);
    }
}