Files
buzz/crates/buzz-db/src/test_support.rs
Luke Tornquist 74abe2f0fe perf(db): split newest-first events reads at a month floor so Postgres prunes partitions (#9091)
## Summary

Newest-first `events` page reads are now split at the start of the
previous UTC month, so Postgres can prune the older partitions whenever
the newest ones fill the page. Results are unchanged.

**Why.** `events` is range-partitioned by month, and every partition
carries the full index set. A page read with no `created_at` lower bound
can't prune, so Postgres plans and locks every partition and every
partition index: about 200 relations per read with 19 partitions. A
backend has 16 fast-path lock slots, and every lock past those goes
through the shared lock table. On a production-shaped load rig at pool
200, active backends spent 36% of their time waiting on
`LWLock:LockManager` and 1.6% on disk.

**What changes.** `query_events_on` (REQ history, bridge `/query`,
mentions, rosters, notices) and `get_channel_window_on` (channel
windows) now read in up to two passes on the same connection, through
one helper (`store/recent_pass.rs`):

1. **Recent pass:** the same read plus `created_at >= <floor>`, where
the floor is the start of the previous UTC month. When it returns a full
page, that page is the answer, because every row at or above the floor
sorts before every row below it.
2. **Older pass,** only when the recent pass comes up short: `created_at
< <floor>`, limited to the rows still missing, appended after the recent
rows. The ranges don't overlap, so no row is read twice and the page is
exact. For channel windows the limit includes the +1 probe row, so
`has_more` stays exact.

**One snapshot, one deadline.** The two passes run in one `REPEATABLE
READ, READ ONLY` transaction (`ReadTransaction`), so both read the same
snapshot. Under `READ COMMITTED`, a replacement committing between the
passes (a replaceable event, an artifact head) could move the live row
from below the floor to above it, and it would land in neither pass.
Inside a transaction the passes run in a savepoint instead; the only
callers that read inside a transaction are routed replica reads, which
are already `REPEATABLE READ`. Each pass after the first runs under
whatever is left of the 20 s read deadline, so a split read never gets
more time than an unsplit one. Single-statement reads (COUNT, unsplit
pages) keep their old path, now through the same type. Channel windows
now take the read deadline too. On a routed reader that is the reader's
session deadline. On the writer they used to run under the writer's own
session timeout (2 minutes), and now get the 20 s read deadline, like
every other event read there.

The floor is a SQL literal, not a bind parameter, so Postgres prunes
when it plans the statement, cached generic plans included. It changes
once a month, so the statement cache holds one statement per pass and
shape. `#p` reads put the same bound on
`event_mentions.event_created_at`.

These reads are not split:
- Reads whose own bounds already prune (`since` at or after the floor)
or fall entirely before the floor (`until` before it).
- `OFFSET` reads, since the offset spans both sides of the floor.
- Lookups that match a fixed set of rows wherever they sit: `ids`, `#e`,
`#d`, artifact `d` tags.
- Fetch-everything reads (`max_limit`, the COUNT fallback).
- Reads limited to replaceable or addressable kinds. Those are
latest-state rows, often old and few, so a recent pass would rarely fill
the page.

No protocol, schema, index or client change.

**Not covered:** COUNT and the per-row aux `#e` reads, neither of which
has a page to fill. The recent pass also has no upper bound, because
rows dated in the future must stay visible. So partitions ahead of the
current month (the manager keeps 6) and the catch-all are still planned.

## Evidence

A production-shaped load rig (6,600 desktop clients connecting cold at
100/s, against a 19-partition `events` table), comparing `main` with
this branch. Each row is one arm, so expect run-to-run noise of about
±10%.

| Arm | Postgres exec time (top statements) | Errors | Relation locks
outside fast path (avg) |
|---|---|---|---|
| Pool 200 + channel-first index, `main` | 13,217 s | 4.75% | 9,481 |
| Pool 200 + channel-first index, this PR | 10,931 s | 3.68% | 4,988 |
| Default pool, no extra index, `main` | 6,705 s | 5.96% | — |
| Default pool, no extra index, this PR | 6,145 s | 5.15% | — |
| Default pool + index, `main` (repeat) | 1,070 s | 0.99% | — |
| Default pool + index, this PR (repeat) | 1,013 s | 0.93% | — |

Pages that fill above the floor stop planning old partitions, and that
halves the locks taken outside the fast path. `LWLock:LockManager` stays
at about 41% of active backend samples at pool 200, though. The dominant
remaining cost there is the cold unread repair: a multi-channel read
with no `since` and a page that never fills. That read needs its own fix
(a lookback bound or a better plan) and is out of scope here.

An earlier version re-ran the whole read after a short recent pass. That
doubled the work on non-filling reads: errors went from 4.75% to 11.88%
at pool 200. This version reads only the missing older rows.

## Tests

- `recent_pass::tests`: floor arithmetic (including the year wrap), the
predicate literals, an eligibility table, and an in-memory exactness
test that drives the passes at every limit and pins which passes run
with which limits.
-
`e_tag_filter_shape_tests::split_passes_bound_created_at_with_a_literal`
/ `recent_pass_skips_lookups`: SQL shape (both the `e.` and `m.` sides
for `#p`, no bound on unsplit reads or COUNT) and the exclusions,
including offset.
- `postgres_tests::split_page_reads_match_the_unsplit_read` (buzz-db
event): rows on both sides of the floor. A full recent page is answered
alone; short pages at limits 4, 5 and 6 add exactly the missing older
rows; offset reads across the floor are not split; a cursor below the
floor works. It reads `pg_prepared_statements` to show which passes ran.
With another session locking a partition only the older pass plans (and
a short `lock_timeout`), a full recent page still answers and a short
one returns `Err` rather than a partial page.
- `postgres_tests::channel_window_split_matches_the_unsplit_read`
(buzz-db thread): the same checks for channel windows, including
`has_more` when the recent pass has no probe row, and an `Err` when the
older pass fails.
- `split_read_passes_share_one_snapshot` (event, both deadline modes)
and `channel_window_passes_share_one_snapshot` (thread): another session
holds a lock that only the older pass needs, so the read stops between
its passes. A replacement then commits that deletes a row below the
floor and makes its replacement live above it. The page must still be
the first pass's snapshot: the replaced row is present and its
replacement is absent.
- `read_statements_share_one_deadline`: per-read, session and
inside-a-transaction reads. The second statement runs under less than
the full deadline, and finishing the read restores the connection's own
timeout.
- `channel_window_passes_share_one_deadline` (thread): through the
production writer entry. The recent pass waits 8 s on a lock and the
older pass is blocked for good. The window must be cancelled within the
one shared deadline, not a full deadline after its older pass starts.
- `channel_scoped_mention_reads_share_one_prepared_statement` now
expects one recent and one older statement, shared by every channel
count.

Mutation-checked. Each of these mutants fails a new Postgres test:
dropping the older pass, giving the older pass the full limit, removing
the split, splitting offset reads, dropping the window's probe row from
the split limit, removing the window's split, swallowing an older-pass
error in either path, opening the split read `READ COMMITTED` (events
and windows), opening it with a plain `BEGIN`, giving every pass the
full deadline, and running a window with no read deadline.

## What got simpler

Nothing got simpler overall. This adds one small module
(`store/recent_pass.rs`) shared by both read paths. The single-statement
deadline helper is now a thin wrapper over `ReadTransaction`, so the
begin, savepoint and tighten logic lives in one place. The alternative,
a `since` in every client plus a new index per read shape, means more
code in more places and does nothing for app versions already shipped.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

---------

Signed-off-by: Luke Tornquist <tornquist@squareup.com>
Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-05 16:03:15 -05:00

625 lines
23 KiB
Rust

const DEFAULT_DATABASE_URL: &str = "postgres://buzz:buzz_dev@localhost:5432/buzz"; // sadscan:disable np.postgres.1 -- local test-only credentials
/// Resolve the database URL shared by PostgreSQL-backed unit tests.
pub(crate) fn database_url() -> String {
std::env::var("BUZZ_TEST_DATABASE_URL")
.or_else(|_| std::env::var("TEST_DATABASE_URL"))
.or_else(|_| std::env::var("DATABASE_URL"))
.unwrap_or_else(|_| DEFAULT_DATABASE_URL.to_owned())
}
/// A uniquely named database on the test server that is dropped when the
/// guard goes out of scope, including when the test panics.
///
/// The drop uses `WITH (FORCE)`, which ends every session still connected,
/// so pools on the database need not be closed first. Bind the guard to a
/// named variable for the whole test: `let (pool, _) = ScratchDb::migrated(..)`
/// drops the database immediately.
pub(crate) struct ScratchDb {
name: String,
url: String,
admin_url: String,
}
impl ScratchDb {
/// Create an empty database named `{prefix}_{uuid}` on the server
/// `admin_url` connects to.
pub(crate) async fn create(admin_url: &str, prefix: &str) -> Self {
use sqlx::Connection as _;
let name = format!("{prefix}_{}", uuid::Uuid::new_v4().simple());
let mut admin = sqlx::PgConnection::connect(admin_url)
.await
.expect("connect admin database");
sqlx::query(sqlx::AssertSqlSafe(format!("CREATE DATABASE {name}")))
.execute(&mut admin)
.await
.expect("create scratch database");
let _ = admin.close().await;
// Swap the database path segment of the admin URL for the scratch name.
let idx = admin_url.rfind('/').expect("db url has a path segment");
let url = format!("{}/{name}", &admin_url[..idx]);
Self {
name,
url,
admin_url: admin_url.to_owned(),
}
}
/// [`ScratchDb::create`], then connect a pool and run every migration.
pub(crate) async fn migrated(admin_url: &str, prefix: &str) -> (sqlx::PgPool, Self) {
let scratch = Self::create(admin_url, prefix).await;
let pool = scratch.connect().await;
crate::migration::run_migrations(&pool)
.await
.expect("migrate scratch db");
(pool, scratch)
}
/// [`ScratchDb::create`], then connect a pool and run migrations through
/// `target`.
pub(crate) async fn migrated_through(
admin_url: &str,
prefix: &str,
target: i64,
) -> (sqlx::PgPool, Self) {
let scratch = Self::create(admin_url, prefix).await;
let pool = scratch.connect().await;
crate::migration::run_migrations_through(&pool, target)
.await
.expect("migrate scratch db through target");
(pool, scratch)
}
/// A default pool on this database.
pub(crate) async fn connect(&self) -> sqlx::PgPool {
sqlx::PgPool::connect(&self.url)
.await
.expect("connect scratch db")
}
pub(crate) fn name(&self) -> &str {
&self.name
}
pub(crate) fn url(&self) -> &str {
&self.url
}
}
impl Drop for ScratchDb {
fn drop(&mut self) {
drop_cluster_object(
&self.admin_url,
&format!("scratch database {}", self.name),
format!("DROP DATABASE IF EXISTS {} WITH (FORCE)", self.name),
);
}
}
/// A uniquely named role on the test server that is dropped when the guard
/// goes out of scope, including when the test panics. The role must not own
/// objects or hold privileges by then, or the drop fails with a warning.
pub(crate) struct ScratchRole {
name: String,
admin_url: String,
}
impl ScratchRole {
/// Create a role named `{prefix}_{uuid}` with `options` (for example
/// `LOGIN PASSWORD '...'`) on the server `admin_url` connects to.
pub(crate) async fn create(admin_url: &str, prefix: &str, options: &str) -> Self {
use sqlx::Connection as _;
let name = format!("{prefix}_{}", uuid::Uuid::new_v4().simple());
let mut admin = sqlx::PgConnection::connect(admin_url)
.await
.expect("connect admin database");
sqlx::query(sqlx::AssertSqlSafe(format!("CREATE ROLE {name} {options}")))
.execute(&mut admin)
.await
.expect("create scratch role");
let _ = admin.close().await;
Self {
name,
admin_url: admin_url.to_owned(),
}
}
pub(crate) fn name(&self) -> &str {
&self.name
}
}
impl Drop for ScratchRole {
fn drop(&mut self) {
drop_cluster_object(
&self.admin_url,
&format!("scratch role {}", self.name),
format!("DROP ROLE IF EXISTS {}", self.name),
);
}
}
/// Run one cleanup statement from `Drop` over a fresh admin connection.
///
/// Drop runs on the test's runtime thread, which cannot drive the test's
/// pools while it blocks here, so the statement runs on a thread with its
/// own runtime. Failures are printed rather than raised: a panic in Drop
/// while a failing test unwinds would abort the test binary.
fn drop_cluster_object(admin_url: &str, object: &str, statement: String) {
use sqlx::Connection as _;
let url = admin_url.to_owned();
let cleanup = std::thread::spawn(move || {
tokio::runtime::Builder::new_current_thread()
.enable_all()
.build()
.map_err(|error| error.to_string())?
.block_on(async {
let mut connection = sqlx::PgConnection::connect(&url)
.await
.map_err(|error| error.to_string())?;
let result = sqlx::query(sqlx::AssertSqlSafe(statement))
.execute(&mut connection)
.await;
let _ = connection.close().await;
result.map(drop).map_err(|error| error.to_string())
})
})
.join();
let error = match cleanup {
Ok(Ok(())) => return,
Ok(Err(error)) => error,
Err(_) => "cleanup thread panicked".to_owned(),
};
eprintln!("warning: failed to drop {object}: {error}");
}
/// One-session fixture that bypasses ordinary write-fence triggers while
/// retaining application admission. Communities are active, quiescing, fenced.
pub(crate) async fn admission_fixture() -> (crate::Db, [buzz_core::CommunityId; 3]) {
use buzz_core::CommunityId;
use uuid::Uuid;
let db = crate::Db::new(&crate::DbConfig {
database_url: database_url(),
max_connections: 1,
min_connections: 1,
..crate::DbConfig::default()
})
.await
.expect("connect test database");
let pool = &db.pool;
let mut communities = [CommunityId::from_uuid(Uuid::nil()); 3];
for community in &mut communities {
let id = Uuid::new_v4();
sqlx::query("INSERT INTO communities (id, host) VALUES ($1, $2)")
.bind(id)
.bind(format!("admission-{id}.example"))
.execute(pool)
.await
.expect("create community");
*community = CommunityId::from_uuid(id);
}
set_deletion_state(pool, *communities[1].as_uuid(), "quiescing").await;
set_deletion_state(pool, *communities[2].as_uuid(), "fenced").await;
// The one physical session serves every API call in this fixture.
sqlx::query("SET session_replication_role = replica")
.execute(pool)
.await
.expect("disable ordinary triggers");
let role: String = sqlx::query_scalar("SHOW session_replication_role")
.fetch_one(pool)
.await
.expect("read replication role");
assert_eq!(role, "replica");
// A raw write to the fenced tenant proves the DB backstop is bypassed.
sqlx::query("INSERT INTO users (community_id, pubkey) VALUES ($1, $2)")
.bind(communities[2].as_uuid())
.bind(vec![0xfe_u8; 32])
.execute(pool)
.await
.expect("raw fenced insert succeeds with triggers disabled");
(db, communities)
}
/// In one per-test database, make any return to the retired SQL predicate
/// observable without changing the production function in the public schema.
pub(crate) async fn reject_legacy_sql_fence_predicate(pool: &sqlx::PgPool) -> String {
let schema = format!("admission_test_{}", uuid::Uuid::new_v4().simple());
sqlx::query(sqlx::AssertSqlSafe(format!("CREATE SCHEMA {schema}")))
.execute(pool)
.await
.expect("create test-only function schema");
sqlx::query(sqlx::AssertSqlSafe(format!(
"CREATE FUNCTION {schema}.community_write_allowed(uuid) RETURNS boolean \
LANGUAGE plpgsql VOLATILE AS $$ BEGIN \
RAISE EXCEPTION 'legacy SQL fence predicate called'; END $$",
)))
.execute(pool)
.await
.expect("shadow legacy SQL predicate");
sqlx::query(sqlx::AssertSqlSafe(format!(
"SET search_path = {schema}, public"
)))
.execute(pool)
.await
.expect("resolve test-local predicate first");
let shadowed: bool = sqlx::query_scalar(
"SELECT to_regprocedure('community_write_allowed(uuid)') = to_regprocedure($1)",
)
.bind(format!("{schema}.community_write_allowed(uuid)"))
.fetch_one(pool)
.await
.expect("resolve shadowed predicate");
assert!(shadowed);
schema
}
/// Hold the executor's exclusive lock until a sweep reaches shared admission,
/// then quiesce in that same transaction before releasing it. The ordinary
/// second pool keeps lifecycle triggers enabled; the sweep fixture bypasses them.
pub(crate) async fn fence_while_sweep_waits<F, T>(community: buzz_core::CommunityId, sweep: F) -> T
where
F: std::future::Future<Output = T> + Send + 'static,
T: Send + 'static,
{
let pool = sqlx::postgres::PgPoolOptions::new()
.max_connections(1)
.connect(&database_url())
.await
.expect("connect deletion executor pool");
let mut tx = pool.begin().await.expect("begin deletion executor");
sqlx::query("SELECT pg_advisory_xact_lock(community_deletion_lock_key($1))")
.bind(community.as_uuid())
.execute(&mut *tx)
.await
.expect("hold exclusive deletion lock");
let mut task = tokio::spawn(sweep);
let waiting = tokio::time::timeout(std::time::Duration::from_secs(5), async {
loop {
let blocked: bool = sqlx::query_scalar(
"SELECT EXISTS(SELECT 1 FROM pg_locks \
WHERE locktype = 'advisory' AND NOT granted \
AND database = (SELECT oid FROM pg_database WHERE datname = current_database()) \
AND pg_backend_pid() = ANY(pg_blocking_pids(pid)))",
)
.fetch_one(&mut *tx)
.await
.expect("inspect blocked advisory admission");
if blocked {
break;
}
tokio::task::yield_now().await;
}
})
.await;
if waiting.is_err() {
task.abort();
panic!("sweep did not block in admission before deadline");
}
set_deletion_state_in_transaction(&mut tx, *community.as_uuid(), "quiescing").await;
tx.commit().await.expect("commit deletion fence");
let result = tokio::time::timeout(std::time::Duration::from_secs(5), &mut task).await;
if result.is_err() {
task.abort();
panic!("sweep did not finish after deletion fence committed");
}
pool.close().await;
result.unwrap().expect("sweep task")
}
/// Move a community fixture into a deletion lifecycle state the way the
/// executor does: the tombstone trigger only accepts lifecycle changes from a
/// transaction carrying the executor GUCs for that community, including its
/// current fence generation.
pub(crate) async fn set_deletion_state(pool: &sqlx::PgPool, id: uuid::Uuid, state: &str) {
let mut tx = pool.begin().await.expect("begin lifecycle fixture");
set_deletion_state_in_transaction(&mut tx, id, state).await;
tx.commit().await.expect("commit lifecycle fixture");
}
async fn set_deletion_state_in_transaction(
tx: &mut sqlx::Transaction<'_, sqlx::Postgres>,
id: uuid::Uuid,
state: &str,
) {
sqlx::query(
"SELECT set_config('buzz.deletion_executor_community', $1, true), \
set_config('buzz.deletion_fence_generation', \
(SELECT deletion_fence_generation::text FROM communities WHERE id = $2), true)",
)
.bind(id.to_string())
.bind(id)
.execute(&mut **tx)
.await
.expect("authorize lifecycle fixture");
sqlx::query(
"UPDATE communities SET deletion_state = $2, \
deleted_at = CASE WHEN $2 = 'tombstone' THEN now() END \
WHERE id = $1",
)
.bind(id)
.bind(state)
.execute(&mut **tx)
.await
.expect("set lifecycle state");
}
/// Move a disposable test community into `quiescing`, the state in which
/// admission must reject new serving writes.
pub(crate) async fn quiesce_community_for_tests(
pool: &sqlx::PgPool,
community: buzz_core::CommunityId,
) {
set_deletion_state(pool, *community.as_uuid(), "quiescing").await;
}
/// Whether `error` is the community-admission rejection taken at transaction
/// entry, as opposed to a later commit-time trigger rejection.
pub(crate) fn is_admission_rejection(error: &crate::DbError) -> bool {
matches!(error, crate::DbError::AccessDenied(message) if message.contains("write-fenced (quiescing)"))
}
/// The desired-state schema as one SQL script.
///
/// `schema/schema.sql` is a manifest of `\i` includes resolved relative to the
/// including file (as `pgschema` does). This expands every include in place,
/// so text assertions and `sqlx::raw_sql` bootstraps see the same statements,
/// in the same order, that `pgschema apply` loads.
pub(crate) fn desired_state_schema_sql() -> String {
fn expand(path: &std::path::Path, out: &mut String) {
let text = std::fs::read_to_string(path)
.unwrap_or_else(|error| panic!("read {}: {error}", path.display()));
let dir = path.parent().expect("schema file has a parent directory");
for line in text.split_inclusive('\n') {
match line.trim_end().strip_prefix("\\i ") {
Some(include) => expand(&dir.join(include.trim()), out),
None => out.push_str(line),
}
}
if !out.ends_with('\n') {
out.push('\n');
}
}
let mut out = String::new();
expand(&desired_state_schema_dir().join("schema.sql"), &mut out);
out
}
/// `schema/` at the workspace root.
pub(crate) fn desired_state_schema_dir() -> std::path::PathBuf {
std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("../../schema")
}
/// Rewrite every `ON DELETE CASCADE`, `SET NULL` and `SET DEFAULT` foreign key
/// in this test's database as `NO ACTION`: the schema the cascade retirement
/// targets. Every trigger stays live, including the foreign-key checks and the
/// community fence, so a delete path exercised afterwards must remove its
/// children itself, in an order the remaining constraints accept.
///
/// Each PostgreSQL test process owns its database (`crates/buzz-db/TESTING.md`),
/// so the rewrite does not reach other tests. It refuses any database whose
/// name does not match the per-test wrapper's (`buzz_nt_` plus 24 hex digits),
/// which excludes the run's `_desired` template. The name is the only check:
/// a manually created database with that shape would pass.
pub(crate) async fn retire_foreign_key_delete_actions(pool: &sqlx::PgPool) {
let database: String = sqlx::query_scalar("SELECT current_database()")
.fetch_one(pool)
.await
.expect("read current database");
assert!(
database
.strip_prefix("buzz_nt_")
.is_some_and(|hash| hash.len() == 24
&& hash
.bytes()
.all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))),
"refusing to rewrite foreign keys in shared database {database}; \
run this test through scripts/postgres-test-run.sh"
);
sqlx::raw_sql(
r#"DO $$
DECLARE c record;
BEGIN
FOR c IN SELECT conrelid::regclass AS tbl, conname, pg_get_constraintdef(oid) AS def
FROM pg_constraint
WHERE contype = 'f' AND confdeltype IN ('c', 'n', 'd') AND conparentid = 0
LOOP
EXECUTE format('ALTER TABLE %s DROP CONSTRAINT %I, ADD CONSTRAINT %I %s',
c.tbl, c.conname, c.conname,
regexp_replace(c.def, ' ON DELETE (CASCADE|SET NULL|SET DEFAULT)( \([^)]*\))?', ''));
END LOOP;
END $$"#,
)
.execute(pool)
.await
.expect("retire foreign-key delete actions");
let remaining: i64 = sqlx::query_scalar(
"SELECT count(*) FROM pg_constraint WHERE contype = 'f' AND confdeltype IN ('c', 'n', 'd')",
)
.fetch_one(pool)
.await
.expect("count foreign-key delete actions");
assert_eq!(remaining, 0, "every foreign-key delete action is retired");
}
/// A single-connection pool whose session carries `application_name`, so
/// [`wait_for_lock_wait`] can tell when its statement is blocked.
pub(crate) async fn named_pool(application_name: &str) -> sqlx::PgPool {
use std::str::FromStr as _;
let options = sqlx::postgres::PgConnectOptions::from_str(&database_url())
.expect("parse test database URL")
.application_name(application_name);
sqlx::postgres::PgPoolOptions::new()
.max_connections(1)
.connect_with(options)
.await
.expect("connect named test pool")
}
/// Prebuild `idx_channels_listed_name` concurrently, as an operator of a
/// deployment that migrates out of band does before migration 0083
/// (docs/channel-name-uniqueness-deployment.md). Run it with no open
/// transaction in the database: the build waits for every one.
pub(crate) async fn prebuild_listed_channel_name_index(pool: &sqlx::PgPool) {
sqlx::query(
"CREATE UNIQUE INDEX CONCURRENTLY IF NOT EXISTS idx_channels_listed_name \
ON channels (community_id, lower(name)) \
WHERE visibility = 'open' AND NOT unlisted \
AND channel_type IN ('stream', 'forum') AND deleted_at IS NULL",
)
.execute(pool)
.await
.expect("prebuild idx_channels_listed_name");
}
/// Wait until the session named `application_name` in this test's database is
/// blocked on a lock.
pub(crate) async fn wait_for_lock_wait(pool: &sqlx::PgPool, application_name: &str) {
tokio::time::timeout(std::time::Duration::from_secs(5), async {
loop {
let waiting: bool = sqlx::query_scalar(
"SELECT EXISTS(SELECT 1 FROM pg_stat_activity \
WHERE datname = current_database() AND application_name = $1 \
AND wait_event_type = 'Lock')",
)
.bind(application_name)
.fetch_one(pool)
.await
.expect("inspect lock wait");
if waiting {
break;
}
tokio::task::yield_now().await;
}
})
.await
.expect("session reached a lock wait");
}
/// Loopback TCP proxy that can pause the upstream database, like
/// `docker pause` on a replica. While paused, every session (established or
/// new) holds the bytes it receives with both sockets open; on resume the
/// held bytes are delivered in order and the sessions carry on.
///
/// [`PausingProxy::stall_established`] instead holds only the sessions open
/// at that moment, for good, while later sessions relay normally: a failover
/// whose old sockets stop answering without closing.
/// [`PausingProxy::stall_from`] is the reverse: sessions from a given number
/// on hold for good, so dials past that point stall mid-handshake.
pub(crate) struct PausingProxy {
pub(crate) port: u16,
hold: tokio::sync::watch::Sender<ProxyHold>,
next_session: std::sync::Arc<std::sync::atomic::AtomicU64>,
}
/// Which proxy sessions hold their bytes.
#[derive(Clone, Copy, Default)]
struct ProxyHold {
all: bool,
/// Sessions numbered below this hold their bytes.
below: u64,
/// Sessions numbered at or above this hold their bytes.
from: Option<u64>,
}
impl ProxyHold {
fn holds(self, session: u64) -> bool {
self.all || session < self.below || self.from.is_some_and(|from| session >= from)
}
}
impl PausingProxy {
pub(crate) async fn spawn(upstream_host: String, upstream_port: u16) -> Self {
use std::sync::atomic::Ordering;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
async fn pump(
mut from: tokio::net::tcp::OwnedReadHalf,
mut to: tokio::net::tcp::OwnedWriteHalf,
mut hold: tokio::sync::watch::Receiver<ProxyHold>,
session: u64,
) {
let mut buf = [0u8; 8192];
loop {
let Ok(n) = from.read(&mut buf).await else {
return;
};
if n == 0 {
return;
}
if hold.wait_for(|hold| !hold.holds(session)).await.is_err() {
return;
}
if to.write_all(&buf[..n]).await.is_err() {
return;
}
}
}
let listener = tokio::net::TcpListener::bind("127.0.0.1:0")
.await
.expect("bind proxy");
let port = listener.local_addr().expect("proxy addr").port();
let (hold, _) = tokio::sync::watch::channel(ProxyHold::default());
let accept_hold = hold.subscribe();
let next_session = std::sync::Arc::new(std::sync::atomic::AtomicU64::new(0));
let accept_next = next_session.clone();
tokio::spawn(async move {
while let Ok((client, _)) = listener.accept().await {
let session = accept_next.fetch_add(1, Ordering::SeqCst);
let Ok(server) =
tokio::net::TcpStream::connect((upstream_host.as_str(), upstream_port)).await
else {
continue;
};
let (client_read, client_write) = client.into_split();
let (server_read, server_write) = server.into_split();
tokio::spawn(pump(
client_read,
server_write,
accept_hold.clone(),
session,
));
tokio::spawn(pump(
server_read,
client_write,
accept_hold.clone(),
session,
));
}
});
Self {
port,
hold,
next_session,
}
}
pub(crate) fn set_paused(&self, paused: bool) {
self.hold.send_modify(|hold| hold.all = paused);
}
/// Hold every session open now, for good; later sessions relay normally.
pub(crate) fn stall_established(&self) {
let below = self.next_session.load(std::sync::atomic::Ordering::SeqCst);
self.hold.send_modify(|hold| hold.below = below);
}
/// Relay the first `from` sessions normally and hold every later one,
/// for good.
pub(crate) fn stall_from(&self, from: u64) {
self.hold.send_modify(|hold| hold.from = Some(from));
}
/// Sessions accepted so far.
pub(crate) fn sessions(&self) -> u64 {
self.next_session.load(std::sync::atomic::Ordering::SeqCst)
}
}
mod recent_pass_tests;
pub(crate) use recent_pass_tests::replace_between_passes;