Skip to content

To fix concurrency problems in RpcStatus and RoundRobinLoadBalance#5881

Merged
chickenlj merged 3 commits intoapache:masterfrom
trytocatch:master
Apr 7, 2020
Merged

To fix concurrency problems in RpcStatus and RoundRobinLoadBalance#5881
chickenlj merged 3 commits intoapache:masterfrom
trytocatch:master

Conversation

@trytocatch
Copy link
Copy Markdown
Contributor

@trytocatch trytocatch commented Mar 17, 2020

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

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

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:

  • Make sure there is a GITHUB_issue field for the change (usually before you start working on it). Trivial changes like typos do not require a GITHUB issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue.
  • Format the pull request title like [Dubbo-XXX] Fix UnknownException when host config not exist #XXX. Each commit in the pull request should have a meaningful subject line and body.
  • Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
  • Write necessary unit-test to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add sample in dubbo samples project.
  • Run mvn clean install -DskipTests=false & mvn clean test-compile failsafe:integration-test to make sure unit-test and integration-test pass.
  • If this contribution is large, please follow the Software Donation Guide.

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
@trytocatch trytocatch changed the title [Dubbo-2.7.7]To fix a concurrency problem in RpcStatus.java To fix a concurrency problem in RpcStatus.java Mar 17, 2020
@trytocatch trytocatch force-pushed the master branch 2 times, most recently from af898fd to 52a75a2 Compare March 19, 2020 02:55
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-io
Copy link
Copy Markdown

codecov-io commented Mar 19, 2020

Codecov Report

Merging #5881 into master will increase coverage by 0.05%.
The diff coverage is 100.00%.

Impacted file tree graph

@@             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     
Impacted Files Coverage Δ Complexity Δ
.../src/main/java/org/apache/dubbo/rpc/RpcStatus.java 58.33% <100.00%> (+1.01%) 0.00 <0.00> (ø)
...pache/dubbo/remoting/transport/AbstractServer.java 53.75% <0.00%> (-3.75%) 0.00% <0.00%> (ø%)
...pache/dubbo/registry/support/AbstractRegistry.java 78.06% <0.00%> (-1.49%) 0.00% <0.00%> (ø%)
...org/apache/dubbo/registry/redis/RedisRegistry.java 47.12% <0.00%> (+0.27%) 32.00% <0.00%> (+1.00%)
...g/apache/dubbo/registry/consul/ConsulRegistry.java 62.73% <0.00%> (+0.62%) 30.00% <0.00%> (ø%)
.../dubbo/remoting/transport/netty4/NettyChannel.java 68.31% <0.00%> (+1.98%) 0.00% <0.00%> (ø%)
...e/dubbo/remoting/transport/netty/NettyChannel.java 55.68% <0.00%> (+3.40%) 20.00% <0.00%> (+1.00%)
...he/dubbo/remoting/transport/netty/NettyServer.java 73.68% <0.00%> (+3.50%) 9.00% <0.00%> (+1.00%)
...dubbo/remoting/exchange/support/DefaultFuture.java 88.79% <0.00%> (+9.48%) 0.00% <0.00%> (ø%)
.../apache/dubbo/rpc/protocol/AsyncToSyncInvoker.java 82.75% <0.00%> (+10.34%) 0.00% <0.00%> (ø%)
... and 2 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update d55f062...84ad0cd. Read the comment docs.

format the code
@trytocatch trytocatch changed the title To fix a concurrency problem in RpcStatus.java To fix concurrency problems in RpcStatus and RoundRobinLoadBalance Mar 19, 2020
@chickenlj
Copy link
Copy Markdown
Contributor

LGTM.

@chickenlj chickenlj merged commit 317e62f into apache:master Apr 7, 2020
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.

3 participants