Skip to content

Commit 30d2941

Browse files
npub1qyvc0c5kl4gqv2fd97fsk46tu378sqgy35vc83rvgfwne90sel7s0ed67dTyler Longwell
andcommitted
perf(workflow): cache enabled-workflow lookups per channel with sync invalidation
E3 (correctness ruling §4.10): every ingested channel event ran a list_enabled_channel_workflows SELECT to find trigger candidates, and most channels have no workflows at all. Add a moka look-aside cache in WorkflowEngine keyed (community_id, channel_id) so the trigger path hits the DB once per channel per TTL instead of once per event. §4.10 fences: - Key is community-scoped (community_id, channel_id) — channel UUIDs collide across communities (Inv_LabelPropagation). - TTL is 10s (ruling allows ≤30s), matching the relay's other moka caches. - Negative results are cached: an empty list is inserted like any other, which is where most of the win is. - Synchronous invalidation at every live mutation site. Audit (per Wren's broad definition — ingest upsert/delete, HTTP/API toggles, admin/test helpers, soft-delete/reactivation): the only paths that write trigger eligibility or channel binding are the kind:30620 command upsert and NIP-09 a-tag deletion; both invalidate immediately after the DB write. delete_workflow_for_owner now RETURNING channel_id so the deletion path invalidates without a second lookup. The unused buzz-db mutators (create_workflow, update_workflow, update_workflow_status, set_workflow_enabled, delete_workflow) have no callers anywhere in the workspace — CLI and desktop route through event submission, webhook and approval-resume only write workflow_runs — and each carries a doc note requiring shared invalidation from any future caller. Consistency: no cross-pod invalidation, deliberately. Workflow triggering is not an access-control fence; the worst case on another pod is a just-deleted workflow firing (or a just-created one missing events) for up to 10s. The same TTL bounds the look-aside fill race. Co-authored-by: Tyler Longwell <[email protected]> Signed-off-by: Tyler Longwell <[email protected]>
1 parent 42dd950 commit 30d2941

7 files changed

Lines changed: 96 additions & 18 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

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1826,11 +1826,16 @@ async fn handle_a_tag_deletion(
18261826
buzz_core::kind::KIND_WORKFLOW_DEF => {
18271827
// Try UUID first (workflow_id); fall back to name-based lookup.
18281828
if let Ok(wf_id) = uuid::Uuid::parse_str(d_tag) {
1829-
state
1829+
let channel_id = state
18301830
.db
18311831
.delete_workflow_for_owner(tenant.community(), wf_id, &actor_bytes)
18321832
.await
18331833
.map_err(|e| anyhow::anyhow!("failed to delete workflow {wf_id}: {e}"))?;
1834+
if let Some(channel_id) = channel_id {
1835+
state
1836+
.workflow_engine
1837+
.invalidate_channel_workflows(tenant.community(), channel_id);
1838+
}
18341839
tracing::info!(workflow_id = %wf_id, "Workflow deleted via NIP-09 a-tag (UUID)");
18351840
} else {
18361841
// Name-based lookup
@@ -1840,13 +1845,18 @@ async fn handle_a_tag_deletion(
18401845
.await
18411846
{
18421847
Ok(Some(wf)) => {
1843-
state
1848+
let channel_id = state
18441849
.db
18451850
.delete_workflow_for_owner(tenant.community(), wf.id, &actor_bytes)
18461851
.await
18471852
.map_err(|e| {
18481853
anyhow::anyhow!("failed to delete workflow {}: {e}", wf.id)
18491854
})?;
1855+
if let Some(channel_id) = channel_id {
1856+
state
1857+
.workflow_engine
1858+
.invalidate_channel_workflows(tenant.community(), channel_id);
1859+
}
18501860
tracing::info!(workflow_id = %wf.id, name = d_tag, "Workflow deleted via NIP-09 a-tag (name)");
18511861
}
18521862
Ok(None) => {

‎crates/buzz-workflow/Cargo.toml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ serde = { workspace = true }
1515
serde_json = { workspace = true }
1616
serde_yaml = { workspace = true }
1717
dashmap = { workspace = true }
18+
moka = { workspace = true }
1819
evalexpr = "11"
1920
cron = "0.16"
2021
uuid = { workspace = true }

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

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ pub struct WorkflowEngine {
8787
/// Action sink for executing side-effects (SendMessage, etc.).
8888
/// Late-initialized via [`set_action_sink`] after `AppState` construction.
8989
pub(crate) action_sink: OnceLock<Arc<dyn ActionSink>>,
90+
/// Short-TTL cache for the per-event enabled-workflow lookup, keyed
91+
/// `(community_id, channel_id)`. Most channels have no workflows, so this
92+
/// removes one SELECT from nearly every ingested event.
93+
///
94+
/// Consistency: the relay invalidates this cache on its own pod at the two
95+
/// workflow mutation sites (command upsert, NIP-09 deletion). There is
96+
/// deliberately no cross-pod invalidation — workflow triggering is not an
97+
/// access-control fence, so the worst case on another pod is a just-deleted
98+
/// workflow firing (or a just-created one missing events) for up to the TTL.
99+
/// The same TTL also bounds the same-pod look-aside race (a stale fill
100+
/// landing just after an invalidation). Workflow mutations are rare; the
101+
/// 10s window matches the relay's other moka caches (see `AppState` in
102+
/// `buzz-relay`).
103+
pub(crate) workflow_cache:
104+
moka::sync::Cache<(CommunityId, Uuid), Arc<Vec<buzz_db::workflow::WorkflowRecord>>>,
90105
}
91106

92107
impl WorkflowEngine {
@@ -100,9 +115,23 @@ impl WorkflowEngine {
100115
run_semaphore,
101116
last_fired: DashMap::new(),
102117
action_sink: OnceLock::new(),
118+
workflow_cache: moka::sync::Cache::builder()
119+
.max_capacity(10_000)
120+
.time_to_live(std::time::Duration::from_secs(10))
121+
.build(),
103122
}
104123
}
105124

125+
/// Drop the cached enabled-workflow list for a channel.
126+
///
127+
/// Must be called after any write to a workflow's trigger eligibility or
128+
/// channel binding (currently the relay's command upsert and NIP-09
129+
/// deletion paths) so same-pod trigger matching sees the change
130+
/// immediately instead of after the cache TTL.
131+
pub fn invalidate_channel_workflows(&self, community_id: CommunityId, channel_id: Uuid) {
132+
self.workflow_cache.invalidate(&(community_id, channel_id));
133+
}
134+
106135
/// Set the action sink. Called once after `AppState` construction.
107136
///
108137
/// # Panics
@@ -265,11 +294,20 @@ impl WorkflowEngine {
265294
return Ok(());
266295
}
267296

268-
let workflows = self
269-
.db
270-
.list_enabled_channel_workflows(community_id, channel_id)
271-
.await
272-
.map_err(WorkflowError::from)?;
297+
let cache_key = (community_id, channel_id);
298+
let workflows = match self.workflow_cache.get(&cache_key) {
299+
Some(cached) => cached,
300+
None => {
301+
let fresh = Arc::new(
302+
self.db
303+
.list_enabled_channel_workflows(community_id, channel_id)
304+
.await
305+
.map_err(WorkflowError::from)?,
306+
);
307+
self.workflow_cache.insert(cache_key, Arc::clone(&fresh));
308+
fresh
309+
}
310+
};
273311

274312
if workflows.is_empty() {
275313
return Ok(());
@@ -285,7 +323,7 @@ impl WorkflowEngine {
285323
}
286324
};
287325

288-
for workflow in &workflows {
326+
for workflow in workflows.iter() {
289327
let def: WorkflowDef = match serde_json::from_value(workflow.definition.clone()) {
290328
Ok(d) => d,
291329
Err(e) => {

0 commit comments

Comments
 (0)