Skip to content

Commit a504ad6

Browse files
tlongwell-blocknpub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67dTyler Longwell
authored
E1+E3: reduce relay ingest/fan-out DB round trips; ack p99 −7–16%, fd p99 −6–28%, p999 tails −29–53% vs PR #1453 tip (#1454)
Signed-off-by: Tyler Longwell <[email protected]> Co-authored-by: npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67d <011987e296fd5006292d2f930b574be47c7801048d1983c46c425d3c95f0cffd@sprout-oss.stage.blox.sqprod.co> Co-authored-by: Tyler Longwell <[email protected]>
1 parent 7bd3760 commit a504ad6

13 files changed

Lines changed: 377 additions & 51 deletions

File tree

‎Cargo.lock‎

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1758,12 +1758,13 @@ impl Db {
17581758
}
17591759

17601760
/// Delete a workflow only when it belongs to the provided owner.
1761+
/// Returns the deleted workflow's `channel_id`.
17611762
pub async fn delete_workflow_for_owner(
17621763
&self,
17631764
community_id: CommunityId,
17641765
id: Uuid,
17651766
owner_pubkey: &[u8],
1766-
) -> Result<()> {
1767+
) -> Result<Option<Uuid>> {
17671768
workflow::delete_workflow_for_owner(&self.pool, community_id, id, owner_pubkey).await
17681769
}
17691770

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

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,9 @@ pub struct ApprovalRecord {
270270

271271
/// Insert a new workflow record. Returns the new workflow's UUID.
272272
/// New workflows start as `active` and `enabled = TRUE`.
273+
///
274+
/// NOTE: see the cache-invalidation note on [`update_workflow`]. The relay's
275+
/// creation path is [`upsert_workflow`] via event ingest. (No current callers.)
273276
pub async fn create_workflow(
274277
pool: &PgPool,
275278
community_id: CommunityId,
@@ -607,6 +610,11 @@ pub async fn prune_scheduled_workflow_fires_before(
607610
}
608611

609612
/// Update a workflow's name, definition, and definition_hash.
613+
///
614+
/// NOTE: the relay's `WorkflowEngine` caches enabled workflows per
615+
/// `(community_id, channel_id)`; a caller mutating trigger behavior must
616+
/// invalidate via `WorkflowEngine::invalidate_channel_workflows` or trigger
617+
/// matching lags the change by up to the cache TTL. (No current callers.)
610618
pub async fn update_workflow(
611619
pool: &PgPool,
612620
community_id: CommunityId,
@@ -638,6 +646,9 @@ pub async fn update_workflow(
638646
}
639647

640648
/// Update a workflow's status (active -> disabled -> archived).
649+
///
650+
/// NOTE: status gates trigger eligibility; see the cache-invalidation note on
651+
/// [`update_workflow`]. (No current callers.)
641652
pub async fn update_workflow_status(
642653
pool: &PgPool,
643654
community_id: CommunityId,
@@ -665,6 +676,9 @@ pub async fn update_workflow_status(
665676
}
666677

667678
/// Enable or disable a workflow without changing its status.
679+
///
680+
/// NOTE: `enabled` gates trigger eligibility; see the cache-invalidation note
681+
/// on [`update_workflow`]. (No current callers.)
668682
pub async fn set_workflow_enabled(
669683
pool: &PgPool,
670684
community_id: CommunityId,
@@ -692,6 +706,10 @@ pub async fn set_workflow_enabled(
692706
}
693707

694708
/// Delete a workflow and all its runs/approvals (CASCADE).
709+
///
710+
/// NOTE: see the cache-invalidation note on [`update_workflow`]. The relay's
711+
/// deletion path uses [`delete_workflow_for_owner`], which returns the
712+
/// `channel_id` needed for invalidation. (No current callers.)
695713
pub async fn delete_workflow(pool: &PgPool, community_id: CommunityId, id: Uuid) -> Result<()> {
696714
let affected = sqlx::query("DELETE FROM workflows WHERE community_id = $1 AND id = $2")
697715
.bind(community_id.as_uuid())
@@ -712,26 +730,29 @@ pub async fn delete_workflow(pool: &PgPool, community_id: CommunityId, id: Uuid)
712730
/// controlled. Keeping the owner predicate in the DELETE statement avoids a
713731
/// check-then-delete race and ensures a caller cannot delete another user's
714732
/// workflow just by learning its UUID.
733+
///
734+
/// Returns the deleted workflow's `channel_id` so the caller can invalidate
735+
/// the per-channel trigger cache without a separate lookup.
715736
pub async fn delete_workflow_for_owner(
716737
pool: &PgPool,
717738
community_id: CommunityId,
718739
id: Uuid,
719740
owner_pubkey: &[u8],
720-
) -> Result<()> {
721-
let affected = sqlx::query(
722-
"DELETE FROM workflows WHERE community_id = $1 AND id = $2 AND owner_pubkey = $3",
741+
) -> Result<Option<Uuid>> {
742+
let row = sqlx::query(
743+
"DELETE FROM workflows WHERE community_id = $1 AND id = $2 AND owner_pubkey = $3 \
744+
RETURNING channel_id",
723745
)
724746
.bind(community_id.as_uuid())
725747
.bind(id)
726748
.bind(owner_pubkey)
727-
.execute(pool)
728-
.await?
729-
.rows_affected();
749+
.fetch_optional(pool)
750+
.await?;
730751

731-
if affected == 0 {
732-
return Err(DbError::NotFound(format!("workflow {id}")));
752+
match row {
753+
Some(row) => Ok(row.try_get("channel_id")?),
754+
None => Err(DbError::NotFound(format!("workflow {id}"))),
733755
}
734-
Ok(())
735756
}
736757

737758
// -- Workflow Run CRUD --------------------------------------------------------

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,6 +756,12 @@ async fn handle_workflow_def(
756756
other => IngestError::Internal(format!("error: db upsert_workflow: {other}")),
757757
})?;
758758

759+
// Drop the trigger-path cache entry so the new/updated definition fires on
760+
// the next matching event instead of after the cache TTL.
761+
state
762+
.workflow_engine
763+
.invalidate_channel_workflows(community_id, channel_id);
764+
759765
// Commit the event transaction after the idempotent workflow upsert succeeds.
760766
tx.commit()
761767
.await

0 commit comments

Comments
 (0)