Skip to content

Commit 6860c80

Browse files
authored
Merge pull request moby#5359 from tonistiigi/gc-free-max-support
update default and basic gc control to use free and max storage
2 parents f3f34fb + a97b4d3 commit 6860c80

File tree

20 files changed

+739
-610
lines changed

20 files changed

+739
-610
lines changed

api/services/control/control.pb.go

Lines changed: 440 additions & 438 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: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,9 @@ message PruneRequest {
2929
bool all = 2;
3030
int64 keepDuration = 3;
3131

32-
int64 minStorage = 5;
33-
int64 maxStorage = 4;
34-
int64 free = 6;
32+
int64 reservedSpace = 4;
33+
int64 maxUsedSpace = 5;
34+
int64 minFreeSpace = 6;
3535
}
3636

3737
message DiskUsageRequest {

api/types/worker.pb.go

Lines changed: 29 additions & 28 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: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ 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;
25-
int64 free = 6;
22+
// reservedSpace was renamed from freeBytes
23+
int64 reservedSpace = 3;
24+
int64 maxUsedSpace = 5;
25+
int64 minFreeSpace = 6;
2626
}
2727

2828
message BuildkitVersion {

cache/manager.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,7 +1045,7 @@ func (cm *cacheManager) pruneOnce(ctx context.Context, ch chan client.UsageInfo,
10451045
}
10461046

10471047
totalSize := int64(0)
1048-
if opt.MaxStorage != 0 {
1048+
if opt.MaxUsedSpace != 0 || opt.ReservedSpace != 0 || opt.MinFreeSpace != 0 {
10491049
du, err := cm.DiskUsage(ctx, client.DiskUsageInfo{})
10501050
if err != nil {
10511051
return err
@@ -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.MinFreeSpace != 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{
@@ -1080,24 +1078,24 @@ func (cm *cacheManager) pruneOnce(ctx context.Context, ch chan client.UsageInfo,
10801078

10811079
func calculateKeepBytes(totalSize int64, dstat disk.DiskStat, opt client.PruneInfo) int64 {
10821080
// 0 values are special, and means we have no keep cap
1083-
if opt.MaxStorage == 0 && opt.MinStorage == 0 && opt.Free == 0 {
1081+
if opt.MaxUsedSpace == 0 && opt.ReservedSpace == 0 && opt.MinFreeSpace == 0 {
10841082
return 0
10851083
}
10861084

10871085
// try and keep as many bytes as we can
1088-
keepBytes := opt.MaxStorage
1086+
keepBytes := opt.MaxUsedSpace
10891087

10901088
// if we need to free up space, then decrease to that
1091-
if excess := opt.Free - dstat.Free; excess > 0 {
1089+
if excess := opt.MinFreeSpace - dstat.Free; excess > 0 {
10921090
if keepBytes == 0 {
10931091
keepBytes = totalSize - excess
10941092
} else {
10951093
keepBytes = min(keepBytes, totalSize-excess)
10961094
}
10971095
}
10981096

1099-
// but make sure we don't take the total below the minimum
1100-
keepBytes = max(keepBytes, opt.MinStorage)
1097+
// but make sure we don't take the total below the reserved space
1098+
keepBytes = max(keepBytes, opt.ReservedSpace)
11011099

11021100
return keepBytes
11031101
}

cache/manager_test.go

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2480,7 +2480,7 @@ func TestCalculateKeepBytes(t *testing.T) {
24802480
Free: 9000,
24812481
},
24822482
opt: client.PruneInfo{
2483-
MaxStorage: 2000, // 20% of the disk
2483+
MaxUsedSpace: 2000, // 20% of the disk
24842484
},
24852485
result: 2000,
24862486
},
@@ -2492,7 +2492,7 @@ func TestCalculateKeepBytes(t *testing.T) {
24922492
Free: 3000,
24932493
},
24942494
opt: client.PruneInfo{
2495-
Free: 5000, // 50% of the disk
2495+
MinFreeSpace: 5000, // 50% of the disk
24962496
},
24972497
result: 5000,
24982498
},
@@ -2504,8 +2504,8 @@ func TestCalculateKeepBytes(t *testing.T) {
25042504
Free: 3000,
25052505
},
25062506
opt: client.PruneInfo{
2507-
Free: 5000, // 50% of the disk
2508-
MinStorage: 6000, // 60% of the disk,
2507+
MinFreeSpace: 5000, // 50% of the disk
2508+
ReservedSpace: 6000, // 60% of the disk,
25092509
},
25102510
result: 6000,
25112511
},
@@ -2517,9 +2517,9 @@ func TestCalculateKeepBytes(t *testing.T) {
25172517
Free: 3000,
25182518
},
25192519
opt: client.PruneInfo{
2520-
Free: 5000, // 50% of the disk
2521-
MinStorage: 2000, // 20% of the disk
2522-
MaxStorage: 4000, // 40% of the disk
2520+
MinFreeSpace: 5000, // 50% of the disk
2521+
ReservedSpace: 2000, // 20% of the disk
2522+
MaxUsedSpace: 4000, // 40% of the disk
25232523
},
25242524
result: 4000,
25252525
},
@@ -2531,7 +2531,7 @@ func TestCalculateKeepBytes(t *testing.T) {
25312531
Free: 2000, // something else is using 4000
25322532
},
25332533
opt: client.PruneInfo{
2534-
MaxStorage: 2000, // 20% of the disk
2534+
MaxUsedSpace: 2000, // 20% of the disk
25352535
},
25362536
result: 2000,
25372537
},
@@ -2543,7 +2543,7 @@ func TestCalculateKeepBytes(t *testing.T) {
25432543
Free: 2000, // something else is using 4000
25442544
},
25452545
opt: client.PruneInfo{
2546-
Free: 5000, // 50% of the disk
2546+
MinFreeSpace: 5000, // 50% of the disk
25472547
},
25482548
result: 1000,
25492549
},
@@ -2555,8 +2555,8 @@ func TestCalculateKeepBytes(t *testing.T) {
25552555
Free: 2000, // something else is using 4000
25562556
},
25572557
opt: client.PruneInfo{
2558-
Free: 5000, // 50% of the disk
2559-
MinStorage: 2000, // 20% of the disk
2558+
MinFreeSpace: 5000, // 50% of the disk
2559+
ReservedSpace: 2000, // 20% of the disk
25602560
},
25612561
result: 2000,
25622562
},
@@ -2568,9 +2568,9 @@ func TestCalculateKeepBytes(t *testing.T) {
25682568
Free: 2000, // something else is using 4000
25692569
},
25702570
opt: client.PruneInfo{
2571-
Free: 5000, // 50% of the disk
2572-
MinStorage: 2000, // 20% of the disk
2573-
MaxStorage: 4000, // 40% of the disk
2571+
MinFreeSpace: 5000, // 50% of the disk
2572+
ReservedSpace: 2000, // 20% of the disk
2573+
MaxUsedSpace: 4000, // 40% of the disk
25742574
},
25752575
result: 2000,
25762576
},

client/prune.go

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ func (c *Client) Prune(ctx context.Context, ch chan UsageInfo, opts ...PruneOpti
1616
}
1717

1818
req := &controlapi.PruneRequest{
19-
Filter: info.Filter,
20-
KeepDuration: int64(info.KeepDuration),
21-
MinStorage: int64(info.MinStorage),
22-
MaxStorage: int64(info.MaxStorage),
23-
Free: int64(info.Free),
19+
Filter: info.Filter,
20+
KeepDuration: int64(info.KeepDuration),
21+
ReservedSpace: int64(info.ReservedSpace),
22+
MaxUsedSpace: int64(info.MaxUsedSpace),
23+
MinFreeSpace: int64(info.MinFreeSpace),
2424
}
2525
if info.All {
2626
req.All = true
@@ -71,9 +71,9 @@ type PruneInfo struct {
7171
Filter []string `json:"filter"`
7272
KeepDuration time.Duration `json:"keepDuration"`
7373

74-
MinStorage int64 `json:"minStorage"`
75-
MaxStorage int64 `json:"maxStorage"`
76-
Free int64 `json:"free"`
74+
ReservedSpace int64 `json:"reservedSpace"`
75+
MaxUsedSpace int64 `json:"maxUsedSpace"`
76+
MinFreeSpace int64 `json:"minFreeSpace"`
7777
}
7878

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

89-
func WithKeepOpt(duration time.Duration, minStorage int64, maxStorage int64, free int64) PruneOption {
89+
func WithKeepOpt(duration time.Duration, reserved int64, max int64, free int64) PruneOption {
9090
return pruneOptionFunc(func(pi *PruneInfo) {
9191
pi.KeepDuration = duration
92-
pi.MinStorage = minStorage
93-
pi.MaxStorage = maxStorage
94-
pi.Free = free
92+
pi.ReservedSpace = reserved
93+
pi.MaxUsedSpace = max
94+
pi.MinFreeSpace = free
9595
})
9696
}

client/workers.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,12 @@ func fromAPIGCPolicy(in []*apitypes.GCPolicy) []PruneInfo {
6262
out := make([]PruneInfo, 0, len(in))
6363
for _, p := range in {
6464
out = append(out, PruneInfo{
65-
All: p.All,
66-
Filter: p.Filters,
67-
KeepDuration: time.Duration(p.KeepDuration),
68-
MinStorage: p.MinStorage,
69-
MaxStorage: p.MaxStorage,
70-
Free: p.Free,
65+
All: p.All,
66+
Filter: p.Filters,
67+
KeepDuration: time.Duration(p.KeepDuration),
68+
ReservedSpace: p.ReservedSpace,
69+
MaxUsedSpace: p.MaxUsedSpace,
70+
MinFreeSpace: p.MinFreeSpace,
7171
})
7272
}
7373
return out

cmd/buildctl/debug/workers.go

Lines changed: 7 additions & 7 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())
92+
fmt.Fprintf(tw, "\tKeep duration:\t%v\n", rule.KeepDuration.String())
9393
}
94-
if rule.MaxStorage > 0 {
95-
fmt.Fprintf(tw, "\tKeep Bytes:\t%g\n", units.Bytes(rule.MaxStorage))
94+
if rule.ReservedSpace > 0 {
95+
fmt.Fprintf(tw, "\tReserved space:\t%g\n", units.Bytes(rule.ReservedSpace))
9696
}
97-
if rule.MinStorage > 0 {
98-
fmt.Fprintf(tw, "\tKeep Bytes (min):\t%g\n", units.Bytes(rule.MinStorage))
97+
if rule.MinFreeSpace > 0 {
98+
fmt.Fprintf(tw, "\tMinimum free space:\t%g\n", units.Bytes(rule.MinFreeSpace))
9999
}
100-
if rule.Free > 0 {
101-
fmt.Fprintf(tw, "\tFree Bytes:\t%g\n", units.Bytes(rule.MinStorage))
100+
if rule.MaxUsedSpace > 0 {
101+
fmt.Fprintf(tw, "\tMaximum used space:\t%g\n", units.Bytes(rule.MaxUsedSpace))
102102
}
103103
}
104104
fmt.Fprintf(tw, "\n")

0 commit comments

Comments
 (0)