Skip to content

Commit 12f6a88

Browse files
committed
perf(db): prune post-commit push enqueue by signed event timestamp
Signed-off-by: Jemiah Westerman <[email protected]>
1 parent ab39923 commit 12f6a88

11 files changed

Lines changed: 512 additions & 32 deletions

File tree

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

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use std::collections::BTreeSet;
44

55
use buzz_core::CommunityId;
6+
use chrono::{DateTime, Utc};
67
use sqlx::{PgConnection, Postgres, Transaction};
78
use uuid::Uuid;
89

@@ -75,7 +76,12 @@ pub struct AdmittedTx {
7576
/// Channels whose TTL deadline [`AdmittedTx::commit`] refreshes.
7677
ttl_refresh_channels: BTreeSet<Uuid>,
7778
push_enqueue: Option<super::push_enqueue::PushEnqueue>,
78-
push_events: Vec<Vec<u8>>,
79+
push_events: Vec<StagedPushEvent>,
80+
}
81+
82+
struct StagedPushEvent {
83+
event_id: Vec<u8>,
84+
created_at: DateTime<Utc>,
7985
}
8086

8187
impl AdmittedTx {
@@ -118,13 +124,21 @@ impl AdmittedTx {
118124
self.push_enqueue = producer;
119125
}
120126

121-
pub(crate) fn record_push_event(&mut self, event_id: &[u8], kind: i32) {
127+
pub(crate) fn record_push_event(
128+
&mut self,
129+
event_id: &[u8],
130+
created_at: DateTime<Utc>,
131+
kind: i32,
132+
) {
122133
if self.push_enqueue.is_some()
123134
&& crate::store::event_follow_up::PUSH_MATCH_KINDS.contains(&kind)
124135
{
125136
// Bound transaction-local staging as well as the shared worker queue.
126137
if self.push_events.len() < super::push_enqueue::CAPACITY {
127-
self.push_events.push(event_id.to_vec());
138+
self.push_events.push(StagedPushEvent {
139+
event_id: event_id.to_vec(),
140+
created_at,
141+
});
128142
} else {
129143
super::push_enqueue::record_drop("transaction_full");
130144
}
@@ -170,8 +184,8 @@ impl AdmittedTx {
170184
}
171185
self.tx.commit().await?;
172186
if let Some(producer) = self.push_enqueue {
173-
for event_id in self.push_events {
174-
producer.submit(self.community, event_id);
187+
for event in self.push_events {
188+
producer.submit(self.community, event.event_id, event.created_at);
175189
}
176190
}
177191
Ok(())
@@ -191,3 +205,7 @@ impl std::fmt::Debug for AdmittedTx {
191205
.finish_non_exhaustive()
192206
}
193207
}
208+
209+
#[cfg(test)]
210+
#[path = "admitted_tx/postgres_tests.rs"]
211+
mod postgres_tests;
Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
//! Inspect staging at the real savepoint writer boundary, before a missing
2+
//! database row could hide incorrectly staged work from the enqueue worker.
3+
4+
use super::*;
5+
use crate::replaceable::{ParameterizedReplacePrecondition, ParameterizedReplaceStatus};
6+
use crate::{Db, DbConfig};
7+
use nostr::{EventBuilder, Keys, Kind, Tag, Timestamp};
8+
9+
#[tokio::test]
10+
#[ignore = "requires Postgres"]
11+
async fn savepoint_failure_and_duplicate_preserve_only_surviving_timestamp_pairs() {
12+
let db = Db::new(&DbConfig {
13+
database_url: crate::test_support::database_url(),
14+
push_enabled: true,
15+
..DbConfig::default()
16+
})
17+
.await
18+
.unwrap();
19+
let community = CommunityId::from_uuid(Uuid::new_v4());
20+
sqlx::query("INSERT INTO communities (id, host) VALUES ($1, $2)")
21+
.bind(community.as_uuid())
22+
.bind(format!("push-savepoint-{community}.example"))
23+
.execute(db.pool())
24+
.await
25+
.unwrap();
26+
let keys = Keys::generate();
27+
let mention = Keys::generate().public_key().to_hex();
28+
// The generic transaction-bound replacement seam supports arbitrary kinds.
29+
// Use a push-eligible kind here so staging before savepoint release is
30+
// observable; ordinary NIP-33 kinds are not push eligible.
31+
let event = |slot: &str, timestamp: u64| {
32+
EventBuilder::new(Kind::Custom(9), slot)
33+
.tags([
34+
Tag::identifier(slot),
35+
Tag::parse(["p", mention.as_str()]).unwrap(),
36+
])
37+
.custom_created_at(Timestamp::from(timestamp))
38+
.sign_with_keys(&keys)
39+
.unwrap()
40+
};
41+
let retained = event("retained", 1_769_904_000);
42+
let failed = event("failed", 1_772_323_200);
43+
let duplicate = event("duplicate", 1_775_001_600);
44+
let later = event("later", 1_777_593_600);
45+
// A tombstoned row makes the replacement's insert take its actual
46+
// ON CONFLICT / ROLLBACK TO SAVEPOINT arm, not its early live-head no-op.
47+
crate::event::insert_event(db.pool(), community, &duplicate, None)
48+
.await
49+
.unwrap();
50+
sqlx::query("UPDATE events SET deleted_at = now() WHERE community_id = $1 AND id = $2")
51+
.bind(community.as_uuid())
52+
.bind(duplicate.id.as_bytes().as_slice())
53+
.execute(db.pool())
54+
.await
55+
.unwrap();
56+
// Test-local constraint injects an indexing failure after the event INSERT,
57+
// without changing production triggers or any persistent schema contract.
58+
sqlx::query(sqlx::AssertSqlSafe(format!(
59+
"ALTER TABLE event_mentions ADD CONSTRAINT reject_failed_timestamp_test \
60+
CHECK (event_id <> decode('{}', 'hex'))",
61+
hex::encode(failed.id.as_bytes()),
62+
)))
63+
.execute(db.pool())
64+
.await
65+
.unwrap();
66+
let mut tx = db.begin_event_write_transaction(community).await.unwrap();
67+
assert!(
68+
crate::event::insert_event_in_transaction(&mut tx, &retained, None)
69+
.await
70+
.unwrap()
71+
.1
72+
);
73+
assert_eq!(tx.push_events.len(), 1);
74+
let error = db
75+
.replace_parameterized_event_in_transaction(
76+
&mut tx,
77+
&failed,
78+
"failed",
79+
None,
80+
ParameterizedReplacePrecondition::Unconditional,
81+
)
82+
.await
83+
.expect_err("index failure rolls replacement back to savepoint");
84+
assert!(error.to_string().contains("reject_failed_timestamp_test"));
85+
assert_eq!(tx.push_events.len(), 1, "failed savepoint stages nothing");
86+
let replay = db
87+
.replace_parameterized_event_in_transaction(
88+
&mut tx,
89+
&duplicate,
90+
"duplicate",
91+
None,
92+
ParameterizedReplacePrecondition::Unconditional,
93+
)
94+
.await
95+
.unwrap();
96+
assert_eq!(replay.status, ParameterizedReplaceStatus::Duplicate);
97+
assert_eq!(
98+
tx.push_events.len(),
99+
1,
100+
"duplicate savepoint stages nothing"
101+
);
102+
let result = db
103+
.replace_parameterized_event_in_transaction(
104+
&mut tx,
105+
&later,
106+
"later",
107+
None,
108+
ParameterizedReplacePrecondition::Unconditional,
109+
)
110+
.await
111+
.unwrap();
112+
assert_eq!(result.status, ParameterizedReplaceStatus::Inserted);
113+
assert_eq!(tx.push_events.len(), 2);
114+
for (staged, event) in tx.push_events.iter().zip([&retained, &later]) {
115+
assert_eq!(staged.event_id, event.id.as_bytes().as_slice());
116+
assert_eq!(
117+
staged.created_at.timestamp(),
118+
event.created_at.as_secs() as i64
119+
);
120+
}
121+
tx.commit().await.unwrap();
122+
db.push_enqueue.as_ref().unwrap().flush().await;
123+
}

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

Lines changed: 37 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use std::sync::Arc;
44
use std::time::Duration;
55

66
use buzz_core::CommunityId;
7+
use chrono::{DateTime, Utc};
78
use sqlx::{postgres::PgPoolOptions, PgPool};
89
use tokio::sync::{mpsc, Mutex};
910
use tokio::task::JoinHandle;
@@ -12,6 +13,17 @@ use tokio_util::sync::CancellationToken;
1213
pub(crate) const CAPACITY: usize = 256;
1314
const ENQUEUE_TIMEOUT: Duration = Duration::from_secs(2);
1415

16+
// Bind the signed event timestamp, never receipt time: only its monthly partition
17+
// can contain the admitted row. Durable queue records intentionally stay ID-only.
18+
const ENQUEUE_SQL: &str = "INSERT INTO push_match_queue (community_id, event_id) \
19+
SELECT $1, $2 FROM events e \
20+
WHERE e.community_id = $1 AND e.id = $2 AND e.created_at = $3 AND e.deleted_at IS NULL \
21+
AND EXISTS (SELECT 1 FROM push_leases l \
22+
WHERE l.community_id = $1 AND l.active AND l.endpoint_enabled \
23+
AND l.updated_at <= e.received_at \
24+
AND l.expires_at > EXTRACT(EPOCH FROM now())::bigint) \
25+
ON CONFLICT DO NOTHING";
26+
1527
#[derive(Clone, Debug)]
1628
pub(crate) struct PushEnqueue {
1729
sender: mpsc::Sender<Job>,
@@ -23,6 +35,7 @@ pub(crate) struct PushEnqueue {
2335
struct Job {
2436
community: CommunityId,
2537
event_id: Vec<u8>,
38+
created_at: DateTime<Utc>,
2639
#[cfg(test)]
2740
barrier: Option<tokio::sync::oneshot::Sender<()>>,
2841
}
@@ -96,14 +109,20 @@ impl PushEnqueue {
96109
}
97110
}
98111

99-
pub(crate) fn submit(&self, community: CommunityId, event_id: Vec<u8>) {
112+
pub(crate) fn submit(
113+
&self,
114+
community: CommunityId,
115+
event_id: Vec<u8>,
116+
created_at: DateTime<Utc>,
117+
) {
100118
if self.cancel.is_cancelled() {
101119
record_drop("shutdown");
102120
return;
103121
}
104122
if let Err(error) = self.sender.try_send(Job {
105123
community,
106124
event_id,
125+
created_at,
107126
#[cfg(test)]
108127
barrier: None,
109128
}) {
@@ -121,6 +140,7 @@ impl PushEnqueue {
121140
.send(Job {
122141
community: CommunityId::from_uuid(uuid::Uuid::nil()),
123142
event_id: Vec::new(),
143+
created_at: DateTime::UNIX_EPOCH,
124144
barrier: Some(sender),
125145
})
126146
.await
@@ -157,13 +177,15 @@ pub(crate) async fn enqueue_with_gate_barrier(
157177
pool: &PgPool,
158178
community: CommunityId,
159179
event_id: Vec<u8>,
180+
created_at: DateTime<Utc>,
160181
barrier: GateBarrier,
161182
) -> crate::Result<()> {
162183
enqueue(
163184
pool,
164185
&Job {
165186
community,
166187
event_id,
188+
created_at,
167189
barrier: None,
168190
},
169191
Some(barrier),
@@ -202,20 +224,12 @@ async fn enqueue(
202224
// Compare recorded receipt/update times without imposing commit ordering.
203225
// Registration overlapping message arrival may send or suppress a wake.
204226
// This bounded job never scans historical messages on lease activation.
205-
sqlx::query(
206-
"INSERT INTO push_match_queue (community_id, event_id) \
207-
SELECT $1, $2 FROM events e \
208-
WHERE e.community_id = $1 AND e.id = $2 AND e.deleted_at IS NULL \
209-
AND EXISTS (SELECT 1 FROM push_leases l \
210-
WHERE l.community_id = $1 AND l.active AND l.endpoint_enabled \
211-
AND l.updated_at <= e.received_at \
212-
AND l.expires_at > EXTRACT(EPOCH FROM now())::bigint) \
213-
ON CONFLICT DO NOTHING",
214-
)
215-
.bind(job.community.as_uuid())
216-
.bind(&job.event_id)
217-
.execute(&mut *tx)
218-
.await?;
227+
sqlx::query(ENQUEUE_SQL)
228+
.bind(job.community.as_uuid())
229+
.bind(&job.event_id)
230+
.bind(job.created_at)
231+
.execute(&mut *tx)
232+
.await?;
219233
tx.commit().await?;
220234
Ok(())
221235
}
@@ -234,12 +248,12 @@ mod tests {
234248
};
235249
let community = CommunityId::from_uuid(uuid::Uuid::new_v4());
236250
for _ in 0..CAPACITY + 1 {
237-
producer.submit(community, vec![1; 32]);
251+
producer.submit(community, vec![1; 32], DateTime::UNIX_EPOCH);
238252
}
239253
assert_eq!(receiver.len(), CAPACITY);
240254
receiver.try_recv().unwrap();
241255
producer.cancel();
242-
producer.submit(community, vec![2; 32]);
256+
producer.submit(community, vec![2; 32], DateTime::UNIX_EPOCH);
243257
assert_eq!(receiver.len(), CAPACITY - 1);
244258
producer.join().await;
245259
}
@@ -283,11 +297,13 @@ mod tests {
283297
let active = Job {
284298
community: communities[0],
285299
event_id: vec![0_u8; 32],
300+
created_at: DateTime::from_timestamp(1, 0).unwrap(),
286301
barrier: None,
287302
};
288303
let quiescing = Job {
289304
community: communities[1],
290305
event_id: vec![1_u8; 32],
306+
created_at: DateTime::from_timestamp(1, 0).unwrap(),
291307
barrier: None,
292308
};
293309
let err = enqueue(&db.pool, &quiescing, None)
@@ -310,3 +326,7 @@ mod tests {
310326
}
311327
}
312328
}
329+
330+
#[cfg(test)]
331+
#[path = "push_enqueue/postgres_tests.rs"]
332+
mod postgres_tests;

0 commit comments

Comments
 (0)