cache: load parent chain only once in remote cache LoadWithParents - #7129
Draft
ljluestc wants to merge 1 commit into
Draft
cache: load parent chain only once in remote cache LoadWithParents#7129ljluestc wants to merge 1 commit into
ljluestc wants to merge 1 commit into
Conversation
On a remote cache match, LoadWithParents loads the matched record and every parent record so that cache metadata is saved for all parent layers. Each of these records was loaded with its own worker.FromRemote call, and because every parent's remote contains the full descriptor chain up to that record, each call verified and loaded all the layers below it again, resulting in O(n^2) layer verifications for a chain of n layers. This is especially costly when layer verification hits the local disk (e.g. via the moby download manager). Since every parent record's remote is a strict prefix of the matched result's remote, collect the records during the walk, call FromRemote once for the full chain, and take each record's ref from the returned ref's LayerChain instead. Each layer is now verified and loaded only once. If a worker returns a ref whose layer chain does not map 1:1 to the remote descriptors, the affected records fall back to the previous per-record load. This also fixes a ref leak where multiple results on the same item overwrote earlier entries in the result map without releasing them. Fixes moby#2184
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
cache: load parent chain only once in remote cache LoadWithParents
Fixes #2184
Problem
When a remote cache match is found, cacheManager.LoadWithParents loads not only the matched record but every
parent record as well, so that cache metadata is saved for all parent layers and a later build with only loca
cache can still get a partial match.
In cacheResultStorage.LoadWithParents (cache/remotecache/v1/cachestorage.go), each of those records was loaded with its own worker.FromRemote call. Every parent record's solver.Remote contains the full descriptor chain
from the root up to that record, so each call verified (Provider.Info) and loaded (GetByBlob) all the layers below it again. For a chain of n layers this is n + (n-1) + ... + 1 = O(n²) layer verifications. The checks
are cheap in the common case, but noticeable for large images, and expensive in the moby integration where verification of existing layers goes through the download manager and touches the local disk.
Approach
The key observation is that every record collected by the walk passes isSubRemote(subRes.Result, result.Result), i.e. its remote is a strict prefix of the matched result's remote. So there is no need to load each prefix independently:
Each layer is now verified and loaded exactly once — O(n) — while the results map handed back to the solver is unchanged, so cache metadata is still recorded for every parent record.
No function signatures change, and no state needs to be threaded through the solver: the "parent already loaded" state the issue mentions is exactly what the loaded ref's layer chain already represents.
Safety fallback
If a worker ever returns a ref whose layer chain does not map 1:1 onto the remote's descriptors, the affected records fall back to the previous per-record FromRemote call, so behavior is preserved for non-standard workers.
Also fixed
The old code did m[id] = worker.NewWorkerRefResult(...) for every matching result of an item, overwriting earlier entries for the same id without releasing them — a ref leak. Overwritten entries are now released.
Testing