madrob commented on a change in pull request #2429:
URL: https://github.com/apache/lucene-solr/pull/2429#discussion_r583176395



##########
File path: lucene/core/src/java/org/apache/lucene/util/BytesRefHash.java
##########
@@ -31,18 +31,21 @@
  * to the id is encapsulated inside {@link BytesRefHash} and is guaranteed to 
be increased for each
  * added {@link BytesRef}.
  *
+ * <p><strong>Note that this implementation is not synchronized.</strong> If 
multiple threads access
+ * a {@link BytesRefHash} instance concurrently, and at least one of the 
threads modifies it
+ * structurally, it <i>must</i> be synchronized externally. (A structural 
modification is any
+ * operation on the map except operations explicitly listed in {@link 
UnmodifiableBytesRefHash}

Review comment:
       not a map

##########
File path: lucene/core/src/test/org/apache/lucene/util/TestBytesRefHash.java
##########
@@ -267,6 +271,71 @@ public void testFind() throws Exception {
     }
   }
 
+  @Test
+  public void testConcurrentAccessToUnmodifiableBytesRefHash() throws 
Exception {
+    int num = atLeast(2);
+    for (int j = 0; j < num; j++) {
+      int numStrings = 797;
+      List<String> strings = new ArrayList<>(numStrings);
+      for (int i = 0; i < numStrings; i++) {
+        final String str = TestUtil.randomRealisticUnicodeString(random(), 1, 
1000);
+        hash.add(new BytesRef(str));
+        assertTrue(strings.add(str));
+      }
+      int hashSize = hash.size();
+
+      UnmodifiableBytesRefHash unmodifiableHash = hash;
+
+      AtomicInteger notFound = new AtomicInteger();
+      AtomicInteger notEquals = new AtomicInteger();
+      AtomicInteger wrongSize = new AtomicInteger();
+      int numThreads = 10;
+      CountDownLatch latch = new CountDownLatch(numThreads);
+      Thread[] threads = new Thread[numThreads];
+      for (int i = 0; i < threads.length; i++) {
+        int loops = atLeast(100);
+        threads[i] =
+            new Thread(
+                () -> {
+                  BytesRef scratch = new BytesRef();
+                  latch.countDown();
+                  try {
+                    latch.await();
+                  } catch (InterruptedException e) {

Review comment:
       This should fail the test?

##########
File path: lucene/core/src/java/org/apache/lucene/util/BytesRefHash.java
##########
@@ -31,18 +31,21 @@
  * to the id is encapsulated inside {@link BytesRefHash} and is guaranteed to 
be increased for each
  * added {@link BytesRef}.
  *
+ * <p><strong>Note that this implementation is not synchronized.</strong> If 
multiple threads access
+ * a {@link BytesRefHash} instance concurrently, and at least one of the 
threads modifies it
+ * structurally, it <i>must</i> be synchronized externally. (A structural 
modification is any
+ * operation on the map except operations explicitly listed in {@link 
UnmodifiableBytesRefHash}
+ * interface).
+ *
  * <p>Note: The maximum capacity {@link BytesRef} instance passed to {@link 
#add(BytesRef)} must not
  * be longer than {@link ByteBlockPool#BYTE_BLOCK_SIZE}-2. The internal 
storage is limited to 2GB
  * total byte storage.
  *
  * @lucene.internal
  */
-public final class BytesRefHash implements Accountable {
+public final class BytesRefHash implements Accountable, 
UnmodifiableBytesRefHash {

Review comment:
       this feels weird




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

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



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

Reply via email to