-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfjall_storage.rs
More file actions
432 lines (374 loc) · 14.6 KB
/
fjall_storage.rs
File metadata and controls
432 lines (374 loc) · 14.6 KB
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
429
430
431
432
//! [`fjall`] based storage for Raft logs and state.
//!
//! This module provides an implementation of the `LogStorage` trait that persists
//! Raft entries and hard state to a [`fjall`] database.
use crate::Result;
use crate::log_storage::{Entry, HardState, LogStorage};
use fjall::UserValue;
use raft::eraftpb::{ConfState, Snapshot};
use raft::{GetEntriesContext, RaftState, Storage, StorageError};
const COMMIT_KEY: &[u8] = b"c";
const TERM_KEY: &[u8] = b"t";
const VOTE_KEY: &[u8] = b"v";
/// Storage implementation that persists logs to Fjall.
pub struct FjallStorage {
/// Cached Raft state.
raft_state: RaftState,
/// The index of the first entry in the storage.
first_log_index: u64,
/// The index of the last entry in the storage.
last_log_index: u64,
/// [`fjall`] database.
db: fjall::Database,
/// [`fjall`] keyspace containing log entries.
log_keyspace: fjall::Keyspace,
/// [`fjall`] keyspace containing hard state.
hard_state_keyspace: fjall::Keyspace,
}
impl FjallStorage {
/// Creates a new [`FjallStorage`] using the provided database and keyspaces.
///
/// # Errors
///
/// Returns an error if [`fjall`] returns an error when reading from the database.
pub fn new(
db: fjall::Database,
log_keyspace: fjall::Keyspace,
hard_state_keyspace: fjall::Keyspace,
) -> Result<Self> {
let first_log_index = match log_keyspace.first_key_value() {
Some(key_value) => {
let entry: Entry = key_value.value()?.try_into()?;
entry.index
}
None => 1,
};
let last_log_index = match log_keyspace.last_key_value() {
Some(key_value) => {
let entry: Entry = key_value.value()?.try_into()?;
entry.index
}
None => 0,
};
// Load hard state from disk if it exists
let mut raft_state = RaftState::default();
if let (Some(commit), Some(term), Some(vote)) = (
hard_state_keyspace.get(COMMIT_KEY)?,
hard_state_keyspace.get(TERM_KEY)?,
hard_state_keyspace.get(VOTE_KEY)?,
) {
let commit_bytes = commit
.as_ref()
.try_into()
.map_err(|_| "invalid commit value in hard state")?;
let term_bytes = term
.as_ref()
.try_into()
.map_err(|_| "invalid term value in hard state")?;
let vote_bytes = vote
.as_ref()
.try_into()
.map_err(|_| "invalid vote value in hard state")?;
raft_state.hard_state.commit = u64::from_le_bytes(commit_bytes);
raft_state.hard_state.term = u64::from_le_bytes(term_bytes);
raft_state.hard_state.vote = u64::from_le_bytes(vote_bytes);
}
Ok(Self {
raft_state,
first_log_index,
last_log_index,
db,
log_keyspace,
hard_state_keyspace,
})
}
fn entry(&self, idx: u64) -> raft::Result<Option<raft::prelude::Entry>> {
if idx > self.last_index()? {
return Ok(None);
}
let entries = self.entries(idx, idx + 1, None, GetEntriesContext::empty(false))?;
assert!(
entries.len() <= 1,
"unexpected number of entries: {entries:?}"
);
let entry = entries.into_iter().next();
Ok(entry)
}
}
impl Storage for FjallStorage {
fn initial_state(&self) -> raft::Result<RaftState> {
Ok(self.raft_state.clone())
}
fn entries(
&self,
low: u64,
high: u64,
max_size: impl Into<Option<u64>>,
_context: GetEntriesContext,
) -> raft::Result<Vec<raft::prelude::Entry>> {
assert!(low <= high, "low {low} is larger than high {high}");
if low < self.first_log_index {
return Err(raft::Error::Store(StorageError::Compacted));
}
if high > self.last_log_index + 1 {
return Err(raft::Error::Store(StorageError::Unavailable));
}
let size = std::cmp::min(high - low, max_size.into().unwrap_or(u64::MAX));
let high = low + size;
let low = raft_index_to_key(low);
let high = raft_index_to_key(high);
let entries = self
.log_keyspace
.range(low..high)
.map(|entry| {
let value = entry.value().map_err(fjall_error_to_raft_error)?;
let entry: Entry = value
.try_into()
.map_err(|e| raft::Error::Store(StorageError::Other(e)))?;
let entry: raft::prelude::Entry = entry
.try_into()
.map_err(|e: String| raft::Error::Store(StorageError::Other(e.into())))?;
Ok(entry)
})
.collect::<raft::Result<_>>()?;
Ok(entries)
}
fn term(&self, idx: u64) -> raft::Result<u64> {
if idx == 0 {
return Ok(0);
}
if idx < self.first_log_index {
return Err(raft::Error::Store(StorageError::Compacted));
}
let entry = self
.entry(idx)?
.ok_or(raft::Error::Store(StorageError::Unavailable))?;
Ok(entry.term)
}
fn first_index(&self) -> raft::Result<u64> {
Ok(self.first_log_index)
}
fn last_index(&self) -> raft::Result<u64> {
Ok(self.last_log_index)
}
fn snapshot(&self, _request_index: u64, _to: u64) -> raft::Result<Snapshot> {
unimplemented!("snapshots are not yet supported");
}
}
impl LogStorage for FjallStorage {
fn set_conf_state(&mut self, conf_state: ConfState) {
self.raft_state.conf_state = conf_state;
}
fn append(&mut self, entries: &[raft::eraftpb::Entry]) -> raft::Result<()> {
if entries.is_empty() {
return Ok(());
}
assert!(
self.first_log_index <= entries[0].index,
"overwrite compacted raft logs, compacted: {}, append: {}",
self.first_log_index - 1,
entries[0].index,
);
assert!(
self.last_log_index + 1 >= entries[0].index,
"raft logs should be continuous, last index: {}, new appended: {}",
self.last_log_index,
entries[0].index,
);
let mut batch = self.db.batch();
// Remove all entries overwritten by the new entries.
for index in entries[0].index..=self.last_log_index {
batch.remove(&self.log_keyspace, raft_index_to_key(index));
}
for entry in entries {
let entry: Entry = entry.into();
batch.insert(&self.log_keyspace, raft_index_to_key(entry.index), entry);
}
batch.commit().map_err(fjall_error_to_raft_error)?;
self.last_log_index = entries.last().expect("empty entries return early").index;
assert!(
self.raft_state.hard_state.commit <= self.last_log_index,
"commit index is invalid, commit: {}, last_log_index: {}",
self.raft_state.hard_state.commit,
self.last_log_index
);
Ok(())
}
fn set_hard_state(&mut self, hard_state: raft::prelude::HardState) -> raft::Result<()> {
self.raft_state.hard_state = hard_state;
let HardState { commit, term, vote } = (&self.raft_state.hard_state).into();
let mut batch = self.db.batch();
batch.insert(&self.hard_state_keyspace, COMMIT_KEY, commit.to_le_bytes());
batch.insert(&self.hard_state_keyspace, TERM_KEY, term.to_le_bytes());
batch.insert(&self.hard_state_keyspace, VOTE_KEY, vote.to_le_bytes());
batch.commit().map_err(fjall_error_to_raft_error)?;
Ok(())
}
fn set_commit_index(&mut self, commit_index: u64) -> raft::Result<()> {
self.raft_state.hard_state.commit = commit_index;
self.hard_state_keyspace
.insert(COMMIT_KEY, commit_index.to_le_bytes())
.map_err(fjall_error_to_raft_error)?;
Ok(())
}
}
fn raft_index_to_key(index: u64) -> [u8; size_of::<u64>()] {
// Use big endian so that the lexicographical ordering of keys is consistent with the numeric
// ordering of indices.
index.to_be_bytes()
}
impl TryFrom<UserValue> for Entry {
type Error = Box<dyn std::error::Error + Send + Sync>;
fn try_from(value: UserValue) -> std::result::Result<Self, Self::Error> {
let entry = Entry::from_bytes(value.as_ref())?;
Ok(entry)
}
}
impl From<Entry> for UserValue {
fn from(entry: Entry) -> Self {
UserValue::new(&entry.to_bytes())
}
}
fn fjall_error_to_raft_error(e: fjall::Error) -> raft::Error {
match e {
fjall::Error::Io(e) => raft::Error::Io(e),
e => raft::Error::Store(StorageError::Other(e.into())),
}
}
#[cfg(test)]
mod tests {
use super::*;
use bytes::Bytes;
use protobuf::{CachedSize, UnknownFields};
use raft::Storage;
use raft::prelude::EntryType;
use tempfile::TempDir;
#[test]
fn test_fjall_storage_orders_entries_by_numeric_index() {
let temp_dir = TempDir::new().unwrap();
let db = fjall::Database::builder(temp_dir.path()).open().unwrap();
let log_keyspace = db
.keyspace("log", fjall::KeyspaceCreateOptions::default)
.unwrap();
let hard_state_keyspace = db
.keyspace("hard_state", fjall::KeyspaceCreateOptions::default)
.unwrap();
let mut storage = FjallStorage::new(db.clone(), log_keyspace, hard_state_keyspace).unwrap();
let make_entry = |index: u64, term: u64| raft::prelude::Entry {
entry_type: EntryType::EntryNormal,
term,
index,
data: Bytes::from_static(b"payload"),
context: Bytes::new(),
sync_log: false,
unknown_fields: UnknownFields::default(),
cached_size: CachedSize::default(),
};
let entries = vec![make_entry(1, 1), make_entry(2, 1), make_entry(3, 1)];
storage.append(&entries).unwrap();
assert_eq!(storage.first_index().unwrap(), 1);
assert_eq!(storage.last_index().unwrap(), 3);
let fetched = storage
.entries(1, 4, None, GetEntriesContext::empty(false))
.unwrap();
let fetched_indexes: Vec<_> = fetched.into_iter().map(|e| e.index).collect();
assert_eq!(fetched_indexes, vec![1, 2, 3]);
drop(storage);
let log_keyspace = db
.keyspace("log", fjall::KeyspaceCreateOptions::default)
.unwrap();
let hard_state_keyspace = db
.keyspace("hard_state", fjall::KeyspaceCreateOptions::default)
.unwrap();
let storage = FjallStorage::new(db, log_keyspace, hard_state_keyspace).unwrap();
let fetched = storage
.entries(1, 4, None, GetEntriesContext::empty(false))
.unwrap();
let fetched_indexes: Vec<_> = fetched.into_iter().map(|e| e.index).collect();
assert_eq!(fetched_indexes, vec![1, 2, 3]);
}
#[test]
fn test_fjall_storage_append_truncates_log_at_overwrite() {
let temp_dir = TempDir::new().unwrap();
let db = fjall::Database::builder(temp_dir.path()).open().unwrap();
let log_keyspace = db
.keyspace("log", fjall::KeyspaceCreateOptions::default)
.unwrap();
let hard_state_keyspace = db
.keyspace("hard_state", fjall::KeyspaceCreateOptions::default)
.unwrap();
let mut storage = FjallStorage::new(db, log_keyspace, hard_state_keyspace).unwrap();
let make_entry = |index: u64, term: u64| raft::prelude::Entry {
entry_type: EntryType::EntryNormal,
term,
index,
data: Bytes::from_static(b"payload"),
context: Bytes::new(),
sync_log: false,
unknown_fields: UnknownFields::default(),
cached_size: CachedSize::default(),
};
// Append 1, 2, 3
storage
.append(&[make_entry(1, 1), make_entry(2, 1), make_entry(3, 1)])
.unwrap();
assert_eq!(storage.last_index().unwrap(), 3);
// Overwrite at index 2 with new entry at term 2
storage.append(&[make_entry(2, 2)]).unwrap();
assert_eq!(storage.last_index().unwrap(), 2);
let entries = storage
.entries(1, 3, None, GetEntriesContext::empty(false))
.unwrap();
assert_eq!(entries.len(), 2);
assert_eq!(entries[0].index, 1);
assert_eq!(entries[0].term, 1);
assert_eq!(entries[1].index, 2);
assert_eq!(entries[1].term, 2);
// Ensure 3 is gone
assert!(storage.entry(3).unwrap().is_none());
}
#[test]
fn test_fjall_storage_append_truncates_log_at_overwrite_with_offset() {
let temp_dir = TempDir::new().unwrap();
let db = fjall::Database::builder(temp_dir.path()).open().unwrap();
let log_keyspace = db
.keyspace("log", fjall::KeyspaceCreateOptions::default)
.unwrap();
let hard_state_keyspace = db
.keyspace("hard_state", fjall::KeyspaceCreateOptions::default)
.unwrap();
let mut storage = FjallStorage::new(db, log_keyspace, hard_state_keyspace).unwrap();
// Force first_log_index to 10
storage.first_log_index = 10;
storage.last_log_index = 9;
let make_entry = |index: u64, term: u64| raft::prelude::Entry {
entry_type: EntryType::EntryNormal,
term,
index,
data: Bytes::from_static(b"payload"),
context: Bytes::new(),
sync_log: false,
unknown_fields: UnknownFields::default(),
cached_size: CachedSize::default(),
};
// Pretend we have entries 10, 11, 12
// We need to set first_log_index manually or by compacting,
// but new() initializes first_log_index from the first key in log_keyspace.
storage
.append(&[make_entry(10, 1), make_entry(11, 1), make_entry(12, 1)])
.unwrap();
assert_eq!(storage.first_index().unwrap(), 10);
assert_eq!(storage.last_index().unwrap(), 12);
// Overwrite at index 11
storage.append(&[make_entry(11, 2)]).unwrap();
assert_eq!(storage.last_index().unwrap(), 11);
let entries = storage
.entries(10, 12, None, GetEntriesContext::empty(false))
.unwrap();
assert_eq!(entries.len(), 2);
assert_eq!(entries[0].index, 10);
assert_eq!(entries[1].index, 11);
assert_eq!(entries[1].term, 2);
assert!(storage.entry(12).unwrap().is_none());
}
}