You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "gyli (via GitHub)" <gi...@apache.org> on 2024/07/14 22:08:33 UTC

[PR] fix: update updated_at when saving to db as session.merge does not tr… [airflow]

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

   fixes: https://github.com/apache/airflow/issues/40623
   
   Tested with query
   ```
   select start_date, end_date, updated_at from task_instance where task_id = 'sleep' ORDER BY start_date DESC;
   ```
   The lower result is the initial test with example provided in the issue (sleep time is reduced from 50 to 5), showing `updated_at` is before `end_date`.
   The upper result is the log after this fix, showing `updated_at` is now after `end_date`.
   <img width="1007" alt="Screenshot 2024-07-14 at 6 02 47 PM" src="https://github.com/user-attachments/assets/7cc02eb3-4e65-42c7-8638-cab9e7a70443">
   
   I have also tried [flag_modified](https://docs.sqlalchemy.org/en/14/orm/session_api.html#sqlalchemy.orm.attributes.flag_modified), while it does not work at the same place.
   
   
   <!--
    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] fix: update updated_at when saving to db as session.merge does not tr… [airflow]

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


##########
airflow/models/taskinstance.py:
##########
@@ -3244,6 +3244,7 @@ def fetch_handle_failure_context(
     @provide_session
     def save_to_db(ti: TaskInstance | TaskInstancePydantic, session: Session = NEW_SESSION):
         ti = _coalesce_to_orm_ti(ti=ti, session=session)
+        ti.updated_at = timezone.utcnow()

Review Comment:
   In that case, don't we want to simply align the value of `updated_at` with the value of `end_date`? (i.e, `ti.updated_at = ti.end_date`) 
   What is the purpose of `utcnow()` as the value of `updated_at`? It will be a just date in a delay of some miliseconds after the `end_date` - what is special about this value?



-- 
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] Update `updated_at` when saving TI to DB [airflow]

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


##########
airflow/models/taskinstance.py:
##########
@@ -3244,6 +3244,7 @@ def fetch_handle_failure_context(
     @provide_session
     def save_to_db(ti: TaskInstance | TaskInstancePydantic, session: Session = NEW_SESSION):
         ti = _coalesce_to_orm_ti(ti=ti, session=session)
+        ti.updated_at = timezone.utcnow()

Review Comment:
   Thanks for the clarification! It makes sense.
   LGTM then.



-- 
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] Update `updated_at` when saving TI to DB [airflow]

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


##########
airflow/models/taskinstance.py:
##########
@@ -3244,6 +3244,7 @@ def fetch_handle_failure_context(
     @provide_session
     def save_to_db(ti: TaskInstance | TaskInstancePydantic, session: Session = NEW_SESSION):
         ti = _coalesce_to_orm_ti(ti=ti, session=session)
+        ti.updated_at = timezone.utcnow()

Review Comment:
   I can confirm the same issue happens even if the task is not SKIPPED or SUCCESS. 
   Here is an example result when testing with a task that only raises AirflowTaskTimeout, showing `updated_at` is smaller than `end_date`. 
   <img width="1126" alt="Screenshot 2024-07-17 at 12 19 35 AM" src="https://github.com/user-attachments/assets/79deeffb-c623-4d4b-82f3-97e97b68fed9">
   
   Since `save_to_db` is also called when the task is skipped, timeout, or failed with other states, I believe it's a better and easier place fixing this issue.



-- 
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] Update `updated_at` when saving TI to DB [airflow]

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


-- 
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] Update `updated_at` when saving TI to DB [airflow]

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


##########
airflow/models/taskinstance.py:
##########
@@ -3244,6 +3244,7 @@ def fetch_handle_failure_context(
     @provide_session
     def save_to_db(ti: TaskInstance | TaskInstancePydantic, session: Session = NEW_SESSION):
         ti = _coalesce_to_orm_ti(ti=ti, session=session)
+        ti.updated_at = timezone.utcnow()

Review Comment:
   Firstly I think `save_to_db` is the best place to fix this issue to make sure all db writes have updated `updated_at`. Also, `save_to_db` is not only called when task ended, but also before the starting of the task and failed due to timeout or other reasons (see `_run_raw_task`), and end_date could be empty in some of those cases.



-- 
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] Update `updated_at` when saving TI to DB [airflow]

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

   Hi @shahar1 and @romsharon98, could you help me unblock the failed check here? 
   The failed unit tests seem unrelated to my change, and they both pass on my local when I do `breeze shell --force-lowest-dependencies` and then `pytest tests/triggers/test_external_task.py`.


-- 
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] Update `updated_at` when saving TI to DB [airflow]

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

   > Hi @shahar1 and @romsharon98, could you help me unblock the failed check here? Does it matter for merging PR? The failed unit tests seem unrelated to my change, and they both pass on my local when I do `breeze shell --force-lowest-dependencies` and then `pytest tests/triggers/test_external_task.py`.
   
   I apologize as it seems like I've missed your last message.
   I think that we're good to go :)


-- 
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] Update `updated_at` when saving TI to DB [airflow]

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


##########
airflow/models/taskinstance.py:
##########
@@ -3244,6 +3244,7 @@ def fetch_handle_failure_context(
     @provide_session
     def save_to_db(ti: TaskInstance | TaskInstancePydantic, session: Session = NEW_SESSION):
         ti = _coalesce_to_orm_ti(ti=ti, session=session)
+        ti.updated_at = timezone.utcnow()

Review Comment:
   I can confirm the same issue happens even if the task is not SKIPPED or SUCCESS. 
   Here is an example result when testing with a task that only raises AirflowTaskTimeout, showing `updated_at` is smaller than `end_date`. 
   <img width="1126" alt="Screenshot 2024-07-17 at 12 19 35 AM" src="https://github.com/user-attachments/assets/79deeffb-c623-4d4b-82f3-97e97b68fed9">
   
   Since `save_to_db` is also called when the task is skipped, timeout, or failed with other states, I believe it's a better and easier place fixing this issue.



-- 
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] Update `updated_at` when saving TI to DB [airflow]

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


##########
airflow/models/taskinstance.py:
##########
@@ -3244,6 +3244,7 @@ def fetch_handle_failure_context(
     @provide_session
     def save_to_db(ti: TaskInstance | TaskInstancePydantic, session: Session = NEW_SESSION):
         ti = _coalesce_to_orm_ti(ti=ti, session=session)
+        ti.updated_at = timezone.utcnow()

Review Comment:
   I can confirm the same issue happens even if the task is not SKIPPED or SUCCESS. 
   Here is an example result when testing with a task that only raises AirflowTaskTimeout, showing `updated_at` is smaller than `end_date`. 
   <img width="1126" alt="Screenshot 2024-07-17 at 12 19 35 AM" src="https://github.com/user-attachments/assets/79deeffb-c623-4d4b-82f3-97e97b68fed9">
   
   Because `save_to_db` is used with all possible return state, I believe it's a better and easier place fixing this issue.



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