Skip to content

fix(cassandra): a Query of a partition with no items left answers no items - #611

Open
LeeroyHannigan wants to merge 12 commits into
fix/cassandra-scan-emptied-partitionfrom
fix/cassandra-query-emptied-partition
Open

LeeroyHannigan wants to merge 12 commits into
fix/cassandra-scan-emptied-partitionfrom
fix/cassandra-query-emptied-partition

Conversation

@LeeroyHannigan

@LeeroyHannigan LeeroyHannigan commented Oct 26, 2026 •

Copy link
Copy Markdown
Collaborator

Changes since the last review

The branch is at 56027b4. Five commits sit on 9ef14ed, the head of the last review:

The message of 1df5da3 says that a transaction prepare on an absent key writes a row with only prepared_txn_id set. Such a prepare writes item_data with the JSON of the key. The message of a6b8571 gives the rows that hold no item, as the Why section below does. The read again with twice the LIMIT that 9c1e6af describes is gone.

The message of 2a0c47a says the cursor drops rows past a partition's first 500 rows. It drops the rest of any partition that a page ends inside.

What

On Cassandra, a Query of a partition whose items have all been deleted now answers no items. Before, it answered 500 InternalServerError. A GSI added by UpdateTable to a table holding such a partition stayed CREATING. A Query can also read other rows without an item, such as a TTL claim on a key being written. It now answers the items around them instead of 500. A GSI added by UpdateTable now gets every item of a partition that a backfill page of 500 rows ends inside. Before, the backfill moved on to the next partition and left out the rest of that one.

The change

  • crates/storage-cassandra/src/data/query.rs: a row without item_data is skipped. Query asks Cassandra for LIMIT limit + 1 rows, so skipped rows take places in the read. Query now reports more pages when the read filled its LIMIT, counting the rows Cassandra returned rather than the items kept. So a page that skipped rows can be short but carries a LastEvaluatedKey, and the engine reads on from its last item.
  • A full base-table read in which no row holds an item has no item to continue from. Query then reads on after the last row's sort key: sk > ? forward and sk < ? backward, with the key condition's bound on the far side. The first read takes Limit + 1 rows, as on main. Each further read takes up to twice as many rows as the read before, and at most max(1000, Limit + 1). No row is read twice. Cassandra can refuse a further read with a read failure (for example READ_TOO_MANY_TOMBSTONES). That read then runs again with half the rows, down to one, and the later reads of the call stay at that size. Index tables never hold a row without item_data, and their reads are unchanged.
  • crates/storage-cassandra/src/data/ddl.rs: the GSI backfill skips a row without item_data. It reads the base table in pages of 500 rows. After a full page whose last row has a sort key, it reads the rest of that partition (pk = ? AND sk > ? LIMIT 500) before its token cursor moves on. A hash-only table has one row per partition and keeps the token cursor only.
  • crates/storage-cassandra/src/data/rows.rs (from fix(cassandra): a Scan reads on past rows without an item #612) gains row_sort_key, the sort key of a row as a bind value, and row_items, the items that the rows of a read hold. Query and the backfill use row_sort_key. Query and Scan use row_items, so scan.rs calls it in place of its own copy of that parse. The backfill uses fix(cassandra): a Scan reads on past rows without an item #612's row_item, and Query uses fix(cassandra): a Scan reads on past rows without an item #612's read_halving and rows_of. Every read runs through PolicySession::run(Stmt::Read, ...), the path and the consistency level of main's other reads.

Why

This PR fixes no open issue. #612, its base, fixes the same defect in Scan.

What happened

DeleteItem sets the partition's static column partition_max_delete_timestamp. When the last item of a partition is deleted, Cassandra still answers a read of that partition with one row. That row carries only the static columns and no item_data. Query parsed that row as an item and failed with Column or Udt property 'item_data' is empty (server log), answering 500. The GSI backfill, which scans the base table, read the same row, failed the same way, and retried for as long as the index existed.

The rows without item_data are:

  • the static-only row of a partition whose items have all been deleted, on a table with a sort key.
  • the row a TTL claim on an absent key writes, with only prepared_txn_id and prepared_txn_timestamp set, on any table.
  • the row a transaction prepare leaves when its UPDATE ... IF prepared_txn_id = null lands on an item deleted after the transaction read it.

Query answered 500 on all of them. A transaction prepare on an absent key writes item_data with the JSON of the key, so that row is not one of them (#1495).

A Query with a sort key condition or an ExclusiveStartKey, an index Query and a hash-only table do not see the static-only row. Cassandra returns it only when the read restricts no clustering column. An index Query reads the index table, and a hash-only table keeps the delete timestamp as a regular column of the item's own row.

The backfill's page counted the skipped rows. After a full page, its token cursor moved past the partition of the last row. One partition held 1,000 claim rows ahead of the items z1 and z2. The index then went ACTIVE without z1 and z2, where main stayed CREATING (demohan's review measured this on 24 Nov). The same cursor drops the rest of any partition that a page ends inside, on main too. A GSI added to a table with one 600-item partition held 500 entries.

What the service answers

Measured on 2026-10-26 at 06:48, 06:52, 06:59, 07:09, 08:00 and 14:45 UTC in us-east-1, each case twice with the same answer:

  • A Query of a partition whose items have all been deleted answers Count 0 with no items. That holds in six shapes (plain, ConsistentRead, Limit 1, ScanIndexForward false, Select COUNT, a sort key condition) and on a global secondary index. GetItem of a deleted item answers no Item.
  • The same holds with a FilterExpression, a ProjectionExpression, Select SPECIFIC_ATTRIBUTES, and an ExclusiveStartKey before, inside or after the deleted items. It holds when TransactWriteItems or BatchWriteItem deleted the items. Such a Query consumes 0.5 capacity units, or 1.0 with ConsistentRead.
  • A partition with one item left answers that item. A hash-only table whose item was deleted answers nothing to Query, GetItem and Scan.
  • A GSI added by UpdateTable to a table with an emptied partition and one live item becomes ACTIVE, after 520 s and 1,011 s. It answers the live item only.

Not changed here

  • Scan skipped rows without item_data the same way and ended early. fix(cassandra): a Scan reads on past rows without an item #612, the base of this PR, fixes that.
  • The first read of each Query storage call is the read main makes, with no retry at half the rows.
  • When a page reaches its Limit on the partition's last item, extenddb returns a LastEvaluatedKey and the next page is empty. The engine's page loop in read_helpers.rs, which every backend uses, sets it. At 14:45 UTC on 2026-10-26, the service answered such a page with no LastEvaluatedKey in 46 of 48 cases. The cases were partitions of one, two and three items, with and without deleted items, both read consistencies and both directions. The service answered with a key and an empty next page in the other 2 (second round, descending, two deleted items before the live one).

Merging

Review and merge #612 first: this PR is based on its branch.

git merge-tree --write-tree of 56027b4, on 2026-11-25:

Testing done

At 56027b4, with toolchain 1.97.0 and cassandra:4.1 with PasswordAuthenticator, as in CI:

  • The modules index, query, query_rows_without_item, scan, scan_rows_without_item and scan_sort_key_types of cargo test -p extenddb-storage-cassandra --test direct_integration, with EXTENDDB_TEST_CASSANDRA=required: 65 passed (353.25 s).
  • crates/storage-cassandra/tests/direct/query_rows_without_item.rs: 8 tests, the 4 of the last review and 4 new ones. They are the tests of the paging change and of the backfill paging. demohan's review found that the 17 wire tests pass with the first commit's skip-only query.rs. CI runs the direct tests in the cassandra-storage job with EXTENDDB_TEST_CASSANDRA=required.
    • an emptied partition, read both ways with and without Limit.
    • rows without an item before, between and after ten items. They are paged both ways with page sizes 1 to 5 and 9 to 11, and with no Limit.
    • one such row ahead of 3,000 items.
    • 3,000 such rows ahead of two items. With Limit 1, the Query makes 11 reads of the data table (system_views.local_read_latency).
    • 1,030 and 3,500 such rows ahead of 300 items, Limit 128. The system_views.rows_per_read histogram shows no read of more than 1,000 rows.
    • 2,100 such rows, each followed by 110 deleted rows, Limit 1. Cassandra refuses the read of 1,000 rows, and the read of 500 rows passes.
    • 3,000 such rows inside a key condition (sort < b forward, sort > b backward), with items outside it. Limit 1 answers no item and no key. With the far bound left out of the read, the Query answers an item outside the condition.
    • a GSI added by UpdateTable to a table with one partition of 600 items and one of 1,000 claim rows ahead of z1 and z2. The index goes ACTIVE with 602 entries.
  • The checks of the format, clippy and MSRV jobs, for this crate: clean. They are cargo +1.97.0 fmt in check mode, and the two clippy commands of .github/workflows/clippy.yml with warnings denied. One builds with the crate's test-hooks, the other with the engine's backup-test-hooks. The MSRV check is cargo +1.91.1 check -p extenddb-storage-cassandra --all-targets.
  • cargo test -p extenddb-storage-cassandra --lib: 191 passed. --test consistency_policy: 10 passed.
  • python3 -m pytest --collect-only -q of tests/ (6,693 tests) and of tests/python (672): no error.

At 2a0c47a, with toolchain 1.97.0 and cassandra:4.1 with PasswordAuthenticator:

  • cargo test -p extenddb-storage-cassandra --test direct_integration with EXTENDDB_TEST_CASSANDRA=required: 282 of 288 passed (5,327.96 s). The 6 failures were in backup_engine, index_delay_reads, keyspace_migrations and secret_rows. Each was the same error in its setup. A schema change of a catalog migration or of the catalog's ledger got no answer within 20 s. The host's load average was between 30 and 69 on 16 cores. This PR does not change those files. Run alone, 5 of the 6 passed at the first try. keyspace_migrations::migrations_ignore_the_keyspace_another_caller_selected failed alone twice with the same kind of timeout, at 11:29 and 11:45 UTC. It passed alone at 11:51 UTC (276.48 s). On main a417b2f it passed alone at 11:37 UTC (267.20 s).
  • Controls, with the tests of that head and the production files of 762fc35: 4 of the 7 tests of query_rows_without_item.rs fail. Those files hold 9ef14ed's code, merged with fix(cassandra): a Scan reads on past rows without an item #612 and main. Limit 1 behind 3,000 rows makes 13 reads. One read returns 1,330 rows (1,331 in the histogram). The Query behind the deleted rows fails with READ_TOO_MANY_TOMBSTONES. The index goes ACTIVE with 500 entries.
  • Mutants, each on its own, and each fails a test:
    • the read size does not double: 1,501 reads with Limit 1.
    • the read size has no cap: one read of 1,865 rows, 1,916 in the histogram.
    • a refused read is not read again: READ_TOO_MANY_TOMBSTONES.
  • tests/test_query_emptied_partition.py on a Cassandra-backed debug server of that head, with the runtime settings devtools/run-tests applies in CI: 17 passed.

Checklist

  • I have read CONTRIBUTING.md
  • All tests pass (cargo test --workspace)
  • Code is formatted (cargo fmt --check)
  • Clippy is clean (cargo clippy -- -W clippy::pedantic)
  • I have added or updated tests for new functionality
  • I have updated documentation if behavior changed
  • Breaking changes are noted below (if any)
  • If this changes the wire protocol, Storage trait, auth model, on-disk
    format, or public CLI surface, an RFC has been accepted or is linked
    below. Otherwise, an ADR captures the decision (link below).

ADR / RFC: n/a

Breaking changes

None. Main answers 500 InternalServerError to a Query whose read returns a row without item_data, because it reads item_data as a required column. On every other read, this branch makes main's reads and returns main's page. On 2026-11-25, a partition held 1 claim row, 128 items and then 110,000 deleted rows. With Limit 128, main answered 500. This branch answered the 128 items with a LastEvaluatedKey, and the page after it failed with READ_TOO_MANY_TOMBSTONES. A smaller LIMIT does not avoid that failure: deleted rows return no row, so the read passes all of them.


By submitting this pull request, I confirm that my contribution is made under
the terms of the Apache License 2.0 and I agree to the Developer Certificate of
Origin (DCO). See CONTRIBUTING.md for details.

DeleteItem sets the partition's static column partition_max_delete_timestamp.
When the last item of a partition is deleted, Cassandra still answers a read
of that partition with one row carrying only the static columns, with no
item_data. Query parsed that row as an item and failed with
`Column or Udt property 'item_data' is empty`, a 500, where Amazon DynamoDB
answers Count 0 (measured 2026-10-26 at 06:48 UTC, every Query shape, twice).
The GSI backfill's scan of the base table read the same row and failed the
same way.

Both now skip a row without item_data, as Scan already did.
Dual-target wire tests from the 2026-10-26 measurements: a Query of an
emptied partition in every shape (plain, consistent, Limit 1, descending,
COUNT, with a sort key condition) answers no items, as does an index
Query; GetItem of a deleted item answers no Item; a partition with one
item left answers it; and a GSI added by UpdateTable to a table with an
emptied partition becomes ACTIVE and holds only the live item. On
Cassandra before the fix the first five answered 500 and the index stayed
CREATING.
A hash-only table stores the delete timestamp as a regular column, so it
has no static-only row; Query, GetItem and Scan after deleting its item
answer nothing on the service (measured 2026-10-26 at 08:00 UTC, twice)
and on Cassandra before and after the fix. The test keeps that covered.
A TTL claim or a transaction prepare on an absent key writes a data row
with only prepared_txn_id set and no item_data. Query asks Cassandra for
LIMIT limit + 1 rows, so such rows take places in the read. Skipping them
and then deciding "more pages" from the items kept returned a short page
with no LastEvaluatedKey while items remained: a TTL table under
concurrent writes to new keys returned 128 of 300 items in 9 of 1,162
full reads. On main the same rows answer 500.

When rows were skipped and the read filled its LIMIT, Query now reads
again with room for the skipped rows. The direct test writes such rows
with CQL before, between and after the items and pages through the
partition with every page size.

The wire tests read with ConsistentRead where the shape allows, and the
index test waits 120 s against extenddb instead of 1,500 s.
The previous commit read again with a LIMIT widened by the rows it had
skipped, and the wider read applied the same test to itself. A row
without an item stays inside every wider window, so one such row in front
of a large partition made the Query read the whole partition one item per
level: 1,501 Cassandra reads for a Limit 1 page over 1,500 items, and at
3,000 items the server overflowed its stack and aborted.

Query now decides "more pages" from the rows Cassandra returned: a read
that filled its LIMIT reports more pages, even when rows without an item
leave the page short, and the engine reads on from the last item. Only a
read in which no row holds an item is read again, with twice the LIMIT,
so the reads grow with the run of such rows, not with the partition.

The direct tests add a 3,000-item partition behind one such row and a
run of 3,000 such rows in front of two items.
The index query test deleted the items right after putting them and then
waited for the index to answer Count 0, which an index that had not yet
received the puts answers too; a later descending query then saw an item
on SQLite. The test now waits for the index to hold the three items,
deletes them and waits for Count 0.
Adds the Query shapes measured on the service at 14:45 UTC to the
emptied-partition wire file: the capacity such a Query consumes (0.5 units,
1.0 with ConsistentRead), an ExclusiveStartKey before, inside and after the
deleted items, and partitions emptied by TransactWriteItems and by
BatchWriteItem.

@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.

Every answer this PR changes was a 500 on the merge-base and on main 3534c9e, and now matches the service; no answer that was 200 changed. On Cassandra 4.1 builds of head, the merge-base, main and head merged with main, and on the service in us-east-1, two rounds each:

  • 508 query shapes over partitions emptied by DeleteItem, BatchWriteItem or TransactWriteItems, partly deleted, moved by a transaction, with index keys moved or removed, and a hash-only table, on the base table, an LSI and a GSI, plain, ConsistentRead, both directions, COUNT, Limit 1 to 3 paged, filters, projections, sort key conditions and ExclusiveStartKey. The merge-base answers 500 on 64 of them; head answers them, and the merged build matches the service on items, Count, ScannedCount and ConsumedCapacity across all 508.
  • 132 shapes with rows that hold no item between, before (runs of 40 and 300) and after items: base and main answer 500 on 72, and head answers what the service gives without those rows. A 1,200-item partition with 54 such rows returns every item once, in order, at every Limit tried, and 416 randomized cases on head and the merged build pass.
  • A partition whose only item TTL removed answers Count 0 with 0.5 (1.0 consistent), and a GSI added later to a table with an emptied partition goes ACTIVE with the service's answers, where main stays CREATING. A GSI stuck in CREATING under the merge-base goes ACTIVE 63 s after this head starts on the same keyspaces.

fmt and clippy are clean, the 1.88 MSRV build passes, 1,794 workspace tests, 61 Cassandra lib tests and 153 direct tests pass, the wire file passes 17 of 17 on head and twice on the service and fails 9 on main, CI is green on 34 checks with the direct tests run, and every negative control fails where it should. The merge with main is clean, and the merged tree passes clippy, the direct Query tests and the wire file.

I will approve once the re-read is bounded and the backfill pages past rows without an item.

#[allow(clippy::cast_sign_loss, clippy::cast_possible_truncation)]
let actual_limit = limit.map_or(1_000_000_usize, |l| l.max(0) as usize);
let has_more = items.len() > actual_limit;
if items.is_empty() && fetched_rows > actual_limit {

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.

Each wider read starts again at the same position, and the last one reads every row up to the doubled LIMIT, outside the engine's 1,024-item chunk and the 1 MB page budget. With 5,000 rows without an item before 3,000 items of 100 KB, a Query with no Limit took 207.68 s and 920 MB of server memory for a 10-item page, against 0.35 s and 52 MB without those rows; twice it missed a 120 s client timeout with one worker at 99.9% CPU. With 100,000 such rows before 2 items, Limit 1 read about 262,000 rows in 18 reads. Selecting the clustering columns and reading on from the last row with the same LIMIT, as #612 does for Scan, would bound each read and read no row twice. The body's cost statement can then count rows as well as reads.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

a6b8571 replaces the wider read with a read on after the last row's sort key. The read on goes through the retry with half the rows that #612 adds for Scan. Each read after the first takes up to twice as many rows as the read before, and at most max(1000, Limit + 1). No row is read twice. The read keeps the key condition's bound on the far side, and index reads are unchanged.

The direct tests count it. One partition holds 1,030 rows without an item ahead of 300 items. There, Limit 128 now reads 129, 258, 516 and then 427 rows, and rows_per_read shows no read above 1,000 rows. With the previous code, one read returned all 1,330 rows (1,331 in the histogram). Limit 1 behind 3,000 such rows now takes 11 reads, and the previous code took 13. The body states the cost per read in rows.

@@ -498,8 +498,15 @@ pub(crate) async fn backfill_gsi(
let mut batch_has_rows = false;

for row in &rows {

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.

Skipped rows still count toward the backfill's 500-row page, so when a partition's first 500 rows hold no item, the token cursor moves to the next partition and that partition's items are not indexed. With 1,000 such rows before items z1 and z2 and a GSI on sk, the index went ACTIVE after 2 s with 0 entries; on the merge-base it stays CREATING. The skip turns a visible stuck index into an ACTIVE one missing items. Paging within the partition past skipped rows, with a direct test of this shape, would cover it. The comment names only the emptied partition; rows with only prepared_txn_id are skipped too, as query.rs:728 says.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

2a0c47a pages the backfill inside a partition before its token cursor moves on. After a full page whose last row has a sort key, it reads pk = ? AND sk > ? LIMIT 500 until a page is short.

The direct test test_a_gsi_backfill_reads_the_rest_of_a_partition builds your shape, 1,000 claim rows ahead of z1 and z2, next to one partition of 600 items. The GSI added by UpdateTable goes ACTIVE with 602 entries. With the previous backfill it went ACTIVE with 500. The same change covers the 600-item partition, which lost its last 100 items on main too. The comment now names the three kinds of rows without an item.

// Step 8: Parse results
// Step 8: Parse results. A row without `item_data` holds no item:
// a partition whose items have all been deleted answers a partition
// read with one row of its static columns

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.

A transaction prepare on an absent key writes item_data holding the key's JSON (transactions.rs:1365), not a row with only prepared_txn_id, so that row is not skipped: written that way, Query and GetItem return a key-only {pk, sk} item, as main does. The comment, the new direct test's doc and the body can say which rows hold no item.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

A prepare on an absent key writes item_data with the JSON of the key (prepare_item in transactions.rs), so that row is not skipped. a6b8571 corrects the comment in query.rs and the doc of the direct test module, and the body now names the three rows without item_data:

  • the static-only row of an emptied partition.
  • the row a TTL claim on an absent key writes.
  • the row a prepare leaves when its UPDATE ... IF prepared_txn_id = null lands on an item deleted after the transaction read it.

The row that prepare_item writes reads back from GetItem, Query and Scan as a key-only item, at this head as on main. #1495 tracks that.

@@ -0,0 +1,237 @@
# Copyright 2026 ExtendDB contributors

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.

With the first commit's skip-only query.rs, all 17 wire tests pass, so only the 4 direct tests hold the paging half. CI runs them with EXTENDDB_TEST_CASSANDRA=required; the Tests section of the body can say they carry that half.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

The Testing done section of the body now says so. The direct tests in query_rows_without_item.rs test the paging change and the backfill paging, and the wire tests pass with the skip alone (your measurement). CI runs the direct tests in the cassandra-storage job with EXTENDDB_TEST_CASSANDRA=required.

…query-emptied-partition

The merge brings #612's branch at 9da5dce, which merges main at
a417b2f (#1173). Git merged it with no conflict. In
crates/storage-cassandra/src/data/query.rs, this branch's skip of rows
without item_data and its "more pages" rule sit next to main's sort key
range (sk_range.rs). In data/ddl.rs, the backfill's skip of such rows sits
next to main's batch builder and version stamps.
crates/storage-cassandra/tests/direct_integration.rs keeps the modules of
both branches and of main.
…an item

In 9c1e6af, Query read the partition again from the same start when no
row of a full read held an item. The new read had twice the LIMIT, and
Query did so again while every row held no item. Each wider read went back
over the rows of the read before it, and the last one returned every row
up to its LIMIT. One partition held 1,030 rows without an item ahead of 300
items. There, a Query with Limit 128 made a read of LIMIT 2,049, which
returned all 1,330 rows of the partition.

Query now reads on after the last row's sort key, through the retry of
data/rows.rs that #612 adds for Scan. A base-table read also selects the
sort key column. The next read is `sk > ?` forward and `sk < ?` backward,
and it keeps the key condition's bound on the far side. Each further read
takes up to twice as many rows as the read before, and at most
max(1000, LIMIT + 1). Cassandra can refuse a read with a read failure. The
read then runs again with half the rows, down to one, and the later reads
of the call stay at that size. No row is read twice. Index tables never
hold a row without item_data, and their reads are unchanged.

The message of 1df5da3 says that a TTL claim or a transaction prepare on
an absent key writes a row with only prepared_txn_id set. A prepare on an
absent key writes item_data with the JSON of the key (prepare_item in
transactions.rs). The rows without item_data are these:

- the static-only row of a partition whose items have all been deleted,
  on a table with a sort key.
- the row a TTL claim on an absent key writes, with only prepared_txn_id
  and prepared_txn_timestamp set.
- the row a transaction prepare leaves when its
  `UPDATE ... IF prepared_txn_id = null` lands on an item deleted after
  the transaction read it.

The comment in query.rs and the doc of the direct test module now name
these rows. Query parses a row with row_item from data/rows.rs, and the
direct tests use the helpers in tests/common/mod.rs.

Tests:

- Limit 1 over 3,000 rows without an item: 11 reads of the data table
  (13 with 9c1e6af's read again from the start).
- 1,030 and 3,500 such rows ahead of 300 items, Limit 128: the
  rows_per_read histogram shows no read of more than 1,000 rows. With
  9c1e6af, one read returned 1,330 rows. With no cap on the read size,
  the fifth read in the second shape returns 1,865 rows.
- 2,100 such rows, each followed by 110 deleted rows, read with Limit 1.
  Cassandra refuses the read of 1,000 rows. The read of 500 rows passes,
  and the Query answers the first item. With 9c1e6af, the Query fails
  with READ_TOO_MANY_TOMBSTONES.
…re its cursor moves on

The GSI backfill reads the base table in pages of 500 rows. After a full
page, its token cursor moves past the partition of the page's last row.
Rows without an item count toward the page. With the skip of e2d348b, a
page of such rows no longer fails. So a partition with 1,000 claim rows
ahead of the items z1 and z2 gave a page of 500 rows with no item. The
cursor then moved past that partition, and the index went ACTIVE without
z1 and z2. On main the backfill fails on the first such row, and the index
stays CREATING. The same cursor drops the rows of any partition past its
first 500 rows, on main too. A partition of 600 items gave 500 index
entries.

After a full page whose last row has a sort key, the backfill now reads
the rest of that partition. It reads it in pages of
`pk = ? AND sk > ? LIMIT 500`. The token cursor moves past the partition
after a short page. A hash-only table has one row per partition and keeps
the token cursor only. The backfill parses a row with row_item from
data/rows.rs.

Test: a table with one partition of 600 items, and one of 1,000 claim
rows ahead of z1 and z2. The GSI added by UpdateTable goes ACTIVE with
602 entries. With e2d348b's backfill, it goes ACTIVE with 500.
When no row of a full read holds an item, Query reads on after the last
row's sort key (a6b8571). That read keeps the key condition's bound on
the far side. No test had a sort key condition, so no test read on with a
far bound. The new direct test writes 3,000 rows without an item inside
the key condition and two items outside it. It reads with Limit 1, forward
with `sort < b` and backward with `sort > b`, and gets no item and no key.
With the far bound left out of the read on, the forward Query answers the
item c0, which is outside the condition, with its key.

The new test sends its Query through query_where, which takes the sort
key condition. The query helper of the module now calls query_where with
no sort key condition.
query.rs and scan.rs parsed the items of a read with the same chain of
row_item calls. rows::row_items now holds that parse, and both call it.
The parse is unchanged. A row without item_data holds no item, and a row
whose item_data does not parse fails the read.
@LeeroyHannigan

Copy link
Copy Markdown
Collaborator Author

a6b8571 bounds the read past rows without an item. It reads on after the last row's sort key, takes at most max(1000, Limit + 1) rows a read, and reads no row twice. 2a0c47a makes the GSI backfill read the rest of a partition before its token cursor moves on. The direct test builds your shape next to a partition of 600 items, and the index goes ACTIVE with all 602 entries. The branch merges #612's branch (762fc35), which brings main at a417b2f, and the read on uses the retry with half the rows that #612 adds. #612 is now the base of this PR.

@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 backfill pages past rows without an item at 56027b4, and the re-read is bounded in rows; the items the last read holds are not yet bounded.

  • On S, N and B sort keys and a hash-only table, with claim rows ahead of items, partitions of 500, 600 and 1,600 items and runs between items, the index gets every entry with none missing or repeated (3,682 of 3,682 and 1,028 of 1,028), serially and concurrently; main goes ACTIVE with 2,090 of 3,682. The backfill test fails with the reviewed ddl.rs and with the in-partition branch off.
  • Each read on starts after the last row of the one before, carries the key condition's far bound and takes at most max(1000, Limit + 1) rows: 100,000 rows without an item take 108 reads and 100,022 rows for Limit 1, about 262,000 at 9ef14ed, and a read refused for tombstones runs again at 500 rows and answers. Paging is right in 3,276 randomized cases over S, N and B sort keys, both directions and ExclusiveStartKey, and 936 far-bound shapes answer as SQLite does. Removing the cap, the doubling, the halving retry or the far bound, or counting has_more from items, each fails a new test.
  • The comments, the test docs and the body name the same three kinds of row. fmt, both clippy forms, the 1.91.1 check, 191 lib tests and the 8 new direct tests are clean, and CI is green with one job queued.

I will approve once the read that reaches the items holds no more than the engine asked for.

(Some(sk_type), Some(row)) if full => Some((sk_type, row_sort_key(row, sk_type)?)),
_ => None,
};
let items = row_items(&rows, "query")?;

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 read that ends the run is parsed whole before take(actual_limit), so it can hold up to 1,000 items whatever the engine asked for. With 5,000 rows without an item ahead of 3,000 items of 100 KB, a Query with no Limit answers Count 10 after 20.9 s and 19.8 s with the server at 334 and 317 MB, where the same items alone take 0.34 s and 63 MB; the last read returned 903 items for a 10-item page. With Limit 10 it takes 2.9 s and 172 MB. Under load the same Query once answered 500 after 19.3 s from a ReadTimeout in query_read_on, which read_halving does not retry. Main answers 500 here, so this is not a regression, and it is ten times better than 9ef14ed; at 400 KB items the bound is about 400 MB for a 1-item page. Growing the reads over the run with a column only item rows carry, then reading item_data from the first such row with the engine's LIMIT, or going back to fetch_limit once a read returns an item, would bound it.

/// 3,000 rows without an item ahead of two items. With Limit 1, the reads
/// take 2, 4, 8 and so on up to 512 rows, then 1,000, and the 11th read
/// reaches the items. A read again from the start with a doubled LIMIT takes
/// 13 reads, and reads that stay at 2 rows take 1,500.

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. With the doubling removed the test counts 1,501 reads, as the PR body says; the doc says 1,500.

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

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants