2023-01-10 10:59:57 +00:00
|
|
|
//! Implements a RangeSeenMarker, a data type used in the PollRange API
|
|
|
|
//! to indicate which items in the range have already been seen
|
|
|
|
//! and which have not been seen yet.
|
|
|
|
//!
|
|
|
|
//! It consists of a vector clock that indicates that for each node,
|
|
|
|
//! all items produced by that node with timestamps <= the value in the
|
|
|
|
//! vector clock has been seen, as well as a set of causal contexts for
|
|
|
|
//! individual items.
|
|
|
|
|
|
|
|
use std::collections::BTreeMap;
|
|
|
|
|
2023-01-26 15:46:40 +00:00
|
|
|
use base64::prelude::*;
|
2023-01-10 10:59:57 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
|
|
|
|
use garage_util::data::Uuid;
|
|
|
|
use garage_util::encode::{nonversioned_decode, nonversioned_encode};
|
2023-01-11 11:27:19 +00:00
|
|
|
use garage_util::error::Error;
|
2023-01-10 10:59:57 +00:00
|
|
|
|
2023-01-11 11:27:19 +00:00
|
|
|
use crate::helper::error::{Error as HelperError, OkOrBadRequest};
|
2023-01-10 10:59:57 +00:00
|
|
|
use crate::k2v::causality::*;
|
|
|
|
use crate::k2v::item_table::*;
|
2023-01-10 11:54:24 +00:00
|
|
|
use crate::k2v::sub::*;
|
2023-01-10 10:59:57 +00:00
|
|
|
|
|
|
|
#[derive(Debug, Serialize, Deserialize, Default)]
|
|
|
|
pub struct RangeSeenMarker {
|
|
|
|
vector_clock: VectorClock,
|
|
|
|
items: BTreeMap<String, VectorClock>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RangeSeenMarker {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Self::default()
|
|
|
|
}
|
|
|
|
|
2023-01-10 11:54:24 +00:00
|
|
|
pub fn restrict(&mut self, range: &PollRange) {
|
|
|
|
if let Some(start) = &range.start {
|
|
|
|
self.items = self.items.split_off(start);
|
|
|
|
}
|
|
|
|
if let Some(end) = &range.end {
|
|
|
|
self.items.split_off(end);
|
|
|
|
}
|
|
|
|
if let Some(pfx) = &range.prefix {
|
|
|
|
self.items.retain(|k, _v| k.starts_with(pfx));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-10 10:59:57 +00:00
|
|
|
pub fn mark_seen_node_items<'a, I: IntoIterator<Item = &'a K2VItem>>(
|
|
|
|
&mut self,
|
|
|
|
node: Uuid,
|
|
|
|
items: I,
|
|
|
|
) {
|
|
|
|
let node = make_node_id(node);
|
|
|
|
for item in items.into_iter() {
|
|
|
|
let cc = item.causal_context();
|
|
|
|
|
|
|
|
if let Some(ts) = cc.vector_clock.get(&node) {
|
|
|
|
let ent = self.vector_clock.entry(node).or_insert(0);
|
|
|
|
*ent = std::cmp::max(*ent, *ts);
|
|
|
|
}
|
|
|
|
|
|
|
|
if vclock_gt(&cc.vector_clock, &self.vector_clock) {
|
|
|
|
match self.items.get_mut(&item.sort_key) {
|
|
|
|
None => {
|
|
|
|
self.items.insert(item.sort_key.clone(), cc.vector_clock);
|
|
|
|
}
|
2023-01-26 16:26:32 +00:00
|
|
|
Some(ent) => *ent = vclock_max(ent, &cc.vector_clock),
|
2023-01-10 10:59:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn canonicalize(&mut self) {
|
|
|
|
let self_vc = &self.vector_clock;
|
2023-01-26 16:26:32 +00:00
|
|
|
self.items.retain(|_sk, vc| vclock_gt(vc, self_vc))
|
2023-01-10 10:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn encode(&mut self) -> Result<String, Error> {
|
|
|
|
self.canonicalize();
|
|
|
|
|
|
|
|
let bytes = nonversioned_encode(&self)?;
|
|
|
|
let bytes = zstd::stream::encode_all(&mut &bytes[..], zstd::DEFAULT_COMPRESSION_LEVEL)?;
|
2023-05-09 19:49:34 +00:00
|
|
|
Ok(BASE64_STANDARD.encode(bytes))
|
2023-01-10 10:59:57 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 11:27:19 +00:00
|
|
|
/// Decode from msgpack+zstd+b64 representation, returns None on error.
|
|
|
|
pub fn decode(s: &str) -> Option<Self> {
|
2023-01-26 16:26:32 +00:00
|
|
|
let bytes = BASE64_STANDARD.decode(s).ok()?;
|
2023-01-11 11:27:19 +00:00
|
|
|
let bytes = zstd::stream::decode_all(&mut &bytes[..]).ok()?;
|
|
|
|
nonversioned_decode(&bytes).ok()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn decode_helper(s: &str) -> Result<Self, HelperError> {
|
|
|
|
Self::decode(s).ok_or_bad_request("Invalid causality token")
|
2023-01-10 10:59:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is_new_item(&self, item: &K2VItem) -> bool {
|
|
|
|
let cc = item.causal_context();
|
|
|
|
vclock_gt(&cc.vector_clock, &self.vector_clock)
|
|
|
|
&& self
|
|
|
|
.items
|
|
|
|
.get(&item.sort_key)
|
2023-01-26 16:26:32 +00:00
|
|
|
.map(|vc| vclock_gt(&cc.vector_clock, vc))
|
2023-01-10 10:59:57 +00:00
|
|
|
.unwrap_or(true)
|
|
|
|
}
|
|
|
|
}
|