From bf7032c14d6d061ab9dd3627b33773d918ab6f2d Mon Sep 17 00:00:00 2001 From: breezeTuT Date: Tue, 15 Aug 2023 11:32:22 +0800 Subject: [PATCH] feat: prevent unnecessary conversions If content store provides remote cache, we can use remote cache to avoid unnecessary conversions. The layer converted using cache is the same as the layer converted normally, both containing two annotations: LayerAnnotationUncompressed, LayerAnnotationNydusBlob. Also add two constant LayerAnnotations labels. Signed-off-by: breezeTuT --- pkg/converter/constant.go | 2 ++ pkg/converter/convert_unix.go | 24 +++++++++++++++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/pkg/converter/constant.go b/pkg/converter/constant.go index e23f3122df..8fd2c401e9 100644 --- a/pkg/converter/constant.go +++ b/pkg/converter/constant.go @@ -20,6 +20,8 @@ const ( LayerAnnotationNydusBootstrap = "containerd.io/snapshot/nydus-bootstrap" LayerAnnotationNydusSourceChainID = "containerd.io/snapshot/nydus-source-chainid" LayerAnnotationNydusEncryptedBlob = "containerd.io/snapshot/nydus-encrypted-blob" + LayerAnnotationNydusSourceDigest = "containerd.io/snapshot/nydus-source-digest" + LayerAnnotationNydusTargetDigest = "containerd.io/snapshot/nydus-target-digest" LayerAnnotationNydusReferenceBlobIDs = "containerd.io/snapshot/nydus-reference-blob-ids" diff --git a/pkg/converter/convert_unix.go b/pkg/converter/convert_unix.go index 3b26b7131d..8d2a01ccab 100644 --- a/pkg/converter/convert_unix.go +++ b/pkg/converter/convert_unix.go @@ -771,6 +771,28 @@ func LayerConvertFunc(opt PackOption) converter.ConvertFunc { return nil, nil } + // Use remote cache to avoid unnecessary conversion + info, err := cs.Info(ctx, desc.Digest) + if err != nil { + return nil, errors.Wrapf(err, "get blob info %s", desc.Digest) + } + if info.Labels[LayerAnnotationNydusTargetDigest] != "" { + targetInfo, err := cs.Info(ctx, digest.Digest(info.Labels[LayerAnnotationNydusTargetDigest])) + if err != nil { + return nil, errors.Wrapf(err, "get blob info %s", desc.Digest) + } + targetDesc := ocispec.Descriptor{ + Digest: targetInfo.Digest, + Size: targetInfo.Size, + MediaType: MediaTypeNydusBlob, + Annotations: map[string]string{ + LayerAnnotationUncompressed: targetInfo.Digest.String(), + LayerAnnotationNydusBlob: "true", + }, + } + return &targetDesc, nil + } + ra, err := cs.ReaderAt(ctx, desc) if err != nil { return nil, errors.Wrap(err, "get source blob reader") @@ -825,7 +847,7 @@ func LayerConvertFunc(opt PackOption) converter.ConvertFunc { } blobDigest := digester.Digest() - info, err := cs.Info(ctx, blobDigest) + info, err = cs.Info(ctx, blobDigest) if err != nil { return nil, errors.Wrapf(err, "get blob info %s", blobDigest) }