-
Notifications
You must be signed in to change notification settings - Fork 157
Expand file tree
/
Copy pathdata_store.rs
More file actions
604 lines (527 loc) · 19.4 KB
/
Copy pathdata_store.rs
File metadata and controls
604 lines (527 loc) · 19.4 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
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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
// This file is Copyright its original authors, visible in version control history.
//
// This file is licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
// http://opensource.org/licenses/MIT>, at your option. You may not use this file except in
// accordance with one or both of these licenses.
use std::collections::HashMap;
use std::ops::Deref;
use std::sync::{Arc, Mutex};
use lightning::util::persist::KVStore;
use lightning::util::ser::{Readable, Writeable};
use crate::logger::{log_error, LdkLogger};
use crate::types::DynStore;
use crate::Error;
pub(crate) trait StorableObject: Clone + Readable + Writeable {
type Id: StorableObjectId;
type Update: StorableObjectUpdate<Self>;
fn id(&self) -> Self::Id;
fn update(&mut self, update: Self::Update) -> bool;
fn to_update(&self) -> Self::Update;
}
pub(crate) trait StorableObjectId: std::hash::Hash + PartialEq + Eq {
fn encode_to_hex_str(&self) -> String;
}
pub(crate) trait StorableObjectUpdate<SO: StorableObject> {
fn id(&self) -> SO::Id;
}
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub(crate) enum DataStoreUpdateResult {
Updated,
Unchanged,
NotFound,
}
#[derive(PartialEq, Eq, Debug, Clone, Copy)]
pub(crate) enum DataStoreUpdateOrInsertResult {
Inserted,
Updated,
Unchanged,
}
pub(crate) struct DataStore<SO: StorableObject, L: Deref>
where
L::Target: LdkLogger,
{
objects: Mutex<HashMap<SO::Id, SO>>,
mutation_lock: tokio::sync::Mutex<()>,
primary_namespace: String,
secondary_namespace: String,
kv_store: Arc<DynStore>,
logger: L,
}
impl<SO: StorableObject, L: Deref> DataStore<SO, L>
where
L::Target: LdkLogger,
{
pub(crate) fn new(
objects: Vec<SO>, primary_namespace: String, secondary_namespace: String,
kv_store: Arc<DynStore>, logger: L,
) -> Self {
let objects =
Mutex::new(HashMap::from_iter(objects.into_iter().map(|obj| (obj.id(), obj))));
Self {
objects,
mutation_lock: tokio::sync::Mutex::new(()),
primary_namespace,
secondary_namespace,
kv_store,
logger,
}
}
pub(crate) async fn insert(&self, object: SO) -> Result<bool, Error> {
let _guard = self.mutation_lock.lock().await;
self.persist(&object).await?;
let mut locked_objects = self.objects.lock().expect("lock");
let updated = locked_objects.insert(object.id(), object).is_some();
Ok(updated)
}
/// Like [`Self::insert`], but when an entry with the object's id already exists, merges the
/// object's full update ([`StorableObject::to_update`]) into it instead of replacing it.
///
/// Unlike [`Self::update_or_insert`], the caller does not choose what is merged into an
/// existing entry: the full update is always applied.
pub(crate) async fn insert_or_update(&self, object: SO) -> Result<bool, Error> {
let _guard = self.mutation_lock.lock().await;
let id = object.id();
let data_to_persist = {
let locked_objects = self.objects.lock().expect("lock");
if let Some(existing_object) = locked_objects.get(&id) {
let mut updated_object = existing_object.clone();
let updated = updated_object.update(object.to_update());
if updated {
Some(updated_object)
} else {
None
}
} else {
Some(object)
}
};
match data_to_persist {
Some(updated_object) => {
self.persist(&updated_object).await?;
let mut locked_objects = self.objects.lock().expect("lock");
locked_objects.insert(id, updated_object);
Ok(true)
},
None => Ok(false),
}
}
pub(crate) async fn remove(&self, id: &SO::Id) -> Result<(), Error> {
let _guard = self.mutation_lock.lock().await;
let should_remove = { self.objects.lock().expect("lock").contains_key(id) };
if should_remove {
let store_key = id.encode_to_hex_str();
KVStore::remove(
&*self.kv_store,
&self.primary_namespace,
&self.secondary_namespace,
&store_key,
false,
)
.await
.map_err(|e| {
log_error!(
self.logger,
"Removing object data for key {}/{}/{} failed due to: {}",
&self.primary_namespace,
&self.secondary_namespace,
store_key,
e
);
Error::PersistenceFailed
})?;
self.objects.lock().expect("lock").remove(id);
}
Ok(())
}
/// Returns the current in-memory object for `id`.
///
/// The async mutation lock serializes writers, but this synchronous reader cannot wait on it.
/// Until store reads are async, callers may temporarily see in-memory state that has not yet
/// caught up to a write in progress.
pub(crate) fn get(&self, id: &SO::Id) -> Option<SO> {
self.objects.lock().expect("lock").get(id).cloned()
}
pub(crate) async fn update(&self, update: SO::Update) -> Result<DataStoreUpdateResult, Error> {
let _guard = self.mutation_lock.lock().await;
let id = update.id();
let updated_object = {
let locked_objects = self.objects.lock().expect("lock");
let Some(object) = locked_objects.get(&id) else {
return Ok(DataStoreUpdateResult::NotFound);
};
let mut updated_object = object.clone();
if !updated_object.update(update) {
return Ok(DataStoreUpdateResult::Unchanged);
}
updated_object
};
self.persist(&updated_object).await?;
let mut locked_objects = self.objects.lock().expect("lock");
locked_objects.insert(id, updated_object);
Ok(DataStoreUpdateResult::Updated)
}
/// Applies `update` when an object with its id already exists, or inserts `object` when none
/// does.
///
/// Like [`Self::update`], but falls back to inserting `object` instead of returning
/// [`DataStoreUpdateResult::NotFound`]. Unlike [`Self::insert_or_update`], the caller chooses
/// exactly what is merged into an existing entry: `update` may carry less than the full
/// object.
///
/// The existence check and the write share one critical section of the mutation lock, so a
/// concurrent writer cannot land in between and later have its state clobbered by the insert
/// fallback — the check-then-act race that separate [`Self::contains_key`] +
/// [`Self::insert_or_update`] calls reintroduce.
pub(crate) async fn update_or_insert(
&self, update: SO::Update, object: SO,
) -> Result<DataStoreUpdateOrInsertResult, Error> {
debug_assert!(update.id() == object.id(), "update and object must share an id");
let _guard = self.mutation_lock.lock().await;
let id = update.id();
let (data_to_persist, result) = {
let locked_objects = self.objects.lock().expect("lock");
match locked_objects.get(&id) {
Some(existing_object) => {
let mut updated_object = existing_object.clone();
if updated_object.update(update) {
(Some(updated_object), DataStoreUpdateOrInsertResult::Updated)
} else {
(None, DataStoreUpdateOrInsertResult::Unchanged)
}
},
None => (Some(object), DataStoreUpdateOrInsertResult::Inserted),
}
};
if let Some(object) = data_to_persist {
self.persist(&object).await?;
self.objects.lock().expect("lock").insert(id, object);
}
Ok(result)
}
/// Returns in-memory objects matching `f`.
///
/// The async mutation lock serializes writers, but this synchronous reader cannot wait on it.
/// Until store reads are async, callers may temporarily see in-memory state that has not yet
/// caught up to a write in progress.
pub(crate) fn list_filter<F: FnMut(&&SO) -> bool>(&self, f: F) -> Vec<SO> {
self.objects.lock().expect("lock").values().filter(f).cloned().collect::<Vec<SO>>()
}
async fn persist(&self, object: &SO) -> Result<(), Error> {
let (store_key, data) = Self::encode_object(object);
self.persist_encoded(store_key, data).await
}
fn encode_object(object: &SO) -> (String, Vec<u8>) {
(object.id().encode_to_hex_str(), object.encode())
}
async fn persist_encoded(&self, store_key: String, data: Vec<u8>) -> Result<(), Error> {
KVStore::write(
&*self.kv_store,
&self.primary_namespace,
&self.secondary_namespace,
&store_key,
data,
)
.await
.map_err(|e| {
log_error!(
self.logger,
"Write for key {}/{}/{} failed due to: {}",
&self.primary_namespace,
&self.secondary_namespace,
store_key,
e
);
Error::PersistenceFailed
})?;
Ok(())
}
/// Returns whether the in-memory store contains `id`.
///
/// The async mutation lock serializes writers, but this synchronous reader cannot wait on it.
/// Until store reads are async, callers may temporarily see in-memory state that has not yet
/// caught up to a write in progress.
pub(crate) fn contains_key(&self, id: &SO::Id) -> bool {
self.objects.lock().expect("lock").contains_key(id)
}
}
#[cfg(test)]
mod tests {
use lightning::util::persist::{PageToken, PaginatedKVStore, PaginatedListResponse};
use lightning::util::test_utils::TestLogger;
use lightning::{impl_writeable_tlv_based, io};
use super::*;
use crate::hex_utils;
use crate::io::test_utils::InMemoryStore;
use crate::types::DynStoreWrapper;
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
struct TestObjectId {
id: [u8; 4],
}
impl StorableObjectId for TestObjectId {
fn encode_to_hex_str(&self) -> String {
hex_utils::to_string(&self.id)
}
}
impl_writeable_tlv_based!(TestObjectId, { (0, id, required) });
struct TestObjectUpdate {
id: TestObjectId,
data: [u8; 3],
}
impl StorableObjectUpdate<TestObject> for TestObjectUpdate {
fn id(&self) -> TestObjectId {
self.id
}
}
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
struct TestObject {
id: TestObjectId,
data: [u8; 3],
}
impl StorableObject for TestObject {
type Id = TestObjectId;
type Update = TestObjectUpdate;
fn id(&self) -> Self::Id {
self.id
}
fn update(&mut self, update: Self::Update) -> bool {
if self.data != update.data {
self.data = update.data;
true
} else {
false
}
}
fn to_update(&self) -> Self::Update {
Self::Update { id: self.id, data: self.data }
}
}
impl_writeable_tlv_based!(TestObject, {
(0, id, required),
(2, data, required),
});
struct FailingStore;
impl KVStore for FailingStore {
fn read(
&self, _primary_namespace: &str, _secondary_namespace: &str, _key: &str,
) -> impl std::future::Future<Output = Result<Vec<u8>, io::Error>> + 'static + Send {
async { Err(io::Error::new(io::ErrorKind::Other, "read failed")) }
}
fn write(
&self, _primary_namespace: &str, _secondary_namespace: &str, _key: &str, _buf: Vec<u8>,
) -> impl std::future::Future<Output = Result<(), io::Error>> + 'static + Send {
async { Err(io::Error::new(io::ErrorKind::Other, "write failed")) }
}
fn remove(
&self, _primary_namespace: &str, _secondary_namespace: &str, _key: &str, _lazy: bool,
) -> impl std::future::Future<Output = Result<(), io::Error>> + 'static + Send {
async { Err(io::Error::new(io::ErrorKind::Other, "remove failed")) }
}
fn list(
&self, _primary_namespace: &str, _secondary_namespace: &str,
) -> impl std::future::Future<Output = Result<Vec<String>, io::Error>> + 'static + Send {
async { Err(io::Error::new(io::ErrorKind::Other, "list failed")) }
}
}
impl PaginatedKVStore for FailingStore {
fn list_paginated(
&self, _primary_namespace: &str, _secondary_namespace: &str,
_page_token: Option<PageToken>,
) -> impl std::future::Future<Output = Result<PaginatedListResponse, io::Error>> + 'static + Send
{
async { Err(io::Error::new(io::ErrorKind::Other, "list_paginated failed")) }
}
}
fn new_failing_data_store(objects: Vec<TestObject>) -> DataStore<TestObject, Arc<TestLogger>> {
let store: Arc<DynStore> = Arc::new(DynStoreWrapper(FailingStore));
let logger = Arc::new(TestLogger::new());
DataStore::new(
objects,
"datastore_test_primary".to_string(),
"datastore_test_secondary".to_string(),
store,
logger,
)
}
#[tokio::test]
async fn data_is_persisted() {
let store: Arc<DynStore> = Arc::new(DynStoreWrapper(InMemoryStore::new()));
let logger = Arc::new(TestLogger::new());
let primary_namespace = "datastore_test_primary".to_string();
let secondary_namespace = "datastore_test_secondary".to_string();
let data_store: DataStore<TestObject, Arc<TestLogger>> = DataStore::new(
Vec::new(),
primary_namespace.clone(),
secondary_namespace.clone(),
Arc::clone(&store),
logger,
);
let id = TestObjectId { id: [42u8; 4] };
assert!(data_store.get(&id).is_none());
let store_key = id.encode_to_hex_str();
// Check we start empty.
assert!(KVStore::read(&*store, &primary_namespace, &secondary_namespace, &store_key)
.await
.is_err());
// Check we successfully store an object and return `false`
let object = TestObject { id, data: [23u8; 3] };
assert_eq!(Ok(false), data_store.insert(object.clone()).await);
assert_eq!(Some(object), data_store.get(&id));
assert!(KVStore::read(&*store, &primary_namespace, &secondary_namespace, &store_key)
.await
.is_ok());
// Test re-insertion returns `true`
let mut override_object = object.clone();
override_object.data = [24u8; 3];
assert_eq!(Ok(true), data_store.insert(override_object).await);
assert_eq!(Some(override_object), data_store.get(&id));
// Check update returns `Updated`
let update = TestObjectUpdate { id, data: [25u8; 3] };
assert_eq!(Ok(DataStoreUpdateResult::Updated), data_store.update(update).await);
assert_eq!(data_store.get(&id).unwrap().data, [25u8; 3]);
// Check no-op update yields `Unchanged`
let update = TestObjectUpdate { id, data: [25u8; 3] };
assert_eq!(Ok(DataStoreUpdateResult::Unchanged), data_store.update(update).await);
// Check bogus update yields `NotFound`
let bogus_id = TestObjectId { id: [84u8; 4] };
let update = TestObjectUpdate { id: bogus_id, data: [12u8; 3] };
assert_eq!(Ok(DataStoreUpdateResult::NotFound), data_store.update(update).await);
// Check `insert_or_update` inserts unknown objects
let iou_id = TestObjectId { id: [55u8; 4] };
let iou_object = TestObject { id: iou_id, data: [34u8; 3] };
assert_eq!(Ok(true), data_store.insert_or_update(iou_object.clone()).await);
// Check `insert_or_update` doesn't update the same object
assert_eq!(Ok(false), data_store.insert_or_update(iou_object.clone()).await);
// Check `insert_or_update` updates if object changed
let mut new_iou_object = iou_object;
new_iou_object.data[0] += 1;
assert_eq!(Ok(true), data_store.insert_or_update(new_iou_object).await);
}
#[tokio::test]
async fn update_or_insert_inserts_when_absent() {
let store: Arc<DynStore> = Arc::new(DynStoreWrapper(InMemoryStore::new()));
let logger = Arc::new(TestLogger::new());
let primary_namespace = "datastore_test_primary".to_string();
let secondary_namespace = "datastore_test_secondary".to_string();
let data_store: DataStore<TestObject, Arc<TestLogger>> = DataStore::new(
Vec::new(),
primary_namespace.clone(),
secondary_namespace.clone(),
Arc::clone(&store),
logger,
);
let id = TestObjectId { id: [42u8; 4] };
let object = TestObject { id, data: [23u8; 3] };
let update = TestObjectUpdate { id, data: [25u8; 3] };
assert_eq!(
Ok(DataStoreUpdateOrInsertResult::Inserted),
data_store.update_or_insert(update, object).await
);
// The insert path stores the fallback object as-is; the update is not applied to it.
assert_eq!(Some(object), data_store.get(&id));
let store_key = id.encode_to_hex_str();
assert!(KVStore::read(&*store, &primary_namespace, &secondary_namespace, &store_key)
.await
.is_ok());
}
#[tokio::test]
async fn update_or_insert_applies_update_when_present() {
let store: Arc<DynStore> = Arc::new(DynStoreWrapper(InMemoryStore::new()));
let logger = Arc::new(TestLogger::new());
let id = TestObjectId { id: [42u8; 4] };
let existing_object = TestObject { id, data: [23u8; 3] };
let data_store: DataStore<TestObject, Arc<TestLogger>> = DataStore::new(
vec![existing_object],
"datastore_test_primary".to_string(),
"datastore_test_secondary".to_string(),
store,
logger,
);
// When an entry exists, only the update is applied; the fallback object must not replace
// it.
let update = TestObjectUpdate { id, data: [24u8; 3] };
let object = TestObject { id, data: [99u8; 3] };
assert_eq!(
Ok(DataStoreUpdateOrInsertResult::Updated),
data_store.update_or_insert(update, object).await
);
assert_eq!(data_store.get(&id).unwrap().data, [24u8; 3]);
}
#[tokio::test]
async fn update_or_insert_returns_unchanged_without_persisting() {
let id = TestObjectId { id: [42u8; 4] };
let existing_object = TestObject { id, data: [23u8; 3] };
let data_store = new_failing_data_store(vec![existing_object]);
// A no-op update returns `Unchanged` without attempting to persist (the store fails all
// writes) and without falling back to the object.
let update = TestObjectUpdate { id, data: [23u8; 3] };
let object = TestObject { id, data: [99u8; 3] };
assert_eq!(
Ok(DataStoreUpdateOrInsertResult::Unchanged),
data_store.update_or_insert(update, object).await
);
assert_eq!(Some(existing_object), data_store.get(&id));
}
#[tokio::test]
async fn update_or_insert_does_not_mutate_memory_if_persist_fails() {
let existing_id = TestObjectId { id: [42u8; 4] };
let existing_object = TestObject { id: existing_id, data: [23u8; 3] };
let data_store = new_failing_data_store(vec![existing_object]);
let update = TestObjectUpdate { id: existing_id, data: [24u8; 3] };
let object = TestObject { id: existing_id, data: [24u8; 3] };
assert_eq!(
Err(Error::PersistenceFailed),
data_store.update_or_insert(update, object).await
);
assert_eq!(Some(existing_object), data_store.get(&existing_id));
let new_id = TestObjectId { id: [55u8; 4] };
let new_object = TestObject { id: new_id, data: [34u8; 3] };
let new_update = TestObjectUpdate { id: new_id, data: [34u8; 3] };
assert_eq!(
Err(Error::PersistenceFailed),
data_store.update_or_insert(new_update, new_object).await
);
assert!(data_store.get(&new_id).is_none());
}
#[tokio::test]
async fn insert_or_update_does_not_mutate_memory_if_persist_fails() {
let existing_id = TestObjectId { id: [42u8; 4] };
let existing_object = TestObject { id: existing_id, data: [23u8; 3] };
let data_store = new_failing_data_store(vec![existing_object]);
let updated_object = TestObject { id: existing_id, data: [24u8; 3] };
assert_eq!(
Err(Error::PersistenceFailed),
data_store.insert_or_update(updated_object).await
);
assert_eq!(Some(existing_object), data_store.get(&existing_id));
let new_id = TestObjectId { id: [55u8; 4] };
let new_object = TestObject { id: new_id, data: [34u8; 3] };
assert_eq!(Err(Error::PersistenceFailed), data_store.insert_or_update(new_object).await);
assert!(data_store.get(&new_id).is_none());
}
#[tokio::test]
async fn insert_does_not_mutate_memory_if_persist_fails() {
let id = TestObjectId { id: [42u8; 4] };
let object = TestObject { id, data: [23u8; 3] };
let data_store = new_failing_data_store(vec![]);
assert_eq!(Err(Error::PersistenceFailed), data_store.insert(object).await);
assert!(data_store.get(&id).is_none());
}
#[tokio::test]
async fn update_does_not_mutate_memory_if_persist_fails() {
let id = TestObjectId { id: [42u8; 4] };
let object = TestObject { id, data: [23u8; 3] };
let data_store = new_failing_data_store(vec![object]);
let update = TestObjectUpdate { id, data: [24u8; 3] };
assert_eq!(Err(Error::PersistenceFailed), data_store.update(update).await);
assert_eq!(Some(object), data_store.get(&id));
}
#[tokio::test]
async fn remove_does_not_mutate_memory_if_persist_fails() {
let id = TestObjectId { id: [42u8; 4] };
let object = TestObject { id, data: [23u8; 3] };
let data_store = new_failing_data_store(vec![object]);
assert_eq!(Err(Error::PersistenceFailed), data_store.remove(&id).await);
assert_eq!(Some(object), data_store.get(&id));
}
}