aboutsummaryrefslogtreecommitdiff
path: root/src/table.rs
blob: f45f48c224f986a5b3732e82aa10dc54e4ab945b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
use std::time::Duration;
use std::sync::Arc;
use serde::{Serialize, Deserialize};
use async_trait::async_trait;
use reduce::Reduce;

use crate::error::Error;
use crate::proto::*;
use crate::data::*;
use crate::membership::System;
use crate::rpc_client::*;


pub struct Table<F: TableFormat> {
	pub instance: F,

	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)?)
	}
}

#[derive(Serialize, Deserialize)]
pub enum TableRPC<F: TableFormat> {
	Ok,
	Read(Vec<F::K>),
	Update(Vec<(F::K, F::V)>),
}

pub struct Partition {
	pub begin: Hash,
	pub end: Hash,
	pub other_nodes: Vec<UUID>,
}

pub trait KeyHash {
	fn hash(&self) -> Hash;
}

pub trait ValueMerge {
	fn merge(&mut self, other: &Self);
}

#[async_trait]
pub trait TableFormat: Send + Sync {
	type K: Clone + PartialEq + Serialize + for<'de> Deserialize<'de> + KeyHash + Send + Sync;
	type V: Clone + Serialize + for<'de> Deserialize<'de> + ValueMerge + Send + Sync;

	async fn updated(&self, key: &Self::K, old: Option<&Self::V>, new: &Self::V);
}

impl<F: TableFormat + 'static> Table<F> {
	pub fn new(instance: F, system: Arc<System>, db: &sled::Db, name: String, param: TableReplicationParams) -> Self {
		let store = db.open_tree(&name)
				.expect("Unable to open DB tree");
		Self{
			instance,
			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> {
		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(())
	}

	pub async fn get(&self, k: &F::K) -> Result<F::V, Error> {
		let hash = k.hash();
		let who = self.system.members.read().await
			.walk_ring(&hash, self.param.replication_factor);

		let rpc = &TableRPC::<F>::Read(vec![k.clone()]);
		let resps = self.rpc_try_call_many(&who[..],
										   &rpc,
										   self.param.read_quorum)
			.await?;

		let mut values = vec![];
		for resp in resps {
			if let TableRPC::Update(mut pairs) = resp {
				if pairs.len() == 0 {
					continue;
				} else if pairs.len() == 1 && pairs[0].0 == *k {
					values.push(pairs.drain(..).next().unwrap().1);
					continue;
				}
			}
			return Err(Error::Message(format!("Invalid return value to read")));
		}
		values.drain(..)
			.reduce(|mut x, y| { x.merge(&y); x })
			.map(Ok)
			.unwrap_or(Err(Error::NotFound))
	}

	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,
						  self.param.timeout).await?;

		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)
	}

	async fn handle(&self, msg: TableRPC<F>) -> Result<TableRPC<F>, Error> {
		match msg {
			TableRPC::Read(keys) => {
				Ok(TableRPC::Update(self.handle_read(&keys)?))
			}
			TableRPC::Update(pairs) => {
				self.handle_write(pairs).await?;
				Ok(TableRPC::Ok)
			}
			_ => Err(Error::RPCError(format!("Unexpected table RPC")))
		}
	}

	fn handle_read(&self, keys: &[F::K]) -> Result<Vec<(F::K, F::V)>, Error> {
		let mut results = vec![];
		for key in keys.iter() {
			if let Some(bytes) = self.store.get(&key.hash())? {
				let pair = rmp_serde::decode::from_read_ref::<_, (F::K, F::V)>(bytes.as_ref())?;
				results.push(pair);
			}
		}
		Ok(results)
	}

	async fn handle_write(&self, mut pairs: Vec<(F::K, F::V)>) -> Result<(), Error> {
		for mut pair in pairs.drain(..) {
			let hash = pair.0.hash();

			let old_val = match self.store.get(&hash)? {
				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)?;
			self.store.insert(&hash, new_bytes)?;

			self.instance.updated(&pair.0, old_val.as_ref(), &pair.1).await;
		}
		Ok(())
	}
}