Repository navigation
erofs: enable parallel unpack with content cache - #13826
Conversation
There was a problem hiding this comment.
Pull request overview
This PR updates the layer-content-cache “remote snapshot” flow to remain compatible with parallel unpack (rebase-at-Commit) by introducing a new snapshots.ErrAlreadyStaged sentinel and switching EROFS cache hits from “commit during Prepare” to “stage during Prepare, commit later”.
Changes:
- Add
snapshots.ErrAlreadyStagedand teach the metadata snapshotter to record the active snapshot while still propagating the staged signal. - Update the unpacker to treat
ErrAlreadyStagedas “skip fetch/apply, but still commit (with rebase parent)”. - Update the EROFS snapshotter cache-hit path to stage cached layer blobs into the active snapshot and return
ErrAlreadyStaged(and advertise rebase unconditionally).
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| plugins/snapshots/overlay/plugin/plugin.go | Uses shared snapshots.RebaseCap capability constant. |
| plugins/snapshots/erofs/plugin/plugin.go | Always advertises snapshots.RebaseCap now that cache hits no longer break rebase. |
| plugins/snapshots/erofs/erofs.go | Switch cache hits from “commit in Prepare” to “stage in Prepare” and return ErrAlreadyStaged. |
| plugins/snapshots/erofs/erofs_linux_test.go | Updates cache-hit tests to validate staged behavior + commit finalization. |
| core/unpack/unpacker.go | Adds staged-layer handling: skip fetch/apply, still serialized commit with rebase parent. |
| core/unpack/unpacker_test.go | Adds coverage for staged-layer behavior in parallel mode. |
| core/snapshots/snapshotter.go | Introduces ErrAlreadyStaged and RebaseCap. |
| core/metadata/snapshot.go | Threads ErrAlreadyStaged through metadata snapshotter Prepare path. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 8 out of 8 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
core/snapshots/snapshotter.go:38
- ErrAlreadyStaged is defined as a plain sentinel (errors.New) and is propagated/checked via errors.Is in the metadata snapshotter and unpacker. This will not survive the snapshots gRPC boundary: snapshotservice/service.go maps errors through errgrpc.ToGRPC (errdefs-based), and core/snapshots/proxy converts back with errgrpc.ToNative, so a remote/proxied snapshotter returning this condition will arrive as ErrUnknown and the staged fast-path will be skipped.
// ErrAlreadyStaged is returned from Prepare when the snapshotter has staged a
// layer's content into the active snapshot (e.g. from a layer content cache) so
// the caller should skip fetching and applying the layer. Unlike
// ErrAlreadyExists — which reports an already-committed snapshot and ends the
// layer's lifecycle — the snapshot is NOT committed: the caller must still Commit
// it, which is where the parent is applied. This keeps the cache compatible with
// parallel unpacking (the "rebase" capability), where the parent is only known
// at Commit time.
var ErrAlreadyStaged = errors.New("snapshot already staged")
Do you have example image for this which only has A/B/D? or it just has cache for A/B/D without C and E. |
ya, this one ^ That's existing logic: if fetchErr == nil {
fetchOffset = i
n := len(layers) - fetchOffset
fetchErr = make([]chan error, n)
fetchC = make([]chan struct{}, n)
for i := range n {
fetchC[i] = make(chan struct{})
fetchErr[i] = make(chan error, 1)
}
go func(i int) {
err := u.fetch(ctx, h, layers[i:], fetchC)
if err != nil {
for _, fc := range fetchErr {
fc <- err
close(fc)
}
}
}(i)
}It's just that for cache scenario it'd make sense to make it more precise to avoid unnecessary cache misses. |
|
Right now I don't see a better way to support non-contiguous cached layers without introducing a new concept like "staged", so I'm fine with the approach — it gets 100% reuse on the conversion side. One question for the layout: how common are fragmented layouts like |
|
It's pretty easy to have fragmented layouts in practice. Empty layers in the middle of a manifest turn up relatively frequently, as do layers with a single file where that file itself is a common file. |
In my case, when I cache image X and run image X on the node, I get 100% cache hits. I'll TAL if we could be more precise after this PR as it makes sense to reuse all cached layers available locally. |
|
https://samuel.karp.dev/blog/2024/12/containerd-internals-images/ some examples of different layer sharing the the diagrams there. |
|
Thanks @mxpv and @samuelkarp for the sharing. My experience on that is to rebuild with new base image so I see a lot of images sharing the same prefix. |
|
This approach LGTM. The new error type allows the operation to continue through the bottom half, so the snapshot chain is built correctly. The shared-layer issue seems like a separate issue to address? As I would expect it to occur regardless of whether parallel unpack is enabled. |
Yes, correct. This PR is to unblock parallel unpack when using cache in erofs. |
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.
Comments suppressed due to low confidence (1)
plugins/snapshots/erofs/erofs.go:329
- Staging the cached layer as a symlink means the later Commit() will run fs-verity enabling and/or IMMUTABLE_FL setting on
layerBlob(see Commit() calling fsverity.Enable / setImmutable onlayerBlob). With a symlink, those operations target the operator-owned cache blob and can fail if the cache is mounted read-only, violating the expectation that containerd never writes to the cache.
Consider materializing the cached blob into the snapshot directory when fs-verity or immutable is enabled (copy/reflink), so Commit mutates only local state.
if cacheBlob != "" {
layerBlob := filepath.Join(td, "layer.erofs")
if err := os.Symlink(cacheBlob, layerBlob); err != nil {
return td, fmt.Errorf("failed to symlink cached layer blob: %w", err)
}
| // layer content cache; View and container-rootfs Prepares get an empty path | ||
| // and fall through to the normal path. | ||
| var cacheBlob string | ||
| if kind == snapshots.KindActive { |
There was a problem hiding this comment.
| if kind == snapshots.KindActive { | |
| if kind == snapshots.KindActive && parent == "" { | |
| cacheBlob = s.lookupCache(ctx, opts...) | |
| } |
There was a problem hiding this comment.
Yep, fixed. Will take a look more in follow up.
There was a problem hiding this comment.
if i > 0 && !parallel {
parent = chainIDs[i-1].String()
}if parent is present, the mount will return overlayfs mount with upperdir.
the isStage will return false and it will continue to unpack the content.
The symlink will be removed (probably including original cache).
For such case, we can just return ErrExist error in the followup.
The cache used to serve a hit by committing the layer during Prepare and returning ErrAlreadyExists, which is incompatible with parallel unpacking: the "rebase" capability defers the parent to Commit time, so a commit-at-Prepare layer ends up parentless. The erofs snapshotter therefore had to disable rebase whenever the cache was enabled, making every cache *miss* fall back to a fully sequential (slower-than-cold) pull. Introduce snapshots.ErrAlreadyStaged, returned from Prepare to mean "the active snapshot's content is staged; skip the layer download and apply, but still Commit it" (where the parent is applied). Unlike ErrAlreadyExists it does not end the layer's lifecycle. The unpacker handles it by emitting a status whose bottom half runs the normal serialized commit (rebasing the parent in), and the metadata snapshotter threads it through Prepare like a normal active snapshot. The erofs cache hit now stages the blob and returns ErrAlreadyStaged instead of committing, so it can advertise "rebase" unconditionally: hits skip download+conversion and misses stay parallel. Also promote the "rebase" capability string to snapshots.RebaseCap (shared by the unpacker, erofs, and overlay). Follow up: fetch is range-based, so only a fully cached contiguous prefix skips downloads; a miss in a lower layer still pulls everything above it. Per-layer fetch-skip is left as a follow-up. Signed-off-by: Maksym Pavlenko <[email protected]>
Replace the snapshots.ErrAlreadyStaged sentinel error with a read-only-mounts signal: a snapshotter (e.g. erofs serving a layer content cache hit) now returns Prepare mounts normally, with no error, when the layer content is already staged into the active snapshot. The unpacker's isStaged helper checks the last mount's ReadOnly() to decide whether to skip fetch+apply and just commit. Signed-off-by: Maksym Pavlenko <[email protected]>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 9 out of 9 changed files in this pull request and generated no new comments.
Suppressed comments (1)
core/unpack/unpacker.go:410
- The comment explaining why
unpack.SnapshotOptsis cloned is misleading: within a singleunpack()call,topHalf(and itsappend) runs sequentially over layers (the parallelism is in the apply/fetch goroutines and the serialized bottom half). If the intent is to avoid races from concurrentu.unpack()calls sharingPlatform.SnapshotOpts, the comment should say that explicitly so future readers don't infer per-layer concurrent mutation here.
// Clone before appending: topHalf runs concurrently per layer in
// parallel mode, and appending directly to unpack.SnapshotOpts could
// write into its shared backing array from multiple goroutines.
opts = append(slices.Clone(unpack.SnapshotOpts), snapshots.WithLabels(snapshotLabels))
This is a follow up to #13813 to unblock parallel unpack.
The cache used to serve a hit by committing the layer during
Prepareand returningErrAlreadyExists, which is incompatible with parallel unpacking: the "rebase" capability defers the parent to Commit time, so a commit-at-Prepare layer ends up parentless. The erofs snapshotter therefore had to disable rebase whenever the cache was enabled, making every cache miss fall back to a fully sequential (slower-than-cold) pull.This PRs unblock parallel unpack. In case of cache hits,
Preparewould return RO mounts. In which case, the unpacker would skip download+unpack and fast forwards directly to commit.NOTE: this is partial, "good-enough" mitigation: fetch is range-based, so only a fully cached contiguous prefix skips downloads; a miss in a lower layer still pulls everything above it. Per-layer fetch-skip is left as a follow-up.
Why partial: a staged (cache-hit) layer returns before this block, so it never triggers a fetch. But once some miss triggers it, the fetcher grabs the whole tail — including any cache hits that happen to sit above that miss.
So the only layers guaranteed not to download are the cache hits forming an unbroken run from layer 0 — the "contiguous cached prefix."
Example, layers
[A B C D E], cache hasA B D:A, B→ staged, no fetch.C→ miss → spawnsfetch(C, D, E).D→ staged, but it's already inside theC..Efetch range → downloaded anyway (wastefully; the staged snapshot ignores the blob).E→ miss.If we think this is the right direction, I can follow up with more precise fetcher.