ruvector-cluster 2.0.6

Distributed clustering and sharding for ruvector
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
//! DAG-based consensus protocol inspired by QuDAG
//!
//! Implements a directed acyclic graph for transaction ordering and consensus.

use chrono::{DateTime, Utc};
use dashmap::DashMap;
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet, VecDeque};
use std::sync::Arc;
use tracing::{debug, info, warn};
use uuid::Uuid;

use crate::{ClusterError, Result};

/// A vertex in the consensus DAG
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DagVertex {
    /// Unique vertex ID
    pub id: String,
    /// Node that created this vertex
    pub node_id: String,
    /// Transaction data
    pub transaction: Transaction,
    /// Parent vertices (edges in the DAG)
    pub parents: Vec<String>,
    /// Timestamp when vertex was created
    pub timestamp: DateTime<Utc>,
    /// Vector clock for causality tracking
    pub vector_clock: HashMap<String, u64>,
    /// Signature (in production, this would be cryptographic)
    pub signature: String,
}

impl DagVertex {
    /// Create a new DAG vertex
    pub fn new(
        node_id: String,
        transaction: Transaction,
        parents: Vec<String>,
        vector_clock: HashMap<String, u64>,
    ) -> Self {
        Self {
            id: Uuid::new_v4().to_string(),
            node_id,
            transaction,
            parents,
            timestamp: Utc::now(),
            vector_clock,
            signature: String::new(), // Would be computed cryptographically
        }
    }

    /// Verify the vertex signature
    pub fn verify_signature(&self) -> bool {
        // In production, verify cryptographic signature
        true
    }
}

/// A transaction in the consensus system
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Transaction {
    /// Transaction ID
    pub id: String,
    /// Transaction type
    pub tx_type: TransactionType,
    /// Transaction data
    pub data: Vec<u8>,
    /// Nonce for ordering
    pub nonce: u64,
}

/// Type of transaction
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum TransactionType {
    /// Write operation
    Write,
    /// Read operation
    Read,
    /// Delete operation
    Delete,
    /// Batch operation
    Batch,
    /// System operation
    System,
}

/// DAG-based consensus engine
pub struct DagConsensus {
    /// Node ID
    node_id: String,
    /// DAG vertices (vertex_id -> vertex)
    vertices: Arc<DashMap<String, DagVertex>>,
    /// Finalized vertices
    finalized: Arc<RwLock<HashSet<String>>>,
    /// Vector clock for this node
    vector_clock: Arc<RwLock<HashMap<String, u64>>>,
    /// Pending transactions
    pending_txs: Arc<RwLock<VecDeque<Transaction>>>,
    /// Minimum quorum size
    min_quorum_size: usize,
    /// Transaction nonce counter
    nonce_counter: Arc<RwLock<u64>>,
}

impl DagConsensus {
    /// Create a new DAG consensus engine
    pub fn new(node_id: String, min_quorum_size: usize) -> Self {
        let mut vector_clock = HashMap::new();
        vector_clock.insert(node_id.clone(), 0);

        Self {
            node_id,
            vertices: Arc::new(DashMap::new()),
            finalized: Arc::new(RwLock::new(HashSet::new())),
            vector_clock: Arc::new(RwLock::new(vector_clock)),
            pending_txs: Arc::new(RwLock::new(VecDeque::new())),
            min_quorum_size,
            nonce_counter: Arc::new(RwLock::new(0)),
        }
    }

    /// Submit a transaction to the consensus system
    pub fn submit_transaction(&self, tx_type: TransactionType, data: Vec<u8>) -> Result<String> {
        let mut nonce = self.nonce_counter.write();
        *nonce += 1;

        let transaction = Transaction {
            id: Uuid::new_v4().to_string(),
            tx_type,
            data,
            nonce: *nonce,
        };

        let tx_id = transaction.id.clone();

        let mut pending = self.pending_txs.write();
        pending.push_back(transaction);

        debug!("Transaction {} submitted to consensus", tx_id);
        Ok(tx_id)
    }

    /// Create a new vertex for pending transactions
    pub fn create_vertex(&self) -> Result<Option<DagVertex>> {
        let mut pending = self.pending_txs.write();

        if pending.is_empty() {
            return Ok(None);
        }

        // Take the next transaction
        let transaction = pending.pop_front().unwrap();

        // Find parent vertices (tips of the DAG)
        let parents = self.find_tips();

        // Update vector clock
        let mut clock = self.vector_clock.write();
        let count = clock.entry(self.node_id.clone()).or_insert(0);
        *count += 1;

        let vertex = DagVertex::new(self.node_id.clone(), transaction, parents, clock.clone());

        let vertex_id = vertex.id.clone();
        self.vertices.insert(vertex_id.clone(), vertex.clone());

        debug!(
            "Created vertex {} for transaction {}",
            vertex_id, vertex.transaction.id
        );
        Ok(Some(vertex))
    }

    /// Find tip vertices (vertices with no children)
    fn find_tips(&self) -> Vec<String> {
        let mut has_children = HashSet::new();

        // Mark all vertices that have children
        for entry in self.vertices.iter() {
            for parent in &entry.value().parents {
                has_children.insert(parent.clone());
            }
        }

        // Find vertices without children
        self.vertices
            .iter()
            .filter(|entry| !has_children.contains(entry.key()))
            .map(|entry| entry.key().clone())
            .collect()
    }

    /// Add a vertex from another node
    pub fn add_vertex(&self, vertex: DagVertex) -> Result<()> {
        // Verify signature
        if !vertex.verify_signature() {
            return Err(ClusterError::ConsensusError(
                "Invalid vertex signature".to_string(),
            ));
        }

        // Verify parents exist
        for parent_id in &vertex.parents {
            if !self.vertices.contains_key(parent_id) && !self.is_finalized(parent_id) {
                return Err(ClusterError::ConsensusError(format!(
                    "Parent vertex {} not found",
                    parent_id
                )));
            }
        }

        // Merge vector clock
        let mut clock = self.vector_clock.write();
        for (node, count) in &vertex.vector_clock {
            let existing = clock.entry(node.clone()).or_insert(0);
            *existing = (*existing).max(*count);
        }

        self.vertices.insert(vertex.id.clone(), vertex);
        Ok(())
    }

    /// Check if a vertex is finalized
    pub fn is_finalized(&self, vertex_id: &str) -> bool {
        let finalized = self.finalized.read();
        finalized.contains(vertex_id)
    }

    /// Finalize vertices using the wave algorithm
    pub fn finalize_vertices(&self) -> Result<Vec<String>> {
        let mut finalized_ids = Vec::new();

        // Find vertices that can be finalized
        // A vertex is finalized if it has enough confirmations from different nodes
        let mut confirmations: HashMap<String, HashSet<String>> = HashMap::new();

        for entry in self.vertices.iter() {
            let vertex = entry.value();

            // Count confirmations (vertices that reference this one)
            for other_entry in self.vertices.iter() {
                if other_entry.value().parents.contains(&vertex.id) {
                    confirmations
                        .entry(vertex.id.clone())
                        .or_insert_with(HashSet::new)
                        .insert(other_entry.value().node_id.clone());
                }
            }
        }

        // Finalize vertices with enough confirmations
        let mut finalized = self.finalized.write();

        for (vertex_id, confirming_nodes) in confirmations {
            if confirming_nodes.len() >= self.min_quorum_size && !finalized.contains(&vertex_id) {
                finalized.insert(vertex_id.clone());
                finalized_ids.push(vertex_id.clone());
                info!("Finalized vertex {}", vertex_id);
            }
        }

        Ok(finalized_ids)
    }

    /// Get the total order of finalized transactions
    pub fn get_finalized_order(&self) -> Vec<Transaction> {
        let finalized = self.finalized.read();
        let mut ordered_txs = Vec::new();

        // Topological sort of finalized vertices
        let finalized_vertices: Vec<_> = self
            .vertices
            .iter()
            .filter(|entry| finalized.contains(entry.key()))
            .map(|entry| entry.value().clone())
            .collect();

        // Sort by vector clock and timestamp
        let mut sorted = finalized_vertices;
        sorted.sort_by(|a, b| {
            // First by vector clock dominance
            let a_dominates = Self::vector_clock_dominates(&a.vector_clock, &b.vector_clock);
            let b_dominates = Self::vector_clock_dominates(&b.vector_clock, &a.vector_clock);

            if a_dominates && !b_dominates {
                std::cmp::Ordering::Less
            } else if b_dominates && !a_dominates {
                std::cmp::Ordering::Greater
            } else {
                // Fall back to timestamp
                a.timestamp.cmp(&b.timestamp)
            }
        });

        for vertex in sorted {
            ordered_txs.push(vertex.transaction);
        }

        ordered_txs
    }

    /// Check if vector clock a dominates vector clock b
    fn vector_clock_dominates(a: &HashMap<String, u64>, b: &HashMap<String, u64>) -> bool {
        let mut dominates = false;

        for (node, &a_count) in a {
            let b_count = b.get(node).copied().unwrap_or(0);
            if a_count < b_count {
                return false;
            }
            if a_count > b_count {
                dominates = true;
            }
        }

        dominates
    }

    /// Detect conflicts between transactions
    pub fn detect_conflicts(&self, tx1: &Transaction, tx2: &Transaction) -> bool {
        // In a real implementation, this would analyze transaction data
        // For now, conservatively assume all writes conflict
        matches!(
            (&tx1.tx_type, &tx2.tx_type),
            (TransactionType::Write, TransactionType::Write)
                | (TransactionType::Delete, TransactionType::Write)
                | (TransactionType::Write, TransactionType::Delete)
        )
    }

    /// Get consensus statistics
    pub fn get_stats(&self) -> ConsensusStats {
        let finalized = self.finalized.read();
        let pending = self.pending_txs.read();

        ConsensusStats {
            total_vertices: self.vertices.len(),
            finalized_vertices: finalized.len(),
            pending_transactions: pending.len(),
            tips: self.find_tips().len(),
        }
    }

    /// Prune old finalized vertices to save memory
    pub fn prune_old_vertices(&self, keep_count: usize) {
        let finalized = self.finalized.read();

        if finalized.len() <= keep_count {
            return;
        }

        // Remove oldest finalized vertices
        let mut vertices_to_remove = Vec::new();

        for vertex_id in finalized.iter() {
            if let Some(vertex) = self.vertices.get(vertex_id) {
                vertices_to_remove.push((vertex_id.clone(), vertex.timestamp));
            }
        }

        vertices_to_remove.sort_by_key(|(_, ts)| *ts);

        let to_remove = vertices_to_remove.len().saturating_sub(keep_count);
        for (vertex_id, _) in vertices_to_remove.iter().take(to_remove) {
            self.vertices.remove(vertex_id);
        }

        debug!("Pruned {} old vertices", to_remove);
    }
}

/// Consensus statistics
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ConsensusStats {
    pub total_vertices: usize,
    pub finalized_vertices: usize,
    pub pending_transactions: usize,
    pub tips: usize,
}

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

    #[test]
    fn test_consensus_creation() {
        let consensus = DagConsensus::new("node1".to_string(), 2);
        let stats = consensus.get_stats();

        assert_eq!(stats.total_vertices, 0);
        assert_eq!(stats.pending_transactions, 0);
    }

    #[test]
    fn test_submit_transaction() {
        let consensus = DagConsensus::new("node1".to_string(), 2);

        let tx_id = consensus
            .submit_transaction(TransactionType::Write, vec![1, 2, 3])
            .unwrap();

        assert!(!tx_id.is_empty());

        let stats = consensus.get_stats();
        assert_eq!(stats.pending_transactions, 1);
    }

    #[test]
    fn test_create_vertex() {
        let consensus = DagConsensus::new("node1".to_string(), 2);

        consensus
            .submit_transaction(TransactionType::Write, vec![1, 2, 3])
            .unwrap();

        let vertex = consensus.create_vertex().unwrap();
        assert!(vertex.is_some());

        let stats = consensus.get_stats();
        assert_eq!(stats.total_vertices, 1);
        assert_eq!(stats.pending_transactions, 0);
    }

    #[test]
    fn test_vector_clock_dominance() {
        let mut clock1 = HashMap::new();
        clock1.insert("node1".to_string(), 2);
        clock1.insert("node2".to_string(), 1);

        let mut clock2 = HashMap::new();
        clock2.insert("node1".to_string(), 1);
        clock2.insert("node2".to_string(), 1);

        assert!(DagConsensus::vector_clock_dominates(&clock1, &clock2));
        assert!(!DagConsensus::vector_clock_dominates(&clock2, &clock1));
    }

    #[test]
    fn test_conflict_detection() {
        let consensus = DagConsensus::new("node1".to_string(), 2);

        let tx1 = Transaction {
            id: "1".to_string(),
            tx_type: TransactionType::Write,
            data: vec![1],
            nonce: 1,
        };

        let tx2 = Transaction {
            id: "2".to_string(),
            tx_type: TransactionType::Write,
            data: vec![2],
            nonce: 2,
        };

        assert!(consensus.detect_conflicts(&tx1, &tx2));
    }

    #[test]
    fn test_finalization() {
        let consensus = DagConsensus::new("node1".to_string(), 2);

        // Create some vertices
        for i in 0..5 {
            consensus
                .submit_transaction(TransactionType::Write, vec![i])
                .unwrap();
            consensus.create_vertex().unwrap();
        }

        // Try to finalize
        let finalized = consensus.finalize_vertices().unwrap();

        // Without enough confirmations, nothing should be finalized yet
        // (would need vertices from other nodes)
        assert_eq!(finalized.len(), 0);
    }
}