libsql-wal 0.1.0-alpha.1

wal implementation for libsql
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
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
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
use std::collections::BTreeMap;
use std::fmt::Debug;
use std::path::{Path, PathBuf};
use std::pin::Pin;
use std::str::FromStr;
use std::sync::Arc;
use std::{fmt, future::Future};

use chrono::{DateTime, Utc};
use fst::Map;
use hashbrown::HashMap;
use libsql_sys::name::NamespaceName;
use libsql_sys::wal::either::Either;
use tempfile::{tempdir, TempDir};
use tokio_stream::Stream;

use crate::io::{FileExt, Io, StdIO};
use crate::segment::compacted::CompactedSegment;
use crate::segment::{sealed::SealedSegment, Segment};

use self::backend::{FindSegmentReq, SegmentMeta};
pub use self::error::Error;

pub mod async_storage;
pub mod backend;
pub mod compaction;
pub(crate) mod error;
mod job;
mod scheduler;

pub type Result<T, E = self::error::Error> = std::result::Result<T, E>;

pub enum RestoreOptions {
    Latest,
    Timestamp(DateTime<Utc>),
}

/// SegmentKey is used to index segment data, where keys a lexicographically ordered.
/// The scheme is `{u64::MAX - start_frame_no}-{u64::MAX - end_frame_no}`. With that naming convention, when looking for
/// the segment containing 'n', we can perform a prefix search with "{u64::MAX - n}". The first
/// element of the range will be the biggest segment that contains n if it exists.
/// Beware that if no segments contain n, either the smallest segment not containing n, if n < argmin
/// {start_frame_no}, or the largest segment if n > argmax {end_frame_no} will be returned.
/// e.g:
/// ```ignore
/// let mut map = BTreeMap::new();
///
/// let meta = SegmentMeta { start_frame_no: 1, end_frame_no: 100 };
/// map.insert(SegmentKey(&meta).to_string(), meta);
///
/// let meta = SegmentMeta { start_frame_no: 101, end_frame_no: 500 };
/// map.insert(SegmentKey(&meta).to_string(), meta);
///
/// let meta = SegmentMeta { start_frame_no: 101, end_frame_no: 1000 };
/// map.insert(SegmentKey(&meta).to_string(), meta);
///
/// map.range(format!("{:020}", u64::MAX - 50)..).next();
/// map.range(format!("{:020}", u64::MAX - 0)..).next();
/// map.range(format!("{:020}", u64::MAX - 1)..).next();
/// map.range(format!("{:020}", u64::MAX - 100)..).next();
/// map.range(format!("{:020}", u64::MAX - 101)..).next();
/// map.range(format!("{:020}", u64::MAX - 5000)..).next();
/// ```
#[derive(Clone, Copy, PartialEq, Eq)]
pub struct SegmentKey {
    pub start_frame_no: u64,
    pub end_frame_no: u64,
    pub timestamp: u64,
}

impl Debug for SegmentKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("SegmentKey")
            .field("start_frame_no", &self.start_frame_no)
            .field("end_frame_no", &self.end_frame_no)
            .field("timestamp", &self.timestamp())
            .finish()
    }
}

impl PartialOrd for SegmentKey {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        match self.start_frame_no.partial_cmp(&other.start_frame_no) {
            Some(core::cmp::Ordering::Equal) => {}
            ord => return ord,
        }
        self.end_frame_no.partial_cmp(&other.end_frame_no)
    }
}

impl Ord for SegmentKey {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.partial_cmp(other).unwrap()
    }
}

impl SegmentKey {
    pub(crate) fn includes(&self, frame_no: u64) -> bool {
        (self.start_frame_no..=self.end_frame_no).contains(&frame_no)
    }

    #[tracing::instrument]
    fn validate_from_path(mut path: &Path, ns: &NamespaceName) -> Option<Self> {
        // path in the form "v2/clusters/{cluster-id}/namespaces/{namespace}/indexes/{index-key}"
        let key: Self = path.file_name()?.to_str()?.parse().ok()?;

        path = path.parent()?;

        if path.file_name()? != "indexes" {
            tracing::debug!("invalid key, ignoring");
            return None;
        }

        path = path.parent()?;

        if path.file_name()? != ns.as_str() {
            tracing::debug!("invalid namespace for key");
            return None;
        }

        Some(key)
    }

    fn timestamp(&self) -> DateTime<Utc> {
        DateTime::from_timestamp_millis(self.timestamp as _)
            .unwrap()
            .to_utc()
    }
}

impl From<&SegmentMeta> for SegmentKey {
    fn from(value: &SegmentMeta) -> Self {
        Self {
            start_frame_no: value.start_frame_no,
            end_frame_no: value.end_frame_no,
            timestamp: value.segment_timestamp.timestamp_millis() as _,
        }
    }
}

impl FromStr for SegmentKey {
    type Err = ();

    fn from_str(s: &str) -> std::result::Result<Self, Self::Err> {
        let (rev_end_fno, s) = s.split_at(20);
        let end_frame_no = u64::MAX - rev_end_fno.parse::<u64>().map_err(|_| ())?;
        let (start_fno, timestamp) = s[1..].split_at(20);
        let start_frame_no = start_fno.parse::<u64>().map_err(|_| ())?;
        let timestamp = timestamp[1..].parse().map_err(|_| ())?;
        Ok(Self {
            start_frame_no,
            end_frame_no,
            timestamp,
        })
    }
}

impl fmt::Display for SegmentKey {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{:020}-{:020}-{:020}",
            u64::MAX - self.end_frame_no,
            self.start_frame_no,
            self.timestamp,
        )
    }
}

/// takes the new durable frame_no and returns a future
pub type OnStoreCallback = Box<
    dyn FnOnce(u64) -> Pin<Box<dyn Future<Output = ()> + Send + Sync + 'static>>
        + Send
        + Sync
        + 'static,
>;

pub trait Storage: Send + Sync + 'static {
    type Segment: Segment;
    type Config: Clone + Send;
    /// store the passed segment for `namespace`. This function is called in a context where
    /// blocking is acceptable.
    /// returns a future that resolves when the segment is stored
    /// The segment should be stored whether or not the future is polled.
    fn store(
        &self,
        namespace: &NamespaceName,
        seg: Self::Segment,
        config_override: Option<Self::Config>,
        on_store: OnStoreCallback,
    );

    fn durable_frame_no(
        &self,
        namespace: &NamespaceName,
        config_override: Option<Self::Config>,
    ) -> impl Future<Output = Result<u64>> + Send;

    async fn restore(
        &self,
        file: impl FileExt,
        namespace: &NamespaceName,
        restore_options: RestoreOptions,
        config_override: Option<Self::Config>,
    ) -> Result<()>;

    fn find_segment(
        &self,
        namespace: &NamespaceName,
        frame_no: FindSegmentReq,
        config_override: Option<Self::Config>,
    ) -> impl Future<Output = Result<SegmentKey>> + Send;

    fn fetch_segment_index(
        &self,
        namespace: &NamespaceName,
        key: &SegmentKey,
        config_override: Option<Self::Config>,
    ) -> impl Future<Output = Result<Map<Arc<[u8]>>>> + Send;

    fn fetch_segment_data(
        &self,
        namespace: &NamespaceName,
        key: &SegmentKey,
        config_override: Option<Self::Config>,
    ) -> impl Future<Output = Result<CompactedSegment<impl FileExt>>> + Send;

    fn shutdown(&self) -> impl Future<Output = ()> + Send {
        async { () }
    }

    fn list_segments<'a>(
        &'a self,
        namespace: &'a NamespaceName,
        until: u64,
        config_override: Option<Self::Config>,
    ) -> impl Stream<Item = Result<SegmentInfo>> + 'a;
}

#[derive(Debug)]
pub struct SegmentInfo {
    pub key: SegmentKey,
    pub size: usize,
}

/// special zip function for Either storage implementation
fn zip<A, B, C, D>(
    x: &Either<A, B>,
    y: Option<Either<C, D>>,
) -> Either<(&A, Option<C>), (&B, Option<D>)> {
    match (x, y) {
        (Either::A(a), Some(Either::A(c))) => Either::A((a, Some(c))),
        (Either::B(b), Some(Either::B(d))) => Either::B((b, Some(d))),
        (Either::A(a), None) => Either::A((a, None)),
        (Either::B(b), None) => Either::B((b, None)),
        _ => panic!("incompatible options"),
    }
}

impl<A, B, S> Storage for Either<A, B>
where
    A: Storage<Segment = S>,
    B: Storage<Segment = S>,
    S: Segment,
{
    type Segment = S;
    type Config = Either<A::Config, B::Config>;

    fn store(
        &self,
        namespace: &NamespaceName,
        seg: Self::Segment,
        config_override: Option<Self::Config>,
        on_store: OnStoreCallback,
    ) {
        match zip(self, config_override) {
            Either::A((s, c)) => s.store(namespace, seg, c, on_store),
            Either::B((s, c)) => s.store(namespace, seg, c, on_store),
        }
    }

    async fn durable_frame_no(
        &self,
        namespace: &NamespaceName,
        config_override: Option<Self::Config>,
    ) -> Result<u64> {
        match zip(self, config_override) {
            Either::A((s, c)) => s.durable_frame_no(namespace, c).await,
            Either::B((s, c)) => s.durable_frame_no(namespace, c).await,
        }
    }

    async fn restore(
        &self,
        file: impl FileExt,
        namespace: &NamespaceName,
        restore_options: RestoreOptions,
        config_override: Option<Self::Config>,
    ) -> Result<()> {
        match zip(self, config_override) {
            Either::A((s, c)) => s.restore(file, namespace, restore_options, c).await,
            Either::B((s, c)) => s.restore(file, namespace, restore_options, c).await,
        }
    }

    fn find_segment(
        &self,
        namespace: &NamespaceName,
        frame_no: FindSegmentReq,
        config_override: Option<Self::Config>,
    ) -> impl Future<Output = Result<SegmentKey>> + Send {
        async move {
            match zip(self, config_override) {
                Either::A((s, c)) => s.find_segment(namespace, frame_no, c).await,
                Either::B((s, c)) => s.find_segment(namespace, frame_no, c).await,
            }
        }
    }

    fn fetch_segment_index(
        &self,
        namespace: &NamespaceName,
        key: &SegmentKey,
        config_override: Option<Self::Config>,
    ) -> impl Future<Output = Result<Map<Arc<[u8]>>>> + Send {
        async move {
            match zip(self, config_override) {
                Either::A((s, c)) => s.fetch_segment_index(namespace, key, c).await,
                Either::B((s, c)) => s.fetch_segment_index(namespace, key, c).await,
            }
        }
    }

    fn fetch_segment_data(
        &self,
        namespace: &NamespaceName,
        key: &SegmentKey,
        config_override: Option<Self::Config>,
    ) -> impl Future<Output = Result<CompactedSegment<impl FileExt>>> + Send {
        async move {
            match zip(self, config_override) {
                Either::A((s, c)) => {
                    let seg = s.fetch_segment_data(namespace, key, c).await?;
                    let seg = seg.remap_file_type(Either::A);
                    Ok(seg)
                }
                Either::B((s, c)) => {
                    let seg = s.fetch_segment_data(namespace, key, c).await?;
                    let seg = seg.remap_file_type(Either::B);
                    Ok(seg)
                }
            }
        }
    }

    async fn shutdown(&self) {
        match self {
            Either::A(a) => a.shutdown().await,
            Either::B(b) => b.shutdown().await,
        }
    }

    fn list_segments<'a>(
        &'a self,
        namespace: &'a NamespaceName,
        until: u64,
        config_override: Option<Self::Config>,
    ) -> impl Stream<Item = Result<SegmentInfo>> + 'a {
        match zip(self, config_override) {
            Either::A((s, c)) => {
                tokio_util::either::Either::Left(s.list_segments(namespace, until, c))
            }
            Either::B((s, c)) => {
                tokio_util::either::Either::Right(s.list_segments(namespace, until, c))
            }
        }
    }
}

/// a placeholder storage that doesn't store segment
#[derive(Debug, Clone, Copy)]
pub struct NoStorage;

impl Storage for NoStorage {
    type Config = ();
    type Segment = SealedSegment<std::fs::File>;

    fn store(
        &self,
        _namespace: &NamespaceName,
        _seg: Self::Segment,
        _config: Option<Self::Config>,
        _on_store: OnStoreCallback,
    ) {
    }

    async fn durable_frame_no(
        &self,
        _namespace: &NamespaceName,
        _config: Option<Self::Config>,
    ) -> Result<u64> {
        Ok(u64::MAX)
    }

    async fn restore(
        &self,
        _file: impl FileExt,
        _namespace: &NamespaceName,
        _restore_options: RestoreOptions,
        _config_override: Option<Self::Config>,
    ) -> Result<()> {
        panic!("can restore from no storage")
    }

    async fn find_segment(
        &self,
        _namespace: &NamespaceName,
        _frame_no: FindSegmentReq,
        _config_override: Option<Self::Config>,
    ) -> Result<SegmentKey> {
        unimplemented!()
    }

    async fn fetch_segment_index(
        &self,
        _namespace: &NamespaceName,
        _key: &SegmentKey,
        _config_override: Option<Self::Config>,
    ) -> Result<Map<Arc<[u8]>>> {
        unimplemented!()
    }

    async fn fetch_segment_data(
        &self,
        _namespace: &NamespaceName,
        _key: &SegmentKey,
        _config_override: Option<Self::Config>,
    ) -> Result<CompactedSegment<impl FileExt>> {
        unimplemented!();
        #[allow(unreachable_code)]
        Result::<CompactedSegment<std::fs::File>>::Err(Error::InvalidIndex(""))
    }

    fn list_segments<'a>(
        &'a self,
        _namespace: &'a NamespaceName,
        _until: u64,
        _config_override: Option<Self::Config>,
    ) -> impl Stream<Item = Result<SegmentInfo>> + 'a {
        unimplemented!("no storage!");
        #[allow(unreachable_code)]
        tokio_stream::empty()
    }
}

#[doc(hidden)]
#[derive(Debug)]
pub struct TestStorage<IO = StdIO> {
    inner: Arc<async_lock::Mutex<TestStorageInner<IO>>>,
}

#[derive(Debug)]
struct TestStorageInner<IO> {
    stored: HashMap<NamespaceName, BTreeMap<SegmentKey, (PathBuf, Map<Arc<[u8]>>)>>,
    dir: TempDir,
    io: IO,
    store: bool,
}

impl<F> Clone for TestStorage<F> {
    fn clone(&self) -> Self {
        Self {
            inner: self.inner.clone(),
        }
    }
}

impl TestStorage<StdIO> {
    pub fn new() -> Self {
        Self::new_io(false, StdIO(()))
    }

    pub fn new_store() -> Self {
        Self::new_io(true, StdIO(()))
    }
}

impl<IO: Io> TestStorage<IO> {
    pub fn new_io(store: bool, io: IO) -> Self {
        let dir = tempdir().unwrap();
        Self {
            inner: Arc::new(
                TestStorageInner {
                    dir,
                    stored: Default::default(),
                    io,
                    store,
                }
                .into(),
            ),
        }
    }
}

impl<IO: Io> Storage for TestStorage<IO> {
    type Segment = SealedSegment<IO::File>;
    type Config = ();

    fn store(
        &self,
        namespace: &NamespaceName,
        seg: Self::Segment,
        _config: Option<Self::Config>,
        on_store: OnStoreCallback,
    ) {
        let mut inner = self.inner.lock_blocking();
        if inner.store {
            let id = uuid::Uuid::new_v4();
            let out_path = inner.dir.path().join(id.to_string());
            let out_file = inner.io.open(true, true, true, &out_path).unwrap();
            let index = tokio::runtime::Handle::current()
                .block_on(seg.compact(&out_file, id))
                .unwrap();
            let end_frame_no = seg.header().last_committed();
            let key = SegmentKey {
                start_frame_no: seg.header().start_frame_no.get(),
                end_frame_no,
                timestamp: seg.header().sealed_at_timestamp.get(),
            };
            let index = Map::new(index.into()).unwrap();
            inner
                .stored
                .entry(namespace.clone())
                .or_default()
                .insert(key, (out_path, index));
            tokio::runtime::Handle::current().block_on(on_store(end_frame_no));
        } else {
            // HACK: we need to spawn because many tests just call this method indirectly in
            // async context. That makes tests easier to write.
            tokio::task::spawn_blocking(move || {
                tokio::runtime::Handle::current().block_on(on_store(u64::MAX));
            });
        }
    }

    async fn durable_frame_no(
        &self,
        _namespace: &NamespaceName,
        _config: Option<Self::Config>,
    ) -> Result<u64> {
        Ok(u64::MAX)
    }

    async fn restore(
        &self,
        _file: impl FileExt,
        _namespace: &NamespaceName,
        _restore_options: RestoreOptions,
        _config_override: Option<Self::Config>,
    ) -> Result<()> {
        todo!();
    }

    async fn find_segment(
        &self,
        namespace: &NamespaceName,
        req: FindSegmentReq,
        _config_override: Option<Self::Config>,
    ) -> Result<SegmentKey> {
        let inner = self.inner.lock().await;
        if inner.store {
            let FindSegmentReq::EndFrameNoLessThan(fno) = req else {
                panic!("unsupported lookup by ts")
            };
            if let Some(segs) = inner.stored.get(namespace) {
                let Some((key, _path)) = segs.iter().find(|(k, _)| k.includes(fno)) else {
                    return Err(Error::SegmentNotFound(req));
                };
                return Ok(*key);
            } else {
                panic!("namespace not found");
            }
        } else {
            panic!("store not enabled")
        }
    }

    async fn fetch_segment_index(
        &self,
        namespace: &NamespaceName,
        key: &SegmentKey,
        _config_override: Option<Self::Config>,
    ) -> Result<Map<Arc<[u8]>>> {
        let inner = self.inner.lock().await;
        if inner.store {
            match inner.stored.get(namespace) {
                Some(segs) => Ok(segs.get(&key).unwrap().1.clone()),
                None => panic!("unknown namespace"),
            }
        } else {
            panic!("not storing")
        }
    }

    async fn fetch_segment_data(
        &self,
        namespace: &NamespaceName,
        key: &SegmentKey,
        _config_override: Option<Self::Config>,
    ) -> Result<CompactedSegment<impl FileExt>> {
        let inner = self.inner.lock().await;
        if inner.store {
            match inner.stored.get(namespace) {
                Some(segs) => {
                    let path = &segs.get(&key).unwrap().0;
                    let file = inner.io.open(false, true, false, path).unwrap();
                    Ok(CompactedSegment::open(file).await?)
                }
                None => panic!("unknown namespace"),
            }
        } else {
            panic!("not storing")
        }
    }

    fn list_segments<'a>(
        &'a self,
        _namespace: &'a NamespaceName,
        _until: u64,
        _config_override: Option<Self::Config>,
    ) -> impl Stream<Item = Result<SegmentInfo>> + 'a {
        todo!();
        #[allow(unreachable_code)]
        tokio_stream::empty()
    }
}

pub struct StoreSegmentRequest<S, C> {
    namespace: NamespaceName,
    /// Path to the segment. Read-only for bottomless
    segment: S,
    /// When this segment was created
    created_at: DateTime<Utc>,

    /// alternative configuration to use with the storage layer.
    /// e.g: S3 overrides
    storage_config_override: Option<C>,
    /// Called after the segment was stored, with the new durable index
    on_store_callback: OnStoreCallback,
}

impl<S, C> Debug for StoreSegmentRequest<S, C>
where
    S: Debug,
{
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("StoreSegmentRequest")
            .field("namespace", &self.namespace)
            .field("segment", &self.segment)
            .field("created_at", &self.created_at)
            .finish()
    }
}