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
//! Consistent-hash shard router for distributed RDF storage
//!
//! Implements a virtual-node consistent-hash ring that maps arbitrary byte keys
//! to shard nodes with configurable replication. Provides deterministic routing,
//! healthy-node filtering, distribution statistics, and rebalance cost estimation.

use std::collections::{BTreeMap, HashMap};

// ─── ShardNode ────────────────────────────────────────────────────────────────

/// A physical shard node in the cluster
#[derive(Debug, Clone)]
pub struct ShardNode {
    /// Unique node identifier
    pub id: String,
    /// Network address (e.g. "10.0.0.1:7000")
    pub address: String,
    /// Relative weight; more weight → more virtual nodes
    pub weight: usize,
    /// Whether this node is currently considered healthy
    pub is_healthy: bool,
}

impl ShardNode {
    /// Create a new, healthy node with weight 1
    pub fn new(id: impl Into<String>, address: impl Into<String>) -> Self {
        ShardNode {
            id: id.into(),
            address: address.into(),
            weight: 1,
            is_healthy: true,
        }
    }

    /// Set the weight for this node
    pub fn with_weight(mut self, weight: usize) -> Self {
        self.weight = weight;
        self
    }
}

// ─── VirtualNode ──────────────────────────────────────────────────────────────

/// A point on the consistent-hash ring
#[derive(Debug, Clone)]
pub struct VirtualNode {
    /// Hash value at this ring position
    pub hash: u64,
    /// The real node ID this virtual node maps to
    pub real_node_id: String,
}

// ─── ShardConfig ─────────────────────────────────────────────────────────────

/// Configuration for the shard router
#[derive(Debug, Clone)]
pub struct ShardConfig {
    /// Number of virtual nodes to place per physical node (multiplied by weight)
    pub virtual_nodes_per_server: usize,
    /// Number of replicas per key (1 = primary only)
    pub replication_factor: usize,
}

impl Default for ShardConfig {
    fn default() -> Self {
        ShardConfig {
            virtual_nodes_per_server: 150,
            replication_factor: 3,
        }
    }
}

// ─── RouteResult ─────────────────────────────────────────────────────────────

/// The routing decision for a given key
#[derive(Debug, Clone)]
pub struct RouteResult {
    /// Primary node for this key
    pub primary: String,
    /// Additional replica nodes (up to replication_factor - 1)
    pub replicas: Vec<String>,
    /// Hash value used to route this key
    pub shard_id: u64,
}

// ─── ShardRouter ─────────────────────────────────────────────────────────────

/// Consistent-hash shard router
pub struct ShardRouter {
    nodes: HashMap<String, ShardNode>,
    ring: BTreeMap<u64, VirtualNode>,
    config: ShardConfig,
}

impl ShardRouter {
    /// Create an empty router with the given configuration
    pub fn new(config: ShardConfig) -> Self {
        ShardRouter {
            nodes: HashMap::new(),
            ring: BTreeMap::new(),
            config,
        }
    }

    /// Add a physical node; populates virtual nodes on the ring
    pub fn add_node(&mut self, node: ShardNode) {
        let virtual_count = self.config.virtual_nodes_per_server * node.weight;
        let node_id = node.id.clone();
        for i in 0..virtual_count {
            let key = format!("{}-vn{}", node_id, i);
            let hash = fnv1a_hash(key.as_bytes());
            // Handle collision with a secondary probe
            let final_hash = if self.ring.contains_key(&hash) {
                let probe = format!("{}-vn{}-probe", node_id, i);
                fnv1a_hash(probe.as_bytes())
            } else {
                hash
            };
            self.ring.insert(
                final_hash,
                VirtualNode {
                    hash: final_hash,
                    real_node_id: node_id.clone(),
                },
            );
        }
        self.nodes.insert(node_id, node);
    }

    /// Remove a physical node and all its virtual nodes from the ring
    /// Returns `true` if the node existed
    pub fn remove_node(&mut self, id: &str) -> bool {
        if self.nodes.remove(id).is_none() {
            return false;
        }
        self.ring.retain(|_, vn| vn.real_node_id != id);
        true
    }

    /// Route a raw byte key; returns `None` if the ring is empty
    pub fn route(&self, key: &[u8]) -> Option<RouteResult> {
        if self.ring.is_empty() {
            return None;
        }
        let hash = fnv1a_hash(key);
        self.route_hash(hash)
    }

    /// Route a string key
    pub fn route_str(&self, key: &str) -> Option<RouteResult> {
        self.route(key.as_bytes())
    }

    /// Mark a node as unhealthy; returns `true` if the node exists
    pub fn mark_unhealthy(&mut self, id: &str) -> bool {
        if let Some(node) = self.nodes.get_mut(id) {
            node.is_healthy = false;
            true
        } else {
            false
        }
    }

    /// Mark a node as healthy again; returns `true` if the node exists
    pub fn mark_healthy(&mut self, id: &str) -> bool {
        if let Some(node) = self.nodes.get_mut(id) {
            node.is_healthy = true;
            true
        } else {
            false
        }
    }

    /// Borrow all healthy nodes
    pub fn healthy_nodes(&self) -> Vec<&ShardNode> {
        self.nodes.values().filter(|n| n.is_healthy).collect()
    }

    /// Total number of physical nodes
    pub fn node_count(&self) -> usize {
        self.nodes.len()
    }

    /// Total number of virtual nodes on the ring
    pub fn virtual_node_count(&self) -> usize {
        self.ring.len()
    }

    /// Map from node_id to count of virtual nodes assigned to that node
    pub fn distribution(&self) -> HashMap<String, usize> {
        let mut map: HashMap<String, usize> = HashMap::new();
        for vn in self.ring.values() {
            *map.entry(vn.real_node_id.clone()).or_insert(0) += 1;
        }
        map
    }

    /// Estimate the fraction of keys that would move if a new node were added.
    ///
    /// A new node takes `1/(n+1)` of the ring (assuming uniform weight), so
    /// approximately `1/(n+1)` of keys would be reassigned.
    pub fn rebalance_stats(&self, _new_node_id: &str) -> f64 {
        let n = self.nodes.len();
        if n == 0 {
            return 0.0;
        }
        1.0 / (n as f64 + 1.0)
    }

    // ── private ──────────────────────────────────────────────────────────────

    fn route_hash(&self, hash: u64) -> Option<RouteResult> {
        if self.ring.is_empty() {
            return None;
        }

        // Walk the ring clockwise from `hash`, collecting distinct node IDs
        let mut assigned: Vec<String> = Vec::new();
        let mut seen: std::collections::HashSet<String> = std::collections::HashSet::new();

        // Nodes clockwise from hash (ring-wrapped)
        let tail = self.ring.range(hash..);
        let head = self.ring.range(..hash);

        let max_replicas = self.config.replication_factor.min(self.nodes.len());

        for vn in tail.chain(head).map(|(_, v)| v) {
            if seen.insert(vn.real_node_id.clone()) {
                assigned.push(vn.real_node_id.clone());
                if assigned.len() >= max_replicas {
                    break;
                }
            }
        }

        if assigned.is_empty() {
            return None;
        }

        let primary = assigned[0].clone();
        let replicas = assigned[1..].to_vec();

        Some(RouteResult {
            primary,
            replicas,
            shard_id: hash,
        })
    }
}

// ─── FNV-1a hash ─────────────────────────────────────────────────────────────

/// FNV-1a 64-bit hash
pub fn fnv1a_hash(data: &[u8]) -> u64 {
    const OFFSET: u64 = 0xcbf2_9ce4_8422_2325;
    const PRIME: u64 = 0x0000_0001_0000_01b3;
    let mut hash = OFFSET;
    for byte in data {
        hash ^= u64::from(*byte);
        hash = hash.wrapping_mul(PRIME);
    }
    hash
}

// ─── Tests ────────────────────────────────────────────────────────────────────

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

    fn three_node_router() -> ShardRouter {
        let config = ShardConfig {
            virtual_nodes_per_server: 10,
            replication_factor: 3,
        };
        let mut router = ShardRouter::new(config);
        router.add_node(ShardNode::new("node-1", "10.0.0.1:7000"));
        router.add_node(ShardNode::new("node-2", "10.0.0.2:7000"));
        router.add_node(ShardNode::new("node-3", "10.0.0.3:7000"));
        router
    }

    // ── add_node ─────────────────────────────────────────────────────────────

    #[test]
    fn test_add_node_increases_count() {
        let mut router = ShardRouter::new(ShardConfig::default());
        router.add_node(ShardNode::new("n1", "127.0.0.1:7000"));
        assert_eq!(router.node_count(), 1);
    }

    #[test]
    fn test_add_node_populates_virtual_nodes() {
        let config = ShardConfig {
            virtual_nodes_per_server: 5,
            replication_factor: 1,
        };
        let mut router = ShardRouter::new(config);
        router.add_node(ShardNode::new("n1", "127.0.0.1:7000"));
        // At least 5 virtual nodes (may be fewer if collisions merged)
        assert!(router.virtual_node_count() >= 1);
    }

    #[test]
    fn test_add_multiple_nodes() {
        let router = three_node_router();
        assert_eq!(router.node_count(), 3);
    }

    // ── remove_node ──────────────────────────────────────────────────────────

    #[test]
    fn test_remove_node_decreases_count() {
        let mut router = three_node_router();
        assert!(router.remove_node("node-1"));
        assert_eq!(router.node_count(), 2);
    }

    #[test]
    fn test_remove_node_removes_virtual_nodes() {
        let config = ShardConfig {
            virtual_nodes_per_server: 10,
            replication_factor: 1,
        };
        let mut router = ShardRouter::new(config);
        router.add_node(ShardNode::new("n1", "127.0.0.1:7000"));
        let before = router.virtual_node_count();
        router.remove_node("n1");
        assert!(router.virtual_node_count() < before);
    }

    #[test]
    fn test_remove_nonexistent_node_returns_false() {
        let mut router = three_node_router();
        assert!(!router.remove_node("nonexistent"));
    }

    // ── route ────────────────────────────────────────────────────────────────

    #[test]
    fn test_route_returns_some_when_nodes_exist() {
        let router = three_node_router();
        assert!(router.route(b"some_key").is_some());
    }

    #[test]
    fn test_route_returns_none_when_no_nodes() {
        let router = ShardRouter::new(ShardConfig::default());
        assert!(router.route(b"key").is_none());
    }

    #[test]
    fn test_route_primary_is_valid_node() {
        let router = three_node_router();
        let result = router.route(b"test_key").unwrap();
        assert!(router.nodes.contains_key(&result.primary));
    }

    #[test]
    fn test_route_replicas_count() {
        let router = three_node_router();
        let result = router.route(b"hello").unwrap();
        // With 3 nodes and replication_factor=3, we should get 2 replicas
        assert_eq!(result.replicas.len(), 2);
    }

    #[test]
    fn test_route_all_replicas_are_distinct() {
        let router = three_node_router();
        let result = router.route(b"distinct_test").unwrap();
        let mut all = vec![result.primary.clone()];
        all.extend(result.replicas.clone());
        let set: std::collections::HashSet<_> = all.iter().collect();
        assert_eq!(set.len(), all.len());
    }

    #[test]
    fn test_route_replication_factor_capped_at_node_count() {
        let config = ShardConfig {
            virtual_nodes_per_server: 10,
            replication_factor: 10, // more than nodes
        };
        let mut router = ShardRouter::new(config);
        router.add_node(ShardNode::new("n1", "10.0.0.1:7000"));
        router.add_node(ShardNode::new("n2", "10.0.0.2:7000"));
        let result = router.route(b"key").unwrap();
        // primary + replicas should not exceed 2 (node count)
        let total = 1 + result.replicas.len();
        assert!(total <= 2);
    }

    // ── route_str ────────────────────────────────────────────────────────────

    #[test]
    fn test_route_str_deterministic() {
        let router = three_node_router();
        let r1 = router.route_str("sparql:query:1").unwrap();
        let r2 = router.route_str("sparql:query:1").unwrap();
        assert_eq!(r1.primary, r2.primary);
        assert_eq!(r1.shard_id, r2.shard_id);
    }

    #[test]
    fn test_route_str_different_keys_may_differ() {
        let router = three_node_router();
        let mut differences = 0;
        for i in 0..20 {
            let r1 = router.route_str(&format!("key{}", i)).unwrap();
            let r2 = router.route_str(&format!("other{}", i)).unwrap();
            if r1.primary != r2.primary {
                differences += 1;
            }
        }
        // With 3 nodes, at least some keys should land on different nodes
        assert!(differences > 0);
    }

    // ── mark_unhealthy / healthy_nodes ───────────────────────────────────────

    #[test]
    fn test_mark_unhealthy_returns_true_for_known_node() {
        let mut router = three_node_router();
        assert!(router.mark_unhealthy("node-1"));
    }

    #[test]
    fn test_mark_unhealthy_returns_false_for_unknown() {
        let mut router = three_node_router();
        assert!(!router.mark_unhealthy("unknown"));
    }

    #[test]
    fn test_healthy_nodes_excludes_unhealthy() {
        let mut router = three_node_router();
        router.mark_unhealthy("node-1");
        let healthy = router.healthy_nodes();
        let healthy_ids: Vec<&str> = healthy.iter().map(|n| n.id.as_str()).collect();
        assert!(!healthy_ids.contains(&"node-1"));
        assert_eq!(healthy.len(), 2);
    }

    #[test]
    fn test_healthy_nodes_includes_all_initially() {
        let router = three_node_router();
        assert_eq!(router.healthy_nodes().len(), 3);
    }

    #[test]
    fn test_mark_healthy_restores_node() {
        let mut router = three_node_router();
        router.mark_unhealthy("node-1");
        router.mark_healthy("node-1");
        assert_eq!(router.healthy_nodes().len(), 3);
    }

    // ── virtual_node_count ───────────────────────────────────────────────────

    #[test]
    fn test_virtual_node_count_scales_with_nodes() {
        let config = ShardConfig {
            virtual_nodes_per_server: 10,
            replication_factor: 1,
        };
        let mut router = ShardRouter::new(config);
        router.add_node(ShardNode::new("n1", "127.0.0.1:7000"));
        let after_one = router.virtual_node_count();
        router.add_node(ShardNode::new("n2", "127.0.0.2:7000"));
        let after_two = router.virtual_node_count();
        assert!(after_two > after_one);
    }

    #[test]
    fn test_virtual_node_count_weight_multiplied() {
        let config = ShardConfig {
            virtual_nodes_per_server: 5,
            replication_factor: 1,
        };
        let mut router_weight1 = ShardRouter::new(config.clone());
        router_weight1.add_node(ShardNode::new("n1", "127.0.0.1:7000").with_weight(1));

        let mut router_weight2 = ShardRouter::new(config);
        router_weight2.add_node(ShardNode::new("n1", "127.0.0.1:7000").with_weight(2));

        assert!(router_weight2.virtual_node_count() > router_weight1.virtual_node_count());
    }

    // ── distribution ─────────────────────────────────────────────────────────

    #[test]
    fn test_distribution_totals_equal_virtual_node_count() {
        let router = three_node_router();
        let dist = router.distribution();
        let total: usize = dist.values().sum();
        assert_eq!(total, router.virtual_node_count());
    }

    #[test]
    fn test_distribution_has_entry_for_each_node() {
        let router = three_node_router();
        let dist = router.distribution();
        assert!(dist.contains_key("node-1"));
        assert!(dist.contains_key("node-2"));
        assert!(dist.contains_key("node-3"));
    }

    #[test]
    fn test_distribution_all_counts_positive() {
        let router = three_node_router();
        for count in router.distribution().values() {
            assert!(*count > 0);
        }
    }

    // ── rebalance_stats ──────────────────────────────────────────────────────

    #[test]
    fn test_rebalance_stats_between_zero_and_one() {
        let router = three_node_router();
        let fraction = router.rebalance_stats("node-new");
        assert!((0.0..=1.0).contains(&fraction));
    }

    #[test]
    fn test_rebalance_stats_empty_ring_is_zero() {
        let router = ShardRouter::new(ShardConfig::default());
        let fraction = router.rebalance_stats("node-new");
        assert_eq!(fraction, 0.0);
    }

    #[test]
    fn test_rebalance_stats_decreases_with_more_nodes() {
        let config = ShardConfig {
            virtual_nodes_per_server: 10,
            replication_factor: 1,
        };
        let mut router1 = ShardRouter::new(config.clone());
        router1.add_node(ShardNode::new("n1", "127.0.0.1:7000"));
        let f1 = router1.rebalance_stats("new");

        let mut router2 = ShardRouter::new(config);
        router2.add_node(ShardNode::new("n1", "127.0.0.1:7000"));
        router2.add_node(ShardNode::new("n2", "127.0.0.2:7000"));
        let f2 = router2.rebalance_stats("new");

        // More nodes → smaller fraction per added node
        assert!(f2 < f1);
    }

    // ── fnv1a_hash ───────────────────────────────────────────────────────────

    #[test]
    fn test_fnv1a_deterministic() {
        let h1 = fnv1a_hash(b"consistent hashing rocks");
        let h2 = fnv1a_hash(b"consistent hashing rocks");
        assert_eq!(h1, h2);
    }

    #[test]
    fn test_fnv1a_different_inputs_differ() {
        let h1 = fnv1a_hash(b"foo");
        let h2 = fnv1a_hash(b"bar");
        assert_ne!(h1, h2);
    }

    #[test]
    fn test_fnv1a_empty_input() {
        let h = fnv1a_hash(b"");
        assert_ne!(h, 0); // FNV-1a offset basis is non-zero
    }

    // ── edge cases ───────────────────────────────────────────────────────────

    #[test]
    fn test_single_node_all_keys_route_to_it() {
        let config = ShardConfig {
            virtual_nodes_per_server: 10,
            replication_factor: 1,
        };
        let mut router = ShardRouter::new(config);
        router.add_node(ShardNode::new("only", "127.0.0.1:7000"));
        for i in 0u32..20 {
            let result = router.route(&i.to_le_bytes()).unwrap();
            assert_eq!(result.primary, "only");
        }
    }

    #[test]
    fn test_remove_all_nodes_route_returns_none() {
        let mut router = three_node_router();
        router.remove_node("node-1");
        router.remove_node("node-2");
        router.remove_node("node-3");
        assert!(router.route(b"key").is_none());
    }
}