o-nikolas opened a new pull request, #70207:
URL: https://github.com/apache/airflow/pull/70207
### NOTE: The description is long, but it's worth reading to get the context
of a deep in the weeds issue
## Problem
When a Dag file's *behavior* changes but its *structure* doesn't (the very
common case — e.g.
editing a constant inside a `@task` function body, which is not
serialized), `write_dag`
does not create a new `DagVersion`. Instead it refreshes the latest row's
bundle pointer
**in place**:
dag_version.bundle_version = bundle_version # overwrite
dag_version.version_data = version_data # overwrite
For Git bundles this is harmless: a pinned run resolves its code from
`dag_run.bundle_version` (a commit SHA, recorded on the run itself and
never overwritten),
and git's immutable object store can always check that SHA out.
For bundles that rely on `version_data` (the json blob which represents the
state of the code for that bundle version; for S3 this is a dictionary
manifest containing the S3 version for each file in the bucket) the
overwrite is destructive. The manifest stored in `DagVersion.version_data`
is the **only**
way to reconstruct the pinned code.
Taking the upcoming S3 Bundle as an example: when the bucket content
changes mid-run
(for a change that doesn't modify the dag structure itself), the parse
overwrites the row and the pinned run's
manifest is gone:
BEFORE (run pinned to v1) AFTER re-parse of v2 (hash
unchanged)
─────────────────────────
─────────────────────────────────────
DagRun.bundle_version: v1 DagRun.bundle_version: v1 (still!)
DagVersion row: DagVersion row (same row):
bundle_version: v1 bundle_version: v2 ◄─
overwritten
version_data: manifest_v1 version_data: manifest_v2 ◄─ v1
manifest DESTROYED
A second, pre-existing symptom of the same overwrite (which in this case
affects all versioned
Bundles, including Git): triggering a run
against a specific `bundle_version` resolves the serialized code via a
`DagVersion` lookup keyed on
`bundle_version`, so when the DagVersion is overwritten the
`bundle_version`s are lost, `DagVersionNotFound`
are raised. This will be possible to do now with these changes.
## Fix
Now, when version_data is involved (e.g. a Bundle is being used which
needs to store metadata in the DagVersion row AND versioning is
enabled for that Bundle AND the DagVersion has active tasks) a new
DagVersion is created instead of overwriting (mirroring the existing
path we take when the dag has a structural change). The old row keeps
serving pinned runs; the new row becomes the latest for new runs.
Bundles without version_data (e.g. Git) keep the cheap in-place
pointer refresh.
BEFORE (run pinned to v1) AFTER re-parse of v2 (hash
unchanged)
─────────────────────────
─────────────────────────────────────
DagRun.bundle_version: v1 DagRun.bundle_version: v1
DagVersion vN: DagVersion vN (untouched):
bundle_version: v1 bundle_version: v1
version_data: manifest_v1 version_data: manifest_v1 ◄─
pinned runs keep it
DagVersion vN+1 (new, latest):
bundle_version: v2 ◄─
new runs inherit it
version_data: manifest_v2
**Once a `(bundle_version, version_data)` pairing has been observed by
task instances, it is
effectively immutable.**
Safety measures:
- **Bundles without `version_data` (Git, Local): no change.** Either their
`bundle_version` alone
recovers the code or they don't support versioning at all. So the
in-place DagVersion refresh
behaviour remains.
- **No task instances on the current version: no change.** Nothing is
pinned to it, so the
metadata (including `version_data`) is still refreshed in place.
- New rows are created only when bucket content actually changes. Bucket
updates are a deliberate
action by the user and will not be as frequent as repository or local FS
changes.
- Rows are garbage collected by the existing `db_cleanup` handling for
`dag_version`.
<!-- 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".
-->
- [ ] 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)
-->
---
* 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]