Skip to content

erofs: enable parallel unpack with content cache - #13826

Merged
mxpv merged 2 commits into
containerd:mainfrom
mxpv:cache-parallel
Aug 3, 2026
Merged

mxpv merged 2 commits into
containerd:mainfrom
mxpv:cache-parallel

Conversation

@mxpv

@mxpv mxpv commented Jul 22, 2026 •

Copy link
Copy Markdown
Member

This is a follow up to #13813 to unblock parallel unpack.

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.

This PRs unblock parallel unpack. In case of cache hits, Prepare would 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 has A B D:

  • A, B → staged, no fetch.
  • C → miss → spawns fetch(C, D, E).
  • D → staged, but it's already inside the C..E fetch 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.

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.ErrAlreadyStaged and teach the metadata snapshotter to record the active snapshot while still propagating the staged signal.
  • Update the unpacker to treat ErrAlreadyStaged as “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.

Comment thread core/snapshots/snapshotter.go Resolved
Comment thread plugins/snapshots/erofs/erofs.go Resolved
Copilot AI review requested due to automatic review settings July 22, 2026 02:17
@mxpv
mxpv force-pushed the cache-parallel branch from e4fdb7c to 06002d2 Compare July 22, 2026 02:17
@mxpv mxpv changed the title erofs: make the layer content cache work with parallel unpack erofs: enable parallel unpack with content cache Jul 22, 2026

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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")

@fuweid

fuweid commented Jul 22, 2026

Copy link
Copy Markdown
Member

Example, layers [A B C D E], cache has A B D:

A, B → staged, no fetch.
C → miss → spawns fetch(C, D, E).
D → staged, but it's already inside the C..E fetch range → downloaded anyway (wastefully; the staged snapshot ignores the blob).
E → miss.

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.

@mxpv

mxpv commented Jul 22, 2026 •

Copy link
Copy Markdown
Member Author

or it just has cache for A/B/D without C and E.

ya, this one ^

That's existing logic:
https://github.com/containerd/containerd/pull/13826/changes#diff-30b4c384df0eca60871243f447ea74e5d1ede181733807fdb7e4eaa65700428fR519

    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.

@fuweid

fuweid commented Jul 22, 2026

Copy link
Copy Markdown
Member

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 [A, B, D] in your case? Image layer sharing is prefix-structured by construction, and as far as I know, the warm cache layers hit from shared base images to naturally form a contiguous prefix. So a fragmented layout, it seems that there's GC or some per-layer eviction dropping C from the cache. Just a guess.

@samuelkarp

Copy link
Copy Markdown
Member

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.

@mxpv mxpv moved this from Needs Triage to Needs Reviewers in Pull Request Review Jul 23, 2026
@mxpv mxpv added this to the 2.4 milestone Jul 23, 2026
@mxpv

mxpv commented Jul 23, 2026 •

Copy link
Copy Markdown
Member Author

how common are fragmented layouts like [A, B, D] in your case.

In my case, when I cache image X and run image X on the node, I get 100% cache hits.
The problem is more like with other images that share same layers with X.
And in this case we'd potentially get more cache misses (with things like empty layers in Sam's case).

I'll TAL if we could be more precise after this PR as it makes sense to reuse all cached layers available locally.

@samuelkarp

Copy link
Copy Markdown
Member

@fuweid

fuweid commented Jul 23, 2026

Copy link
Copy Markdown
Member

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.

@mxpv mxpv mentioned this pull request Jul 23, 2026
7 tasks done
@henry118

Copy link
Copy Markdown
Member

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.

@mxpv

mxpv commented Jul 24, 2026

Copy link
Copy Markdown
Member Author

The shared-layer issue seems like a separate issue to address?

Yes, correct. This PR is to unblock parallel unpack when using cache in erofs.

@dmcgowan dmcgowan self-assigned this Jul 24, 2026
Comment thread plugins/snapshots/erofs/erofs.go Resolved
Copilot AI review requested due to automatic review settings July 28, 2026 22:57
@mxpv
mxpv force-pushed the cache-parallel branch from c4cd199 to d6e59cc Compare July 28, 2026 22:57
@mxpv
mxpv requested a review from dmcgowan July 28, 2026 22:58

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Pull request overview

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Comment thread core/mount/mount.go Resolved
Copilot AI review requested due to automatic review settings July 28, 2026 23:05
@mxpv
mxpv force-pushed the cache-parallel branch from d6e59cc to 7adbb84 Compare July 28, 2026 23:05

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 on layerBlob). 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)
		}

@github-project-automation github-project-automation Bot moved this from Needs Reviewers to Review In Progress in Pull Request Review Jul 30, 2026
@dmcgowan
dmcgowan requested a review from henry118 July 30, 2026 23:13
Comment thread core/mount/mount.go Resolved
Comment thread plugins/snapshots/erofs/erofs.go Outdated
// 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 {

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.

Suggested change
if kind == snapshots.KindActive {
if kind == snapshots.KindActive && parent == "" {
cacheBlob = s.lookupCache(ctx, opts...)
}

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.

Yep, fixed. Will take a look more in follow up.

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.

		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.

Comment thread plugins/snapshots/erofs/erofs.go Resolved
mxpv added 2 commits July 31, 2026 09:18
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]>
Copilot AI review requested due to automatic review settings July 31, 2026 16:18
@mxpv
mxpv force-pushed the cache-parallel branch from 7adbb84 to 257a590 Compare July 31, 2026 16:18
@mxpv
mxpv requested a review from fuweid July 31, 2026 16:19

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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.SnapshotOpts is cloned is misleading: within a single unpack() call, topHalf (and its append) 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 concurrent u.unpack() calls sharing Platform.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))

@mxpv
mxpv added this pull request to the merge queue Aug 3, 2026
Merged via the queue into containerd:main with commit 1a34bf3 Aug 3, 2026
88 of 90 checks passed
@github-project-automation github-project-automation Bot moved this from Review In Progress to Done in Pull Request Review Aug 3, 2026
@mxpv
mxpv deleted the cache-parallel branch August 3, 2026 20:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

6 participants