Cancel the Redshift statement when a user kills the deferred task#69676
Draft
steveahnahn wants to merge 1 commit into
Draft
Cancel the Redshift statement when a user kills the deferred task#69676steveahnahn wants to merge 1 commit into
steveahnahn wants to merge 1 commit into
Conversation
A deferred RedshiftDataOperator parks its query in the triggerer, so the operator's own on_kill no longer runs once the task is deferred. When a user marks that task failed, clears it, or marks it success, the trigger had no on_kill hook, so the Redshift statement kept running against the cluster or workgroup even though the operator already cancels the statement on kill in the non-deferred path. On Redshift Serverless a runaway statement keeps billing RPU-hours, and on a provisioned cluster it holds a WLM slot, until it finishes on its own. This adds on_kill to RedshiftDataTrigger to cancel the running statement when the user acts on the deferred task, matching the behaviour already shipped for the EMR, Dataproc, BigQuery, and Dataflow triggers. A cancel_on_kill flag on both the operator and the trigger lets users opt out.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Supersedes #69631 (closed during a verification rework; GitHub cannot reopen a PR after its branch is rebased).
Problem
RedshiftDataOperator.on_killcancels the running statement, but once a deferrable run enters the triggerer that method can no longer run: the worker process exits atdefer(). When a user then marks the deferred task failed, clears it, or marks it success,RedshiftDataTriggerhad noon_killhook (theBaseTriggerdefault is a no op), so the statement kept executing against the cluster or workgroup. The operator's documented kill semantics silently stop applying the moment the task defers. On Redshift Serverless the orphaned statement keeps billing RPU hours, and on a provisioned cluster it holds a WLM slot, until it finishes on its own.Sibling deferrable triggers already cancel their remote work on user kill: EMR in this provider, and BigQuery, Dataproc and Dataflow in the Google provider.
Change
RedshiftDataTriggergains anon_killthat cancels the running statement through the hook's async connection (cancel_statement). Acancel_on_killflag (defaultTrue) is added to bothRedshiftDataOperatorandRedshiftDataTriggerso users can opt out, and the operator threads it into the trigger at the defer site. Cancellation is best effort: failures while cancelling are logged and swallowed so trigger teardown is not blocked.Live verification
Verified in a running Airflow instance (
airflow standalone: real scheduler, triggerer and API server) against a liveredshift-dataAPI endpoint served by moto, with no mocks anywhere in the execution path:A deferrable
RedshiftDataOperatorsubmitted a statement (ExecuteStatementover HTTP, statusSTARTED) and the task reacheddeferred, with the trigger polling the statement from the triggerer's event loop through the real aiobotocore client.Marking the deferred task failed from the UI drove the user action cancel path. Triggerer log:
No exception escaped
on_kill.DescribeStatementafterwards returnedStatus: ABORTED, and without the change the same kill leaves the statementSTARTED(the baseon_killis a no op, so nothing contacts the API).This exercises the full deferred kill chain end to end: trigger serialization round trip,
on_killinvocation by the triggerer, async connection creation inside the event loop, and theCancelStatementHTTP call. What it does not cover is AWS side cancellation semantics; the trigger issues the samecancel_statementcall the operator's existing non deferredon_killalready ships and the unit tests cover.Tests
Trigger tests cover cancel on kill, the disabled and missing statement id no ops, best effort error swallowing, and serialization of the new flag. Operator tests cover the
cancel_on_kill=Falseguard and that the flag is threaded into the trigger on defer. Every new assertion fails without the source change.Was generative AI tooling used to co-author this PR?
Generated-by: Claude Code (Fable 5) following the guidelines