Skip to content

Commit

Permalink
Merge pull request moby#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

(cherry picked from commit 6860c80)
  • Loading branch information
tonistiigi authored and jedevc committed Oct 10, 2024
1 parent 303f846 commit f69c079
Show file tree
Hide file tree
Showing 20 changed files with 511 additions and 384 deletions.
360 changes: 180 additions & 180 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 @@ -32,9 +32,9 @@ message PruneRequest {
bool all = 2;
int64 keepDuration = 3 [(gogoproto.nullable) = true];

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

message DiskUsageRequest {
Expand Down
121 changes: 61 additions & 60 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 @@ -22,10 +22,10 @@ message GCPolicy {
int64 keepDuration = 2;
repeated string filters = 4;

int64 minStorage = 5 [(gogoproto.nullable) = true];
// maxStorage was renamed from freeBytes
int64 maxStorage = 3 [(gogoproto.nullable) = true];
int64 free = 6 [(gogoproto.nullable) = true];
// reservedSpace was renamed from freeBytes
int64 reservedSpace = 3 [(gogoproto.nullable) = true];
int64 maxUsedSpace = 5 [(gogoproto.nullable) = true];
int64 minFreeSpace = 6 [(gogoproto.nullable) = true];
}

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
Loading

0 comments on commit f69c079

Please sign in to comment.