Skip to content

Commit e4f91c2

Browse files
committed
copy: remove max number of ErrResets
If a writer continually asks to be reset then it should always succeed - it should be the responsibility of the underlying content.Writer to stop producing ErrReset after some amount of time and to instead return the underlying issue - which pushWriter already does today, using the doWithRetries function. doWithRetries already has a separate cap for retries of 6 requests (5 retries after the original failure), and it seems like this would be previously overridden by content.Copy's max number of 5 attempts, hiding the original error. Signed-off-by: Justin Chadwell <[email protected]>
1 parent 651cfa2 commit e4f91c2

File tree

2 files changed

+37
-14
lines changed

2 files changed

+37
-14
lines changed

core/content/helpers.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ import (
3131
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
3232
)
3333

34-
// maxResets is the no.of times the Copy() method can tolerate a reset of the body
35-
const maxResets = 5
36-
3734
var ErrReset = errors.New("writer has been reset")
3835

3936
var bufPool = sync.Pool{
@@ -149,7 +146,7 @@ func OpenWriter(ctx context.Context, cs Ingester, opts ...WriterOpt) (Writer, er
149146
// Copy is buffered, so no need to wrap reader in buffered io.
150147
func Copy(ctx context.Context, cw Writer, or io.Reader, size int64, expected digest.Digest, opts ...Opt) error {
151148
r := or
152-
for i := 0; i < maxResets; i++ {
149+
for i := 0; ; i++ {
153150
if i >= 1 {
154151
log.G(ctx).WithField("digest", expected).Debugf("retrying copy due to reset")
155152
}
@@ -189,9 +186,6 @@ func Copy(ctx context.Context, cw Writer, or io.Reader, size int64, expected dig
189186
}
190187
return nil
191188
}
192-
193-
log.G(ctx).WithField("digest", expected).Errorf("failed to copy after %d retries", maxResets)
194-
return fmt.Errorf("failed to copy after %d retries", maxResets)
195189
}
196190

197191
// CopyReaderAt copies to a writer from a given reader at for the given

core/content/helpers_test.go

Lines changed: 36 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ import (
2020
"bytes"
2121
"context"
2222
_ "crypto/sha256" // required by go-digest
23-
"fmt"
23+
"errors"
2424
"io"
2525
"strings"
2626
"testing"
@@ -42,7 +42,7 @@ func TestCopy(t *testing.T) {
4242
cf1 := func(buf *bytes.Buffer, st Status) commitFunction {
4343
i := 0
4444
return func() error {
45-
// function resets the first time
45+
// function resets the first time, but then succeeds after
4646
if i == 0 {
4747
// this is the case where, the pipewriter to which the data was being written has
4848
// changed. which means we need to clear the buffer
@@ -55,11 +55,28 @@ func TestCopy(t *testing.T) {
5555
}
5656
}
5757

58+
cf2err := errors.New("commit failed")
5859
cf2 := func(buf *bytes.Buffer, st Status) commitFunction {
5960
i := 0
6061
return func() error {
61-
// function resets for more than the maxReset value
62-
if i < maxResets+1 {
62+
// function resets a lot of times, and eventually fails
63+
if i < 10 {
64+
// this is the case where, the pipewriter to which the data was being written has
65+
// changed. which means we need to clear the buffer
66+
i++
67+
buf.Reset()
68+
st.Offset = 0
69+
return ErrReset
70+
}
71+
return cf2err
72+
}
73+
}
74+
75+
cf3 := func(buf *bytes.Buffer, st Status) commitFunction {
76+
i := 0
77+
return func() error {
78+
// function resets a lot of times, and eventually succeeds
79+
if i < 10 {
6380
// this is the case where, the pipewriter to which the data was being written has
6481
// changed. which means we need to clear the buffer
6582
i++
@@ -73,8 +90,10 @@ func TestCopy(t *testing.T) {
7390

7491
s1 := Status{}
7592
s2 := Status{}
93+
s3 := Status{}
7694
b1 := bytes.Buffer{}
7795
b2 := bytes.Buffer{}
96+
b3 := bytes.Buffer{}
7897

7998
var testcases = []struct {
8099
name string
@@ -130,15 +149,25 @@ func TestCopy(t *testing.T) {
130149
expected: "content to copy",
131150
},
132151
{
133-
name: "write fails more than maxReset times due to reset",
152+
name: "write fails after lots of resets",
134153
source: newCopySource("content to copy"),
135154
writer: fakeWriter{
136155
Buffer: &b2,
137156
status: s2,
138157
commitFunc: cf2(&b2, s2),
139158
},
140159
expected: "",
141-
expectedErr: fmt.Errorf("failed to copy after %d retries", maxResets),
160+
expectedErr: cf2err,
161+
},
162+
{
163+
name: "write succeeds after lots of resets",
164+
source: newCopySource("content to copy"),
165+
writer: fakeWriter{
166+
Buffer: &b3,
167+
status: s3,
168+
commitFunc: cf3(&b3, s3),
169+
},
170+
expected: "content to copy",
142171
},
143172
}
144173

@@ -153,7 +182,7 @@ func TestCopy(t *testing.T) {
153182

154183
// if an error is expected then further comparisons are not required
155184
if testcase.expectedErr != nil {
156-
assert.Equal(t, testcase.expectedErr, err)
185+
assert.ErrorIs(t, err, testcase.expectedErr)
157186
return
158187
}
159188

0 commit comments

Comments
 (0)