seanghaeli commented on code in PR #76560: URL: https://github.com/apache/airflow/pull/76560#discussion_r4755145882
########## airflow-core/src/airflow/utils/log/trigger_log_reader.py: ########## @@ -0,0 +1,801 @@ +# +# 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. +"""Read a trigger's log lines, whatever its kind, live from its triggerer, from remote logging and locally.""" + +from __future__ import annotations + +import datetime +import functools +import heapq +import inspect +import itertools +import json +import os +from collections import deque +from contextvars import ContextVar +from pathlib import Path +from typing import TYPE_CHECKING + +from sqlalchemy import not_, select + +from airflow.configuration import conf +from airflow.jobs.job import Job +from airflow.logging_config import get_remote_task_log +from airflow.models.trigger import TriggerKind +from airflow.utils.log.file_task_handler import ( + DEFAULT_SORT_DATETIME, + FileTaskHandler, + LogType, + StructuredLogMessage, + _get_compatible_log_stream, + fetch_logs_from_service, + get_log_server_url, + log_stream_to_parsed_log_stream, +) +from airflow.utils.log.path_log_reader import read_local_logs, validate_log_path_component +from airflow.utils.log.task_log_address import prepare_task_log_contexts, render_task_log_filename +from airflow.utils.log.trigger_log import ( + TRIGGERER_JOB_ID_KEY, + get_callback_trigger_log_relative_path, + get_trigger_log_dir, + get_trigger_log_held_path, + get_trigger_log_relative_path, + get_trigger_log_rotated_path, + list_trigger_log_files, + parse_trigger_log_dir_id, +) + +if TYPE_CHECKING: + from collections.abc import Generator, Iterable, Sequence + + from sqlalchemy.orm import Session + + from airflow._shared.logging.remote import RawLogStream + from airflow.models.trigger import Trigger + +# Bytes asked of a log server per line; a tail with fewer whole lines is asked again, GROWTH times larger. +LIVE_TAIL_BYTES_PER_LINE = 2048 +LIVE_TAIL_GROWTH = 8 +LIVE_TAIL_MAX_BYTES = 64 * 1024 * 1024 +# Most bytes of log lines one request keeps; past it, older files and triggerer jobs are not read. +READ_MAX_BYTES = 64 * 1024 * 1024 +_REMOTE_READ_FAILED = "Could not read remote logs" +# How a store that does not raise answers a file it could not read (S3, Azure Blob Storage). +_STORE_COULD_NOT_READ = "Could not read logs from " +_REMOTE_NOT_READ_AFTER_FAILURE = ( + "Remote logs of older triggerer jobs were not read: the remote log store failed." +) +# Most remote files one request asks for after the store could not read them: each can cost its retries, seconds. +READ_MAX_UNREADABLE_FILES = 3 +_REMOTE_NOT_READ_AFTER_NOTICE = ( + f"Older remote log files were not read: the remote log store could not read {READ_MAX_UNREADABLE_FILES}." +) +_TRIGGERER_NOT_READ = "Older files of a triggerer were not read once the store could not read a newer one." +_NOT_READ_PAST_CAP = f"Older lines were not shown: a request keeps at most {READ_MAX_BYTES // 2**20} MiB." +# Most triggerer jobs looked at for a watcher: each costs a remote read, and every triggerer start is a job. +READ_MAX_JOBS = 50 +# Recent lines remembered to drop copies read from another source. +_SEEN_MAX = 10_000 +# While set, where each live file read ends: just past its last whole line. +_live_ends: ContextVar[dict[str, int] | None] = ContextVar("_live_ends", default=None) + + +def read_trigger_logs(trigger: Trigger, *, tail_lines: int, session: Session) -> list[StructuredLogMessage]: + """ + Return the last ``tail_lines`` lines a trigger logged, read live, from remote logging and from local disk. + + A task's trigger logs to ``<try log>.trigger.<triggerer job id>.log``, a callback's to the callback's log, + and an asset watcher to its own files. Needs ``triggerer_job`` and the owner loaded. + """ + job = trigger.triggerer_job + owner = (job.id, job.hostname) if job is not None and trigger.is_owner_alive() else None + kind = trigger.kind + if kind == TriggerKind.TASK and (ti := trigger.task_instance) is not None: + context = prepare_task_log_contexts([ti], session=session)[ti.id] + try_log = render_task_log_filename(ti, ti.try_number, context=context) + prefix = f"{FileTaskHandler.add_triggerer_suffix(try_log)}." + live = FileTaskHandler.add_triggerer_suffix(try_log, job_id=owner[0]) if owner else None + if _is_remote_handler_streaming() and ( + job_ids := _get_triggerer_job_ids(trigger, session=session, limit=READ_MAX_JOBS) + ): + return _read_task_trigger_streams( + str(ti.id), + try_log, + prefix, + job_ids, + live=live, + owner=owner, + tail_lines=tail_lines, + session=session, + ) + return _read_tail(stored=prefix, local=[prefix], live=live, owner=owner, tail_lines=tail_lines) Review Comment: Yes, that's what #76631 is there to fix. This problem goes away when that merges. -- 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]
