seanghaeli commented on code in PR #76527:
URL: https://github.com/apache/airflow/pull/76527#discussion_r4640784557


##########
airflow-core/src/airflow/utils/log/watcher_log.py:
##########
@@ -0,0 +1,746 @@
+#
+# 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.
+"""An asset watcher's own log file on the triggerer: the capped file, its 
uploads and the orphan scan."""
+
+from __future__ import annotations
+
+import copy
+import os
+import shutil
+import threading
+import time
+from collections.abc import Callable
+from concurrent.futures import Future, ThreadPoolExecutor
+from contextlib import suppress
+from pathlib import Path
+from typing import TYPE_CHECKING
+
+import attrs
+import structlog
+from sqlalchemy import select
+
+from airflow.configuration import conf
+from airflow.models.trigger import Trigger
+from airflow.utils.helpers import chunks
+from airflow.utils.log.trigger_log import (
+    TRIGGER_ID_KEY,
+    TRIGGER_LOG_FILE_NAME,
+    TRIGGER_LOG_HELD_FILE_NAME,
+    TRIGGER_LOG_ROOT,
+    TRIGGER_LOG_ROTATED_DIR,
+    TRIGGERER_JOB_ID_KEY,
+    get_trigger_log_held_path,
+    get_trigger_log_part_path,
+    get_trigger_log_rotated_path,
+    list_trigger_log_files,
+    parse_trigger_log_dir_id,
+)
+from airflow.utils.session import create_session
+
+if TYPE_CHECKING:
+    from structlog.typing import FilteringBoundLogger
+
+    from airflow.jobs.triggerer_job_runner import TriggerLoggingFactory
+
+log: FilteringBoundLogger = structlog.get_logger(logger_name=__name__)
+
+
+class CappedBytesLogger(structlog.BytesLogger):
+    """
+    ``BytesLogger`` appending to ``path``, moved to the rotated directory once 
it reaches ``max_bytes``.
+
+    ``may_rotate`` can hold a rotation off, ``on_rotate`` is called after one; 
``max_bytes <= 0`` is no
+    cap. A closed or removed file (a log cleaner, ``delete_local_logs``, or on 
a shared log folder another
+    triggerer's orphan scan) is reopened by the next line.
+    """
+
+    __slots__ = ("_file_mode", "_folder_mode", "_max_bytes", "_may_rotate", 
"_on_rotate", "_path", "_size")
+
+    def __init__(
+        self,
+        path: Path,
+        max_bytes: int,
+        may_rotate: Callable[[], bool] | None = None,
+        on_rotate: Callable[[], None] | None = None,
+    ):
+        self._path, self._max_bytes = path, max_bytes
+        self._may_rotate, self._on_rotate = may_rotate, on_rotate
+        self._folder_mode, self._file_mode = get_log_permissions()
+        super().__init__(path.open("ab"))
+        self._size = self._file.tell()
+
+    @property
+    def rotated_path(self) -> Path:
+        return self._path.parent / TRIGGER_LOG_ROTATED_DIR / self._path.name
+
+    def ensure_open(self) -> None:
+        if self._file.closed:
+            self._path.parent.mkdir(mode=self._folder_mode, parents=True, 
exist_ok=True)
+            self._path.touch(mode=self._file_mode)
+            self._file = self._path.open("ab")
+            self._write = self._file.write
+            self._flush = self._file.flush
+            self._size = self._file.tell()
+
+    def _unlinked(self) -> bool:
+        try:
+            return os.fstat(self._file.fileno()).st_nlink == 0
+        except (OSError, ValueError):
+            return False
+
+    def msg(self, message: bytes) -> None:
+        with self._lock:
+            try:
+                if not self._file.closed and self._unlinked():
+                    # Lines written to a removed file are lost.
+                    with suppress(OSError):
+                        self._file.close()
+                self.ensure_open()
+                self._write(message + b"\n")
+                self._flush()
+                self._size += len(message) + 1
+                if 0 < self._max_bytes <= self._size and (self._may_rotate is 
None or self._may_rotate()):
+                    self._file.close()
+                    self.rotated_path.parent.mkdir(mode=self._folder_mode, 
parents=True, exist_ok=True)
+                    with suppress(FileNotFoundError):
+                        os.replace(self._path, self.rotated_path)
+                    if self._on_rotate is not None:
+                        try:
+                            self._on_rotate()
+                        except Exception:
+                            # The line is written: the caller, reading the 
runner's logs, must not see this.
+                            log.exception(
+                                "Failed to upload a rotated trigger log", 
path=str(self.rotated_path)
+                            )
+                    self.ensure_open()  # last: if this fails, the rotated 
lines are sent all the same
+            except OSError:
+                # Reopened by the next line: a file removed meanwhile (to free 
a full disk) frees its space.
+                with suppress(OSError):
+                    self._file.close()
+                raise
+
+    def close(self) -> None:
+        # Closing flushes; on a full disk those lines are lost either way.
+        with suppress(OSError):
+            self._file.close()
+
+    log = debug = info = warn = warning = msg
+    fatal = failure = err = error = critical = exception = msg
+
+
+# Marks a dead triggerer's log directory that one triggerer is uploading (see 
handle_orphaned_trigger_logs).
+_ORPHAN_UPLOAD_CLAIM = ".uploading"
+_ORPHAN_UPLOAD_CLAIM_MAX_AGE = 600.0
+_TRIGGER_ID_QUERY_CHUNK_SIZE = 500
+# Remote log handlers that take only task logs: a watcher's file is left as it 
was however often it is sent.
+_TASK_LOGS_ONLY_HANDLERS = frozenset({"ElasticsearchRemoteLogIO", 
"OpensearchRemoteLogIO"})
+# Remote log handlers that empty a file they stored and only log a failed 
write: lines left mean it failed.
+_EMPTIES_STORED_FILE_HANDLERS = frozenset({"S3RemoteLogIO", "GCSRemoteLogIO", 
"WasbRemoteLogIO"})

Review Comment:
   It wasn't. With those older versions the file kept its lines after a 
successful upload, so they were stored again later. The file is now emptied 
here once the handler's `write` returns True, which every version I checked 
does on success.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to