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

   ## What
   
   Adds `S3RemoteLogIO.stream()` so that remote task logs stored in S3 are read 
as a stream of lines instead of being materialised as one string per object, 
and lets `read()` delegate to it.
   
   ## Why
   
   `S3RemoteLogIO.read()` loads every remote log object into a single string:
   
   ```python
   for key in keys:
       logs.append(self.s3_read(key, return_error=True))   # S3Hook.read_key() 
-> Body.read().decode()
   ```
   
   Core then wraps that string in an `io.StringIO` 
(`_get_compatible_log_stream`), so opening a task log in the UI costs the 
api-server at least twice the size of the log, and a large enough log OOM-kills 
it.
   
   That failure is worse than a failed log view. When the api-server runs with 
the default `--apps all`, the UI and the **Task Execution API** share a 
process, so the OOM kill also drops the endpoint that running tasks heartbeat 
to — viewing a log can take down tasks that had nothing to do with it.
   
   Everything downstream of the provider is already bounded: 
`LogStreamAccumulator` spills to disk every `HEAP_DUMP_SIZE` lines and the 
K-way merge heap is capped by the same value. The provider read is the only 
unbounded step left.
   
   ## Why now
   
   Core has had the streaming path since 3.2.0 — 
`FileTaskHandler._read_remote_logs` prefers `remote_io.stream()` when the 
provider implements it and falls back to `read()` otherwise:
   
   ```python
   if stream_method := getattr(remote_io, "stream", None):
       sources, logs = stream_method(path, ti)
       return sources, logs or []
   sources, logs = remote_io.read(path, ti)
   ```
   
   `gs` (google) and `cloudwatch` (amazon) already implement `stream()`. S3 was 
the remaining legacy reader, so it is the one backend where this still costs 
the whole object in memory.
   
   ## How
   
   `stream()` follows the same shape as the GCS and CloudWatch implementations:
   
   * It returns **one generator per object**. The `GetObject` is issued when 
the generator is first consumed, so listing the keys of a task instance no 
longer implies downloading them.
   * `_get_log_stream()` iterates `Body.iter_chunks()` and splits on `"\n"` 
only, with the trailing newline stripped — the same records 
`file_task_handler._stream_lines_by_chunk` produces for local logs, so the log 
reader sees exactly what it saw before. An incremental UTF-8 decoder keeps 
multi-byte characters intact across chunk boundaries.
   * A failure to read one object is yielded as a log line rather than raised, 
so one unreadable object does not hide the other objects of the same task 
instance. This preserves the previous `s3_read(..., return_error=True)` 
behaviour.
   * `read()` re-joins the lines with their newline, so the string it returns 
is byte-identical to the object for callers that cannot consume a stream 
(Airflow < 3.2, and `S3TaskHandler._read_remote_logs`, which is left calling 
`io.read()` exactly like the GCS and CloudWatch handlers do).
   
   Peak memory for a log read becomes a function of the chunk size instead of 
the log size.
   
   ## Tests
   
   `providers/amazon/tests/unit/amazon/aws/log/test_s3_task_handler.py` — 43 
passed (8 new).
   
   The new tests pin the two properties that matter:
   
   * `test_read_does_not_load_the_object_as_a_single_string` fails if `s3_read` 
is ever called again from this path — a regression back to the whole-object 
read is caught by the suite, not by review.
   * `test_stream_does_not_fetch_the_object_until_it_is_consumed` pins the 
laziness.
   
   Plus: line splitting without the trailing newline, key ordering, multi-byte 
characters split across chunk boundaries (with `CHUNK_SIZE` patched to 3 
bytes), the empty case, and the unreadable-object case.
   
   ## Real-world verification
   
   An equivalent implementation (same `iter_chunks` + incremental-decoder read 
path, installed via `[logging] logging_config_class`) has been running on 
Airflow 3.1.8 with S3 remote logging in our dev, staging and production 
clusters since 2026-09-10.
   
   * Before, reading a 250 MB / 2,000,000-line log peaked at **1,168.7 MiB** of 
Python heap; after, **29.5 MiB** (`tracemalloc`, same input).
   * In production, reading a real task log through 
`TaskLogReader.read_log_stream` — the same path the api-server uses to serve 
the UI — streams 11,326 lines at a **61.9 MiB** heap peak.
   * No api-server OOM on log views since.
   
   I did not re-run those numbers against this patch in a synthetic harness; 
the mechanism is the same and the unit tests above pin the behaviour. Happy to 
add a benchmark if that would help review.
   
   ## Notes
   
   * Behaviour for existing configs is unchanged — no config, no key layout and 
no message formatting changes. The Airflow 2 message format (`Found logs in 
s3:` / `  * <key>`) is preserved as-is.
   * `CHUNK_SIZE` is defined in the provider rather than imported from core, so 
the provider keeps working against Airflow versions that do not expose it.
   


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