Skip to content
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

[WIP]external storage: use ListObjectsV2 and handle S3 express one zone #58953

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion br/pkg/storage/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func TestCreateStorage(t *testing.T) {
require.Equal(t, "https://s3.example.com", s3.Endpoint)
require.False(t, s3.ForcePathStyle)

s, err = ParseBackend("ks3://bucket2/prefix/", s3opt)
s, err = ParseBackend("ks3://bucket2/prefix/?storage-class=express-one-zone", s3opt)
require.NoError(t, err)
s3 = s.GetS3()
require.NotNil(t, s3)
Expand All @@ -65,6 +65,7 @@ func TestCreateStorage(t *testing.T) {
require.Equal(t, "https://s3.example.com", s3.Endpoint)
require.Equal(t, ks3SDKProvider, s3.Provider)
require.False(t, s3.ForcePathStyle)
require.Equal(t, "express-one-zone", s3.StorageClass)

// nolint:lll
s, err = ParseBackend(`s3://bucket3/prefix/path?endpoint=https://127.0.0.1:9000&force_path_style=0&SSE=aws:kms&sse-kms-key-id=TestKey&xyz=abc`, nil)
Expand Down
26 changes: 18 additions & 8 deletions br/pkg/storage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,10 @@ const (
s3ProviderOption = "s3.provider"
s3RoleARNOption = "s3.role-arn"
s3ExternalIDOption = "s3.external-id"
notFound = "NotFound"

storageExpressOneZone = "express-one-zone"

notFound = "NotFound"
// number of retries to make of operations.
maxRetries = 7
// max number of retries when meets error
Expand Down Expand Up @@ -348,6 +351,16 @@ func NewS3Storage(ctx context.Context, backend *backuppb.S3, opts *ExternalStora
request.WithRetryer(awsConfig, defaultS3Retryer())
}

if qs.StorageClass == storageExpressOneZone {
if qs.Endpoint == "" {
return nil, errors.Errorf("must specify endpoint for S3 Express One Zone storage class")
}
if qs.Region == "" {
return nil, errors.Errorf("must specify region for S3 Express One Zone storage class")
}
awsConfig.WithS3DisableContentMD5Validation(true).
WithS3ForcePathStyle(false)
}
if qs.Endpoint != "" {
awsConfig.WithEndpoint(qs.Endpoint)
}
Expand Down Expand Up @@ -402,7 +415,7 @@ func NewS3Storage(ctx context.Context, backend *backuppb.S3, opts *ExternalStora
c := s3.New(ses, s3CliConfigs...)

var region string
if len(qs.Provider) == 0 || qs.Provider == "aws" {
if (len(qs.Provider) == 0 || qs.Provider == "aws") && qs.StorageClass != storageExpressOneZone {
confCred := ses.Config.Credentials
setCredOpt := func(req *request.Request) {
// s3manager.GetBucketRegionWithClient will set credential anonymous, which works with s3.
Expand Down Expand Up @@ -718,17 +731,14 @@ func (rs *S3Storage) WalkDir(ctx context.Context, opt *WalkOption, fn func(strin
if opt.ListCount > 0 {
maxKeys = opt.ListCount
}
req := &s3.ListObjectsInput{
req := &s3.ListObjectsV2Input{
Bucket: aws.String(rs.options.Bucket),
Prefix: aws.String(prefix),
MaxKeys: aws.Int64(maxKeys),
}

for {
// FIXME: We can't use ListObjectsV2, it is not universally supported.
// (Ceph RGW supported ListObjectsV2 since v15.1.0, released 2020 Jan 30th)
// (as of 2020, DigitalOcean Spaces still does not support V2 - https://developers.digitalocean.com/documentation/spaces/#list-bucket-contents)
res, err := rs.svc.ListObjectsWithContext(ctx, req)
res, err := rs.svc.ListObjectsV2WithContext(ctx, req)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

not sure what previous comment means?

does Aliyun OSS or minio support this listObjectV2? they share the same code base

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently DigitalOcean object storage server does not support this API. But now I have checked the URL, it should be supported

if err != nil {
return errors.Trace(err)
}
Expand All @@ -742,7 +752,7 @@ func (rs *S3Storage) WalkDir(ctx context.Context, opt *WalkOption, fn func(strin
// "If response does not include the NextMarker and it is truncated,
// you can use the value of the last Key in the response as the marker
// in the subsequent request to get the next set of object keys."
req.Marker = r.Key
req.StartAfter = r.Key

// when walk on specify directory, the result include storage.Prefix,
// which can not be reuse in other API(Open/Read) directly.
Expand Down
84 changes: 52 additions & 32 deletions br/pkg/storage/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1019,74 +1019,74 @@ func TestWalkDir(t *testing.T) {

// first call serve item #0, #1; second call #2, #3; third call #4.
firstCall := s.s3.EXPECT().
ListObjectsWithContext(ctx, gomock.Any()).
DoAndReturn(func(_ context.Context, input *s3.ListObjectsInput, opt ...request.Option) (*s3.ListObjectsOutput, error) {
ListObjectsV2WithContext(ctx, gomock.Any()).
DoAndReturn(func(_ context.Context, input *s3.ListObjectsV2Input, opt ...request.Option) (*s3.ListObjectsV2Output, error) {
require.Equal(t, "bucket", aws.StringValue(input.Bucket))
require.Equal(t, "prefix/sp/", aws.StringValue(input.Prefix))
require.Equal(t, "", aws.StringValue(input.Marker))
require.Equal(t, "", aws.StringValue(input.StartAfter))
require.Equal(t, int64(2), aws.Int64Value(input.MaxKeys))
require.Equal(t, "", aws.StringValue(input.Delimiter))
return &s3.ListObjectsOutput{
return &s3.ListObjectsV2Output{
IsTruncated: aws.Bool(true),
Contents: contents[:2],
}, nil
})
secondCall := s.s3.EXPECT().
ListObjectsWithContext(ctx, gomock.Any()).
DoAndReturn(func(_ context.Context, input *s3.ListObjectsInput, opt ...request.Option) (*s3.ListObjectsOutput, error) {
require.Equal(t, aws.StringValue(contents[1].Key), aws.StringValue(input.Marker))
ListObjectsV2WithContext(ctx, gomock.Any()).
DoAndReturn(func(_ context.Context, input *s3.ListObjectsV2Input, opt ...request.Option) (*s3.ListObjectsV2Output, error) {
require.Equal(t, aws.StringValue(contents[1].Key), aws.StringValue(input.StartAfter))
require.Equal(t, int64(2), aws.Int64Value(input.MaxKeys))
return &s3.ListObjectsOutput{
return &s3.ListObjectsV2Output{
IsTruncated: aws.Bool(true),
Contents: contents[2:4],
}, nil
}).
After(firstCall)
thirdCall := s.s3.EXPECT().
ListObjectsWithContext(ctx, gomock.Any()).
DoAndReturn(func(_ context.Context, input *s3.ListObjectsInput, opt ...request.Option) (*s3.ListObjectsOutput, error) {
require.Equal(t, aws.StringValue(contents[3].Key), aws.StringValue(input.Marker))
ListObjectsV2WithContext(ctx, gomock.Any()).
DoAndReturn(func(_ context.Context, input *s3.ListObjectsV2Input, opt ...request.Option) (*s3.ListObjectsV2Output, error) {
require.Equal(t, aws.StringValue(contents[3].Key), aws.StringValue(input.StartAfter))
require.Equal(t, int64(2), aws.Int64Value(input.MaxKeys))
return &s3.ListObjectsOutput{
return &s3.ListObjectsV2Output{
IsTruncated: aws.Bool(false),
Contents: contents[4:],
}, nil
}).
After(secondCall)
fourthCall := s.s3.EXPECT().
ListObjectsWithContext(ctx, gomock.Any()).
DoAndReturn(func(_ context.Context, input *s3.ListObjectsInput, opt ...request.Option) (*s3.ListObjectsOutput, error) {
ListObjectsV2WithContext(ctx, gomock.Any()).
DoAndReturn(func(_ context.Context, input *s3.ListObjectsV2Input, opt ...request.Option) (*s3.ListObjectsV2Output, error) {
require.Equal(t, "bucket", aws.StringValue(input.Bucket))
require.Equal(t, "prefix/", aws.StringValue(input.Prefix))
require.Equal(t, "", aws.StringValue(input.Marker))
require.Equal(t, "", aws.StringValue(input.StartAfter))
require.Equal(t, int64(4), aws.Int64Value(input.MaxKeys))
require.Equal(t, "", aws.StringValue(input.Delimiter))
return &s3.ListObjectsOutput{
return &s3.ListObjectsV2Output{
IsTruncated: aws.Bool(true),
Contents: contents[:4],
}, nil
}).
After(thirdCall)
fifthCall := s.s3.EXPECT().
ListObjectsWithContext(ctx, gomock.Any()).
DoAndReturn(func(_ context.Context, input *s3.ListObjectsInput, opt ...request.Option) (*s3.ListObjectsOutput, error) {
require.Equal(t, aws.StringValue(contents[3].Key), aws.StringValue(input.Marker))
ListObjectsV2WithContext(ctx, gomock.Any()).
DoAndReturn(func(_ context.Context, input *s3.ListObjectsV2Input, opt ...request.Option) (*s3.ListObjectsV2Output, error) {
require.Equal(t, aws.StringValue(contents[3].Key), aws.StringValue(input.StartAfter))
require.Equal(t, int64(4), aws.Int64Value(input.MaxKeys))
return &s3.ListObjectsOutput{
return &s3.ListObjectsV2Output{
IsTruncated: aws.Bool(false),
Contents: contents[4:],
}, nil
}).
After(fourthCall)
s.s3.EXPECT().
ListObjectsWithContext(ctx, gomock.Any()).
DoAndReturn(func(_ context.Context, input *s3.ListObjectsInput, opt ...request.Option) (*s3.ListObjectsOutput, error) {
ListObjectsV2WithContext(ctx, gomock.Any()).
DoAndReturn(func(_ context.Context, input *s3.ListObjectsV2Input, opt ...request.Option) (*s3.ListObjectsV2Output, error) {
require.Equal(t, "bucket", aws.StringValue(input.Bucket))
require.Equal(t, "prefix/sp/1", aws.StringValue(input.Prefix))
require.Equal(t, "", aws.StringValue(input.Marker))
require.Equal(t, "", aws.StringValue(input.StartAfter))
require.Equal(t, int64(3), aws.Int64Value(input.MaxKeys))
require.Equal(t, "", aws.StringValue(input.Delimiter))
return &s3.ListObjectsOutput{
return &s3.ListObjectsV2Output{
IsTruncated: aws.Bool(false),
Contents: contents[2:],
}, nil
Expand Down Expand Up @@ -1168,27 +1168,27 @@ func TestWalkDirWithEmptyPrefix(t *testing.T) {
},
}
firstCall := s3API.EXPECT().
ListObjectsWithContext(ctx, gomock.Any()).
DoAndReturn(func(_ context.Context, input *s3.ListObjectsInput, opt ...request.Option) (*s3.ListObjectsOutput, error) {
ListObjectsV2WithContext(ctx, gomock.Any()).
DoAndReturn(func(_ context.Context, input *s3.ListObjectsV2Input, opt ...request.Option) (*s3.ListObjectsV2Output, error) {
require.Equal(t, "bucket", aws.StringValue(input.Bucket))
require.Equal(t, "", aws.StringValue(input.Prefix))
require.Equal(t, "", aws.StringValue(input.Marker))
require.Equal(t, "", aws.StringValue(input.StartAfter))
require.Equal(t, int64(2), aws.Int64Value(input.MaxKeys))
require.Equal(t, "", aws.StringValue(input.Delimiter))
return &s3.ListObjectsOutput{
return &s3.ListObjectsV2Output{
IsTruncated: aws.Bool(false),
Contents: contents,
}, nil
})
s3API.EXPECT().
ListObjectsWithContext(ctx, gomock.Any()).
DoAndReturn(func(_ context.Context, input *s3.ListObjectsInput, opt ...request.Option) (*s3.ListObjectsOutput, error) {
ListObjectsV2WithContext(ctx, gomock.Any()).
DoAndReturn(func(_ context.Context, input *s3.ListObjectsV2Input, opt ...request.Option) (*s3.ListObjectsV2Output, error) {
require.Equal(t, "bucket", aws.StringValue(input.Bucket))
require.Equal(t, "sp/", aws.StringValue(input.Prefix))
require.Equal(t, "", aws.StringValue(input.Marker))
require.Equal(t, "", aws.StringValue(input.StartAfter))
require.Equal(t, int64(2), aws.Int64Value(input.MaxKeys))
require.Equal(t, "", aws.StringValue(input.Delimiter))
return &s3.ListObjectsOutput{
return &s3.ListObjectsV2Output{
IsTruncated: aws.Bool(false),
Contents: contents[:1],
}, nil
Expand Down Expand Up @@ -1472,3 +1472,23 @@ func TestS3ReadFileRetryable(t *testing.T) {
require.Error(t, err)
require.True(t, strings.Contains(err.Error(), errMsg))
}

func TestS3ExpressEndpoint(t *testing.T) {
ctx := context.Background()
_, err := NewS3Storage(ctx, &backuppb.S3{
Endpoint: "https://s3express-use1-az6.us-east-1.amazonaws.com",
Bucket: "xxxxxxx-global-sort-test--use1-az6--x-s3",
Region: "us-east-1",
ForcePathStyle: true,
StorageClass: "express-one-zone",
}, &ExternalStorageOptions{})
require.NoError(t, err)
// if storage class is not S3 express, this function will fail because no network for auto adjust region.
_, err = NewS3Storage(ctx, &backuppb.S3{
Endpoint: "https://s3express-use1-az6.us-east-1.amazonaws.com",
Bucket: "xxxxxxx-global-sort-test--use1-az6--x-s3",
Region: "us-east-1",
ForcePathStyle: true,
}, &ExternalStorageOptions{})
require.Error(t, err)
}
Loading