Skip to content

feat: add dagop - #9395

Merged
jedevc merged 9 commits into
dagger:mainfrom
jedevc:add-dagop
Feb 28, 2025
Merged

jedevc merged 9 commits into
dagger:mainfrom
jedevc:add-dagop

Conversation

@jedevc

@jedevc jedevc commented Jan 16, 2025 •

Copy link
Copy Markdown
Contributor

Fixes #6927, enables #9098, and continues the project of rebuilding the Ship of Theseus 🎉

This PR introduces a new way of marrying dagger's dagql and buildkit's LLB. Currently, dagql is used to query the graphql API schema, while LLB is used to query the buildkit dag. dagql is much more flexible and easy to extend, while LLB has significantly better caching behavior.

DagOp allows us to combine the best of both worlds. Essentially, it allows us to execute a graphql selection as a buildkit op - caching it exactly as a native buildkit operation.

How it works

  • We inject custom "LLB" by piggybacking on the Description map of an existing LLB operation. Then during ResolveOp, we transform this into a magical CustomOpWrapper. This LLB is very hacky, but such is life 🤷 Without entirely ripping out LLB (or forking that package), there's not really a neat way of handling this.
  • The CustomOpWrapper delegates all of it's operations to Registered CustomOps - at the moment, only DagOp.
    • Note: since CustomOps are serialized into a string, then the DagOp has to be JSON-marshallable. This means we have to do some hackery around sending IDs instead of Selectors directly.
  • The DagOp evaluates the query, and gets the result as a root Directory - it then evaluates this the Directorys ref, and returns it.

This effectively allows the dagql query execution to be cached!

Performance

To show this off, I've ported AsTarball to use this (to fix #6927). Essentially, AsTarball attempts to self-call itself inside a DagOp (using a similar technique to #8724). This now means that if the Container receiver hasn't changed, then the File result isn't forced to be re-evaluated (which it was before).

Just this simple transformation seems to shave a couple seconds off an engine build with dagger call engine container.

Without this patch:

$ time dagger call engine container
...
Full trace at https://dagger.cloud/jedevc/traces/85dea4974204db7872fb31f732bbdcb2
dagger call engine container  5.77s user 1.31s system 25% cpu 27.576 total

With this patch:

$ time dagger call engine container
...
Full trace at https://dagger.cloud/jedevc/traces/b384cb5631d72ebaddab576fae164d24
dagger-with-dev dagger call engine container  6.38s user 1.45s system 30% cpu 25.252 total

In the traces, you can see that before, the tar unpacking as part of unbundling the SDK images is uncached, while after, they are:

Before:
image
After:
image

TODO

@jedevc
jedevc force-pushed the add-dagop branch 2 times, most recently from 513947d to 2c8c2e8 Compare January 27, 2025 17:59
Comment thread core/schema/container.go Resolved
@jedevc
jedevc force-pushed the add-dagop branch 3 times, most recently from f44f9e4 to 6adcacd Compare January 28, 2025 15:29
@jedevc

jedevc commented Jan 28, 2025

Copy link
Copy Markdown
Contributor Author

Hm, seems to be some issues with running some of the jobs, looks like AsTarball can just get deadlocked. Wonder if there's a way that it's causing an issue being run in parallel.

@jedevc
jedevc force-pushed the add-dagop branch 6 times, most recently from 02fe939 to b30810f Compare January 30, 2025 17:00
@jedevc

jedevc commented Jan 30, 2025

Copy link
Copy Markdown
Contributor Author

Oooookay, have done a lot of refactoring, and now - these DagOps are implemented as middlewares 🎉 And all tests are passing 😄 There's also a few related dagql fixes+improvements that I found while in the area.

This is the first step - it's very easy to mark the asTarball method to be cached using buildkit now, with very very few changes to the actual content of the method - we'll be able to reuse this approach later.

However, the end goal is to completely own the cache refs - this is completely possible, it's just not immediately apparent how this should look in dagql. One option is something like 7a36d89. This is another custom op, and requires a lot more manual intervention, but you can write directly to a cache ref and skip the nasty llb.Local hack. Eventually we can switch to this approach, but didn't want to keep blocking this PR from getting in.

@jedevc
jedevc requested review from sipsma and vito and removed request for sipsma January 30, 2025 17:19
@jedevc
jedevc marked this pull request as ready for review January 30, 2025 17:19
@jedevc
jedevc requested a review from sipsma January 30, 2025 17:19
@github-actions

Copy link
Copy Markdown
Contributor

This PR is stale because it has been open 14 days with no activity. Remove stale label or comment or this will be closed in 7 days.

@github-actions

Copy link
Copy Markdown
Contributor

This PR was closed because it has been stalled for 7 days with no activity.

@sipsma sipsma 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.

As a general comment, probably worth a double check we aren't accidentally leaking dagql/client/session memory via all this plumbing through buildkit. I couldn't see anything obvious but there's too many layers of abstraction to really know just by looking at the code

Comment thread core/schema/middlewares.go Resolved
Comment thread engine/server/session.go

const InstrumentationLibrary = "dagger.io/engine.server"

func (srv *Server) DagqlServer(ctx context.Context) (*dagql.Server, error) {

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I'm probably missing something, but on creation, the CustomOp only has access to the worker. We don't actually have access to the engine server, or the list of sessions, or even a specific client - so I don't think there's a way of getting it out?

We just need some interface to pass this through via, we could use core.Server honestly, it's just that that's in core, and we need this in buildkit. So I just selected the one function we need here.

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.

I misread this originally, I thought it was a method on session.... This all lgtm

Comment thread core/dagop.go Resolved
Comment thread core/dagop.go Resolved
Comment thread core/schema/middlewares.go Resolved
if err != nil {
return nil, err
}
deps := make([]llb.State, 0, len(depsDefs))

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.

nit: it looks like some of our impls of PBDefinitions use a map so the order is random, which I think might change the buildkit cache key and result in extra invalidation?

I'm sort of doubtful this is anything remotely near a big deal atm, so not a blocker. If there's a really easy fix then maybe worth it, otherwise just worth keeping in mind

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Gooood catch! This doesn't actually effect us at the moment, since only user interfaces/structs rely on a Fields map - since you can only add this to core implementations, this isn't an issue.

But this is actually a really easy fix, so thrown this into a new commit - we can just iterate over the typedef fields, which are more consistently ordered.

@jedevc jedevc added benchmark triggers pre-merge benchmarks area/engine About dagger core engine labels Feb 24, 2025
@jedevc

jedevc commented Feb 24, 2025

Copy link
Copy Markdown
Contributor Author

As a general comment, probably worth a double check we aren't accidentally leaking dagql/client/session memory via all this plumbing through buildkit

Anything specific to do here? I've added the benchmark label, so we can get some data from that hopefully? Not really sure if there's anything specific to do for measuring "leakage".

@sipsma

sipsma commented Feb 26, 2025

Copy link
Copy Markdown
Contributor

Anything specific to do here? I've added the benchmark label, so we can get some data from that hopefully? Not really sure if there's anything specific to do for measuring "leakage".

For now, just start the dev engine locally with debug endpoints exposed, run some reasonable set of tests for a while (I often just run TestModule for 5 or so minutes), ctrl-c and then see what the RSS looks like. Hit the /debug/gc and pprofs endpoints of the dev engine to double check things.

@sipsma sipsma 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.

Double checking memory here is worth a check before merge, but overall LGTM!

@jedevc
jedevc force-pushed the add-dagop branch 2 times, most recently from 0b90187 to 5fe6ac6 Compare February 27, 2025 11:49
This little refactor ensures we can get the Typed value of an array,
even if it doesn't actually have any values in it.

Signed-off-by: Justin Chadwell <[email protected]>
Without evaluating any of the fields! This allows neatly deferring
operations, but primarily is there to ensure that we can serialized
selectors for the use in DagOP.

Signed-off-by: Justin Chadwell <[email protected]>
Because we start using these as cache keys for dagop, it makes sense
that we should order these (to avoid bad invalidations). We won't hit
this case in practice soon, but good practice to do it anyways, so at
least it works as expected if we try.

Signed-off-by: Justin Chadwell <[email protected]>
Implements sipsma's suggestions, and also renames middlewares to
wrappers - since the llm branch is using middlewares to mean something
entirely different (and that was claimed first).

Signed-off-by: Justin Chadwell <[email protected]>
@jedevc

jedevc commented Feb 28, 2025

Copy link
Copy Markdown
Contributor Author

Okay, having gone through a few runs of TestModule, and taking some heap profiles, it doesn't look like there's any more leaks than there used to be.

Still seeing a lot of log.newRing (we know about) and FileActionMkFile.Unmarshal (we know about), but at least nothing new seems to be leaking.

@jedevc
jedevc merged commit a2511ed into dagger:main Feb 28, 2025
@sipsma

sipsma commented Feb 28, 2025 •

Copy link
Copy Markdown
Contributor

Still seeing a lot of log.newRing (we know about) and FileActionMkFile.Unmarshal (we know about), but at least nothing new seems to be leaking.

@jedevc those shouldn't be leaking anymore after the other fixes, if they are then something might be going wrong. I'll try locally too.

EDIT: I didn't see any leaks after running TestContainer (which calls AsTarball), so afaik I think we're good for now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/engine About dagger core engine benchmark triggers pre-merge benchmarks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Container.AsTarball does not cache as expected

2 participants