2021-03-11 12:47:21 +00:00
|
|
|
use std::convert::TryInto;
|
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
|
|
|
|
|
|
|
use futures::select;
|
|
|
|
use futures_util::future::*;
|
2021-03-12 13:37:46 +00:00
|
|
|
use log::{debug, warn};
|
2021-03-11 12:47:21 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use sled::transaction::{
|
|
|
|
ConflictableTransactionError, ConflictableTransactionResult, TransactionalTree,
|
|
|
|
};
|
|
|
|
use tokio::sync::{watch, Notify};
|
|
|
|
|
|
|
|
use garage_util::background::BackgroundRunner;
|
|
|
|
use garage_util::data::*;
|
|
|
|
use garage_util::error::Error;
|
|
|
|
|
2021-03-11 17:28:03 +00:00
|
|
|
pub type MerklePartition = [u8; 2];
|
|
|
|
|
|
|
|
pub fn hash_of_merkle_partition(p: MerklePartition) -> Hash {
|
|
|
|
let mut partition_pos = [0u8; 32];
|
|
|
|
partition_pos[0..2].copy_from_slice(&p[..]);
|
|
|
|
partition_pos.into()
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn hash_of_merkle_partition_opt(p: Option<MerklePartition>) -> Hash {
|
|
|
|
p.map(hash_of_merkle_partition)
|
|
|
|
.unwrap_or([0xFFu8; 32].into())
|
|
|
|
}
|
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
// This modules partitions the data in 2**16 partitions, based on the top
|
|
|
|
// 16 bits (two bytes) of item's partition keys' hashes.
|
|
|
|
// It builds one Merkle tree for each of these 2**16 partitions.
|
|
|
|
|
2021-03-12 14:40:54 +00:00
|
|
|
pub struct MerkleUpdater {
|
2021-03-11 12:47:21 +00:00
|
|
|
table_name: String,
|
|
|
|
background: Arc<BackgroundRunner>,
|
|
|
|
|
|
|
|
// Content of the todo tree: items where
|
|
|
|
// - key = the key of an item in the main table, ie hash(partition_key)+sort_key
|
|
|
|
// - value = the hash of the full serialized item, if present,
|
|
|
|
// or an empty vec if item is absent (deleted)
|
2021-03-12 14:40:54 +00:00
|
|
|
pub todo: sled::Tree,
|
2021-03-11 12:47:21 +00:00
|
|
|
pub(crate) todo_notify: Notify,
|
|
|
|
|
|
|
|
// Content of the merkle tree: items where
|
|
|
|
// - key = .bytes() for MerkleNodeKey
|
|
|
|
// - value = serialization of a MerkleNode, assumed to be MerkleNode::empty if not found
|
2021-03-15 17:27:26 +00:00
|
|
|
pub merkle_tree: sled::Tree,
|
2021-03-11 12:47:21 +00:00
|
|
|
empty_node_hash: Hash,
|
|
|
|
}
|
|
|
|
|
2021-03-11 18:30:24 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2021-03-11 12:47:21 +00:00
|
|
|
pub struct MerkleNodeKey {
|
|
|
|
// partition: first 16 bits (two bytes) of the partition_key's hash
|
2021-03-11 17:28:03 +00:00
|
|
|
pub partition: MerklePartition,
|
2021-03-11 12:47:21 +00:00
|
|
|
|
|
|
|
// prefix: a prefix for the hash of full keys, i.e. hash(hash(partition_key)+sort_key)
|
2021-03-15 17:40:27 +00:00
|
|
|
#[serde(with = "serde_bytes")]
|
2021-03-11 12:47:21 +00:00
|
|
|
pub prefix: Vec<u8>,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Debug, Serialize, Deserialize)]
|
|
|
|
pub enum MerkleNode {
|
|
|
|
// The empty Merkle node
|
|
|
|
Empty,
|
|
|
|
|
|
|
|
// An intermediate Merkle tree node for a prefix
|
|
|
|
// Contains the hashes of the 256 possible next prefixes
|
|
|
|
Intermediate(Vec<(u8, Hash)>),
|
|
|
|
|
|
|
|
// A final node for an item
|
|
|
|
// Contains the full key of the item and the hash of the value
|
|
|
|
Leaf(Vec<u8>, Hash),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MerkleUpdater {
|
2021-03-11 15:54:15 +00:00
|
|
|
pub(crate) fn launch(
|
2021-03-11 12:47:21 +00:00
|
|
|
table_name: String,
|
|
|
|
background: Arc<BackgroundRunner>,
|
|
|
|
todo: sled::Tree,
|
|
|
|
merkle_tree: sled::Tree,
|
|
|
|
) -> Arc<Self> {
|
|
|
|
let empty_node_hash = blake2sum(&rmp_to_vec_all_named(&MerkleNode::Empty).unwrap()[..]);
|
|
|
|
|
2021-03-11 15:54:15 +00:00
|
|
|
let ret = Arc::new(Self {
|
2021-03-11 12:47:21 +00:00
|
|
|
table_name,
|
|
|
|
background,
|
|
|
|
todo,
|
|
|
|
todo_notify: Notify::new(),
|
|
|
|
merkle_tree,
|
|
|
|
empty_node_hash,
|
2021-03-11 15:54:15 +00:00
|
|
|
});
|
2021-03-11 12:47:21 +00:00
|
|
|
|
2021-03-11 15:54:15 +00:00
|
|
|
let ret2 = ret.clone();
|
|
|
|
ret.background.spawn_worker(
|
|
|
|
format!("Merkle tree updater for {}", ret.table_name),
|
|
|
|
|must_exit: watch::Receiver<bool>| ret2.updater_loop(must_exit),
|
2021-03-11 12:47:21 +00:00
|
|
|
);
|
2021-03-11 15:54:15 +00:00
|
|
|
|
|
|
|
ret
|
2021-03-11 12:47:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
async fn updater_loop(
|
|
|
|
self: Arc<Self>,
|
|
|
|
mut must_exit: watch::Receiver<bool>,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
while !*must_exit.borrow() {
|
|
|
|
if let Some(x) = self.todo.iter().next() {
|
|
|
|
match x {
|
|
|
|
Ok((key, valhash)) => {
|
|
|
|
if let Err(e) = self.update_item(&key[..], &valhash[..]) {
|
2021-03-12 14:05:26 +00:00
|
|
|
warn!(
|
|
|
|
"({}) Error while updating Merkle tree item: {}",
|
|
|
|
self.table_name, e
|
|
|
|
);
|
2021-03-11 12:47:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => {
|
2021-03-12 14:05:26 +00:00
|
|
|
warn!(
|
|
|
|
"({}) Error while iterating on Merkle todo tree: {}",
|
|
|
|
self.table_name, e
|
|
|
|
);
|
2021-03-11 12:47:21 +00:00
|
|
|
tokio::time::delay_for(Duration::from_secs(10)).await;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
select! {
|
|
|
|
_ = self.todo_notify.notified().fuse() => (),
|
|
|
|
_ = must_exit.recv().fuse() => (),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_item(&self, k: &[u8], vhash_by: &[u8]) -> Result<(), Error> {
|
|
|
|
let khash = blake2sum(k);
|
|
|
|
|
|
|
|
let new_vhash = if vhash_by.len() == 0 {
|
|
|
|
None
|
|
|
|
} else {
|
2021-03-12 18:57:37 +00:00
|
|
|
Some(Hash::try_from(&vhash_by[..]).unwrap())
|
2021-03-11 12:47:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let key = MerkleNodeKey {
|
|
|
|
partition: k[0..2].try_into().unwrap(),
|
|
|
|
prefix: vec![],
|
|
|
|
};
|
|
|
|
self.merkle_tree
|
|
|
|
.transaction(|tx| self.update_item_rec(tx, k, khash, &key, new_vhash))?;
|
|
|
|
|
|
|
|
let deleted = self
|
|
|
|
.todo
|
|
|
|
.compare_and_swap::<_, _, Vec<u8>>(k, Some(vhash_by), None)?
|
|
|
|
.is_ok();
|
|
|
|
|
|
|
|
if !deleted {
|
2021-03-12 13:37:46 +00:00
|
|
|
debug!(
|
|
|
|
"({}) Item not deleted from Merkle todo because it changed: {:?}",
|
2021-03-12 14:05:26 +00:00
|
|
|
self.table_name, k
|
2021-03-11 12:47:21 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update_item_rec(
|
|
|
|
&self,
|
|
|
|
tx: &TransactionalTree,
|
|
|
|
k: &[u8],
|
|
|
|
khash: Hash,
|
|
|
|
key: &MerkleNodeKey,
|
|
|
|
new_vhash: Option<Hash>,
|
|
|
|
) -> ConflictableTransactionResult<Option<Hash>, Error> {
|
|
|
|
let i = key.prefix.len();
|
2021-03-11 15:54:15 +00:00
|
|
|
|
|
|
|
// Read node at current position (defined by the prefix stored in key)
|
|
|
|
// Calculate an update to apply to this node
|
|
|
|
// This update is an Option<_>, so that it is None if the update is a no-op
|
|
|
|
// and we can thus skip recalculating and re-storing everything
|
2021-03-11 12:47:21 +00:00
|
|
|
let mutate = match self.read_node_txn(tx, &key)? {
|
|
|
|
MerkleNode::Empty => {
|
|
|
|
if let Some(vhv) = new_vhash {
|
|
|
|
Some(MerkleNode::Leaf(k.to_vec(), vhv))
|
|
|
|
} else {
|
2021-03-11 15:54:15 +00:00
|
|
|
// Nothing to do, keep empty node
|
2021-03-11 12:47:21 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MerkleNode::Intermediate(mut children) => {
|
|
|
|
let key2 = key.next_key(khash);
|
|
|
|
if let Some(subhash) = self.update_item_rec(tx, k, khash, &key2, new_vhash)? {
|
2021-03-11 15:54:15 +00:00
|
|
|
// Subtree changed, update this node as well
|
2021-03-11 12:47:21 +00:00
|
|
|
if subhash == self.empty_node_hash {
|
|
|
|
intermediate_rm_child(&mut children, key2.prefix[i]);
|
|
|
|
} else {
|
|
|
|
intermediate_set_child(&mut children, key2.prefix[i], subhash);
|
|
|
|
}
|
2021-03-11 15:54:15 +00:00
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
if children.len() == 0 {
|
|
|
|
// should not happen
|
2021-03-12 14:05:26 +00:00
|
|
|
warn!(
|
|
|
|
"({}) Replacing intermediate node with empty node, should not happen.",
|
|
|
|
self.table_name
|
|
|
|
);
|
2021-03-11 12:47:21 +00:00
|
|
|
Some(MerkleNode::Empty)
|
|
|
|
} else if children.len() == 1 {
|
2021-03-11 15:54:15 +00:00
|
|
|
// We now have a single node (case when the update deleted one of only two
|
|
|
|
// children). Move that single child to this level of the tree.
|
2021-03-11 12:47:21 +00:00
|
|
|
let key_sub = key.add_byte(children[0].0);
|
|
|
|
let subnode = self.read_node_txn(tx, &key_sub)?;
|
|
|
|
tx.remove(key_sub.encode())?;
|
|
|
|
Some(subnode)
|
|
|
|
} else {
|
|
|
|
Some(MerkleNode::Intermediate(children))
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-11 15:54:15 +00:00
|
|
|
// Subtree not changed, nothing to do
|
2021-03-11 12:47:21 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
MerkleNode::Leaf(exlf_key, exlf_hash) => {
|
|
|
|
if exlf_key == k {
|
2021-03-11 15:54:15 +00:00
|
|
|
// This leaf is for the same key that the one we are updating
|
2021-03-11 12:47:21 +00:00
|
|
|
match new_vhash {
|
|
|
|
Some(vhv) if vhv == exlf_hash => None,
|
|
|
|
Some(vhv) => Some(MerkleNode::Leaf(k.to_vec(), vhv)),
|
|
|
|
None => Some(MerkleNode::Empty),
|
|
|
|
}
|
|
|
|
} else {
|
2021-03-11 15:54:15 +00:00
|
|
|
// This is an only leaf for another key
|
2021-03-11 12:47:21 +00:00
|
|
|
if let Some(vhv) = new_vhash {
|
2021-03-11 15:54:15 +00:00
|
|
|
// Move that other key to a subnode, create another subnode for our
|
|
|
|
// insertion and replace current node by an intermediary node
|
2021-03-11 12:47:21 +00:00
|
|
|
let (pos1, h1) = {
|
|
|
|
let key2 = key.next_key(blake2sum(&exlf_key[..]));
|
2021-03-11 17:28:03 +00:00
|
|
|
let subhash = self.put_node_txn(
|
|
|
|
tx,
|
|
|
|
&key2,
|
|
|
|
&MerkleNode::Leaf(exlf_key, exlf_hash),
|
|
|
|
)?;
|
2021-03-11 12:47:21 +00:00
|
|
|
(key2.prefix[i], subhash)
|
|
|
|
};
|
|
|
|
let (pos2, h2) = {
|
|
|
|
let key2 = key.next_key(khash);
|
|
|
|
let subhash =
|
|
|
|
self.put_node_txn(tx, &key2, &MerkleNode::Leaf(k.to_vec(), vhv))?;
|
|
|
|
(key2.prefix[i], subhash)
|
|
|
|
};
|
|
|
|
let mut int = vec![];
|
|
|
|
intermediate_set_child(&mut int, pos1, h1);
|
|
|
|
intermediate_set_child(&mut int, pos2, h2);
|
|
|
|
Some(MerkleNode::Intermediate(int))
|
|
|
|
} else {
|
2021-03-11 15:54:15 +00:00
|
|
|
// Nothing to do, we don't want to insert this value because it is None,
|
|
|
|
// and we don't want to change the other value because it's for something
|
|
|
|
// else
|
2021-03-11 12:47:21 +00:00
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
if let Some(new_node) = mutate {
|
|
|
|
let hash = self.put_node_txn(tx, &key, &new_node)?;
|
|
|
|
Ok(Some(hash))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Merkle tree node manipulation
|
|
|
|
|
|
|
|
fn read_node_txn(
|
|
|
|
&self,
|
|
|
|
tx: &TransactionalTree,
|
|
|
|
k: &MerkleNodeKey,
|
|
|
|
) -> ConflictableTransactionResult<MerkleNode, Error> {
|
|
|
|
let ent = tx.get(k.encode())?;
|
|
|
|
match ent {
|
|
|
|
None => Ok(MerkleNode::Empty),
|
|
|
|
Some(v) => Ok(rmp_serde::decode::from_read_ref::<_, MerkleNode>(&v[..])
|
|
|
|
.map_err(|e| ConflictableTransactionError::Abort(e.into()))?),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn put_node_txn(
|
|
|
|
&self,
|
|
|
|
tx: &TransactionalTree,
|
|
|
|
k: &MerkleNodeKey,
|
|
|
|
v: &MerkleNode,
|
|
|
|
) -> ConflictableTransactionResult<Hash, Error> {
|
2021-03-11 18:30:24 +00:00
|
|
|
trace!("Put Merkle node: {:?} => {:?}", k, v);
|
2021-03-11 12:47:21 +00:00
|
|
|
if *v == MerkleNode::Empty {
|
|
|
|
tx.remove(k.encode())?;
|
|
|
|
Ok(self.empty_node_hash)
|
|
|
|
} else {
|
|
|
|
let vby = rmp_to_vec_all_named(v)
|
|
|
|
.map_err(|e| ConflictableTransactionError::Abort(e.into()))?;
|
|
|
|
let rethash = blake2sum(&vby[..]);
|
|
|
|
tx.insert(k.encode(), vby)?;
|
|
|
|
Ok(rethash)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-11 15:54:15 +00:00
|
|
|
// Access a node in the Merkle tree, used by the sync protocol
|
2021-03-11 17:28:03 +00:00
|
|
|
pub(crate) fn read_node(&self, k: &MerkleNodeKey) -> Result<MerkleNode, Error> {
|
2021-03-11 12:47:21 +00:00
|
|
|
let ent = self.merkle_tree.get(k.encode())?;
|
|
|
|
match ent {
|
|
|
|
None => Ok(MerkleNode::Empty),
|
2021-03-11 17:28:03 +00:00
|
|
|
Some(v) => Ok(rmp_serde::decode::from_read_ref::<_, MerkleNode>(&v[..])?),
|
2021-03-11 12:47:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl MerkleNodeKey {
|
|
|
|
fn encode(&self) -> Vec<u8> {
|
|
|
|
let mut ret = Vec::with_capacity(2 + self.prefix.len());
|
|
|
|
ret.extend(&self.partition[..]);
|
|
|
|
ret.extend(&self.prefix[..]);
|
|
|
|
ret
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn next_key(&self, h: Hash) -> Self {
|
|
|
|
assert!(&h.as_slice()[0..self.prefix.len()] == &self.prefix[..]);
|
|
|
|
let mut s2 = self.clone();
|
|
|
|
s2.prefix.push(h.as_slice()[self.prefix.len()]);
|
|
|
|
s2
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn add_byte(&self, b: u8) -> Self {
|
|
|
|
let mut s2 = self.clone();
|
|
|
|
s2.prefix.push(b);
|
|
|
|
s2
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn intermediate_set_child(ch: &mut Vec<(u8, Hash)>, pos: u8, v: Hash) {
|
|
|
|
for i in 0..ch.len() {
|
|
|
|
if ch[i].0 == pos {
|
|
|
|
ch[i].1 = v;
|
|
|
|
return;
|
|
|
|
} else if ch[i].0 > pos {
|
|
|
|
ch.insert(i, (pos, v));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
ch.insert(ch.len(), (pos, v));
|
|
|
|
}
|
|
|
|
|
|
|
|
fn intermediate_rm_child(ch: &mut Vec<(u8, Hash)>, pos: u8) {
|
|
|
|
for i in 0..ch.len() {
|
|
|
|
if ch[i].0 == pos {
|
|
|
|
ch.remove(i);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn test_intermediate_aux() {
|
|
|
|
let mut v = vec![];
|
2021-03-11 17:28:03 +00:00
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
intermediate_set_child(&mut v, 12u8, [12u8; 32].into());
|
2021-03-11 17:28:03 +00:00
|
|
|
assert_eq!(v, vec![(12u8, [12u8; 32].into())]);
|
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
intermediate_set_child(&mut v, 42u8, [42u8; 32].into());
|
2021-03-11 17:28:03 +00:00
|
|
|
assert_eq!(
|
|
|
|
v,
|
|
|
|
vec![(12u8, [12u8; 32].into()), (42u8, [42u8; 32].into())]
|
|
|
|
);
|
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
intermediate_set_child(&mut v, 4u8, [4u8; 32].into());
|
2021-03-11 17:28:03 +00:00
|
|
|
assert_eq!(
|
|
|
|
v,
|
|
|
|
vec![
|
|
|
|
(4u8, [4u8; 32].into()),
|
|
|
|
(12u8, [12u8; 32].into()),
|
|
|
|
(42u8, [42u8; 32].into())
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
intermediate_set_child(&mut v, 12u8, [8u8; 32].into());
|
2021-03-11 17:28:03 +00:00
|
|
|
assert_eq!(
|
|
|
|
v,
|
|
|
|
vec![
|
|
|
|
(4u8, [4u8; 32].into()),
|
|
|
|
(12u8, [8u8; 32].into()),
|
|
|
|
(42u8, [42u8; 32].into())
|
|
|
|
]
|
|
|
|
);
|
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
intermediate_set_child(&mut v, 6u8, [6u8; 32].into());
|
2021-03-11 17:28:03 +00:00
|
|
|
assert_eq!(
|
|
|
|
v,
|
|
|
|
vec![
|
|
|
|
(4u8, [4u8; 32].into()),
|
|
|
|
(6u8, [6u8; 32].into()),
|
|
|
|
(12u8, [8u8; 32].into()),
|
|
|
|
(42u8, [42u8; 32].into())
|
|
|
|
]
|
|
|
|
);
|
2021-03-11 12:47:21 +00:00
|
|
|
|
|
|
|
intermediate_rm_child(&mut v, 42u8);
|
2021-03-11 17:28:03 +00:00
|
|
|
assert_eq!(
|
|
|
|
v,
|
|
|
|
vec![
|
|
|
|
(4u8, [4u8; 32].into()),
|
|
|
|
(6u8, [6u8; 32].into()),
|
|
|
|
(12u8, [8u8; 32].into())
|
|
|
|
]
|
|
|
|
);
|
2021-03-11 12:47:21 +00:00
|
|
|
|
|
|
|
intermediate_rm_child(&mut v, 11u8);
|
2021-03-11 17:28:03 +00:00
|
|
|
assert_eq!(
|
|
|
|
v,
|
|
|
|
vec![
|
|
|
|
(4u8, [4u8; 32].into()),
|
|
|
|
(6u8, [6u8; 32].into()),
|
|
|
|
(12u8, [8u8; 32].into())
|
|
|
|
]
|
|
|
|
);
|
2021-03-11 12:47:21 +00:00
|
|
|
|
|
|
|
intermediate_rm_child(&mut v, 6u8);
|
2021-03-11 17:28:03 +00:00
|
|
|
assert_eq!(v, vec![(4u8, [4u8; 32].into()), (12u8, [8u8; 32].into())]);
|
|
|
|
|
2021-03-11 12:47:21 +00:00
|
|
|
intermediate_set_child(&mut v, 6u8, [7u8; 32].into());
|
2021-03-11 17:28:03 +00:00
|
|
|
assert_eq!(
|
|
|
|
v,
|
|
|
|
vec![
|
|
|
|
(4u8, [4u8; 32].into()),
|
|
|
|
(6u8, [7u8; 32].into()),
|
|
|
|
(12u8, [8u8; 32].into())
|
|
|
|
]
|
|
|
|
);
|
2021-03-11 12:47:21 +00:00
|
|
|
}
|