Skip to content

Commit 2c10f56

Browse files
committed
history: remove records without attached blobs at startup
Signed-off-by: CrazyMax <[email protected]>
1 parent 8193e88 commit 2c10f56

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

solver/llbsolver/history.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"github.com/moby/buildkit/cmd/buildkitd/config"
2222
"github.com/moby/buildkit/identity"
2323
containerdsnapshot "github.com/moby/buildkit/snapshot/containerd"
24+
"github.com/moby/buildkit/util/bklog"
2425
"github.com/moby/buildkit/util/db"
2526
"github.com/moby/buildkit/util/grpcerrors"
2627
"github.com/moby/buildkit/util/iohelper"
@@ -128,6 +129,7 @@ func NewHistoryQueue(opt HistoryQueueOpt) (*HistoryQueue, error) {
128129
}
129130

130131
go func() {
132+
h.clearOrphans()
131133
for {
132134
h.gc()
133135
time.Sleep(120 * time.Second)
@@ -330,6 +332,47 @@ func (h *HistoryQueue) gc() error {
330332
return nil
331333
}
332334

335+
func (h *HistoryQueue) clearOrphans() error {
336+
ctx := context.Background()
337+
var records []*controlapi.BuildHistoryRecord
338+
339+
if err := h.opt.DB.View(func(tx *bolt.Tx) error {
340+
b := tx.Bucket([]byte(recordsBucket))
341+
if b == nil {
342+
return nil
343+
}
344+
return b.ForEach(func(key, dt []byte) error {
345+
var br controlapi.BuildHistoryRecord
346+
if err := proto.Unmarshal(dt, &br); err != nil {
347+
return errors.Wrapf(err, "failed to unmarshal build record %s", key)
348+
}
349+
recs, err := h.hLeaseManager.ListResources(ctx, leases.Lease{ID: h.leaseID(string(key))})
350+
if (err != nil && cerrdefs.IsNotFound(err)) || len(recs) == 0 {
351+
records = append(records, &br)
352+
}
353+
return nil
354+
})
355+
}); err != nil {
356+
return err
357+
}
358+
359+
if len(records) == 0 {
360+
return nil
361+
}
362+
363+
h.mu.Lock()
364+
defer h.mu.Unlock()
365+
366+
for _, r := range records {
367+
bklog.G(ctx).Warnf("deleting build record %s due to missing blobs", r.Ref)
368+
if err := h.delete(r.Ref, false); err != nil {
369+
return err
370+
}
371+
}
372+
373+
return nil
374+
}
375+
333376
func (h *HistoryQueue) delete(ref string, sync bool) error {
334377
if _, ok := h.refs[ref]; ok {
335378
h.deleted[ref] = struct{}{}

0 commit comments

Comments
 (0)