-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
fix(pubsub): fix memory leak issue in publish scheduler #4282
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
mzanibelli
reviewed
Jun 18, 2021
if !ok { | ||
s.outstanding[key] = 1 | ||
s.outstanding.Store(key, 1) | ||
b = bundler.NewBundler(item, func(bundle interface{}) { | ||
s.workers <- struct{}{} | ||
s.handle(bundle) | ||
<-s.workers | ||
|
||
nlen := reflect.ValueOf(bundle).Len() | ||
s.mu.Lock() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So that's it! You left the lock between Add() and handle() so you don't load non-existing keys from the map inside the else clause. This gave me a hard time and I thought I was doing something wrong when I tried this.
kamalaboulhosn
approved these changes
Jun 21, 2021
gcf-merge-on-green bot
pushed a commit
that referenced
this pull request
Jun 24, 2021
🤖 I have created a release \*beep\* \*boop\* --- ## [1.12.0](https://www.github.com/googleapis/google-cloud-go/compare/pubsub/v1.11.0...pubsub/v1.12.0) (2021-06-23) ### Features * **pubsub/pstest:** add channel to support user-defined publish responses ([#4251](https://www.github.com/googleapis/google-cloud-go/issues/4251)) ([e1304f4](https://www.github.com/googleapis/google-cloud-go/commit/e1304f435fed4a767f4a652f32f1386979ff794f)) ### Bug Fixes * **pubsub:** fix memory leak issue in publish scheduler ([#4282](https://www.github.com/googleapis/google-cloud-go/issues/4282)) ([22ffc18](https://www.github.com/googleapis/google-cloud-go/commit/22ffc18e522c0f943db57f8c943e7356067bedfd)) This PR was generated with [Release Please](https://github.com/googleapis/release-please). See [documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
api: pubsub
Issues related to the Pub/Sub API.
cla: yes
This human has signed the Contributor License Agreement.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR builds off the work completed by @mzanibelli in #4253. A detailed description was written there, but essentially we were instantiating outstanding messages to be 2 instead of 1, leading bundles to never be freed, which leads to memory leaks when using large number of ordering keys. This switches the underlying implementation to a
sync.Map
to properly synchronize access to thebundlers
andoutstanding
maps, allowing the user to safely callFlush
andFlushAndStop
without race conditions.Running benchmarks against both the unordered and ordered publish benchmarks reveals no performance degradation. Below are results from the ordered case, publishing 100k 10kb messages, using different ordering keys.
prev(using built-in maps)
now(using
sync.Map
)In addition, a test was added to
topic_test
to verify the correct behavior of callingPublishScheduler.Add
while aFlush
is in flight.This also fixes a small issue when using the pstest fake's
AddPublishResponse
method which allows you to callPublish
before callingAddPublishResponse
by removing the lock acquisition.Fixes #4288