Repository navigation
perf(LogController): Use streamed responses and only read log once #2331
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,6 +16,7 @@ | |
| use OCP\AppFramework\Http; | ||
| use OCP\AppFramework\Http\Attribute\AuthorizedAdminSetting; | ||
| use OCP\AppFramework\Http\JSONResponse; | ||
| use OCP\AppFramework\Http\StreamTraversableResponse; | ||
| use OCP\IRequest; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
|
|
@@ -26,6 +27,11 @@ | |
| * @psalm-api | ||
| */ | ||
| class LogController extends Controller { | ||
| /** | ||
| * Maximum number of entries of the shown levels a search or a poll goes through, hidden ones are not counted. | ||
| * A search does not find older entries, see poll() for what happens to them there. | ||
| */ | ||
| private const int MAX_SCANNED_ENTRIES = 100000; | ||
|
|
||
| public function __construct( | ||
| $appName, | ||
|
|
@@ -41,10 +47,9 @@ public function __construct( | |
| * @param string $query | ||
| * @param int $count | ||
| * @param int $offset | ||
| * @return JSONResponse | ||
| */ | ||
| #[AuthorizedAdminSetting(settings: Admin::class)] | ||
| public function get($query = '', $count = 50, $offset = 0): JSONResponse { | ||
| public function get($query = '', $count = 50, $offset = 0): JSONResponse|StreamTraversableResponse { | ||
| $logType = $this->settingsService->getLoggingType(); | ||
| // we only support web access when `log_type` is set to `file` (the default) | ||
| if ($logType !== 'file') { | ||
|
|
@@ -55,26 +60,12 @@ public function get($query = '', $count = 50, $offset = 0): JSONResponse { | |
| $iterator = $this->logIteratorFactory->getLogIterator($this->settingsService->getShownLevels()); | ||
|
|
||
| if ($query !== '') { | ||
| $iterator = new \LimitIterator($iterator, 0, 100000); // limit the number of message we search to avoid huge search times | ||
| $iterator->rewind(); | ||
| $iterator = new SearchFilter($iterator, $query); | ||
| $iterator->rewind(); | ||
| return $this->responseFromIterator($iterator, $count, $offset); | ||
| $iterator = new SearchFilter(new \LimitIterator($iterator, 0, self::MAX_SCANNED_ENTRIES), $query); | ||
| } | ||
|
|
||
| return $this->responseFromIterator($iterator, $count, $offset); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Gets the last item in the log, bypassing any cache. | ||
| * @return mixed | ||
| */ | ||
| private function getLastItem() { | ||
| $iterator = $this->logIteratorFactory->getLogIterator($this->settingsService->getShownLevels()); | ||
| $iterator->rewind(); | ||
| return $iterator->current(); | ||
| } | ||
|
|
||
| /** | ||
| * @brief Use to poll for new log messages since $lastReqId. | ||
| * | ||
|
|
@@ -87,60 +78,105 @@ private function getLastItem() { | |
| * - a combination of reqid and counting the messages for that specific reqid | ||
| * will work in some cases but not when there are more than 50 messages of that | ||
| * request. | ||
| * | ||
| * @note Only the newest MAX_SCANNED_ENTRIES messages are sent. If there are more since | ||
| * $lastReqId, the log viewer misses the older ones and loads some messages twice, until | ||
| * it is reloaded. | ||
| */ | ||
| #[AuthorizedAdminSetting(settings: Admin::class)] | ||
| public function poll(string $lastReqId): JSONResponse { | ||
| public function poll(string $lastReqId): JSONResponse|StreamTraversableResponse { | ||
| $logType = $this->settingsService->getLoggingType(); | ||
| // we only support web access when `log_type` is set to `file` (the default) | ||
| if ($logType !== 'file') { | ||
| $this->logger->debug('File-based logging must be enabled to access logs from the Web UI.'); | ||
| return new JSONResponse([], Http::STATUS_FAILED_DEPENDENCY); | ||
| } | ||
|
|
||
| $lastItem = $this->getLastItem(); | ||
| if ($lastItem === null || $lastItem['reqId'] === $lastReqId) { | ||
| return new JSONResponse([]); | ||
| } | ||
|
|
||
| $iterator = $this->logIteratorFactory->getLogIterator($this->settingsService->getShownLevels()); | ||
| $iterator->rewind(); | ||
|
|
||
| $data = []; | ||
|
|
||
| while ($iterator->valid()) { | ||
| $line = $iterator->current(); | ||
|
|
||
| if ($line['reqId'] === $lastReqId) { | ||
| break; | ||
| } | ||
| return $this->streamJson($this->encodeEntriesNewerThan($iterator, $lastReqId)); | ||
| } | ||
|
|
||
| if (!is_null($line)) { | ||
| $line['id'] = uniqid(); | ||
| $data[] = $line; | ||
| } | ||
| protected function responseFromIterator(\Iterator $iterator, int $count, int $offset): StreamTraversableResponse { | ||
| $iterator->rewind(); | ||
| for ($i = 0; $i < $offset && $iterator->valid(); $i++) { | ||
| $iterator->next(); | ||
| } | ||
| return $this->streamJson($this->encodePage($iterator, $count)); | ||
| } | ||
|
|
||
| return new JSONResponse($data); | ||
| /** | ||
| * @return \Generator<string> | ||
| */ | ||
| private function encodeEntriesNewerThan(\Iterator $iterator, string $lastReqId): \Generator { | ||
| yield '['; | ||
| yield from $this->encodeEntries($this->takeEntriesNewerThan($iterator, $lastReqId)); | ||
| yield ']'; | ||
| } | ||
|
|
||
| protected function responseFromIterator(\Iterator $iterator, $count, $offset): JSONResponse { | ||
| $iterator->rewind(); | ||
| for ($i = 0; $i < $offset; $i++) { | ||
| $iterator->next(); | ||
| /** | ||
| * @return \Generator<string> | ||
| */ | ||
| private function encodePage(\Iterator $iterator, int $count): \Generator { | ||
| yield '{"data":['; | ||
| yield from $this->encodeEntries($this->takeEntries($iterator, $count)); | ||
| yield '],"remain":' . ($iterator->valid() ? 'true' : 'false') . '}'; | ||
| } | ||
|
|
||
| /** | ||
| * One part per entry, so only a single entry is held in memory at a time | ||
| * | ||
| * @param iterable<array> $entries | ||
| * @return \Generator<string> | ||
| */ | ||
| private function encodeEntries(iterable $entries): \Generator { | ||
| $separator = ''; | ||
| foreach ($entries as $entry) { | ||
| $entry['id'] = uniqid(); | ||
| yield $separator . $this->encodeJson($entry); | ||
| $separator = ','; | ||
| } | ||
| $data = []; | ||
| } | ||
|
|
||
| /** | ||
| * Leaves the iterator behind the last taken entry, so its valid() tells whether entries remain | ||
| * | ||
| * @return \Generator<array> | ||
| */ | ||
| private function takeEntries(\Iterator $iterator, int $count): \Generator { | ||
| for ($i = 0; $i < $count && $iterator->valid(); $i++) { | ||
| $line = $iterator->current(); | ||
| if (!is_null($line)) { | ||
| $line['id'] = uniqid(); | ||
| $data[] = $line; | ||
| } | ||
| yield $iterator->current(); | ||
| $iterator->next(); | ||
| } | ||
| return new JSONResponse([ | ||
| 'data' => $data, | ||
| 'remain' => $iterator->valid() | ||
| ]); | ||
| } | ||
|
|
||
| /** | ||
| * @return \Generator<array> | ||
| */ | ||
| private function takeEntriesNewerThan(\Iterator $iterator, string $lastReqId): \Generator { | ||
| foreach ($this->takeEntries($iterator, self::MAX_SCANNED_ENTRIES) as $entry) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correct: with more than 100,000 new entries between two polls, the viewer misses some and loads others twice, until it's reloaded. The limit is a deliberate trade-off, otherwise a poll with an unknown A proper fix needs a truncation signal in the response and a reset in the frontend. I'd do that in a follow-up, so that this PR stays easy to backport. Assisted by ClaudeCode:claude-opus-5-5
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Correction: in Chromium, the viewer already gets stuck from about 60,000 new entries. #2440 fixes that, and then it behaves as described above. Instead of the truncation signal, I proposed paging by position in #2439. With a reset in the frontend, entries could still get lost or be listed twice while the log grows. Assisted by ClaudeCode:claude-opus-5-5 |
||
| if (($entry['reqId'] ?? null) === $lastReqId) { | ||
| return; | ||
| } | ||
| yield $entry; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * @param \Generator<string> $json | ||
| */ | ||
| private function streamJson(\Generator $json): StreamTraversableResponse { | ||
| return new StreamTraversableResponse($json, Http::STATUS_OK, ['Content-Type' => 'application/json; charset=utf-8']); | ||
| } | ||
|
|
||
| /** | ||
| * Like JSONResponse::render(), but never fails and replaces values JSON cannot represent (e.g. INF), as the | ||
| * response is already being sent and skipping the entry would shift the offsets of the next pages | ||
| * | ||
| * @psalm-suppress FalsableReturnStatement, InvalidFalsableReturnType | ||
| */ | ||
| private function encodeJson(mixed $data): string { | ||
| return json_encode($data, JSON_HEX_TAG | JSON_PARTIAL_OUTPUT_ON_ERROR, 2048); | ||
| } | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.