Add job_id to DagRun table and remove BackfillJob zombies
- Dominant language
- Python
- Stars
- 46.9k
- Forks
- 17.8k
- Avg merge
- 2d 9h
- Merged PRs (30d)
- 472
Description
# Description
Currently, we have `run_type` in the DagRun table but there's no way to determine what job created a DagRun (no 1-1 relation between DagRun and Job). This can be helpful in debugging (I think especially in the case of Scheduler HA and BackfillJobs) as this will also allow users to check which scheduler / backfill job triggered their task (now the job_id in TaskInstance is always id of LocalTaskJob).
Introducing job_id may help us with making backfill runnable remotely as we will be able to clean up after jobs that failed thus reducing possible zombies.
## The problem
Run:
```
airflow dags backfill -v -s 2020-10-06 example_bash_operator
# once there's a process running single task, do the following:
pkill -9 -f backfil
```
this will result in "zombie" DagRun and related task instance that will not be cleaned up by the scheduler (at least that's my understanding). Example:

However, querying the the job table we see:

So, the backfill job is still running according to Airflow state but that's not true as we killed the job 👎
## Possible solution
Link a specific job to DagRun triggered by it (using the `job_id`) and then run a process that will kill the zombies.
This can be done either by:
- killing a DR (and related TIs) that is in an unfinished state (running, none, scheduled, queued) but the job that was running it is in error state
- killing a DR (and related TIs) that is in an unfinished state but the job that was running it didn't heartbeat for the last few minutes (configurable)
Cleaning of such zombies can be easily triggered by the scheduler.
I think this may bring us closer to triggering backfill via API / UI.
## Use case / motivation
Introduce the relation between DagRun and Job tables and implement a process that will clean up zombies created by BackfillJobs.
**Related Issues**
https://github.com/apache/airflow/pull/8227
Thanks to our friends from [Databand](https://databand.ai) for hinting this!
Contributor guide
Assessment
This issue has not been assessed yet.