Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Resolve OOM When Reading Large Logs in Webserver] Refactor to Use K-Way Merge for Log Streams Instead of Sorting Entire Log Records #45129

Open
wants to merge 23 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
d2b9798
Refactor _interleave_logs and _read_from_local
jason810496 Dec 21, 2024
ca85bae
Refactor FileTaskHandler and TaskLogReader public method interface
jason810496 Dec 21, 2024
a835f9b
Refactor test for get from local, parse log timestamp
jason810496 Dec 21, 2024
a818a4a
Fix line_num in parsed_log_stream, dedupe logic
jason810496 Dec 21, 2024
9651d1c
Fix interleave releated test
jason810496 Dec 21, 2024
47627a9
Fix format for _read and read_log_stream methods
jason810496 Dec 22, 2024
820f902
Refactor _read_from_logs_server, read resp in chunk
jason810496 Dec 22, 2024
439c288
Add compatible utility for old and new log source
jason810496 Dec 22, 2024
1f5fdf3
Fix test_log_handlers
jason810496 Dec 22, 2024
b6a023e
Refactor read_log_chunks, read_log_stream methods
jason810496 Dec 23, 2024
0e5e6ad
Fix test_log_reader, minor fix test_log_handlers
jason810496 Dec 23, 2024
ea70696
Fix get_log and test_get_log in api_fastapi
jason810496 Dec 23, 2024
8c0a6a5
Fix get_log and test_get_log in api_connexion
jason810496 Dec 23, 2024
f4b6acf
Fix _interleave_logs break loop condition
jason810496 Dec 23, 2024
2730808
Move log_str_to_parsed_log_stream to test utils
jason810496 Dec 23, 2024
c483b8c
Add _get_compatible_read_for_providers function
jason810496 Dec 25, 2024
f93b26e
Fix providers test cases that use read or _read method
jason810496 Dec 25, 2024
ad76323
Fix TestGCSTaskHandler log sample
jason810496 Dec 25, 2024
28356dd
Fix legacy view and corresponding test
jason810496 Dec 25, 2024
0759400
Fix mypy typing error
jason810496 Dec 25, 2024
eaa50da
Fix test_file_task_handler_rotate_size_limit
jason810496 Dec 25, 2024
84f7aa6
Fix _get_compatible_parse_log_streams
jason810496 Dec 25, 2024
8d7b609
Fix providers compact tests
jason810496 Dec 26, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions airflow/api_connexion/endpoints/log_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,14 @@ def get_log(
# return_type would be either the above two or None
logs: Any
if return_type == "application/json" or return_type is None: # default
logs, metadata = task_log_reader.read_log_chunks(ti, task_try_number, metadata)
logs = logs[0] if task_try_number is not None else logs
hosts, log_streams, metadata = task_log_reader.read_log_chunks(ti, task_try_number, metadata)
host = f"{hosts[0] or ''}\n"
logs = log_streams[0] if task_try_number is not None else log_streams
# we must have token here, so we can safely ignore it
token = URLSafeSerializer(key).dumps(metadata) # type: ignore[assignment]
return logs_schema.dump(LogResponseObject(continuation_token=token, content=logs))
return logs_schema.dump(
LogResponseObject(continuation_token=token, content=host + "\n".join(log for log in logs))
)
# text/plain. Stream
logs = task_log_reader.read_log_stream(ti, task_try_number, metadata)

Expand Down
20 changes: 13 additions & 7 deletions airflow/api_fastapi/core_api/routes/public/log.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,14 @@
from __future__ import annotations

import textwrap
from typing import Any
from collections.abc import Iterable

from fastapi import HTTPException, Request, Response, status
from fastapi import HTTPException, Request, status
from itsdangerous import BadSignature, URLSafeSerializer
from pydantic import PositiveInt
from sqlalchemy.orm import joinedload
from sqlalchemy.sql import select
from starlette.responses import StreamingResponse

from airflow.api_fastapi.common.db.common import SessionDep
from airflow.api_fastapi.common.headers import HeaderAcceptJsonOrText
Expand Down Expand Up @@ -135,12 +136,17 @@ def get_log(
except TaskNotFound:
pass

logs: Any
log_streams: list[Iterable[str]]
log_stream: Iterable[str]
if accept == Mimetype.JSON or accept == Mimetype.ANY: # default
logs, metadata = task_log_reader.read_log_chunks(ti, try_number, metadata)
hosts, log_streams, metadata = task_log_reader.read_log_chunks(ti, try_number, metadata)
# we must have token here, so we can safely ignore it
token = URLSafeSerializer(request.app.state.secret_key).dumps(metadata) # type: ignore[assignment]
return TaskInstancesLogResponse(continuation_token=token, content=str(logs[0])).model_dump()
host = f"{hosts[0] or ''}\n"
log_stream = log_streams[0]
return TaskInstancesLogResponse(
continuation_token=token, content=host + "\n".join(log for log in log_stream)
).model_dump()
# text/plain. Stream
logs = task_log_reader.read_log_stream(ti, try_number, metadata)
return Response(media_type=accept, content="".join(list(logs)))
log_stream = task_log_reader.read_log_stream(ti, try_number, metadata)
return StreamingResponse(log_stream, media_type=Mimetype.TEXT.value)
13 changes: 10 additions & 3 deletions airflow/executors/base_executor.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import logging
import sys
from collections import defaultdict, deque
from collections.abc import Sequence
from collections.abc import Generator, Sequence
from dataclasses import dataclass, field
from typing import TYPE_CHECKING, Any, Optional

Expand Down Expand Up @@ -544,13 +544,20 @@ def execute_async(
"""
raise NotImplementedError()

def get_task_log(self, ti: TaskInstance, try_number: int) -> tuple[list[str], list[str]]:
def get_task_log(
self, ti: TaskInstance, try_number: int
) -> (
tuple[list[str], list[Generator[tuple[pendulum.DateTime | None, int, str], None, None]], int]
| tuple[list[str], list[str]]
):
"""
Return the task logs.

:param ti: A TaskInstance object
:param try_number: current try_number to read log from
:return: tuple of logs and messages
:return:
- old interface: Tuple of messages and list of log lines.
- new interface: Tuple of messages, parsed log streams, total size of logs.
"""
return [], []

Expand Down
Loading
Loading