Skip to content

Commit

Permalink
feat: handle different plantform in cache remote
Browse files Browse the repository at this point in the history
We can use manifestIndex to record different remote cache on different plantforms. And conversion will use cache which matches current plantfotm.

Signed-off-by: breezeTuT <y_q_email@163.com>
  • Loading branch information
PerseidMeteor committed Sep 14, 2023
1 parent fe66f09 commit fb3320e
Show file tree
Hide file tree
Showing 6 changed files with 267 additions and 107 deletions.
3 changes: 0 additions & 3 deletions pkg/adapter/adapter.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,9 +120,6 @@ func (adp *LocalAdapter) Convert(ctx context.Context, source string) error {
return nil
}
}
if err = adp.content.NewRemoteCache(cacheRef); err != nil {
return err
}
adp.content.GcMutex.RLock()
defer adp.content.GcMutex.RUnlock()
if _, err = adp.cvt.Convert(ctx, source, target, cacheRef); err != nil {
Expand Down
4 changes: 4 additions & 0 deletions pkg/content/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ func (content *Content) Info(ctx context.Context, dgst digest.Digest) (ctrconten
return info, err
}

// Here we use the Update method to update or add a cache layer to the content store. When the remote cache is nil,
// updating to the content will return that the error NotFound, notifying that there is no cache implemented here.
func (content *Content) Update(ctx context.Context, info ctrcontent.Info, fieldpaths ...string) (ctrcontent.Info, error) {
if content.remoteCache != nil {
sourceDesc, ok := info.Labels[nydusutils.LayerAnnotationNydusSourceDigest]
Expand All @@ -300,6 +302,8 @@ func (content *Content) Update(ctx context.Context, info ctrcontent.Info, fieldp
return info, nil
}
}
// containerd content store write labels to annotate some blobs belong to a same repo,
// cleaning labels is needed by GC
if info.Labels != nil {
info.Labels = nil
}
Expand Down
15 changes: 10 additions & 5 deletions pkg/content/local.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ type LocalProvider struct {
content *Content
hosts remote.HostFunc
platformMC platforms.MatchComparer
cacheRef string
}

func NewLocalProvider(cfg *config.Config, platformMC platforms.MatchComparer) (Provider, *Content, error) {
Expand Down Expand Up @@ -130,14 +129,20 @@ func (pvd *LocalProvider) getImage(ref string) (*ocispec.Descriptor, error) {
return nil, errdefs.ErrNotFound
}

func (pvd *LocalProvider) SetCacheRef(ref string) {
func (pvd *LocalProvider) ClearCache(ref string) error {
pvd.mutex.Lock()
defer pvd.mutex.Unlock()
pvd.cacheRef = ref
if err := pvd.content.NewRemoteCache(ref); err != nil {
return errors.Wrap(err, "create new remote cache")
}
return nil
}

func (pvd *LocalProvider) GetCacheRef() string {
func (pvd *LocalProvider) GetCacheInfo() (string, int) {
pvd.mutex.Lock()
defer pvd.mutex.Unlock()
return pvd.cacheRef
if pvd.content.remoteCache != nil {
return pvd.content.remoteCache.cacheRef, pvd.content.remoteCache.cacheSize
}
return "", 0
}
8 changes: 4 additions & 4 deletions pkg/content/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ type Provider interface {
Image(ctx context.Context, ref string) (*ocispec.Descriptor, error)
// ContentStore gets the content store object of containerd.
ContentStore() content.Store
// SetCacheRef sets the cache reference of the source image.
SetCacheRef(ref string)
// GetCacheRef gets the cache reference of the source image.
GetCacheRef() string
// ClearCache clear the cache in content store and set new cache reference.
ClearCache(ref string) error
// GetCacheInfo gets the cache reference of the source image and cache size.
GetCacheInfo() (string, int)
}
4 changes: 3 additions & 1 deletion pkg/converter/converter.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ func (cvt *Converter) Convert(ctx context.Context, source, target, cacheRef stri

logger.Infof("converting image %s", source)
start = time.Now()
cvt.provider.SetCacheRef(cacheRef)
if err := cvt.provider.ClearCache(cacheRef); err != nil {
return nil, errors.Wrap(err, "clear cache")
}
desc, err := cvt.driver.Convert(ctx, cvt.provider, source)
if err != nil {
return nil, errors.Wrap(err, "convert image")
Expand Down
Loading

0 comments on commit fb3320e

Please sign in to comment.