sleet 0.1.0

A fleet manager for SlateDB databases
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
//! Test support: the seams the chaos and DST test suites need.
//!
//! `TestStore` decorates any `ObjectStore` with per-operation counters,
//! deterministic fault injection, and, when given a `TestClock`, a
//! simulated `LastModified`, so heartbeat ages follow virtual time
//! instead of the wall clock. `TestClock` also implements
//! `crate::root::Clock` for injection into `FleetRoot`.

use std::collections::HashMap;
use std::fmt;
use std::ops::Range;
use std::sync::Arc;
use std::sync::Mutex;
use std::sync::atomic::{AtomicU64, Ordering};

use async_trait::async_trait;
use chrono::{DateTime, Utc};
use futures::stream::BoxStream;
use futures::{StreamExt, TryStreamExt};
use object_store::path::Path;
use object_store::{
    CopyOptions, GetOptions, GetResult, ListResult, MultipartUpload, ObjectMeta, ObjectStore,
    PutMultipartOptions, PutOptions, PutPayload, PutResult,
};

use crate::root::Clock;

/// A controllable clock: `now()` returns virtual time, advanced
/// explicitly by tests.
pub struct TestClock(Mutex<DateTime<Utc>>);

impl TestClock {
    /// A clock reading `start`.
    pub fn new(start: DateTime<Utc>) -> Arc<Self> {
        Arc::new(Self(Mutex::new(start)))
    }

    /// Advance virtual time by `by`.
    pub fn advance(&self, by: std::time::Duration) {
        let mut now = self.0.lock().expect("clock lock");
        *now += chrono::Duration::from_std(by).expect("duration fits");
    }
}

impl Clock for TestClock {
    fn now(&self) -> DateTime<Utc> {
        *self.0.lock().expect("clock lock")
    }
}

/// Coarse operation classes for counting and fault injection.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum Op {
    /// GET, including ranged reads.
    Get,
    /// PUT, including multipart uploads.
    Put,
    /// LIST, in all its variants.
    List,
    /// DELETE.
    Delete,
    /// Server-side copy.
    Copy,
}

/// Per-operation call counters.
#[derive(Default)]
pub struct Counters {
    get: AtomicU64,
    put: AtomicU64,
    list: AtomicU64,
    delete: AtomicU64,
    copy: AtomicU64,
}

impl Counters {
    /// Calls of `op` so far, including failed ones.
    pub fn count(&self, op: Op) -> u64 {
        self.cell(op).load(Ordering::SeqCst)
    }

    fn bump(&self, op: Op) {
        self.cell(op).fetch_add(1, Ordering::SeqCst);
    }

    fn cell(&self, op: Op) -> &AtomicU64 {
        match op {
            Op::Get => &self.get,
            Op::Put => &self.put,
            Op::List => &self.list,
            Op::Delete => &self.delete,
            Op::Copy => &self.copy,
        }
    }
}

#[derive(Default)]
struct Faults {
    /// Fail the next N calls of an op.
    next: HashMap<Op, u64>,
    /// Fail every call of these ops until healed.
    always: HashMap<Op, bool>,
    /// Fail any op with this probability, from a seeded xorshift.
    probability: Option<(f64, u64)>,
    /// Sleep this long before each call of an op (async methods only;
    /// list streams are not delayed).
    latency: HashMap<Op, std::time::Duration>,
}

/// An `ObjectStore` decorator with counters, deterministic faults, and
/// optional simulated `LastModified`.
pub struct TestStore {
    inner: Arc<dyn ObjectStore>,
    counters: Arc<Counters>,
    faults: Arc<Mutex<Faults>>,
    clock: Option<Arc<TestClock>>,
    times: Arc<Mutex<HashMap<Path, DateTime<Utc>>>>,
}

impl TestStore {
    /// Decorate an existing store.
    pub fn new(inner: Arc<dyn ObjectStore>) -> Arc<Self> {
        Arc::new(Self {
            inner,
            counters: Arc::default(),
            faults: Arc::default(),
            clock: None,
            times: Arc::default(),
        })
    }

    /// A fresh in-memory store.
    pub fn in_memory() -> Arc<Self> {
        Self::new(Arc::new(object_store::memory::InMemory::new()))
    }

    /// A fresh in-memory store whose `LastModified` follows `clock`.
    pub fn in_memory_at(clock: Arc<TestClock>) -> Arc<Self> {
        Arc::new(Self {
            inner: Arc::new(object_store::memory::InMemory::new()),
            counters: Arc::default(),
            faults: Arc::default(),
            clock: Some(clock),
            times: Arc::default(),
        })
    }

    /// The per-operation call counters.
    pub fn counters(&self) -> &Counters {
        &self.counters
    }

    /// Fail the next `n` calls of `op`.
    pub fn fail_next(&self, op: Op, n: u64) {
        *self
            .faults
            .lock()
            .expect("faults")
            .next
            .entry(op)
            .or_default() += n;
    }

    /// Fail every call of `op` until `heal`.
    pub fn fail_all(&self, op: Op) {
        self.faults.lock().expect("faults").always.insert(op, true);
    }

    /// Fail any operation with probability `p`, deterministically from
    /// `seed`.
    pub fn fail_probability(&self, p: f64, seed: u64) {
        self.faults.lock().expect("faults").probability = Some((p, seed.max(1)));
    }

    /// Sleep `by` before every call of `op`, so tests can hold an
    /// operation in flight (e.g. a copy that outlives a pin lifetime).
    pub fn set_latency(&self, op: Op, by: std::time::Duration) {
        self.faults.lock().expect("faults").latency.insert(op, by);
    }

    /// Clear all fault injection.
    pub fn heal(&self) {
        *self.faults.lock().expect("faults") = Faults::default();
    }

    fn latency(&self, op: Op) -> Option<std::time::Duration> {
        self.faults
            .lock()
            .expect("faults")
            .latency
            .get(&op)
            .copied()
    }

    fn check(&self, op: Op) -> Result<(), object_store::Error> {
        self.counters.bump(op);
        let mut faults = self.faults.lock().expect("faults");
        if faults.always.get(&op).copied().unwrap_or(false) {
            return Err(fault(op));
        }
        if let Some(n) = faults.next.get_mut(&op)
            && *n > 0
        {
            *n -= 1;
            return Err(fault(op));
        }
        if let Some((p, seed)) = &mut faults.probability {
            // xorshift64: deterministic per seed, independent of order
            // of ops only in the sense of a fixed sequence.
            *seed ^= *seed << 13;
            *seed ^= *seed >> 7;
            *seed ^= *seed << 17;
            let roll = (*seed >> 11) as f64 / (1u64 << 53) as f64;
            if roll < *p {
                return Err(fault(op));
            }
        }
        Ok(())
    }

    fn stamp(&self, location: &Path) {
        if let Some(clock) = &self.clock {
            self.times
                .lock()
                .expect("times")
                .insert(location.clone(), clock.now());
        }
    }

    /// Override one object's simulated `LastModified`, so a single
    /// heartbeat can read as stale while the rest stay fresh. Only
    /// meaningful on stores built with a `TestClock`.
    pub fn set_modified(&self, location: &Path, time: DateTime<Utc>) {
        self.times
            .lock()
            .expect("times")
            .insert(location.clone(), time);
    }

    fn restamp(&self, mut meta: ObjectMeta) -> ObjectMeta {
        if self.clock.is_some()
            && let Some(time) = self.times.lock().expect("times").get(&meta.location)
        {
            meta.last_modified = *time;
        }
        meta
    }
}

fn fault(op: Op) -> object_store::Error {
    object_store::Error::Generic {
        store: "TestStore",
        source: format!("injected {op:?} fault").into(),
    }
}

impl fmt::Display for TestStore {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "TestStore({})", self.inner)
    }
}

impl fmt::Debug for TestStore {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "TestStore({:?})", self.inner)
    }
}

#[async_trait]
impl ObjectStore for TestStore {
    async fn put_opts(
        &self,
        location: &Path,
        payload: PutPayload,
        opts: PutOptions,
    ) -> object_store::Result<PutResult> {
        self.check(Op::Put)?;
        if let Some(by) = self.latency(Op::Put) {
            tokio::time::sleep(by).await;
        }
        let result = self.inner.put_opts(location, payload, opts).await?;
        self.stamp(location);
        Ok(result)
    }

    async fn put_multipart_opts(
        &self,
        location: &Path,
        opts: PutMultipartOptions,
    ) -> object_store::Result<Box<dyn MultipartUpload>> {
        self.check(Op::Put)?;
        let result = self.inner.put_multipart_opts(location, opts).await?;
        self.stamp(location);
        Ok(result)
    }

    async fn get_opts(
        &self,
        location: &Path,
        options: GetOptions,
    ) -> object_store::Result<GetResult> {
        self.check(Op::Get)?;
        if let Some(by) = self.latency(Op::Get) {
            tokio::time::sleep(by).await;
        }
        let mut result = self.inner.get_opts(location, options).await?;
        result.meta = self.restamp(result.meta);
        Ok(result)
    }

    async fn get_ranges(
        &self,
        location: &Path,
        ranges: &[Range<u64>],
    ) -> object_store::Result<Vec<bytes::Bytes>> {
        self.check(Op::Get)?;
        self.inner.get_ranges(location, ranges).await
    }

    fn delete_stream(
        &self,
        locations: BoxStream<'static, object_store::Result<Path>>,
    ) -> BoxStream<'static, object_store::Result<Path>> {
        if let Err(e) = self.check(Op::Delete) {
            return futures::stream::once(async move { Err(e) }).boxed();
        }
        let times = self.times.clone();
        self.inner
            .delete_stream(locations)
            .map_ok(move |path| {
                times.lock().expect("times").remove(&path);
                path
            })
            .boxed()
    }

    fn list(&self, prefix: Option<&Path>) -> BoxStream<'static, object_store::Result<ObjectMeta>> {
        if let Err(e) = self.check(Op::List) {
            return futures::stream::once(async move { Err(e) }).boxed();
        }
        let times: HashMap<Path, DateTime<Utc>> = if self.clock.is_some() {
            self.times.lock().expect("times").clone()
        } else {
            HashMap::new()
        };
        self.inner
            .list(prefix)
            .map_ok(move |mut meta| {
                if let Some(time) = times.get(&meta.location) {
                    meta.last_modified = *time;
                }
                meta
            })
            .boxed()
    }

    fn list_with_offset(
        &self,
        prefix: Option<&Path>,
        offset: &Path,
    ) -> BoxStream<'static, object_store::Result<ObjectMeta>> {
        if let Err(e) = self.check(Op::List) {
            return futures::stream::once(async move { Err(e) }).boxed();
        }
        self.inner.list_with_offset(prefix, offset)
    }

    async fn list_with_delimiter(&self, prefix: Option<&Path>) -> object_store::Result<ListResult> {
        self.check(Op::List)?;
        let mut result = self.inner.list_with_delimiter(prefix).await?;
        result.objects = result
            .objects
            .into_iter()
            .map(|meta| self.restamp(meta))
            .collect();
        Ok(result)
    }

    async fn copy_opts(
        &self,
        from: &Path,
        to: &Path,
        options: CopyOptions,
    ) -> object_store::Result<()> {
        self.check(Op::Copy)?;
        self.inner.copy_opts(from, to, options).await?;
        self.stamp(to);
        Ok(())
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use object_store::ObjectStoreExt;

    #[tokio::test]
    async fn counts_and_faults() {
        let store = TestStore::in_memory();
        let path = Path::from("x");
        store.put(&path, PutPayload::from("v")).await.unwrap();
        assert_eq!(store.counters().count(Op::Put), 1);

        store.fail_next(Op::Get, 1);
        assert!(store.get(&path).await.is_err());
        assert!(store.get(&path).await.is_ok());
        assert_eq!(store.counters().count(Op::Get), 2);

        store.fail_all(Op::List);
        assert!(store.list(None).try_collect::<Vec<_>>().await.is_err());
        store.heal();
        assert_eq!(
            store
                .list(None)
                .try_collect::<Vec<_>>()
                .await
                .unwrap()
                .len(),
            1
        );
    }

    #[tokio::test]
    async fn sim_clock_stamps_last_modified() {
        let clock = TestClock::new(Utc::now());
        let store = TestStore::in_memory_at(clock.clone());
        let path = Path::from("hb");
        store.put(&path, PutPayload::from("v")).await.unwrap();
        let t0 = clock.now();

        clock.advance(std::time::Duration::from_secs(120));
        let metas: Vec<ObjectMeta> = store.list(None).try_collect().await.unwrap();
        assert_eq!(metas[0].last_modified, t0, "age follows virtual time");

        store.put(&path, PutPayload::from("v2")).await.unwrap();
        let metas: Vec<ObjectMeta> = store.list(None).try_collect().await.unwrap();
        assert_eq!(metas[0].last_modified, clock.now());
    }
}