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
/// Partition rebalancing for distributed cluster data redistribution.
///
/// Computes optimal partition movement plans to balance load across
/// cluster nodes using configurable strategies.
use std::collections::HashMap;

/// A data partition assigned to a specific cluster node.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Partition {
    /// Unique partition identifier.
    pub id: u32,
    /// ID of the node currently hosting this partition.
    pub node_id: String,
    /// Number of triples in this partition.
    pub triple_count: usize,
    /// Size in bytes.
    pub byte_size: usize,
}

impl Partition {
    /// Create a new partition.
    pub fn new(id: u32, node_id: impl Into<String>, triple_count: usize, byte_size: usize) -> Self {
        Self {
            id,
            node_id: node_id.into(),
            triple_count,
            byte_size,
        }
    }
}

/// A single planned partition movement.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct PartitionMove {
    /// ID of the partition to move.
    pub partition_id: u32,
    /// Source node.
    pub from_node: String,
    /// Destination node.
    pub to_node: String,
    /// Number of triples being moved.
    pub triple_count: usize,
}

impl PartitionMove {
    /// Return the number of triples this move will transfer.
    pub fn transfer_size(&self) -> usize {
        self.triple_count
    }
}

/// A complete rebalancing plan.
#[derive(Debug, Clone)]
pub struct RebalancePlan {
    /// Ordered list of partition moves.
    pub moves: Vec<PartitionMove>,
    /// Expected improvement in imbalance score after applying all moves.
    pub expected_improvement: f64,
}

impl RebalancePlan {
    /// Return true if this plan requires no moves.
    pub fn is_noop(&self) -> bool {
        self.moves.is_empty()
    }

    /// Return the total number of triples to be transferred.
    pub fn total_transfer(&self) -> usize {
        self.moves.iter().map(|m| m.triple_count).sum()
    }
}

/// Load snapshot for a single cluster node.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NodeLoad {
    /// Node identifier.
    pub node_id: String,
    /// Number of partitions currently assigned.
    pub partition_count: usize,
    /// Total triples across all partitions.
    pub total_triples: usize,
}

impl NodeLoad {
    /// Create a new node load record.
    pub fn new(node_id: impl Into<String>, partition_count: usize, total_triples: usize) -> Self {
        Self {
            node_id: node_id.into(),
            partition_count,
            total_triples,
        }
    }
}

/// Rebalancing strategy.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum RebalanceStrategy {
    /// Move partitions to the least-loaded node.
    LeastLoaded,
    /// Distribute partitions in round-robin order.
    RoundRobin,
    /// Weight moves by node capacity (triple count).
    WeightedCapacity,
}

/// Computes partition rebalancing plans for a cluster.
pub struct PartitionRebalancer {
    strategy: RebalanceStrategy,
    max_moves_per_plan: usize,
}

impl PartitionRebalancer {
    /// Create a new rebalancer.
    pub fn new(strategy: RebalanceStrategy, max_moves_per_plan: usize) -> Self {
        Self {
            strategy,
            max_moves_per_plan,
        }
    }

    /// Compute a rebalancing plan for the given partitions and node state.
    ///
    /// Returns a plan with at most `max_moves_per_plan` moves.
    pub fn compute_plan(&self, partitions: &[Partition], nodes: &[NodeLoad]) -> RebalancePlan {
        if nodes.is_empty() || partitions.is_empty() {
            return RebalancePlan {
                moves: vec![],
                expected_improvement: 0.0,
            };
        }

        let current_score = Self::imbalance_score(nodes);

        let moves = match self.strategy {
            RebalanceStrategy::LeastLoaded => self.compute_least_loaded(partitions, nodes),
            RebalanceStrategy::RoundRobin => self.compute_round_robin(partitions, nodes),
            RebalanceStrategy::WeightedCapacity => {
                self.compute_weighted_capacity(partitions, nodes)
            }
        };

        // Simulate the new node loads to compute expected improvement.
        let simulated_nodes = self.simulate_moves(nodes, partitions, &moves);
        let new_score = Self::imbalance_score(&simulated_nodes);
        let expected_improvement = (current_score - new_score).max(0.0);

        RebalancePlan {
            moves,
            expected_improvement,
        }
    }

    /// Compute imbalance score as coefficient of variation (std_dev / mean).
    ///
    /// Returns 0.0 if there are no nodes or if the mean is zero.
    pub fn imbalance_score(nodes: &[NodeLoad]) -> f64 {
        if nodes.is_empty() {
            return 0.0;
        }

        let loads: Vec<f64> = nodes.iter().map(|n| n.total_triples as f64).collect();
        let n = loads.len() as f64;
        let mean = loads.iter().sum::<f64>() / n;

        if mean < 1e-9 {
            return 0.0;
        }

        let variance = loads.iter().map(|x| (x - mean).powi(2)).sum::<f64>() / n;
        let std_dev = variance.sqrt();
        std_dev / mean
    }

    /// Return true if the cluster is balanced within the given threshold.
    pub fn is_balanced(nodes: &[NodeLoad], threshold: f64) -> bool {
        Self::imbalance_score(nodes) <= threshold
    }

    /// Compute a `node_id → NodeLoad` map from a partition list.
    pub fn node_loads(partitions: &[Partition]) -> HashMap<String, NodeLoad> {
        let mut map: HashMap<String, NodeLoad> = HashMap::new();
        for p in partitions {
            let entry = map
                .entry(p.node_id.clone())
                .or_insert_with(|| NodeLoad::new(p.node_id.clone(), 0, 0));
            entry.partition_count += 1;
            entry.total_triples += p.triple_count;
        }
        map
    }

    // ---- Private helpers ----

    /// Compute the mean triple count across nodes.
    fn mean_load(nodes: &[NodeLoad]) -> f64 {
        if nodes.is_empty() {
            return 0.0;
        }
        nodes.iter().map(|n| n.total_triples as f64).sum::<f64>() / nodes.len() as f64
    }

    /// Least-loaded strategy: move the largest partition from the heaviest
    /// node to the lightest node, up to `max_moves_per_plan`.
    fn compute_least_loaded(
        &self,
        partitions: &[Partition],
        nodes: &[NodeLoad],
    ) -> Vec<PartitionMove> {
        let mut moves = Vec::new();
        let mut node_map: HashMap<String, usize> = nodes
            .iter()
            .map(|n| (n.node_id.clone(), n.total_triples))
            .collect();

        let mean = Self::mean_load(nodes);

        for _ in 0..self.max_moves_per_plan {
            // Find the most loaded node with above-average load.
            let source_node = node_map
                .iter()
                .filter(|(_, &load)| load as f64 > mean * 1.1)
                .max_by_key(|(_, &load)| load)
                .map(|(id, _)| id.clone());

            let source_id = match source_node {
                Some(id) => id,
                None => break,
            };

            // Find the least loaded destination.
            let dest_id = node_map
                .iter()
                .filter(|(id, _)| **id != source_id)
                .min_by_key(|(_, &load)| load)
                .map(|(id, _)| id.clone());

            let dest_id = match dest_id {
                Some(id) => id,
                None => break,
            };

            // Find the largest partition on the source node.
            let partition = partitions
                .iter()
                .filter(|p| p.node_id == source_id)
                .max_by_key(|p| p.triple_count);

            let p = match partition {
                Some(p) => p,
                None => break,
            };

            // Record the move.
            moves.push(PartitionMove {
                partition_id: p.id,
                from_node: source_id.clone(),
                to_node: dest_id.clone(),
                triple_count: p.triple_count,
            });

            // Update simulated loads.
            *node_map.entry(source_id).or_insert(0) = node_map
                .get(&source_id)
                .copied()
                .unwrap_or(0)
                .saturating_sub(p.triple_count);
            *node_map.entry(dest_id).or_insert(0) += p.triple_count;
        }

        moves
    }

    /// Round-robin strategy: redistribute partitions evenly across nodes.
    fn compute_round_robin(
        &self,
        partitions: &[Partition],
        nodes: &[NodeLoad],
    ) -> Vec<PartitionMove> {
        if nodes.len() < 2 {
            return vec![];
        }

        let mut moves = Vec::new();
        let node_ids: Vec<String> = nodes.iter().map(|n| n.node_id.clone()).collect();

        // Assign each partition to nodes in round-robin order.
        for (idx, partition) in partitions.iter().enumerate() {
            if moves.len() >= self.max_moves_per_plan {
                break;
            }
            let target_node = &node_ids[idx % node_ids.len()];
            if *target_node != partition.node_id {
                moves.push(PartitionMove {
                    partition_id: partition.id,
                    from_node: partition.node_id.clone(),
                    to_node: target_node.clone(),
                    triple_count: partition.triple_count,
                });
            }
        }

        moves
    }

    /// Weighted capacity: move partitions proportional to their size.
    fn compute_weighted_capacity(
        &self,
        partitions: &[Partition],
        nodes: &[NodeLoad],
    ) -> Vec<PartitionMove> {
        // For weighted capacity, use same logic as least-loaded but choose
        // destination by available capacity (inverse of current load).
        self.compute_least_loaded(partitions, nodes)
    }

    /// Simulate applying moves to produce new NodeLoad states.
    fn simulate_moves(
        &self,
        nodes: &[NodeLoad],
        _partitions: &[Partition],
        moves: &[PartitionMove],
    ) -> Vec<NodeLoad> {
        let mut loads: HashMap<String, usize> = nodes
            .iter()
            .map(|n| (n.node_id.clone(), n.total_triples))
            .collect();
        let mut counts: HashMap<String, usize> = nodes
            .iter()
            .map(|n| (n.node_id.clone(), n.partition_count))
            .collect();

        for m in moves {
            *loads.entry(m.from_node.clone()).or_insert(0) = loads
                .get(&m.from_node)
                .copied()
                .unwrap_or(0)
                .saturating_sub(m.triple_count);
            *loads.entry(m.to_node.clone()).or_insert(0) += m.triple_count;
            *counts.entry(m.from_node.clone()).or_insert(0) = counts
                .get(&m.from_node)
                .copied()
                .unwrap_or(0)
                .saturating_sub(1);
            *counts.entry(m.to_node.clone()).or_insert(0) += 1;
        }

        nodes
            .iter()
            .map(|n| NodeLoad {
                node_id: n.node_id.clone(),
                partition_count: *counts.get(&n.node_id).unwrap_or(&n.partition_count),
                total_triples: *loads.get(&n.node_id).unwrap_or(&n.total_triples),
            })
            .collect()
    }
}

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

    fn make_partitions() -> Vec<Partition> {
        vec![
            Partition::new(1, "node-a", 1000, 10_000),
            Partition::new(2, "node-a", 2000, 20_000),
            Partition::new(3, "node-a", 3000, 30_000),
            Partition::new(4, "node-b", 500, 5_000),
            Partition::new(5, "node-c", 400, 4_000),
        ]
    }

    fn make_nodes() -> Vec<NodeLoad> {
        vec![
            NodeLoad::new("node-a", 3, 6000),
            NodeLoad::new("node-b", 1, 500),
            NodeLoad::new("node-c", 1, 400),
        ]
    }

    // --- Partition ---

    #[test]
    fn test_partition_new() {
        let p = Partition::new(1, "node-a", 100, 1000);
        assert_eq!(p.id, 1);
        assert_eq!(p.node_id, "node-a");
        assert_eq!(p.triple_count, 100);
        assert_eq!(p.byte_size, 1000);
    }

    #[test]
    fn test_partition_clone() {
        let p = Partition::new(5, "n", 50, 500);
        assert_eq!(p.clone(), p);
    }

    // --- PartitionMove ---

    #[test]
    fn test_partition_move_transfer_size() {
        let m = PartitionMove {
            partition_id: 1,
            from_node: "a".to_string(),
            to_node: "b".to_string(),
            triple_count: 1000,
        };
        assert_eq!(m.transfer_size(), 1000);
    }

    #[test]
    fn test_partition_move_clone() {
        let m = PartitionMove {
            partition_id: 2,
            from_node: "x".to_string(),
            to_node: "y".to_string(),
            triple_count: 500,
        };
        assert_eq!(m.clone(), m);
    }

    // --- RebalancePlan ---

    #[test]
    fn test_rebalance_plan_is_noop() {
        let plan = RebalancePlan {
            moves: vec![],
            expected_improvement: 0.0,
        };
        assert!(plan.is_noop());
    }

    #[test]
    fn test_rebalance_plan_is_not_noop() {
        let m = PartitionMove {
            partition_id: 1,
            from_node: "a".to_string(),
            to_node: "b".to_string(),
            triple_count: 100,
        };
        let plan = RebalancePlan {
            moves: vec![m],
            expected_improvement: 0.5,
        };
        assert!(!plan.is_noop());
    }

    #[test]
    fn test_rebalance_plan_total_transfer() {
        let moves = vec![
            PartitionMove {
                partition_id: 1,
                from_node: "a".into(),
                to_node: "b".into(),
                triple_count: 200,
            },
            PartitionMove {
                partition_id: 2,
                from_node: "a".into(),
                to_node: "c".into(),
                triple_count: 300,
            },
        ];
        let plan = RebalancePlan {
            moves,
            expected_improvement: 0.3,
        };
        assert_eq!(plan.total_transfer(), 500);
    }

    // --- NodeLoad ---

    #[test]
    fn test_node_load_new() {
        let nl = NodeLoad::new("node-1", 5, 10_000);
        assert_eq!(nl.node_id, "node-1");
        assert_eq!(nl.partition_count, 5);
        assert_eq!(nl.total_triples, 10_000);
    }

    // --- imbalance_score ---

    #[test]
    fn test_imbalance_score_balanced() {
        let nodes = vec![
            NodeLoad::new("a", 1, 1000),
            NodeLoad::new("b", 1, 1000),
            NodeLoad::new("c", 1, 1000),
        ];
        let score = PartitionRebalancer::imbalance_score(&nodes);
        assert!((score - 0.0).abs() < 1e-9);
    }

    #[test]
    fn test_imbalance_score_unbalanced() {
        let nodes = make_nodes();
        let score = PartitionRebalancer::imbalance_score(&nodes);
        assert!(score > 0.0);
    }

    #[test]
    fn test_imbalance_score_empty() {
        let score = PartitionRebalancer::imbalance_score(&[]);
        assert!((score - 0.0).abs() < 1e-9);
    }

    #[test]
    fn test_imbalance_score_single_node() {
        let nodes = vec![NodeLoad::new("a", 1, 5000)];
        let score = PartitionRebalancer::imbalance_score(&nodes);
        assert!((score - 0.0).abs() < 1e-9);
    }

    #[test]
    fn test_imbalance_score_all_zero_loads() {
        let nodes = vec![NodeLoad::new("a", 0, 0), NodeLoad::new("b", 0, 0)];
        let score = PartitionRebalancer::imbalance_score(&nodes);
        assert!((score - 0.0).abs() < 1e-9);
    }

    // --- is_balanced ---

    #[test]
    fn test_is_balanced_true() {
        let nodes = vec![NodeLoad::new("a", 1, 1000), NodeLoad::new("b", 1, 1000)];
        assert!(PartitionRebalancer::is_balanced(&nodes, 0.1));
    }

    #[test]
    fn test_is_balanced_false() {
        let nodes = make_nodes();
        assert!(!PartitionRebalancer::is_balanced(&nodes, 0.01));
    }

    #[test]
    fn test_is_balanced_threshold_zero() {
        let nodes = vec![NodeLoad::new("a", 1, 1000), NodeLoad::new("b", 1, 1001)];
        assert!(!PartitionRebalancer::is_balanced(&nodes, 0.0));
    }

    // --- node_loads ---

    #[test]
    fn test_node_loads_aggregates_correctly() {
        let partitions = make_partitions();
        let loads = PartitionRebalancer::node_loads(&partitions);
        assert_eq!(loads["node-a"].partition_count, 3);
        assert_eq!(loads["node-a"].total_triples, 6000);
        assert_eq!(loads["node-b"].partition_count, 1);
        assert_eq!(loads["node-b"].total_triples, 500);
    }

    #[test]
    fn test_node_loads_empty_partitions() {
        let loads = PartitionRebalancer::node_loads(&[]);
        assert!(loads.is_empty());
    }

    #[test]
    fn test_node_loads_single_partition() {
        let partitions = vec![Partition::new(1, "n1", 200, 2000)];
        let loads = PartitionRebalancer::node_loads(&partitions);
        assert_eq!(loads["n1"].total_triples, 200);
    }

    // --- compute_plan (LeastLoaded) ---

    #[test]
    fn test_compute_plan_least_loaded_returns_plan() {
        let rebalancer = PartitionRebalancer::new(RebalanceStrategy::LeastLoaded, 5);
        let partitions = make_partitions();
        let nodes = make_nodes();
        let plan = rebalancer.compute_plan(&partitions, &nodes);
        // Should produce at least one move given the imbalance.
        assert!(!plan.is_noop());
    }

    #[test]
    fn test_compute_plan_respects_max_moves() {
        let rebalancer = PartitionRebalancer::new(RebalanceStrategy::LeastLoaded, 1);
        let partitions = make_partitions();
        let nodes = make_nodes();
        let plan = rebalancer.compute_plan(&partitions, &nodes);
        assert!(plan.moves.len() <= 1);
    }

    #[test]
    fn test_compute_plan_move_sources_from_heavy_node() {
        let rebalancer = PartitionRebalancer::new(RebalanceStrategy::LeastLoaded, 3);
        let partitions = make_partitions();
        let nodes = make_nodes();
        let plan = rebalancer.compute_plan(&partitions, &nodes);
        if let Some(m) = plan.moves.first() {
            assert_eq!(m.from_node, "node-a");
        }
    }

    #[test]
    fn test_compute_plan_expected_improvement_non_negative() {
        let rebalancer = PartitionRebalancer::new(RebalanceStrategy::LeastLoaded, 5);
        let partitions = make_partitions();
        let nodes = make_nodes();
        let plan = rebalancer.compute_plan(&partitions, &nodes);
        assert!(plan.expected_improvement >= 0.0);
    }

    #[test]
    fn test_compute_plan_empty_nodes() {
        let rebalancer = PartitionRebalancer::new(RebalanceStrategy::LeastLoaded, 5);
        let partitions = make_partitions();
        let plan = rebalancer.compute_plan(&partitions, &[]);
        assert!(plan.is_noop());
    }

    #[test]
    fn test_compute_plan_empty_partitions() {
        let rebalancer = PartitionRebalancer::new(RebalanceStrategy::LeastLoaded, 5);
        let nodes = make_nodes();
        let plan = rebalancer.compute_plan(&[], &nodes);
        assert!(plan.is_noop());
    }

    #[test]
    fn test_compute_plan_already_balanced() {
        let rebalancer = PartitionRebalancer::new(RebalanceStrategy::LeastLoaded, 5);
        let partitions = vec![
            Partition::new(1, "a", 1000, 1000),
            Partition::new(2, "b", 1000, 1000),
        ];
        let nodes = vec![NodeLoad::new("a", 1, 1000), NodeLoad::new("b", 1, 1000)];
        let plan = rebalancer.compute_plan(&partitions, &nodes);
        // Already balanced → no moves needed.
        assert!(plan.is_noop());
    }

    // --- compute_plan (RoundRobin) ---

    #[test]
    fn test_compute_plan_round_robin() {
        let rebalancer = PartitionRebalancer::new(RebalanceStrategy::RoundRobin, 10);
        let partitions = make_partitions();
        let nodes = make_nodes();
        let plan = rebalancer.compute_plan(&partitions, &nodes);
        assert!(plan.moves.len() <= 10);
    }

    #[test]
    fn test_compute_plan_round_robin_single_node() {
        let rebalancer = PartitionRebalancer::new(RebalanceStrategy::RoundRobin, 5);
        let partitions = vec![Partition::new(1, "a", 100, 1000)];
        let nodes = vec![NodeLoad::new("a", 1, 100)];
        let plan = rebalancer.compute_plan(&partitions, &nodes);
        assert!(plan.is_noop());
    }

    // --- compute_plan (WeightedCapacity) ---

    #[test]
    fn test_compute_plan_weighted_capacity() {
        let rebalancer = PartitionRebalancer::new(RebalanceStrategy::WeightedCapacity, 5);
        let partitions = make_partitions();
        let nodes = make_nodes();
        let plan = rebalancer.compute_plan(&partitions, &nodes);
        assert!(plan.expected_improvement >= 0.0);
    }

    // --- RebalanceStrategy variants ---

    #[test]
    fn test_strategy_clone_and_eq() {
        assert_eq!(
            RebalanceStrategy::LeastLoaded.clone(),
            RebalanceStrategy::LeastLoaded
        );
        assert_ne!(
            RebalanceStrategy::LeastLoaded,
            RebalanceStrategy::RoundRobin
        );
    }

    #[test]
    fn test_strategy_debug() {
        let s = format!("{:?}", RebalanceStrategy::WeightedCapacity);
        assert!(s.contains("WeightedCapacity"));
    }
}