perf(segcache): load catalog in background to avoid blocking startup#757
Merged
Merged
Conversation
loadCatalog was called synchronously in NewSegmentCache, statting every cached segment on disk before the constructor returned. On a large cache this blocked the entire serve startup path for seconds to minutes. Move the load into Manager.Start as a third wg-tracked goroutine (mirroring the existing cleanupLoop/catalogFlushLoop split). NewSegmentCache now constructs only; LoadCatalog is exported so tests can call it synchronously. The merge loop now skips keys already present (a Put during load already wrote a newer entry and .seg file) instead of clobbering c.items, fixing the race that the old overwrite would have introduced.
The old message "Segment cache initialized" implied the catalog was warm. Now that the load runs in a background goroutine, reword to "Segment cache started (catalog loads in background)".
Owner
|
@evulhotdog nice one, but don't you think that blocking the put while loading can cause cpu leak if there is a lot of put waiting to succed? maybe we should ignore the put while loading |
Contributor
Author
|
@javi11 yeah, making it a no-op is probably easier to handle and reduces complexity. I was just trying to think about continuity and ensuring that we caching wherever we can. In general, altmount is a long running process, so this should be an edge case where you're streaming right away. I was just tired of waiting 5 minutes to load my cache each time, since it stat's each file! I simplified things, let me know what you think. |
javi11
reviewed
Jul 3, 2026
Contributor
Author
|
@javi11 ready for ya again. |
javi11
reviewed
Jul 6, 2026
javi11
reviewed
Jul 6, 2026
Per review: the loading flag's lifecycle (set on entry, cleared on exit) now lives entirely in LoadCatalog. NewSegmentCache no longer pokes at load-state, so a cache can no longer be left permanently gating Puts if a future caller forgets to call LoadCatalog.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem: Startup hangs while
loadCatalog()stats every cached segment on disk synchronously (oneos.Statper ~750KB segment, can be tens of thousands of calls).Fix: Moved catalog loading from the
NewSegmentCacheconstructor intoManager.Startas a background goroutine. Startup no longer blocks; the cache serves misses from Usenet until the catalog hydrates. Merge-under-lock prevents aPutduring load from being clobbered.