kaccy-core 0.2.0

Core business logic for Kaccy Protocol - batching, fee optimization, and transaction management
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
//! Database Read Replicas
//!
//! This module provides read replica management for database scaling.
//! Read replicas allow read queries to be distributed across multiple database
//! instances, improving read performance and availability.
//!
//! # Features
//!
//! - Master-slave replication tracking
//! - Read query routing to replicas
//! - Replication lag monitoring
//! - Automatic failover mechanisms
//!
//! # Examples
//!
//! ```
//! use kaccy_core::utils::db_replicas::{ReplicaManager, ReplicaConfig};
//!
//! let config = ReplicaConfig::default();
//! let mut manager = ReplicaManager::new(
//!     "postgresql://localhost:5432/master",
//!     config
//! );
//!
//! manager.add_replica("replica_1", "postgresql://localhost:5433/db");
//! manager.add_replica("replica_2", "postgresql://localhost:5434/db");
//!
//! // Route a read query
//! let replica_url = manager.get_replica_for_read();
//! ```

use crate::{CoreError as Error, Result};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::Instant;

/// Load balancing strategy for read replicas
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum LoadBalanceStrategy {
    /// Round-robin selection
    RoundRobin,
    /// Random selection
    Random,
    /// Least connections
    LeastConnections,
    /// Least replication lag
    LeastLag,
}

/// Replica configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReplicaConfig {
    /// Maximum acceptable replication lag (milliseconds)
    pub max_replication_lag_ms: u64,
    /// Health check interval (seconds)
    pub health_check_interval_secs: u64,
    /// Load balancing strategy
    pub load_balance_strategy: LoadBalanceStrategy,
    /// Automatic failover enabled
    pub auto_failover: bool,
}

impl Default for ReplicaConfig {
    fn default() -> Self {
        Self {
            max_replication_lag_ms: 1000, // 1 second
            health_check_interval_secs: 30,
            load_balance_strategy: LoadBalanceStrategy::LeastLag,
            auto_failover: true,
        }
    }
}

/// Replica information
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Replica {
    /// Replica identifier
    pub id: String,
    /// Database connection string
    pub connection_string: String,
    /// Is replica currently available?
    pub is_available: bool,
    /// Replication lag in milliseconds
    pub replication_lag_ms: u64,
    /// Number of active connections
    pub active_connections: usize,
    /// Last health check time (not serialized)
    #[serde(skip)]
    pub last_health_check: Option<Instant>,
    /// Weight for load balancing (0.0 to 1.0)
    pub weight: f64,
}

impl Replica {
    /// Create a new replica
    pub fn new(id: String, connection_string: String) -> Self {
        Self {
            id,
            connection_string,
            is_available: true,
            replication_lag_ms: 0,
            active_connections: 0,
            last_health_check: None,
            weight: 1.0,
        }
    }

    /// Check if replica is healthy
    pub fn is_healthy(&self, max_lag_ms: u64) -> bool {
        self.is_available && self.replication_lag_ms <= max_lag_ms
    }
}

/// Replica manager
pub struct ReplicaManager {
    /// Master database connection string
    master_connection: String,
    /// Configuration
    config: ReplicaConfig,
    /// Read replicas
    replicas: HashMap<String, Replica>,
    /// Round-robin index
    round_robin_index: usize,
}

impl ReplicaManager {
    /// Create a new replica manager
    pub fn new(master_connection: &str, config: ReplicaConfig) -> Self {
        Self {
            master_connection: master_connection.to_string(),
            config,
            replicas: HashMap::new(),
            round_robin_index: 0,
        }
    }

    /// Add a read replica
    pub fn add_replica(&mut self, id: &str, connection_string: &str) -> Result<()> {
        let replica = Replica::new(id.to_string(), connection_string.to_string());
        self.replicas.insert(id.to_string(), replica);
        Ok(())
    }

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

    /// Get connection string for read query
    pub fn get_replica_for_read(&mut self) -> String {
        let max_lag = self.config.max_replication_lag_ms;
        let strategy = self.config.load_balance_strategy;

        let healthy_replicas: Vec<_> = self
            .replicas
            .values()
            .filter(|r| r.is_healthy(max_lag))
            .cloned()
            .collect();

        if healthy_replicas.is_empty() {
            // Fall back to master if no replicas available
            return self.master_connection.clone();
        }

        match strategy {
            LoadBalanceStrategy::RoundRobin => self.get_replica_round_robin(&healthy_replicas),
            LoadBalanceStrategy::Random => self.get_replica_random(&healthy_replicas),
            LoadBalanceStrategy::LeastConnections => {
                self.get_replica_least_connections(&healthy_replicas)
            }
            LoadBalanceStrategy::LeastLag => self.get_replica_least_lag(&healthy_replicas),
        }
    }

    /// Get connection string for write query (always master)
    pub fn get_master_for_write(&self) -> String {
        self.master_connection.clone()
    }

    /// Update replication lag for a replica
    pub fn update_replication_lag(&mut self, replica_id: &str, lag_ms: u64) -> Result<()> {
        if let Some(replica) = self.replicas.get_mut(replica_id) {
            replica.replication_lag_ms = lag_ms;
            replica.last_health_check = Some(Instant::now());
            Ok(())
        } else {
            Err(Error::Validation(format!(
                "Replica {} not found",
                replica_id
            )))
        }
    }

    /// Mark replica as available/unavailable
    pub fn set_replica_availability(&mut self, replica_id: &str, available: bool) -> Result<()> {
        if let Some(replica) = self.replicas.get_mut(replica_id) {
            replica.is_available = available;
            Ok(())
        } else {
            Err(Error::Validation(format!(
                "Replica {} not found",
                replica_id
            )))
        }
    }

    /// Get replica statistics
    pub fn get_replica_stats(&self) -> Vec<ReplicaStats> {
        self.replicas
            .values()
            .map(|r| ReplicaStats {
                replica_id: r.id.clone(),
                is_available: r.is_available,
                replication_lag_ms: r.replication_lag_ms,
                active_connections: r.active_connections,
                is_healthy: r.is_healthy(self.config.max_replication_lag_ms),
            })
            .collect()
    }

    /// Perform health check on all replicas
    pub fn health_check(&mut self) -> HealthCheckReport {
        let total = self.replicas.len();
        let mut healthy = 0;
        let mut lagging = 0;
        let mut unavailable = 0;

        for replica in self.replicas.values() {
            if !replica.is_available {
                unavailable += 1;
            } else if replica.replication_lag_ms > self.config.max_replication_lag_ms {
                lagging += 1;
            } else {
                healthy += 1;
            }
        }

        HealthCheckReport {
            total_replicas: total,
            healthy_replicas: healthy,
            lagging_replicas: lagging,
            unavailable_replicas: unavailable,
        }
    }

    /// Initiate failover to a replica (promote replica to master)
    pub fn failover_to_replica(&mut self, replica_id: &str) -> Result<FailoverResult> {
        if !self.replicas.contains_key(replica_id) {
            return Err(Error::Validation(format!(
                "Replica {} not found",
                replica_id
            )));
        }

        let replica = self.replicas.get(replica_id).unwrap();
        if !replica.is_available {
            return Err(Error::Validation(format!(
                "Replica {} is not available",
                replica_id
            )));
        }

        let old_master = self.master_connection.clone();
        let new_master = replica.connection_string.clone();

        // Update master connection
        self.master_connection = new_master.clone();

        // Remove promoted replica from replica pool
        self.replicas.remove(replica_id);

        Ok(FailoverResult {
            old_master,
            new_master,
            promoted_replica_id: replica_id.to_string(),
        })
    }

    /// Get replica using round-robin strategy
    fn get_replica_round_robin(&mut self, replicas: &[Replica]) -> String {
        let replica = &replicas[self.round_robin_index % replicas.len()];
        self.round_robin_index = (self.round_robin_index + 1) % replicas.len();
        replica.connection_string.clone()
    }

    /// Get replica using random strategy
    fn get_replica_random(&self, replicas: &[Replica]) -> String {
        use std::collections::hash_map::RandomState;
        use std::hash::BuildHasher;

        let random_state = RandomState::new();
        let index = (random_state.hash_one(Instant::now()) as usize) % replicas.len();

        replicas[index].connection_string.clone()
    }

    /// Get replica with least connections
    fn get_replica_least_connections(&self, replicas: &[Replica]) -> String {
        replicas
            .iter()
            .min_by_key(|r| r.active_connections)
            .map(|r| r.connection_string.clone())
            .unwrap_or_else(|| self.master_connection.clone())
    }

    /// Get replica with least replication lag
    fn get_replica_least_lag(&self, replicas: &[Replica]) -> String {
        replicas
            .iter()
            .min_by_key(|r| r.replication_lag_ms)
            .map(|r| r.connection_string.clone())
            .unwrap_or_else(|| self.master_connection.clone())
    }
}

/// Replica statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ReplicaStats {
    /// Replica identifier
    pub replica_id: String,
    /// Is replica available?
    pub is_available: bool,
    /// Replication lag in milliseconds
    pub replication_lag_ms: u64,
    /// Number of active connections
    pub active_connections: usize,
    /// Is replica healthy?
    pub is_healthy: bool,
}

/// Health check report
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HealthCheckReport {
    /// Total number of replicas
    pub total_replicas: usize,
    /// Number of healthy replicas
    pub healthy_replicas: usize,
    /// Number of lagging replicas
    pub lagging_replicas: usize,
    /// Number of unavailable replicas
    pub unavailable_replicas: usize,
}

impl HealthCheckReport {
    /// Get health percentage
    pub fn health_percentage(&self) -> f64 {
        if self.total_replicas == 0 {
            100.0
        } else {
            (self.healthy_replicas as f64 / self.total_replicas as f64) * 100.0
        }
    }
}

/// Failover result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FailoverResult {
    /// Old master connection string
    pub old_master: String,
    /// New master connection string
    pub new_master: String,
    /// ID of the promoted replica
    pub promoted_replica_id: String,
}

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

    #[test]
    fn test_replica_manager_creation() {
        let config = ReplicaConfig::default();
        let manager = ReplicaManager::new("postgresql://localhost/master", config);
        assert_eq!(manager.replicas.len(), 0);
    }

    #[test]
    fn test_add_replica() {
        let config = ReplicaConfig::default();
        let mut manager = ReplicaManager::new("postgresql://localhost/master", config);

        assert!(
            manager
                .add_replica("replica_1", "postgresql://localhost/replica_1")
                .is_ok()
        );
        assert_eq!(manager.replicas.len(), 1);
    }

    #[test]
    fn test_remove_replica() {
        let config = ReplicaConfig::default();
        let mut manager = ReplicaManager::new("postgresql://localhost/master", config);

        manager
            .add_replica("replica_1", "postgresql://localhost/replica_1")
            .unwrap();
        assert_eq!(manager.replicas.len(), 1);

        assert!(manager.remove_replica("replica_1").is_ok());
        assert_eq!(manager.replicas.len(), 0);
    }

    #[test]
    fn test_get_replica_for_read() {
        let config = ReplicaConfig::default();
        let mut manager = ReplicaManager::new("postgresql://localhost/master", config);

        manager
            .add_replica("replica_1", "postgresql://localhost/replica_1")
            .unwrap();

        let conn = manager.get_replica_for_read();
        assert!(conn.contains("postgresql://"));
    }

    #[test]
    fn test_get_master_for_write() {
        let config = ReplicaConfig::default();
        let manager = ReplicaManager::new("postgresql://localhost/master", config);

        let conn = manager.get_master_for_write();
        assert_eq!(conn, "postgresql://localhost/master");
    }

    #[test]
    fn test_update_replication_lag() {
        let config = ReplicaConfig::default();
        let mut manager = ReplicaManager::new("postgresql://localhost/master", config);

        manager
            .add_replica("replica_1", "postgresql://localhost/replica_1")
            .unwrap();

        assert!(manager.update_replication_lag("replica_1", 500).is_ok());

        let stats = manager.get_replica_stats();
        assert_eq!(stats[0].replication_lag_ms, 500);
    }

    #[test]
    fn test_health_check() {
        let config = ReplicaConfig::default();
        let mut manager = ReplicaManager::new("postgresql://localhost/master", config);

        manager
            .add_replica("replica_1", "postgresql://localhost/replica_1")
            .unwrap();
        manager
            .add_replica("replica_2", "postgresql://localhost/replica_2")
            .unwrap();

        let report = manager.health_check();
        assert_eq!(report.total_replicas, 2);
        assert_eq!(report.healthy_replicas, 2);
    }

    #[test]
    fn test_failover() {
        let config = ReplicaConfig::default();
        let mut manager = ReplicaManager::new("postgresql://localhost/master", config);

        manager
            .add_replica("replica_1", "postgresql://localhost/replica_1")
            .unwrap();

        let result = manager.failover_to_replica("replica_1").unwrap();
        assert_eq!(result.old_master, "postgresql://localhost/master");
        assert_eq!(result.new_master, "postgresql://localhost/replica_1");
        assert_eq!(manager.replicas.len(), 0); // Promoted replica removed from pool
    }

    #[test]
    fn test_health_percentage() {
        let report = HealthCheckReport {
            total_replicas: 4,
            healthy_replicas: 3,
            lagging_replicas: 1,
            unavailable_replicas: 0,
        };
        assert_eq!(report.health_percentage(), 75.0);
    }
}