Skip to content

Filesync performance improvements - #8818

Merged
sipsma merged 34 commits into
dagger:mainfrom
sipsma:too-much-ctx
Dec 4, 2024
Merged

sipsma merged 34 commits into
dagger:mainfrom
sipsma:too-much-ctx

Conversation

@sipsma

@sipsma sipsma commented Oct 30, 2024 •

Copy link
Copy Markdown
Contributor

Goals

Primary:

  1. Improve caching behavior of local filesyncs; re-use cache of already synced files in more cases
  2. Use the above to simplify module context/source loading + reduce unneeded cache invalidation of module builds
    • Splitting this out to a separate follow-up PR since this one is big enough on its own, but it's all setup here
  3. Take control of the filesync implementation to enable future support for more features in the shell, live filesync, etc.

Secondary:

  1. Improve performance of filesync in all cases (memory+CPU usage in the engine)
  2. Explicitly identify and error out when concurrent changes on the client filesystem would result in inconsistent loads (previously was just undefined behavior essentially)

Performance Improvements

I used the git repo for the Linux kernel as a stress-test case since it contains multiple GB of source files.

The main improvements are around filesyncs of already loaded data and parallel syncs, but there's some meaningful improvement to uncached syncs too for huge filesync cases.

The improvements in cache re-use and parallel syncs are expected to be most applicable to module loading and contextual directory argument usage.

  • Uncached load of full repo except .git (.host | directory . --exclude .git | entries)

    • v0.14.0 - 44.6s, ~1.3GB RSS (trace)
    • This PR - 29.7s, ~650MB RSS (trace)
    • ~33% faster and 50% less memory
  • Subsequent load of drivers/ subdir (.host | directory drivers | entries)

    • v0.14.0 - 20.2s (trace)
    • This PR - 4.9s (trace)
    • ~75% faster
    • The drivers/ subdir was already loaded in the previous sync so there shouldn't be any loading of new data. The old implementation didn't handle this, but the new one does. You can see in the traces that the majority of the time in the new implementation is spent copying files/dirs (in order to make an immutable cache entry); this is also possible to improve in the future too.
  • Cached load of full repo except .git using include instead of exclude (.host | directory . --include **,!.git | entries)

    • v0.14.0 - 22.5s (trace)
    • This PR - 1.9s (trace)
    • ~91% faster
    • This case is especially silly; we are re-running the same load as before but using include settings instead of exclude settings, so there should be nothing to sync or copy. Previous implementation didn't realize this though.
  • Re-run full repo load from separate parallel session (.host | directory . --exclude .git | entries)

    • v0.14.0 - 21.8s (trace)
    • This PR - 1.8s (trace)
    • ~91% faster
    • The previous implementation re-ran a lot of equivalent work when it happened from different clients/sessions. Concurrency across clients/sessions is handled now, which allows more work to be re-used.

Implementation Details

Biggest problems with previous implementation:

  1. Buildkit would create a new cache ref for every "name" of a provided local source
    • The name we set was derived from the path being loaded, so each path on the client's filesystem would get its own ref
    • We also couldn't use a single name and sync in different dirs under it because buildkit's cache logic requires local cache refs be locked while in use; if one is locked it just creates a brand new one (which was hard to predict and control and thus would result in lots of mysterious uncached syncs). That would have gotten much worse if we tried to maintain a single cache ref for each sync from a client.
    • Now, there's a single cache ref for each client (as identified by their stable client ID) for their entire rootfs. Each sync of an individual dir just fills the relevant part of the cache ref. Concurrency is handled allowing different syncs to safely operate on the same underlying cache ref.
    • Additionally, if the client changed files in the middle of sync previously, it was undefined what would happen. Now the implementation explicitly notices these situations and returns an error.
  2. Making local syncs immutable required exporting files into the content store, which had a lot of overhead
    • We have always needed to include an extra step after a sync to copy data and make it immutable. This became our blob API and went through the content store for simplicity. Going through the content store involves a ton of overhead and extra copying though. It also created more situations in which the local cache sync ref was locked and would thus result in more mysterious uncached syncs.
    • Now, the copy step that creates an immutable ref is part of the filesync implementation. It uses the contenthash lib from upstream to calculate and store content hash metadata rather than going through the content store. It also re-uses existing equivalent refs whenever possible.

There's a ton of other little optimizations in various places (noted most of them with comments in the code).

Future improvements and features

  • Handle nested clients better?
    • Nested clients all get a brand new random stable client ID, so they don't benefit from cache reuse a ton. This is because the COW semantics of withExec layers complicate cache re-use quite a bit (you can end up with different filesystems that have the same stable client ID)
    • Handling this would mostly be an improvement to our integ tests (which is likely the heaviest user of nested clients by far), so may investigate this angle when looking into improving test performance
  • The overhead of the copy step could be lessened by maintaining a "hardlink farm" of files stored by their content hash and just linking in the files rather than copying them on disk.
    • In the example above, copying the 1.1GB drivers/ subdir added ~5s of overhead, to give an idea of how much of a difference this might be able to make (not enough to be worth it here, but possibly worth it someday)
  • We can enable options to force reloads of data rather than caching given ops permanently within the context of the session. This is needed for dagger shell use cases
  • We're in more control of the filesync low-levels now, which may help "live" filesync use cases being discussed.

@sipsma
sipsma force-pushed the too-much-ctx branch 4 times, most recently from 6a9bf60 to 89b2377 Compare November 14, 2024 19:37
@sipsma

sipsma commented Nov 15, 2024

Copy link
Copy Markdown
Contributor Author

Need an upstream fix for some (potentially all?) of the remaining test failures: moby/buildkit#5521

@sipsma sipsma mentioned this pull request Nov 18, 2024
@sipsma
sipsma force-pushed the too-much-ctx branch 5 times, most recently from fabf369 to d2666a8 Compare November 22, 2024 03:28
@sipsma
sipsma force-pushed the too-much-ctx branch 6 times, most recently from 179b75a to 5f2ec52 Compare November 27, 2024 00:29
@sipsma sipsma added the needs/changelog Pull requests that require a changelog entry label Nov 27, 2024
@sipsma sipsma changed the title [WIP] filesync and module context dir improvements Filesync performance improvements Nov 27, 2024
@sipsma
sipsma marked this pull request as ready for review November 27, 2024 04:35
@sipsma
sipsma requested review from a team as code owners November 27, 2024 04:35
sipsma added 20 commits December 4, 2024 12:53
This code was necessary in early iterations of modules that had function
caching enabled universally.

However, currently we only enable function caching for some internal SDK
function calls. Those are limited enough that I believe the blob linking
is not necessary. They have inputs that are sourced from filesyncs, but
because those are inputs you won't end up in a situation where you get a
cached result from an SDK that refers to filesync cache refs that
weren't loaded.

We will need something along these lines again when we enable general
function call caching, but it will have to look a little different since
filesync blobs no longer go through the content store (which is good
since it avoids performance overhead).

Signed-off-by: Erik Sipsma <[email protected]>
The hashing of stats via creating tar headers was a surprisingly large
source of allocations+memory and also unnecessary. The new implemention
elminates ~1m allocations over the course of syncing the full linux src
code repo (test case I'm using) and the engine's memory usage is
noticeable lower+more stable watching htop.

Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
Signed-off-by: Erik Sipsma <[email protected]>
The verification that concurrent syncs of conflicting data before this
was too coarse and led to errors when unrelated changes were made to
completely different parts of the local filesystem.

Now the verification is fine grain and should only error when conflicts
arise for specific paths.

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]>
@sipsma

sipsma commented Dec 4, 2024

Copy link
Copy Markdown
Contributor Author

Pushed the fix for case-insensitive filesystems cfae16d

Basically just wrote a version of os.Getwd that special cases darwin and normalizes the returned path to use the "official" casing of paths (i.e. the value you get from doing readdir syscalls) so then we don't get inconsistencies.

@helderco I was able to repro that error on my macbook and confirmed it's fixed, so gonna merge now but appreciate a double-check on your end that it's all gone for you to whenever you have a min.

@sipsma
sipsma merged commit f2ad189 into dagger:main Dec 4, 2024
@helderco

helderco commented Dec 5, 2024

Copy link
Copy Markdown
Contributor

@helderco I was able to repro that error on my macbook and confirmed it's fixed, so gonna merge now but appreciate a double-check on your end that it's all gone for you to whenever you have a min.

Yeah, I can confirm it works for me now 🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

needs/changelog Pull requests that require a changelog entry

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants