diff options
author | Alex Auvolat <alex@adnab.me> | 2020-04-19 15:14:23 +0200 |
---|---|---|
committer | Alex Auvolat <alex@adnab.me> | 2020-04-19 15:14:23 +0200 |
commit | 302502f4c10b4c1cd03d3b098b3e55a3f70054f2 (patch) | |
tree | 3e32c8751dc5d62b1723bcc2738aa77f12d45123 /src/table_fullcopy.rs | |
parent | 7131553c53d4414d2da0e9b60e6e3425f1b46ec2 (diff) | |
download | garage-302502f4c10b4c1cd03d3b098b3e55a3f70054f2.tar.gz garage-302502f4c10b4c1cd03d3b098b3e55a3f70054f2.zip |
Add support for fully replicated tables with epidemic dissemination of updates
Diffstat (limited to 'src/table_fullcopy.rs')
-rw-r--r-- | src/table_fullcopy.rs | 100 |
1 files changed, 100 insertions, 0 deletions
diff --git a/src/table_fullcopy.rs b/src/table_fullcopy.rs new file mode 100644 index 00000000..d5194d55 --- /dev/null +++ b/src/table_fullcopy.rs @@ -0,0 +1,100 @@ +use arc_swap::ArcSwapOption; +use std::sync::Arc; + +use crate::data::*; +use crate::membership::{Ring, System}; +use crate::table::*; + +#[derive(Clone)] +pub struct TableFullReplication { + pub write_factor: usize, + pub write_quorum: usize, + + neighbors: ArcSwapOption<Neighbors>, +} + +#[derive(Clone)] +struct Neighbors { + ring: Arc<Ring>, + neighbors: Vec<UUID>, +} + +impl TableFullReplication { + pub fn new(write_factor: usize, write_quorum: usize) -> Self { + TableFullReplication { + write_factor, + write_quorum, + neighbors: ArcSwapOption::from(None), + } + } + + fn get_neighbors(&self, system: &System) -> Vec<UUID> { + let neighbors = self.neighbors.load_full(); + if let Some(n) = neighbors { + if Arc::ptr_eq(&n.ring, &system.ring.borrow()) { + return n.neighbors.clone(); + } + } + + // Recalculate neighbors + let ring = system.ring.borrow().clone(); + let my_id = system.id.clone(); + + let mut nodes = vec![]; + for (node, _) in ring.config.members.iter() { + let node_ranking = hash(&[node.as_slice(), my_id.as_slice()].concat()); + nodes.push((node.clone(), node_ranking)); + } + nodes.sort_by(|(_, rank1), (_, rank2)| rank1.cmp(rank2)); + let mut neighbors = nodes + .drain(..) + .map(|(node, _)| node) + .filter(|node| *node != my_id) + .take(self.write_factor) + .collect::<Vec<_>>(); + neighbors.push(my_id); + self.neighbors.swap(Some(Arc::new(Neighbors { + ring, + neighbors: neighbors.clone(), + }))); + neighbors + } +} + +impl TableReplication for TableFullReplication { + // Full replication schema: all nodes store everything + // Writes are disseminated in an epidemic manner in the network + + // Advantage: do all reads locally, extremely fast + // Inconvenient: only suitable to reasonably small tables + + fn read_nodes(&self, _hash: &Hash, system: &System) -> Vec<UUID> { + vec![system.id.clone()] + } + fn read_quorum(&self) -> usize { + 1 + } + + fn write_nodes(&self, _hash: &Hash, system: &System) -> Vec<UUID> { + self.get_neighbors(system) + } + fn write_quorum(&self) -> usize { + self.write_quorum + } + fn max_write_errors(&self) -> usize { + self.write_factor - self.write_quorum + } + fn epidemic_writes(&self) -> bool { + true + } + + fn replication_nodes(&self, _hash: &Hash, ring: &Ring) -> Vec<UUID> { + ring.config.members.keys().cloned().collect::<Vec<_>>() + } + fn split_points(&self, _ring: &Ring) -> Vec<Hash> { + let mut ret = vec![]; + ret.push([0u8; 32].into()); + ret.push([0xFFu8; 32].into()); + ret + } +} |