Skip to content

perf(storage-cassandra): the legacy restore copy writes through put_restored_item_at instead of the LWT path - #527

Open
LeeroyHannigan wants to merge 8 commits into
mainfrom
fix/404-cassandra-restore-copy
Open

LeeroyHannigan wants to merge 8 commits into
mainfrom
fix/404-cassandra-restore-copy

Conversation

@LeeroyHannigan

@LeeroyHannigan LeeroyHannigan commented Oct 24, 2026 •

Copy link
Copy Markdown
Collaborator

Summary

On Cassandra, RestoreTableFromBackup of a backup kept in the catalog (the legacy copy, copy_backup_into_target) copied each item through the ordinary put path (#404). Per item, that path read the empty target row, ran a lightweight transaction, and wrote the index rows in a logged batch. The target is CREATING for the whole copy and the copy is its only writer, so none of that guards anything.

Main's #740 added put_restored_item_at, a restore write with no read and no lightweight transaction, which the store-backed restore uses. The legacy copy now writes each item through it as well. Per item it sends one logged batch at LOCAL_QUORUM. The batch holds the base row and one row per GSI and LSI the item carries keys for. The index rows come from the put path's index row builder, so their shape and projection are the put path's.

Each copy reads the target's indexes once, at LOCAL_QUORUM, and sets each GSI's propagation delay to 0. So the index rows go into each item's batch, and the indexes are complete when the target turns ACTIVE. The copy keeps 8 items in flight (RESTORE_WRITE_CONCURRENCY, the count the store-backed restore uses) and reads two payload buckets ahead. This change removes the put path's put_restored_item and IndexWrites, which have no caller left.

Measured on this host, from the CREATING response to ACTIVE, release build, one Cassandra 4.1 node, three runs each:

Shape This branch
60,000 items, no index 3.075 s, 2.642 s, 2.635 s
20,000 items, one GSI (18,000 rows) and one LSI (20,000 rows) 1.304 s, 1.473 s, 1.411 s

In every run the restored items and index rows compared equal to the source. The same shapes took 27.8 s and 33.9 s on the put path in an earlier run on this host. The put path's times were not measured again.

Stamps on restored rows

Each restored item takes the ExtendDB host clock in microseconds as the base row's version. The base row and every index row also carry it as their write timestamp, the way the write path stamps index rows since #424. A later write's version is above it, and a later delete stamps its index tombstones above it. On the ACTIVE table, a host-clock stamp meets coordinator-stamped lightweight transactions and deletes. So the clocks of the ExtendDB hosts and the Cassandra nodes must agree. restore_engine's module doc states this rule, and the copy's comment refers to it.

The restore write checks index key types per item. A backup item that does not fit an index key schema fails the restore, and the target is removed.

Recovery

The copy runs in the task detached from the request, with its heartbeat. It checks the target's ownership per bucket and per item, so a target taken for removal stops taking writes within one item.

Before the merge with main, runs with the earlier writer gave these results:

  • A target data table dropped from outside mid-copy was detected, and the target was removed from the catalog in 1.2 s.
  • Two restores at once completed.
  • A 60,000-item restore under a sixteen-thread PutItem storm on another table took 10.3 s with zero writer errors.

Those runs were not repeated on this branch.

Tests

  • crates/storage-cassandra/tests/direct/restore_write.rs (2):
    • restored rows take every later write, and every GSI and LSI row of a restored item carries its base row's version as its write timestamp
    • pk-only rows across two payload buckets
  • crates/storage-cassandra/tests/direct/restore_write_controls.rs (3):
    • a backup item with a mistyped GSI key fails the restore (the target is removed, never ACTIVE)
    • a recorded item count that disagrees with the payload fails the restore
    • the restored row shape equals the put path's
  • crates/storage-cassandra/tests/direct/restore_timing.rs (2, ignored): the two measured shapes, run with --ignored.
  • Four existing tests that probe the CREATING window use 10,000 items, so the copy outlasts their probes. The simultaneous-restore test holds a registration on the backup and asserts that every concurrent restore is BackupInUse naming the backup.
  • Main's assert_index_rows_carry_the_base_versions takes the attribute that identifies an item and the index names, so the restore test and main's store-backed restore tests share it.
  • Changing the stamp of put_restored_item_at by one microsecond fails the restore test's new assertion.
  • On a Cassandra node on this host, release build: the restore tests and the backup engine tests, 103 passed and 1 failed. The failure is a --move-store test, which fails on main too because of this host's directory ownership. cargo fmt, clippy 1.97.0 with -D warnings: clean. No dependency change.

Not in this PR

Closes #404

…WT put path

RestoreTableFromBackup copied each backup item through put_item_impl:
a read of the (empty) target row, a lightweight transaction, and a
logged batch for the index rows, eight items at a time. The target is
CREATING for the whole copy and the copy is its only writer, so none of
that guards anything.

data/restore_copy.rs holds the copy's reads and writes. read_backup_bucket
returns one payload bucket's items. restore_writer resolves the target's
indexes once per copy (LOCAL_QUORUM), and write_restored_item writes an
item as plain INSERTs with no read and no IF condition: the base row
(pk, sort key column, item_data, version) and one row per GSI and LSI
the item carries keys for, issued concurrently at LOCAL_QUORUM. Index
rows come from index_row_insert, so their shape and projection are the
put path's. The index key type and emptiness validation is kept.

version is the clock reading in microseconds, the value a later
IF version = ? fence compares against. Neither the base row nor the
index rows carry a client timestamp: every later index mutation on this
tree (put, update, delete, transaction commit, the GSI queue worker) is
stamped by the coordinator, and a restored row stamped from the host
clock that ran the restore could outlive a later delete whose
coordinator clock trailed that host.

Nothing else is written: a restored table starts with streams disabled,
TTL settings are not restored, and every GSI row is written in the copy,
so there is no gsi_pending entry.

copy_backup_into_target reads payload buckets two ahead of the writes
and writes 64 items at a time across bucket boundaries, checking the
target's ownership per item as well as per bucket, so a swept target
stops taking writes within one item. The size accumulation and the item
count check are unchanged. The IndexWrites enum and put_restored_item
that the old copy needed are removed from put_get_item.rs.

Measured on one host against a single cassandra:4.1 node with a 4 GB
heap, from the CREATING response to ACTIVE: 60,000 items with no index
27.8 s before, 2.2 s after; 20,000 items with one GSI and one LSI 33.9 s
before, 0.7 s after. Item counts, every item and every index row
compared equal between source and target in both shapes.

Direct tests: restored rows take every later write (update, conditional
put and delete, transactions, across GSI and LSI rows); the index rows
are coordinator-stamped; a backup item with a mistyped index key fails
the restore; a recorded item count that disagrees with the payload fails
the restore; the restored row shape equals the put path's. Four existing
tests that probe the CREATING window use 10,000 items so the copy
outlasts their probes, and the simultaneous-restore test holds a
registration on the backup so exclusivity is asserted against a held
registration rather than a race.

Closes #404
crates/storage-cassandra/src/backup_engine.rs keeps the JobPermit and LegacyBackupReader imports and the shared RESTORE_WRITE_CONCURRENCY of 8 from main, and the TryStreamExt import and RESTORE_READ_AHEAD_BUCKETS from this branch.
crates/storage-cassandra/src/data/put_get_item.rs keeps the text from main, with IndexWrites, put_restored_item, put_restored_item_at and the system delay read. It drops the removal of IndexWrites and put_restored_item on this branch.
…stored_item_at

Main added put_restored_item_at for the store-backed restore. It writes each item as one logged batch of the base row and its index rows, with no read and no lightweight transaction. The batch carries the item's version as its write timestamp, as the write path since #424 stamps index rows.

The legacy copy, copy_backup_into_target, now writes each item through put_restored_item_at. This replaces data/restore_copy.rs from this branch, which the commit deletes. The copy reads the target's indexes once at LOCAL_QUORUM and sets their propagation delay to 0, as put_restored_item_at requires. Each item's stamp is the clock reading that a new row's version takes.

The copy keeps the read-ahead of two buckets and the ownership check per item from this branch. It writes RESTORE_WRITE_CONCURRENCY items at a time, the 8 that bulk_load uses.

data/index.rs and data/mod.rs return to the text on main, which also fixes the compile error that the merge left in index.rs. put_restored_item and IndexWrites have no caller left, so they go, and the retry loop of a put returns to put_item_impl. The design document names put_restored_item_at for the Cassandra load.
… and writer of main

Main replaced BackupEngine::create_backup with LegacyBackupReader::create, which takes a JobPermit, and added warm_throughput to GsiInput. The test files this branch adds now call create with JobPermit::detached() and set warm_throughput to None.

The BackupInUse message on main names the backup, not the target that holds it, and backup_in_use logs the target. The test of a held registration now checks that each refused restore names b2.

The test that restored index rows take the timestamp of the coordinator goes. The copy now writes through put_restored_item_at, whose rows carry the version of their base row as their write timestamp. On main, assert_index_rows_carry_the_base_versions in tests/direct/backup_engine.rs checks that stamp. The tests of a mistyped index key, of an item count above the payload and of the row shape stay.
…a legacy restore writes

No test checked the write timestamp of the rows that the legacy restore copy writes. On main, assert_index_rows_carry_the_base_versions checks that each GSI and LSI row carries the version of its base row. It does this only for the bulk_load tests, with the item attribute sk and the indexes gix and lix fixed in its code.

The helper now takes the attribute that identifies an item and the names of the indexes, and stamps_by_sk becomes stamps_by_attr. The bulk_load tests pass sk, gix and lix, as before. test_restored_rows_carry_a_version_and_take_every_later_write calls the helper after the restore, with value and the indexes ByGroup and ByLocal. The two helpers lose their test-hooks gate, because that test runs without the feature.

The comment on the stamp in copy_backup_into_target now points to the clock rule in the module doc of restore_engine, which applies to this copy too.
@LeeroyHannigan LeeroyHannigan changed the title perf(storage-cassandra): restore copy writes plain INSERTs, not the LWT path perf(storage-cassandra): the legacy restore copy writes through put_restored_item_at instead of the LWT path Nov 14, 2026
crates/storage-cassandra/src/backup_engine.rs keeps the copy of this branch through put_restored_item_at, and the payload read of main at Stmt::Read in read_backup_bucket.
…ch_indexes_for_table

Main removed fetch_indexes_for_table_quorum. Its fetch_indexes_for_table now reads at LOCAL_QUORUM through Stmt::Read.

The legacy restore copy now calls fetch_indexes_for_table, so it reads the indexes of the target at LOCAL_QUORUM as before.
@demohan demohan assigned demohan and unassigned demohan Nov 24, 2026

@demohan demohan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The legacy restore copy is much faster at b5a389a and gives main's result, but its base rows now carry the ExtendDB host's clock, so writes made right after the restore can be lost when that clock runs ahead of the Cassandra node (below). Through release servers, twice each, a 60,000-item table restored in 1.58 and 1.52 s against 95.4 and 93.3 s on main, and a 20,000-item table with a GSI and an LSI in 0.96 and 0.97 s against 32.9 and 32.8 s; through the PR's timing tests on the old source the plain shape took 6.04 to 6.14 s, not the body's 27.8 s, and the indexed one 33.7 to 34.3 s. Every run gave the source's items, GSI and LSI rows, the GSI ACTIVE with the table, main's item count and size, no stream and TTL off. During CREATING, PutItem and GetItem answer ResourceNotFoundException with the service's text; after a kill mid-copy the target stayed CREATING, its backup refused new restores, and the reconciler removed the target at 311 s, after which the backup restored cleanly; with the target's data table dropped, the target was gone 2.5 s after the request. The new tests fail with the stamp off by a microsecond, without index key validation and without the count check, fmt, clippy with and without test hooks and the 1.91.1 check are clean, CI is green on 58 of 58, and the branch merges with main at 222d80a and builds there.

I will approve once the legacy copy's base rows do not carry the host's clock, with a test for it, and a test holds the per-item ownership check.

// item's base and index rows, so the host and node
// clock rule of `restore_engine`'s module doc applies to
// this copy too.
let stamp = crate::data::version::now_micros();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

This stamp becomes the base row's write timestamp through with_timestamp(stamp) in put_restored_item_at. Every later write is a lightweight transaction whose cells take the coordinator's clock, so when the host is ahead they lose to the restored cells while their index rows, stamped with the new version, still land. With only the ExtendDB process's clock set ahead, 2,000 items with a GSI and an LSI, then 25 UpdateItem and 25 DeleteItem right after ACTIVE and a ConsistentRead of each, twice: at +5 s all 25 updates and all 25 deletes were lost though each answered 200, and 25 GSI rows disagreed with their items two minutes later; at +300 ms 25 of 25 updates and 25 and 14 deletes; at +50 ms 0 and 1 update. Main lost none at +5 s and +300 ms, and neither lost any with no skew. The store-backed restore needs the stamp as a fence against a takeover, but a legacy restore is never resumed, as the crash run above shows. Leaving the batch timestamp off for the legacy copy and passing the stamp to sync_indexes_at per statement lost nothing at +5 s and +300 ms, twice each, and kept the indexed restore at 1.003 s with every item and index row equal.

// buckets read ahead and the writes in flight, items of
// up to three buckets would otherwise still be written
// into a target the sweep has claimed.
if heartbeat.is_lost() {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

No test holds this check: with it deleted, all three ownership tests pass, and the two "stops the restore copy" tests fail only when the per-bucket check goes too ("the copy stopped short: 20000 of 20000 items"). Up to 8 writes already in flight still land after the loss, so "stops taking writes within one item" in the body says more than this holds.

.await;

// Simultaneous: four restores of b2 started together, exactly one admitted.
// Simultaneous, against a held registration: a CREATING restore target

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Optional. The test where four restores race to register on one backup is replaced by four against a registration inserted by hand, and no other Cassandra test covers that race now. Main's version fails here 3 of 3 with 2 of 4 admitted, which fits the copy finishing before the second target's table creation; a version where the first target stays CREATING, asserting one of four admitted, would keep it. The loop at backup_engine.rs:1301-1303 setting each GSI's delay to 0 changes nothing on any table a restore creates: with it removed, all five new tests pass.

@demohan

demohan commented Nov 29, 2026

Copy link
Copy Markdown
Collaborator

Main's store-backed restore has the same pattern and loses writes the same way under a clock offset; filed as #1529.

@navareamol navareamol left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

In the legacy copy the heartbeat check comes before the clock read for the write timestamp. The comment on put_restored_item_at describes the opposite order: the job reads the stamp before it checks its claim, so a late batch after a takeover carries a lower timestamp than the successor's writes. Is the order in copy_backup_into_target intended? If it is, could the comment say which order each caller uses?

I think two items with the same key in one backup payload would now leave index rows behind when their GSI key values differ. The old path read the row and removed the old index rows. Can a backup hold two items with one key? If it cannot, a short comment saying so would help the next reader.

The timing table and the 103 passed count come from the author's host. Could you add the run of the two ignored timing tests on the current head to the description?

I will approve once the comment and the code agree on the order, the duplicate key question is answered, and the timing numbers are for the current head.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

v1 Needed for the 1.0 release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

storage-cassandra: restore copy writes every item through the LWT put path

3 participants