-
Notifications
You must be signed in to change notification settings - Fork 17.5k
Cancel Snowflake queries when a user kills the deferred task #69635
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,6 +20,8 @@ | |
| from collections.abc import AsyncIterator | ||
| from typing import TYPE_CHECKING, Any | ||
|
|
||
| from asgiref.sync import sync_to_async | ||
|
|
||
| from airflow.providers.snowflake.hooks.snowflake_sql_api import SnowflakeSqlApiHook | ||
| from airflow.triggers.base import BaseTrigger, TriggerEvent | ||
|
|
||
|
|
@@ -36,6 +38,9 @@ class SnowflakeSqlApiTrigger(BaseTrigger): | |
| :param snowflake_conn_id: Reference to Snowflake connection id | ||
| :param token_life_time: lifetime of the JWT Token in timedelta | ||
| :param token_renewal_delta: Renewal time of the JWT Token in timedelta | ||
| :param cancel_on_kill: If True (default), cancel the running Snowflake queries when the user | ||
| kills the deferred task (mark failed, clear, or mark success). Requires a version of | ||
| ``apache-airflow`` with ``BaseTrigger.on_kill()`` support; on older versions it is inert. | ||
| """ | ||
|
|
||
| def __init__( | ||
|
|
@@ -45,13 +50,15 @@ def __init__( | |
| snowflake_conn_id: str, | ||
| token_life_time: timedelta, | ||
| token_renewal_delta: timedelta, | ||
| cancel_on_kill: bool = True, | ||
| ): | ||
| super().__init__() | ||
| self.poll_interval = poll_interval | ||
| self.query_ids = query_ids | ||
| self.snowflake_conn_id = snowflake_conn_id | ||
| self.token_life_time = token_life_time | ||
| self.token_renewal_delta = token_renewal_delta | ||
| self.cancel_on_kill = cancel_on_kill | ||
|
|
||
| def serialize(self) -> tuple[str, dict[str, Any]]: | ||
| """Serialize SnowflakeSqlApiTrigger arguments and classpath.""" | ||
|
|
@@ -63,6 +70,7 @@ def serialize(self) -> tuple[str, dict[str, Any]]: | |
| "snowflake_conn_id": self.snowflake_conn_id, | ||
| "token_life_time": self.token_life_time, | ||
| "token_renewal_delta": self.token_renewal_delta, | ||
| "cancel_on_kill": self.cancel_on_kill, | ||
| }, | ||
| ) | ||
|
|
||
|
|
@@ -93,6 +101,27 @@ async def run(self) -> AsyncIterator[TriggerEvent]: | |
| except Exception as e: | ||
| yield TriggerEvent({"status": "error", "message": str(e)}) | ||
|
|
||
| async def on_kill(self) -> None: | ||
| """Cancel the running Snowflake queries when the user kills the deferred task.""" | ||
| if not self.cancel_on_kill or not self.query_ids: | ||
| return | ||
| self.log.info("Cancelling Snowflake query ids %s", self.query_ids) | ||
| try: | ||
| await sync_to_async(self._cancel_queries)() | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Non-blocking, more of a docs note: the triggerer wraps this in Cancellation here is N sequential blocking POSTs, each with tenacity retries on 429/503/504. A task with many statements against a slow warehouse could exceed that budget, and because Benign in practice, but the docstring's "best effort" framing might be worth extending to mention the timeout so operators know the knob exists. Drafted-by: Claude Code (Opus 4.8); reviewed by @potiuk before posting |
||
| self.log.info("Snowflake query ids %s cancelled.", self.query_ids) | ||
| except Exception: | ||
| self.log.exception( | ||
| "Failed to cancel Snowflake query ids %s. They may still be running.", self.query_ids | ||
| ) | ||
|
|
||
| def _cancel_queries(self) -> None: | ||
| hook = SnowflakeSqlApiHook( | ||
| self.snowflake_conn_id, | ||
| self.token_life_time, | ||
| self.token_renewal_delta, | ||
| ) | ||
| hook.cancel_queries(self.query_ids) | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
That matters most in exactly the case this PR targets: Mitigating factor: Did your live test cover killing a multi-statement task where some statements had already finished? If 422 is what Snowflake actually returns there, this is a non-issue and worth a one-line comment saying so. If not, a per-query Drafted-by: Claude Code (Opus 4.8); reviewed by @potiuk before posting |
||
|
|
||
| async def get_query_status( | ||
| self, query_id: str, hook: SnowflakeSqlApiHook | None = None | ||
| ) -> dict[str, Any]: | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the first and only
asgirefusage in the Snowflake provider, andasgirefisn't in this provider'sdependenciesinproviders/snowflake/pyproject.toml. It resolves today only transitively viaapache-airflow, but providers should declare what they import.Amazon and Google both do exactly this where they use
sync_to_async:Worth carrying over the
>=3.11.1floor for 3.14 rather than a bare pin.Drafted-by: Claude Code (Opus 4.8); reviewed by @potiuk before posting