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 2 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
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.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#L218-L221

Added lines #L218 - 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,

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

View check run for this annotation

Codecov / codecov/patch

br/pkg/storage/azblob.go#L253-L254

Added lines #L253 - L254 were not covered by tests
}, nil
}

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

clientOptions,

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

View check run for this annotation

Codecov / codecov/patch

br/pkg/storage/azblob.go#L283-L284

Added lines #L283 - L284 were not covered by tests
}, 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,

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

View check run for this annotation

Codecov / codecov/patch

br/pkg/storage/azblob.go#L312-L313

Added lines #L312 - L313 were not covered by tests
}, nil
}

Expand Down
33 changes: 33 additions & 0 deletions br/pkg/storage/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import (
"context"
"io"
"net"
"net/http"
"time"

"github.com/aws/aws-sdk-go/aws/request"
"github.com/pingcap/errors"
Expand All @@ -27,6 +29,8 @@
GetObject Permission = "GetObject"
// PutObject represents PutObject permission
PutObject Permission = "PutObject"

DefaultRequestConcurrency uint = 128
)

// WalkOption is the option of storage.WalkDir.
Expand Down Expand Up @@ -169,6 +173,9 @@
if opts == nil {
opts = &ExternalStorageOptions{}
}
if opts.HTTPClient == nil {
opts.HTTPClient = GetDefaultHttpClient(DefaultRequestConcurrency)
}

Check warning on line 178 in br/pkg/storage/storage.go

View check run for this annotation

Codecov / codecov/patch

br/pkg/storage/storage.go#L176-L178

Added lines #L176 - L178 were not covered by tests
switch backend := backend.Backend.(type) {
case *backuppb.StorageBackend_Local:
if backend.Local == nil {
Expand Down Expand Up @@ -198,3 +205,29 @@
return nil, errors.Annotatef(berrors.ErrStorageInvalidConfig, "storage %T is not supported yet", backend)
}
}

// copy from `http.defaultTransportDialContext`
lance6716 marked this conversation as resolved.
Show resolved Hide resolved
func defaultTransportDialContext(dialer *net.Dialer) func(context.Context, string, string) (net.Conn, error) {
return dialer.DialContext

Check warning on line 211 in br/pkg/storage/storage.go

View check run for this annotation

Codecov / codecov/patch

br/pkg/storage/storage.go#L210-L211

Added lines #L210 - L211 were not covered by tests
}

// 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 {
return &http.Client{
// copy from `http.DefaultTransport`
Transport: &http.Transport{
Proxy: http.ProxyFromEnvironment,
DialContext: defaultTransportDialContext(&net.Dialer{
Timeout: 30 * time.Second,
KeepAlive: 30 * time.Second,
}),
ForceAttemptHTTP2: true,
MaxIdleConns: int(concurrency),
MaxIdleConnsPerHost: int(concurrency),
IdleConnTimeout: 90 * time.Second,
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we avoid to hard code them but copy attributes from http.DefaultTransport

Copy link
Contributor Author

Choose a reason for hiding this comment

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

the http.DefaultTransport is a interface type( interface RoundTripper ), so we only can force convert back to Transport and ignore the possible error.
And it has some fields, such as sync.Mutex and map, I'm worried that the copy leaves behind the risk of golang updating.

Copy link
Contributor

Choose a reason for hiding this comment

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

I also like @BornChanger 's idea. We can add an unit test to detect the future implementation change of interface RoundTripper , and use Transport.Clone to correctly handle its private fields.

TLSHandshakeTimeout: 10 * time.Second,
ExpectContinueTimeout: 1 * time.Second,
},
}

Check warning on line 232 in br/pkg/storage/storage.go

View check run for this annotation

Codecov / codecov/patch

br/pkg/storage/storage.go#L216-L232

Added lines #L216 - L232 were not covered by tests
}
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