Skip to content

Commit

Permalink
cache: fix cache set on layer conversion
Browse files Browse the repository at this point in the history
Should set the cache item when layer conversion finish.

Signed-off-by: Yan Song <yansong.ys@antgroup.com>
  • Loading branch information
imeoer committed Oct 13, 2023
1 parent fa78e22 commit 4a42d92
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
12 changes: 11 additions & 1 deletion pkg/content/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ func GetFromContext(ctx context.Context, dgst digest.Digest) (*RemoteCache, *oci
return nil, nil
}

func SetFromContext(ctx context.Context, dgst digest.Digest, labels map[string]string) (*RemoteCache, *ocispec.Descriptor) {
func UpdateFromContext(ctx context.Context, dgst digest.Digest, labels map[string]string) (*RemoteCache, *ocispec.Descriptor) {
rc, ok := ctx.Value(cacheKey{}).(*RemoteCache)
if ok {
if item := rc.getBySource(dgst); item != nil {
Expand All @@ -96,6 +96,13 @@ func SetFromContext(ctx context.Context, dgst digest.Digest, labels map[string]s
return nil, nil
}

func SetFromContext(ctx context.Context, source, target ocispec.Descriptor) {
rc, ok := ctx.Value(cacheKey{}).(*RemoteCache)
if ok {
rc.Set(source, target)
}
}

func NewRemoteCache(ctx context.Context, cacheSize int, cacheRef string, host remote.HostFunc) (context.Context, *RemoteCache) {
cache := &RemoteCache{
cache: make(map[digest.Digest]*CacheItem),
Expand Down Expand Up @@ -221,6 +228,9 @@ func (rc *RemoteCache) Fetch(ctx context.Context, pvd Provider, platformMC platf
for _, manifest := range targetManifests {
for _, targetDesc := range manifest.Layers {
sourceDigest := digest.Digest(targetDesc.Annotations[nydusify.LayerAnnotationNydusSourceDigest])
if sourceDigest.Validate() != nil {
continue
}
reader, sourceDesc, err := fetcher.(remotes.FetcherByDigest).FetchByDigest(ctx, sourceDigest)
if err != nil {
return nil, err
Expand Down
4 changes: 2 additions & 2 deletions pkg/content/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (content *Content) Update(ctx context.Context, info ctrcontent.Info, fieldp
}

info, err := content.store.Update(ctx, info, fieldpaths...)
if _, cached := SetFromContext(ctx, info.Digest, info.Labels); cached != nil {
if _, cached := UpdateFromContext(ctx, info.Digest, info.Labels); cached != nil {
return ctrcontent.Info{
Digest: cached.Digest,
Size: cached.Size,
Expand Down Expand Up @@ -318,7 +318,7 @@ func (content *Content) Writer(ctx context.Context, opts ...ctrcontent.WriterOpt
opt(&wopts)
}
if wopts.Desc.Digest != "" {
if _, cached := SetFromContext(ctx, wopts.Desc.Digest, wopts.Desc.Annotations); cached != nil {
if _, cached := UpdateFromContext(ctx, wopts.Desc.Digest, wopts.Desc.Annotations); cached != nil {
return nil, errdefs.ErrAlreadyExists
}
}
Expand Down
9 changes: 8 additions & 1 deletion pkg/driver/nydus/nydus.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,8 +320,15 @@ func (d *Driver) convert(ctx context.Context, provider accelcontent.Provider, so
convertHooks := converter.ConvertHooks{
PostConvertHook: convertHookFunc,
}
convertFunc := func(ctx context.Context, cs content.Store, desc ocispec.Descriptor) (*ocispec.Descriptor, error) {
target, err := nydusify.LayerConvertFunc(packOpt)(ctx, cs, desc)
if err == nil && target != nil {
accelcontent.SetFromContext(ctx, desc, *target)
}
return target, err
}
indexConvertFunc := converter.IndexConvertFuncWithHook(
nydusify.LayerConvertFunc(packOpt),
convertFunc,
d.docker2oci,
d.platformMC,
convertHooks,
Expand Down

0 comments on commit 4a42d92

Please sign in to comment.