You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "amoghrajesh (via GitHub)" <gi...@apache.org> on 2024/12/13 09:58:09 UTC

[PR] AIP-72: Handling up_for_retry task instance states [airflow]

amoghrajesh opened a new pull request, #44907:
URL: https://github.com/apache/airflow/pull/44907

   <!--
    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.
    -->
   
   <!--
   Thank you for contributing! 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.
   
   Feel free to ping committers for the review!
   
   In case of an existing issue, reference it using one of the following:
   
   closes: #ISSUE
   related: #ISSUE
   
   How to write a good git commit message:
   http://chris.beams.io/posts/git-commit/
   -->
   
   
   
   <!-- Please keep an empty line above the dashes. -->
   ---
   **^ Add meaningful description above**
   Read the **[Pull Request Guidelines](https://github.com/apache/airflow/blob/main/contributing-docs/05_pull_requests.rst#pull-request-guidelines)** for more information.
   In case of fundamental code changes, an Airflow Improvement Proposal ([AIP](https://cwiki.apache.org/confluence/display/AIRFLOW/Airflow+Improvement+Proposals)) is needed.
   In case of a new dependency, check compliance with the [ASF 3rd Party License Policy](https://www.apache.org/legal/resolved.html#category-x).
   In case of backwards incompatible changes please leave a note in a newsfragment file, named `{pr_number}.significant.rst` or `{issue_number}.significant.rst`, in [newsfragments](https://github.com/apache/airflow/tree/main/newsfragments).
   


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1890248249


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -228,7 +230,28 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.get(TI, ti_id_str)
+        actual_start_date = timezone.utcnow()
+        # add changes to TaskReschedule table to the session

Review Comment:
   Removed



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1887976353


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -33,9 +33,11 @@
     TIDeferredStatePayload,
     TIEnterRunningPayload,
     TIHeartbeatInfo,
+    TIRescheduleStatePayload,
     TIStateUpdate,
     TITerminalStatePayload,
 )
+from airflow.models import TaskReschedule

Review Comment:
   Sure, handled



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1889732973


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -224,7 +226,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.get(TI, ti_id_str)
+        actual_start_date = timezone.utcnow()
+        # add changes to TaskReschedule table to the session
+        session.add(
+            TaskReschedule(
+                task_instance.task_id,
+                task_instance.dag_id,
+                task_instance.run_id,
+                task_instance.try_number,
+                actual_start_date,
+                task_instance.end_date,

Review Comment:
   Just ran an example and realised that. I will fix it.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1886674813


##########
task_sdk/src/airflow/sdk/api/client.py:
##########
@@ -134,6 +135,13 @@ def defer(self, id: uuid.UUID, msg):
         # Create a deferred state payload from msg
         self.client.patch(f"task-instances/{id}/state", content=body.model_dump_json())
 
+    def reschedule(self, id: uuid.UUID, msg):
+        """Tell the API server that this TI has been reschduled."""
+        body = TIRescheduleStatePayload(**msg.model_dump(exclude_unset=True))

Review Comment:
   WDYT @kaxil ?



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1890075601


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -228,7 +230,28 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.get(TI, ti_id_str)
+        actual_start_date = timezone.utcnow()
+        # add changes to TaskReschedule table to the session

Review Comment:
   ```suggestion
   ```



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1888293743


##########
task_sdk/src/airflow/sdk/api/client.py:
##########
@@ -134,6 +135,13 @@ def defer(self, id: uuid.UUID, msg):
         # Create a deferred state payload from msg
         self.client.patch(f"task-instances/{id}/state", content=body.model_dump_json())
 
+    def reschedule(self, id: uuid.UUID, msg):
+        """Tell the API server that this TI has been reschduled."""
+        body = TIRescheduleStatePayload(**msg.model_dump(exclude_unset=True))

Review Comment:
   Yeah, I go back and forth on it.
   
   `RescheduleTask` is an easy one because it currently has only one attribute: `reschedule_date`, which is needed from `Task Runner` -> `Supervisor`, so it can be passed an explicit argument.
   
   But for something like defer it has `classpath`, `trigger_kwargs`, `next_method` and `trigger_timeout`, so we just need to decide whether we want to keep client code decoupled. The downside is some code duplication
   
   We don't strictly need to do that, as the `payload` expected by various endpoints, and therefore for client, are all in `_generated.py` which are inherited in `comms.py` anyway. So there is already a coupling of "comm messages" between Supervisor & Task Runner with the Some of the Payloads.
   
   ----
   
   But regardless of that, yes we should have the typehints.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1890244581


##########
tests/api_fastapi/execution_api/routes/test_task_instances.py:
##########
@@ -286,6 +286,57 @@ def test_ti_update_state_to_deferred(self, client, session, create_task_instance
         assert t[0].classpath == "my-classpath"
         assert t[0].kwargs == {"key": "value"}
 
+    def test_ti_update_state_to_reschedule(self, client, session, create_task_instance, time_machine):
+        """
+        Test that tests if the transition to reschedule state is handled correctly.
+        """
+
+        from math import ceil
+
+        instant = timezone.datetime(2024, 10, 30)
+        time_machine.move_to(instant, tick=False)
+
+        ti = create_task_instance(
+            task_id="test_ti_update_state_to_reschedule",
+            state=State.RUNNING,
+            session=session,
+        )
+        ti.start_date = instant
+        ti.end_date = DEFAULT_END_DATE

Review Comment:
   Good call. removing



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1886673852


##########
task_sdk/src/airflow/sdk/api/client.py:
##########
@@ -134,6 +135,13 @@ def defer(self, id: uuid.UUID, msg):
         # Create a deferred state payload from msg
         self.client.patch(f"task-instances/{id}/state", content=body.model_dump_json())
 
+    def reschedule(self, id: uuid.UUID, msg):
+        """Tell the API server that this TI has been reschduled."""
+        body = TIRescheduleStatePayload(**msg.model_dump(exclude_unset=True))

Review Comment:
   What is msg here? It should probably have a type, or if it's just the datetime pass that in directly and create the message type in this function (similar to how we do for `heartbeat` and `finish` methods. We should likely have done `defer` differently too. Maybe.)



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1887525189


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -147,7 +149,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.get(TI, (ti_id_str,))

Review Comment:
   ```suggestion
           task_instance = session.get(TI, ti_id_str)
   ```



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh merged PR #44907:
URL: https://github.com/apache/airflow/pull/44907


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1888263391


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -224,7 +226,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.get(TI, ti_id_str)
+        actual_start_date = timezone.utcnow()
+        # add changes to TaskReschedule table to the session
+        session.add(
+            TaskReschedule(
+                task_instance.task_id,
+                task_instance.dag_id,
+                task_instance.run_id,
+                task_instance.try_number,
+                actual_start_date,
+                task_instance.end_date,

Review Comment:
   Hmm? How would be `end_date`?
   
   Task was set to running in `run` endpoint, it was then running via Supervisor/Task Runner, now it caught `AirflowRescheduleException`, so it will now set its state to `UP_FOR_RESCHEDULE` along with all the other things on TI table  like `end_date, duration`.
   
   You would need something similar to 
   https://github.com/apache/airflow/blob/38201cf1cfa23a6a363c31f66c39529a2b9b7ae3/airflow/api_fastapi/execution_api/routes/task_instances.py#L205



##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -224,7 +226,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.get(TI, ti_id_str)
+        actual_start_date = timezone.utcnow()
+        # add changes to TaskReschedule table to the session
+        session.add(
+            TaskReschedule(
+                task_instance.task_id,
+                task_instance.dag_id,
+                task_instance.run_id,
+                task_instance.try_number,
+                actual_start_date,
+                task_instance.end_date,

Review Comment:
   Hmm? How would `end_date` have any value here?
   
   Task was set to running in `run` endpoint, it was then running via Supervisor/Task Runner, now it caught `AirflowRescheduleException`, so it will now set its state to `UP_FOR_RESCHEDULE` along with all the other things on TI table  like `end_date, duration`.
   
   You would need something similar to 
   https://github.com/apache/airflow/blob/38201cf1cfa23a6a363c31f66c39529a2b9b7ae3/airflow/api_fastapi/execution_api/routes/task_instances.py#L205



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling up_for_retry task instance states [airflow]

Posted by "ephraimbuddy (via GitHub)" <gi...@apache.org>.
ephraimbuddy commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1884378885


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -147,7 +149,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.scalar(select(TI).where(TI.id == ti_id_str))
+        actual_start_date = timezone.utcnow()
+        # update the task_reschedule table first
+        session.add(
+            TaskReschedule(

Review Comment:
   Are we creating a new TaskReschedule here or updating as per comment on line 155?



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling up_for_retry task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1884381482


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -147,7 +149,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.scalar(select(TI).where(TI.id == ti_id_str))
+        actual_start_date = timezone.utcnow()
+        # update the task_reschedule table first
+        session.add(
+            TaskReschedule(

Review Comment:
   We are creating a new one here. By update i meant, handle the TaskReschedule first. I will change the comment to word it better 



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1890061756


##########
airflow/models/taskreschedule.py:
##########
@@ -21,7 +21,17 @@
 
 from typing import TYPE_CHECKING
 
-from sqlalchemy import Column, ForeignKeyConstraint, Index, Integer, String, asc, desc, select, text
+from sqlalchemy import (
+    Column,
+    ForeignKeyConstraint,
+    Index,
+    Integer,
+    String,
+    asc,
+    desc,
+    select,
+    text,
+)

Review Comment:
   unrelated 



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1890247666


##########
tests/api_fastapi/execution_api/routes/test_task_instances.py:
##########
@@ -286,6 +286,57 @@ def test_ti_update_state_to_deferred(self, client, session, create_task_instance
         assert t[0].classpath == "my-classpath"
         assert t[0].kwargs == {"key": "value"}
 
+    def test_ti_update_state_to_reschedule(self, client, session, create_task_instance, time_machine):
+        """
+        Test that tests if the transition to reschedule state is handled correctly.
+        """
+
+        from math import ceil
+
+        instant = timezone.datetime(2024, 10, 30)
+        time_machine.move_to(instant, tick=False)
+
+        ti = create_task_instance(
+            task_id="test_ti_update_state_to_reschedule",
+            state=State.RUNNING,
+            session=session,
+        )
+        ti.start_date = instant
+        ti.end_date = DEFAULT_END_DATE
+        session.commit()
+
+        payload = {
+            "state": "up_for_reschedule",
+            "reschedule_date": timezone.parse("2025-1-1T12:00:00Z").isoformat(),

Review Comment:
   Yeah, haha. I was keeping the reschedule date to a much later date on purpose. Let me move it closer
   
   > Why this? i.e. string to date to string?
   No specific reason, I will just directly use the string. Direclty passing string now



##########
tests/api_fastapi/execution_api/routes/test_task_instances.py:
##########
@@ -286,6 +286,57 @@ def test_ti_update_state_to_deferred(self, client, session, create_task_instance
         assert t[0].classpath == "my-classpath"
         assert t[0].kwargs == {"key": "value"}
 
+    def test_ti_update_state_to_reschedule(self, client, session, create_task_instance, time_machine):
+        """
+        Test that tests if the transition to reschedule state is handled correctly.
+        """
+
+        from math import ceil
+
+        instant = timezone.datetime(2024, 10, 30)
+        time_machine.move_to(instant, tick=False)
+
+        ti = create_task_instance(
+            task_id="test_ti_update_state_to_reschedule",
+            state=State.RUNNING,
+            session=session,
+        )
+        ti.start_date = instant
+        ti.end_date = DEFAULT_END_DATE
+        session.commit()
+
+        payload = {
+            "state": "up_for_reschedule",
+            "reschedule_date": timezone.parse("2025-1-1T12:00:00Z").isoformat(),

Review Comment:
   Yeah, haha. I was keeping the reschedule date to a much later date on purpose. Let me move it closer
   
   > Why this? i.e. string to date to string?
   
   No specific reason, I will just directly use the string. Direclty passing string now



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1886669634


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -147,7 +149,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.scalar(select(TI).where(TI.id == ti_id_str))

Review Comment:
   Sqla has a method for getting rows my their primary key -- we should use that here.
   ```suggestion
           task_instance = session.get(TI, (ti_id_str,))
   ```



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1890255159


##########
tests/api_fastapi/execution_api/routes/test_task_instances.py:
##########
@@ -286,6 +286,57 @@ def test_ti_update_state_to_deferred(self, client, session, create_task_instance
         assert t[0].classpath == "my-classpath"
         assert t[0].kwargs == {"key": "value"}
 
+    def test_ti_update_state_to_reschedule(self, client, session, create_task_instance, time_machine):
+        """
+        Test that tests if the transition to reschedule state is handled correctly.
+        """
+
+        from math import ceil
+
+        instant = timezone.datetime(2024, 10, 30)
+        time_machine.move_to(instant, tick=False)
+
+        ti = create_task_instance(
+            task_id="test_ti_update_state_to_reschedule",
+            state=State.RUNNING,
+            session=session,
+        )
+        ti.start_date = instant
+        ti.end_date = DEFAULT_END_DATE
+        session.commit()
+
+        payload = {
+            "state": "up_for_reschedule",
+            "reschedule_date": timezone.parse("2025-1-1T12:00:00Z").isoformat(),

Review Comment:
   Handled in b8f81e53ff810537c0b03041f3840f9996bfafce



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1886671257


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -147,7 +149,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.scalar(select(TI).where(TI.id == ti_id_str))
+        actual_start_date = timezone.utcnow()
+        # add changes to TaskReschedule table to the session
+        session.add(
+            TaskReschedule(
+                task_instance.task_id,
+                task_instance.dag_id,
+                task_instance.run_id,
+                task_instance.try_number,
+                actual_start_date,
+                task_instance.end_date,
+                ti_patch_payload.reschedule_date,
+                task_instance.map_index,
+            )
+        )
 
+        query = update(TI).where(TI.id == ti_id_str)
+        # clear the next_method and next_kwargs so that none of the retries pick them up
+        query = query.values(state=State.UP_FOR_RESCHEDULE, next_method=None, next_kwargs=None)

Review Comment:
   We've already got the TI row loaded, so lets do this:
   ```suggestion
           ti.state = State.UP_FOR_RESCHEDULE
           ti.next_method = None
           ti.next_kwargs = None
           session.flush()
   ```



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1887976860


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -147,7 +149,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.get(TI, (ti_id_str,))

Review Comment:
   Oh yea. Handled



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1889747947


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -224,7 +226,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.get(TI, ti_id_str)
+        actual_start_date = timezone.utcnow()
+        # add changes to TaskReschedule table to the session
+        session.add(
+            TaskReschedule(
+                task_instance.task_id,
+                task_instance.dag_id,
+                task_instance.run_id,
+                task_instance.try_number,
+                actual_start_date,
+                task_instance.end_date,

Review Comment:
   Fixed in 7cc6e9d86c1245100a56c9e78fb82269779cb827, should be good now. Added in the test to validate too



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1886756035


##########
task_sdk/src/airflow/sdk/api/client.py:
##########
@@ -134,6 +135,13 @@ def defer(self, id: uuid.UUID, msg):
         # Create a deferred state payload from msg
         self.client.patch(f"task-instances/{id}/state", content=body.model_dump_json())
 
+    def reschedule(self, id: uuid.UUID, msg):
+        """Tell the API server that this TI has been reschduled."""
+        body = TIRescheduleStatePayload(**msg.model_dump(exclude_unset=True))

Review Comment:
   Its of `RescheduleTask` class. The reason we just passed it down to this api endpoint is so that we do not need to worry about unpacking the object in the `handle_requests`. 
   ```
           elif isinstance(msg, RescheduleTask):
               self._terminal_state = IntermediateTIState.UP_FOR_RESCHEDULE
               self.client.task_instances.reschedule(self.id, msg)
               resp = None
   ```
   
   But a type can be added for sure. Let me do that



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1888265733


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -224,7 +226,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.get(TI, ti_id_str)
+        actual_start_date = timezone.utcnow()
+        # add changes to TaskReschedule table to the session
+        session.add(
+            TaskReschedule(
+                task_instance.task_id,
+                task_instance.dag_id,
+                task_instance.run_id,
+                task_instance.try_number,
+                actual_start_date,
+                task_instance.end_date,

Review Comment:
   You would need 
   
   https://github.com/apache/airflow/blob/8848d6e35f6ead2cc1ed9887af8360fbd61c78c3/airflow/models/taskinstance.py#L1621-L1630



##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -224,7 +226,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.get(TI, ti_id_str)
+        actual_start_date = timezone.utcnow()
+        # add changes to TaskReschedule table to the session
+        session.add(
+            TaskReschedule(
+                task_instance.task_id,
+                task_instance.dag_id,
+                task_instance.run_id,
+                task_instance.try_number,
+                actual_start_date,
+                task_instance.end_date,

Review Comment:
   Similar to how it is handled today
   
   https://github.com/apache/airflow/blob/8848d6e35f6ead2cc1ed9887af8360fbd61c78c3/airflow/models/taskinstance.py#L1621-L1630



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1887524739


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -33,9 +33,11 @@
     TIDeferredStatePayload,
     TIEnterRunningPayload,
     TIHeartbeatInfo,
+    TIRescheduleStatePayload,
     TIStateUpdate,
     TITerminalStatePayload,
 )
+from airflow.models import TaskReschedule

Review Comment:
   ```suggestion
   from airflow.models.taskreschedule import TaskReschedule
   ```
   
   for consistency



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1890073332


##########
tests/api_fastapi/execution_api/routes/test_task_instances.py:
##########
@@ -286,6 +286,57 @@ def test_ti_update_state_to_deferred(self, client, session, create_task_instance
         assert t[0].classpath == "my-classpath"
         assert t[0].kwargs == {"key": "value"}
 
+    def test_ti_update_state_to_reschedule(self, client, session, create_task_instance, time_machine):
+        """
+        Test that tests if the transition to reschedule state is handled correctly.
+        """
+
+        from math import ceil
+
+        instant = timezone.datetime(2024, 10, 30)
+        time_machine.move_to(instant, tick=False)
+
+        ti = create_task_instance(
+            task_id="test_ti_update_state_to_reschedule",
+            state=State.RUNNING,
+            session=session,
+        )
+        ti.start_date = instant
+        ti.end_date = DEFAULT_END_DATE
+        session.commit()
+
+        payload = {
+            "state": "up_for_reschedule",
+            "reschedule_date": timezone.parse("2025-1-1T12:00:00Z").isoformat(),

Review Comment:
   Why this? i.e. string to date to string?
   
   also the start date vs reschedule date vs end date are poles apart :) 
   
   



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1889660017


##########
task_sdk/src/airflow/sdk/api/client.py:
##########
@@ -134,6 +135,13 @@ def defer(self, id: uuid.UUID, msg):
         # Create a deferred state payload from msg
         self.client.patch(f"task-instances/{id}/state", content=body.model_dump_json())
 
+    def reschedule(self, id: uuid.UUID, msg):
+        """Tell the API server that this TI has been reschduled."""
+        body = TIRescheduleStatePayload(**msg.model_dump(exclude_unset=True))

Review Comment:
   Typehints are already added. Maybe in a follow up we can decide and perform some cleanup.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1889728215


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -224,7 +226,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.get(TI, ti_id_str)
+        actual_start_date = timezone.utcnow()
+        # add changes to TaskReschedule table to the session
+        session.add(
+            TaskReschedule(
+                task_instance.task_id,
+                task_instance.dag_id,
+                task_instance.run_id,
+                task_instance.try_number,
+                actual_start_date,
+                task_instance.end_date,

Review Comment:
   >we do not need that to calculate the duration because the model itself has logic to do that as long as rthe start and end dates are set properly. I have adding a unit test that can validate this part as well. https://github.com/apache/airflow/blob/main/airflow/models/taskreschedule.py#L95-L98
   
   That is TaskReschedule, but we need duration for TI



##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -224,7 +226,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.get(TI, ti_id_str)
+        actual_start_date = timezone.utcnow()
+        # add changes to TaskReschedule table to the session
+        session.add(
+            TaskReschedule(
+                task_instance.task_id,
+                task_instance.dag_id,
+                task_instance.run_id,
+                task_instance.try_number,
+                actual_start_date,
+                task_instance.end_date,

Review Comment:
   >we do not need that to calculate the duration because the model itself has logic to do that as long as rthe start and end dates are set properly. I have adding a unit test that can validate this part as well. https://github.com/apache/airflow/blob/main/airflow/models/taskreschedule.py#L95-L98
   
   That is TaskReschedule, but we need duration for TI too



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1884385082


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -147,7 +149,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.scalar(select(TI).where(TI.id == ti_id_str))
+        actual_start_date = timezone.utcnow()
+        # update the task_reschedule table first
+        session.add(
+            TaskReschedule(

Review Comment:
   Should be better now @ephraimbuddy
   Handled in 53c80679d878ad2f6ad9f70fb4f2304176591b1e



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on PR #44907:
URL: https://github.com/apache/airflow/pull/44907#issuecomment-2551453265

   @kaxil merging this one. I think I have handled every comment


-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1890243629


##########
tests/api_fastapi/execution_api/routes/test_task_instances.py:
##########
@@ -286,6 +286,57 @@ def test_ti_update_state_to_deferred(self, client, session, create_task_instance
         assert t[0].classpath == "my-classpath"
         assert t[0].kwargs == {"key": "value"}
 
+    def test_ti_update_state_to_reschedule(self, client, session, create_task_instance, time_machine):
+        """
+        Test that tests if the transition to reschedule state is handled correctly.
+        """
+
+        from math import ceil
+
+        instant = timezone.datetime(2024, 10, 30)
+        time_machine.move_to(instant, tick=False)
+
+        ti = create_task_instance(
+            task_id="test_ti_update_state_to_reschedule",
+            state=State.RUNNING,
+            session=session,
+        )
+        ti.start_date = instant
+        ti.end_date = DEFAULT_END_DATE
+        session.commit()
+
+        payload = {
+            "state": "up_for_reschedule",
+            "reschedule_date": timezone.parse("2025-1-1T12:00:00Z").isoformat(),
+            "end_date": DEFAULT_END_DATE.isoformat(),
+        }
+
+        response = client.patch(f"/execution/task-instances/{ti.id}/state", json=payload)
+
+        assert response.status_code == 204
+        assert response.text == ""
+
+        session.expire_all()
+
+        tis = session.query(TaskInstance).all()
+        assert len(tis) == 1
+        assert tis[0].state == TaskInstanceState.UP_FOR_RESCHEDULE
+        assert tis[0].next_method is None
+        assert tis[0].next_kwargs is None
+        assert ceil(tis[0].duration) == 129600
+
+        trs = session.query(TaskReschedule).all()
+        assert len(trs) == 1
+        assert trs[0].dag_id == "dag"
+        assert trs[0].task_id == "test_ti_update_state_to_reschedule"
+        assert trs[0].run_id == "test"
+        assert trs[0].try_number == 0
+        assert trs[0].start_date == instant
+        assert trs[0].end_date == DEFAULT_END_DATE
+        assert trs[0].reschedule_date == timezone.parse("2025-1-1T12:00:00Z")
+        assert trs[0].map_index == -1
+        assert ceil(trs[0].duration) == 129600

Review Comment:
   Handled



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1890243211


##########
airflow/models/taskreschedule.py:
##########
@@ -21,7 +21,17 @@
 
 from typing import TYPE_CHECKING
 
-from sqlalchemy import Column, ForeignKeyConstraint, Index, Integer, String, asc, desc, select, text
+from sqlalchemy import (
+    Column,
+    ForeignKeyConstraint,
+    Index,
+    Integer,
+    String,
+    asc,
+    desc,
+    select,
+    text,
+)

Review Comment:
   Removed



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1890069049


##########
tests/api_fastapi/execution_api/routes/test_task_instances.py:
##########
@@ -286,6 +286,57 @@ def test_ti_update_state_to_deferred(self, client, session, create_task_instance
         assert t[0].classpath == "my-classpath"
         assert t[0].kwargs == {"key": "value"}
 
+    def test_ti_update_state_to_reschedule(self, client, session, create_task_instance, time_machine):
+        """
+        Test that tests if the transition to reschedule state is handled correctly.
+        """
+
+        from math import ceil
+
+        instant = timezone.datetime(2024, 10, 30)
+        time_machine.move_to(instant, tick=False)
+
+        ti = create_task_instance(
+            task_id="test_ti_update_state_to_reschedule",
+            state=State.RUNNING,
+            session=session,
+        )
+        ti.start_date = instant
+        ti.end_date = DEFAULT_END_DATE
+        session.commit()
+
+        payload = {
+            "state": "up_for_reschedule",
+            "reschedule_date": timezone.parse("2025-1-1T12:00:00Z").isoformat(),
+            "end_date": DEFAULT_END_DATE.isoformat(),
+        }
+
+        response = client.patch(f"/execution/task-instances/{ti.id}/state", json=payload)
+
+        assert response.status_code == 204
+        assert response.text == ""
+
+        session.expire_all()
+
+        tis = session.query(TaskInstance).all()
+        assert len(tis) == 1
+        assert tis[0].state == TaskInstanceState.UP_FOR_RESCHEDULE
+        assert tis[0].next_method is None
+        assert tis[0].next_kwargs is None
+        assert ceil(tis[0].duration) == 129600
+
+        trs = session.query(TaskReschedule).all()
+        assert len(trs) == 1
+        assert trs[0].dag_id == "dag"
+        assert trs[0].task_id == "test_ti_update_state_to_reschedule"
+        assert trs[0].run_id == "test"
+        assert trs[0].try_number == 0
+        assert trs[0].start_date == instant
+        assert trs[0].end_date == DEFAULT_END_DATE
+        assert trs[0].reschedule_date == timezone.parse("2025-1-1T12:00:00Z")
+        assert trs[0].map_index == -1
+        assert ceil(trs[0].duration) == 129600

Review Comment:
   We shouldn't need `ceil` anymore



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1890073332


##########
tests/api_fastapi/execution_api/routes/test_task_instances.py:
##########
@@ -286,6 +286,57 @@ def test_ti_update_state_to_deferred(self, client, session, create_task_instance
         assert t[0].classpath == "my-classpath"
         assert t[0].kwargs == {"key": "value"}
 
+    def test_ti_update_state_to_reschedule(self, client, session, create_task_instance, time_machine):
+        """
+        Test that tests if the transition to reschedule state is handled correctly.
+        """
+
+        from math import ceil
+
+        instant = timezone.datetime(2024, 10, 30)
+        time_machine.move_to(instant, tick=False)
+
+        ti = create_task_instance(
+            task_id="test_ti_update_state_to_reschedule",
+            state=State.RUNNING,
+            session=session,
+        )
+        ti.start_date = instant
+        ti.end_date = DEFAULT_END_DATE
+        session.commit()
+
+        payload = {
+            "state": "up_for_reschedule",
+            "reschedule_date": timezone.parse("2025-1-1T12:00:00Z").isoformat(),

Review Comment:
   Why this? i.e. string to date to string?
   
   also the start date vs reschedule date vs end date are poles apart :) 
   
   
   ```
   DEFAULT_START_DATE = timezone.parse("2024-10-31T11:00:00Z")
   DEFAULT_END_DATE = timezone.parse("2024-10-31T12:00:00Z")
   reschedule_date = timezone.parse("2025-1-1T12:00:00Z")
   ```
   
   



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "kaxil (via GitHub)" <gi...@apache.org>.
kaxil commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1890070376


##########
tests/api_fastapi/execution_api/routes/test_task_instances.py:
##########
@@ -286,6 +286,57 @@ def test_ti_update_state_to_deferred(self, client, session, create_task_instance
         assert t[0].classpath == "my-classpath"
         assert t[0].kwargs == {"key": "value"}
 
+    def test_ti_update_state_to_reschedule(self, client, session, create_task_instance, time_machine):
+        """
+        Test that tests if the transition to reschedule state is handled correctly.
+        """
+
+        from math import ceil
+
+        instant = timezone.datetime(2024, 10, 30)
+        time_machine.move_to(instant, tick=False)
+
+        ti = create_task_instance(
+            task_id="test_ti_update_state_to_reschedule",
+            state=State.RUNNING,
+            session=session,
+        )
+        ti.start_date = instant
+        ti.end_date = DEFAULT_END_DATE

Review Comment:
   Hmm this shouldn't be set already since task is marked as running



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "ashb (via GitHub)" <gi...@apache.org>.
ashb commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1886671257


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -147,7 +149,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.scalar(select(TI).where(TI.id == ti_id_str))
+        actual_start_date = timezone.utcnow()
+        # add changes to TaskReschedule table to the session
+        session.add(
+            TaskReschedule(
+                task_instance.task_id,
+                task_instance.dag_id,
+                task_instance.run_id,
+                task_instance.try_number,
+                actual_start_date,
+                task_instance.end_date,
+                ti_patch_payload.reschedule_date,
+                task_instance.map_index,
+            )
+        )
 
+        query = update(TI).where(TI.id == ti_id_str)
+        # clear the next_method and next_kwargs so that none of the retries pick them up
+        query = query.values(state=State.UP_FOR_RESCHEDULE, next_method=None, next_kwargs=None)

Review Comment:
   We've already got the TI row loaded, so lets do this:
   ```suggestion
           ti.state = State.UP_FOR_RESCHEDULE
           ti.next_method = None
           ti.next_kwargs = None
           session.flush()
   ```



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1886739232


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -147,7 +149,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.scalar(select(TI).where(TI.id == ti_id_str))

Review Comment:
   Oh nice. Thanks.
   Made the change



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1888813509


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -224,7 +226,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.get(TI, ti_id_str)
+        actual_start_date = timezone.utcnow()
+        # add changes to TaskReschedule table to the session
+        session.add(
+            TaskReschedule(
+                task_instance.task_id,
+                task_instance.dag_id,
+                task_instance.run_id,
+                task_instance.try_number,
+                actual_start_date,
+                task_instance.end_date,

Review Comment:
   Thanks for the point. I agree with you that we need "end_date". I have changed the code now to handle it like it is handled for the `TITerminalStatePayload` related states.
   
   As for adding query like `query = TI.duration_expression_update(ti_patch_payload.end_date, query, session.bind) `, we do not need that to calculate the duration because the model itself has logic to do that as long as rthe start and end dates are set properly. I have adding a unit test that can validate this part as well. https://github.com/apache/airflow/blob/main/airflow/models/taskreschedule.py#L95-L98



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1888815180


##########
airflow/api_fastapi/execution_api/routes/task_instances.py:
##########
@@ -224,7 +226,26 @@ def ti_update_state(
             next_kwargs=ti_patch_payload.trigger_kwargs,
             trigger_timeout=timeout,
         )
+    elif isinstance(ti_patch_payload, TIRescheduleStatePayload):
+        task_instance = session.get(TI, ti_id_str)
+        actual_start_date = timezone.utcnow()
+        # add changes to TaskReschedule table to the session
+        session.add(
+            TaskReschedule(
+                task_instance.task_id,
+                task_instance.dag_id,
+                task_instance.run_id,
+                task_instance.try_number,
+                actual_start_date,
+                task_instance.end_date,

Review Comment:
   https://github.com/apache/airflow/pull/44907/commits/80f9ddc59d60ea1f6c6e2ee260845d0b4736d5b2 handles these scenarios. I also realised that there was no UT on the task_runner side. I added that as well.



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] AIP-72: Handling "up_for_reschedule" task instance states [airflow]

Posted by "amoghrajesh (via GitHub)" <gi...@apache.org>.
amoghrajesh commented on code in PR #44907:
URL: https://github.com/apache/airflow/pull/44907#discussion_r1890324340


##########
task_sdk/src/airflow/sdk/api/client.py:
##########
@@ -134,6 +135,13 @@ def defer(self, id: uuid.UUID, msg):
         # Create a deferred state payload from msg
         self.client.patch(f"task-instances/{id}/state", content=body.model_dump_json())
 
+    def reschedule(self, id: uuid.UUID, msg):
+        """Tell the API server that this TI has been reschduled."""
+        body = TIRescheduleStatePayload(**msg.model_dump(exclude_unset=True))

Review Comment:
   Resolving for now, will revisit during cleanup



-- 
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: commits-unsubscribe@airflow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org