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 <1951866+crazy-max@users.noreply.github.com>
  • Loading branch information
crazy-max committed Oct 3, 2024
1 parent 8f8fc88 commit 4d76d6f
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 @@ -22,6 +22,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 @@ -127,6 +128,7 @@ func NewHistoryQueue(opt HistoryQueueOpt) (*HistoryQueue, error) {
}

go func() {
h.clearOrphans()
for {
h.gc()
time.Sleep(120 * time.Second)
Expand Down Expand Up @@ -329,6 +331,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 := br.Unmarshal(dt); 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 4d76d6f

Please sign in to comment.