2017-05-27 06:12:13 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2017-07-25 22:14:46 +00:00
|
|
|
"strings"
|
2017-05-27 06:12:13 +00:00
|
|
|
"sync"
|
2017-07-25 19:11:52 +00:00
|
|
|
"time"
|
2017-05-27 06:12:13 +00:00
|
|
|
|
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-22 20:15:46 +00:00
|
|
|
"github.com/moby/buildkit/client"
|
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/snapshot"
|
2017-05-27 06:12:13 +00:00
|
|
|
"github.com/pkg/errors"
|
2017-07-19 01:05:19 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2017-06-30 22:54:51 +00:00
|
|
|
"golang.org/x/sync/errgroup"
|
2017-05-27 06:12:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
errLocked = errors.New("locked")
|
|
|
|
errNotFound = errors.New("not found")
|
|
|
|
errInvalid = errors.New("invalid")
|
|
|
|
)
|
|
|
|
|
|
|
|
type ManagerOpt struct {
|
2017-07-05 02:00:27 +00:00
|
|
|
Snapshotter snapshot.Snapshotter
|
|
|
|
GCPolicy GCPolicy
|
|
|
|
MetadataStore *metadata.Store
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Accessor interface {
|
2017-07-25 19:11:52 +00:00
|
|
|
Get(ctx context.Context, id string, opts ...RefOption) (ImmutableRef, error)
|
|
|
|
New(ctx context.Context, s ImmutableRef, opts ...RefOption) (MutableRef, error)
|
2017-06-13 23:01:47 +00:00
|
|
|
GetMutable(ctx context.Context, id string) (MutableRef, error) // Rebase?
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Controller interface {
|
2017-07-25 22:14:46 +00:00
|
|
|
DiskUsage(ctx context.Context, info client.DiskUsageInfo) ([]*client.UsageInfo, error)
|
2017-05-27 06:12:13 +00:00
|
|
|
Prune(ctx context.Context) (map[string]int64, error)
|
|
|
|
GC(ctx context.Context) error
|
|
|
|
}
|
|
|
|
|
|
|
|
type Manager interface {
|
|
|
|
Accessor
|
|
|
|
Controller
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
|
|
|
type cacheManager struct {
|
|
|
|
records map[string]*cacheRecord
|
|
|
|
mu sync.Mutex
|
|
|
|
ManagerOpt
|
2017-07-03 23:08:20 +00:00
|
|
|
md *metadata.Store
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewManager(opt ManagerOpt) (Manager, error) {
|
|
|
|
cm := &cacheManager{
|
|
|
|
ManagerOpt: opt,
|
2017-07-05 02:00:27 +00:00
|
|
|
md: opt.MetadataStore,
|
2017-05-27 06:12:13 +00:00
|
|
|
records: make(map[string]*cacheRecord),
|
|
|
|
}
|
|
|
|
|
2017-07-03 23:08:20 +00:00
|
|
|
if err := cm.init(context.TODO()); err != nil {
|
2017-05-27 06:12:13 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// cm.scheduleGC(5 * time.Minute)
|
|
|
|
|
|
|
|
return cm, nil
|
|
|
|
}
|
|
|
|
|
2017-07-03 23:08:20 +00:00
|
|
|
func (cm *cacheManager) init(ctx context.Context) error {
|
|
|
|
items, err := cm.md.All()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, si := range items {
|
|
|
|
if _, err := cm.load(ctx, si.ID()); err != nil {
|
|
|
|
logrus.Debugf("could not load snapshot %s, %v", si.ID(), err)
|
|
|
|
cm.md.Clear(si.ID())
|
|
|
|
}
|
|
|
|
}
|
2017-05-27 06:12:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *cacheManager) Close() error {
|
|
|
|
// TODO: allocate internal context and cancel it here
|
2017-07-03 23:08:20 +00:00
|
|
|
return cm.md.Close()
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:11:52 +00:00
|
|
|
func (cm *cacheManager) Get(ctx context.Context, id string, opts ...RefOption) (ImmutableRef, error) {
|
2017-05-27 06:12:13 +00:00
|
|
|
cm.mu.Lock()
|
|
|
|
defer cm.mu.Unlock()
|
2017-07-25 19:11:52 +00:00
|
|
|
return cm.get(ctx, id, opts...)
|
2017-05-30 21:17:04 +00:00
|
|
|
}
|
2017-07-10 20:03:38 +00:00
|
|
|
|
2017-07-25 19:11:52 +00:00
|
|
|
func (cm *cacheManager) get(ctx context.Context, id string, opts ...RefOption) (ImmutableRef, error) {
|
|
|
|
rec, err := cm.load(ctx, id, opts...)
|
2017-07-03 23:08:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
rec.mu.Lock()
|
|
|
|
defer rec.mu.Unlock()
|
|
|
|
|
2017-07-03 23:08:20 +00:00
|
|
|
if rec.mutable {
|
2017-09-28 05:38:54 +00:00
|
|
|
if rec.dead || len(rec.refs) != 0 {
|
2017-05-27 06:12:13 +00:00
|
|
|
return nil, errors.Wrapf(errLocked, "%s is locked", id)
|
|
|
|
}
|
2017-07-20 22:55:24 +00:00
|
|
|
if rec.equalImmutable != nil {
|
|
|
|
return rec.equalImmutable.ref(), nil
|
|
|
|
}
|
2017-07-14 18:59:31 +00:00
|
|
|
return rec.mref().commit(ctx)
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return rec.ref(), nil
|
|
|
|
}
|
2017-07-03 23:08:20 +00:00
|
|
|
|
2017-07-25 19:11:52 +00:00
|
|
|
func (cm *cacheManager) load(ctx context.Context, id string, opts ...RefOption) (*cacheRecord, error) {
|
2017-07-03 23:08:20 +00:00
|
|
|
if rec, ok := cm.records[id]; ok {
|
|
|
|
return rec, nil
|
|
|
|
}
|
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
md, _ := cm.md.Get(id)
|
2017-08-10 01:20:33 +00:00
|
|
|
if mutableID := getEqualMutable(md); mutableID != "" {
|
2017-07-14 18:59:31 +00:00
|
|
|
mutable, err := cm.load(ctx, mutableID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rec := &cacheRecord{
|
|
|
|
cm: cm,
|
|
|
|
refs: make(map[Mountable]struct{}),
|
|
|
|
parent: mutable.parent,
|
2017-08-10 01:20:33 +00:00
|
|
|
md: md,
|
2017-07-14 18:59:31 +00:00
|
|
|
equalMutable: &mutableRef{cacheRecord: mutable},
|
|
|
|
}
|
|
|
|
mutable.equalImmutable = &immutableRef{cacheRecord: rec}
|
|
|
|
cm.records[id] = rec
|
|
|
|
return rec, nil
|
|
|
|
}
|
|
|
|
|
2017-07-03 23:08:20 +00:00
|
|
|
info, err := cm.Snapshotter.Stat(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(errNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var parent ImmutableRef
|
|
|
|
if info.Parent != "" {
|
2017-07-25 19:11:52 +00:00
|
|
|
parent, err = cm.get(ctx, info.Parent, opts...)
|
2017-07-03 23:08:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := &cacheRecord{
|
|
|
|
mutable: info.Kind != cdsnapshot.KindCommitted,
|
|
|
|
cm: cm,
|
|
|
|
refs: make(map[Mountable]struct{}),
|
|
|
|
parent: parent,
|
2017-08-10 01:20:33 +00:00
|
|
|
md: md,
|
2017-07-03 23:08:20 +00:00
|
|
|
}
|
2017-07-25 19:11:52 +00:00
|
|
|
|
|
|
|
if err := initializeMetadata(rec, opts...); err != nil {
|
|
|
|
if parent != nil {
|
2017-09-22 17:30:30 +00:00
|
|
|
parent.Release(context.TODO())
|
2017-07-25 19:11:52 +00:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
cm.records[id] = rec
|
2017-07-03 23:08:20 +00:00
|
|
|
return rec, nil
|
|
|
|
}
|
|
|
|
|
2017-07-19 23:39:32 +00:00
|
|
|
func (cm *cacheManager) New(ctx context.Context, s ImmutableRef, opts ...RefOption) (MutableRef, error) {
|
2017-06-30 22:54:51 +00:00
|
|
|
id := identity.NewID()
|
2017-05-27 06:12:13 +00:00
|
|
|
|
|
|
|
var parent ImmutableRef
|
|
|
|
var parentID string
|
|
|
|
if s != nil {
|
|
|
|
var err error
|
2017-06-13 23:01:47 +00:00
|
|
|
parent, err = cm.Get(ctx, s.ID())
|
2017-05-27 06:12:13 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-14 18:59:31 +00:00
|
|
|
if err := parent.Finalize(ctx); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-05-27 06:12:13 +00:00
|
|
|
parentID = parent.ID()
|
|
|
|
}
|
|
|
|
|
2017-11-06 09:44:23 +00:00
|
|
|
labels := map[string]string{
|
|
|
|
"containerd.io/gc.root": time.Now().UTC().Format(time.RFC3339Nano),
|
|
|
|
}
|
|
|
|
if _, err := cm.Snapshotter.Prepare(ctx, id, parentID, cdsnapshot.WithLabels(labels)); err != nil {
|
2017-05-27 06:12:13 +00:00
|
|
|
if parent != nil {
|
2017-09-22 17:30:30 +00:00
|
|
|
parent.Release(context.TODO())
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
return nil, errors.Wrapf(err, "failed to prepare %s", id)
|
|
|
|
}
|
|
|
|
|
2017-07-03 23:08:20 +00:00
|
|
|
md, _ := cm.md.Get(id)
|
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
rec := &cacheRecord{
|
|
|
|
mutable: true,
|
|
|
|
cm: cm,
|
2017-06-01 22:24:23 +00:00
|
|
|
refs: make(map[Mountable]struct{}),
|
2017-05-27 06:12:13 +00:00
|
|
|
parent: parent,
|
2017-08-10 01:20:33 +00:00
|
|
|
md: md,
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 19:11:52 +00:00
|
|
|
if err := initializeMetadata(rec, opts...); err != nil {
|
|
|
|
if parent != nil {
|
2017-09-22 17:30:30 +00:00
|
|
|
parent.Release(context.TODO())
|
2017-07-19 23:39:32 +00:00
|
|
|
}
|
2017-07-25 19:11:52 +00:00
|
|
|
return nil, err
|
2017-07-19 23:39:32 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
cm.mu.Lock()
|
|
|
|
defer cm.mu.Unlock()
|
|
|
|
|
|
|
|
cm.records[id] = rec // TODO: save to db
|
|
|
|
|
2017-06-01 22:24:23 +00:00
|
|
|
return rec.mref(), nil
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
2017-07-14 18:59:31 +00:00
|
|
|
func (cm *cacheManager) GetMutable(ctx context.Context, id string) (MutableRef, error) {
|
2017-05-27 06:12:13 +00:00
|
|
|
cm.mu.Lock()
|
|
|
|
defer cm.mu.Unlock()
|
|
|
|
|
2017-07-03 23:08:20 +00:00
|
|
|
rec, err := cm.load(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rec.mu.Lock()
|
|
|
|
defer rec.mu.Unlock()
|
|
|
|
if !rec.mutable {
|
|
|
|
return nil, errors.Wrapf(errInvalid, "%s is not mutable", id)
|
|
|
|
}
|
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
if len(rec.refs) != 0 {
|
2017-05-27 06:12:13 +00:00
|
|
|
return nil, errors.Wrapf(errLocked, "%s is locked", id)
|
|
|
|
}
|
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
if rec.equalImmutable != nil {
|
2017-07-19 23:39:32 +00:00
|
|
|
if len(rec.equalImmutable.refs) != 0 {
|
|
|
|
return nil, errors.Wrapf(errLocked, "%s is locked", id)
|
|
|
|
}
|
2017-07-14 18:59:31 +00:00
|
|
|
delete(cm.records, rec.equalImmutable.ID())
|
|
|
|
if err := rec.equalImmutable.remove(ctx, false); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rec.equalImmutable = nil
|
|
|
|
}
|
|
|
|
|
2017-06-01 22:24:23 +00:00
|
|
|
return rec.mref(), nil
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-07-25 22:14:46 +00:00
|
|
|
func (cm *cacheManager) DiskUsage(ctx context.Context, opt client.DiskUsageInfo) ([]*client.UsageInfo, error) {
|
2017-05-27 06:12:13 +00:00
|
|
|
cm.mu.Lock()
|
|
|
|
|
2017-06-30 22:54:51 +00:00
|
|
|
type cacheUsageInfo struct {
|
2017-07-25 19:11:52 +00:00
|
|
|
refs int
|
|
|
|
parent string
|
|
|
|
size int64
|
|
|
|
mutable bool
|
|
|
|
createdAt time.Time
|
|
|
|
usageCount int
|
|
|
|
lastUsedAt *time.Time
|
|
|
|
description string
|
2017-06-30 22:54:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m := make(map[string]*cacheUsageInfo, len(cm.records))
|
|
|
|
rescan := make(map[string]struct{}, len(cm.records))
|
2017-05-27 06:12:13 +00:00
|
|
|
|
|
|
|
for id, cr := range cm.records {
|
|
|
|
cr.mu.Lock()
|
2017-07-14 18:59:31 +00:00
|
|
|
// ignore duplicates that share data
|
|
|
|
if cr.equalImmutable != nil && len(cr.equalImmutable.refs) > 0 || cr.equalMutable != nil && len(cr.refs) == 0 {
|
|
|
|
cr.mu.Unlock()
|
|
|
|
continue
|
|
|
|
}
|
2017-07-25 22:14:46 +00:00
|
|
|
|
2017-07-25 19:11:52 +00:00
|
|
|
usageCount, lastUsedAt := getLastUsed(cr.md)
|
2017-06-30 22:54:51 +00:00
|
|
|
c := &cacheUsageInfo{
|
2017-07-25 19:11:52 +00:00
|
|
|
refs: len(cr.refs),
|
|
|
|
mutable: cr.mutable,
|
|
|
|
size: getSize(cr.md),
|
|
|
|
createdAt: getCreatedAt(cr.md),
|
|
|
|
usageCount: usageCount,
|
|
|
|
lastUsedAt: lastUsedAt,
|
|
|
|
description: getDescription(cr.md),
|
2017-06-30 22:54:51 +00:00
|
|
|
}
|
|
|
|
if cr.parent != nil {
|
|
|
|
c.parent = cr.parent.ID()
|
|
|
|
}
|
2017-07-14 18:59:31 +00:00
|
|
|
if cr.mutable && c.refs > 0 {
|
2017-06-30 22:54:51 +00:00
|
|
|
c.size = 0 // size can not be determined because it is changing
|
|
|
|
}
|
|
|
|
m[id] = c
|
|
|
|
rescan[id] = struct{}{}
|
2017-07-14 18:59:31 +00:00
|
|
|
cr.mu.Unlock()
|
2017-06-30 22:54:51 +00:00
|
|
|
}
|
|
|
|
cm.mu.Unlock()
|
|
|
|
|
|
|
|
for {
|
|
|
|
if len(rescan) == 0 {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
for id := range rescan {
|
2017-07-01 01:09:29 +00:00
|
|
|
v := m[id]
|
|
|
|
if v.refs == 0 && v.parent != "" {
|
|
|
|
m[v.parent].refs--
|
|
|
|
rescan[v.parent] = struct{}{}
|
2017-06-30 22:54:51 +00:00
|
|
|
}
|
|
|
|
delete(rescan, id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var du []*client.UsageInfo
|
|
|
|
for id, cr := range m {
|
2017-07-25 22:14:46 +00:00
|
|
|
if opt.Filter != "" && !strings.HasPrefix(id, opt.Filter) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-06-08 18:17:44 +00:00
|
|
|
c := &client.UsageInfo{
|
2017-07-25 19:11:52 +00:00
|
|
|
ID: id,
|
|
|
|
Mutable: cr.mutable,
|
|
|
|
InUse: cr.refs > 0,
|
|
|
|
Size: cr.size,
|
|
|
|
Parent: cr.parent,
|
|
|
|
CreatedAt: cr.createdAt,
|
|
|
|
Description: cr.description,
|
|
|
|
LastUsedAt: cr.lastUsedAt,
|
|
|
|
UsageCount: cr.usageCount,
|
2017-05-31 23:45:04 +00:00
|
|
|
}
|
2017-05-27 06:12:13 +00:00
|
|
|
du = append(du, c)
|
|
|
|
}
|
2017-05-31 23:45:04 +00:00
|
|
|
|
|
|
|
eg, ctx := errgroup.WithContext(ctx)
|
|
|
|
|
|
|
|
for _, d := range du {
|
|
|
|
if d.Size == sizeUnknown {
|
2017-06-08 18:17:44 +00:00
|
|
|
func(d *client.UsageInfo) {
|
2017-05-31 23:45:04 +00:00
|
|
|
eg.Go(func() error {
|
2017-07-19 23:39:32 +00:00
|
|
|
ref, err := cm.Get(ctx, d.ID)
|
|
|
|
if err != nil {
|
|
|
|
d.Size = 0
|
|
|
|
return nil
|
2017-05-31 23:45:04 +00:00
|
|
|
}
|
2017-07-19 23:39:32 +00:00
|
|
|
s, err := ref.Size(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Size = s
|
|
|
|
return ref.Release(context.TODO())
|
2017-05-31 23:45:04 +00:00
|
|
|
})
|
|
|
|
}(d)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := eg.Wait(); err != nil {
|
|
|
|
return du, err
|
|
|
|
}
|
2017-05-27 06:12:13 +00:00
|
|
|
|
|
|
|
return du, nil
|
|
|
|
}
|
2017-07-07 21:35:10 +00:00
|
|
|
|
|
|
|
func IsLocked(err error) bool {
|
|
|
|
return errors.Cause(err) == errLocked
|
|
|
|
}
|
2017-07-19 23:39:32 +00:00
|
|
|
|
2017-11-11 02:10:10 +00:00
|
|
|
func IsNotFound(err error) bool {
|
|
|
|
return errors.Cause(err) == errNotFound
|
|
|
|
}
|
|
|
|
|
2017-07-20 22:55:24 +00:00
|
|
|
type RefOption func(withMetadata) error
|
2017-07-19 23:39:32 +00:00
|
|
|
|
|
|
|
type cachePolicy int
|
|
|
|
|
|
|
|
const (
|
|
|
|
cachePolicyDefault cachePolicy = iota
|
2017-07-20 22:55:24 +00:00
|
|
|
cachePolicyRetain
|
2017-07-19 23:39:32 +00:00
|
|
|
)
|
|
|
|
|
2017-07-20 22:55:24 +00:00
|
|
|
type withMetadata interface {
|
|
|
|
Metadata() *metadata.StorageItem
|
|
|
|
}
|
|
|
|
|
2017-08-10 01:20:33 +00:00
|
|
|
func HasCachePolicyRetain(m withMetadata) bool {
|
|
|
|
return getCachePolicy(m.Metadata()) == cachePolicyRetain
|
|
|
|
}
|
|
|
|
|
2017-07-20 22:55:24 +00:00
|
|
|
func CachePolicyRetain(m withMetadata) error {
|
2017-07-25 19:11:52 +00:00
|
|
|
return queueCachePolicy(m.Metadata(), cachePolicyRetain)
|
|
|
|
}
|
|
|
|
|
|
|
|
func WithDescription(descr string) RefOption {
|
|
|
|
return func(m withMetadata) error {
|
|
|
|
return queueDescription(m.Metadata(), descr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func initializeMetadata(m withMetadata, opts ...RefOption) error {
|
|
|
|
md := m.Metadata()
|
|
|
|
if tm := getCreatedAt(md); !tm.IsZero() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, opt := range opts {
|
|
|
|
if err := opt(m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := queueCreatedAt(md); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:35 +00:00
|
|
|
return md.Commit()
|
2017-07-19 23:39:32 +00:00
|
|
|
}
|