You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "sunank200 (via GitHub)" <gi...@apache.org> on 2024/04/10 20:46:07 UTC
[PR] Add logic to handle on_kill for BigQueryInsertJobOperator when deferrable=True [airflow]
sunank200 opened a new pull request, #38912:
URL: https://github.com/apache/airflow/pull/38912
<!--
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
-->
<!--
Thank you for contributing! Please make sure that your code changes
are covered with tests. And in case of new features or big changes
remember to adjust the documentation.
Feel free to ping committers for the review!
In case of an existing issue, reference it using one of the following:
closes: #ISSUE
related: #ISSUE
How to write a good git commit message:
http://chris.beams.io/posts/git-commit/
-->
- Add logic to handle on_kill for BigQueryInsertJobOperator when `deferrable=True`
<!-- Please keep an empty line above the dashes. -->
---
**^ Add meaningful description above**
Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add logic to handle on_kill for BigQueryInsertJobOperator when deferrable=True [airflow]
Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on code in PR #38912:
URL: https://github.com/apache/airflow/pull/38912#discussion_r1565286434
##########
airflow/providers/google/cloud/hooks/bigquery.py:
##########
@@ -3389,6 +3389,31 @@ async def create_job_for_partition_get(
job_query_resp = await job_client.query(query_request, cast(Session, session))
return job_query_resp["jobReference"]["jobId"]
+ async def cancel_job(self, job_id: str, project_id: str | None, location: str | None) -> None:
+ """
+ Asynchronously cancel a BigQuery job.
Review Comment:
```suggestion
Cancel a BigQuery job.
```
I think the `async` keyword along is sufficient
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add logic to handle on_kill for BigQueryInsertJobOperator when deferrable=True [airflow]
Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on code in PR #38912:
URL: https://github.com/apache/airflow/pull/38912#discussion_r1562736743
##########
airflow/providers/google/cloud/hooks/bigquery.py:
##########
@@ -3389,6 +3389,31 @@ async def create_job_for_partition_get(
job_query_resp = await job_client.query(query_request, cast(Session, session))
return job_query_resp["jobReference"]["jobId"]
+ async def cancel_job(self, job_id: str, project_id: str | None, location: str | None) -> None:
+ """
+ Asynchronously cancel a BigQuery job.
+
+ :param job_id: ID of the job to cancel.
+ :param project_id: Google Cloud Project where the job was running.
+ :param location: Location where the job was running.
+ """
+ async with ClientSession() as session:
+ token = await self.get_token(session=session)
+ job = Job(job_id=job_id, project=project_id, location=location, token=token, session=session) # type: ignore
Review Comment:
Would it be possible for us to narrow down the type ignore to which kinds of type ignore? Or is it supposed to ignore all?
##########
tests/providers/google/cloud/hooks/test_bigquery.py:
##########
@@ -2186,6 +2186,25 @@ async def test_get_job_output_assert_once_with(self, mock_job_instance):
resp = await hook.get_job_output(job_id=JOB_ID, project_id=PROJECT_ID)
assert resp == response
+ @pytest.mark.asyncio
+ @mock.patch("google.auth.default")
+ @mock.patch("airflow.providers.google.cloud.hooks.bigquery.Job")
+ async def test_cancel_job_success(self, mock_job, mock_auth_default):
Review Comment:
Let's create a test case for failed case.
##########
airflow/providers/google/cloud/triggers/bigquery.py:
##########
@@ -113,6 +116,14 @@ async def run(self) -> AsyncIterator[TriggerEvent]: # type: ignore[override]
self.poll_interval,
)
await asyncio.sleep(self.poll_interval)
+ except asyncio.CancelledError:
Review Comment:
Would it be posible for us to add an unit test for this case?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add logic to handle on_kill for BigQueryInsertJobOperator when deferrable=True [airflow]
Posted by "tirkarthi (via GitHub)" <gi...@apache.org>.
tirkarthi commented on PR #38912:
URL: https://github.com/apache/airflow/pull/38912#issuecomment-2067009288
@sunank200 As I understand this will kill the job when triggerer is restarted for deployment, maintenance etc or marked unhealthy in HA mode with the trigger to be handled by another triggerer job as the task is cancelled by asyncio and the `CancellationError` is caught in except clause to kill the job. Is it expected that triggerer restarts should kill the job?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add logic to handle on_kill for BigQueryInsertJobOperator when deferrable=True [airflow]
Posted by "sunank200 (via GitHub)" <gi...@apache.org>.
sunank200 commented on code in PR #38912:
URL: https://github.com/apache/airflow/pull/38912#discussion_r1565259416
##########
tests/providers/google/cloud/hooks/test_bigquery.py:
##########
@@ -2186,6 +2186,25 @@ async def test_get_job_output_assert_once_with(self, mock_job_instance):
resp = await hook.get_job_output(job_id=JOB_ID, project_id=PROJECT_ID)
assert resp == response
+ @pytest.mark.asyncio
+ @mock.patch("google.auth.default")
+ @mock.patch("airflow.providers.google.cloud.hooks.bigquery.Job")
+ async def test_cancel_job_success(self, mock_job, mock_auth_default):
Review Comment:
Added it.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add logic to handle on_kill for BigQueryInsertJobOperator when deferrable=True [airflow]
Posted by "sunank200 (via GitHub)" <gi...@apache.org>.
sunank200 commented on code in PR #38912:
URL: https://github.com/apache/airflow/pull/38912#discussion_r1565245253
##########
airflow/providers/google/cloud/triggers/bigquery.py:
##########
@@ -113,6 +116,14 @@ async def run(self) -> AsyncIterator[TriggerEvent]: # type: ignore[override]
self.poll_interval,
)
await asyncio.sleep(self.poll_interval)
+ except asyncio.CancelledError:
Review Comment:
Added the test.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add logic to handle on_kill for BigQueryInsertJobOperator when deferrable=True [airflow]
Posted by "thesuperzapper (via GitHub)" <gi...@apache.org>.
thesuperzapper commented on PR #38912:
URL: https://github.com/apache/airflow/pull/38912#issuecomment-2094973537
@Lee-W @sunank200 @tirkarthi this PR is NOT safe, and needs to be reverted or updated.
Please see my https://github.com/apache/airflow/issues/36090#issuecomment-2094972855 for more information.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add logic to handle on_kill for BigQueryInsertJobOperator when deferrable=True [airflow]
Posted by "sunank200 (via GitHub)" <gi...@apache.org>.
sunank200 commented on code in PR #38912:
URL: https://github.com/apache/airflow/pull/38912#discussion_r1565245537
##########
airflow/providers/google/cloud/hooks/bigquery.py:
##########
@@ -3389,6 +3389,31 @@ async def create_job_for_partition_get(
job_query_resp = await job_client.query(query_request, cast(Session, session))
return job_query_resp["jobReference"]["jobId"]
+ async def cancel_job(self, job_id: str, project_id: str | None, location: str | None) -> None:
+ """
+ Asynchronously cancel a BigQuery job.
+
+ :param job_id: ID of the job to cancel.
+ :param project_id: Google Cloud Project where the job was running.
+ :param location: Location where the job was running.
+ """
+ async with ClientSession() as session:
+ token = await self.get_token(session=session)
+ job = Job(job_id=job_id, project=project_id, location=location, token=token, session=session) # type: ignore
Review Comment:
I have added specific type ignore
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add logic to handle on_kill for BigQueryInsertJobOperator when deferrable=True [airflow]
Posted by "tirkarthi (via GitHub)" <gi...@apache.org>.
tirkarthi commented on code in PR #38912:
URL: https://github.com/apache/airflow/pull/38912#discussion_r1563729073
##########
airflow/providers/google/cloud/triggers/bigquery.py:
##########
@@ -113,6 +116,14 @@ async def run(self) -> AsyncIterator[TriggerEvent]: # type: ignore[override]
self.poll_interval,
)
await asyncio.sleep(self.poll_interval)
+ except asyncio.CancelledError:
Review Comment:
Triggers also seemed to be cancelled when triggerer job restarts which can cause the job to be cancelled.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add logic to handle on_kill for BigQueryInsertJobOperator when deferrable=True [airflow]
Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W commented on PR #38912:
URL: https://github.com/apache/airflow/pull/38912#issuecomment-2056207822
This PR looks good to me. I'm planning on merging it later today. But please let me know if anyone wants to take a deeper look 🙂
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add logic to handle on_kill for BigQueryInsertJobOperator when deferrable=True [airflow]
Posted by "Lee-W (via GitHub)" <gi...@apache.org>.
Lee-W merged PR #38912:
URL: https://github.com/apache/airflow/pull/38912
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
Re: [PR] Add logic to handle on_kill for BigQueryInsertJobOperator when deferrable=True [airflow]
Posted by "sunank200 (via GitHub)" <gi...@apache.org>.
sunank200 commented on code in PR #38912:
URL: https://github.com/apache/airflow/pull/38912#discussion_r1565246726
##########
airflow/providers/google/cloud/triggers/bigquery.py:
##########
@@ -113,6 +116,14 @@ async def run(self) -> AsyncIterator[TriggerEvent]: # type: ignore[override]
self.poll_interval,
)
await asyncio.sleep(self.poll_interval)
+ except asyncio.CancelledError:
Review Comment:
There is a separate issue for this one: https://github.com/apache/airflow/issues/36090 This change can be done as part of a separate PR which changes airflow core code to handle `on_kill`.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@airflow.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org