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

[TT-5477] Fix/jsvm memory usage #4249

Merged
merged 41 commits into from Oct 3, 2022
Merged

[TT-5477] Fix/jsvm memory usage #4249

merged 41 commits into from Oct 3, 2022

Conversation

buger
Copy link
Member

@buger buger commented Aug 22, 2022

This is an alternative implementation of https://github.com/TykTechnologies/tyk/pull/4215/files

Description

Reloading APIs, which use JSVM, cause gateway memory growth, and eventual crash.

Initialise JSVM only when needed

First of all I found that JSVM get initialized when ANY plugin is used. So first fix is:
Initialise JSVM only when VirtualEndpoint used, or plugin with JSVM engine is used.
It was done by removing initialization code from here https://github.com/TykTechnologies/tyk/compare/fix/jsvm-memory-usage?expand=1#diff-0cf80174bbafb36f6d4f4308ebbd971b2833b76a936bad568220aa1a4ba0ee8bL337-L354
And putting it after we already know plugin type https://github.com/TykTechnologies/tyk/compare/fix/jsvm-memory-usage?expand=1#diff-cdf0b7f176c9d18e1a314b78ddefc2cb3a94b3de66f1f360174692c915734c68R233-R237

Releasing pointers to easy GC

JSVM code by itself, is "a bit of" circular pointer hell. Trying to fix it leads to huge rewrite (and some parts really non obvious on how to do without having links to spec/gateway), which I do not want do as part of this PR. So now it cleanups pointers inside JSVM object, before API gets reloaded, to make GC job easier https://github.com/TykTechnologies/tyk/compare/fix/jsvm-memory-usage?expand=1#diff-78cd278aba997558b7daa7897051a794ef860076d45c93be792791db39381ca0R374-R380

Reloading only APIs which actually changed

For each API we now calculate SHA256 checksum, and if during API reload event, API has not changed, re-use already loaded API and its resources instead. https://github.com/TykTechnologies/tyk/compare/fix/jsvm-memory-usage?expand=1#diff-cdf0b7f176c9d18e1a314b78ddefc2cb3a94b3de66f1f360174692c915734c68R886-R898

How This Has Been Tested

Screenshots (if appropriate)

Types of changes

  • 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 change)
  • Refactoring or add test (improvements in base code or adds test coverage to functionality)

Checklist

  • Make sure you are requesting to pull a topic/feature/bugfix branch (right side). If pulling from your own
    fork, don't request your master!
  • Make sure you are making a pull request against the master branch (left side). Also, you should start
    your branch off our latest master.
  • My change requires a change to the documentation.
    • If you've changed APIs, describe what needs to be updated in the documentation.
    • If new config option added, ensure that it can be set via ENV variable
  • I have updated the documentation accordingly.
  • Modules and vendor dependencies have been updated; run go mod tidy && go mod vendor
  • When updating library version must provide reason/explanation for this update.
  • I have added tests to cover my changes.
  • All new and existing tests passed.
  • Check your code additions will not fail linting checks:
    • go fmt -s
    • go vet

@@ -319,6 +326,16 @@

spec.APIDefinition = def

apiString, err := json.Marshal(def)
if err != nil {
logger.WithError(err).WithField("name", def.Name).Error("Failed to JSON marshal API definition")

Check failure

Code scanning / CodeQL

Log entries created from user input

This log write receives unsanitized user input from [here](1).
@TykTechnologies TykTechnologies deleted a comment from github-actions bot Aug 22, 2022
@TykTechnologies TykTechnologies deleted a comment from github-actions bot Aug 22, 2022
@TykTechnologies TykTechnologies deleted a comment from github-actions bot Aug 22, 2022
@TykTechnologies TykTechnologies deleted a comment from github-actions bot Aug 23, 2022
@TykTechnologies TykTechnologies deleted a comment from github-actions bot Aug 23, 2022
@TykTechnologies TykTechnologies deleted a comment from github-actions bot Aug 24, 2022
@TykTechnologies TykTechnologies deleted a comment from github-actions bot Aug 24, 2022
@TykTechnologies TykTechnologies deleted a comment from github-actions bot Aug 24, 2022
@TykTechnologies TykTechnologies deleted a comment from github-actions bot Aug 24, 2022
@TykTechnologies TykTechnologies deleted a comment from github-actions bot Aug 24, 2022
@TykTechnologies TykTechnologies deleted a comment from github-actions bot Aug 25, 2022
@TykTechnologies TykTechnologies deleted a comment from github-actions bot Aug 27, 2022
@TykTechnologies TykTechnologies deleted a comment from github-actions bot Aug 27, 2022
@TykTechnologies TykTechnologies deleted a comment from github-actions bot Aug 27, 2022
@TykTechnologies TykTechnologies deleted a comment from github-actions bot Aug 28, 2022
@TykTechnologies TykTechnologies deleted a comment from github-actions bot Aug 28, 2022
@TykTechnologies TykTechnologies deleted a comment from github-actions bot Aug 28, 2022
@TykTechnologies TykTechnologies deleted a comment from github-actions bot Aug 28, 2022
defer ts.Close()
defer ts.MockHandle.ShutdownDnsMock()
Copy link
Contributor

Choose a reason for hiding this comment

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

If you see dnsmock being used somewhere and you're touching that part of the code, just remove it. There's no safe way to use the dns mock (particularly modifying net.DefaultResolver), and we have various utilities that can avoid it in the /test pkg (net.dialer, http client, etc.). Can this be avoided?

gateway/server.go Outdated Show resolved Hide resolved
gateway/testutil.go Outdated Show resolved Hide resolved
gateway/testutil.go Outdated Show resolved Hide resolved
gateway/api_test.go Show resolved Hide resolved
gateway/api_definition.go Outdated Show resolved Hide resolved
gateway/api_loader.go Outdated Show resolved Hide resolved
gateway/api_loader.go Outdated Show resolved Hide resolved
gateway/testutil.go Outdated Show resolved Hide resolved
* remove gw.started flag - out of scope from PR
* use mutex lock instead of RLock while removing APIs from gw.
* cast assertion for chainObject in serveHTTP
@sonarcloud
Copy link

sonarcloud bot commented Oct 3, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

87.6% 87.6% Coverage
0.0% 0.0% Duplication

@Tyk-ITS
Copy link
Contributor

Tyk-ITS commented Oct 3, 2022

API tests result: success
Branch used: refs/pull/4249/merge
Commit: fae61d7
Triggered by: pull_request (@jeffy-mathew)
Execution page

@jeffy-mathew jeffy-mathew merged commit 648bd90 into master Oct 3, 2022
@jeffy-mathew jeffy-mathew deleted the fix/jsvm-memory-usage branch October 3, 2022 11:29
@jeffy-mathew
Copy link
Contributor

/release to release-4

@jeffy-mathew
Copy link
Contributor

/release to release-4-lts

@tykbot
Copy link

tykbot bot commented Oct 3, 2022

Working on it! Note that it can take a few minutes.

1 similar comment
@tykbot
Copy link

tykbot bot commented Oct 3, 2022

Working on it! Note that it can take a few minutes.

tykbot bot pushed a commit that referenced this pull request Oct 3, 2022
* Ensure that JSVM initialized only if plugin is JSVM or there is virtual endpoint

In past it was initialized if ANY plugin is enabled. Now it checks the driver.

* Ensure that only APIs which changed will be reloaded

If APi checksum has not chaned, re-use and not reload it.
Additionally added logic for JSVM to release holding pointers to Gateway and Spec, so it will not have issues with being garbage collected

Co-authored-by: sredny buitrago <sredny.buitrago@gmail.com>
Co-authored-by: jeff <jeffy.mathew100@gmail.com>
(cherry picked from commit 648bd90)
@tykbot
Copy link

tykbot bot commented Oct 3, 2022

@jeffy-mathew Succesfully merged 648bd90c69b9ee857185ce8fe0190f45498254b0 to release-4 branch.

@tykbot
Copy link

tykbot bot commented Oct 3, 2022

@jeffy-mathew Seems like there is conflict and it require manual merge.

@Tyk-ITS
Copy link
Contributor

Tyk-ITS commented Oct 3, 2022

API tests result: success
Branch used: refs/heads/master
Commit: 648bd90 TT-6635 Fix/jsvm memory usage (#4249)

  • Ensure that JSVM initialized only if plugin is JSVM or there is virtual endpoint

In past it was initialized if ANY plugin is enabled. Now it checks the driver.

  • Ensure that only APIs which changed will be reloaded

If APi checksum has not chaned, re-use and not reload it.
Additionally added logic for JSVM to release holding pointers to Gateway and Spec, so it will not have issues with being garbage collected

Co-authored-by: sredny buitrago sredny.buitrago@gmail.com
Co-authored-by: jeff jeffy.mathew100@gmail.com
Triggered by: push (@jeffy-mathew)
Execution page

jeffy-mathew added a commit that referenced this pull request Oct 3, 2022
* Ensure that JSVM initialized only if plugin is JSVM or there is virtual endpoint

In past it was initialized if ANY plugin is enabled. Now it checks the driver.

* Ensure that only APIs which changed will be reloaded

If APi checksum has not chaned, re-use and not reload it.
Additionally added logic for JSVM to release holding pointers to Gateway and Spec, so it will not have issues with being garbage collected

Co-authored-by: sredny buitrago <sredny.buitrago@gmail.com>
Co-authored-by: jeff <jeffy.mathew100@gmail.com>
@jeffy-mathew
Copy link
Contributor

/release to release-4.2

@tykbot
Copy link

tykbot bot commented Oct 4, 2022

Working on it! Note that it can take a few minutes.

@jeffy-mathew
Copy link
Contributor

/release to release-4.2.2

@tykbot
Copy link

tykbot bot commented Oct 4, 2022

Working on it! Note that it can take a few minutes.

@tykbot
Copy link

tykbot bot commented Oct 4, 2022

@jeffy-mathew Seems like there is conflict and it require manual merge.

1 similar comment
@tykbot
Copy link

tykbot bot commented Oct 4, 2022

@jeffy-mathew Seems like there is conflict and it require manual merge.

jeffy-mathew added a commit that referenced this pull request Oct 4, 2022
* Ensure that JSVM initialized only if plugin is JSVM or there is virtual endpoint

In past it was initialized if ANY plugin is enabled. Now it checks the driver.

* Ensure that only APIs which changed will be reloaded

If APi checksum has not chaned, re-use and not reload it.
Additionally added logic for JSVM to release holding pointers to Gateway and Spec, so it will not have issues with being garbage collected

Co-authored-by: sredny buitrago <sredny.buitrago@gmail.com>
Co-authored-by: jeff <jeffy.mathew100@gmail.com>
jeffy-mathew added a commit that referenced this pull request Oct 4, 2022
* Ensure that JSVM initialized only if plugin is JSVM or there is virtual endpoint

In past it was initialized if ANY plugin is enabled. Now it checks the driver.

* Ensure that only APIs which changed will be reloaded

If APi checksum has not chaned, re-use and not reload it.
Additionally added logic for JSVM to release holding pointers to Gateway and Spec, so it will not have issues with being garbage collected

Co-authored-by: sredny buitrago <sredny.buitrago@gmail.com>
Co-authored-by: jeff <jeffy.mathew100@gmail.com>
@jeffy-mathew jeffy-mathew changed the title [TT-6635] Fix/jsvm memory usage [TT-5477] Fix/jsvm memory usage Dec 16, 2022
jeffy-mathew added a commit that referenced this pull request Dec 19, 2022
* Ensure that JSVM initialized only if plugin is JSVM or there is virtual endpoint

In past it was initialized if ANY plugin is enabled. Now it checks the driver.

* Ensure that only APIs which changed will be reloaded

If APi checksum has not chaned, re-use and not reload it.
Additionally added logic for JSVM to release holding pointers to Gateway and Spec, so it will not have issues with being garbage collected

Co-authored-by: sredny buitrago <sredny.buitrago@gmail.com>
Co-authored-by: jeff <jeffy.mathew100@gmail.com>
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.

None yet

6 participants