Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/migration-progress-output.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
121 changes: 118 additions & 3 deletions crates/nostream-cli/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,17 @@ pub async fn migrate_with(
let mut sum = RowSum::from_hex(&entry.checksum)?;
let mut after = decode_key(&entry.after)?;

// D13: what a resumed run continues from, printed before this table's first read.
if entry.records_progress() {
writeln!(
out,
"Resuming {} from {rows}/{source_rows} rows, after {}",
table.name(),
format_cursor(table, after.as_deref())
)
.map_err(write_failed)?;
}

loop {
let batch = source
.read_batch(table, after.as_deref(), options.batch_size)
Expand Down Expand Up @@ -275,6 +286,17 @@ pub async fn migrate_with(
entry.complete = complete;
write_manifest(root, &options.target, &manifest).map_err(as_resumable)?;

// D13: printed after the manifest write, so the numbers are the durable entry's.
if reports_progress(rows, written, complete) {
writeln!(
out,
"{}: {rows}/{source_rows} rows, after {}",
table.name(),
format_cursor(table, after.as_deref())
)
.map_err(write_failed)?;
}

if complete {
break;
}
Expand All @@ -296,6 +318,11 @@ pub async fn migrate_with(
)
.map_err(write_failed)?;

// D13: the migration's own total, the sum its entries carry and a resume never resets,
// beside this run's — which the `Migrated` line above already states.
let migrated: u64 = manifest.tables.values().map(|entry| entry.rows).sum();
writeln!(out, "Migration total: {migrated} rows ({total} this run)").map_err(write_failed)?;

Ok(CommandOutput::default())
}

Expand Down Expand Up @@ -423,9 +450,7 @@ impl Manifest {
/// so a fresh store beside it is honest; one that records progress is only meaningful
/// beside the store the progress went into.
fn records_progress(&self) -> bool {
self.tables
.values()
.any(|entry| entry.rows != 0 || !entry.after.is_empty() || entry.complete)
self.tables.values().any(TableEntry::records_progress)
}

/// This table's entry, as the empty one when the manifest carries none.
Expand Down Expand Up @@ -465,6 +490,15 @@ struct TableEntry {
complete: bool,
}

impl TableEntry {
/// Whether this entry records that a run did anything: a row written, a cursor moved, or the
/// table finished. The resume line asks it too, where the entry is incomplete by the skip
/// above it, so the two readings cannot drift.
fn records_progress(&self) -> bool {
self.rows != 0 || !self.after.is_empty() || self.complete
}
}

impl Default for TableEntry {
fn default() -> Self {
Self {
Expand Down Expand Up @@ -1275,10 +1309,91 @@ fn write_failed(error: std::io::Error) -> CliError {
CliError::Migrate(format!("unable to write the migration's output: {error}"))
}

/// The rows a table's progress line is throttled by: at the default batch of 1 000 that is one
/// line per 100 batches. A 10 000 000-row table at that batch prints 102 lines — its first
/// batch, 100 crossings of the stride and the read that completes it — where one line per batch
/// would print 10 001; the completing read carries the last full batch's numbers, so the final
/// two lines are identical.
const PROGRESS_STRIDE: u64 = 100_000;

/// Whether a batch earns a progress line: a table's first batch, one that carries its row count
/// across a stride boundary, and the read that completes it — which is also the one read an
/// empty table makes.
fn reports_progress(rows: u64, written: u64, complete: bool) -> bool {
let crossed = rows / PROGRESS_STRIDE > (rows - written) / PROGRESS_STRIDE;
complete || (written > 0 && (rows == written || crossed))
}

/// A cursor as a progress line prints it: the manifest's hexadecimal spelling, `none` before the
/// table's first batch, and `redacted` for a table whose key is a credential — `invite_codes`,
/// whose key is the invite code itself (ADR 0013, D5), as `MigratedTable::key_is_credential`
/// decides beside `name` and `key`.
fn format_cursor(table: MigratedTable, after: Option<&[u8]>) -> String {
match after {
None => "none".to_owned(),
Some(_) if table.key_is_credential() => "redacted".to_owned(),
Some(key) => hex::encode(key),
}
}

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

/// The four cases the copy's progress line is printed for, and the volume the stride is
/// chosen for: a 10 000 000-row table at the default batch of 1 000 prints one line for its
/// first batch, one per 100 000 rows, and one for the read that completes it — 102, not one
/// per batch.
#[test]
fn a_progress_line_is_reported_at_each_stride_boundary() {
assert!(reports_progress(0, 0, true), "an empty table reports");
assert!(
!reports_progress(0, 0, false),
"a read that wrote nothing does not"
);
assert!(
reports_progress(1, 1, false),
"a table's first batch reports"
);
assert!(
!reports_progress(2, 1, false),
"a batch inside the stride does not"
);
assert!(!reports_progress(PROGRESS_STRIDE - 1, 1, false));
assert!(
reports_progress(PROGRESS_STRIDE, 1, false),
"the crossing reports"
);
assert!(
reports_progress(PROGRESS_STRIDE + 500, 1_000, false),
"a batch that overshoots a boundary reports"
);
assert!(
!reports_progress(PROGRESS_STRIDE + 1, 1, false),
"a batch just past a boundary does not"
);
assert!(reports_progress(2 * PROGRESS_STRIDE, 1_000, false));
assert!(reports_progress(1, 1, true), "the completing read reports");

let mut reported = 0;
let mut rows = 0u64;
for _ in 0..10_000 {
rows += 1_000;
if reports_progress(rows, 1_000, false) {
reported += 1;
}
}
assert_eq!(
reported, 101,
"the first batch plus one line per stride crossing"
);
assert_eq!(rows, 10_000_000);
assert!(
reports_progress(rows, 0, true),
"the completing read is the 102nd line, carrying the last full batch's rows"
);
}

/// A link where the data file belongs is followed by `is_file()` and by LMDB both, so the
/// copy would read or write through it and heed would put `lock.mdb` beside the link's
/// target. The predicate over the file's own metadata is what AC18 asks for.
Expand Down
Loading
Loading