@@ -3407,30 +3407,55 @@ pub async fn reconcile_nip43_membership_snapshots(state: &Arc<AppState>) -> anyh
34073407 Ok ( reconciled)
34083408}
34093409
3410+ /// Deployment-wide leader lock for the NIP-43 snapshot reconciler.
3411+ pub const NIP43_RECONCILE_LOCK_KEY : i64 = 0x4255_5A5A_4E34_3352 ;
3412+
34103413/// Spawn the background NIP-43 snapshot reconciler.
34113414///
3412- /// The first sweep starts as soon as the task runs; each later sweep starts
3413- /// `every` after the previous one finishes. Startup never waits on it: the
3414- /// snapshots live in the shared database, so a new replica has nothing of its
3415- /// own to repair before it serves. Trade-off: on a single-replica deployment, a
3416- /// missing or stale kind:13534 snapshot is served until this replica's first
3417- /// sweep reaches that community.
3415+ /// Only the replica holding the `lock_key` session advisory lock sweeps; the
3416+ /// others retry the lock once per `every`. The holder keeps the lock across
3417+ /// sweeps, and Postgres releases it when that session ends, so a killed leader
3418+ /// is replaced on a standby's next attempt. Each sweep (and each attempt to
3419+ /// lead) starts `every` after the previous one finished; the first runs as soon
3420+ /// as the task does. Startup never waits on it: the snapshots live in the shared
3421+ /// database, so a new replica has nothing of its own to repair before it serves.
3422+ /// Trade-off: a missing or stale kind:13534 snapshot is served until the
3423+ /// leader's sweep reaches that community.
34183424pub fn spawn_nip43_membership_reconciler (
34193425 state : Arc < AppState > ,
3426+ lock_key : i64 ,
34203427 every : std:: time:: Duration ,
34213428) -> tokio:: task:: JoinHandle < ( ) > {
34223429 tokio:: spawn ( async move {
3423- // Sweep first, then sleep: the first sweep starts immediately and
3424- // every later one waits a full `every` after the previous sweep ends,
3425- // even when a sweep outlasts `every`.
3430+ let mut leader: Option < buzz_db:: SessionLeader > = None ;
34263431 loop {
3427- match reconcile_nip43_membership_snapshots ( & state) . await {
3428- Ok ( repaired) if repaired > 0 => {
3429- info ! ( count = repaired, "NIP-43 membership snapshots repaired" )
3432+ if let Some ( guard) = leader. as_mut ( ) {
3433+ if !guard. is_live ( ) . await {
3434+ warn ! ( "NIP-43 reconciler leader lock connection lost; demoting" ) ;
3435+ leader = None ;
3436+ }
3437+ }
3438+ if leader. is_none ( ) {
3439+ match state. db . try_lock_session_leader ( lock_key) . await {
3440+ Ok ( Some ( guard) ) => {
3441+ info ! ( "Acquired NIP-43 reconciler leader lock" ) ;
3442+ leader = Some ( guard) ;
3443+ }
3444+ Ok ( None ) => { }
3445+ Err ( error) => {
3446+ warn ! ( %error, "NIP-43 reconciler leader lock attempt failed" )
3447+ }
34303448 }
3431- Ok ( _) => { }
3432- Err ( error) => {
3433- warn ! ( %error, "periodic NIP-43 membership snapshot reconciliation failed" )
3449+ }
3450+ if leader. is_some ( ) {
3451+ match reconcile_nip43_membership_snapshots ( & state) . await {
3452+ Ok ( repaired) if repaired > 0 => {
3453+ info ! ( count = repaired, "NIP-43 membership snapshots repaired" )
3454+ }
3455+ Ok ( _) => { }
3456+ Err ( error) => {
3457+ warn ! ( %error, "periodic NIP-43 membership snapshot reconciliation failed" )
3458+ }
34343459 }
34353460 }
34363461 tokio:: time:: sleep ( every) . await ;
@@ -4354,6 +4379,7 @@ mod tests {
43544379 // sweep can publish the snapshot.
43554380 let reconciler = spawn_nip43_membership_reconciler (
43564381 Arc :: clone ( & state) ,
4382+ scratch_lock_key ( ) ,
43574383 std:: time:: Duration :: from_secs ( 24 * 60 * 60 ) ,
43584384 ) ;
43594385 let published = tokio:: time:: timeout ( std:: time:: Duration :: from_secs ( 60 ) , async {
@@ -4370,6 +4396,86 @@ mod tests {
43704396 assert_eq ! ( membership_snapshots( & pool, community) . await , 1 ) ;
43714397 }
43724398
4399+ /// A per-test leader key, so concurrent tests and any relay sharing
4400+ /// the test database never contend for the production lock.
4401+ fn scratch_lock_key ( ) -> i64 {
4402+ Uuid :: new_v4 ( ) . as_u64_pair ( ) . 0 as i64
4403+ }
4404+
4405+ /// Only the leader-lock holder sweeps. A replica that can't take the
4406+ /// lock never publishes, and it takes over once the holder lets go.
4407+ /// Runs in a private database so no other test's sweep can publish
4408+ /// the snapshot this test watches.
4409+ #[ tokio:: test]
4410+ #[ ignore = "requires Postgres" ]
4411+ async fn background_reconciler_sweeps_only_while_holding_leader_lock ( ) {
4412+ let admin_url = crate :: test_support:: database_url ( ) ;
4413+ let admin = sqlx:: PgPool :: connect ( & admin_url)
4414+ . await
4415+ . expect ( "connect admin to create scratch db" ) ;
4416+ let name = format ! ( "nip43_leader_{}" , Uuid :: new_v4( ) . simple( ) ) ;
4417+ sqlx:: query ( sqlx:: AssertSqlSafe ( format ! ( "CREATE DATABASE {name}" ) ) )
4418+ . execute ( & admin)
4419+ . await
4420+ . expect ( "create scratch db" ) ;
4421+ let slash = admin_url
4422+ . rfind ( '/' )
4423+ . expect ( "URL must have a path component" ) ;
4424+ let pool = sqlx:: PgPool :: connect ( & format ! ( "{}/{name}" , & admin_url[ ..slash] ) )
4425+ . await
4426+ . expect ( "connect scratch db" ) ;
4427+ buzz_db:: migration:: run_migrations ( & pool)
4428+ . await
4429+ . expect ( "migrate scratch db" ) ;
4430+ let state = crate :: state:: tests:: test_state_with_database_pool ( pool. clone ( ) ) . await ;
4431+ let community = insert_community ( & pool) . await ;
4432+
4433+ // Another "replica" holds the leader lock.
4434+ let key = scratch_lock_key ( ) ;
4435+ let mut holder = pool. acquire ( ) . await . expect ( "acquire lock holder" ) ;
4436+ sqlx:: query ( "SELECT pg_advisory_lock($1)" )
4437+ . bind ( key)
4438+ . execute ( & mut * holder)
4439+ . await
4440+ . expect ( "take leader lock" ) ;
4441+
4442+ let every = std:: time:: Duration :: from_millis ( 100 ) ;
4443+ let reconciler = spawn_nip43_membership_reconciler ( Arc :: clone ( & state) , key, every) ;
4444+ // Twenty lock attempts' worth of standby: none may sweep.
4445+ tokio:: time:: sleep ( every * 20 ) . await ;
4446+ let standby_snapshots = membership_snapshots ( & pool, community) . await ;
4447+
4448+ sqlx:: query ( "SELECT pg_advisory_unlock($1)" )
4449+ . bind ( key)
4450+ . execute ( & mut * holder)
4451+ . await
4452+ . expect ( "release leader lock" ) ;
4453+ let published = tokio:: time:: timeout ( std:: time:: Duration :: from_secs ( 60 ) , async {
4454+ while membership_snapshots ( & pool, community) . await == 0 {
4455+ tokio:: time:: sleep ( std:: time:: Duration :: from_millis ( 50 ) ) . await ;
4456+ }
4457+ } )
4458+ . await ;
4459+ reconciler. abort ( ) ;
4460+ drop ( holder) ;
4461+ drop ( state) ;
4462+ pool. close ( ) . await ;
4463+ let _ = sqlx:: query ( sqlx:: AssertSqlSafe ( format ! (
4464+ "DROP DATABASE IF EXISTS {name} WITH (FORCE)"
4465+ ) ) )
4466+ . execute ( & admin)
4467+ . await ;
4468+
4469+ assert_eq ! (
4470+ standby_snapshots, 0 ,
4471+ "a replica without the leader lock must not sweep"
4472+ ) ;
4473+ assert ! (
4474+ published. is_ok( ) ,
4475+ "the standby must take over and sweep once the leader lock is released"
4476+ ) ;
4477+ }
4478+
43734479 /// Regression for #7558 on the worker path: repeated NIP-43
43744480 /// maintenance sweeps reconcile the active community once and never
43754481 /// hand archived, quiescing, fenced, or tombstoned communities to the
0 commit comments