-
Notifications
You must be signed in to change notification settings - Fork 816
Fix S3 BucketWithRetries upload empty content issue #5217
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
alvinlin123
merged 9 commits into
cortexproject:master
from
alexqyle:s3-upload-retry-issue
Mar 20, 2023
Merged
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
4821ba3
Implement grpc.Compressor.DecompressedSize for snappy to optimize mem…
damnever 5a5e662
Fix S3 BucketWithRetries upload empty content issue
alexqyle c0517e2
Update CHANGELOG
alexqyle 0f5980e
Revert "Implement grpc.Compressor.DecompressedSize for snappy to opti…
alexqyle c507d33
Only retry if input reader is seekable
alexqyle f6077b8
Rename mock type
alexqyle 4f742d3
Add logging
alexqyle b47a3f9
nit fixing
alexqyle 437d2b1
add comment
alexqyle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,123 @@ | ||
package s3 | ||
|
||
import ( | ||
"bytes" | ||
"context" | ||
"fmt" | ||
"io" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"github.com/thanos-io/objstore" | ||
) | ||
|
||
func TestBucketWithRetries_UploadSeekable(t *testing.T) { | ||
t.Parallel() | ||
|
||
m := mockBucket{ | ||
FailCount: 3, | ||
} | ||
b := BucketWithRetries{ | ||
bucket: &m, | ||
operationRetries: 5, | ||
retryMinBackoff: 10 * time.Millisecond, | ||
retryMaxBackoff: time.Second, | ||
} | ||
|
||
input := []byte("test input") | ||
err := b.Upload(context.Background(), "dummy", bytes.NewReader(input)) | ||
require.NoError(t, err) | ||
require.Equal(t, input, m.uploadedContent) | ||
} | ||
|
||
func TestBucketWithRetries_UploadNonSeekable(t *testing.T) { | ||
t.Parallel() | ||
|
||
maxFailCount := 3 | ||
m := mockBucket{ | ||
FailCount: maxFailCount, | ||
} | ||
b := BucketWithRetries{ | ||
bucket: &m, | ||
operationRetries: 5, | ||
retryMinBackoff: 10 * time.Millisecond, | ||
retryMaxBackoff: time.Second, | ||
} | ||
|
||
input := &fakeReader{} | ||
err := b.Upload(context.Background(), "dummy", input) | ||
require.Errorf(t, err, "empty byte slice") | ||
require.Equal(t, maxFailCount, m.FailCount) | ||
} | ||
|
||
type fakeReader struct { | ||
} | ||
|
||
func (f *fakeReader) Read(p []byte) (n int, err error) { | ||
return 0, fmt.Errorf("empty byte slice") | ||
} | ||
|
||
type mockBucket struct { | ||
FailCount int | ||
uploadedContent []byte | ||
} | ||
|
||
// Upload mocks objstore.Bucket.Upload() | ||
func (m *mockBucket) Upload(ctx context.Context, name string, r io.Reader) error { | ||
var buf bytes.Buffer | ||
if _, err := buf.ReadFrom(r); err != nil { | ||
return err | ||
} | ||
m.uploadedContent = buf.Bytes() | ||
if m.FailCount > 0 { | ||
m.FailCount-- | ||
return fmt.Errorf("failed upload: %d", m.FailCount) | ||
} | ||
return nil | ||
} | ||
|
||
// Delete mocks objstore.Bucket.Delete() | ||
func (m *mockBucket) Delete(ctx context.Context, name string) error { | ||
return nil | ||
} | ||
|
||
// Name mocks objstore.Bucket.Name() | ||
func (m *mockBucket) Name() string { | ||
return "mock" | ||
} | ||
|
||
// Iter mocks objstore.Bucket.Iter() | ||
func (m *mockBucket) Iter(ctx context.Context, dir string, f func(string) error, options ...objstore.IterOption) error { | ||
return nil | ||
} | ||
|
||
// Get mocks objstore.Bucket.Get() | ||
func (m *mockBucket) Get(ctx context.Context, name string) (io.ReadCloser, error) { | ||
return nil, nil | ||
} | ||
|
||
// GetRange mocks objstore.Bucket.GetRange() | ||
func (m *mockBucket) GetRange(ctx context.Context, name string, off, length int64) (io.ReadCloser, error) { | ||
return nil, nil | ||
} | ||
|
||
// Exists mocks objstore.Bucket.Exists() | ||
func (m *mockBucket) Exists(ctx context.Context, name string) (bool, error) { | ||
return false, nil | ||
} | ||
|
||
// IsObjNotFoundErr mocks objstore.Bucket.IsObjNotFoundErr() | ||
func (m *mockBucket) IsObjNotFoundErr(err error) bool { | ||
return false | ||
} | ||
|
||
// ObjectSize mocks objstore.Bucket.Attributes() | ||
func (m *mockBucket) Attributes(ctx context.Context, name string) (objstore.ObjectAttributes, error) { | ||
return objstore.ObjectAttributes{Size: 0, LastModified: time.Now()}, nil | ||
} | ||
|
||
// Close mocks objstore.Bucket.Close() | ||
func (m *mockBucket) Close() error { | ||
return nil | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.