Skip to content

perf(FileListener): early return from fs events where node does not q… - #299

Open
kyteinsky wants to merge 17 commits into
mainfrom
perf/file-listener-fixes
Open

kyteinsky wants to merge 17 commits into
mainfrom
perf/file-listener-fixes

Conversation

@kyteinsky

@kyteinsky kyteinsky commented Nov 10, 2026 •

Copy link
Copy Markdown
Contributor

…ualify

fixes #297

🤖 AI (if applicable)

  • The content of this PR was partly or fully generated using AI (N/A)

…ualify

Signed-off-by: kyteinsky <kyteinsky@gmail.com>
@kyteinsky

Copy link
Copy Markdown
Contributor Author

noticed we have NodeRemovedFromCache but no NodeAddedToCache, instead we listen to CacheEntryInsertedEvent
should we switch to NodeAddedToCache for consistency?

Comment thread lib/Listener/FileListener.php Resolved
@marcelklehr

Copy link
Copy Markdown
Member

should we switch to NodeAddedToCache for consistency?

I vaguely remember CacheEntryInserted also covers external storage which others might not, but not sure

Signed-off-by: kyteinsky <kyteinsky@gmail.com>
@marcelklehr

Copy link
Copy Markdown
Member

mmh.

A file deleted while still queued is never removed from the queue (lib/Listener/FileListener.php:107, and the same at :128). A queued file keeps its fileid when it moves to the trash. When the trash is emptied, the cache-removed event's path starts with files_trashbin/. The new allowedPath(..., relativePath: true) check returns early on that path, so the queue row stays.

Comment thread lib/Listener/FileListener.php Resolved
Comment thread lib/Listener/FileListener.php Resolved
@marcelklehr

Copy link
Copy Markdown
Member

Moving a file to another storage loses its pending update flag (FileListener.php:112). The move fires "removed" then "inserted" for the same id. The queue row is deleted and re-added with update=false, so a pending update is treated as a new file. When a folder moves, its children keep queue rows that still carry the old storage_id and root_id.

Signed-off-by: kyteinsky <kyteinsky@gmail.com>
@kyteinsky

Copy link
Copy Markdown
Contributor Author

Moving a file to another storage loses its pending update flag (FileListener.php:112). The move fires "removed" then "inserted" for the same id. The queue row is deleted and re-added with update=false, so a pending update is treated as a new file.

can confirm but I suppose this is fine, we only plan to use this flag for prioritisation later on and right now it's just for printing the metric.

When a folder moves, its children keep queue rows that still carry the old storage_id and root_id.

this was reproducible too, got 3 events in a live test moving a folder from home storage to a groupfolder.
It retains the file id as expected.
fixed it by modifying the insertIntoQueue function to update the storage and root ids when the file exists in queue instead of just returning the passed in QueueFile object.
doesn't seem like there is any regression from this but please have a look too.

224123a9-ffda-4165-8efd-966851b7ab0e 917fdff3-9e71-4be1-b06b-d38fae1fafd9 c6c545a0-14a4-4a41-a982-60d56c957727

Signed-off-by: kyteinsky <kyteinsky@gmail.com>
@kyteinsky
kyteinsky force-pushed the perf/file-listener-fixes branch from 4068d1d to 69c5cd7 Compare November 13, 2026 10:03
Signed-off-by: kyteinsky <kyteinsky@gmail.com>
Signed-off-by: kyteinsky <kyteinsky@gmail.com>
Signed-off-by: kyteinsky <kyteinsky@gmail.com>
Assisted-by: Copilot:claude-code-5-5
@kyteinsky
kyteinsky force-pushed the perf/file-listener-fixes branch from 69c5cd7 to a5d6c94 Compare November 13, 2026 10:54
@nextcloud-command nextcloud-command added the AI assisted This PR contains AI-assisted commits label Nov 13, 2026
Comment thread lib/Service/FsEventService.php Resolved
Comment thread lib/Service/FsEventService.php Resolved
Comment thread lib/Service/FsEventService.php Resolved
Comment thread lib/Service/FsEventService.php Resolved
kyteinsky and others added 6 commits November 23, 2026 13:29
Co-authored-by: Marcel Klehr <mklehr@gmx.net>
Signed-off-by: Anupam Kumar <kyteinsky@gmail.com>
Signed-off-by: kyteinsky <kyteinsky@gmail.com>
The path checks for versions and trash assumed the layout of a home or
groupfolder storage on every storage, so folders of external storages
named trash, versions, files_versions or files_trashbin were never indexed.

- classify the storage of the cache event: home (only files/), groupfolder
  with a separate storage (not trash/ and versions/), root (only
  __groupfolders/<id>/) and everything else (no restriction)
- do not filter removals caused by a move to another storage by layout,
  the event carries the target storage with the source path
- add unit tests with mocked storages and integration tests that run the
  scanner on the real groupfolders and files_external storages
- add a workflow to run them against groupfolders on stable33-35 and master

Signed-off-by: kyteinsky <kyteinsky@gmail.com>
Assisted-by: Claude Code:claude-sonnet-5-5
… a database

- tearDown accessed the uninitialised user when setUp skipped the test, so
  the generic phpunit workflows (no groupfolders) errored instead of skipping
- only delete the actions the test queued, not the whole action queue
- correct the comment on jailed storages: groupfolders with encryption fire
  their cache events through the jail too, those are filtered by mimetype

Signed-off-by: kyteinsky <kyteinsky@gmail.com>
Assisted-by: Claude Code:claude-sonnet-5-5
Jailed storages were allowed outright. With groupfolders encryption, all
groupfolder cache events carry the jail, so files with an indexable name
in the groupfolder trash or versions passed the path check; their removal
scheduled delete actions.

- unjail the storage and path before applying the layout rules
- for inserts, read the unjailed path from the filecache row, groupfolders
  rewrites inserted paths relative to its jail
- add a psalm stub for the private Wrapper and Jail classes instead of
  suppressing them
- shorten the comments and link the code they refer to
- test jailed mounts in the unit tests, and groupfolders:scan with
  groupfolders encryption off and on in the integration test

Signed-off-by: kyteinsky <kyteinsky@gmail.com>
Assisted-by: Claude Code:claude-sonnet-5-5
A scan that finds a file changed on the storage (external storage, data
directory) only fires CacheEntryUpdatedEvent, which was not handled, so
the file was never re-indexed.

- handle CacheEntryUpdatedEvent like an insert, queued as an update
- check the extension and the storage layout before any lookup, for
  inserts and updates alike: most cache events of a scan are folders (#297)
- skip the fs event of renames whose target is a non-indexable file
- test the updated event, the folder short-cut and renames; integration
  test an edit on an external storage

Signed-off-by: kyteinsky <kyteinsky@gmail.com>
Assisted-by: Claude Code:claude-sonnet-5-5
A restore moves a folder's children with one SQL update and no cache events,
and the folder itself is dropped by the extension check, so restored files
were never indexed again. groupfolders emits no NodeRestoredEvent, only the
legacy post_restore hook, which the user trashbin emits too.

- listen to the post_restore hook and schedule the restored node; folders
  re-index their children in the background job
- share the cheap indexable-node check with the rename handling
- todo: switch to NodeUpdatedEvent once groupfolders emits it for restores
- unit tests for the listener; integration tests restoring a folder from
  the user trash and from a groupfolder trash

Signed-off-by: kyteinsky <kyteinsky@gmail.com>
Assisted-by: Claude Code:claude-sonnet-5-5
Signed-off-by: kyteinsky <kyteinsky@gmail.com>
The first update of a trash restore still has the octet-stream mimetype of
the trashed name; the server's mimetype fix fires another update in the
same request.

Assisted-by: Claude Code:claude-sonnet-5-5
Signed-off-by: kyteinsky <kyteinsky@gmail.com>
The post_restore hook was connected in boot(), which never runs on WebDAV
requests: remote.php loads only authentication/filesystem/logging apps, so
restores from the Files UI were not re-indexed. Listen to the typed
NodeRestoredEvent instead, registered lazily, which fires on DAV too.
A restore is handled like a rename: schedule the target node, filtered
by isIndexableNode(). Drop the separate TrashRestoreListener and cover
both events in the same unit test.

- schedule the event's target node, with the indexable-node filter for files
- groupfolders does not dispatch the event yet: its restore test skips
  until it does; groupfolder restores re-index single files only
- psalm stub for the files_trashbin event

Assisted-by: Claude Code:claude-sonnet-5-5
Signed-off-by: kyteinsky <kyteinsky@gmail.com>
@kyteinsky

Copy link
Copy Markdown
Contributor Author

now two more events are listened to NodeRestoredEvent (for trashbin restores) and CacheEntryUpdatedEvent (for file updates outside NC)

groupfolders does not emit NodeRestoredEvent on trash restores, the PR for that is here: nextcloud/groupfolders#5287

@kyteinsky

kyteinsky commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor Author

phpunit storages job fails on stable34 due to this not being merged: nextcloud/server#64497

*/
public function allowedCacheEventPath(ICacheEvent $event): bool {
$storage = $event->getStorage();
if ($storage->getCache()->getNumericStorageId() !== $event->getStorageId()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Doesn't this mean that files moved between storages, e.g. from home to a groupfolder, would now queue a delete and a re-index? This would be a) unnecessary and b) a race condition, possibly leaving the file unindexed, depending on the order these two are run in.

// folders are detected as "application/octet-stream" which is fine for this use-case
// since they are not indexable either, the files inside the folder are received in
// separate cache entry events
if (!$this->allowedMimeType($this->mimeTypeDetector->detectPath($event->getPath()))) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

And what about README files?

$fileRefs[] = ProviderConfigService::getSourceId($fileId);
}
try {
$this->actionService->deleteSources($fileRefs);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Should we also remove the files from the queue in case they're still queued?

return;
}

$node = $this->rootFolder->getFirstNodeById($storageCacheEntry->getId());

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This is quite expensive, I think. CacheEntryUpdatedEvenjt seems to be called multiple times per upload or edit. Should we check if the affected storage is actually an external storage? Otherwise NodeWrittenEvent does the job fine, I think.

@marcelklehr

marcelklehr commented Nov 25, 2026 •

Copy link
Copy Markdown
Member

it seems at the moment each deleted file is deleted 2–3 times in the backend: BeforeNodeDeletedEvent, NodeRemovedFromCache and CacheEntryRemovedEvent each queue their own delete for the same id. A trash restore is queued once by CacheEntryUpdatedEvent and again by NodeRestoredEvent. Renames within one storage are double-queued the same way.

@marcelklehr

Copy link
Copy Markdown
Member

Sorry the review isn't greener :/

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

Labels

AI assisted This PR contains AI-assisted commits

Projects

None yet

Development

Successfully merging this pull request may close these issues.

CacheEntryInsertedEvent performs expensive global node resolution for directories

3 participants