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
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
//! Reinforcement Learning-based Consensus Optimizer
//!
//! This module implements RL algorithms to dynamically optimize Raft consensus parameters
//! based on observed cluster performance metrics using SciRS2-Core integration.
//!
//! # Features
//!
//! - **Q-Learning** for parameter optimization
//! - **Policy Gradient** methods for continuous parameter spaces
//! - **Multi-Armed Bandit** for exploration/exploitation balance
//! - **Adaptive parameter tuning** based on cluster state
//! - **Performance reward modeling** using throughput, latency, and consistency metrics
//!
//! # SciRS2-Core Integration
//!
//! This module leverages the full power of scirs2-core:
//! - `scirs2_core::ndarray_ext` for state-action matrices and Q-tables
//! - `scirs2_core::random` for epsilon-greedy exploration
//! - `scirs2_core::stats` for reward smoothing and normalization
//! - `scirs2_core::profiling` for performance tracking
//! - `scirs2_core::metrics` for learning metrics

use anyhow::{anyhow, Result};
use scirs2_core::metrics::MetricsRegistry;
use scirs2_core::ndarray_ext::Array1;
use scirs2_core::profiling::Profiler;
use scirs2_core::random::{rngs::StdRng, Random};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use tracing::{debug, info};

/// Consensus parameters that can be optimized
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum ConsensusParameter {
    /// Election timeout in milliseconds (150-300ms typical)
    ElectionTimeout,
    /// Heartbeat interval in milliseconds (50-150ms typical)
    HeartbeatInterval,
    /// Log batch size for replication (10-1000 entries typical)
    LogBatchSize,
    /// Snapshot threshold in log entries (1000-10000 typical)
    SnapshotThreshold,
    /// Maximum in-flight append entries (1-100 typical)
    MaxInflightAppends,
}

impl ConsensusParameter {
    /// Get the valid range for this parameter
    pub fn range(&self) -> (f64, f64) {
        match self {
            ConsensusParameter::ElectionTimeout => (150.0, 300.0),
            ConsensusParameter::HeartbeatInterval => (50.0, 150.0),
            ConsensusParameter::LogBatchSize => (10.0, 1000.0),
            ConsensusParameter::SnapshotThreshold => (1000.0, 10000.0),
            ConsensusParameter::MaxInflightAppends => (1.0, 100.0),
        }
    }

    /// Discretize continuous value into action index
    pub fn discretize(&self, value: f64, num_bins: usize) -> usize {
        let (min_val, max_val) = self.range();
        let bin_size = (max_val - min_val) / num_bins as f64;
        let normalized = (value - min_val).max(0.0).min(max_val - min_val);
        (normalized / bin_size).floor().min((num_bins - 1) as f64) as usize
    }

    /// Convert action index back to continuous value
    pub fn from_action(&self, action_idx: usize, num_bins: usize) -> f64 {
        let (min_val, max_val) = self.range();
        let bin_size = (max_val - min_val) / num_bins as f64;
        min_val + (action_idx as f64 + 0.5) * bin_size
    }
}

/// Cluster state representation for RL
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ClusterState {
    /// Number of nodes in the cluster
    pub node_count: usize,
    /// Average throughput (ops/sec)
    pub avg_throughput: f64,
    /// Average latency (ms)
    pub avg_latency: f64,
    /// Leader election frequency (elections/hour)
    pub election_frequency: f64,
    /// Replication lag (ms)
    pub replication_lag: f64,
    /// CPU utilization (0.0-1.0)
    pub cpu_utilization: f64,
    /// Network congestion indicator (0.0-1.0)
    pub network_congestion: f64,
}

impl ClusterState {
    /// Convert state to feature vector for RL agent
    pub fn to_features(&self) -> Array1<f64> {
        Array1::from_vec(vec![
            self.node_count as f64 / 100.0, // Normalize to [0, 1]
            self.avg_throughput / 10000.0,  // Normalize assuming max 10K ops/sec
            self.avg_latency / 1000.0,      // Normalize assuming max 1s latency
            self.election_frequency / 10.0, // Normalize assuming max 10 elections/hour
            self.replication_lag / 1000.0,  // Normalize assuming max 1s lag
            self.cpu_utilization,           // Already in [0, 1]
            self.network_congestion,        // Already in [0, 1]
        ])
    }

    /// Discretize state into a state index for Q-learning
    pub fn discretize(&self, num_bins_per_feature: usize) -> usize {
        let features = self.to_features();
        let mut state_idx = 0;
        let mut multiplier = 1;

        for &feature in features.iter() {
            let bin = (feature * num_bins_per_feature as f64)
                .floor()
                .min((num_bins_per_feature - 1) as f64) as usize;
            state_idx += bin * multiplier;
            multiplier *= num_bins_per_feature;
        }

        state_idx
    }
}

/// Performance reward components
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PerformanceReward {
    /// Throughput component (higher is better)
    pub throughput_score: f64,
    /// Latency component (lower is better)
    pub latency_score: f64,
    /// Consistency component (lower election frequency is better)
    pub consistency_score: f64,
    /// Resource efficiency component
    pub efficiency_score: f64,
    /// Total weighted reward
    pub total_reward: f64,
}

impl PerformanceReward {
    /// Calculate reward from cluster state
    pub fn from_state(state: &ClusterState, weights: &RewardWeights) -> Self {
        // Normalize and invert latency (lower is better)
        let latency_score = weights.latency_weight * (1.0 - (state.avg_latency / 1000.0).min(1.0));

        // Normalize throughput (higher is better)
        let throughput_score =
            weights.throughput_weight * (state.avg_throughput / 10000.0).min(1.0);

        // Consistency: penalize frequent elections
        let consistency_score =
            weights.consistency_weight * (1.0 - (state.election_frequency / 10.0).min(1.0));

        // Efficiency: reward low resource usage with good performance
        let efficiency_score = weights.efficiency_weight
            * (1.0 - state.cpu_utilization)
            * (state.avg_throughput / 10000.0).min(1.0);

        let total_reward = throughput_score + latency_score + consistency_score + efficiency_score;

        Self {
            throughput_score,
            latency_score,
            consistency_score,
            efficiency_score,
            total_reward,
        }
    }
}

/// Reward weights for multi-objective optimization
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RewardWeights {
    pub throughput_weight: f64,
    pub latency_weight: f64,
    pub consistency_weight: f64,
    pub efficiency_weight: f64,
}

impl Default for RewardWeights {
    fn default() -> Self {
        Self {
            throughput_weight: 0.3,
            latency_weight: 0.3,
            consistency_weight: 0.25,
            efficiency_weight: 0.15,
        }
    }
}

/// RL algorithm type
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RLAlgorithm {
    /// Q-Learning for discrete action spaces
    QLearning,
    /// SARSA (on-policy TD control)
    SARSA,
    /// Policy Gradient (REINFORCE)
    PolicyGradient,
    /// Actor-Critic
    ActorCritic,
}

/// Configuration for RL-based consensus optimizer
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RLOptimizerConfig {
    /// RL algorithm to use
    pub algorithm: RLAlgorithm,
    /// Learning rate (alpha) for Q-learning/SARSA
    pub learning_rate: f64,
    /// Discount factor (gamma) for future rewards
    pub discount_factor: f64,
    /// Exploration rate (epsilon) for epsilon-greedy
    pub epsilon: f64,
    /// Epsilon decay rate per episode
    pub epsilon_decay: f64,
    /// Minimum epsilon value
    pub min_epsilon: f64,
    /// Number of discrete bins per state feature
    pub num_state_bins: usize,
    /// Number of discrete actions per parameter
    pub num_action_bins: usize,
    /// Reward weights for multi-objective optimization
    pub reward_weights: RewardWeights,
    /// Enable experience replay
    pub use_experience_replay: bool,
    /// Experience replay buffer size
    pub replay_buffer_size: usize,
    /// Batch size for experience replay
    pub replay_batch_size: usize,
}

impl Default for RLOptimizerConfig {
    fn default() -> Self {
        Self {
            algorithm: RLAlgorithm::QLearning,
            learning_rate: 0.1,
            discount_factor: 0.95,
            epsilon: 0.3,
            epsilon_decay: 0.995,
            min_epsilon: 0.01,
            num_state_bins: 5,
            num_action_bins: 10,
            reward_weights: RewardWeights::default(),
            use_experience_replay: true,
            replay_buffer_size: 10000,
            replay_batch_size: 32,
        }
    }
}

/// Experience tuple for replay buffer
#[derive(Debug, Clone)]
struct Experience {
    state: ClusterState,
    action: HashMap<ConsensusParameter, usize>,
    reward: f64,
    next_state: ClusterState,
    #[allow(dead_code)] // Reserved for future episode termination logic
    done: bool,
}

/// Action representation as sorted vector of (parameter, value) pairs
type ActionRepr = Vec<(ConsensusParameter, usize)>;

/// Convert HashMap action to sorted vector representation for hashing
fn action_to_repr(action: &HashMap<ConsensusParameter, usize>) -> ActionRepr {
    let mut vec: Vec<_> = action.iter().map(|(&k, &v)| (k, v)).collect();
    vec.sort_by_key(|&(param, _)| param as u8);
    vec
}

/// Reinforcement Learning-based Consensus Optimizer
pub struct RLConsensusOptimizer {
    /// Configuration
    config: RLOptimizerConfig,
    /// Q-table: state -> action -> Q-value
    q_table: Arc<RwLock<HashMap<(usize, ActionRepr), f64>>>,
    /// Experience replay buffer
    replay_buffer: Arc<RwLock<Vec<Experience>>>,
    /// Current epsilon for exploration
    epsilon: Arc<RwLock<f64>>,
    /// Random number generator
    rng: Arc<RwLock<Random<StdRng>>>,
    /// Metrics registry
    #[allow(dead_code)] // Reserved for future metrics collection integration
    metrics: Arc<MetricsRegistry>,
    /// Profiler
    profiler: Arc<Profiler>,
    /// Episode counter
    episode_count: Arc<RwLock<usize>>,
    /// Total steps counter
    total_steps: Arc<RwLock<usize>>,
    /// Best reward observed
    best_reward: Arc<RwLock<f64>>,
}

impl RLConsensusOptimizer {
    /// Create a new RL-based consensus optimizer
    pub fn new(config: RLOptimizerConfig) -> Result<Self> {
        let metrics = Arc::new(MetricsRegistry::new());
        let profiler = Arc::new(Profiler::new());

        let epsilon = config.epsilon;

        Ok(Self {
            config,
            q_table: Arc::new(RwLock::new(HashMap::new())),
            replay_buffer: Arc::new(RwLock::new(Vec::new())),
            epsilon: Arc::new(RwLock::new(epsilon)),
            rng: Arc::new(RwLock::new(Random::seed(42))),
            metrics,
            profiler,
            episode_count: Arc::new(RwLock::new(0)),
            total_steps: Arc::new(RwLock::new(0)),
            best_reward: Arc::new(RwLock::new(f64::NEG_INFINITY)),
        })
    }

    /// Select action using epsilon-greedy policy
    pub async fn select_action(
        &self,
        state: &ClusterState,
    ) -> Result<HashMap<ConsensusParameter, usize>> {
        let epsilon = *self.epsilon.read().await;
        let mut rng = self.rng.write().await;

        // Epsilon-greedy exploration
        if rng.gen_range(0.0..1.0) < epsilon {
            // Explore: random action
            self.random_action(&mut rng).await
        } else {
            // Exploit: greedy action based on Q-values
            self.greedy_action(state).await
        }
    }

    /// Generate random action for exploration
    async fn random_action(
        &self,
        rng: &mut Random<StdRng>,
    ) -> Result<HashMap<ConsensusParameter, usize>> {
        let mut action = HashMap::new();
        for param in &[
            ConsensusParameter::ElectionTimeout,
            ConsensusParameter::HeartbeatInterval,
            ConsensusParameter::LogBatchSize,
            ConsensusParameter::SnapshotThreshold,
            ConsensusParameter::MaxInflightAppends,
        ] {
            let action_idx = (rng.gen_range(0.0..1.0) * self.config.num_action_bins as f64)
                .floor()
                .min((self.config.num_action_bins - 1) as f64)
                as usize;
            action.insert(*param, action_idx);
        }
        Ok(action)
    }

    /// Select greedy action based on maximum Q-value
    async fn greedy_action(
        &self,
        state: &ClusterState,
    ) -> Result<HashMap<ConsensusParameter, usize>> {
        let state_idx = state.discretize(self.config.num_state_bins);
        let q_table = self.q_table.read().await;

        // Find action with maximum Q-value for this state
        let mut best_action = None;
        let mut best_q_value = f64::NEG_INFINITY;

        // Generate all possible action combinations (simplified: sample random actions)
        let mut rng = self.rng.write().await;
        for _ in 0..100 {
            // Sample 100 random actions
            let action = self.random_action(&mut rng).await?;
            let action_repr = action_to_repr(&action);
            let q_value = *q_table.get(&(state_idx, action_repr)).unwrap_or(&0.0);

            if q_value > best_q_value {
                best_q_value = q_value;
                best_action = Some(action);
            }
        }

        best_action.ok_or_else(|| anyhow!("No action found"))
    }

    /// Update Q-table using Q-learning update rule
    pub async fn update_q_value(
        &self,
        state: &ClusterState,
        action: &HashMap<ConsensusParameter, usize>,
        reward: f64,
        next_state: &ClusterState,
    ) -> Result<()> {
        let state_idx = state.discretize(self.config.num_state_bins);
        let next_state_idx = next_state.discretize(self.config.num_state_bins);

        // Get current Q-value
        let mut q_table = self.q_table.write().await;
        let action_repr = action_to_repr(action);
        let current_q = *q_table
            .get(&(state_idx, action_repr.clone()))
            .unwrap_or(&0.0);

        // Find max Q-value for next state
        let max_next_q = self.max_q_value(&q_table, next_state_idx).await;

        // Q-learning update: Q(s,a) <- Q(s,a) + α[r + γ max Q(s',a') - Q(s,a)]
        let td_target = reward + self.config.discount_factor * max_next_q;
        let td_error = td_target - current_q;
        let new_q = current_q + self.config.learning_rate * td_error;

        q_table.insert((state_idx, action_repr), new_q);

        // Note: SciRS2-Core MetricsRegistry doesn't support dynamic histogram recording
        // Metrics would be recorded through the profiler instead

        debug!(
            "Q-learning update: Q({}, {:?}) = {:.4} (was {:.4})",
            state_idx, action, new_q, current_q
        );

        Ok(())
    }

    /// Find maximum Q-value for a given state
    async fn max_q_value(
        &self,
        q_table: &HashMap<(usize, ActionRepr), f64>,
        state_idx: usize,
    ) -> f64 {
        q_table
            .iter()
            .filter(|((s, _), _)| *s == state_idx)
            .map(|(_, &q)| q)
            .fold(0.0, f64::max)
    }

    /// Store experience in replay buffer
    pub async fn store_experience(
        &self,
        state: ClusterState,
        action: HashMap<ConsensusParameter, usize>,
        reward: f64,
        next_state: ClusterState,
        done: bool,
    ) -> Result<()> {
        if !self.config.use_experience_replay {
            return Ok(());
        }

        let mut buffer = self.replay_buffer.write().await;

        let experience = Experience {
            state,
            action,
            reward,
            next_state,
            done,
        };

        buffer.push(experience);

        // Maintain buffer size limit
        if buffer.len() > self.config.replay_buffer_size {
            buffer.remove(0);
        }

        Ok(())
    }

    /// Train from experience replay
    pub async fn replay_train(&self) -> Result<()> {
        if !self.config.use_experience_replay {
            return Ok(());
        }

        let buffer = self.replay_buffer.read().await;
        if buffer.len() < self.config.replay_batch_size {
            return Ok(()); // Not enough experiences yet
        }

        // Sample random batch
        let mut rng = self.rng.write().await;
        let mut batch_indices = Vec::new();
        for _ in 0..self.config.replay_batch_size {
            let idx = (rng.gen_range(0.0..1.0) * buffer.len() as f64).floor() as usize;
            batch_indices.push(idx);
        }

        drop(rng); // Release lock before async operations

        // Train on sampled experiences
        for idx in batch_indices {
            let exp = &buffer[idx];
            self.update_q_value(&exp.state, &exp.action, exp.reward, &exp.next_state)
                .await?;
        }

        Ok(())
    }

    /// Decay epsilon for exploration/exploitation balance
    pub async fn decay_epsilon(&self) -> Result<()> {
        let mut epsilon = self.epsilon.write().await;
        *epsilon = (*epsilon * self.config.epsilon_decay).max(self.config.min_epsilon);

        debug!("Epsilon decayed to {:.4}", *epsilon);

        Ok(())
    }

    /// Complete an episode
    pub async fn complete_episode(&self, total_reward: f64) -> Result<()> {
        let mut episode_count = self.episode_count.write().await;
        *episode_count += 1;

        // Update best reward
        let mut best_reward = self.best_reward.write().await;
        if total_reward > *best_reward {
            *best_reward = total_reward;
            info!(
                "New best reward: {:.4} (episode {})",
                total_reward, *episode_count
            );
        }

        // Decay epsilon
        self.decay_epsilon().await?;

        // Experience replay training
        self.replay_train().await?;

        info!(
            "Episode {} completed: reward={:.4}, best={:.4}",
            *episode_count, total_reward, *best_reward
        );

        Ok(())
    }

    /// Get optimized parameter values
    pub async fn get_optimized_parameters(
        &self,
        state: &ClusterState,
    ) -> Result<HashMap<ConsensusParameter, f64>> {
        let action = self.greedy_action(state).await?;

        let mut parameters = HashMap::new();
        for (param, action_idx) in action {
            let value = param.from_action(action_idx, self.config.num_action_bins);
            parameters.insert(param, value);
        }

        Ok(parameters)
    }

    /// Get performance report
    pub fn get_performance_report(&self) -> String {
        self.profiler.get_report()
    }

    /// Get current statistics
    pub async fn get_statistics(&self) -> RLStatistics {
        RLStatistics {
            episode_count: *self.episode_count.read().await,
            total_steps: *self.total_steps.read().await,
            best_reward: *self.best_reward.read().await,
            current_epsilon: *self.epsilon.read().await,
            q_table_size: self.q_table.read().await.len(),
            replay_buffer_size: self.replay_buffer.read().await.len(),
        }
    }
}

/// RL optimizer statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct RLStatistics {
    pub episode_count: usize,
    pub total_steps: usize,
    pub best_reward: f64,
    pub current_epsilon: f64,
    pub q_table_size: usize,
    pub replay_buffer_size: usize,
}

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

    #[test]
    fn test_consensus_parameter_range() {
        assert_eq!(ConsensusParameter::ElectionTimeout.range(), (150.0, 300.0));
        assert_eq!(ConsensusParameter::HeartbeatInterval.range(), (50.0, 150.0));
    }

    #[test]
    fn test_consensus_parameter_discretize() {
        let param = ConsensusParameter::ElectionTimeout;
        assert_eq!(param.discretize(150.0, 10), 0);
        assert_eq!(param.discretize(225.0, 10), 5);
        assert_eq!(param.discretize(300.0, 10), 9);
    }

    #[test]
    fn test_consensus_parameter_from_action() {
        let param = ConsensusParameter::ElectionTimeout;
        let value = param.from_action(5, 10);
        // ElectionTimeout range is (150.0, 300.0), bin_size = 15.0
        // Action 5 should give: 150.0 + (5 + 0.5) * 15.0 = 150.0 + 82.5 = 232.5
        assert!((value - 232.5).abs() < 1.0);
    }

    #[test]
    fn test_cluster_state_to_features() {
        let state = ClusterState {
            node_count: 10,
            avg_throughput: 5000.0,
            avg_latency: 50.0,
            election_frequency: 2.0,
            replication_lag: 100.0,
            cpu_utilization: 0.5,
            network_congestion: 0.3,
        };

        let features = state.to_features();
        assert_eq!(features.len(), 7);
        assert!((features[0] - 0.1).abs() < 1e-6); // node_count / 100
        assert!((features[1] - 0.5).abs() < 1e-6); // throughput / 10000
    }

    #[test]
    fn test_cluster_state_discretize() {
        let state = ClusterState {
            node_count: 10,
            avg_throughput: 5000.0,
            avg_latency: 50.0,
            election_frequency: 2.0,
            replication_lag: 100.0,
            cpu_utilization: 0.5,
            network_congestion: 0.3,
        };

        let state_idx = state.discretize(5);
        // State index should be a valid usize
        assert!(state_idx < usize::MAX);
    }

    #[test]
    fn test_performance_reward_calculation() {
        let state = ClusterState {
            node_count: 10,
            avg_throughput: 8000.0,
            avg_latency: 20.0,
            election_frequency: 1.0,
            replication_lag: 50.0,
            cpu_utilization: 0.4,
            network_congestion: 0.2,
        };

        let weights = RewardWeights::default();
        let reward = PerformanceReward::from_state(&state, &weights);

        assert!(reward.total_reward > 0.0);
        assert!(reward.throughput_score > 0.0);
        assert!(reward.latency_score > 0.0);
        assert!(reward.consistency_score > 0.0);
        assert!(reward.efficiency_score > 0.0);
    }

    #[tokio::test]
    async fn test_rl_optimizer_creation() {
        let config = RLOptimizerConfig::default();
        let optimizer = RLConsensusOptimizer::new(config);
        assert!(optimizer.is_ok());
    }

    #[tokio::test]
    async fn test_rl_optimizer_random_action() {
        let config = RLOptimizerConfig::default();
        let optimizer = RLConsensusOptimizer::new(config).unwrap();

        let mut rng = optimizer.rng.write().await;
        let action = optimizer.random_action(&mut rng).await.unwrap();

        assert_eq!(action.len(), 5); // 5 consensus parameters
        for param in &[
            ConsensusParameter::ElectionTimeout,
            ConsensusParameter::HeartbeatInterval,
            ConsensusParameter::LogBatchSize,
            ConsensusParameter::SnapshotThreshold,
            ConsensusParameter::MaxInflightAppends,
        ] {
            assert!(action.contains_key(param));
        }
    }

    #[tokio::test]
    async fn test_rl_optimizer_q_learning_update() {
        let config = RLOptimizerConfig::default();
        let optimizer = RLConsensusOptimizer::new(config).unwrap();

        let state = ClusterState {
            node_count: 10,
            avg_throughput: 5000.0,
            avg_latency: 50.0,
            election_frequency: 2.0,
            replication_lag: 100.0,
            cpu_utilization: 0.5,
            network_congestion: 0.3,
        };

        let mut action = HashMap::new();
        action.insert(ConsensusParameter::ElectionTimeout, 5);
        action.insert(ConsensusParameter::HeartbeatInterval, 3);
        action.insert(ConsensusParameter::LogBatchSize, 7);
        action.insert(ConsensusParameter::SnapshotThreshold, 4);
        action.insert(ConsensusParameter::MaxInflightAppends, 6);

        let next_state = state.clone();

        let result = optimizer
            .update_q_value(&state, &action, 1.0, &next_state)
            .await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn test_rl_optimizer_experience_replay() {
        let mut config = RLOptimizerConfig::default();
        config.use_experience_replay = true;
        config.replay_buffer_size = 100;

        let optimizer = RLConsensusOptimizer::new(config).unwrap();

        let state = ClusterState {
            node_count: 10,
            avg_throughput: 5000.0,
            avg_latency: 50.0,
            election_frequency: 2.0,
            replication_lag: 100.0,
            cpu_utilization: 0.5,
            network_congestion: 0.3,
        };

        let mut action = HashMap::new();
        action.insert(ConsensusParameter::ElectionTimeout, 5);

        optimizer
            .store_experience(state.clone(), action, 1.0, state.clone(), false)
            .await
            .unwrap();

        let buffer_size = optimizer.replay_buffer.read().await.len();
        assert_eq!(buffer_size, 1);
    }

    #[tokio::test]
    async fn test_rl_optimizer_epsilon_decay() {
        let config = RLOptimizerConfig::default();
        let optimizer = RLConsensusOptimizer::new(config).unwrap();

        let initial_epsilon = *optimizer.epsilon.read().await;
        optimizer.decay_epsilon().await.unwrap();
        let decayed_epsilon = *optimizer.epsilon.read().await;

        assert!(decayed_epsilon < initial_epsilon);
        assert!(decayed_epsilon >= optimizer.config.min_epsilon);
    }

    #[tokio::test]
    async fn test_rl_optimizer_get_statistics() {
        let config = RLOptimizerConfig::default();
        let optimizer = RLConsensusOptimizer::new(config).unwrap();

        let stats = optimizer.get_statistics().await;
        assert_eq!(stats.episode_count, 0);
        assert_eq!(stats.total_steps, 0);
        assert_eq!(stats.q_table_size, 0);
        assert_eq!(stats.replay_buffer_size, 0);
    }
}