id: elasticsearch-ilm-stuck-step-retry-guard
namespace: company.team
description: |
Find Elasticsearch indices stuck on a failing ILM step, surface the step
and the reason Elasticsearch recorded, then re-run that step with
POST /<index>/_ilm/retry behind a dry run plus a human approval. The
run fails if a targeted index is still failing the same step afterwards.
inputs:
- id: elasticsearch_url
type: STRING
defaults: "http://localhost:9200"
description: Base URL of the Elasticsearch cluster, scheme host and port.
- id: elasticsearch_username
type: STRING
defaults: "{{ secret('ELASTICSEARCH_USERNAME') }}"
description: Username for basic auth. Clusters with security disabled ignore it.
- id: elasticsearch_password
type: STRING
defaults: "{{ secret('ELASTICSEARCH_PASSWORD') }}"
description: Password for basic auth. Clusters with security disabled ignore it.
- id: index_pattern
type: STRING
defaults: "*"
description: Index or pattern to inspect. Defaults to every index.
- id: include_system_indices
type: BOOL
defaults: false
description: >-
When true, dot-prefixed and hidden indices are inspected too. Off by
default because those belong to Elasticsearch and its plugins.
- id: max_error_indices
type: INT
defaults: 25
description: >-
The flow refuses to retry anything when more than this many indices sit in
the ERROR step. A large simultaneous count points at one cluster-wide
cause such as a missing snapshot repository. Retrying every index then
re-enters the same failure.
- id: verify_wait
type: DURATION
defaults: PT2M
description: >-
How long to wait after the retry before re-reading ILM. A retried step is
re-run on an ILM poll. A failure takes one more poll to show, so set this
to at least twice indices.lifecycle.poll_interval (10m by default, so
PT20M on a stock cluster) or the re-read is a false green.
- id: dry_run
type: BOOL
defaults: true
description: When true the flow only reports, it never calls the retry endpoint.
- id: auto_remediate
type: BOOL
defaults: false
description: When true and dry_run is false the flow retries the failed step
after approval.
triggers:
- id: daily
type: io.kestra.plugin.core.trigger.Schedule
description: Scan for indices stuck in the ILM ERROR step every morning in safe
report-only mode.
cron: "0 7 * * *"
disabled: true
inputs:
dry_run: true
auto_remediate: false
tasks:
- id: detect_status
type: io.kestra.plugin.elasticsearch.Request
description: Read the ILM operation mode. A retry is pointless while ILM is
stopped, so this is read before anything else.
connection:
hosts:
- "{{ inputs.elasticsearch_url }}"
basicAuth:
username: "{{ inputs.elasticsearch_username }}"
password: "{{ inputs.elasticsearch_password }}"
method: GET
endpoint: "_ilm/status"
- id: detect_explain
type: io.kestra.plugin.elasticsearch.Request
description: >-
Read full ILM state for the matching indices. The whole explain output is
read rather than only_errors, because Elasticsearch takes a retryable
index out of the ERROR step for the whole poll in which it re-runs the
failed step. only_errors drops it for that window.
connection:
hosts:
- "{{ inputs.elasticsearch_url }}"
basicAuth:
username: "{{ inputs.elasticsearch_username }}"
password: "{{ inputs.elasticsearch_password }}"
method: GET
endpoint: "{{ inputs.index_pattern }}/_ilm/explain"
parameters:
expand_wildcards: "open,hidden"
- id: detect_settings
type: io.kestra.plugin.elasticsearch.Request
description: Read the hidden flag for every matching index so system indices can
be filtered out of the target list.
connection:
hosts:
- "{{ inputs.elasticsearch_url }}"
basicAuth:
username: "{{ inputs.elasticsearch_username }}"
password: "{{ inputs.elasticsearch_password }}"
method: GET
endpoint: "{{ inputs.index_pattern }}/_settings/index.hidden"
parameters:
flat_settings: "true"
expand_wildcards: "open,hidden"
- id: check_reads
type: io.kestra.plugin.core.flow.If
description: >-
The Request task reports task success on any HTTP status, so every read is
gated on its status here. A 404 on the pattern would otherwise pass for a
cluster with no ILM errors.
condition: "{{ outputs.detect_status.status != 200 or
outputs.detect_explain.status != 200 or outputs.detect_settings.status !=
200 }}"
then:
- id: fail_reads
type: io.kestra.plugin.core.execution.Fail
description: Fail when ILM could not be read so nothing downstream acts on a
partial picture.
errorMessage: >-
Could not read ILM. status {{ outputs.detect_status.status }}, explain
{{ outputs.detect_explain.status }}, settings {{
outputs.detect_settings.status }}.
- id: plan
type: io.kestra.plugin.core.output.OutputValues
description: >-
Keep every index that is failing an ILM step: either its current step is
ERROR or it still carries a failed_step_retry_count, which is what stays
set while Elasticsearch re-runs the step. An ERROR with no failed step and
no retry count is held separately because a retry cannot fix it.
values:
ilm_mode: "{{ outputs.detect_status.response | jq('.operation_mode') | first }}"
targets_json: >-
{{ {'e': outputs.detect_explain.response, 's':
outputs.detect_settings.response, 'inc': inputs.include_system_indices}
| toJson | jq('. as $r | [($r.e.indices // {}) | to_entries[] |
select(.value.managed == true) | select((((.value.step) // "") ==
"ERROR") or ((.value.failed_step_retry_count) != null)) |
select(((.value.failed_step) != null) or
((.value.failed_step_retry_count) != null)) | {index: .key, policy:
(.value.policy // "none"), phase: (.value.phase // "unknown"), action:
(.value.action // "unknown"), step: (.value.step // "unknown"),
failed_step: ((.value.failed_step) // (.value.step) // "unknown"),
in_error: (((.value.step) // "") == "ERROR"), retries:
(.value.failed_step_retry_count // 0), auto_retryable:
(.value.is_auto_retryable_error // false), type: (.value.step_info.type
// "none"), reason: (.value.step_info.reason // "reason not reported
while Elasticsearch is re-running the step, it reappears on the next
poll"), system: ((.key | startswith(".")) or
((($r.s[.key].settings["index.hidden"]) // "false") == "true"))}] |
map(select($r.inc or (.system == false)))') | first | toJson }}
noretry_json: >-
{{ {'e': outputs.detect_explain.response, 's':
outputs.detect_settings.response, 'inc': inputs.include_system_indices}
| toJson | jq('. as $r | [($r.e.indices // {}) | to_entries[] |
select(.value.managed == true) | select((((.value.step) // "") ==
"ERROR") and (((.value.failed_step) // "") == "") and
((.value.failed_step_retry_count) == null)) | {index: .key, policy:
(.value.policy // "none"), reason: (.value.step_info.reason // "no
reason reported"), system: ((.key | startswith(".")) or
((($r.s[.key].settings["index.hidden"]) // "false") == "true"))}] |
map(select($r.inc or (.system == false)))') | first | toJson }}
inspected: "{{ outputs.detect_explain.response | jq('[(.indices // {}) |
to_entries[] | select(.value.managed == true)] | length') | first }}"
- id: summary
type: io.kestra.plugin.core.output.OutputValues
description: Build one line of per-index detail and one line of distinct causes,
so the operator reads the real reason instead of a count.
values:
names: >-
{{ outputs.plan.values.targets_json | jq('map(.index + " (" + .phase +
"/" + .action + ", failed step " + .failed_step + ", current step " +
.step + ", " + (.retries|tostring) + " auto retries burned,
auto_retryable " + (.auto_retryable|tostring) + ")") | join(" | ")') |
first }}
causes: >-
{{ outputs.plan.values.targets_json | jq('map(.failed_step + ": " +
.type + " " + .reason) | unique | join(" || ")') | first }}
policies: >-
{{ outputs.plan.values.targets_json | jq('map(.index + "=" + .policy) |
join(" | ")') | first }}
noretry_names: >-
{{ outputs.plan.values.noretry_json | jq('map(.index + " (" + .policy +
": " + .reason + ")") | join(" | ")') | first }}
- id: report
type: io.kestra.plugin.core.log.Log
description: Record the ILM mode, the flagged indices, their policies and the
exact causes.
message: |
ILM stuck step scan on pattern {{ inputs.index_pattern }}.
ILM operation mode: {{ outputs.plan.values.ilm_mode }}. ILM managed indices inspected: {{ outputs.plan.values.inspected }}.
Flagged after the system index filter: {{ outputs.plan.values.targets_json | jq('length') | first }}. {{ outputs.summary.values.names }}
In the ERROR step right now of those flagged: {{ outputs.plan.values.targets_json | jq('[.[] | select(.in_error)] | length') | first }}. The rest are mid auto retry, which is the same stuck index read one poll later.
Policies: {{ outputs.summary.values.policies }}
Causes recorded by Elasticsearch: {{ outputs.summary.values.causes }}
In ERROR with no failed step, so a retry cannot help them: {{ outputs.plan.values.noretry_json | jq('length') | first }}. {{ outputs.summary.values.noretry_names }}
Mode dry_run={{ inputs.dry_run }} auto_remediate={{ inputs.auto_remediate }} include_system_indices={{ inputs.include_system_indices }} max_error_indices={{ inputs.max_error_indices }} verify_wait={{ inputs.verify_wait }}.
- id: alert_detected_gate
type: io.kestra.plugin.core.flow.If
description: Alert Slack the moment an index is found in the ERROR step, before
and apart from any remediation decision.
condition: "{{ (outputs.plan.values.targets_json | jq('length') | first) > 0 or
(outputs.plan.values.noretry_json | jq('length') | first) > 0 }}"
then:
- id: alert_detected
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Post the flagged indices and the causes. The payload is built as a
map and serialized, so a reason holding quotes or newlines cannot
break the JSON.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: >-
{{ {'text': 'Elasticsearch ILM stuck step guard: ' ~
(outputs.plan.values.targets_json | jq('length') | first) ~ '
index(es) on pattern ' ~ inputs.index_pattern ~ ' are parked in the
ILM ERROR step, so rollover, forcemerge, shrink and delete have all
stopped for them while the cluster stays green. ' ~
outputs.summary.values.names ~ '. Causes: ' ~
outputs.summary.values.causes ~ '. In ERROR with no failed step, which
a retry cannot fix: ' ~ (outputs.plan.values.noretry_json |
jq('length') | first) ~ '. ' ~ outputs.summary.values.noretry_names ~
'. ILM mode ' ~ outputs.plan.values.ilm_mode ~ ', dry_run ' ~
inputs.dry_run ~ ', auto_remediate ' ~ inputs.auto_remediate ~ '.'} |
toJson }}
- id: route
type: io.kestra.plugin.core.flow.If
description: Act only when an index is in the ERROR step and the operator asked
for a real change.
condition: "{{ (outputs.plan.values.targets_json | jq('length') | first) > 0 and
inputs.auto_remediate and not inputs.dry_run }}"
then:
- id: scope_gate
type: io.kestra.plugin.core.flow.If
description: >-
Retry only while ILM is running and the error is index-level. A
stopped ILM never picks the step back up. More than max_error_indices
at once points at one shared cause, where every retry re-enters the
same failure.
condition: "{{ outputs.plan.values.ilm_mode == 'RUNNING' and
(outputs.plan.values.targets_json | jq('length') | first) <=
inputs.max_error_indices }}"
then:
- id: root_cause_brief
type: io.kestra.plugin.core.log.Log
description: >-
Print what the approver is actually being asked to confirm. ILM
re-runs the step that failed, so this is the list of causes that
must already be fixed.
message: |
Approval brief for pattern {{ inputs.index_pattern }}.
A retry re-runs the step that failed. It does not skip it, change it or work around it. If the cause below is still present the step fails again within one poll interval and this run fails.
Indices: {{ outputs.summary.values.names }}
Policies: {{ outputs.summary.values.policies }}
What Elasticsearch says went wrong: {{ outputs.summary.values.causes }}
Clear the cause first. A bad index.lifecycle.rollover_alias is fixed in the index settings or the template, a read-only block is cleared with index.blocks.read_only_allow_delete, a failed shrink needs the target name free and enough nodes. Then resume with approved true.
- id: approval_gate
type: io.kestra.plugin.core.flow.Pause
description: >-
Hold the run until a human confirms the root cause is cleared.
Approving here is not approving a retry, it is stating that the
condition in the root_cause_brief log no longer applies.
pauseDuration: PT2H
behavior: FAIL
onResume:
- id: approved
type: BOOL
defaults: false
description: >-
Set true only when the cause listed in root_cause_brief has
been cleared. ILM re-runs the same step, so approving with the
cause still present puts the index straight back into ERROR
and fails this run.
- id: apply
type: io.kestra.plugin.core.flow.If
description: Run the retry only on an explicit approval. An unanswered gate
never reads as approval.
condition: "{{ outputs.approval_gate.onResume.approved ?? false }}"
then:
- id: remediate_retry
type: io.kestra.plugin.core.flow.Loop
description: >-
Remediation loop. One POST per flagged index, concurrencyLimit
1 so the retries land in turn and never race on the cluster
state. allowFailure so one rejected retry does not stop the
pass and the re-read stays the real gate.
concurrencyLimit: 1
values: "{{ outputs.plan.values.targets_json }}"
tasks:
- id: retry_one
type: io.kestra.plugin.elasticsearch.Request
description: Ask ILM to re-run the failed step for one index.
allowFailure: true
connection:
hosts:
- "{{ inputs.elasticsearch_url }}"
basicAuth:
username: "{{ inputs.elasticsearch_username }}"
password: "{{ inputs.elasticsearch_password }}"
method: POST
endpoint: "{{ fromJson(item.value).index }}/_ilm/retry"
- id: retry_log
type: io.kestra.plugin.core.log.Log
message: >-
POST {{ fromJson(item.value).index }}/_ilm/retry on failed
step {{ fromJson(item.value).failed_step }} returned HTTP
{{ outputs.retry_one.status }}.
- id: settle
type: io.kestra.plugin.core.flow.Sleep
description: >-
Wait before re-reading. A retried index leaves the ERROR step
immediately and only returns to it when ILM runs the step
again, so an instant re-read reports success even when nothing
was fixed.
duration: "{{ inputs.verify_wait }}"
- id: reverify_explain
type: io.kestra.plugin.elasticsearch.Request
description: Re-read the full ILM state so the verdict comes from the cluster,
covering both halves of the retry cycle.
connection:
hosts:
- "{{ inputs.elasticsearch_url }}"
basicAuth:
username: "{{ inputs.elasticsearch_username }}"
password: "{{ inputs.elasticsearch_password }}"
method: GET
endpoint: "{{ inputs.index_pattern }}/_ilm/explain"
parameters:
expand_wildcards: "open,hidden"
- id: verify_plan
type: io.kestra.plugin.core.output.OutputValues
description: >-
Keep the targeted indices that are still cycling through the
error. It also records where every targeted index sits now. A
single step reading ERROR is not the test, because
Elasticsearch moves an index out of ERROR for the moment it
re-runs a retryable step. The retry counter is what survives
that window, so an index counts as unfixed while it is in
ERROR or still carries failed_step_retry_count.
values:
still_json: >-
{{ {'t': outputs.plan.values.targets_json, 'd':
outputs.reverify_explain.response} | toJson | jq('. as $r |
($r.t | fromjson) as $t | [$t[] | . as $x | (($r.d.indices
// {})[$x.index] // {}) as $a | select(((($a.step) // "") ==
"ERROR") or (($a.failed_step_retry_count) != null)) |
{index: $x.index, step: ($a.step // "gone"), failed_step:
($a.failed_step // "none"), retries:
($a.failed_step_retry_count // -1), type: ($a.step_info.type
// "none"), reason: ($a.step_info.reason // "no reason
reported")}]') | first | toJson }}
still_names: >-
{{ {'t': outputs.plan.values.targets_json, 'd':
outputs.reverify_explain.response} | toJson | jq('. as $r |
($r.t | fromjson) as $t | [$t[] | . as $x | (($r.d.indices
// {})[$x.index] // {}) as $a | select(((($a.step) // "") ==
"ERROR") or (($a.failed_step_retry_count) != null)) |
($x.index + " (step " + (($a.step // "gone")|tostring) + ",
failed step " + (($a.failed_step // "none")|tostring) + ", "
+ (($a.failed_step_retry_count // -1)|tostring) + " retries,
" + (($a.step_info.reason // "no reason reported")|tostring)
+ ")")] | join(" | ")') | first }}
after_steps: >-
{{ {'t': outputs.plan.values.targets_json, 'd':
outputs.reverify_explain.response} | toJson | jq('. as $r |
($r.t | fromjson) as $t | [$t[] | .index as $i |
(($r.d.indices // {})[$i] // {}) | ($i + "=" + ((.phase //
"gone")|tostring) + "/" + ((.action // "none")|tostring) +
"/" + ((.step // "none")|tostring) + " retries " +
((.failed_step_retry_count // 0)|tostring))] | join(" | ")')
| first }}
- id: verify_report
type: io.kestra.plugin.core.log.Log
description: Record the ILM state after the pass so the audit trail carries the
result.
message: |
After the pass on pattern {{ inputs.index_pattern }}.
Targeted indices now: {{ outputs.verify_plan.values.after_steps }}.
ILM managed indices in the re-read: {{ outputs.reverify_explain.response | jq('[(.indices // {}) | to_entries[] | select(.value.managed == true)] | length') | first }}.
Still cycling through the error of the {{ outputs.plan.values.targets_json | jq('length') | first }} targeted: {{ outputs.verify_plan.values.still_json | jq('length') | first }}. {{ outputs.verify_plan.values.still_names }}
- id: verify_result
type: io.kestra.plugin.core.flow.If
description: Pass only when every targeted index left the error behind,
otherwise fail loudly.
condition: "{{ outputs.reverify_explain.status != 200 or
(outputs.verify_plan.values.still_json | jq('length') | first)
> 0 }}"
then:
- id: remediation_failed
type: io.kestra.plugin.core.execution.Fail
description: Fail when an index is still cycling through the error, because that
means the cause was never cleared and the retry only
re-ran the same failing step.
errorMessage: >-
{{ outputs.verify_plan.values.still_json | jq('length') |
first }} of {{ outputs.plan.values.targets_json |
jq('length') | first }} targeted index(es) are still
failing the same ILM step after the retry: {{
outputs.verify_plan.values.still_names }}. Re-read
statuses explain {{ outputs.reverify_explain.status }}. A
retry re-runs the step that failed, so the recorded reason
is still true. Fix that first, then run again.
else:
- id: remediated
type: io.kestra.plugin.core.log.Log
description: Record that every targeted index left the ERROR step and held.
message: >-
Retried and verified. {{ outputs.plan.values.targets_json
| jq('length') | first }} index(es) were in the ILM ERROR
step, 0 of them after the verify_wait settle window.
Targeted indices now {{
outputs.verify_plan.values.after_steps }}.
else:
- id: not_approved
type: io.kestra.plugin.core.log.Log
description: Record that approval was withheld so no retry was sent.
message: >-
Approval not granted, so no retry was sent. {{
outputs.plan.values.targets_json | jq('length') | first }}
index(es) are still in the ILM ERROR step: {{
outputs.summary.values.names }}.
else:
- id: refuse_report
type: io.kestra.plugin.core.log.Log
description: Record that the flow refused to retry, with which of the two
conditions stopped it.
message: >-
Refusing to retry on pattern {{ inputs.index_pattern }}. ILM
operation mode {{ outputs.plan.values.ilm_mode }}, where RUNNING
is required. {{ outputs.plan.values.targets_json | jq('length') |
first }} index(es) failing against max_error_indices {{
inputs.max_error_indices }}. A stopped ILM never re-runs the step.
A large simultaneous count points at one shared cause, where a
retry per index re-enters the same failure.
- id: fail_refused
type: io.kestra.plugin.core.execution.Fail
description: Fail so the alert fires, a retry under these conditions cannot
work.
errorMessage: >-
Refused to retry on pattern {{ inputs.index_pattern }}. ILM mode
{{ outputs.plan.values.ilm_mode }}, {{
outputs.plan.values.targets_json | jq('length') | first }}
index(es) in ERROR, max_error_indices {{ inputs.max_error_indices
}}. Start ILM with POST _ilm/start. For a shared cause, fix that
first then raise max_error_indices once the count is understood.
else:
- id: report_only
type: io.kestra.plugin.core.log.Log
description: Record a no-change outcome, either nothing is in the ERROR step or
the run is in report mode.
message: >-
No retry sent on pattern {{ inputs.index_pattern }}. Flagged {{
outputs.plan.values.targets_json | jq('length') | first }}, dry_run {{
inputs.dry_run }}, auto_remediate {{ inputs.auto_remediate }}.
errors:
- id: alert
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Alert Slack on any failure so a refused pass or an index that fell
back into ERROR is never silent.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: >-
{{ {'text': 'elasticsearch-ilm-stuck-step-retry-guard failed in flow ' ~
flow.id ~ ' (execution ' ~ execution.id ~ ') on pattern ' ~
inputs.index_pattern ~ '. One of these happened: ILM could not be read,
the flow refused to retry because ILM is stopped or too many indices
failed at once, a targeted index fell back into the ERROR step after the
retry. Check the execution logs.'} | toJson }}