Skip to content

Commit 31c3999

Browse files
TheSentinel454Elrondclaude
committed
buzz-db: read both recent-first passes in one snapshot and deadline
A split page read ran its recent and older passes as separate statements, each with its own READ COMMITTED snapshot and its own statement_timeout. A replacement (a replaceable event, an artifact head) that committed between them could move the live row from below the floor to above it, out of both passes, and a short page could take twice the read deadline. Split reads now run in one ReadTransaction: REPEATABLE READ, READ ONLY outside a transaction (a savepoint inside one; routed replica reads are already REPEATABLE READ). Each statement after the first runs under what is left of READ_STATEMENT_TIMEOUT. The channel window uses the same transaction. Single-statement reads keep their path, now through the same type. Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz> Co-Authored-By: Claude Opus 5.5 <[email protected]> Signed-off-by: Luke Tornquist <[email protected]>
1 parent 5bf0de0 commit 31c3999

4 files changed

Lines changed: 432 additions & 68 deletions

File tree

‎crates/buzz-db/src/store/event.rs‎

Lines changed: 232 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -571,15 +571,25 @@ pub(crate) async fn query_events_on(
571571
// Recent-first (see `recent_pass`): the newest partitions answer a page
572572
// that fills there; older rows are read only for what is still missing.
573573
let persistent = !has_tag_values(q);
574-
let floor = recent_pass_floor(q, Utc::now());
575574
let limit = page_limit(q);
576-
let mut rows = Vec::new();
577-
let mut pass = Some((crate::store::recent_pass::first(floor), limit));
578-
while let Some((range, pass_limit)) = pass {
579-
let mut qb = build_query_events_sql(q, range, pass_limit);
580-
rows.extend(fetch_with_read_deadline(conn, &mut qb, persistent, deadline).await?);
581-
pass = crate::store::recent_pass::next(range, rows.len(), limit);
582-
}
575+
let rows = match recent_pass_floor(q, Utc::now()) {
576+
None => {
577+
let mut qb = build_query_events_sql(q, Range::All, limit);
578+
fetch_with_read_deadline(conn, &mut qb, persistent, deadline).await?
579+
}
580+
Some(floor) => {
581+
let mut read = ReadTransaction::begin(conn, Some(deadline)).await?;
582+
let mut rows = Vec::new();
583+
let mut pass = Some((Range::Recent(floor), limit));
584+
while let Some((range, pass_limit)) = pass {
585+
let mut qb = build_query_events_sql(q, range, pass_limit);
586+
rows.extend(read.fetch(qb.build().persistent(persistent)).await?);
587+
pass = crate::store::recent_pass::next(range, rows.len(), limit);
588+
}
589+
read.finish().await?;
590+
rows
591+
}
592+
};
583593

584594
let mut out = Vec::with_capacity(rows.len());
585595
for row in rows {
@@ -899,11 +909,11 @@ fn push_mention_join(
899909
}
900910
}
901911

902-
/// [`READ_STATEMENT_TIMEOUT`] as a literal, so [`BEGIN_WITH_READ_DEADLINE_SQL`]
903-
/// can splice it into a static statement.
904-
macro_rules! read_statement_timeout {
912+
/// [`READ_STATEMENT_TIMEOUT`] in seconds, as a literal, so
913+
/// [`BEGIN_WITH_READ_DEADLINE_SQL`] can splice it into a static statement.
914+
macro_rules! read_statement_timeout_secs {
905915
() => {
906-
"20s"
916+
20
907917
};
908918
}
909919

@@ -913,8 +923,14 @@ macro_rules! read_statement_timeout {
913923
/// once no client can still be waiting for it. It only ever tightens the
914924
/// effective `statement_timeout`: a shorter operator or caller cap is kept,
915925
/// and a disabled (`0`) or longer one is lowered to 20 s. The limit is per
916-
/// statement, not per request.
917-
pub const READ_STATEMENT_TIMEOUT: &str = read_statement_timeout!();
926+
/// read, not per request: the passes of a split read share one (see
927+
/// [`ReadTransaction`]).
928+
pub const READ_STATEMENT_TIMEOUT: &str = concat!(read_statement_timeout_secs!(), "s");
929+
930+
/// [`READ_STATEMENT_TIMEOUT`] as a duration, for the budget a split read's
931+
/// later passes run under.
932+
const READ_DEADLINE: std::time::Duration =
933+
std::time::Duration::from_secs(read_statement_timeout_secs!());
918934

919935
/// Lower `statement_timeout` to `$1` unless it is already tighter; `$2`
920936
/// selects transaction-local (`true`) or session (`false`) scope.
@@ -923,17 +939,22 @@ pub(crate) const TIGHTEN_STATEMENT_TIMEOUT_SQL: &str = "SELECT set_config('state
923939
OR current_setting('statement_timeout')::interval > $1::text::interval \
924940
THEN $1::text ELSE current_setting('statement_timeout') END, $2)";
925941

926-
/// `BEGIN` plus the [`TIGHTEN_STATEMENT_TIMEOUT_SQL`] tighten to
942+
/// Opens a [`ReadTransaction`]: `REPEATABLE READ, READ ONLY`, so every
943+
/// statement in it reads one snapshot.
944+
const BEGIN_READ_SQL: &str = "BEGIN ISOLATION LEVEL REPEATABLE READ, READ ONLY";
945+
946+
/// [`BEGIN_READ_SQL`] plus the [`TIGHTEN_STATEMENT_TIMEOUT_SQL`] tighten to
927947
/// [`READ_STATEMENT_TIMEOUT`], transaction-local, as one simple-query message:
928948
/// one round trip instead of two.
929949
const BEGIN_WITH_READ_DEADLINE_SQL: &str = concat!(
930-
"BEGIN; SELECT set_config('statement_timeout', \
950+
"BEGIN ISOLATION LEVEL REPEATABLE READ, READ ONLY; \
951+
SELECT set_config('statement_timeout', \
931952
CASE WHEN current_setting('statement_timeout')::interval = interval '0' \
932953
OR current_setting('statement_timeout')::interval > interval '",
933-
read_statement_timeout!(),
934-
"' THEN '",
935-
read_statement_timeout!(),
936-
"' ELSE current_setting('statement_timeout') END, true)"
954+
read_statement_timeout_secs!(),
955+
"s' THEN '",
956+
read_statement_timeout_secs!(),
957+
"s' ELSE current_setting('statement_timeout') END, true)"
937958
);
938959

939960
/// How an event read runs under [`READ_STATEMENT_TIMEOUT`].
@@ -963,44 +984,109 @@ pub async fn tighten_session_read_statement_timeout(
963984
.map(|_| ())
964985
}
965986

966-
/// Run an event read under [`READ_STATEMENT_TIMEOUT`] without changing the
967-
/// deadline for anything else on `conn`. Inside an open transaction the read
968-
/// takes a savepoint, so a failed or cancelled read leaves the transaction
969-
/// usable and the read's locks are released when it ends. With
970-
/// [`ReadDeadline::PerRead`] the savepoint tightens the deadline, and an
971-
/// autocommit connection opens a transaction whose `BEGIN` carries the
972-
/// transaction-local tighten. Rolling either back (the read has no writes to
973-
/// keep) reverts the tighten.
987+
/// The statements of one event read, in one transaction that reads one
988+
/// snapshot under one deadline.
989+
///
990+
/// A read split at the recent-pass floor (see `recent_pass`) runs its passes
991+
/// as separate statements, and they must see the same rows: under
992+
/// `READ COMMITTED` a replacement that commits between them (a replaceable
993+
/// event, an artifact head) can move a live row from below the floor to
994+
/// above it, out of both passes. Outside a transaction this opens
995+
/// `REPEATABLE READ, READ ONLY`, whose snapshot every statement reads. Inside
996+
/// one it takes a savepoint, so a failed or cancelled statement leaves the
997+
/// transaction usable and its locks are released when the read ends; the
998+
/// callers that read inside a transaction are routed replica reads, already
999+
/// `REPEATABLE READ`.
1000+
///
1001+
/// With a [`ReadDeadline`] the read runs under [`READ_STATEMENT_TIMEOUT`]
1002+
/// without changing the deadline for anything else on the connection, and
1003+
/// its statements share it: each after the first runs under what the earlier
1004+
/// ones left. [`ReadDeadline::PerRead`] tightens the deadline in the
1005+
/// transaction or savepoint. Rolling it back (the read has no writes to keep)
1006+
/// reverts every tighten.
1007+
pub(crate) struct ReadTransaction<'c> {
1008+
tx: sqlx::Transaction<'c, sqlx::Postgres>,
1009+
deadline_from: Option<std::time::Instant>,
1010+
statements: usize,
1011+
}
1012+
1013+
impl<'c> ReadTransaction<'c> {
1014+
/// Open the read on `conn`; `deadline` `None` leaves `statement_timeout`
1015+
/// as it is.
1016+
pub(crate) async fn begin(
1017+
conn: &'c mut sqlx::PgConnection,
1018+
deadline: Option<ReadDeadline>,
1019+
) -> Result<Self> {
1020+
use sqlx::Connection as _;
1021+
let per_read = deadline == Some(ReadDeadline::PerRead);
1022+
let tx = if conn.is_in_transaction() {
1023+
let mut tx = conn.begin().await?;
1024+
if per_read {
1025+
tighten_read_deadline(&mut tx, READ_STATEMENT_TIMEOUT).await?;
1026+
}
1027+
tx
1028+
} else if per_read {
1029+
conn.begin_with(BEGIN_WITH_READ_DEADLINE_SQL).await?
1030+
} else {
1031+
conn.begin_with(BEGIN_READ_SQL).await?
1032+
};
1033+
Ok(Self {
1034+
tx,
1035+
deadline_from: deadline.map(|_| std::time::Instant::now()),
1036+
statements: 0,
1037+
})
1038+
}
1039+
1040+
/// Run one statement of the read.
1041+
pub(crate) async fn fetch(
1042+
&mut self,
1043+
query: sqlx::query::Query<'_, sqlx::Postgres, sqlx::postgres::PgArguments>,
1044+
) -> Result<Vec<sqlx::postgres::PgRow>> {
1045+
if let Some(from) = self.deadline_from.filter(|_| self.statements > 0) {
1046+
let left = READ_DEADLINE
1047+
.saturating_sub(from.elapsed())
1048+
.max(std::time::Duration::from_millis(1));
1049+
tighten_read_deadline(&mut self.tx, &format!("{}ms", left.as_millis())).await?;
1050+
}
1051+
self.statements += 1;
1052+
Ok(query.fetch_all(&mut *self.tx).await?)
1053+
}
1054+
1055+
/// End the read, rolling back everything it set.
1056+
pub(crate) async fn finish(self) -> Result<()> {
1057+
Ok(self.tx.rollback().await?)
1058+
}
1059+
}
1060+
1061+
/// Lower the transaction-local `statement_timeout` to `timeout`.
1062+
async fn tighten_read_deadline(
1063+
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
1064+
timeout: &str,
1065+
) -> Result<()> {
1066+
sqlx::query(TIGHTEN_STATEMENT_TIMEOUT_SQL)
1067+
.bind(timeout)
1068+
.bind(true)
1069+
.execute(&mut **tx)
1070+
.await?;
1071+
Ok(())
1072+
}
1073+
1074+
/// Run a single-statement event read as a [`ReadTransaction`], except on an
1075+
/// autocommit session already under the deadline, where the statement alone
1076+
/// is the read.
9741077
async fn fetch_with_read_deadline(
9751078
conn: &mut sqlx::PgConnection,
9761079
qb: &mut QueryBuilder<sqlx::Postgres>,
9771080
persistent: bool,
9781081
deadline: ReadDeadline,
9791082
) -> Result<Vec<sqlx::postgres::PgRow>> {
9801083
use sqlx::Connection as _;
981-
let mut tx = match (conn.is_in_transaction(), deadline) {
982-
(false, ReadDeadline::Session) => {
983-
return Ok(qb.build().persistent(persistent).fetch_all(conn).await?);
984-
}
985-
(false, ReadDeadline::PerRead) => conn.begin_with(BEGIN_WITH_READ_DEADLINE_SQL).await?,
986-
(true, _) => {
987-
let mut tx = conn.begin().await?;
988-
if deadline == ReadDeadline::PerRead {
989-
sqlx::query(TIGHTEN_STATEMENT_TIMEOUT_SQL)
990-
.bind(READ_STATEMENT_TIMEOUT)
991-
.bind(true)
992-
.execute(&mut *tx)
993-
.await?;
994-
}
995-
tx
996-
}
997-
};
998-
let rows = qb
999-
.build()
1000-
.persistent(persistent)
1001-
.fetch_all(&mut *tx)
1002-
.await?;
1003-
tx.rollback().await?;
1084+
if !conn.is_in_transaction() && deadline == ReadDeadline::Session {
1085+
return Ok(qb.build().persistent(persistent).fetch_all(conn).await?);
1086+
}
1087+
let mut read = ReadTransaction::begin(conn, Some(deadline)).await?;
1088+
let rows = read.fetch(qb.build().persistent(persistent)).await?;
1089+
read.finish().await?;
10041090
Ok(rows)
10051091
}
10061092

@@ -3581,6 +3667,101 @@ mod postgres_tests {
35813667
.expect("reset lock_timeout");
35823668
}
35833669

3670+
/// The passes of a split read share one snapshot: a replacement that
3671+
/// commits between them neither drops the row it replaces nor shows the
3672+
/// row it adds. Both deadline modes open the read the same way.
3673+
#[tokio::test]
3674+
#[ignore = "requires Postgres"]
3675+
async fn split_read_passes_share_one_snapshot() {
3676+
let pool = setup_pool().await;
3677+
let community_uuid = make_test_community(&pool).await;
3678+
let community = CommunityId::from_uuid(community_uuid);
3679+
for deadline in [ReadDeadline::PerRead, ReadDeadline::Session] {
3680+
let channel = make_test_channel(&pool, community_uuid, None).await;
3681+
let q = EventQuery {
3682+
kinds: Some(vec![9]),
3683+
channel_id: Some(channel),
3684+
limit: Some(10),
3685+
..EventQuery::for_community(community)
3686+
};
3687+
let (page, expected) = crate::test_support::replace_between_passes(
3688+
&pool,
3689+
community,
3690+
channel,
3691+
move |mut conn| async move {
3692+
query_events_on(&mut conn, &q, deadline)
3693+
.await
3694+
.expect("split read")
3695+
.into_iter()
3696+
.map(|stored| stored.event.id)
3697+
.collect::<Vec<_>>()
3698+
},
3699+
)
3700+
.await;
3701+
assert_eq!(page, expected, "{deadline:?}");
3702+
}
3703+
}
3704+
3705+
/// The statements of one read share its deadline: each after the first
3706+
/// runs under what the earlier ones left, and ending the read restores
3707+
/// the connection's own.
3708+
#[tokio::test]
3709+
#[ignore = "requires Postgres"]
3710+
async fn read_statements_share_one_deadline() {
3711+
use sqlx::Connection as _;
3712+
let pool = setup_pool().await;
3713+
let timeout_ms = || {
3714+
sqlx::query(
3715+
"SELECT (EXTRACT(EPOCH FROM current_setting('statement_timeout')::interval) \
3716+
* 1000)::float8",
3717+
)
3718+
};
3719+
let full = READ_DEADLINE.as_millis() as f64;
3720+
for case in ["per read", "session", "inside a transaction"] {
3721+
let mut conn = pool.acquire().await.expect("acquire").detach();
3722+
let deadline = match case {
3723+
"session" => {
3724+
tighten_session_read_statement_timeout(&mut conn)
3725+
.await
3726+
.expect("session deadline");
3727+
ReadDeadline::Session
3728+
}
3729+
_ => ReadDeadline::PerRead,
3730+
};
3731+
let mut outer = None;
3732+
let conn: &mut sqlx::PgConnection = if case == "inside a transaction" {
3733+
outer = Some(conn.begin().await.expect("begin outer"));
3734+
outer.as_mut().expect("outer")
3735+
} else {
3736+
&mut conn
3737+
};
3738+
let before: String = sqlx::query_scalar("SHOW statement_timeout")
3739+
.fetch_one(&mut *conn)
3740+
.await
3741+
.expect("show timeout");
3742+
let mut read = ReadTransaction::begin(&mut *conn, Some(deadline))
3743+
.await
3744+
.expect("begin read");
3745+
let first: f64 = read.fetch(timeout_ms()).await.expect("first")[0].get(0);
3746+
let second: f64 = read.fetch(timeout_ms()).await.expect("second")[0].get(0);
3747+
read.finish().await.expect("finish");
3748+
assert_eq!(
3749+
first, full,
3750+
"{case}: the first statement gets the whole deadline"
3751+
);
3752+
assert!(
3753+
(1.0..full).contains(&second),
3754+
"{case}: the second gets what is left, got {second} ms"
3755+
);
3756+
let after: String = sqlx::query_scalar("SHOW statement_timeout")
3757+
.fetch_one(&mut *conn)
3758+
.await
3759+
.expect("show timeout");
3760+
assert_eq!(after, before, "{case}: restored");
3761+
drop(outer);
3762+
}
3763+
}
3764+
35843765
/// Effective `statement_timeout` observed *inside* the read deadline, and
35853766
/// the value restored on `conn` afterwards.
35863767
async fn deadline_inside_and_after(conn: &mut sqlx::PgConnection) -> (String, String) {

‎crates/buzz-db/src/store/recent_pass.rs‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,17 @@
88
//! locks every backend contends on (`LWLock:LockManager`).
99
//!
1010
//! A newest-first page (`ORDER BY created_at DESC … LIMIT n`) usually lies in
11-
//! the newest partitions, so it is split at a floor ([`first`], [`next`]), the
12-
//! start of the previous UTC month:
11+
//! the newest partitions, so it is split at a floor ([`floor_for`], [`next`]),
12+
//! the start of the previous UTC month:
1313
//!
1414
//! 1. The *recent* pass reads `created_at >= floor`. When it returns `n` rows,
1515
//! they are the page: every row at or above the floor sorts before every
1616
//! row below it.
1717
//! 2. Otherwise the *older* pass reads `created_at < floor` for the rows
1818
//! still missing, and they follow the recent rows. The two ranges are
19-
//! disjoint, so no row is read twice and the page is exact.
19+
//! disjoint, so no row is read twice and the page is exact, provided both
20+
//! passes read one snapshot (the stores run them in one `REPEATABLE READ`
21+
//! transaction, `ReadTransaction`).
2022
//!
2123
//! The floor is a SQL literal, so Postgres prunes when it plans the statement,
2224
//! cached generic plans included. It changes once a month, so the
@@ -98,12 +100,6 @@ pub(crate) fn floor_for(
98100
(!latest_state_only && !since_prunes && !until_before_floor).then_some(floor)
99101
}
100102

101-
/// The first pass of a newest-first read split at `floor`, or of the whole
102-
/// read when there is no floor.
103-
pub(crate) fn first(floor: Option<DateTime<Utc>>) -> Range {
104-
floor.map_or(Range::All, Range::Recent)
105-
}
106-
107103
/// The pass to run after `range` once the read holds `found` of its `limit`
108104
/// rows, with that pass's limit: the older pass for the rows still missing
109105
/// after a short recent pass, otherwise none. `limit` is the SQL `LIMIT`.
@@ -238,7 +234,7 @@ mod tests {
238234
for split in [None, Some(floor)] {
239235
let mut passes = Vec::new();
240236
let mut rows = Vec::new();
241-
let mut pass = Some((first(split), limit));
237+
let mut pass = Some((split.map_or(Range::All, Range::Recent), limit));
242238
while let Some((range, n)) = pass {
243239
passes.push((range, n));
244240
rows.extend(

0 commit comments

Comments
 (0)