Skip to content

Commit d50b4d4

Browse files
authored
Merge pull request #2603 from ktock/blobgc
cache: Allow sharing compression variants among refs
2 parents b4617dc + 4280dfd commit d50b4d4

File tree

5 files changed

+597
-146
lines changed

5 files changed

+597
-146
lines changed

cache/blobs.go

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -241,7 +241,7 @@ func computeBlobChain(ctx context.Context, sr *immutableRef, createIfNeeded bool
241241
return nil, errors.Errorf("unknown layer compression type")
242242
}
243243

244-
if err := sr.setBlob(ctx, comp.Type, desc); err != nil {
244+
if err := sr.setBlob(ctx, desc); err != nil {
245245
return nil, err
246246
}
247247
return nil, nil
@@ -267,10 +267,15 @@ func computeBlobChain(ctx context.Context, sr *immutableRef, createIfNeeded bool
267267

268268
// setBlob associates a blob with the cache record.
269269
// A lease must be held for the blob when calling this function
270-
func (sr *immutableRef) setBlob(ctx context.Context, compressionType compression.Type, desc ocispecs.Descriptor) error {
270+
func (sr *immutableRef) setBlob(ctx context.Context, desc ocispecs.Descriptor) (rerr error) {
271271
if _, ok := leases.FromContext(ctx); !ok {
272272
return errors.Errorf("missing lease requirement for setBlob")
273273
}
274+
defer func() {
275+
if rerr == nil {
276+
rerr = sr.linkBlob(ctx, desc)
277+
}
278+
}()
274279

275280
diffID, err := diffIDFromDescriptor(desc)
276281
if err != nil {
@@ -280,10 +285,6 @@ func (sr *immutableRef) setBlob(ctx context.Context, compressionType compression
280285
return err
281286
}
282287

283-
if compressionType == compression.UnknownCompression {
284-
return errors.Errorf("unhandled layer media type: %q", desc.MediaType)
285-
}
286-
287288
sr.mu.Lock()
288289
defer sr.mu.Unlock()
289290

@@ -311,9 +312,6 @@ func (sr *immutableRef) setBlob(ctx context.Context, compressionType compression
311312
return err
312313
}
313314

314-
if err := sr.addCompressionBlob(ctx, desc, compressionType); err != nil {
315-
return err
316-
}
317315
return nil
318316
}
319317

@@ -437,11 +435,11 @@ func ensureCompression(ctx context.Context, ref *immutableRef, comp compression.
437435
// This ref can be used as the specified compressionType. Keep it lazy.
438436
return nil, nil
439437
}
440-
return nil, ref.addCompressionBlob(ctx, desc, comp.Type)
438+
return nil, ref.linkBlob(ctx, desc)
441439
}
442440

443441
// First, lookup local content store
444-
if _, err := ref.getCompressionBlob(ctx, comp.Type); err == nil {
442+
if _, err := ref.getBlobWithCompression(ctx, comp.Type); err == nil {
445443
return nil, nil // found the compression variant. no need to convert.
446444
}
447445

@@ -460,7 +458,7 @@ func ensureCompression(ctx context.Context, ref *immutableRef, comp compression.
460458
}
461459

462460
// Start to track converted layer
463-
if err := ref.addCompressionBlob(ctx, *newDesc, comp.Type); err != nil {
461+
if err := ref.linkBlob(ctx, *newDesc); err != nil {
464462
return nil, errors.Wrapf(err, "failed to add compression blob")
465463
}
466464
return nil, nil

cache/converter.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/containerd/containerd/images/converter"
1515
"github.com/containerd/containerd/labels"
1616
"github.com/moby/buildkit/identity"
17+
"github.com/moby/buildkit/util/bklog"
1718
"github.com/moby/buildkit/util/compression"
1819
digest "github.com/opencontainers/go-digest"
1920
ocispecs "github.com/opencontainers/image-spec/specs-go/v1"
@@ -129,6 +130,7 @@ var bufioPool = sync.Pool{
129130
}
130131

131132
func (c *conversion) convert(ctx context.Context, cs content.Store, desc ocispecs.Descriptor) (*ocispecs.Descriptor, error) {
133+
bklog.G(ctx).WithField("blob", desc).WithField("target", c.target).Debugf("converting blob to the target compression")
132134
// prepare the source and destination
133135
labelz := make(map[string]string)
134136
ref := fmt.Sprintf("convert-from-%s-to-%s-%s", desc.Digest, c.target.Type.String(), identity.NewID())

0 commit comments

Comments
 (0)