Skip to content

Commit 2b4a7b2

Browse files
author
Elrond
committed
fix(db): attribute community lifecycle fence transactions
Signed-off-by: Elrond <28d6302a099e5225b02c4155ac4236e4912603df2ab08dbfc2f4fef08ce598c8@buzz.block.builderlab.xyz>
1 parent acaadfe commit 2b4a7b2

2 files changed

Lines changed: 65 additions & 40 deletions

File tree

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

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -226,17 +226,21 @@ pub(crate) enum TransactionOperation {
226226
AcceptPushLeaseEvent,
227227
BeginCommunityDeletionQuiescing,
228228
FenceCommunityDeletion,
229+
CommunityArchiveFence,
230+
InactiveCommunityFence,
229231
}
230232

231233
impl TransactionOperation {
232234
#[cfg(test)]
233-
pub(crate) const ALL: [Self; 6] = [
235+
pub(crate) const ALL: [Self; 8] = [
234236
Self::ReplaceParameterizedEvent,
235237
Self::ReplaceAddressableEvent,
236238
Self::PublishNip43MembershipLocked,
237239
Self::AcceptPushLeaseEvent,
238240
Self::BeginCommunityDeletionQuiescing,
239241
Self::FenceCommunityDeletion,
242+
Self::CommunityArchiveFence,
243+
Self::InactiveCommunityFence,
240244
];
241245

242246
pub(crate) const fn as_str(self) -> &'static str {
@@ -247,6 +251,8 @@ impl TransactionOperation {
247251
Self::AcceptPushLeaseEvent => "accept_push_lease_event",
248252
Self::BeginCommunityDeletionQuiescing => "begin_community_deletion_quiescing",
249253
Self::FenceCommunityDeletion => "fence_community_deletion",
254+
Self::CommunityArchiveFence => "community_archive_fence",
255+
Self::InactiveCommunityFence => "inactive_community_fence",
250256
}
251257
}
252258

@@ -256,9 +262,10 @@ impl TransactionOperation {
256262
| Self::ReplaceAddressableEvent
257263
| Self::PublishNip43MembershipLocked
258264
| Self::AcceptPushLeaseEvent => WriterOperation::EventWrite,
259-
Self::BeginCommunityDeletionQuiescing | Self::FenceCommunityDeletion => {
260-
WriterOperation::Maintenance
261-
}
265+
Self::BeginCommunityDeletionQuiescing
266+
| Self::FenceCommunityDeletion
267+
| Self::CommunityArchiveFence
268+
| Self::InactiveCommunityFence => WriterOperation::Maintenance,
262269
}
263270
}
264271
}
@@ -636,6 +643,8 @@ mod tests {
636643
"accept_push_lease_event",
637644
"begin_community_deletion_quiescing",
638645
"fence_community_deletion",
646+
"community_archive_fence",
647+
"inactive_community_fence",
639648
]
640649
);
641650
}

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

Lines changed: 52 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -197,29 +197,37 @@ impl Db {
197197
archived_at: DateTime<Utc>,
198198
apply: impl FnOnce() -> T,
199199
) -> Result<Option<T>> {
200-
let mut tx = self.pool.begin().await?;
201-
let matches = sqlx::query_scalar::<_, bool>(
202-
r#"SELECT COALESCE(archived_at = $2, FALSE)
200+
let (mut tx, transaction_timer) = crate::observability::begin_transaction(
201+
&self.pool,
202+
crate::observability::TransactionOperation::CommunityArchiveFence,
203+
)
204+
.await?;
205+
transaction_timer
206+
.observe(async move {
207+
let matches = sqlx::query_scalar::<_, bool>(
208+
r#"SELECT COALESCE(archived_at = $2, FALSE)
203209
FROM communities
204210
WHERE id = $1
205211
AND deletion_state = 'active'
206212
AND deleted_at IS NULL
207213
FOR UPDATE"#,
208-
)
209-
.bind(community_id.as_uuid())
210-
.bind(archived_at)
211-
.fetch_optional(&mut *tx)
212-
.await?
213-
.unwrap_or(false);
214+
)
215+
.bind(community_id.as_uuid())
216+
.bind(archived_at)
217+
.fetch_optional(&mut *tx)
218+
.await?
219+
.unwrap_or(false);
214220

215-
if !matches {
216-
tx.rollback().await?;
217-
return Ok(None);
218-
}
221+
if !matches {
222+
tx.rollback().await?;
223+
return Ok(None);
224+
}
219225

220-
let result = apply();
221-
tx.commit().await?;
222-
Ok(Some(result))
226+
let result = apply();
227+
tx.commit().await?;
228+
Ok(Some(result))
229+
})
230+
.await
223231
}
224232

225233
/// Runs `apply` while holding the community row lock only when the
@@ -235,30 +243,38 @@ impl Db {
235243
community_id: CommunityId,
236244
apply: impl FnOnce(Option<DateTime<Utc>>) -> T,
237245
) -> Result<Option<T>> {
238-
let mut tx = self.pool.begin().await?;
239-
let lifecycle = sqlx::query_as::<_, (Option<DateTime<Utc>>, bool)>(
240-
r#"SELECT archived_at,
246+
let (mut tx, transaction_timer) = crate::observability::begin_transaction(
247+
&self.pool,
248+
crate::observability::TransactionOperation::InactiveCommunityFence,
249+
)
250+
.await?;
251+
transaction_timer
252+
.observe(async move {
253+
let lifecycle = sqlx::query_as::<_, (Option<DateTime<Utc>>, bool)>(
254+
r#"SELECT archived_at,
241255
deleted_at IS NOT NULL OR deletion_state <> 'active'
242256
FROM communities
243257
WHERE id = $1
244258
FOR UPDATE"#,
245-
)
246-
.bind(community_id.as_uuid())
247-
.fetch_optional(&mut *tx)
248-
.await?;
249-
250-
let archived_at = match lifecycle {
251-
Some((None, false)) => {
252-
tx.rollback().await?;
253-
return Ok(None);
254-
}
255-
Some((archived_at, false)) => archived_at,
256-
Some((_, true)) | None => None,
257-
};
258-
259-
let result = apply(archived_at);
260-
tx.commit().await?;
261-
Ok(Some(result))
259+
)
260+
.bind(community_id.as_uuid())
261+
.fetch_optional(&mut *tx)
262+
.await?;
263+
264+
let archived_at = match lifecycle {
265+
Some((None, false)) => {
266+
tx.rollback().await?;
267+
return Ok(None);
268+
}
269+
Some((archived_at, false)) => archived_at,
270+
Some((_, true)) | None => None,
271+
};
272+
273+
let result = apply(archived_at);
274+
tx.commit().await?;
275+
Ok(Some(result))
276+
})
277+
.await
262278
}
263279

264280
/// Returns a community by host regardless of lifecycle state. Operator-plane only.

0 commit comments

Comments
 (0)