Skip to content

cache result wrapping and context cancel management in dagql cache - #9774

Merged
sipsma merged 2 commits into
dagger:mainfrom
sipsma:singleflight-cache
Mar 5, 2025
Merged

sipsma merged 2 commits into
dagger:mainfrom
sipsma:singleflight-cache

Conversation

@sipsma

@sipsma sipsma commented Mar 5, 2025

Copy link
Copy Markdown
Contributor

Splitting out another seperable chunk from #9682

This updates dagql's cache to use the same "singleflight" implementation added for the local sync rework.

The idea of deduping equivalent calls is the same as the previous cachemap implementation, but with two important additional features:

  • Context cancellation is handled better.
    • Previously when calls were deduped, the "first" context was always used, which meant that if it was canceled (i.e. first client disconnected for any reason), all other clients waiting on the same call would get canceled too.
    • Now, the call is only canceled if all caller contexts are canceled.
  • Results returned from the cache are wrapped in a type that are ref-counted and releasable. When all refs for the result are released, they are removed from the cache.
    • This is not used in dagql quite yet, but will be in follow-up PRs. It still makes sense to include in this PR since it comes along for the ride with re-using the singleflight implementation from the local sync code.

The main goals here are to:

  • Test the context cancelation improvements in isolation; they are a subtle change so worth verifying by themselves
  • Setup the follow-ups that actually use the ref counted results to support engine-wide dagql cache

@sipsma
sipsma requested review from jedevc and vito March 5, 2025 05:50
@sipsma sipsma mentioned this pull request Mar 5, 2025
5 tasks done
Comment thread engine/cache/cache.go
)

type Cache[K comparable, V any] interface {
GetOrInitializeValue(context.Context, K, V) (Result[K, V], error)

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: doc comments would be very helpful 🙏

@jedevc

jedevc commented Mar 5, 2025 •

Copy link
Copy Markdown
Contributor

all other clients waiting on the same call would get canceled too

Huh. I wonder if this will potentially relieve some of the weird everything-gets-context-cancelled at the same time issues we've seen.

I wonder, would it be worth adding a test for this behavior now we've fixed it? Make two long-running calls, cancel one of them, and check the other one runs successfully.

@sipsma

sipsma commented Mar 5, 2025

Copy link
Copy Markdown
Contributor Author

Huh. I wonder if this will potentially relieve some of the weird everything-gets-context-cancelled at the same time issues we've seen.

Yeah wondering that too. Or even if errors don't go away, I have been wondering if something about the behavior before this PR is resulting in the "real" error getting swallowed and just context canceled from another call showing up. We'll see 🤞

I wonder, would it be worth adding a test for this behavior now we've fixed it? Make two long-running calls, cancel one of them, and check the other one runs successfully.

Yeah totally, I did add unit tests already but integ test should be trivial too and is valuable I agree

@sipsma
sipsma force-pushed the singleflight-cache branch from e857495 to 6854e99 Compare March 5, 2025 20:14
This updates dagql's cache to use the same "singleflight" implementation
added for the local sync rework.

The idea of deduping equivalent calls is the same as the previous
cachemap implementation, but with two important additional features:
* Context cancellation is handled better.
  * Previously when calls were deduped, the "first" context was always
    used, which meant that if it was canceled (i.e. first client
    disconnected for any reason), all other clients waiting on the
    same call would get canceled too.
  * Now, the call is only canceled if *all* caller contexts are
    canceled.
* Results returned from the cache are wrapped in a type that are
  ref-counted and releasable. When all refs for the result are released,
  they are removed from the cache.
  * This is not used in dagql quite yet, but will be in follow-up PRs.
    It still makes sense to include in this PR since it comes along for
    the ride with re-using the singleflight implementation from the
    local sync code.

The main goals here are to:
* Test the context cancelation improvements in isolation; they are a
  subtle change so worth verifying by themselves
* Setup the follow-ups that actually use the ref counted results to
  support engine-wide dagql cache

Signed-off-by: Erik Sipsma <[email protected]>
@sipsma
sipsma force-pushed the singleflight-cache branch from 6854e99 to b07e483 Compare March 5, 2025 20:28
@sipsma
sipsma force-pushed the singleflight-cache branch from b07e483 to 432111c Compare March 5, 2025 20:29
@sipsma
sipsma merged commit c1c8da7 into dagger:main Mar 5, 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.

2 participants