Skip to content

Commit 3564dc2

Browse files
committed
Preserve archive snapshots on duplicate requests
Signed-off-by: Ravneet Arora <[email protected]>
1 parent 572e30e commit 3564dc2

3 files changed

Lines changed: 150 additions & 3 deletions

File tree

‎crates/buzz-relay/src/handlers/side_effects.rs‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4010,6 +4010,33 @@ pub async fn publish_nipia_archival_list(
40104010
..buzz_db::event::EventQuery::for_community(tenant.community())
40114011
})
40124012
.await?;
4013+
// A retry may need to repair a failed publication, but a current
4014+
// snapshot must retain its id/timestamp and emit no new notification.
4015+
// Tag order is not meaningful for the protected set of archived keys.
4016+
if previous.first().is_some_and(|snapshot| {
4017+
snapshot.event.content.is_empty()
4018+
&& snapshot
4019+
.event
4020+
.tags
4021+
.iter()
4022+
.map(Tag::as_slice)
4023+
.collect::<std::collections::BTreeSet<_>>()
4024+
== tags.iter().map(Tag::as_slice).collect()
4025+
}) {
4026+
// The canonical read can become stale before we inspect the head.
4027+
// Keep the same drift protection as the publication path below.
4028+
let current_archived = state.db.list_archived(tenant.community()).await?;
4029+
if archived
4030+
.iter()
4031+
.map(|identity| identity.pubkey.as_str())
4032+
.eq(current_archived
4033+
.iter()
4034+
.map(|identity| identity.pubkey.as_str()))
4035+
{
4036+
return Ok(());
4037+
}
4038+
continue;
4039+
}
40134040
let created_at = previous
40144041
.first()
40154042
.map(|event| (event.event.created_at.as_secs() + 1).max(now))

‎crates/buzz-relay/src/handlers/user_deletion_archive_postgres_tests.rs‎

Lines changed: 120 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,47 @@ async fn archive_and_unarchive_refusals_leave_no_state_or_notification_events()
7979
}
8080
}
8181

82+
#[tokio::test]
83+
#[ignore = "requires Postgres"]
84+
async fn archive_replays_preserve_snapshot_identity_and_timestamp() {
85+
for websocket in [false, true] {
86+
let f = Fixture::new().await;
87+
let archive = archive_request(&f.user, 9035);
88+
let unarchive = archive_request(&f.user, 9036);
89+
for event in [&archive, &unarchive] {
90+
assert!(f.send(&f.user, event, websocket).await.0);
91+
let before = archive_snapshot(&f).await;
92+
for _ in 0..2 {
93+
assert!(f.send(&f.user, event, websocket).await.0);
94+
assert_eq!(
95+
archive_snapshot(&f).await,
96+
before,
97+
"duplicate must preserve every row, including snapshot id and created_at"
98+
);
99+
}
100+
}
101+
let before = archive_snapshot(&f).await;
102+
assert!(f.send(&f.user, &archive, websocket).await.0);
103+
assert_eq!(
104+
archive_snapshot(&f).await,
105+
before,
106+
"old archive replay must not replace the newer unarchive snapshot"
107+
);
108+
assert!(
109+
f.send(&f.user, &archive_request(&f.user, 9035), websocket)
110+
.await
111+
.0
112+
);
113+
let before = archive_snapshot(&f).await;
114+
assert!(f.send(&f.user, &unarchive, websocket).await.0);
115+
assert_eq!(
116+
archive_snapshot(&f).await,
117+
before,
118+
"old unarchive replay must not replace the newer archive snapshot"
119+
);
120+
}
121+
}
122+
82123
#[tokio::test]
83124
#[ignore = "requires Postgres"]
84125
async fn archive_delta_failure_rolls_back_request_and_mutation() {
@@ -142,8 +183,15 @@ async fn archive_delta_failure_rolls_back_request_and_mutation() {
142183
#[tokio::test]
143184
#[ignore = "requires Postgres"]
144185
async fn archive_snapshot_failure_is_retryable_without_reapplying_mutation() {
145-
for websocket in [false, true] {
186+
for (websocket, seed_snapshot) in [(false, false), (false, true), (true, false), (true, true)] {
146187
let f = Fixture::new().await;
188+
if seed_snapshot {
189+
assert!(
190+
f.send(&f.peer, &archive_request(&f.peer, 9035), websocket)
191+
.await
192+
.0
193+
);
194+
}
147195
let archive = archive_request(&f.user, 9035);
148196
sqlx::raw_sql(
149197
"CREATE FUNCTION fail_archive_snapshot() RETURNS trigger LANGUAGE plpgsql AS $$
@@ -182,6 +230,26 @@ async fn archive_snapshot_failure_is_retryable_without_reapplying_mutation() {
182230
f.send(&f.user, &archive, websocket).await.0,
183231
"duplicate retry refreshes snapshot"
184232
);
233+
let tags: Value = sqlx::query_scalar(
234+
"SELECT tags FROM events WHERE community_id=$1 AND kind=13535 AND deleted_at IS NULL",
235+
)
236+
.bind(f.tenant.community().as_uuid())
237+
.fetch_one(&f.pool)
238+
.await
239+
.unwrap();
240+
assert!(
241+
tags.as_array()
242+
.unwrap()
243+
.contains(&serde_json::json!(["p", f.user.public_key().to_hex()])),
244+
"retry must repair a missing or stale snapshot"
245+
);
246+
let repaired = archive_snapshot(&f).await;
247+
assert!(f.send(&f.user, &archive, websocket).await.0);
248+
assert_eq!(
249+
archive_snapshot(&f).await,
250+
repaired,
251+
"retry after repair must leave the snapshot unchanged"
252+
);
185253
let unarchive = archive_request(&f.user, 9036);
186254
assert!(f.send(&f.user, &unarchive, websocket).await.0);
187255
assert!(f.send(&f.user, &archive, websocket).await.0);
@@ -200,7 +268,11 @@ async fn archive_snapshot_failure_is_retryable_without_reapplying_mutation() {
200268
.fetch_one(&f.pool)
201269
.await
202270
.unwrap();
203-
assert_eq!(count, 2, "retries must not create new deltas");
271+
assert_eq!(
272+
count,
273+
if seed_snapshot { 3 } else { 2 },
274+
"retries must not create new deltas"
275+
);
204276
assert!(
205277
f.send(&f.user, &archive_request(&f.user, 9035), websocket)
206278
.await
@@ -218,6 +290,52 @@ async fn archive_snapshot_failure_is_retryable_without_reapplying_mutation() {
218290
}
219291
}
220292

293+
#[tokio::test]
294+
#[ignore = "requires Postgres"]
295+
async fn archive_snapshot_noop_rechecks_concurrent_state_changes() {
296+
let f = Fixture::new().await;
297+
assert!(
298+
f.send(&f.user, &archive_request(&f.user, 9035), false)
299+
.await
300+
.0
301+
);
302+
let (reached, release) = super::side_effects::publish_test_hooks::arm(f.tenant.community());
303+
let state = f.state.clone();
304+
let tenant = f.tenant.clone();
305+
let publisher = tokio::spawn(async move {
306+
super::side_effects::publish_nipia_archival_list(&tenant, &state).await
307+
});
308+
tokio::time::timeout(Duration::from_secs(10), reached)
309+
.await
310+
.unwrap()
311+
.unwrap();
312+
// Keep the old matching snapshot while changing canonical state. A stale
313+
// no-op decision must not leave that snapshot in place.
314+
f.state
315+
.db
316+
.unarchive(f.tenant.community(), &f.user.public_key().to_hex())
317+
.await
318+
.unwrap();
319+
release.notify_one();
320+
tokio::time::timeout(Duration::from_secs(10), publisher)
321+
.await
322+
.unwrap()
323+
.unwrap()
324+
.unwrap();
325+
let tags: Value = sqlx::query_scalar(
326+
"SELECT tags FROM events WHERE community_id=$1 AND kind=13535 AND deleted_at IS NULL",
327+
)
328+
.bind(f.tenant.community().as_uuid())
329+
.fetch_one(&f.pool)
330+
.await
331+
.unwrap();
332+
assert_eq!(
333+
tags,
334+
serde_json::json!([["-"]]),
335+
"publisher must repair the snapshot after detecting canonical drift"
336+
);
337+
}
338+
221339
#[tokio::test]
222340
#[ignore = "requires Postgres"]
223341
async fn archive_requests_wait_for_fence_commit_or_rollback() {

‎docs/user-deletion-message-fencing.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ Their persistence transaction stores the guarded request, changes
4545
back all three. Notifications are dispatched after commit. Duplicate requests
4646
do not reapply the archive change or create another delta.
4747

48-
The archival snapshot (13535) is refreshed after commit. A refresh failure
48+
The archival snapshot (13535) is refreshed after commit only when missing or
49+
stale. An already-current snapshot retains its event ID and timestamp and is
50+
not published again, including on duplicate requests. A refresh failure
4951
propagates to the caller; retrying the request refreshes the snapshot without
5052
repeating the mutation. The committed request and delta remain available for
5153
query/replay. This is not a durable background retry queue: a process exit

0 commit comments

Comments
 (0)