Merge pull request #2674 from ktock/variantlease

Fix refs ignoring gc labels of contents
master
Tõnis Tiigi 2022-02-24 21:06:57 -08:00 committed by GitHub
commit b9657e33c6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 6 deletions

View File

@ -1266,10 +1266,6 @@ func TestSharingCompressionVariant(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
defer cleanup() defer cleanup()
ctx, done, err := leaseutil.WithLease(ctx, co.lm, leaseutil.MakeTemporary)
require.NoError(t, err)
defer done(context.TODO())
allCompressions := []compression.Type{compression.Uncompressed, compression.Gzip, compression.Zstd, compression.EStargz} allCompressions := []compression.Type{compression.Uncompressed, compression.Gzip, compression.Zstd, compression.EStargz}
do := func(test func(testCaseSharingCompressionVariant)) { do := func(test func(testCaseSharingCompressionVariant)) {
@ -1382,7 +1378,7 @@ func testSharingCompressionVariant(ctx context.Context, t *testing.T, co *cmOut,
require.NoError(t, err) require.NoError(t, err)
defer aRef.Release(ctx) defer aRef.Release(ctx)
var bDesc ocispecs.Descriptor var bDesc ocispecs.Descriptor
for _, compressionType := range testCase.aVariants { for _, compressionType := range append([]compression.Type{testCase.a}, testCase.aVariants...) {
remotes, err := aRef.GetRemotes(ctx, true, config.RefConfig{Compression: compression.New(compressionType).SetForce(true)}, false, nil) remotes, err := aRef.GetRemotes(ctx, true, config.RefConfig{Compression: compression.New(compressionType).SetForce(true)}, false, nil)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(remotes)) require.Equal(t, 1, len(remotes))
@ -1395,7 +1391,7 @@ func testSharingCompressionVariant(ctx context.Context, t *testing.T, co *cmOut,
bRef, err := cm.GetByBlob(ctx, bDesc, nil, descHandlers) bRef, err := cm.GetByBlob(ctx, bDesc, nil, descHandlers)
require.NoError(t, err) require.NoError(t, err)
defer bRef.Release(ctx) defer bRef.Release(ctx)
for _, compressionType := range testCase.bVariants { for _, compressionType := range append([]compression.Type{testCase.b}, testCase.bVariants...) {
remotes, err := bRef.GetRemotes(ctx, true, config.RefConfig{Compression: compression.New(compressionType).SetForce(true)}, false, nil) remotes, err := bRef.GetRemotes(ctx, true, config.RefConfig{Compression: compression.New(compressionType).SetForce(true)}, false, nil)
require.NoError(t, err) require.NoError(t, err)
require.Equal(t, 1, len(remotes)) require.Equal(t, 1, len(remotes))

22
cache/refs.go vendored
View File

@ -308,6 +308,10 @@ func (cr *cacheRecord) viewLeaseID() string {
return cr.ID() + "-view" return cr.ID() + "-view"
} }
func (cr *cacheRecord) compressionVariantsLeaseID() string {
return cr.ID() + "-variants"
}
func (cr *cacheRecord) viewSnapshotID() string { func (cr *cacheRecord) viewSnapshotID() string {
return cr.getSnapshotID() + "-view" return cr.getSnapshotID() + "-view"
} }
@ -438,6 +442,11 @@ func (cr *cacheRecord) remove(ctx context.Context, removeSnapshot bool) error {
}); err != nil && !errdefs.IsNotFound(err) { }); err != nil && !errdefs.IsNotFound(err) {
return errors.Wrapf(err, "failed to delete lease for %s", cr.ID()) return errors.Wrapf(err, "failed to delete lease for %s", cr.ID())
} }
if err := cr.cm.LeaseManager.Delete(ctx, leases.Lease{
ID: cr.compressionVariantsLeaseID(),
}); err != nil && !errdefs.IsNotFound(err) {
return errors.Wrapf(err, "failed to delete compression variant lease for %s", cr.ID())
}
} }
if err := cr.cm.MetadataStore.Clear(cr.ID()); err != nil { if err := cr.cm.MetadataStore.Clear(cr.ID()); err != nil {
return errors.Wrapf(err, "failed to delete metadata of %s", cr.ID()) return errors.Wrapf(err, "failed to delete metadata of %s", cr.ID())
@ -699,6 +708,19 @@ const (
// this ref. This doesn't record the blob to the cache record (i.e. the passed blob can't // this ref. This doesn't record the blob to the cache record (i.e. the passed blob can't
// be acquired through getBlob). Use setBlob for that purpose. // be acquired through getBlob). Use setBlob for that purpose.
func (sr *immutableRef) linkBlob(ctx context.Context, desc ocispecs.Descriptor) error { func (sr *immutableRef) linkBlob(ctx context.Context, desc ocispecs.Descriptor) error {
if _, err := sr.cm.LeaseManager.Create(ctx, func(l *leases.Lease) error {
l.ID = sr.compressionVariantsLeaseID()
// do not make it flat lease to allow linking blobs using gc label
return nil
}); err != nil && !errdefs.IsAlreadyExists(err) {
return err
}
if err := sr.cm.LeaseManager.AddResource(ctx, leases.Lease{ID: sr.compressionVariantsLeaseID()}, leases.Resource{
ID: desc.Digest.String(),
Type: "content",
}); err != nil {
return err
}
cs := sr.cm.ContentStore cs := sr.cm.ContentStore
blobDigest := sr.getBlob() blobDigest := sr.getBlob()
info, err := cs.Info(ctx, blobDigest) info, err := cs.Info(ctx, blobDigest)