Skip to content

Commit

Permalink
Merge pull request #5515 from tonistiigi/history-gc-sync-fix
Browse files Browse the repository at this point in the history
fix gc after delete history records
  • Loading branch information
tonistiigi authored Nov 15, 2024
2 parents 9a33f71 + 65f5dad commit 9dd9f64
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 21 deletions.
1 change: 1 addition & 0 deletions cmd/buildkitd/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -850,6 +850,7 @@ func newController(c *cli.Context, cfg *config.Config) (*control.Controller, err
LeaseManager: w.LeaseManager(),
ContentStore: w.ContentStore(),
HistoryConfig: cfg.History,
GarbageCollect: w.GarbageCollect,
})
}

Expand Down
10 changes: 6 additions & 4 deletions control/control.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ type Opt struct {
LeaseManager *leaseutil.Manager
ContentStore *containerdsnapshot.Store
HistoryConfig *config.HistoryConfig
GarbageCollect func(context.Context) error
}

type Controller struct { // TODO: ControlService
Expand All @@ -89,10 +90,11 @@ func NewController(opt Opt) (*Controller, error) {
gatewayForwarder := controlgateway.NewGatewayForwarder()

hq, err := llbsolver.NewHistoryQueue(llbsolver.HistoryQueueOpt{
DB: opt.HistoryDB,
LeaseManager: opt.LeaseManager,
ContentStore: opt.ContentStore,
CleanConfig: opt.HistoryConfig,
DB: opt.HistoryDB,
LeaseManager: opt.LeaseManager,
ContentStore: opt.ContentStore,
CleanConfig: opt.HistoryConfig,
GarbageCollect: opt.GarbageCollect,
})
if err != nil {
return nil, errors.Wrap(err, "failed to create history queue")
Expand Down
38 changes: 21 additions & 17 deletions solver/llbsolver/history.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,11 @@ const (
)

type HistoryQueueOpt struct {
DB db.Transactor
LeaseManager *leaseutil.Manager
ContentStore *containerdsnapshot.Store
CleanConfig *config.HistoryConfig
DB db.Transactor
LeaseManager *leaseutil.Manager
ContentStore *containerdsnapshot.Store
CleanConfig *config.HistoryConfig
GarbageCollect func(context.Context) error
}

type HistoryQueue struct {
Expand Down Expand Up @@ -323,7 +324,7 @@ func (h *HistoryQueue) gc() error {
now := time.Now()
for _, r := range records[h.opt.CleanConfig.MaxEntries:] {
if now.Add(-h.opt.CleanConfig.MaxAge.Duration).After(r.CompletedAt.AsTime()) {
if err := h.delete(r.Ref, false); err != nil {
if _, err := h.delete(r.Ref); err != nil {
return err
}
}
Expand Down Expand Up @@ -365,18 +366,18 @@ func (h *HistoryQueue) clearOrphans() error {

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 {
if _, err := h.delete(r.Ref); err != nil {
return err
}
}

return nil
}

func (h *HistoryQueue) delete(ref string, sync bool) error {
func (h *HistoryQueue) delete(ref string) (bool, error) {
if _, ok := h.refs[ref]; ok {
h.deleted[ref] = struct{}{}
return nil
return false, nil
}
delete(h.deleted, ref)
h.ps.Send(&controlapi.BuildHistoryEvent{
Expand All @@ -389,19 +390,15 @@ func (h *HistoryQueue) delete(ref string, sync bool) error {
return errors.Wrapf(os.ErrNotExist, "failed to retrieve bucket %s", recordsBucket)
}
err1 := b.Delete([]byte(ref))
var opts []leases.DeleteOpt
if sync {
opts = append(opts, leases.SynchronousDelete)
}
err2 := h.hLeaseManager.Delete(context.TODO(), leases.Lease{ID: h.leaseID(ref)}, opts...)
err2 := h.hLeaseManager.Delete(context.TODO(), leases.Lease{ID: h.leaseID(ref)})
if err1 != nil {
return err1
}
return err2
}); err != nil {
return err
return false, err
}
return nil
return true, nil
}

func (h *HistoryQueue) init() error {
Expand Down Expand Up @@ -683,7 +680,14 @@ func (h *HistoryQueue) Delete(ctx context.Context, ref string) error {
h.mu.Lock()
defer h.mu.Unlock()

return h.delete(ref, true)
v, err := h.delete(ref)
if err != nil {
return err
}
if v {
return h.opt.GarbageCollect(ctx)
}
return nil
}

func (h *HistoryQueue) OpenBlobWriter(ctx context.Context, mt string) (_ *Writer, err error) {
Expand Down Expand Up @@ -909,7 +913,7 @@ func (h *HistoryQueue) Listen(ctx context.Context, req *controlapi.BuildHistoryR
if _, ok := h.deleted[req.Ref]; ok {
if h.refs[req.Ref] == 0 {
delete(h.refs, req.Ref)
h.delete(req.Ref, false)
h.delete(req.Ref)
}
}
h.mu.Unlock()
Expand Down
8 changes: 8 additions & 0 deletions worker/base/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,14 @@ func NewWorker(ctx context.Context, opt WorkerOpt) (*Worker, error) {
}, nil
}

func (w *Worker) GarbageCollect(ctx context.Context) error {
if w.WorkerOpt.GarbageCollect == nil {
return nil
}
_, err := w.WorkerOpt.GarbageCollect(ctx)
return err
}

func (w *Worker) Close() error {
var rerr error
if err := w.MetadataStore.Close(); err != nil {
Expand Down
1 change: 1 addition & 0 deletions worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ type Worker interface {
Executor() executor.Executor
CacheManager() cache.Manager
LeaseManager() *leaseutil.Manager
GarbageCollect(context.Context) error
}

type Infos interface {
Expand Down

0 comments on commit 9dd9f64

Please sign in to comment.