Skip to content

Commit a73e1b0

Browse files
committed
update default and basic gc control to use free and max storage
Update default policy to include maximum and free storage controls. New default policy is combination of all three controls. Minimum reserved storage: 10GB / 10% (10% was old default) Maintain free storage: 20% Maximum allowed storage: 100GB / 80% Signed-off-by: Tonis Tiigi <[email protected]>
1 parent 293ef59 commit a73e1b0

File tree

14 files changed

+185
-68
lines changed

14 files changed

+185
-68
lines changed

api/services/control/control.pb.go

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/services/control/control.proto

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ message PruneRequest {
2929
bool all = 2;
3030
int64 keepDuration = 3;
3131

32-
int64 minStorage = 5;
33-
int64 maxStorage = 4;
32+
int64 minStorage = 4;
33+
int64 maxStorage = 5;
3434
int64 free = 6;
3535
}
3636

api/types/worker.pb.go

Lines changed: 5 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/types/worker.proto

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ message GCPolicy {
1919
int64 keepDuration = 2;
2020
repeated string filters = 4;
2121

22-
int64 minStorage = 5;
23-
// maxStorage was renamed from freeBytes
24-
int64 maxStorage = 3;
22+
// minStorage was renamed from freeBytes
23+
int64 minStorage = 3;
24+
int64 maxStorage = 5;
2525
int64 free = 6;
2626
}
2727

cache/manager.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,14 +1058,12 @@ func (cm *cacheManager) pruneOnce(ctx context.Context, ch chan client.UsageInfo,
10581058
}
10591059
}
10601060

1061-
dstat, err := disk.GetDiskStat(cm.root)
1062-
if err != nil {
1063-
if opt.Free != 0 {
1064-
// if we are pruning based on disk space, failing to get info on it
1065-
// is fatal
1061+
var dstat disk.DiskStat
1062+
if opt.Free != 0 {
1063+
dstat, err = disk.GetDiskStat(cm.root)
1064+
if err != nil {
10661065
return err
10671066
}
1068-
bklog.L.Warnf("failed to get disk size: %v", err)
10691067
}
10701068

10711069
return cm.prune(ctx, ch, pruneOpt{

cmd/buildctl/debug/workers.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -89,16 +89,16 @@ func printWorkersVerbose(tw *tabwriter.Writer, winfo []*client.WorkerInfo) {
8989
fmt.Fprintf(tw, "\tFilters:\t%s\n", strings.Join(rule.Filter, " "))
9090
}
9191
if rule.KeepDuration > 0 {
92-
fmt.Fprintf(tw, "\tKeep Duration:\t%v\n", rule.KeepDuration.String())
93-
}
94-
if rule.MaxStorage > 0 {
95-
fmt.Fprintf(tw, "\tKeep Bytes:\t%g\n", units.Bytes(rule.MaxStorage))
92+
fmt.Fprintf(tw, "\tKeep duration:\t%v\n", rule.KeepDuration.String())
9693
}
9794
if rule.MinStorage > 0 {
98-
fmt.Fprintf(tw, "\tKeep Bytes (min):\t%g\n", units.Bytes(rule.MinStorage))
95+
fmt.Fprintf(tw, "\tReserved storage:\t%g\n", units.Bytes(rule.MinStorage))
9996
}
10097
if rule.Free > 0 {
101-
fmt.Fprintf(tw, "\tFree Bytes:\t%g\n", units.Bytes(rule.MinStorage))
98+
fmt.Fprintf(tw, "\tMaintain free storage:\t%g\n", units.Bytes(rule.Free))
99+
}
100+
if rule.MaxStorage > 0 {
101+
fmt.Fprintf(tw, "\tMaximum storage:\t%g\n", units.Bytes(rule.MaxStorage))
102102
}
103103
}
104104
fmt.Fprintf(tw, "\n")

cmd/buildkitd/config/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,12 @@ type OTELConfig struct {
7575
}
7676

7777
type GCConfig struct {
78-
GC *bool `toml:"gc"`
78+
GC *bool `toml:"gc"`
79+
// Deprecated: use GCMinStorage instead
7980
GCKeepStorage DiskSpace `toml:"gckeepstorage"`
81+
GCMaxStorage DiskSpace `toml:"gcmaxstorage"`
82+
GCMinStorage DiskSpace `toml:"gcminstorage"`
83+
GCFreeStorage DiskSpace `toml:"gcfreestorage"`
8084
GCPolicy []GCPolicy `toml:"gcpolicy"`
8185
}
8286

cmd/buildkitd/config/gcpolicy.go

Lines changed: 36 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import (
77
"time"
88

99
"github.com/docker/go-units"
10-
"github.com/moby/buildkit/util/bklog"
1110
"github.com/moby/buildkit/util/disk"
1211
"github.com/pkg/errors"
1312
)
@@ -69,9 +68,12 @@ func (d *DiskSpace) UnmarshalText(textb []byte) error {
6968

7069
const defaultCap int64 = 2e9 // 2GB
7170

72-
func DefaultGCPolicy(keep DiskSpace) []GCPolicy {
73-
if keep == (DiskSpace{}) {
74-
keep = DetectDefaultGCCap()
71+
func DefaultGCPolicy(cfg GCConfig, dstat disk.DiskStat) []GCPolicy {
72+
if cfg.IsUnset() {
73+
cfg.GCMinStorage = cfg.GCKeepStorage
74+
}
75+
if cfg.IsUnset() {
76+
cfg = DetectDefaultGCCap(dstat)
7577
}
7678
return []GCPolicy{
7779
// if build cache uses more than 512MB delete the most easily reproducible data after it has not been used for 2 days
@@ -83,16 +85,22 @@ func DefaultGCPolicy(keep DiskSpace) []GCPolicy {
8385
// remove any data not used for 60 days
8486
{
8587
KeepDuration: Duration{Duration: time.Duration(60) * 24 * time.Hour}, // 60d
86-
MaxStorage: keep,
88+
Free: cfg.GCFreeStorage,
89+
MinStorage: cfg.GCMinStorage,
90+
MaxStorage: cfg.GCMaxStorage,
8791
},
8892
// keep the unshared build cache under cap
8993
{
90-
MaxStorage: keep,
94+
Free: cfg.GCFreeStorage,
95+
MinStorage: cfg.GCMinStorage,
96+
MaxStorage: cfg.GCMaxStorage,
9197
},
9298
// if previous policies were insufficient start deleting internal data to keep build cache under cap
9399
{
94100
All: true,
95-
MaxStorage: keep,
101+
Free: cfg.GCFreeStorage,
102+
MinStorage: cfg.GCMinStorage,
103+
MaxStorage: cfg.GCMaxStorage,
96104
},
97105
}
98106
}
@@ -107,24 +115,39 @@ func stripQuotes(s string) string {
107115
return s
108116
}
109117

110-
func DetectDefaultGCCap() DiskSpace {
111-
return DiskSpace{Percentage: DiskSpacePercentage}
118+
func DetectDefaultGCCap(dstat disk.DiskStat) GCConfig {
119+
reserve := DiskSpace{Percentage: DiskSpaceReservePercentage}
120+
if reserve.AsBytes(dstat) > DiskSpaceReserveBytes {
121+
reserve = DiskSpace{Bytes: DiskSpaceReserveBytes}
122+
}
123+
max := DiskSpace{Percentage: DiskSpaceMaxPercentage}
124+
if max.AsBytes(dstat) > DiskSpaceMaxBytes {
125+
max = DiskSpace{Bytes: DiskSpaceMaxBytes}
126+
}
127+
return GCConfig{
128+
GCMinStorage: reserve,
129+
GCMaxStorage: max,
130+
GCFreeStorage: DiskSpace{Percentage: DiskSpaceFreePercentage},
131+
}
112132
}
113133

114-
func (d DiskSpace) AsBytes(root string) int64 {
134+
func (d DiskSpace) AsBytes(dstat disk.DiskStat) int64 {
115135
if d.Bytes != 0 {
116136
return d.Bytes
117137
}
118138
if d.Percentage == 0 {
119139
return 0
120140
}
121141

122-
dstat, err := disk.GetDiskStat(root)
123-
if err != nil {
124-
bklog.L.Warnf("failed to get disk size: %v", err)
142+
if dstat.Total == 0 {
125143
return defaultCap
126144
}
145+
127146
avail := dstat.Total * d.Percentage / 100
128147
rounded := (avail/(1<<30) + 1) * 1e9 // round up
129148
return rounded
130149
}
150+
151+
func (cfg *GCConfig) IsUnset() bool {
152+
return cfg.GCMinStorage == DiskSpace{} && cfg.GCMaxStorage == DiskSpace{} && cfg.GCFreeStorage == DiskSpace{}
153+
}

cmd/buildkitd/config/gcpolicy_unix.go

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,10 @@
33

44
package config
55

6-
var DiskSpacePercentage int64 = 10
6+
const (
7+
DiskSpaceReservePercentage int64 = 10
8+
DiskSpaceReserveBytes int64 = 10 * 1e9 // 10GB
9+
DiskSpaceFreePercentage int64 = 20
10+
DiskSpaceMaxPercentage int64 = 80
11+
DiskSpaceMaxBytes int64 = 100 * 1e9 // 100GB
12+
)

cmd/buildkitd/config/gcpolicy_windows.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33

44
package config
55

6-
// set as double that for Linux since
7-
// Windows images are generally larger.
8-
var DiskSpacePercentage int64 = 20
6+
const (
7+
// Windows images are generally larger.
8+
// set as double that for Linux since
9+
DiskSpaceReservePercentage int64 = 20
10+
DiskSpaceReserveBytes int64 = 10 * 1e9 // 10GB
11+
DiskSpaceFreePercentage int64 = 20
12+
DiskSpaceMaxPercentage int64 = 80
13+
DiskSpaceMaxBytes int64 = 100 * 1e9 // 100GB
14+
)

0 commit comments

Comments
 (0)