Skip to content

Commit

Permalink
Merge pull request #5359 from tonistiigi/gc-free-max-support
Browse files Browse the repository at this point in the history
update default and basic gc control to use free and max storage
  • Loading branch information
tonistiigi authored Oct 8, 2024
2 parents f3f34fb + a97b4d3 commit 6860c80
Show file tree
Hide file tree
Showing 20 changed files with 739 additions and 610 deletions.
878 changes: 440 additions & 438 deletions api/services/control/control.pb.go

Large diffs are not rendered by default.

6 changes: 3 additions & 3 deletions api/services/control/control.proto
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ message PruneRequest {
bool all = 2;
int64 keepDuration = 3;

int64 minStorage = 5;
int64 maxStorage = 4;
int64 free = 6;
int64 reservedSpace = 4;
int64 maxUsedSpace = 5;
int64 minFreeSpace = 6;
}

message DiskUsageRequest {
Expand Down
57 changes: 29 additions & 28 deletions api/types/worker.pb.go

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

8 changes: 4 additions & 4 deletions api/types/worker.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ message GCPolicy {
int64 keepDuration = 2;
repeated string filters = 4;

int64 minStorage = 5;
// maxStorage was renamed from freeBytes
int64 maxStorage = 3;
int64 free = 6;
// reservedSpace was renamed from freeBytes
int64 reservedSpace = 3;
int64 maxUsedSpace = 5;
int64 minFreeSpace = 6;
}

message BuildkitVersion {
Expand Down
22 changes: 10 additions & 12 deletions cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1045,7 +1045,7 @@ func (cm *cacheManager) pruneOnce(ctx context.Context, ch chan client.UsageInfo,
}

totalSize := int64(0)
if opt.MaxStorage != 0 {
if opt.MaxUsedSpace != 0 || opt.ReservedSpace != 0 || opt.MinFreeSpace != 0 {
du, err := cm.DiskUsage(ctx, client.DiskUsageInfo{})
if err != nil {
return err
Expand All @@ -1058,14 +1058,12 @@ func (cm *cacheManager) pruneOnce(ctx context.Context, ch chan client.UsageInfo,
}
}

dstat, err := disk.GetDiskStat(cm.root)
if err != nil {
if opt.Free != 0 {
// if we are pruning based on disk space, failing to get info on it
// is fatal
var dstat disk.DiskStat
if opt.MinFreeSpace != 0 {
dstat, err = disk.GetDiskStat(cm.root)
if err != nil {
return err
}
bklog.L.Warnf("failed to get disk size: %v", err)
}

return cm.prune(ctx, ch, pruneOpt{
Expand All @@ -1080,24 +1078,24 @@ func (cm *cacheManager) pruneOnce(ctx context.Context, ch chan client.UsageInfo,

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 {
if opt.MaxUsedSpace == 0 && opt.ReservedSpace == 0 && opt.MinFreeSpace == 0 {
return 0
}

// try and keep as many bytes as we can
keepBytes := opt.MaxStorage
keepBytes := opt.MaxUsedSpace

// if we need to free up space, then decrease to that
if excess := opt.Free - dstat.Free; excess > 0 {
if excess := opt.MinFreeSpace - dstat.Free; excess > 0 {
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)
// but make sure we don't take the total below the reserved space
keepBytes = max(keepBytes, opt.ReservedSpace)

return keepBytes
}
Expand Down
28 changes: 14 additions & 14 deletions cache/manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2480,7 +2480,7 @@ func TestCalculateKeepBytes(t *testing.T) {
Free: 9000,
},
opt: client.PruneInfo{
MaxStorage: 2000, // 20% of the disk
MaxUsedSpace: 2000, // 20% of the disk
},
result: 2000,
},
Expand All @@ -2492,7 +2492,7 @@ func TestCalculateKeepBytes(t *testing.T) {
Free: 3000,
},
opt: client.PruneInfo{
Free: 5000, // 50% of the disk
MinFreeSpace: 5000, // 50% of the disk
},
result: 5000,
},
Expand All @@ -2504,8 +2504,8 @@ func TestCalculateKeepBytes(t *testing.T) {
Free: 3000,
},
opt: client.PruneInfo{
Free: 5000, // 50% of the disk
MinStorage: 6000, // 60% of the disk,
MinFreeSpace: 5000, // 50% of the disk
ReservedSpace: 6000, // 60% of the disk,
},
result: 6000,
},
Expand All @@ -2517,9 +2517,9 @@ func TestCalculateKeepBytes(t *testing.T) {
Free: 3000,
},
opt: client.PruneInfo{
Free: 5000, // 50% of the disk
MinStorage: 2000, // 20% of the disk
MaxStorage: 4000, // 40% of the disk
MinFreeSpace: 5000, // 50% of the disk
ReservedSpace: 2000, // 20% of the disk
MaxUsedSpace: 4000, // 40% of the disk
},
result: 4000,
},
Expand All @@ -2531,7 +2531,7 @@ func TestCalculateKeepBytes(t *testing.T) {
Free: 2000, // something else is using 4000
},
opt: client.PruneInfo{
MaxStorage: 2000, // 20% of the disk
MaxUsedSpace: 2000, // 20% of the disk
},
result: 2000,
},
Expand All @@ -2543,7 +2543,7 @@ func TestCalculateKeepBytes(t *testing.T) {
Free: 2000, // something else is using 4000
},
opt: client.PruneInfo{
Free: 5000, // 50% of the disk
MinFreeSpace: 5000, // 50% of the disk
},
result: 1000,
},
Expand All @@ -2555,8 +2555,8 @@ func TestCalculateKeepBytes(t *testing.T) {
Free: 2000, // something else is using 4000
},
opt: client.PruneInfo{
Free: 5000, // 50% of the disk
MinStorage: 2000, // 20% of the disk
MinFreeSpace: 5000, // 50% of the disk
ReservedSpace: 2000, // 20% of the disk
},
result: 2000,
},
Expand All @@ -2568,9 +2568,9 @@ func TestCalculateKeepBytes(t *testing.T) {
Free: 2000, // something else is using 4000
},
opt: client.PruneInfo{
Free: 5000, // 50% of the disk
MinStorage: 2000, // 20% of the disk
MaxStorage: 4000, // 40% of the disk
MinFreeSpace: 5000, // 50% of the disk
ReservedSpace: 2000, // 20% of the disk
MaxUsedSpace: 4000, // 40% of the disk
},
result: 2000,
},
Expand Down
24 changes: 12 additions & 12 deletions client/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ func (c *Client) Prune(ctx context.Context, ch chan UsageInfo, opts ...PruneOpti
}

req := &controlapi.PruneRequest{
Filter: info.Filter,
KeepDuration: int64(info.KeepDuration),
MinStorage: int64(info.MinStorage),
MaxStorage: int64(info.MaxStorage),
Free: int64(info.Free),
Filter: info.Filter,
KeepDuration: int64(info.KeepDuration),
ReservedSpace: int64(info.ReservedSpace),
MaxUsedSpace: int64(info.MaxUsedSpace),
MinFreeSpace: int64(info.MinFreeSpace),
}
if info.All {
req.All = true
Expand Down Expand Up @@ -71,9 +71,9 @@ type PruneInfo struct {
Filter []string `json:"filter"`
KeepDuration time.Duration `json:"keepDuration"`

MinStorage int64 `json:"minStorage"`
MaxStorage int64 `json:"maxStorage"`
Free int64 `json:"free"`
ReservedSpace int64 `json:"reservedSpace"`
MaxUsedSpace int64 `json:"maxUsedSpace"`
MinFreeSpace int64 `json:"minFreeSpace"`
}

type pruneOptionFunc func(*PruneInfo)
Expand All @@ -86,11 +86,11 @@ var PruneAll = pruneOptionFunc(func(pi *PruneInfo) {
pi.All = true
})

func WithKeepOpt(duration time.Duration, minStorage int64, maxStorage int64, free int64) PruneOption {
func WithKeepOpt(duration time.Duration, reserved int64, max int64, free int64) PruneOption {
return pruneOptionFunc(func(pi *PruneInfo) {
pi.KeepDuration = duration
pi.MinStorage = minStorage
pi.MaxStorage = maxStorage
pi.Free = free
pi.ReservedSpace = reserved
pi.MaxUsedSpace = max
pi.MinFreeSpace = free
})
}
12 changes: 6 additions & 6 deletions client/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@ func fromAPIGCPolicy(in []*apitypes.GCPolicy) []PruneInfo {
out := make([]PruneInfo, 0, len(in))
for _, p := range in {
out = append(out, PruneInfo{
All: p.All,
Filter: p.Filters,
KeepDuration: time.Duration(p.KeepDuration),
MinStorage: p.MinStorage,
MaxStorage: p.MaxStorage,
Free: p.Free,
All: p.All,
Filter: p.Filters,
KeepDuration: time.Duration(p.KeepDuration),
ReservedSpace: p.ReservedSpace,
MaxUsedSpace: p.MaxUsedSpace,
MinFreeSpace: p.MinFreeSpace,
})
}
return out
Expand Down
14 changes: 7 additions & 7 deletions cmd/buildctl/debug/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,16 +89,16 @@ func printWorkersVerbose(tw *tabwriter.Writer, winfo []*client.WorkerInfo) {
fmt.Fprintf(tw, "\tFilters:\t%s\n", strings.Join(rule.Filter, " "))
}
if rule.KeepDuration > 0 {
fmt.Fprintf(tw, "\tKeep Duration:\t%v\n", rule.KeepDuration.String())
fmt.Fprintf(tw, "\tKeep duration:\t%v\n", rule.KeepDuration.String())
}
if rule.MaxStorage > 0 {
fmt.Fprintf(tw, "\tKeep Bytes:\t%g\n", units.Bytes(rule.MaxStorage))
if rule.ReservedSpace > 0 {
fmt.Fprintf(tw, "\tReserved space:\t%g\n", units.Bytes(rule.ReservedSpace))
}
if rule.MinStorage > 0 {
fmt.Fprintf(tw, "\tKeep Bytes (min):\t%g\n", units.Bytes(rule.MinStorage))
if rule.MinFreeSpace > 0 {
fmt.Fprintf(tw, "\tMinimum free space:\t%g\n", units.Bytes(rule.MinFreeSpace))
}
if rule.Free > 0 {
fmt.Fprintf(tw, "\tFree Bytes:\t%g\n", units.Bytes(rule.MinStorage))
if rule.MaxUsedSpace > 0 {
fmt.Fprintf(tw, "\tMaximum used space:\t%g\n", units.Bytes(rule.MaxUsedSpace))
}
}
fmt.Fprintf(tw, "\n")
Expand Down
Loading

0 comments on commit 6860c80

Please sign in to comment.