scirs2-metrics 0.4.2

Machine Learning evaluation metrics module for SciRS2 (scirs2-metrics)
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
//! Consensus algorithms for distributed systems
//!
//! This module provides implementations of various consensus algorithms
//! including Raft, PBFT, Proof of Stake, and simple majority consensus.

pub mod coordinator;
pub mod majority;
pub mod pbft;
pub mod proof_of_stake;
pub mod raft;

use crate::error::{MetricsError, Result};
use scirs2_core::numeric::Float;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::time::{Duration, Instant, SystemTime};

// Re-export main components
pub use coordinator::*;
pub use majority::*;
pub use pbft::*;
pub use proof_of_stake::*;
pub use raft::*;

/// Consensus algorithm configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsensusConfig {
    /// Algorithm to use
    pub algorithm: ConsensusAlgorithm,

    /// Minimum number of nodes for quorum
    pub quorum_size: usize,

    /// Election timeout (milliseconds)
    pub election_timeout_ms: u64,

    /// Heartbeat interval (milliseconds)
    pub heartbeat_interval_ms: u64,

    /// Maximum entries per append
    pub max_entries_per_append: usize,

    /// Log compaction threshold
    pub log_compaction_threshold: usize,

    /// Snapshot creation interval
    pub snapshot_interval: Duration,

    /// Byzantine fault tolerance threshold
    pub byzantine_threshold: usize,

    /// Enable consensus optimization
    pub enable_optimization: bool,

    /// Consensus timeout (milliseconds)
    pub consensus_timeout_ms: u64,
}

/// Consensus algorithms supported
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum ConsensusAlgorithm {
    /// Raft consensus algorithm
    Raft,

    /// Practical Byzantine Fault Tolerance
    PBFT,

    /// Proof of Stake consensus
    ProofOfStake,

    /// Simple majority consensus
    SimpleMajority,

    /// Custom consensus algorithm
    Custom(String),
}

/// Consensus system state
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsensusSystemState {
    /// Current consensus leader
    pub current_leader: Option<String>,

    /// Active participants
    pub active_participants: HashSet<String>,

    /// Current term/epoch
    pub current_term: u64,

    /// Last consensus timestamp
    pub last_consensus: SystemTime,

    /// Consensus statistics
    pub consensus_stats: ConsensusStats,

    /// Algorithm-specific state
    pub algorithm_state: AlgorithmSpecificState,
}

/// Algorithm-specific consensus state
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum AlgorithmSpecificState {
    /// Raft-specific state
    Raft(raft::RaftState),

    /// PBFT-specific state
    PBFT(pbft::PbftState),

    /// Proof of Stake specific state
    ProofOfStake(proof_of_stake::PoSState),

    /// Simple majority state
    SimpleMajority(majority::MajorityState),

    /// Custom algorithm state
    Custom(HashMap<String, String>),
}

/// Consensus statistics
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ConsensusStats {
    /// Total consensus decisions made
    pub total_decisions: u64,

    /// Average consensus time (milliseconds)
    pub avg_consensus_time_ms: f64,

    /// Failed consensus attempts
    pub failed_attempts: u64,

    /// Leader changes
    pub leader_changes: u64,

    /// Byzantine failures detected
    pub byzantine_failures: u64,

    /// Network partitions handled
    pub network_partitions: u64,

    /// Current consensus health (0.0-1.0)
    pub health_score: f64,
}

/// Consensus proposal for distributed decisions
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsensusProposal<T> {
    /// Proposal identifier
    pub proposal_id: String,

    /// Proposer node identifier
    pub proposer: String,

    /// Proposal timestamp
    pub timestamp: SystemTime,

    /// Proposal data
    pub data: T,

    /// Proposal priority
    pub priority: u32,

    /// Required quorum size
    pub required_quorum: usize,

    /// Proposal metadata
    pub metadata: HashMap<String, String>,
}

/// Consensus decision result
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsensusDecision<T> {
    /// Decision identifier
    pub decision_id: String,

    /// Original proposal
    pub proposal: ConsensusProposal<T>,

    /// Decision outcome
    pub outcome: DecisionOutcome,

    /// Participating nodes
    pub participants: HashSet<String>,

    /// Decision timestamp
    pub timestamp: SystemTime,

    /// Consensus algorithm used
    pub algorithm: ConsensusAlgorithm,

    /// Decision metadata
    pub metadata: HashMap<String, String>,
}

/// Consensus decision outcomes
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum DecisionOutcome {
    /// Consensus reached, proposal accepted
    Accepted,

    /// Consensus reached, proposal rejected
    Rejected,

    /// Consensus failed, timeout occurred
    Timeout,

    /// Consensus failed, insufficient participants
    InsufficientQuorum,

    /// Consensus failed, Byzantine fault detected
    ByzantineFault,

    /// Consensus failed, network partition
    NetworkPartition,
}

/// Node vote in consensus
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsensusVote {
    /// Voting node identifier
    pub voter: String,

    /// Vote decision
    pub vote: VoteDecision,

    /// Vote timestamp
    pub timestamp: SystemTime,

    /// Vote signature/proof
    pub signature: Option<String>,

    /// Vote metadata
    pub metadata: HashMap<String, String>,
}

/// Vote decisions
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum VoteDecision {
    /// Vote in favor
    Accept,

    /// Vote against
    Reject,

    /// Abstain from voting
    Abstain,
}

/// Consensus manager trait for different algorithms
#[allow(async_fn_in_trait)]
pub trait ConsensusManager<T>: Send + Sync {
    /// Initialize consensus manager
    async fn initialize(&mut self) -> Result<()>;

    /// Propose a value for consensus
    async fn propose(&mut self, proposal: ConsensusProposal<T>) -> Result<String>;

    /// Vote on a proposal
    async fn vote(&mut self, proposal_id: &str, vote: ConsensusVote) -> Result<()>;

    /// Get consensus decision if available
    async fn get_decision(&self, proposal_id: &str) -> Result<Option<ConsensusDecision<T>>>;

    /// Handle node failure
    async fn handle_node_failure(&mut self, node_id: &str) -> Result<()>;

    /// Handle node recovery
    async fn handle_node_recovery(&mut self, node_id: &str) -> Result<()>;

    /// Get current consensus state
    async fn get_state(&self) -> Result<ConsensusSystemState>;

    /// Get health score
    async fn get_health_score(&self) -> Result<f64>;

    /// Shutdown consensus manager
    async fn shutdown(&mut self) -> Result<()>;
}

/// Consensus event for monitoring and debugging
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsensusEvent {
    /// Event identifier
    pub event_id: String,

    /// Event type
    pub event_type: ConsensusEventType,

    /// Event timestamp
    pub timestamp: SystemTime,

    /// Affected nodes
    pub nodes: Vec<String>,

    /// Event data
    pub data: HashMap<String, String>,
}

/// Types of consensus events
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum ConsensusEventType {
    /// New proposal submitted
    ProposalSubmitted,

    /// Vote cast by node
    VoteCast,

    /// Consensus decision reached
    DecisionReached,

    /// Leader election started
    LeaderElection,

    /// Leader change occurred
    LeaderChange,

    /// Node failure detected
    NodeFailure,

    /// Node recovery detected
    NodeRecovery,

    /// Byzantine behavior detected
    ByzantineBehavior,

    /// Network partition detected
    NetworkPartition,

    /// Network partition healed
    PartitionHealed,
}

/// Consensus performance metrics
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct ConsensusPerformanceMetrics {
    /// Average proposal processing time
    pub avg_proposal_time_ms: f64,

    /// Average voting time
    pub avg_voting_time_ms: f64,

    /// Consensus success rate
    pub success_rate: f64,

    /// Network message overhead
    pub message_overhead: f64,

    /// Memory usage (bytes)
    pub memory_usage_bytes: usize,

    /// CPU utilization (0.0-1.0)
    pub cpu_utilization: f64,
}

impl Default for ConsensusConfig {
    fn default() -> Self {
        Self {
            algorithm: ConsensusAlgorithm::Raft,
            quorum_size: 3,
            election_timeout_ms: 5000,
            heartbeat_interval_ms: 1000,
            max_entries_per_append: 100,
            log_compaction_threshold: 10000,
            snapshot_interval: Duration::from_secs(300), // 5 minutes
            byzantine_threshold: 1,
            enable_optimization: true,
            consensus_timeout_ms: 30000, // 30 seconds
        }
    }
}

impl<T> ConsensusProposal<T> {
    /// Create new consensus proposal
    pub fn new(proposal_id: String, proposer: String, data: T) -> Self {
        Self {
            proposal_id,
            proposer,
            timestamp: SystemTime::now(),
            data,
            priority: 0,
            required_quorum: 3,
            metadata: HashMap::new(),
        }
    }

    /// Set proposal priority
    pub fn with_priority(mut self, priority: u32) -> Self {
        self.priority = priority;
        self
    }

    /// Set required quorum size
    pub fn with_quorum(mut self, quorum: usize) -> Self {
        self.required_quorum = quorum;
        self
    }

    /// Add metadata
    pub fn with_metadata(mut self, key: String, value: String) -> Self {
        self.metadata.insert(key, value);
        self
    }
}

impl ConsensusVote {
    /// Create new consensus vote
    pub fn new(voter: String, vote: VoteDecision) -> Self {
        Self {
            voter,
            vote,
            timestamp: SystemTime::now(),
            signature: None,
            metadata: HashMap::new(),
        }
    }

    /// Add signature to vote
    pub fn with_signature(mut self, signature: String) -> Self {
        self.signature = Some(signature);
        self
    }

    /// Add metadata to vote
    pub fn with_metadata(mut self, key: String, value: String) -> Self {
        self.metadata.insert(key, value);
        self
    }
}

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

    #[test]
    fn test_consensus_config_default() {
        let config = ConsensusConfig::default();
        assert_eq!(config.algorithm, ConsensusAlgorithm::Raft);
        assert_eq!(config.quorum_size, 3);
        assert!(config.enable_optimization);
    }

    #[test]
    fn test_consensus_proposal() {
        let proposal = ConsensusProposal::new(
            "test_proposal".to_string(),
            "node1".to_string(),
            "test_data".to_string(),
        );

        assert_eq!(proposal.proposal_id, "test_proposal");
        assert_eq!(proposal.proposer, "node1");
        assert_eq!(proposal.priority, 0);
    }

    #[test]
    fn test_consensus_vote() {
        let vote = ConsensusVote::new("node1".to_string(), VoteDecision::Accept);
        assert_eq!(vote.voter, "node1");
        assert_eq!(vote.vote, VoteDecision::Accept);
        assert!(vote.signature.is_none());
    }

    #[test]
    fn test_consensus_stats() {
        let stats = ConsensusStats::default();
        assert_eq!(stats.total_decisions, 0);
        assert_eq!(stats.health_score, 0.0);
    }
}