-
Notifications
You must be signed in to change notification settings - Fork 249
[narInfoCache] Use sync.Map and simplify IsInBinaryCache #1473
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 10 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
0bc1dd6
[narInfoCache] Simplify IsInBinaryCache
mikeland73 f2542f9
Fix
mikeland73 252ffb6
Fix2
mikeland73 f9b3cca
Use sync.Map
mikeland73 7cada0b
Simplify more
mikeland73 7e527a4
Remove comment
mikeland73 4fc377e
Reduce race condition
mikeland73 e042a78
Fix race
mikeland73 c747352
Use once
mikeland73 fc483da
Bump go version
mikeland73 67d0f5b
comments
mikeland73 ad45762
simplify code
mikeland73 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
module go.jetpack.io/devbox | ||
|
||
go 1.20 | ||
go 1.21 | ||
|
||
require ( | ||
github.com/AlecAivazis/survey/v2 v2.3.6 | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,53 +21,16 @@ import ( | |
// It is used as FromStore in builtins.fetchClosure. | ||
const BinaryCache = "https://cache.nixos.org" | ||
|
||
// isNarInfoInCache checks if the .narinfo for this package is in the `BinaryCache`. | ||
// This cannot be a field on the Package struct, because that struct | ||
// is constructed multiple times in a request (TODO: we could fix that). | ||
var isNarInfoInCache = struct { | ||
// The key is the `Package.Raw` string. | ||
status map[string]bool | ||
lock sync.RWMutex | ||
// re-use httpClient to re-use the connection | ||
httpClient http.Client | ||
}{ | ||
status: map[string]bool{}, | ||
httpClient: http.Client{}, | ||
} | ||
|
||
// IsInBinaryCache returns true if the package is in the binary cache. | ||
// ALERT: Callers in a perf-sensitive code path should call FillNarInfoCache | ||
// before calling this function. | ||
func (p *Package) IsInBinaryCache() (bool, error) { | ||
|
||
if eligible, err := p.isEligibleForBinaryCache(); err != nil { | ||
return false, err | ||
} else if !eligible { | ||
return false, nil | ||
} | ||
|
||
// Check if the narinfo is present in the binary cache | ||
isNarInfoInCache.lock.RLock() | ||
status, statusExists := isNarInfoInCache.status[p.Raw] | ||
isNarInfoInCache.lock.RUnlock() | ||
if !statusExists { | ||
// Fallback to synchronously filling the nar info cache | ||
if err := p.fillNarInfoCache(); err != nil { | ||
return false, err | ||
} | ||
|
||
// Check again | ||
isNarInfoInCache.lock.RLock() | ||
status, statusExists = isNarInfoInCache.status[p.Raw] | ||
isNarInfoInCache.lock.RUnlock() | ||
if !statusExists { | ||
return false, errors.Errorf( | ||
"narInfo cache miss: %v. Should be filled by now", | ||
p.Raw, | ||
) | ||
} | ||
} | ||
return status, nil | ||
return p.fetchNarInfoStatusOnce() | ||
} | ||
|
||
// FillNarInfoCache checks the remote binary cache for the narinfo of each | ||
|
@@ -80,9 +43,8 @@ func FillNarInfoCache(ctx context.Context, packages ...*Package) error { | |
|
||
eligiblePackages := []*Package{} | ||
for _, p := range packages { | ||
// NOTE: isEligibleForBinaryCache also ensures the package is | ||
// resolved in the lockfile, which must be done before the concurrent | ||
// section in this function below. | ||
// IMPORTANT: isEligibleForBinaryCache will call resolve() which is NOT | ||
// concurrency safe. Hence, we call it outside of the go-routine. | ||
isEligible, err := p.isEligibleForBinaryCache() | ||
// If the package is not eligible or there is an error in determining that, then skip it. | ||
if isEligible && err == nil { | ||
|
@@ -103,39 +65,44 @@ func FillNarInfoCache(ctx context.Context, packages ...*Package) error { | |
|
||
group, _ := errgroup.WithContext(ctx) | ||
for _, p := range eligiblePackages { | ||
// If the package's NarInfo status is already known, skip it | ||
isNarInfoInCache.lock.RLock() | ||
_, ok := isNarInfoInCache.status[p.Raw] | ||
isNarInfoInCache.lock.RUnlock() | ||
if ok { | ||
continue | ||
} | ||
pkg := p // copy the loop variable since its used in a closure below | ||
group.Go(func() error { | ||
err := pkg.fillNarInfoCache() | ||
if err != nil { | ||
// default to false if there was an error, so we don't re-try | ||
isNarInfoInCache.lock.Lock() | ||
isNarInfoInCache.status[pkg.Raw] = false | ||
isNarInfoInCache.lock.Unlock() | ||
} | ||
_, err := pkg.fetchNarInfoStatusOnce() | ||
return err | ||
}) | ||
} | ||
return group.Wait() | ||
} | ||
|
||
// fillNarInfoCache fills the cache value for the narinfo of this package, | ||
// assuming it is eligible for the binary cache. Callers are responsible | ||
// for checking isEligibleForBinaryCache before calling this function. | ||
// | ||
// NOTE: this must be concurrency safe. | ||
func (p *Package) fillNarInfoCache() error { | ||
// narInfoStatusFnCache contains cached OnceValues functions that return cache | ||
// status for a package. In the future we can remove this cache by caching | ||
// package objects and ensuring packages are shared globally. | ||
var narInfoStatusFnCache = sync.Map{} | ||
|
||
// fetchNarInfoStatusOnce is like fetchNarInfoStatus, but will only ever run | ||
// once and cache the result. | ||
func (p *Package) fetchNarInfoStatusOnce() (bool, error) { | ||
type inCacheFunc func() (bool, error) | ||
f, ok := narInfoStatusFnCache.Load(p.Raw) | ||
if !ok { | ||
f = inCacheFunc(sync.OnceValues(func() (bool, error) { | ||
return p.fetchNarInfoStatus() | ||
})) | ||
f, _ = narInfoStatusFnCache.LoadOrStore(p.Raw, f) | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could replace this with a single LoadOrStore but that would create throwaway OnceValues function every time. |
||
return f.(inCacheFunc)() | ||
} | ||
|
||
// fetchNarInfoStatus fetches the cache status for the package. It returns | ||
// true if cache exists, false otherwise. | ||
// NOTE: This function always performs an HTTP request and should not be called | ||
// more than once. | ||
func (p *Package) fetchNarInfoStatus() (bool, error) { | ||
sysInfo, err := p.sysInfoIfExists() | ||
if err != nil { | ||
return err | ||
return false, err | ||
} else if sysInfo == nil { | ||
return errors.New( | ||
return false, errors.New( | ||
"sysInfo is nil, but should not be because" + | ||
" the package is eligible for binary cache", | ||
) | ||
|
@@ -147,20 +114,18 @@ func (p *Package) fillNarInfoCache() error { | |
defer cancel() | ||
req, err := http.NewRequestWithContext(ctx, http.MethodHead, reqURL, nil) | ||
if err != nil { | ||
return err | ||
return false, err | ||
} | ||
res, err := isNarInfoInCache.httpClient.Do(req) | ||
res, err := http.DefaultClient.Do(req) | ||
if err != nil { | ||
return err | ||
return false, err | ||
} | ||
// read the body fully, and close it to ensure the connection is reused. | ||
_, _ = io.Copy(io.Discard, res.Body) | ||
defer res.Body.Close() | ||
|
||
isNarInfoInCache.lock.Lock() | ||
isNarInfoInCache.status[p.Raw] = res.StatusCode == 200 | ||
isNarInfoInCache.lock.Unlock() | ||
return nil | ||
// Use LoadOrStore to avoid ever changing an existing value. | ||
return res.StatusCode == 200, nil | ||
} | ||
|
||
// isEligibleForBinaryCache returns true if we have additional metadata about | ||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.