Skip to content
Open
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
21 changes: 21 additions & 0 deletions clippy.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
# Copyright 2026 ExtendDB contributors
# SPDX-License-Identifier: Apache-2.0
#
# The Cassandra data path states the consistency level of every statement it
# runs. Each entry below runs a statement at the driver default, `ONE`, or
# builds parameters or a batch at it. The extenddb-storage-cassandra crate
# allows them outside `src/data/`, and `src/data/mod.rs` turns the lint back on
# for the data path, where the only allow is on a schema statement.
disallowed-methods = [
{ path = "cdrs_tokio::cluster::session::Session::query_with_values", reason = "the data path states its consistency: use cassandra_util::{query_rows_at, query_optional_at, execute_quorum, query_lwt}, or query_with_params / execute_at with read_params, write_params or lwt_params" },
{ path = "cdrs_tokio::cluster::session::Session::query", reason = "the data path states its consistency: use cassandra_util::{query_rows_at, query_optional_at, execute_quorum, query_lwt}, or query_with_params / execute_at with read_params, write_params or lwt_params" },
{ path = "cdrs_tokio::query::BatchQueryBuilder::new", reason = "a batch starts at the driver default: the data path builds batches with cassandra_util::batch_builder" },
{ path = "cdrs_tokio::cluster::session::Session::exec", reason = "the data path states its consistency: run a prepared statement through execute_at with read_params, write_params or lwt_params" },
{ path = "cdrs_tokio::cluster::session::Session::exec_with_values", reason = "the data path states its consistency: run a prepared statement through execute_at with read_params, write_params or lwt_params" },
{ path = "cdrs_tokio::cluster::session::Session::paged", reason = "the data path states its consistency: page with query_rows_at and a paging state in the parameters" },
{ path = "cdrs_tokio::statement::StatementParamsBuilder::new", reason = "the data path builds statement parameters only through cassandra_util::{read_params, write_params, lwt_params}" },
{ path = "extenddb_storage_cassandra::cassandra_util::query_rows", reason = "runs at the driver default: the data path uses query_rows_at" },
{ path = "extenddb_storage_cassandra::cassandra_util::query_optional", reason = "runs at the driver default: the data path uses query_optional_at" },
{ path = "extenddb_storage_cassandra::cassandra_util::execute", reason = "runs at the driver default: the data path uses execute_quorum" },
{ path = "extenddb_storage_cassandra::cassandra_util::apply_lwt", reason = "runs at the driver default: the data path uses query_lwt or execute_at with lwt_params" },
]
1 change: 1 addition & 0 deletions crates/storage-cassandra/src/backup_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1978,6 +1978,7 @@ impl LegacyBackupReader for CassandraEngine {
None,
None,
None,
true,
)
.await
.map_err(CopyError::Failed)?;
Expand Down
280 changes: 232 additions & 48 deletions crates/storage-cassandra/src/cassandra_util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,11 @@

use cdrs_tokio::cluster::TcpConnectionManager;
use cdrs_tokio::cluster::session::Session;
use cdrs_tokio::consistency::Consistency;
use cdrs_tokio::frame::Envelope;
use cdrs_tokio::load_balancing::RoundRobinLoadBalancingStrategy;
use cdrs_tokio::query::QueryValues;
use cdrs_tokio::statement::{StatementParams, StatementParamsBuilder};
use cdrs_tokio::transport::TransportTcp;
use cdrs_tokio::types::rows::Row;
use std::sync::Arc;
Expand All @@ -17,25 +20,162 @@ pub type CassandraSession = Session<
RoundRobinLoadBalancingStrategy<TransportTcp, TcpConnectionManager>,
>;

/// Execute a lightweight transaction with explicit regional quorum and serial
/// consistency. TTL claims and lifecycle metadata use this rather than the
/// driver's default consistency.
pub async fn query_lwt(
session: &CassandraSession,
query: &str,
values: QueryValues,
) -> Result<cdrs_tokio::frame::Envelope, extenddb_storage::error::StorageError> {
use cdrs_tokio::consistency::Consistency;
use cdrs_tokio::statement::StatementParamsBuilder;
// ══════════════════════════════════════════════════════════════════════════════
// Statement consistency for the data path
// ══════════════════════════════════════════════════════════════════════════════
//
// Every statement under `data/` states its level through `read_params`,
// `read_params_at`, `write_params` or `lwt_params`; schema statements in
// `data/ddl.rs` are the one exception. `clippy.toml` disallows the driver calls that run at the
// driver default, `Consistency::One`, and `data/mod.rs` turns that lint on for
// the data path.

/// The level a data-path read runs at for a request's `ConsistentRead`.
///
/// Base-row writes and lightweight transactions are acknowledged at
/// `LOCAL_QUORUM` ([`write_params`], [`lwt_params`]). A `LOCAL_QUORUM` read
/// intersects that write set within the datacenter, because a quorum plus a
/// quorum exceeds the replication factor, so a `true` read reflects every
/// write acknowledged before it started. `LOCAL_ONE` reads one replica and
/// may miss recently acknowledged writes, which is the contract for `false`.
#[must_use]
pub const fn read_consistency(consistent_read: bool) -> Consistency {
if consistent_read {
Consistency::LocalQuorum
} else {
Consistency::LocalOne
}
}

let params = StatementParamsBuilder::new()
/// Parameters for a data-path read at [`read_consistency`].
#[must_use]
pub fn read_params(consistent_read: bool, values: QueryValues) -> StatementParams {
read_params_at(read_consistency(consistent_read), values)
}

/// Parameters for a data-path read at `consistency`: a level
/// [`read_consistency`] returns, or `LOCAL_SERIAL` for a read that must see
/// every lightweight transaction that has committed, and complete one still
/// in progress, before it answers (a COMMIT reading its own fenced row).
#[must_use]
pub fn read_params_at(consistency: Consistency, values: QueryValues) -> StatementParams {
debug_assert!(
matches!(
consistency,
Consistency::LocalQuorum | Consistency::LocalOne | Consistency::LocalSerial
),
"a data-path read runs at LOCAL_QUORUM, LOCAL_ONE or LOCAL_SERIAL, not {consistency:?}"
);
StatementParamsBuilder::new()
.with_consistency(consistency)
.with_values(values)
.build()
}

/// Parameters for a data-path write with no `IF` clause, acknowledged at
/// `LOCAL_QUORUM`.
#[must_use]
pub fn write_params(values: QueryValues) -> StatementParams {
StatementParamsBuilder::new()
.with_consistency(Consistency::LocalQuorum)
.with_values(values)
.build()
}

/// A batch of plain writes, acknowledged at `LOCAL_QUORUM`. `Session::batch`
/// sends the builder's own consistency, and `BatchQueryBuilder::new()` starts
/// at the driver default, so every batch of the data path is built here.
#[must_use]
#[allow(clippy::disallowed_methods)] // the one place a batch builder is created
pub fn batch_builder() -> cdrs_tokio::query::BatchQueryBuilder {
cdrs_tokio::query::BatchQueryBuilder::new().with_consistency(Consistency::LocalQuorum)
}

/// Parameters for a lightweight transaction (a statement with an `IF`
/// clause): the Paxos round runs at `LOCAL_SERIAL` and the commit is
/// acknowledged at `LOCAL_QUORUM`.
#[must_use]
pub fn lwt_params(values: QueryValues) -> StatementParams {
StatementParamsBuilder::new()
.with_consistency(Consistency::LocalQuorum)
.with_serial_consistency(Consistency::LocalSerial)
.with_values(values)
.build();
.build()
}

/// Execute one statement with parameters from [`read_params`],
/// [`write_params`] or [`lwt_params`] and return the response envelope, from
/// which a lightweight transaction's `[applied]` column is read with
/// [`lwt_applied`].
///
/// # Errors
/// Returns an error if Cassandra does not execute the statement at the stated
/// level.
pub async fn execute_at<E: FromDbError>(
session: &CassandraSession,
query: &str,
params: StatementParams,
context: &str,
) -> Result<Envelope, E> {
session
.query_with_params(query, params)
.await
.map_err(|e| E::db_error(format!("{context}: {e}")))
}

/// Execute a data-path read at [`read_consistency`] and return all rows.
///
/// # Errors
/// Returns an error if the Cassandra query fails or the response cannot be parsed.
pub async fn query_rows_at<E: FromDbError>(
session: &CassandraSession,
query: &str,
values: QueryValues,
consistent_read: bool,
context: &str,
) -> Result<Vec<Row>, E> {
let result = session
.query_with_params(query, read_params(consistent_read, values))
.await
.map_err(|e| {
tracing::error!("{context} query failed: {e}");
E::db_error(format!("{context}: {e}"))
})?;

let body = result.response_body().map_err(|e| {
tracing::error!("{context} response_body failed: {e}");
E::db_error(format!("{context} response_body: {e}"))
})?;

Ok(body.into_rows().unwrap_or_default())
}

/// [`query_rows_at`], returning the first row, if any.
///
/// # Errors
/// Returns an error if the Cassandra query fails or the response cannot be parsed.
pub async fn query_optional_at<E: FromDbError>(
session: &CassandraSession,
query: &str,
values: QueryValues,
consistent_read: bool,
context: &str,
) -> Result<Option<Row>, E> {
let rows = query_rows_at(session, query, values, consistent_read, context).await?;
Ok(rows.into_iter().next())
}

/// Execute a lightweight transaction at [`lwt_params`]: a statement with an
/// `IF` clause runs with these parameters, through this helper or through
/// `query_with_params` and the `data/mod.rs` helpers.
pub async fn query_lwt(
session: &CassandraSession,
query: &str,
values: QueryValues,
) -> Result<Envelope, extenddb_storage::error::StorageError> {
session
.query_with_params(query, lwt_params(values))
.await
.map_err(|error| {
extenddb_storage::error::StorageError::Internal(format!("LWT query failed: {error}"))
})
Expand Down Expand Up @@ -85,7 +225,11 @@ pub fn is_fk_violation(_err: &cdrs_tokio::error::Error) -> bool {
// Phase 1: Core Query Helpers
// ══════════════════════════════════════════════════════════════════════════════

/// Execute a query and return all rows with standardized error handling.
/// Execute a query at the driver's default consistency, `ONE`, and return all
/// rows with standardized error handling.
///
/// For catalog, metadata and backup reads outside the data path. A read under
/// `data/` states its level with [`query_rows_at`] instead.
///
/// # Errors
/// Returns an error if the Cassandra query fails or the response cannot be parsed.
Expand All @@ -111,7 +255,8 @@ pub async fn query_rows<E: FromDbError>(
Ok(body.into_rows().unwrap_or_default())
}

/// Execute a query at `LOCAL_QUORUM` and return all rows.
/// Execute a query at `LOCAL_QUORUM` and return all rows: [`query_rows_at`]
/// with `consistent_read` set.
///
/// [`query_rows`] uses the driver's default consistency, which is `ONE`. Use
/// this instead wherever an *empty* result is treated as authoritative and acted
Expand All @@ -124,27 +269,7 @@ pub async fn query_rows_quorum<E: FromDbError>(
values: QueryValues,
context: &str,
) -> Result<Vec<Row>, E> {
use cdrs_tokio::consistency::Consistency;
use cdrs_tokio::statement::StatementParamsBuilder;

let params = StatementParamsBuilder::new()
.with_consistency(Consistency::LocalQuorum)
.with_values(values)
.build();
let result = session
.query_with_params(query, params)
.await
.map_err(|e| {
tracing::error!("{context} quorum query failed: {e}");
E::db_error(format!("{context}: {e}"))
})?;

let body = result.response_body().map_err(|e| {
tracing::error!("{context} response_body failed: {e}");
E::db_error(format!("{context} response_body: {e}"))
})?;

Ok(body.into_rows().unwrap_or_default())
query_rows_at(session, query, values, true, context).await
}

/// Execute a query at `LOCAL_SERIAL` and return all rows: the read sees
Expand Down Expand Up @@ -182,7 +307,8 @@ pub async fn query_rows_serial<E: FromDbError>(

/// Execute a query and return the first row, if any.
///
/// Similar to `sqlx::query().fetch_optional()`.
/// Similar to `sqlx::query().fetch_optional()`. Runs at the driver's default
/// consistency like [`query_rows`]; under `data/` use [`query_optional_at`].
///
/// # Errors
/// Returns an error if the Cassandra query fails or the response cannot be parsed.
Expand All @@ -196,7 +322,8 @@ pub async fn query_optional<E: FromDbError>(
Ok(rows.drain(..).next())
}

/// Execute a non-query statement (INSERT/UPDATE/DELETE).
/// Execute a non-query statement (INSERT/UPDATE/DELETE) at the driver's
/// default consistency; under `data/` use [`execute_quorum`].
///
/// # Errors
/// Returns an error if the Cassandra statement fails.
Expand All @@ -217,10 +344,10 @@ pub async fn execute<E: FromDbError>(
Ok(())
}

/// Execute a non-query statement at `LOCAL_QUORUM`.
/// Execute a non-query statement at [`write_params`] (`LOCAL_QUORUM`).
///
/// Use this for writes that are prerequisites for a later quorum operation or
/// whose loss would make an absence decision unsafe.
/// A plain write of the data path runs with [`write_params`]; this helper
/// runs one that returns no rows. A batch runs through [`batch_builder`].
///
/// # Errors
/// Returns an error if Cassandra does not acknowledge the statement at quorum.
Expand All @@ -230,15 +357,8 @@ pub async fn execute_quorum<E: FromDbError>(
values: QueryValues,
context: &str,
) -> Result<(), E> {
use cdrs_tokio::consistency::Consistency;
use cdrs_tokio::statement::StatementParamsBuilder;

let params = StatementParamsBuilder::new()
.with_consistency(Consistency::LocalQuorum)
.with_values(values)
.build();
session
.query_with_params(query, params)
.query_with_params(query, write_params(values))
.await
.map_err(|error| {
tracing::error!("{context} quorum execute failed: {error}");
Expand Down Expand Up @@ -376,6 +496,12 @@ pub fn lwt_applied(
})
}

/// Execute a lightweight transaction at the driver's default consistency and
/// return whether it applied.
///
/// For catalog and management writes outside the data path; a conditional
/// write under `data/` runs with [`lwt_params`] instead, through
/// [`query_lwt`], [`execute_at`] or `query_with_params`.
///
/// Parses the `[applied]` column from the Cassandra LWT response.
/// Use for `INSERT ... IF NOT EXISTS` and `UPDATE ... IF ...` statements.
Expand Down Expand Up @@ -449,3 +575,61 @@ fn lwt_applied_flag<E: FromDbError>(
.and_then(|row| row.get_r_by_name("[applied]").ok())
.unwrap_or(false))
}

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

fn bound() -> QueryValues {
cdrs_tokio::query_values!("pk", 7_i64)
}

#[test]
fn consistent_read_selects_local_quorum_and_eventual_selects_local_one() {
assert_eq!(read_consistency(true), Consistency::LocalQuorum);
assert_eq!(read_consistency(false), Consistency::LocalOne);
}

#[test]
fn read_params_carry_the_read_level_and_no_serial_level() {
for (consistent_read, level) in [
(true, Consistency::LocalQuorum),
(false, Consistency::LocalOne),
] {
let params = read_params(consistent_read, bound()).query_params;
assert_eq!(params.consistency, level);
assert_eq!(params.serial_consistency, None);
assert_eq!(params.values, Some(bound()));
}
}

#[test]
fn read_params_at_carry_the_level_they_are_given_and_no_serial_level() {
for level in [
Consistency::LocalQuorum,
Consistency::LocalOne,
Consistency::LocalSerial,
] {
let params = read_params_at(level, bound()).query_params;
assert_eq!(params.consistency, level);
assert_eq!(params.serial_consistency, None);
assert_eq!(params.values, Some(bound()));
}
}

#[test]
fn write_params_are_local_quorum_with_no_serial_level() {
let params = write_params(bound()).query_params;
assert_eq!(params.consistency, Consistency::LocalQuorum);
assert_eq!(params.serial_consistency, None);
assert_eq!(params.values, Some(bound()));
}

#[test]
fn lwt_params_commit_at_local_quorum_and_run_paxos_at_local_serial() {
let params = lwt_params(bound()).query_params;
assert_eq!(params.consistency, Consistency::LocalQuorum);
assert_eq!(params.serial_consistency, Some(Consistency::LocalSerial));
assert_eq!(params.values, Some(bound()));
}
}
Loading
Loading