Repository navigation
Description
Problem
Developers are often confused by a property of the engine called "laziness": pipelines are executed at the latest possible moment, to maximize performance.
Sometimes developers want to selectively resolve the laziness: they need to know that at a certain point in their code, the pipeline they have defined so far has actually been executed. Today this can only be done with painful workarounds.
Solution
Allow developers to selectively resolve the laziness, by forcing synchronous execution of the pipeline up to a specific point. By default, lazy execution remains unchanged.
Design
Status: in discussion
There are several design proposals for this. Some have been proposed as part of broader proposals, which has made consensus more difficult. This issue aims to narrow the focus to one specific problem, with one specific solution - hopefully a narrower scope of design will lead to consensus more easily.
The current candidate is proposed by @aluzzardi and @helderco :
extend type Container {
sync: ContainerID!
}
extend type Directory {
sync: DirectoryID!
}
extend type File {
sync: FileID!
}
In codegen, instead of returning the ID, passthrough the object so you can continue chaining lazily.
Examples
Instead of using ExitCode for everything, especially when you don’t actually care about the command’s exit code value, replace with Sync:
- _, err = ctr.ExitCode(ctx)
+ _, err = ctr.Sync(ctx)Unlike ExitCode however, Sync doesn’t assume you want to run the entrypoint when there’s no WithExec. This is perfect for making sure a Dockerfile just builds successfully, without running it’s command:
- _, err := client.Container().Build(src).Rootfs().Entries(ctx)
+ _, err := client.Container().Build(src).Sync(ctx)If you do need to execute the command, than just add a WithExec(nil). The empty arguments will be replaced by the entrypoint and default args.
You can also use the return value to continue chaining, without having to break into a before and after Sync:
// just build, don't run command
ctr, err := client.Container().Build(src).Sync(ctx)
if err != nil {
return err
}
// you can continue chaining
// (empty WithExec will run default command)
_, err := ctr.WithExec(nil).Sync(ctx)
if err != nil {
return err
}Same example in Python, which raises exceptions on errors (same in Node.js):
# just build, don't run command
ctr = await client.container().build(src).sync()
# you can continue chaining
# (empty with_exec will run default command)
await ctr.with_exec([]).sync()Note: This example is silly and is just for illustration purposes. If you do intend to execute the command, it’s best not to do the first sync since build is a dependency and will already run first and fail early.
Remaining questions
Which object types?
It doesn’t make sense to add sync to all types so we need to decide where this is useful.
### Tasks
- [x] https://github.com/dagger/dagger/pull/5071
- [ ] https://github.com/dagger/dagger/pull/5414
- [ ] https://github.com/dagger/dagger/pull/5416
Activity
helderco commentedon May 2, 2023
helderco commentedon May 4, 2023
This is actually possible in Python:
Just by adding
awaitwe can implicitly call.sync().helderco commentedon May 29, 2023
Update:
Container.synchas been released last week, need to add to more types.vito commentedon Jul 29, 2023
@helderco @aluzzardi Nice call with having
syncAPIs returnFooID!For similar APIs like
Service.startandService.stopI've been toying with aVoidtype which the SDK codegen special cases to just returnerror.I was about to propose switching
Foo.syncto it, but realized the advantage of theFooIDapproach is that you can chain from it, which is particularly expressive in theService.startuse case:This feature was already called out in the PR description but I didn't totally appreciate the use case.
I do wonder if we should tweak the API to return
Foo!instead ofFooID!someday and use some sort of schema annotation to tell SDK codegen that it's synchronous. Right now it feels a little coupled to our codegen heuristics. But maybe y'all have already discussed those trade-offs.helderco commentedon Jul 29, 2023
I don't think there's any major trade-offs. We just needed a leaf to trigger execution.
We can continue to conventionalize on the
syncfield name for this:Since we need a leaf, we can use
idfor example:{ container { - sync + sync { + id + } } }The need for adding the leaf is a trivial change and well worth the benefit of unlocking chaining in the API (single query) and less of an "SDK thing":
{ container { from(address: "alpine") { with_exec(args: ["false"]) { sync { with_exec(args: ["echo", "short-cirtuited"]) { stdout } } } } } }But in the SDKs it would still have to work like today, by making
Syncselectidfor the leaf, which is easy to do.Am I understanding correctly? If so I'm onboard with that. Atm I don't see any problems.
vito commentedon Jul 30, 2023
@helderco Yeah exactly! I like the idea of just selecting
id.I'm about to add two more APIs that do something synchronous though (
Service.startandService.stop) so I wonder if we should use some kind of hint to the SDKs. In the short-term we can just special-case these fields, but in the long term it would be nice if SDKs didn't have to change for each new synchronous API we add or change.Maybe we can use some sort of annotation?
helderco commentedon Jul 30, 2023
Yes, I was going to suggest a
@syncdirective in my previous comment but I only had thesyncfield to go with. Adding more fields with this behavior makes sense to use a custom directive. 👍helderco commentedon Jul 30, 2023
Today, looking at my query example above is silly because it'll fail on
withExec(args: "false")and never reachsync! 😜Now I'm wondering what's the use case of using
synclike that in the API? Makes sense forService.start, but I can't think of anything for usingsyncwhere it's not the leaf. 🤔vito commentedon Jul 30, 2023
Yeah I don't think there's much use case for chaining
syncat the GraphQL API layer, but I would argue it should just follow the same rules as everything else anyway. If nothing else there's at least a use case forsync { id }. The rest of the API is designed to not requiresyncand it's really just there to do one tiny thing (force evaluation), so it makes sense to me that its use case would be narrow (but potent).helderco commentedon Jul 30, 2023
Yeah it's interesting because id executes the query but not the DAG. Just wrap it in sync and the dag is solved.
In the SDKs we hide the id just like in inputs.