Skip to content

engine: prune DAGQL cache by metadata size - #13846

Merged
sipsma merged 20 commits into
dagger:mainfrom
sipsma:metadata-memory-pruning-implementer-d394f80d
Aug 5, 2026
Merged

sipsma merged 20 commits into
dagger:mainfrom
sipsma:metadata-memory-pruning-implementer-d394f80d

Conversation

@sipsma

@sipsma sipsma commented Aug 5, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Persisted DAGQL cache roots can retain substantial in-memory result, e-graph, module, and schema state while consuming little or no physical cache space. Existing automatic pruning is triggered by disk usage, so those roots can remain indefinitely when disk thresholds are not crossed.

This change adds a coarse structural estimate for DAGQL cache metadata:

3072 * results + 512 * terms + 768 * allocated equivalence-class slots

Reading the estimate is O(1) and allocation-free because these counts already exist in the cache. When the estimate exceeds the provisional 4 GiB maximum, the engine uses the existing pruning candidate order and ownership simulation to prune toward 3 GiB. This makes zero- and tiny-disk persisted roots eligible without weakening active-session or unpruneable-root protection.

Disk GC remains independent. Disk-stat failures do not suppress structural pruning, and structural no-progress suppression does not suppress disk policies. The persistence schema remains version 17; an oversized graph may still create an import peak before the scheduled startup trim.

The change also adds:

  • gc.dagqlCache.maxEstimatedBytes and targetEstimatedBytes configuration;
  • dagger_dagql_cache_metadata_estimated_bytes;
  • aggregate-only pruning logs and traces;
  • schema-17 restart/startup coverage, zero-disk integration coverage, race tests, and scale/calibration benchmarks.

The 4 GiB/3 GiB defaults are provisional rollout values. The estimate deliberately approximates cache-owned memory rather than attributing exact retained heap.

Performance

At one million results on the benchmark host:

  • estimate read: 6.5 ns/op, zero allocations;
  • structural snapshot: 1.004 s, versus 2.025 s for the existing disk snapshot path;
  • slowest measured full-prune fixture: 27.318 s;
  • maximum sampled pruning scratch: 888,102,912 bytes;
  • sparse forced class compaction from 1,000,000 slots to 2,000: 31.7 ms write-lock hold.

Testing

On the rebased head:

go test -race ./dagql -run '^(TestCacheMetadata|TestCachePruneMetadata|TestRemovePersistedEdge|TestLazyRefArg)' -count=1
go test -race ./engine/server -run '^(TestResolveDagqlCacheGCConfig|TestGCLocked.*Metadata.*|TestScheduledGCTrimsImportedMetadataWithoutPersistenceReset|TestLocalCachePressureCheckUsesMetadataWhenDiskStatFails|TestMetadataPrune.*|TestMonitorMetadata.*|TestGCSessionCompletionClearsMetadataPruneBlock|TestExplicitPruneClearsMetadataPruneBlock|TestCanceledMonitorMetadataPruneDoesNotSetBlock|TestEnsureWorkspaceLoaded.*)$' -count=1
go test ./dagql ./engine/server ./engine/engineutil ./core/schema -count=1
go test ./cmd/json-schema ./engine/config -count=1
go run ./cmd/json-schema engine.json > /tmp/engine.schema.json
cmp docs/static/reference/engine.schema.json /tmp/engine.schema.json
dagger api call engine-dev test --pkg ./core/integration --run='TestLocalCache/TestDagqlMetadataGCProtectsActiveZeroDiskResults'
dagger api call engine-dev test --pkg ./core/integration --run='TestEngine/TestPrometheusMetrics'

The metadata-pressure E2E passed with trace b22591406cd051b4de63502a8d7ae884; the metrics E2E passed with trace 9146773cba455b106d9255c81edf24e8.

After the lint-only follow-up commit, the affected server race suite and repository lint check also passed:

go test -race ./engine/server -run '^(TestResolveDagqlCacheGCConfig|TestGCLocked.*Metadata.*|TestScheduledGCTrimsImportedMetadataWithoutPersistenceReset|TestLocalCachePressureCheckUsesMetadataWhenDiskStatFails|TestMetadataPrune.*|TestMonitorMetadata.*|TestGCSessionCompletionClearsMetadataPruneBlock|TestExplicitPruneClearsMetadataPruneBlock|TestCanceledMonitorMetadataPruneDoesNotSetBlock)$' -count=1
dagger check 'golang:lint-all'

sipsma added 19 commits August 5, 2026 16:58
Lower the class-slot coefficient to 768 bytes after the required 200k churn benchmark measured the 1024-byte hypothesis at a 2.199 estimate-to-post-GC-heap ratio. The calibrated coefficient measures 1.649 while preserving the approved structural formula and keeping a common one-million-entry graph above the 4 GiB trigger.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma requested a review from a team as a code owner August 5, 2026 17:11
@sipsma
sipsma force-pushed the metadata-memory-pruning-implementer-d394f80d branch from 505daef to 06c452b Compare August 5, 2026 17:13
@grouville
grouville self-requested a review August 5, 2026 19:58

@grouville grouville left a comment •

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.

While testing this branch I think I might have found a small race in pruning. It may be a known trade-off, so genuine question at the end, AND, as it's best effort, approving to unblock. LGTMm after the linting issues get fixed ✅

The scenario:

  1. Pruning starts by taking a snapshot of the results that active sessions are using, and builds its plan from that.
  2. Right after that, a new session gets a cache hit on a persisted result.
  3. Pruning applies the plan and deletes that result's persisted edge. Before deleting, it re-checks the unpruneable flag, but not whether a session started using the result in the meantime.

Nothing crashes: the session keeps its reference and finishes fine. But when the session closes, the result is dropped from the cache, so the next run has to rebuild it. This matters more with this PR, because metadata pruning now runs in the background whenever the estimate goes over the limit: exactly when sessions are live and getting hits.

I've tried checking the docs you made on the subject, it seems to read both ways: "ownership may have changed... The implementation accepts that", but also "prune will not cut it while it is still in that active closure".

I guess it's an accepted trade-off, just questioning wether it's worth closing?

In case it's the latter, I've made a small repro and a possible fix: grouville/dagger@repro-dagql-prune-active-session-race

@sipsma

sipsma commented Aug 5, 2026

Copy link
Copy Markdown
Contributor Author

While testing this branch I think I might have found a small race in pruning. It may be a known trade-off, so genuine question at the end, AND, as it's best effort, approving to unblock, after the linting issue gets fixed ✅

The scenario:

1. Pruning starts by taking a snapshot of the results that active sessions are using, and builds its plan from that.

2. Right after that, a new session gets a cache hit on a persisted result.

3. Pruning applies the plan and deletes that result's persisted edge. Before deleting, it re-checks the `unpruneable` flag, but not whether a session started using the result in the meantime.

Nothing crashes: the session keeps its reference and finishes fine. But when the session closes, the result is dropped from the cache, so the next run has to rebuild it. This matters more with this PR, because metadata pruning now runs in the background whenever the estimate goes over the limit: exactly when sessions are live and getting hits.

I've tried checking the docs you made on the subject, it seems to read both ways: "ownership may have changed... The implementation accepts that", but also "prune will not cut it while it is still in that active closure".

I guess it's an accepted trade-off, just questioning wether it's worth closing?

In case it's the latter, I've made a small repro and a possible fix: grouville/dagger@repro-dagql-prune-active-session-race

That scenario was present before the changes here and while it's a legit possibility it is indeed something we just accept as a tradeoff. The problem is that rechecking adds quite a bit of overhead (i.e. the fix you linked to has to re-scan a potentially large map while holding an contended lock and repeats that work for each edge being considered for removal). We don't want pruning to be a "stop-the-world" type GC and accept that what gets pruned and what gets retained is intrinsically timing and heuristic based anyways, which is okay since it's managing a cache (as opposed to a database or something).

social4hyq pushed a commit to social4hyq/homebrew-core that referenced this pull request Sep 20, 2026
dagger 0.21.9

Created-by: HarmonybrewBot
Commit-by: HarmonybrewBot
Merged-by: HarmonybrewBot
Description: Created by `brew bump`

---

Created with `brew bump-formula-pr`.<details>
  <summary>release notes</summary>
  <pre>## v0.21.9 - 2026-08-25

### Added
- Added SSHFS-backed volumes, allowing remote filesystems to be mounted into containers without copying their contents into Dagger's content store by @sipsma in dagger/dagger#13462

### Fixed
- `Container.withDirectory` and `Directory.withDirectory` no longer restore by @sipsma in dagger/dagger#13848 \
  deleted nested files after execs remove and recreate an ancestor
  directory.
- Engine client enumeration no longer blocks behind another session's by @sipsma in dagger/dagger#13542 \
  initialization or teardown, preventing Cloud keeper heartbeats from stalling
  and live engines from being suspended.
- Session teardown now runs in the background after the main client disconnects, by @sipsma in dagger/dagger#13681 \
  preventing shutdown responses from timing out while the engine releases
  session-owned cache results.
- The engine now bounds retained DAGQL cache metadata independently of disk by @sipsma in dagger/dagger#13846 \
  usage by pruning cold persisted results when the structural estimate exceeds
  its configured limit. This may mitigate metadata growth at scale while
  preserving active and engine-lifetime cache roots.
- Registry protocol probes no longer appear as scary errors in progress output by @vito in dagger/dagger#13410 \
  when image pulls or publishes succeed, while genuine registry failures remain
  visible.
- Registry authentication no longer invalidates downstream container cache reuse across sessions when applied before or after `Container.from` by @sipsma in dagger/dagger#13901

### What to do next?
- Read the [documentation](https://docs.dagger.io)
- Join our [Discord server](https://discord.gg/dagger-io)
- Follow us on [Twitter](https://twitter.com/dagger_io)

</pre>
  <p>View the full release notes at <a href="https://github.com/dagger/dagger/releases/tag/v0.21.9">https://github.com/dagger/dagger/releases/tag/v0.21.9</a>.</p>
</details>
<hr>

See merge request: Harmonybrew/homebrew-core!17786
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.

2 participants