Apache Solr open-source search software
LUCENE-6745: RAMInputStream.clone was not thread safe (Mike McCandless)
Michael McCandlessbe9471debb05
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txtindex f10f0d6efa0..6c6b7fc0307 100644--- a/lucene/CHANGES.txt+++ b/lucene/CHANGES.txt@@ -79,6 +79,8 @@ Bug Fixestest data. In addition, the performance of those filters was improvedsignificantly. (Uwe Schindler, Robert Muir)+* LUCENE-6745: RAMInputStream.clone was not thread safe (Mike McCandless)+Other* LUCENE-6174: Improve "ant eclipse" to select right JRE for building.diff --git a/lucene/core/src/java/org/apache/lucene/store/RAMInputStream.java b/lucene/core/src/java/org/apache/lucene/store/RAMInputStream.javaindex 2aee222e915..a5b50668ac9 100644--- a/lucene/core/src/java/org/apache/lucene/store/RAMInputStream.java+++ b/lucene/core/src/java/org/apache/lucene/store/RAMInputStream.java@@ -116,7 +116,7 @@ public class RAMInputStream extends IndexInput implements Cloneable {@Overridepublic void seek(long pos) throws IOException {- if (currentBuffer==null || pos < bufferStart || pos >= bufferStart + BUFFER_SIZE) {+ if (currentBuffer == null || pos < bufferStart || pos >= bufferStart + BUFFER_SIZE) {currentBufferIndex = (int) (pos / BUFFER_SIZE);switchCurrentBuffer(false);}@@ -157,4 +157,19 @@ public class RAMInputStream extends IndexInput implements Cloneable {}};}++ @Override+ public RAMInputStream clone() {+ RAMInputStream clone = (RAMInputStream) super.clone();+ // If another thread was using our instance, this new clone could have a mismatched currentBuffer and currentBufferIndex, so we do+ // a "fresh seek" here:+ clone.currentBuffer = null;+ try {+ clone.seek(getFilePointer());+ } catch (IOException ioe) {+ // Should not happen!+ throw new AssertionError(ioe);+ }+ return clone;+ }}diff --git a/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.javaindex c7494048a69..48d915b95e7 100644--- a/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java+++ b/lucene/test-framework/src/java/org/apache/lucene/store/BaseDirectoryTestCase.java@@ -1167,5 +1167,59 @@ public abstract class BaseDirectoryTestCase extends LuceneTestCase {in.close(); // close againdir.close();}-}+ public void testCloneThreadSafety() throws Exception {+ Directory dir = getDirectory(createTempDir());+ IndexOutput out = dir.createOutput("randombytes", IOContext.DEFAULT);++ // Write file with at least 20 K random bytes:+ final int numBytes = atLeast(20*1024);+ final byte[] bytes = new byte[numBytes];+ random().nextBytes(bytes);+ out.writeBytes(bytes, 0, bytes.length);+ out.close();++ // Then read the bytes back at random seek points from multiple threads:+ final IndexInput in = dir.openInput("randombytes", IOContext.DEFAULT);++ int numThreads = 4;+ Thread[] threads = new Thread[numThreads];+ for(int i=0;i<numThreads;i++) {+ int finalI = i;+ threads[i] = new Thread() {+ @Override+ public void run() {+ int numIters = atLeast(1000);+ byte[] scratch = new byte[numBytes];+ for(int iter=0;iter<numIters;iter++) {+ // First thread uses the original IndexInput, all other threads use clone:+ IndexInput myIn;+ if (finalI == 0) {+ myIn = in;+ } else {+ myIn = in.clone();+ }+ int spot = random().nextInt(numBytes/2);+ try {+ myIn.seek(spot);+ int length = numBytes-spot;+ myIn.readBytes(scratch, 0, length);+ for(int i=0;i<length;i++) {+ assertEquals(bytes[spot+i], scratch[i]);+ }+ } catch (IOException ioe) {+ throw new RuntimeException(ioe);+ }+ }+ }+ };+ threads[i].start();+ }++ for(Thread thread : threads) {+ thread.join();+ }+ in.close();+ dir.close();+ }+}