You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "amoghrajesh (via GitHub)" <gi...@apache.org> on 2024/12/31 08:15:54 UTC
[PR] AIP-72: Handling `execution_timeout` for tasks in task SDK [airflow]
amoghrajesh opened a new pull request, #45310:
URL: https://github.com/apache/airflow/pull/45310
<!--
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/
-->
closes: #45307
### Testing results
DAG:
```
from time import sleep
from airflow import DAG
from airflow.providers.standard.operators.python import PythonOperator
from datetime import datetime, timedelta
def print_hello():
print("Going to sleep 100 seconds")
sleep(100)
with DAG(
dag_id="tasktimeout",
schedule=None,
catchup=False,
) as dag:
hello_task = PythonOperator(
task_id="ttimemout",
execution_timeout=timedelta(seconds=10),
python_callable=print_hello,
)
```
#### Legacy
1. No retries

2. With timeouts

#### Task sdk
1. No retries
<img width="1725" alt="image" src="https://github.com/user-attachments/assets/7c319262-1729-4777-9d03-6cef0843618a" />
2. With retries
<img width="1725" alt="image" src="https://github.com/user-attachments/assets/92908ae2-8a72-4563-9d92-7438797ea0fb" />
<!-- 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] AIP-72: Handling `execution_timeout` for tasks in task SDK [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh merged PR #45310:
URL: https://github.com/apache/airflow/pull/45310
--
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] AIP-72: Handling `execution_timeout` for tasks in task SDK [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #45310:
URL: https://github.com/apache/airflow/pull/45310#discussion_r1899978312
##########
task_sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -390,19 +390,37 @@ def run(ti: RuntimeTaskInstance, log: Logger):
# - Update RTIF
# - Pre Execute
# etc
- result = ti.task.execute(context) # type: ignore[attr-defined]
- _push_xcom_if_needed(result, ti)
+ result = None
+ if ti.task.execution_timeout:
+ # TODO: handle timeout in case of deferral
+ from airflow.utils.timeout import timeout
Review Comment:
We should be able to import utils. We do in client.py
--
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] AIP-72: Handling `execution_timeout` for tasks in task SDK [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #45310:
URL: https://github.com/apache/airflow/pull/45310#discussion_r1900191350
##########
task_sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -423,13 +441,14 @@ def run(ti: RuntimeTaskInstance, log: Logger):
state=TerminalTIState.FAIL_WITHOUT_RETRY,
end_date=datetime.now(tz=timezone.utc),
)
-
# TODO: Run task failure callbacks here
- except AirflowTaskTimeout:
- # TODO: handle the case of up_for_retry here
- # TODO: coagulate this exception handling with AirflowException
- # once https://github.com/apache/airflow/issues/45307 is handled
- ...
Review Comment:
Coagulating both these exceptions into a single handler, as they do the same thing.
--
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] AIP-72: Handling `execution_timeout` for tasks in task SDK [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #45310:
URL: https://github.com/apache/airflow/pull/45310#discussion_r1899977240
##########
task_sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -390,19 +390,37 @@ def run(ti: RuntimeTaskInstance, log: Logger):
# - Update RTIF
# - Pre Execute
# etc
- result = ti.task.execute(context) # type: ignore[attr-defined]
- _push_xcom_if_needed(result, ti)
+ result = None
+ if ti.task.execution_timeout:
+ # TODO: handle timeout in case of deferral
+ from airflow.utils.timeout import timeout
+
+ timeout_seconds = ti.task.execution_timeout.total_seconds()
+ try:
+ # It's possible we're already timed out, so fast-fail if true
+ if timeout_seconds <= 0:
+ raise AirflowTaskTimeout()
+ # Run task in timeout wrapper
+ with timeout(timeout_seconds):
+ result = ti.task.execute(context) # type: ignore[attr-defined]
+ except AirflowTaskTimeout:
+ # TODO: handle on kill callback here
Review Comment:
Will be done when we port call backs
##########
task_sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -390,19 +390,37 @@ def run(ti: RuntimeTaskInstance, log: Logger):
# - Update RTIF
# - Pre Execute
# etc
- result = ti.task.execute(context) # type: ignore[attr-defined]
- _push_xcom_if_needed(result, ti)
+ result = None
+ if ti.task.execution_timeout:
+ # TODO: handle timeout in case of deferral
Review Comment:
Will handle this in a follow up, getting basic functionality in now
##########
task_sdk/src/airflow/sdk/execution_time/task_runner.py:
##########
@@ -425,7 +443,14 @@ def run(ti: RuntimeTaskInstance, log: Logger):
)
# TODO: Run task failure callbacks here
- except (AirflowTaskTimeout, AirflowException):
+ except AirflowTaskTimeout:
+ # We should allow retries if the task has defined it
+ msg = TaskState(
+ state=TerminalTIState.FAILED,
+ end_date=datetime.now(tz=timezone.utc),
+ )
+ # TODO: Run task failure callbacks here
Review Comment:
Might conflict with https://github.com/apache/airflow/pull/45308. Will handle it once the first of these 2 is merged
##########
task_sdk/tests/execution_time/test_task_runner.py:
##########
@@ -330,6 +330,48 @@ def test_run_raises_system_exit(time_machine, mocked_parse, make_ti_context, moc
)
+def test_run_task_timeout(time_machine, mocked_parse, make_ti_context, mock_supervisor_comms):
+ """Test running a basic task that times out."""
+ from time import sleep
+
+ from airflow.providers.standard.operators.python import PythonOperator
+
+ task = PythonOperator(
+ task_id="sleep",
+ execution_timeout=timedelta(milliseconds=10),
+ python_callable=lambda: sleep(2),
Review Comment:
Keeping a minimum "kill" delay to not add overhead to the test infra
--
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] AIP-72: Handling `execution_timeout` for tasks in task SDK [airflow]
Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on PR #45310:
URL: https://github.com/apache/airflow/pull/45310#issuecomment-2566850977
Will merge this PR with one approval as it is functional. Can handle any issues or shortcomings, if any, in follow ups.
--
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