Skip to content
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

Merged
merged 5 commits into from
May 22, 2024

Conversation

gjjjj0101
Copy link
Contributor

What's changed?

Refactored some code to ensure thread safety. And I think addNode() and removeNode() in ConsistentHash class may cause thread unsafe issues. Please give some suggestions.

Checklist

  • I have read the Contributing Guide
  • I have written the necessary doc or comment.
  • I have added the necessary unit tests and all cases have passed.

Add or update API

  • I have added the necessary e2e tests and all cases have passed.

Copy link
Member

@crossoverJie crossoverJie left a 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) -> {
Copy link
Member

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.

Copy link
Contributor Author

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.

@TJxiaobao
Copy link
Contributor

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.

@gjjjj0101
Copy link
Contributor Author

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 addNode and removeNode functions into smaller, synchronized operations: addVMNode and removeVMNode? In this way, addNode and removeNode also can be called in parallel.

class CommonCacheTest {

@Mock
private AbstractConnection<?> mockConnection;
Copy link
Member

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.

Copy link
Contributor Author

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);
Copy link
Member

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?

Copy link
Contributor Author

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.

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!

Copy link
Contributor Author

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!

@TJxiaobao
Copy link
Contributor

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 addNode and removeNode functions into smaller, synchronized operations: addVMNode and removeVMNode? In this way, addNode and removeNode also can be called in parallel.

Sorry to reply today, personally think this idea is also very good, you can try it.

Copy link
Contributor

@tomsun28 tomsun28 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍👍LGTM!

@tomsun28 tomsun28 merged commit ddf1010 into apache:master May 22, 2024
3 checks passed
@gjjjj0101 gjjjj0101 deleted the fix-thread-safe branch May 26, 2024 10:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants