thetumbled opened a new issue, #21113:
URL: https://github.com/apache/pulsar/issues/21113

   ### Search before asking
   
   - [X] I searched in the [issues](https://github.com/apache/pulsar/issues) 
and found nothing similar.
   
   
   ### Version
   
   all.
   
   ### Minimal reproduce step
   
   Concurrent write while iterate the map.
   ```
   @Test
       public void testConcurrentWriteAndIterateV2()  throws Throwable {
           ConcurrentLongLongPairHashMap map = 
ConcurrentLongLongPairHashMap.newBuilder()
                   .expectedItems(2)
                   .concurrencyLevel(1)
                   .autoShrink(true)
                   .mapIdleFactor(0.25f)
                   .build();
           assertEquals(map.capacity(), 4);
   
           ExecutorService executor = Executors.newCachedThreadPool();
           final int readThreads = 16;
           final int writeThreads = 1;
           final int n = 2_000;
           CyclicBarrier barrier = new CyclicBarrier(writeThreads + 
readThreads);
           Future<?> future = null;
           AtomicReference<Exception> ex = new AtomicReference<>();
   
           for (int i = 0; i < readThreads; i++) {
               executor.submit(() -> {
                   try {
                       barrier.await();
                   } catch (Exception e) {
                       throw new RuntimeException(e);
                   }
                   Map<Long, Long> keys = new HashMap<>();
                   while (true) {
                       map.forEach((k1, k2, v1, v2) -> {
                           if(keys.containsKey(k1)) {
                               ex.set(new Exception("Duplicate mapping pair 
with same key: (" + k1
                                       + "," + keys.get(k1) + "), (" + k1 + "," 
+ v1 + ")"));
                           }
                           keys.put(k1, v1);
                       });
                       keys.clear();
                   }
               });
           }
   
           future = executor.submit(() -> {
               try {
                   barrier.await();
               } catch (Exception e) {
                   throw new RuntimeException(e);
               }
   
               for (int i = 0; i < n; i++) {
                   // expand hashmap
                   assertTrue(map.put(9, 9, 99, 99));
                   assertTrue(map.put(10, 10, 1010, 1010));
   
                   // shrink hashmap
                   map.clear();
                   assertEquals(map.capacity(), 4);
   
                   assertTrue(map.put(10, 10, 99, 99));
                   assertTrue(map.put(9, 9, 1010, 1010));
   
                   // shrink hashmap
                   map.clear();
                   assertEquals(map.capacity(), 4);
               }
           });
   
           future.get();
           System.out.println(ex.get());
           assertTrue(ex.get() == null);
           // shut down pool
           executor.shutdown();
       }
   ```
   
   
   ### What did you expect to see?
   
   I expect that the results of iteration of map, there can't be two mapping 
pair with the same key whatever the value is same or not.
   
   
   ### What did you see instead?
   
   There are two mapping pair with the same key, with or without same value.
   Running the test code above, i get the result like this:
   ```
   java.lang.Exception: Duplicate mapping pair with same key: (9,99), (9,1010)
   
   java.lang.AssertionError
        at org.junit.Assert.fail(Assert.java:86)
        at org.junit.Assert.assertTrue(Assert.java:41)
        at org.junit.Assert.assertTrue(Assert.java:52)
        at 
org.apache.bookkeeper.util.collections.ConcurrentLongLongPairHashMapTest.testConcurrentWriteAndIterateV2(ConcurrentLongLongPairHashMapTest.java:397)
   ```
   <img width="885" alt="image" 
src="https://github.com/apache/pulsar/assets/52550727/8fe79788-a06a-4300-b6ab-29251fc7c9e7";>
   
   
   ### Anything else?
   
   _No response_
   
   ### Are you willing to submit a PR?
   
   - [X] I'm willing to submit a PR!


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