Skip to content

fix(cassandra): give every data-path statement a consistency level - #591

Open
LeeroyHannigan wants to merge 9 commits into
feat/read-optionsfrom
fix/cassandra-consistent-read
Open

LeeroyHannigan wants to merge 9 commits into
feat/read-optionsfrom
fix/cassandra-consistent-read

Conversation

@LeeroyHannigan

Copy link
Copy Markdown
Collaborator

What

Every statement on the Cassandra data path that stated no consistency level ran at the driver default, ONE: the reads behind GetItem, Query and Scan, the OCC and put-if-absent lightweight transactions (their Paxos round at the server default SERIAL, their commit at ONE), the plain delete, the transaction prepare and commit statements and the transaction ledger. With the default keyspace replication factor of 3, a ConsistentRead: true read could answer from a replica a write had not reached, so the flag was not honoured above replication factor 1.

Every statement under crates/storage-cassandra/src/data/ now states its level through one of four constructors in cassandra_util:

  • read_params(consistent_read, values): LOCAL_QUORUM for true, LOCAL_ONE for false. GetItem, Query and Scan pass the request's ConsistentRead. A read whose result decides a write (the OCC reads before put, update and delete, the transaction reads, the idempotency token reads, the ledger reads the recovery worker acts on) and a table walk (the export scan, the metadata walks, the global secondary index backfill) pass true; catalog and queue metadata reads pass false.
  • write_params(values): LOCAL_QUORUM, for every statement without an IF clause.
  • lwt_params(values): LOCAL_QUORUM with serial LOCAL_SERIAL, for every statement with an IF clause.
  • batch_builder(): a BatchQueryBuilder at LOCAL_QUORUM, for every batch.

The four schema statements in ddl.rs stay at the driver default with a statement-level allow. Two checks hold the contract. clippy.toml disallows Session::query, query_with_values, exec, exec_with_values and paged, StatementParamsBuilder::new, BatchQueryBuilder::new and the four cassandra_util helpers that run at the default; the crate allows the lint outside data/ (225 calls in the catalog, metadata, backup and bootstrap code keep the default there) and data/mod.rs turns it back on. The lint cannot see which constructor a call passes, so data/level_pairing_tests.rs reads every statement site under data/ at test time and asserts that a SELECT carries a read level, a plain write write_params, an IF statement lwt_params and every batch batch_builder.

The backend creates each keyspace with NetworkTopologyStrategy in the one datacenter named by datacenter, so a LOCAL_QUORUM read and a LOCAL_QUORUM write each reach a majority of the same replication_factor replicas, and a true read reflects every write acknowledged before it started. GetItem now derives the partition key with composite_pk_to_text, as the write path does, so a table whose key has more than one HASH attribute reads its row; the TTL worker's quorum read shares the derivation (it bound the first attribute alone before and read None on such a table).

ADR-0025 records the levels, the rejected options, the lint and the one-datacenter bound. The storage design, the high availability design, the backend author manual, the ReadOptions doc and the get_item doc state the levels and cite it. The replication_factor and datacenter field docs, a row in the differences table and an entry in the upgrade manual tell an operator what changed: a write and a ConsistentRead: true read need replication_factor / 2 + 1 replicas up in datacenter (two of three at the default; the lightweight transactions already needed a Paxos majority), a request that cannot reach its level fails with HTTP 500 InternalServerError, and a coordinator in a datacenter that holds no replica refuses every data-path statement, so the contact points and their peers must all be in datacenter.

The second commit lets the integration suites read the node address from EXTENDDB_TEST_CASSANDRA_ADDR (default 127.0.0.1:9042), so a suite can run against a node on another port.

Why

Closes #560.

A client that reads with ConsistentRead: true after a write it saw acknowledged expects the write; on a replicated keyspace the backend could answer with the item as it was before. Raising the read level alone does not close it, since a quorum read and a one-replica write do not intersect; the writes move to LOCAL_QUORUM with the reads, which is the trade the issue names.

Filed separately after this PR: the four catalog and queue metadata reads kept at one replica that feed a write or a cleanup; the classification of the driver's unavailable error as a storage fault (500) rather than transient (503); routing coordinators to datacenter.

Testing done

Unit tests (66): the four constructors carry the expected level and serial level; the pairing test over src/data/*.rs resolves 90 statement sites and finds every one paired. Integration, on a fresh single node: direct_integration 152 passed (149 at the base plus three new: GetItem, Query and Scan return the item with both ConsistentRead values on both table shapes; with the account keyspace raised to replication factor 3 on the one node, where LOCAL_ONE can be met and LOCAL_QUORUM cannot, the false reads return the item and the true reads are refused, which tells the two levels apart end to end; a table with a two-part HASH key reads its row with both values); ttl_integration 26 passed, 1 failed, 1 ignored, the failure (test_ttl_enable_rejects_asynchronous_gsi) pre-existing at the base and unrelated.

Negative controls, each on a clean committed tree and restored after: read_consistency(true) returning LOCAL_ONE fails the unit test and the replication factor 3 test; lwt_params without the serial level fails its unit test (Cassandra accepts an IF statement without a serial level and runs Paxos at its default SERIAL, so no integration test can see it); get_item passing false for a true read fails the replication factor 3 test; a raw session.query_with_values, a BatchQueryBuilder::new() or any of the four default-level helpers added under data/ fails clippy; a plain DELETE moved to read_params(false), an INSERT ... IF NOT EXISTS moved to write_params and a batch built without its level each fail the pairing test; the first-attribute key derivation fails the two-part HASH key test.

cargo +1.97.0 fmt --all -- --check
cargo +1.97.0 clippy -p extenddb-storage-cassandra --all-targets -- -D warnings
cargo test -p extenddb-storage-cassandra --lib                                  # 66 passed
EXTENDDB_TEST_CASSANDRA_ADDR=127.0.0.1:9342 EXTENDDB_TEST_CASSANDRA=required \
  cargo test -p extenddb-storage-cassandra --test direct_integration            # 152 passed

Checklist

  • I have read CONTRIBUTING.md
  • All tests pass (cargo test --workspace --lib; the Cassandra integration suites run in CI against a service container)
  • Code is formatted (cargo fmt --check)
  • Clippy is clean (cargo clippy --all-targets -- -D warnings)
  • 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: ADR-0025

Breaking changes

None to the wire protocol or the storage trait. Operational: on a Cassandra keyspace with a replication factor above 1, a ConsistentRead: true read, an export and a DeleteItem of an absent item need a local quorum of replicas up where one answered before, and every item write waits for a local quorum to acknowledge its commit. The upgrade manual entry gives the ALTER KEYSPACE to lower an existing single-node keyspace to replication factor 1.


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.

Every statement on the Cassandra data path that did not state a
consistency level ran at the driver default, ONE: GetItem, Query and
Scan, the OCC and put-if-absent lightweight transactions, the plain
delete, and the TransactWriteItems prepare and commit statements. With
the default keyspace replication factor of 3, a ConsistentRead: true
read could return an item as it was before a write the client had seen
acknowledged (issue #560).

Each statement under data/ now states its level through one of four
constructors in cassandra_util:

- read_params(consistent_read): LOCAL_QUORUM for true, LOCAL_ONE for
  false
- write_params: LOCAL_QUORUM, for a write with no IF clause
- lwt_params: LOCAL_QUORUM with serial consistency LOCAL_SERIAL, for a
  statement with an IF clause
- batch_builder: a BatchQueryBuilder at LOCAL_QUORUM, for every batch

GetItem, Query and Scan pass ReadOptions::consistent_read. The reads
whose result decides a write run at LOCAL_QUORUM. Those are the OCC
reads before a put, update or delete and the transaction, idempotency
token and ledger reads. The export scan, the TTL backfill and table size
scans and the GSI backfill read at LOCAL_QUORUM too, so that a page
reflects every write acknowledged before it was read. Catalog and queue
metadata reads that ran at ONE run at LOCAL_ONE. The backend creates
each keyspace with NetworkTopologyStrategy in the one datacenter named
by `datacenter`, so a LOCAL_QUORUM read and a LOCAL_QUORUM write each
reach a majority of the same replicas and a true read reflects every
write acknowledged before it started. Schema statements stay at the
driver default.

GetItem derives the partition key with composite_pk_to_text, as the
write path does, so a table whose key has more than one HASH attribute
reads its row; the TTL worker's quorum read shares the derivation.

clippy.toml disallows the Session calls (query, query_with_values, exec,
exec_with_values, paged), StatementParamsBuilder::new,
BatchQueryBuilder::new and the cassandra_util helpers that run at the
driver default. The crate allows the lint outside data/, where clippy
counts 225 of those calls in the catalog, metadata, backup and bootstrap
code, and data/mod.rs turns it back on. The lint cannot see which
constructor a call passes, so a test in data/level_pairing_tests.rs
reads the statement sites under data/ whose CQL and level it can
resolve (121 on this tree) and checks that a SELECT carries a read
level, a plain write write_params and an IF statement lwt_params, and
that no BatchQueryBuilder::new, BatchQueryBuilder::default,
Default::default() or .with_consistency( appears under data/, so a
batch is built by batch_builder() or not at all. The integration test
crates allow the lint for their setup statements.

A base-row write now waits for a local quorum to acknowledge its commit,
and a ConsistentRead: true read needs a local quorum to answer: two of
three replicas at the default replication factor. The lightweight
transactions already needed a Paxos majority. The replication_factor and
datacenter docs in config.rs, the upgrade manual and the differences
table say so. ADR-0025 records the levels, the trade and the lint; the
storage and HA designs, the backend author manual and the ReadOptions
doc state the new levels and cite it.

Tests: unit tests check the levels each constructor sets. A direct
integration test reads an item through GetItem, Query and Scan with both
ConsistentRead values. A second raises the account keyspace to
replication factor 3 on a one-node cluster, where LOCAL_ONE can be met
and LOCAL_QUORUM cannot: the false reads return the item and the true
reads are refused. A third reads a row of a table with a two-part HASH
key.
The integration suites hardwired 127.0.0.1:9042 in the test configuration
and the readiness probe. Both now read EXTENDDB_TEST_CASSANDRA_ADDR as
host:port and fall back to 127.0.0.1:9042, so a suite can run against a
node on another port beside a node another job uses. The crate docs of
the two suites name the variable in place of the port.
crates/storage-cassandra/src/lib.rs: the base replaced CATALOG_VERSION
with DEFAULT_CONTROL_PLANE_DELAY_SECONDS, and the branch added the
crate-level allow of clippy::disallowed_methods above it. The merge
keeps the allow and the base's constant.

crates/storage-cassandra/src/data/ddl.rs: the branch dropped the
Consistency and BatchQueryBuilder imports from the index backfill,
which builds its batch with batch_builder(). The base added an
IntoRustByName import there. The merge keeps the IntoRustByName import
only.

crates/storage-cassandra/src/data/update_item.rs and put_get_item.rs:
the base rewrote the comment above the item batch to describe the
version stamps, and the branch built the batch with batch_builder().
The merge keeps the base's comment and batch_builder().

crates/storage-cassandra/src/data/delete_item.rs: in both delete paths
the base computes a delete stamp and builds the claimed-row batch at
the pinned mutation timestamp. The branch built the batch with
batch_builder() and set the timestamp only for a claimed row. The merge
keeps the base's code and builds the batch with batch_builder() at the
mutation timestamp. The base's LOCAL_SERIAL fence read in this file
passes Consistency::LocalSerial to select_by_pk_at, so the merge
restores the Consistency import.

crates/storage-cassandra/src/data/query.rs and query_helpers.rs: the
base replaced query_with_pk_sk_sk_sk and the per-shape calls with
query_with_pk_sks over the rendered sort key range. The branch passed
consistent_read to each helper. The merge keeps the base's helpers and
query paths and gives query_with_pk_sks a consistent_read parameter,
which it passes to query_rows_at, query_with_pk_sk and
query_with_pk_sk_sk. The hash-only index cursor read moves from
query_rows to query_rows_at. The branch's query_with_pk_sk_sk_sk is
superseded.

crates/storage-cassandra/src/data/mod.rs: the base deleted
query_with_item_ts_pk_sk_txnid and split select_by_pk into select_by_pk
and select_by_pk_at, which takes an optional level and reads at the
driver default without one. The branch gave
query_with_item_ts_pk_sk_txnid a params argument and made select_by_pk
read at read_consistency(consistent_read). The merge keeps the deletion
and the split. select_by_pk passes read_consistency(consistent_read) to
select_by_pk_at, which now takes a required level and builds its
parameters with the new cassandra_util::read_params_at.

crates/storage-cassandra/src/data/transactions.rs: the base binds the
next row version in the commit statements, reads the fenced row at
LOCAL_SERIAL on COMMIT and syncs index rows with version stamps. The
branch ran the commit statements with lwt_params and the commit batch
through batch_builder(). The merge keeps the base's statements, binds
and index sync, runs the commit statements through query_with_params
with lwt_params, and builds the batch with batch_builder().
fetch_row_for_transaction keeps its optional level, and without one it
reads at LOCAL_QUORUM, as the branch's read did.

crates/storage-cassandra/tests/common/mod.rs: both sides added a helper
that reads the node address from the environment. The base's
test_contact_point() and EXTENDDB_TEST_CASSANDRA_CONTACT_POINT, which
the Cassandra workflow sets, supersede the branch's
test_cassandra_addr() and EXTENDDB_TEST_CASSANDRA_ADDR. The merge keeps
the base's helper and reachability check, and points the doc comments
in common/mod.rs, direct_integration.rs and ttl_integration.rs at the
base's variable.

crates/storage-cassandra/tests/direct_integration.rs: each side added
a module. The merge keeps both.

docs/adr/README.md: the base added rows 0023, 0026 and 0027, and the
branch added row 0025. The merge lists all five in order. No other
branch uses 0025, so ADR-0025 keeps its number.

docs/manuals/07-upgrade-manual.md: each side added an entry at the top
of the behavior changes. The merge keeps the branch's Cassandra entry
first, then the base's two v0.1.15 entries.

Changes beyond the conflicts, for the build and the level contract:
cassandra_util gains read_params_at(level, values) with a unit test,
and read_params calls it. ADR-0025 and the data module docs name it.
The base's catalog read in recover_index_rows_by_table_id moves from
query_rows to query_rows_at at LOCAL_ONE, as the branch reads other
catalog rows. Two more select_by_pk_at calls in transactions.rs pass
Consistency::LocalSerial directly. The transact_stream.rs tests build
their batches with batch_builder(). The base's migrations.rs and
migrations_stopped.rs test binaries allow clippy::disallowed_methods,
as the branch's other test binaries do. The level pairing test lists
query_with_pk_sks in place of the removed query_with_pk_sk_sk_sk.
Brings the base branch, which carries origin/main at 38992eb, into
this branch. No file conflicted. The data path statements keep the
levels this branch gave them, and the pairing test over
`src/data/*.rs` still finds every statement paired with its level,
including the transaction code main changed for the backup catalog.
The catalog, metadata, backup and bootstrap code main added outside
`data/` runs at the driver default, as that code did before.
@LeeroyHannigan LeeroyHannigan added the v1 Needed for the 1.0 release label Nov 6, 2026
crates/storage-cassandra/src/config.rs keeps the note on the written replication_factor line from feat/read-options and the local quorum paragraph from this branch.
crates/storage-cassandra/tests/direct_integration.rs keeps the contact point sentence from feat/read-options and the disallowed_methods allow from this branch.
crates/storage-cassandra/tests/ttl_integration.rs keeps the integration-cassandra workflow sentence from feat/read-options and the disallowed_methods allow from this branch.
docs/manuals/07-upgrade-manual.md keeps the v0.1.15 sections from feat/read-options and the local quorum section from this branch.
Main added put_restored_item_at, which starts its batch with
BatchQueryBuilder::new() and sets LOCAL_QUORUM on it. This branch builds every
data path batch with cassandra_util::batch_builder, which sets the same level,
and its lint and level pairing test refuse any other constructor. The batch
now comes from batch_builder. It keeps its level and its timestamp.

Main also added the warm_throughput field to CreateTableInput. The table
literal in the consistent_read integration test sets it to None.

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

Main has moved under this branch. #945 (51cb18c, merged 2026-11-17) runs Cassandra reads and writes at LOCAL_QUORUM and closed #560, which this PR closes. A trial merge of this head into main 89fc971 conflicts in 14 files, ten of them under data/.

The two designs answer differently: main reads ConsistentRead: false at LOCAL_QUORUM, as its v1.0.0 upgrade manual says, and this head reads it at LOCAL_ONE. The lint reasons in clippy.toml, the #![allow] at lib.rs:14, ADR-0025 and the upgrade note describe a main that runs at ONE outside data/, which no longer holds. What this PR adds beyond #945 is LOCAL_ONE for false reads, the lint, the pairing test, and GetItem building the partition key from every HASH attribute (main's get_item_impl reads key_schema[0] alone).

Rebasing onto main and keeping only those parts, or closing this in favour of #945, comes first. I will review the rebased change when it is up; the items I have against this head (a 500 rather than a 503 when the quorum is lost, gaps in the new guards, and LOCAL_SERIAL across datacenters) carry over to it.

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

Main already runs Cassandra reads and writes at LOCAL_QUORUM since #945 (cassandra_util.rs maps Stmt::Read, Stmt::Write and Stmt::Lwt, with LOCAL_SERIAL for conditional statements), and #560 is closed. This branch conflicts with main in the data files under storage-cassandra and in engine/import_export.rs, and it targets feat/read-options, which main does not have, so ReadOptions is not available to rebase onto. I think the PR needs to be rebuilt on main and cut to what main lacks.

Two parts of what is left need a closer look.

  1. The description says GetItem on a table with two HASH attributes reads None today, and the new test builds such a table through the storage layer. On a build of main, CreateTable with two HASH attributes returns ValidationException "Second KeySchemaElement is not a RANGE type". Which request creates a base table with two HASH attributes? If there is none, I think this belongs out of the PR, or the test should say that it covers a state the API does not produce.

  2. The numbers in the description no longer match the code: the pairing test comment at level_pairing_tests.rs line 449 says 121 sites (the check is at least 110), the description says 90, and the lib test count at this head is 163, the description says 66.

I will approve once the change is rebased onto main and limited to what main does not already do, and the two-part key claim is either reachable through a request or removed.

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.

3 participants