-
Notifications
You must be signed in to change notification settings - Fork 385
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
blobinfocache: track compression types for locations
Extend the blob info cache to also cache the name of the type of compression used on a blob that we've seen, or specific values that indicate that we know the blob was not compressed, or that we don't know whether or not it was compressed. New methods for adding known blob-compression pairs and reading candidate locations including compression information are part of a new internal BlobInfoCache2 interface which the library's BlobInfoCache implementors also implement. When we copy a blob, try to record the state of compression for the source blob, and if we applied any changes, the blob we produced. Make sure that when TryReusingBlob successfully uses a blob from the blob info cache, that it provides compression information in the BlobInfo that it returns, so that manifests can be updated to describe layers using the correct MIME types. When attempting to write a manifest, if a manifest can't be written because layers were compressed using an algorithm which can't be expressed using that manifest type, continue on to trying other manifest formats. Signed-off-by: Nalin Dahyabhai <[email protected]>
- Loading branch information
Showing
21 changed files
with
469 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package blobinfocache | ||
|
||
import ( | ||
"github.com/containers/image/v5/pkg/compression" | ||
"github.com/containers/image/v5/types" | ||
digest "github.com/opencontainers/go-digest" | ||
) | ||
|
||
// FromBlobInfoCache returns a BlobInfoCache2 based on a BlobInfoCache, returning the original | ||
// object if it implements BlobInfoCache2, or a wrapper which discards compression information | ||
// if it only implements BlobInfoCache. | ||
func FromBlobInfoCache(bic types.BlobInfoCache) BlobInfoCache2 { | ||
if bic2, ok := bic.(BlobInfoCache2); ok { | ||
return bic2 | ||
} | ||
return &wrappedBlobInfoCache{ | ||
BlobInfoCache: bic, | ||
} | ||
} | ||
|
||
type wrappedBlobInfoCache struct { | ||
types.BlobInfoCache | ||
} | ||
|
||
func (bic *wrappedBlobInfoCache) RecordDigestCompressorName(anyDigest digest.Digest, compressorName string) { | ||
} | ||
|
||
func (bic *wrappedBlobInfoCache) CandidateLocations2(transport types.ImageTransport, scope types.BICTransportScope, digest digest.Digest, canSubstitute bool) []BICReplacementCandidate2 { | ||
oldCandidates := bic.CandidateLocations(transport, scope, digest, canSubstitute) | ||
results := make([]BICReplacementCandidate2, 0, len(oldCandidates)) | ||
for _, c := range oldCandidates { | ||
results = append(results, BICReplacementCandidate2{ | ||
Digest: c.Digest, | ||
Location: c.Location, | ||
CompressorName: UnknownCompression, | ||
}) | ||
} | ||
return results | ||
} | ||
|
||
// CandidateLocationsFromV2 converts a slice of BICReplacementCandidate2 to a slice of | ||
// types.BICReplacementCandidate, dropping compression information. | ||
func CandidateLocationsFromV2(v2candidates []BICReplacementCandidate2) []types.BICReplacementCandidate { | ||
candidates := make([]types.BICReplacementCandidate, 0, len(v2candidates)) | ||
for _, c := range v2candidates { | ||
candidates = append(candidates, types.BICReplacementCandidate{ | ||
Digest: c.Digest, | ||
Location: c.Location, | ||
}) | ||
} | ||
return candidates | ||
} | ||
|
||
// OperationAndAlgorithmForCompressor returns CompressionOperation and CompressionAlgorithm | ||
// values suitable for inclusion in a types.BlobInfo structure, based on the name of the | ||
// compression algorithm, or Uncompressed, or UnknownCompression. This is typically used by | ||
// TryReusingBlob() implementations to set values in the BlobInfo structure that they return | ||
// upon success. | ||
func OperationAndAlgorithmForCompressor(compressorName string) (types.LayerCompression, *compression.Algorithm, error) { | ||
switch compressorName { | ||
case Uncompressed: | ||
return types.Decompress, nil, nil | ||
case UnknownCompression: | ||
return types.PreserveOriginal, nil, nil | ||
default: | ||
algo, err := compression.AlgorithmByName(compressorName) | ||
if err == nil { | ||
return types.Compress, &algo, nil | ||
} | ||
return types.PreserveOriginal, nil, err | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package blobinfocache | ||
|
||
import ( | ||
"github.com/containers/image/v5/types" | ||
digest "github.com/opencontainers/go-digest" | ||
) | ||
|
||
const ( | ||
// Uncompressed is the value we store in a blob info cache to indicate that we know that the blob in | ||
// the corresponding location is not compressed. | ||
Uncompressed = "uncompressed" | ||
// UnknownCompression s the value we store in a blob info cache to indicate that we don't know if the | ||
// blob in the corresponding location is compressed or not. | ||
UnknownCompression = "unknown" | ||
) | ||
|
||
// BlobInfoCache2 extends BlobInfoCache by adding the ability to track information about what kind | ||
// of compression was applied to the blobs it keeps information about. | ||
type BlobInfoCache2 interface { | ||
types.BlobInfoCache | ||
blobInfoCache2WithoutV1 | ||
} | ||
|
||
// BICReplacementCandidate2 is an item returned by BlobInfoCache2.CandidateLocations2. | ||
type BICReplacementCandidate2 struct { | ||
Digest digest.Digest | ||
CompressorName string // either the Name() of a known pkg/compression.Algorithm, or Uncompressed or UnknownCompression | ||
Location types.BICLocationReference | ||
} | ||
|
||
// BlobInfoCache2WithoutV1 contains extended versions of the BlobInfoCache methods which add the | ||
// ability to track information about what kind of compression was applied to the blobs it keeps | ||
// information about. | ||
type blobInfoCache2WithoutV1 interface { | ||
// RecordDigestCompressorName records a compressor for the blob with the specified digest, | ||
// or Uncompressed or UnknownCompression. | ||
RecordDigestCompressorName(anyDigest digest.Digest, compressorName string) | ||
// CandidateLocations2 returns a prioritized, limited, number of blobs and their locations | ||
// that could possibly be reused within the specified (transport scope) (if they still | ||
// exist, which is not guaranteed). | ||
// | ||
// If !canSubstitute, the returned cadidates will match the submitted digest exactly; if | ||
// canSubstitute, data from previous RecordDigestUncompressedPair calls is used to also look | ||
// up variants of the blob which have the same uncompressed digest. | ||
CandidateLocations2(transport types.ImageTransport, scope types.BICTransportScope, digest digest.Digest, canSubstitute bool) []BICReplacementCandidate2 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.