Skip to content

Commit

Permalink
feat:add cached info∙ add cached layer info in a conversion.eg. conve…
Browse files Browse the repository at this point in the history
…rted image xxx(cached 10/13)

Signed-off-by: YuQiang <y_q_email@163.com>
  • Loading branch information
PerseidMeteor committed Oct 20, 2023
1 parent 7d9909a commit c825642
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 2 deletions.
21 changes: 21 additions & 0 deletions pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type RemoteCache struct {
records map[digest.Digest]*Item
// size is the cache record capacity of target layers.
size int
// updateCount records the number of new caches added in one conversion.
updateCount int
}

func New(ctx context.Context, ref string, size int, pvd Provider) (context.Context, *RemoteCache) {
Expand Down Expand Up @@ -157,6 +159,9 @@ func Get(ctx context.Context, dgst digest.Digest) (*RemoteCache, *ocispec.Descri
func Set(ctx context.Context, source, target ocispec.Descriptor) {
rc, ok := ctx.Value(cacheKey{}).(*RemoteCache)
if ok {
if rc.get(source.Digest) == nil {
rc.updateCount++
}
rc.set(source, target)
}
}
Expand Down Expand Up @@ -456,3 +461,19 @@ func appendLayers(orgDescs, newDescs []ocispec.Descriptor, size int) []ocispec.D
}
return mergedLayers
}

// HitCount returns the hit count of cache layers in a conversion.
func (rc *RemoteCache) HitCount(ctx context.Context, desc ocispec.Descriptor) (string, error) {
manifest := ocispec.Manifest{}
_, err := utils.ReadJSON(ctx, rc.provider.ContentStore(), &manifest, desc)
if err != nil {
return "", err
}
hitCount := 0
for _, sourceLayer := range manifest.Layers {
if item := rc.get(sourceLayer.Digest); item != nil {
hitCount++
}
}
return fmt.Sprintf("%d/%d", (hitCount - rc.updateCount), len(manifest.Layers)), nil
}
28 changes: 26 additions & 2 deletions pkg/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,19 @@ func (cvt *Converter) Convert(ctx context.Context, source, target, cacheRef stri
if err := metric.SetSourceImageSize(ctx, cvt, source); err != nil {
return nil, errors.Wrap(err, "get source image size")
}
logger.Infof("pulled image %s, elapse %s", source, metric.SourcePullElapsed)
cacheHit := ""
if cache != nil {
sourceImage, err := cvt.provider.Image(ctx, source)
if err != nil {
return nil, errors.Wrap(err, "get source image")
}
cacheHitInfo, err := cache.HitCount(ctx, *sourceImage)
if err != nil {
logger.Warnf("failed to get cache hit count: %s", err.Error())
}
cacheHit = fmt.Sprintf("(cached %s)", cacheHitInfo)
}
logger.Infof("pulled image %s%s, elapse %s", source, cacheHit, metric.SourcePullElapsed)

logger.Infof("converting image %s", source)
start = time.Now()
Expand All @@ -158,7 +170,19 @@ func (cvt *Converter) Convert(ctx context.Context, source, target, cacheRef stri
if err := metric.SetTargetImageSize(ctx, cvt, desc); err != nil {
return nil, errors.Wrap(err, "get target image size")
}
logger.Infof("converted image %s, elapse %s", target, metric.ConversionElapsed)
cacheHit = ""
if cache != nil {
sourceImage, err := cvt.provider.Image(ctx, source)
if err != nil {
return nil, errors.Wrap(err, "get source image")
}
cacheHitCount, err := cache.HitCount(ctx, *sourceImage)
if err != nil {
logger.Warnf("failed to get cache hit count: %s", err.Error())
}
cacheHit = fmt.Sprintf("(cached %s)", cacheHitCount)
}
logger.Infof("converted image %s%s, elapse %s", target, cacheHit, metric.ConversionElapsed)

if cache != nil {
sourceImage, err := cvt.provider.Image(ctx, source)
Expand Down

0 comments on commit c825642

Please sign in to comment.