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
1 change: 1 addition & 0 deletions crates/storage-cassandra/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub mod index;
mod put_get_item;
mod query;
mod query_helpers;
mod rows;
mod scan;
mod sk_range;
mod transact_stream;
Expand Down
187 changes: 187 additions & 0 deletions crates/storage-cassandra/src/data/rows.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,187 @@
// Copyright 2026 ExtendDB contributors
// SPDX-License-Identifier: Apache-2.0

//! Data rows for the Cassandra Scan and Query: the item a row holds, reads
//! that go on after the last row read, and reads that Cassandra refuses.
//!
//! A partition whose items have all been deleted answers a read with one row
//! of its static columns. A TTL claim on an absent key, and a transaction
//! prepare that lands on an item deleted after the transaction read it, leave
//! a row with no `item_data`. The reads skip such rows. When every row of a
//! full read holds no item, the scan reads on after the last row read (its
//! partition key and, for a row with one, its sort key), with up to twice as
//! many rows each time and at most [`RUN_READ_ROWS`] (or the read's own
//! LIMIT, if larger). Cassandra counts a tombstone for each deleted row in
//! the partitions a read passes, so every read of a call, the first one
//! included, that Cassandra refuses with a read failure (for example
//! `READ_TOO_MANY_TOMBSTONES`) is tried again with half as many rows, down to
//! one, and the later reads in the same call stay at that size.

use cdrs_tokio::error::Error as CdrsError;
use cdrs_tokio::frame::message_error::ErrorType;
use cdrs_tokio::query::QueryValues;
use cdrs_tokio::types::IntoRustByName;
use cdrs_tokio::types::blob::Blob;
use cdrs_tokio::types::decimal::Decimal;
use cdrs_tokio::types::value::Value;
use extenddb_core::types::{Item, ScalarAttributeType};
use extenddb_storage::error::StorageError;
use extenddb_storage::util::sk_column;
use std::sync::Arc;

use super::json_to_item;
use crate::cassandra_util::{CassandraSession, PolicySession, Stmt};

/// The most rows one read takes while a read goes on through rows that hold
/// no item, unless the read's own LIMIT is larger.
pub(super) const RUN_READ_ROWS: usize = 1_000;

pub(super) type Row = cdrs_tokio::types::rows::Row;

/// A clustering predicate after `pk = ? AND`, and the values it binds.
pub(super) type Clustering = (String, Vec<Value>);

/// A position to read on after: a partition key text, and a clustering
/// predicate when the read goes on inside that partition.
pub(super) type Position = (String, Option<Clustering>);

/// The item `row` holds, or `None` for a row without `item_data`.
pub(super) fn row_item(row: &Row, context: &str) -> Result<Option<Item>, StorageError> {
let item_data: Option<String> = row.get_by_name("item_data").map_err(|error| {
StorageError::Internal(format!("{context} parse item_data failed: {error}"))
})?;
item_data.map(json_to_item).transpose()
}

/// The position of a base-table row, to read on after it: its partition key
/// text, and its sort key as a clustering predicate when the row has one. A
/// static-only row has no sort key, and neither does a hash-only table's row.
pub(super) fn row_position(
row: &Row,
sk_type: Option<ScalarAttributeType>,
) -> Result<Position, StorageError> {
let parse =
|error: CdrsError| StorageError::Internal(format!("scan parse row key failed: {error}"));
let pk: Option<String> = row.get_by_name("pk").map_err(parse)?;
let pk = pk.ok_or_else(|| StorageError::Internal("scan row without pk".to_owned()))?;
let Some(sk_type) = sk_type else {
return Ok((pk, None));
};
let col = sk_column(sk_type);
let value = match sk_type {
ScalarAttributeType::S => IntoRustByName::<String>::get_by_name(row, col)
.map_err(parse)?
.map(Value::from),
ScalarAttributeType::N => IntoRustByName::<Decimal>::get_by_name(row, col)
.map_err(parse)?
.map(Value::from),
ScalarAttributeType::B => IntoRustByName::<Blob>::get_by_name(row, col)
.map_err(parse)?
.map(Value::from),
};
Ok((pk, value.map(|v| (format!("{col} > ?"), vec![v]))))
}

/// Runs one read at the consistency level of [`Stmt::Read`] and returns its
/// rows, keeping the driver's error.
async fn rows_of(
session: &Arc<CassandraSession>,
query: &str,
values: Vec<Value>,
context: &'static str,
) -> Result<Vec<Row>, (&'static str, CdrsError)> {
let result = session
.run(Stmt::Read, query, QueryValues::SimpleValues(values))
.await
.map_err(|e| (context, e))?;
let body = result.response_body().map_err(|e| (context, e))?;
Ok(body.into_rows().unwrap_or_default())
}

/// Up to `limit` rows of a scan within the inclusive `token_bounds` (the
/// whole ring when `None`). With no `position`, the rows from the lower
/// bound. Else the rows after `position`: the rest of its partition past its
/// clustering predicate (query 1, when it has one), then the partitions after
/// it in token order up to the upper bound (query 2).
pub(super) async fn scan_rows(
session: &Arc<CassandraSession>,
select_prefix: &str,
position: Option<&Position>,
token_bounds: Option<(i64, i64)>,
limit: usize,
) -> Result<Vec<Row>, (&'static str, CdrsError)> {
let Some((pk_text, clustering)) = position else {
let (bounds, values) = match token_bounds {
Some((lo, hi)) => (
" WHERE token(pk) >= ? AND token(pk) <= ?",
vec![Value::from(lo), Value::from(hi)],
),
None => ("", Vec::new()),
};
let query = format!("{select_prefix}{bounds} LIMIT {limit}");
return rows_of(session, &query, values, "scan_first_page").await;
};
let mut all_rows = Vec::new();

if let Some((clustering_predicate, clustering_binds)) = clustering {
let query1 =
format!("{select_prefix} WHERE pk = ? AND {clustering_predicate} LIMIT {limit}");
let mut values: Vec<Value> = Vec::with_capacity(1 + clustering_binds.len());
values.push(Value::from(pk_text.as_str()));
values.extend(clustering_binds.iter().cloned());
all_rows.extend(rows_of(session, &query1, values, "scan_finish_partition").await?);
}

if all_rows.len() < limit {
let remaining = limit - all_rows.len();
let token_clause = if token_bounds.is_some() {
" AND token(pk) <= ?"
} else {
""
};
let query2 =
format!("{select_prefix} WHERE token(pk) > token(?){token_clause} LIMIT {remaining}");
let mut values: Vec<Value> = vec![Value::from(pk_text.as_str())];
if let Some((_, hi)) = token_bounds {
values.push(Value::from(hi));
}
all_rows.extend(rows_of(session, &query2, values, "scan_next_partitions").await?);
}

Ok(all_rows)
}

/// Runs `read` with `*limit` rows. A read Cassandra refuses with a read
/// failure runs again with half the rows, down to one; `*limit` ends as the
/// size that was read and `*ceiling` is lowered to it, so the reads after it
/// do not grow back past it.
pub(super) async fn read_halving<F, Fut>(
limit: &mut usize,
ceiling: &mut usize,
mut read: F,
) -> Result<Vec<Row>, StorageError>
where
F: FnMut(usize) -> Fut,
Fut: Future<Output = Result<Vec<Row>, (&'static str, CdrsError)>>,
{
loop {
match read(*limit).await {
Ok(rows) => return Ok(rows),
Err((context, CdrsError::Server { body, .. }))
if *limit > 1 && matches!(body.ty, ErrorType::ReadFailure(_)) =>
{
tracing::warn!(
rows = *limit,
"{context}: read failure ({}), reading again with half the rows",
body.message
);
*limit /= 2;
*ceiling = *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.

No test pins the two read-size rules: with *ceiling = *limit; removed, or the doubling at scan.rs:258 removed, all 10 new tests pass. Without the ceiling, Cassandra logged the tombstone failure 9 times in one run instead of 2.

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.

a3057fe adds two checks that count range reads in system_views.local_scan_latency, which counts a refused read too.

test_scan_past_a_long_run_of_emptied_partitions now requires each pass over the 5,000 emptied partitions to take at most 100 range reads (53 with Limit 1). With the doubling removed, the Limit 1 pass takes 2,504 and the test fails.

test_scan_after_items_past_partitions_with_many_deleted_rows requires 6 range reads for the call after the fifth item: 1,025, 512 and 256 rows refused, then 128, 128 and 44. With *ceiling = *limit; removed, the read after the first 128 rows takes up to 256 rows, the call makes 5 reads, and the test fails.

}
Err((context, error)) => {
tracing::error!("{context} query failed: {error}");
return Err(StorageError::Internal(format!("{context}: {error}")));
}
}
}
}
Loading
Loading