You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by "HsiuChuanHsu (via GitHub)" <gi...@apache.org> on 2025/09/01 23:07:41 UTC

[PR] Fix: Preserve TaskInstance history during Kubernetes API rate limiting errors [airflow]

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

   <!--
    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. -->
   ## Description
   This PR fixes issue #49517 where TaskInstanceHistory records were lost when Kubernetes API rate limiting (429 errors) prevented task adoption during scheduler restarts.
   
   ## Problem
   When using KubernetesExecutor or CeleryKubernetesExecutor:
   
   1. Task pods launch successfully but K8s API starts returning 429 errors
   2. KubernetesJobWatcher crashes, causing Scheduler restart
   3. Scheduler fails to re-adopt running pods due to continued 429s
   4. Tasks are marked orphaned with state reset to `None`
   5. TaskInstanceHistory is not recorded since state ≠ `RUNNING`
   6. Airflow UI shows missing log links for failed attempts
   
   ## Solution
   **KubernetesExecutor**: Add 429 error handling to retry logic and detailed logging for adoption failures
   **TaskInstance**: Detect orphaned tasks (`state=None` + `start_date set` + `end_date unset` ) and record TaskInstanceHistory
   
   ## Impact 
   **Before**:
   Task Running → K8s API 429 → Scheduler Restart → Task Orphaned → State Reset to None → 
   No History → Missing UI Logs
   
   **After**:
   Task Running → K8s API 429 → Scheduler Restart → Task Orphaned → State Reset to None → 
   History Recorded → UI Logs Available
   
   Fixes: #49517 
   Related: #49244 
   
   
   ---
   **^ 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 [airflow-core/newsfragments](https://github.com/apache/airflow/tree/main/airflow-core/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: Preserve TaskInstance history during Kubernetes API rate limiting errors - Task Instance Fix [airflow]

Posted by "potiuk (via GitHub)" <gi...@apache.org>.
potiuk closed pull request #55159: Fix: Preserve TaskInstance history during Kubernetes API rate limiting errors - Task Instance Fix
URL: https://github.com/apache/airflow/pull/55159


-- 
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: Preserve TaskInstance history during Kubernetes API rate limiting errors - Task Instance Fix [airflow]

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

   @HsiuChuanHsu This draft PR has had no activity for 2 weeks. Closing to keep the queue clean.
   
   You are welcome to reopen and continue when you're ready. If you'd like to pick it back up, please rebase onto the current `main` branch first.
   
   ---
   
   _Note: This comment was drafted by an AI-assisted triage tool and may contain mistakes. Once you have addressed the points above, an Apache Airflow maintainer — a real person — will take the next look at your PR. We use this [two-stage triage process](https://github.com/apache/airflow/blob/main/contributing-docs/25_maintainer_pr_triage.md#why-the-first-pass-is-automated) so that our maintainers' limited time is spent where it matters most: the conversation with you._


-- 
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: Preserve TaskInstance history during Kubernetes API rate limiting errors [airflow]

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

   Fix looks reasonable but tests don’t agree. This should include a test case 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] Fix: Preserve TaskInstance history during Kubernetes API rate limiting errors [airflow]

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

   When implementing unit tests for the new orphaned task detection logic in the `fetch_handle_failure_context` method of `taskinstance.py`, a critical timing issue was discovered that prevented the logic from functioning correctly.
   
   https://github.com/apache/airflow/blob/bcf51254380209a9c2e260d0cf9407964ba9d012/airflow-core/src/airflow/models/taskinstance.py#L1585
   
   **Original problem**
   - The orphaned task detection relies on the condition: `ti.end_date is None`
   - However, `ti.end_date = timezone.utcnow()` was executed earlier in the method
   - This made the `ti.end_date is None` condition impossible to satisfy
   ```
   def fetch_handle_failure_context(...):
       # ... other code ...
       ti.end_date = timezone.utcnow()  # ← Sets end_date first
       
       # ... later in the method ...
       if ti.state is None and ti.start_date is not None and ti.end_date is None:
           # ← This condition can never be True because end_date was already set above
   
   ```
   
   
   
   **Solution**
   The orphaned task detection logic was moved to execute before the `end_date` assignment:
   ```
   def fetch_handle_failure_context(...):
       # Check for orphaned task BEFORE setting end_date
       if (
           ti.is_eligible_to_retry()
           and ti.state is None
           and ti.start_date is not None
           and ti.end_date is None  # ← Now this can actually be True
       ):
           # Handle orphaned task detection and history recording
       
       # THEN set end_date
       ti.end_date = timezone.utcnow()
   ```
   


-- 
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: Preserve TaskInstance history during Kubernetes API rate limiting errors - Task Instance Fix [airflow]

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

   @eladkal  Just submit a new [PR](https://github.com/apache/airflow/pull/57152) to split the changes from the original one here. 
   Thanks for the reminder!


-- 
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: Preserve TaskInstance history during Kubernetes API rate limiting errors - Task Instance Fix [airflow]

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

   @HsiuChuanHsu This PR has had no activity for 5 weeks. Converting to draft to signal that maintainer review is paused until you resume work.
   
   When you're ready to continue, please rebase onto the current `main` branch, address any newly-appearing CI failures, and mark the PR as "Ready for review" again. There is no rush.
   
   ---
   
   _Note: This comment was drafted by an AI-assisted triage tool and may contain mistakes. Once you have addressed the points above, an Apache Airflow maintainer — a real person — will take the next look at your PR. We use this [two-stage triage process](https://github.com/apache/airflow/blob/main/contributing-docs/25_maintainer_pr_triage.md#why-the-first-pass-is-automated) so that our maintainers' limited time is spent where it matters most: the conversation with you._
   


-- 
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: Preserve TaskInstance history during Kubernetes API rate limiting errors [airflow]

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

   @HsiuChuanHsu this PR combines changes to airflow core and k8s provider. If these changes are not coupled can you please separate? Providers and core have different release cycles 


-- 
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: Preserve TaskInstance history during Kubernetes API rate limiting errors [airflow]

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

   @eladkal  Sure, will work on 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