Skip to content

engine-wide dagql caching - #9682

Merged
sipsma merged 44 commits into
dagger:mainfrom
sipsma:cachedresults
Apr 8, 2025
Merged

sipsma merged 44 commits into
dagger:mainfrom
sipsma:cachedresults

Conversation

@sipsma

@sipsma sipsma commented Feb 22, 2025 •

Copy link
Copy Markdown
Contributor

This is the first major upgrade to dagql caching, adding a shared engine-wide cache. This is a pre-req for everything else we want to do (persistent dagql caching, default function caching, remote caching, etc.). It forces a bunch of problems to be addressed w/out being too overwhelming in scope.

The biggest problems that needed addressing were around secrets and private git repos, which need to work in the case of "cross-session cache hits" while not leaking e.g. private git repos from an authorized client to a client that does not have auth for them.

Overall, this is a mix of some good long-term fixes and some one-off mole-whacking. Quite a bit of the remaining ugliness will be addressed in the next part of this effort that adds support for persistence, cache result dependency tracking and support for multiple cache keys per-result (which are all intertwined).


Cache Changes

  1. There is now an engine-wide dagql cache shared by all sessions
    • It's still in memory, as before. Persistence is coming next
  2. Each session has a dagql.SessionCache, which consumes the base engine-wide cache
    • SessionCache keeps track of every cache result returned in the session and releases all of them when the session is removed (i.e. main client disconnects). Cache results are ref-counted, so this means that they stay in the engine-wide cache until no sessions have open refs for them.
    • SessionCache also helps w/ telemetry a bit (mentioned more below)
  3. Function calls are included in this, so we technically enable a very limited form of global function caching now (if two clients make the exact same function call w/ exact same args+parent at the same time, they can hit cache from each other)
    • Function calls have a custom cache key which ensures that they are cached based both on the call (i.e. the default dagql cache key) and the content-hash of the ModuleSource.
  4. A few other tweaks to the cache keys of IDs for various types, mentioned below

Telemetry Changes

  1. dagql-level cache hits now actually show up as CACHED in telemetry; previously only buildkit cache hits were displayed
    • To maintain previous behavior, a given SessionCache (mentioned above) will only send telemetry for a given call digest once, rather than sending CACHED over and over every time it appears in any DAG in a session

Secret Changes

  1. loadSecretFromName is rm'd
    • This was only meant for internal usage but isn't needed anymore after updates to secret and setSecret, so good to clean up
    • The most direct motivation to including this here is simply to remove a little bit of complication from these APIs while working through various cross-session secret issues
  2. setSecret hides plaintext in the returned ID by changing the returned ID to show it as ***, rather than relying on loadSecretFromName. The real plaintext is stored in the Secret object and loadable by ID thanks to the returned ID having a custom digest.
  3. setSecretFile just uses the file:// secret URL now rather than setSecret
  4. ResourceTransferPostCall works a bit differently now
    • It now figures out all the secrets that need to be transferred at the time the post-call itself is created (rather than when the post call is invoked)
    • Among other things this allows it to store plaintext for setSecret-created secrets and thus ensure those stay available in-memory for as long as the returned object is needed. This avoids problems w/ the secret disappearing when clients disconnect and cache refs get released.
    • This is not exactly beautiful still. The better approach would be to update dagql and our core types w/ more explicit dependency tracking such that holding a dagql cache ref open implicitly holds all dependent refs in the DAG open too. This is coming in the next set of dagql cache updates.

Git API

  1. Fixes for pre-existing issues uncovered during this PR
    1. It was possible to use git repos w/out having correct auth because buildkit cache didn't check auth for each client in many cases. You could not provide auth but hit cache from a previous different client and successfully use the repo.
      • Turns out a number of our existing tests accidentally relied on this and should not have worked, those are fixed here
      • This is fixed primarily by updating our gitdns source to always check auth in the buildkit CacheKey method
    2. The git API did not automatically set username to git@ for SSH urls that lacked a username (unlike git module source refs, which do have this behavior). This wasn't caught before because of the point above.
    3. We error out in git now if an SSH URL is provided but no SSH auth sock was provided or found from the client. This previously at best errored out much later w/ an incomprehensible buildkit error (socket "default" not found) and at worst never errored due to getting a cache hit from a different client.
    4. Git experimentalServiceBindings weren't called before trying to resolve a commit, which is needed in some cases
  2. Implicit SSH auth socks from client metadata now show up in the DAG explicitly (via the API re-invoking itself in this case). This helps with various socket/secret transfer shenanigans in the case of cross-session cache hits.
  3. The cache key of git is now CachePerClient
    • This was needed in order to ensure we always check for auth since a lot of the auth is implicitly found in the implementation of git rather than provided as args. If we didn't cache per client you could easily just specify a private repo URL you have no auth for and get a cache hit
    • There are thus highly-specific scenarios in which clients will actually get less cache hits than before on this specific API, but that's justifiable given what it's fixing
  4. To partially make up for the above, the tree API now sets a content-hashed ID on the Directory it returns
    • For public git repos, this is cheap and based mainly off the commit (plus URL if the .git dir is retained, since that has git remote metadata)
    • For private git repos, this requires an actual content-hash of the Directory in order to prevent making the cache key predictable based on non-sensitive metadata only (commit, url, etc.)
      • Directory content-hashing can be a bit expensive, but it's fairly optimized for the codepaths we hit and has internal caching which saves work if/when it is re-run.
    • This also replaces the full dir/file content hashing git module sources used to run on every git directory (whether public or private), so probably a net improvement for git module source performance

Pre-existing problems uncovered but not fixed here

  • Contextual local dir arguments don't count for the cache key of the function call
    • Highly obscure, but two source-identical modules could have different files/dirs in their full context, which can result in function calls between them incorrectly sharing a cache key

Status

After battling lots of puzzles around secrets and then numerous flakes caused by subtle issues, this is in a good state and has not flaked out in CI for 5+ runs.

TODOs:

  • Update telemetry golden tests (lots of things CACHED that didn't used to be)
    • Ended up needing to do this by disabling the parallelism of TestGolden, otherwise it seemed to be extremely nondeterministic whether we'd get cache hits (due to the fact that it depends on what other sessions are running at the same time).
  • Make a final call on whether to keep the removal of LoadSecretFromName or to leave it in for back-compat
    • It was originally only added for internal use cases but is no longer needed after changes here. The tradeoff is theoretically someone could have started using it somehow.
  • Current LLM tests pass, but since those are still being worked, it's worth a manual check that LLM stuff isn't impacted by all this is surprising ways
  • Appease linter
  • Fix weird error in .NET SDK related to marking setSecretFile as deprecated

@sipsma

sipsma commented Feb 26, 2025

Copy link
Copy Markdown
Contributor Author

Split out one seperable chunk here: #9719

@sipsma
sipsma force-pushed the cachedresults branch 2 times, most recently from e24dde5 to de126e2 Compare February 26, 2025 22:47
@sipsma

sipsma commented Feb 27, 2025

Copy link
Copy Markdown
Contributor Author

Note to self: be sure to update secret transferring to not cross session-boundaries (or equivalent fix)

@sipsma

sipsma commented Mar 5, 2025 •

Copy link
Copy Markdown
Contributor Author

Another chunk split out #9774 while I work through some more tests here

@sipsma sipsma mentioned this pull request Mar 7, 2025
@sipsma
sipsma force-pushed the cachedresults branch 9 times, most recently from e877072 to 34cb34e Compare March 19, 2025 19:04
@sipsma
sipsma marked this pull request as ready for review March 20, 2025 03:45
@sipsma
sipsma requested review from a team as code owners March 20, 2025 03:45
@sipsma
sipsma requested a review from jedevc March 20, 2025 03:45
sipsma added 21 commits April 8, 2025 10:09
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]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

benchmark triggers pre-merge benchmarks

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants