Skip to content

dagql, core: sync only the leases that changed, and borrow snapshots dependencies hold - #14871

Merged
sipsma merged 12 commits into
mainfrom
sipsma/engine-owner-lease-sync
Oct 27, 2026
Merged

sipsma merged 12 commits into
mainfrom
sipsma/engine-owner-lease-sync

Conversation

@sipsma

@sipsma sipsma commented Oct 27, 2026

Copy link
Copy Markdown
Contributor

A result that holds snapshots keeps an owner lease for each, so a snapshot stays while a result needs it. This PR cuts how much work and waiting that costs.

Sync only the leases of the parts that changed. A Container's parts (its root filesystem, its exec metadata, each mount) are filled by separate evaluations. After each one, the engine synced the result's owner leases by reading every part. That read waited for the container's other evaluations still running, while holding the result's lease sync lock: in a per-package Go build of dagger's CLI, ~29,000 syncs per build, with seconds of summed waiting. The sync after a part evaluation, a native group or a delegation sweep now reads and reconciles only the parts it filled. Anything that can't be read by part is synced whole, as before.

Borrow a snapshot instead of leasing it again. A result whose snapshot one of its dependencies already holds now borrows it: it gets no lease of its own, and none is removed when it goes. The dependency is kept while the result keeps its value, so the dependency's lease keeps the snapshot. That covers:

  • a container part copied from a dependency;
  • a dependency mounted as it is;
  • any result that selects from another: a container's root filesystem, a subdirectory, a container's directory at a path, a file read from a directory.

After a restart, the import attaches each persisted result's own lease before any result is synced, so a dependency holds the snapshot before anything borrows it.

What it changes for users

Faster builds that make many containers and selections. On a cold build of dagger's CLI with dagger/go's per-package build:

  • lease writes per build fell from 21,233 to 13,535 with the first two commits;
  • with borrowing for selections, the first build took ~1.4 s less and the engine used ~5.8 CPU-seconds less. About 1.0 s of that is at the end of the build, where the session's teardown removed 3,445 owner leases one at a time (1.07 s); with this PR that wait is 0.08 s.

No API or behaviour change otherwise.

Numbers

AWS c8gd.4xlarge (arm64, 16 cores), one fresh dev engine per build, 3 interleaved builds per arm, every build compiling the same source tree.

Engine First build (s) Engine CPU (s)
without the borrowing commits 37.19, 37.54, 36.76 74.3, 72.6, 72.0
with them 35.81, 36.10, 35.02 66.8, 69.4, 65.7

Tests

  • dagql: part lease syncs, borrowed links, and TestCacheSnapshotOwnerLeaseBorrowsADependencysSnapshot (a result depending on another that holds snapshot X gets no lease of its own for X, and releasing both removes only the other's lease).
  • core: container part lease syncs, including that a group's delegation sweep replaces a part's own snapshot with its parent's, borrowed, and removes the part's lease.
    All run in CI's test-split/test-packages.

Every container a chain of with* calls makes copies each part it doesn't change from the container before it, and its owner lease sync attached a lease of its own for each. A chain of n links holding m mounts attached about n*m leases, each a write to the snapshot metadata: about 21,000 for one build of dagger's CLI with dagger.io/go, about 12 per package exec.

When a container copies a part from a result it depends on (delegateContainerPart), it records the snapshot the part then holds. While the part still holds that snapshot, its link is marked borrowed, and the owner lease sync and its cleanup skip it; a part written after the copy, as a path writer copies and then writes, holds a snapshot of its own and keeps its lease. The links are still recorded with the result. The result copied from outlives the copy: a result's dependencies are kept while it keeps its value, and its value is dropped only when nothing depends on it, so the original's lease keeps the snapshot as long as the copy needs it.

Borrowing isn't persisted: after a restart, imported links attach leases of their own, as before.

Signed-off-by: Erik Sipsma <[email protected]>
withMountedDirectory and withMountedFile put their source's snapshot in the mount unchanged, unless they chown it, and the source is a result the container depends on, which owns that snapshot and outlives the container, as for a copied part. Such a mount's link is now borrowed too, while it holds that snapshot: one mount of a directory or file per container link attached a lease of its own, so a package exec built with a handful of mounts attached a lease for each. A chowned mount is a result of its own and keeps its lease.

Signed-off-by: Erik Sipsma <[email protected]>
After an evaluation fills parts of a result, the owner lease sync read every part of the result to diff its leases. For a Container that read waits for each of the container's evaluations still running, to see all its parts coherently, while it holds the result's lease sync lock. A container's parts are filled by separate evaluations (its root filesystem, its exec metadata, each mount), so each one's sync waited on the others and re-read all of them: in a per-package Go build of dagger's CLI, 29,000 syncs and seconds of summed waits, and with one file mount per dependency, 65,000.

The sync after a part evaluation now reads and reconciles the parts that evaluation filled, and nothing else. They are complete, and every other part keeps the leases and stored links it has, so the read needs no wait. A value that can't be read by part (anything but a Container, a container acquired whole or restored from persistence, a part below the result) is synced whole, as before.

Signed-off-by: Erik Sipsma <[email protected]>
A Container's own lazy groups (its root filesystem, a mount, its metadata) run through its PartHost with no task continuation, and their lease sync was still the whole one: it read every part and waited for the container's other running groups. Those are the syncs that dominated a per-package Go build. The PartHost records the parts each such group filled on the attempt's token; the sync after the group now reconciles those parts alone, as after a part task. A body that records no parts fills the whole value and is synced whole.

A test runs two of a container's groups through the cache: the root filesystem's group completes, with its lease, while the mount's group is still running. Without this change it waits for the mount's group.

Signed-off-by: Erik Sipsma <[email protected]>
A container group's body also copies the delegated parts whose parent part is already final (consumeFinalParentDelegations), so a part can be filled by another group's body than its own. Its own group then finds it consumed and runs no sync. The scoped sync after the body covered only the group's installed outputs, so such a part kept the owner lease it had at publication, now that it is borrowed and needs none, until another whole sync or the result's release. The sweep now notes each part it copies on the body's task (PartTaskToken.AlsoFilled), and the sync after the body reconciles those parts as well.

Signed-off-by: Erik Sipsma <[email protected]>
The full lease sync opens none; these came from a profiling build.

Signed-off-by: Erik Sipsma <[email protected]>
A result that selects from another, such as a container's rootfs or a
directory's subdirectory, holds the same snapshot as the result it depends
on, and attached an owner lease of its own for it: a metadata write per
selection. Its dependency is kept while the result keeps its value, so the
dependency's hold keeps the snapshot. Mark such links borrowed, as a part
copied from a dependency is.

Signed-off-by: Erik Sipsma <[email protected]>
With a dependency's snapshot borrowed from the start, the child in this
test no longer took its own lease on the parent's root filesystem, so
the sweep had no lease left to remove. The child now starts with a root
filesystem of its own; the sweep replaces it with the parent's, borrowed,
and removes the child's lease.

Signed-off-by: Erik Sipsma <[email protected]>
@grouville
grouville self-requested a review October 27, 2026 06:03
golangci-lint's gocyclo limit, and a tagged switch where staticcheck asks for
one. No change in behaviour.

Signed-off-by: Erik Sipsma <[email protected]>
The rootfs selection depends on the container, which holds the imported
snapshot, so it borrows it and attaches no owner lease of its own: two
owners, not three, and one lease fewer at close. Borrowed links aren't
persisted, so after the restart its imported link attaches a lease again
until the results are loaded and their leases synced.

Signed-off-by: Erik Sipsma <[email protected]>
The sync-retry case injected a failure into the child's owner attach
during a delegated install, to check the install stays retryable. A
delegated child's snapshot is its parent's, which the parent holds, so
the child now borrows it and its sync attaches nothing: that failure
can't happen on this path. The case becomes borrowed-sync and checks
that the child's sync makes no owner attach, that its links are
borrowed, that the temporary parent hold is released and that the
child's pin is released once.

The handling of a failed owner attach stays covered by
part_acquisition_test.go and part_inline_test.go.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma merged commit bdad721 into main Oct 27, 2026
91 of 92 checks passed
@sipsma
sipsma deleted the sipsma/engine-owner-lease-sync branch October 27, 2026 07:13
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