You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@dubbo.apache.org by "tangcent (GitHub)" <gi...@apache.org> on 2020/04/14 23:31:09 UTC
[GitHub] [dubbo] tangcent opened pull request #6018: [Dubbo-5987] Fix
lock in DubboProtocol#getSharedClient
## What is the purpose of the change
- Refactor getSharedClient
- see #5987
## Brief changelog
- The current lock mode
```java
locks.putIfAbsent(key, new Object());
synchronized (locks.get(key)) {
//do some thing
//remove lock
locks.remove(key);
}
```
- The situation which there is a problem:
When thread A finishes executing locks.putIfAbsent(key, new Object()),
another thread B which holding the lock executes locks.remove(key),
and then thread A executes synchronized (locks.get(key)).
- Attempt get lock by computeIfAbsent like this:
```java
synchronized (locks.computeIfAbsent(key, (k) -> new Object())) {
try{
//do some thing
} finally {
//remove lock
locks.remove(key);
}
}
```
- The situation which there is a problem:
1.When thread A finishes executing `synchronized (locks.computeIfAbsent(key, (k) -> new Object()))` create the lock `LockA` And synchronized ,
2.Another thread B synchronized with `LockA`,
3.Thread A executes `locks.remove(key)`.
4.Thread B hold the `LockA` And synchronized.
5.Thread C executing `synchronized (locks.computeIfAbsent(key, (k) -> new Object()))` create the lock `LockC` And synchronized .
-It seems difficult to remove a lock without any threads waiting for it exactly.
## Verifying this change
Run test cases.
Follow this checklist to help us incorporate your contribution quickly and easily:
- [x] Make sure there is a [GITHUB_issue](https://github.com/apache/dubbo/issues) 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.
- [x] 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.
- [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
- [x] 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](https://github.com/apache/dubbo-samples) project.
- [x] Run `mvn clean install -DskipTests=false` & `mvn clean test-compile failsafe:integration-test` to make sure unit-test and integration-test pass.
- [x] If this contribution is large, please follow the [Software Donation Guide](https://github.com/apache/dubbo/wiki/Software-donation-guide).
[ Full content available at: https://github.com/apache/dubbo/pull/6018 ]
This message was relayed via gitbox.apache.org for notifications@dubbo.apache.org