Skip to content

Invalidate cached Drive directories after moves and removals - #408

Open
shcheklein wants to merge 10 commits into
mainfrom
fix/fs-directory-cache-invalidation
Open

shcheklein wants to merge 10 commits into
mainfrom
fix/fs-directory-cache-invalidation

Conversation

@shcheklein

@shcheklein shcheklein commented Nov 22, 2026 •

Copy link
Copy Markdown
Member

Fixes #232.

Directory moves and removals used to leave stale descendants in the path-to-ID cache. The same problem occurred when another Drive client moved a folder and PyDrive2 rediscovered its ID at a different path.

The shared path resolver retains matching folder IDs, including duplicate names, and returns fresh file metadata. Directory mutations replace the affected portion of the cache so older lookups can finish without repopulating it. Batched traversal builds paths from the current Drive parent links, even when children are returned before their renamed parent.

The tests cover local and external moves, trash, failed operations, duplicate names, in-flight lookups, and exact request counts. The live deep-path caching benchmark and its HTTP-level counterpart cover cold creation and warm reads. #409 and #411 build on this foundation.

@shcheklein shcheklein left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

AI-generated review of 61d250680aa2787d0f97e48c427e7e9228bc3511.

Overall the explicit _directory_ids lifecycle and atomic snapshot swap in _without_subtree are clean and well-structured. Inline comments highlight a couple of concrete bugs (mv with implicit-root destinations, invalidate_cache("") on non-root mounts), two concurrency/cross-codebase effects (_create_dir unlocked list read and mv post-upload directory check), an unintended neutralization of an existing deduplication test, and a few targeted test/simplification improvements.

Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/fs/spec.py Resolved
Comment thread pydrive2/test/test_fs_listing.py Resolved

@shcheklein shcheklein left a comment

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

AI-generated re-review of 89c9518d9720b615f5893262200bfe54cc028459 (from scratch).

Looks solid and ready to merge. All earlier edge cases are cleanly resolved:

  • mv() normalizes both paths via _split_mounted_path (fixing implicit-root and trailing-slash moves), checks file1.get("mimeType") == FOLDER_MIME_TYPE from the Drive update response (closing the concurrent-eviction gap), and invalidates both subtrees atomically via _invalidate_paths.
  • invalidate_cache("") works on folder-ID mounts, _create_dir reads/writes under _cache_lock on the resolved cache snapshot, and the HTTP-level tests in test_fs_unit.py + live test in test_fs.py cover all key paths cleanly (including paginated duplicate-ID deduplication in ls).

No new bugs or simplifications to flag.

@shcheklein

Copy link
Copy Markdown
Member Author

The stale-descendant report reproduced on both main and the earlier PR head. After the same folder ID reappeared under new, exists("root/old/child") returned True with zero requests. A warm find() could also emit a file under its former path when Drive returned its renamed parent in the same response.

The fix evicts the affected descendants and detaches older in-flight lookups from the active cache. Traversal uses current Drive parent IDs to assemble paths, including when the child arrives before the parent. Regression tests cover both response orders, duplicate directory names, a lookup finishing during the rename, and an unrelated warm sibling.

I kept invalidation scoped. A global reset would turn the next lookup of that sibling from zero Drive requests into a fresh path resolution. Ordinary file removals continue to skip directory-cache invalidation. The typed path resolver in this PR also serves the stacked mkdir and find changes.

@shcheklein

Copy link
Copy Markdown
Member Author

Addressed the locking and resolver feedback. Name lookup now returns the deepest resolved component without creating anything. Folder creation coordinates by filesystem instance, Drive parent ID and name. Mount discovery and metadata requests run outside the general cache lock. The overlap tests exercise two unrelated creates both on one instance and across instances.

The larger parent-edge cache and generation change is tracked in #413. It documents how snapshots invalidate today, where CPU and memory costs grow, and how to measure the replacement. DVC has a concrete reason to preserve batching: its Google Drive adapter favors traversal under quota pressure, and #393 records a real failure on 251 cache prefixes.

I exercised the real dvc-gdrive adapter and dvc-objects against a simulated Drive at both fsspec 2024.2.0 and 2026.9.0. The adapter's 251-folder traversal took seven requests cold and six warm. A single warm object existence check took one request.

@shcheklein

Copy link
Copy Markdown
Member Author

Fixed the same-filesystem duplicate-create race in 156e028. Two writers could both miss ab when an upload under ef invalidated the cache between their lookups. They took the same creation lock but consulted different copies of the cache. Writers now look up an uncached child and create it, if necessary, under the same parent-ID/name lock. A waiting writer with a different cache sees the folder on Drive. Known parent folders skip the creation lock.

The public cp_file regression test failed on the previous head and passes on the fix. With the real DVC adapter and a simulated Drive, the original interleaving created two folders before the fix and one afterward on #408, #409, #411 and #414. The existing duplicate-root, duplicate-prefix and download cases also passed.

Local benchmark main Previous #408 Fixed #408
Warm folder exists(), one thread, µs per call 1.74 2.35 2.37
Peak simultaneous creates for eight distinct prefixes 1 8 8
Drive requests to upload eight objects to the same new prefix 25 25 18
Full traversal of 251 prefixes, cold / warm requests 7 / 6 7 / 6 7 / 6

The upload benchmark used eight workers with a simulated 25 ms folder create. The remaining scaling concern is selective invalidation: the directory-index copy holds the general cache lock. On #411 the median hold was about 0.10 ms for 256 cached folders, 4 ms for 10,000 and 54 ms for 100,000. DVC's put_file() explicitly invalidates its parent on success. The parent-index redesign in #413 addresses this cost.

@shcheklein
shcheklein added this pull request to stack #416 November 25, 2026 05:23

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement a method to invalidate the cache

1 participant