2017-05-27 06:12:13 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sync"
|
|
|
|
|
|
|
|
"github.com/boltdb/bolt"
|
2017-05-30 21:17:04 +00:00
|
|
|
cdsnapshot "github.com/containerd/containerd/snapshot"
|
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
|
|
|
)
|
|
|
|
|
|
|
|
const dbFile = "cache.db"
|
|
|
|
|
|
|
|
var (
|
|
|
|
errLocked = errors.New("locked")
|
|
|
|
errNotFound = errors.New("not found")
|
|
|
|
errInvalid = errors.New("invalid")
|
|
|
|
)
|
|
|
|
|
|
|
|
type ManagerOpt struct {
|
|
|
|
Snapshotter snapshot.Snapshotter
|
|
|
|
Root string
|
|
|
|
GCPolicy GCPolicy
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
db *bolt.DB // note: no particual reason for bolt
|
|
|
|
records map[string]*cacheRecord
|
|
|
|
mu sync.Mutex
|
|
|
|
ManagerOpt
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewManager(opt ManagerOpt) (Manager, error) {
|
|
|
|
if err := os.MkdirAll(opt.Root, 0700); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to create %s", opt.Root)
|
|
|
|
}
|
|
|
|
|
|
|
|
p := filepath.Join(opt.Root, dbFile)
|
|
|
|
db, err := bolt.Open(p, 0600, nil)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to open database file %s", p)
|
|
|
|
}
|
|
|
|
|
|
|
|
cm := &cacheManager{
|
|
|
|
ManagerOpt: opt,
|
|
|
|
db: db,
|
|
|
|
records: make(map[string]*cacheRecord),
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := cm.init(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// cm.scheduleGC(5 * time.Minute)
|
|
|
|
|
|
|
|
return cm, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *cacheManager) init() error {
|
|
|
|
// load all refs from db
|
|
|
|
// compare with the walk from Snapshotter
|
|
|
|
// delete items that are not in db (or implement broken transaction detection)
|
|
|
|
// keep all refs in memory(maybe in future work on disk only or with lru)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *cacheManager) Close() error {
|
|
|
|
// TODO: allocate internal context and cancel it here
|
|
|
|
return cm.db.Close()
|
|
|
|
}
|
|
|
|
|
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-05-27 06:12:13 +00:00
|
|
|
rec, ok := cm.records[id]
|
|
|
|
if !ok {
|
2017-06-13 23:01:47 +00:00
|
|
|
info, err := cm.Snapshotter.Stat(ctx, id)
|
2017-05-30 21:17:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if info.Kind != cdsnapshot.KindCommitted {
|
|
|
|
return nil, errors.Wrapf(errInvalid, "can't lazy load active %s", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
var parent ImmutableRef
|
|
|
|
if info.Parent != "" {
|
2017-06-13 23:01:47 +00:00
|
|
|
parent, err = cm.get(ctx, info.Parent)
|
2017-05-30 21:17:04 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rec = &cacheRecord{
|
|
|
|
id: id,
|
|
|
|
cm: cm,
|
2017-06-01 22:24:23 +00:00
|
|
|
refs: make(map[Mountable]struct{}),
|
2017-05-30 21:17:04 +00:00
|
|
|
parent: parent,
|
2017-05-31 23:45:04 +00:00
|
|
|
size: sizeUnknown,
|
2017-05-30 21:17:04 +00:00
|
|
|
}
|
|
|
|
cm.records[id] = rec // TODO: store to db
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rec.mu.Lock()
|
|
|
|
defer rec.mu.Unlock()
|
|
|
|
|
|
|
|
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-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)
|
|
|
|
}
|
|
|
|
|
|
|
|
rec := &cacheRecord{
|
|
|
|
mutable: true,
|
|
|
|
id: id,
|
|
|
|
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-05-31 23:45:04 +00:00
|
|
|
size: sizeUnknown,
|
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()
|
|
|
|
|
|
|
|
rec, ok := cm.records[id]
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Wrapf(errNotFound, "%s not found", id)
|
|
|
|
}
|
|
|
|
|
|
|
|
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,
|
|
|
|
size: cr.size,
|
|
|
|
}
|
|
|
|
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
|
|
|
|
}
|