Skip to content

Commit 19e9d12

Browse files
committed
nydusify(refactor): handle layer with retry
Signed-off-by: Yang Kaiyong <[email protected]>
1 parent c288169 commit 19e9d12

File tree

4 files changed

+55
-20
lines changed

4 files changed

+55
-20
lines changed

contrib/nydusify/pkg/copier/copier.go

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -67,23 +67,6 @@ type output struct {
6767
Blobs []string
6868
}
6969

70-
func withRetry(handle func() error, total int) error {
71-
for {
72-
total--
73-
err := handle()
74-
if err == nil {
75-
return nil
76-
}
77-
78-
if total > 0 && !errors.Is(err, context.Canceled) {
79-
logrus.WithError(err).Warnf("retry (remain %d times)", total)
80-
continue
81-
}
82-
83-
return err
84-
}
85-
}
86-
8770
func hosts(opt Opt) remote.HostFunc {
8871
maps := map[string]bool{
8972
opt.Source: opt.SourceInsecure,
@@ -189,7 +172,7 @@ func pushBlobFromBackend(
189172
},
190173
}
191174

192-
if err := withRetry(func() error {
175+
if err := nydusifyUtils.RetryWithAttempts(func() error {
193176
pusher, err := getPusherInChunked(ctx, pvd, blobDescs[idx], opt)
194177
if err != nil {
195178
if errdefs.NeedsRetryWithHTTP(err) {

contrib/nydusify/pkg/external/modctl/remote.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,16 @@ func (handler *RemoteHandler) Handle(ctx context.Context) (*backend.Backend, []b
8888
eg *errgroup.Group
8989
)
9090
eg, ctx = errgroup.WithContext(ctx)
91-
eg.SetLimit(5)
91+
eg.SetLimit(10)
9292

9393
for idx, layer := range handler.manifest.Layers {
9494
eg.Go(func() error {
95-
fa, err := handler.handle(ctx, layer, int32(idx))
95+
var fa []backend.FileAttribute
96+
err := utils.RetryWithAttempts(func() error {
97+
_fa, err := handler.handle(ctx, layer, int32(idx))
98+
fa = _fa
99+
return err
100+
}, 5)
96101
if err != nil {
97102
return err
98103
}

contrib/nydusify/pkg/utils/utils.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ package utils
66

77
import (
88
"archive/tar"
9+
"context"
910
"encoding/json"
1011
"fmt"
1112
"io"
@@ -79,6 +80,23 @@ func WithRetry(op func() error) error {
7980
return err
8081
}
8182

83+
func RetryWithAttempts(handle func() error, attempts int) error {
84+
for {
85+
attempts--
86+
err := handle()
87+
if err == nil {
88+
return nil
89+
}
90+
91+
if attempts > 0 && !errors.Is(err, context.Canceled) {
92+
logrus.WithError(err).Warnf("retry (remain %d times)", attempts)
93+
continue
94+
}
95+
96+
return err
97+
}
98+
}
99+
82100
func RetryWithHTTP(err error) bool {
83101
return err != nil && (errors.Is(err, http.ErrSchemeMismatch) || errors.Is(err, syscall.ECONNREFUSED)) || errdefs.NeedsRetryWithHTTP(err)
84102
}

contrib/nydusify/pkg/utils/utils_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ package utils
88
import (
99
"archive/tar"
1010
"compress/gzip"
11+
"context"
1112
"fmt"
1213
"io"
1314
"net/http"
@@ -19,6 +20,7 @@ import (
1920
"github.com/opencontainers/go-digest"
2021
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
2122
"github.com/pkg/errors"
23+
"github.com/stretchr/testify/assert"
2224
"github.com/stretchr/testify/require"
2325
)
2426

@@ -273,3 +275,30 @@ func TestGetNydusFsVersionOrDefault(t *testing.T) {
273275
fsVersion = GetNydusFsVersionOrDefault(testAnnotations, V5)
274276
require.Equal(t, fsVersion, V5)
275277
}
278+
279+
func TestRetryWithAttempts_SuccessOnFirstAttempt(t *testing.T) {
280+
err := RetryWithAttempts(func() error {
281+
return nil
282+
}, 3)
283+
assert.NoError(t, err)
284+
285+
attempts := 0
286+
err = RetryWithAttempts(func() error {
287+
attempts++
288+
if attempts == 1 {
289+
return errors.New("first attempt failed")
290+
}
291+
return nil
292+
}, 3)
293+
assert.NoError(t, err)
294+
295+
err = RetryWithAttempts(func() error {
296+
return errors.New("always fails")
297+
}, 3)
298+
assert.Error(t, err)
299+
300+
err = RetryWithAttempts(func() error {
301+
return context.Canceled
302+
}, 3)
303+
assert.Equal(t, context.Canceled, err)
304+
}

0 commit comments

Comments
 (0)