Skip to content

Force synchronous execution (disable laziness) #5065

Closed

Description

@shykes

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

  1. helderco commented on May 2, 2023

    @helderco
  2. helderco commented on May 4, 2023

    @helderco
    Contributor

    This is actually possible in Python:

    # lazy
    ctr = client.container().build(src)
    
    # not lazy
    ctr = await client.container().build(src)

    Just by adding await we can implicitly call .sync().

  3. helderco commented on May 29, 2023

    @helderco
    Contributor

    Update: Container.sync has been released last week, need to add to more types.

  4. vito commented on Jul 29, 2023

    @vito
    Contributor

    @helderco @aluzzardi Nice call with having sync APIs return FooID!

    For similar APIs like Service.start and Service.stop I've been toying with a Void type which the SDK codegen special cases to just return error.

    I was about to propose switching Foo.sync to it, but realized the advantage of the FooID approach is that you can chain from it, which is particularly expressive in the Service.start use case:

    // somewhere in your test suite setup
    dockerd, err := c.Container().From("docker:dind").Service().Start(ctx)
    
    // dockerd is now running, and will stay running
    // so you don't have to worry about it restarting after a 10 second gap
    
    // then in all of your tests, continue to use an explicit binding:
    tests, err := c.Container().From("golang").
      WithServiceBinding("docker", dockerd).
      WithEnvVariable("DOCKER_HOST", "tcp://docker:2375").
      WithExec([]string{"go", "test", "./..."}).
      Sync(ctx)
    
    // or, if you prefer not to Google what port `docker:dind` listens on,
    // trust `Endpoint` to construct the address.
    //
    // note that this has the exact same non-cache-busting semantics as WithServiceBinding,
    // since hostnames are stable and content-addressed
    //
    // this could go in global suite setup.
    dockerHost, err := dockerd.Endpoint(ctx, dagger.ServiceEndpointOpts{
      Scheme: "tcp",
    })
    
    tests, err := c.Container().From("golang").
      WithEnvVariable("DOCKER_HOST", dockerHost).
      WithExec([]string{"go", "test", "./..."}).
      Sync(ctx)
    
    // if your tests created local files (e.g. failed browser test screenshots),
    // access them using the tests var

    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 of FooID! 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.

  5. helderco commented on Jul 29, 2023

    @helderco
    Contributor

    I do wonder if we should tweak the API to return Foo! instead of FooID! 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.

    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 sync field name for this:

    type Container {
        sync: Container!
    }

    Since we need a leaf, we can use id for 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 Sync select id for 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.

  6. vito commented on Jul 30, 2023

    @vito
    Contributor

    @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.start and Service.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?

    type Container {
      sync: Container! @sync
    }
    
    type Service {
      start: Service! @sync
      stop: Service! @sync
    }
  7. helderco commented on Jul 30, 2023

    @helderco
    Contributor

    Yes, I was going to suggest a @sync directive in my previous comment but I only had the sync field to go with. Adding more fields with this behavior makes sense to use a custom directive. 👍

  8. helderco commented on Jul 30, 2023

    @helderco
    Contributor

    Today, looking at my query example above is silly because it'll fail on withExec(args: "false") and never reach sync! 😜

    Now I'm wondering what's the use case of using sync like that in the API? Makes sense for Service.start, but I can't think of anything for using sync where it's not the leaf. 🤔

  9. vito commented on Jul 30, 2023

    @vito
    Contributor

    Now I'm wondering what's the use case of using sync like that in the API? Makes sense for Service.start, but I can't think of anything for using sync where it's not the leaf. thinking

    Yeah I don't think there's much use case for chaining sync at 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 for sync { id }. The rest of the API is designed to not require sync and 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).

  10. helderco commented on Jul 30, 2023

    @helderco
    Contributor

    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.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions