garage/src/table.rs

215 lines
5.4 KiB
Rust
Raw Normal View History

2020-04-08 20:00:41 +00:00
use std::time::Duration;
use std::sync::Arc;
use serde::{Serialize, Deserialize};
use async_trait::async_trait;
use crate::error::Error;
use crate::proto::*;
use crate::data::*;
use crate::membership::System;
use crate::rpc_client::*;
pub struct Table<F: TableFormat> {
2020-04-08 21:01:49 +00:00
pub instance: F,
2020-04-08 20:00:41 +00:00
pub name: String,
pub system: Arc<System>,
pub store: sled::Tree,
pub partitions: Vec<Partition>,
pub param: TableReplicationParams,
}
#[derive(Clone)]
pub struct TableReplicationParams {
pub replication_factor: usize,
pub read_quorum: usize,
pub write_quorum: usize,
pub timeout: Duration,
}
#[async_trait]
pub trait TableRpcHandler {
async fn handle(&self, rpc: &[u8]) -> Result<Vec<u8>, Error>;
}
struct TableRpcHandlerAdapter<F: TableFormat> {
table: Arc<Table<F>>,
}
#[async_trait]
impl<F: TableFormat + 'static> TableRpcHandler for TableRpcHandlerAdapter<F> {
async fn handle(&self, rpc: &[u8]) -> Result<Vec<u8>, Error> {
let msg = rmp_serde::decode::from_read_ref::<_, TableRPC<F>>(rpc)?;
let rep = self.table.handle(msg).await?;
Ok(rmp_serde::encode::to_vec_named(&rep)?)
}
}
2020-04-08 21:01:49 +00:00
#[derive(Serialize, Deserialize)]
2020-04-08 20:00:41 +00:00
pub enum TableRPC<F: TableFormat> {
2020-04-08 21:01:49 +00:00
Ok,
2020-04-08 21:47:34 +00:00
ReadEntry(F::K, Vec<u8>),
ReadEntryResponse(Option<F::V>),
2020-04-08 21:01:49 +00:00
Update(Vec<(F::K, F::V)>),
2020-04-08 20:00:41 +00:00
}
pub struct Partition {
pub begin: Hash,
pub end: Hash,
pub other_nodes: Vec<UUID>,
}
2020-04-08 21:47:34 +00:00
pub trait TableKey {
2020-04-08 20:00:41 +00:00
fn hash(&self) -> Hash;
}
2020-04-08 21:47:34 +00:00
pub trait TableValue {
fn sort_key(&self) -> Vec<u8>;
2020-04-08 20:00:41 +00:00
fn merge(&mut self, other: &Self);
}
#[async_trait]
pub trait TableFormat: Send + Sync {
2020-04-08 21:47:34 +00:00
type K: Clone + PartialEq + Serialize + for<'de> Deserialize<'de> + TableKey + Send + Sync;
type V: Clone + Serialize + for<'de> Deserialize<'de> + TableValue + Send + Sync;
2020-04-08 20:00:41 +00:00
async fn updated(&self, key: &Self::K, old: Option<&Self::V>, new: &Self::V);
}
impl<F: TableFormat + 'static> Table<F> {
2020-04-08 21:01:49 +00:00
pub fn new(instance: F, system: Arc<System>, db: &sled::Db, name: String, param: TableReplicationParams) -> Self {
2020-04-08 20:00:41 +00:00
let store = db.open_tree(&name)
.expect("Unable to open DB tree");
Self{
2020-04-08 21:01:49 +00:00
instance,
2020-04-08 20:00:41 +00:00
name,
system,
store,
partitions: Vec::new(),
param,
}
}
pub fn rpc_handler(self: Arc<Self>) -> Box<dyn TableRpcHandler + Send + Sync> {
Box::new(TableRpcHandlerAdapter::<F>{ table: self })
}
pub async fn insert(&self, k: &F::K, v: &F::V) -> Result<(), Error> {
2020-04-08 21:01:49 +00:00
let hash = k.hash();
let who = self.system.members.read().await
.walk_ring(&hash, self.param.replication_factor);
let rpc = &TableRPC::<F>::Update(vec![(k.clone(), v.clone())]);
self.rpc_try_call_many(&who[..],
&rpc,
self.param.write_quorum).await?;
Ok(())
}
2020-04-08 20:00:41 +00:00
2020-04-08 21:47:34 +00:00
pub async fn get(&self, k: &F::K, sort_key: &[u8]) -> Result<Option<F::V>, Error> {
2020-04-08 20:00:41 +00:00
let hash = k.hash();
let who = self.system.members.read().await
.walk_ring(&hash, self.param.replication_factor);
2020-04-08 21:47:34 +00:00
let rpc = &TableRPC::<F>::ReadEntry(k.clone(), sort_key.to_vec());
2020-04-08 21:01:49 +00:00
let resps = self.rpc_try_call_many(&who[..],
&rpc,
self.param.read_quorum)
.await?;
2020-04-08 21:47:34 +00:00
let mut ret = None;
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 {
if let Some(v) = value {
ret = match ret {
None => Some(v),
Some(mut x) => {
x.merge(&v);
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-08 21:47:34 +00:00
Ok(ret)
2020-04-08 21:01:49 +00:00
}
async fn rpc_try_call_many(&self, who: &[UUID], rpc: &TableRPC<F>, quorum: usize) -> Result<Vec<TableRPC<F>>, Error> {
let rpc_bytes = rmp_serde::encode::to_vec_named(rpc)?;
let rpc_msg = Message::TableRPC(self.name.to_string(), rpc_bytes);
let resps = rpc_try_call_many(self.system.clone(),
who,
&rpc_msg,
quorum,
2020-04-08 20:00:41 +00:00
self.param.timeout).await?;
2020-04-08 21:01:49 +00:00
let mut resps_vals = vec![];
for resp in resps {
if let Message::TableRPC(tbl, rep_by) = &resp {
if *tbl == self.name {
resps_vals.push(rmp_serde::decode::from_read_ref(&rep_by)?);
continue;
}
}
return Err(Error::Message(format!("Invalid reply to TableRPC: {:?}", resp)))
}
Ok(resps_vals)
2020-04-08 20:00:41 +00:00
}
async fn handle(&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
}
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)
}
_ => Err(Error::RPCError(format!("Unexpected table RPC")))
}
}
2020-04-08 21:47:34 +00:00
fn handle_read_entry(&self, key: &F::K, sort_key: &[u8]) -> Result<Option<F::V>, Error> {
let mut tree_key = key.hash().to_vec();
tree_key.extend(sort_key);
if let Some(bytes) = self.store.get(&tree_key)? {
let (_, v) = rmp_serde::decode::from_read_ref::<_, (F::K, F::V)>(&bytes)?;
Ok(Some(v))
} else {
Ok(None)
2020-04-08 21:01:49 +00:00
}
}
2020-04-08 21:47:34 +00:00
async fn handle_update(&self, mut pairs: Vec<(F::K, F::V)>) -> Result<(), Error> {
2020-04-08 21:01:49 +00:00
for mut pair in pairs.drain(..) {
2020-04-08 21:47:34 +00:00
let mut tree_key = pair.0.hash().to_vec();
tree_key.extend(pair.1.sort_key());
2020-04-08 21:01:49 +00:00
2020-04-08 21:47:34 +00:00
let old_val = match self.store.get(&tree_key)? {
2020-04-08 21:01:49 +00:00
Some(prev_bytes) => {
let (_, old_val) = rmp_serde::decode::from_read_ref::<_, (F::K, F::V)>(&prev_bytes)?;
pair.1.merge(&old_val);
Some(old_val)
}
None => None
};
let new_bytes = rmp_serde::encode::to_vec_named(&pair)?;
2020-04-08 21:47:34 +00:00
self.store.insert(&tree_key, new_bytes)?;
2020-04-08 21:01:49 +00:00
self.instance.updated(&pair.0, old_val.as_ref(), &pair.1).await;
}
Ok(())
2020-04-08 20:00:41 +00:00
}
}