-
Notifications
You must be signed in to change notification settings - Fork 807
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix S3 BucketWithRetries upload empty content issue (#5217)
* Implement grpc.Compressor.DecompressedSize for snappy to optimize memory allocations (#5213) Signed-off-by: Xiaochao Dong (@damnever) <[email protected]> Signed-off-by: Alex Le <[email protected]> * Fix S3 BucketWithRetries upload empty content issue Signed-off-by: Alex Le <[email protected]> * Update CHANGELOG Signed-off-by: Alex Le <[email protected]> * Revert "Implement grpc.Compressor.DecompressedSize for snappy to optimize memory allocations (#5213)" This reverts commit 4821ba3. Signed-off-by: Alex Le <[email protected]> * Only retry if input reader is seekable Signed-off-by: Alex Le <[email protected]> * Rename mock type Signed-off-by: Alex Le <[email protected]> * Add logging Signed-off-by: Alex Le <[email protected]> * nit fixing Signed-off-by: Alex Le <[email protected]> * add comment Signed-off-by: Alex Le <[email protected]> --------- Signed-off-by: Xiaochao Dong (@damnever) <[email protected]> Signed-off-by: Alex Le <[email protected]> Co-authored-by: Xiaochao Dong <[email protected]>
- Loading branch information
Showing
3 changed files
with
142 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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 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 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 | ||
} |