slatedb-mirror 0.18.1

Local whole-object mirror for an object store.
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
//! The mirror's on-disk file layout.
//!
//! The cache root is flat. Every object-related file name starts with the hex
//! MD5 of the object's parent path, then a `.`, then the object's file name:
//!
//! - `<md5>.<name>.obj` is the complete object.
//! - `<md5>.<name>.obj.meta` holds its canonical path and metadata.
//! - `<md5>.<name>.obj.<counter>` and `<md5>.<name>.obj.meta.<counter>` are
//!   temporary files. The counter is unique within the mirror.
//! - `LOCK` is held for the mirror's lifetime.
//!
//! The suffix after `<name>` always belongs to the mirror, so any object file
//! name maps to distinct local names. Object `foo.meta` is
//! `<md5>.foo.meta.obj`, while the metadata of object `foo` is
//! `<md5>.foo.obj.meta`.

use std::collections::{BTreeMap, HashMap};

use chrono::{DateTime, Utc};
use md5::{Digest, Md5};
use object_store::path::Path;
use object_store::{Attribute, AttributeValue, Attributes, ObjectMeta};
use serde::{Deserialize, Serialize};

use crate::MirrorError;

/// The lock file held for the mirror's lifetime.
pub(crate) const LOCK_FILE: &str = "LOCK";

const OBJECT_SUFFIX: &str = ".obj";

const META_SUFFIX: &str = ".meta";

/// Length of a hex MD5 digest.
const PREFIX_LEN: usize = 32;

/// The `.meta` format written by this version.
const META_FORMAT: u32 = 1;

/// Splits an object path into its parent path and file name. Objects at the
/// root have an empty parent.
pub(crate) fn split_path(path: &Path) -> (&str, &str) {
    path.as_ref()
        .rsplit_once('/')
        .unwrap_or(("", path.as_ref()))
}

/// Strips a trailing `.` followed by one or more ASCII digits, or returns
/// `None` if `name` doesn't end in one.
#[inline]
fn strip_temp_suffix(name: &str) -> Option<&str> {
    let (stem, suffix) = name.rsplit_once('.')?;
    (!suffix.is_empty() && suffix.bytes().all(|b| b.is_ascii_digit())).then_some(stem)
}

/// The local file names for one object path.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct LocalName {
    /// Hex MD5 of `parent`.
    pub(crate) prefix: String,
    /// The object's parent path.
    pub(crate) parent: String,
    /// `<prefix>.<file name>.obj`, the complete object.
    pub(crate) object_file_name: String,
}

impl LocalName {
    /// Returns the local names for `path`.
    pub(crate) fn new(path: &Path) -> Self {
        let (parent, name) = split_path(path);
        let prefix = hex::encode(Md5::digest(parent.as_bytes()));
        let object_file_name = format!("{prefix}.{name}{OBJECT_SUFFIX}");
        Self {
            prefix,
            parent: parent.to_string(),
            object_file_name,
        }
    }

    /// `<prefix>.<file name>.obj.meta`
    pub(crate) fn meta(&self) -> String {
        format!("{}{META_SUFFIX}", self.object_file_name)
    }

    /// `<prefix>.<file name>.obj.<counter>`
    pub(crate) fn temp(&self, counter: u64) -> String {
        format!("{}.{counter}", self.object_file_name)
    }

    /// `<prefix>.<file name>.obj.meta.<counter>`
    pub(crate) fn meta_temp(&self, counter: u64) -> String {
        format!("{}{META_SUFFIX}.{counter}", self.object_file_name)
    }
}

/// What a file in the cache root is, judging by its name alone.
#[derive(Debug, PartialEq, Eq)]
pub(crate) enum FileKind<'a> {
    Lock,
    /// A leftover temporary file.
    Temp,
    /// A `.meta` file for the local object file named `object_file_name`.
    Meta {
        object_file_name: &'a str,
    },
    /// A complete object with MD5 prefix `prefix` and object file name `name`.
    Object {
        prefix: &'a str,
        name: &'a str,
    },
    /// Not part of the layout. Left alone.
    Unknown,
}

pub(crate) fn classify(name: &str) -> FileKind<'_> {
    if name == LOCK_FILE {
        return FileKind::Lock;
    }
    let has_prefix = name.len() > PREFIX_LEN
        && name.as_bytes()[PREFIX_LEN] == b'.'
        && name.as_bytes()[..PREFIX_LEN]
            .iter()
            .all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(b));
    if !has_prefix {
        return FileKind::Unknown;
    }
    // The prefix is ASCII, so these slices are on character boundaries.
    let prefix = &name[..PREFIX_LEN];
    let rest = &name[PREFIX_LEN + 1..];
    if let Some(stem) = strip_temp_suffix(rest) {
        let stem = stem.strip_suffix(META_SUFFIX).unwrap_or(stem);
        if stem.ends_with(OBJECT_SUFFIX) {
            FileKind::Temp
        } else {
            FileKind::Unknown
        }
    } else if let Some(object_name) = rest.strip_suffix(OBJECT_SUFFIX) {
        FileKind::Object {
            prefix,
            name: object_name,
        }
    } else if rest
        .strip_suffix(META_SUFFIX)
        .is_some_and(|stem| stem.ends_with(OBJECT_SUFFIX))
    {
        FileKind::Meta {
            object_file_name: &name[..name.len() - META_SUFFIX.len()],
        }
    } else {
        FileKind::Unknown
    }
}

/// A complete local copy's metadata, as returned by HEAD and GET.
#[derive(Debug, Clone)]
pub(crate) struct LocalObject {
    pub(crate) meta: ObjectMeta,
    pub(crate) attributes: Attributes,
}

/// The JSON contents of a `.meta` file.
#[derive(Debug, Serialize, Deserialize)]
struct MetaFile {
    format: u32,
    location: String,
    last_modified: DateTime<Utc>,
    size: u64,
    e_tag: Option<String>,
    version: Option<String>,
    /// Standard attributes, keyed by HTTP header name.
    #[serde(default)]
    attributes: BTreeMap<String, String>,
    /// User metadata attributes.
    #[serde(default)]
    metadata: BTreeMap<String, String>,
}

fn attribute_name(attribute: &Attribute) -> Option<&'static str> {
    Some(match attribute {
        Attribute::CacheControl => "Cache-Control",
        Attribute::ContentDisposition => "Content-Disposition",
        Attribute::ContentEncoding => "Content-Encoding",
        Attribute::ContentLanguage => "Content-Language",
        Attribute::ContentType => "Content-Type",
        Attribute::StorageClass => "Storage-Class",
        _ => return None,
    })
}

fn attribute_from_name(name: &str) -> Option<Attribute> {
    Some(match name {
        "Cache-Control" => Attribute::CacheControl,
        "Content-Disposition" => Attribute::ContentDisposition,
        "Content-Encoding" => Attribute::ContentEncoding,
        "Content-Language" => Attribute::ContentLanguage,
        "Content-Type" => Attribute::ContentType,
        "Storage-Class" => Attribute::StorageClass,
        _ => return None,
    })
}

impl LocalObject {
    /// Serializes this object's `.meta` file.
    pub(crate) fn to_meta_bytes(&self) -> Result<Vec<u8>, MirrorError> {
        let mut attributes = BTreeMap::new();
        let mut metadata = BTreeMap::new();
        for (key, value) in self.attributes.iter() {
            match key {
                Attribute::Metadata(key) => {
                    metadata.insert(key.to_string(), value.to_string());
                }
                key => {
                    if let Some(name) = attribute_name(key) {
                        attributes.insert(name.to_string(), value.to_string());
                    }
                }
            }
        }
        let file = MetaFile {
            format: META_FORMAT,
            location: self.meta.location.to_string(),
            last_modified: self.meta.last_modified,
            size: self.meta.size,
            e_tag: self.meta.e_tag.clone(),
            version: self.meta.version.clone(),
            attributes,
            metadata,
        };
        serde_json::to_vec(&file).map_err(|err| MirrorError::Local {
            source: std::io::Error::other(err),
        })
    }

    /// Parses a `.meta` file. Unknown standard attributes are dropped.
    pub(crate) fn from_meta_bytes(bytes: &[u8]) -> Result<Self, MirrorError> {
        let file: MetaFile = serde_json::from_slice(bytes).map_err(|err| MirrorError::Local {
            source: std::io::Error::new(std::io::ErrorKind::InvalidData, err),
        })?;
        if file.format != META_FORMAT {
            return Err(MirrorError::Local {
                source: std::io::Error::new(
                    std::io::ErrorKind::InvalidData,
                    format!("unknown format {}", file.format),
                ),
            });
        }
        let location = Path::parse(&file.location).map_err(|err| MirrorError::Local {
            source: std::io::Error::new(std::io::ErrorKind::InvalidData, err),
        })?;
        let mut attributes = Attributes::new();
        for (name, value) in file.attributes {
            if let Some(attribute) = attribute_from_name(&name) {
                attributes.insert(attribute, AttributeValue::from(value));
            }
        }
        for (key, value) in file.metadata {
            attributes.insert(Attribute::Metadata(key.into()), AttributeValue::from(value));
        }
        Ok(Self {
            meta: ObjectMeta {
                location,
                last_modified: file.last_modified,
                size: file.size,
                e_tag: file.e_tag,
                version: file.version,
            },
            attributes,
        })
    }
}

/// Maps each MD5 prefix to the parent path it stands for.
///
/// A different parent with the same prefix is an MD5 collision. The mirror
/// rejects it rather than mixing two directories' files.
#[derive(Debug, Default)]
pub(crate) struct PrefixMap {
    parents: HashMap<String, String>,
}

impl PrefixMap {
    /// Records `name`'s prefix, or fails if it already maps to another parent.
    pub(crate) fn check_or_insert(&mut self, name: &LocalName) -> Result<(), MirrorError> {
        match self.parents.get(&name.prefix) {
            Some(parent) if *parent == name.parent => Ok(()),
            Some(parent) => Err(MirrorError::Local {
                source: std::io::Error::other(format!(
                    "MD5 prefix `{}` already maps to parent `{parent}`, not `{}`",
                    name.prefix, name.parent
                )),
            }),
            None => {
                self.parents
                    .insert(name.prefix.clone(), name.parent.clone());
                Ok(())
            }
        }
    }

    /// Returns the parent path `prefix` stands for, if it's known.
    pub(crate) fn parent(&self, prefix: &str) -> Option<&str> {
        self.parents.get(prefix).map(String::as_str)
    }
}

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

    #[test]
    fn should_name_files_by_parent_md5() {
        let name = LocalName::new(&Path::from("path/to/db/compacted/01K.sst"));
        assert_eq!(
            name.prefix,
            hex::encode(Md5::digest(b"path/to/db/compacted"))
        );
        assert_eq!(name.parent, "path/to/db/compacted");
        assert_eq!(
            name.object_file_name,
            format!("{}.01K.sst.obj", name.prefix)
        );
        assert_eq!(name.meta(), format!("{}.01K.sst.obj.meta", name.prefix));
        assert_eq!(name.temp(7), format!("{}.01K.sst.obj.7", name.prefix));
        assert_eq!(
            name.meta_temp(7),
            format!("{}.01K.sst.obj.meta.7", name.prefix)
        );

        let root = LocalName::new(&Path::from("01K.sst"));
        assert_eq!(root.parent, "");
        assert_eq!(root.prefix, "d41d8cd98f00b204e9800998ecf8427e");
    }

    #[test]
    fn should_classify_files_for_any_object_name() {
        for path in [
            "a/b.sst",
            "a/b.meta",
            "a/b.123",
            "a/7",
            "a/meta",
            "a/obj",
            "a/b.obj",
            "a/b.obj.meta",
            "a/b.obj.7",
            "a/.obj",
            "meta.1",
            "",
        ] {
            let path = Path::from(path);
            let name = LocalName::new(&path);
            let (_, file_name) = split_path(&path);
            assert_eq!(
                classify(&name.object_file_name),
                FileKind::Object {
                    prefix: &name.prefix,
                    name: file_name,
                },
                "{path}"
            );
            assert_eq!(
                classify(&name.meta()),
                FileKind::Meta {
                    object_file_name: &name.object_file_name
                },
                "{path}"
            );
            assert_eq!(classify(&name.temp(3)), FileKind::Temp, "{path}");
            assert_eq!(classify(&name.meta_temp(3)), FileKind::Temp, "{path}");
        }
    }

    #[test]
    fn should_not_collide_with_metadata_or_temp_files() {
        let foo = LocalName::new(&Path::from("a/foo"));
        let foo_meta = LocalName::new(&Path::from("a/foo.meta"));
        let foo_7 = LocalName::new(&Path::from("a/foo.7"));
        assert_ne!(foo_meta.object_file_name, foo.meta());
        assert_ne!(foo_7.object_file_name, foo.temp(7));
    }

    #[test]
    fn should_classify_unknown_files() {
        let name = LocalName::new(&Path::from("a/b.sst"));
        assert_eq!(classify("LOCK"), FileKind::Lock);
        assert_eq!(classify("notes.txt"), FileKind::Unknown);
        assert_eq!(
            classify("D41D8CD98F00B204E9800998ECF8427E.b.sst.obj"),
            FileKind::Unknown
        );
        for suffix in [
            "",
            ".",
            ".b.sst",
            ".b.sst.meta",
            ".b.sst.7",
            ".b.sst.meta.7",
            ".obj",
            ".obj.meta",
            ".b.obj.x",
            ".b.obj.meta.meta",
        ] {
            let file = format!("{}{suffix}", name.prefix);
            assert_eq!(classify(&file), FileKind::Unknown, "{file}");
        }
    }

    #[test]
    fn should_round_trip_meta_files() {
        let mut attributes = Attributes::new();
        attributes.insert(Attribute::ContentType, "application/octet-stream".into());
        attributes.insert(Attribute::StorageClass, "STANDARD".into());
        attributes.insert(Attribute::Metadata("Content-Type".into()), "user".into());
        let object = LocalObject {
            meta: ObjectMeta {
                location: Path::from("a/b.sst"),
                last_modified: DateTime::from_timestamp(1_700_000_000, 123).unwrap(),
                size: 42,
                e_tag: Some("\"etag\"".to_string()),
                version: None,
            },
            attributes,
        };

        let parsed = LocalObject::from_meta_bytes(&object.to_meta_bytes().unwrap()).unwrap();
        assert_eq!(parsed.meta, object.meta);
        assert_eq!(parsed.attributes, object.attributes);
    }

    #[test]
    fn should_reject_malformed_meta_files() {
        let wrong_format = br#"{"format":2,"location":"a","last_modified":"2024-01-01T00:00:00Z","size":1,"e_tag":null,"version":null}"#;
        let bad_path = br#"{"format":1,"location":"a//b","last_modified":"2024-01-01T00:00:00Z","size":1,"e_tag":null,"version":null}"#;
        for bytes in [b"".as_slice(), b"{}".as_slice(), wrong_format, bad_path] {
            let err = LocalObject::from_meta_bytes(bytes).unwrap_err();
            let MirrorError::Local { source } = err else {
                panic!("expected a local mirror error, got {err:?}");
            };
            assert_eq!(source.kind(), std::io::ErrorKind::InvalidData);
            if bytes == bad_path {
                assert!(source.get_ref().unwrap().is::<object_store::path::Error>());
            }
        }
    }

    #[test]
    fn should_reject_conflicting_prefixes() {
        let mut prefixes = PrefixMap::default();
        let name = LocalName::new(&Path::from("a/b.sst"));
        prefixes.check_or_insert(&name).unwrap();
        prefixes.check_or_insert(&name).unwrap();

        let collision = LocalName {
            parent: "other".to_string(),
            ..name
        };
        assert!(matches!(
            prefixes.check_or_insert(&collision),
            Err(MirrorError::Local { .. })
        ));
    }
}