Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

history: remove records without attached blobs at startup #5378

Merged
merged 1 commit into from
Oct 2, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading