Skip to content

Commit

Permalink
history: remove records without attached blobs during gc
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <1951866+crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Oct 1, 2024
1 parent 9cdd15e commit 882208d
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions solver/llbsolver/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,9 @@ func (h *HistoryQueue) migrateBlobV2(ctx context.Context, id string, detectSkipL
}

func (h *HistoryQueue) gc() error {
ctx := context.Background()
var records []*controlapi.BuildHistoryRecord
var orphanRecords []*controlapi.BuildHistoryRecord

if err := h.opt.DB.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(recordsBucket))
Expand All @@ -298,13 +300,28 @@ func (h *HistoryQueue) gc() error {
if br.Pinned {
return nil
}
records = append(records, &br)
recs, err := h.hLeaseManager.ListResources(ctx, leases.Lease{ID: h.leaseID(string(key))})
if (err != nil && cerrdefs.IsNotFound(err)) || len(recs) == 0 {
orphanRecords = append(orphanRecords, &br)
} else {
records = append(records, &br)
}
return nil
})
}); err != nil {
return err
}

h.mu.Lock()
defer h.mu.Unlock()

// delete records that don't have any blobs associated with them
for _, r := range orphanRecords {
if err := h.delete(r.Ref, false); err != nil {
return err
}
}

// in order for record to get deleted by gc it exceed both maxentries and maxage criteria
if len(records) < int(h.opt.CleanConfig.MaxEntries) {
return nil
Expand All @@ -315,9 +332,6 @@ func (h *HistoryQueue) gc() error {
return records[i].CompletedAt.AsTime().After(records[j].CompletedAt.AsTime())
})

h.mu.Lock()
defer h.mu.Unlock()

now := time.Now()
for _, r := range records[h.opt.CleanConfig.MaxEntries:] {
if now.Add(-h.opt.CleanConfig.MaxAge.Duration).After(r.CompletedAt.AsTime()) {
Expand Down

0 comments on commit 882208d

Please sign in to comment.