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

br: configure the httpclient for external storage #46040

Merged
merged 8 commits into from
Aug 16, 2023
Merged
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/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,12 @@ go_test(
"memstore_test.go",
"parse_test.go",
"s3_test.go",
"storage_test.go",
"writer_test.go",
],
embed = [":storage"],
flaky = True,
shard_count = 45,
shard_count = 47,
deps = [
"//br/pkg/mock",
"@com_github_aws_aws_sdk_go//aws",
Expand Down
25 changes: 22 additions & 3 deletions br/pkg/storage/azblob.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,10 +157,12 @@
cred *azblob.SharedKeyCredential
accountName string
serviceURL string

clientOptions *azblob.ClientOptions
}

func (b *sharedKeyClientBuilder) GetServiceClient() (*azblob.Client, error) {
return azblob.NewClientWithSharedKeyCredential(b.serviceURL, b.cred, getDefaultClientOptions())
return azblob.NewClientWithSharedKeyCredential(b.serviceURL, b.cred, b.clientOptions)

Check warning on line 165 in br/pkg/storage/azblob.go

View check run for this annotation

Codecov / codecov/patch

br/pkg/storage/azblob.go#L165

Added line #L165 was not covered by tests
}

func (b *sharedKeyClientBuilder) GetAccountName() string {
Expand All @@ -172,10 +174,12 @@
accountName string
// Example of serviceURL: https://<account>.blob.core.windows.net/?<sas token>
serviceURL string

clientOptions *azblob.ClientOptions
}

func (b *sasClientBuilder) GetServiceClient() (*azblob.Client, error) {
return azblob.NewClientWithNoCredential(b.serviceURL, getDefaultClientOptions())
return azblob.NewClientWithNoCredential(b.serviceURL, b.clientOptions)

Check warning on line 182 in br/pkg/storage/azblob.go

View check run for this annotation

Codecov / codecov/patch

br/pkg/storage/azblob.go#L182

Added line #L182 was not covered by tests
}

func (b *sasClientBuilder) GetAccountName() string {
Expand All @@ -187,10 +191,12 @@
cred *azidentity.ClientSecretCredential
accountName string
serviceURL string

clientOptions *azblob.ClientOptions
}

func (b *tokenClientBuilder) GetServiceClient() (*azblob.Client, error) {
return azblob.NewClient(b.serviceURL, b.cred, getDefaultClientOptions())
return azblob.NewClient(b.serviceURL, b.cred, b.clientOptions)

Check warning on line 199 in br/pkg/storage/azblob.go

View check run for this annotation

Codecov / codecov/patch

br/pkg/storage/azblob.go#L199

Added line #L199 was not covered by tests
}

func (b *tokenClientBuilder) GetAccountName() string {
Expand All @@ -209,6 +215,11 @@
return nil, errors.New("bucket(container) cannot be empty to access azure blob storage")
}

clientOptions := getDefaultClientOptions()
if opts != nil && opts.HTTPClient != nil {
clientOptions.Transport = opts.HTTPClient
}

Check warning on line 221 in br/pkg/storage/azblob.go

View check run for this annotation

Codecov / codecov/patch

br/pkg/storage/azblob.go#L220-L221

Added lines #L220 - L221 were not covered by tests

if len(options.AccountName) > 0 && len(options.AccessSig) > 0 {
serviceURL := options.Endpoint
if len(serviceURL) == 0 {
Expand All @@ -221,6 +232,8 @@
return &sasClientBuilder{
options.AccountName,
serviceURL,

clientOptions,

Check warning on line 236 in br/pkg/storage/azblob.go

View check run for this annotation

Codecov / codecov/patch

br/pkg/storage/azblob.go#L235-L236

Added lines #L235 - L236 were not covered by tests
}, nil
}

Expand All @@ -237,6 +250,8 @@
cred,
options.AccountName,
serviceURL,

clientOptions,
}, nil
}

Expand Down Expand Up @@ -265,6 +280,8 @@
cred,
accountName,
serviceURL,

clientOptions,
}, nil
}
log.Warn("Failed to get azure token credential but environment variables exist, try to use shared key.", zap.String("tenantId", tenantID), zap.String("clientId", clientID), zap.String("clientSecret", "?"))
Expand Down Expand Up @@ -292,6 +309,8 @@
cred,
accountName,
serviceURL,

clientOptions,
}, nil
}

Expand Down
18 changes: 18 additions & 0 deletions br/pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ const (
GetObject Permission = "GetObject"
// PutObject represents PutObject permission
PutObject Permission = "PutObject"

DefaultRequestConcurrency uint = 128
)

// WalkOption is the option of storage.WalkDir.
Expand Down Expand Up @@ -198,3 +200,19 @@ func New(ctx context.Context, backend *backuppb.StorageBackend, opts *ExternalSt
return nil, errors.Annotatef(berrors.ErrStorageInvalidConfig, "storage %T is not supported yet", backend)
}
}

// Different from `http.DefaultTransport`, set the `MaxIdleConns` and `MaxIdleConnsPerHost`
// to the actual request concurrency to reuse tcp connection as much as possible.
func GetDefaultHttpClient(concurrency uint) *http.Client {
transport, _ := CloneDefaultHttpTransport()
transport.MaxIdleConns = int(concurrency)
transport.MaxIdleConnsPerHost = int(concurrency)
return &http.Client{
Transport: transport,
}
}

func CloneDefaultHttpTransport() (*http.Transport, bool) {
transport, ok := http.DefaultTransport.(*http.Transport)
return transport.Clone(), ok
}
26 changes: 26 additions & 0 deletions br/pkg/storage/storage_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright 2022 PingCAP, Inc. Licensed under Apache-2.0.

package storage_test

import (
"net/http"
"testing"

"github.com/pingcap/tidb/br/pkg/storage"
"github.com/stretchr/testify/require"
)

func TestDefaultHttpTransport(t *testing.T) {
transport, ok := storage.CloneDefaultHttpTransport()
require.True(t, ok)
require.True(t, transport.MaxConnsPerHost == 0)
require.True(t, transport.MaxIdleConns > 0)
}

func TestDefaultHttpClient(t *testing.T) {
var concurrency uint = 128
transport, ok := storage.GetDefaultHttpClient(concurrency).Transport.(*http.Transport)
require.True(t, ok)
require.Equal(t, int(concurrency), transport.MaxIdleConnsPerHost)
require.Equal(t, int(concurrency), transport.MaxIdleConns)
}
2 changes: 2 additions & 0 deletions br/pkg/task/stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@
opts := storage.ExternalStorageOptions{
NoCredentials: cfg.NoCreds,
SendCredentials: cfg.SendCreds,
HTTPClient: storage.GetDefaultHttpClient(cfg.MetadataDownloadBatchSize),

Check warning on line 138 in br/pkg/task/stream.go

View check run for this annotation

Codecov / codecov/patch

br/pkg/task/stream.go#L138

Added line #L138 was not covered by tests
}
storage, err := storage.New(ctx, u, &opts)
if err != nil {
Expand Down Expand Up @@ -1471,6 +1472,7 @@
opts := storage.ExternalStorageOptions{
NoCredentials: cfg.NoCreds,
SendCredentials: cfg.SendCreds,
HTTPClient: storage.GetDefaultHttpClient(cfg.MetadataDownloadBatchSize),

Check warning on line 1475 in br/pkg/task/stream.go

View check run for this annotation

Codecov / codecov/patch

br/pkg/task/stream.go#L1475

Added line #L1475 was not covered by tests
}
if err = client.SetStorage(ctx, u, &opts); err != nil {
return nil, errors.Trace(err)
Expand Down
Loading