Schedule icon
Query icon
If icon
Switch icon
Log icon
SlackIncomingWebhook icon

Trino Stuck Query and Node Health Gate

Classify Trino cluster health from system.runtime.nodes/queries, alert on risk, and optionally kill the oldest stuck query.

Categories
DataInfrastructure

A worker that silently drops out of 'active' state, or a query stuck in RUNNING for an hour because of a bad join plan or a dead downstream connector, both degrade a Trino cluster the same way: slowly, until someone notices from a downstream timeout. This blueprint reads Trino's own system connector tables - system.runtime.nodes for worker state and system.runtime.queries for in-flight queries - classifies the cluster into a three-state risk level, alerts on anything but a clean bill of health, and can kill a single stuck query with Trino's own system.runtime.kill_query() procedure, behind two independent gates.

How it works

  1. periodic_health_check (io.kestra.plugin.core.trigger.Schedule) runs every 10 minutes, always forcing auto_remediate: "false" and dry_run: "true" through its own inputs: override regardless of this flow's defaults. Shipped disabled so you can validate a manual run first.
  2. check_node_health (io.kestra.plugin.jdbc.trino.Query, fetchType: FETCH_ONE) counts system.runtime.nodes rows by state = 'active' versus not, populating row.total_nodes, row.active_nodes, row.non_active_nodes.
  3. check_long_running_queries (io.kestra.plugin.jdbc.trino.Query, fetchType: FETCH_ONE) counts system.runtime.queries rows in state RUNNING with elapsed time (date_diff('second', started, now())) past long_running_threshold_seconds, and picks the oldest one's query_id through a scalar subquery in the same statement, populating row.stuck_count, row.max_elapsed_seconds, row.oldest_stuck_query_id.
  4. evaluate_cluster_visibility (io.kestra.plugin.core.flow.If) branches on total_nodes > 0.
    • else (UNREACHABLE): alert_cluster_unreachable reports that no nodes are visible at all.
    • then (nodes visible): nested evaluate_health_risk (io.kestra.plugin.core.flow.If) branches on non_active_nodes > 0 or active_nodes < min_active_nodes or stuck_count > 0.
      • else (HEALTHY): log_cluster_healthy records a clean check.
      • then (AT_RISK): route_remediation (io.kestra.plugin.core.flow.Switch on auto_remediate) either considers remediation ("true", further gated by check_has_stuck_query - only a stuck query can be killed, not a non-active worker - and then by dry_run through check_dry_run) or only logs that remediation is disabled ("false"). When every gate allows it, kill_stuck_query calls system.runtime.kill_query() on oldest_stuck_query_id only. alert_cluster_at_risk always fires to Slack regardless of which remediation path ran.
  5. log_audit_complete always runs last, printing the headline numbers regardless of which branch fired.
  6. The errors block alerts Slack separately if the flow itself fails - an unreachable coordinator or bad credentials must not read as "no risk" or "healthy".

What you get

  • A direct, three-state answer (HEALTHY / AT_RISK / UNREACHABLE) sourced from Trino's own system connector tables, not inferred from an external monitoring agent.
  • A distinct UNREACHABLE state so a coordinator that answers but reports zero nodes is never conflated with a healthy or merely degraded cluster.
  • Remediation scoped to exactly one query, by query_id, picked deterministically (the oldest stuck query) - it never kills an arbitrary or unrelated query.
  • Two independent gates (auto_remediate, dry_run) between "stuck query found" and "query actually killed".
  • A final status log every run, risk or not, so the execution history doubles as a cluster-health trend.

Who it's for

  • Platform teams running a self-managed Trino cluster who want an early signal when a worker silently stops being active or a query hangs past a normal ceiling.
  • On-call engineers who want one Slack alert with the exact node and query numbers instead of digging through the Trino Web UI during an incident.
  • Anyone evaluating the Trino plugin who wants a worked example of Query driving both system-table introspection and a system-connector procedure call (kill_query), not just catalog data reads.

Why orchestrate this with Kestra

system.runtime.nodes and system.runtime.queries run by hand answer one moment in time; they do not decide a cadence, classify the result into a three-state risk level, pick a single deterministic remediation target, gate a kill behind two independent confirmations, or notify anyone. Kestra supplies the schedule, the HEALTHY/AT_RISK/UNREACHABLE classification as a first-class branch, two independent authorization gates before any query is killed, and an execution history that shows exactly when the cluster degraded and whether anything was done about it.

Prerequisites

  • A reachable Trino coordinator (Trino's system connector, including runtime.nodes, runtime.queries, and the runtime.kill_query() procedure, is built in and requires no extra connector configuration).
  • A user with permission to view and kill other users' queries if auto_remediate will ever be set to "true" against queries submitted by other users.
  • A Slack incoming webhook for alerts.

Local testing: docker run -d --name trino-gate -p 8081:8080 trinodb/trino docker network connect YOUR_KESTRA_NETWORK trino-gate (only needed if the Kestra Worker runs in a separate Docker network than this container; set TRINO_URL to jdbc:trino://trino-gate:8080 from inside that network, or jdbc:trino://localhost:8081 from the host). Host port 8081 is used here specifically so it never collides with Kestra's own UI on 8080.

Secrets

  • TRINO_URL: JDBC URL, e.g. jdbc:trino://host:8081 (use TLS on a production port; password authentication requires TLS).
  • TRINO_USERNAME / TRINO_PASSWORD: Trino credentials used by every io.kestra.plugin.jdbc.trino.Query task in this flow.
  • SLACK_WEBHOOK_URL: Slack incoming webhook used by alert_cluster_at_risk, alert_cluster_unreachable, and the errors block.
  • In Kestra OSS (no Enterprise secrets backend), secrets are supplied as environment variables prefixed SECRET_, base64-encoded, and read back in flows with {{ secret('NAME') }} - for example SECRET_TRINO_PASSWORD=$(echo -n '<your-local-password>' | base64). This keeps credentials out of the flow YAML but, per Kestra's own documentation, offers no encryption at rest or access control beyond the host environment; use the Enterprise secrets backend for stronger guarantees.

Inputs

  • min_active_nodes (INT, default 1): active-node floor before non-active nodes alone count as AT_RISK.
  • long_running_threshold_seconds (INT, default 300): RUNNING-query elapsed-time ceiling before a query counts as stuck.
  • auto_remediate (SELECT: "false", "true"; default "false"): must be "true" for remediation to even be considered.
  • dry_run (SELECT: "true", "false"; default "true"): must be explicitly "false", together with auto_remediate: "true", for kill_query() to actually run.

Outputs

  • outputs.check_node_health.row.total_nodes / .active_nodes / .non_active_nodes: node counts for every run.
  • outputs.check_long_running_queries.row.stuck_count / .max_elapsed_seconds / .oldest_stuck_query_id: stuck-query metrics for every run; oldest_stuck_query_id is absent when stuck_count is 0.

Quick start

  1. Start Trino locally with the command above and run a deliberately slow query against it (e.g. a cross join over a large tpch table) from another session to put a query into RUNNING for longer than long_running_threshold_seconds.
  2. Add the TRINO_URL, TRINO_USERNAME, TRINO_PASSWORD, and SLACK_WEBHOOK_URL secrets.
  3. Run the flow manually with the defaults and confirm alert_cluster_at_risk fires once the slow query crosses the threshold.
  4. Run once with auto_remediate: "true" and dry_run: "true" and confirm log_dry_run_remediation describes the kill without running it.
  5. Run once with auto_remediate: "true" and dry_run: "false" and confirm kill_stuck_query runs, then verify in the Trino Web UI that the query now shows FAILED with the message set by this flow.
  6. Enable periodic_health_check once you trust the check; it always runs in the safe auto_remediate: "false" / dry_run: "true" mode regardless of what you leave the flow's own defaults set to.

How to extend

  • Add a second threshold on queued_time_ms or analysis_time_ms from system.runtime.queries to catch queries stuck in planning, not just RUNNING.
  • Join in coordinator from system.runtime.nodes to alert with higher severity when the coordinator itself (not a worker) is the non-active node.
  • Loop over several coordinators with io.kestra.plugin.core.flow.Loop if more than one Trino cluster needs the same gate.
  • Replace the Slack-only alert with a page (PagerDuty, Opsgenie) specifically for the UNREACHABLE branch, since zero visible nodes is a more urgent signal than a single stuck query.

Pitfalls

  • UNREACHABLE here means "zero nodes reported", not "query failed to run". A coordinator that is actually down or unreachable over the network fails the Query task itself and is caught by the errors block, not by evaluate_cluster_visibility - that branch only fires when the coordinator answers but system.runtime.nodes is empty.
  • There is no structured "list nodes" or "kill query" task in this plugin. Verified from this repo's own existing Trino flows and Trino's system connector documentation: schema introspection (system.runtime.nodes, system.runtime.queries) and the procedure call (CALL system.runtime.kill_query(...)) both go through the same generic io.kestra.plugin.jdbc.trino.Query task as ordinary SQL text.
  • kill_query() only targets the single oldest_stuck_query_id picked by the scalar subquery. It never iterates or kills every stuck query in one run; a cluster with several stuck queries needs several runs (or a loop extension) to clear them one at a time.
  • auto_remediate and dry_run are independent gates, not a single boolean. Both must be "true"/"false" respectively for kill_query() to run; setting only one leaves the other still blocking.
  • Switch case keys "true"/"false" are quoted strings. auto_remediate renders as the literal string "true" or "false"; unquoted true:/false: YAML map keys would parse as booleans instead and would not match.
  • This blueprint is UNTESTED against a live Trino cluster. The system.runtime.nodes and system.runtime.queries column names are taken verbatim from NodeSystemTable.java and QuerySystemTable.java in the trinodb/trino source, and kill_query() is taken verbatim from Trino's own system connector documentation - but no Docker container or Kestra engine was run to execute this flow end to end.

Links

See How

New to Kestra?

Use blueprints to kickstart your first workflows.