shihchengwei-lab opened a new issue, #77504:
URL: https://github.com/apache/airflow/issues/77504

   At `1311365a3872e5c0b89bc5e24ce913dfc59d8864`, I reproduced a late-result 
ordering problem through Airflow's direct database write and read APIs.
   
   1. A prepares a v1 DAG containing `old_task` but has not published its 
result.
   2. B publishes v2 containing `new_task`. An existing run gets a task 
instance pointing to that DAG version.
   3. A publishes its earlier v1 result.
   
   | Fresh read | Latest DAG | B's existing run |
   |---|---|---|
   | Before A publishes | Version 1, `new_task` | Version 1, `new_task` |
   | After A publishes | Version 2, `old_task` | Version 1, `new_task` |
   
   Each observation uses a new database session and `DBDagBag`. The script 
asserts that B's task instance points to version 1.
   
   I expected A's result to be rejected or rescheduled once v2 had been 
published. Could the direct database publication path check whether the parsed 
bundle version is still current before updating DAG state? A late result can 
otherwise receive the highest DAG version number, which subsequent latest-DAG 
reads select.
   
   This is a function-level reproduction using Airflow's database write and 
read APIs. I controlled the publication order and have not run two DAG 
processor processes against each other. The script sets `min_update_interval=0` 
so the serialization throttle does not mask the ordering behavior.
   
   [Manager publication 
path](https://github.com/apache/airflow/blob/1311365a3872e5c0b89bc5e24ce913dfc59d8864/airflow-core/src/airflow/dag_processing/manager.py#L1796-L1854)
 ยท [Version 
creation](https://github.com/apache/airflow/blob/1311365a3872e5c0b89bc5e24ce913dfc59d8864/airflow-core/src/airflow/models/serialized_dag.py#L740-L826)
   
   <details>
   <summary>Reproduction script and setup</summary>
   
   Use an empty SQLite metadata database. From the repository root at the 
commit above, install its local packages and initialize the database:
   
   ```sh
   python -m pip install -e . -e airflow-core -e task-sdk
   export AIRFLOW_HOME=/tmp/airflow-test
   export 
AIRFLOW__DATABASE__SQL_ALCHEMY_CONN=sqlite:////tmp/airflow-test/airflow.db
   export AIRFLOW__CORE__LOAD_EXAMPLES=False
   airflow db migrate
   python repro.py
   ```
   
   Save this as `repro.py`:
   
   ```python
   import os
   
   os.environ['AIRFLOW_HOME'] = '/tmp/airflow-test'
   os.environ['AIRFLOW__DATABASE__SQL_ALCHEMY_CONN'] = 
'sqlite:////tmp/airflow-test/airflow.db'
   os.environ['AIRFLOW__CORE__LOAD_EXAMPLES'] = 'False'
   
   from sqlalchemy import select
   
   from airflow.models.dagbag import DBDagBag
   from airflow.models.dag import DagModel
   from airflow.models.dagbundle import DagBundleModel
   from airflow.models.dagrun import DagRun
   from airflow.models.dag_version import DagVersion
   from airflow.models.serialized_dag import SerializedDagModel
   from airflow.models.taskinstance import TaskInstance
   from airflow.providers.standard.operators.empty import EmptyOperator
   from airflow.sdk import DAG
   from airflow.serialization.serialized_objects import LazyDeserializedDAG
   from airflow.utils.session import create_session
   from airflow.utils.state import DagRunState
   from airflow.utils.types import DagRunType
   
   
   def make_dag(task_id):
       with DAG(dag_id='late_result_repro', schedule=None) as dag:
           EmptyOperator(task_id=task_id)
       return dag
   
   
   def observe(label):
       # A new Session and DBDagBag on each observation avoid either cache.
       with create_session() as session:
           latest = DagVersion.get_latest_version('late_result_repro', 
session=session)
           latest_dag = 
DBDagBag().get_latest_version_of_dag('late_result_repro', session=session)
           run = session.scalar(select(DagRun).where(DagRun.run_id == 
'b_existing_run'))
           run_dag = DBDagBag().get_dag_for_run(run, session=session)
           task_instance = 
session.scalar(select(TaskInstance).where(TaskInstance.run_id == run.run_id))
           assert task_instance.dag_version_id == run.created_dag_version_id
           print(label, 'latest', latest.version_number, 
sorted(latest_dag.task_ids),
                 'B run', run.created_dag_version.version_number, 
sorted(run_dag.task_ids),
                 'TI version', run.created_dag_version.version_number, 
flush=True)
   
   
   a = make_dag('old_task')
   b = make_dag('new_task')
   
   with create_session() as session:
       session.add(DagBundleModel(name='repro', version='v2'))
       session.flush()
       session.add(DagModel(dag_id='late_result_repro', 
fileloc='/tmp/repro_dag.py',
                            bundle_name='repro', bundle_version='v2'))
   
   # A has completed parsing v1 but its result has not been persisted.
   SerializedDagModel.write_dag(LazyDeserializedDAG.from_dag(b), 
bundle_name='repro',
                                bundle_version='v2', min_update_interval=0)
   with create_session() as session:
       n = DagVersion.get_latest_version('late_result_repro', session=session)
       run = DagRun(dag_id='late_result_repro', run_id='b_existing_run',
                    run_type=DagRunType.MANUAL, state=DagRunState.QUEUED,
                    bundle_version='v2')
       run.created_dag_version_id = n.id
       session.add(run)
       session.flush()
       session.add(TaskInstance(task=b.get_task('new_task'), 
dag_version_id=n.id,
                                run_id=run.run_id))
   
   observe('BEFORE A')
   
   # The same direct-DB writer receives A's already parsed result after B 
committed.
   SerializedDagModel.write_dag(LazyDeserializedDAG.from_dag(a), 
bundle_name='repro',
                                bundle_version='v1', min_update_interval=0)
   observe('AFTER A')
   ```
   
   Observed output:
   
   ```text
   BEFORE A latest 1 ['new_task'] B run 1 ['new_task'] TI version 1
   AFTER A latest 2 ['old_task'] B run 1 ['new_task'] TI version 1
   ```
   
   </details>
   
   


-- 
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]

Reply via email to