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

prevent vtctld from creating tons of S3 connections #15296

Merged
merged 5 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
20 changes: 17 additions & 3 deletions go/vt/mysqlctl/s3backupstorage/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,9 @@ var (

// path component delimiter
delimiter = "/"

// use a shared transport for all connections
defaultS3Transport *http.Transport
Copy link
Contributor

@mattlord mattlord Feb 28, 2024

Choose a reason for hiding this comment

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

Nit, but s3 is a bit redundant here. We can just call it 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.

got rid of the global variable 😌

)

func registerFlags(fs *pflag.FlagSet) {
Expand Down Expand Up @@ -445,9 +448,7 @@ func (bs *S3BackupStorage) client() (*s3.S3, error) {
if bs._client == nil {
logLevel := getLogLevel()

tlsClientConf := &tls.Config{InsecureSkipVerify: tlsSkipVerifyCert}
httpTransport := &http.Transport{TLSClientConfig: tlsClientConf}
httpClient := &http.Client{Transport: httpTransport}
httpClient := &http.Client{Transport: getS3Transport()}

session, err := session.NewSession()
if err != nil {
Expand Down Expand Up @@ -496,6 +497,19 @@ func objName(parts ...string) *string {
return &res
}

// This creates a new transport based off http.DefaultTransport the first time and returns the same
// transport on subsequent calls so connections can be reused as part of the same transport.
func getS3Transport() *http.Transport {
if defaultS3Transport == nil {
tlsClientConf := &tls.Config{InsecureSkipVerify: tlsSkipVerifyCert}

defaultS3Transport = http.DefaultTransport.(*http.Transport).Clone()
Copy link
Contributor

Choose a reason for hiding this comment

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

This seems correct after reading through: golang/go#26013

Copy link
Contributor

Choose a reason for hiding this comment

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

I think that we should get rid of this getter and instead initialize the variable in init() though. This is a more clear signal of our intent and a more standard pattern. Does that make sense?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, that makes sense. I will move it to the init() function. I will updated the PR tomorrow

Copy link
Contributor Author

Choose a reason for hiding this comment

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

as mentioned in the other comment, I have moved this to the S3BackupStorage struct, so we don't need to initialise a global anymore 🙌

defaultS3Transport.TLSClientConfig = tlsClientConf
}

return defaultS3Transport
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this concurrency safe / does it need to be? Since it doesn't seem like it is.

I'm also not a big fan of having a global for this, those tend to bite us later on. Can we move it at least into S3BackupStorage instead then? That's still kinda global, but at least a tad better then.

Copy link
Contributor

Choose a reason for hiding this comment

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

We can then also initialize it already in the init() to avoid the whole concurrency question / potential problem?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes, it should be, specially considering the default on the SDK already uses http.DefaultClient, which in turn uses http.DefaultTransport, this would have been a shared default in most projects that don't define their transport/client from scratch, see the Config docs.

I thought the S3BackupStorage would be created for each request being processed, but if that is not the case we could move it there. I will test it out and report back.

if the above is not possible and setting it up on init() is preferred I can do that too

Copy link
Member

Choose a reason for hiding this comment

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

I see that you added a unit test for this. What's the conclusion?
@dbussink time for a re-review?

Copy link
Contributor

Choose a reason for hiding this comment

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

@deepthi I’m back next week but I don’t see a refactor of this or a reason why that couldn’t be done. I strongly prefer not having a global like this if that can reasonably be avoided.

Also happy to push up that refactor once I’m back.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I have moved this to inside the S3BackupStorage and re-run our tests and seems the connections are getting re-used, so I have update the PR to reflect that, thanks for pointing it out!

}

func init() {
backupstorage.BackupStorageMap["s3"] = &S3BackupStorage{params: backupstorage.NoParams()}

Expand Down
13 changes: 13 additions & 0 deletions go/vt/mysqlctl/s3backupstorage/s3_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -276,3 +276,16 @@ func TestSSECustomerFileBase64Key(t *testing.T) {
assert.Nil(t, sseData.customerKey, "customerKey expected to be nil")
assert.Nil(t, sseData.customerMd5, "customerMd5 expected to be nil")
}

func Test_getS3Transport(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit, but TestGetS3Transport would be the typical name in the 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.

I updated it to TestGetTransport to avoid the redundancy

transport := getS3Transport()

// checking some of the values are present in the returned transport and match the http.DefaultTransport.
assert.Equal(t, http.DefaultTransport.(*http.Transport).IdleConnTimeout, transport.IdleConnTimeout)
assert.Equal(t, http.DefaultTransport.(*http.Transport).MaxIdleConns, transport.MaxIdleConns)
assert.NotNil(t, transport.DialContext)
assert.NotNil(t, transport.Proxy)

newTransport := getS3Transport()
assert.Same(t, transport, newTransport) // new call should return the same transport
}
Loading