thetumbled commented on PR #18390:
URL: https://github.com/apache/pulsar/pull/18390#issuecomment-1310528449

   > > In the `rehash` method, the member `this.table` of `Section` will be 
reallocated a new long[] array, which is different from old table member.
   > > ```
   > >         private void rehash(int newCapacity) {
   > >             long[] newTable = new long[4 * newCapacity];
   > >             ...
   > >             table = newTable;
   > >             ...
   > >         }
   > > ```
   > 
   > This inspires me. Can we fix the `get` issue in the same way?
   > 
   > ```java
   >             boolean acquiredLock = false;
   >             // add local variable here, so OutOfBound won't happen
   >             long[] table = this.table;
   >             // calculate table.length / 4 as capacity to avoid rehash 
changing capacity
   >             int bucket = signSafeMod(keyHash, table.length / 4);
   > 
   >             try {
   >                 while (true) {
   >                     long storedKey1 = table[bucket];
   >                     long storedKey2 = table[bucket + 1];
   >                     long storedValue1 = table[bucket + 2];
   >                     long storedValue2 = table[bucket + 3];
   >                     
   >                     // compare table to ensure not rehash
   >                     if (!acquiredLock && validate(stamp) && table == 
this.table) {
   >                         if (key1 == storedKey1 && key2 == storedKey2) {
   >                             return new LongPair(storedValue1, 
storedValue2);
   >                         } else if (storedKey1 == EmptyKey) {
   >                             return null;
   >                         }
   >                     } else {
   >                         if (!acquiredLock) {
   >                             stamp = readLock();
   >                             acquiredLock = true;
   >                             // update local variable
   >                             table = this.table;
   >                             bucket = signSafeMod(keyHash, capacity);
   >                             storedKey1 = table[bucket];
   >                             storedKey2 = table[bucket + 1];
   >                             storedValue1 = table[bucket + 2];
   >                             storedValue2 = table[bucket + 3];
   >                         }
   > ```
   
   It can also fix the bug. But is `&& table == this.table` indispensable?  If 
`table != this.table` ,there must be other thread acquire write lock and change 
`this.table`, which make `validate(stamp)` return false. 


-- 
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]

Reply via email to