Skip to content

Backport: services via gateway, minus API changes - #5764

Merged
gerhard merged 26 commits into
dagger:mainfrom
vito:services-via-gateway
Sep 13, 2023
Merged

gerhard merged 26 commits into
dagger:mainfrom
vito:services-via-gateway

Conversation

@vito

@vito vito commented Sep 5, 2023 •

Copy link
Copy Markdown
Contributor

@vito

This comment was marked as outdated.

@vito

This comment was marked as outdated.

@vito
vito marked this pull request as ready for review September 6, 2023 04:00
@vito
vito requested review from a team as code owners September 6, 2023 04:00
@vito

This comment was marked as outdated.

Comment thread sdk/elixir/lib/dagger.ex Resolved
@vito
vito force-pushed the services-via-gateway branch from 971ddee to 43f278e Compare September 6, 2023 16:34
@vito

This comment was marked as resolved.

@vito
vito force-pushed the services-via-gateway branch from 43f278e to 5689e6a Compare September 6, 2023 20:43
@vito

This comment was marked as outdated.

@vito
vito requested review from TomChv, helderco and sipsma September 7, 2023 15:23
@helderco

helderco commented Sep 7, 2023

Copy link
Copy Markdown
Contributor

Sorry @vito, I should have given an heads up that I spent a bit of time today taking care of the Python bit. I'll replace it in a minute.

@vito

vito commented Sep 7, 2023

Copy link
Copy Markdown
Contributor Author

@helderco All good, feel free to replace it! 🙏

@gerhard

gerhard commented Sep 7, 2023

Copy link
Copy Markdown
Contributor

@vito re various SDKs:

  • Dotnet is the most experimental one, nothing to do there
  • Java is yet to be merged - Import Java SDK as Experimental #5504 - nothing to do there (blocked on me)
  • Rust is something that @kjuulh can help with post-merge, no need to block

@helderco

helderco commented Sep 7, 2023

Copy link
Copy Markdown
Contributor

The reverts were from a GUI, weren't signed-off, I'm going to force push to fix.

@kjuulh

kjuulh commented Sep 7, 2023

Copy link
Copy Markdown
Contributor

Yep, I will handle the follow up inconsistencies for rust, if the tests don't complain that is.

@helderco
helderco force-pushed the services-via-gateway branch from c169a35 to 027fd3e Compare September 7, 2023 16:55
@helderco

helderco commented Sep 7, 2023 •

Copy link
Copy Markdown
Contributor

@vito what should be the behavior in dagger run and in Zenith (existing session)? Should the SDK call stop in both cases?

@vito

vito commented Sep 7, 2023 •

Copy link
Copy Markdown
Contributor Author

@helderco That's a good question. It seems that we should only call stop when we know the ClientID is going away for good. Otherwise you might end up stopping the services started by a separate client in the same session, before they're actually done being used. For example, you might dagger run a script that spawns a bunch of pipelines in parallel, each with their own client. If we naively call stop when closing an individual client in this case, one concurrent client would stop all the other client's services.

So with that rule, it seems most correct to not call stop in the DAGGER_SESSION_PORT case, because you can't be sure that the session is completed.

For Zenith/Dagger-in-Dagger, we can work with the above constraint by having it automatically do the equivalent of the stop API in the same place where the session is created (the shim).

In that case I think the current Go, NodeJS, and Elixir SDKs implementations are not right as they will unconditionally stop no matter what the internal connection type is.

Thinking about it more, if we're now saying this only needs to happen only in the case where a dagger session is spawned, I wonder if we can just handle it there, without adding a whole new API? Basically the client closing would send a signal to the process somehow, which would trigger all the stopping, and we'd wait for that to complete, which should be roughly already what's happening. I'm pretty sure I looked into this already, but I don't remember why I backed out. 😅 Looking into it now.

@helderco

helderco commented Sep 7, 2023

Copy link
Copy Markdown
Contributor

Thinking about it more, if we're now saying this only needs to happen only in the case where a dagger session is spawned, I wonder if we can just handle it there, without adding a whole new API? Basically the client closing would send a signal to the process somehow, which would trigger all the stopping, and we'd wait for that to complete, which should be roughly already what's happening.

Yes, a graceful shutdown. 👍

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

Left some comments, saw after I started reviewing that you're making a few more updates so apologies if a few of the comments are more nit-picky than is helpful now :-)

Comment thread core/net.go Resolved
Comment thread core/services.go Resolved
Comment thread core/services.go Resolved
Comment thread core/services.go Resolved
Comment thread core/services.go Resolved
Comment thread core/services.go Resolved
Comment thread core/services.go Resolved
vito added 3 commits September 7, 2023 14:28
Ran into a few panics/cryptic errors here while switching services over
to the gateway. I don't remember exactly if these were fixed (a lot
happened between now and then) but it seems worth committing anyway.

Also, change ResolveImageConfig to be a passthru along the way, for no
real reason.

Signed-off-by: Alex Suraci <[email protected]>
* Service is now a distinct type, but only used internally
* Services now run via the gateway interface instead
* (core.Container).Service returns a Service that runs the container
* Services can be explicitly started and stopped (not exposed yet)
* refactor ServiceBindings, merge with HostAliases
  * embed a Service instead of a ServiceID

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito force-pushed the services-via-gateway branch from 027fd3e to 35fdd1d Compare September 7, 2023 18:55
@vito

vito commented Sep 7, 2023

Copy link
Copy Markdown
Contributor Author

Pushed a prospective switch to a new internal HTTP endpoint, /shutdown, which gets called on (engine/client.Client).Close. I'm on shoddy internet, so relying on CI to tell me if it works. 😅 I reverted all the sdk/ changes, but they're still in the history if you want to salvage any part of your changes @helderco (it looked like part of that was refactoring)

@vito
vito force-pushed the services-via-gateway branch from 51343c9 to 7808c25 Compare September 7, 2023 20:23
helderco and others added 7 commits September 7, 2023 16:32
This reverts commits c142c49 and 51429e5.

Signed-off-by: Helder Correia <[email protected]>
The Python SDK has multiple types of connections which are decoupled from the client instance. It would be more natural to call `client.stop()` when automatically closing that connection but that's in a lower level.

Both `dagger.Connection` (legacy) and `dagger.connection` (experimental) support automatic provisioning, so pushed the common client setup to the engine connection manager.

The connect timeout was documented as 10.0 but set as 30.0 by mistake during development of the default client. Using the same value for stop to avoid adding more settings. It could be better documented but the timeout setting is in the middle of a change in format (see Config.__post_init__). This can all be revisited later.

To simplify the use case of codegen starting out with a blank `gen.py` file, it's now required to run under an existing session like `dagger run`. Note that this type of connection doesn't setup a client and so it doesn't add the `client.stop` as well.

Signed-off-by: Helder Correia <[email protected]>
* Get: fix missing Unlock
* Start: use a good old loop + label to fix case where multiple Starts
  are waiting, and the first Start fails.

Signed-off-by: Alex Suraci <[email protected]>
use a loop; if multiple start attempts overlap we can just keep waiting

Signed-off-by: Alex Suraci <[email protected]>
@vito
vito force-pushed the services-via-gateway branch from dd28a40 to 517eecc Compare September 7, 2023 20:32

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

Changes looks, I need to try on my local host to verify if everything is okay.

Will the SDK clients have an update? I saw the old implementation and see that we now use shutdown but as far as I understand, this is supported to be called by the client?

Do I need to wait before my attempt to test the PR?

@vito

vito commented Sep 8, 2023

Copy link
Copy Markdown
Contributor Author

@TomChv Thanks for taking a look! SDKs no longer need updating - I'll minimize those comments to avoid confusion.

This PR is ready for review + testing, but I'm going to add some unit tests for the extra-tricky service lifecycle synchronization since @sipsma noticed some bugs there. They're theoretically fixed now, but I would like to have higher confidence in it.

vito added 3 commits September 8, 2023 13:33
also remove Service from RunningService; not needed

Signed-off-by: Alex Suraci <[email protected]>
refactor stub service along the way

Signed-off-by: Alex Suraci <[email protected]>
Signed-off-by: Alex Suraci <[email protected]>
@vito
vito force-pushed the services-via-gateway branch from 3390a1d to 4428189 Compare September 10, 2023 16:50
@vito

vito commented Sep 11, 2023

Copy link
Copy Markdown
Contributor Author

Fully ready now! Backfilled unit tests for the tricky service lifecycle and did some follow-up cleanups.

Comment thread engine/client/client.go
Comment on lines +305 to +308
// shutdown happens outside of c.closeMu, since it requires a connection
if err := c.shutdownServer(); err != nil {
rerr = errors.Join(rerr, fmt.Errorf("shutdown: %w", err))
}

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.

This solution is great!!! We don't need to deal with each SDK now. 👍

@gerhard gerhard added this to the v0.? milestone Sep 12, 2023
@jpadams
jpadams requested a review from TomChv September 13, 2023 02:22
@marcosnils

Copy link
Copy Markdown
Contributor

LGTM from the integration testing perspective.

@jpadams

jpadams commented Sep 13, 2023 •

Copy link
Copy Markdown
Contributor

I tested this on Go, Python, Node.js SDKs using the examples/sdk/*/db-service example with an engine from this PR brought up with ./hack/dev compared to the v0.8.4 engine.

In each case I got the old undesirable "running" behavior from v0.8.4 and the desired behavior from this PR build.

I ran some unrelated things for good measure. Looks good!

An hour long video tells the tale and can be produced as evidence, if required ;)

Node.js
image

Golang
image

Python
image

Typical broken one using v0.8.4 engine. After 30 minutes the yellow badge with ... elipses at top goes to gray stopwatch indicating timeout.
image

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

From my perspective of testing this live, I've had no issues with this at all. Seems super solid to me.

@jpadams

jpadams commented Sep 13, 2023

Copy link
Copy Markdown
Contributor

@TomChv any blocker to you giving this the 👍

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

This was a pleasure to read! Straightforward code, good tests & some nice refactorings along the way. While we don't have the new API yet - I was especially excited about not starting services when ops that require them are cached - everything else works as expected.

Before this PR, services wouldn't show as stopped in the interactive TUI (@jpadams mentioned it in the context of Cloud):

before-this-pr-service-in-interactive-tui.mp4

With the changes in this PR, services show as stopped:

after-this-pr-service-in-interactive-tui.mp4

Same command with dagger run --debug:

after-this-pr.mp4

@vito what would you like to add to the changelog, via changie new?

Signed-off-by: Alex Suraci <[email protected]>
@vito

vito commented Sep 13, 2023 •

Copy link
Copy Markdown
Contributor Author

@marcosnils @jpadams @gerhard Thanks so much for the thorough testing!

what would you like to add to the changelog, via changie new?

Added one! The best one-line summary I could come up with was "services: refactor to per-client instances that stop on client close."

@gerhard
gerhard merged commit 311e21d into dagger:main Sep 13, 2023
@vito
vito deleted the services-via-gateway branch September 13, 2023 14:39
@gerhard gerhard added the area/engine About dagger core engine label Sep 13, 2023
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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants