ipfrs-transport 0.2.0

Transport protocols and zero-copy data exchange for IPFRS distributed system
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
//! Network partition detection and handling
//!
//! Provides mechanisms to:
//! - Detect network partitions
//! - Queue requests during partitions
//! - Automatically recover when partition heals
//! - Monitor peer health and connectivity

use dashmap::DashMap;
use parking_lot::RwLock;
use std::collections::VecDeque;
use std::net::SocketAddr;
use std::sync::Arc;
use std::time::{Duration, Instant};
use thiserror::Error;
use tokio::sync::watch;
use tracing::{debug, info, warn};

/// Partition detection error
#[derive(Error, Debug)]
pub enum PartitionError {
    #[error("Network partition detected")]
    PartitionDetected,

    #[error("Peer unreachable: {0}")]
    PeerUnreachable(String),

    #[error("Queue full: cannot accept more requests")]
    QueueFull,

    #[error("Recovery timeout")]
    RecoveryTimeout,
}

/// Network partition state
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum PartitionState {
    /// Network is healthy
    #[default]
    Healthy,
    /// Partition suspected (some failures)
    Suspected,
    /// Partition confirmed
    Partitioned,
    /// Recovering from partition
    Recovering,
}

/// Partition detection configuration
#[derive(Debug, Clone)]
pub struct PartitionConfig {
    /// Number of consecutive failures to trigger suspicion
    pub failure_threshold: usize,
    /// Time window for failure counting
    pub failure_window: Duration,
    /// Probe interval when partition suspected
    pub probe_interval: Duration,
    /// Maximum queued requests during partition
    pub max_queued_requests: usize,
    /// Recovery probe count before declaring healthy
    pub recovery_probe_count: usize,
    /// Peer timeout duration
    pub peer_timeout: Duration,
}

impl Default for PartitionConfig {
    fn default() -> Self {
        Self {
            failure_threshold: 3,
            failure_window: Duration::from_secs(10),
            probe_interval: Duration::from_secs(5),
            max_queued_requests: 1000,
            recovery_probe_count: 3,
            peer_timeout: Duration::from_secs(30),
        }
    }
}

/// Partition statistics
#[derive(Debug, Clone, Default)]
pub struct PartitionStats {
    /// Total partitions detected
    pub partitions_detected: u64,
    /// Total recoveries
    pub recoveries: u64,
    /// Current queued requests
    pub queued_requests: usize,
    /// Requests dropped due to queue full
    pub dropped_requests: u64,
    /// Average partition duration
    pub avg_partition_duration: Option<Duration>,
    /// Current state
    pub state: PartitionState,
}

/// Queued request during partition
#[derive(Debug, Clone)]
#[allow(dead_code)]
struct QueuedRequest {
    peer: SocketAddr,
    data: Vec<u8>,
    queued_at: Instant,
}

/// Peer health information
#[derive(Debug, Clone)]
struct PeerHealth {
    /// Last successful contact
    last_success: Option<Instant>,
    /// Last failure
    last_failure: Option<Instant>,
    /// Recent failure count
    failure_count: usize,
    /// Failure timestamps in window
    failures: VecDeque<Instant>,
}

impl PeerHealth {
    fn new() -> Self {
        Self {
            last_success: None,
            last_failure: None,
            failure_count: 0,
            failures: VecDeque::new(),
        }
    }

    /// Record a successful contact
    fn record_success(&mut self) {
        self.last_success = Some(Instant::now());
        self.failure_count = 0;
        self.failures.clear();
    }

    /// Record a failure
    fn record_failure(&mut self, window: Duration) {
        let now = Instant::now();
        self.last_failure = Some(now);
        self.failures.push_back(now);

        // Clean old failures outside window
        while let Some(&first) = self.failures.front() {
            if now.duration_since(first) > window {
                self.failures.pop_front();
            } else {
                break;
            }
        }

        self.failure_count = self.failures.len();
    }

    /// Check if peer is unhealthy
    fn is_unhealthy(&self, threshold: usize, timeout: Duration) -> bool {
        // Too many recent failures
        if self.failure_count >= threshold {
            return true;
        }

        // No recent success and timeout exceeded
        if let Some(last_success) = self.last_success {
            if last_success.elapsed() > timeout {
                return true;
            }
        } else if let Some(last_failure) = self.last_failure {
            if last_failure.elapsed() > timeout {
                return true;
            }
        }

        false
    }
}

/// Network partition detector and handler
pub struct PartitionDetector {
    config: PartitionConfig,
    state: Arc<RwLock<PartitionState>>,
    peer_health: Arc<DashMap<SocketAddr, PeerHealth>>,
    queued_requests: Arc<RwLock<VecDeque<QueuedRequest>>>,
    stats: Arc<RwLock<PartitionStats>>,
    state_tx: watch::Sender<PartitionState>,
    state_rx: watch::Receiver<PartitionState>,
    partition_start: Arc<RwLock<Option<Instant>>>,
}

impl PartitionDetector {
    /// Create a new partition detector
    pub fn new(config: PartitionConfig) -> Self {
        let (state_tx, state_rx) = watch::channel(PartitionState::Healthy);

        Self {
            config,
            state: Arc::new(RwLock::new(PartitionState::Healthy)),
            peer_health: Arc::new(DashMap::new()),
            queued_requests: Arc::new(RwLock::new(VecDeque::new())),
            stats: Arc::new(RwLock::new(PartitionStats::default())),
            state_tx,
            state_rx,
            partition_start: Arc::new(RwLock::new(None)),
        }
    }

    /// Record a successful peer interaction
    pub fn record_success(&self, peer: &SocketAddr) {
        {
            let mut health = self
                .peer_health
                .entry(*peer)
                .or_insert_with(PeerHealth::new);
            health.record_success();
        } // Release DashMap entry guard here

        // Check if we should transition to healthy
        if *self.state.read() != PartitionState::Healthy {
            self.check_recovery();
        }
    }

    /// Record a peer failure
    pub fn record_failure(&self, peer: &SocketAddr) {
        {
            let mut health = self
                .peer_health
                .entry(*peer)
                .or_insert_with(PeerHealth::new);
            health.record_failure(self.config.failure_window);

            debug!("Peer {} failure count: {}", peer, health.failure_count);
        } // Release DashMap entry guard here

        // Check if we should transition to partitioned state
        self.check_partition();
    }

    /// Check if network partition should be declared
    fn check_partition(&self) {
        let unhealthy_count = self
            .peer_health
            .iter()
            .filter(|entry| {
                entry
                    .value()
                    .is_unhealthy(self.config.failure_threshold, self.config.peer_timeout)
            })
            .count();

        let total_peers = self.peer_health.len();

        // If majority of peers are unhealthy, declare partition
        if total_peers > 0 && unhealthy_count * 2 > total_peers {
            let current_state = *self.state.read();

            if current_state == PartitionState::Healthy {
                self.transition_to_suspected();
            } else if current_state == PartitionState::Suspected {
                self.transition_to_partitioned();
            }
        }
    }

    /// Check if network has recovered
    fn check_recovery(&self) {
        let healthy_count = self
            .peer_health
            .iter()
            .filter(|entry| {
                !entry
                    .value()
                    .is_unhealthy(self.config.failure_threshold, self.config.peer_timeout)
            })
            .count();

        let total_peers = self.peer_health.len();

        // If majority of peers are healthy, start recovery
        if total_peers > 0 && healthy_count * 2 > total_peers {
            let current_state = *self.state.read();

            if current_state == PartitionState::Partitioned {
                self.transition_to_recovering();
            } else if current_state == PartitionState::Recovering {
                self.transition_to_healthy();
            }
        }
    }

    /// Transition to suspected state
    fn transition_to_suspected(&self) {
        *self.state.write() = PartitionState::Suspected;
        let _ = self.state_tx.send(PartitionState::Suspected);
        warn!("Network partition suspected");
    }

    /// Transition to partitioned state
    fn transition_to_partitioned(&self) {
        *self.state.write() = PartitionState::Partitioned;
        *self.partition_start.write() = Some(Instant::now());
        let _ = self.state_tx.send(PartitionState::Partitioned);

        let mut stats = self.stats.write();
        stats.partitions_detected += 1;
        stats.state = PartitionState::Partitioned;

        warn!("Network partition detected - queueing requests");
    }

    /// Transition to recovering state
    fn transition_to_recovering(&self) {
        *self.state.write() = PartitionState::Recovering;
        let _ = self.state_tx.send(PartitionState::Recovering);
        info!("Network partition recovering");
    }

    /// Transition to healthy state
    fn transition_to_healthy(&self) {
        *self.state.write() = PartitionState::Healthy;
        let _ = self.state_tx.send(PartitionState::Healthy);

        // Update partition duration stats
        if let Some(start) = *self.partition_start.read() {
            let duration = start.elapsed();
            let mut stats = self.stats.write();

            stats.avg_partition_duration = Some(
                stats
                    .avg_partition_duration
                    .map(|avg| (avg + duration) / 2)
                    .unwrap_or(duration),
            );
            stats.recoveries += 1;
            stats.state = PartitionState::Healthy;
        }

        *self.partition_start.write() = None;

        info!("Network partition recovered - processing queued requests");

        // Process queued requests
        self.flush_queue();
    }

    /// Queue a request during partition
    pub fn queue_request(&self, peer: SocketAddr, data: Vec<u8>) -> Result<(), PartitionError> {
        let mut queue = self.queued_requests.write();

        if queue.len() >= self.config.max_queued_requests {
            self.stats.write().dropped_requests += 1;
            return Err(PartitionError::QueueFull);
        }

        queue.push_back(QueuedRequest {
            peer,
            data,
            queued_at: Instant::now(),
        });

        self.stats.write().queued_requests = queue.len();

        Ok(())
    }

    /// Flush queued requests
    fn flush_queue(&self) {
        let requests: Vec<_> = {
            let mut queue = self.queued_requests.write();
            queue.drain(..).collect()
        };

        info!("Flushing {} queued requests", requests.len());

        // Requests would be processed here by the caller
        self.stats.write().queued_requests = 0;
    }

    /// Get queued requests for processing
    pub fn drain_queue(&self) -> Vec<(SocketAddr, Vec<u8>)> {
        let requests: Vec<_> = {
            let mut queue = self.queued_requests.write();
            queue.drain(..).collect()
        };

        self.stats.write().queued_requests = 0;

        requests
            .into_iter()
            .map(|req| (req.peer, req.data))
            .collect()
    }

    /// Get current partition state
    pub fn state(&self) -> PartitionState {
        *self.state.read()
    }

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

    /// Wait for state change
    pub async fn wait_state_change(&self) -> PartitionState {
        let mut rx = self.state_rx.clone();
        let _ = rx.changed().await;
        let state = *rx.borrow();
        state
    }

    /// Check if a specific peer is reachable
    pub fn is_peer_reachable(&self, peer: &SocketAddr) -> bool {
        if let Some(health) = self.peer_health.get(peer) {
            !health.is_unhealthy(self.config.failure_threshold, self.config.peer_timeout)
        } else {
            true // Unknown peers are assumed reachable
        }
    }

    /// Get unhealthy peers
    pub fn unhealthy_peers(&self) -> Vec<SocketAddr> {
        self.peer_health
            .iter()
            .filter(|entry| {
                entry
                    .value()
                    .is_unhealthy(self.config.failure_threshold, self.config.peer_timeout)
            })
            .map(|entry| *entry.key())
            .collect()
    }

    /// Clear peer health data
    pub fn clear_peer_health(&self) {
        self.peer_health.clear();
        info!("Cleared peer health data");
    }
}

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

    #[test]
    fn test_peer_health() {
        let mut health = PeerHealth::new();
        let window = Duration::from_secs(10);

        // Record some failures
        health.record_failure(window);
        assert_eq!(health.failure_count, 1);

        health.record_failure(window);
        assert_eq!(health.failure_count, 2);

        // Success should reset
        health.record_success();
        assert_eq!(health.failure_count, 0);
    }

    #[test]
    fn test_partition_detection() {
        let config = PartitionConfig {
            failure_threshold: 2,
            ..Default::default()
        };

        let detector = PartitionDetector::new(config);
        let peer: SocketAddr = "127.0.0.1:8080".parse().expect("test: valid socket addr");

        assert_eq!(detector.state(), PartitionState::Healthy);

        // Record failures
        detector.record_failure(&peer);
        detector.record_failure(&peer);
        detector.record_failure(&peer);

        // Should transition to suspected or partitioned
        let state = detector.state();
        assert!(state == PartitionState::Suspected || state == PartitionState::Partitioned);
    }

    #[test]
    fn test_queue_request() {
        let detector = PartitionDetector::new(PartitionConfig::default());
        let peer: SocketAddr = "127.0.0.1:8080".parse().expect("test: valid socket addr");

        let result = detector.queue_request(peer, vec![1, 2, 3]);
        assert!(result.is_ok());

        let stats = detector.stats();
        assert_eq!(stats.queued_requests, 1);
    }

    #[test]
    fn test_queue_full() {
        let config = PartitionConfig {
            max_queued_requests: 2,
            ..Default::default()
        };

        let detector = PartitionDetector::new(config);
        let peer: SocketAddr = "127.0.0.1:8080".parse().expect("test: valid socket addr");

        detector
            .queue_request(peer, vec![1])
            .expect("test: queue request");
        detector
            .queue_request(peer, vec![2])
            .expect("test: queue request");

        // Third request should fail
        let result = detector.queue_request(peer, vec![3]);
        assert!(result.is_err());
    }

    #[test]
    fn test_drain_queue() {
        let detector = PartitionDetector::new(PartitionConfig::default());
        let peer: SocketAddr = "127.0.0.1:8080".parse().expect("test: valid socket addr");

        detector
            .queue_request(peer, vec![1, 2, 3])
            .expect("test: queue request");
        detector
            .queue_request(peer, vec![4, 5, 6])
            .expect("test: queue request");

        let drained = detector.drain_queue();
        assert_eq!(drained.len(), 2);
        assert_eq!(detector.stats().queued_requests, 0);
    }

    #[tokio::test]
    async fn test_state_transitions() {
        let config = PartitionConfig {
            failure_threshold: 1,
            ..Default::default()
        };

        let detector = PartitionDetector::new(config);
        let peer: SocketAddr = "127.0.0.1:8080".parse().expect("test: valid socket addr");

        // Start healthy
        assert_eq!(detector.state(), PartitionState::Healthy);

        // Cause partition
        detector.record_failure(&peer);

        // Should transition
        assert!(detector.state() != PartitionState::Healthy);

        // Recover
        detector.record_success(&peer);

        // May still be recovering or might still be in suspected/partitioned state
        // since we only have one peer and one success after one failure
        let state = detector.state();
        assert!(
            state == PartitionState::Recovering
                || state == PartitionState::Healthy
                || state == PartitionState::Suspected
                || state == PartitionState::Partitioned,
            "Expected one of the valid states, got: {:?}",
            state
        );
    }
}