Skip to content

Commit 896378c

Browse files
committed
Make DM resurfacing atomic with message acceptance
Signed-off-by: kenny lopez <[email protected]>
1 parent 1101c6f commit 896378c

8 files changed

Lines changed: 540 additions & 129 deletions

File tree

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

Lines changed: 99 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
use chrono::{DateTime, Utc};
77
use sha2::{Digest, Sha256};
8-
use sqlx::{PgPool, Row};
8+
use sqlx::{PgPool, Postgres, Row, Transaction};
99
use uuid::Uuid;
1010

1111
use crate::channel::ChannelRecord;
@@ -505,6 +505,20 @@ pub async fn unhide_dm_recipients(
505505
community_id: CommunityId,
506506
channel_id: Uuid,
507507
sender_pubkey: &[u8],
508+
) -> Result<Vec<Vec<u8>>> {
509+
let mut tx = pool.begin().await?;
510+
let viewers = unhide_dm_recipients_tx(&mut tx, community_id, channel_id, sender_pubkey).await?;
511+
tx.commit().await?;
512+
Ok(viewers)
513+
}
514+
515+
/// Transactional form of [`unhide_dm_recipients`] used when accepted-message
516+
/// persistence and the canonical visibility mutation must commit together.
517+
pub(crate) async fn unhide_dm_recipients_tx(
518+
tx: &mut Transaction<'_, Postgres>,
519+
community_id: CommunityId,
520+
channel_id: Uuid,
521+
sender_pubkey: &[u8],
508522
) -> Result<Vec<Vec<u8>>> {
509523
let rows = sqlx::query(
510524
r#"
@@ -539,7 +553,7 @@ pub async fn unhide_dm_recipients(
539553
.bind(community_id.as_uuid())
540554
.bind(channel_id)
541555
.bind(sender_pubkey)
542-
.fetch_all(pool)
556+
.fetch_all(&mut **tx)
543557
.await?;
544558

545559
rows.into_iter()
@@ -602,17 +616,97 @@ pub async fn claim_dm_visibility_dirty_viewers(
602616
ORDER BY q.lease_until, q.community_id, q.viewer
603617
FOR UPDATE OF q SKIP LOCKED
604618
LIMIT $3
619+
), overflow_fresh AS (
620+
SELECT q.community_id, q.viewer
621+
FROM dm_visibility_dirty_viewers q
622+
JOIN communities c ON c.id = q.community_id
623+
WHERE c.archived_at IS NULL
624+
AND c.deleted_at IS NULL
625+
AND c.deletion_state = 'active'
626+
AND community_write_allowed(q.community_id)
627+
AND q.state = 'pending'
628+
AND q.attempts = 0
629+
AND q.next_attempt_at <= NOW()
630+
AND NOT EXISTS (
631+
SELECT 1 FROM fresh_candidates reserved
632+
WHERE reserved.community_id = q.community_id
633+
AND reserved.viewer = q.viewer
634+
)
635+
ORDER BY q.dirty_at, q.community_id, q.viewer
636+
FOR UPDATE OF q SKIP LOCKED
637+
LIMIT GREATEST(
638+
$4 - (SELECT COUNT(*) FROM fresh_candidates)
639+
- (SELECT COUNT(*) FROM retry_candidates)
640+
- (SELECT COUNT(*) FROM recovery_candidates),
641+
0
642+
)
643+
), overflow_retry AS (
644+
SELECT q.community_id, q.viewer
645+
FROM dm_visibility_dirty_viewers q
646+
JOIN communities c ON c.id = q.community_id
647+
WHERE c.archived_at IS NULL
648+
AND c.deleted_at IS NULL
649+
AND c.deletion_state = 'active'
650+
AND community_write_allowed(q.community_id)
651+
AND q.state = 'pending'
652+
AND q.attempts > 0
653+
AND q.next_attempt_at <= NOW()
654+
AND NOT EXISTS (
655+
SELECT 1 FROM retry_candidates reserved
656+
WHERE reserved.community_id = q.community_id
657+
AND reserved.viewer = q.viewer
658+
)
659+
ORDER BY q.next_attempt_at, q.dirty_at, q.community_id, q.viewer
660+
FOR UPDATE OF q SKIP LOCKED
661+
LIMIT GREATEST(
662+
$4 - (SELECT COUNT(*) FROM fresh_candidates)
663+
- (SELECT COUNT(*) FROM retry_candidates)
664+
- (SELECT COUNT(*) FROM recovery_candidates)
665+
- (SELECT COUNT(*) FROM overflow_fresh),
666+
0
667+
)
668+
), overflow_recovery AS (
669+
SELECT q.community_id, q.viewer
670+
FROM dm_visibility_dirty_viewers q
671+
JOIN communities c ON c.id = q.community_id
672+
WHERE c.archived_at IS NULL
673+
AND c.deleted_at IS NULL
674+
AND c.deletion_state = 'active'
675+
AND community_write_allowed(q.community_id)
676+
AND q.state = 'publishing'
677+
AND q.lease_until <= NOW()
678+
AND NOT EXISTS (
679+
SELECT 1 FROM recovery_candidates reserved
680+
WHERE reserved.community_id = q.community_id
681+
AND reserved.viewer = q.viewer
682+
)
683+
ORDER BY q.lease_until, q.community_id, q.viewer
684+
FOR UPDATE OF q SKIP LOCKED
685+
LIMIT GREATEST(
686+
$4 - (SELECT COUNT(*) FROM fresh_candidates)
687+
- (SELECT COUNT(*) FROM retry_candidates)
688+
- (SELECT COUNT(*) FROM recovery_candidates)
689+
- (SELECT COUNT(*) FROM overflow_fresh)
690+
- (SELECT COUNT(*) FROM overflow_retry),
691+
0
692+
)
605693
), candidates AS (
606694
SELECT community_id, viewer FROM fresh_candidates
607695
UNION ALL
608696
SELECT community_id, viewer FROM retry_candidates
609697
UNION ALL
610698
SELECT community_id, viewer FROM recovery_candidates
699+
UNION ALL
700+
SELECT community_id, viewer FROM overflow_fresh
701+
UNION ALL
702+
SELECT community_id, viewer FROM overflow_retry
703+
UNION ALL
704+
SELECT community_id, viewer FROM overflow_recovery
611705
)
612706
UPDATE dm_visibility_dirty_viewers q
613707
SET state = 'publishing',
614-
claim_id = $4,
615-
lease_until = NOW() + make_interval(secs => $5::int),
708+
claim_id = $5,
709+
lease_until = NOW() + make_interval(secs => $6::int),
616710
attempts = q.attempts + 1
617711
FROM candidates candidate, communities c
618712
WHERE q.community_id = candidate.community_id
@@ -624,6 +718,7 @@ pub async fn claim_dm_visibility_dirty_viewers(
624718
.bind(fresh_limit)
625719
.bind(retry_limit)
626720
.bind(recovery_limit)
721+
.bind(limit)
627722
.bind(claim_id)
628723
.bind(lease_seconds.clamp(1, 3_600) as i32)
629724
.fetch_all(pool)

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

Lines changed: 195 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2265,6 +2265,48 @@ impl Db {
22652265
Ok(result)
22662266
}
22672267

2268+
/// Atomically inserts a DM message, its optional thread metadata, and the
2269+
/// recipient visibility mutations that make the accepted message visible.
2270+
///
2271+
/// Duplicate events do not repeat the visibility mutation. The returned
2272+
/// viewers are the recipients whose canonical hidden state changed and
2273+
/// whose relay-authored visibility snapshots should be published after
2274+
/// this transaction commits.
2275+
#[datastore_span(name = "insert_dm_event_with_thread_metadata", system = "postgresql")]
2276+
pub async fn insert_dm_event_with_thread_metadata(
2277+
&self,
2278+
community_id: CommunityId,
2279+
event: &nostr::Event,
2280+
channel_id: Uuid,
2281+
thread_meta: Option<event::ThreadMetadataParams<'_>>,
2282+
sender_pubkey: &[u8],
2283+
) -> Result<(StoredEvent, bool, Vec<Vec<u8>>)> {
2284+
let mut tx = self.pool.begin().await?;
2285+
let (stored_event, was_inserted) = event::insert_event_with_thread_metadata_tx(
2286+
&mut tx,
2287+
community_id,
2288+
event,
2289+
Some(channel_id),
2290+
thread_meta,
2291+
)
2292+
.await?;
2293+
let resurfaced_viewers = if was_inserted {
2294+
dm::unhide_dm_recipients_tx(&mut tx, community_id, channel_id, sender_pubkey).await?
2295+
} else {
2296+
Vec::new()
2297+
};
2298+
tx.commit().await?;
2299+
2300+
if was_inserted {
2301+
if let Err(e) = insert_mentions(&self.pool, community_id, event, Some(channel_id)).await
2302+
{
2303+
tracing::warn!(event_id = %event.id, "Failed to insert mentions: {e}");
2304+
}
2305+
}
2306+
2307+
Ok((stored_event, was_inserted, resurfaced_viewers))
2308+
}
2309+
22682310
/// Atomically insert a kind:7 reaction event and its reaction row.
22692311
#[allow(clippy::too_many_arguments)]
22702312
#[datastore_span(
@@ -7500,6 +7542,156 @@ mod tests {
75007542
);
75017543
}
75027544

7545+
#[tokio::test]
7546+
#[ignore = "requires Postgres"]
7547+
async fn dm_event_insert_and_recipient_resurface_roll_back_together() {
7548+
use nostr::{EventBuilder, Keys, Kind, Tag};
7549+
7550+
let admin = PgPool::connect(&admin_url().await)
7551+
.await
7552+
.expect("connect admin");
7553+
let (pool, scratch_name) = create_scratch_db(&admin, "dm_insert_resurface").await;
7554+
let db = Db::from_pool(pool.clone());
7555+
let community_uuid = Uuid::new_v4();
7556+
let community = CommunityId::from_uuid(community_uuid);
7557+
let dm_channel = Uuid::new_v4();
7558+
let unrelated_dm = Uuid::new_v4();
7559+
let sender = Keys::generate();
7560+
let recipient = Keys::generate();
7561+
let sender_bytes = sender.public_key().to_bytes();
7562+
let recipient_bytes = recipient.public_key().to_bytes();
7563+
7564+
sqlx::query("INSERT INTO communities (id, host) VALUES ($1, $2)")
7565+
.bind(community_uuid)
7566+
.bind(format!("dm-insert-{}.example", community_uuid.simple()))
7567+
.execute(&pool)
7568+
.await
7569+
.expect("insert community");
7570+
for channel_id in [dm_channel, unrelated_dm] {
7571+
sqlx::query(
7572+
"INSERT INTO channels \
7573+
(id, community_id, name, channel_type, visibility, created_by) \
7574+
VALUES ($1, $2, 'atomic DM', 'dm', 'private', $3)",
7575+
)
7576+
.bind(channel_id)
7577+
.bind(community_uuid)
7578+
.bind(sender_bytes.as_slice())
7579+
.execute(&pool)
7580+
.await
7581+
.expect("insert DM");
7582+
}
7583+
for (channel_id, pubkey) in [
7584+
(dm_channel, sender_bytes.as_slice()),
7585+
(dm_channel, recipient_bytes.as_slice()),
7586+
(unrelated_dm, recipient_bytes.as_slice()),
7587+
] {
7588+
sqlx::query(
7589+
"INSERT INTO channel_members \
7590+
(community_id, channel_id, pubkey, hidden_at) \
7591+
VALUES ($1, $2, $3, NOW())",
7592+
)
7593+
.bind(community_uuid)
7594+
.bind(channel_id)
7595+
.bind(pubkey)
7596+
.execute(&pool)
7597+
.await
7598+
.expect("insert hidden DM member");
7599+
}
7600+
7601+
sqlx::raw_sql(
7602+
"CREATE FUNCTION fail_dm_visibility_enqueue() RETURNS trigger \
7603+
LANGUAGE plpgsql AS $$ BEGIN RAISE EXCEPTION 'injected enqueue failure'; END $$; \
7604+
CREATE TRIGGER fail_dm_visibility_enqueue \
7605+
BEFORE INSERT OR UPDATE ON dm_visibility_dirty_viewers \
7606+
FOR EACH ROW EXECUTE FUNCTION fail_dm_visibility_enqueue();",
7607+
)
7608+
.execute(&pool)
7609+
.await
7610+
.expect("install visibility failure injection");
7611+
7612+
let event = EventBuilder::new(Kind::Custom(9), "accepted DM")
7613+
.tags(vec![
7614+
Tag::parse(["h", &dm_channel.to_string()]).expect("h tag")
7615+
])
7616+
.sign_with_keys(&sender)
7617+
.expect("sign DM event");
7618+
db.insert_dm_event_with_thread_metadata(
7619+
community,
7620+
&event,
7621+
dm_channel,
7622+
None,
7623+
sender_bytes.as_slice(),
7624+
)
7625+
.await
7626+
.expect_err("injected enqueue failure must abort accepted-message transaction");
7627+
7628+
let persisted_after_failure: i64 =
7629+
sqlx::query_scalar("SELECT COUNT(*) FROM events WHERE community_id = $1 AND id = $2")
7630+
.bind(community_uuid)
7631+
.bind(event.id.as_bytes().as_slice())
7632+
.fetch_one(&pool)
7633+
.await
7634+
.expect("count rolled-back event");
7635+
assert_eq!(persisted_after_failure, 0);
7636+
let hidden_after_failure: bool = sqlx::query_scalar(
7637+
"SELECT hidden_at IS NOT NULL FROM channel_members \
7638+
WHERE community_id = $1 AND channel_id = $2 AND pubkey = $3",
7639+
)
7640+
.bind(community_uuid)
7641+
.bind(dm_channel)
7642+
.bind(recipient_bytes.as_slice())
7643+
.fetch_one(&pool)
7644+
.await
7645+
.expect("read recipient after rollback");
7646+
assert!(hidden_after_failure);
7647+
7648+
sqlx::raw_sql(
7649+
"DROP TRIGGER fail_dm_visibility_enqueue ON dm_visibility_dirty_viewers; \
7650+
DROP FUNCTION fail_dm_visibility_enqueue();",
7651+
)
7652+
.execute(&pool)
7653+
.await
7654+
.expect("remove visibility failure injection");
7655+
let (_stored, inserted, resurfaced) = db
7656+
.insert_dm_event_with_thread_metadata(
7657+
community,
7658+
&event,
7659+
dm_channel,
7660+
None,
7661+
sender_bytes.as_slice(),
7662+
)
7663+
.await
7664+
.expect("retry accepted DM transaction");
7665+
assert!(inserted);
7666+
assert_eq!(resurfaced, vec![recipient_bytes.to_vec()]);
7667+
7668+
let hidden_states: Vec<(Uuid, Vec<u8>, bool)> = sqlx::query_as(
7669+
"SELECT channel_id, pubkey, hidden_at IS NOT NULL \
7670+
FROM channel_members WHERE community_id = $1 ORDER BY channel_id, pubkey",
7671+
)
7672+
.bind(community_uuid)
7673+
.fetch_all(&pool)
7674+
.await
7675+
.expect("read final hidden states");
7676+
assert!(hidden_states
7677+
.iter()
7678+
.any(|(channel, pubkey, hidden)| *channel == dm_channel
7679+
&& pubkey == sender_bytes.as_slice()
7680+
&& *hidden));
7681+
assert!(hidden_states
7682+
.iter()
7683+
.any(|(channel, pubkey, hidden)| *channel == dm_channel
7684+
&& pubkey == recipient_bytes.as_slice()
7685+
&& !*hidden));
7686+
assert!(hidden_states
7687+
.iter()
7688+
.any(|(channel, pubkey, hidden)| *channel == unrelated_dm
7689+
&& pubkey == recipient_bytes.as_slice()
7690+
&& *hidden));
7691+
7692+
drop_scratch_db(&admin, pool, &scratch_name).await;
7693+
}
7694+
75037695
#[tokio::test]
75047696
#[ignore = "requires Postgres"]
75057697
async fn dm_visibility_fresh_claims_are_not_starved_by_poison_retries() {
@@ -7547,8 +7739,8 @@ mod tests {
75477739
);
75487740
assert_eq!(
75497741
claims.len(),
7550-
26,
7551-
"the batch reserves half for fresh work and a quarter for retries"
7742+
100,
7743+
"reserved fairness capacity must not leave the rest of the batch idle"
75527744
);
75537745

75547746
let retry_claim = claims
@@ -7563,7 +7755,7 @@ mod tests {
75637755
.await
75647756
.expect("release retry claim");
75657757
let retry_delay_seconds: f64 = sqlx::query_scalar(
7566-
"SELECT EXTRACT(EPOCH FROM (next_attempt_at - NOW())) \
7758+
"SELECT EXTRACT(EPOCH FROM (next_attempt_at - NOW()))::DOUBLE PRECISION \
75677759
FROM dm_visibility_dirty_viewers \
75687760
WHERE community_id = $1 AND viewer = $2",
75697761
)

0 commit comments

Comments
 (0)