-
Notifications
You must be signed in to change notification settings - Fork 552
/
raft.rs
393 lines (342 loc) · 12.8 KB
/
raft.rs
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
use super::super::types::schema::Table;
use super::super::types::{Expression, Row, Rows, Value};
use super::{Catalog, Engine as _, IndexScan, Transaction as _};
use crate::encoding::{self, bincode, Value as _};
use crate::errdata;
use crate::error::Result;
use crate::raft::{self, Entry};
use crate::storage::{self, mvcc::TransactionState};
use crossbeam::channel::Sender;
use serde::{de::DeserializeOwned, Deserialize, Serialize};
use std::collections::HashSet;
/// A Raft state machine mutation.
///
/// TODO: use Cows for these.
#[derive(Clone, Serialize, Deserialize)]
enum Mutation {
/// Begins a read-write transaction
Begin,
/// Commits the given transaction
Commit(TransactionState),
/// Rolls back the given transaction
Rollback(TransactionState),
/// Creates a new row
Create { txn: TransactionState, table: String, row: Row },
/// Deletes a row
Delete { txn: TransactionState, table: String, id: Value },
/// Updates a row
Update { txn: TransactionState, table: String, id: Value, row: Row },
/// Creates a table
CreateTable { txn: TransactionState, schema: Table },
/// Deletes a table
DeleteTable { txn: TransactionState, table: String },
}
impl encoding::Value for Mutation {}
/// A Raft state machine query.
///
/// TODO: use Cows for these.
#[derive(Clone, Serialize, Deserialize)]
enum Query {
/// Begins a read-only transaction
BeginReadOnly { as_of: Option<u64> },
/// Fetches engine status
Status,
/// Reads a row
Read { txn: TransactionState, table: String, id: Value },
/// Reads an index entry
ReadIndex { txn: TransactionState, table: String, column: String, value: Value },
/// Scans a table's rows
Scan { txn: TransactionState, table: String, filter: Option<Expression> },
/// Scans an index
ScanIndex { txn: TransactionState, table: String, column: String },
/// Scans the tables
ScanTables { txn: TransactionState },
/// Reads a table
ReadTable { txn: TransactionState, table: String },
}
impl encoding::Value for Query {}
/// Status for the Raft SQL engine.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Status {
pub raft: raft::Status,
pub mvcc: storage::mvcc::Status,
}
/// A client for the local Raft node.
#[derive(Clone)]
struct Client {
tx: Sender<(raft::Request, Sender<Result<raft::Response>>)>,
}
impl Client {
/// Creates a new Raft client.
fn new(tx: Sender<(raft::Request, Sender<Result<raft::Response>>)>) -> Self {
Self { tx }
}
/// Executes a request against the Raft cluster.
fn execute(&self, request: raft::Request) -> Result<raft::Response> {
let (response_tx, response_rx) = crossbeam::channel::bounded(1);
self.tx.send((request, response_tx))?;
response_rx.recv()?
}
/// Mutates the Raft state machine, deserializing the response into the
/// return type.
fn mutate<V: DeserializeOwned>(&self, mutation: Mutation) -> Result<V> {
match self.execute(raft::Request::Write(mutation.encode()))? {
raft::Response::Write(response) => Ok(bincode::deserialize(&response)?),
resp => errdata!("unexpected Raft mutation response {resp:?}"),
}
}
/// Queries the Raft state machine, deserializing the response into the
/// return type.
fn query<V: DeserializeOwned>(&self, query: Query) -> Result<V> {
match self.execute(raft::Request::Read(query.encode()))? {
raft::Response::Read(response) => Ok(bincode::deserialize(&response)?),
resp => errdata!("unexpected Raft query response {resp:?}"),
}
}
/// Fetches Raft node status.
fn status(&self) -> Result<raft::Status> {
match self.execute(raft::Request::Status)? {
raft::Response::Status(status) => Ok(status),
resp => errdata!("unexpected Raft status response {resp:?}"),
}
}
}
/// A SQL engine using a Raft state machine.
#[derive(Clone)]
pub struct Raft {
client: Client,
}
impl Raft {
/// Creates a new Raft-based SQL engine.
pub fn new(tx: Sender<(raft::Request, Sender<Result<raft::Response>>)>) -> Self {
Self { client: Client::new(tx) }
}
/// Creates an underlying state machine for a Raft engine.
pub fn new_state<E: storage::Engine>(engine: E) -> Result<State<E>> {
State::new(engine)
}
/// Returns Raft SQL engine status.
pub fn status(&self) -> Result<Status> {
Ok(Status { raft: self.client.status()?, mvcc: self.client.query(Query::Status)? })
}
}
impl super::Engine for Raft {
type Transaction = Transaction;
fn begin(&self) -> Result<Self::Transaction> {
Transaction::begin(self.client.clone(), false, None)
}
fn begin_read_only(&self) -> Result<Self::Transaction> {
Transaction::begin(self.client.clone(), true, None)
}
fn begin_as_of(&self, version: u64) -> Result<Self::Transaction> {
Transaction::begin(self.client.clone(), true, Some(version))
}
}
/// A Raft-based SQL transaction.
#[derive(Clone)]
pub struct Transaction {
client: Client,
state: TransactionState,
}
impl Transaction {
/// Starts a transaction in the given mode.
fn begin(client: Client, read_only: bool, as_of: Option<u64>) -> Result<Self> {
let state = if read_only || as_of.is_some() {
client.query(Query::BeginReadOnly { as_of })?
} else {
client.mutate(Mutation::Begin)?
};
Ok(Self { client, state })
}
}
impl super::Transaction for Transaction {
fn version(&self) -> u64 {
self.state.version
}
fn read_only(&self) -> bool {
self.state.read_only
}
fn commit(self) -> Result<()> {
if !self.read_only() {
self.client.mutate(Mutation::Commit(self.state.clone()))?
}
Ok(())
}
fn rollback(self) -> Result<()> {
if !self.read_only() {
self.client.mutate(Mutation::Rollback(self.state.clone()))?;
}
Ok(())
}
fn create(&mut self, table: &str, row: Row) -> Result<()> {
self.client.mutate(Mutation::Create {
txn: self.state.clone(),
table: table.to_string(),
row,
})
}
fn delete(&mut self, table: &str, id: &Value) -> Result<()> {
self.client.mutate(Mutation::Delete {
txn: self.state.clone(),
table: table.to_string(),
id: id.clone(),
})
}
fn read(&self, table: &str, id: &Value) -> Result<Option<Row>> {
self.client.query(Query::Read {
txn: self.state.clone(),
table: table.to_string(),
id: id.clone(),
})
}
fn read_index(&self, table: &str, column: &str, value: &Value) -> Result<HashSet<Value>> {
self.client.query(Query::ReadIndex {
txn: self.state.clone(),
table: table.to_string(),
column: column.to_string(),
value: value.clone(),
})
}
fn scan(&self, table: &str, filter: Option<Expression>) -> Result<Rows> {
Ok(Box::new(
self.client
.query::<Vec<_>>(Query::Scan {
txn: self.state.clone(),
table: table.to_string(),
filter,
})?
.into_iter()
.map(Ok),
))
}
fn scan_index(&self, table: &str, column: &str) -> Result<IndexScan> {
Ok(Box::new(
self.client
.query::<Vec<_>>(Query::ScanIndex {
txn: self.state.clone(),
table: table.to_string(),
column: column.to_string(),
})?
.into_iter()
.map(Ok),
))
}
fn update(&mut self, table: &str, id: &Value, row: Row) -> Result<()> {
self.client.mutate(Mutation::Update {
txn: self.state.clone(),
table: table.to_string(),
id: id.clone(),
row,
})
}
}
impl Catalog for Transaction {
fn create_table(&mut self, table: Table) -> Result<()> {
self.client.mutate(Mutation::CreateTable { txn: self.state.clone(), schema: table })
}
fn delete_table(&mut self, table: &str) -> Result<()> {
self.client
.mutate(Mutation::DeleteTable { txn: self.state.clone(), table: table.to_string() })
}
fn read_table(&self, table: &str) -> Result<Option<Table>> {
self.client.query(Query::ReadTable { txn: self.state.clone(), table: table.to_string() })
}
fn list_tables(&self) -> Result<Vec<Table>> {
self.client.query(Query::ScanTables { txn: self.state.clone() })
}
}
/// The Raft state machine for the Raft-based SQL engine, using a KV SQL engine
pub struct State<E: storage::Engine> {
/// The underlying local SQL engine.
engine: super::Local<E>,
/// The last applied index
applied_index: u64,
}
impl<E: storage::Engine> State<E> {
/// Creates a new Raft state maching using the given storage engine.
pub fn new(engine: E) -> Result<Self> {
let engine = super::Local::new(engine);
let applied_index = engine
.get_metadata(b"applied_index")?
.map(|b| bincode::deserialize(&b))
.unwrap_or(Ok(0))?;
Ok(State { engine, applied_index })
}
/// Mutates the state machine.
fn mutate(&mut self, mutation: Mutation) -> Result<Vec<u8>> {
let response = match mutation {
Mutation::Begin => self.engine.begin()?.state().encode(),
Mutation::Commit(txn) => bincode::serialize(&self.engine.resume(txn)?.commit()?),
Mutation::Rollback(txn) => bincode::serialize(&self.engine.resume(txn)?.rollback()?),
Mutation::Create { txn, table, row } => {
bincode::serialize(&self.engine.resume(txn)?.create(&table, row)?)
}
Mutation::Delete { txn, table, id } => {
bincode::serialize(&self.engine.resume(txn)?.delete(&table, &id)?)
}
Mutation::Update { txn, table, id, row } => {
bincode::serialize(&self.engine.resume(txn)?.update(&table, &id, row)?)
}
Mutation::CreateTable { txn, schema } => {
bincode::serialize(&self.engine.resume(txn)?.create_table(schema)?)
}
Mutation::DeleteTable { txn, table } => {
bincode::serialize(&self.engine.resume(txn)?.delete_table(&table)?)
}
};
Ok(response)
}
}
impl<E: storage::Engine> raft::State for State<E> {
fn get_applied_index(&self) -> u64 {
self.applied_index
}
fn apply(&mut self, entry: Entry) -> Result<Vec<u8>> {
assert_eq!(entry.index, self.applied_index + 1, "entry index not after applied index");
let result = match &entry.command {
Some(command) => match self.mutate(Mutation::decode(command)?) {
// We must panic on non-deterministic apply failures to prevent
// replica divergence. See [`raft::State`] documentation for
// details.
Err(e) if !e.is_deterministic() => panic!("non-deterministic apply failure: {e}"),
result => result,
},
None => Ok(Vec::new()),
};
self.applied_index = entry.index;
self.engine.set_metadata(b"applied_index", bincode::serialize(&entry.index))?;
result
}
fn read(&self, command: Vec<u8>) -> Result<Vec<u8>> {
let response = match Query::decode(&command)? {
Query::BeginReadOnly { as_of } => {
let txn = if let Some(version) = as_of {
self.engine.begin_as_of(version)?
} else {
self.engine.begin_read_only()?
};
txn.state().encode()
}
Query::Read { txn, table, id } => self.engine.resume(txn)?.read(&table, &id)?.encode(),
Query::ReadIndex { txn, table, column, value } => {
self.engine.resume(txn)?.read_index(&table, &column, &value)?.encode()
}
// FIXME These need to stream rows somehow
Query::Scan { txn, table, filter } => {
self.engine.resume(txn)?.scan(&table, filter)?.collect::<Result<Vec<_>>>()?.encode()
}
Query::ScanIndex { txn, table, column } => self
.engine
.resume(txn)?
.scan_index(&table, &column)?
.collect::<Result<Vec<_>>>()?
.encode(),
Query::Status => self.engine.kv.status()?.encode(),
Query::ReadTable { txn, table } => {
self.engine.resume(txn)?.read_table(&table)?.encode()
}
Query::ScanTables { txn } => self.engine.resume(txn)?.list_tables()?.encode(),
};
Ok(response)
}
}