thetumbled commented on code in PR #18390:
URL: https://github.com/apache/pulsar/pull/18390#discussion_r1019133140
##########
pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/ConcurrentLongLongPairHashMap.java:
##########
@@ -327,10 +330,24 @@ LongPair get(long key1, long key2, int keyHash) {
try {
while (true) {
// First try optimistic locking
- long storedKey1 = table[bucket];
- long storedKey2 = table[bucket + 1];
- long storedValue1 = table[bucket + 2];
- long storedValue2 = table[bucket + 3];
+ long storedKey1 = 0;
+ long storedKey2 = 0;
+ long storedValue1 = 0;
+ long storedValue2 = 0;
+ try {
+ storedKey1 = table[bucket];
+ storedKey2 = table[bucket + 1];
+ storedValue1 = table[bucket + 2];
+ storedValue2 = table[bucket + 3];
+ } catch (ArrayIndexOutOfBoundsException e) {
Review Comment:
As long as we use optimistic read, we can't predict when the shrinking
process triggered. it seems that we can only use try/catch block to surround
the array accessing code.
In fact, we can predict the results of code execution.
It is worth noting that following code is not only used for optimistic
reading, but also used for pessimistic reading.
```
try {
storedKey1 = table[bucket];
storedKey2 = table[bucket + 1];
storedValue1 = table[bucket + 2];
storedValue2 = table[bucket + 3];
} catch (ArrayIndexOutOfBoundsException e) {
```
First, execute optimistic reading, that is, execute the above code. After
reading the array content, execute the following code block.
```
if (!acquiredLock && validate(stamp)) {
// The values we have read are consistent
if (key1 == storedKey1 && key2 == storedKey2) {
return new LongPair(storedValue1, storedValue2);
} else if (storedKey1 == EmptyKey) {
// Not found
return null;
}
}
```
At some time, other thread acquire the write lock, we may catch
`ArrayIndexOutOfBoundsException` and then execute the following code block
**for only once**. When acquiring the read lock for the first time, we need to
recalculate the initial value of the traversal bucket, because the capacity may
have changed.
```
if (!acquiredLock) {
stamp = readLock();
acquiredLock = true;
**bucket = signSafeMod(keyHash, capacity);**
storedKey1 = table[bucket];
storedKey2 = table[bucket + 1];
storedValue1 = table[bucket + 2];
storedValue2 = table[bucket + 3];
}
```
After that, the buckets will be traversed **using pessimistic reading**. The
loop will terminate only when the bucket encounters the same `(key1, key2)` or
`EmptyKey`. Because we have acquired pessimistic read lock, we can ensure that
there is no any concurrent problem.
I don't think that there are any unpredictable cases with these code.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]