amoghrajesh commented on code in PR #65759: URL: https://github.com/apache/airflow/pull/65759#discussion_r3145867282
########## airflow-core/src/airflow/models/task_state.py: ########## @@ -0,0 +1,61 @@ +# +# 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. +from __future__ import annotations + +from datetime import datetime + +from sqlalchemy import ForeignKeyConstraint, Index, Integer, PrimaryKeyConstraint, String, Text +from sqlalchemy.orm import Mapped, mapped_column + +from airflow._shared.timezones import timezone +from airflow.models.base import COLLATION_ARGS, ID_LEN, Base +from airflow.utils.sqlalchemy import UtcDateTime + + +class TaskStateModel(Base): + """ + Persists key/value state for a task within a single DAG run. + + Scoped to (dag_run_id, task_id, map_index). Retries of the same task share + the same rows — that is the point. Different DAG runs have different dag_run_id + values so they get independent namespaces automatically. + """ + + __tablename__ = "task_state" + + dag_run_id: Mapped[int] = mapped_column(Integer, nullable=False, primary_key=True) + task_id: Mapped[str] = mapped_column(String(ID_LEN, **COLLATION_ARGS), nullable=False, primary_key=True) + map_index: Mapped[int] = mapped_column(Integer, primary_key=True, nullable=False, server_default="-1") + key: Mapped[str] = mapped_column(String(512, **COLLATION_ARGS), nullable=False, primary_key=True) + + dag_id: Mapped[str] = mapped_column(String(ID_LEN, **COLLATION_ARGS), nullable=False) + run_id: Mapped[str] = mapped_column(String(ID_LEN, **COLLATION_ARGS), nullable=False) Review Comment: I think that would break the use case AIP-103 is trying to handle. Each retry gets a new `task_instance.id` UUID — verified this empirically too. 11 tries on a task with retries=10, 11 unique UUIDs across task_instance + task_instance_history. ``` [Breeze:3.10.19] root@eaabf698afd2:/opt/airflow$ python /opt/airflow/dev/check_ti_uuid_across_retries.py 2026-04-27T08:23:06.814648Z [info ] setup plugin alembic.autogenerate.schemas [alembic.runtime.plugins] loc=plugins.py:37 2026-04-27T08:23:06.814749Z [info ] setup plugin alembic.autogenerate.tables [alembic.runtime.plugins] loc=plugins.py:37 2026-04-27T08:23:06.814794Z [info ] setup plugin alembic.autogenerate.types [alembic.runtime.plugins] loc=plugins.py:37 2026-04-27T08:23:06.814838Z [info ] setup plugin alembic.autogenerate.constraints [alembic.runtime.plugins] loc=plugins.py:37 2026-04-27T08:23:06.814889Z [info ] setup plugin alembic.autogenerate.defaults [alembic.runtime.plugins] loc=plugins.py:37 2026-04-27T08:23:06.814968Z [info ] setup plugin alembic.autogenerate.comments [alembic.runtime.plugins] loc=plugins.py:37 === task_instance (current row) === id=019dce01-ef14-74ff-9cf9-f028420d8a84 try_number=11 state=failed === task_instance_history (one row per past try) === id=019dce01-1899-750b-b37e-c98048cc5340 try_number=1 state=failed id=019dce01-240f-7fc5-9385-65124bab87bd try_number=2 state=failed id=019dce01-3a7f-771c-9865-3ae6ec04aa90 try_number=3 state=failed id=019dce01-51ad-774a-b84e-514d7b6b1f32 try_number=4 state=failed id=019dce01-66c4-7daf-afa0-85737a5c34b6 try_number=5 state=failed id=019dce01-7d62-78bf-a1b2-c441b09cf032 try_number=6 state=failed id=019dce01-922b-7790-8b4b-79f608209506 try_number=7 state=failed id=019dce01-aad4-7e5e-8895-eb3353ab81af try_number=8 state=failed id=019dce01-c0d1-7135-b7dc-67ee806c386f try_number=9 state=failed id=019dce01-d809-7f47-8f47-302231b38f2b try_number=10 state=failed Total tries: 11 Unique UUIDs: 1 ``` Take this as example: - Try 1 runs, writes remote_job_id = "app_001" — stored under UUID `aaa` - Try 1 fails - Try 2 starts — it has UUID `bbb` - Try 2 looks up remote_job_id under UUID `bbb` and finds nothing - Try 2 submits a new job instead of reconnecting to app_001 - With `(dag_run_id, task_id, map_index)` as the key, all retries share the same rows because that triple combination never changes between tries of the same task within a run This is how that looks: - Try 1 writes remote_job_id = "app_001" under `(dag_run_id=42, task_id="submit_spark", map_index=-1)` - Try 1 fails - Try 2 starts — same DAG run, same task, same map index -> same key `(42, "submit_spark", -1)` - Try 2 looks up `remote_job_id` → finds "app_001" - Try 2 reconnects to the existing job instead of submitting a new one -- 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]
