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

Add a new freeBytes gc policy #5079

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
385 changes: 228 additions & 157 deletions api/services/control/control.pb.go

Large diffs are not rendered by default.

5 changes: 4 additions & 1 deletion api/services/control/control.proto
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ message PruneRequest {
repeated string filter = 1;
bool all = 2;
int64 keepDuration = 3 [(gogoproto.nullable) = true];
int64 keepBytes = 4 [(gogoproto.nullable) = true];

int64 minStorage = 5 [(gogoproto.nullable) = true];
int64 maxStorage = 4 [(gogoproto.nullable) = true];
int64 free = 6 [(gogoproto.nullable) = true];
}

message DiskUsageRequest {
Expand Down
150 changes: 111 additions & 39 deletions api/types/worker.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion api/types/worker.proto
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,11 @@ message WorkerRecord {
message GCPolicy {
bool all = 1;
int64 keepDuration = 2;
int64 keepBytes = 3;
repeated string filters = 4;

int64 minStorage = 5 [(gogoproto.nullable) = true];
int64 maxStorage = 3 [(gogoproto.nullable) = true];
Copy link
Member

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.

int64 free = 6 [(gogoproto.nullable) = true];
}

message BuildkitVersion {
Expand Down
46 changes: 39 additions & 7 deletions cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Copy link
Member

Choose a reason for hiding this comment

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

At least in Windows it looks like dstat can be empty. In that case we should not use the values from it.

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{}{}

Expand Down Expand Up @@ -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 {
Expand Down
Loading
Loading