Skip to content
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

store: refactor and improve store cache #14524

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 18 additions & 26 deletions store/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -165,54 +165,46 @@ func (cm *CacheManager) cleanup() error {
return err
}

// we need the modtime so convert to FileInfo
fil := make([]os.FileInfo, 0, len(entries))
if len(entries) <= cm.maxItems {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Smart!

return nil
}

// most of the entries will have more than one hardlink, but a minority may
// be referenced only the cache and thus be a candidate for pruning
pruneCandidates := make([]os.FileInfo, 0, len(entries)/5)

for _, entry := range entries {
fi, err := entry.Info()
if err != nil {
return err
}

fil = append(fil, fi)
}

if len(fil) <= cm.maxItems {
return nil
}

numOwned := 0
for _, fi := range fil {
n, err := hardLinkCount(fi)
if err != nil {
logger.Noticef("cannot inspect cache: %s", err)
}
// Only count the file if it is not referenced elsewhere in the filesystem
// If the file is referenced in the filesystem somewhere else our copy
// is "free" so skip it.
if n <= 1 {
numOwned++
pruneCandidates = append(pruneCandidates, fi)
}
}

if numOwned <= cm.maxItems {
if len(pruneCandidates) <= cm.maxItems {
// nothing to prune
return nil
}

var lastErr error
sort.Sort(changesByMtime(fil))
sort.Sort(changesByMtime(pruneCandidates))
numOwned := len(pruneCandidates)
deleted := 0
for _, fi := range fil {
for _, fi := range pruneCandidates {
path := cm.path(fi.Name())
n, err := hardLinkCount(fi)
if err != nil {
logger.Noticef("cannot inspect cache: %s", err)
}
// If the file is referenced in the filesystem somewhere
// else our copy is "free" so skip it. If there is any
// error we cleanup the file (it is just a cache afterall).
if n > 1 {
continue
}
if err := osRemove(path); err != nil {
if !os.IsNotExist(err) {
// If there is any error we cleanup the file (it is just a cache
// afterall).
logger.Noticef("cannot cleanup cache: %s", err)
lastErr = err
}
Expand Down
Loading