Skip to content

Commit 7f6ffd5

Browse files
TheSentinel454Elrondolympusbuildz
authored
fix(db): enumerate only active communities for maintenance (#8061)
Fixes #7558. Supersedes #7630. The SQL predicate here comes from #7630 by @olympusbuildz, who is credited as co-author on the commit. This PR keeps that fix unchanged and adds the lifecycle fixtures and worker-path coverage the fix needed. ## Problem `community_hosts_with_operation` ran `SELECT id, host FROM communities` with no filter. After a whole-community deletion, the `communities` row stays behind as a `tombstone` until retention expires, which is by design. But the NIP-43 maintenance sweep kept handing that community to `publish_nip43_membership_list`. The community write fence rejected the write, so the relay logged `NIP-43 membership reconciliation failed` about every 60s, indefinitely, for every deleted community. ## Change The helper is split by caller purpose: - `active_community_hosts` (maintenance) and `bootstrap_community_hosts` filter to `archived_at IS NULL AND deleted_at IS NULL AND deletion_state = 'active'`. That's the same active-community predicate already used in `community.rs` and `deletion.rs`. - `usage_community_hosts` still enumerates every row, exactly as on main. The startup large-roster reconciliation query (`list_large_channel_rosters_needing_reconciliation`) joined `communities` without any lifecycle filter, so it gets the same active predicate on its `JOIN communities`. Tombstone rows aren't modified. ### Caller impact | Caller | Enumeration | Effect | |---|---|---| | NIP-43 maintenance sweep (`side_effects.rs`) | active | Stops trying fenced writes and logging warnings forever. This is the bug fix. | | NIP-43 bootstrap reconciliation | active | Same as maintenance, at startup. | | Push-wake worker (`push_runtime.rs`) | active | Stops claiming wakes for these communities. Archived communities are unreachable through host resolution; quiescing, fenced and tombstone writes are DB-fenced. Wakes already queued for an archived community aren't delivered late after unarchive, because claim and send both re-check `expires_at`. | | Startup large-roster reconciliation (`side_effects.rs`, kind 39002) | active | Stops rewriting roster snapshots for archived communities and attempting fenced writes for deleting or tombstoned ones. | | Usage-metrics host map (`main.rs`) | all rows | Unchanged. Per-community labels, fleet totals and storage attribution keep covering the same rows as the unfiltered usage count queries. | ## Tests #7630's test tombstoned a row with a plain `UPDATE`. On current main, the `enforce_community_tombstone` trigger from migration 0029 rejects that unless the transaction carries the deletion-executor GUCs. The fixtures here set those GUCs the way the executor does. - `buzz-db` `store::usage::postgres_tests::test_community_hosts_excludes_archived_and_deleted_communities` sets up active, archived, quiescing, fenced and tombstoned fixtures. Across two sweeps it checks that `active_community_hosts` (maintenance) and `bootstrap_community_hosts` both return only the active community, and that `usage_community_hosts` still returns all five. It also checks that the tombstone row still exists with `deleted_at` set. - `buzz-relay` `handlers::side_effects::tests::postgres_tests::maintenance_sweep_skips_archived_and_deleted_communities` is the worker-path regression. It runs `reconcile_nip43_membership_snapshots_with_purpose(Maintenance)` three times against the same five fixtures. It checks that `buzz_nip43_membership_reconciliation_failures_total` stays at 0, the active community gets exactly one membership snapshot, the four non-active communities get no maintenance write, and the tombstone row is still there. - `buzz-db` `store::channel_members::postgres_tests::large_roster_reconciliation_skips_archived_and_deleted_communities` seeds a channel whose roster has diverged from its live relay-authored kind-39002 snapshot in each of the five fixture communities. Only the active community's channel may be returned. All three tests are `#[ignore = "requires Postgres"]` inside `postgres_tests::`, so the PostgreSQL CI lane picks them up through its existing structural filter. ## What got simpler The background writers share one definition of "community eligible for maintenance": the same active predicate the rest of the store uses. Usage metrics keep their existing semantics, so this PR changes no metric. One enumeration helper is added (`active_community_hosts`), and the old `community_hosts_with_operation` indirection is gone. The executor-GUC lifecycle fixture lives once in `test_support` and is shared by both DB tests instead of being duplicated. --------- Signed-off-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz> Co-authored-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz> Co-authored-by: Olympusbuildz <[email protected]>
1 parent 8746bfe commit 7f6ffd5

5 files changed

Lines changed: 389 additions & 15 deletions

File tree

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

Lines changed: 102 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1098,6 +1098,10 @@ pub struct LargeChannelRoster {
10981098

10991099
/// Returns active channels whose canonical roster exceeds `minimum_members`.
11001100
///
1101+
/// Only active communities are scanned: archived communities must not have
1102+
/// their discovery state rewritten, and deleting or tombstoned communities
1103+
/// would only produce fenced-write failures.
1104+
///
11011105
/// This is an internal cross-community maintenance read. Callers must preserve
11021106
/// the returned community id when reading or rewriting discovery state.
11031107
pub async fn list_large_channel_rosters_needing_reconciliation(
@@ -1125,7 +1129,11 @@ pub async fn list_large_channel_rosters_needing_reconciliation(
11251129
)
11261130
SELECT lr.community_id, community.host, lr.channel_id, lr.member_count
11271131
FROM large_rosters lr
1128-
JOIN communities community ON community.id = lr.community_id
1132+
JOIN communities community
1133+
ON community.id = lr.community_id
1134+
AND community.archived_at IS NULL
1135+
AND community.deleted_at IS NULL
1136+
AND community.deletion_state = 'active'
11291137
JOIN LATERAL (
11301138
SELECT roster.tags
11311139
FROM events roster
@@ -2334,6 +2342,99 @@ mod postgres_tests {
23342342
assert!(converged.is_empty());
23352343
}
23362344

2345+
/// Seed a channel whose roster (owner + one member) has diverged from its
2346+
/// live relay-authored kind-39002 snapshot (owner only).
2347+
async fn seed_divergent_roster(pool: &PgPool, community_id: Uuid, relay_pubkey: &[u8]) -> Uuid {
2348+
let creator = random_pubkey();
2349+
let channel = create_test_channel(
2350+
pool,
2351+
community_id,
2352+
"divergent-roster",
2353+
ChannelType::Stream,
2354+
ChannelVisibility::Open,
2355+
None,
2356+
&creator,
2357+
None,
2358+
)
2359+
.await
2360+
.expect("create test channel");
2361+
let tags = serde_json::json!([
2362+
["d", channel.id.to_string()],
2363+
["p", hex::encode(&creator), "", "owner"]
2364+
]);
2365+
sqlx::query(
2366+
r#"
2367+
INSERT INTO events
2368+
(community_id, id, pubkey, created_at, kind, tags, content, sig, channel_id, d_tag)
2369+
VALUES ($1, $2, $3, NOW(), 39002, $4, '', $5, $6, $7)
2370+
"#,
2371+
)
2372+
.bind(community_id)
2373+
.bind(random_pubkey())
2374+
.bind(relay_pubkey)
2375+
.bind(tags)
2376+
.bind(vec![0u8; 64])
2377+
.bind(channel.id)
2378+
.bind(channel.id.to_string())
2379+
.execute(pool)
2380+
.await
2381+
.expect("insert roster snapshot");
2382+
sqlx::query(
2383+
"INSERT INTO channel_members (community_id, channel_id, pubkey, role, joined_at) \
2384+
VALUES ($1, $2, $3, 'member', NOW())",
2385+
)
2386+
.bind(community_id)
2387+
.bind(channel.id)
2388+
.bind(random_pubkey())
2389+
.execute(pool)
2390+
.await
2391+
.expect("diverge roster");
2392+
channel.id
2393+
}
2394+
2395+
/// Startup roster reconciliation must not rewrite discovery state for
2396+
/// archived communities, nor attempt fenced writes for deleting or
2397+
/// tombstoned ones. Same active-community predicate as #7558.
2398+
#[tokio::test]
2399+
#[ignore = "requires Postgres"]
2400+
async fn large_roster_reconciliation_skips_archived_and_deleted_communities() {
2401+
let pool = setup_pool().await;
2402+
let relay_pubkey = random_pubkey();
2403+
let mut fixtures = Vec::new();
2404+
for label in ["active", "archived", "quiescing", "fenced", "tombstone"] {
2405+
let community_id = make_test_community(&pool).await;
2406+
let channel_id = seed_divergent_roster(&pool, community_id, &relay_pubkey).await;
2407+
fixtures.push((label, community_id, channel_id));
2408+
}
2409+
for (label, community_id, _) in &fixtures {
2410+
match *label {
2411+
"active" => {}
2412+
"archived" => {
2413+
sqlx::query("UPDATE communities SET archived_at = now() WHERE id = $1")
2414+
.bind(community_id)
2415+
.execute(&pool)
2416+
.await
2417+
.expect("archive fixture");
2418+
}
2419+
state => crate::test_support::set_deletion_state(&pool, *community_id, state).await,
2420+
}
2421+
}
2422+
2423+
let candidates = list_large_channel_rosters_needing_reconciliation(&pool, 1, &relay_pubkey)
2424+
.await
2425+
.expect("list candidates");
2426+
let found: Vec<(CommunityId, Uuid)> = candidates
2427+
.iter()
2428+
.map(|c| (c.community_id, c.channel_id))
2429+
.collect();
2430+
let (_, active_community, active_channel) = fixtures[0];
2431+
assert_eq!(
2432+
found,
2433+
vec![(CommunityId::from_uuid(active_community), active_channel)],
2434+
"only the active community's divergent roster may be reconciled"
2435+
);
2436+
}
2437+
23372438
/// A random non-admin, non-owner user cannot remove someone else's bot.
23382439
#[tokio::test]
23392440
#[ignore = "requires Postgres"]

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

Lines changed: 127 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -386,23 +386,46 @@ pub struct CommunityHost {
386386
pub host: String,
387387
}
388388

389-
/// Fetch all community id → host mappings in one query.
389+
/// Fetch every community id → host mapping in one query, including archived,
390+
/// deleting, and tombstoned communities.
391+
///
392+
/// Usage metrics label per-community series and attribute storage with this
393+
/// map, so it must cover the same rows as the unfiltered usage count queries.
390394
pub async fn community_hosts(pool: &PgPool) -> Result<Vec<CommunityHost>> {
391-
community_hosts_with_operation(pool, observability::WriterOperation::Maintenance).await
395+
let mut connection =
396+
observability::acquire_writer(pool, observability::WriterOperation::Maintenance).await?;
397+
let rows = sqlx::query_as::<_, (Uuid, String)>("SELECT id, host FROM communities")
398+
.fetch_all(&mut *connection)
399+
.await?;
400+
Ok(into_community_hosts(rows))
392401
}
393402

394-
async fn community_hosts_with_operation(
403+
/// Fetch the id → host mapping for every active community in one query.
404+
///
405+
/// Archived communities are unreachable through host resolution, and
406+
/// communities anywhere in the deletion lifecycle (`quiescing`, `fenced`,
407+
/// `tombstone`) have their writes DB-fenced, so background workers that write
408+
/// per community must skip them. Tombstone rows stay in place for retention.
409+
async fn active_community_hosts(
395410
pool: &PgPool,
396411
operation: observability::WriterOperation,
397412
) -> Result<Vec<CommunityHost>> {
398413
let mut connection = observability::acquire_writer(pool, operation).await?;
399-
let rows = sqlx::query_as::<_, (Uuid, String)>("SELECT id, host FROM communities")
400-
.fetch_all(&mut *connection)
401-
.await?;
402-
Ok(rows
403-
.into_iter()
414+
let rows = sqlx::query_as::<_, (Uuid, String)>(
415+
"SELECT id, host FROM communities \
416+
WHERE archived_at IS NULL \
417+
AND deleted_at IS NULL \
418+
AND deletion_state = 'active'",
419+
)
420+
.fetch_all(&mut *connection)
421+
.await?;
422+
Ok(into_community_hosts(rows))
423+
}
424+
425+
fn into_community_hosts(rows: Vec<(Uuid, String)>) -> Vec<CommunityHost> {
426+
rows.into_iter()
404427
.map(|(id, host)| CommunityHost { id, host })
405-
.collect())
428+
.collect()
406429
}
407430

408431
impl Db {
@@ -497,16 +520,22 @@ impl Db {
497520
active_channel_counts(&self.pool, interval_sql).await
498521
}
499522

500-
/// Return all community id → host mappings.
523+
/// Return all community id → host mappings, for usage-metrics labels.
501524
#[datastore_span(name = "usage_community_hosts", system = "postgresql")]
502525
pub async fn usage_community_hosts(&self) -> Result<Vec<CommunityHost>> {
503526
community_hosts(&self.pool).await
504527
}
505528

506-
/// Return community host mappings during startup bootstrap work.
529+
/// Return active community host mappings for background maintenance writers.
530+
#[datastore_span(name = "active_community_hosts", system = "postgresql")]
531+
pub async fn active_community_hosts(&self) -> Result<Vec<CommunityHost>> {
532+
active_community_hosts(&self.pool, observability::WriterOperation::Maintenance).await
533+
}
534+
535+
/// Return active community host mappings during startup bootstrap work.
507536
#[datastore_span(name = "bootstrap_community_hosts", system = "postgresql")]
508537
pub async fn bootstrap_community_hosts(&self) -> Result<Vec<CommunityHost>> {
509-
community_hosts_with_operation(&self.pool, observability::WriterOperation::Bootstrap).await
538+
active_community_hosts(&self.pool, observability::WriterOperation::Bootstrap).await
510539
}
511540
}
512541

@@ -805,6 +834,92 @@ mod postgres_tests {
805834
assert_eq!(found.unwrap().host, host);
806835
}
807836

837+
/// Regression for #7558: maintenance and bootstrap enumeration return only
838+
/// active communities. Archived, logically deleted (quiescing/fenced), and
839+
/// tombstoned rows are skipped on every sweep, and the tombstone row itself
840+
/// is left intact for retention. The usage-metrics map stays unfiltered so
841+
/// its per-community labels and storage attribution keep covering every
842+
/// row the unfiltered usage count queries return.
843+
#[tokio::test]
844+
#[ignore = "requires Postgres"]
845+
async fn test_community_hosts_excludes_archived_and_deleted_communities() {
846+
let pool = get_pool().await;
847+
let (active, _, active_host) = make_community(&pool).await;
848+
let (archived, _, _) = make_community(&pool).await;
849+
let (quiescing, _, _) = make_community(&pool).await;
850+
let (fenced, _, _) = make_community(&pool).await;
851+
let (tombstone, _, _) = make_community(&pool).await;
852+
853+
sqlx::query("UPDATE communities SET archived_at = now() WHERE id = $1")
854+
.bind(archived)
855+
.execute(&pool)
856+
.await
857+
.expect("archive fixture");
858+
crate::test_support::set_deletion_state(&pool, quiescing, "quiescing").await;
859+
crate::test_support::set_deletion_state(&pool, fenced, "fenced").await;
860+
crate::test_support::set_deletion_state(&pool, tombstone, "tombstone").await;
861+
862+
let db = Db::from_pool(pool.clone());
863+
for sweep in 0..2 {
864+
for (caller, hosts) in [
865+
("maintenance", db.active_community_hosts().await),
866+
("bootstrap", db.bootstrap_community_hosts().await),
867+
] {
868+
let hosts = hosts.unwrap_or_else(|e| panic!("{caller} sweep {sweep}: {e}"));
869+
let ids: std::collections::HashSet<Uuid> = hosts.iter().map(|h| h.id).collect();
870+
assert_eq!(
871+
hosts
872+
.iter()
873+
.find(|h| h.id == active)
874+
.map(|h| h.host.as_str()),
875+
Some(active_host.as_str()),
876+
"{caller} sweep {sweep}: active community must be returned"
877+
);
878+
for (label, id) in [
879+
("archived", archived),
880+
("quiescing", quiescing),
881+
("fenced", fenced),
882+
("tombstone", tombstone),
883+
] {
884+
assert!(
885+
!ids.contains(&id),
886+
"{caller} sweep {sweep}: {label} community must be excluded"
887+
);
888+
}
889+
}
890+
}
891+
892+
let metrics_ids: std::collections::HashSet<Uuid> = db
893+
.usage_community_hosts()
894+
.await
895+
.expect("usage-metrics hosts")
896+
.iter()
897+
.map(|h| h.id)
898+
.collect();
899+
for (label, id) in [
900+
("active", active),
901+
("archived", archived),
902+
("quiescing", quiescing),
903+
("fenced", fenced),
904+
("tombstone", tombstone),
905+
] {
906+
assert!(
907+
metrics_ids.contains(&id),
908+
"usage-metrics map must still include the {label} community"
909+
);
910+
}
911+
912+
let (state, deleted): (String, bool) = sqlx::query_as(
913+
"SELECT deletion_state, deleted_at IS NOT NULL FROM communities WHERE id = $1",
914+
)
915+
.bind(tombstone)
916+
.fetch_one(&pool)
917+
.await
918+
.expect("tombstone row is retained");
919+
assert_eq!(state, "tombstone");
920+
assert!(deleted, "tombstone keeps its deleted_at");
921+
}
922+
808923
/// community_count reflects newly inserted communities.
809924
#[tokio::test]
810925
#[ignore = "requires Postgres"]

‎crates/buzz-db/src/test_support.rs‎

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,29 @@ pub(crate) fn database_url() -> String {
77
.or_else(|_| std::env::var("DATABASE_URL"))
88
.unwrap_or_else(|_| DEFAULT_DATABASE_URL.to_owned())
99
}
10+
11+
/// Move a community fixture into a deletion lifecycle state the way the
12+
/// executor does: the tombstone trigger only accepts lifecycle changes from a
13+
/// transaction carrying the executor GUCs for that community.
14+
pub(crate) async fn set_deletion_state(pool: &sqlx::PgPool, id: uuid::Uuid, state: &str) {
15+
let mut tx = pool.begin().await.expect("begin lifecycle fixture");
16+
sqlx::query(
17+
"SELECT set_config('buzz.deletion_executor_community', $1, true), \
18+
set_config('buzz.deletion_fence_generation', '0', true)",
19+
)
20+
.bind(id.to_string())
21+
.execute(&mut *tx)
22+
.await
23+
.expect("authorize lifecycle fixture");
24+
sqlx::query(
25+
"UPDATE communities SET deletion_state = $2, \
26+
deleted_at = CASE WHEN $2 = 'tombstone' THEN now() END \
27+
WHERE id = $1",
28+
)
29+
.bind(id)
30+
.bind(state)
31+
.execute(&mut *tx)
32+
.await
33+
.expect("set lifecycle state");
34+
tx.commit().await.expect("commit lifecycle fixture");
35+
}

0 commit comments

Comments
 (0)