datacake-cluster 0.1.0

Eventually consistent state replication as a library (consensus, RPC and conflict resolution) for building your own eventually consistent databases.
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
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
use std::collections::HashMap;
use std::error::Error;
use std::mem;
use std::ops::AddAssign;

use datacake_crdt::{HLCTimestamp, Key};
use parking_lot::{Mutex, RwLock};

use crate::storage::BulkMutationError;
use crate::{Document, PutContext, Storage};

/// A wrapping type around another `Storage` implementation that
/// logs all the activity going into and out of the store.
///
/// This is a very useful system for debugging issues with your store.
pub struct InstrumentedStorage<S: Storage>(pub S);

impl<S: Storage + Clone> Clone for InstrumentedStorage<S> {
    fn clone(&self) -> Self {
        Self(self.0.clone())
    }
}

#[async_trait::async_trait]
impl<S: Storage + Send + Sync + 'static> Storage for InstrumentedStorage<S> {
    type Error = S::Error;
    type DocsIter = S::DocsIter;
    type MetadataIter = S::MetadataIter;

    async fn get_keyspace_list(&self) -> Result<Vec<String>, Self::Error> {
        info!("get_keyspace_list");
        self.0.get_keyspace_list().await
    }

    async fn iter_metadata(
        &self,
        keyspace: &str,
    ) -> Result<Self::MetadataIter, Self::Error> {
        info!(keyspace = keyspace, "iter_metadata");
        self.0.iter_metadata(keyspace).await
    }

    async fn remove_tombstones(
        &self,
        keyspace: &str,
        keys: impl Iterator<Item = Key> + Send,
    ) -> Result<(), BulkMutationError<Self::Error>> {
        let keys = keys.collect::<Vec<_>>();
        info!(keyspace = keyspace, keys = ?keys, "remove_many_metadata");
        self.0.remove_tombstones(keyspace, keys.into_iter()).await
    }

    async fn put_with_ctx(
        &self,
        keyspace: &str,
        document: Document,
        ctx: Option<&PutContext>,
    ) -> Result<(), Self::Error> {
        info!(keyspace = keyspace, document = ?document, "put_with_ctx");
        self.0.put_with_ctx(keyspace, document, ctx).await
    }

    async fn put(&self, keyspace: &str, document: Document) -> Result<(), Self::Error> {
        info!(keyspace = keyspace, document = ?document, "put");
        self.0.put(keyspace, document).await
    }

    async fn multi_put_with_ctx(
        &self,
        keyspace: &str,
        documents: impl Iterator<Item = Document> + Send,
        ctx: Option<&PutContext>,
    ) -> Result<(), BulkMutationError<Self::Error>> {
        let documents = documents.collect::<Vec<_>>();
        info!(keyspace = keyspace, documents = ?documents, "put_with_ctx");
        self.0
            .multi_put_with_ctx(keyspace, documents.into_iter(), ctx)
            .await
    }

    async fn multi_put(
        &self,
        keyspace: &str,
        documents: impl Iterator<Item = Document> + Send,
    ) -> Result<(), BulkMutationError<Self::Error>> {
        let documents = documents.collect::<Vec<_>>();
        info!(keyspace = keyspace, documents = ?documents, "multi_put");
        self.0.multi_put(keyspace, documents.into_iter()).await
    }

    async fn mark_as_tombstone(
        &self,
        keyspace: &str,
        doc_id: Key,
        timestamp: HLCTimestamp,
    ) -> Result<(), Self::Error> {
        info!(keyspace = keyspace, doc_id = doc_id, timestamp = %timestamp, "mark_as_tombstone");
        self.0.mark_as_tombstone(keyspace, doc_id, timestamp).await
    }

    async fn mark_many_as_tombstone(
        &self,
        keyspace: &str,
        documents: impl Iterator<Item = (Key, HLCTimestamp)> + Send,
    ) -> Result<(), BulkMutationError<Self::Error>> {
        let documents = documents.collect::<Vec<_>>();
        info!(keyspace = keyspace, documents = ?documents, "mark_many_as_tombstone");
        self.0
            .mark_many_as_tombstone(keyspace, documents.into_iter())
            .await
    }

    async fn get(
        &self,
        keyspace: &str,
        doc_id: Key,
    ) -> Result<Option<Document>, Self::Error> {
        info!(keyspace = keyspace, doc_id = doc_id, "get");
        self.0.get(keyspace, doc_id).await
    }

    async fn multi_get(
        &self,
        keyspace: &str,
        doc_ids: impl Iterator<Item = Key> + Send,
    ) -> Result<Self::DocsIter, Self::Error> {
        let doc_ids = doc_ids.collect::<Vec<_>>();
        info!(keyspace = keyspace, doc_ids = ?doc_ids, "multi_get");
        self.0.multi_get(keyspace, doc_ids.into_iter()).await
    }
}

#[derive(Debug, thiserror::Error)]
#[error("{0}")]
pub struct MockError(pub String);
impl MockError {
    pub fn from(e: impl Error) -> Self {
        Self(e.to_string())
    }
}

#[derive(Default)]
struct MockCounters {
    expected_method_counts: HashMap<&'static str, usize>,
    actual_counts: Mutex<HashMap<&'static str, usize>>,
}

impl MockCounters {
    fn register_expected_counts(&mut self, name: &'static str, count: usize) {
        self.expected_method_counts.insert(name, count);
    }

    fn inc(&self, name: &'static str) {
        let mut lock = self.actual_counts.lock();
        let count = lock.entry(name).or_default();
        count.add_assign(&1);
    }
}

impl Drop for MockCounters {
    fn drop(&mut self) {
        let lock = self.actual_counts.lock();
        for (name, count) in mem::take(&mut self.expected_method_counts) {
            let actual_count = lock.get(name).copied().unwrap_or_default();

            assert_eq!(
                actual_count, count,
                "Expected method {:?} to be called {} times, but was called {} times.",
                name, count, actual_count,
            );
        }
    }
}

macro_rules! mock_storage_methods {
    ($name:ident { $($field_name:tt: params = ($($param:ty,)*) returns = $returntp:ty => $method_name:ident)* }) => {
        #[derive(Default)]
        /// A mock storage handler for testing handlers.
        pub struct $name {
            mock_counters: MockCounters,
            #[allow(clippy::type_complexity)]
            $($field_name: Option<(Box<dyn Fn($($param,)*) -> $returntp + Send + Sync>, &'static str)>,)*
        }

        impl $name {
            $(
                #[allow(clippy::type_complexity)]
                pub fn $method_name(
                    mut self,
                    count: usize,
                    cb: impl Fn($($param,)*) -> $returntp + Send + Sync + 'static
                ) -> Self{
                    self.$field_name = Some((Box::new(cb), stringify!($field_name)));
                    self.mock_counters.register_expected_counts(stringify!($field_name), count);
                    self
                }
            )*
        }
    };
}

mock_storage_methods!(MockStorage {
    get_keyspace_list:
        params = ()
        returns = Result<Vec<String>, MockError>
        => expect_get_keyspace_list
    iter_metadata:
        params = (&str,)
        returns = Result<std::vec::IntoIter<(Key, HLCTimestamp, bool)>, MockError>
        => expect_iter_metadata
    remove_tombstones:
        params = (&str, Box<dyn Iterator<Item = Key> + Send>,)
        returns = Result<(), BulkMutationError<MockError>>
        => expect_remove_tombstones
    put_with_ctx:
        params = (&str, Document, Option<&PutContext>,)
        returns = Result<(), MockError>
        => expect_put_with_ctx
    put:
        params = (&str, Document,)
        returns = Result<(), MockError>
        => expect_put
    multi_put_with_ctx:
        params = (&str, Box<dyn Iterator<Item = Document> + Send>, Option<&PutContext>,)
        returns = Result<(), BulkMutationError<MockError>>
        => expect_multi_put_with_ctx
    multi_put:
        params = (&str, Box<dyn Iterator<Item = Document> + Send>,)
        returns = Result<(), BulkMutationError<MockError>>
        => expect_multi_put
    mark_as_tombstone:
        params = (&str, Key, HLCTimestamp,)
        returns = Result<(), MockError>
        => expect_mark_as_tombstone
    mark_many_as_tombstone:
        params = (&str, Box<dyn Iterator<Item = (Key, HLCTimestamp)> + Send>,)
        returns = Result<(), BulkMutationError<MockError>>
        => expect_mark_many_as_tombstone
    get:
        params = (&str, Key,)
        returns = Result<Option<Document>, MockError>
        => expect_get
    multi_get:
        params = (&str, Box<dyn Iterator<Item = Key> + Send>,)
        returns = Result<std::vec::IntoIter<Document>, MockError>
        => expect_multi_get
});

#[async_trait::async_trait]
impl Storage for MockStorage {
    type Error = MockError;
    type DocsIter = std::vec::IntoIter<Document>;
    type MetadataIter = std::vec::IntoIter<(Key, HLCTimestamp, bool)>;

    async fn get_keyspace_list(&self) -> Result<Vec<String>, Self::Error> {
        if let Some((expected, name)) = self.get_keyspace_list.as_ref() {
            self.mock_counters.inc(name);
            return (*expected)();
        }
        panic!("Storage operation was not expected to be called.");
    }

    async fn iter_metadata(
        &self,
        keyspace: &str,
    ) -> Result<Self::MetadataIter, Self::Error> {
        if let Some((expected, name)) = self.iter_metadata.as_ref() {
            self.mock_counters.inc(name);
            return (*expected)(keyspace);
        }
        panic!("iter_metadata operation was not expected to be called.");
    }

    async fn remove_tombstones(
        &self,
        keyspace: &str,
        keys: impl Iterator<Item = Key> + Send,
    ) -> Result<(), BulkMutationError<Self::Error>> {
        if let Some((expected, name)) = self.remove_tombstones.as_ref() {
            self.mock_counters.inc(name);
            #[allow(clippy::needless_collect)]
            let items = keys.collect::<Vec<_>>();
            return (*expected)(keyspace, Box::new(items.into_iter()));
        }
        panic!("remove_tombstones operation was not expected to be called.");
    }

    async fn put_with_ctx(
        &self,
        keyspace: &str,
        document: Document,
        ctx: Option<&PutContext>,
    ) -> Result<(), Self::Error> {
        if let Some((expected, name)) = self.put_with_ctx.as_ref() {
            self.mock_counters.inc(name);
            return (*expected)(keyspace, document, ctx);
        }
        panic!("put_with_ctx operation was not expected to be called.");
    }

    async fn put(&self, keyspace: &str, document: Document) -> Result<(), Self::Error> {
        if let Some((expected, name)) = self.put.as_ref() {
            self.mock_counters.inc(name);
            return (*expected)(keyspace, document);
        }
        panic!("put operation was not expected to be called.");
    }

    async fn multi_put_with_ctx(
        &self,
        keyspace: &str,
        documents: impl Iterator<Item = Document> + Send,
        ctx: Option<&PutContext>,
    ) -> Result<(), BulkMutationError<Self::Error>> {
        if let Some((expected, name)) = self.multi_put_with_ctx.as_ref() {
            self.mock_counters.inc(name);
            #[allow(clippy::needless_collect)]
            let items = documents.collect::<Vec<_>>();
            return (*expected)(keyspace, Box::new(items.into_iter()), ctx);
        }
        panic!("multi_put_with_ctx operation was not expected to be called.");
    }

    async fn multi_put(
        &self,
        keyspace: &str,
        documents: impl Iterator<Item = Document> + Send,
    ) -> Result<(), BulkMutationError<Self::Error>> {
        if let Some((expected, name)) = self.multi_put.as_ref() {
            self.mock_counters.inc(name);
            #[allow(clippy::needless_collect)]
            let items = documents.collect::<Vec<_>>();
            return (*expected)(keyspace, Box::new(items.into_iter()));
        }
        panic!("multi_put operation was not expected to be called.");
    }

    async fn mark_as_tombstone(
        &self,
        keyspace: &str,
        doc_id: Key,
        timestamp: HLCTimestamp,
    ) -> Result<(), Self::Error> {
        if let Some((expected, name)) = self.mark_as_tombstone.as_ref() {
            self.mock_counters.inc(name);
            return (*expected)(keyspace, doc_id, timestamp);
        }
        panic!("mark_as_tombstone operation was not expected to be called.");
    }

    async fn mark_many_as_tombstone(
        &self,
        keyspace: &str,
        documents: impl Iterator<Item = (Key, HLCTimestamp)> + Send,
    ) -> Result<(), BulkMutationError<Self::Error>> {
        if let Some((expected, name)) = self.mark_many_as_tombstone.as_ref() {
            self.mock_counters.inc(name);
            #[allow(clippy::needless_collect)]
            let items = documents.collect::<Vec<_>>();
            return (*expected)(keyspace, Box::new(items.into_iter()));
        }
        panic!("mark_many_as_tombstone operation was not expected to be called.");
    }

    async fn get(
        &self,
        keyspace: &str,
        doc_id: Key,
    ) -> Result<Option<Document>, Self::Error> {
        if let Some((expected, name)) = self.get.as_ref() {
            self.mock_counters.inc(name);
            return (*expected)(keyspace, doc_id);
        }
        panic!("get operation was not expected to be called.");
    }

    async fn multi_get(
        &self,
        keyspace: &str,
        doc_ids: impl Iterator<Item = Key> + Send,
    ) -> Result<Self::DocsIter, Self::Error> {
        if let Some((expected, name)) = self.multi_get.as_ref() {
            self.mock_counters.inc(name);
            #[allow(clippy::needless_collect)]
            let items = doc_ids.collect::<Vec<_>>();
            return (*expected)(keyspace, Box::new(items.into_iter()));
        }
        panic!("multi_get operation was not expected to be called.");
    }
}

#[derive(Debug, Default)]
/// A in-memory storage implementor.
///
/// This is not suitable for any sort of real world usage outside of testing.
pub struct MemStore {
    #[allow(clippy::complexity)]
    metadata: RwLock<HashMap<String, HashMap<Key, (HLCTimestamp, bool)>>>,
    data: RwLock<HashMap<String, HashMap<Key, Document>>>,
}

#[derive(Debug, thiserror::Error)]
#[error("{0}")]
pub struct MemStoreError(#[from] pub anyhow::Error);

#[async_trait::async_trait]
impl Storage for MemStore {
    type Error = MemStoreError;
    type DocsIter = std::vec::IntoIter<Document>;
    type MetadataIter = std::vec::IntoIter<(Key, HLCTimestamp, bool)>;

    async fn get_keyspace_list(&self) -> Result<Vec<String>, Self::Error> {
        Ok(self.metadata.read().keys().cloned().collect())
    }

    async fn iter_metadata(
        &self,
        keyspace: &str,
    ) -> Result<Self::MetadataIter, Self::Error> {
        if let Some(ks) = self.metadata.read().get(keyspace) {
            return Ok(ks
                .iter()
                .map(|(k, (ts, tombstone))| (*k, *ts, *tombstone))
                .collect::<Vec<_>>()
                .into_iter());
        };

        Ok(Vec::new().into_iter())
    }

    async fn remove_tombstones(
        &self,
        keyspace: &str,
        keys: impl Iterator<Item = Key> + Send,
    ) -> Result<(), BulkMutationError<Self::Error>> {
        if let Some(ks) = self.metadata.write().get_mut(keyspace) {
            for key in keys {
                ks.remove(&key);
            }
        }

        Ok(())
    }

    async fn put(&self, keyspace: &str, document: Document) -> Result<(), Self::Error> {
        self.multi_put(keyspace, [document].into_iter())
            .await
            .map_err(|e| e.inner)
    }

    async fn multi_put(
        &self,
        keyspace: &str,
        documents: impl Iterator<Item = Document> + Send,
    ) -> Result<(), BulkMutationError<Self::Error>> {
        let documents = documents.collect::<Vec<_>>();
        self.data
            .write()
            .entry(keyspace.to_string())
            .and_modify(|entries| {
                for doc in documents.clone() {
                    entries.insert(doc.id, doc);
                }
            })
            .or_insert_with(|| {
                HashMap::from_iter(
                    documents.clone().into_iter().map(|doc| (doc.id, doc)),
                )
            });
        self.metadata
            .write()
            .entry(keyspace.to_string())
            .and_modify(|entries| {
                for doc in documents.clone() {
                    entries.insert(doc.id, (doc.last_updated, false));
                }
            })
            .or_insert_with(|| {
                HashMap::from_iter(
                    documents
                        .into_iter()
                        .map(|doc| (doc.id, (doc.last_updated, false))),
                )
            });

        Ok(())
    }

    async fn mark_as_tombstone(
        &self,
        keyspace: &str,
        doc_id: Key,
        timestamp: HLCTimestamp,
    ) -> Result<(), Self::Error> {
        self.mark_many_as_tombstone(keyspace, [(doc_id, timestamp)].into_iter())
            .await
            .map_err(|e| e.inner)
    }

    async fn mark_many_as_tombstone(
        &self,
        keyspace: &str,
        documents: impl Iterator<Item = (Key, HLCTimestamp)> + Send,
    ) -> Result<(), BulkMutationError<Self::Error>> {
        let docs = documents.collect::<Vec<_>>();
        self.data
            .write()
            .entry(keyspace.to_string())
            .and_modify(|entries| {
                for (doc_id, _) in docs.iter() {
                    entries.remove(doc_id);
                }
            });
        self.metadata
            .write()
            .entry(keyspace.to_string())
            .and_modify(|entries| {
                for (doc_id, ts) in docs {
                    entries.insert(doc_id, (ts, true));
                }
            });

        Ok(())
    }

    async fn get(
        &self,
        keyspace: &str,
        doc_id: Key,
    ) -> Result<Option<Document>, Self::Error> {
        Ok(self
            .data
            .read()
            .get(keyspace)
            .and_then(|ks| ks.get(&doc_id).cloned()))
    }

    async fn multi_get(
        &self,
        keyspace: &str,
        doc_ids: impl Iterator<Item = Key> + Send,
    ) -> Result<Self::DocsIter, Self::Error> {
        let mut docs = Vec::new();

        if let Some(ks) = self.data.read().get(keyspace) {
            for doc_id in doc_ids {
                if let Some(doc) = ks.get(&doc_id) {
                    docs.push(doc.clone());
                }
            }
        }

        Ok(docs.into_iter())
    }
}