id: dremio-reflection-job-health-gate
namespace: company.team
description: |
Query Dremio's sys.reflections and sys.jobs system tables on a schedule,
flag any Reflection that failed to refresh and any query job stuck in
RUNNING past a runtime threshold, classify the result as CLEAN,
AT_RISK, or UNREACHABLE, alert Slack, and optionally cancel the stuck
jobs through Dremio's REST API, then re-query to verify none remain
RUNNING.
inputs:
- id: dremio_jdbc_url
type: STRING
displayName: Dremio JDBC URL
description: JDBC URL of a self-hosted Dremio OSS coordinator, format
jdbc:dremio:direct=host:31010.
defaults: "jdbc:dremio:direct=dremio:31010"
- id: dremio_username
type: STRING
displayName: Dremio username
defaults: "dremio"
- id: dremio_rest_base_url
type: STRING
displayName: Dremio REST API base URL
description: Base URL of the Dremio web/REST port, used only to cancel a stuck job.
defaults: "http://dremio:9047"
- id: reflection_failure_threshold
type: INT
displayName: Reflection failure threshold
description: A Reflection with num_failures at or above this value is flagged,
in addition to any Reflection already in a FAILED or
CAN_ACCELERATE_WITH_FAILURES state.
defaults: 1
- id: stuck_job_after_minutes
type: INT
displayName: Stuck job threshold in minutes
description: A job still in RUNNING status this many minutes after it was
submitted is flagged as stuck.
defaults: 30
- id: auto_cancel_stuck_jobs
type: BOOL
displayName: Auto cancel stuck jobs
description: Allow the REST cancel call to run. Failed Reflections are always
report-only, they are never cancelled automatically.
defaults: false
- id: dry_run
type: BOOL
displayName: Dry run
description: Must be false, together with auto_cancel_stuck_jobs, for a job
cancel to apply.
defaults: true
tasks:
- id: probe_reflections
type: io.kestra.plugin.jdbc.dremio.Query
description: Read-only. Reflections already failed or failing to refresh.
url: "{{ inputs.dremio_jdbc_url }}"
username: "{{ inputs.dremio_username }}"
password: "{{ secret('DREMIO_PASSWORD') }}"
sql: >-
SELECT reflection_id, reflection_name, dataset_name, status, num_failures,
last_failure_message FROM sys.reflections WHERE status IN ('FAILED',
'CAN_ACCELERATE_WITH_FAILURES')
OR num_failures >= {{ inputs.reflection_failure_threshold }}
fetchType: FETCH
allowFailure: true
- id: probe_stuck_jobs
type: io.kestra.plugin.jdbc.dremio.Query
description: Read-only. Every job currently in RUNNING status; the runtime
threshold is applied afterward in jq, not in SQL.
url: "{{ inputs.dremio_jdbc_url }}"
username: "{{ inputs.dremio_username }}"
password: "{{ secret('DREMIO_PASSWORD') }}"
sql: >-
SELECT job_id, user_name, queue_name, query, submitted_epoch_millis FROM
sys.jobs WHERE status = 'RUNNING'
fetchType: FETCH
allowFailure: true
- id: findings
type: io.kestra.plugin.core.output.OutputValues
description: Read-only. Combines failed Reflections with jobs that have been
RUNNING past stuck_job_after_minutes into one findings array.
values:
items: >-
{{ {'reflections': outputs.probe_reflections.rows ?? [], 'jobs':
outputs.probe_stuck_jobs.rows ?? [], 'threshold':
inputs.stuck_job_after_minutes} | jq('(.reflections | map({kind:
"REFLECTION_FAILED", id: .reflection_id, name: .reflection_name,
dataset: .dataset_name, status: .status, detail: (.last_failure_message
// ("num_failures=" + (.num_failures | tostring)))})) as $r | .threshold
as $t | (.jobs | map(. + {minutesRunning: (((now * 1000) -
.submitted_epoch_millis) / 60000 | floor)}) | map(select(.minutesRunning
> $t)) | map({kind: "STUCK_JOB", id: .job_id, name: .user_name, dataset:
(.queue_name // "default"), status: "RUNNING", detail: (.minutesRunning
| tostring) + " minutes running"})) as $j | $r + $j') | first | toJson
}}
- id: classify_status
type: io.kestra.plugin.core.debug.Return
description: UNREACHABLE when either probe has no rows field, AT_RISK when the
findings array is non-empty, otherwise CLEAN.
format: "{{ (outputs.probe_reflections.rows is defined and
outputs.probe_reflections.rows is not null and
outputs.probe_stuck_jobs.rows is defined and outputs.probe_stuck_jobs.rows
is not null) ? (fromJson(outputs.findings.values.items) | length > 0 ?
'AT_RISK' : 'CLEAN') : 'UNREACHABLE' }}"
- id: route_by_status
type: io.kestra.plugin.core.flow.Switch
description: Log a quiet confirmation when clean, page Slack and optionally
cancel stuck jobs when at risk, or page Slack when Dremio could not be
queried.
value: "{{ outputs.classify_status.value }}"
cases:
"CLEAN":
- id: log_clean
type: io.kestra.plugin.core.log.Log
message: "No failed Reflection and no job RUNNING past {{
inputs.stuck_job_after_minutes }} minutes."
"AT_RISK":
- id: alert_at_risk
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"text": "Dremio health gate AT_RISK: {{ fromJson(outputs.findings.values.items) | length }} finding(s): {{ fromJson(outputs.findings.values.items) | jq('map(\"\\(.kind) \\(.name // .id) (\\(.dataset)): \\(.detail)\") | join(\"; \")') | first }}. {{ inputs.auto_cancel_stuck_jobs and not inputs.dry_run ? 'Cancelling stuck jobs now.' : 'No job cancelled (auto_cancel_stuck_jobs=' ~ inputs.auto_cancel_stuck_jobs ~ ', dry_run=' ~ inputs.dry_run ~ ').' }} Failed Reflections are report-only. Execution {{ execution.id }}."
}
- id: cancel_gate
type: io.kestra.plugin.core.flow.If
condition: "{{ inputs.auto_cancel_stuck_jobs and not inputs.dry_run and
fromJson(outputs.findings.values.items) | jq('map(select(.kind ==
\"STUCK_JOB\")) | length') | first > 0 }}"
then:
- id: cancel_loop
type: io.kestra.plugin.core.flow.Loop
description: One REST cancel call per stuck job, one at a time.
concurrencyLimit: 1
values: "{{ fromJson(outputs.findings.values.items) | jq('map(select(.kind ==
\"STUCK_JOB\"))') | first }}"
tasks:
- id: cancel_job
type: io.kestra.plugin.core.http.Request
description: "plugin-jdbc-dremio exposes only Query, Queries, and Trigger, there
is no SQL CANCEL JOB statement in Dremio, so this calls
Dremio's own job-cancel REST endpoint directly."
uri: "{{ inputs.dremio_rest_base_url }}/api/v3/job/{{ item.value.id }}/cancel"
method: POST
headers:
Authorization: "Bearer {{ secret('DREMIO_PAT') }}"
options:
allowFailed: true
allowFailure: true
- id: reread_stuck_jobs
type: io.kestra.plugin.jdbc.dremio.Query
description: Read-only. Re-lists every job still RUNNING after the cancel
requests were sent, to compare against the cancelled job IDs.
url: "{{ inputs.dremio_jdbc_url }}"
username: "{{ inputs.dremio_username }}"
password: "{{ secret('DREMIO_PASSWORD') }}"
sql: >-
SELECT job_id, status FROM sys.jobs WHERE status = 'RUNNING'
fetchType: FETCH
allowFailure: true
- id: verify_cancel
type: io.kestra.plugin.core.flow.If
description: Fails if any job ID that was sent a cancel request is still present
among the still-RUNNING jobs.
condition: "{{ {'cancelled_ids': (fromJson(outputs.findings.values.items) |
jq('map(select(.kind == \"STUCK_JOB\") | .id)') | first),
'still_running_ids': (outputs.reread_stuck_jobs.rows ?? [] |
jq('map(.job_id)') | first)} | jq('(.cancelled_ids -
(.cancelled_ids - .still_running_ids)) | length > 0') | first
}}"
then:
- id: cancel_failed
type: io.kestra.plugin.core.execution.Fail
errorMessage: "Cancel request sent, but at least one job is still RUNNING."
else:
- id: cancelled
type: io.kestra.plugin.core.log.Log
message: "Sent a cancel request for {{ fromJson(outputs.findings.values.items) |
jq('map(select(.kind == \"STUCK_JOB\")) | length') | first
}} stuck job(s) and verified none are still RUNNING."
else:
- id: not_cancelled
type: io.kestra.plugin.core.log.Log
message: "At-risk findings recorded but no job cancelled
(auto_cancel_stuck_jobs={{ inputs.auto_cancel_stuck_jobs }},
dry_run={{ inputs.dry_run }})."
"UNREACHABLE":
- id: alert_unreachable
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"text": "Dremio health gate UNREACHABLE: querying sys.reflections or sys.jobs on {{ inputs.dremio_jdbc_url }} did not return rows (coordinator down, auth failure, or system tables not accessible to this user). Execution {{ execution.id }}."
}
defaults:
- id: log_unclassified
type: io.kestra.plugin.core.log.Log
message: "Completed Dremio health audit with status: {{
outputs.classify_status.value }}"
- id: log_audit_status
type: io.kestra.plugin.core.log.Log
description: Post-audit status line recorded on every run regardless of branch,
for a continuous Reflection and job health trend in the execution history.
message: "Dremio health audit: url={{ inputs.dremio_jdbc_url }},
reflection_failure_threshold={{ inputs.reflection_failure_threshold }},
stuck_job_after_minutes={{ inputs.stuck_job_after_minutes }}, status={{
outputs.classify_status.value }}, execution={{ execution.id }}."
errors:
- id: alert_flow_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Alert Slack when the gate flow itself fails, so a broken audit
never silently stops monitoring Reflection and job health.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{
"text": "Dremio health gate flow {{ flow.id }} FAILED (execution {{ execution.id }}). Reflection and job health status is now unknown; check the task logs and the Dremio coordinator manually."
}
outputs:
- id: status
type: STRING
value: "{{ outputs.classify_status.value }}"
triggers:
- id: dremio_health_gate_schedule
type: io.kestra.plugin.core.trigger.Schedule
description: Audit Reflections and running jobs every 15 minutes, always in safe mode.
cron: "*/15 * * * *"
disabled: true
inputs:
dremio_jdbc_url: "jdbc:dremio:direct=dremio:31010"
dremio_username: "dremio"
dremio_rest_base_url: "http://dremio:9047"
reflection_failure_threshold: 1
stuck_job_after_minutes: 30
auto_cancel_stuck_jobs: false
dry_run: true