You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@airflow.apache.org by GitBox <gi...@apache.org> on 2022/12/13 14:24:18 UTC

[GitHub] [airflow] moiseenkov opened a new pull request, #28336: Fixed hanged KubernetesPodOperator

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

   Fix for the issue #23497
   
   - Inserted a container status check before pulling longs from a stream, because KubernetesPodOperator hangs when attempting to read logs of the terminated container. With the current fix we make sure that the container is alive before reading logs.
   - Updates unit tests


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


[GitHub] [airflow] potiuk merged pull request #28336: Fixed hanged KubernetesPodOperator

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


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


[GitHub] [airflow] potiuk commented on pull request #28336: Fixed hanged KubernetesPodOperator

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

   Needs re-review @dimberman @dstandish I guess.


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


[GitHub] [airflow] dstandish commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
dstandish commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059322223


##########
airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py:
##########
@@ -168,6 +168,7 @@ class KubernetesPodOperator(BaseOperator):
     :param labels: labels to apply to the Pod. (templated)
     :param startup_timeout_seconds: timeout in seconds to startup the pod.
     :param get_logs: get the stdout of the container as logs of the tasks.
+    :param logs_timeout: timeout in seconds to read logs after container termination.

Review Comment:
   To be clear, I don't veto making it configurable. But I suggest you don't. And if you must make it configurable, I would suggest a more specific name.



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


[GitHub] [airflow] jedcunningham commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
jedcunningham commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059470652


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,61 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        timeout: int = 120,
+    ):
+        self.response = response
+        self.pod = pod
+        self.pod_manager = pod_manager
+        self.container_name = container_name
+        self.timeout = timeout
+
+    def __iter__(self) -> Generator[bytes, None, None]:
+        messages: list[bytes] = []
+        if self.logs_available():
+            for chunk in self.response.stream(amt=None, decode_content=True):
+                if b"\n" in chunk:
+                    chunks = chunk.split(b"\n")
+                    yield b"".join(messages) + chunks[0] + b"\n"
+                    for x in chunks[1:-1]:
+                        yield x + b"\n"
+                    if chunks[-1]:
+                        messages = [chunks[-1]]
+                    else:
+                        messages = []
+                else:
+                    messages.append(chunk)
+                if not self.logs_available():

Review Comment:
   This whole approach seems a little backwards to me, if I'm understanding things correctly. _Sometimes_ we can't continue fetching logs after 2 minutes (or whatever), so we _always_ stop trying then?
   
   Would setting a timeout on the request itself guard us from getting stuck forever? Maybe `_request_timeout` on `read_namespaced_pod_log`? If not, I feel like we should find a way to bail once we know we've hit it instead of just not attempting it in the first place.



##########
airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py:
##########
@@ -168,6 +168,7 @@ class KubernetesPodOperator(BaseOperator):
     :param labels: labels to apply to the Pod. (templated)
     :param startup_timeout_seconds: timeout in seconds to startup the pod.
     :param get_logs: get the stdout of the container as logs of the tasks.
+    :param logs_timeout: timeout in seconds to read logs after container termination.

Review Comment:
   I think a constant is enough for this case. Really don't think it should be in the user facing api.



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


[GitHub] [airflow] dstandish commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
dstandish commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059322223


##########
airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py:
##########
@@ -168,6 +168,7 @@ class KubernetesPodOperator(BaseOperator):
     :param labels: labels to apply to the Pod. (templated)
     :param startup_timeout_seconds: timeout in seconds to startup the pod.
     :param get_logs: get the stdout of the container as logs of the tasks.
+    :param logs_timeout: timeout in seconds to read logs after container termination.

Review Comment:
   To be clear, I don't veto making it configurable. But I recommend you don't. And if you must make it configurable, I would suggest a more specific name.



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


[GitHub] [airflow] moiseenkov commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
moiseenkov commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1071339924


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,61 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        timeout: int = 120,
+    ):
+        self.response = response
+        self.pod = pod
+        self.pod_manager = pod_manager
+        self.container_name = container_name
+        self.timeout = timeout
+
+    def __iter__(self) -> Generator[bytes, None, None]:
+        messages: list[bytes] = []
+        if self.logs_available():
+            for chunk in self.response.stream(amt=None, decode_content=True):
+                if b"\n" in chunk:
+                    chunks = chunk.split(b"\n")
+                    yield b"".join(messages) + chunks[0] + b"\n"
+                    for x in chunks[1:-1]:
+                        yield x + b"\n"
+                    if chunks[-1]:
+                        messages = [chunks[-1]]
+                    else:
+                        messages = []
+                else:
+                    messages.append(chunk)
+                if not self.logs_available():

Review Comment:
   > I think what's happening Jed is we always stop 2 minutse after container completes.
   > I think that what's different here is author only applies timeout in the "container done" scenario. But _request_timeout would be across the whole request, which if following, could be a long time.
   
   @dstandish , you got it right.
   
   Limiting these calls by 2 minutes is a good idea. I'll update PR along with other fixes.



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


[GitHub] [airflow] dstandish commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
dstandish commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1058615241


##########
airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py:
##########
@@ -168,6 +168,7 @@ class KubernetesPodOperator(BaseOperator):
     :param labels: labels to apply to the Pod. (templated)
     :param startup_timeout_seconds: timeout in seconds to startup the pod.
     :param get_logs: get the stdout of the container as logs of the tasks.
+    :param logs_timeout: timeout in seconds to read logs after container termination.

Review Comment:
   I am not sure this needs to be configurable.
   
   It reads like a setting "if no logs for logs_timeout settings then quit". but... really this timeout is applied only after pod completes. and, in this case, 2 minutes seems fine, it's a potentially confusing param, and i don't think we need to make that configurable.



##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,61 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        timeout: int = 120,
+    ):
+        self.response = response
+        self.pod = pod
+        self.pod_manager = pod_manager
+        self.container_name = container_name
+        self.timeout = timeout
+
+    def __iter__(self) -> Generator[bytes, None, None]:
+        messages: list[bytes] = []
+        if self.logs_available():
+            for chunk in self.response.stream(amt=None, decode_content=True):

Review Comment:
   does this work when follow=False?



##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,61 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        timeout: int = 120,
+    ):
+        self.response = response
+        self.pod = pod
+        self.pod_manager = pod_manager
+        self.container_name = container_name
+        self.timeout = timeout
+
+    def __iter__(self) -> Generator[bytes, None, None]:
+        messages: list[bytes] = []
+        if self.logs_available():
+            for chunk in self.response.stream(amt=None, decode_content=True):
+                if b"\n" in chunk:
+                    chunks = chunk.split(b"\n")
+                    yield b"".join(messages) + chunks[0] + b"\n"
+                    for x in chunks[1:-1]:
+                        yield x + b"\n"
+                    if chunks[-1]:
+                        messages = [chunks[-1]]
+                    else:
+                        messages = []
+                else:
+                    messages.append(chunk)
+                if not self.logs_available():

Review Comment:
   this would seem to result in a _lot_ of calls to the kube API.  it may be wise to limit such cheking to every couple minutes?  wdyt? @jedcunningham ?



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


[GitHub] [airflow] dstandish commented on pull request #28336: Fixed hanged KubernetesPodOperator

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

   Hi, I dismissed my old review, so it's not blocking.
   
   I do have a suggestion though I'm sorry if it's a bit late in the game.  And maybe it doesn't have to be done in this PR.  
   
   But so the thing that stuck out to me when looking at this is, we do a kube api call (in logs_available) every chunk in the log stream.  This seems like it could result in a lot of calls and depending on how many such processes on the cluster could cause problems.  Just a hunch I guess.  But so it would seem to me that to avoid this, perhaps you could run the `logs_available` check in a thread, just have it run periodically, like once every 30 seconds or something, and then when it returns false, just set a `stop` boolean on the consumer so that it knows to exit the loop.  This decouples the checking from the log stream so that you that checking does not increase in response to log volume.


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


[GitHub] [airflow] moiseenkov commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
moiseenkov commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1081351277


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,61 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        timeout: int = 120,

Review Comment:
   done



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


[GitHub] [airflow] moiseenkov commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
moiseenkov commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1081348993


##########
airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py:
##########
@@ -168,6 +168,7 @@ class KubernetesPodOperator(BaseOperator):
     :param labels: labels to apply to the Pod. (templated)
     :param startup_timeout_seconds: timeout in seconds to startup the pod.
     :param get_logs: get the stdout of the container as logs of the tasks.
+    :param logs_timeout: timeout in seconds to read logs after container termination.

Review Comment:
   done



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


[GitHub] [airflow] moiseenkov commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
moiseenkov commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1052271143


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +98,62 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        timeout: int = 120,
+    ):
+        self.response = response
+        self.pod = pod
+        self.pod_manager = pod_manager
+        self.container_name = container_name
+        self.timeout = timeout
+
+    def __iter__(self) -> Generator[bytes, None, None]:
+        messages: list[bytes] = []
+        if self.logs_available():
+            for chunk in self.response.stream(amt=None, decode_content=True):
+                if b"\n" in chunk:
+                    chunks = chunk.split(b"\n")
+                    yield b"".join(messages) + chunks[0] + b"\n"
+                    for x in chunks[1:-1]:
+                        yield x + b"\n"
+                    if chunks[-1]:
+                        messages = [chunks[-1]]
+                    else:
+                        messages = []
+                else:
+                    messages.append(chunk)
+                if not self.logs_available():
+                    break
+        if messages:
+            yield b"".join(messages)
+
+    def logs_available(self):
+        remote_pod = self.pod_manager.read_pod(self.pod)
+        if container_is_running(pod=remote_pod, container_name=self.container_name):
+            return True
+        container_status = get_container_status(pod=remote_pod, container_name=self.container_name)
+        state = container_status.state if container_status else None
+        terminated = state.terminated if state else None
+        if terminated:
+            termination_time = terminated.finished_at
+            if termination_time:
+                now = datetime.now(tz=timezone.utc)
+                return termination_time + timedelta(seconds=self.timeout) > now

Review Comment:
   Thanks for the advice!



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


[GitHub] [airflow] dstandish commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
dstandish commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059320899


##########
airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py:
##########
@@ -168,6 +168,7 @@ class KubernetesPodOperator(BaseOperator):
     :param labels: labels to apply to the Pod. (templated)
     :param startup_timeout_seconds: timeout in seconds to startup the pod.
     :param get_logs: get the stdout of the container as logs of the tasks.
+    :param logs_timeout: timeout in seconds to read logs after container termination.

Review Comment:
   If the container is done, shouldn't there be no more logs forthcoming? In which case, I'm not sure what need there would be to increase time beyond the 2 minutes. If there is a need to add configurability, we can always do so later. It's easy to add things like this but much more difficult to remove or rename. Current name is ambiguous.



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


[GitHub] [airflow] dstandish commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
dstandish commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059322223


##########
airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py:
##########
@@ -168,6 +168,7 @@ class KubernetesPodOperator(BaseOperator):
     :param labels: labels to apply to the Pod. (templated)
     :param startup_timeout_seconds: timeout in seconds to startup the pod.
     :param get_logs: get the stdout of the container as logs of the tasks.
+    :param logs_timeout: timeout in seconds to read logs after container termination.

Review Comment:
   To be clear, I don't veto making it configurable. I just suggest you don't. And if you must make it configurable, I would suggest a more specific name.



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


[GitHub] [airflow] dstandish commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
dstandish commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059467769


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,61 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        timeout: int = 120,

Review Comment:
   maybe it's better named ... `post_completion_timeout` or `completion_timeout` or `termination_timeout`.  when i think of `timeout` i think of max time beteen each chunk.



##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,61 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        timeout: int = 120,

Review Comment:
   maybe it's better named ... `post_completion_timeout` or `completion_timeout` or `termination_timeout`.  when i think of just `timeout` i think of max time beteen each chunk.



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


[GitHub] [airflow] moiseenkov commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
moiseenkov commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059410960


##########
airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py:
##########
@@ -168,6 +168,7 @@ class KubernetesPodOperator(BaseOperator):
     :param labels: labels to apply to the Pod. (templated)
     :param startup_timeout_seconds: timeout in seconds to startup the pod.
     :param get_logs: get the stdout of the container as logs of the tasks.
+    :param logs_timeout: timeout in seconds to read logs after container termination.

Review Comment:
   @dstandish, thank you for noting that. Actually, I agree that exposing this parameter isn't a good idea - it handles logic that doesn't belong to the operator's level of abstraction and we will have to maintain it. At the same time I'm sure that it's better to leave the ability for configuring it because updates of third-party libraries can change the system's behavior and users would be able to adjust this parameter and stop suffering.
   As a compromise, could we consider a different approach based on envs or maybe something else? I didn't find any recommendations regarding envs in the contribution manuals.



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


[GitHub] [airflow] moiseenkov commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
moiseenkov commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1051931655


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +98,63 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        timeout: int = 120,
+    ):
+        self.response = response
+        self.pod = pod
+        self.pod_manager = pod_manager
+        self.container_name = container_name
+        self.timeout = timeout
+
+    def __iter__(self) -> Generator[bytes, None, None]:
+        messages: list[bytes] = []
+        if self.logs_available():
+            for chunk in self.response.stream(amt=None, decode_content=True):
+                if b"\n" in chunk:
+                    chunks = chunk.split(b"\n")
+                    yield b"".join(messages) + chunks[0] + b"\n"
+                    for x in chunks[1:-1]:
+                        yield x + b"\n"
+                    if chunks[-1]:
+                        messages = [chunks[-1]]
+                    else:
+                        messages = []
+                else:
+                    messages.append(chunk)
+                if not self.logs_available():
+                    break
+        if messages:
+            yield b"".join(messages)
+
+    def logs_available(self):
+        remote_pod = self.pod_manager.read_pod(self.pod)
+        if container_is_running(pod=remote_pod, container_name=self.container_name):
+            return True
+        container_status = get_container_status(pod=remote_pod, container_name=self.container_name)
+        state = container_status.state if container_status else None
+        terminated = state.terminated if state else None
+        if terminated:
+            termination_time = terminated.finished_at
+            if termination_time:
+                termination_time_ts = datetime.fromtimestamp(termination_time.timestamp())
+                now_ts = datetime.fromtimestamp(datetime.now().timestamp())

Review Comment:
   Yeah, I see now it wasn't a good idea. I updated the PR.



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


[GitHub] [airflow] dimberman commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

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


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,77 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+
+    :meta private:
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        post_termination_timeout: int = 120,
+        read_pod_cache_timeout: int = 120,

Review Comment:
   Can you please add descriptions for these variables into the docstring



##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -70,15 +73,20 @@ class PodPhase:
     terminal_states = {FAILED, SUCCEEDED}
 
 
+def get_container_status(pod: V1Pod, container_name: str) -> V1ContainerStatus | None:
+    """Retrieves container status"""
+    container_statuses = pod.status.container_statuses if pod and pod.status else None
+    if container_statuses:
+        return next((x for x in container_statuses if x.name == container_name), None)

Review Comment:
   Can you leave a short comment above this line explaining why you're using next. I get it, but it's a bit "deep python" and might come off as magic for someone just reading this the first time.



##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,77 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+
+    :meta private:
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        post_termination_timeout: int = 120,
+        read_pod_cache_timeout: int = 120,
+    ):
+        self.response = response
+        self.pod = pod
+        self.pod_manager = pod_manager
+        self.container_name = container_name
+        self.post_termination_timeout = post_termination_timeout
+        self.last_read_pod_at = None
+        self.read_pod_cache = None
+        self.read_pod_cache_timeout = read_pod_cache_timeout
+
+    def __iter__(self) -> Generator[bytes, None, None]:
+        messages: list[bytes] = []
+        if self.logs_available():
+            for chunk in self.response.stream(amt=None, decode_content=True):
+                if b"\n" in chunk:
+                    chunks = chunk.split(b"\n")
+                    yield b"".join(messages) + chunks[0] + b"\n"
+                    for x in chunks[1:-1]:
+                        yield x + b"\n"
+                    if chunks[-1]:
+                        messages = [chunks[-1]]
+                    else:
+                        messages = []
+                else:
+                    messages.append(chunk)
+                if not self.logs_available():
+                    break
+        if messages:
+            yield b"".join(messages)

Review Comment:
   I'm not a fan of how deep the nesting gets over here. Can you please break out a few helper functions to simplify this code?



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


[GitHub] [airflow] moiseenkov commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
moiseenkov commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059315589


##########
airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py:
##########
@@ -168,6 +168,7 @@ class KubernetesPodOperator(BaseOperator):
     :param labels: labels to apply to the Pod. (templated)
     :param startup_timeout_seconds: timeout in seconds to startup the pod.
     :param get_logs: get the stdout of the container as logs of the tasks.
+    :param logs_timeout: timeout in seconds to read logs after container termination.

Review Comment:
   Well, the problem occurs when we read logs after a container is being terminated. We researched this issue but the actual root cause wasn't found because it seems that the problem is at a very low level (third-party library `urllib3` hangs at this line: https://github.com/urllib3/urllib3/blob/d393b4a5091c27d2e158074f81feb264c5c175af/src/urllib3/response.py#L999).
   Experiments show that reading logs works fine for about  2-3 minutes after container termination, but if not all logs were red then it hangs. Because we didn't figure out what exactly causes hanging, we decided to make this parameter configurable for cases if `urllib3`'s behavior will change in the future.



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


[GitHub] [airflow] dstandish commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
dstandish commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059444913


##########
airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py:
##########
@@ -168,6 +168,7 @@ class KubernetesPodOperator(BaseOperator):
     :param labels: labels to apply to the Pod. (templated)
     :param startup_timeout_seconds: timeout in seconds to startup the pod.
     :param get_logs: get the stdout of the container as logs of the tasks.
+    :param logs_timeout: timeout in seconds to read logs after container termination.

Review Comment:
   Well, I guess you could make it a constant on the consumer classes, then that could be monkey patched.  Though that's a more advanced / hacky way. But maybe sufficient?



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


[GitHub] [airflow] dstandish commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
dstandish commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059467214


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,61 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        timeout: int = 120,
+    ):
+        self.response = response
+        self.pod = pod
+        self.pod_manager = pod_manager
+        self.container_name = container_name
+        self.timeout = timeout
+
+    def __iter__(self) -> Generator[bytes, None, None]:
+        messages: list[bytes] = []
+        if self.logs_available():
+            for chunk in self.response.stream(amt=None, decode_content=True):

Review Comment:
   i ask because if not follow, i don't know if maybe it returns a list or something?



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


[GitHub] [airflow] dstandish commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
dstandish commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059320899


##########
airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py:
##########
@@ -168,6 +168,7 @@ class KubernetesPodOperator(BaseOperator):
     :param labels: labels to apply to the Pod. (templated)
     :param startup_timeout_seconds: timeout in seconds to startup the pod.
     :param get_logs: get the stdout of the container as logs of the tasks.
+    :param logs_timeout: timeout in seconds to read logs after container termination.

Review Comment:
   If the container is done, shouldn't there be no more logs forthcoming? In which case, I'm not sure what need there would be to increase time beyond that. If there is a need to add configurability, we can always do so later. It's easy to add things like this but much more difficult to remove or rename. Current name is ambiguous.



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


[GitHub] [airflow] moiseenkov commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
moiseenkov commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1081350619


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,61 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        timeout: int = 120,
+    ):
+        self.response = response
+        self.pod = pod
+        self.pod_manager = pod_manager
+        self.container_name = container_name
+        self.timeout = timeout
+
+    def __iter__(self) -> Generator[bytes, None, None]:
+        messages: list[bytes] = []
+        if self.logs_available():
+            for chunk in self.response.stream(amt=None, decode_content=True):

Review Comment:
   yes, it works fine



##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,61 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """

Review Comment:
   done



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


[GitHub] [airflow] Taragolis commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
Taragolis commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1052084840


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +98,62 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        timeout: int = 120,
+    ):
+        self.response = response
+        self.pod = pod
+        self.pod_manager = pod_manager
+        self.container_name = container_name
+        self.timeout = timeout
+
+    def __iter__(self) -> Generator[bytes, None, None]:
+        messages: list[bytes] = []
+        if self.logs_available():
+            for chunk in self.response.stream(amt=None, decode_content=True):
+                if b"\n" in chunk:
+                    chunks = chunk.split(b"\n")
+                    yield b"".join(messages) + chunks[0] + b"\n"
+                    for x in chunks[1:-1]:
+                        yield x + b"\n"
+                    if chunks[-1]:
+                        messages = [chunks[-1]]
+                    else:
+                        messages = []
+                else:
+                    messages.append(chunk)
+                if not self.logs_available():
+                    break
+        if messages:
+            yield b"".join(messages)
+
+    def logs_available(self):
+        remote_pod = self.pod_manager.read_pod(self.pod)
+        if container_is_running(pod=remote_pod, container_name=self.container_name):
+            return True
+        container_status = get_container_status(pod=remote_pod, container_name=self.container_name)
+        state = container_status.state if container_status else None
+        terminated = state.terminated if state else None
+        if terminated:
+            termination_time = terminated.finished_at
+            if termination_time:
+                now = datetime.now(tz=timezone.utc)
+                return termination_time + timedelta(seconds=self.timeout) > now

Review Comment:
   General approach for get datetime now in UTC timezone in Airflow by use this helper:
   
   https://github.com/apache/airflow/blob/e377e869da9f0e42ac1e0a615347cf7cd6565d54/airflow/utils/timezone.py#L55-L67
   
   



##########
tests/providers/cncf/kubernetes/utils/test_pod_manager.py:
##########
@@ -403,3 +421,77 @@ def test_container_is_running(remote_pod, result):
     an object `e` such that `e.status.container_statuses` is None, and so on.  This test
     verifies the expected behavior."""
     assert container_is_running(remote_pod, "base") is result
+
+
+class TestPodLogsConsumer:
+    @pytest.mark.parametrize(
+        "chunks, expected_logs",
+        [
+            ([b"message"], [b"message"]),
+            ([b"message1\nmessage2"], [b"message1\n", b"message2"]),
+            ([b"message1\n", b"message2"], [b"message1\n", b"message2"]),
+            ([b"first_part", b"_second_part"], [b"first_part_second_part"]),
+            ([b""], [b""]),
+        ],
+    )
+    def test_chunks(self, chunks, expected_logs):
+        with mock.patch.object(PodLogsConsumer, "logs_available") as logs_available:
+            logs_available.return_value = True
+            consumer = PodLogsConsumer(
+                response=mock.MagicMock(stream=mock.MagicMock(return_value=chunks)),
+                pod=mock.MagicMock(),
+                pod_manager=mock.MagicMock(container_is_running=mock.MagicMock(return_value=True)),
+                container_name="base",
+            )
+            assert list(consumer) == expected_logs
+
+    def test_container_is_not_running(self):
+        with mock.patch.object(PodLogsConsumer, "logs_available") as logs_available:
+            logs_available.return_value = False
+            consumer = PodLogsConsumer(
+                response=mock.MagicMock(stream=mock.MagicMock(return_value=[b"message1", b"message2"])),
+                pod=mock.MagicMock(),
+                pod_manager=mock.MagicMock(container_is_running=mock.MagicMock(return_value=False)),
+                container_name="base",
+            )
+            assert list(consumer) == []
+
+    @pytest.mark.parametrize(
+        "container_run, termination_time, now_time, timeout, expected_logs_available",
+        [
+            (False, datetime(2022, 1, 1, 0, 0, 0, 0), datetime(2022, 1, 1, 0, 1, 0, 0), 120, True),
+            (False, datetime(2022, 1, 1, 0, 0, 0, 0), datetime(2022, 1, 1, 0, 2, 0, 0), 120, False),
+            (False, datetime(2022, 1, 1, 0, 0, 0, 0), datetime(2022, 1, 1, 0, 5, 0, 0), 120, False),
+            (True, datetime(2022, 1, 1, 0, 0, 0, 0), datetime(2022, 1, 1, 0, 1, 0, 0), 120, True),
+            (True, datetime(2022, 1, 1, 0, 0, 0, 0), datetime(2022, 1, 1, 0, 2, 0, 0), 120, True),
+            (True, datetime(2022, 1, 1, 0, 0, 0, 0), datetime(2022, 1, 1, 0, 5, 0, 0), 120, True),
+        ],
+    )
+    @mock.patch("airflow.providers.cncf.kubernetes.utils.pod_manager.container_is_running")
+    @mock.patch("airflow.providers.cncf.kubernetes.utils.pod_manager.get_container_status")
+    def test_logs_available(
+        self,
+        mock_get_container_status,
+        mock_container_is_running,
+        container_run,
+        termination_time,
+        now_time,
+        timeout,
+        expected_logs_available,
+    ):
+        mock_container_is_running.return_value = container_run
+        mock_get_container_status.return_value = mock.MagicMock(
+            state=mock.MagicMock(terminated=mock.MagicMock(finished_at=termination_time))
+        )
+        with mock.patch(
+            "airflow.providers.cncf.kubernetes.utils.pod_manager.datetime",
+            mock.MagicMock(now=mock.MagicMock(return_value=now_time), fromtimestamp=datetime.fromtimestamp),
+        ):

Review Comment:
   In general we use [`time-machine`](https://github.com/adamchainz/time-machine), previously `freezegun`, for mock everything related to current datetime rather than manually mock it.
   
   For sure there are some tests exists which use mock for datetime/time however better to use time-machine (as context manager or fixture).
   
   In additional I'm not familiar with k8s objects, and do not know is [`V1ContainerStateTerminated.finished_at`](https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1ContainerStateTerminated.md) return timezone-naive or timezone-aware datetime.



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


[GitHub] [airflow] moiseenkov commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
moiseenkov commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1052276734


##########
tests/providers/cncf/kubernetes/utils/test_pod_manager.py:
##########
@@ -403,3 +421,77 @@ def test_container_is_running(remote_pod, result):
     an object `e` such that `e.status.container_statuses` is None, and so on.  This test
     verifies the expected behavior."""
     assert container_is_running(remote_pod, "base") is result
+
+
+class TestPodLogsConsumer:
+    @pytest.mark.parametrize(
+        "chunks, expected_logs",
+        [
+            ([b"message"], [b"message"]),
+            ([b"message1\nmessage2"], [b"message1\n", b"message2"]),
+            ([b"message1\n", b"message2"], [b"message1\n", b"message2"]),
+            ([b"first_part", b"_second_part"], [b"first_part_second_part"]),
+            ([b""], [b""]),
+        ],
+    )
+    def test_chunks(self, chunks, expected_logs):
+        with mock.patch.object(PodLogsConsumer, "logs_available") as logs_available:
+            logs_available.return_value = True
+            consumer = PodLogsConsumer(
+                response=mock.MagicMock(stream=mock.MagicMock(return_value=chunks)),
+                pod=mock.MagicMock(),
+                pod_manager=mock.MagicMock(container_is_running=mock.MagicMock(return_value=True)),
+                container_name="base",
+            )
+            assert list(consumer) == expected_logs
+
+    def test_container_is_not_running(self):
+        with mock.patch.object(PodLogsConsumer, "logs_available") as logs_available:
+            logs_available.return_value = False
+            consumer = PodLogsConsumer(
+                response=mock.MagicMock(stream=mock.MagicMock(return_value=[b"message1", b"message2"])),
+                pod=mock.MagicMock(),
+                pod_manager=mock.MagicMock(container_is_running=mock.MagicMock(return_value=False)),
+                container_name="base",
+            )
+            assert list(consumer) == []
+
+    @pytest.mark.parametrize(
+        "container_run, termination_time, now_time, timeout, expected_logs_available",
+        [
+            (False, datetime(2022, 1, 1, 0, 0, 0, 0), datetime(2022, 1, 1, 0, 1, 0, 0), 120, True),
+            (False, datetime(2022, 1, 1, 0, 0, 0, 0), datetime(2022, 1, 1, 0, 2, 0, 0), 120, False),
+            (False, datetime(2022, 1, 1, 0, 0, 0, 0), datetime(2022, 1, 1, 0, 5, 0, 0), 120, False),
+            (True, datetime(2022, 1, 1, 0, 0, 0, 0), datetime(2022, 1, 1, 0, 1, 0, 0), 120, True),
+            (True, datetime(2022, 1, 1, 0, 0, 0, 0), datetime(2022, 1, 1, 0, 2, 0, 0), 120, True),
+            (True, datetime(2022, 1, 1, 0, 0, 0, 0), datetime(2022, 1, 1, 0, 5, 0, 0), 120, True),
+        ],
+    )
+    @mock.patch("airflow.providers.cncf.kubernetes.utils.pod_manager.container_is_running")
+    @mock.patch("airflow.providers.cncf.kubernetes.utils.pod_manager.get_container_status")
+    def test_logs_available(
+        self,
+        mock_get_container_status,
+        mock_container_is_running,
+        container_run,
+        termination_time,
+        now_time,
+        timeout,
+        expected_logs_available,
+    ):
+        mock_container_is_running.return_value = container_run
+        mock_get_container_status.return_value = mock.MagicMock(
+            state=mock.MagicMock(terminated=mock.MagicMock(finished_at=termination_time))
+        )
+        with mock.patch(
+            "airflow.providers.cncf.kubernetes.utils.pod_manager.datetime",
+            mock.MagicMock(now=mock.MagicMock(return_value=now_time), fromtimestamp=datetime.fromtimestamp),
+        ):

Review Comment:
   Thank you for pointing out the [time-machine](https://github.com/adamchainz/time-machine) - it solves lots of problems.
   Speaking about the [V1ContainerStateTerminated.finished_at](https://github.com/kubernetes-client/python/blob/master/kubernetes/docs/V1ContainerStateTerminated.md), experiments show that it retrieves datetime in UTC. I also added timezone specifications to the unit test.



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


[GitHub] [airflow] dstandish commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
dstandish commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059322223


##########
airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py:
##########
@@ -168,6 +168,7 @@ class KubernetesPodOperator(BaseOperator):
     :param labels: labels to apply to the Pod. (templated)
     :param startup_timeout_seconds: timeout in seconds to startup the pod.
     :param get_logs: get the stdout of the container as logs of the tasks.
+    :param logs_timeout: timeout in seconds to read logs after container termination.

Review Comment:
   To be clear, I don't veto making it configurable. I just only _suggest_ you don't. And if you must make it configurable, I would suggest a more specific name.



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


[GitHub] [airflow] dstandish commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
dstandish commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059466881


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,61 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """

Review Comment:
   ```suggestion
   
       :meta private:
       """
   ```



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


[GitHub] [airflow] dstandish commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
dstandish commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059466881


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,61 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """

Review Comment:
   ```suggestion
           :meta private:
   
           """
   ```



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


[GitHub] [airflow] dimberman commented on pull request #28336: Fixed hanged KubernetesPodOperator

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

   LGTM Let's merge when tests pass


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


[GitHub] [airflow] dstandish commented on pull request #28336: Fixed hanged KubernetesPodOperator

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

   OK I just experimented with our own "event scheduler" helper and it seems that we could use it to limit calls without managing threads.  Here's a code sample:
   ```python
   import time
   
   from airflow.utils.event_scheduler import EventScheduler
   
   class Tracker:
       stop = False
       counter = 0
   
   def hello(tracker: Tracker):
       tracker.counter += 1
       print("hi! %s" % tracker.counter)
       if tracker.counter > 10:
           tracker.stop = True
   
   e = EventScheduler()
   tracker = Tracker()
   e.call_regular_interval(2, hello, (tracker,))
   
   while True:
       e.run(blocking=False)
       time.sleep(0.5)
       if tracker.stop is True:
           break
   ```


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


[GitHub] [airflow] moiseenkov commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
moiseenkov commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1081349335


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,61 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        timeout: int = 120,
+    ):
+        self.response = response
+        self.pod = pod
+        self.pod_manager = pod_manager
+        self.container_name = container_name
+        self.timeout = timeout
+
+    def __iter__(self) -> Generator[bytes, None, None]:
+        messages: list[bytes] = []
+        if self.logs_available():
+            for chunk in self.response.stream(amt=None, decode_content=True):
+                if b"\n" in chunk:
+                    chunks = chunk.split(b"\n")
+                    yield b"".join(messages) + chunks[0] + b"\n"
+                    for x in chunks[1:-1]:
+                        yield x + b"\n"
+                    if chunks[-1]:
+                        messages = [chunks[-1]]
+                    else:
+                        messages = []
+                else:
+                    messages.append(chunk)
+                if not self.logs_available():

Review Comment:
   done



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


[GitHub] [airflow] dstandish commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
dstandish commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059467769


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,61 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        timeout: int = 120,

Review Comment:
   maybe it's better named ... `post_completion_timeout` or `completion_timeout` or `termination_timeout`.  when i think of timeout i think of max time beteen each chunk.



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


[GitHub] [airflow] dstandish commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
dstandish commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059466881


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,61 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """

Review Comment:
   ```suggestion
       :meta private:
   
       """
   ```



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


[GitHub] [airflow] Taragolis commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
Taragolis commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1051281701


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +98,63 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        timeout: int = 120,
+    ):
+        self.response = response
+        self.pod = pod
+        self.pod_manager = pod_manager
+        self.container_name = container_name
+        self.timeout = timeout
+
+    def __iter__(self) -> Generator[bytes, None, None]:
+        messages: list[bytes] = []
+        if self.logs_available():
+            for chunk in self.response.stream(amt=None, decode_content=True):
+                if b"\n" in chunk:
+                    chunks = chunk.split(b"\n")
+                    yield b"".join(messages) + chunks[0] + b"\n"
+                    for x in chunks[1:-1]:
+                        yield x + b"\n"
+                    if chunks[-1]:
+                        messages = [chunks[-1]]
+                    else:
+                        messages = []
+                else:
+                    messages.append(chunk)
+                if not self.logs_available():
+                    break
+        if messages:
+            yield b"".join(messages)
+
+    def logs_available(self):
+        remote_pod = self.pod_manager.read_pod(self.pod)
+        if container_is_running(pod=remote_pod, container_name=self.container_name):
+            return True
+        container_status = get_container_status(pod=remote_pod, container_name=self.container_name)
+        state = container_status.state if container_status else None
+        terminated = state.terminated if state else None
+        if terminated:
+            termination_time = terminated.finished_at
+            if termination_time:
+                termination_time_ts = datetime.fromtimestamp(termination_time.timestamp())
+                now_ts = datetime.fromtimestamp(datetime.now().timestamp())

Review Comment:
   Just wondering why you do transformation datetime -> timestamp -> datetime?



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


[GitHub] [airflow] moiseenkov commented on pull request #28336: Fixed hanged KubernetesPodOperator

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

   @dstandish , thank you for proposing a nicer approach.
   You mentioned earlier that we do k8s API calls every chunk. It is true for earlier versions of this PR, but currently the API response is being cached for 120 seconds. Please, take a look at the method `PodLogsConsumer.read_pod()` - it invokes `PodManager.read_pod()` only if the cache is empty or last call was more than `self.read_pod_cache_timeout` seconds ago (by default 120s). Thus there's no such problem now, however the `EventScheduler` approach would look nicer, and the code can always be refactored if needed.


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


[GitHub] [airflow] moiseenkov commented on pull request #28336: Fixed hanged KubernetesPodOperator

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

   @dimberman , @dstandish , @jedcunningham, hi.
   Could you please review my fixes?


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


[GitHub] [airflow] moiseenkov commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

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


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,77 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+
+    :meta private:
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        post_termination_timeout: int = 120,
+        read_pod_cache_timeout: int = 120,
+    ):
+        self.response = response
+        self.pod = pod
+        self.pod_manager = pod_manager
+        self.container_name = container_name
+        self.post_termination_timeout = post_termination_timeout
+        self.last_read_pod_at = None
+        self.read_pod_cache = None
+        self.read_pod_cache_timeout = read_pod_cache_timeout
+
+    def __iter__(self) -> Generator[bytes, None, None]:
+        messages: list[bytes] = []
+        if self.logs_available():
+            for chunk in self.response.stream(amt=None, decode_content=True):
+                if b"\n" in chunk:
+                    chunks = chunk.split(b"\n")
+                    yield b"".join(messages) + chunks[0] + b"\n"
+                    for x in chunks[1:-1]:
+                        yield x + b"\n"
+                    if chunks[-1]:
+                        messages = [chunks[-1]]
+                    else:
+                        messages = []
+                else:
+                    messages.append(chunk)
+                if not self.logs_available():
+                    break
+        if messages:
+            yield b"".join(messages)

Review Comment:
   Yes, thanks for noticing. I refactored this method to make it more readable.



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


[GitHub] [airflow] dstandish commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
dstandish commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059322223


##########
airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py:
##########
@@ -168,6 +168,7 @@ class KubernetesPodOperator(BaseOperator):
     :param labels: labels to apply to the Pod. (templated)
     :param startup_timeout_seconds: timeout in seconds to startup the pod.
     :param get_logs: get the stdout of the container as logs of the tasks.
+    :param logs_timeout: timeout in seconds to read logs after container termination.

Review Comment:
   To be clear, I don't veto making it configurable. I recommend you don't make it configurable. And if you must make it configurable, I would suggest a more specific name.



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


[GitHub] [airflow] dstandish commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
dstandish commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059475542


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,61 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        timeout: int = 120,
+    ):
+        self.response = response
+        self.pod = pod
+        self.pod_manager = pod_manager
+        self.container_name = container_name
+        self.timeout = timeout
+
+    def __iter__(self) -> Generator[bytes, None, None]:
+        messages: list[bytes] = []
+        if self.logs_available():
+            for chunk in self.response.stream(amt=None, decode_content=True):
+                if b"\n" in chunk:
+                    chunks = chunk.split(b"\n")
+                    yield b"".join(messages) + chunks[0] + b"\n"
+                    for x in chunks[1:-1]:
+                        yield x + b"\n"
+                    if chunks[-1]:
+                        messages = [chunks[-1]]
+                    else:
+                        messages = []
+                else:
+                    messages.append(chunk)
+                if not self.logs_available():

Review Comment:
   > _request_timeout
   
   I think that what's different here is author only applies timeout in the "container done" scenario.  But `_request_timeout` would be across the whole request, which if following, could be a long time.



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


[GitHub] [airflow] dstandish commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
dstandish commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059474973


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,61 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        timeout: int = 120,
+    ):
+        self.response = response
+        self.pod = pod
+        self.pod_manager = pod_manager
+        self.container_name = container_name
+        self.timeout = timeout
+
+    def __iter__(self) -> Generator[bytes, None, None]:
+        messages: list[bytes] = []
+        if self.logs_available():
+            for chunk in self.response.stream(amt=None, decode_content=True):
+                if b"\n" in chunk:
+                    chunks = chunk.split(b"\n")
+                    yield b"".join(messages) + chunks[0] + b"\n"
+                    for x in chunks[1:-1]:
+                        yield x + b"\n"
+                    if chunks[-1]:
+                        messages = [chunks[-1]]
+                    else:
+                        messages = []
+                else:
+                    messages.append(chunk)
+                if not self.logs_available():

Review Comment:
   > Sometimes we can't continue fetching logs after 2 minutes (or whatever), so we always stop trying then?
   
   I think what's happening Jed is we always stop 2 minutse _after container completes_.



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


[GitHub] [airflow] moiseenkov commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

Posted by GitBox <gi...@apache.org>.
moiseenkov commented on code in PR #28336:
URL: https://github.com/apache/airflow/pull/28336#discussion_r1059315589


##########
airflow/providers/cncf/kubernetes/operators/kubernetes_pod.py:
##########
@@ -168,6 +168,7 @@ class KubernetesPodOperator(BaseOperator):
     :param labels: labels to apply to the Pod. (templated)
     :param startup_timeout_seconds: timeout in seconds to startup the pod.
     :param get_logs: get the stdout of the container as logs of the tasks.
+    :param logs_timeout: timeout in seconds to read logs after container termination.

Review Comment:
   Well, the problem occurs when we read logs after a container is being terminated. We researched this issue but the actual root cause wasn't found because it seems that the problem is at a very low level (third-party library `urllib3` hangs at this line: https://github.com/urllib3/urllib3/blob/d393b4a5091c27d2e158074f81feb264c5c175af/src/urllib3/response.py#L999).
   Experiments show that reading logs works fine for about  2-3 minutes after container termination, but if not all logs were red then it hangs. Because we didn't figure out what exactly causes hanging, we decided to make this parameter configurable for cases if `urllib3`'s behavior will change in the future.
   This workaround will let users to continue working with no worries.



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


[GitHub] [airflow] dstandish commented on pull request #28336: Fixed hanged KubernetesPodOperator

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

   Ah ok thanks 👍


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


[GitHub] [airflow] moiseenkov commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

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


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -91,6 +99,77 @@ def get_container_termination_message(pod: V1Pod, container_name: str):
         return container_status.state.terminated.message if container_status else None
 
 
+class PodLogsConsumer:
+    """
+    PodLogsConsumer is responsible for pulling pod logs from a stream with checking a container status before
+    reading data.
+    This class is a workaround for the issue https://github.com/apache/airflow/issues/23497
+
+    :meta private:
+    """
+
+    def __init__(
+        self,
+        response: HTTPResponse,
+        pod: V1Pod,
+        pod_manager: PodManager,
+        container_name: str,
+        post_termination_timeout: int = 120,
+        read_pod_cache_timeout: int = 120,

Review Comment:
   Sure, thank you for noticing.



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


[GitHub] [airflow] moiseenkov commented on a diff in pull request #28336: Fixed hanged KubernetesPodOperator

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


##########
airflow/providers/cncf/kubernetes/utils/pod_manager.py:
##########
@@ -70,15 +73,20 @@ class PodPhase:
     terminal_states = {FAILED, SUCCEEDED}
 
 
+def get_container_status(pod: V1Pod, container_name: str) -> V1ContainerStatus | None:
+    """Retrieves container status"""
+    container_statuses = pod.status.container_statuses if pod and pod.status else None
+    if container_statuses:
+        return next((x for x in container_statuses if x.name == container_name), None)

Review Comment:
   Sure



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