corium-transactor 0.1.27

Corium transactor
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
//! Offline, content-addressed database backup and restore.
//!
//! A backup directory is itself a Corium blob/root store plus a durable log
//! and a small manifest. Re-running [`backup`] against the same directory is
//! incremental: immutable blobs already present are not copied.

use std::fs;
use std::future::Future;
use std::io;
use std::path::{Path, PathBuf};
use std::pin::Pin;

use corium_log::{FileLog, LogError, TransactionLog, TxRecord, VersionedLog};
use corium_store::{
    BlobId, BlobStore, DbRoot, FORMAT_VERSION, FsStore, RootStore, StoreError, db_root_name,
};
use thiserror::Error;

const MANIFEST_NAME: &str = "manifest";
const MANIFEST_MAGIC: &str = "corium-backup-v1";

/// Counters and snapshot identity returned by a backup.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct BackupReport {
    /// Transaction basis preserved by the snapshot.
    pub basis_t: u64,
    /// Index basis named by the copied root.
    pub index_basis_t: u64,
    /// Immutable blobs newly copied during this run.
    pub copied_blobs: usize,
    /// Immutable blobs already present in an incremental destination.
    pub reused_blobs: usize,
}

/// Counters and identity returned by a restore.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct RestoreReport {
    /// Source database name recorded in the backup.
    pub source_db: String,
    /// Restored database name (which may differ for clone restores).
    pub target_db: String,
    /// Restored transaction basis.
    pub basis_t: u64,
    /// Immutable blobs newly copied into the target store.
    pub copied_blobs: usize,
    /// Immutable blobs already shared by the target store.
    pub reused_blobs: usize,
}

/// Backup or restore failure.
#[derive(Debug, Error)]
pub enum BackupError {
    /// Filesystem operation failed.
    #[error("backup I/O failed: {0}")]
    Io(#[from] io::Error),
    /// Blob/root store operation failed.
    #[error(transparent)]
    Store(#[from] StoreError),
    /// Transaction log could not be read.
    #[error(transparent)]
    Log(#[from] LogError),
    /// The requested database does not exist.
    #[error("database {0:?} does not exist")]
    MissingDatabase(String),
    /// A required root, blob, log, or manifest is malformed/missing.
    #[error("invalid backup: {0}")]
    Invalid(String),
    /// Restore refuses to replace an existing database.
    #[error("database {0:?} already exists in the target")]
    TargetExists(String),
    /// The stored format cannot be read by this binary.
    #[error("storage format {found} is newer than supported format {supported}")]
    UnsupportedFormat {
        /// Version found in the backup.
        found: u32,
        /// Newest version understood by this binary.
        supported: u32,
    },
}

#[derive(Clone, Debug, Eq, PartialEq)]
struct Manifest {
    source_db: String,
    basis_t: u64,
    index_basis_t: u64,
    format_version: u32,
}

impl Manifest {
    fn encode(&self) -> String {
        format!(
            "{MANIFEST_MAGIC}\nsource-db={}\nbasis-t={}\nindex-basis-t={}\nformat-version={}\n",
            self.source_db, self.basis_t, self.index_basis_t, self.format_version
        )
    }

    fn decode(bytes: &[u8]) -> Result<Self, BackupError> {
        let text = std::str::from_utf8(bytes)
            .map_err(|_| BackupError::Invalid("manifest is not UTF-8".into()))?;
        let mut lines = text.lines();
        if lines.next() != Some(MANIFEST_MAGIC) {
            return Err(BackupError::Invalid("unknown manifest header".into()));
        }
        let field = |line: Option<&str>, name: &str| -> Result<String, BackupError> {
            line.and_then(|line| line.strip_prefix(&format!("{name}=")))
                .map(str::to_owned)
                .ok_or_else(|| BackupError::Invalid(format!("manifest has no {name}")))
        };
        let source_db = field(lines.next(), "source-db")?;
        let basis_t = field(lines.next(), "basis-t")?
            .parse()
            .map_err(|_| BackupError::Invalid("manifest basis-t is invalid".into()))?;
        let index_basis_t = field(lines.next(), "index-basis-t")?
            .parse()
            .map_err(|_| BackupError::Invalid("manifest index-basis-t is invalid".into()))?;
        let format_version = field(lines.next(), "format-version")?
            .parse()
            .map_err(|_| BackupError::Invalid("manifest format-version is invalid".into()))?;
        if format_version > FORMAT_VERSION {
            return Err(BackupError::UnsupportedFormat {
                found: format_version,
                supported: FORMAT_VERSION,
            });
        }
        Ok(Self {
            source_db,
            basis_t,
            index_basis_t,
            format_version,
        })
    }
}

fn valid_db_name(name: &str) -> bool {
    !name.is_empty()
        && name.len() <= 128
        && name
            .bytes()
            .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-' || byte == b'_')
}

fn meta_root_name(db: &str) -> String {
    format!("meta:{db}")
}

fn log_path(data_dir: &Path, db: &str) -> PathBuf {
    data_dir.join("logs").join(format!("{db}.log"))
}

async fn put_root(store: &dyn RootStore, name: &str, bytes: &[u8]) -> Result<(), StoreError> {
    loop {
        let previous = store.get_root(name).await?;
        if previous.as_deref() == Some(bytes) {
            return Ok(());
        }
        match store.cas_root(name, previous.as_deref(), bytes).await {
            Ok(()) => return Ok(()),
            Err(StoreError::CasFailed { .. }) => {}
            Err(error) => return Err(error),
        }
    }
}

fn copy_file_atomically(source: &Path, target: &Path) -> Result<(), io::Error> {
    if let Some(parent) = target.parent() {
        fs::create_dir_all(parent)?;
    }
    let temporary = target.with_extension("tmp");
    fs::copy(source, &temporary)?;
    fs::rename(temporary, target)
}

/// Writes `records` as a single consolidated log file, atomically.
fn write_log_atomically(records: &[TxRecord], target: &Path) -> Result<(), BackupError> {
    if let Some(parent) = target.parent() {
        fs::create_dir_all(parent)?;
    }
    let temporary = target.with_extension("tmp");
    match fs::remove_file(&temporary) {
        Ok(()) => {}
        Err(error) if error.kind() == io::ErrorKind::NotFound => {}
        Err(error) => return Err(error.into()),
    }
    let log = FileLog::open(&temporary)?;
    for record in records {
        log.append(record)?;
    }
    fs::rename(temporary, target)?;
    Ok(())
}

async fn copy_root_blobs(
    source: &dyn BlobStore,
    target: &dyn BlobStore,
    root: &DbRoot,
) -> Result<(usize, usize), BackupError> {
    let mut copied = 0;
    let mut reused = 0;
    let mut seen = std::collections::HashSet::new();
    for id in root.roots.iter().flatten() {
        copy_blob_tree(source, target, id, &mut seen, &mut copied, &mut reused).await?;
    }
    Ok((copied, reused))
}

/// Copies one blob and everything it references, children first.
///
/// Child references are decoded with the same helper GC's mark phase uses
/// (`index_blob_children`), so backup and reachability cannot diverge.
/// Post-order preserves the store invariant that a present parent's
/// children are present too — an interrupted run never leaves a manifest
/// whose chunks a later incremental backup would wrongly skip — and lets a
/// blob already in the target count as reused without re-reading its tree.
fn copy_blob_tree<'a>(
    source: &'a dyn BlobStore,
    target: &'a dyn BlobStore,
    id: &'a BlobId,
    seen: &'a mut std::collections::HashSet<BlobId>,
    copied: &'a mut usize,
    reused: &'a mut usize,
) -> Pin<Box<dyn Future<Output = Result<(), BackupError>> + Send + 'a>> {
    Box::pin(async move {
        if !seen.insert(id.clone()) {
            return Ok(());
        }
        if target.contains(id).await? {
            *reused += 1;
            return Ok(());
        }
        let bytes = source
            .get(id)
            .await?
            .ok_or_else(|| BackupError::Invalid(format!("root references missing blob {id}")))?;
        for child in corium_store::index_blob_children(&bytes)? {
            copy_blob_tree(source, target, &child, seen, copied, reused).await?;
        }
        let copied_id = target.put(&bytes).await?;
        if copied_id != *id {
            return Err(BackupError::Invalid(format!(
                "blob digest changed while copying {id}"
            )));
        }
        *copied += 1;
        Ok(())
    })
}

/// Copies one offline database into `destination`.
///
/// `source_data_dir` is a transactor data directory. The transactor must be
/// stopped so the root, metadata, and log form one stable snapshot. Reusing a
/// destination performs an incremental backup and copies only absent blobs.
///
/// # Errors
/// Returns an error if the database is missing, the snapshot is inconsistent,
/// or files cannot be read/written.
pub async fn backup(
    source_data_dir: impl AsRef<Path>,
    db: &str,
    destination: impl AsRef<Path>,
) -> Result<BackupReport, BackupError> {
    if !valid_db_name(db) {
        return Err(BackupError::MissingDatabase(db.to_owned()));
    }
    let source_data_dir = source_data_dir.as_ref();
    let destination = destination.as_ref();
    let source = FsStore::open(source_data_dir.join("store"))?;
    let db_name = db_root_name(db);
    let meta_name = meta_root_name(db);
    let db_bytes = source
        .get_root(&db_name)
        .await?
        .ok_or_else(|| BackupError::MissingDatabase(db.to_owned()))?;
    let meta = source
        .get_root(&meta_name)
        .await?
        .ok_or_else(|| BackupError::MissingDatabase(db.to_owned()))?;
    let root = DbRoot::decode(&db_bytes)
        .ok_or_else(|| BackupError::Invalid("database root cannot be decoded".into()))?;
    if root.format_version > FORMAT_VERSION {
        return Err(BackupError::UnsupportedFormat {
            found: root.format_version,
            supported: FORMAT_VERSION,
        });
    }
    let source_logs = source_data_dir.join("logs");
    if !VersionedLog::exists(&source_logs, db) {
        return Err(BackupError::Invalid("transaction log is missing".into()));
    }
    // Consolidate the (possibly lease-versioned) log into one clean file:
    // the merged read applies the HA takeover cutoffs, so the backup never
    // carries a deposed writer's stale appends.
    let records = VersionedLog::open_read_only(&source_logs, db)?.replay()?;
    let basis_t = records.last().map_or(0, |record| record.t);
    if root.index_basis_t > basis_t {
        return Err(BackupError::Invalid(format!(
            "index basis {} is ahead of log basis {basis_t}",
            root.index_basis_t
        )));
    }

    fs::create_dir_all(destination)?;
    let target = FsStore::open(destination.join("store"))?;
    let (copied_blobs, reused_blobs) = copy_root_blobs(&source, &target, &root).await?;
    put_root(&target, &db_name, &db_bytes).await?;
    put_root(&target, &meta_name, &meta).await?;
    write_log_atomically(&records, &log_path(destination, db))?;
    let manifest = Manifest {
        source_db: db.to_owned(),
        basis_t,
        index_basis_t: root.index_basis_t,
        format_version: root.format_version,
    };
    let manifest_tmp = destination.join("manifest.tmp");
    fs::write(&manifest_tmp, manifest.encode())?;
    fs::rename(manifest_tmp, destination.join(MANIFEST_NAME))?;
    Ok(BackupReport {
        basis_t,
        index_basis_t: root.index_basis_t,
        copied_blobs,
        reused_blobs,
    })
}

/// Restores a backup under `target_db`, allowing restore-as-clone by choosing
/// a name different from the manifest's source database.
///
/// Existing target databases are never overwritten. Immutable blobs already
/// present in the target store are shared rather than recopied.
///
/// # Errors
/// Returns an error for malformed/incomplete backups, unsupported formats,
/// existing targets, or I/O failures.
pub async fn restore(
    source: impl AsRef<Path>,
    target_data_dir: impl AsRef<Path>,
    target_db: &str,
) -> Result<RestoreReport, BackupError> {
    if !valid_db_name(target_db) {
        return Err(BackupError::Invalid(format!(
            "invalid target database name {target_db:?}"
        )));
    }
    let source = source.as_ref();
    let target_data_dir = target_data_dir.as_ref();
    let manifest = Manifest::decode(&fs::read(source.join(MANIFEST_NAME))?)?;
    let source_store = FsStore::open(source.join("store"))?;
    let source_db_name = db_root_name(&manifest.source_db);
    let source_meta_name = meta_root_name(&manifest.source_db);
    let db_bytes = source_store
        .get_root(&source_db_name)
        .await?
        .ok_or_else(|| BackupError::Invalid("database root is missing".into()))?;
    let meta = source_store
        .get_root(&source_meta_name)
        .await?
        .ok_or_else(|| BackupError::Invalid("metadata root is missing".into()))?;
    let root = DbRoot::decode(&db_bytes)
        .ok_or_else(|| BackupError::Invalid("database root cannot be decoded".into()))?;
    if root.index_basis_t != manifest.index_basis_t
        || root.format_version != manifest.format_version
    {
        return Err(BackupError::Invalid(
            "manifest does not match the database root".into(),
        ));
    }
    let source_log = log_path(source, &manifest.source_db);
    if !source_log.is_file() {
        return Err(BackupError::Invalid("transaction log is missing".into()));
    }
    let log = FileLog::open(&source_log)?;
    let actual_basis = log.replay()?.last().map_or(0, |record| record.t);
    if actual_basis != manifest.basis_t {
        return Err(BackupError::Invalid(format!(
            "manifest basis {} does not match log basis {actual_basis}",
            manifest.basis_t
        )));
    }

    let target_store = FsStore::open(target_data_dir.join("store"))?;
    let target_db_name = db_root_name(target_db);
    let target_meta_name = meta_root_name(target_db);
    if target_store.get_root(&target_db_name).await?.is_some()
        || target_store.get_root(&target_meta_name).await?.is_some()
        || log_path(target_data_dir, target_db).exists()
    {
        return Err(BackupError::TargetExists(target_db.to_owned()));
    }
    let (copied_blobs, reused_blobs) = copy_root_blobs(&source_store, &target_store, &root).await?;
    let target_log = log_path(target_data_dir, target_db);
    copy_file_atomically(&source_log, &target_log)?;
    // Metadata is the catalog entry, so publish it last. A node can never
    // discover a partially restored database.
    let publish = match target_store
        .cas_root(&target_db_name, None, &db_bytes)
        .await
    {
        Ok(()) => target_store.cas_root(&target_meta_name, None, &meta).await,
        Err(error) => Err(error),
    };
    if let Err(error) = publish {
        let _ = target_store.delete_root(&target_db_name).await;
        let _ = target_store.delete_root(&target_meta_name).await;
        let _ = fs::remove_file(&target_log);
        return Err(error.into());
    }
    Ok(RestoreReport {
        source_db: manifest.source_db,
        target_db: target_db.to_owned(),
        basis_t: manifest.basis_t,
        copied_blobs,
        reused_blobs,
    })
}