Apply cargo fmt
This commit is contained in:
parent
c1d1646c4d
commit
2aeaddd5e2
2 changed files with 795 additions and 745 deletions
|
@ -1,12 +1,12 @@
|
|||
use std::cmp::min;
|
||||
use std::cmp::Ordering;
|
||||
use std::cmp::{min};
|
||||
use std::collections::{HashMap};
|
||||
use std::collections::HashMap;
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use garage_util::bipartite::*;
|
||||
use garage_util::crdt::{AutoCrdt, Crdt, LwwMap};
|
||||
use garage_util::data::*;
|
||||
use garage_util::bipartite::*;
|
||||
|
||||
use rand::prelude::SliceRandom;
|
||||
|
||||
|
@ -168,7 +168,6 @@ impl ClusterLayout {
|
|||
true
|
||||
}
|
||||
|
||||
|
||||
/// This function calculates a new partition-to-node assignation.
|
||||
/// The computed assignation maximizes the capacity of a
|
||||
/// partition (assuming all partitions have the same size).
|
||||
|
@ -177,7 +176,6 @@ impl ClusterLayout {
|
|||
/// data to be moved. A heuristic ensures node triplets
|
||||
/// dispersion (in garage_util::bipartite::optimize_matching()).
|
||||
pub fn calculate_partition_assignation(&mut self) -> bool {
|
||||
|
||||
//The nodes might have been updated, some might have been deleted.
|
||||
//So we need to first update the list of nodes and retrieve the
|
||||
//assignation.
|
||||
|
@ -191,7 +189,8 @@ impl ClusterLayout {
|
|||
//We collect part_per_zone in a vec to not rely on the
|
||||
//arbitrary order in which elements are iterated in
|
||||
//Hashmap::iter()
|
||||
let part_per_zone_vec = part_per_zone.iter()
|
||||
let part_per_zone_vec = part_per_zone
|
||||
.iter()
|
||||
.map(|(x, y)| (x.clone(), *y))
|
||||
.collect::<Vec<(String, usize)>>();
|
||||
//We create an indexing of the zones
|
||||
|
@ -206,15 +205,11 @@ impl ClusterLayout {
|
|||
let nb_nodes = part_per_nod.len();
|
||||
let nb_partitions = 1 << PARTITION_BITS;
|
||||
let left_cap_vec = vec![self.replication_factor as u32; nb_partitions];
|
||||
let right_cap_vec = part_per_zone_vec.iter().map(|(_,y)| *y as u32)
|
||||
.collect();
|
||||
let mut zone_assignation =
|
||||
dinic_compute_matching(left_cap_vec, right_cap_vec);
|
||||
|
||||
let right_cap_vec = part_per_zone_vec.iter().map(|(_, y)| *y as u32).collect();
|
||||
let mut zone_assignation = dinic_compute_matching(left_cap_vec, right_cap_vec);
|
||||
|
||||
//We create the structure for the partition-to-node assignation.
|
||||
let mut node_assignation =
|
||||
vec![vec![None; self.replication_factor ];nb_partitions];
|
||||
let mut node_assignation = vec![vec![None; self.replication_factor]; nb_partitions];
|
||||
//We will decrement part_per_nod to keep track of the number
|
||||
//of partitions that we still have to associate.
|
||||
let mut part_per_nod = part_per_nod.clone();
|
||||
|
@ -224,27 +219,33 @@ impl ClusterLayout {
|
|||
//We get the id of the zones of the former assignation
|
||||
//(and the id no_zone if there is no node assignated)
|
||||
let no_zone = part_per_zone_vec.len();
|
||||
let old_zone_assignation : Vec<Vec<usize>> =
|
||||
old_node_assignation.iter().map(|x| x.iter().map(
|
||||
|id| match *id { Some(i) => zone_id[&node_zone[i]] ,
|
||||
None => no_zone }
|
||||
).collect()).collect();
|
||||
let old_zone_assignation: Vec<Vec<usize>> = old_node_assignation
|
||||
.iter()
|
||||
.map(|x| {
|
||||
x.iter()
|
||||
.map(|id| match *id {
|
||||
Some(i) => zone_id[&node_zone[i]],
|
||||
None => no_zone,
|
||||
})
|
||||
.collect()
|
||||
})
|
||||
.collect();
|
||||
|
||||
//We minimize the distance to the former zone assignation
|
||||
zone_assignation = optimize_matching(
|
||||
&old_zone_assignation, &zone_assignation, nb_zones+1); //+1 for no_zone
|
||||
zone_assignation =
|
||||
optimize_matching(&old_zone_assignation, &zone_assignation, nb_zones + 1); //+1 for no_zone
|
||||
|
||||
//We need to assign partitions to nodes in their zone
|
||||
//We first put the nodes assignation that can stay the same
|
||||
for i in 0..nb_partitions {
|
||||
for j in 0..self.replication_factor {
|
||||
if let Some(Some(former_node)) = old_node_assignation[i].iter().find(
|
||||
|x| if let Some(id) = x {
|
||||
if let Some(Some(former_node)) = old_node_assignation[i].iter().find(|x| {
|
||||
if let Some(id) = x {
|
||||
zone_id[&node_zone[*id]] == zone_assignation[i][j]
|
||||
} else {
|
||||
false
|
||||
}
|
||||
else {false}
|
||||
)
|
||||
{
|
||||
}) {
|
||||
if part_per_nod[*former_node] > 0 {
|
||||
node_assignation[i][j] = Some(*former_node);
|
||||
part_per_nod[*former_node] -= 1;
|
||||
|
@ -253,16 +254,17 @@ impl ClusterLayout {
|
|||
}
|
||||
}
|
||||
|
||||
|
||||
//We complete the assignation of partitions to nodes
|
||||
let mut rng = rand::thread_rng();
|
||||
for i in 0..nb_partitions {
|
||||
for j in 0..self.replication_factor {
|
||||
if node_assignation[i][j] == None {
|
||||
let possible_nodes: Vec<usize> = (0..nb_nodes)
|
||||
.filter(
|
||||
|id| zone_id[&node_zone[*id]] == zone_assignation[i][j]
|
||||
&& part_per_nod[*id] > 0).collect();
|
||||
.filter(|id| {
|
||||
zone_id[&node_zone[*id]] == zone_assignation[i][j]
|
||||
&& part_per_nod[*id] > 0
|
||||
})
|
||||
.collect();
|
||||
assert!(possible_nodes.len() > 0);
|
||||
//We randomly pick a node
|
||||
if let Some(nod) = possible_nodes.choose(&mut rng) {
|
||||
|
@ -279,14 +281,16 @@ impl ClusterLayout {
|
|||
for j in 0..self.replication_factor {
|
||||
if let Some(id) = node_assignation[i][j] {
|
||||
self.ring_assignation_data.push(id as CompactNodeType);
|
||||
} else {
|
||||
assert!(false)
|
||||
}
|
||||
else {assert!(false)}
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
else { false }
|
||||
}
|
||||
|
||||
/// The LwwMap of node roles might have changed. This function updates the node_id_vec
|
||||
|
@ -296,19 +300,17 @@ impl ClusterLayout {
|
|||
/// do modify assignation_ring and node_id_vec.
|
||||
fn update_nodes_and_ring(&mut self) -> Vec<Vec<Option<usize>>> {
|
||||
let nb_partitions = 1usize << PARTITION_BITS;
|
||||
let mut node_assignation =
|
||||
vec![vec![None; self.replication_factor ];nb_partitions];
|
||||
let mut node_assignation = vec![vec![None; self.replication_factor]; nb_partitions];
|
||||
let rf = self.replication_factor;
|
||||
let ring = &self.ring_assignation_data;
|
||||
|
||||
let new_node_id_vec : Vec::<Uuid> = self.roles.items().iter()
|
||||
.map(|(k, _, _)| *k)
|
||||
.collect();
|
||||
let new_node_id_vec: Vec<Uuid> = self.roles.items().iter().map(|(k, _, _)| *k).collect();
|
||||
|
||||
if ring.len() == rf * nb_partitions {
|
||||
for i in 0..nb_partitions {
|
||||
for j in 0..self.replication_factor {
|
||||
node_assignation[i][j] = new_node_id_vec.iter()
|
||||
node_assignation[i][j] = new_node_id_vec
|
||||
.iter()
|
||||
.position(|id| *id == self.node_id_vec[ring[i * rf + j] as usize]);
|
||||
}
|
||||
}
|
||||
|
@ -324,18 +326,18 @@ impl ClusterLayout {
|
|||
///self.replication_factor times and the capacity of a partition
|
||||
///is maximized.
|
||||
fn optimal_proportions(&mut self) -> Option<(Vec<usize>, HashMap<String, usize>)> {
|
||||
|
||||
let mut zone_capacity: HashMap<String, u32> = HashMap::new();
|
||||
|
||||
let (node_zone, node_capacity) = self.get_node_zone_capacity();
|
||||
let nb_nodes = self.node_id_vec.len();
|
||||
|
||||
for i in 0..nb_nodes
|
||||
{
|
||||
for i in 0..nb_nodes {
|
||||
if zone_capacity.contains_key(&node_zone[i]) {
|
||||
zone_capacity.insert(node_zone[i].clone(), zone_capacity[&node_zone[i]] + node_capacity[i]);
|
||||
}
|
||||
else{
|
||||
zone_capacity.insert(
|
||||
node_zone[i].clone(),
|
||||
zone_capacity[&node_zone[i]] + node_capacity[i],
|
||||
);
|
||||
} else {
|
||||
zone_capacity.insert(node_zone[i].clone(), node_capacity[i]);
|
||||
}
|
||||
}
|
||||
|
@ -355,37 +357,52 @@ impl ClusterLayout {
|
|||
//However, a large zone can be associated to at most
|
||||
//nb_partitions to ensure replication of the date.
|
||||
//So we take the min with nb_partitions:
|
||||
let mut part_per_zone : HashMap<String, usize> =
|
||||
zone_capacity.iter()
|
||||
.map(|(k, v)| (k.clone(), min(nb_partitions,
|
||||
(self.replication_factor*nb_partitions
|
||||
**v as usize)/sum_capacities as usize) ) ).collect();
|
||||
let mut part_per_zone: HashMap<String, usize> = zone_capacity
|
||||
.iter()
|
||||
.map(|(k, v)| {
|
||||
(
|
||||
k.clone(),
|
||||
min(
|
||||
nb_partitions,
|
||||
(self.replication_factor * nb_partitions * *v as usize)
|
||||
/ sum_capacities as usize,
|
||||
),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
|
||||
//The replication_factor-1 upper bounds the number of
|
||||
//part_per_zones that are greater than nb_partitions
|
||||
for _ in 1..self.replication_factor {
|
||||
//The number of partitions that are not assignated to
|
||||
//a zone that takes nb_partitions.
|
||||
let sum_capleft : u32 = zone_capacity.keys()
|
||||
.filter(| k | {part_per_zone[*k] < nb_partitions} )
|
||||
.map(|k| zone_capacity[k]).sum();
|
||||
let sum_capleft: u32 = zone_capacity
|
||||
.keys()
|
||||
.filter(|k| part_per_zone[*k] < nb_partitions)
|
||||
.map(|k| zone_capacity[k])
|
||||
.sum();
|
||||
|
||||
//The number of replication of the data that we need
|
||||
//to ensure.
|
||||
let repl_left = self.replication_factor
|
||||
- part_per_zone.values()
|
||||
.filter(|x| {**x == nb_partitions})
|
||||
- part_per_zone
|
||||
.values()
|
||||
.filter(|x| **x == nb_partitions)
|
||||
.count();
|
||||
if repl_left == 0 {
|
||||
break;
|
||||
}
|
||||
|
||||
for k in zone_capacity.keys() {
|
||||
if part_per_zone[k] != nb_partitions
|
||||
{
|
||||
part_per_zone.insert(k.to_string() , min(nb_partitions,
|
||||
(nb_partitions*zone_capacity[k] as usize
|
||||
*repl_left)/sum_capleft as usize));
|
||||
if part_per_zone[k] != nb_partitions {
|
||||
part_per_zone.insert(
|
||||
k.to_string(),
|
||||
min(
|
||||
nb_partitions,
|
||||
(nb_partitions * zone_capacity[k] as usize * repl_left)
|
||||
/ sum_capleft as usize,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -393,20 +410,21 @@ impl ClusterLayout {
|
|||
//Now we divide the zone's partition share proportionally
|
||||
//between their nodes.
|
||||
|
||||
let mut part_per_nod : Vec<usize> = (0..nb_nodes).map(
|
||||
|i| (part_per_zone[&node_zone[i]]*node_capacity[i] as usize)/zone_capacity[&node_zone[i]] as usize
|
||||
)
|
||||
let mut part_per_nod: Vec<usize> = (0..nb_nodes)
|
||||
.map(|i| {
|
||||
(part_per_zone[&node_zone[i]] * node_capacity[i] as usize)
|
||||
/ zone_capacity[&node_zone[i]] as usize
|
||||
})
|
||||
.collect();
|
||||
|
||||
//We must update the part_per_zone to make it correspond to
|
||||
//part_per_nod (because of integer rounding)
|
||||
part_per_zone = part_per_zone.iter().map(|(k,_)|
|
||||
(k.clone(), 0))
|
||||
.collect();
|
||||
part_per_zone = part_per_zone.iter().map(|(k, _)| (k.clone(), 0)).collect();
|
||||
for i in 0..nb_nodes {
|
||||
part_per_zone.insert(
|
||||
node_zone[i].clone(),
|
||||
part_per_zone[&node_zone[i]] + part_per_nod[i]);
|
||||
part_per_zone[&node_zone[i]] + part_per_nod[i],
|
||||
);
|
||||
}
|
||||
|
||||
//Because of integer rounding, the total sum of part_per_nod
|
||||
|
@ -419,8 +437,7 @@ impl ClusterLayout {
|
|||
// part_per_zone capped
|
||||
|
||||
let discrepancy: usize =
|
||||
nb_partitions*self.replication_factor
|
||||
- part_per_nod.iter().sum::<usize>();
|
||||
nb_partitions * self.replication_factor - part_per_nod.iter().sum::<usize>();
|
||||
|
||||
//We use a stupid O(N^2) algorithm. If the number of nodes
|
||||
//is actually expected to be high, one should optimize this.
|
||||
|
@ -428,64 +445,77 @@ impl ClusterLayout {
|
|||
for _ in 0..discrepancy {
|
||||
if let Some(idmax) = (0..nb_nodes)
|
||||
.filter(|i| part_per_zone[&node_zone[*i]] < nb_partitions)
|
||||
.max_by( |i,j|
|
||||
.max_by(|i, j| {
|
||||
(node_capacity[*i] * (part_per_nod[*j] + 1) as u32)
|
||||
.cmp(&(node_capacity[*j] * (part_per_nod[*i] + 1) as u32))
|
||||
)
|
||||
{
|
||||
}) {
|
||||
part_per_nod[idmax] += 1;
|
||||
part_per_zone.insert(node_zone[idmax].clone(),part_per_zone[&node_zone[idmax]]+1);
|
||||
part_per_zone.insert(
|
||||
node_zone[idmax].clone(),
|
||||
part_per_zone[&node_zone[idmax]] + 1,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
//We check the algorithm consistency
|
||||
|
||||
let discrepancy: usize =
|
||||
nb_partitions*self.replication_factor
|
||||
- part_per_nod.iter().sum::<usize>();
|
||||
nb_partitions * self.replication_factor - part_per_nod.iter().sum::<usize>();
|
||||
assert!(discrepancy == 0);
|
||||
assert!(if let Some(v) = part_per_zone.values().max()
|
||||
{*v <= nb_partitions} else {false} );
|
||||
assert!(if let Some(v) = part_per_zone.values().max() {
|
||||
*v <= nb_partitions
|
||||
} else {
|
||||
false
|
||||
});
|
||||
|
||||
Some((part_per_nod, part_per_zone))
|
||||
}
|
||||
|
||||
|
||||
//Returns vectors of zone and capacity; indexed by the same (temporary)
|
||||
//indices as node_id_vec.
|
||||
fn get_node_zone_capacity(&self) -> (Vec<String>, Vec<u32>) {
|
||||
let node_zone = self
|
||||
.node_id_vec
|
||||
.iter()
|
||||
.map(|id_nod| match self.node_role(id_nod) {
|
||||
Some(NodeRole {
|
||||
zone,
|
||||
capacity: _,
|
||||
tags: _,
|
||||
}) => zone.clone(),
|
||||
_ => "".to_string(),
|
||||
})
|
||||
.collect();
|
||||
|
||||
let node_zone = self.node_id_vec.iter().map(
|
||||
|id_nod| match self.node_role(id_nod) {
|
||||
Some(NodeRole{zone,capacity:_,tags:_}) => zone.clone() ,
|
||||
_ => "".to_string()
|
||||
let node_capacity = self
|
||||
.node_id_vec
|
||||
.iter()
|
||||
.map(|id_nod| match self.node_role(id_nod) {
|
||||
Some(NodeRole {
|
||||
zone: _,
|
||||
capacity,
|
||||
tags: _,
|
||||
}) => {
|
||||
if let Some(c) = capacity {
|
||||
*c
|
||||
} else {
|
||||
0
|
||||
}
|
||||
).collect();
|
||||
|
||||
let node_capacity = self.node_id_vec.iter().map(
|
||||
|id_nod| match self.node_role(id_nod) {
|
||||
Some(NodeRole{zone:_,capacity,tags:_}) =>
|
||||
if let Some(c)=capacity
|
||||
{*c}
|
||||
else {0},
|
||||
_ => 0
|
||||
}
|
||||
).collect();
|
||||
_ => 0,
|
||||
})
|
||||
.collect();
|
||||
|
||||
(node_zone, node_capacity)
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use itertools::Itertools;
|
||||
|
||||
fn check_assignation(cl: &ClusterLayout) {
|
||||
|
||||
//Check that input data has the right format
|
||||
let nb_partitions = 1usize << PARTITION_BITS;
|
||||
assert!([1, 2, 3].contains(&cl.replication_factor));
|
||||
|
@ -493,93 +523,117 @@ mod tests {
|
|||
|
||||
let (node_zone, node_capacity) = cl.get_node_zone_capacity();
|
||||
|
||||
|
||||
//Check that is is a correct assignation with zone redundancy
|
||||
let rf = cl.replication_factor;
|
||||
for i in 0..nb_partitions {
|
||||
assert!( rf ==
|
||||
cl.ring_assignation_data[rf*i..rf*(i+1)].iter()
|
||||
assert!(
|
||||
rf == cl.ring_assignation_data[rf * i..rf * (i + 1)]
|
||||
.iter()
|
||||
.map(|nod| node_zone[*nod as usize].clone())
|
||||
.unique()
|
||||
.count() );
|
||||
.count()
|
||||
);
|
||||
}
|
||||
|
||||
let nb_nodes = cl.node_id_vec.len();
|
||||
//Check optimality
|
||||
let node_nb_part =(0..nb_nodes).map(|i| cl.ring_assignation_data
|
||||
let node_nb_part = (0..nb_nodes)
|
||||
.map(|i| {
|
||||
cl.ring_assignation_data
|
||||
.iter()
|
||||
.filter(|x| **x == i as u8)
|
||||
.count())
|
||||
.collect::<Vec::<_>>();
|
||||
.count()
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
let zone_vec = node_zone.iter().unique().collect::<Vec::<_>>();
|
||||
let zone_nb_part = zone_vec.iter().map( |z| cl.ring_assignation_data.iter()
|
||||
let zone_vec = node_zone.iter().unique().collect::<Vec<_>>();
|
||||
let zone_nb_part = zone_vec
|
||||
.iter()
|
||||
.map(|z| {
|
||||
cl.ring_assignation_data
|
||||
.iter()
|
||||
.filter(|x| node_zone[**x as usize] == **z)
|
||||
.count()
|
||||
).collect::<Vec::<_>>();
|
||||
})
|
||||
.collect::<Vec<_>>();
|
||||
|
||||
//Check optimality of the zone assignation : would it be better for the
|
||||
//node_capacity/node_partitions ratio to change the assignation of a partition
|
||||
|
||||
if let Some(idmin) = (0..nb_nodes).min_by(
|
||||
|i,j| (node_capacity[*i]*node_nb_part[*j] as u32)
|
||||
if let Some(idmin) = (0..nb_nodes).min_by(|i, j| {
|
||||
(node_capacity[*i] * node_nb_part[*j] as u32)
|
||||
.cmp(&(node_capacity[*j] * node_nb_part[*i] as u32))
|
||||
){
|
||||
}) {
|
||||
if let Some(idnew) = (0..nb_nodes)
|
||||
.filter( |i| if let Some(p) = zone_vec.iter().position(|z| **z==node_zone[*i])
|
||||
{zone_nb_part[p] < nb_partitions }
|
||||
else { false })
|
||||
.max_by(
|
||||
|i,j| (node_capacity[*i]*(node_nb_part[*j]as u32+1))
|
||||
.cmp(&(node_capacity[*j]*(node_nb_part[*i] as u32+1)))
|
||||
){
|
||||
assert!(node_capacity[idmin]*(node_nb_part[idnew] as u32+1) >=
|
||||
node_capacity[idnew]*node_nb_part[idmin] as u32);
|
||||
.filter(|i| {
|
||||
if let Some(p) = zone_vec.iter().position(|z| **z == node_zone[*i]) {
|
||||
zone_nb_part[p] < nb_partitions
|
||||
} else {
|
||||
false
|
||||
}
|
||||
})
|
||||
.max_by(|i, j| {
|
||||
(node_capacity[*i] * (node_nb_part[*j] as u32 + 1))
|
||||
.cmp(&(node_capacity[*j] * (node_nb_part[*i] as u32 + 1)))
|
||||
}) {
|
||||
assert!(
|
||||
node_capacity[idmin] * (node_nb_part[idnew] as u32 + 1)
|
||||
>= node_capacity[idnew] * node_nb_part[idmin] as u32
|
||||
);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
//In every zone, check optimality of the nod assignation
|
||||
for z in zone_vec {
|
||||
let node_of_z_iter = (0..nb_nodes).filter(|id| node_zone[*id] == *z);
|
||||
if let Some(idmin) = node_of_z_iter.clone().min_by(
|
||||
|i,j| (node_capacity[*i]*node_nb_part[*j] as u32)
|
||||
if let Some(idmin) = node_of_z_iter.clone().min_by(|i, j| {
|
||||
(node_capacity[*i] * node_nb_part[*j] as u32)
|
||||
.cmp(&(node_capacity[*j] * node_nb_part[*i] as u32))
|
||||
){
|
||||
if let Some(idnew) = node_of_z_iter.min_by(
|
||||
|i,j| (node_capacity[*i]*(node_nb_part[*j] as u32+1))
|
||||
}) {
|
||||
if let Some(idnew) = node_of_z_iter.min_by(|i, j| {
|
||||
(node_capacity[*i] * (node_nb_part[*j] as u32 + 1))
|
||||
.cmp(&(node_capacity[*j] * (node_nb_part[*i] as u32 + 1)))
|
||||
}) {
|
||||
assert!(
|
||||
node_capacity[idmin] * (node_nb_part[idnew] as u32 + 1)
|
||||
>= node_capacity[idnew] * node_nb_part[idmin] as u32
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn update_layout(
|
||||
cl: &mut ClusterLayout,
|
||||
node_id_vec: &Vec<u8>,
|
||||
node_capacity_vec: &Vec<u32>,
|
||||
node_zone_vec: &Vec<String>,
|
||||
) {
|
||||
assert!(node_capacity[idmin]*(node_nb_part[idnew] as u32+1) >=
|
||||
node_capacity[idnew]*node_nb_part[idmin] as u32);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
fn update_layout(cl : &mut ClusterLayout, node_id_vec : &Vec<u8>,
|
||||
node_capacity_vec : &Vec<u32> , node_zone_vec : &Vec<String>) {
|
||||
for i in 0..node_id_vec.len() {
|
||||
if let Some(x) = FixedBytes32::try_from(&[i as u8; 32]) {
|
||||
cl.node_id_vec.push(x);
|
||||
}
|
||||
|
||||
let update = cl.roles.update_mutator(cl.node_id_vec[i] ,
|
||||
let update = cl.roles.update_mutator(
|
||||
cl.node_id_vec[i],
|
||||
NodeRoleV(Some(NodeRole {
|
||||
zone: (node_zone_vec[i].to_string()),
|
||||
capacity: (Some(node_capacity_vec[i])),
|
||||
tags : (vec![])})));
|
||||
tags: (vec![]),
|
||||
})),
|
||||
);
|
||||
cl.roles.merge(&update);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_assignation() {
|
||||
|
||||
let mut node_id_vec = vec![1, 2, 3];
|
||||
let mut node_capacity_vec = vec![4000, 1000, 2000];
|
||||
let mut node_zone_vec= vec!["A", "B", "C"].into_iter().map(|x| x.to_string()).collect();
|
||||
let mut node_zone_vec = vec!["A", "B", "C"]
|
||||
.into_iter()
|
||||
.map(|x| x.to_string())
|
||||
.collect();
|
||||
|
||||
let mut cl = ClusterLayout {
|
||||
node_id_vec: vec![],
|
||||
|
@ -598,7 +652,10 @@ mod tests {
|
|||
|
||||
node_id_vec = vec![1, 2, 3, 4, 5, 6, 7, 8, 9];
|
||||
node_capacity_vec = vec![4000, 1000, 1000, 3000, 1000, 1000, 2000, 10000, 2000];
|
||||
node_zone_vec= vec!["A", "B", "C", "C", "C", "B", "G", "H", "I"].into_iter().map(|x| x.to_string()).collect();
|
||||
node_zone_vec = vec!["A", "B", "C", "C", "C", "B", "G", "H", "I"]
|
||||
.into_iter()
|
||||
.map(|x| x.to_string())
|
||||
.collect();
|
||||
update_layout(&mut cl, &node_id_vec, &node_capacity_vec, &node_zone_vec);
|
||||
cl.calculate_partition_assignation();
|
||||
check_assignation(&cl);
|
||||
|
@ -608,14 +665,9 @@ mod tests {
|
|||
cl.calculate_partition_assignation();
|
||||
check_assignation(&cl);
|
||||
|
||||
|
||||
node_capacity_vec = vec![4000, 4000, 2000, 7000, 1000, 9000, 2000, 10, 2000];
|
||||
update_layout(&mut cl, &node_id_vec, &node_capacity_vec, &node_zone_vec);
|
||||
cl.calculate_partition_assignation();
|
||||
check_assignation(&cl);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
|
|
@ -4,9 +4,9 @@
|
|||
* assignation.
|
||||
* */
|
||||
|
||||
use std::cmp::{min,max};
|
||||
use std::collections::VecDeque;
|
||||
use rand::prelude::SliceRandom;
|
||||
use std::cmp::{max, min};
|
||||
use std::collections::VecDeque;
|
||||
|
||||
//Graph data structure for the flow algorithm.
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
|
@ -25,16 +25,16 @@ struct WeightedEdge{
|
|||
v: usize,
|
||||
}
|
||||
|
||||
|
||||
/* This function takes two matchings (old_match and new_match) in a
|
||||
* complete bipartite graph. It returns a matching that has the
|
||||
* same degree as new_match at every vertex, and that is as close
|
||||
* as possible to old_match.
|
||||
* */
|
||||
pub fn optimize_matching( old_match : &Vec<Vec<usize>> ,
|
||||
pub fn optimize_matching(
|
||||
old_match: &Vec<Vec<usize>>,
|
||||
new_match: &Vec<Vec<usize>>,
|
||||
nb_right : usize )
|
||||
-> Vec<Vec<usize>> {
|
||||
nb_right: usize,
|
||||
) -> Vec<Vec<usize>> {
|
||||
let nb_left = old_match.len();
|
||||
let ed = WeightedEdge { w: -1, u: 0, v: 0 };
|
||||
let mut edge_vec = vec![ed; nb_left * nb_right];
|
||||
|
@ -55,8 +55,7 @@ pub fn optimize_matching( old_match : &Vec<Vec<usize>> ,
|
|||
}
|
||||
//We add the new matchings
|
||||
if new_match[edge_vec[i].u].contains(&(edge_vec[i].v - nb_left)) {
|
||||
(edge_vec[i].u,edge_vec[i].v) =
|
||||
(edge_vec[i].v,edge_vec[i].u);
|
||||
(edge_vec[i].u, edge_vec[i].v) = (edge_vec[i].v, edge_vec[i].u);
|
||||
edge_vec[i].w *= -1;
|
||||
}
|
||||
}
|
||||
|
@ -77,12 +76,10 @@ pub fn optimize_matching( old_match : &Vec<Vec<usize>> ,
|
|||
if let Some(cycle) = positive_cycle(&edge_vec, nb_left, nb_right) {
|
||||
for i in cycle {
|
||||
//We flip the edges of the cycle.
|
||||
(edge_vec[i].u,edge_vec[i].v) =
|
||||
(edge_vec[i].v,edge_vec[i].u);
|
||||
(edge_vec[i].u, edge_vec[i].v) = (edge_vec[i].v, edge_vec[i].u);
|
||||
edge_vec[i].w *= -1;
|
||||
}
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
//If there is no cycle, we return the optimal matching.
|
||||
break;
|
||||
}
|
||||
|
@ -99,8 +96,11 @@ pub fn optimize_matching( old_match : &Vec<Vec<usize>> ,
|
|||
}
|
||||
|
||||
//This function finds a positive cycle in a bipartite wieghted graph.
|
||||
fn positive_cycle( edge_vec : &Vec<WeightedEdge>, nb_left : usize,
|
||||
nb_right : usize) -> Option<Vec<usize>> {
|
||||
fn positive_cycle(
|
||||
edge_vec: &Vec<WeightedEdge>,
|
||||
nb_left: usize,
|
||||
nb_right: usize,
|
||||
) -> Option<Vec<usize>> {
|
||||
let nb_side_min = min(nb_left, nb_right);
|
||||
let nb_vertices = nb_left + nb_right;
|
||||
let weight_lowerbound = -((nb_left + nb_right) as i32) - 1;
|
||||
|
@ -164,22 +164,23 @@ fn positive_cycle( edge_vec : &Vec<WeightedEdge>, nb_left : usize,
|
|||
None
|
||||
}
|
||||
|
||||
|
||||
// This function takes two arrays of capacity and computes the
|
||||
// maximal matching in the complete bipartite graph such that the
|
||||
// left vertex i is matched to left_cap_vec[i] right vertices, and
|
||||
// the right vertex j is matched to right_cap_vec[j] left vertices.
|
||||
// To do so, we use Dinic's maximum flow algorithm.
|
||||
pub fn dinic_compute_matching( left_cap_vec : Vec<u32>,
|
||||
right_cap_vec : Vec<u32>) -> Vec< Vec<usize> >
|
||||
{
|
||||
let mut graph = Vec::<Vec::<EdgeFlow> >::new();
|
||||
let ed = EdgeFlow{c:0,flow:0,v:0, rev:0};
|
||||
pub fn dinic_compute_matching(left_cap_vec: Vec<u32>, right_cap_vec: Vec<u32>) -> Vec<Vec<usize>> {
|
||||
let mut graph = Vec::<Vec<EdgeFlow>>::new();
|
||||
let ed = EdgeFlow {
|
||||
c: 0,
|
||||
flow: 0,
|
||||
v: 0,
|
||||
rev: 0,
|
||||
};
|
||||
|
||||
// 0 will be the source
|
||||
graph.push(vec![ed; left_cap_vec.len()]);
|
||||
for i in 0..left_cap_vec.len()
|
||||
{
|
||||
for i in 0..left_cap_vec.len() {
|
||||
graph[0][i].c = left_cap_vec[i] as i32;
|
||||
graph[0][i].v = i + 2;
|
||||
graph[0][i].rev = 0;
|
||||
|
@ -187,8 +188,7 @@ pub fn dinic_compute_matching( left_cap_vec : Vec<u32>,
|
|||
|
||||
//1 will be the sink
|
||||
graph.push(vec![ed; right_cap_vec.len()]);
|
||||
for i in 0..right_cap_vec.len()
|
||||
{
|
||||
for i in 0..right_cap_vec.len() {
|
||||
graph[1][i].c = right_cap_vec[i] as i32;
|
||||
graph[1][i].v = i + 2 + left_cap_vec.len();
|
||||
graph[1][i].rev = 0;
|
||||
|
@ -270,16 +270,14 @@ pub fn dinic_compute_matching( left_cap_vec : Vec<u32>,
|
|||
let flow_upper_bound;
|
||||
if let Some(x) = left_cap_vec.iter().max() {
|
||||
flow_upper_bound = *x as i32;
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
flow_upper_bound = 0;
|
||||
assert!(false);
|
||||
}
|
||||
|
||||
lifo.push_back((0, flow_upper_bound));
|
||||
|
||||
loop
|
||||
{
|
||||
loop {
|
||||
if let Some((id_tmp, f_tmp)) = lifo.back() {
|
||||
let id = *id_tmp;
|
||||
let f = *f_tmp;
|
||||
|
@ -310,29 +308,30 @@ pub fn dinic_compute_matching( left_cap_vec : Vec<u32>,
|
|||
continue;
|
||||
}
|
||||
//else we can try to send flow from id to its nbd
|
||||
let new_flow = min(f,graph[id][nbd].c
|
||||
- graph[id][nbd].flow);
|
||||
if level[graph[id][nbd].v] <= level[id] ||
|
||||
new_flow == 0 {
|
||||
let new_flow = min(f, graph[id][nbd].c - graph[id][nbd].flow);
|
||||
if level[graph[id][nbd].v] <= level[id] || new_flow == 0 {
|
||||
//We cannot send flow to nbd.
|
||||
next_nbd[id] += 1;
|
||||
continue;
|
||||
}
|
||||
//otherwise, we send flow to nbd.
|
||||
lifo.push_back((graph[id][nbd].v, new_flow));
|
||||
}
|
||||
else {
|
||||
} else {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//We return the association
|
||||
let assoc_table = (0..left_cap_vec.len()).map(
|
||||
|id| graph[id+2].iter()
|
||||
let assoc_table = (0..left_cap_vec.len())
|
||||
.map(|id| {
|
||||
graph[id + 2]
|
||||
.iter()
|
||||
.filter(|e| e.flow > 0)
|
||||
.map(|e| e.v - 2 - left_cap_vec.len())
|
||||
.collect()).collect();
|
||||
.collect()
|
||||
})
|
||||
.collect();
|
||||
|
||||
//consistency check
|
||||
|
||||
|
@ -346,20 +345,21 @@ pub fn dinic_compute_matching( left_cap_vec : Vec<u32>,
|
|||
|
||||
//it solves the matching problem
|
||||
for i in 0..left_cap_vec.len() {
|
||||
assert!(left_cap_vec[i] as i32 ==
|
||||
graph[i+2].iter().map(|e| max(0,e.flow)).sum::<i32>());
|
||||
assert!(left_cap_vec[i] as i32 == graph[i + 2].iter().map(|e| max(0, e.flow)).sum::<i32>());
|
||||
}
|
||||
for i in 0..right_cap_vec.len() {
|
||||
assert!(right_cap_vec[i] as i32 ==
|
||||
graph[i+2+left_cap_vec.len()].iter()
|
||||
.map(|e| max(0,e.flow)).sum::<i32>());
|
||||
assert!(
|
||||
right_cap_vec[i] as i32
|
||||
== graph[i + 2 + left_cap_vec.len()]
|
||||
.iter()
|
||||
.map(|e| max(0, e.flow))
|
||||
.sum::<i32>()
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
assoc_table
|
||||
}
|
||||
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -374,5 +374,3 @@ mod tests {
|
|||
|
||||
//maybe add tests relative to the matching optilization ?
|
||||
}
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue