Skip to content

Commit 8219feb

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

File tree

4 files changed

+54
-20
lines changed

4 files changed

+54
-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: 28 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"
@@ -273,3 +274,30 @@ func TestGetNydusFsVersionOrDefault(t *testing.T) {
273274
fsVersion = GetNydusFsVersionOrDefault(testAnnotations, V5)
274275
require.Equal(t, fsVersion, V5)
275276
}
277+
278+
func TestRetryWithAttempts_SuccessOnFirstAttempt(t *testing.T) {
279+
err := RetryWithAttempts(func() error {
280+
return nil
281+
}, 3)
282+
require.NoError(t, err)
283+
284+
attempts := 0
285+
err = RetryWithAttempts(func() error {
286+
attempts++
287+
if attempts == 1 {
288+
return errors.New("first attempt failed")
289+
}
290+
return nil
291+
}, 3)
292+
require.NoError(t, err)
293+
294+
err = RetryWithAttempts(func() error {
295+
return errors.New("always fails")
296+
}, 3)
297+
require.Error(t, err)
298+
299+
err = RetryWithAttempts(func() error {
300+
return context.Canceled
301+
}, 3)
302+
require.Equal(t, context.Canceled, err)
303+
}

0 commit comments

Comments
 (0)