Skip to content

Proposal: Helper for multiple pipeline synchronization #5083

Closed

Description

@helderco

Summary

This proposal suggests adding an SDK helper for efficiently synchronizing multiple pipelines, in an easy to use way.

Background

This is based on one aspect of:

Motivation

Often times, you want to execute multiple pipelines in parallel (e.g. build & test). This is surprisingly tedious to do:

eg, gctx := errgroup.WithContext(ctx)

eg.Go(func() error {
    _, err := test.Sync(gctx)
    return err
})

eg.Go(func() error {
    _, err := lint.Sync(gctx)
    return err
})

return eg.Wait()

Even though it’s simpler in Python, when using an await inside an async function, some users think that it’s already asynchronous, but that’s different from being concurrent.

Even then, it’s not obvious to newcomers that the third-party library AnyIO offers a more friendly API (using structured concurrency) compared to the built-in AsyncIO module:

async with anyio.create_task_group() as tg:
    tg.start_soon(lint)
    tg.start_soon(test)

It’s even simpler in Node.js, but the need for concurrency may still be obscure:

await Promise.all([
    lint.sync(),
    test.sync(),
])

Documentation can help here. The following issue was created not only to demonstrate that using concurrency can make a big difference, but also how:

However, can we also convenience users with an easy to use DX.

Specification

Introduce a client.Sync() helper as a convenience or syntax sugar to easily synchronize multiple lazy pipelines:

Go

err := client.Sync(ctx, test, lint)

Python & Node.js

await client.sync(test, lint)

This can simply be an abstraction over the language’s concurrency features as seen above, or if possible, use the underlying query builder to make a single query with multiple sub-queries:

query {
    sync1: container { 
        ... withExec(["go", "test", ...]) {
            sync
        }
    }
    sync2: container {
        ... withExec(["gofmt", ...]) {
            sync
        }
    }
}

All objects need to be resolved to IDs first, but that’s already handled by the SDK and that process can also use the same optimization.

This is simple to do with sync because we don’t need to return multiple values to the user. Just an error group with all the errors from the GraphQL response.

Rationale

This proposal doesn’t prevent using the language’s concurrency features if needed or desired, but it provides the following benefits:

  • Very easy/simple to use;
  • Reduces concurrency burden on user to implement correctly;
  • Uniform in all SDKs, which eases adoption (doing concurrency directly is different in all of them);

Trade-offs

Besides requiring adding code to the SDKs which can become a source of drift, this only works with sync, so you still need to learn how to do concurrency properly for other fields.

Since the main purpose is to reduce the concurrency burden on the user, a more general solution would be better. I have some rough ideas in mind for that, but need to invest time and prototype for feasibility.

Alternatives considered

Doing this in the API would prevent the need for custom helper code in each SDK. The general idea was something like:

type Query { 
    "Execute several pipelines in parallel"
    sync([ID!]!): Query! 
}

There’s multiple problems with this:

  • We don’t have a unified ID in Dagger, and I’m not sure we can (at least for now);
  • We can’t use unions, interfaces or variadic input arguments in GraphQL;
  • For an array of multiple types in Go, you’d have to call it like client.Sync(ctx, []interface{lint, test})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions