2017-05-27 06:12:13 +00:00
|
|
|
package cache
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-07-31 20:14:53 +00:00
|
|
|
"sort"
|
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
|
|
|
|
2019-07-22 21:43:16 +00:00
|
|
|
"github.com/containerd/containerd/content"
|
2019-09-18 00:18:32 +00:00
|
|
|
"github.com/containerd/containerd/diff"
|
2020-05-28 20:46:33 +00:00
|
|
|
"github.com/containerd/containerd/errdefs"
|
2018-07-26 00:01:37 +00:00
|
|
|
"github.com/containerd/containerd/filters"
|
2019-07-22 21:43:16 +00:00
|
|
|
"github.com/containerd/containerd/gc"
|
|
|
|
"github.com/containerd/containerd/leases"
|
2019-03-20 06:40:24 +00:00
|
|
|
"github.com/docker/docker/pkg/idtools"
|
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"
|
2020-10-27 06:13:39 +00:00
|
|
|
"github.com/moby/buildkit/session"
|
2018-04-16 22:23:10 +00:00
|
|
|
"github.com/moby/buildkit/snapshot"
|
2020-05-28 20:46:33 +00:00
|
|
|
"github.com/moby/buildkit/util/flightcontrol"
|
2020-05-13 15:37:27 +00:00
|
|
|
digest "github.com/opencontainers/go-digest"
|
2019-10-03 21:11:54 +00:00
|
|
|
imagespecidentity "github.com/opencontainers/image-spec/identity"
|
2019-09-18 00:18:32 +00:00
|
|
|
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
|
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 (
|
2018-06-18 20:57:36 +00:00
|
|
|
ErrLocked = errors.New("locked")
|
2017-05-27 06:12:13 +00:00
|
|
|
errNotFound = errors.New("not found")
|
|
|
|
errInvalid = errors.New("invalid")
|
|
|
|
)
|
|
|
|
|
|
|
|
type ManagerOpt struct {
|
2019-09-18 00:18:32 +00:00
|
|
|
Snapshotter snapshot.Snapshotter
|
2018-07-27 00:53:48 +00:00
|
|
|
MetadataStore *metadata.Store
|
2019-07-22 21:43:16 +00:00
|
|
|
ContentStore content.Store
|
|
|
|
LeaseManager leases.Manager
|
2018-07-27 00:53:48 +00:00
|
|
|
PruneRefChecker ExternalRefCheckerFunc
|
2019-07-22 21:43:16 +00:00
|
|
|
GarbageCollect func(ctx context.Context) (gc.Stats, error)
|
2019-09-18 00:18:32 +00:00
|
|
|
Applier diff.Applier
|
2020-05-28 20:46:33 +00:00
|
|
|
Differ diff.Comparer
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Accessor interface {
|
2019-09-18 00:18:32 +00:00
|
|
|
GetByBlob(ctx context.Context, desc ocispec.Descriptor, parent ImmutableRef, opts ...RefOption) (ImmutableRef, error)
|
2017-07-25 19:11:52 +00:00
|
|
|
Get(ctx context.Context, id string, opts ...RefOption) (ImmutableRef, error)
|
2019-07-22 21:43:16 +00:00
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
New(ctx context.Context, parent ImmutableRef, s session.Group, opts ...RefOption) (MutableRef, error)
|
2020-05-28 20:46:33 +00:00
|
|
|
GetMutable(ctx context.Context, id string, opts ...RefOption) (MutableRef, error) // Rebase?
|
2019-03-20 06:40:24 +00:00
|
|
|
IdentityMapping() *idtools.IdentityMapping
|
2019-07-29 22:41:49 +00:00
|
|
|
Metadata(string) *metadata.StorageItem
|
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)
|
2018-08-30 21:06:27 +00:00
|
|
|
Prune(ctx context.Context, ch chan client.UsageInfo, info ...client.PruneInfo) error
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Manager interface {
|
|
|
|
Accessor
|
|
|
|
Controller
|
|
|
|
Close() error
|
|
|
|
}
|
|
|
|
|
2019-09-20 21:49:29 +00:00
|
|
|
type ExternalRefCheckerFunc func() (ExternalRefChecker, error)
|
2018-07-27 00:53:48 +00:00
|
|
|
|
|
|
|
type ExternalRefChecker interface {
|
2019-09-20 21:49:29 +00:00
|
|
|
Exists(string, []digest.Digest) bool
|
2018-07-27 00:53:48 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
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
|
2020-05-28 20:46:33 +00:00
|
|
|
unlazyG flightcontrol.Group
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
func (cm *cacheManager) GetByBlob(ctx context.Context, desc ocispec.Descriptor, parent ImmutableRef, opts ...RefOption) (ir ImmutableRef, rerr error) {
|
2019-09-18 00:18:32 +00:00
|
|
|
diffID, err := diffIDFromDescriptor(desc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-07-22 21:43:16 +00:00
|
|
|
chainID := diffID
|
2019-10-03 21:11:54 +00:00
|
|
|
blobChainID := imagespecidentity.ChainID([]digest.Digest{desc.Digest, diffID})
|
2019-07-22 21:43:16 +00:00
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
descHandlers := descHandlersOf(opts...)
|
2020-11-12 23:36:16 +00:00
|
|
|
if desc.Digest != "" && (descHandlers == nil || descHandlers[desc.Digest] == nil) {
|
2020-05-28 20:46:33 +00:00
|
|
|
if _, err := cm.ContentStore.Info(ctx, desc.Digest); errors.Is(err, errdefs.ErrNotFound) {
|
|
|
|
return nil, NeedsRemoteProvidersError([]digest.Digest{desc.Digest})
|
|
|
|
} else if err != nil {
|
|
|
|
return nil, err
|
2019-10-22 00:11:22 +00:00
|
|
|
}
|
2019-07-22 21:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var p *immutableRef
|
|
|
|
var parentID string
|
|
|
|
if parent != nil {
|
|
|
|
pInfo := parent.Info()
|
|
|
|
if pInfo.ChainID == "" || pInfo.BlobChainID == "" {
|
2020-01-25 07:51:00 +00:00
|
|
|
return nil, errors.Errorf("failed to get ref by blob on non-addressable parent")
|
2019-07-22 21:43:16 +00:00
|
|
|
}
|
2019-10-03 21:11:54 +00:00
|
|
|
chainID = imagespecidentity.ChainID([]digest.Digest{pInfo.ChainID, chainID})
|
|
|
|
blobChainID = imagespecidentity.ChainID([]digest.Digest{pInfo.BlobChainID, blobChainID})
|
2020-05-28 20:46:33 +00:00
|
|
|
|
|
|
|
p2, err := cm.Get(ctx, parent.ID(), NoUpdateLastUsed, descHandlers)
|
2019-09-18 00:18:32 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-01 02:50:54 +00:00
|
|
|
p = p2.(*immutableRef)
|
|
|
|
if err := p.finalizeLocked(ctx); err != nil {
|
2019-09-18 00:18:32 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2021-07-01 02:50:54 +00:00
|
|
|
parentID = p.ID()
|
2019-07-22 21:43:16 +00:00
|
|
|
}
|
|
|
|
|
2019-09-24 01:02:17 +00:00
|
|
|
releaseParent := false
|
2019-07-22 21:43:16 +00:00
|
|
|
defer func() {
|
2020-05-28 20:46:33 +00:00
|
|
|
if releaseParent || rerr != nil && p != nil {
|
2019-07-22 21:43:16 +00:00
|
|
|
p.Release(context.TODO())
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
cm.mu.Lock()
|
|
|
|
defer cm.mu.Unlock()
|
|
|
|
|
|
|
|
sis, err := cm.MetadataStore.Search("blobchainid:" + blobChainID.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2021-06-08 03:26:05 +00:00
|
|
|
for _, si := range sis {
|
|
|
|
ref, err := cm.get(ctx, si.ID(), opts...)
|
2020-04-19 05:17:47 +00:00
|
|
|
if err != nil && !IsNotFound(err) {
|
2020-07-18 21:52:44 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to get record %s by blobchainid", sis[0].ID())
|
2019-07-22 21:43:16 +00:00
|
|
|
}
|
2021-06-08 03:26:05 +00:00
|
|
|
if ref == nil {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-24 01:02:17 +00:00
|
|
|
if p != nil {
|
|
|
|
releaseParent = true
|
|
|
|
}
|
2020-08-05 23:51:19 +00:00
|
|
|
if err := setImageRefMetadata(ref, opts...); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to append image ref metadata to ref %s", ref.ID())
|
|
|
|
}
|
2019-07-22 21:43:16 +00:00
|
|
|
return ref, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
sis, err = cm.MetadataStore.Search("chainid:" + chainID.String())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var link ImmutableRef
|
2021-06-08 03:26:05 +00:00
|
|
|
for _, si := range sis {
|
|
|
|
ref, err := cm.get(ctx, si.ID(), opts...)
|
2021-07-07 15:56:02 +00:00
|
|
|
// if the error was NotFound or NeedsRemoteProvider, we can't re-use the snapshot from the blob so just skip it
|
|
|
|
if err != nil && !IsNotFound(err) && !errors.As(err, &NeedsRemoteProvidersError{}) {
|
2020-07-18 21:52:44 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to get record %s by chainid", sis[0].ID())
|
2019-07-22 21:43:16 +00:00
|
|
|
}
|
2021-06-08 03:26:05 +00:00
|
|
|
if ref != nil {
|
|
|
|
link = ref
|
|
|
|
break
|
|
|
|
}
|
2019-07-22 21:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
id := identity.NewID()
|
|
|
|
snapshotID := chainID.String()
|
|
|
|
blobOnly := true
|
|
|
|
if link != nil {
|
|
|
|
snapshotID = getSnapshotID(link.Metadata())
|
|
|
|
blobOnly = getBlobOnly(link.Metadata())
|
2019-09-24 01:02:17 +00:00
|
|
|
go link.Release(context.TODO())
|
2019-07-22 21:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
l, err := cm.ManagerOpt.LeaseManager.Create(ctx, func(l *leases.Lease) error {
|
|
|
|
l.ID = id
|
|
|
|
l.Labels = map[string]string{
|
|
|
|
"containerd.io/gc.flat": time.Now().UTC().Format(time.RFC3339Nano),
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create lease")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
2020-05-28 20:46:33 +00:00
|
|
|
if rerr != nil {
|
2019-07-22 21:43:16 +00:00
|
|
|
if err := cm.ManagerOpt.LeaseManager.Delete(context.TODO(), leases.Lease{
|
|
|
|
ID: l.ID,
|
|
|
|
}); err != nil {
|
|
|
|
logrus.Errorf("failed to remove lease: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := cm.ManagerOpt.LeaseManager.AddResource(ctx, l, leases.Resource{
|
2019-09-18 00:18:32 +00:00
|
|
|
ID: snapshotID,
|
|
|
|
Type: "snapshots/" + cm.ManagerOpt.Snapshotter.Name(),
|
2019-07-22 21:43:16 +00:00
|
|
|
}); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to add snapshot %s to lease", id)
|
|
|
|
}
|
|
|
|
|
2019-10-22 00:11:22 +00:00
|
|
|
if desc.Digest != "" {
|
|
|
|
if err := cm.ManagerOpt.LeaseManager.AddResource(ctx, leases.Lease{ID: id}, leases.Resource{
|
|
|
|
ID: desc.Digest.String(),
|
|
|
|
Type: "content",
|
|
|
|
}); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to add blob %s to lease", id)
|
|
|
|
}
|
2019-07-22 21:43:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
md, _ := cm.md.Get(id)
|
|
|
|
|
|
|
|
rec := &cacheRecord{
|
|
|
|
mu: &sync.Mutex{},
|
|
|
|
cm: cm,
|
|
|
|
refs: make(map[ref]struct{}),
|
|
|
|
parent: p,
|
|
|
|
md: md,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := initializeMetadata(rec, parentID, opts...); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-05 23:51:19 +00:00
|
|
|
if err := setImageRefMetadata(rec, opts...); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to append image ref metadata to ref %s", rec.ID())
|
|
|
|
}
|
|
|
|
|
2019-07-22 21:43:16 +00:00
|
|
|
queueDiffID(rec.md, diffID.String())
|
2019-09-18 00:18:32 +00:00
|
|
|
queueBlob(rec.md, desc.Digest.String())
|
2019-07-22 21:43:16 +00:00
|
|
|
queueChainID(rec.md, chainID.String())
|
|
|
|
queueBlobChainID(rec.md, blobChainID.String())
|
|
|
|
queueSnapshotID(rec.md, snapshotID)
|
|
|
|
queueBlobOnly(rec.md, blobOnly)
|
2019-09-18 00:18:32 +00:00
|
|
|
queueMediaType(rec.md, desc.MediaType)
|
2020-05-28 20:46:33 +00:00
|
|
|
queueBlobSize(rec.md, desc.Size)
|
2019-09-18 00:18:32 +00:00
|
|
|
queueCommitted(rec.md)
|
2019-07-22 21:43:16 +00:00
|
|
|
|
|
|
|
if err := rec.md.Commit(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
cm.records[id] = rec
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
return rec.ref(true, descHandlers), nil
|
2019-07-22 21:43:16 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2019-07-22 21:43:16 +00:00
|
|
|
if _, err := cm.getRecord(ctx, si.ID()); err != nil {
|
2019-09-18 00:18:32 +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())
|
2019-09-19 21:55:10 +00:00
|
|
|
cm.LeaseManager.Delete(ctx, leases.Lease{ID: si.ID()})
|
2017-07-03 23:08:20 +00:00
|
|
|
}
|
|
|
|
}
|
2017-05-27 06:12:13 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-03-20 06:40:24 +00:00
|
|
|
// IdentityMapping returns the userns remapping used for refs
|
|
|
|
func (cm *cacheManager) IdentityMapping() *idtools.IdentityMapping {
|
|
|
|
return cm.Snapshotter.IdentityMapping()
|
|
|
|
}
|
|
|
|
|
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()
|
2019-07-22 21:43:16 +00:00
|
|
|
return cm.get(ctx, id, opts...)
|
2018-05-10 22:51:00 +00:00
|
|
|
}
|
|
|
|
|
2019-07-29 22:41:49 +00:00
|
|
|
func (cm *cacheManager) Metadata(id string) *metadata.StorageItem {
|
|
|
|
cm.mu.Lock()
|
|
|
|
defer cm.mu.Unlock()
|
|
|
|
r, ok := cm.records[id]
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return r.Metadata()
|
|
|
|
}
|
|
|
|
|
2017-12-26 19:42:14 +00:00
|
|
|
// get requires manager lock to be taken
|
2019-07-22 21:43:16 +00:00
|
|
|
func (cm *cacheManager) get(ctx context.Context, id string, opts ...RefOption) (*immutableRef, error) {
|
|
|
|
rec, err := cm.getRecord(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()
|
|
|
|
|
2018-09-14 20:35:41 +00:00
|
|
|
triggerUpdate := true
|
|
|
|
for _, o := range opts {
|
|
|
|
if o == NoUpdateLastUsed {
|
|
|
|
triggerUpdate = false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
descHandlers := descHandlersOf(opts...)
|
|
|
|
|
2017-07-03 23:08:20 +00:00
|
|
|
if rec.mutable {
|
2017-12-26 19:42:14 +00:00
|
|
|
if len(rec.refs) != 0 {
|
2018-06-18 20:57:36 +00:00
|
|
|
return nil, errors.Wrapf(ErrLocked, "%s is locked", id)
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
2017-07-20 22:55:24 +00:00
|
|
|
if rec.equalImmutable != nil {
|
2020-05-28 20:46:33 +00:00
|
|
|
return rec.equalImmutable.ref(triggerUpdate, descHandlers), nil
|
2017-07-20 22:55:24 +00:00
|
|
|
}
|
2020-05-28 20:46:33 +00:00
|
|
|
return rec.mref(triggerUpdate, descHandlers).commit(ctx)
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
return rec.ref(triggerUpdate, descHandlers), nil
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
2017-07-03 23:08:20 +00:00
|
|
|
|
2017-12-26 19:42:14 +00:00
|
|
|
// getRecord returns record for id. Requires manager lock.
|
2019-07-22 21:43:16 +00:00
|
|
|
func (cm *cacheManager) getRecord(ctx context.Context, id string, opts ...RefOption) (cr *cacheRecord, retErr error) {
|
2020-05-28 20:46:33 +00:00
|
|
|
checkLazyProviders := func(rec *cacheRecord) error {
|
|
|
|
missing := NeedsRemoteProvidersError(nil)
|
|
|
|
dhs := descHandlersOf(opts...)
|
|
|
|
for {
|
|
|
|
blob := digest.Digest(getBlob(rec.md))
|
|
|
|
if isLazy, err := rec.isLazy(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
} else if isLazy && dhs[blob] == nil {
|
|
|
|
missing = append(missing, blob)
|
|
|
|
}
|
|
|
|
|
|
|
|
if rec.parent == nil {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
rec = rec.parent.cacheRecord
|
|
|
|
}
|
|
|
|
if len(missing) > 0 {
|
|
|
|
return missing
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-26 19:42:14 +00:00
|
|
|
if rec, ok := cm.records[id]; ok {
|
|
|
|
if rec.isDead() {
|
2019-06-01 23:34:02 +00:00
|
|
|
return nil, errors.Wrapf(errNotFound, "failed to get dead record %s", id)
|
2017-12-26 19:42:14 +00:00
|
|
|
}
|
2020-05-28 20:46:33 +00:00
|
|
|
if err := checkLazyProviders(rec); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
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)
|
2019-07-22 21:43:16 +00:00
|
|
|
if !ok {
|
2021-03-29 11:17:06 +00:00
|
|
|
return nil, errors.Wrap(errNotFound, id)
|
2018-05-10 22:51:00 +00:00
|
|
|
}
|
2017-08-10 01:20:33 +00:00
|
|
|
if mutableID := getEqualMutable(md); mutableID != "" {
|
2019-07-22 21:43:16 +00:00
|
|
|
mutable, err := cm.getRecord(ctx, mutableID)
|
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
|
2020-04-19 05:17:47 +00:00
|
|
|
if IsNotFound(err) {
|
2017-12-27 01:22:50 +00:00
|
|
|
cm.md.Clear(id)
|
|
|
|
}
|
2017-07-14 18:59:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-08-17 09:34:33 +00:00
|
|
|
|
|
|
|
// parent refs are possibly lazy so keep it hold the description handlers.
|
|
|
|
var dhs DescHandlers
|
|
|
|
if mutable.parent != nil {
|
|
|
|
dhs = mutable.parent.descHandlers
|
|
|
|
}
|
2017-07-14 18:59:31 +00:00
|
|
|
rec := &cacheRecord{
|
2020-08-17 09:34:33 +00:00
|
|
|
mu: &sync.Mutex{},
|
|
|
|
cm: cm,
|
|
|
|
refs: make(map[ref]struct{}),
|
|
|
|
parent: mutable.parentRef(false, dhs),
|
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
|
|
|
|
}
|
|
|
|
|
2019-05-25 01:37:24 +00:00
|
|
|
var parent *immutableRef
|
2019-09-18 00:18:32 +00:00
|
|
|
if parentID := getParent(md); parentID != "" {
|
|
|
|
var err error
|
|
|
|
parent, err = cm.get(ctx, parentID, append(opts, NoUpdateLastUsed)...)
|
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 {
|
2019-05-25 01:37:24 +00:00
|
|
|
parent.mu.Lock()
|
|
|
|
parent.release(context.TODO())
|
|
|
|
parent.mu.Unlock()
|
2017-12-27 01:22:50 +00:00
|
|
|
}
|
|
|
|
}()
|
2017-07-03 23:08:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
rec := &cacheRecord{
|
2017-12-26 19:42:14 +00:00
|
|
|
mu: &sync.Mutex{},
|
2019-07-22 21:43:16 +00:00
|
|
|
mutable: !getCommitted(md),
|
2017-07-03 23:08:20 +00:00
|
|
|
cm: cm,
|
2018-09-14 20:35:41 +00:00
|
|
|
refs: make(map[ref]struct{}),
|
2017-07-03 23:08:20 +00:00
|
|
|
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
|
|
|
|
}
|
2019-06-01 23:34:02 +00:00
|
|
|
return nil, errors.Wrapf(errNotFound, "failed to get deleted record %s", id)
|
2017-12-27 01:22:50 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 21:43:16 +00:00
|
|
|
if err := initializeMetadata(rec, getParent(md), opts...); err != nil {
|
2017-07-25 19:11:52 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-08-05 23:51:19 +00:00
|
|
|
if err := setImageRefMetadata(rec, opts...); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to append image ref metadata to ref %s", rec.ID())
|
|
|
|
}
|
|
|
|
|
2017-07-14 18:59:31 +00:00
|
|
|
cm.records[id] = rec
|
2020-05-28 20:46:33 +00:00
|
|
|
if err := checkLazyProviders(rec); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-07-03 23:08:20 +00:00
|
|
|
return rec, nil
|
|
|
|
}
|
|
|
|
|
2020-10-27 06:13:39 +00:00
|
|
|
func (cm *cacheManager) New(ctx context.Context, s ImmutableRef, sess session.Group, opts ...RefOption) (mr MutableRef, err error) {
|
2017-06-30 22:54:51 +00:00
|
|
|
id := identity.NewID()
|
2017-05-27 06:12:13 +00:00
|
|
|
|
2019-05-25 01:37:24 +00:00
|
|
|
var parent *immutableRef
|
2017-05-27 06:12:13 +00:00
|
|
|
var parentID string
|
2019-09-18 00:18:32 +00:00
|
|
|
var parentSnapshotID string
|
2017-05-27 06:12:13 +00:00
|
|
|
if s != nil {
|
2020-05-28 20:46:33 +00:00
|
|
|
if _, ok := s.(*immutableRef); ok {
|
|
|
|
parent = s.Clone().(*immutableRef)
|
|
|
|
} else {
|
|
|
|
p, err := cm.Get(ctx, s.ID(), append(opts, NoUpdateLastUsed)...)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
parent = p.(*immutableRef)
|
|
|
|
}
|
2021-07-01 02:50:54 +00:00
|
|
|
if err := parent.finalizeLocked(ctx); err != nil {
|
2017-05-27 06:12:13 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-10-27 06:13:39 +00:00
|
|
|
if err := parent.Extract(ctx, sess); err != nil {
|
2017-07-14 18:59:31 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-09-18 00:18:32 +00:00
|
|
|
parentSnapshotID = getSnapshotID(parent.md)
|
|
|
|
parentID = parent.ID()
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2019-07-22 21:43:16 +00:00
|
|
|
defer func() {
|
|
|
|
if err != nil && parent != nil {
|
2017-09-22 17:30:30 +00:00
|
|
|
parent.Release(context.TODO())
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
2019-07-22 21:43:16 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
l, err := cm.ManagerOpt.LeaseManager.Create(ctx, func(l *leases.Lease) error {
|
|
|
|
l.ID = id
|
|
|
|
l.Labels = map[string]string{
|
|
|
|
"containerd.io/gc.flat": time.Now().UTC().Format(time.RFC3339Nano),
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrap(err, "failed to create lease")
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err != nil {
|
|
|
|
if err := cm.ManagerOpt.LeaseManager.Delete(context.TODO(), leases.Lease{
|
|
|
|
ID: l.ID,
|
|
|
|
}); err != nil {
|
|
|
|
logrus.Errorf("failed to remove lease: %+v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
if err := cm.ManagerOpt.LeaseManager.AddResource(ctx, l, leases.Resource{
|
|
|
|
ID: id,
|
|
|
|
Type: "snapshots/" + cm.ManagerOpt.Snapshotter.Name(),
|
|
|
|
}); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to add snapshot %s to lease", id)
|
|
|
|
}
|
2017-05-27 06:12:13 +00:00
|
|
|
|
2020-10-11 12:56:00 +00:00
|
|
|
if cm.Snapshotter.Name() == "stargz" && parent != nil {
|
|
|
|
if rerr := parent.withRemoteSnapshotLabelsStargzMode(ctx, sess, func() {
|
|
|
|
err = cm.Snapshotter.Prepare(ctx, id, parentSnapshotID)
|
|
|
|
}); rerr != nil {
|
|
|
|
return nil, rerr
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
err = cm.Snapshotter.Prepare(ctx, id, parentSnapshotID)
|
|
|
|
}
|
|
|
|
if err != nil {
|
2019-09-24 01:02:17 +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,
|
2018-09-14 20:35:41 +00:00
|
|
|
refs: make(map[ref]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
|
|
|
}
|
|
|
|
|
2019-07-22 21:43:16 +00:00
|
|
|
if err := initializeMetadata(rec, parentID, opts...); err != nil {
|
2017-07-25 19:11:52 +00:00
|
|
|
return nil, err
|
2017-07-19 23:39:32 +00:00
|
|
|
}
|
|
|
|
|
2020-08-05 23:51:19 +00:00
|
|
|
if err := setImageRefMetadata(rec, opts...); err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "failed to append image ref metadata to ref %s", rec.ID())
|
|
|
|
}
|
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
cm.mu.Lock()
|
|
|
|
defer cm.mu.Unlock()
|
|
|
|
|
|
|
|
cm.records[id] = rec // TODO: save to db
|
|
|
|
|
2020-08-17 09:34:33 +00:00
|
|
|
// parent refs are possibly lazy so keep it hold the description handlers.
|
|
|
|
var dhs DescHandlers
|
|
|
|
if parent != nil {
|
|
|
|
dhs = parent.descHandlers
|
|
|
|
}
|
|
|
|
return rec.mref(true, dhs), nil
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
2020-05-28 20:46:33 +00:00
|
|
|
|
|
|
|
func (cm *cacheManager) GetMutable(ctx context.Context, id string, opts ...RefOption) (MutableRef, error) {
|
2017-05-27 06:12:13 +00:00
|
|
|
cm.mu.Lock()
|
|
|
|
defer cm.mu.Unlock()
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
rec, err := cm.getRecord(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()
|
|
|
|
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 {
|
2018-06-18 20:57:36 +00:00
|
|
|
return nil, errors.Wrapf(ErrLocked, "%s is locked", id)
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
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 {
|
2018-06-18 20:57:36 +00:00
|
|
|
return nil, errors.Wrapf(ErrLocked, "%s is locked", id)
|
2017-07-19 23:39:32 +00:00
|
|
|
}
|
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
|
|
|
|
}
|
|
|
|
|
2020-05-28 20:46:33 +00:00
|
|
|
return rec.mref(true, descHandlersOf(opts...)), nil
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 21:06:27 +00:00
|
|
|
func (cm *cacheManager) Prune(ctx context.Context, ch chan client.UsageInfo, opts ...client.PruneInfo) error {
|
2017-12-28 23:09:07 +00:00
|
|
|
cm.muPrune.Lock()
|
2018-07-26 00:20:57 +00:00
|
|
|
|
2018-08-30 21:06:27 +00:00
|
|
|
for _, opt := range opts {
|
|
|
|
if err := cm.pruneOnce(ctx, ch, opt); err != nil {
|
2019-07-22 21:43:16 +00:00
|
|
|
cm.muPrune.Unlock()
|
2018-08-30 21:06:27 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2019-07-22 21:43:16 +00:00
|
|
|
|
|
|
|
cm.muPrune.Unlock()
|
|
|
|
|
2019-09-18 00:18:32 +00:00
|
|
|
if cm.GarbageCollect != nil {
|
|
|
|
if _, err := cm.GarbageCollect(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-07-22 21:43:16 +00:00
|
|
|
}
|
|
|
|
|
2018-08-30 21:06:27 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *cacheManager) pruneOnce(ctx context.Context, ch chan client.UsageInfo, opt client.PruneInfo) error {
|
2018-07-26 00:20:57 +00:00
|
|
|
filter, err := filters.ParseAll(opt.Filter...)
|
|
|
|
if err != nil {
|
2019-06-01 23:34:02 +00:00
|
|
|
return errors.Wrapf(err, "failed to parse prune filters %v", opt.Filter)
|
2018-07-26 00:20:57 +00:00
|
|
|
}
|
|
|
|
|
2018-07-27 00:53:48 +00:00
|
|
|
var check ExternalRefChecker
|
|
|
|
if f := cm.PruneRefChecker; f != nil && (!opt.All || len(opt.Filter) > 0) {
|
2019-09-20 21:49:29 +00:00
|
|
|
c, err := f()
|
2018-07-27 00:53:48 +00:00
|
|
|
if err != nil {
|
2019-06-01 23:34:02 +00:00
|
|
|
return errors.WithStack(err)
|
2018-07-27 00:53:48 +00:00
|
|
|
}
|
|
|
|
check = c
|
|
|
|
}
|
|
|
|
|
2018-07-31 20:14:53 +00:00
|
|
|
totalSize := int64(0)
|
|
|
|
if opt.KeepBytes != 0 {
|
|
|
|
du, err := cm.DiskUsage(ctx, client.DiskUsageInfo{})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
for _, ui := range du {
|
2019-09-20 21:49:29 +00:00
|
|
|
if ui.Shared {
|
|
|
|
continue
|
2018-07-31 20:14:53 +00:00
|
|
|
}
|
|
|
|
totalSize += ui.Size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return cm.prune(ctx, ch, pruneOpt{
|
|
|
|
filter: filter,
|
|
|
|
all: opt.All,
|
|
|
|
checkShared: check,
|
|
|
|
keepDuration: opt.KeepDuration,
|
|
|
|
keepBytes: opt.KeepBytes,
|
|
|
|
totalSize: totalSize,
|
|
|
|
})
|
2017-12-28 23:09:07 +00:00
|
|
|
}
|
2017-12-27 01:22:50 +00:00
|
|
|
|
2018-07-31 20:14:53 +00:00
|
|
|
func (cm *cacheManager) prune(ctx context.Context, ch chan client.UsageInfo, opt pruneOpt) error {
|
|
|
|
var toDelete []*deleteRecord
|
|
|
|
|
|
|
|
if opt.keepBytes != 0 && opt.totalSize < opt.keepBytes {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-12-27 01:22:50 +00:00
|
|
|
cm.mu.Lock()
|
|
|
|
|
2018-07-31 20:14:53 +00:00
|
|
|
gcMode := opt.keepBytes != 0
|
|
|
|
cutOff := time.Now().Add(-opt.keepDuration)
|
|
|
|
|
2018-08-31 18:59:53 +00:00
|
|
|
locked := map[*sync.Mutex]struct{}{}
|
2018-07-31 20:14:53 +00:00
|
|
|
|
2017-12-27 01:22:50 +00:00
|
|
|
for _, cr := range cm.records {
|
2018-08-31 18:59:53 +00:00
|
|
|
if _, ok := locked[cr.mu]; ok {
|
2018-07-31 20:14:53 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-12-27 01:22:50 +00:00
|
|
|
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 {
|
2018-07-26 20:51:28 +00:00
|
|
|
recordType := GetRecordType(cr)
|
|
|
|
if recordType == "" {
|
|
|
|
recordType = client.UsageRecordTypeRegular
|
|
|
|
}
|
|
|
|
|
2018-07-27 00:53:48 +00:00
|
|
|
shared := false
|
2018-07-31 20:14:53 +00:00
|
|
|
if opt.checkShared != nil {
|
2019-09-20 21:49:29 +00:00
|
|
|
shared = opt.checkShared.Exists(cr.ID(), cr.parentChain())
|
2018-07-27 00:53:48 +00:00
|
|
|
}
|
|
|
|
|
2018-07-31 20:14:53 +00:00
|
|
|
if !opt.all {
|
2018-07-27 00:53:48 +00:00
|
|
|
if recordType == client.UsageRecordTypeInternal || recordType == client.UsageRecordTypeFrontend || shared {
|
|
|
|
cr.mu.Unlock()
|
|
|
|
continue
|
|
|
|
}
|
2018-07-26 22:31:35 +00:00
|
|
|
}
|
|
|
|
|
2018-07-26 00:20:57 +00:00
|
|
|
c := &client.UsageInfo{
|
2018-07-26 20:51:28 +00:00
|
|
|
ID: cr.ID(),
|
|
|
|
Mutable: cr.mutable,
|
|
|
|
RecordType: recordType,
|
2018-07-27 00:53:48 +00:00
|
|
|
Shared: shared,
|
2018-07-26 00:20:57 +00:00
|
|
|
}
|
2017-12-27 01:22:50 +00:00
|
|
|
|
2018-07-31 20:14:53 +00:00
|
|
|
usageCount, lastUsedAt := getLastUsed(cr.md)
|
|
|
|
c.LastUsedAt = lastUsedAt
|
|
|
|
c.UsageCount = usageCount
|
2018-07-26 00:20:57 +00:00
|
|
|
|
2018-07-31 20:14:53 +00:00
|
|
|
if opt.keepDuration != 0 {
|
|
|
|
if lastUsedAt != nil && lastUsedAt.After(cutOff) {
|
2018-07-26 00:20:57 +00:00
|
|
|
cr.mu.Unlock()
|
2018-07-31 20:14:53 +00:00
|
|
|
continue
|
2018-07-26 00:20:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-31 20:14:53 +00:00
|
|
|
if opt.filter.Match(adaptUsageInfo(c)) {
|
|
|
|
toDelete = append(toDelete, &deleteRecord{
|
|
|
|
cacheRecord: cr,
|
|
|
|
lastUsedAt: c.LastUsedAt,
|
|
|
|
usageCount: c.UsageCount,
|
|
|
|
})
|
|
|
|
if !gcMode {
|
|
|
|
cr.dead = true
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
|
|
|
} else {
|
2018-08-31 18:59:53 +00:00
|
|
|
locked[cr.mu] = struct{}{}
|
2018-07-31 20:14:53 +00:00
|
|
|
continue // leave the record locked
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-12-27 01:22:50 +00:00
|
|
|
cr.mu.Unlock()
|
|
|
|
}
|
|
|
|
|
2018-07-31 20:14:53 +00:00
|
|
|
if gcMode && len(toDelete) > 0 {
|
|
|
|
sortDeleteRecords(toDelete)
|
|
|
|
var err error
|
|
|
|
for i, cr := range toDelete {
|
|
|
|
// only remove single record at a time
|
|
|
|
if i == 0 {
|
|
|
|
cr.dead = true
|
|
|
|
err = setDeleted(cr.md)
|
|
|
|
}
|
|
|
|
cr.mu.Unlock()
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
toDelete = toDelete[:1]
|
|
|
|
}
|
|
|
|
|
2017-12-27 01:22:50 +00:00
|
|
|
cm.mu.Unlock()
|
|
|
|
|
|
|
|
if len(toDelete) == 0 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-06-15 02:25:46 +00:00
|
|
|
// calculate sizes here so that lock does not need to be held for slow process
|
|
|
|
for _, cr := range toDelete {
|
|
|
|
size := getSize(cr.md)
|
|
|
|
|
|
|
|
if size == sizeUnknown && cr.equalImmutable != nil {
|
|
|
|
size = getSize(cr.equalImmutable.md) // benefit from DiskUsage calc
|
|
|
|
}
|
|
|
|
if size == sizeUnknown {
|
|
|
|
// calling size will warm cache for next call
|
|
|
|
if _, err := cr.Size(ctx); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
cm.mu.Lock()
|
2017-12-27 01:22:50 +00:00
|
|
|
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()
|
|
|
|
}
|
2018-08-31 19:00:13 +00:00
|
|
|
if c.Size == sizeUnknown && cr.equalImmutable != nil {
|
|
|
|
c.Size = getSize(cr.equalImmutable.md) // benefit from DiskUsage calc
|
|
|
|
}
|
2017-12-27 01:22:50 +00:00
|
|
|
|
2018-08-31 19:00:13 +00:00
|
|
|
opt.totalSize -= c.Size
|
|
|
|
|
2017-12-27 01:22:50 +00:00
|
|
|
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()
|
|
|
|
}
|
2021-06-15 02:25:46 +00:00
|
|
|
cm.mu.Unlock()
|
2017-12-27 01:22:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
return ctx.Err()
|
|
|
|
default:
|
2018-07-31 20:14:53 +00:00
|
|
|
return cm.prune(ctx, ch, opt)
|
2018-07-27 00:53:48 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (cm *cacheManager) markShared(m map[string]*cacheUsageInfo) error {
|
|
|
|
if cm.PruneRefChecker == nil {
|
|
|
|
return nil
|
|
|
|
}
|
2019-09-20 21:49:29 +00:00
|
|
|
c, err := cm.PruneRefChecker()
|
2018-07-27 00:53:48 +00:00
|
|
|
if err != nil {
|
2019-06-01 23:34:02 +00:00
|
|
|
return errors.WithStack(err)
|
2018-07-27 00:53:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var markAllParentsShared func(string)
|
|
|
|
markAllParentsShared = func(id string) {
|
|
|
|
if v, ok := m[id]; ok {
|
|
|
|
v.shared = true
|
|
|
|
if v.parent != "" {
|
|
|
|
markAllParentsShared(v.parent)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for id := range m {
|
|
|
|
if m[id].shared {
|
|
|
|
continue
|
|
|
|
}
|
2019-09-20 21:49:29 +00:00
|
|
|
if b := c.Exists(id, m[id].parentChain); b {
|
2018-07-27 00:53:48 +00:00
|
|
|
markAllParentsShared(id)
|
|
|
|
}
|
2017-12-27 01:22:50 +00:00
|
|
|
}
|
2018-07-27 00:53:48 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type cacheUsageInfo struct {
|
|
|
|
refs int
|
|
|
|
parent string
|
|
|
|
size int64
|
|
|
|
mutable bool
|
|
|
|
createdAt time.Time
|
|
|
|
usageCount int
|
|
|
|
lastUsedAt *time.Time
|
|
|
|
description string
|
|
|
|
doubleRef bool
|
|
|
|
recordType client.UsageRecordType
|
|
|
|
shared bool
|
2019-09-20 21:49:29 +00:00
|
|
|
parentChain []digest.Digest
|
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) {
|
2018-07-26 00:01:37 +00:00
|
|
|
filter, err := filters.ParseAll(opt.Filter...)
|
|
|
|
if err != nil {
|
2019-06-01 23:34:02 +00:00
|
|
|
return nil, errors.Wrapf(err, "failed to parse diskusage filters %v", opt.Filter)
|
2018-07-26 00:01:37 +00:00
|
|
|
}
|
|
|
|
|
2017-05-27 06:12:13 +00:00
|
|
|
cm.mu.Lock()
|
|
|
|
|
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,
|
2018-07-26 19:07:52 +00:00
|
|
|
recordType: GetRecordType(cr),
|
2019-09-20 21:49:29 +00:00
|
|
|
parentChain: cr.parentChain(),
|
2018-07-26 19:07:52 +00:00
|
|
|
}
|
|
|
|
if c.recordType == "" {
|
|
|
|
c.recordType = client.UsageRecordTypeRegular
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-27 00:53:48 +00:00
|
|
|
if err := cm.markShared(m); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-06-30 22:54:51 +00:00
|
|
|
var du []*client.UsageInfo
|
|
|
|
for id, cr := range m {
|
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,
|
2018-07-26 19:07:52 +00:00
|
|
|
RecordType: cr.recordType,
|
2018-07-27 00:53:48 +00:00
|
|
|
Shared: cr.shared,
|
2017-05-31 23:45:04 +00:00
|
|
|
}
|
2018-07-26 00:01:37 +00:00
|
|
|
if filter.Match(adaptUsageInfo(c)) {
|
|
|
|
du = append(du, c)
|
|
|
|
}
|
2017-05-27 06:12:13 +00:00
|
|
|
}
|
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 {
|
2018-09-14 20:35:41 +00:00
|
|
|
ref, err := cm.Get(ctx, d.ID, NoUpdateLastUsed)
|
2017-07-19 23:39:32 +00:00
|
|
|
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
|
|
|
|
2017-11-11 02:10:10 +00:00
|
|
|
func IsNotFound(err error) bool {
|
2020-04-19 05:17:47 +00:00
|
|
|
return errors.Is(err, errNotFound)
|
2017-11-11 02:10:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-14 20:35:41 +00:00
|
|
|
type RefOption interface{}
|
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
|
|
|
|
}
|
|
|
|
|
2018-09-14 20:35:41 +00:00
|
|
|
type noUpdateLastUsed struct{}
|
|
|
|
|
|
|
|
var NoUpdateLastUsed noUpdateLastUsed
|
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2019-05-28 20:55:00 +00:00
|
|
|
func CachePolicyDefault(m withMetadata) error {
|
|
|
|
return queueCachePolicy(m.Metadata(), cachePolicyDefault)
|
|
|
|
}
|
|
|
|
|
2017-07-25 19:11:52 +00:00
|
|
|
func WithDescription(descr string) RefOption {
|
|
|
|
return func(m withMetadata) error {
|
|
|
|
return queueDescription(m.Metadata(), descr)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-26 19:07:52 +00:00
|
|
|
func WithRecordType(t client.UsageRecordType) RefOption {
|
|
|
|
return func(m withMetadata) error {
|
|
|
|
return queueRecordType(m.Metadata(), t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-05 05:18:11 +00:00
|
|
|
func WithCreationTime(tm time.Time) RefOption {
|
|
|
|
return func(m withMetadata) error {
|
|
|
|
return queueCreatedAt(m.Metadata(), tm)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-05 23:51:19 +00:00
|
|
|
// Need a separate type for imageRef because it needs to be called outside
|
|
|
|
// initializeMetadata while still being a RefOption, so wrapping it in a
|
|
|
|
// different type ensures initializeMetadata won't catch it too and duplicate
|
|
|
|
// setting the metadata.
|
|
|
|
type imageRefOption func(m withMetadata) error
|
|
|
|
|
|
|
|
// WithImageRef appends the given imageRef to the cache ref's metadata
|
|
|
|
func WithImageRef(imageRef string) RefOption {
|
|
|
|
return imageRefOption(func(m withMetadata) error {
|
|
|
|
return appendImageRef(m.Metadata(), imageRef)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func setImageRefMetadata(m withMetadata, opts ...RefOption) error {
|
|
|
|
md := m.Metadata()
|
|
|
|
for _, opt := range opts {
|
|
|
|
if fn, ok := opt.(imageRefOption); ok {
|
|
|
|
if err := fn(m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return md.Commit()
|
|
|
|
}
|
|
|
|
|
2019-07-22 21:43:16 +00:00
|
|
|
func initializeMetadata(m withMetadata, parent string, opts ...RefOption) error {
|
2017-07-25 19:11:52 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2019-07-22 21:43:16 +00:00
|
|
|
if err := queueParent(md, parent); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
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 {
|
2018-09-14 20:35:41 +00:00
|
|
|
if fn, ok := opt.(func(withMetadata) error); ok {
|
|
|
|
if err := fn(m); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-07-25 19:11:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-17 02:09:35 +00:00
|
|
|
return md.Commit()
|
2017-07-19 23:39:32 +00:00
|
|
|
}
|
2018-07-26 00:01:37 +00:00
|
|
|
|
|
|
|
func adaptUsageInfo(info *client.UsageInfo) filters.Adaptor {
|
|
|
|
return filters.AdapterFunc(func(fieldpath []string) (string, bool) {
|
|
|
|
if len(fieldpath) == 0 {
|
|
|
|
return "", false
|
|
|
|
}
|
|
|
|
|
|
|
|
switch fieldpath[0] {
|
|
|
|
case "id":
|
|
|
|
return info.ID, info.ID != ""
|
|
|
|
case "parent":
|
|
|
|
return info.Parent, info.Parent != ""
|
|
|
|
case "description":
|
|
|
|
return info.Description, info.Description != ""
|
|
|
|
case "inuse":
|
|
|
|
return "", info.InUse
|
|
|
|
case "mutable":
|
|
|
|
return "", info.Mutable
|
|
|
|
case "immutable":
|
|
|
|
return "", !info.Mutable
|
2018-07-26 19:07:52 +00:00
|
|
|
case "type":
|
|
|
|
return string(info.RecordType), info.RecordType != ""
|
2018-07-27 00:53:48 +00:00
|
|
|
case "shared":
|
|
|
|
return "", info.Shared
|
|
|
|
case "private":
|
|
|
|
return "", !info.Shared
|
2018-07-26 00:01:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: add int/datetime/bytes support for more fields
|
|
|
|
|
|
|
|
return "", false
|
|
|
|
})
|
|
|
|
}
|
2018-07-31 20:14:53 +00:00
|
|
|
|
|
|
|
type pruneOpt struct {
|
|
|
|
filter filters.Filter
|
|
|
|
all bool
|
|
|
|
checkShared ExternalRefChecker
|
|
|
|
keepDuration time.Duration
|
|
|
|
keepBytes int64
|
|
|
|
totalSize int64
|
|
|
|
}
|
|
|
|
|
|
|
|
type deleteRecord struct {
|
|
|
|
*cacheRecord
|
|
|
|
lastUsedAt *time.Time
|
|
|
|
usageCount int
|
|
|
|
lastUsedAtIndex int
|
|
|
|
usageCountIndex int
|
|
|
|
}
|
|
|
|
|
|
|
|
func sortDeleteRecords(toDelete []*deleteRecord) {
|
|
|
|
sort.Slice(toDelete, func(i, j int) bool {
|
|
|
|
if toDelete[i].lastUsedAt == nil {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if toDelete[j].lastUsedAt == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
return toDelete[i].lastUsedAt.Before(*toDelete[j].lastUsedAt)
|
|
|
|
})
|
|
|
|
|
|
|
|
maxLastUsedIndex := 0
|
|
|
|
var val time.Time
|
|
|
|
for _, v := range toDelete {
|
|
|
|
if v.lastUsedAt != nil && v.lastUsedAt.After(val) {
|
|
|
|
val = *v.lastUsedAt
|
|
|
|
maxLastUsedIndex++
|
|
|
|
}
|
|
|
|
v.lastUsedAtIndex = maxLastUsedIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(toDelete, func(i, j int) bool {
|
|
|
|
return toDelete[i].usageCount < toDelete[j].usageCount
|
|
|
|
})
|
|
|
|
|
|
|
|
maxUsageCountIndex := 0
|
|
|
|
var count int
|
|
|
|
for _, v := range toDelete {
|
|
|
|
if v.usageCount != count {
|
|
|
|
count = v.usageCount
|
|
|
|
maxUsageCountIndex++
|
|
|
|
}
|
|
|
|
v.usageCountIndex = maxUsageCountIndex
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(toDelete, func(i, j int) bool {
|
|
|
|
return float64(toDelete[i].lastUsedAtIndex)/float64(maxLastUsedIndex)+
|
|
|
|
float64(toDelete[i].usageCountIndex)/float64(maxUsageCountIndex) <
|
|
|
|
float64(toDelete[j].lastUsedAtIndex)/float64(maxLastUsedIndex)+
|
|
|
|
float64(toDelete[j].usageCountIndex)/float64(maxUsageCountIndex)
|
|
|
|
})
|
|
|
|
}
|
2019-09-18 00:18:32 +00:00
|
|
|
|
|
|
|
func diffIDFromDescriptor(desc ocispec.Descriptor) (digest.Digest, error) {
|
|
|
|
diffIDStr, ok := desc.Annotations["containerd.io/uncompressed"]
|
|
|
|
if !ok {
|
|
|
|
return "", errors.Errorf("missing uncompressed annotation for %s", desc.Digest)
|
|
|
|
}
|
|
|
|
diffID, err := digest.Parse(diffIDStr)
|
|
|
|
if err != nil {
|
|
|
|
return "", errors.Wrapf(err, "failed to parse diffID %q for %s", diffIDStr, desc.Digest)
|
|
|
|
}
|
|
|
|
return diffID, nil
|
|
|
|
}
|