Uh oh!
There was an error while loading. Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork 17.7k
Cancel the Dataflow job when a user kills the deferred task#69586
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
Open
steveahnahn
wants to merge
2
commits into
apache:mainChoose a base branch
from
steveahnahn:dataflow-cancel-job-on-user-kill-deferred
base:main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Uh oh!
There was an error while loading. Please reload this page.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading. Please reload this page.
Diff view
Diff view
There are no files selected for viewing
37 changes: 33 additions & 4 deletions
37 providers/google/src/airflow/providers/google/cloud/operators/dataflow.py
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -298,6 +298,9 @@ class DataflowTemplatedJobStartOperator(GoogleCloudBaseOperator): | ||
| https://cloud.google.com/dataflow/docs/templates/executing-templates | ||
| :param deferrable: Run operator in the deferrable mode. | ||
| :param cancel_on_kill: If True (default), cancel the Dataflow job when the task is killed, | ||
| both while the operator is running and, for a deferred task, while it waits in the | ||
| triggerer. | ||
| """ | ||
| template_fields: Sequence[str] = ( | ||
| @@ -334,11 +337,13 @@ def __init__( | ||
| append_job_name: bool = True, | ||
| deferrable: bool = conf.getboolean("operators", "default_deferrable", fallback=False), | ||
| expected_terminal_state: str | None = None, | ||
| cancel_on_kill: bool = True, | ||
| **kwargs, | ||
| ) -> None: | ||
| super().__init__(**kwargs) | ||
| self.template = template | ||
| self.cancel_on_kill = cancel_on_kill | ||
| self.job_name = job_name | ||
| self.options = options or {} | ||
| self.dataflow_default_options = dataflow_default_options or {} | ||
| @@ -437,6 +442,7 @@ def set_current_job(current_job): | ||
| poll_sleep=self.poll_sleep, | ||
| impersonation_chain=self.impersonation_chain, | ||
| cancel_timeout=self.cancel_timeout, | ||
| cancel_on_kill=self.cancel_on_kill, | ||
| ), | ||
| method_name=GOOGLE_DEFAULT_DEFERRABLE_METHOD_NAME, | ||
| ) | ||
| @@ -453,7 +459,10 @@ def execute_complete(self, context: Context, event: dict[str, Any]) -> str: | ||
| return job_id | ||
| def on_kill(self) -> None: | ||
| """Cancel the running job; a kill of a deferred task cancels through the trigger instead.""" | ||
| self.log.info("On kill.") | ||
| if not self.cancel_on_kill: | ||
| return | ||
| if self.job is not None: | ||
| self.log.info("Cancelling job %s", self.job_name) | ||
| self.hook.cancel_job( | ||
| @@ -525,6 +534,9 @@ class DataflowStartFlexTemplateOperator(GoogleCloudBaseOperator): | ||
| Service Account Token Creator IAM role to the directly preceding identity, with first | ||
| account from the list granting this role to the originating account (templated). | ||
| :param deferrable: Run operator in the deferrable mode. | ||
| :param cancel_on_kill: If True (default), cancel the Dataflow job when the task is killed, | ||
| both while the operator is running and, for a deferred task, while it waits in the | ||
| triggerer. | ||
| :param expected_terminal_state: The expected final status of the operator on which the corresponding | ||
| Airflow task succeeds. When not specified, it will be determined by the hook. | ||
| :param append_job_name: True if unique suffix has to be appended to job name. | ||
| @@ -550,11 +562,13 @@ def __init__( | ||
| append_job_name: bool = True, | ||
| expected_terminal_state: str | None = None, | ||
| poll_sleep: int = 10, | ||
| cancel_on_kill: bool = True, | ||
| *args, | ||
| **kwargs, | ||
| ) -> None: | ||
| super().__init__(*args, **kwargs) | ||
| self.body = body | ||
| self.cancel_on_kill = cancel_on_kill | ||
| self.location = location | ||
| self.project_id = project_id | ||
| self.gcp_conn_id = gcp_conn_id | ||
| @@ -633,6 +647,8 @@ def set_current_job(current_job): | ||
| poll_sleep=self.poll_sleep, | ||
| impersonation_chain=self.impersonation_chain, | ||
| cancel_timeout=self.cancel_timeout, | ||
| drain_pipeline=self.drain_pipeline, | ||
| cancel_on_kill=self.cancel_on_kill, | ||
| ), | ||
| method_name=GOOGLE_DEFAULT_DEFERRABLE_METHOD_NAME, | ||
| ) | ||
| @@ -658,7 +674,10 @@ def execute_complete(self, context: Context, event: dict) -> dict[str, str]: | ||
| return job | ||
| def on_kill(self) -> None: | ||
| """Cancel the running job; a kill of a deferred task cancels through the trigger instead.""" | ||
| self.log.info("On kill.") | ||
| if not self.cancel_on_kill: | ||
| return | ||
| if self.job is not None: | ||
| self.hook.cancel_job( | ||
| job_id=self.job.get("id"), | ||
| @@ -689,10 +708,13 @@ class DataflowStartYamlJobOperator(GoogleCloudBaseOperator): | ||
| :param append_job_name: Optional. Set to True if a unique suffix has to be appended to the `job_name`. | ||
| Defaults to True. | ||
| :param drain_pipeline: Optional. Set to True if you want to stop a streaming pipeline job by draining it | ||
| instead of canceling when killing the task instance. Note that this does not work for batch pipeline jobs | ||
| or in the deferrable mode. Defaults to False. | ||
| instead of canceling when killing the task instance. Note that this does not work for batch pipeline jobs. | ||
| Defaults to False. | ||
| For more info see: https://cloud.google.com/dataflow/docs/guides/stopping-a-pipeline | ||
| :param deferrable: Optional. Run operator in the deferrable mode. | ||
| :param cancel_on_kill: If True (default), cancel the Dataflow job when the task is killed, | ||
| both while the operator is running and, for a deferred task, while it waits in the | ||
| triggerer. | ||
| :param expected_terminal_state: Optional. The expected terminal state of the Dataflow job at which the | ||
| operator task is set to succeed. Defaults to 'JOB_STATE_DONE' for the batch jobs and 'JOB_STATE_RUNNING' | ||
| for the streaming jobs. | ||
| @@ -748,10 +770,12 @@ def __init__( | ||
| jinja_variables: dict[str, str] | None = None, | ||
| options: dict[str, Any] | None = None, | ||
| impersonation_chain: str | Sequence[str] | None = None, | ||
| cancel_on_kill: bool = True, | ||
| **kwargs, | ||
| ) -> None: | ||
| super().__init__(**kwargs) | ||
| self.job_name = job_name | ||
| self.cancel_on_kill = cancel_on_kill | ||
| self.yaml_pipeline_file = yaml_pipeline_file | ||
| self.region = region | ||
| self.project_id = project_id | ||
| @@ -793,6 +817,8 @@ def execute(self, context: Context) -> dict[str, Any]: | ||
| cancel_timeout=self.cancel_timeout, | ||
| expected_terminal_state=self.expected_terminal_state, | ||
| impersonation_chain=self.impersonation_chain, | ||
| drain_pipeline=self.drain_pipeline, | ||
| cancel_on_kill=self.cancel_on_kill, | ||
| ), | ||
steveahnahn marked this conversation as resolved.
Uh oh!There was an error while loading. Please reload this page. | ||
| method_name=GOOGLE_DEFAULT_DEFERRABLE_METHOD_NAME, | ||
| ) | ||
| @@ -818,10 +844,13 @@ def on_kill(self): | ||
| """ | ||
| Cancel the dataflow job if a task instance gets killed. | ||
| This method will not be called if a task instance is killed in a deferred | ||
| state. | ||
| This method is not called for a task instance killed in a deferred state; | ||
| in that case the trigger cancels the job instead, honoring cancel_on_kill | ||
| and drain_pipeline. | ||
| """ | ||
| self.log.info("On kill called.") | ||
| if not self.cancel_on_kill: | ||
| return | ||
| if self.job_id: | ||
| self.hook.cancel_job( | ||
| job_id=self.job_id, | ||
101 changes: 100 additions & 1 deletion
101 providers/google/src/airflow/providers/google/cloud/triggers/dataflow.py
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
Oops, something went wrong.
Uh oh!
There was an error while loading. Please reload this page.
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.
Uh oh!
There was an error while loading. Please reload this page.