2017-05-27 06:12:13 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sync"
|
2017-11-06 09:44:23 +00:00
|
|
|
"time"
|
2017-05-27 06:12:13 +00:00
|
|
|
|
|
|
|
"github.com/containerd/containerd/mount"
|
2017-12-05 07:34:25 +00:00
|
|
|
cdsnapshot "github.com/containerd/containerd/snapshots"
|
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"
|
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-31 23:45:04 +00:00
|
|
|
"golang.org/x/net/context"
|
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
|
2017-07-14 18:59:31 +00:00
|
|
|
Finalize(ctx context.Context) error // Make sure reference is flushed to driver
|
2017-05-27 06:12:13 +00:00
|
|
|
// Prepare() / ChainID() / Meta()
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
2017-07-18 06:08:22 +00:00
|
|
|
Mount(ctx context.Context, readonly bool) ([]mount.Mount, error)
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type cacheRecord struct {
|
2017-07-14 18:59:31 +00:00
|
|
|
mu sync.Mutex
|
|
|
|
mutable bool
|
2017-06-01 22:24:23 +00:00
|
|
|
refs map[Mountable]struct{}
|
2017-05-30 21:17:04 +00:00
|
|
|
cm *cacheManager
|
|
|
|
parent ImmutableRef
|
2017-07-03 23:08:20 +00:00
|
|
|
md *metadata.StorageItem
|
2017-05-30 21:17:04 +00:00
|
|
|
view string
|
|
|
|
viewMount []mount.Mount
|
2017-09-28 05:38:54 +00:00
|
|
|
dead bool
|
2017-05-31 23:45:04 +00:00
|
|
|
|
|
|
|
sizeG flightcontrol.Group
|
2017-07-03 23:08:20 +00:00
|
|
|
// size int64
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// hold manager 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
|
|
|
|
}
|
|
|
|
|
|
|
|
// hold manager lock before calling
|
|
|
|
func (cr *cacheRecord) mref() *mutableRef {
|
|
|
|
ref := &mutableRef{cacheRecord: cr}
|
2017-05-27 06:12:13 +00:00
|
|
|
cr.refs[ref] = struct{}{}
|
|
|
|
return ref
|
|
|
|
}
|
|
|
|
|
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 {
|
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 {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return cr.parent.(*immutableRef).ref()
|
|
|
|
}
|
|
|
|
|
2017-07-18 06:08:22 +00:00
|
|
|
func (cr *cacheRecord) Mount(ctx context.Context, readonly bool) ([]mount.Mount, 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
|
|
|
|
}
|
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
if err := cr.finalize(ctx); err != nil {
|
|
|
|
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-11-06 09:44:23 +00:00
|
|
|
labels := map[string]string{
|
|
|
|
"containerd.io/gc.root": time.Now().UTC().Format(time.RFC3339Nano),
|
|
|
|
}
|
|
|
|
m, err := cr.cm.Snapshotter.View(ctx, cr.view, cr.ID(), cdsnapshot.WithLabels(labels))
|
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-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-07-14 18:59:31 +00:00
|
|
|
if err := cr.cm.md.Clear(cr.ID()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if removeSnapshot {
|
|
|
|
if err := cr.cm.Snapshotter.Remove(ctx, cr.ID()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|
|
|
|
|
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-07-25 19:11:52 +00:00
|
|
|
updateLastUsed(sr.md)
|
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
delete(sr.refs, sr)
|
|
|
|
|
|
|
|
if len(sr.refs) == 0 {
|
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
|
|
|
|
}
|
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
func (sr *immutableRef) Finalize(ctx context.Context) error {
|
2017-05-27 06:12:13 +00:00
|
|
|
sr.mu.Lock()
|
|
|
|
defer sr.mu.Unlock()
|
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
return sr.finalize(ctx)
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:35 +00:00
|
|
|
func (cr *cacheRecord) finalize(ctx context.Context) error {
|
|
|
|
mutable := cr.equalMutable
|
2017-07-14 18:59:31 +00:00
|
|
|
if mutable == nil {
|
|
|
|
return nil
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
2017-12-11 23:35:13 +00:00
|
|
|
labels := map[string]string{
|
|
|
|
"containerd.io/gc.root": time.Now().UTC().Format(time.RFC3339Nano),
|
|
|
|
}
|
|
|
|
err := cr.cm.Snapshotter.Commit(ctx, cr.ID(), mutable.ID(), cdsnapshot.WithLabels(labels))
|
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-05-27 06:12:13 +00:00
|
|
|
return nil, errors.Wrapf(errInvalid, "invalid mutable")
|
|
|
|
}
|
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
id := identity.NewID()
|
|
|
|
md, _ := sr.cm.md.Get(id)
|
|
|
|
|
|
|
|
rec := &cacheRecord{
|
|
|
|
cm: sr.cm,
|
|
|
|
parent: sr.parent,
|
|
|
|
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-25 19:11:52 +00:00
|
|
|
updateLastUsed(sr.md)
|
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)
|
|
|
|
}
|
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
|
|
|
|
|
|
|
func setReadonly(mounts []mount.Mount) []mount.Mount {
|
|
|
|
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
|
|
|
|
}
|
|
|
|
return mounts
|
|
|
|
}
|