Repository navigation
Refactor core module implementation as a DAG - #6167
Conversation
|
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 🙏 |
There was a problem hiding this comment.
@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.
There was a problem hiding this comment.
Ha, I ran into a deadlock in the same place in #6158 so 👍
There was a problem hiding this comment.
@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?
There was a problem hiding this comment.
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
decodeMyIDwhich 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 :-)
There was a problem hiding this comment.
Yup that's fine, the comment looks clear enough, thanks! 🎉
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
🎉 wooo! Thanks, nice to see it cleared up 💜
There was a problem hiding this comment.
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
decodeMyIDwhich 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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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).
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Split up into separate files some more:
moddag.go- just theModinterface andModDepsstructmodtypes.go- just theModTypeinterface and the trivial implementations (PrimitiveandList)coremod.go- the special implementation ofModandModTypefor core apisusermod.go- theModimplementation for a user module andModTypeimplementation for user mod objectsusermodfunction.go- the implementation ofUserModFunction, which is chonky enough to deserve its own separate file I think
There was a problem hiding this comment.
What's the bool for in the return here? If it's just an ok, could we just do ModType == nil?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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.
vito
left a comment
There was a problem hiding this comment.
[will continue reviewing moddag.go and modtypes.go, just don't like having pending comments for too long]
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.)
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
nit: Hard to tell what these true, nil, nil values are - maybe worth a CallOpts or something?
There was a problem hiding this comment.
Nice seeing higher level types at this level.
There was a problem hiding this comment.
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
left a comment
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
English is annoying here 😭
| Description: formatGqlDescription("A reference to %s", objName), | |
| Description: formatGqlDescription("A reference to a(n) %s", objName), |
| Description: formatGqlDescription("A reference to %s", objName), | |
| Description: formatGqlDescription("%s identifier", objName), |
There was a problem hiding this comment.
Yeah good call, went with the second suggestion
jedevc
left a comment
There was a problem hiding this comment.
LGTM, aside from the currently failing lints 👀
🧹 🧹 gotta love a good cleanup PR ❤️
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
|
Oop, looks like something unexpected changed here, and now we can't have IDable types set to type Foo struct {
Bar *Directory
}
// constructor
func New() *Foo {
return &Foo{}
}
func (f *Foo) DoThing() string {
return ""
}I get something like: Not quite sure why the constructor is required here... |
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 casesThis 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
moduleSchemaobject, keeping track of passing the rightschemaView, 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:
core/schema/package maintains its own wrapper types around most of the core types related to modules.schema/moddag.goandschema/modtypes.go; those are good places to start reviewingSome more misc benefits + changes
coreis modeled as a module to a limited extent now; it has a special implementation of the newModinterface and is after that treated the same as any other module dependency (besides being an implicit forced dependency of every user module).ifstatements that were scattered all over to handle the core types separately.