New to Kestra?
Use blueprints to kickstart your first workflows.
Find Couchbase N1QL requests over an elapsed-time threshold in system:active_requests and cancel each by requestId behind a dry-run gate.
One runaway N1QL request can starve the whole Couchbase query service. An unindexed full-keyspace
scan or a cartesian join pins a query node on CPU and memory. Every other query on that node
slows down or stalls behind it. Couchbase has no circuit breaker for this, so the request runs until
it finishes, times out or someone cancels it by hand. This blueprint reads system:active_requests,
finds requests whose elapsed time is over a threshold, cancels each one by requestId behind a safe
gate, then re-reads system:active_requests and fails the run if anything is still over the
threshold.
This blueprint was created by zkasuran.
Cancelling a request is disruptive to whoever launched it. The request stops where it is and its partial work is discarded, so a half-finished write-shaped statement leaves nothing behind and a read returns nothing. The controls are in the flow, not only in this text.
dry_run defaults to true and auto_remediate to false, so a plain run
only reports. A cancel runs only when auto_remediate is true, dry_run is false and a human
answers the Pause with approved: true. An unanswered resume reads as not approved.system:active_requests, which is exactly what the detect query, the cancel statements and the
re-read all do, so none of them can ever appear in the target set. A monitoring query against the
same keyspace is left alone for the same reason.elapsed_threshold_seconds to match the slowest query the workload runs on purpose.concurrencyLimit: 1, so cancels are serialised rather than racing.allowFailure, so a cancel of a request that already left system:active_requests does
not abort the batch, so the re-read is the single source of truth on whether the service is clear.detect (io.kestra.plugin.couchbase.Query) runs SELECT ... FROM system:active_requests WHERE STR_TO_DURATION(elapsedTime) > <threshold> AND statement NOT LIKE '%active_requests%' with fetchType: FETCH, so every long-running request that is not a monitoring query comes back in outputs.detect.rows.plan (io.kestra.plugin.core.output.OutputValues) turns the rows into the count, the list of requestId values to cancel and a label for the alert.report (io.kestra.plugin.core.log.Log) logs the count and the full detail.on_findings (io.kestra.plugin.core.flow.If) posts a Slack findings alert through io.kestra.plugin.slack.notifications.SlackIncomingWebhook when the count is above zero. This fires on a dry run too.remediate (io.kestra.plugin.core.flow.If) runs only when there is a runaway and auto_remediate is true and dry_run is false. approval_hold (io.kestra.plugin.core.flow.Pause) waits for a human. On approval cancel_runaway (io.kestra.plugin.core.flow.Loop, concurrencyLimit: 1) issues one DELETE FROM system:active_requests WHERE requestId = <id> per request, the documented way to cancel a running N1QL request.reverify (io.kestra.plugin.core.flow.LoopUntil) re-reads system:active_requests on an interval, bounded by poll_max_duration. A cancel is not always instant, so the loop ends when no request is over the threshold and fails the run if the window closes first. confirm_cancelled logs the success. On a declined approval the flow logs it and cancels nothing. The errors block posts a Slack alert on any failure: a bad credential, an unreachable cluster or a request still over the threshold after the cancel.DELETE FROM system:active_requests, gated on a dry run, an explicit flag and a human approval.Couchbase does not stop a request for using too much time. It runs until it finishes or hits its own
timeout. Until then it holds CPU and memory on the query node. Finding it needs a read of
system:active_requests. Stopping it needs a cancel that must never hit the wrong request. Kestra
runs the read on a schedule, keeps the cancel behind an approval, re-reads the cluster to confirm the
service is clear, then routes findings and failures to Slack. Every run is an execution you can replay
with the exact requests it acted on.
Verified on Kestra 2.0.5 against a real Couchbase cluster (couchbase/server:community) with the
data, index and query services enabled, confirmed before and after with direct N1QL queries against
system:active_requests.
detect found 0, no alert fired, nothing was cancelled.dry_run=true: detect found exactly that one request, a findings alert posted, nothing was cancelled. A direct N1QL read confirmed the request still running afterward.elapsed_threshold_seconds=0 and no runaway, every active request qualified on time yet detect returned 0, because the only active request was the detector itself and it is excluded by the statement filter.dry_run=true with auto_remediate=true: still no cancel. The dry-run gate held.dry_run=false with auto_remediate=true, approved: the request was cancelled, the re-read returned 0 and a direct N1QL read confirmed the request was gone. The before read showed the real requestId and a climbing elapsedTime.Pause was still over the threshold after the approved cancel of the first, the re-read saw it and the run failed instead of reporting success.detect and the errors block posted a Slack alert.Couchbase needs the query and index services enabled, otherwise system:active_requests is not
reachable through N1QL. The user needs privileges to read system:active_requests and to cancel
requests, which the full administrator role grants.
COUCHBASE_PASSWORD: password for the Couchbase user. It is the default for the password input.SLACK_WEBHOOK_URL: incoming webhook used by the findings alert and the error alert.connection_string: Couchbase connection string, for example couchbase://host1,host2.username: Couchbase user allowed to read system:active_requests and cancel requests.password: user password, defaulting to the COUCHBASE_PASSWORD secret.elapsed_threshold_seconds: a request is a target when its elapsed time is over this many seconds. Defaults to 300.dry_run: report only when true. Defaults to true.auto_remediate: cancel the requests when true and dry_run is false. Defaults to false.poll_max_duration: upper bound on the re-read poll. Defaults to PT30S.COUCHBASE_PASSWORD and SLACK_WEBHOOK_URL secrets in Kestra.dry_run true to list the requests over the threshold.dry_run false and auto_remediate true, then answer the Pause with approved: true.elapsed_threshold_seconds on a workload with a tight query budget. Raise it where long analytical queries are normal.node to the alert and group by it to see which query node is under pressure.requestId values seen over the threshold so a request is only cancelled after it has been slow across two runs.userAgent or clientContextID by adding the column to the detect filter.