Skip to content

Centralize GraphQL server in the Engine Runner - #5415

Merged
sipsma merged 88 commits into
dagger:mainfrom
sipsma:session-frontend
Aug 2, 2023
Merged

sipsma merged 88 commits into
dagger:mainfrom
sipsma:session-frontend

Conversation

@sipsma

@sipsma sipsma commented Jul 6, 2023 •

Copy link
Copy Markdown
Contributor

Previously, every Dagger client had its own graphql server and buildkit session, all running in the CLI.

This rearchitects the server to no longer be in the CLI but instead be in the engine runner (i.e. the container/pod/etc. running the buildkitd wrapper).

  • Clients can still have separate sessions, but any "nested" clients that run in containers started by a client (e.g. environments and their entrypoints) now connect back to the same session + graphql server.
  • Each client still uses its own session attachables, which the graphql server coordinates between and enforces that each client only accesses resources it's allowed to access. This enables safe and robust selective sharing of client-specific resources like local dirs, secrets, sockets, etc.

Docs

Motivation

The most immediate motivation is to make Zenith possible. Zenith environments are nested dagger clients, which means that they are clients running in an exec that connect back to the engine API to make more Dagger API calls.

The big wrench our old architecture threw into this was that dagger clients and environments often need to selectively share certain state, including local directories, secrets, and dynamically loaded graphql schemas/resolvers. In the previous model, every client, including nested clients, all got their own fully isolated graphql server and buildkit session/job. This has some good security properties but is unnecessarily restricting:

  1. Schema stitching for loaded environments was extremely complicated, to the point where after several days I gave up and pivoted to implementing this PR instead. Trying to get the nested client to stitch its resolvers into its own server and that of a totally separate one was a headache to even think about much less actually implement. It would have only gotten worse as Zenith was built out too.
  2. We had a hack for sharing local dirs across sessions/jobs, but as noted in the Caveats in the original PR, there was an inherent race condition that made it possible for it to break in sometimes catastrophic ways (namely, completely incorrect dirs being loaded; you were lucky if you only got an error).
  3. Similar problems as above would be repeated every time we wanted to add any new feature to Zenith that involved client specific resources, such as passing secret resources, sockets, registry creds, v2 services, etc.

The new architecture fixes this by creating a centralized Dagger Server in the runner for every client and its environments (i.e. nested clients). Rather than trying to implement hacks for coordinating distributed graphql servers + sessions + buildkit jobs, we coordinate everything in one central server, which makes all the previous problems much easier, and at times totally trivial.

To be clear, there is still a single engine runner (the container process) but there is not one single Dagger Server handling all clients now; when a client connects to the runner it starts its own Dagger Server instance in the runner. That Dagger Server instance is only shared with nested clients created by that client.

Additionally, the idea is not that all resources managed by a single Dagger Server instance are inherently shared, it's that they can be selectively shared, with enforcement on who can access what to be enforced by the server. For example, a dagger do host client could choose to pass one of its local dirs to an environment nested client, but a nested client can't just load arbitrary dirs from the host client.

  • Note that this PR doesn't implement most of the enforcement yet; all of the plumbing is in place but left out for sake of time + PR size

More Benefits

Zenith is why we prioritized this immediately, but there's a surprisingly long list of other benefits we are picking up as a side effect of this change. This is thanks to the fact that:

  1. We now have access to way more buildkit components + features by virtue of implementing our API in the same process as those components, rather than being limited to the grpc client APIs
  2. We can now implement coordination across clients+sessions, which makes previously difficult+impossible problems tractable.

In this PR already

A few non-Zenith benefits were picked up inherently as part of the change:

  1. The very long-standing problem of needing to open separate sessions to run an export (i.e. this issue, amongst many others) is gone.
    • Now we just invoke buildkit exporters directly
  2. The ugly hack of obtaining stdout/stderr/exitcode of failed execs by running a gateway container that invoked the shim to collect all those together and pass them through stdio back to the graphql api is gone.
    • Now we just read all this directly after a solve call fails, no container needed.

New Possibilities

There's a lot of things outside the scope of this PR that will now be possible:

  1. Cache Volume syncing no longer has to be limited to engine start+stop time.
    • We can coordinate access to the cache mounts and can thus dynamically import them and even continuously sync them throughout the engine's runtime
  2. Layer cache export can be made MUCH more efficient by just exporting cache refs as they are solved, rather than needing to read the entire local cache and send it all to the cache service as metadata every export cycle.
  3. We can change cloud tokens to be provided by clients rather than forced to be set on the long-running engine container
  4. We finally have a route to replace the super long resource IDs (i.e. ContainerID, DirectoryID, etc.) with short hashes, thanks to the fact that we can now persist id<->hash mappings in a safe way (alongside the existing persisted caches in the engine container)
  5. "Long-running services" that get selectively shared between different clients (i.e. a registry sidecar, a shared dockerd, etc.) are now fairly straightforward to support if desired (especially in combination w/ services v2)
  6. Session attachables are fully customizable. We also aren't limited to that model anymore and can implement our own custom connections between clients and the server as needed.
  7. We aren't inherently limited to LLB anymore; we can plug in our own vertexes to Buildkit's core LLB-agnostic solver (in the same way that Buildkit converts LLB to that generic representation)
  8. We can customize exporters however we want and even implement our own
  9. Client-side and server-side logs can be combined into one stream, which would potentially make debugging much easier
  10. The CLI is much lighter-weight, it mostly consists of buildkit session attachables, which reduces SDK<->CLI incompatibility opportunities (though see the Caveats, the version compatibility improvements come to full fruition only after a few more steps)

Caveats (and paths to improving)

The biggest downside to this change is that it means adding a new API to our schema will require users to upgrade both their SDKs and their long-running engine, whereas previously it usually only meant they had to upgrade the SDK and the CLI, which is often easier.

However, by taking this 1 step back we are setting ourselves up to take several steps forward by making the change described here.

This PR in combination with that change should result in SDKs that can always start an engine compatible with their version without needing to rely on the "CLI magic auto-download" feature in SDKs. The only source of backwards-incompatible changes would be from "internal protocol" changes to how SDKs, the CLI and the engine talk to each other, which are much rarer and usually easier to avoid entirely.

Architecture

Before

ArchBefore drawio

After

ArchAfter drawio

(minor) Breaking Changes

This change is almost entirely internal-only and backwards compatible, with minor exceptions:

  1. It used to be possible to add service dependencies to container.Build, but that's no longer possible. We were sort of surprised this ever worked at all (to the extent it did previously) and was a highly obscure use case anyways.

@sipsma

sipsma commented Jul 8, 2023

Copy link
Copy Markdown
Contributor Author

Update on this: I realized that for Zenith I needed to flesh out the session proxying aspect of this earlier than I thought. I was under the impression buildkit did something that it apparently doesn't (the session ID param provided to the llb bridge solve is ignored when you provide an llb definition), so I wasn't able to save this chunk of work for later.

This has gone well but resulted in some annoying import loops in our code, so I've been in depths of refactoring hell the last two days. It's actually starting to look nice and can see light at the end of the tunnel, but has delayed things a bit.

@sipsma
sipsma force-pushed the session-frontend branch 4 times, most recently from cbf0b90 to 2c6b27d Compare July 10, 2023 19:10
@sipsma

sipsma commented Jul 10, 2023

Copy link
Copy Markdown
Contributor Author

Okay, got this branch to a point where I can consistently pass all the existing project tests, which are crucial since they involve passing local dirs back and forth between caller's host and nested execs.

Now gonna pivot back once again to rebasing zenith on this branch as is so I can unblock our further efforts there.

After that, will return here and finish the remaining missing features (secrets, sockets, auth, and non-local exports are the main ones) and do cleanup.

@sipsma
sipsma force-pushed the session-frontend branch from 359925a to 27e13b9 Compare July 13, 2023 22:01
vito added a commit to vito/dagger that referenced this pull request Jul 14, 2023
This is causing container hostnames to change on every run, causing
caches to bust.

We tried changing Container.WithExec to only set a hostname if the
container exposes ports, but that breaks two use cases:

1. Calling WithExec before WithExposedPort.
1. Using a container as a service without explicitly exposed ports.

We could stomach temporarily breaking the latter case, but the former
case is probably pretty common, since that's what our docs show.

The impact of this change on the other hand should be pretty minor, only
breaking the unannounced `dagger do` command, which is already being
completely rearchitected such that we can probably approach the intended
fix here in a different manner. (dagger#5415)

Signed-off-by: Alex Suraci <[email protected]>
@sipsma
sipsma force-pushed the session-frontend branch from 42cc21a to aeecad6 Compare July 17, 2023 21:25
@sipsma sipsma changed the title Centralize graphql server to be a frontend in the engine runner Centralize graphql server in the engine runner Jul 19, 2023
@vito vito mentioned this pull request Jul 19, 2023
@sipsma
sipsma force-pushed the session-frontend branch from 400d440 to 9003575 Compare July 26, 2023 01:41
@sipsma

sipsma commented Jul 26, 2023

Copy link
Copy Markdown
Contributor Author

Another general update, getting very close now, all the major features have been re-added, with just a few scattered test failures to make fixes for left. After that the plan is:

  1. Do some final renaming of the various new objects/concepts (most names are placeholders since I was wasting too much time going through re-naming iterations 😁)
  2. Finally add all the documentation, architecture diagrams, etc. to explain the new approach
  3. Do some basic cleanup of a few parts that are silly copy-pastes of large chunks of code (to help reviewers)
  4. Request some official reviews
  5. Do the rest of the minor cleanups along with updates based on review feedback
  6. Add any additional test coverage that is missing from our current main but feels important given the size of this change
  7. Merge

I want to reach at least step 6 by this Friday, with a stretch goal of 7, but worst case 7 may not happen until early next week.

@sipsma sipsma changed the title Centralize graphql server in the engine runner [Maybe 0.8.0] Centralize graphql server in the engine runner Jul 26, 2023
@sipsma sipsma added this to the 0.8.0 BREAKING CHANGES RELEASE milestone Jul 26, 2023
@sipsma
sipsma force-pushed the session-frontend branch 3 times, most recently from 511f75c to e349836 Compare July 27, 2023 19:54
@sipsma

sipsma commented Jul 28, 2023 •

Copy link
Copy Markdown
Contributor Author

Another update, the final few test failures/flakes have been real doozies to debug, but squashed a lot of them, including a bunch related to services that were fixed by @vito backporting bits and pieces of services v2 to here (thank you again!). The final remaining known failure is a flake (1/2 runs in CI, super rare locally) where it seems that random service container is somehow becoming associated w/ unrelated jobs and never exiting, which causes the jobs to not be able to exit either and just stall.

Pretty hard to repro locally, and very hard to debug in CI, so taking a while but have a few workarounds in mind to try if we end up not fully root causing today.

EDIT: I was completely misinterpreting the stack traces, the problems were actually that some nested containers couldn't close their connections due to some circular dependencies between trying to close their connection and us enforcing that connections can't close until everything in the buildkit job has exited.

@sipsma

sipsma commented Jul 28, 2023

Copy link
Copy Markdown
Contributor Author

Tests just passed 5 times in a row after a few more fixes to various connection lifecycle management issues 🎉

So gonna call that good (🤞) and move onto some renaming and docs for everything

@sipsma
sipsma force-pushed the session-frontend branch 2 times, most recently from d3f285e to 6b8896a Compare July 29, 2023 00:43
Comment thread core/schema/host.go

secretFileContent, err := os.ReadFile(absPath)
func (s *hostSchema) setSecretFile(ctx *core.Context, _ any, args setSecretFileArgs) (*core.Secret, error) {
secretFileContent, err := s.bk.ReadCallerHostFile(ctx, args.Path)

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.

@grouville @helderco Just FYI was able to re-integrate support for this API. Ended being able to add this ReadCallerHostFile util that lets the engine server read files from the client caller directly into memory, so preserves all the same security properties as before.

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.

That's great! 🚀

@sipsma
sipsma force-pushed the session-frontend branch from 525be20 to 6acfdd9 Compare July 31, 2023 23:50
@sipsma
sipsma marked this pull request as ready for review August 1, 2023 00:17
sipsma and others added 11 commits August 2, 2023 07:40
Signed-off-by: Erik Sipsma <[email protected]>
This was just an experiment with `dagger do` which was nice while it
lasted, but it currently can't work due to catch-22 dependency issues
which will only be fixed w/ full project/environment support. So just
removing it for now.

Signed-off-by: Erik Sipsma <[email protected]>
currently these are just computed by JSON encoding the values, which is
pretty wack, and lets any little thing lead to a cache bust.

we put all that work into stableDigest, so let's use it here too!

Signed-off-by: Alex Suraci <[email protected]>
Along the way, change the import tests to use an image built with `apko`
so we're not triple-testing Export and Host APIs. I was getting
different image digests on every run before; by using the result of a
cached container command, they're stable across test runs, allowing me
to buildctl prune out-of-band.

In the future it'd be nice if we had a prune API we could use in tests.

Signed-off-by: Alex Suraci <[email protected]>
I truly don't think any of these cases matter. It's arguably strange
before that Hostname wasn't affected by certain config changes. And no
one should be using services from Dockerfiles.

Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the session-frontend branch from c5fe3e4 to 5c7aa4d Compare August 2, 2023 14:50
@sipsma
sipsma requested a review from a team as a code owner August 2, 2023 14:50
@sipsma
sipsma removed the request for review from a team August 2, 2023 15:17
@kjuulh

kjuulh commented Aug 2, 2023 •

Copy link
Copy Markdown
Contributor

As far as I can tell the sdks doesn't really have to change after this update?

We don't have an internal graphql server, as we rely entirely on download and starting the clis, which is what we communicate with. I am not super familiar with the entrypoints and environments, so I can't speak on that front.

That is if I haven't missed something, if we instead could rely on always being started with dagger run that would simplify some things, as we could throw away the cli fetcher code. But I don't know whether that is something you're going for down the line.

Anyways, just wanted to give my 2 cents.

Edit: missed that you'd just removed dagger/sdk-rust :)

@sipsma

sipsma commented Aug 2, 2023

Copy link
Copy Markdown
Contributor Author

As far as I can tell the sdks doesn't really have to change after this update?

@kjuulh oh yeah sorry, I just had to regenerate the SDK to pick up a new API but apparently that automatically requests a review from Rust SDK team, nothing to review :-)

@sipsma
sipsma force-pushed the session-frontend branch from 5c7aa4d to 30bc3b8 Compare August 2, 2023 15:31
@sipsma
sipsma merged commit b024790 into dagger:main Aug 2, 2023
@gerhard gerhard changed the title [Maybe 0.8.0] Centralize graphql server in the engine runner Centralize GraphQL server in the Engine Runner Aug 2, 2023
sipsma added a commit that referenced this pull request Oct 29, 2026
The engine sent a host import's include, exclude and follow-path
patterns twice: inside the single encoded options value, and again as
one gRPC header value per pattern. Since Go 1.27, an HTTP/2 server
accepts at most 500 header values per request by default, and when a
request over that limit spans several frames it closes the whole
connection. The CLI serves the session's attachables over that
connection, so reading a host directory with more than about 500
patterns ended the session ("http2: server connection error ...
PROTOCOL_ERROR", then "failed to sync").

Every CLI since #5415 reads the encoded value first, so the per-pattern
copies are dropped. Receiving them still works, for requests that come
straight from BuildKit.

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma sipsma mentioned this pull request Oct 29, 2026
sipsma added a commit that referenced this pull request Oct 30, 2026
The engine sent a host import's include, exclude and follow-path
patterns twice: inside the single encoded options value, and again as
one gRPC header value per pattern. Since Go 1.27, an HTTP/2 server
accepts at most 500 header values per request by default, and when a
request over that limit spans several frames it closes the whole
connection. The CLI serves the session's attachables over that
connection, so reading a host directory with more than about 500
patterns ended the session ("http2: server connection error ...
PROTOCOL_ERROR", then "failed to sync").

Every CLI since #5415 reads the encoded value first, so the per-pattern
copies are dropped. Receiving them still works, for requests that come
straight from BuildKit.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit that referenced this pull request Oct 30, 2026
The engine sent a host import's include, exclude and follow-path
patterns twice: inside the single encoded options value, and again as
one gRPC header value per pattern. Since Go 1.27, an HTTP/2 server
accepts at most 500 header values per request by default, and when a
request over that limit spans several frames it closes the whole
connection. The CLI serves the session's attachables over that
connection, so reading a host directory with more than about 500
patterns ended the session ("http2: server connection error ...
PROTOCOL_ERROR", then "failed to sync").

Every CLI since #5415 reads the encoded value first, so the per-pattern
copies are dropped. Receiving them still works, for requests that come
straight from BuildKit.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit that referenced this pull request Nov 2, 2026
The engine sent a host import's include, exclude and follow-path
patterns twice: inside the single encoded options value, and again as
one gRPC header value per pattern. Since Go 1.27, an HTTP/2 server
accepts at most 500 header values per request by default, and when a
request over that limit spans several frames it closes the whole
connection. The CLI serves the session's attachables over that
connection, so reading a host directory with more than about 500
patterns ended the session ("http2: server connection error ...
PROTOCOL_ERROR", then "failed to sync").

Every CLI since #5415 reads the encoded value first, so the per-pattern
copies are dropped. Receiving them still works, for requests that come
straight from BuildKit.

Signed-off-by: Erik Sipsma <[email protected]>
sipsma added a commit that referenced this pull request Nov 2, 2026
The engine sent a host import's include, exclude and follow-path
patterns twice: inside the single encoded options value, and again as
one gRPC header value per pattern. Since Go 1.27, an HTTP/2 server
accepts at most 500 header values per request by default, and when a
request over that limit spans several frames it closes the whole
connection. The CLI serves the session's attachables over that
connection, so reading a host directory with more than about 500
patterns ended the session ("http2: server connection error ...
PROTOCOL_ERROR", then "failed to sync").

Every CLI since #5415 reads the encoded value first, so the per-pattern
copies are dropped. Receiving them still works, for requests that come
straight from BuildKit.

Signed-off-by: Erik Sipsma <[email protected]>
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.

4 participants