Skip to content

Commit

Permalink
config: allow configuring free gc policies
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Chadwell <me@jedevc.com>
  • Loading branch information
jedevc committed Sep 13, 2024
1 parent 83bc8df commit 14c9297
Show file tree
Hide file tree
Showing 22 changed files with 647 additions and 296 deletions.
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];
int64 free = 6 [(gogoproto.nullable) = true];
}

message BuildkitVersion {
Expand Down
33 changes: 26 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,44 @@ 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 {
keepBytes := opt.MaxStorage
if excess := opt.Free - dstat.Free; excess > 0 {
keepBytes = totalSize - excess
}
if keepBytes < opt.MinStorage {
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 +1628,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

0 comments on commit 14c9297

Please sign in to comment.