Skip to content

Commit a3712e2

Browse files
tonistiigicrazy-max
authored andcommitted
allow duration based filters on diskusage requests
Allows similar time-based filter that is allowed for prune requests so that DiskUsage request can be used to check which records would be candidates for pruning. Signed-off-by: Tonis Tiigi <[email protected]> (cherry picked from commit 2307fb7)
1 parent 93b71cd commit a3712e2

File tree

6 files changed

+500
-437
lines changed

6 files changed

+500
-437
lines changed

api/services/control/control.pb.go

Lines changed: 442 additions & 432 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ message PruneRequest {
3636

3737
message DiskUsageRequest {
3838
repeated string filter = 1;
39+
int64 ageLimit = 2;
3940
}
4041

4142
message DiskUsageResponse {

api/services/control/control_vtproto.pb.go

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

cache/manager.go

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1452,6 +1452,7 @@ func (cm *cacheManager) DiskUsage(ctx context.Context, opt client.DiskUsageInfo)
14521452
if err := cm.markShared(m); err != nil {
14531453
return nil, err
14541454
}
1455+
cutOff := time.Now().Add(-opt.AgeLimit)
14551456

14561457
var du []*client.UsageInfo
14571458
for id, cr := range m {
@@ -1468,9 +1469,15 @@ func (cm *cacheManager) DiskUsage(ctx context.Context, opt client.DiskUsageInfo)
14681469
RecordType: cr.recordType,
14691470
Shared: cr.shared,
14701471
}
1471-
if filter.Match(adaptUsageInfo(c)) {
1472-
du = append(du, c)
1472+
if !filter.Match(adaptUsageInfo(c)) {
1473+
continue
1474+
}
1475+
if opt.AgeLimit > 0 {
1476+
if c.LastUsedAt != nil && c.LastUsedAt.After(cutOff) {
1477+
continue
1478+
}
14731479
}
1480+
du = append(du, c)
14741481
}
14751482

14761483
eg, ctx := errgroup.WithContext(ctx)

client/diskusage.go

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func (c *Client) DiskUsage(ctx context.Context, opts ...DiskUsageOption) ([]*Usa
3131
o.SetDiskUsageOption(info)
3232
}
3333

34-
req := &controlapi.DiskUsageRequest{Filter: info.Filter}
34+
req := &controlapi.DiskUsageRequest{Filter: info.Filter, AgeLimit: int64(info.AgeLimit)}
3535
resp, err := c.ControlClient().DiskUsage(ctx, req)
3636
if err != nil {
3737
return nil, errors.Wrap(err, "failed to call diskusage")
@@ -72,7 +72,8 @@ type DiskUsageOption interface {
7272
}
7373

7474
type DiskUsageInfo struct {
75-
Filter []string
75+
Filter []string
76+
AgeLimit time.Duration
7677
}
7778

7879
type UsageRecordType string
@@ -85,3 +86,15 @@ const (
8586
UsageRecordTypeCacheMount UsageRecordType = "exec.cachemount"
8687
UsageRecordTypeRegular UsageRecordType = "regular"
8788
)
89+
90+
type diskUsageOptionFunc func(*DiskUsageInfo)
91+
92+
func (f diskUsageOptionFunc) SetDiskUsageOption(info *DiskUsageInfo) {
93+
f(info)
94+
}
95+
96+
func WithAgeLimit(age time.Duration) DiskUsageOption {
97+
return diskUsageOptionFunc(func(info *DiskUsageInfo) {
98+
info.AgeLimit = age
99+
})
100+
}

control/control.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,8 @@ func (c *Controller) DiskUsage(ctx context.Context, r *controlapi.DiskUsageReque
167167
}
168168
for _, w := range workers {
169169
du, err := w.DiskUsage(ctx, client.DiskUsageInfo{
170-
Filter: r.Filter,
170+
Filter: r.Filter,
171+
AgeLimit: time.Duration(r.AgeLimit),
171172
})
172173
if err != nil {
173174
return nil, err

0 commit comments

Comments
 (0)