-
Notifications
You must be signed in to change notification settings - Fork 1k
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] Fix possible potential thread safe bugs #2021
Conversation
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.
Great job, please add unit tests for these changes.
cacheTime[0] = System.currentTimeMillis(); | ||
timeoutMap.put(key, cacheTime); | ||
} | ||
C value = cacheMap.compute(key, (k, v) -> { |
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.
LGTM, please add a unit test.
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.
Thanks, I have added unit tests. Please review.
I think it is possible to use a global lock to solve this problem, the amount of concurrent addnode removenode will not greatly affect performance. |
How about divides the existing |
class CommonCacheTest { | ||
|
||
@Mock | ||
private AbstractConnection<?> mockConnection; |
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.
ConnectionCommonCache
does not depend on external services, so there is no need to use mock
.
Here you can create an anonymous AbstractConnection
class for testing.
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.
Thanks, PTAL.
C value = cacheMap.get(key); | ||
if (value == null) { | ||
log.error("[connection common cache] value is null, remove it, key {}.", key); | ||
cacheMap.remove(key); |
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.
cacheMap.remove(key);
Why was this line of code deleted?
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.
Because cacheMap
uses ConcurrentLinkedHashMap<K, V>
. The implementation of put()
method in ConcurrentLinkedHashMap<K, V>
checks for null value and throws NullPointerException
if value is null. So cache.get(key) == null
is true only when the key does not exist in the map.
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.
Please consider upgrading from CLHM to Caffeine at some opportune time. Thanks!
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.
Please consider upgrading from CLHM to Caffeine at some opportune time. Thanks!
Thanks, we will upgrade to Caffeine in the near future!
Sorry to reply today, personally think this idea is also very good, you can try it. |
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.
👍👍LGTM!
What's changed?
Refactored some code to ensure thread safety. And I think
addNode()
andremoveNode()
inConsistentHash
class may cause thread unsafe issues. Please give some suggestions.Checklist
Add or update API