-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add a new freeBytes
gc policy
#5079
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ import ( | |
"github.com/moby/buildkit/session" | ||
"github.com/moby/buildkit/snapshot" | ||
"github.com/moby/buildkit/util/bklog" | ||
"github.com/moby/buildkit/util/disk" | ||
"github.com/moby/buildkit/util/flightcontrol" | ||
"github.com/moby/buildkit/util/progress" | ||
digest "github.com/opencontainers/go-digest" | ||
|
@@ -1040,7 +1041,7 @@ func (cm *cacheManager) pruneOnce(ctx context.Context, ch chan client.UsageInfo, | |
} | ||
|
||
totalSize := int64(0) | ||
if opt.KeepBytes != 0 { | ||
if opt.MaxStorage != 0 { | ||
du, err := cm.DiskUsage(ctx, client.DiskUsageInfo{}) | ||
if err != nil { | ||
return err | ||
|
@@ -1053,27 +1054,57 @@ func (cm *cacheManager) pruneOnce(ctx context.Context, ch chan client.UsageInfo, | |
} | ||
} | ||
|
||
// TODO: pick a better path here | ||
dstat, err := disk.GetDiskStat(cm.mountPool.tmpdirRoot) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return cm.prune(ctx, ch, pruneOpt{ | ||
filter: filter, | ||
all: opt.All, | ||
checkShared: check, | ||
keepDuration: opt.KeepDuration, | ||
keepBytes: opt.KeepBytes, | ||
keepBytes: calculateKeepBytes(totalSize, dstat, opt), | ||
totalSize: totalSize, | ||
}) | ||
} | ||
|
||
func (cm *cacheManager) prune(ctx context.Context, ch chan client.UsageInfo, opt pruneOpt) (err error) { | ||
var toDelete []*deleteRecord | ||
func calculateKeepBytes(totalSize int64, dstat disk.DiskStat, opt client.PruneInfo) int64 { | ||
// 0 values are special, and means we have no keep cap | ||
if opt.MaxStorage == 0 && opt.MinStorage == 0 && opt.Free == 0 { | ||
return 0 | ||
} | ||
|
||
// try and keep as many bytes as we can | ||
keepBytes := opt.MaxStorage | ||
|
||
// if we need to free up space, then decrease to that | ||
if excess := opt.Free - dstat.Free; excess > 0 { | ||
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. At least in Windows it looks like |
||
if keepBytes == 0 { | ||
keepBytes = totalSize - excess | ||
} else { | ||
keepBytes = min(keepBytes, totalSize-excess) | ||
} | ||
} | ||
|
||
// but make sure we don't take the total below the minimum | ||
keepBytes = max(keepBytes, opt.MinStorage) | ||
|
||
return keepBytes | ||
} | ||
|
||
func (cm *cacheManager) prune(ctx context.Context, ch chan client.UsageInfo, opt pruneOpt) (err error) { | ||
if opt.keepBytes != 0 && opt.totalSize < opt.keepBytes { | ||
return nil | ||
} | ||
|
||
var toDelete []*deleteRecord | ||
|
||
cm.mu.Lock() | ||
|
||
gcMode := opt.keepBytes != 0 | ||
cutOff := time.Now().Add(-opt.keepDuration) | ||
gcMode := opt.keepBytes != 0 | ||
|
||
locked := map[*sync.Mutex]struct{}{} | ||
|
||
|
@@ -1610,8 +1641,9 @@ type pruneOpt struct { | |
all bool | ||
checkShared ExternalRefChecker | ||
keepDuration time.Duration | ||
keepBytes int64 | ||
totalSize int64 | ||
|
||
keepBytes int64 | ||
totalSize int64 | ||
} | ||
|
||
type deleteRecord struct { | ||
|
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.
Add a comment for this. Or just move it back to the correct place for the index order.