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

CLOUDP-211581: Improve memory consumption in Atlas SDK #331

Merged
merged 13 commits into from
Jun 4, 2024
Merged

Conversation

fmenezes
Copy link
Collaborator

Description

Improve memory consumption in Atlas SDK (by not reading response.Body when we don't need)

Link to any related issue(s): CLOUDP-211581

Type of change:

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • This change requires a documentation update

Required Checklist:

  • I have signed the MongoDB CLA
  • I have added tests that prove my fix is effective or that my feature works
  • I have added any necessary documentation (if appropriate)
  • I have run make fmt and formatted my code

Further comments

@fmenezes fmenezes requested a review from a team as a code owner May 30, 2024 17:09
@fmenezes
Copy link
Collaborator Author

running tests at mongodb/mongodb-atlas-cli#2979

@@ -13,6 +13,7 @@ jobs:
- uses: actions/setup-go@v5
with:
go-version-file: 'go.mod'
- run: go install golang.org/x/tools/cmd/[email protected]
Copy link
Member

@wtrocki wtrocki Jun 3, 2024

Choose a reason for hiding this comment

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

golangcilint wraps goimports for checks. Since we need it her for code fixing I think it is nicer to consider adding it to

Copy link
Member

Choose a reason for hiding this comment

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

Basically [super nit] I think we still need it locally (as part of the git hooks - tools) and for generation.
Once we add it there would not be need for any autoupdate workflow changes.

Those are quite hard to test locally and might fail after merge.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it is already in make tools

@@ -4,7 +4,6 @@ STAGED_GO_FILES=$(git diff --cached --name-only | grep ".go$")

for FILE in ${STAGED_GO_FILES}
do
gofmt -w -s "${FILE}"
Copy link
Member

Choose a reason for hiding this comment

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

❤️

Copy link
Member

Choose a reason for hiding this comment

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

There are still some differences between gofmt and goimports ( goimports does less).
My guess is that we want to align SDK hooks in other golang repose like CLI?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

goimports is needed because some imports have to be removed after generation to avoid compilation errors (e.g. io)

@wtrocki
Copy link
Member

wtrocki commented Jun 3, 2024

Verified locally:

  1. Creation of AWS cluster
▶ go run ./aws_cluster/aws.go
SDK: Created new MongoDB cluster: 665d9c2735f85255276102f9 
Please wait up to 10 minutes for cluster to provision.
  1. Getting file from backend

}
newErr.error = formatErrorMessage(localVarHTTPResponse.Status, localVarHTTPMethod, localVarPath, v)
newErr.model = v
newErr := a.client.makeApiError(localVarHTTPResponse, localVarHTTPMethod, localVarPath)
Copy link
Member

Choose a reason for hiding this comment

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

❤️

if err != nil {
defer localVarHTTPResponse.Body.Close()
Copy link
Member

@wtrocki wtrocki Jun 3, 2024

Choose a reason for hiding this comment

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

[Question] Why closing only on error? Should each method be responsible for closing body independently from if decode succeeded or not?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

close only happens in this case because it already happens in decode and makeApiError

@wtrocki
Copy link
Member

wtrocki commented Jun 3, 2024

When testing error case I noticed that resp.Body is not returned as pointer.
I modified aws.go to cause error by providing wrong region:

go run ./aws_cluster/aws.go  
{0x1400024a180}
https://cloud-dev.mongodb.com/api/atlas/v2/groups/65bd48ebe4cb1d1533f14fd6/clusters POST: HTTP 400 Bad Request (Error code: "INVALID_ATTRIBUTE") Detail: Invalid attribute regionName specified. Reason: Bad Request. Params: [regionName]
2024/06/03 12:57:28 Error when performing SDK request: Invalid attribute regionName specified.
exit status 1

On master that renders:

{{"detail":"Invalid attribute regionName specified.","error":400,"errorCode":"INVALID_ATTRIBUTE","parameters":["regionName"],"reason":"Bad Request"}}
https://cloud-dev.mongodb.com/api/atlas/v2/groups/65bd48ebe4cb1d1533f14fd6/clusters POST: HTTP 400 Bad Request (Error code: "INVALID_ATTRIBUTE") Detail: Invalid attribute regionName specified. Reason: Bad Request. Params: [regionName]
2024/06/03 13:01:38 Error when performing SDK request: Invalid attribute regionName specified.
exit status 1

Is there way to keep previous behaviour?

@wtrocki
Copy link
Member

wtrocki commented Jun 3, 2024

@fmenezes Awesome work on making this major improvement!

I have done some testing and left couple questions, but everything looks LGTM.
We need to clarify possibility of breaking change (as this might require some additional steps for that PR.

@fmenezes
Copy link
Collaborator Author

fmenezes commented Jun 4, 2024

When testing error case I noticed that resp.Body is not returned as pointer. I modified aws.go to cause error by providing wrong region:

go run ./aws_cluster/aws.go  
{0x1400024a180}
https://cloud-dev.mongodb.com/api/atlas/v2/groups/65bd48ebe4cb1d1533f14fd6/clusters POST: HTTP 400 Bad Request (Error code: "INVALID_ATTRIBUTE") Detail: Invalid attribute regionName specified. Reason: Bad Request. Params: [regionName]
2024/06/03 12:57:28 Error when performing SDK request: Invalid attribute regionName specified.
exit status 1

On master that renders:

{{"detail":"Invalid attribute regionName specified.","error":400,"errorCode":"INVALID_ATTRIBUTE","parameters":["regionName"],"reason":"Bad Request"}}
https://cloud-dev.mongodb.com/api/atlas/v2/groups/65bd48ebe4cb1d1533f14fd6/clusters POST: HTTP 400 Bad Request (Error code: "INVALID_ATTRIBUTE") Detail: Invalid attribute regionName specified. Reason: Bad Request. Params: [regionName]
2024/06/03 13:01:38 Error when performing SDK request: Invalid attribute regionName specified.
exit status 1

Is there way to keep previous behaviour?

TL;DR we have to change our examples

Basically in our handleErr function we fmt.Println(resp.Body) instead we should have been printing err.body from generic error.

this is misleading because we can't tell what is the state of resp.Body after the body was already read.

@wtrocki
Copy link
Member

wtrocki commented Jun 4, 2024

Basically in our handleErr function we fmt.Println(resp.Body) instead we should have been printing err.body from generic error. this is misleading because we can't tell what is the state of resp.Body after the body was already read.

So basically we need documentation to not rely on the resp.Body for printing body from request and follow your advice.
If customers use that they might need to adjust their changes.
If that is correct we might want

  • Short note in error handling documentation (./docs folder)
  • Breaking change note - no need to make it here (we can reuse existing breaking change update PR as follow up
  • Update example

No strong opinion on those.

@wtrocki
Copy link
Member

wtrocki commented Jun 4, 2024

Will LGTM once we address the conflict (regenerate code). I'm ok to take follow up documentation actions if you want.

@fmenezes fmenezes merged commit 53bb4f8 into main Jun 4, 2024
4 checks passed
@fmenezes fmenezes deleted the CLOUDP-211581 branch June 4, 2024 13:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants