Skip to content

Commit 050e593

Browse files
author
Elrond
committed
feat(buzz-db): retire the channel-TTL trigger
The application refreshes channel TTL deadlines in the event transaction (event_follow_up::refresh_channel_ttls), so the 0022/0024 deferred trigger only repeats that work. #8352 already retired the push-match trigger in 0063; migration 0065 retires the TTL trigger and its function with the same bounded lock_timeout, completing the follow-up trigger retirement. Desired schema, reconcile script and the follow-up contract tests drop the TTL trigger. The contract keeps two arms: app-only (shipped) and the reconstructed historical overlap of both triggers. Signed-off-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
1 parent 93ffc37 commit 050e593

14 files changed

Lines changed: 149 additions & 139 deletions

crates/buzz-db/src/runtime/push_retirement_tests.rs renamed to crates/buzz-db/src/runtime/follow_up_retirement_tests.rs

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
//! Upgrade contract for push-only trigger retirement.
1+
//! Upgrade contracts for the event follow-up trigger retirements: push
2+
//! (0063), then channel TTL (0065).
23
use super::postgres_tests::{connect_test_pool, reset_public_schema};
34
use super::{run_migrations, run_migrations_through};
45
use sqlx::PgPool;
@@ -47,7 +48,9 @@ async fn migration_schema_push_retirement_preserves_ttl_and_published_checksums(
4748
.await
4849
.unwrap();
4950

50-
run_migrations(&pool).await.expect("retire push trigger");
51+
run_migrations_through(&pool, 63)
52+
.await
53+
.expect("retire push trigger");
5154
assert_eq!(
5255
push_trigger_count(&pool).await,
5356
0,
@@ -113,3 +116,49 @@ async fn migration_schema_push_retirement_preserves_ttl_and_published_checksums(
113116
);
114117
pool.close().await;
115118
}
119+
120+
async fn ttl_function_present(pool: &PgPool) -> bool {
121+
sqlx::query_scalar(
122+
"SELECT to_regprocedure('refresh_channel_ttl_after_event_insert()') IS NOT NULL",
123+
)
124+
.fetch_one(pool)
125+
.await
126+
.expect("look up TTL function")
127+
}
128+
129+
#[tokio::test]
130+
#[ignore = "requires Postgres"]
131+
async fn migration_schema_ttl_retirement_drops_every_partition_trigger_and_keeps_checksums() {
132+
let pool = connect_test_pool().await;
133+
reset_public_schema(&pool).await;
134+
run_migrations_through(&pool, 64)
135+
.await
136+
.expect("apply published migrations");
137+
assert!(
138+
ttl_definitions(&pool).await.len() > 1,
139+
"parent and partition TTL triggers exist before upgrade"
140+
);
141+
assert!(ttl_function_present(&pool).await);
142+
let checksums_before: Vec<(i64, Vec<u8>)> = sqlx::query_as(
143+
"SELECT version, checksum FROM _sqlx_migrations WHERE version <= 64 ORDER BY version",
144+
)
145+
.fetch_all(&pool)
146+
.await
147+
.unwrap();
148+
149+
run_migrations(&pool).await.expect("retire TTL trigger");
150+
assert!(
151+
ttl_definitions(&pool).await.is_empty(),
152+
"retired on parent and every partition"
153+
);
154+
assert!(!ttl_function_present(&pool).await);
155+
assert_eq!(push_trigger_count(&pool).await, 0);
156+
let checksums_after: Vec<(i64, Vec<u8>)> = sqlx::query_as(
157+
"SELECT version, checksum FROM _sqlx_migrations WHERE version <= 64 ORDER BY version",
158+
)
159+
.fetch_all(&pool)
160+
.await
161+
.unwrap();
162+
assert_eq!(checksums_after, checksums_before);
163+
pool.close().await;
164+
}

‎crates/buzz-db/src/runtime/migration.rs‎

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ async fn reject_legacy_nip_rs_cardinality_ambiguity(conn: &mut PgConnection) ->
186186
}
187187

188188
#[cfg(test)]
189-
#[path = "push_retirement_tests.rs"]
189+
#[path = "follow_up_retirement_tests.rs"]
190190
mod retirement_postgres_tests;
191191

192192
#[cfg(test)]
@@ -709,7 +709,19 @@ mod postgres_tests {
709709
let mut migrations: Vec<_> = MIGRATOR.iter().collect();
710710
migrations.sort_by_key(|migration| migration.version);
711711

712-
assert_eq!(migrations.len(), 64);
712+
assert_eq!(migrations.len(), 65);
713+
assert_eq!(migrations[64].version, 65);
714+
let retire_ttl = strip_sql_comments(migrations[64].sql.as_str());
715+
for statement in [
716+
"SET LOCAL lock_timeout = '5s';",
717+
"DROP TRIGGER events_refresh_channel_ttl ON events;",
718+
"DROP FUNCTION refresh_channel_ttl_after_event_insert();",
719+
] {
720+
assert!(retire_ttl.contains(statement), "0065 must run {statement}");
721+
}
722+
let desired_schema = crate::test_support::desired_state_schema_sql();
723+
assert!(!desired_schema.contains("events_refresh_channel_ttl"));
724+
assert!(!desired_schema.contains("refresh_channel_ttl_after_event_insert"));
713725
assert_eq!(migrations[58].version, 59);
714726
assert!(migrations[58].sql.as_str().contains("buzz.push_enabled"));
715727
assert_eq!(migrations[59].version, 60);

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -601,12 +601,13 @@ pub async fn update_channel(
601601
q = q.bind(community_id.as_uuid());
602602
q = q.bind(channel_id);
603603

604-
// T1a repair: a TTL change can flip this channel's event-trigger fast
605-
// path (migration 0024 reads ttl_seconds under a SHARED per-channel
606-
// advisory lock). Take the same key EXCLUSIVE before the UPDATE so a
607-
// concurrent event either sees the committed TTL or strictly precedes
608-
// this transition — whose own deadline reset is then the latest word.
609-
// Non-TTL updates don't touch the fast path and skip the lock.
604+
// T1a repair: a TTL change can flip this channel's event-commit fast
605+
// path (`event_follow_up::refresh_channel_ttls` reads ttl_seconds under
606+
// a SHARED per-channel advisory lock). Take the same key EXCLUSIVE
607+
// before the UPDATE so a concurrent event either sees the committed TTL
608+
// or strictly precedes this transition — whose own deadline reset is
609+
// then the latest word. Non-TTL updates don't touch the fast path and
610+
// skip the lock.
610611
if updates.ttl_seconds.is_some() {
611612
let mut tx = crate::begin_community_write_transaction(
612613
pool,
@@ -1138,9 +1139,9 @@ impl Db {
11381139
}
11391140

11401141
/// The per-channel TTL advisory lock key. Event commits take it SHARED
1141-
/// (`event_follow_up::refresh_channel_ttls`, and the 0024 trigger until it is
1142-
/// retired); TTL transitions in [`update_channel`] take it EXCLUSIVE. Both
1143-
/// sides build it here, so the keys cannot drift.
1142+
/// (`event_follow_up::refresh_channel_ttls`); TTL transitions in
1143+
/// [`update_channel`] take it EXCLUSIVE. Both sides build it here, so the keys
1144+
/// cannot drift.
11441145
pub(crate) fn channel_ttl_lock_key(community: CommunityId, channel: Uuid) -> String {
11451146
format!("buzz_channel_ttl:{}:{channel}", community.as_uuid())
11461147
}

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3259,7 +3259,7 @@ mod postgres_tests {
32593259

32603260
#[tokio::test]
32613261
#[ignore = "requires Postgres"]
3262-
async fn event_insert_ttl_trigger_handles_permanent_ephemeral_duplicate_and_activation_race() {
3262+
async fn event_insert_ttl_refresh_handles_permanent_ephemeral_duplicate_and_activation_race() {
32633263
let pool = setup_pool().await;
32643264
let community_uuid = make_test_community(&pool).await;
32653265
let community = CommunityId::from_uuid(community_uuid);
@@ -3326,8 +3326,8 @@ mod postgres_tests {
33263326

33273327
// Reproduce the blocked stale-prefetch ordering: ingest has already
33283328
// observed a permanent channel, then TTL activation locks/updates the
3329-
// row before the event INSERT reaches its trigger. The trigger must
3330-
// wait and refresh from the later event after activation commits.
3329+
// row before the event INSERT reaches its pre-commit TTL refresh. The
3330+
// refresh must wait, then use the later event once activation commits.
33313331
let racing = make_test_channel(&pool, community_uuid, None).await;
33323332
let stale_ttl: Option<i32> = sqlx::query_scalar(
33333333
"SELECT ttl_seconds FROM channels WHERE community_id = $1 AND id = $2",
@@ -3342,7 +3342,7 @@ mod postgres_tests {
33423342
let mut activation = pool.begin().await.expect("begin TTL activation");
33433343
// Model the repaired update_channel protocol (migration 0024): the
33443344
// TTL transition holds the per-channel advisory key EXCLUSIVE, which
3345-
// is what the event trigger's shared acquisition now waits on.
3345+
// is what the event TTL refresh's shared acquisition waits on.
33463346
sqlx::query("SELECT pg_advisory_xact_lock(hashtextextended($1, 0))")
33473347
.bind(format!("buzz_channel_ttl:{community_uuid}:{racing}"))
33483348
.execute(&mut *activation)
@@ -3367,7 +3367,7 @@ mod postgres_tests {
33673367
tokio::time::sleep(std::time::Duration::from_millis(100)).await;
33683368
assert!(
33693369
!insert.is_finished(),
3370-
"event trigger must wait on TTL activation"
3370+
"event TTL refresh must wait on TTL activation"
33713371
);
33723372
activation.commit().await.expect("commit TTL activation");
33733373
assert!(

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,9 @@
1111
//! [`refresh_channel_ttls`]). A refresh failure is logged and counted, and
1212
//! the event still commits.
1313
//!
14-
//! This is the application-side replacement for the `events_enqueue_push_match`
15-
//! and `events_refresh_channel_ttl` triggers (migrations 0023, 0024, 0040).
16-
//! Migration 0063 retires the push trigger; the TTL trigger remains installed.
17-
//! Push requires application enqueueing on every supported writer. Every production
14+
//! This replaced the `events_enqueue_push_match` and `events_refresh_channel_ttl`
15+
//! triggers (migrations 0023, 0024, 0040; retired by 0063 and 0065), so nothing
16+
//! in the database does this work any more. Every production
1817
//! `INSERT INTO events` must call this module, enforced by source-policy tests.
1918
2019
use std::collections::BTreeSet;

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

Lines changed: 43 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! PostgreSQL contract for the event follow-up producers (push match
22
//! enqueue and channel TTL refresh), on the desired and migration schemas,
3-
//! with historical overlap, push retired, and both follow-ups app-only.
3+
//! app-only as shipped and with the historical trigger overlap reconstructed.
44
use crate::replaceable::{ParameterizedReplacePrecondition, ParameterizedReplaceStatus};
55
use crate::{event, migration, push, AdmittedTx, Db, DbConfig, DbError};
66
use buzz_core::CommunityId;
@@ -214,67 +214,66 @@ async fn activate_lease(
214214
);
215215
}
216216

217-
/// Preserve the overlap and fully app-only compatibility contracts alongside
218-
/// the shipped push-only retirement (the TTL trigger remains installed).
217+
/// Which producers are live. `AppOnly` is the shipped schema: 0063 and 0065
218+
/// retired both follow-up triggers. `Dual` reconstructs the historical overlap
219+
/// (app follow-ups plus both triggers) in this disposable database only.
219220
#[derive(Clone, Copy, Debug)]
220221
enum Arm {
221222
Dual,
222223
AppOnly,
223-
PushRetired,
224224
}
225225

226226
async fn apply_arm(pool: &PgPool, arm: Arm) {
227227
let triggers: i64 = sqlx::query_scalar(
228228
"SELECT count(*) FROM pg_trigger \
229-
WHERE tgrelid = 'events'::regclass \
230-
AND tgname IN ('events_enqueue_push_match', 'events_refresh_channel_ttl')",
229+
WHERE tgname IN ('events_enqueue_push_match', 'events_refresh_channel_ttl')",
231230
)
232231
.fetch_one(pool)
233232
.await
234-
.expect("count follow-up triggers");
233+
.expect("count follow-up triggers, including partition clones");
235234
assert_eq!(
236-
triggers, 1,
237-
"only the TTL trigger remains in the shipped schema"
235+
triggers, 0,
236+
"no follow-up trigger remains on any partition of the shipped schema"
238237
);
239-
let absent: bool =
240-
sqlx::query_scalar("SELECT to_regprocedure('enqueue_push_match_job()') IS NULL")
241-
.fetch_one(pool)
242-
.await
243-
.unwrap();
244-
assert!(absent);
245-
match arm {
246-
Arm::Dual => {
247-
// Reconstruct the historical overlap explicitly in this test only.
248-
sqlx::raw_sql(include_str!(
249-
"../../../../migrations/0040_push_message_kinds.sql"
250-
))
251-
.execute(pool)
252-
.await
253-
.unwrap();
254-
sqlx::raw_sql(include_str!(
255-
"../../../../migrations/0059_push_rollback_gate.sql"
256-
))
257-
.execute(pool)
258-
.await
259-
.unwrap();
260-
sqlx::query("CREATE TRIGGER events_enqueue_push_match AFTER INSERT ON events FOR EACH ROW EXECUTE FUNCTION enqueue_push_match_job()")
261-
.execute(pool).await.unwrap();
262-
}
263-
Arm::AppOnly => {
264-
sqlx::query("DROP TRIGGER events_refresh_channel_ttl ON events")
265-
.execute(pool)
266-
.await
267-
.unwrap();
268-
}
269-
Arm::PushRetired => {}
270-
}
271-
if !matches!(arm, Arm::Dual) {
238+
let absent: bool = sqlx::query_scalar(
239+
"SELECT to_regprocedure('enqueue_push_match_job()') IS NULL \
240+
AND to_regprocedure('refresh_channel_ttl_after_event_insert()') IS NULL",
241+
)
242+
.fetch_one(pool)
243+
.await
244+
.unwrap();
245+
assert!(absent, "no follow-up trigger function remains");
246+
if let Arm::Dual = arm {
272247
sqlx::raw_sql(include_str!(
273-
"../../../../migrations/0059_push_rollback_gate.sql"
248+
"../../../../migrations/0040_push_message_kinds.sql"
274249
))
275250
.execute(pool)
276251
.await
277252
.unwrap();
253+
sqlx::raw_sql(include_str!(
254+
"../../../../migrations/0024_event_ttl_refresh_shared_lock.sql"
255+
))
256+
.execute(pool)
257+
.await
258+
.unwrap();
259+
}
260+
sqlx::raw_sql(include_str!(
261+
"../../../../migrations/0059_push_rollback_gate.sql"
262+
))
263+
.execute(pool)
264+
.await
265+
.unwrap();
266+
if let Arm::Dual = arm {
267+
for statement in [
268+
"CREATE TRIGGER events_enqueue_push_match AFTER INSERT ON events \
269+
FOR EACH ROW EXECUTE FUNCTION enqueue_push_match_job()",
270+
"CREATE CONSTRAINT TRIGGER events_refresh_channel_ttl AFTER INSERT ON events \
271+
DEFERRABLE INITIALLY DEFERRED \
272+
FOR EACH ROW EXECUTE FUNCTION refresh_channel_ttl_after_event_insert()",
273+
] {
274+
sqlx::query(statement).execute(pool).await.unwrap();
275+
}
276+
} else {
278277
let absent: bool =
279278
sqlx::query_scalar("SELECT to_regprocedure('enqueue_push_match_job()') IS NULL")
280279
.fetch_one(pool)
@@ -512,7 +511,7 @@ async fn assert_contract(migration_schema: bool, arm: Arm) {
512511
assert!(
513512
deadline(pool, community, channel).await
514513
>= database_time_before_commit + chrono::Duration::seconds(60),
515-
"the deferred trigger must refresh TTL at commit"
514+
"the TTL refresh must set the deadline at commit time"
516515
);
517516
assert_eq!(match_count(&db, community, &committed).await, 1);
518517

@@ -824,18 +823,6 @@ async fn migration_schema_event_follow_up_contract_app_only() {
824823
assert_contract(true, Arm::AppOnly).await;
825824
}
826825

827-
#[tokio::test]
828-
#[ignore = "requires Postgres"]
829-
async fn desired_event_follow_up_contract_push_retired() {
830-
assert_contract(false, Arm::PushRetired).await;
831-
}
832-
833-
#[tokio::test]
834-
#[ignore = "requires Postgres"]
835-
async fn migration_schema_event_follow_up_contract_push_retired() {
836-
assert_contract(true, Arm::PushRetired).await;
837-
}
838-
839826
// Runs with both schema sources and with/without the overlap trigger.
840827
async fn assert_rollback(db: &Db, community: CommunityId, channel: Uuid, keys: &Keys) {
841828
let pool = db.pool();

‎crates/buzz-db/src/store/personal_read/postgres_tests.rs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,7 @@ async fn personal_read_intent_does_not_lock_shared_conversation_rows() {
283283
.await
284284
.unwrap();
285285
assert!(matches!(result, IntentOutcome::Applied));
286-
// Exercise the actual event-insert TTL trigger while private progress is
286+
// Exercise the actual event-insert TTL refresh while private progress is
287287
// uncommitted, then verify event deletion can update the observed row.
288288
let incoming = EventBuilder::new(Kind::Custom(9), "concurrent ephemeral ingest")
289289
.sign_with_keys(&Keys::generate())

‎crates/buzz-db/tests/observability_source.rs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1925,8 +1925,8 @@ fn every_production_event_insert_runs_the_follow_up_hook() {
19251925

19261926
assert!(
19271927
violations.is_empty(),
1928-
"production `INSERT INTO events` writers must call `event_follow_up` so the push \
1929-
enqueue and channel TTL refresh survive the trigger retirement: {violations:?}"
1928+
"production `INSERT INTO events` writers must call `event_follow_up` because no \
1929+
trigger does the push enqueue or channel TTL refresh: {violations:?}"
19301930
);
19311931
// The seven writers at the time of BUZZ-176. A lower count means the scan
19321932
// stopped seeing a writer, not that one was removed safely.

‎docs/push-gateway-deployment.md‎

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -236,10 +236,12 @@ migration disabled during the transition. Resolve overlapping retirement
236236
migrations and verify the published migration ledger before execution. Migration
237237
0063's lock timeout bounds acquisition waits, not lock-hold or statement duration.
238238

239-
After migration, verify that `events_enqueue_push_match` is absent from the
240-
parent and every partition, `enqueue_push_match_job()` is absent, and the
241-
channel-TTL trigger/function remain intact. Verify application enqueueing and
242-
flag-only rollback on the exact deployed candidate. Legacy images and raw SQL
239+
After migration 0063, verify that `events_enqueue_push_match` is absent from
240+
the parent and every partition and `enqueue_push_match_job()` is absent.
241+
Migration 0065 then retires `events_refresh_channel_ttl` and
242+
`refresh_channel_ttl_after_event_insert()` the same way; after it, raw SQL
243+
writers no longer refresh ephemeral-channel deadlines. Verify application
244+
enqueueing and flag-only rollback on the exact deployed candidate. Legacy images and raw SQL
243245
writers without the application producer are unsupported after this point:
244246
their messages may commit without producing notifications. Do not restore the
245247
trigger or downgrade an image as an urgent rollback step. Reintroducing legacy
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- Channel-TTL refresh is done by the application in the event transaction
2+
-- (crates/buzz-db/src/store/event_follow_up.rs). Retire the trigger that
3+
-- repeats that work, completing the follow-up trigger retirement begun by
4+
-- 0063. All event writers and rollback targets must support application TTL
5+
-- refresh before this migration runs. Published migrations remain
6+
-- checksum-frozen.
7+
--
8+
-- DROP TRIGGER locks the partitioned parent and its partitions. Bound the
9+
-- lock acquisition wait. This does not bound statement duration or the time
10+
-- ingestion can be blocked while these locks are held.
11+
SET LOCAL lock_timeout = '5s';
12+
13+
DROP TRIGGER events_refresh_channel_ttl ON events;
14+
DROP FUNCTION refresh_channel_ttl_after_event_insert();

0 commit comments

Comments
 (0)