Skip to content

perf(LogController): Use streamed responses and only read log once - #2331

Open
DerDreschner wants to merge 2 commits into
masterfrom
perf/stream-log-responses
Open

DerDreschner wants to merge 2 commits into
masterfrom
perf/stream-log-responses

Conversation

@DerDreschner

Copy link
Copy Markdown
Contributor

At the moment, the endpoints /api/poll and /api/log collect all entries they return and then JSON-encode them all at once. In a test scenario with ten 25 MB entries among the newest 50, the request needed 720 MB, more than the usual memory limit of 512 MB, leaving the log viewer empty.

This is being improved by this PR in two ways:

  1. Use StreamTraversableResponse, available since Nextcloud 33. This allows us to stream each log entry to the browser, which means the memory usage is only dependent on the largest entry. For the test scenario, that was 95 MB peak memory consumption.
  2. A search scanned the log twice, as the same iterator was rewound twice, and a poll opened the log twice with two iterators. Now both read the log only once, which cuts the time of a search without a match in half.

As large log entries can happen (like after an upgrade, during which the log level is temporarily set to debug and exceptions are logged with all their arguments), improvements here are necessary IMHO.

Fixes #1025
Fixes #1170

馃 AI (if applicable)

  • The content of this PR was partly or fully generated using AI (Claude Opus 5.5)

@DerDreschner
DerDreschner requested review from Antreesy and CarlSchwan and a balanced review from Copilot October 19, 2026 14:17
@DerDreschner DerDreschner self-assigned this Oct 19, 2026
@DerDreschner

Copy link
Copy Markdown
Contributor Author

/backport to stable35

@DerDreschner

Copy link
Copy Markdown
Contributor Author

/backport to stable34

@DerDreschner

Copy link
Copy Markdown
Contributor Author

/backport to stable33

This comment was marked as outdated.

This comment was marked as outdated.

At the moment, the endpoints /api/poll and /api/log collect all entries
they return and then JSON-encode them all at once. In a test scenario
with ten 25 MB entries among the newest 50, the request needed 720 MB
and failed on the memory limit, leaving the log viewer empty.

Since Nextcloud 33, there is a better way to do it: using
StreamTraversableResponse and sending one entry at a time. That way,
the same test scenario only needs 95 MB at peak. Such large entries can
occur after an upgrade, as the upgrader sets the log level to debug, and
exceptions are then logged with all their arguments.

As the response is already being sent while the entries are read,
values JSON cannot represent are replaced instead of failing halfway,
and a poll reads at most the newest 100,000 entries like a search.
LogIteratorFactory now only returns lines that decode to an entry with
a level, so lines without a level are no longer shown.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: David Dreschner <david.dreschner@nextcloud.com>
A search rewound its iterator in the SearchFilter constructor, in get()
and again in responseFromIterator(). Each rewind of a filter reads the
log up to the first matching entry, so a search without a match read
the whole log several times. A poll opened the log once to compare the
newest entry with lastReqId, and then a second time to send the new
entries. Skipping to the offset of a page also went on after the end of
the log.

Rewind a search only in responseFromIterator(), use a single iterator
for a poll, and stop skipping at the end of the log. The streamed
response already answers with an empty list when the newest entry is
the last known one. A search without a match over 100,000 entries now
takes 0.20 s instead of 0.40 s.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: David Dreschner <david.dreschner@nextcloud.com>

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

馃數 Needs a closer look

The polling cap can silently omit unseen entries without a continuation mechanism.

2 open issues

Review effort: Balanced

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SetupCheck is taking too long / Detect and log slow running setup checks [Bug]: After upgrade the webUI page with logs started to decline to be shown

2 participants