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-source-rows.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
4 changes: 3 additions & 1 deletion crates/nostream-cli/src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,6 +696,8 @@ enum MigrateCommand {
/// rows and no manifest is refused, so starting over takes a fresh `--target`, or the
/// manifest and the store removed together. Nothing may write to either store while a run
/// is in progress, so the relay must be stopped on both before the first batch is read.
///
/// It reads the store only and proves the copy read every source row the counts recorded
Comment on lines +699 to +700

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Copy help claims read-only behavior

This paragraph belongs to PgToLmdb, so the copy command's help now says “It reads the store only.” The copy actually reads Postgres and writes LMDB. Move this explanation to verify and finish the sentence, which currently ends at “recorded.” Otherwise operators get the wrong description of what the command changes.

PgToLmdb {
/// The store root (the environment lives under `<DIR>/lmdb`).
#[arg(long, value_name = "DIR")]
Expand All @@ -708,7 +710,7 @@ enum MigrateCommand {
value_parser = clap::builder::RangedU64ValueParser::<usize>::new().range(1..)
)]
batch_size: usize,
/// Verify the store against its manifest and copy nothing.
/// Verify the store against the manifest and recorded source counts.
#[arg(long)]
verify: bool,
},
Expand Down
33 changes: 28 additions & 5 deletions crates/nostream-cli/src/commands/migrate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crate::error::CliError;
const MANIFEST_FILE: &str = "migration-manifest.json";

/// The manifest format this tool writes.
const MANIFEST_FORMAT: u32 = 1;
const MANIFEST_FORMAT: u32 = 2;

/// The options one run carries.
#[derive(Debug, Clone, PartialEq, Eq)]
Expand All @@ -40,7 +40,7 @@ pub struct MigrateOptions {
pub target: PathBuf,
/// Rows per batch, which is also the page size verify reads with.
pub batch_size: usize,
/// Verify the target against its manifest and copy nothing.
/// Verify the store against the manifest and its recorded source counts, and copy nothing.
pub verify: bool,
}

Expand Down Expand Up @@ -232,6 +232,12 @@ pub async fn migrate_with(
if entry.complete {
continue;
}
// The source's own count, asked once before this table's first page: what verify
// requires the target's row count to equal.
let source_rows = source
.count_rows(table)
.await
.map_err(|error| as_resumable(table_failed(table, &error)))?;
let mut rows = entry.rows;
let mut sum = RowSum::from_hex(&entry.checksum)?;
let mut after = decode_key(&entry.after)?;
Expand Down Expand Up @@ -263,6 +269,7 @@ pub async fn migrate_with(
let complete = count < options.batch_size;
let entry = manifest.entry(table);
entry.rows = rows;
entry.source_rows = source_rows;
entry.checksum = sum.to_hex();
entry.after = after.as_deref().map(hex::encode).unwrap_or_default();
entry.complete = complete;
Expand Down Expand Up @@ -296,7 +303,8 @@ pub async fn migrate_with(
/// the nine entries with the manifest.
///
/// Every table is paged to its end, so a table longer than one page is read rather than cut at
/// its first. The pass writes nothing to the store and never opens the source.
/// its first. The pass writes nothing to the store and never opens the source; it proves the
/// store matches the manifest, and that the copy read every source row the manifest recorded.
///
/// `root` is the path the store and the manifest are read through: the target's own descriptor
/// spelling where procfs answers, and the operator's path otherwise.
Expand Down Expand Up @@ -354,13 +362,20 @@ pub async fn verify_with(
));
} else if !entry.complete {
failures.push(format!("[{}] the manifest is incomplete", table.name()));
} else if rows != entry.source_rows {
failures.push(format!(
"[{}] source {} rows, target {rows} rows",
table.name(),
entry.source_rows
));
}
}

if failures.is_empty() {
writeln!(
out,
"Verified {} tables, {total} rows",
"Verified {} tables, {total} rows: the store matches the manifest, and the copy read \
every source row",
MigratedTable::ALL.len()
)
.map_err(write_failed)?;
Expand Down Expand Up @@ -436,6 +451,12 @@ impl Manifest {
struct TableEntry {
/// The rows the table's batches have written.
rows: u64,
/// The rows the source held when the copy first asked, its own `count(*)`.
///
/// Defaulted so a manifest of the previous shape parses and is refused by its format
/// number rather than by a serde `missing field` error.
#[serde(default)]
source_rows: u64,
/// The hex of the table's running row sum; sixty-four `0` characters when it is empty.
checksum: String,
/// The hex of the last written key; empty before the first batch.
Expand All @@ -448,6 +469,7 @@ impl Default for TableEntry {
fn default() -> Self {
Self {
rows: 0,
source_rows: 0,
checksum: RowSum::default().to_hex(),
after: String::new(),
complete: false,
Expand Down Expand Up @@ -575,7 +597,8 @@ fn read_manifest(root: &Path, target: &Path) -> Result<Option<Manifest>, CliErro
.map_err(|error| CliError::Migrate(shown(format!("{}: {error}", path.display()))))?;
if manifest.format != MANIFEST_FORMAT {
return Err(CliError::Migrate(shown(format!(
"{}: manifest format {} is not supported; this tool reads and writes {MANIFEST_FORMAT}",
"{}: manifest format {} is not supported; this tool reads and writes \
{MANIFEST_FORMAT}; restart on a fresh --target",
path.display(),
manifest.format
))));
Expand Down
2 changes: 1 addition & 1 deletion crates/nostream-cli/src/commands/migrate_unsupported.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub struct MigrateOptions {
pub target: PathBuf,
/// Rows per batch, which is also the page size verify reads with.
pub batch_size: usize,
/// Verify the target against its manifest and copy nothing.
/// Verify the store against the manifest and its recorded source counts, and copy nothing.
pub verify: bool,
}

Expand Down
Loading
Loading