2017-05-27 06:12:13 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
2018-01-16 22:30:10 +00:00
|
|
|
"context"
|
2017-05-27 06:12:13 +00:00
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/containerd/containerd/mount"
|
2017-07-03 23:08:20 +00:00
|
|
|
"github.com/moby/buildkit/cache/metadata"
|
2017-06-30 22:54:51 +00:00
|
|
|
"github.com/moby/buildkit/identity"
|
2018-04-16 22:23:10 +00:00
|
|
|
"github.com/moby/buildkit/snapshot"
|
2017-06-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/util/flightcontrol"
|
2017-05-27 06:12:13 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-09-28 05:38:54 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-05-27 06:12:13 +00:00
|
|
|
)
|
|
|
|
|
2017-11-06 06:30:16 +00:00
|
|
|
// Ref is a reference to cacheable objects.
|
|
|
|
type Ref interface {
|
2017-05-27 06:12:13 +00:00
|
|
|
Mountable
|
|
|
|
ID() string
|
2017-06-13 23:01:47 +00:00
|
|
|
Release(context.Context) error
|
2017-05-31 23:45:04 +00:00
|
|
|
Size(ctx context.Context) (int64, error)
|
2017-11-06 06:30:16 +00:00
|
|
|
Metadata() *metadata.StorageItem
|
|
|
|
}
|
|
|
|
|
|
|
|
type ImmutableRef interface {
|
|
|
|
Ref
|
2017-07-10 20:03:38 +00:00
|
|
|
Parent() ImmutableRef
|
2018-07-11 22:02:58 +00:00
|
|
|
Finalize(ctx context.Context, commit bool) error // Make sure reference is flushed to driver
|
2018-05-10 04:52:05 +00:00
|
|
|
Clone() ImmutableRef
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type MutableRef interface {
|
2017-11-06 06:30:16 +00:00
|
|
|
Ref
|
2017-07-14 18:59:31 +00:00
|
|
|
Commit(context.Context) (ImmutableRef, error)
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Mountable interface {
|
2018-04-16 22:23:10 +00:00
|
|
|
Mount(ctx context.Context, readonly bool) (snapshot.Mountable, error)
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type cacheRecord struct {
|
2017-12-26 19:42:14 +00:00
|
|
|
cm *cacheManager
|
|
|
|
mu *sync.Mutex // the mutex is shared by records sharing data
|
|
|
|
|
|
|
|
mutable bool
|
|
|
|
refs map[Mountable]struct{}
|
|
|
|
parent ImmutableRef
|
|
|
|
md *metadata.StorageItem
|
|
|
|
|
|
|
|
// dead means record is marked as deleted
|
|
|
|
dead bool
|
|
|
|
|
2017-05-30 21:17:04 +00:00
|
|
|
view string
|
2018-04-16 22:23:10 +00:00
|
|
|
viewMount snapshot.Mountable
|
2017-05-31 23:45:04 +00:00
|
|
|
|
|
|
|
sizeG flightcontrol.Group
|
2017-07-14 18:59:31 +00:00
|
|
|
|
|
|
|
// these are filled if multiple refs point to same data
|
|
|
|
equalMutable *mutableRef
|
|
|
|
equalImmutable *immutableRef
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-12-26 19:42:14 +00:00
|
|
|
// hold ref lock before calling
|
2017-06-01 22:24:23 +00:00
|
|
|
func (cr *cacheRecord) ref() *immutableRef {
|
|
|
|
ref := &immutableRef{cacheRecord: cr}
|
|
|
|
cr.refs[ref] = struct{}{}
|
|
|
|
return ref
|
|
|
|
}
|
|
|
|
|
2017-12-26 19:42:14 +00:00
|
|
|
// hold ref lock before calling
|
2017-06-01 22:24:23 +00:00
|
|
|
func (cr *cacheRecord) mref() *mutableRef {
|
|
|
|
ref := &mutableRef{cacheRecord: cr}
|
2017-05-27 06:12:13 +00:00
|
|
|
cr.refs[ref] = struct{}{}
|
|
|
|
return ref
|
|
|
|
}
|
|
|
|
|
2017-12-26 19:42:14 +00:00
|
|
|
// hold ref lock before calling
|
|
|
|
func (cr *cacheRecord) isDead() bool {
|
|
|
|
return cr.dead || (cr.equalImmutable != nil && cr.equalImmutable.dead) || (cr.equalMutable != nil && cr.equalMutable.dead)
|
|
|
|
}
|
|
|
|
|
2017-05-31 23:45:04 +00:00
|
|
|
func (cr *cacheRecord) Size(ctx context.Context) (int64, error) {
|
|
|
|
// this expects that usage() is implemented lazily
|
2017-07-03 23:08:20 +00:00
|
|
|
s, err := cr.sizeG.Do(ctx, cr.ID(), func(ctx context.Context) (interface{}, error) {
|
2017-05-31 23:45:04 +00:00
|
|
|
cr.mu.Lock()
|
2017-07-03 23:08:20 +00:00
|
|
|
s := getSize(cr.md)
|
2017-05-31 23:45:04 +00:00
|
|
|
if s != sizeUnknown {
|
2017-07-14 18:59:31 +00:00
|
|
|
cr.mu.Unlock()
|
2017-05-31 23:45:04 +00:00
|
|
|
return s, nil
|
|
|
|
}
|
2017-07-14 18:59:31 +00:00
|
|
|
driverID := cr.ID()
|
|
|
|
if cr.equalMutable != nil {
|
|
|
|
driverID = cr.equalMutable.ID()
|
|
|
|
}
|
|
|
|
cr.mu.Unlock()
|
|
|
|
usage, err := cr.cm.ManagerOpt.Snapshotter.Usage(ctx, driverID)
|
2017-05-31 23:45:04 +00:00
|
|
|
if err != nil {
|
2018-04-30 17:38:47 +00:00
|
|
|
cr.mu.Lock()
|
|
|
|
isDead := cr.isDead()
|
|
|
|
cr.mu.Unlock()
|
|
|
|
if isDead {
|
2017-12-27 01:22:50 +00:00
|
|
|
return int64(0), nil
|
|
|
|
}
|
2017-07-03 23:08:20 +00:00
|
|
|
return s, errors.Wrapf(err, "failed to get usage for %s", cr.ID())
|
2017-05-31 23:45:04 +00:00
|
|
|
}
|
|
|
|
cr.mu.Lock()
|
2017-07-03 23:08:20 +00:00
|
|
|
setSize(cr.md, usage.Size)
|
|
|
|
if err := cr.md.Commit(); err != nil {
|
2018-04-30 17:38:47 +00:00
|
|
|
cr.mu.Unlock()
|
2017-07-03 23:08:20 +00:00
|
|
|
return s, err
|
|
|
|
}
|
2017-05-31 23:45:04 +00:00
|
|
|
cr.mu.Unlock()
|
|
|
|
return usage.Size, nil
|
|
|
|
})
|
|
|
|
return s.(int64), err
|
|
|
|
}
|
|
|
|
|
2017-07-10 20:03:38 +00:00
|
|
|
func (cr *cacheRecord) Parent() ImmutableRef {
|
|
|
|
if cr.parent == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-26 19:42:14 +00:00
|
|
|
p := cr.parent.(*immutableRef)
|
|
|
|
p.mu.Lock()
|
2017-12-28 23:03:49 +00:00
|
|
|
defer p.mu.Unlock()
|
2017-12-26 19:42:14 +00:00
|
|
|
return p.ref()
|
2017-07-10 20:03:38 +00:00
|
|
|
}
|
|
|
|
|
2018-04-16 22:23:10 +00:00
|
|
|
func (cr *cacheRecord) Mount(ctx context.Context, readonly bool) (snapshot.Mountable, error) {
|
2017-06-01 22:24:23 +00:00
|
|
|
cr.mu.Lock()
|
|
|
|
defer cr.mu.Unlock()
|
2017-05-27 06:12:13 +00:00
|
|
|
|
2017-06-01 22:24:23 +00:00
|
|
|
if cr.mutable {
|
2017-07-03 23:08:20 +00:00
|
|
|
m, err := cr.cm.Snapshotter.Mounts(ctx, cr.ID())
|
2017-05-27 06:12:13 +00:00
|
|
|
if err != nil {
|
2017-07-03 23:08:20 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to mount %s", cr.ID())
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
2017-07-18 06:08:22 +00:00
|
|
|
if readonly {
|
|
|
|
m = setReadonly(m)
|
|
|
|
}
|
2017-05-27 06:12:13 +00:00
|
|
|
return m, nil
|
2017-06-13 08:32:30 +00:00
|
|
|
}
|
2017-07-18 06:08:22 +00:00
|
|
|
|
|
|
|
if cr.equalMutable != nil && readonly {
|
|
|
|
m, err := cr.cm.Snapshotter.Mounts(ctx, cr.equalMutable.ID())
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to mount %s", cr.equalMutable.ID())
|
|
|
|
}
|
|
|
|
return setReadonly(m), nil
|
|
|
|
}
|
|
|
|
|
2018-07-11 22:02:58 +00:00
|
|
|
if err := cr.finalize(ctx, true); err != nil {
|
2017-07-14 18:59:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2017-06-13 08:32:30 +00:00
|
|
|
if cr.viewMount == nil { // TODO: handle this better
|
2017-06-30 22:54:51 +00:00
|
|
|
cr.view = identity.NewID()
|
2017-12-28 07:07:13 +00:00
|
|
|
m, err := cr.cm.Snapshotter.View(ctx, cr.view, cr.ID())
|
2017-06-13 08:32:30 +00:00
|
|
|
if err != nil {
|
|
|
|
cr.view = ""
|
2017-07-03 23:08:20 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to mount %s", cr.ID())
|
2017-05-30 21:17:04 +00:00
|
|
|
}
|
2017-06-13 08:32:30 +00:00
|
|
|
cr.viewMount = m
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
2017-06-13 08:32:30 +00:00
|
|
|
return cr.viewMount, nil
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-12-27 01:22:50 +00:00
|
|
|
// call when holding the manager lock
|
2017-07-14 18:59:31 +00:00
|
|
|
func (cr *cacheRecord) remove(ctx context.Context, removeSnapshot bool) error {
|
2017-07-19 23:39:32 +00:00
|
|
|
delete(cr.cm.records, cr.ID())
|
2017-12-27 01:22:50 +00:00
|
|
|
if cr.parent != nil {
|
|
|
|
if err := cr.parent.(*immutableRef).release(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-14 18:59:31 +00:00
|
|
|
}
|
|
|
|
if removeSnapshot {
|
|
|
|
if err := cr.cm.Snapshotter.Remove(ctx, cr.ID()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2017-12-27 01:22:50 +00:00
|
|
|
if err := cr.cm.md.Clear(cr.ID()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-14 18:59:31 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-06-01 22:24:23 +00:00
|
|
|
func (cr *cacheRecord) ID() string {
|
2017-07-03 23:08:20 +00:00
|
|
|
return cr.md.ID()
|
2017-06-01 22:24:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type immutableRef struct {
|
|
|
|
*cacheRecord
|
|
|
|
}
|
|
|
|
|
|
|
|
type mutableRef struct {
|
|
|
|
*cacheRecord
|
|
|
|
}
|
|
|
|
|
2018-05-10 04:52:05 +00:00
|
|
|
func (sr *immutableRef) Clone() ImmutableRef {
|
|
|
|
sr.mu.Lock()
|
|
|
|
ref := sr.ref()
|
|
|
|
sr.mu.Unlock()
|
|
|
|
return ref
|
|
|
|
}
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
func (sr *immutableRef) Release(ctx context.Context) error {
|
2017-05-27 06:12:13 +00:00
|
|
|
sr.cm.mu.Lock()
|
|
|
|
defer sr.cm.mu.Unlock()
|
|
|
|
|
|
|
|
sr.mu.Lock()
|
|
|
|
defer sr.mu.Unlock()
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
return sr.release(ctx)
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
func (sr *immutableRef) release(ctx context.Context) error {
|
2017-05-27 06:12:13 +00:00
|
|
|
delete(sr.refs, sr)
|
|
|
|
|
|
|
|
if len(sr.refs) == 0 {
|
2018-05-11 05:33:51 +00:00
|
|
|
updateLastUsed(sr.md)
|
2017-12-11 23:35:42 +00:00
|
|
|
if sr.viewMount != nil { // TODO: release viewMount earlier if possible
|
|
|
|
if err := sr.cm.Snapshotter.Remove(ctx, sr.view); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
sr.view = ""
|
|
|
|
sr.viewMount = nil
|
|
|
|
}
|
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
if sr.equalMutable != nil {
|
|
|
|
sr.equalMutable.release(ctx)
|
|
|
|
}
|
|
|
|
// go sr.cm.GC()
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-07-11 22:02:58 +00:00
|
|
|
func (sr *immutableRef) Finalize(ctx context.Context, b bool) error {
|
2017-05-27 06:12:13 +00:00
|
|
|
sr.mu.Lock()
|
|
|
|
defer sr.mu.Unlock()
|
|
|
|
|
2018-07-11 22:02:58 +00:00
|
|
|
return sr.finalize(ctx, b)
|
2017-07-14 18:59:31 +00:00
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:35 +00:00
|
|
|
func (cr *cacheRecord) Metadata() *metadata.StorageItem {
|
|
|
|
return cr.md
|
2017-07-20 22:55:24 +00:00
|
|
|
}
|
|
|
|
|
2018-07-11 22:02:58 +00:00
|
|
|
func (cr *cacheRecord) finalize(ctx context.Context, commit bool) error {
|
2017-11-17 02:09:35 +00:00
|
|
|
mutable := cr.equalMutable
|
2017-07-14 18:59:31 +00:00
|
|
|
if mutable == nil {
|
|
|
|
return nil
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
2018-07-11 22:02:58 +00:00
|
|
|
if !commit {
|
|
|
|
if HasCachePolicyRetain(mutable) {
|
|
|
|
CachePolicyRetain(mutable)
|
|
|
|
return mutable.Metadata().Commit()
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-12-28 07:07:13 +00:00
|
|
|
err := cr.cm.Snapshotter.Commit(ctx, cr.ID(), mutable.ID())
|
2017-07-14 18:59:31 +00:00
|
|
|
if err != nil {
|
2017-09-28 05:38:54 +00:00
|
|
|
return errors.Wrapf(err, "failed to commit %s", mutable.ID())
|
2017-07-14 18:59:31 +00:00
|
|
|
}
|
2017-09-28 05:38:54 +00:00
|
|
|
mutable.dead = true
|
|
|
|
go func() {
|
2017-11-17 02:09:35 +00:00
|
|
|
cr.cm.mu.Lock()
|
|
|
|
defer cr.cm.mu.Unlock()
|
2017-09-28 05:38:54 +00:00
|
|
|
if err := mutable.remove(context.TODO(), false); err != nil {
|
|
|
|
logrus.Error(err)
|
|
|
|
}
|
|
|
|
}()
|
2017-11-17 02:09:35 +00:00
|
|
|
cr.equalMutable = nil
|
|
|
|
clearEqualMutable(cr.md)
|
|
|
|
return cr.md.Commit()
|
2017-07-14 18:59:31 +00:00
|
|
|
}
|
2017-05-27 06:12:13 +00:00
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
func (sr *mutableRef) commit(ctx context.Context) (ImmutableRef, error) {
|
|
|
|
if !sr.mutable || len(sr.refs) == 0 {
|
2017-12-26 19:42:14 +00:00
|
|
|
return nil, errors.Wrapf(errInvalid, "invalid mutable ref")
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
id := identity.NewID()
|
|
|
|
md, _ := sr.cm.md.Get(id)
|
|
|
|
|
|
|
|
rec := &cacheRecord{
|
2017-12-26 19:42:14 +00:00
|
|
|
mu: sr.mu,
|
2017-07-14 18:59:31 +00:00
|
|
|
cm: sr.cm,
|
2017-12-27 01:22:50 +00:00
|
|
|
parent: sr.Parent(),
|
2017-07-14 18:59:31 +00:00
|
|
|
equalMutable: sr,
|
|
|
|
refs: make(map[Mountable]struct{}),
|
2017-08-10 01:20:33 +00:00
|
|
|
md: md,
|
2017-07-14 18:59:31 +00:00
|
|
|
}
|
2017-06-01 22:24:23 +00:00
|
|
|
|
2017-12-08 18:53:17 +00:00
|
|
|
if descr := GetDescription(sr.md); descr != "" {
|
2017-08-10 01:20:33 +00:00
|
|
|
if err := queueDescription(md, descr); err != nil {
|
2017-07-25 19:11:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := initializeMetadata(rec); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
sr.cm.records[id] = rec
|
2017-06-01 22:24:23 +00:00
|
|
|
|
2017-07-03 23:08:20 +00:00
|
|
|
if err := sr.md.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-27 06:12:13 +00:00
|
|
|
|
2017-08-10 01:20:33 +00:00
|
|
|
setSize(md, sizeUnknown)
|
|
|
|
setEqualMutable(md, sr.ID())
|
2017-07-14 18:59:31 +00:00
|
|
|
if err := md.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ref := rec.ref()
|
|
|
|
sr.equalImmutable = ref
|
|
|
|
return ref, nil
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
func (sr *mutableRef) Commit(ctx context.Context) (ImmutableRef, error) {
|
2017-05-27 06:12:13 +00:00
|
|
|
sr.cm.mu.Lock()
|
|
|
|
defer sr.cm.mu.Unlock()
|
|
|
|
|
|
|
|
sr.mu.Lock()
|
2017-07-14 18:59:31 +00:00
|
|
|
defer sr.mu.Unlock()
|
2017-05-27 06:12:13 +00:00
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
return sr.commit(ctx)
|
|
|
|
}
|
2017-07-03 23:08:20 +00:00
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
func (sr *mutableRef) Release(ctx context.Context) error {
|
|
|
|
sr.cm.mu.Lock()
|
|
|
|
defer sr.cm.mu.Unlock()
|
2017-05-27 06:12:13 +00:00
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
sr.mu.Lock()
|
|
|
|
defer sr.mu.Unlock()
|
2017-05-27 06:12:13 +00:00
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
return sr.release(ctx)
|
|
|
|
}
|
2017-05-27 06:12:13 +00:00
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
func (sr *mutableRef) release(ctx context.Context) error {
|
|
|
|
delete(sr.refs, sr)
|
2017-07-20 22:55:24 +00:00
|
|
|
if getCachePolicy(sr.md) != cachePolicyRetain {
|
2017-07-19 23:39:32 +00:00
|
|
|
if sr.equalImmutable != nil {
|
2017-07-20 22:55:24 +00:00
|
|
|
if getCachePolicy(sr.equalImmutable.md) == cachePolicyRetain {
|
|
|
|
return nil
|
|
|
|
}
|
2017-07-19 23:39:32 +00:00
|
|
|
if err := sr.equalImmutable.remove(ctx, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if sr.parent != nil {
|
|
|
|
if err := sr.parent.(*immutableRef).release(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return sr.remove(ctx, true)
|
2018-05-11 05:33:51 +00:00
|
|
|
} else {
|
|
|
|
updateLastUsed(sr.md)
|
2017-07-19 23:39:32 +00:00
|
|
|
}
|
2017-07-14 18:59:31 +00:00
|
|
|
return nil
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
2017-07-18 06:08:22 +00:00
|
|
|
|
2018-04-16 22:23:10 +00:00
|
|
|
func setReadonly(mounts snapshot.Mountable) snapshot.Mountable {
|
|
|
|
return &readOnlyMounter{mounts}
|
|
|
|
}
|
|
|
|
|
|
|
|
type readOnlyMounter struct {
|
|
|
|
snapshot.Mountable
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *readOnlyMounter) Mount() ([]mount.Mount, error) {
|
|
|
|
mounts, err := m.Mountable.Mount()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-18 06:08:22 +00:00
|
|
|
for i, m := range mounts {
|
|
|
|
opts := make([]string, 0, len(m.Options))
|
|
|
|
for _, opt := range m.Options {
|
|
|
|
if opt != "rw" {
|
|
|
|
opts = append(opts, opt)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
opts = append(opts, "ro")
|
|
|
|
mounts[i].Options = opts
|
|
|
|
}
|
2018-04-16 22:23:10 +00:00
|
|
|
return mounts, nil
|
2017-07-18 06:08:22 +00:00
|
|
|
}
|