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
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
#![cfg_attr(feature = "docinclude", feature(external_doc))]
#![cfg_attr(feature = "docinclude", doc(include = "../README.md"))]
#[cfg(test)]
mod test;
use std::collections::{BTreeMap, HashMap};
use std::io::Cursor;
use agreed::async_trait::async_trait;
use agreed::raft::{Entry, EntryPayload, MembershipConfig};
use agreed::storage::{CurrentSnapshotData, HardState, InitialState};
use agreed::{AppData, AppDataResponse, NodeId, RaftStorage};
use anyhow::Result;
use serde::{Deserialize, Serialize};
use thiserror::Error;
use tokio::sync::RwLock;
use tokio::sync::{RwLockReadGuard, RwLockWriteGuard};
const ERR_INCONSISTENT_LOG: &str =
"a query was received which was expecting data to be in place which does not exist in the log";
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ClientRequest {
pub client: String,
pub serial: u64,
pub status: String,
}
impl AppData for ClientRequest {}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct ClientResponse(Option<String>);
impl AppDataResponse for ClientResponse {}
#[derive(Clone, Debug, Error)]
pub enum ShutdownError {
#[error("unsafe storage error")]
UnsafeStorageError,
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct MemStoreSnapshot {
pub index: u64,
pub term: u64,
pub membership: MembershipConfig,
pub data: Vec<u8>,
}
#[derive(Serialize, Deserialize, Debug, Default, Clone)]
pub struct MemStoreStateMachine {
pub last_applied_log: u64,
pub client_serial_responses: HashMap<String, (u64, Option<String>)>,
pub client_status: HashMap<String, String>,
}
pub struct MemStore {
id: NodeId,
log: RwLock<BTreeMap<u64, Entry<ClientRequest>>>,
sm: RwLock<MemStoreStateMachine>,
hs: RwLock<Option<HardState>>,
current_snapshot: RwLock<Option<MemStoreSnapshot>>,
}
impl MemStore {
pub fn new(id: NodeId) -> Self {
let log = RwLock::new(BTreeMap::new());
let sm = RwLock::new(MemStoreStateMachine::default());
let hs = RwLock::new(None);
let current_snapshot = RwLock::new(None);
Self {
id,
log,
sm,
hs,
current_snapshot,
}
}
#[cfg(test)]
pub fn new_with_state(
id: NodeId,
log: BTreeMap<u64, Entry<ClientRequest>>,
sm: MemStoreStateMachine,
hs: Option<HardState>,
current_snapshot: Option<MemStoreSnapshot>,
) -> Self {
let log = RwLock::new(log);
let sm = RwLock::new(sm);
let hs = RwLock::new(hs);
let current_snapshot = RwLock::new(current_snapshot);
Self {
id,
log,
sm,
hs,
current_snapshot,
}
}
pub async fn get_log(&self) -> RwLockWriteGuard<'_, BTreeMap<u64, Entry<ClientRequest>>> {
self.log.write().await
}
pub async fn get_state_machine(&self) -> RwLockWriteGuard<'_, MemStoreStateMachine> {
self.sm.write().await
}
pub async fn read_hard_state(&self) -> RwLockReadGuard<'_, Option<HardState>> {
self.hs.read().await
}
}
#[async_trait]
impl RaftStorage<ClientRequest, ClientResponse> for MemStore {
type Snapshot = Cursor<Vec<u8>>;
type ShutdownError = ShutdownError;
#[tracing::instrument(level = "trace", skip(self))]
async fn get_membership_config(&self) -> Result<MembershipConfig> {
let log = self.log.read().await;
let cfg_opt = log.values().rev().find_map(|entry| match &entry.payload {
EntryPayload::ConfigChange(cfg) => Some(cfg.membership.clone()),
EntryPayload::SnapshotPointer(snap) => Some(snap.membership.clone()),
_ => None,
});
Ok(match cfg_opt {
Some(cfg) => cfg,
None => MembershipConfig::new_initial(self.id),
})
}
#[tracing::instrument(level = "trace", skip(self))]
async fn get_initial_state(&self) -> Result<InitialState> {
let membership = self.get_membership_config().await?;
let mut hs = self.hs.write().await;
let log = self.log.read().await;
let sm = self.sm.read().await;
match &mut *hs {
Some(inner) => {
let (last_log_index, last_log_term) = match log.values().rev().next() {
Some(log) => (log.index, log.term),
None => (0, 0),
};
let last_applied_log = sm.last_applied_log;
Ok(InitialState {
last_log_index,
last_log_term,
last_applied_log,
hard_state: inner.clone(),
membership,
})
}
None => {
let new = InitialState::new_initial(self.id);
*hs = Some(new.hard_state.clone());
Ok(new)
}
}
}
#[tracing::instrument(level = "trace", skip(self, hs))]
async fn save_hard_state(&self, hs: &HardState) -> Result<()> {
*self.hs.write().await = Some(hs.clone());
Ok(())
}
#[tracing::instrument(level = "trace", skip(self))]
async fn get_log_entries(&self, start: u64, stop: u64) -> Result<Vec<Entry<ClientRequest>>> {
if start > stop {
tracing::error!("invalid request, start > stop");
return Ok(vec![]);
}
let log = self.log.read().await;
Ok(log.range(start..stop).map(|(_, val)| val.clone()).collect())
}
#[tracing::instrument(level = "trace", skip(self))]
async fn delete_logs_from(&self, start: u64, stop: Option<u64>) -> Result<()> {
if stop.as_ref().map(|stop| &start > stop).unwrap_or(false) {
tracing::error!("invalid request, start > stop");
return Ok(());
}
let mut log = self.log.write().await;
if let Some(stop) = stop.as_ref() {
for key in start..*stop {
log.remove(&key);
}
return Ok(());
}
log.split_off(&start);
Ok(())
}
#[tracing::instrument(level = "trace", skip(self, entry))]
async fn append_entry_to_log(&self, entry: &Entry<ClientRequest>) -> Result<()> {
let mut log = self.log.write().await;
log.insert(entry.index, entry.clone());
Ok(())
}
#[tracing::instrument(level = "trace", skip(self, entries))]
async fn replicate_to_log(&self, entries: &[Entry<ClientRequest>]) -> Result<()> {
let mut log = self.log.write().await;
for entry in entries {
log.insert(entry.index, entry.clone());
}
Ok(())
}
#[tracing::instrument(level = "trace", skip(self, data))]
async fn apply_entry_to_state_machine(
&self,
index: &u64,
data: &ClientRequest,
) -> Result<ClientResponse> {
let mut sm = self.sm.write().await;
sm.last_applied_log = *index;
if let Some((serial, res)) = sm.client_serial_responses.get(&data.client) {
if serial == &data.serial {
return Ok(ClientResponse(res.clone()));
}
}
let previous = sm
.client_status
.insert(data.client.clone(), data.status.clone());
sm.client_serial_responses
.insert(data.client.clone(), (data.serial, previous.clone()));
Ok(ClientResponse(previous))
}
#[tracing::instrument(level = "trace", skip(self, entries))]
async fn replicate_to_state_machine(&self, entries: &[(&u64, &ClientRequest)]) -> Result<()> {
let mut sm = self.sm.write().await;
for (index, data) in entries {
sm.last_applied_log = **index;
if let Some((serial, _)) = sm.client_serial_responses.get(&data.client) {
if serial == &data.serial {
continue;
}
}
let previous = sm
.client_status
.insert(data.client.clone(), data.status.clone());
sm.client_serial_responses
.insert(data.client.clone(), (data.serial, previous.clone()));
}
Ok(())
}
#[tracing::instrument(level = "trace", skip(self))]
async fn do_log_compaction(&self) -> Result<CurrentSnapshotData<Self::Snapshot>> {
let (data, last_applied_log);
{
let sm = self.sm.read().await;
data = serde_json::to_vec(&*sm)?;
last_applied_log = sm.last_applied_log;
}
let membership_config;
{
let log = self.log.read().await;
membership_config = log
.values()
.rev()
.skip_while(|entry| entry.index > last_applied_log)
.find_map(|entry| match &entry.payload {
EntryPayload::ConfigChange(cfg) => Some(cfg.membership.clone()),
_ => None,
})
.unwrap_or_else(|| MembershipConfig::new_initial(self.id));
}
let snapshot_bytes: Vec<u8>;
let term;
{
let mut log = self.log.write().await;
let mut current_snapshot = self.current_snapshot.write().await;
term = log
.get(&last_applied_log)
.map(|entry| entry.term)
.ok_or_else(|| anyhow::anyhow!(ERR_INCONSISTENT_LOG))?;
*log = log.split_off(&last_applied_log);
log.insert(
last_applied_log,
Entry::new_snapshot_pointer(
last_applied_log,
term,
"".into(),
membership_config.clone(),
),
);
let snapshot = MemStoreSnapshot {
index: last_applied_log,
term,
membership: membership_config.clone(),
data,
};
snapshot_bytes = serde_json::to_vec(&snapshot)?;
*current_snapshot = Some(snapshot);
}
tracing::trace!(
{ snapshot_size = snapshot_bytes.len() },
"log compaction complete"
);
Ok(CurrentSnapshotData {
term,
index: last_applied_log,
membership: membership_config.clone(),
snapshot: Box::new(Cursor::new(snapshot_bytes)),
})
}
#[tracing::instrument(level = "trace", skip(self))]
async fn create_snapshot(&self) -> Result<(String, Box<Self::Snapshot>)> {
Ok((String::from(""), Box::new(Cursor::new(Vec::new()))))
}
#[tracing::instrument(level = "trace", skip(self, snapshot))]
async fn finalize_snapshot_installation(
&self,
index: u64,
term: u64,
delete_through: Option<u64>,
id: String,
snapshot: Box<Self::Snapshot>,
) -> Result<()> {
tracing::trace!(
{ snapshot_size = snapshot.get_ref().len() },
"decoding snapshot for installation"
);
let raw = serde_json::to_string_pretty(snapshot.get_ref().as_slice())?;
println!("JSON SNAP:\n{}", raw);
let new_snapshot: MemStoreSnapshot = serde_json::from_slice(snapshot.get_ref().as_slice())?;
{
let mut log = self.log.write().await;
let membership_config = log
.values()
.rev()
.skip_while(|entry| entry.index > index)
.find_map(|entry| match &entry.payload {
EntryPayload::ConfigChange(cfg) => Some(cfg.membership.clone()),
_ => None,
})
.unwrap_or_else(|| MembershipConfig::new_initial(self.id));
match &delete_through {
Some(through) => {
*log = log.split_off(&(through + 1));
}
None => log.clear(),
}
log.insert(
index,
Entry::new_snapshot_pointer(index, term, id, membership_config),
);
}
{
let new_sm: MemStoreStateMachine = serde_json::from_slice(&new_snapshot.data)?;
let mut sm = self.sm.write().await;
*sm = new_sm;
}
let mut current_snapshot = self.current_snapshot.write().await;
*current_snapshot = Some(new_snapshot);
Ok(())
}
#[tracing::instrument(level = "trace", skip(self))]
async fn get_current_snapshot(&self) -> Result<Option<CurrentSnapshotData<Self::Snapshot>>> {
match &*self.current_snapshot.read().await {
Some(snapshot) => {
let reader = serde_json::to_vec(&snapshot)?;
Ok(Some(CurrentSnapshotData {
index: snapshot.index,
term: snapshot.term,
membership: snapshot.membership.clone(),
snapshot: Box::new(Cursor::new(reader)),
}))
}
None => Ok(None),
}
}
}