omnipaxos_storage 0.2.3

Storage implementations for OmniPaxos.
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
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
use omnipaxos::{
    ballot_leader_election::Ballot,
    storage::{Entry, StopSign, Storage, StorageOp, StorageResult},
};
use rocksdb::{ColumnFamilyDescriptor, ColumnFamilyRef, Options, WriteBatchWithTransaction, DB};
use serde::{Deserialize, Serialize};
use std::marker::PhantomData;
use zerocopy::{AsBytes, FromBytes};

const DEFAULT: &str = "/default_storage/";
const LOG: &str = "log";
const NPROM: &[u8] = b"NPROM";
const ACC: &[u8] = b"ACC";
const DECIDE: &[u8] = b"DECIDE";
const TRIM: &[u8] = b"TRIM";
const STOPSIGN: &[u8] = b"STOPSIGN";
const SNAPSHOT: &[u8] = b"SNAPSHOT";

// Configuration for `PersistentStorage`.
/// # Fields
/// * `path`: Path to the storage directory
/// * `rocksdb_options`: Options for the RocksDB state store
/// * `log_options` : Options for the the RocksDB log store
pub struct PersistentStorageConfig {
    path: String,
    rocksdb_options: Options,
    log_options: Options,
}

impl PersistentStorageConfig {
    /// Returns the current path to the persistent storage.
    pub fn get_path(&self) -> &String {
        &self.path
    }

    /// Sets the path to the persistent storage.
    pub fn set_path(&mut self, path: String) {
        self.path = path;
    }

    /// Returns the RocksDB options for the log.
    pub fn get_log_options(&self) -> Options {
        self.log_options.clone()
    }

    /// Sets the RocksDB options for the log.
    pub fn set_log_options(&mut self, log_opts: Options) {
        self.log_options = log_opts;
    }

    /// Returns the RocksDB options for the state store.
    pub fn get_database_options(&self) -> Options {
        self.rocksdb_options.clone()
    }

    /// Sets the RocksDB options for the state store.
    pub fn set_database_options(&mut self, opts: Options) {
        self.rocksdb_options = opts;
    }

    /// Creates a configuration for `PersistentStorage` with the given path and options for Commitlog and sled
    pub fn with(path: String, log_options: Options, rocksdb_options: Options) -> Self {
        Self {
            path,
            log_options,
            rocksdb_options,
        }
    }

    /// Creates a configuration for `PersistentStorage` with the given path and default configs
    pub fn with_path(path: String) -> Self {
        let mut rocksdb_options = Options::default();
        rocksdb_options.create_missing_column_families(true);
        rocksdb_options.create_if_missing(true);
        Self {
            path,
            log_options: Options::default(),
            rocksdb_options,
        }
    }
}

impl Default for PersistentStorageConfig {
    fn default() -> Self {
        let mut rocksdb_options = Options::default();
        rocksdb_options.create_missing_column_families(true);
        rocksdb_options.create_if_missing(true);
        Self {
            path: DEFAULT.to_string(),
            log_options: Options::default(),
            rocksdb_options,
        }
    }
}

/// A persistent storage implementation, lets sequence paxos write the log
/// and current state to disk. Log entries are serialized and de-serialized
/// into slice of bytes when read or written from the log.
pub struct PersistentStorage<T>
where
    T: Entry,
{
    /// Local RocksDB key-value store
    db: DB,
    /// Buffered, atomic write batch
    write_batch: WriteBatchWithTransaction<false>,
    /// The index of the next log entry to be appended. Will be used as the key of the entry in big
    /// endian format.
    next_log_key: usize,
    /// A placeholder for the T: Entry
    t: PhantomData<T>,
}

impl<T: Entry> PersistentStorage<T>
where
    T: Entry + Serialize + for<'a> Deserialize<'a>,
    T::Snapshot: Serialize + for<'a> Deserialize<'a>,
{
    /// Creates or opens an existing storage
    pub fn open(storage_config: PersistentStorageConfig) -> Self {
        // Create database with log column
        let path = storage_config.path;
        let log_cf = ColumnFamilyDescriptor::new(LOG, storage_config.log_options);
        let db_opts = storage_config.rocksdb_options;
        let db = rocksdb::DB::open_cf_descriptors(&db_opts, path, vec![log_cf])
            .expect("Failed to create RocksDB");
        let log_handle = db
            .cf_handle(LOG)
            .expect("Failed to create RocksDB log column family");

        // Create next log key from the state of the database
        let mut log_iter = db.raw_iterator_cf(log_handle);
        log_iter.seek_to_last();
        let next_log_key = if log_iter.valid() {
            // There's a max key in the database. Next key is 1 greater.
            let key = log_iter.key().unwrap();
            assert_eq!(
                key.len(),
                8,
                "Couldn't recover storage: Log key has unexpected format."
            );
            usize::from_be_bytes([
                key[0], key[1], key[2], key[3], key[4], key[5], key[6], key[7],
            ]) + 1
        } else {
            // No max key in the database. Either there's no entry yet added or they have been
            // trimmed away.
            match db
                .get(TRIM)
                .expect("Couldn't recover storage: Reading compacted_idx failed.")
            {
                Some(bytes) => usize::read_from(bytes.as_bytes())
                    .expect("Couldn't recover storage: Commpacted index has unexpected format."),
                None => 0,
            }
        };
        drop(log_iter);

        Self {
            db,
            write_batch: WriteBatchWithTransaction::<false>::default(),
            next_log_key,
            t: PhantomData,
        }
    }

    /// Creates a new storage instance, panics if a RocksDB instance already exists in the given path
    pub fn new(storage_config: PersistentStorageConfig) -> Self {
        std::fs::metadata(storage_config.path.clone()).expect_err(&format!(
            "Cannot create new instance, database already exists in {}",
            storage_config.path
        ));
        Self::open(storage_config)
    }

    /// Get handle to the log column family of the database
    fn get_log_handle(&self) -> ColumnFamilyRef<'_> {
        self.db
            .cf_handle(LOG)
            .expect("Couldn't find RocksDB log column family")
    }

    fn batch_append_entry(&mut self, entry: T) -> StorageResult<()> {
        self.write_batch.put_cf(
            self.db.cf_handle(LOG).unwrap(),
            self.next_log_key.to_be_bytes(),
            bincode::serialize(&entry)?,
        );
        self.next_log_key += 1;
        Ok(())
    }

    fn batch_append_entries(&mut self, entries: Vec<T>) -> StorageResult<()> {
        for entry in entries {
            self.write_batch.put_cf(
                self.db.cf_handle(LOG).unwrap(),
                self.next_log_key.to_be_bytes(),
                bincode::serialize(&entry)?,
            );
            self.next_log_key += 1;
        }
        Ok(())
    }

    fn batch_append_on_prefix(&mut self, from_idx: usize, entries: Vec<T>) -> StorageResult<()> {
        // Don't need to delete entries that will be overwritten.
        let delete_idx = from_idx + entries.len();
        if delete_idx < self.next_log_key {
            let from_key = delete_idx.to_be_bytes();
            let to_key = self.next_log_key.to_be_bytes();
            let log = self.db.cf_handle(LOG).unwrap();
            self.write_batch.delete_range_cf(log, from_key, to_key);
        }
        self.next_log_key = from_idx;
        self.batch_append_entries(entries)
    }

    fn batch_set_promise(&mut self, n_prom: Ballot) -> StorageResult<()> {
        let prom_bytes = bincode::serialize(&n_prom)?;
        self.write_batch.put(NPROM, prom_bytes);
        Ok(())
    }

    fn batch_set_decided_idx(&mut self, ld: usize) -> StorageResult<()> {
        let ld_bytes = usize::as_bytes(&ld);
        self.write_batch.put(DECIDE, ld_bytes);
        Ok(())
    }

    fn batch_set_accepted_round(&mut self, na: Ballot) -> StorageResult<()> {
        let acc_bytes = bincode::serialize(&na)?;
        self.write_batch.put(ACC, acc_bytes);
        Ok(())
    }

    fn batch_set_compacted_idx(&mut self, trimmed_idx: usize) -> StorageResult<()> {
        let trim_bytes = usize::as_bytes(&trimmed_idx);
        self.write_batch.put(TRIM, trim_bytes);
        Ok(())
    }

    fn batch_trim(&mut self, trimmed_idx: usize) -> StorageResult<()> {
        let from_key = 0_usize.to_be_bytes();
        let to_key = trimmed_idx.to_be_bytes();
        let log = self.db.cf_handle(LOG).unwrap();
        self.write_batch.delete_range_cf(log, from_key, to_key);
        Ok(())
    }

    fn batch_set_stopsign(&mut self, ss: Option<StopSign>) -> StorageResult<()> {
        let stopsign = bincode::serialize(&ss)?;
        self.write_batch.put(STOPSIGN, stopsign);
        Ok(())
    }

    fn batch_set_snapshot(&mut self, snapshot: Option<T::Snapshot>) -> StorageResult<()> {
        let s = bincode::serialize(&snapshot)?;
        self.write_batch.put(SNAPSHOT, s);
        Ok(())
    }
}

/// An error returning the proposal that was failed due to that the current configuration is stopped.
#[derive(Copy, Clone, Debug)]
pub struct ErrHelper {}
impl std::error::Error for ErrHelper {}
impl std::fmt::Display for ErrHelper {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        std::fmt::Debug::fmt(self, f)
    }
}

impl<T> Storage<T> for PersistentStorage<T>
where
    T: Entry + Serialize + for<'a> Deserialize<'a>,
    T::Snapshot: Serialize + for<'a> Deserialize<'a>,
{
    fn write_atomically(&mut self, ops: Vec<StorageOp<T>>) -> StorageResult<()> {
        for op in ops {
            match op {
                StorageOp::AppendEntry(entry) => self.batch_append_entry(entry)?,
                StorageOp::AppendEntries(entries) => self.batch_append_entries(entries)?,
                StorageOp::AppendOnPrefix(from_idx, entries) => {
                    self.batch_append_on_prefix(from_idx, entries)?
                }
                StorageOp::SetPromise(bal) => self.batch_set_promise(bal)?,
                StorageOp::SetDecidedIndex(idx) => self.batch_set_decided_idx(idx)?,
                StorageOp::SetAcceptedRound(bal) => self.batch_set_accepted_round(bal)?,
                StorageOp::SetCompactedIdx(idx) => self.batch_set_compacted_idx(idx)?,
                StorageOp::Trim(idx) => self.batch_trim(idx)?,
                StorageOp::SetStopsign(ss) => self.batch_set_stopsign(ss)?,
                StorageOp::SetSnapshot(snap) => self.batch_set_snapshot(snap)?,
            }
        }
        Ok(self.db.write(std::mem::take(&mut self.write_batch))?)
    }

    fn append_entry(&mut self, entry: T) -> StorageResult<()> {
        let entry_bytes = bincode::serialize(&entry)?;
        self.db.put_cf(
            self.get_log_handle(),
            self.next_log_key.to_be_bytes(),
            entry_bytes,
        )?;
        self.next_log_key += 1;
        Ok(())
    }

    fn append_entries(&mut self, entries: Vec<T>) -> StorageResult<()> {
        let mut batch = WriteBatchWithTransaction::<false>::default();
        for entry in entries {
            batch.put_cf(
                self.get_log_handle(),
                self.next_log_key.to_be_bytes(),
                bincode::serialize(&entry)?,
            );
            self.next_log_key += 1;
        }
        self.db.write(batch)?;
        Ok(())
    }

    fn append_on_prefix(&mut self, from_idx: usize, entries: Vec<T>) -> StorageResult<()> {
        // Don't need to delete entries that will be overwritten.
        let delete_idx = from_idx + entries.len();
        if delete_idx < self.next_log_key {
            let from_key = delete_idx.to_be_bytes();
            let to_key = self.next_log_key.to_be_bytes();
            self.db
                .delete_range_cf(self.get_log_handle(), from_key, to_key)?;
        }
        self.next_log_key = from_idx;
        self.append_entries(entries)
    }

    fn get_entries(&self, from: usize, to: usize) -> StorageResult<Vec<T>> {
        // Check if the log has entries up to the requested endpoint.
        if to > self.next_log_key || from >= to {
            return Ok(vec![]); // Do an early return
        }

        let mut iter = self.db.raw_iterator_cf(self.get_log_handle());
        let mut entries = Vec::with_capacity(to - from);
        iter.seek(from.to_be_bytes());
        for _ in from..to {
            let entry_bytes = iter.value().ok_or(ErrHelper {})?;
            entries.push(bincode::deserialize(entry_bytes)?);
            iter.next();
        }
        Ok(entries)
    }

    fn get_log_len(&self) -> StorageResult<usize> {
        Ok(self.next_log_key - self.get_compacted_idx()?)
    }

    fn get_suffix(&self, from: usize) -> StorageResult<Vec<T>> {
        self.get_entries(from, self.next_log_key)
    }

    fn get_promise(&self) -> StorageResult<Option<Ballot>> {
        let promise = self.db.get_pinned(NPROM)?;
        match promise {
            Some(pinned_bytes) => Ok(Some(bincode::deserialize(&pinned_bytes)?)),
            None => Ok(None),
        }
    }

    fn set_promise(&mut self, n_prom: Ballot) -> StorageResult<()> {
        let prom_bytes = bincode::serialize(&n_prom)?;
        self.db.put(NPROM, prom_bytes)?;
        Ok(())
    }

    fn get_decided_idx(&self) -> StorageResult<usize> {
        let decided = self.db.get_pinned(DECIDE)?;
        match decided {
            Some(ld_bytes) => Ok(usize::read_from(ld_bytes.as_bytes()).ok_or(ErrHelper {})?),
            None => Ok(0),
        }
    }

    fn set_decided_idx(&mut self, ld: usize) -> StorageResult<()> {
        let ld_bytes = usize::as_bytes(&ld);
        self.db.put(DECIDE, ld_bytes)?;
        Ok(())
    }

    fn get_accepted_round(&self) -> StorageResult<Option<Ballot>> {
        let accepted = self.db.get_pinned(ACC)?;
        match accepted {
            Some(acc_bytes) => {
                let ballot = bincode::deserialize(&acc_bytes)?;
                Ok(Some(ballot))
            }
            None => Ok(None),
        }
    }

    fn set_accepted_round(&mut self, na: Ballot) -> StorageResult<()> {
        let acc_bytes = bincode::serialize(&na)?;
        self.db.put(ACC, acc_bytes)?;
        Ok(())
    }

    fn get_compacted_idx(&self) -> StorageResult<usize> {
        let trim = self.db.get(TRIM)?;
        match trim {
            Some(trim_bytes) => Ok(usize::read_from(trim_bytes.as_bytes()).ok_or(ErrHelper {})?),
            None => Ok(0),
        }
    }

    fn set_compacted_idx(&mut self, trimmed_idx: usize) -> StorageResult<()> {
        let trim_bytes = usize::as_bytes(&trimmed_idx);
        self.db.put(TRIM, trim_bytes)?;
        Ok(())
    }

    fn get_stopsign(&self) -> StorageResult<Option<StopSign>> {
        let stopsign = self.db.get_pinned(STOPSIGN)?;
        match stopsign {
            Some(ss_bytes) => Ok(bincode::deserialize(&ss_bytes)?),
            None => Ok(None),
        }
    }

    fn set_stopsign(&mut self, s: Option<StopSign>) -> StorageResult<()> {
        let stopsign = bincode::serialize(&s)?;
        self.db.put(STOPSIGN, stopsign)?;
        Ok(())
    }

    fn get_snapshot(&self) -> StorageResult<Option<T::Snapshot>> {
        let snapshot = self.db.get_pinned(SNAPSHOT)?;
        if let Some(snapshot_bytes) = snapshot {
            Ok(bincode::deserialize(snapshot_bytes.as_bytes())?)
        } else {
            Ok(None)
        }
    }

    fn set_snapshot(&mut self, snapshot: Option<T::Snapshot>) -> StorageResult<()> {
        let s = bincode::serialize(&snapshot)?;
        self.db.put(SNAPSHOT, s)?;
        Ok(())
    }

    fn trim(&mut self, trimmed_idx: usize) -> StorageResult<()> {
        let from_key = 0_usize.to_be_bytes();
        let to_key = trimmed_idx.to_be_bytes();
        self.db
            .delete_range_cf(self.get_log_handle(), from_key, to_key)?;
        Ok(())
    }
}