101
101
import java .util .Optional ;
102
102
import java .util .UUID ;
103
103
import java .util .concurrent .ConcurrentHashMap ;
104
- import java .util .concurrent .ConcurrentMap ;
105
104
import java .util .concurrent .CountDownLatch ;
106
105
import java .util .concurrent .ExecutorService ;
107
106
import java .util .concurrent .Executors ;
@@ -491,7 +490,8 @@ public void testStaleCount_OnRemovalNotificationOfStaleKey_DecrementsStaleCount(
491
490
indexShard .hashCode ()
492
491
);
493
492
// test the mapping
494
- ConcurrentMap <ShardId , ConcurrentMap <String , Integer >> cleanupKeyToCountMap = cache .cacheCleanupManager .getCleanupKeyToCountMap ();
493
+ ConcurrentHashMap <ShardId , ConcurrentHashMap <String , Integer >> cleanupKeyToCountMap = cache .cacheCleanupManager
494
+ .getCleanupKeyToCountMap ();
495
495
// shard id should exist
496
496
assertTrue (cleanupKeyToCountMap .containsKey (shardId ));
497
497
// reader CacheKeyId should NOT exist
@@ -554,7 +554,8 @@ public void testStaleCount_OnRemovalNotificationOfNonStaleKey_DoesNotDecrementsS
554
554
);
555
555
556
556
// test the mapping
557
- ConcurrentMap <ShardId , ConcurrentMap <String , Integer >> cleanupKeyToCountMap = cache .cacheCleanupManager .getCleanupKeyToCountMap ();
557
+ ConcurrentHashMap <ShardId , ConcurrentHashMap <String , Integer >> cleanupKeyToCountMap = cache .cacheCleanupManager
558
+ .getCleanupKeyToCountMap ();
558
559
// shard id should exist
559
560
assertTrue (cleanupKeyToCountMap .containsKey (shardId ));
560
561
// reader CacheKeyId should NOT exist
@@ -722,7 +723,8 @@ public void testCleanupKeyToCountMapAreSetAppropriately() throws Exception {
722
723
cache .getOrCompute (getEntity (indexShard ), getLoader (reader ), reader , getTermBytes ());
723
724
assertEquals (1 , cache .count ());
724
725
// test the mappings
725
- ConcurrentMap <ShardId , ConcurrentMap <String , Integer >> cleanupKeyToCountMap = cache .cacheCleanupManager .getCleanupKeyToCountMap ();
726
+ ConcurrentHashMap <ShardId , ConcurrentHashMap <String , Integer >> cleanupKeyToCountMap = cache .cacheCleanupManager
727
+ .getCleanupKeyToCountMap ();
726
728
assertEquals (1 , (int ) cleanupKeyToCountMap .get (shardId ).get (getReaderCacheKeyId (reader )));
727
729
728
730
cache .getOrCompute (getEntity (indexShard ), getLoader (secondReader ), secondReader , getTermBytes ());
@@ -796,15 +798,15 @@ public void testCleanupKeyToCountMapAreSetAppropriately() throws Exception {
796
798
}
797
799
798
800
// test adding to cleanupKeyToCountMap with multiple threads
799
- public void testAddToCleanupKeyToCountMap () throws Exception {
801
+ public void testAddingToCleanupKeyToCountMapWorksAppropriatelyWithMultipleThreads () throws Exception {
800
802
threadPool = getThreadPool ();
801
803
Settings settings = Settings .builder ().put (INDICES_REQUEST_CACHE_STALENESS_THRESHOLD_SETTING .getKey (), "51%" ).build ();
802
804
cache = getIndicesRequestCache (settings );
803
805
804
806
int numberOfThreads = 10 ;
805
807
int numberOfIterations = 1000 ;
806
808
Phaser phaser = new Phaser (numberOfThreads + 1 ); // +1 for the main thread
807
- AtomicBoolean exceptionDetected = new AtomicBoolean (false );
809
+ AtomicBoolean concurrentModificationExceptionDetected = new AtomicBoolean (false );
808
810
809
811
ExecutorService executorService = Executors .newFixedThreadPool (numberOfThreads );
810
812
@@ -817,7 +819,7 @@ public void testAddToCleanupKeyToCountMap() throws Exception {
817
819
}
818
820
} catch (ConcurrentModificationException e ) {
819
821
logger .error ("ConcurrentModificationException detected in thread : " + e .getMessage ());
820
- exceptionDetected .set (true ); // Set flag if exception is detected
822
+ concurrentModificationExceptionDetected .set (true ); // Set flag if exception is detected
821
823
}
822
824
});
823
825
}
@@ -836,13 +838,17 @@ public void testAddToCleanupKeyToCountMap() throws Exception {
836
838
}
837
839
} catch (ConcurrentModificationException e ) {
838
840
logger .error ("ConcurrentModificationException detected in main thread : " + e .getMessage ());
839
- exceptionDetected .set (true ); // Set flag if exception is detected
841
+ concurrentModificationExceptionDetected .set (true ); // Set flag if exception is detected
840
842
}
841
843
});
842
844
843
845
executorService .shutdown ();
844
- executorService .awaitTermination (60 , TimeUnit .SECONDS );
845
- assertFalse (exceptionDetected .get ());
846
+ assertTrue (executorService .awaitTermination (60 , TimeUnit .SECONDS ));
847
+ assertEquals (
848
+ numberOfThreads * numberOfIterations ,
849
+ cache .cacheCleanupManager .getCleanupKeyToCountMap ().get (indexShard .shardId ()).size ()
850
+ );
851
+ assertFalse (concurrentModificationExceptionDetected .get ());
846
852
}
847
853
848
854
private IndicesRequestCache getIndicesRequestCache (Settings settings ) {
0 commit comments