To fix concurrency problems in RpcStatus and RoundRobinLoadBalance#5881
Merged
chickenlj merged 3 commits intoapache:masterfrom Apr 7, 2020
Merged
To fix concurrency problems in RpcStatus and RoundRobinLoadBalance#5881chickenlj merged 3 commits intoapache:masterfrom
chickenlj merged 3 commits intoapache:masterfrom
Conversation
In some extreme cases, incrementAndGet and decrementAndGet will cause the method 'beginCount' to returen incorrectly if max is 10, active is 10 now,here are 3 thread, thread-b calls the metod 'endCount' to decrease the active thread-a thread-b thread-c active's value increase ------ ------ 11 ------ decrease ------ 10 ------ ------ increase 11 decrease ------ ------ 10 ------ ------ decrease 9 failed ------ failed ------------------------------------- both a and c failed, but active's value is 9 now, it's less than max
af898fd to
52a75a2
Compare
use 'computeIfAbsent' to simplify code, make it thread-safe(it may get null) and efficient(3 times vs 1 time searching in hash map) remove 'updateLock', ConcurrentMap is thread-safe already, no need to do 'copy -> modify -> update reference', unfortunately, it will lost data in some extreme cases in that way.
Codecov Report
@@ Coverage Diff @@
## master #5881 +/- ##
============================================
+ Coverage 61.22% 61.28% +0.05%
- Complexity 496 499 +3
============================================
Files 981 981
Lines 39147 39149 +2
Branches 5645 5646 +1
============================================
+ Hits 23968 23991 +23
+ Misses 12547 12540 -7
+ Partials 2632 2618 -14 Continue to review full report at Codecov.
|
format the code
Contributor
|
LGTM. |
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.
What is the purpose of the change
to fix some concurrency problems
RpcStatus.java
In some extreme cases, incrementAndGet and decrementAndGet will cause the method
"beginCount" to returen incorrectly
if max is 10, active is 10 now,here are 3 thread, thread-b calls the metod 'endCount' to decrease the active
both a and c failed, but active's value is 9 now, it's less than max
RoundRobinLoadBalance.java
let's look at this situation:
there are two thread A and B, A copied the old map to an new map, B put a new WeightedRoundRobin into the old map, and go on to try to get lock, it will failed becuase A hold the lock now, A replace the old map with new map. So the new WeightedRoundRobin object was lost!
ConcurrentHashMap is thread-safe, includes its iterator, it's OK to remove items in original map.
Brief changelog
replace increase and decrease with CAS loop
use 'computeIfAbsent' to replace 'get-create-putIfAbsent-get'
remove 'updateLock'
Verifying this change
it's obvious
Follow this checklist to help us incorporate your contribution quickly and easily:
[Dubbo-XXX] Fix UnknownException when host config not exist #XXX. Each commit in the pull request should have a meaningful subject line and body.mvn clean install -DskipTests=false&mvn clean test-compile failsafe:integration-testto make sure unit-test and integration-test pass.