Skip to content

Refactor core module implementation as a DAG - #6167

Merged
sipsma merged 20 commits into
dagger:mainfrom
sipsma:refactor
Dec 4, 2023
Merged

sipsma merged 20 commits into
dagger:mainfrom
sipsma:refactor

Conversation

@sipsma

@sipsma sipsma commented Nov 28, 2023 •

Copy link
Copy Markdown
Contributor

This refactor cleans up a lot of the spaghetti that has been slowly growing over time in the implementation of the module API in core/schema/module.go. There's no new features or breaking changes, just an internal cleanup and some more robust handling of certain cases

This felt needed as the code was beginning to get really mind bending to modify and required jumping all over the place to different methods on the moduleSchema object, keeping track of passing the right schemaView, keeping track of multiple cachemaps, etc. The confusion was not only slowing me down (presumably others too), but also resulting in bugs due to the non-obvious nature of any given modification.

Given upcoming features being planned or considered like interface support, self calls for modules, function call caching control, etc. this felt like the right time to improve the situation.

The 2 most significant aspects of the new approach here are:

  1. The set of modules loaded into a given server (i.e. for a given session) are explicitly arranged in an in-memory DAG.
    • We don't really do a ton of DAG-stuff like walking/topological-sorting/etc. yet, but it's possible now.
    • The most immediate benefits of this new model are more that it makes caching+re-use of module operations easier to think about.
  2. The core/schema/ package maintains its own wrapper types around most of the core types related to modules.
    • This allows us to store extra information in-memory without needing to wrack our brains around how that will affect (and/or break) serialization/caching of core api types.
    • The biggest benefit here is that the logic that existed before is much more self-contained and well-organized. E.g. the way fields and functions are converted to schemas are all their own self-contained methods rather than spread all over, handling ID conversions on types are all well-contained and well-defined, etc.
    • These are all defined in schema/moddag.go and schema/modtypes.go; those are good places to start reviewing

Some more misc benefits + changes

  1. core is modeled as a module to a limited extent now; it has a special implementation of the new Mod interface and is after that treated the same as any other module dependency (besides being an implicit forced dependency of every user module).
    • This does not result in core being exactly the same as a user module (yet), but it's a step in that direction. And in the meantime it allows us to remove a lot of the if statements that were scattered all over to handle the core types separately.
  2. We now detect circular dependencies in modules and error out rather than deadlock or otherwise fail in undefined ways

@sipsma
sipsma requested review from jedevc and vito November 28, 2023 18:32
@sipsma sipsma changed the title Refactor core module implementation w/ ModDag Refactor core module implementation as a DAG Dec 1, 2023
@sipsma
sipsma marked this pull request as ready for review December 1, 2023 00:34
@sipsma

sipsma commented Dec 1, 2023

Copy link
Copy Markdown
Contributor Author

cc @vito @jedevc this is good to go now, let me know if I can help clarify anything or otherwise help review! I know it's a bit of a doozy in terms of size but the goal is to help us retain our collective sanity as we continue to iterate on the code here, so hopefully it pays dividends pretty quickly 🙏

Comment thread core/cachemap.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.

@vito just want to make sure this isn't lost in the noise

I added this to enable detection of recursive module dependencies w/out deadlock. I originally added the detection using normal DAG walking approaches but then still hit deadlock due to the fact that module loading is wrapped in a CacheMap in schema/schema.go.

This should only ever detect situations that are 100% guaranteed to result in deadlock, so I think it's probably a good change to have in general too.

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.

Ha, I ran into a deadlock in the same place in #6158 so 👍

Comment thread core/schema/modtypes.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.

@jedevc calling this one out so it's not lost in the noise, does the comment here sound correct? Basically, custom objects are still sent to SDKs as inputs in the form of direct json serialization, whereas core types are sent serialized to their ID.

If so, it's probably worth fixing I'd think?

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.

So 😄

With the original implementation of #6102 (before we stopped doing the foreign types), there were two behaviors here when converting a UserModObject to an SDK input:

  • If the object is from the module that is going to be called, then we need to convert it to JSON
  • If the object is from a completely different module, then keep it as an ID

I think there's good reasons for keeping this behavior - I don't think we should do the action from the comment.


One nice benefit of the previous behavior was that modules did not need to be aware of their own ID types - so they would never need to decode their own IDs. There are a couple ways that a module could decode it's own ID:

  • Be aware of how IDs themselves are encoded - I really wanted to avoid this though, since currently the ID encoding is really only known in the engine, and (at least until IDs v2) I think we should keep this in one place. Also, this means that modules don't actually need to keep track of their own names (so they know not to accidentally decode their own IDs).
  • Have the module call a new resolver, something like decodeMyID which would convert the ID into a JSON object. But at this point, in each SDK we would need to call this for on all the input objects, which is quite chatty - it's much easier to just do this work ahead of time since we know the module is going to ask for the JSON representation anyways.

In both cases, the SDK has to get a bit more complicated to handle these cases - since every SDK would need to do this anyways, I think it makes more sense to have each modules own objects not have IDs (from it's own perspective).


I think in the refactor this has also lost the two separate semantics where we only convert our own types to JSON, and all other module types are IDs. Even though we currently don't test for that, since it was removed in #6102 (for very good reasons btw, thanks for following all those up @sipsma and @vito ❤️).

I think we should try and keep the semantics just so that if we ever go back and add this functionality it's already supported at this level. We could do that by adding a new input inputMod ModType arg to ConvertToSDKInput that contains the module we're converting to and then implement that conditional behavior by comparing obj.mod to inputMod to see if they're the same.

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.

There are a couple ways that a module could decode it's own ID

The other option in addition to what you listed is to just have it call the load%sFromID query for the object, but that would require self-api calls which aren't a thing yet.

I think if we support that someday it would probably be worth revisiting since in theory only having one way of encoding/decoding any objects in an SDK should actually result in simpler behavior/less code (in the grand scheme of things, obviously there would be updates to support self-calls and then fix existing SDKs to handle this). But agree that in retrospect there aren't really good ways of doing this in the meantime. I'll update the comment to just explain what's going on rather than suggest we change it now 👍

I think we should try and keep the semantics just so that if we ever go back and add this functionality it's already supported at this level.

I'll look at this, but in general I'm extremely hesitant to add code for cases that we can't test yet and would just be sitting around waiting for a hypothetical future. If it's super trivial to do or possible in such a way that it's inherently handled as part of the existing logic we need then yeah makes sense. But otherwise I'll just add a comment about this not handling objects from other user modules yet.

@sipsma sipsma Dec 4, 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.

Ended up not including code to support this (it's significant complication to handle for a case that may or may not ever happen), but updated the comment and if we ever do need to deal with this, it's fairly a straightforward modification relative to the whole refactor :-)

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.

Yup that's fine, the comment looks clear enough, thanks! 🎉

Comment thread engine/opts.go Resolved
Comment thread core/ids.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.

Just remembered this while looking through this file...

ResourceFromID can now be removed, with #6102, no one calls it.

I would also love to remove ResourceToID, and implement it similarly to ResourceFromID, there's only now one eventual caller to it: https://github.com/dagger/dagger/blob/f5f47f6f6ff6aaf211af1c1fe58e7d38dc93c992/core/schema/modtypes.go#L116-L121.

Then we could avoid having these huge switch statement which make it a pain to add new ID types, which would be really nice.

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.

ResourceFromID can now be removed, with #6102, no one calls it.

Oh nice, yeah I'll remove that

I would also love to remove ResourceToID, and implement it similarly to ResourceFromID, there's only now one eventual caller to it:

It's somewhat less obvious how to do this due to the various subtleties/limitations of go generics, but I'll see if anything can be done reasonably. I don't like that huge switch case either but there's a possibility we might be stuck with it or it would require refactoring way out of scope here.

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.

Removed both, ResourceToID wasn't that hard either; I had flashbacks while writing my last comment to trying to remove it previously which were thwarted by trying to retain the type safety for too long, but the IDableObjectResolver signatures are all just any and string, which provided you accept rather than fight makes this easy.

I was also tempted to remove IDableObjectResolver entirely and just pass this around more explicitly, but my attempts to do so ended up just passing around another map of string->modtype in addition to resolvers which made me realize it probably wasn't that beneficial at the end of the day; can revisit if ever desired though.

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.

🎉 wooo! Thanks, nice to see it cleared up 💜

Comment thread core/module.go Resolved
Comment thread core/schema/modtypes.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.

So 😄

With the original implementation of #6102 (before we stopped doing the foreign types), there were two behaviors here when converting a UserModObject to an SDK input:

  • If the object is from the module that is going to be called, then we need to convert it to JSON
  • If the object is from a completely different module, then keep it as an ID

I think there's good reasons for keeping this behavior - I don't think we should do the action from the comment.


One nice benefit of the previous behavior was that modules did not need to be aware of their own ID types - so they would never need to decode their own IDs. There are a couple ways that a module could decode it's own ID:

  • Be aware of how IDs themselves are encoded - I really wanted to avoid this though, since currently the ID encoding is really only known in the engine, and (at least until IDs v2) I think we should keep this in one place. Also, this means that modules don't actually need to keep track of their own names (so they know not to accidentally decode their own IDs).
  • Have the module call a new resolver, something like decodeMyID which would convert the ID into a JSON object. But at this point, in each SDK we would need to call this for on all the input objects, which is quite chatty - it's much easier to just do this work ahead of time since we know the module is going to ask for the JSON representation anyways.

In both cases, the SDK has to get a bit more complicated to handle these cases - since every SDK would need to do this anyways, I think it makes more sense to have each modules own objects not have IDs (from it's own perspective).


I think in the refactor this has also lost the two separate semantics where we only convert our own types to JSON, and all other module types are IDs. Even though we currently don't test for that, since it was removed in #6102 (for very good reasons btw, thanks for following all those up @sipsma and @vito ❤️).

I think we should try and keep the semantics just so that if we ever go back and add this functionality it's already supported at this level. We could do that by adding a new input inputMod ModType arg to ConvertToSDKInput that contains the module we're converting to and then implement that conditional behavior by comparing obj.mod to inputMod to see if they're the same.

Comment thread core/schema/modtypes.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.

Oooh I really like this approach ❤️ Much neater than my original work!

One little nit: could we consider breaking this down into multiple separate files, maybe in a new sub-package? Maybe primitive.go, core.go and module.go (and maybe even split that one a bit further)? It's still just a little bit difficult to navigate around by itself.

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.

Yeah can definitely break down into files more. I also agree it would be nice to have a separate package for all the new stuff; it may require creating some new interfaces so that we can pass in *APIServer without creating circular dependencies, but probably not that bad (famous last words, past attempts at splitting up into more packages led to surprising circular imports that caused me to back out, we'll see if this one goes smoother).

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.

This is the one I haven't gotten to yet. The naive attempts at splitting out to a separate package are indeed fairly big changes because you need to move a ton of stuff out of schema directly like SchemaResolvers, mergeSchemaResolvers, etc. and call them in both schema and the new package.

That also just raises the point that they don't feel like they especially belong in separate packages.

I can imagine ways of cleaning up the schema package so that there's less files, but I actually think the inverse of moving most everything else out besides schema.go and the mod dag related files would make more sense.

I'll just split up into separate files a little bit more for now (tomorrow morning). The other approaches that make sense to me are too out of scope.

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.

Split up into separate files some more:

  1. moddag.go - just the Mod interface and ModDeps struct
  2. modtypes.go - just the ModType interface and the trivial implementations (Primitive and List)
  3. coremod.go - the special implementation of Mod and ModType for core apis
  4. usermod.go - the Mod implementation for a user module and ModType implementation for user mod objects
  5. usermodfunction.go - the implementation of UserModFunction, which is chonky enough to deserve its own separate file I think

Comment thread core/schema/moddag.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.

What's the bool for in the return here? If it's just an ok, could we just do ModType == nil?

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.

I tend to prefer the extra bool in cases where it's expected that a value may not be found and you want all callers to handle it, since nil checks being required can quickly turn into nil panics in prod.

@vito vito Dec 1, 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.

When checkDirectDeps is true, how does that relate to namespacing? I'm guessing the queried typeDef is already namespaced to FooType for a module Foo?

(edit: deducing 'yes' from the rest of the code)

@sipsma sipsma Dec 4, 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.

I tend to prefer the extra bool in cases where it's expected that a value may not be found and you want all callers to handle it, since nil checks being required can quickly turn into nil panics in prod.

Yes agree that's why I did this; forcing callers to acknowledge the extra bool reminds them to check it and doesn't actually result in overall more code.

When checkDirectDeps is true, how does that relate to namespacing? I'm guessing the queried typeDef is already namespaced to FooType for a module Foo?

Yes namespacing is applied right as objects are loaded, so there is never really any times an outside caller is gonna see the unnamespaced object. I added a few comments in various places clarifying this as it's indeed a good point to be explicit about.

Comment thread core/schema/schema.go Resolved

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

[will continue reviewing moddag.go and modtypes.go, just don't like having pending comments for too long]

Comment thread core/module.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.

Is the API for this being retired? It's always blank at the moment. I'm not really sure where it would come from now that modules have multiple types. Closest thing I can think of is Go's package-level docs, but at this point it's pretty much being replaced with READMEs.

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.

Oh I saw it was unused completely and just removed it; did not realize it was in graphql. I put it back in for now since I'm not trying to scope stuff like that in here (though it technically doesn't matter either way while it's unused); agree it may make sense to remove but can be done separately.

Comment thread core/schema/schema.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.

neat

Comment thread core/schema/schema.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.

Is Go SDK codegen the only special case? Or do you think there may be more in the future, like stdlib? (Though I'm guessing even that we'd probably handle more like the Python SDK.)

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.

This comment made me realize the special case of the Go SDK doesn't even matter, so I ended up just removing this entirely. The special case existed because the Go SDK was being invoked as a pretend module, but atm that's completely unnecessary now that I think about it again.

It's possible that we might want to pretend its a module again in the future, but I think doing that should be a bigger thing where it has its own Mod implementation similar to core. That would be nice now but also functionally completely useless so not going to do it atm. Can easily be done in future if needed.

I don't think there should be any more special casing in general. Like you said the stdlib should also be able to have dependencies similar to the python sdk.

Comment thread core/schema/schema.go Resolved
Comment thread core/schema/sdk.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: Hard to tell what these true, nil, nil values are - maybe worth a CallOpts or something?

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.

Agree, fixed

Comment thread core/schema/sdk.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.

Nice seeing higher level types at this level.

Comment thread core/schema/moddag.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.

I tend to prefer the extra bool in cases where it's expected that a value may not be found and you want all callers to handle it, since nil checks being required can quickly turn into nil panics in prod.

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

Great seeing this refactored!

Looking forward to rebasing #6158 on it -- unironically, that's not a complaint about rebasing, it actually wasn't too hard to revive. 😄 It's currently failing on confusing module schema stuff so it'd be really nice if this magically fixes it or at least makes it easier to figure out.

Comment thread core/schema/modtypes.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.

English is annoying here 😭

Suggested change
Description: formatGqlDescription("A reference to %s", objName),
Description: formatGqlDescription("A reference to a(n) %s", objName),
Suggested change
Description: formatGqlDescription("A reference to %s", objName),
Description: formatGqlDescription("%s identifier", objName),

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.

Yeah good call, went with the second suggestion

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

LGTM, aside from the currently failing lints 👀

🧹 🧹 gotta love a good cleanup PR ❤️

sipsma added 16 commits December 4, 2023 11:07
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
@jedevc

jedevc commented Dec 6, 2023

Copy link
Copy Markdown
Contributor

Oop, looks like something unexpected changed here, and now we can't have IDable types set to nil in a struct, e.g. something like:

type Foo struct {
	Bar *Directory
}

// constructor
func New() *Foo {
	return &Foo{}
}

func (f *Foo) DoThing() string {
    return ""
}

I get something like:

$ dagger call do-thing
error: response from query: input:1: foo.doThing failed to convert parent value: failed to convert field "foo": cannot convert <nil> to ID

Not quite sure why the constructor is required here...

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.

3 participants