slatedb 0.14.0

A cloud native embedded storage engine built on object storage.
Documentation
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
use crate::{
    config::GarbageCollectorScheduleOptions,
    error::SlateDBError,
    manifest::{
        store::{ManifestStore, StoredManifest},
        ExternalDb, Manifest,
    },
};
use chrono::{DateTime, Utc};
use log::{error, info};
use object_store::ObjectStore;
use slatedb_common::clock::SystemClock;
use slatedb_txn_obj::TransactionalObject;
use std::collections::HashSet;
use std::sync::Arc;
use uuid::Uuid;

use super::{GcStats, GcTask};

/// Detaches a clone from its parent database(s) once the clone no longer references
/// any of the parent's SSTs.
///
/// A clone references its parent via an entry in `manifest.external_dbs`. The entry
/// holds a list of parent SST ids the clone may read (`sst_ids`) and a
/// `final_checkpoint_id` that pins a checkpoint in the parent's manifest so the
/// parent's GC does not collect those SSTs.
///
/// Compaction on the clone shrinks `sst_ids` as parent SSTs are rewritten into
/// clone-owned SSTs (see `Manifest::prune_external_sst_ids`). Once `sst_ids` is
/// empty in the current manifest AND in every manifest version referenced by a live
/// checkpoint, the clone no longer needs the parent — and this task detaches it:
///
/// 1. Delete the pinning `final_checkpoint_id` on the parent (idempotent).
/// 2. Remove the `ExternalDb` entry from the clone's manifest.
///
/// Parent-first order is chosen deliberately: a crash between the two leaves the
/// clone with a stale `ExternalDb` whose `sst_ids` is empty and whose
/// `final_checkpoint_id` points to a missing checkpoint — the next tick retries
/// step 1 as a no-op and completes step 2. The reverse order would leak the
/// parent's checkpoint forever on a crash.
#[derive(Clone)]
pub(crate) struct DetachGcTask {
    manifest_store: Arc<ManifestStore>,
    object_store: Arc<dyn ObjectStore>,
    system_clock: Arc<dyn SystemClock>,
    stats: Arc<GcStats>,
    detach_options: GarbageCollectorScheduleOptions,
}

impl std::fmt::Debug for DetachGcTask {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("DetachGcTask")
            .field("detach_options", &self.detach_options)
            .finish()
    }
}

impl DetachGcTask {
    pub(super) fn new(
        manifest_store: Arc<ManifestStore>,
        object_store: Arc<dyn ObjectStore>,
        system_clock: Arc<dyn SystemClock>,
        stats: Arc<GcStats>,
        detach_options: GarbageCollectorScheduleOptions,
    ) -> Self {
        Self {
            manifest_store,
            object_store,
            system_clock,
            stats,
            detach_options,
        }
    }

    async fn find_detachable(
        &self,
        manifest_id: u64,
        manifest: &Manifest,
    ) -> Result<Vec<ExternalDb>, SlateDBError> {
        if manifest.external_dbs.is_empty() {
            return Ok(Vec::new());
        }

        let candidates: Vec<&ExternalDb> = manifest
            .external_dbs
            .iter()
            .filter(|e| e.sst_ids.is_empty() && e.final_checkpoint_id.is_some())
            .collect();

        if candidates.is_empty() {
            return Ok(Vec::new());
        }

        let referenced = self
            .manifest_store
            .read_referenced_manifests(manifest_id, manifest)
            .await?;

        let detachable: Vec<ExternalDb> = candidates
            .into_iter()
            .filter(|candidate| {
                let final_id = candidate
                    .final_checkpoint_id
                    .expect("candidates must have final_checkpoint_id");
                referenced.values().all(|other_manifest| {
                    other_manifest.external_dbs.iter().all(|other| {
                        other.final_checkpoint_id != Some(final_id) || other.sst_ids.is_empty()
                    })
                })
            })
            .cloned()
            .collect();

        Ok(detachable)
    }

    async fn detach_from_parent(&self, external_db: &ExternalDb) -> Result<(), SlateDBError> {
        let final_id = external_db
            .final_checkpoint_id
            .expect("detachable entries must have final_checkpoint_id");

        let parent_store = Arc::new(ManifestStore::new(
            &external_db.path.clone().into(),
            self.object_store.clone(),
        ));
        let mut parent_manifest =
            StoredManifest::load(parent_store, self.system_clock.clone()).await?;
        parent_manifest.delete_checkpoint(final_id).await
    }
}

impl GcTask for DetachGcTask {
    async fn collect(&self, _utc_now: DateTime<Utc>) -> Result<(), SlateDBError> {
        let mut stored_manifest =
            StoredManifest::load(self.manifest_store.clone(), self.system_clock.clone()).await?;
        let manifest_id = stored_manifest.id();
        let manifest_snapshot = stored_manifest.manifest().clone();

        let detachable = self
            .find_detachable(manifest_id, &manifest_snapshot)
            .await?;
        if detachable.is_empty() {
            return Ok(());
        }

        let mut detached_final_ids: HashSet<Uuid> = HashSet::new();
        for external_db in &detachable {
            match self.detach_from_parent(external_db).await {
                Ok(()) => {
                    let final_id = external_db
                        .final_checkpoint_id
                        .expect("detachable entries must have final_checkpoint_id");
                    detached_final_ids.insert(final_id);
                    info!(
                        "detached clone from parent [parent_path={}, final_checkpoint_id={}]",
                        external_db.path, final_id
                    );
                }
                Err(e) => {
                    error!(
                        "failed to delete pinning checkpoint on parent [parent_path={}, error={}]",
                        external_db.path, e
                    );
                }
            }
        }

        if detached_final_ids.is_empty() {
            return Ok(());
        }

        let detached_count = detached_final_ids.len() as u64;

        stored_manifest
            .maybe_apply_update(|sr| {
                let current = sr.object();
                let retained: Vec<ExternalDb> = current
                    .external_dbs
                    .iter()
                    .filter(|e| {
                        e.final_checkpoint_id
                            .map(|id| !detached_final_ids.contains(&id))
                            .unwrap_or(true)
                    })
                    .cloned()
                    .collect();
                if retained.len() == current.external_dbs.len() {
                    Ok(None)
                } else {
                    let mut dirty = sr.prepare_dirty()?;
                    dirty.value.external_dbs = retained;
                    Ok(Some(dirty))
                }
            })
            .await?;

        for _ in 0..detached_count {
            self.stats.gc_detach_count.increment(1);
        }

        Ok(())
    }

    fn resource(&self) -> &str {
        "Clone detach"
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::config::CheckpointOptions;
    use crate::db_state::SsTableId;
    use crate::manifest::ManifestCore;
    use object_store::memory::InMemory;
    use object_store::path::Path;
    use slatedb_common::clock::DefaultSystemClock;
    use slatedb_txn_obj::TransactionalObject;
    use ulid::Ulid;

    const PARENT_PATH: &str = "/parent";
    const CLONE_PATH: &str = "/clone";

    /// Materialize a test setup: shared object store with an initialized parent and an
    /// initialized clone manifest. Returns (object_store, parent_store, clone_store).
    async fn build_setup() -> (Arc<dyn ObjectStore>, Arc<ManifestStore>, Arc<ManifestStore>) {
        let object_store: Arc<dyn ObjectStore> = Arc::new(InMemory::new());
        let parent_store = Arc::new(ManifestStore::new(
            &Path::from(PARENT_PATH),
            object_store.clone(),
        ));
        let clone_store = Arc::new(ManifestStore::new(
            &Path::from(CLONE_PATH),
            object_store.clone(),
        ));
        let clock: Arc<dyn SystemClock> = Arc::new(DefaultSystemClock::new());
        StoredManifest::create_new_db(parent_store.clone(), ManifestCore::new(), clock.clone())
            .await
            .unwrap();
        StoredManifest::create_new_db(clone_store.clone(), ManifestCore::new(), clock.clone())
            .await
            .unwrap();
        (object_store, parent_store, clone_store)
    }

    fn clock() -> Arc<dyn SystemClock> {
        Arc::new(DefaultSystemClock::new())
    }

    fn new_stats() -> Arc<GcStats> {
        let recorder = slatedb_common::metrics::MetricsRecorderHelper::noop();
        Arc::new(GcStats::new(&recorder))
    }

    /// Seed the parent with a checkpoint pinning `final_checkpoint_id`; add a matching
    /// ExternalDb entry to the clone's manifest with the provided sst_ids.
    async fn attach_clone_to_parent(
        parent_store: &Arc<ManifestStore>,
        clone_store: &Arc<ManifestStore>,
        final_checkpoint_id: Uuid,
        sst_ids: Vec<SsTableId>,
    ) {
        let mut parent = StoredManifest::load(parent_store.clone(), clock())
            .await
            .unwrap();
        parent
            .write_checkpoint(
                final_checkpoint_id,
                &CheckpointOptions {
                    lifetime: None,
                    source: None,
                    name: None,
                },
            )
            .await
            .unwrap();

        let mut clone = StoredManifest::load(clone_store.clone(), clock())
            .await
            .unwrap();
        clone
            .maybe_apply_update(|sr| {
                let mut dirty = sr.prepare_dirty()?;
                dirty.value.external_dbs.push(ExternalDb {
                    path: PARENT_PATH.to_string(),
                    source_checkpoint_id: Uuid::new_v4(),
                    final_checkpoint_id: Some(final_checkpoint_id),
                    sst_ids: sst_ids.clone(),
                });
                Ok(Some(dirty))
            })
            .await
            .unwrap();
    }

    fn make_task(
        clone_store: Arc<ManifestStore>,
        object_store: Arc<dyn ObjectStore>,
    ) -> DetachGcTask {
        DetachGcTask::new(
            clone_store,
            object_store,
            clock(),
            new_stats(),
            GarbageCollectorScheduleOptions { interval: None },
        )
    }

    async fn parent_has_checkpoint(parent_store: &Arc<ManifestStore>, id: Uuid) -> bool {
        let parent = StoredManifest::load(parent_store.clone(), clock())
            .await
            .unwrap();
        parent.db_state().find_checkpoint(id).is_some()
    }

    async fn clone_external_dbs_len(clone_store: &Arc<ManifestStore>) -> usize {
        let clone = StoredManifest::load(clone_store.clone(), clock())
            .await
            .unwrap();
        clone.manifest().external_dbs.len()
    }

    #[tokio::test]
    async fn test_detaches_entry_with_empty_sst_ids() {
        let (object_store, parent_store, clone_store) = build_setup().await;
        let final_id = Uuid::new_v4();
        attach_clone_to_parent(&parent_store, &clone_store, final_id, vec![]).await;
        assert!(parent_has_checkpoint(&parent_store, final_id).await);
        assert_eq!(clone_external_dbs_len(&clone_store).await, 1);

        let task = make_task(clone_store.clone(), object_store);
        task.collect(Utc::now()).await.unwrap();

        assert!(
            !parent_has_checkpoint(&parent_store, final_id).await,
            "parent checkpoint should be deleted"
        );
        assert_eq!(
            clone_external_dbs_len(&clone_store).await,
            0,
            "clone external_dbs entry should be removed"
        );
    }

    #[tokio::test]
    async fn test_skips_entry_with_non_empty_sst_ids() {
        let (object_store, parent_store, clone_store) = build_setup().await;
        let final_id = Uuid::new_v4();
        let live_sst = SsTableId::Compacted(Ulid::new());
        attach_clone_to_parent(&parent_store, &clone_store, final_id, vec![live_sst]).await;

        let task = make_task(clone_store.clone(), object_store);
        task.collect(Utc::now()).await.unwrap();

        assert!(
            parent_has_checkpoint(&parent_store, final_id).await,
            "parent checkpoint must be retained while clone still references it"
        );
        assert_eq!(
            clone_external_dbs_len(&clone_store).await,
            1,
            "clone external_dbs entry must be retained"
        );
    }

    #[tokio::test]
    async fn test_skips_entry_held_by_clone_checkpoint() {
        // The clone had a non-empty sst_ids at a prior manifest version, then compaction
        // emptied it. A live checkpoint on the clone still references the older manifest
        // version where sst_ids was non-empty — detach must NOT fire.
        let (object_store, parent_store, clone_store) = build_setup().await;
        let final_id = Uuid::new_v4();
        let live_sst = SsTableId::Compacted(Ulid::new());
        attach_clone_to_parent(&parent_store, &clone_store, final_id, vec![live_sst]).await;

        // Pin this manifest version (with non-empty sst_ids) via a clone-side checkpoint.
        let mut clone = StoredManifest::load(clone_store.clone(), clock())
            .await
            .unwrap();
        clone
            .write_checkpoint(
                Uuid::new_v4(),
                &CheckpointOptions {
                    lifetime: None,
                    source: None,
                    name: None,
                },
            )
            .await
            .unwrap();

        // Now empty sst_ids in the current manifest.
        let mut clone = StoredManifest::load(clone_store.clone(), clock())
            .await
            .unwrap();
        clone
            .maybe_apply_update(|sr| {
                let mut dirty = sr.prepare_dirty()?;
                for entry in dirty.value.external_dbs.iter_mut() {
                    entry.sst_ids.clear();
                }
                Ok(Some(dirty))
            })
            .await
            .unwrap();

        let task = make_task(clone_store.clone(), object_store);
        task.collect(Utc::now()).await.unwrap();

        assert!(
            parent_has_checkpoint(&parent_store, final_id).await,
            "parent checkpoint must remain while a clone-side checkpoint still holds non-empty sst_ids"
        );
        assert_eq!(clone_external_dbs_len(&clone_store).await, 1);
    }

    #[tokio::test]
    async fn test_idempotent_when_parent_checkpoint_already_gone() {
        let (object_store, parent_store, clone_store) = build_setup().await;
        let final_id = Uuid::new_v4();
        attach_clone_to_parent(&parent_store, &clone_store, final_id, vec![]).await;

        // Simulate a prior partial detach: parent's checkpoint is already deleted.
        let mut parent = StoredManifest::load(parent_store.clone(), clock())
            .await
            .unwrap();
        parent.delete_checkpoint(final_id).await.unwrap();
        assert!(!parent_has_checkpoint(&parent_store, final_id).await);

        let task = make_task(clone_store.clone(), object_store);
        task.collect(Utc::now()).await.unwrap();

        assert_eq!(
            clone_external_dbs_len(&clone_store).await,
            0,
            "clone-side removal must still complete even though parent checkpoint was already gone"
        );
    }

    #[tokio::test]
    async fn test_detaches_only_matching_entry_among_many() {
        let (object_store, parent_store, clone_store) = build_setup().await;
        let detachable_id = Uuid::new_v4();
        let retained_id = Uuid::new_v4();
        let live_sst = SsTableId::Compacted(Ulid::new());
        attach_clone_to_parent(&parent_store, &clone_store, detachable_id, vec![]).await;
        attach_clone_to_parent(&parent_store, &clone_store, retained_id, vec![live_sst]).await;
        assert_eq!(clone_external_dbs_len(&clone_store).await, 2);

        let task = make_task(clone_store.clone(), object_store);
        task.collect(Utc::now()).await.unwrap();

        assert!(!parent_has_checkpoint(&parent_store, detachable_id).await);
        assert!(parent_has_checkpoint(&parent_store, retained_id).await);
        let clone = StoredManifest::load(clone_store.clone(), clock())
            .await
            .unwrap();
        let external_dbs = &clone.manifest().external_dbs;
        assert_eq!(external_dbs.len(), 1);
        assert_eq!(external_dbs[0].final_checkpoint_id, Some(retained_id));
    }
}