2020-11-20 20:15:24 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
use garage_util::data::*;
|
|
|
|
|
2020-11-23 16:49:21 +00:00
|
|
|
/// Conflict-free replicated data type (CRDT)
|
|
|
|
///
|
|
|
|
/// CRDT are a type of data structures that do not require coordination.
|
|
|
|
/// In other words, we can edit them in parallel, we will always
|
|
|
|
/// find a way to merge it.
|
|
|
|
///
|
|
|
|
/// A general example is a counter. Its initial value is 0.
|
|
|
|
/// Alice and Bob get a copy of the counter.
|
|
|
|
/// Alice does +1 on her copy, she reads 1.
|
|
|
|
/// Bob does +3 on his copy, he reads 3.
|
|
|
|
/// Now, it is easy to merge their counters, order does not count:
|
|
|
|
/// we always get 4.
|
|
|
|
///
|
|
|
|
/// Learn more about CRDT [on Wikipedia](https://en.wikipedia.org/wiki/Conflict-free_replicated_data_type)
|
2020-11-20 20:15:24 +00:00
|
|
|
pub trait CRDT {
|
2020-11-23 16:49:21 +00:00
|
|
|
/// Merge the two datastructures according to the CRDT rules
|
|
|
|
///
|
|
|
|
/// # Arguments
|
|
|
|
///
|
|
|
|
/// * `other` - the other copy of the CRDT
|
2020-11-20 20:15:24 +00:00
|
|
|
fn merge(&mut self, other: &Self);
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> CRDT for T
|
2020-11-20 22:01:12 +00:00
|
|
|
where
|
|
|
|
T: Ord + Clone,
|
|
|
|
{
|
2020-11-20 20:15:24 +00:00
|
|
|
fn merge(&mut self, other: &Self) {
|
|
|
|
if other > self {
|
|
|
|
*self = other.clone();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ---- LWW Register ----
|
|
|
|
|
2020-11-23 16:49:21 +00:00
|
|
|
/// Last Write Win (LWW)
|
|
|
|
///
|
2020-11-23 17:17:48 +00:00
|
|
|
/// LWW is based on time, the most recent write wins.
|
|
|
|
/// As multiple computers clocks are always desynchronized,
|
|
|
|
/// when operations are close enough, it is equivalent to
|
|
|
|
/// take one copy and drop the other one.
|
|
|
|
///
|
|
|
|
/// Given that clocks are not too desynchronized, this assumption
|
|
|
|
/// is enough for most cases, as there is few chance that two humans
|
|
|
|
/// coordonate themself faster than the time difference between two NTP servers.
|
|
|
|
///
|
|
|
|
/// As a more concret example, let's suppose you want to upload a file
|
|
|
|
/// with the same key (path) in the same bucket at the very same time.
|
|
|
|
/// For each request, the file will be timestamped by the receiving server
|
|
|
|
/// and may differ from what you observed with your atomic clock!
|
|
|
|
///
|
|
|
|
/// This scheme is used by AWS S3 or Soundcloud and often without knowing
|
|
|
|
/// in entreprise when reconciliating databases with ad-hoc scripts.
|
2020-11-20 20:15:24 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
2020-11-20 22:01:12 +00:00
|
|
|
pub struct LWW<T> {
|
2020-11-20 20:15:24 +00:00
|
|
|
ts: u64,
|
|
|
|
v: T,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> LWW<T>
|
2020-11-20 22:01:12 +00:00
|
|
|
where
|
2020-11-20 22:23:55 +00:00
|
|
|
T: CRDT,
|
2020-11-20 20:15:24 +00:00
|
|
|
{
|
2020-11-23 17:17:48 +00:00
|
|
|
/// Creates a new CRDT
|
|
|
|
///
|
|
|
|
/// CRDT's internal timestamp is set with current node's clock.
|
2020-11-20 20:15:24 +00:00
|
|
|
pub fn new(value: T) -> Self {
|
|
|
|
Self {
|
|
|
|
ts: now_msec(),
|
|
|
|
v: value,
|
|
|
|
}
|
|
|
|
}
|
2020-11-23 17:17:48 +00:00
|
|
|
|
|
|
|
/// Build a new CRDT from a previous non-compatible one
|
|
|
|
///
|
|
|
|
/// Compared to new, the CRDT's timestamp is not set to now
|
|
|
|
/// but must be set to the previous, non-compatible, CRDT's timestamp.
|
2020-11-20 20:15:24 +00:00
|
|
|
pub fn migrate_from_raw(ts: u64, value: T) -> Self {
|
2020-11-20 22:01:12 +00:00
|
|
|
Self { ts, v: value }
|
2020-11-20 20:15:24 +00:00
|
|
|
}
|
2020-11-23 17:17:48 +00:00
|
|
|
|
|
|
|
/// Update the LWW CRDT while keeping some causal ordering.
|
2020-11-20 20:15:24 +00:00
|
|
|
pub fn update(&mut self, new_value: T) {
|
|
|
|
self.ts = std::cmp::max(self.ts + 1, now_msec());
|
|
|
|
self.v = new_value;
|
|
|
|
}
|
2020-11-23 17:17:48 +00:00
|
|
|
|
|
|
|
/// Get the CRDT value
|
2020-11-20 20:15:24 +00:00
|
|
|
pub fn get(&self) -> &T {
|
|
|
|
&self.v
|
|
|
|
}
|
2020-11-23 17:17:48 +00:00
|
|
|
|
|
|
|
/// Get a mutable value for the CRDT
|
2020-11-20 22:01:12 +00:00
|
|
|
pub fn get_mut(&mut self) -> &mut T {
|
|
|
|
&mut self.v
|
|
|
|
}
|
2020-11-20 20:15:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> CRDT for LWW<T>
|
2020-11-20 22:01:12 +00:00
|
|
|
where
|
2020-11-20 22:23:55 +00:00
|
|
|
T: Clone + CRDT,
|
2020-11-20 20:15:24 +00:00
|
|
|
{
|
|
|
|
fn merge(&mut self, other: &Self) {
|
|
|
|
if other.ts > self.ts {
|
|
|
|
self.ts = other.ts;
|
|
|
|
self.v = other.v.clone();
|
|
|
|
} else if other.ts == self.ts {
|
|
|
|
self.v.merge(&other.v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 16:49:21 +00:00
|
|
|
/// Boolean
|
|
|
|
///
|
|
|
|
/// with True as absorbing state
|
2020-11-20 20:15:24 +00:00
|
|
|
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
|
|
|
|
pub struct Bool(bool);
|
|
|
|
|
|
|
|
impl Bool {
|
|
|
|
pub fn new(b: bool) -> Self {
|
|
|
|
Self(b)
|
|
|
|
}
|
|
|
|
pub fn set(&mut self) {
|
|
|
|
self.0 = true;
|
|
|
|
}
|
|
|
|
pub fn get(&self) -> bool {
|
|
|
|
self.0
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CRDT for Bool {
|
|
|
|
fn merge(&mut self, other: &Self) {
|
|
|
|
self.0 = self.0 || other.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 16:49:21 +00:00
|
|
|
/// Last Write Win Map
|
|
|
|
///
|
|
|
|
///
|
2020-11-20 20:15:24 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize, PartialEq)]
|
2020-11-20 22:01:12 +00:00
|
|
|
pub struct LWWMap<K, V> {
|
2020-11-20 20:15:24 +00:00
|
|
|
vals: Vec<(K, u64, V)>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K, V> LWWMap<K, V>
|
2020-11-20 22:01:12 +00:00
|
|
|
where
|
2020-11-20 22:23:55 +00:00
|
|
|
K: Ord,
|
|
|
|
V: CRDT,
|
2020-11-20 20:15:24 +00:00
|
|
|
{
|
|
|
|
pub fn new() -> Self {
|
2020-11-20 22:01:12 +00:00
|
|
|
Self { vals: vec![] }
|
2020-11-20 20:15:24 +00:00
|
|
|
}
|
|
|
|
pub fn migrate_from_raw_item(k: K, ts: u64, v: V) -> Self {
|
2020-11-20 22:01:12 +00:00
|
|
|
Self {
|
2020-11-20 20:15:24 +00:00
|
|
|
vals: vec![(k, ts, v)],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn take_and_clear(&mut self) -> Self {
|
|
|
|
let vals = std::mem::replace(&mut self.vals, vec![]);
|
2020-11-20 22:01:12 +00:00
|
|
|
Self { vals }
|
2020-11-20 20:15:24 +00:00
|
|
|
}
|
|
|
|
pub fn clear(&mut self) {
|
|
|
|
self.vals.clear();
|
|
|
|
}
|
|
|
|
pub fn update_mutator(&self, k: K, new_v: V) -> Self {
|
2020-11-20 22:01:12 +00:00
|
|
|
let new_vals = match self.vals.binary_search_by(|(k2, _, _)| k2.cmp(&k)) {
|
2020-11-20 20:15:24 +00:00
|
|
|
Ok(i) => {
|
|
|
|
let (_, old_ts, _) = self.vals[i];
|
2020-11-20 22:01:12 +00:00
|
|
|
let new_ts = std::cmp::max(old_ts + 1, now_msec());
|
2020-11-20 20:15:24 +00:00
|
|
|
vec![(k, new_ts, new_v)]
|
|
|
|
}
|
2020-11-20 22:01:12 +00:00
|
|
|
Err(_) => vec![(k, now_msec(), new_v)],
|
2020-11-20 20:15:24 +00:00
|
|
|
};
|
2020-11-20 22:01:12 +00:00
|
|
|
Self { vals: new_vals }
|
2020-11-20 20:15:24 +00:00
|
|
|
}
|
|
|
|
pub fn get(&self, k: &K) -> Option<&V> {
|
2020-11-20 22:01:12 +00:00
|
|
|
match self.vals.binary_search_by(|(k2, _, _)| k2.cmp(&k)) {
|
2020-11-20 20:15:24 +00:00
|
|
|
Ok(i) => Some(&self.vals[i].2),
|
2020-11-20 22:01:12 +00:00
|
|
|
Err(_) => None,
|
2020-11-20 20:15:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
pub fn items(&self) -> &[(K, u64, V)] {
|
|
|
|
&self.vals[..]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<K, V> CRDT for LWWMap<K, V>
|
2020-11-20 22:01:12 +00:00
|
|
|
where
|
2020-11-20 22:23:55 +00:00
|
|
|
K: Clone + Ord,
|
|
|
|
V: Clone + CRDT,
|
2020-11-20 20:15:24 +00:00
|
|
|
{
|
|
|
|
fn merge(&mut self, other: &Self) {
|
|
|
|
for (k, ts2, v2) in other.vals.iter() {
|
2020-11-20 22:01:12 +00:00
|
|
|
match self.vals.binary_search_by(|(k2, _, _)| k2.cmp(&k)) {
|
2020-11-20 20:15:24 +00:00
|
|
|
Ok(i) => {
|
2020-11-20 22:01:12 +00:00
|
|
|
let (_, ts1, _v1) = &self.vals[i];
|
2020-11-20 20:15:24 +00:00
|
|
|
if ts2 > ts1 {
|
|
|
|
self.vals[i].1 = *ts2;
|
|
|
|
self.vals[i].2 = v2.clone();
|
|
|
|
} else if ts1 == ts2 {
|
|
|
|
self.vals[i].2.merge(&v2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(i) => {
|
|
|
|
self.vals.insert(i, (k.clone(), *ts2, v2.clone()));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|