e-galan opened a new pull request, #39329:
URL: https://github.com/apache/airflow/pull/39329

   <!--
    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/
   -->
   
   Prevent **KubernetesPodOperator**  from raising an exception in a rare 
scenario, wherein the task is running in the sync mode with parameter 
`reattach_on_restart` equal to *False*, and the first task attempt fails 
because the task process is killed externally by the Kubernetes cluster or 
another process.
   
   If the task is killed externally, it breaks the execution flow (including 
any try/except blocks) and immediately exists the task, resulting in a 
situation where the pod created for the first task run try is not properly 
deleted / updated, and consequently in the pod number exception, which will 
repeat in the next task tries until the dag will fail completely.
   
   **Behavior before the fix**:
   1. `KubernetesPodOperator` starts a new task.
   2. A k8s pod is created to process the task.
   3.  For some reason the task in the pod is killed externally and exits with 
some code (-9 for example).
   4. Since the `reattach_on_restart` parameter is set to False, the operator 
does not try to restart the task in the same pod for the next attempt, and 
tries to create a new one while the original pod still exists with the same 
labels.
   5. The new pod is created. 
   6. Before continuing the task, `KubernetesPodOperator` tries to find the pod 
using the pod labels stored in the task context. 
   7. 2 pods with such labels are found, resulting in the exception (*"More 
than one pod running with labels"*).
   8. The exception continues to be raised on the next tries.
   
   **Behavior after the fix**:
   1-6. Same behavior.
   7. 2 pods with such labels are found.
   9. If `reattach_on_restart` is False, then we loop through the pods and pick 
the one that was created last and assign it to be used for the next attempt.  
   10. Depending on the value of the `on_finish_action` parameter, we will 
either update the labels of the previous pod or remove it.
   11. The task will continue without the exception.
   
   <!-- 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-unsubscr...@airflow.apache.org

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

Reply via email to