Skip to content

Commit

Permalink
history: remove records without attached blobs at startup
Browse files Browse the repository at this point in the history
Signed-off-by: CrazyMax <[email protected]>
  • Loading branch information
crazy-max committed Oct 2, 2024
1 parent 8193e88 commit 2c10f56
Showing 1 changed file with 43 additions and 0 deletions.
43 changes: 43 additions & 0 deletions solver/llbsolver/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/moby/buildkit/cmd/buildkitd/config"
"github.com/moby/buildkit/identity"
containerdsnapshot "github.com/moby/buildkit/snapshot/containerd"
"github.com/moby/buildkit/util/bklog"
"github.com/moby/buildkit/util/db"
"github.com/moby/buildkit/util/grpcerrors"
"github.com/moby/buildkit/util/iohelper"
Expand Down Expand Up @@ -128,6 +129,7 @@ func NewHistoryQueue(opt HistoryQueueOpt) (*HistoryQueue, error) {
}

go func() {
h.clearOrphans()
for {
h.gc()
time.Sleep(120 * time.Second)
Expand Down Expand Up @@ -330,6 +332,47 @@ func (h *HistoryQueue) gc() error {
return nil
}

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

if err := h.opt.DB.View(func(tx *bolt.Tx) error {
b := tx.Bucket([]byte(recordsBucket))
if b == nil {
return nil
}
return b.ForEach(func(key, dt []byte) error {
var br controlapi.BuildHistoryRecord
if err := proto.Unmarshal(dt, &br); err != nil {
return errors.Wrapf(err, "failed to unmarshal build record %s", key)
}
recs, err := h.hLeaseManager.ListResources(ctx, leases.Lease{ID: h.leaseID(string(key))})
if (err != nil && cerrdefs.IsNotFound(err)) || len(recs) == 0 {
records = append(records, &br)
}
return nil
})
}); err != nil {
return err
}

if len(records) == 0 {
return nil
}

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

for _, r := range records {
bklog.G(ctx).Warnf("deleting build record %s due to missing blobs", r.Ref)
if err := h.delete(r.Ref, false); err != nil {
return err
}
}

return nil
}

func (h *HistoryQueue) delete(ref string, sync bool) error {
if _, ok := h.refs[ref]; ok {
h.deleted[ref] = struct{}{}
Expand Down

0 comments on commit 2c10f56

Please sign in to comment.