risdenk commented on code in PR #1032:
URL: https://github.com/apache/solr/pull/1032#discussion_r998862676


##########
solr/core/src/test/org/apache/solr/cloud/TestSizeLimitedDistributedMap.java:
##########
@@ -67,6 +74,58 @@ public void testCleanup() throws Exception {
     }
   }
 
+  public void testConcurrentCleanup() throws Exception {
+    final Set<String> expectedKeys = new HashSet<>();
+    final List<String> deletedItems = new LinkedList<>();
+    int numResponsesToStore = TEST_NIGHTLY ? Overseer.NUM_RESPONSES_TO_STORE : 
100;
+
+    try (SolrZkClient zkClient = new SolrZkClient(zkServer.getZkHost(), 
10000)) {
+      String path = getAndMakeInitialPath(zkClient);
+      DistributedMap map =
+          new SizeLimitedDistributedMap(
+              zkClient, path, numResponsesToStore, (element) -> 
deletedItems.add(element));
+      // fill the map to limit first
+      for (int i = 0; i < numResponsesToStore; i++) {
+        map.put("xyz_" + i, new byte[0]);
+      }
+
+      // add more elements concurrently to trigger cleanup
+      final int THREAD_COUNT = Math.min(100, numResponsesToStore);
+      List<Callable<Object>> callables = new ArrayList<>();
+      for (int i = 0; i < THREAD_COUNT; i++) {
+        final String key = "xyz_" + (numResponsesToStore + 1);
+        expectedKeys.add(key);
+        callables.add(
+            () -> {
+              map.put(key, new byte[0]);
+              return null;
+            });
+      }
+
+      ExecutorService executorService =
+          ExecutorUtil.newMDCAwareFixedThreadPool(
+              THREAD_COUNT, new 
SolrNamedThreadFactory("test-concurrent-cleanup"));
+      List<Future<Object>> futures = new ArrayList<>();
+      for (Callable<Object> callable : callables) {
+        futures.add(executorService.submit(callable));
+      }
+      try {
+        for (Future<Object> future : futures) {
+          future.get(); // none of them should throw exception
+        }
+        for (String expectedKey : expectedKeys) {
+          assertTrue(map.contains(expectedKey));
+        }
+        // there's no guarantees on exactly how many elements will be removed, 
but it should at
+        // least NOT throw exception
+        assertTrue(!deletedItems.isEmpty());
+      } finally {
+        executorService.shutdown();

Review Comment:
   Use ExecutorUtil to do this. It will handle things nicely.



-- 
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: issues-unsubscr...@solr.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@solr.apache.org
For additional commands, e-mail: issues-h...@solr.apache.org

Reply via email to