2017-05-27 06:12:13 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
2017-07-03 23:08:20 +00:00
|
|
|
"github.com/Sirupsen/logrus"
|
2017-05-30 21:17:04 +00:00
|
|
|
cdsnapshot "github.com/containerd/containerd/snapshot"
|
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-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-06-13 23:01:47 +00:00
|
|
|
Get(ctx context.Context, id string) (ImmutableRef, error)
|
|
|
|
New(ctx context.Context, s ImmutableRef) (MutableRef, error)
|
|
|
|
GetMutable(ctx context.Context, id string) (MutableRef, error) // Rebase?
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Controller interface {
|
2017-06-08 18:17:44 +00:00
|
|
|
DiskUsage(ctx context.Context) ([]*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-06-13 23:01:47 +00:00
|
|
|
func (cm *cacheManager) Get(ctx context.Context, id string) (ImmutableRef, error) {
|
2017-05-27 06:12:13 +00:00
|
|
|
cm.mu.Lock()
|
|
|
|
defer cm.mu.Unlock()
|
2017-06-13 23:01:47 +00:00
|
|
|
return cm.get(ctx, id)
|
2017-05-30 21:17:04 +00:00
|
|
|
}
|
2017-06-13 23:01:47 +00:00
|
|
|
func (cm *cacheManager) get(ctx context.Context, id string) (ImmutableRef, error) {
|
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()
|
|
|
|
|
2017-07-03 23:08:20 +00:00
|
|
|
if rec.mutable {
|
|
|
|
return nil, errors.Wrapf(errInvalid, "invalid mutable ref %s", id)
|
|
|
|
}
|
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
if rec.mutable && !rec.frozen {
|
|
|
|
if len(rec.refs) != 0 {
|
|
|
|
return nil, errors.Wrapf(errLocked, "%s is locked", id)
|
|
|
|
} else {
|
|
|
|
rec.frozen = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return rec.ref(), nil
|
|
|
|
}
|
2017-07-03 23:08:20 +00:00
|
|
|
|
|
|
|
func (cm *cacheManager) load(ctx context.Context, id string) (*cacheRecord, error) {
|
|
|
|
if rec, ok := cm.records[id]; ok {
|
|
|
|
return rec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
info, err := cm.Snapshotter.Stat(ctx, id)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(errNotFound, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
var parent ImmutableRef
|
|
|
|
if info.Parent != "" {
|
|
|
|
parent, err = cm.get(ctx, info.Parent)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
md, _ := cm.md.Get(id)
|
|
|
|
|
|
|
|
rec := &cacheRecord{
|
|
|
|
mutable: info.Kind != cdsnapshot.KindCommitted,
|
|
|
|
cm: cm,
|
|
|
|
refs: make(map[Mountable]struct{}),
|
|
|
|
parent: parent,
|
|
|
|
md: &md,
|
|
|
|
}
|
|
|
|
cm.records[id] = rec // TODO: store to db
|
|
|
|
return rec, nil
|
|
|
|
}
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
func (cm *cacheManager) New(ctx context.Context, s ImmutableRef) (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
|
|
|
|
}
|
|
|
|
parentID = parent.ID()
|
|
|
|
}
|
|
|
|
|
2017-06-13 23:01:47 +00:00
|
|
|
if _, err := cm.Snapshotter.Prepare(ctx, id, parentID); err != nil {
|
2017-05-27 06:12:13 +00:00
|
|
|
if parent != nil {
|
2017-06-13 23:01:47 +00:00
|
|
|
parent.Release(ctx)
|
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-07-03 23:08:20 +00:00
|
|
|
md: &md,
|
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-06-13 23:01:47 +00:00
|
|
|
func (cm *cacheManager) GetMutable(ctx context.Context, id string) (MutableRef, error) { // Rebase?
|
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)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rec.frozen || len(rec.refs) != 0 {
|
|
|
|
return nil, errors.Wrapf(errLocked, "%s is locked", id)
|
|
|
|
}
|
|
|
|
|
2017-06-01 22:24:23 +00:00
|
|
|
return rec.mref(), nil
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 18:17:44 +00:00
|
|
|
func (cm *cacheManager) DiskUsage(ctx context.Context) ([]*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 {
|
|
|
|
refs int
|
|
|
|
parent string
|
|
|
|
size int64
|
|
|
|
mutable bool
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-30 22:54:51 +00:00
|
|
|
c := &cacheUsageInfo{
|
|
|
|
refs: len(cr.refs),
|
|
|
|
mutable: cr.mutable,
|
2017-07-03 23:08:20 +00:00
|
|
|
size: getSize(cr.md),
|
2017-06-30 22:54:51 +00:00
|
|
|
}
|
|
|
|
if cr.parent != nil {
|
|
|
|
c.parent = cr.parent.ID()
|
|
|
|
}
|
|
|
|
if cr.mutable && c.refs > 0 && !cr.frozen {
|
|
|
|
c.size = 0 // size can not be determined because it is changing
|
|
|
|
}
|
|
|
|
m[id] = c
|
|
|
|
cr.mu.Unlock()
|
|
|
|
rescan[id] = struct{}{}
|
|
|
|
}
|
|
|
|
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-06-08 18:17:44 +00:00
|
|
|
c := &client.UsageInfo{
|
2017-06-08 00:53:36 +00:00
|
|
|
ID: id,
|
|
|
|
Mutable: cr.mutable,
|
2017-06-30 22:54:51 +00:00
|
|
|
InUse: cr.refs > 0,
|
2017-06-08 00:53:36 +00:00
|
|
|
Size: cr.size,
|
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-06-13 23:01:47 +00:00
|
|
|
ref, err := cm.Get(ctx, d.ID)
|
2017-05-31 23:45:04 +00:00
|
|
|
if err != nil {
|
|
|
|
d.Size = 0
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
s, err := ref.Size(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
d.Size = s
|
2017-07-01 01:09:29 +00:00
|
|
|
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
|
|
|
|
}
|