@@ -21,6 +21,7 @@ import (
21
21
"github.com/moby/buildkit/cmd/buildkitd/config"
22
22
"github.com/moby/buildkit/identity"
23
23
containerdsnapshot "github.com/moby/buildkit/snapshot/containerd"
24
+ "github.com/moby/buildkit/util/bklog"
24
25
"github.com/moby/buildkit/util/db"
25
26
"github.com/moby/buildkit/util/grpcerrors"
26
27
"github.com/moby/buildkit/util/iohelper"
@@ -128,6 +129,7 @@ func NewHistoryQueue(opt HistoryQueueOpt) (*HistoryQueue, error) {
128
129
}
129
130
130
131
go func () {
132
+ h .clearOrphans ()
131
133
for {
132
134
h .gc ()
133
135
time .Sleep (120 * time .Second )
@@ -330,6 +332,47 @@ func (h *HistoryQueue) gc() error {
330
332
return nil
331
333
}
332
334
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
+
333
376
func (h * HistoryQueue ) delete (ref string , sync bool ) error {
334
377
if _ , ok := h .refs [ref ]; ok {
335
378
h .deleted [ref ] = struct {}{}
0 commit comments