amoghrajesh opened a new pull request, #77158:
URL: https://github.com/apache/airflow/pull/77158
<!-- SPDX-License-Identifier: Apache-2.0
https://www.apache.org/licenses/LICENSE-2.0 -->
<!--
Thank you for contributing!
Please provide above a brief description of the changes made in this pull
request.
Write a good git commit message following this guide:
https://chris.beams.io/posts/git-commit/
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.
For user-facing UI changes, please attach before/after screenshots (or a
short
screen recording) so reviewers can assess the visual impact.
Feel free to ping (in general) for the review if you do not see reaction for
a few days
(72 Hours is the minimum reaction time you can expect from volunteers) - we
sometimes miss notifications.
In case of an existing issue, reference it using one of the following:
* closes: #ISSUE
* related: #ISSUE
-->
---
##### Was generative AI tooling used to co-author this PR?
<!--
If generative AI tooling has been used in the process of authoring this PR,
please
change below checkbox to `[X]` followed by the name of the tool, uncomment
the "Generated-by".
-->
- [x] Yes (please specify the tool below)
<!--
Generated-by: [Tool Name] following [the
guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#gen-ai-assisted-contributions)
-->
### What
`BatchOperator` waits on a Batch job that can run for hours. If the worker
dies during that wait (OOM, pod eviction, spot reclaim), the retry submits a
second job while the first one keeps running in AWS. You pay for both, and both
write the same output.
### Current behaviour
In synchronous mode, `execute()` calls `submit_job()` on every try. Nothing
records the job ID across tries, so a retry can't find the job the previous try
started.
### Proposed change
`BatchOperator` now uses `ResumableJobMixin`, like the Glue, Redshift Data
and SageMaker operators. On the synchronous path it saves the Batch job ID to
the task state store (key `batch_job_id`) right after submitting. On retry it
calls `describe_jobs` for that ID:
- `SUBMITTED` through `RUNNING`: reconnect and keep waiting
- `SUCCEEDED`: return the job ID without submitting
- `FAILED`, or not returned by Batch: submit a new job
`durable` defaults to `True`. The deferrable path does not change.
### Changes of Note
- `get_job_status` calls `describe_jobs` directly and maps an empty response
to `NOT_FOUND`. `BatchClientHook.get_job_description` retries with backoff and
then raises, which would fail the retry for a job AWS no longer returns.
- On reconnect the operator saves the "Job details" link again, so the UI
points at the reused job.
- `submit_job` now returns the job ID. A subclass whose override returns
`None` keeps working, because the operator keeps the `self.job_id` the override
set.
### User implications / backcompat
- A retry now reconnects instead of resubmitting. Pass `durable=False` to
keep the old behaviour. The changelog has a note.
- Reconnecting needs Airflow 3.3+. On older versions the operator submits a
new job on every try, as before.
### Testing
Unit tests cover fresh submit, reconnect, already succeeded, failed and
missing job, `durable=False`, and a `submit_job` override that returns `None`.
I also ran it end to end in breeze against the
[aws-emulator](https://github.com/aws/aws-emulator) Batch API, killing the task
process with SIGKILL while it waited on the job:
| Scenario | Batch jobs after the retry |
|---|---|
| Prior job still active | 1, retry reconnected |
| Prior job `FAILED` | 2, retry resubmitted |
| Prior job gone (emulator restarted) | 1, retry resubmitted |
| `durable=False` | 2, duplicate |
The emulator runs no containers, so jobs never reach `SUCCEEDED` and every
run ends failed. Only unit tests cover the already-succeeded path.
**Test Dags**
<img width="1600" height="1000" alt="00-airflow-dags"
src="https://github.com/user-attachments/assets/df6ae563-9e4a-4dcb-98ab-1dc4a67db5f8"
/>
**1. Prior job still active: retry reconnects**
Try 1 submits the job, then gets SIGKILLed:
<img width="1600" height="1000"
alt="01a-reconnect-try1-submitted-then-killed"
src="https://github.com/user-attachments/assets/635ceead-2803-4f8b-8b68-e300b77c0e6c"
/>
Try 2 logs `Reconnecting to existing job` and waits on the same job ID:
<img width="1600" height="1000" alt="01b-reconnect-try2-reconnects-same-job"
src="https://github.com/user-attachments/assets/cc8fe12e-b928-4024-8928-e5602f8a1955"
/>
Task state store holds `batch_job_id`:
<img width="1600" height="1000" alt="01c-reconnect-task-state-store"
src="https://github.com/user-attachments/assets/4fffd867-94e4-4267-8a61-1a6b983b9005"
/>
"Job details" link saved again on reconnect:
<img width="1600" height="1000" alt="01d-reconnect-job-details-link-saved"
src="https://github.com/user-attachments/assets/d5d7c152-b120-44c5-bd4b-d96300730343"
/>
**2. Prior job `FAILED`: retry resubmits**
Try 1, the job fails:
<img width="1600" height="1000" alt="02a-resubmit-failed-try1-job-failed"
src="https://github.com/user-attachments/assets/c732bf17-9736-4c99-aa99-7c2c69d18865"
/>
Try 2 logs `Prior job in terminal state, resubmitting fresh`:
<img width="1600" height="1350" alt="02b-resubmit-failed-try2-resubmits"
src="https://github.com/user-attachments/assets/b56db17f-e910-4802-b9b3-3c664ba2b3c7"
/>
Task state store now holds the new job ID:
<img width="1600" height="1000" alt="02c-resubmit-failed-task-state-store"
src="https://github.com/user-attachments/assets/29e8ecb7-103e-449d-b114-135f76e889dc"
/>
**3. Prior job gone: retry resubmits**
Try 1 submits the job, then gets SIGKILLed. The emulator restarts and drops
the job:
<img width="1600" height="1000"
alt="03a-not-found-try1-submitted-then-killed"
src="https://github.com/user-attachments/assets/58a8b527-1b57-4e1c-a646-683b8b9d55ac"
/>
Try 2 sees `NOT_FOUND` and submits a new job:
<img width="1600" height="1000" alt="03b-not-found-try2-resubmits"
src="https://github.com/user-attachments/assets/ac0cc9d5-9756-42f4-b737-4b7d81545a73"
/>
Task state store holds the new job ID:
<img width="1600" height="1000" alt="03c-not-found-task-state-store"
src="https://github.com/user-attachments/assets/9b7e94bb-99b0-459a-9427-44b39e680a47"
/>
**4. `durable=False`: retry submits a duplicate**
Try 1 submits the job, then gets SIGKILLed:
<img width="1600" height="1000"
alt="04a-durable-off-try1-submitted-then-killed"
src="https://github.com/user-attachments/assets/d12854f7-0bde-453a-bc72-d5b5465ff29e"
/>
Try 2 submits a second job while the first is still `RUNNABLE`:
<img width="1600" height="1000" alt="04b-durable-off-try2-duplicate-job"
src="https://github.com/user-attachments/assets/775240f8-7a7c-4c1c-bd6d-01d9379d12a6"
/>
Task state store stays empty:
<img width="1600" height="1000" alt="04c-durable-off-task-state-store-empty"
src="https://github.com/user-attachments/assets/7b06c54e-d1dd-403f-ac5e-c3365d0bc420"
/>
**Batch side: jobs per Dag (emulator `describe-jobs` output)**
<img width="1600" height="1000" alt="05-batch-emulator-jobs"
src="https://github.com/user-attachments/assets/6ecfbe89-5d9d-4a13-aceb-70e1580cc8b8"
/>
### What's next
Deferrable runs don't read task state yet. The mixin can't make the
reconnect decision without also polling; #71485 tracks that. Until it lands,
deferrable `BatchOperator` behaves as before.
---
* Read the **[Pull Request
Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)**
for more information. Note: commit author/co-author name and email in commits
become permanently public when merged.
* For fundamental code changes, an Airflow Improvement Proposal
([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals))
is needed.
* When adding dependency, check compliance with the [ASF 3rd Party License
Policy](https://www.apache.org/legal/resolved.html#category-x).
* For significant user-facing changes create newsfragment:
`{pr_number}.significant.rst`, in
[airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/newsfragments).
You can add this file in a follow-up commit after the PR is created so you
know the PR number.
--
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: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]