Fix query plan cache race condition - #31859
Merged
FabianMeiswinkel merged 5 commits intoNov 1, 2022
Merged
Conversation
kushagraThapar
requested review from
FabianMeiswinkel,
aayush3011,
jeet1995,
kirankumarkolli,
milismsft,
simorenoh and
xinlian12
as code owners
November 1, 2022 05:51
Collaborator
|
API change check API changes are not detected in this pull request. |
FabianMeiswinkel
requested changes
Nov 1, 2022
FabianMeiswinkel
left a comment
Member
There was a problem hiding this comment.
I don't understand why this change would ever avoid an OutOfMemoryError - please elaborate in the PR description or code comments to make it easier for others to understand this later.
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Description
Query plan cache
get sizeandputoperation is not synchronized, which is causing a rare race condition when we check the size of the cache and try to clear it.Race Condition: In a multi-threading environment, query plan cache size can grow in a non-synchronized manner. Because of this there is a race condition when another thread checks the below condition to clear query plan cache
If current queryPlanCache size is 999, and there are two threads executing the put operation, then a third thread which enters the if condition will see the query plan cache size as 1001, and will never clear the cache, and hence the cache keeps growing indefinitely.
This is how existing (bug-code) is:
Fix
Synchronizing this above if condition block will make sure the cache is read and updated atomically when we try to put new entries in the cache.
#Test
Added test and ran
queryPlanCacheSizeHitlocally on main to repro and on this branch to make sure that cache never exceeds the allowed size. Currently the test is disabled on the CI (will try to automate it).Perf
-consistencyLevel Eventual
-concurrency 10
-numberOfOperations 1000000
-operation QuerySingle
-connectionMode Direct
-documentDataFieldSize 1
-documentDataFieldCount 1
-numberOfPreCreatedDocuments 1000
main ->
mean rate = 5770 calls/second
P99 = 3.324 milliseconds
P99.9 = 7.374 milliseconds
fix ->
mean rate = 6059 calls/second
P99 = 3.026 milliseconds
P99.9 = 6.972 milliseconds