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-28 07:07:13 +00:00
|
|
|
"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"
|
2018-04-16 22:23:10 +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 {
|
2018-04-16 22:23:10 +00:00
|
|
|
Snapshotter snapshot.SnapshotterBase
|
2017-07-05 02:00:27 +00:00
|
|
|
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)
|
2018-05-10 22:51:00 +00:00
|
|
|
GetFromSnapshotter(ctx context.Context, id string, opts ...RefOption) (ImmutableRef, error)
|
2017-07-25 19:11:52 +00:00
|
|
|
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-12-27 01:22:50 +00:00
|
|
|
Prune(ctx context.Context, ch chan client.UsageInfo) error
|
2017-05-27 06:12:13 +00:00
|
|
|
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-12-28 23:09:07 +00:00
|
|
|
|
|
|
|
muPrune sync.Mutex // make sure parallel prune is not allowed so there will not be inconsistent results
|
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-12-26 19:42:14 +00:00
|
|
|
// init loads all snapshots from metadata state and tries to load the records
|
|
|
|
// from the snapshotter. If snaphot can't be found, metadata is deleted as well.
|
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 {
|
2018-05-10 22:51:00 +00:00
|
|
|
if _, err := cm.getRecord(ctx, si.ID(), false); err != nil {
|
2017-12-26 19:42:14 +00:00
|
|
|
logrus.Debugf("could not load snapshot %s: %v", si.ID(), err)
|
2017-07-03 23:08:20 +00:00
|
|
|
cm.md.Clear(si.ID())
|
2017-12-26 19:42:14 +00:00
|
|
|
// TODO: make sure content is deleted as well
|
2017-07-03 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-27 06:12:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-26 19:42:14 +00:00
|
|
|
// Close closes the manager and releases the metadata database lock. No other
|
|
|
|
// method should be called after Close.
|
2017-05-27 06:12:13 +00:00
|
|
|
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-12-26 19:42:14 +00:00
|
|
|
// Get returns an immutable snapshot reference for ID
|
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()
|
2018-05-10 22:51:00 +00:00
|
|
|
return cm.get(ctx, id, false, opts...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get returns an immutable snapshot reference for ID
|
|
|
|
func (cm *cacheManager) GetFromSnapshotter(ctx context.Context, id string, opts ...RefOption) (ImmutableRef, error) {
|
|
|
|
cm.mu.Lock()
|
|
|
|
defer cm.mu.Unlock()
|
|
|
|
return cm.get(ctx, id, true, opts...)
|
2017-05-30 21:17:04 +00:00
|
|
|
}
|
2017-07-10 20:03:38 +00:00
|
|
|
|
2017-12-26 19:42:14 +00:00
|
|
|
// get requires manager lock to be taken
|
2018-05-10 22:51:00 +00:00
|
|
|
func (cm *cacheManager) get(ctx context.Context, id string, fromSnapshotter bool, opts ...RefOption) (ImmutableRef, error) {
|
|
|
|
rec, err := cm.getRecord(ctx, id, fromSnapshotter, 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-12-26 19:42:14 +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-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-12-26 19:42:14 +00:00
|
|
|
// getRecord returns record for id. Requires manager lock.
|
2018-05-10 22:51:00 +00:00
|
|
|
func (cm *cacheManager) getRecord(ctx context.Context, id string, fromSnapshotter bool, opts ...RefOption) (cr *cacheRecord, retErr error) {
|
2017-12-26 19:42:14 +00:00
|
|
|
if rec, ok := cm.records[id]; ok {
|
|
|
|
if rec.isDead() {
|
|
|
|
return nil, errNotFound
|
|
|
|
}
|
2017-07-03 23:08:20 +00:00
|
|
|
return rec, nil
|
|
|
|
}
|
|
|
|
|
2018-05-10 22:51:00 +00:00
|
|
|
md, ok := cm.md.Get(id)
|
|
|
|
if !ok && !fromSnapshotter {
|
|
|
|
return nil, errNotFound
|
|
|
|
}
|
2017-08-10 01:20:33 +00:00
|
|
|
if mutableID := getEqualMutable(md); mutableID != "" {
|
2018-05-10 22:51:00 +00:00
|
|
|
mutable, err := cm.getRecord(ctx, mutableID, fromSnapshotter)
|
2017-07-14 18:59:31 +00:00
|
|
|
if err != nil {
|
2017-12-27 01:22:50 +00:00
|
|
|
// check loading mutable deleted record from disk
|
|
|
|
if errors.Cause(err) == errNotFound {
|
|
|
|
cm.md.Clear(id)
|
|
|
|
}
|
2017-07-14 18:59:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
rec := &cacheRecord{
|
2017-12-26 19:42:14 +00:00
|
|
|
mu: &sync.Mutex{},
|
2017-07-14 18:59:31 +00:00
|
|
|
cm: cm,
|
|
|
|
refs: make(map[Mountable]struct{}),
|
2017-12-27 01:22:50 +00:00
|
|
|
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 != "" {
|
2018-05-10 22:51:00 +00:00
|
|
|
parent, err = cm.get(ctx, info.Parent, fromSnapshotter, opts...)
|
2017-07-03 23:08:20 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-12-27 01:22:50 +00:00
|
|
|
defer func() {
|
|
|
|
if retErr != nil {
|
|
|
|
parent.Release(context.TODO())
|
|
|
|
}
|
|
|
|
}()
|
2017-07-03 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rec := &cacheRecord{
|
2017-12-26 19:42:14 +00:00
|
|
|
mu: &sync.Mutex{},
|
2017-12-28 07:07:13 +00:00
|
|
|
mutable: info.Kind != snapshots.KindCommitted,
|
2017-07-03 23:08:20 +00:00
|
|
|
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
|
|
|
|
2017-12-27 01:22:50 +00:00
|
|
|
// the record was deleted but we crashed before data on disk was removed
|
|
|
|
if getDeleted(md) {
|
|
|
|
if err := rec.remove(ctx, true); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nil, errNotFound
|
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|
|
|
|
|
2018-04-16 22:23:10 +00:00
|
|
|
if err := cm.Snapshotter.Prepare(ctx, id, parentID); 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{
|
2017-12-26 19:42:14 +00:00
|
|
|
mu: &sync.Mutex{},
|
2017-05-27 06:12:13 +00:00
|
|
|
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()
|
|
|
|
|
2018-05-10 22:51:00 +00:00
|
|
|
rec, err := cm.getRecord(ctx, id, false)
|
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()
|
|
|
|
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-12-27 01:22:50 +00:00
|
|
|
func (cm *cacheManager) Prune(ctx context.Context, ch chan client.UsageInfo) error {
|
2017-12-28 23:09:07 +00:00
|
|
|
cm.muPrune.Lock()
|
|
|
|
defer cm.muPrune.Unlock()
|
|
|
|
return cm.prune(ctx, ch)
|
|
|
|
}
|
2017-12-27 01:22:50 +00:00
|
|
|
|
2017-12-28 23:09:07 +00:00
|
|
|
func (cm *cacheManager) prune(ctx context.Context, ch chan client.UsageInfo) error {
|
2017-12-27 01:22:50 +00:00
|
|
|
var toDelete []*cacheRecord
|
|
|
|
cm.mu.Lock()
|
|
|
|
|
|
|
|
for _, cr := range cm.records {
|
|
|
|
cr.mu.Lock()
|
2017-12-28 07:07:13 +00:00
|
|
|
|
2017-12-27 01:22:50 +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-12-28 07:07:13 +00:00
|
|
|
if cr.isDead() {
|
|
|
|
cr.mu.Unlock()
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-12-27 01:22:50 +00:00
|
|
|
if len(cr.refs) == 0 {
|
|
|
|
cr.dead = true
|
|
|
|
toDelete = append(toDelete, cr)
|
|
|
|
}
|
|
|
|
|
|
|
|
// mark metadata as deleted in case we crash before cleanup finished
|
|
|
|
if err := setDeleted(cr.md); err != nil {
|
|
|
|
cr.mu.Unlock()
|
|
|
|
cm.mu.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
cr.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
cm.mu.Unlock()
|
|
|
|
|
|
|
|
if len(toDelete) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var err error
|
|
|
|
for _, cr := range toDelete {
|
|
|
|
cr.mu.Lock()
|
|
|
|
|
|
|
|
usageCount, lastUsedAt := getLastUsed(cr.md)
|
|
|
|
|
|
|
|
c := client.UsageInfo{
|
|
|
|
ID: cr.ID(),
|
|
|
|
Mutable: cr.mutable,
|
|
|
|
InUse: len(cr.refs) > 0,
|
|
|
|
Size: getSize(cr.md),
|
2018-05-04 20:29:04 +00:00
|
|
|
CreatedAt: GetCreatedAt(cr.md),
|
2017-12-27 01:22:50 +00:00
|
|
|
Description: GetDescription(cr.md),
|
|
|
|
LastUsedAt: lastUsedAt,
|
|
|
|
UsageCount: usageCount,
|
|
|
|
}
|
|
|
|
|
|
|
|
if cr.parent != nil {
|
|
|
|
c.Parent = cr.parent.ID()
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.Size == sizeUnknown {
|
|
|
|
cr.mu.Unlock() // all the non-prune modifications already protected by cr.dead
|
|
|
|
s, err := cr.Size(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
c.Size = s
|
|
|
|
cr.mu.Lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
if cr.equalImmutable != nil {
|
|
|
|
if err1 := cr.equalImmutable.remove(ctx, false); err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if err1 := cr.remove(ctx, true); err == nil {
|
|
|
|
err = err1
|
|
|
|
}
|
|
|
|
|
|
|
|
if err == nil && ch != nil {
|
|
|
|
ch <- c
|
|
|
|
}
|
|
|
|
cr.mu.Unlock()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
2017-12-28 23:09:07 +00:00
|
|
|
return cm.prune(ctx, ch)
|
2017-12-27 01:22:50 +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-12-27 01:22:50 +00:00
|
|
|
doubleRef bool
|
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),
|
2018-05-04 20:29:04 +00:00
|
|
|
createdAt: GetCreatedAt(cr.md),
|
2017-07-25 19:11:52 +00:00
|
|
|
usageCount: usageCount,
|
|
|
|
lastUsedAt: lastUsedAt,
|
2017-12-08 18:53:17 +00:00
|
|
|
description: GetDescription(cr.md),
|
2017-12-27 01:22:50 +00:00
|
|
|
doubleRef: cr.equalImmutable != nil,
|
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--
|
2017-12-27 01:22:50 +00:00
|
|
|
if v.doubleRef {
|
|
|
|
m[v.parent].refs--
|
|
|
|
}
|
2017-07-01 01:09:29 +00:00
|
|
|
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()
|
2018-05-04 20:29:04 +00:00
|
|
|
if tm := GetCreatedAt(md); !tm.IsZero() {
|
2017-07-25 19:11:52 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-04 20:29:04 +00:00
|
|
|
if err := queueCreatedAt(md, time.Now()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-07-25 19:11:52 +00:00
|
|
|
for _, opt := range opts {
|
|
|
|
if err := opt(m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:35 +00:00
|
|
|
return md.Commit()
|
2017-07-19 23:39:32 +00:00
|
|
|
}
|