Skip to content

replace impurity with mix of custom cache keys + field spec settings - #9719

Merged
sipsma merged 2 commits into
dagger:mainfrom
sipsma:remodel-impurity-rebase
Mar 4, 2025
Merged

sipsma merged 2 commits into
dagger:mainfrom
sipsma:remodel-impurity-rebase

Conversation

@sipsma

@sipsma sipsma commented Feb 26, 2025

Copy link
Copy Markdown
Contributor

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.

@sipsma
sipsma requested review from jedevc and vito February 26, 2025 07:41
@sipsma sipsma mentioned this pull request Feb 26, 2025
5 tasks done
@sipsma
sipsma force-pushed the remodel-impurity-rebase branch 6 times, most recently from f144850 to 0073302 Compare March 1, 2025 00:22

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

LGTM, only minor suggestions

Comment thread core/interface.go Outdated
},
nil,
dagql.CacheSpec{
DoNotCache: "The given ID ultimately determines the purity of its result.",

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.

Maybe something like this, to avoid using the old terminology:

Suggested change
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.",

Comment thread dagql/cachekey.go
_ A,
cacheCfg CacheConfig,
) (*CacheConfig, error) {
randID := identity.NewID()

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.

good time to use rand.Text() now that we're on Go 1.24?

@jedevc jedevc Mar 4, 2025 •

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.

Best to hold that for now, think we need to downgrade due to #9759 😢

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

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

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

Comment thread dagql/server.go Outdated
},
nil,
CacheSpec{
DoNotCache: "The given ID ultimately determines the purity of its result.",

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.

(copying here too)

Suggested change
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.",

Comment thread dagql/call/id.go Outdated
// 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 {

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.

suggestion: we can just rename this to WithDigest now (and remove tainted from the doc string)

Comment thread dagql/objects.go Outdated
// 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] {

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.

suggestion: WithDigest maybe as well?

Comment thread dagql/objects.go
Comment on lines +966 to +967
// CacheConfig is the configuration for caching a field. Currently just custom digest
// but intended to support more in time (TTL, etc).

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.

👀 oh this is coooooool 🎉

Comment on lines -639 to -649
"directives": [
{
"name": "impure",
"args": [
{
"name": "reason",
"value": "Because I said so."
}
]
}
]

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 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 🤔

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.

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.

Comment thread core/schema/query.go
// (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).

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.

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!

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

Comment thread dagql/objects.go
if field.CacheKeyFunc != nil {
customDgst, err := field.CacheKeyFunc(ctx, r, inputArgs, newID.Digest())
doNotCache := field.CacheSpec.DoNotCache != ""
if field.CacheSpec.GetCacheConfig != 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.

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?

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

sipsma added 2 commits March 4, 2025 10:00
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]>
@sipsma
sipsma force-pushed the remodel-impurity-rebase branch from 0073302 to 37b9ad5 Compare March 4, 2025 18:00
@sipsma
sipsma merged commit 1ab60f0 into dagger:main Mar 4, 2025
@sipsma sipsma mentioned this pull request Mar 6, 2025
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