2020-04-17 16:51:29 +00:00
|
|
|
use std::collections::{BTreeMap, HashMap};
|
2020-04-10 20:01:48 +00:00
|
|
|
use std::sync::Arc;
|
|
|
|
use std::time::Duration;
|
2020-04-08 20:00:41 +00:00
|
|
|
|
2020-04-17 16:51:29 +00:00
|
|
|
use arc_swap::ArcSwapOption;
|
2020-04-11 17:43:29 +00:00
|
|
|
use async_trait::async_trait;
|
2020-04-11 21:53:32 +00:00
|
|
|
use futures::stream::*;
|
2020-04-11 17:43:29 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
|
|
|
use serde_bytes::ByteBuf;
|
|
|
|
|
2020-04-08 20:00:41 +00:00
|
|
|
use crate::data::*;
|
2020-04-10 20:01:48 +00:00
|
|
|
use crate::error::Error;
|
2020-04-19 11:22:28 +00:00
|
|
|
use crate::membership::{Ring, System};
|
2020-04-08 20:00:41 +00:00
|
|
|
use crate::rpc_client::*;
|
2020-04-18 17:21:34 +00:00
|
|
|
use crate::rpc_server::*;
|
2020-04-16 16:41:10 +00:00
|
|
|
use crate::table_sync::*;
|
2020-04-08 20:00:41 +00:00
|
|
|
|
2020-04-19 11:22:28 +00:00
|
|
|
const TABLE_RPC_TIMEOUT: Duration = Duration::from_secs(10);
|
|
|
|
|
|
|
|
pub struct Table<F: TableSchema, R: TableReplication> {
|
2020-04-08 21:01:49 +00:00
|
|
|
pub instance: F,
|
2020-04-19 11:22:28 +00:00
|
|
|
pub replication: R,
|
2020-04-08 20:00:41 +00:00
|
|
|
|
|
|
|
pub name: String,
|
2020-04-18 17:21:34 +00:00
|
|
|
pub rpc_client: Arc<RpcClient<TableRPC<F>>>,
|
2020-04-08 20:00:41 +00:00
|
|
|
|
|
|
|
pub system: Arc<System>,
|
|
|
|
pub store: sled::Tree,
|
2020-04-19 11:22:28 +00:00
|
|
|
pub syncer: ArcSwapOption<TableSyncer<F, R>>,
|
2020-04-08 20:00:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-08 21:01:49 +00:00
|
|
|
#[derive(Serialize, Deserialize)]
|
2020-04-12 20:24:53 +00:00
|
|
|
pub enum TableRPC<F: TableSchema> {
|
2020-04-08 21:01:49 +00:00
|
|
|
Ok,
|
2020-04-08 21:47:34 +00:00
|
|
|
|
2020-04-09 14:16:27 +00:00
|
|
|
ReadEntry(F::P, F::S),
|
2020-04-11 17:43:29 +00:00
|
|
|
ReadEntryResponse(Option<ByteBuf>),
|
2020-04-08 21:47:34 +00:00
|
|
|
|
2020-04-19 15:15:48 +00:00
|
|
|
// Read range: read all keys in partition P, possibly starting at a certain sort key offset
|
|
|
|
ReadRange(F::P, Option<F::S>, Option<F::Filter>, usize),
|
2020-04-17 15:09:57 +00:00
|
|
|
|
2020-04-11 17:43:29 +00:00
|
|
|
Update(Vec<Arc<ByteBuf>>),
|
2020-04-16 16:41:10 +00:00
|
|
|
|
2020-04-17 15:09:57 +00:00
|
|
|
SyncRPC(SyncRPC),
|
2020-04-08 20:00:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 17:21:34 +00:00
|
|
|
impl<F: TableSchema> RpcMessage for TableRPC<F> {}
|
|
|
|
|
2020-04-09 15:32:28 +00:00
|
|
|
pub trait PartitionKey {
|
2020-04-08 20:00:41 +00:00
|
|
|
fn hash(&self) -> Hash;
|
|
|
|
}
|
|
|
|
|
2020-04-09 15:32:28 +00:00
|
|
|
pub trait SortKey {
|
2020-04-09 14:16:27 +00:00
|
|
|
fn sort_key(&self) -> &[u8];
|
|
|
|
}
|
|
|
|
|
2020-04-10 20:01:48 +00:00
|
|
|
pub trait Entry<P: PartitionKey, S: SortKey>:
|
|
|
|
PartialEq + Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync
|
|
|
|
{
|
2020-04-09 14:16:27 +00:00
|
|
|
fn partition_key(&self) -> &P;
|
|
|
|
fn sort_key(&self) -> &S;
|
|
|
|
|
2020-04-09 21:45:07 +00:00
|
|
|
fn merge(&mut self, other: &Self);
|
2020-04-08 20:00:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 15:15:48 +00:00
|
|
|
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
|
|
|
|
pub struct EmptyKey;
|
|
|
|
impl SortKey for EmptyKey {
|
2020-04-09 14:16:27 +00:00
|
|
|
fn sort_key(&self) -> &[u8] {
|
|
|
|
&[]
|
|
|
|
}
|
|
|
|
}
|
2020-04-19 15:15:48 +00:00
|
|
|
impl PartitionKey for EmptyKey {
|
|
|
|
fn hash(&self) -> Hash {
|
|
|
|
[0u8; 32].into()
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 14:16:27 +00:00
|
|
|
|
2020-04-09 15:32:28 +00:00
|
|
|
impl<T: AsRef<str>> PartitionKey for T {
|
2020-04-09 14:16:27 +00:00
|
|
|
fn hash(&self) -> Hash {
|
2020-04-09 15:32:28 +00:00
|
|
|
hash(self.as_ref().as_bytes())
|
2020-04-09 14:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-09 15:32:28 +00:00
|
|
|
impl<T: AsRef<str>> SortKey for T {
|
2020-04-09 14:16:27 +00:00
|
|
|
fn sort_key(&self) -> &[u8] {
|
2020-04-09 15:32:28 +00:00
|
|
|
self.as_ref().as_bytes()
|
2020-04-09 14:16:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-09 21:45:07 +00:00
|
|
|
impl PartitionKey for Hash {
|
|
|
|
fn hash(&self) -> Hash {
|
|
|
|
self.clone()
|
|
|
|
}
|
|
|
|
}
|
2020-04-10 20:55:01 +00:00
|
|
|
impl SortKey for Hash {
|
|
|
|
fn sort_key(&self) -> &[u8] {
|
|
|
|
self.as_slice()
|
|
|
|
}
|
|
|
|
}
|
2020-04-09 21:45:07 +00:00
|
|
|
|
2020-04-08 20:00:41 +00:00
|
|
|
#[async_trait]
|
2020-04-12 20:24:53 +00:00
|
|
|
pub trait TableSchema: Send + Sync {
|
2020-04-09 15:32:28 +00:00
|
|
|
type P: PartitionKey + Clone + PartialEq + Serialize + for<'de> Deserialize<'de> + Send + Sync;
|
|
|
|
type S: SortKey + Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync;
|
2020-04-09 14:16:27 +00:00
|
|
|
type E: Entry<Self::P, Self::S>;
|
2020-04-17 15:09:57 +00:00
|
|
|
type Filter: Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync;
|
2020-04-08 20:00:41 +00:00
|
|
|
|
2020-04-19 20:52:20 +00:00
|
|
|
async fn updated(&self, old: Option<Self::E>, new: Option<Self::E>) -> Result<(), Error>;
|
2020-04-17 16:51:29 +00:00
|
|
|
fn matches_filter(_entry: &Self::E, _filter: &Self::Filter) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2020-04-08 20:00:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-19 11:22:28 +00:00
|
|
|
pub trait TableReplication: Send + Sync {
|
|
|
|
// See examples in table_sharded.rs and table_fullcopy.rs
|
|
|
|
// To understand various replication methods
|
|
|
|
|
|
|
|
// Which nodes to send reads from
|
|
|
|
fn read_nodes(&self, hash: &Hash, system: &System) -> Vec<UUID>;
|
|
|
|
fn read_quorum(&self) -> usize;
|
|
|
|
|
|
|
|
// Which nodes to send writes to
|
|
|
|
fn write_nodes(&self, hash: &Hash, system: &System) -> Vec<UUID>;
|
|
|
|
fn write_quorum(&self) -> usize;
|
|
|
|
fn max_write_errors(&self) -> usize;
|
|
|
|
fn epidemic_writes(&self) -> bool;
|
|
|
|
|
|
|
|
// Which are the nodes that do actually replicate the data
|
|
|
|
fn replication_nodes(&self, hash: &Hash, ring: &Ring) -> Vec<UUID>;
|
|
|
|
fn split_points(&self, ring: &Ring) -> Vec<Hash>;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F, R> Table<F, R>
|
|
|
|
where
|
|
|
|
F: TableSchema + 'static,
|
|
|
|
R: TableReplication + 'static,
|
|
|
|
{
|
2020-04-17 15:09:57 +00:00
|
|
|
// =============== PUBLIC INTERFACE FUNCTIONS (new, insert, get, etc) ===============
|
|
|
|
|
2020-04-16 12:50:49 +00:00
|
|
|
pub async fn new(
|
2020-04-10 20:01:48 +00:00
|
|
|
instance: F,
|
2020-04-19 11:22:28 +00:00
|
|
|
replication: R,
|
2020-04-10 20:01:48 +00:00
|
|
|
system: Arc<System>,
|
|
|
|
db: &sled::Db,
|
|
|
|
name: String,
|
2020-04-18 17:21:34 +00:00
|
|
|
rpc_server: &mut RpcServer,
|
2020-04-12 20:24:53 +00:00
|
|
|
) -> Arc<Self> {
|
2020-04-10 20:01:48 +00:00
|
|
|
let store = db.open_tree(&name).expect("Unable to open DB tree");
|
2020-04-18 17:21:34 +00:00
|
|
|
|
|
|
|
let rpc_path = format!("table_{}", name);
|
|
|
|
let rpc_client = system.rpc_client::<TableRPC<F>>(&rpc_path);
|
|
|
|
|
2020-04-16 12:50:49 +00:00
|
|
|
let table = Arc::new(Self {
|
2020-04-08 21:01:49 +00:00
|
|
|
instance,
|
2020-04-19 11:22:28 +00:00
|
|
|
replication,
|
2020-04-08 20:00:41 +00:00
|
|
|
name,
|
2020-04-18 17:21:34 +00:00
|
|
|
rpc_client,
|
2020-04-08 20:00:41 +00:00
|
|
|
system,
|
|
|
|
store,
|
2020-04-17 16:27:29 +00:00
|
|
|
syncer: ArcSwapOption::from(None),
|
2020-04-16 12:50:49 +00:00
|
|
|
});
|
2020-04-18 17:21:34 +00:00
|
|
|
table.clone().register_handler(rpc_server, rpc_path);
|
|
|
|
|
2020-04-16 16:41:10 +00:00
|
|
|
let syncer = TableSyncer::launch(table.clone()).await;
|
2020-04-17 16:27:29 +00:00
|
|
|
table.syncer.swap(Some(syncer));
|
2020-04-18 17:21:34 +00:00
|
|
|
|
2020-04-16 12:50:49 +00:00
|
|
|
table
|
2020-04-08 20:00:41 +00:00
|
|
|
}
|
|
|
|
|
2020-04-09 14:16:27 +00:00
|
|
|
pub async fn insert(&self, e: &F::E) -> Result<(), Error> {
|
|
|
|
let hash = e.partition_key().hash();
|
2020-04-19 11:22:28 +00:00
|
|
|
let who = self.replication.write_nodes(&hash, &self.system);
|
2020-04-17 19:59:07 +00:00
|
|
|
//eprintln!("insert who: {:?}", who);
|
2020-04-08 21:01:49 +00:00
|
|
|
|
2020-04-11 17:43:29 +00:00
|
|
|
let e_enc = Arc::new(ByteBuf::from(rmp_to_vec_all_named(e)?));
|
2020-04-18 17:21:34 +00:00
|
|
|
let rpc = TableRPC::<F>::Update(vec![e_enc]);
|
2020-04-10 20:01:48 +00:00
|
|
|
|
2020-04-18 17:21:34 +00:00
|
|
|
self.rpc_client
|
2020-04-19 11:22:28 +00:00
|
|
|
.try_call_many(
|
|
|
|
&who[..],
|
|
|
|
rpc,
|
|
|
|
self.replication.write_quorum(),
|
|
|
|
TABLE_RPC_TIMEOUT,
|
|
|
|
)
|
2020-04-10 20:01:48 +00:00
|
|
|
.await?;
|
2020-04-08 21:01:49 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
2020-04-08 20:00:41 +00:00
|
|
|
|
2020-04-11 17:43:29 +00:00
|
|
|
pub async fn insert_many(&self, entries: &[F::E]) -> Result<(), Error> {
|
|
|
|
let mut call_list = HashMap::new();
|
|
|
|
|
|
|
|
for entry in entries.iter() {
|
|
|
|
let hash = entry.partition_key().hash();
|
2020-04-19 11:22:28 +00:00
|
|
|
let who = self.replication.write_nodes(&hash, &self.system);
|
2020-04-11 17:43:29 +00:00
|
|
|
let e_enc = Arc::new(ByteBuf::from(rmp_to_vec_all_named(entry)?));
|
|
|
|
for node in who {
|
|
|
|
if !call_list.contains_key(&node) {
|
|
|
|
call_list.insert(node.clone(), vec![]);
|
|
|
|
}
|
|
|
|
call_list.get_mut(&node).unwrap().push(e_enc.clone());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 21:53:32 +00:00
|
|
|
let call_futures = call_list.drain().map(|(node, entries)| async move {
|
|
|
|
let rpc = TableRPC::<F>::Update(entries);
|
|
|
|
|
2020-04-19 11:22:28 +00:00
|
|
|
let resp = self.rpc_client.call(&node, rpc, TABLE_RPC_TIMEOUT).await?;
|
2020-04-11 21:53:32 +00:00
|
|
|
Ok::<_, Error>((node, resp))
|
|
|
|
});
|
2020-04-11 17:43:29 +00:00
|
|
|
let mut resps = call_futures.collect::<FuturesUnordered<_>>();
|
|
|
|
let mut errors = vec![];
|
|
|
|
|
|
|
|
while let Some(resp) = resps.next().await {
|
|
|
|
if let Err(e) = resp {
|
|
|
|
errors.push(e);
|
|
|
|
}
|
|
|
|
}
|
2020-04-19 11:22:28 +00:00
|
|
|
if errors.len() > self.replication.max_write_errors() {
|
2020-04-11 17:43:29 +00:00
|
|
|
Err(Error::Message("Too many errors".into()))
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-16 12:50:49 +00:00
|
|
|
pub async fn get(
|
|
|
|
self: &Arc<Self>,
|
|
|
|
partition_key: &F::P,
|
|
|
|
sort_key: &F::S,
|
|
|
|
) -> Result<Option<F::E>, Error> {
|
2020-04-09 14:16:27 +00:00
|
|
|
let hash = partition_key.hash();
|
2020-04-19 11:22:28 +00:00
|
|
|
let who = self.replication.read_nodes(&hash, &self.system);
|
2020-04-17 19:59:07 +00:00
|
|
|
//eprintln!("get who: {:?}", who);
|
2020-04-08 20:00:41 +00:00
|
|
|
|
2020-04-18 17:21:34 +00:00
|
|
|
let rpc = TableRPC::<F>::ReadEntry(partition_key.clone(), sort_key.clone());
|
2020-04-10 20:01:48 +00:00
|
|
|
let resps = self
|
2020-04-18 17:21:34 +00:00
|
|
|
.rpc_client
|
2020-04-19 11:22:28 +00:00
|
|
|
.try_call_many(
|
|
|
|
&who[..],
|
|
|
|
rpc,
|
|
|
|
self.replication.read_quorum(),
|
|
|
|
TABLE_RPC_TIMEOUT,
|
|
|
|
)
|
2020-04-08 21:01:49 +00:00
|
|
|
.await?;
|
|
|
|
|
2020-04-08 21:47:34 +00:00
|
|
|
let mut ret = None;
|
2020-04-09 18:58:39 +00:00
|
|
|
let mut not_all_same = false;
|
2020-04-08 21:01:49 +00:00
|
|
|
for resp in resps {
|
2020-04-08 21:47:34 +00:00
|
|
|
if let TableRPC::ReadEntryResponse(value) = resp {
|
2020-04-11 17:43:29 +00:00
|
|
|
if let Some(v_bytes) = value {
|
|
|
|
let v = rmp_serde::decode::from_read_ref::<_, F::E>(v_bytes.as_slice())?;
|
2020-04-08 21:47:34 +00:00
|
|
|
ret = match ret {
|
|
|
|
None => Some(v),
|
|
|
|
Some(mut x) => {
|
2020-04-09 21:45:07 +00:00
|
|
|
if x != v {
|
2020-04-09 18:58:39 +00:00
|
|
|
not_all_same = true;
|
2020-04-09 21:45:07 +00:00
|
|
|
x.merge(&v);
|
2020-04-09 18:58:39 +00:00
|
|
|
}
|
2020-04-08 21:47:34 +00:00
|
|
|
Some(x)
|
|
|
|
}
|
|
|
|
}
|
2020-04-08 21:01:49 +00:00
|
|
|
}
|
2020-04-08 21:47:34 +00:00
|
|
|
} else {
|
|
|
|
return Err(Error::Message(format!("Invalid return value to read")));
|
2020-04-08 21:01:49 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-09 18:58:39 +00:00
|
|
|
if let Some(ret_entry) = &ret {
|
|
|
|
if not_all_same {
|
2020-04-17 15:09:57 +00:00
|
|
|
let self2 = self.clone();
|
|
|
|
let ent2 = ret_entry.clone();
|
2020-04-16 12:50:49 +00:00
|
|
|
self.system
|
|
|
|
.background
|
2020-04-17 16:51:29 +00:00
|
|
|
.spawn(async move { self2.repair_on_read(&who[..], ent2).await });
|
2020-04-09 18:58:39 +00:00
|
|
|
}
|
|
|
|
}
|
2020-04-08 21:47:34 +00:00
|
|
|
Ok(ret)
|
2020-04-08 21:01:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 15:09:57 +00:00
|
|
|
pub async fn get_range(
|
|
|
|
self: &Arc<Self>,
|
|
|
|
partition_key: &F::P,
|
2020-04-19 15:15:48 +00:00
|
|
|
begin_sort_key: Option<F::S>,
|
2020-04-17 15:09:57 +00:00
|
|
|
filter: Option<F::Filter>,
|
|
|
|
limit: usize,
|
|
|
|
) -> Result<Vec<F::E>, Error> {
|
|
|
|
let hash = partition_key.hash();
|
2020-04-19 11:22:28 +00:00
|
|
|
let who = self.replication.read_nodes(&hash, &self.system);
|
2020-04-17 15:09:57 +00:00
|
|
|
|
2020-04-19 15:15:48 +00:00
|
|
|
let rpc = TableRPC::<F>::ReadRange(partition_key.clone(), begin_sort_key, filter, limit);
|
|
|
|
|
2020-04-17 15:09:57 +00:00
|
|
|
let resps = self
|
2020-04-18 17:21:34 +00:00
|
|
|
.rpc_client
|
2020-04-19 11:22:28 +00:00
|
|
|
.try_call_many(
|
|
|
|
&who[..],
|
|
|
|
rpc,
|
|
|
|
self.replication.read_quorum(),
|
|
|
|
TABLE_RPC_TIMEOUT,
|
|
|
|
)
|
2020-04-17 15:09:57 +00:00
|
|
|
.await?;
|
|
|
|
|
|
|
|
let mut ret = BTreeMap::new();
|
|
|
|
let mut to_repair = BTreeMap::new();
|
|
|
|
for resp in resps {
|
|
|
|
if let TableRPC::Update(entries) = resp {
|
|
|
|
for entry_bytes in entries.iter() {
|
2020-04-17 16:51:29 +00:00
|
|
|
let entry =
|
|
|
|
rmp_serde::decode::from_read_ref::<_, F::E>(entry_bytes.as_slice())?;
|
2020-04-17 15:09:57 +00:00
|
|
|
let entry_key = self.tree_key(entry.partition_key(), entry.sort_key());
|
|
|
|
match ret.remove(&entry_key) {
|
|
|
|
None => {
|
|
|
|
ret.insert(entry_key, Some(entry));
|
|
|
|
}
|
|
|
|
Some(Some(mut prev)) => {
|
|
|
|
let must_repair = prev != entry;
|
|
|
|
prev.merge(&entry);
|
|
|
|
if must_repair {
|
|
|
|
to_repair.insert(entry_key.clone(), Some(prev.clone()));
|
|
|
|
}
|
|
|
|
ret.insert(entry_key, Some(prev));
|
|
|
|
}
|
|
|
|
Some(None) => unreachable!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !to_repair.is_empty() {
|
|
|
|
let self2 = self.clone();
|
2020-04-17 16:51:29 +00:00
|
|
|
self.system.background.spawn(async move {
|
|
|
|
for (_, v) in to_repair.iter_mut() {
|
|
|
|
self2.repair_on_read(&who[..], v.take().unwrap()).await?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
});
|
2020-04-17 15:09:57 +00:00
|
|
|
}
|
2020-04-17 16:51:29 +00:00
|
|
|
let ret_vec = ret
|
|
|
|
.iter_mut()
|
|
|
|
.take(limit)
|
|
|
|
.map(|(_k, v)| v.take().unwrap())
|
|
|
|
.collect::<Vec<_>>();
|
2020-04-17 15:09:57 +00:00
|
|
|
Ok(ret_vec)
|
|
|
|
}
|
|
|
|
|
|
|
|
// =============== UTILITY FUNCTION FOR CLIENT OPERATIONS ===============
|
|
|
|
|
|
|
|
async fn repair_on_read(&self, who: &[UUID], what: F::E) -> Result<(), Error> {
|
2020-04-16 12:50:49 +00:00
|
|
|
let what_enc = Arc::new(ByteBuf::from(rmp_to_vec_all_named(&what)?));
|
2020-04-18 17:21:34 +00:00
|
|
|
self.rpc_client
|
|
|
|
.try_call_many(
|
|
|
|
&who[..],
|
|
|
|
TableRPC::<F>::Update(vec![what_enc]),
|
|
|
|
who.len(),
|
2020-04-19 11:22:28 +00:00
|
|
|
TABLE_RPC_TIMEOUT,
|
2020-04-18 17:21:34 +00:00
|
|
|
)
|
2020-04-16 12:50:49 +00:00
|
|
|
.await?;
|
|
|
|
Ok(())
|
2020-04-11 17:43:29 +00:00
|
|
|
}
|
|
|
|
|
2020-04-17 15:09:57 +00:00
|
|
|
// =============== HANDLERS FOR RPC OPERATIONS (SERVER SIDE) ==============
|
|
|
|
|
2020-04-18 17:21:34 +00:00
|
|
|
fn register_handler(self: Arc<Self>, rpc_server: &mut RpcServer, path: String) {
|
|
|
|
rpc_server.add_handler::<TableRPC<F>, _, _>(path, move |msg, _addr| {
|
|
|
|
let self2 = self.clone();
|
|
|
|
async move { self2.handle(msg).await }
|
|
|
|
})
|
2020-04-17 15:09:57 +00:00
|
|
|
}
|
|
|
|
|
2020-04-16 17:28:02 +00:00
|
|
|
async fn handle(self: &Arc<Self>, msg: TableRPC<F>) -> Result<TableRPC<F>, Error> {
|
2020-04-08 21:01:49 +00:00
|
|
|
match msg {
|
2020-04-08 21:47:34 +00:00
|
|
|
TableRPC::ReadEntry(key, sort_key) => {
|
|
|
|
let value = self.handle_read_entry(&key, &sort_key)?;
|
|
|
|
Ok(TableRPC::ReadEntryResponse(value))
|
2020-04-08 21:01:49 +00:00
|
|
|
}
|
2020-04-17 17:38:47 +00:00
|
|
|
TableRPC::ReadRange(key, begin_sort_key, filter, limit) => {
|
|
|
|
let values = self.handle_read_range(&key, &begin_sort_key, &filter, limit)?;
|
|
|
|
Ok(TableRPC::Update(values))
|
|
|
|
}
|
2020-04-08 21:01:49 +00:00
|
|
|
TableRPC::Update(pairs) => {
|
2020-04-08 21:47:34 +00:00
|
|
|
self.handle_update(pairs).await?;
|
2020-04-08 21:01:49 +00:00
|
|
|
Ok(TableRPC::Ok)
|
|
|
|
}
|
2020-04-17 15:09:57 +00:00
|
|
|
TableRPC::SyncRPC(rpc) => {
|
2020-04-17 16:27:29 +00:00
|
|
|
let syncer = self.syncer.load_full().unwrap();
|
2020-04-17 15:09:57 +00:00
|
|
|
let response = syncer
|
|
|
|
.handle_rpc(&rpc, self.system.background.stop_signal.clone())
|
2020-04-17 13:36:16 +00:00
|
|
|
.await?;
|
2020-04-17 15:09:57 +00:00
|
|
|
Ok(TableRPC::SyncRPC(response))
|
2020-04-16 16:41:10 +00:00
|
|
|
}
|
2020-04-19 15:15:48 +00:00
|
|
|
_ => Err(Error::BadRequest(format!("Unexpected table RPC"))),
|
2020-04-08 21:01:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-11 17:43:29 +00:00
|
|
|
fn handle_read_entry(&self, p: &F::P, s: &F::S) -> Result<Option<ByteBuf>, Error> {
|
2020-04-09 14:16:27 +00:00
|
|
|
let tree_key = self.tree_key(p, s);
|
2020-04-08 21:47:34 +00:00
|
|
|
if let Some(bytes) = self.store.get(&tree_key)? {
|
2020-04-11 17:43:29 +00:00
|
|
|
Ok(Some(ByteBuf::from(bytes.to_vec())))
|
2020-04-08 21:47:34 +00:00
|
|
|
} else {
|
|
|
|
Ok(None)
|
2020-04-08 21:01:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-17 17:38:47 +00:00
|
|
|
fn handle_read_range(
|
|
|
|
&self,
|
|
|
|
p: &F::P,
|
2020-04-19 15:15:48 +00:00
|
|
|
s: &Option<F::S>,
|
2020-04-17 17:38:47 +00:00
|
|
|
filter: &Option<F::Filter>,
|
|
|
|
limit: usize,
|
|
|
|
) -> Result<Vec<Arc<ByteBuf>>, Error> {
|
|
|
|
let partition_hash = p.hash();
|
2020-04-19 15:15:48 +00:00
|
|
|
let first_key = match s {
|
|
|
|
None => partition_hash.to_vec(),
|
|
|
|
Some(sk) => self.tree_key(p, sk),
|
|
|
|
};
|
2020-04-17 17:38:47 +00:00
|
|
|
let mut ret = vec![];
|
|
|
|
for item in self.store.range(first_key..) {
|
|
|
|
let (key, value) = item?;
|
|
|
|
if &key[..32] != partition_hash.as_slice() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
let keep = match filter {
|
|
|
|
None => true,
|
|
|
|
Some(f) => {
|
|
|
|
let entry = rmp_serde::decode::from_read_ref::<_, F::E>(value.as_ref())?;
|
|
|
|
F::matches_filter(&entry, f)
|
|
|
|
}
|
|
|
|
};
|
|
|
|
if keep {
|
|
|
|
ret.push(Arc::new(ByteBuf::from(value.as_ref())));
|
|
|
|
}
|
|
|
|
if ret.len() >= limit {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok(ret)
|
|
|
|
}
|
|
|
|
|
2020-04-17 16:51:29 +00:00
|
|
|
pub async fn handle_update(
|
|
|
|
self: &Arc<Self>,
|
|
|
|
mut entries: Vec<Arc<ByteBuf>>,
|
|
|
|
) -> Result<(), Error> {
|
2020-04-19 17:59:59 +00:00
|
|
|
let syncer = self.syncer.load_full().unwrap();
|
2020-04-19 13:14:23 +00:00
|
|
|
let mut epidemic_propagate = vec![];
|
|
|
|
|
2020-04-11 17:43:29 +00:00
|
|
|
for update_bytes in entries.drain(..) {
|
|
|
|
let update = rmp_serde::decode::from_read_ref::<_, F::E>(update_bytes.as_slice())?;
|
|
|
|
|
2020-04-09 18:58:39 +00:00
|
|
|
let tree_key = self.tree_key(update.partition_key(), update.sort_key());
|
|
|
|
|
|
|
|
let (old_entry, new_entry) = self.store.transaction(|db| {
|
2020-04-09 21:45:07 +00:00
|
|
|
let (old_entry, new_entry) = match db.get(&tree_key)? {
|
2020-04-09 18:58:39 +00:00
|
|
|
Some(prev_bytes) => {
|
|
|
|
let old_entry = rmp_serde::decode::from_read_ref::<_, F::E>(&prev_bytes)
|
|
|
|
.map_err(Error::RMPDecode)
|
|
|
|
.map_err(sled::ConflictableTransactionError::Abort)?;
|
2020-04-09 21:45:07 +00:00
|
|
|
let mut new_entry = old_entry.clone();
|
|
|
|
new_entry.merge(&update);
|
|
|
|
(Some(old_entry), new_entry)
|
2020-04-09 18:58:39 +00:00
|
|
|
}
|
2020-04-10 20:01:48 +00:00
|
|
|
None => (None, update.clone()),
|
2020-04-09 18:58:39 +00:00
|
|
|
};
|
2020-04-08 21:01:49 +00:00
|
|
|
|
2020-04-09 18:58:39 +00:00
|
|
|
let new_bytes = rmp_to_vec_all_named(&new_entry)
|
|
|
|
.map_err(Error::RMPEncode)
|
|
|
|
.map_err(sled::ConflictableTransactionError::Abort)?;
|
|
|
|
db.insert(tree_key.clone(), new_bytes)?;
|
2020-04-19 13:14:23 +00:00
|
|
|
Ok((old_entry, new_entry))
|
2020-04-09 18:58:39 +00:00
|
|
|
})?;
|
2020-04-08 21:01:49 +00:00
|
|
|
|
2020-04-19 13:14:23 +00:00
|
|
|
if old_entry.as_ref() != Some(&new_entry) {
|
|
|
|
if self.replication.epidemic_writes() {
|
|
|
|
epidemic_propagate.push(new_entry.clone());
|
|
|
|
}
|
|
|
|
|
2020-04-19 20:52:20 +00:00
|
|
|
self.instance.updated(old_entry, Some(new_entry)).await?;
|
2020-04-19 17:59:59 +00:00
|
|
|
self.system
|
|
|
|
.background
|
|
|
|
.spawn(syncer.clone().invalidate(tree_key));
|
2020-04-16 17:28:02 +00:00
|
|
|
}
|
2020-04-08 21:01:49 +00:00
|
|
|
}
|
2020-04-19 13:14:23 +00:00
|
|
|
|
|
|
|
if epidemic_propagate.len() > 0 {
|
|
|
|
let self2 = self.clone();
|
|
|
|
self.system
|
|
|
|
.background
|
|
|
|
.spawn(async move { self2.insert_many(&epidemic_propagate[..]).await });
|
|
|
|
}
|
|
|
|
|
2020-04-08 21:01:49 +00:00
|
|
|
Ok(())
|
2020-04-08 20:00:41 +00:00
|
|
|
}
|
2020-04-09 14:16:27 +00:00
|
|
|
|
2020-04-16 16:41:10 +00:00
|
|
|
pub async fn delete_range(&self, begin: &Hash, end: &Hash) -> Result<(), Error> {
|
2020-04-19 17:59:59 +00:00
|
|
|
let syncer = self.syncer.load_full().unwrap();
|
|
|
|
|
2020-04-21 12:54:55 +00:00
|
|
|
debug!("({}) Deleting range {:?} - {:?}", self.name, begin, end);
|
2020-04-17 12:49:10 +00:00
|
|
|
let mut count = 0;
|
|
|
|
while let Some((key, _value)) = self.store.get_lt(end.as_slice())? {
|
|
|
|
if key.as_ref() < begin.as_slice() {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if let Some(old_val) = self.store.remove(&key)? {
|
|
|
|
let old_entry = rmp_serde::decode::from_read_ref::<_, F::E>(&old_val)?;
|
2020-04-19 20:52:20 +00:00
|
|
|
self.instance.updated(Some(old_entry), None).await?;
|
2020-04-19 17:59:59 +00:00
|
|
|
self.system
|
|
|
|
.background
|
|
|
|
.spawn(syncer.clone().invalidate(key.to_vec()));
|
2020-04-17 12:49:10 +00:00
|
|
|
count += 1;
|
|
|
|
}
|
|
|
|
}
|
2020-04-21 12:54:55 +00:00
|
|
|
debug!("({}) {} entries deleted", self.name, count);
|
2020-04-16 16:41:10 +00:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2020-04-09 14:16:27 +00:00
|
|
|
fn tree_key(&self, p: &F::P, s: &F::S) -> Vec<u8> {
|
|
|
|
let mut ret = p.hash().to_vec();
|
|
|
|
ret.extend(s.sort_key());
|
|
|
|
ret
|
|
|
|
}
|
2020-04-08 20:00:41 +00:00
|
|
|
}
|