Repository navigation
replace impurity with mix of custom cache keys + field spec settings - #9719
Conversation
f144850 to
0073302
Compare
vito
left a comment
There was a problem hiding this comment.
LGTM, only minor suggestions
| }, | ||
| nil, | ||
| dagql.CacheSpec{ | ||
| DoNotCache: "The given ID ultimately determines the purity of its result.", |
There was a problem hiding this comment.
Maybe something like this, to avoid using the old terminology:
| DoNotCache: "The given ID ultimately determines the purity of its result.", | |
| DoNotCache: "There's no point caching the loading call of an ID, vs. letting the ID's calls cache on their own.", |
| _ A, | ||
| cacheCfg CacheConfig, | ||
| ) (*CacheConfig, error) { | ||
| randID := identity.NewID() |
There was a problem hiding this comment.
good time to use rand.Text() now that we're on Go 1.24?
There was a problem hiding this comment.
Best to hold that for now, think we need to downgrade due to #9759 😢
| // Impure for now, could use a finer grain cache key if we had the ability to mix | ||
| // a digest of the dagql server schema into the cache key. | ||
| Impure("Can change when modules are loaded into the schema."). | ||
| dagql.FuncWithCacheKey("currentTypeDefs", s.currentTypeDefs, dagql.CachePerCall). |
There was a problem hiding this comment.
neat - though, not sure about the name CachePerCall 🤔 - initially it sounded like the opposite of what it does (the call itself isn't cached since you get a different result each time, but each result is cached)
that said, don't know what else to suggest.
There was a problem hiding this comment.
Yeah it's a very subtle thing that's hard to summarize in a short name... Nothing better in terms of names is coming to mind for me either, but if I think of anything before merge I'll update it.
| }, | ||
| nil, | ||
| CacheSpec{ | ||
| DoNotCache: "The given ID ultimately determines the purity of its result.", |
There was a problem hiding this comment.
(copying here too)
| DoNotCache: "The given ID ultimately determines the purity of its result.", | |
| DoNotCache: "There's no point caching the loading call of an ID, vs. letting the ID's calls cache on their own.", |
| // customDigest, if not empty string, will become the ID's digest. | ||
| // tainted sets the ID's tainted flag. | ||
| func (id *ID) WithMetadata(customDigest digest.Digest, tainted bool) *ID { | ||
| func (id *ID) WithMetadata(customDigest digest.Digest) *ID { |
There was a problem hiding this comment.
suggestion: we can just rename this to WithDigest now (and remove tainted from the doc string)
| // Generally, customDigest should be used when there's a content-based digest available | ||
| // that won't be caputured by the default, call-chain derived digest. | ||
| func (r Instance[T]) WithMetadata(customDigest digest.Digest, isPure bool) Instance[T] { | ||
| func (r Instance[T]) WithMetadata(customDigest digest.Digest) Instance[T] { |
There was a problem hiding this comment.
suggestion: WithDigest maybe as well?
| // CacheConfig is the configuration for caching a field. Currently just custom digest | ||
| // but intended to support more in time (TTL, etc). |
| "directives": [ | ||
| { | ||
| "name": "impure", | ||
| "args": [ | ||
| { | ||
| "name": "reason", | ||
| "value": "Because I said so." | ||
| } | ||
| ] | ||
| } | ||
| ] |
There was a problem hiding this comment.
I never saw these used in the first place (so seems good to remove them), but also, mildly curious as to why we had an impure directive at all 🤔
There was a problem hiding this comment.
No concrete reason, it was just interesting to surface at the schema layer because it could affect how things use those fields. For example, the CLI used to fetch object fields and print them; it could avoid fetching @impure fields as a safety precaution. But now that feature's gone, and it never used it to begin with.
| // (via the magic __ prefix). | ||
| dagql.NodeFunc("__schemaJSONFile", s.schemaJSONFile). | ||
| Impure("Return value depends on what the caller has installed in the server."). | ||
| dagql.NodeFuncWithCacheKey("__schemaJSONFile", s.schemaJSONFile, dagql.CachePerCall). |
There was a problem hiding this comment.
As an idea for how we could cache these in the future ... everytime we modify client state, we could increase a client counter, and then mixin that counter + the client id to the target.
Maybe you already have ideas for how to do this though!
There was a problem hiding this comment.
Yeah that's not a bad idea. I was figuring when a schema is installed/served we could calculate a digest based on which modules are installed and use that as a content-based digest for this, which technically is slightly better in that we could re-use the same cache for this file when two clients end up with the exact same schema, but practically speaking either approach works and is mostly the same.
| if field.CacheKeyFunc != nil { | ||
| customDgst, err := field.CacheKeyFunc(ctx, r, inputArgs, newID.Digest()) | ||
| doNotCache := field.CacheSpec.DoNotCache != "" | ||
| if field.CacheSpec.GetCacheConfig != nil { |
There was a problem hiding this comment.
Question - why is DoNotCache is called as a chain function, instead of using a function similar to CachePerClient. Since it's set in CacheConfig anyways.
It feels a bit confusing that there's a couple of ways to control caching now, I'm not sure if this was just to make porting easier, or if there's a fundamental reason why we can't?
There was a problem hiding this comment.
Yeah the main issue right now is that we purposely don't re-call custom cache key funcs when loading an ID that was given to us (as opposed to making a new call). The idea there is that if we're given an ID, it's from a call already made and should just re-use the digest given to us. This is currently important for CachePerCall to work.
So that also means at the moment that DoNotCache has to be set on the field's definition rather than set by a custom cache key func.
I am not a huge fan of this behavior for the long term, but it works at the moment. We could change it by adding some ability to set whether a digest should be re-calculated on ID load too, but I couldn't find a clean way to do that so just wanted to go with this for now.
I'm figuring the best time to sort this out will be when moving to the sqlite db. That whole effort is gonna entail all sorts of other new features around mapping between (multiple) digests and results anyways.
This is a mix of smallish cleanups/preparation for more changes: Impure is gone now and replaced with: * `CachePerCall` - a custom cache key impl that lets the API always run when called but be cached after the result of that call is passed around (i.e. for snapshots of mutating state) * `DoNotCache` - a setting that exists purely on the Field now rather than being tied into IDs. We still want to straight-up skip caching values for various reasons (correctness and memory usage), but there's no longer any need for that to be part of the ID. This replaces it. Related to the above, we no longer cache the results of selecting "trivial fields" (i.e. selecting a field already set on a parent object, as opposed to actually executing a an API impl for the field). We skip it via `DoNotCache`. * The reason for this is that it has O(n^2) memory usage for nested struct cases. Selecting a series of nested fields like `Obj.field1.field2.field3` results in progressively caching each object in the chain even though you already have everything cached you need in the "outer" one. * This started mattering a lot more in my trials at cross-session caching, so want to get it fixed now in prep for that. A few other related things are here too, i.e. removing code referenced to `Meta` as a field/id configuration, which hasn't been used for a while. Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
0073302 to
37b9ad5
Compare
This is a mix of smallish cleanups/preparation for more changes:
Impure is gone now and replaced with:
CachePerCall- a custom cache key impl that lets the API always run when called but be cached after the result of that call is passed around (i.e. for snapshots of mutating state)DoNotCache- a setting that exists purely on the Field now rather than being tied into IDs. We still want to straight-up skip caching values for various reasons (correctness and memory usage), but there's no longer any need for that to be part of the ID. This replaces it.Related to the above, we no longer cache the results of selecting "trivial fields" (i.e. selecting a field already set on a parent object, as opposed to actually executing a an API impl for the field). We skip it via
DoNotCache.Obj.field1.field2.field3results in progressively caching each object in the chain even though you already have everything cached you need in the "outer" one.A few other related things are here too, i.e. removing code referenced to
Metaas a field/id configuration, which hasn't been used for a while.