Skip to content

Commit

Permalink
fix: use proxy when copying node with successors (#384)
Browse files Browse the repository at this point in the history
Resolves #383

Signed-off-by: Billy Zha <jinzha1@microsoft.com>
  • Loading branch information
qweeah authored Dec 12, 2022
1 parent 79e13bc commit 4b1d016
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 25 deletions.
46 changes: 22 additions & 24 deletions copy.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,38 +219,36 @@ func copyGraph(ctx context.Context, src content.ReadOnlyStorage, dst content.Sto
}
successors = removeForeignLayers(successors)

// handle leaf nodes
if len(successors) == 0 {
exists, err = proxy.Cache.Exists(ctx, desc)
if err != nil {
if len(successors) != 0 {
// for non-leaf nodes, process successors and wait for them to complete
region.End()
if err := syncutil.Go(ctx, limiter, fn, successors...); err != nil {
return err
}
if exists {
return copyNode(ctx, proxy.Cache, dst, desc, opts)
for _, node := range successors {
done, committed := tracker.TryCommit(node)
if committed {
return fmt.Errorf("%s: %s: successor not committed", desc.Digest, node.Digest)
}
select {
case <-done:
case <-ctx.Done():
return ctx.Err()
}
}
if err := region.Start(); err != nil {
return err
}
return copyNode(ctx, src, dst, desc, opts)
}

// for non-leaf nodes, process successors and wait for them to complete
region.End()
if err := syncutil.Go(ctx, limiter, fn, successors...); err != nil {
exists, err = proxy.Cache.Exists(ctx, desc)
if err != nil {
return err
}
for _, node := range successors {
done, committed := tracker.TryCommit(node)
if committed {
return fmt.Errorf("%s: %s: successor not committed", desc.Digest, node.Digest)
}
select {
case <-done:
case <-ctx.Done():
return ctx.Err()
}
}
if err := region.Start(); err != nil {
return err
if exists {
return copyNode(ctx, proxy.Cache, dst, desc, opts)
}
return copyNode(ctx, proxy.Cache, dst, desc, opts)
return copyNode(ctx, src, dst, desc, opts)
}

return syncutil.Go(ctx, limiter, fn, root)
Expand Down
15 changes: 14 additions & 1 deletion copy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,7 @@ func TestCopyGraph_FullCopy(t *testing.T) {
}
}

// test copy
// test copy graph
srcTracker := &storageTracker{Storage: src}
dstTracker := &storageTracker{Storage: dst}
root := descs[len(descs)-1]
Expand Down Expand Up @@ -1353,6 +1353,19 @@ func TestCopyGraph_WithOptions(t *testing.T) {
}
}

// test successor descriptors not obtained from src
root = descs[3]
opts = oras.DefaultCopyGraphOptions
opts.FindSuccessors = func(ctx context.Context, fetcher content.Fetcher, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
if content.Equal(desc, root) {
return descs[1:3], nil
}
return content.Successors(ctx, fetcher, desc)
}
if err := oras.CopyGraph(ctx, src, cas.NewMemory(), root, opts); err != nil {
t.Fatalf("CopyGraph() error = %v, wantErr %v", err, false)
}

// test partial copy
var preCopyCount int64
var postCopyCount int64
Expand Down

0 comments on commit 4b1d016

Please sign in to comment.