Repository navigation
engine: reclaim released snapshot data and count retained parent layers - #14470
Merged
Merged
Conversation
Removing the last lease on a snapshot only marks containerd's metadata DB dirty; the data stays on disk until GarbageCollect runs. The engine ran that collection only after a prune removed at least one entry. Leases are released all the time without a prune (session teardown, failed execs, stopped services), so an engine whose policies never removed an entry kept that data forever, and none of it was counted in the cache's usage. Track deletions with containerd's mutation callback and collect any pending garbage at the end of every GC pass, whatever the policies decided. GC passes already run after sessions end (at most once a minute) and under disk pressure. The first pass after startup always collects, because containerd's deletion count does not survive a restart. Signed-off-by: Erik Sipsma <[email protected]>
A result's cache usage was the size of its own top snapshot: its overlay diff plus its blob. Its owner lease retains the whole parent chain, though, so the lower layers of a pulled image, and the layers under a result whose own entry was gone, stayed on disk without being counted anywhere. A pulled python:3.12-slim used 163MB on disk and was counted as 17KB. The space limits compare against this total, so they could not see much of what filled the disk. Make each result's usage identities its snapshots plus their ancestors. Shared layers are already deduplicated by identity: each layer counts once, against the earliest result listing it, and the prune simulation credits a layer only once every result listing it is collected. So an image's `from` result shows the whole image, an exec stacked on others still shows and reclaims only its own layer while the results below it are alive, and layers left behind by collected results are charged to the result still retaining them. Parents are looked up through a new SnapshotParent method, outside the graph lock. A snapshot's parent never changes, so each usage pass reuses the links the previous pass resolved and keeps only the ones it visited, which bounds the carried map by the live snapshots. Signed-off-by: Erik Sipsma <[email protected]>
The engine.json reference had no garbage collection section. Describe the space settings and their defaults, how a policy's keepDuration interacts with free space, why a custom policy list should end with a catch-all policy, and how engine.toml settings relate to engine.json. Signed-off-by: Erik Sipsma <[email protected]>
Disabling GC stops the engine from choosing cache entries to drop. Data that nothing references anymore is not such a choice, so it is still collected. Document that, and test it so the behavior stays deliberate. Signed-off-by: Erik Sipsma <[email protected]>
An operation's lazy lease scope protects the snapshots the operation is still producing, such as an exec's output or a service's root filesystem. It was created with a one-hour expiry, and containerd stops treating an expired lease as a root. Any garbage collection after the deadline could delete snapshots a long-running exec or service was still using. Collection now runs on every GC pass, which made that far more likely. Create operation leases without an expiry. The scope deletes the lease when its operation ends, and the engine releases leases an earlier process left behind at startup, as it already does for transfer leases. Signed-off-by: Erik Sipsma <[email protected]>
An explicit prune ran the prune stages but only collected garbage when they removed an entry, so data released outside any prune stayed on disk. Collect pending garbage there too, as a GC pass does. Signed-off-by: Erik Sipsma <[email protected]>
A row whose parent chain was only partly resolved listed fewer ancestors than it retains. The prune simulation then credited those layers as reclaimed once their other holders were collected, though the row still kept them on disk. That happened for rows that appeared after sampling, whose parents are only looked up outside the graph lock, and when a parent lookup failed. Track whether each row's chains are complete, and defer the pass like any other incomplete membership when one is not. A failed lookup is not remembered, so the next pass retries it. Also document that merge snapshots do not charge data hardlinked from other merge inputs. Signed-off-by: Erik Sipsma <[email protected]>
reservedSpace is a floor for pruning to maxUsedSpace, not the size that pruning reaches; maxUsedSpace is. keepDuration does not keep an entry whose cache TTL has expired. Signed-off-by: Erik Sipsma <[email protected]>
Pruning removes whole cache entries until it reaches its target, so a pass can go past maxUsedSpace or below reservedSpace, and pruning for minFreeSpace does not consider reservedSpace. Describe the settings as targets rather than hard limits. Signed-off-by: Erik Sipsma <[email protected]>
Each usage pass rebuilt every row's expanded identities from the parent links: walking its chain, allocating a new slice, and sorting it, although a snapshot's chain never changes. On a synthetic cache of 20,000 results with chains about 35 deep, that was the largest single cost of a pass. Cache each snapshot's sorted chain alongside its parent, carried between passes and bounded by the live snapshots like the parents. Rows on the same snapshot share the slice. Decide whether a payload can size an identity by checking its own identities, instead of building a map of ancestors per row. Signed-off-by: Erik Sipsma <[email protected]>
Prune measured result sizes and rebuilt the prune state for every policy, although a policy only changes the cache by removing entries. Each measurement takes the graph lock twice, so a default three-policy pass that removed nothing held it six times. Measure once per pass, and again only after a policy removes entries or a measurement was deferred. On a synthetic cache of 20,000 results with chains about 35 deep, a three-policy pass that removes nothing now takes about 450-510ms and takes the lock twice. Before parent chains were counted it took about 750ms with six lock holds. Signed-off-by: Erik Sipsma <[email protected]>
A usage pass hands its resolved chains to later passes as soon as sampling ends, and those passes read them without a lock. A row first seen during finalization then cached its newly assembled chain into the handed-off map, racing with those readers; the race detector flags it. Mark the chains published at handoff, and have finalization assemble chains without caching them. Such rows are rare, so not caching them costs nothing. Signed-off-by: Erik Sipsma <[email protected]>
Reusing a pass's measurement across policies also reused its view of which results retain which layers. A result published between policies could then still be retaining layers that a later policy counted as reclaimed when it pruned their other holder. Reuse the measurement only to decide a policy has nothing to do, and measure again before planning removals. A pass that prunes nothing still measures once. Add a test hook that runs before each policy, so the test can publish a result between policies. Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
sipsma
force-pushed
the
cache-gc-released-snapshots
branch
from
October 3, 2026 23:16
4876beb to
18d8526
Compare
TomChv
approved these changes
Oct 5, 2026
sipsma
added a commit
that referenced
this pull request
Oct 5, 2026
When an exec fails and opens a terminal on its output, the terminal shows each writable mount's failed output. It did so by writing through the input mount's accessors, but since #14043 execInputMounts shares those with the exec's parent container, which is a cached result. The cached container then pointed at the failed output snapshots, which only the failed operation holds. That silently gave the next exec from the cached container the failed run's files instead of its inputs. Since #14470 collects released snapshots on every GC pass, the output is usually gone by then, and the exec fails with "parent snapshot ... does not exist", as in TestModule/TestDaggerTerminal/on_failure. Install the failed output on the terminal's own cloned mount with a new accessor, and leave the input mounts alone. Signed-off-by: Erik Sipsma <[email protected]>
neongreen
added a commit
to neongreen/dagger
that referenced
this pull request
Oct 20, 2026
Port of upstream 42edb39 (dagger#14470) onto v0.21.7. An operation's lazy lease scope protects the snapshots the operation is still producing, such as an exec's root filesystem. v0.21.7 creates it with a one-hour expiry, and containerd stops treating an expired lease as a GC root, so any collection after the first hour deletes snapshots a long-running exec is still using. Create operation leases without an expiry and label them dagger.io/operation=true. The scope deletes the lease when its operation ends. At startup the engine deletes leases carrying exactly that label, right after creating the lease manager and before the snapshot manager, the dagql cache or any session use it; a failure stops engine start. Unlabelled and pre-patch operation leases are untouched; the latter still expire on their own. The upstream test depends on engine/snapshots/testutil, which v0.21.7 lacks, so the tests here use a real containerd metadata store, the native snapshotter and containerd's own garbage collector. Co-Authored-By: Claude Opus 5.5 <[email protected]>
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.
A comment on #7711 reports an engine filling a 1 TiB disk in about 11 hours. The GC logged
no reclaim targetevery pass, withusedBytesaround 14 GB while the worker directory held 570 GB.Two engine bugs explain how disk usage can grow far beyond what the GC sees:
GarbageCollectonly after a dagql prune removed at least one entry. Leases are released all the time outside any prune: session teardown, failed execs, stopped services. An engine whose policies never removed anything therefore kept that data forever, and none of it was counted. The commenter's policies all had a 72hkeepDuration, which is exactly that case.Changes
Collecting released data
snapshotGarbagetracks deletions through containerd'sRegisterMutationCallback. Every GC pass, and every explicitlocalCache.prune, collects whatever is pending, whatever the policies decided.gc.enabled=false. Disabling GC stops the engine from choosing entries to drop, but this data is no longer referenced by anything.Counting parent layers
SnapshotManager.SnapshotParentoutside the graph lock. Ancestors are sized by snapshot ID.fromshows the whole image;Keeping the extra accounting cheap
Prunemeasured usage for every policy, taking the graph lock twice each time. It now measures once per pass, and measures again before a later policy plans removals or after a policy removes entries.Docs. The engine.json reference gets a garbage collection section. It covers:
keepDurationbehaves under disk pressure;gckeepstorage, relate to engine.json.internal-docs/cache_pruning.mdis updated as well.Limitations
keepDurationstill keeps recently used entries out of their policy under disk pressure, as before. The new docs describe that.Validation
go test ./dagql/ ./engine/snapshots/ ./engine/server/ ./core/ ./core/schema/ -skip 'TestNativeWorkspaceMerge|TestGitNativeCommit': passes. The skipped git-native tests need a newer git than the 2.43 on the machine used.go test -race ./dagql/ -run 'TestCacheUsage|TestCachePrune': passes.keepDuration72h and 48h):python:3.12-slimis counted at 188 MB, matching its disk usage. Before, it was counted as 17 KB.