-
Notifications
You must be signed in to change notification settings - Fork 3.6k
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
[improve] [doc] Add doc for customized util class #21110
Conversation
PTAL, thanks! |
I am not sure that adding this constraints to It is required that if from one thread you modify the map then you see the the effects on the change, in the same thread (or in another thread in case you have some concurrency management utility that guarantees the 'happens before' semantics) If you are scanning the map in a thread and something else is modifying it (from another thread) then it is legit/expected that you see "some values" of the map, before or after the write. |
I will change the implementation as adding a warning in the javadocs, in case of any developers use it incorrectly. |
17f8d35
to
31f4923
Compare
Given the benchmark results provided in the link, it seems prudent to consider replacing the customized ConcurrentMap with ConcurrentHashMap. The superior performance demonstrated by ConcurrentHashMap in put and remove operations surpasses the benefits derived from garbage collection (GC) optimization. |
I agree with @codelipenghui. The reason to implement customized concurrent hash map might be historical and limited to the low Java version (8, or even earlier before Pulsar was open sourced).
These two reasons are not convincing given the benchmark result above. I think we can open a discussion in the mail list to remove these implementations. |
Create a discussion here: https://lists.apache.org/thread/482q36czdprlchj26f8sqw0k3bghmcgb. |
As the replacement of these customized util class will take a long time, we could add the warning to javadocs first. |
...ar-common/src/main/java/org/apache/pulsar/common/util/collections/ConcurrentLongHashMap.java
Show resolved
Hide resolved
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 modify the PR title. I don't think it should be a "fix". It just adds more explanations in the API doc.
Fixed. |
/pulsarbot rerun-failure-checks |
Fixes #21109
Fixes #21113
Motivation
There is bug in
forEach
method in util class likeConcurrentLongLongPairHashMap
.When it detect there is another thread acquire the write lock, it will fall back to acquire the read lock, which is right. But, it will continue to iterate the array from the last time.
There are posibilities that it may read the new value or can't read the new value, which may volilate the consistence of data. This is the problem discribed in [Bug] [broker] concurrent write and iterate with ConcurrentLongLongPairHashMap. #21109.
https://github.com/apache/pulsar/blob/master/pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/ConcurrentLongLongPairHashMap.java#561
is unpredictable. There may be lots of write/remove/clear operations executed in other threads before
foreach
method fall back to acquire the read lock. After the update from other threads, theforeach
method will still iterate the array from the index(bucket) last time, whose result is completly unpredictable.For example in #21113.
In the case of
capacity=4
, the bucket calculated for key(9,9)
and(10,10)
is 1. So, if we put into pair (9,9,99,99) before (10,10,1010,1010), the array content:if we put into pair (10,10,1010,1010) before (9,9,99,99), the array content:
Given above, i construct a test:
writer
thread put into pair(9,9,99,99)
,(10,10,1010,1010)
first. When theforeach
thread read tobucket 1
(that is9 9 99 99
),writer
thread clear all the content and put into pair(10,10,1010,1010)
,(9,9,99,99)
. Then theforeach
thread continue to read the array frombucket 2
, so it will read9 9 99 99
, which is the case where key and value are both same.Modifications
We can not allow update while we iterate the array, or we can not provide any consistence gurantee. We have to remove the optimistic read logic fromforEach
method in all util class.Add warning in javadocs for these util class.
Verifying this change
This change added tests and can be verified as follows:
(example:)
Does this pull request potentially affect one of the following parts:
If the box was checked, please highlight the changes
Documentation
doc
doc-required
doc-not-needed
doc-complete
Matching PR in forked repository
PR in forked repository: thetumbled#29