id: scylladb-unused-secondary-index-drop-guard
namespace: company.team
description: >-
Find secondary indexes in the keyspaces and optional allow-list you name whose
backing tables have had zero reads in both ScyllaDB's 15-minute and 120-minute
sstable activity windows. The flow records the complete system_schema.indexes
kind and options map, protects the names you exclude, and never treats a
missing activity row as evidence of a read. A dry run only reports the write
tax. With auto_remediate true and dry_run false, a Pause approves the exact
plan, the catalog and counters are read again and any target whose definition
disappeared or whose read rate changed fails before a DROP INDEX. Surviving
indexes fail verification. The flow proves index removal; it does not claim a
latency improvement that CQL cannot measure.
inputs:
- id: scylla_host
type: STRING
displayName: ScyllaDB host
description: One ScyllaDB contact point hostname reachable from the Kestra Worker.
defaults: "localhost"
- id: scylla_port
type: INT
displayName: ScyllaDB CQL port
description: Native CQL port on the contact point.
defaults: 9042
- id: local_datacenter
type: STRING
displayName: Local datacenter
description: Driver local datacenter for ScyllaDB load balancing.
defaults: "datacenter1"
- id: target_keyspaces
type: ARRAY
itemType: STRING
displayName: Keyspaces in scope
description: User keyspaces to inspect. Do not name system keyspaces.
defaults:
- app
- id: target_indexes
type: ARRAY
itemType: STRING
displayName: Index allow-list
description: Fully qualified keyspace.index names. Empty means every index in
target_keyspaces except protected_indexes.
defaults: []
- id: protected_indexes
type: ARRAY
itemType: STRING
displayName: Protected indexes
description: Fully qualified keyspace.index names that must be reported but
never dropped. Use this for a business-critical lookalike or a planned
low-traffic index.
defaults: []
- id: auto_remediate
type: BOOL
displayName: Auto remediate
description: Allow DROP INDEX after the approval and stale-plan re-read gates.
defaults: false
- id: dry_run
type: BOOL
displayName: Dry run
description: Must be false together with auto_remediate true before any DROP
INDEX is sent.
defaults: true
variables:
keyspace_filter: >-
{{ inputs.target_keyspaces | jq('if length == 0 then "\u0027\u0027" else
map("\u0027" + gsub("\u0027"; "\u0027\u0027") + "\u0027") | join(",") end')
| first }}
index_cql: >-
SELECT keyspace_name, table_name, index_name, kind, options FROM
system_schema.indexes WHERE keyspace_name IN ({{
render(vars.keyspace_filter) }}) ALLOW FILTERING
activity_cql: >-
SELECT keyspace_name, columnfamily_name, generation, rate_15m, rate_120m
FROM system.sstable_activity WHERE keyspace_name IN ({{
render(vars.keyspace_filter) }}) ALLOW FILTERING
audit_jq: >-
.indexes as $indexes | .activity as $activity | (.wanted // []) as $wanted |
(.protected // []) as $protected | [ $indexes[] | . as $i |
(($i.keyspace_name // "") + "." + ($i.index_name // "")) as $id
| select(($wanted | length) == 0 or (($wanted | index($id)) != null))
| ([ $activity[] | select((.keyspace_name // "") == ($i.keyspace_name // "")
and (((.columnfamily_name // "") == (($i.table_name // "") + "_" + ($i.index_name // "") + "_index"))
or ((.columnfamily_name // "") == (($i.index_name // "") + "_index"))
or ((.columnfamily_name // "") == ($i.index_name // "")))) ]) as $a
| ([ $a[] | ((.rate_15m // 0) | tonumber) ] | add // 0) as $r15
| ([ $a[] | ((.rate_120m // 0) | tonumber) ] | add // 0) as $r120
| {keyspace_name: $i.keyspace_name, table_name: $i.table_name, index_name: $i.index_name,
id: $id, kind: $i.kind, options: ($i.options // {}), activity_rows: ($a | length),
rate_15m: $r15, rate_120m: $r120,
protected: (($protected | index($id)) != null),
verdict: (if (($protected | index($id)) != null) then "PROTECTED"
elif $r15 == 0 and $r120 == 0 then "UNUSED"
else "IN_USE" end),
drop_cql: ("DROP INDEX \"" + (($i.keyspace_name // "") | gsub("\""; "\"\"")) + "\".\"" + (($i.index_name // "") | gsub("\""; "\"\"")) + "\""),
recreate_cql: ("CREATE INDEX \"" + (($i.index_name // "") | gsub("\""; "\"\"")) + "\" ON \"" + (($i.keyspace_name // "") | gsub("\""; "\"\"")) + "\".\"" + (($i.table_name // "") | gsub("\""; "\"\"")) + "\" (" + (($i.options.target // "") | tostring) + ")")}
]
tasks:
- id: catalog_and_activity
type: io.kestra.plugin.scylladb.Queries
description: Read the ScyllaDB index catalog and its own 15-minute and
120-minute backing-table activity windows in one CQL session.
connection:
contactPoints:
- "{{ inputs.scylla_host }}:{{ inputs.scylla_port }}"
localDatacenter: "{{ inputs.local_datacenter }}"
username: "{{ secret('SCYLLADB_USERNAME') }}"
password: "{{ secret('SCYLLADB_PASSWORD') }}"
cql:
- "{{ render(vars.index_cql) }}"
- "{{ render(vars.activity_cql) }}"
fetchType: FETCH
- id: assess
type: io.kestra.plugin.core.output.OutputValues
description: Classify every allow-listed index from real activity rows,
retaining protected and in-use controls beside unused candidates.
values:
report: >-
{{ {'indexes': outputs.catalog_and_activity.outputs[0].rows, 'activity':
outputs.catalog_and_activity.outputs[1].rows,
'wanted': inputs.target_indexes, 'protected': inputs.protected_indexes}
| toJson | jq(render(vars.audit_jq)) | first | toJson }}
plan: >-
{{ {'indexes': outputs.catalog_and_activity.outputs[0].rows, 'activity':
outputs.catalog_and_activity.outputs[1].rows,
'wanted': inputs.target_indexes, 'protected': inputs.protected_indexes}
| toJson | jq(render(vars.audit_jq) ~ ' | map(select(.verdict == "UNUSED"))') | first | toJson }}
spared: >-
{{ {'indexes': outputs.catalog_and_activity.outputs[0].rows, 'activity':
outputs.catalog_and_activity.outputs[1].rows,
'wanted': inputs.target_indexes, 'protected': inputs.protected_indexes}
| toJson | jq(render(vars.audit_jq) ~ ' | map(select(.verdict != "UNUSED"))') | first | toJson }}
- id: summary
type: io.kestra.plugin.core.log.Log
description: Log exact rates, definitions and the planned reversible definition
before any write gate.
message: >-
ScyllaDB index audit read {{ fromJson(outputs.assess.values.report) |
length }} index(es). Unused={{ fromJson(outputs.assess.values.plan) |
length }}. Spared={{ fromJson(outputs.assess.values.spared) | length }}.
{% for r in fromJson(outputs.assess.values.report) %}{{ r.id }} verdict={{
r.verdict }} rate15m={{ r.rate_15m }} rate120m={{ r.rate_120m }}
activity_rows={{ r.activity_rows }} kind={{ r.kind }} options={{ r.options
| toJson }}; {% endfor %}
- id: route
type: io.kestra.plugin.core.flow.If
condition: "{{ (fromJson(outputs.assess.values.plan) | length) > 0 }}"
then:
- id: alert
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Alert when candidates exist while preserving a read-only default.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{{ {'text': 'ScyllaDB unused-index audit: ' ~ (fromJson(outputs.assess.values.plan) | length) ~ ' candidate(s) with zero reads in both 15m and 120m activity windows. ' ~ (fromJson(outputs.assess.values.plan) | jq('[.[] | .id + " (15m=" + (.rate_15m|tostring) + ", 120m=" + (.rate_120m|tostring) + ")"] | join("; ")') | first) ~ '. Protected and in-use indexes were spared. ' ~ (inputs.auto_remediate and not inputs.dry_run ? 'Approval is required before a re-read and drop.' : 'Read-only: no change applied.')} | toJson }}
- id: remediate_gate
type: io.kestra.plugin.core.flow.If
condition: "{{ inputs.auto_remediate and not inputs.dry_run }}"
then:
- id: approval
type: io.kestra.plugin.core.flow.Pause
description: Approve the exact index names and captured definitions shown in the
preceding audit. The plan is stale until the catalog is read
again.
pauseDuration: PT2H
behavior: FAIL
onResume:
- id: approved
type: BOOL
displayName: Approve these index drops
defaults: false
- id: approval_gate
type: io.kestra.plugin.core.flow.If
description: Continue only when the human explicitly approves the exact captured
plan.
condition: "{{ outputs.approval.onResume.approved ?? false }}"
then:
- id: recheck
type: io.kestra.plugin.scylladb.Queries
description: Re-read the catalog and backing-table rates immediately after
approval. No approved target is written from the old snapshot.
connection:
contactPoints:
- "{{ inputs.scylla_host }}:{{ inputs.scylla_port }}"
localDatacenter: "{{ inputs.local_datacenter }}"
username: "{{ secret('SCYLLADB_USERNAME') }}"
password: "{{ secret('SCYLLADB_PASSWORD') }}"
cql:
- "{{ render(vars.index_cql) }}"
- "{{ render(vars.activity_cql) }}"
fetchType: FETCH
- id: recheck_plan
type: io.kestra.plugin.core.output.OutputValues
description: Compare approved stable names with fresh rates and refuse every
target that disappeared or became active.
values:
result: >-
{{ {'initial': fromJson(outputs.assess.values.plan),
'indexes': outputs.recheck.outputs[0].rows,
'activity': outputs.recheck.outputs[1].rows, 'wanted': inputs.target_indexes, 'protected': inputs.protected_indexes}
| toJson | jq(' . as $x | ($x.initial | map(.id)) as $ids | ($x.initial | map({key: .id, value: .}) | from_entries) as $before | ((' ~ render(vars.audit_jq) ~ ') | map(select(.id as $id | ($ids | index($id)) != null))) as $now | {now: $now, stale: [$ids[] as $id | ($now | map(select(.id == $id)) | .[0]) as $r | select($r == null or $r.verdict != "UNUSED" or $r.table_name != $before[$id].table_name or $r.kind != $before[$id].kind or $r.options != $before[$id].options) | {id: $id, current: ($r // {verdict: "MISSING"})}], drops: ($now | map(.drop_cql)), drop_ids: ($now | map(.id))}') | first | toJson }}
- id: stale_gate
type: io.kestra.plugin.core.flow.If
description: Stop before DROP INDEX if any approved index changed, became read
or vanished while approval was pending.
condition: "{{ (fromJson(outputs.recheck_plan.values.result).stale | length) > 0
}}"
then:
- id: stale_failed
type: io.kestra.plugin.core.execution.Fail
errorMessage: >-
Refusing to drop indexes because the approved snapshot is
stale: {{
fromJson(outputs.recheck_plan.values.result).stale |
toJson }}. No schema write was sent; run a fresh audit.
else:
- id: drop
type: io.kestra.plugin.core.flow.Loop
description: Drop one approved index at a time so schema agreement is not raced.
The captured options map remains in execution outputs for
recreation.
concurrencyLimit: 1
values: "{{ fromJson(outputs.recheck_plan.values.result).drops | toJson }}"
tasks:
- id: drop_one
type: io.kestra.plugin.scylladb.Execute
allowFailure: true
connection:
contactPoints:
- "{{ inputs.scylla_host }}:{{ inputs.scylla_port }}"
localDatacenter: "{{ inputs.local_datacenter }}"
username: "{{ secret('SCYLLADB_USERNAME') }}"
password: "{{ secret('SCYLLADB_PASSWORD') }}"
cql: "{{ item.value }}"
- id: settle
type: io.kestra.plugin.core.flow.Sleep
description: Allow the schema change to reach the contact point before
verification.
duration: PT5S
- id: verify_catalog
type: io.kestra.plugin.scylladb.Query
description: Verify from system_schema.indexes that every approved index is
gone.
connection:
contactPoints:
- "{{ inputs.scylla_host }}:{{ inputs.scylla_port }}"
localDatacenter: "{{ inputs.local_datacenter }}"
username: "{{ secret('SCYLLADB_USERNAME') }}"
password: "{{ secret('SCYLLADB_PASSWORD') }}"
cql: "{{ render(vars.index_cql) }}"
fetchType: FETCH
- id: verification
type: io.kestra.plugin.core.output.OutputValues
description: Compare stable approved names with the final catalog rows.
values:
result: >-
{{ {'expected':
fromJson(outputs.recheck_plan.values.result).drop_ids,
'actual': outputs.verify_catalog.rows}
| toJson | jq('{expected: .expected, survivors: [.expected[] as $id | .actual[] | select(((.keyspace_name // "") + "." + (.index_name // "")) == $id) | $id]}') | first | toJson }}
- id: verify_result
type: io.kestra.plugin.core.flow.If
condition: "{{ (fromJson(outputs.verification.values.result).survivors | length)
> 0 }}"
then:
- id: verify_failed
type: io.kestra.plugin.core.execution.Fail
errorMessage: Approved index definitions still appear in system_schema.indexes
after DROP INDEX. Check schema agreement and
privileges before retrying.
else:
- id: verified
type: io.kestra.plugin.core.log.Log
message: >-
Verified {{
fromJson(outputs.recheck_plan.values.result).drops |
length }} approved index drop(s) absent from
system_schema.indexes. Captured definitions remain in
the audit output for recreation.
else:
- id: declined
type: io.kestra.plugin.core.log.Log
message: Approval declined. No DROP INDEX statement was sent.
else:
- id: clean
type: io.kestra.plugin.core.log.Log
message: No unprotected target index had zero reads in both ScyllaDB activity
windows. Protected and in-use controls were spared.
errors:
- id: alert_flow_failure
type: io.kestra.plugin.slack.notifications.SlackIncomingWebhook
description: Alert when the audit or guarded schema change fails so an unknown
posture is not reported as clean.
url: "{{ secret('SLACK_WEBHOOK_URL') }}"
payload: |
{{ {'text': 'ScyllaDB unused-index guard failed (execution ' ~ execution.id ~ '). Index posture is unknown; inspect the failed task and re-run a read-only audit.'} | toJson }}
triggers:
- id: daily
type: io.kestra.plugin.core.trigger.Schedule
description: Disabled by default and forced to read-only safe mode.
cron: "25 4 * * *"
disabled: true
inputs:
auto_remediate: false
dry_run: true