Skip to content

Commit

Permalink
cache: rename new prune/gc control fields
Browse files Browse the repository at this point in the history
Naming that was chosen during review was
reservedSpace, maxUsedSpace and minFreeSpace.

Signed-off-by: Tonis Tiigi <[email protected]>
  • Loading branch information
tonistiigi committed Oct 3, 2024
1 parent 968f34a commit c02f64a
Show file tree
Hide file tree
Showing 15 changed files with 549 additions and 547 deletions.
876 changes: 439 additions & 437 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 = 4;
int64 maxStorage = 5;
int64 free = 6;
int64 reservedSpace = 4;
int64 maxUsedSpace = 5;
int64 minFreeSpace = 6;
}

message DiskUsageRequest {
Expand Down
14 changes: 7 additions & 7 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 {
du, err := cm.DiskUsage(ctx, client.DiskUsageInfo{})
if err != nil {
return err
Expand All @@ -1059,7 +1059,7 @@ func (cm *cacheManager) pruneOnce(ctx context.Context, ch chan client.UsageInfo,
}

var dstat disk.DiskStat
if opt.Free != 0 {
if opt.MinFreeSpace != 0 {
dstat, err = disk.GetDiskStat(cm.root)
if err != nil {
return err
Expand All @@ -1078,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.MinStorage,
MaxUsedSpace: p.MaxStorage,
MinFreeSpace: p.Free,
})
}
return out
Expand Down
12 changes: 6 additions & 6 deletions cmd/buildctl/debug/workers.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,14 +91,14 @@ func printWorkersVerbose(tw *tabwriter.Writer, winfo []*client.WorkerInfo) {
if rule.KeepDuration > 0 {
fmt.Fprintf(tw, "\tKeep duration:\t%v\n", rule.KeepDuration.String())
}
if rule.MinStorage > 0 {
fmt.Fprintf(tw, "\tReserved storage:\t%g\n", units.Bytes(rule.MinStorage))
if rule.ReservedSpace > 0 {
fmt.Fprintf(tw, "\tReserved space:\t%g\n", units.Bytes(rule.ReservedSpace))
}
if rule.Free > 0 {
fmt.Fprintf(tw, "\tMaintain free storage:\t%g\n", units.Bytes(rule.Free))
if rule.MinFreeSpace > 0 {
fmt.Fprintf(tw, "\tMinimum free space:\t%g\n", units.Bytes(rule.MinFreeSpace))
}
if rule.MaxStorage > 0 {
fmt.Fprintf(tw, "\tMaximum storage:\t%g\n", units.Bytes(rule.MaxStorage))
if rule.MaxUsedSpace > 0 {
fmt.Fprintf(tw, "\tMaximum used space:\t%g\n", units.Bytes(rule.MaxUsedSpace))
}
}
fmt.Fprintf(tw, "\n")
Expand Down
22 changes: 11 additions & 11 deletions cmd/buildkitd/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,12 +76,12 @@ type OTELConfig struct {

type GCConfig struct {
GC *bool `toml:"gc"`
// Deprecated: use GCReserved instead
GCKeepStorage DiskSpace `toml:"gckeepstorage"`
GCReserved DiskSpace `toml:"gcreserved"`
GCMax DiskSpace `toml:"gcmax"`
GCFree DiskSpace `toml:"gcfree"`
GCPolicy []GCPolicy `toml:"gcpolicy"`
// Deprecated: use GCReservedSpace instead
GCKeepStorage DiskSpace `toml:"gckeepstorage"`
GCReservedSpace DiskSpace `toml:"reservedSpace"`
GCMaxUsedSpace DiskSpace `toml:"maxUsedSpace"`
GCMinFreeSpace DiskSpace `toml:"minFreeSpace"`
GCPolicy []GCPolicy `toml:"gcpolicy"`
}

type NetworkConfig struct {
Expand Down Expand Up @@ -174,13 +174,13 @@ type GCPolicy struct {
// Any usage below this threshold will not be reclaimed during garbage collection.
ReservedSpace DiskSpace `toml:"reservedSpace"`

// MaxSpace is the maximum amount of disk space this policy is allowed to use.
// MaxUsedSpace is the maximum amount of disk space this policy is allowed to use.
// Any usage exceeding this limit will be cleaned up during a garbage collection sweep.
MaxSpace DiskSpace `toml:"maxSpace"`
MaxUsedSpace DiskSpace `toml:"maxUsedSpace"`

// FreeSpace is the target amount of free disk space the garbage collector will attempt to leave.
// However, it will never let the available space fall below MinSpace.
FreeSpace DiskSpace `toml:"freeSpace"`
// MinFreeSpace is the target amount of free disk space the garbage collector will attempt to leave.
// However, it will never let the available space fall below ReservedSpace.
MinFreeSpace DiskSpace `toml:"minFreeSpace"`
}

type DNSConfig struct {
Expand Down
34 changes: 17 additions & 17 deletions cmd/buildkitd/config/gcpolicy.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ const defaultCap int64 = 2e9 // 2GB

func DefaultGCPolicy(cfg GCConfig, dstat disk.DiskStat) []GCPolicy {
if cfg.IsUnset() {
cfg.GCReserved = cfg.GCKeepStorage
cfg.GCReservedSpace = cfg.GCKeepStorage
}
if cfg.IsUnset() {
cfg = DetectDefaultGCCap(dstat)
Expand All @@ -80,27 +80,27 @@ func DefaultGCPolicy(cfg GCConfig, dstat disk.DiskStat) []GCPolicy {
{
Filters: []string{"type==source.local,type==exec.cachemount,type==source.git.checkout"},
KeepDuration: Duration{Duration: time.Duration(48) * time.Hour}, // 48h
MaxStorage: DiskSpace{Bytes: 512 * 1e6}, // 512MB
MaxUsedSpace: DiskSpace{Bytes: 512 * 1e6}, // 512MB
},
// remove any data not used for 60 days
{
KeepDuration: Duration{Duration: time.Duration(60) * 24 * time.Hour}, // 60d
Free: cfg.GCFree,
MinStorage: cfg.GCReserved,
MaxStorage: cfg.GCLimit,
KeepDuration: Duration{Duration: time.Duration(60) * 24 * time.Hour}, // 60d
MinFreeSpace: cfg.GCMinFreeSpace,
ReservedSpace: cfg.GCReservedSpace,
MaxUsedSpace: cfg.GCMaxUsedSpace,
},
// keep the unshared build cache under cap
{
Free: cfg.GCFreeStorage,
MinStorage: cfg.GCMinStorage,
MaxStorage: cfg.GCMaxStorage,
MinFreeSpace: cfg.GCMinFreeSpace,
ReservedSpace: cfg.GCReservedSpace,
MaxUsedSpace: cfg.GCMaxUsedSpace,
},
// if previous policies were insufficient start deleting internal data to keep build cache under cap
{
All: true,
Free: cfg.GCFreeStorage,
MinStorage: cfg.GCMinStorage,
MaxStorage: cfg.GCMaxStorage,
All: true,
MinFreeSpace: cfg.GCMinFreeSpace,
ReservedSpace: cfg.GCReservedSpace,
MaxUsedSpace: cfg.GCMaxUsedSpace,
},
}
}
Expand All @@ -125,9 +125,9 @@ func DetectDefaultGCCap(dstat disk.DiskStat) GCConfig {
max = DiskSpace{Bytes: DiskSpaceMaxBytes}
}
return GCConfig{
GCMinStorage: reserve,
GCMaxStorage: max,
GCFreeStorage: DiskSpace{Percentage: DiskSpaceFreePercentage},
GCReservedSpace: reserve,
GCMinFreeSpace: DiskSpace{Percentage: DiskSpaceFreePercentage},
GCMaxUsedSpace: max,
}
}

Expand All @@ -149,5 +149,5 @@ func (d DiskSpace) AsBytes(dstat disk.DiskStat) int64 {
}

func (cfg *GCConfig) IsUnset() bool {
return cfg.GCMinStorage == DiskSpace{} && cfg.GCMaxStorage == DiskSpace{} && cfg.GCFreeStorage == DiskSpace{}
return cfg.GCReservedSpace == DiskSpace{} && cfg.GCMaxUsedSpace == DiskSpace{} && cfg.GCMinFreeSpace == DiskSpace{}
}
6 changes: 3 additions & 3 deletions cmd/buildkitd/config/load_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,9 @@ searchDomains=["example.com"]
require.Equal(t, time.Duration(86400), cfg.Workers.Containerd.GCPolicy[2].KeepDuration.Duration/time.Second)

require.Equal(t, false, cfg.Workers.Containerd.GCPolicy[3].All)
require.Equal(t, int64(10*1024*1024*1024), cfg.Workers.Containerd.GCPolicy[3].MinStorage.Bytes)
require.Equal(t, int64(80), cfg.Workers.Containerd.GCPolicy[3].MaxStorage.Percentage)
require.Equal(t, int64(10), cfg.Workers.Containerd.GCPolicy[3].Free.Percentage)
require.Equal(t, int64(10*1024*1024*1024), cfg.Workers.Containerd.GCPolicy[3].ReservedSpace.Bytes)
require.Equal(t, int64(80), cfg.Workers.Containerd.GCPolicy[3].MaxUsedSpace.Percentage)
require.Equal(t, int64(10), cfg.Workers.Containerd.GCPolicy[3].MinFreeSpace.Percentage)

require.Equal(t, true, *cfg.Registries["docker.io"].PlainHTTP)
require.Equal(t, true, *cfg.Registries["docker.io"].Insecure)
Expand Down
16 changes: 8 additions & 8 deletions cmd/buildkitd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -928,16 +928,16 @@ func getGCPolicy(cfg config.GCConfig, root string) []client.PruneInfo {
out := make([]client.PruneInfo, 0, len(cfg.GCPolicy))
for _, rule := range cfg.GCPolicy {
//nolint:staticcheck
if rule.MinStorage == (config.DiskSpace{}) && rule.KeepBytes != (config.DiskSpace{}) {
rule.MinStorage = rule.KeepBytes
if rule.ReservedSpace == (config.DiskSpace{}) && rule.KeepBytes != (config.DiskSpace{}) {
rule.ReservedSpace = rule.KeepBytes
}
out = append(out, client.PruneInfo{
Filter: rule.Filters,
All: rule.All,
KeepDuration: rule.KeepDuration.Duration,
MinStorage: rule.MinStorage.AsBytes(dstat),
MaxStorage: rule.MaxStorage.AsBytes(dstat),
Free: rule.Free.AsBytes(dstat),
Filter: rule.Filters,
All: rule.All,
KeepDuration: rule.KeepDuration.Duration,
ReservedSpace: rule.ReservedSpace.AsBytes(dstat),
MaxUsedSpace: rule.MaxUsedSpace.AsBytes(dstat),
MinFreeSpace: rule.MinFreeSpace.AsBytes(dstat),
})
}
return out
Expand Down
6 changes: 3 additions & 3 deletions cmd/buildkitd/main_containerd_worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -232,9 +232,9 @@ func applyContainerdFlags(c *cli.Context, cfg *config.Config) error {
if err != nil {
return err
}
cfg.Workers.Containerd.GCMinStorage = gc.GCMinStorage
cfg.Workers.Containerd.GCFreeStorage = gc.GCFreeStorage
cfg.Workers.Containerd.GCMaxStorage = gc.GCMaxStorage
cfg.Workers.Containerd.GCReservedSpace = gc.GCReservedSpace
cfg.Workers.Containerd.GCMinFreeSpace = gc.GCMinFreeSpace
cfg.Workers.Containerd.GCMaxUsedSpace = gc.GCMaxUsedSpace
}

if c.GlobalIsSet("containerd-worker-net") {
Expand Down
Loading

0 comments on commit c02f64a

Please sign in to comment.