Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

allow duration based filters on diskusage requests #5455

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
858 changes: 434 additions & 424 deletions api/services/control/control.pb.go

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions api/services/control/control.proto
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ message PruneRequest {

message DiskUsageRequest {
repeated string filter = 1;
int64 ageLimit = 2;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Struggled to find a good name for this. In Prune it is called KeepDuration but that name does not make sense at all for DiskUsage.

In buildx this is --filter until=<duration> that is detected on client side and converted to another field. buildx du will likely keep the same name there.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ageLimit SGTM - but if we change this, would it also make sense to change the name in the prune request? We're already making changes to these structs in the next release because of the new storage fields.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if prune had ageLimit is it obvious which way it works? Eg. is it pruning the things that are newer than ageLimit or older? Correct is older but I don't know if it is obvious.

}

message DiskUsageResponse {
Expand Down
31 changes: 31 additions & 0 deletions api/services/control/control_vtproto.pb.go

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

11 changes: 9 additions & 2 deletions cache/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -1437,6 +1437,7 @@ func (cm *cacheManager) DiskUsage(ctx context.Context, opt client.DiskUsageInfo)
if err := cm.markShared(m); err != nil {
return nil, err
}
cutOff := time.Now().Add(-opt.AgeLimit)

var du []*client.UsageInfo
for id, cr := range m {
Expand All @@ -1453,9 +1454,15 @@ func (cm *cacheManager) DiskUsage(ctx context.Context, opt client.DiskUsageInfo)
RecordType: cr.recordType,
Shared: cr.shared,
}
if filter.Match(adaptUsageInfo(c)) {
du = append(du, c)
if !filter.Match(adaptUsageInfo(c)) {
continue
}
if opt.AgeLimit > 0 {
if c.LastUsedAt != nil && c.LastUsedAt.After(cutOff) {
continue
}
}
du = append(du, c)
}

eg, ctx := errgroup.WithContext(ctx)
Expand Down
17 changes: 15 additions & 2 deletions client/diskusage.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func (c *Client) DiskUsage(ctx context.Context, opts ...DiskUsageOption) ([]*Usa
o.SetDiskUsageOption(info)
}

req := &controlapi.DiskUsageRequest{Filter: info.Filter}
req := &controlapi.DiskUsageRequest{Filter: info.Filter, AgeLimit: int64(info.AgeLimit)}
resp, err := c.ControlClient().DiskUsage(ctx, req)
if err != nil {
return nil, errors.Wrap(err, "failed to call diskusage")
Expand Down Expand Up @@ -75,7 +75,8 @@ type DiskUsageOption interface {
}

type DiskUsageInfo struct {
Filter []string
Filter []string
AgeLimit time.Duration
}

type UsageRecordType string
Expand All @@ -88,3 +89,15 @@ const (
UsageRecordTypeCacheMount UsageRecordType = "exec.cachemount"
UsageRecordTypeRegular UsageRecordType = "regular"
)

type diskUsageOptionFunc func(*DiskUsageInfo)

func (f diskUsageOptionFunc) SetDiskUsageOption(info *DiskUsageInfo) {
f(info)
}

func WithAgeLimit(age time.Duration) DiskUsageOption {
return diskUsageOptionFunc(func(info *DiskUsageInfo) {
info.AgeLimit = age
})
}
3 changes: 2 additions & 1 deletion control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,8 @@ func (c *Controller) DiskUsage(ctx context.Context, r *controlapi.DiskUsageReque
}
for _, w := range workers {
du, err := w.DiskUsage(ctx, client.DiskUsageInfo{
Filter: r.Filter,
Filter: r.Filter,
AgeLimit: time.Duration(r.AgeLimit),
})
if err != nil {
return nil, err
Expand Down
Loading