Skip to content

Group API - #4248

Merged
aluzzardi merged 10 commits into
dagger:mainfrom
aluzzardi:group-api
Jan 11, 2023
Merged

aluzzardi merged 10 commits into
dagger:mainfrom
aluzzardi:group-api

Conversation

@aluzzardi

@aluzzardi aluzzardi commented Dec 22, 2022 •

Copy link
Copy Markdown
Contributor

The group API allows to programmatically group related operations together for status reporting and visualization purposes (e.g. logs).

Groups are hierarchical and can contain any number of sub-groups.

TODO / Open Questions

  • Validate the API (blocker)
  • codegen: client.Group() currently returns a *dagger.Query
    • should codegen wrap *dagger.Query in a *dagger.Client, so it can be transparently passed around to unsuspecting dagger.Client receivers? (e.g. control grouping from the caller)
    • /cc @slumbering @dolanor @helderco
  • Q? Add custom attributes to groups/vertices?
    • Right now, it's just a name string
    • Built-in attributes (e.g. attached key/values) could provide a much better visualization experience (e.g. see the ENV for an exec)
  • Group passthrough and events stream back for dagger-in-dagger
    • Currently, dagger-in-dagger is a black hole in terms of events
  • Bug in API stitching (blocker)
    • extend type Query { group(...): Query! } doesn't work (complains about Query being undefined
    • Sometimes this randomly fails (group field not found in Query)

Example Usage

Taking this repository as an example, we perform hundreds of operations (e.g. exec, copies) which all get reported back as a single flat stream, with all operations mixed together.

This repository's CI is organized in 20 or so logical "parts": 3 SDKs with a build/lint/test/generate target each, an engine with a few targets, etc.

Using this API, each "logical part" would be grouped programmatically. The group hierarchy could look like this (simplified for illustration purposes):

Group Description
sdk Parent group for all SDKs
sdk/go Group for everything Go SDK related
sdk/go/lint Sub-group for Go SDK linting
engine Engine Group
engine/build Group for engine build operations

API Usage

Query API

  • Groups can be created at the query level
  • They are sticky (e.g. will affect the rest of the query)
  • They are hierarchical (e.g. group can be called multiple times).

Schema

extend type Query {
  group(name: String!): Query!
}

Usage

// `c` belongs to the root group at this point
c, _ := dagger.Connect(ctx)

c.Group("a").Group("b").Group("c").Container(...).Exec(...)  // Exec is part of group `a/b/c`

Example

// `c` belongs to the root group at this point
c, _ := dagger.Connect(ctx)

// everything using `c` is nested under the `sdk` group from now on
c = c.Group("sdk")

common := c.Container().From("alpine").Exec("codegen") // group is `sdk`

nodejs := c.Group("nodejs").Container().From("nodejs").Exec(...) // group is `sdk/nodejs`
golang := c.Group("golang").Container().From("golang").Exec(...) // group is `sdk/golang`

Container/Directory API

  • Groups can be created at the Container and Directory level
  • They are nested under the parent Query group (or Container group)
  • They are sticky (e.g. will affect the rest of the query)
  • They are hierarchical (e.g. group can be called multiple times).

API

extend type Container {
  group(name: String!): Container!
}

extend type Directory {
  group(name: String!): Directory!
}

Usage

c, _ := dagger.Connect(ctx)

root := c.Group("a") // query-level group

alpine := root.Container().From("alpine").Exec("apk add bash") // nested under `a` (query-level group)

alpine.Group("build").Exec("sh build.sh") // group is `a/build`
alpine.Group("test").Exec("sh test.sh") // group is `a/test`

Example

// `c` belongs to the root group at this point
c, _ := dagger.Connect(ctx)

// everything using `c` is nested under the `sdk` group from now on
c = c.Group("sdk")

nodejs := c.Group("nodejs").Container().From("nodejs").Exec("yarn install") // group is `sdk/nodejs`
lint := nodejs.Group("lint").Exec("yarn lint") // group is `sdk/nodejs/lint`
test := nodejs.Group("test").Exec("yarn test") // group is `sdk/nodejs/test`

Built-in Groups

Some high-level operations in the API map to multiple low-level API calls, resulting in an incomprehensible mismatch. As in, executing a single operation can result in multiple "statuses" being sent back, without any way to correlate them.

The problem is even worse because of caching -- the number of low-level API calls might be different each run.

The API now creates a child-group automatically when performing multiple low-level operations, resulting in a 1:1 mapping for high-level calls to statuses.

Changes:

c.Host().Directory()

  • Before
    • local://<path>
    • copy / /
  • With built-in group
    • host.directory <path>
      • upload
      • copy
    • NOTE: This one causes the most cache misses in our pipelines. Without grouping, it's tricky to debug because what matters is the cache status of copy / /, not the upload part.

c.Container().From()

  • Before
    • resolve image config for docker.io/library/node:18-alpine
    • from alpine
  • With built-in group
    • from alpine
      • resolve image config
      • pull alpine
    • NOTE: resolve image config shows up every time whereas the pulling part only if the image digest has changed from cache. Without sub-groups, every single From() maps back to 1 or 2 statuses, depending on cache. With sub-groups, there's always going to be a single from group for each From() call in a predictable way.

c.Container().Build()

  • Before
    • Flat list of all Dockerfile steps, mixed together with your own operation logs
    • [1/12] FROM docker.io/...
    • [2/12] RUN apk add curl
  • With built-in group
    • Every single operation resulting from docker build is nested together in a single group
    • docker build
      • [1/12] FROM docker.io/...
      • [2/12] RUN apk add curl
    • NOTE: Without this, currently all docker build logs are mixed together. As in, if we're building 4 docker files in a pipeline, we would see 4 times [2/12] RUN apk add curl with no idea which build it belongs to

/cc @sipsma @vito @shykes @helderco @dolanor

@shykes

shykes commented Dec 22, 2022

Copy link
Copy Markdown
Contributor

First: wow! This will take our DX and OX to the next level.

Second: my biggest question is on expected consumption model.

  • I know this information will be invaluable for human operators, either in logs or other visualization UI in the console or web.
  • Are there other ways that we anticipate this information being consumed? And does it involve introspecting groups from a graphql client? In other words is this groups API “write only” if that makes sense?

@aluzzardi aluzzardi mentioned this pull request Jan 4, 2023
@aluzzardi

Copy link
Copy Markdown
Contributor Author

Second: my biggest question is on expected consumption model.
Are there other ways that we anticipate this information being consumed? And does it involve introspecting groups from a graphql client? In other words is this groups API “write only” if that makes sense?

Yes, absolutely. This PR is "write only" at this time.

Tentative plan is as follows:

  • Long Term: Event stream (which is what "groups" are for -- events) is accessible via the API (GraphQL Subscriptions seem like a perfect fit). Format is stable and documented. Anyone can build on top. SDKs provide API bindings for the stream

  • Short Term: The only consumer of events (and groups) is the dagger CLI. The Dagger CLI both produces (via the session binary) and consumes (via dagger run) the events to display a visualization UI. Format is opaque, undocumented and unstable (since the same CLI produces and consumes events, the format is free to completely change on each iteration)

@aluzzardi
aluzzardi requested review from sipsma and vito January 4, 2023 14:28

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

API looks great! Just some minor comments.

Comment thread core/group.go Outdated

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.

nit: the name made me think this might return a *pb.Definition

Suggested change
func (g Group) LLB() llb.ConstraintsOpt {
func (g Group) LLBOpt() llb.ConstraintsOpt {

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.

Fixed!

Also renamed PG() to ProgressGroup()

Comment thread core/container.go Outdated

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.

Makes sense, just curious: does something (perhaps your CLI demo) parse this out? Don't see any other reference to dagger.group in this PR.

@sipsma sipsma Jan 4, 2023 •

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.

On this note, if it's straightforward I think we should update our current plain progress code to strip this out when present, otherwise the output looks like: #3 [dagger.group:["from alpine:3.16.2"]] resolve image config for docker.io/library/alpine:3.16.2 which is kind of confusing.

Comment thread core/container.go Outdated

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.

Looks like this supports taking a progress group, so in this case the CustomName is just to make it clearer in the UI that this is an internal thing?

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.

Yes -- this appears as a random mkdir /meta and it's not obvious what it stands for (the user requested an exec and this randomly appears)

  • creating dagger metadata is to make it more obvious than mkdir /meta
  • [dagger.internal] is to hide it altogether from the UI

See comment below for an alternative

Comment thread core/schema/group.go Outdated

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.

Do we need to copy parent? 🤔

Comment thread core/container.go Outdated

@sipsma sipsma Jan 4, 2023 •

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.

On this note, if it's straightforward I think we should update our current plain progress code to strip this out when present, otherwise the output looks like: #3 [dagger.group:["from alpine:3.16.2"]] resolve image config for docker.io/library/alpine:3.16.2 which is kind of confusing.

Comment thread router/schema.go Resolved
@aluzzardi

aluzzardi commented Jan 5, 2023 •

Copy link
Copy Markdown
Contributor Author

@sipsma @vito: Answering to both related comments, with a question/proposal:

On this note, if it's straightforward I think we should update our current plain progress code to strip this out when present, otherwise the output looks like: #3 [dagger.group:["from alpine:3.16.2"]] resolve image config for docker.io/library/alpine:3.16.2 which is kind of confusing.

Looks like this supports taking a progress group, so in this case the CustomName is just to make it clearer in the UI that this is an internal thing?

So there's 2 "special" naming conventions:

  • [dagger.group:["group", "sub"]] name
    • Used for vertices that do not support progress groups (resolve image config)
  • [dagger.internal] name
    • Used for "internal" stuff: right now it's just mkdir /meta
    • mkdir /meta shows up on every exec: it's noisy and pointless. It's doesn't map to something the user actually requested, doesn't produce any logs, doesn't take any significant time to compute.
    • UI just hides dagger.internal vertices. We could have a flag to optionally un-hide them

It's simple enough, however parsing this out is a bit hacky with edge cases, and we'd have to replicate the logic for every consumer.

What do you think of using JSON as vertex names? With name, group and internal keys:

{"name": "go build"}
{"name": "docker.io/library/alpine:3.16.2", "group": ["from alpine:3.16.2"]}
{"name": "mkdir /meta", "internal": true}

In the future, we could actually make this fully structured:

{"op": "exec", "args": ["go", "build"], "env": {"GOOS": "linux"}}

This would give a lot of liberty to the UI for rendering (e.g. color code file ops and execs differently) and observability (e.g. filter pipeline timers by execs, show me the slowest image pulls, ...)

Comment thread core/query.go Outdated

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.

side note: @sipsma if we ever want to, we could use this same mechanism for a query.WithDefaultPlatform()

@aluzzardi
aluzzardi force-pushed the group-api branch 2 times, most recently from e2dfd88 to a0f4920 Compare January 5, 2023 13:08
@helderco

helderco commented Jan 6, 2023

Copy link
Copy Markdown
Contributor

codegen: [...] should codegen wrap *dagger.Query in a *dagger.Client, [...] \cc @helderco

In Python dagger.Client is actually dagger.Query:

return "class Client(Root):" if t.name == "Query" else f"class {t.name}(Type):"

@sipsma
sipsma marked this pull request as ready for review January 6, 2023 17:27
aluzzardi pushed a commit to aluzzardi/dagger that referenced this pull request Jan 9, 2023
Add support for `_EXPERIMENTAL_DAGGER_JOURNAL`. This will dump raw
events to a log file, useful for post analysis.

Currently this will only be used internally and is subject to removal.

Related: dagger#4248

Signed-off-by: Andrea Luzzardi <[email protected]>
@aluzzardi
aluzzardi force-pushed the group-api branch 3 times, most recently from e632eee to 2b0ab02 Compare January 10, 2023 13:44
@aluzzardi

Copy link
Copy Markdown
Contributor Author

@vito @sipsma So there's a bug that prevents query fields from returning a query within an extend (e.g. extend Query { field(...): Query):

failed to resolve all type definitions: [Query]

Seems like it's a bug deep down in graphql-go-tools. I gave up debugging and instead added groups as part of the query schema, now in core (I removed the empty definition from the router).

Thoughts?

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

Sounds fine to me. 👍

The .graphqls just needs some tidying up, otherwise LGTM!

Comment thread core/schema/container.graphqls Outdated
Comment on lines 24 to 25

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.

Dupe?

Suggested change
group(name: String!): Container!

Also needs a doc string

Comment thread core/schema/directory.graphqls Outdated

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.

needs a doc string

Comment thread core/schema/query.graphqls Outdated

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.

doc string

@dolanor

dolanor commented Jan 10, 2023

Copy link
Copy Markdown
Contributor

My first reaction to this is just that Group might not be explicit enough.
Container.Group seems more of a group of containers than a group of event/log placed on this container instance.

I also don't like long names, but in the name of clarity, we could use: ProgressGroup, LogGroup, EventGroup? Open for suggestions.

@dolanor

dolanor commented Jan 10, 2023

Copy link
Copy Markdown
Contributor

I love where this is going! I like pipeline.

@vito

vito commented Jan 10, 2023 •

Copy link
Copy Markdown
Contributor

Pipeline SGTM too! We're already calling the entire outer query a "pipeline" colloquially, and this API call effectively forms a sub-pipeline or branch of the pipeline, so it feels consistent.

Comment thread core/container.go Resolved
aluzzardi pushed a commit that referenced this pull request Jan 11, 2023
* journal file

Add support for `_EXPERIMENTAL_DAGGER_JOURNAL`. This will dump raw
events to a log file, useful for post analysis.

Currently this will only be used internally and is subject to removal.

Related: #4248

Signed-off-by: Andrea Luzzardi <[email protected]>

* journal: append to file

Signed-off-by: Andrea Luzzardi <[email protected]>

Signed-off-by: Andrea Luzzardi <[email protected]>
@aluzzardi

Copy link
Copy Markdown
Contributor Author

@sipsma re: your earlier comment:

On this note, if it's straightforward I think we should update our current plain progress code to strip this out when present, otherwise the output looks like: #3 [dagger.group:["from alpine:3.16.2"]] resolve image config for docker.io/library/alpine:3.16.2 which is kind of confusing.

Just implemented that (engine.go). A bit nasty but does the trick (channel "proxying" for DisplaySolveStatus)

Dealing with one last test then this is good to go

@aluzzardi

Copy link
Copy Markdown
Contributor Author

@vito I think I'm hitting the same issue you described here: #4130 (comment)

I've changed query.directory to do (simplified):

	if args.ID != "" {
		return &core.Directory{
			ID: args.ID,
		}, nil
	}

	return core.NewDirectory(ctx, llb.Scratch(), "", pipeline, platform)
}

I believe the core.NewDirectory() with llb.Scratch() is causing a panic. Investigating

Comment thread core/directory.go Outdated

@aluzzardi aluzzardi Jan 11, 2023 •

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.

@vito PTAL, I think this fixes #4130 (as well as this PR) -- see last commit

  • Copied the original check logic for llb.Scratch() above, however check for res.SingleRef() returning nil (rather than checking payload.LLB == nil). AFAIK gw.Solve() + SingleRef() will always return nil for a scratch result
  • Removed the original scratch check above for repetition. Solving llb.Scratch() (or nil) will result to a scratch result, so the edge case is already captured (the only downside is we lose a bit of time trying to Solve() scratch, but that's ok)
  • Not sure what edge cases path.Clean(src) == "." is supposed to catch, but that didn't work for Directory(""). So I added an explicit / check to make this work for scratch.Directory(""), scratch.Directory("/")
  • Added dir Mode to Stat of scratch. container.Directory() has a if !info.IsDir() { return nil, fmt.Errorf("path %s is a file, not a directory", dirPath) } check, which was failing

/cc @sipsma

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.

Nice, if ref == nil feels a lot more sturdy. LGTM!

@aluzzardi
aluzzardi force-pushed the group-api branch 4 times, most recently from 0bed142 to 86f071e Compare January 11, 2023 18:19
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.

6 participants