Skip to content

Apache Lucene open-source search software

LUCENE-8673: Avoid OOMEs because of IOContext randomization.

Adrien Grandb6f31835ad18
diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
index 545b57b55fc..213d7d7b71a 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
@@ -1332,7 +1332,7 @@ public abstract class LuceneTestCase extends Assert {
* See {@link #newDirectory()} for more information.
*/
public static BaseDirectoryWrapper newDirectory(Random r) {
- return wrapDirectory(r, newDirectoryImpl(r, TEST_DIRECTORY), rarely(r));
+ return wrapDirectory(r, newDirectoryImpl(r, TEST_DIRECTORY), rarely(r), false);
}
/**
@@ -1340,7 +1340,7 @@ public abstract class LuceneTestCase extends Assert {
* See {@link #newDirectory()} for more information.
*/
public static BaseDirectoryWrapper newDirectory(Random r, LockFactory lf) {
- return wrapDirectory(r, newDirectoryImpl(r, TEST_DIRECTORY, lf), rarely(r));
+ return wrapDirectory(r, newDirectoryImpl(r, TEST_DIRECTORY, lf), rarely(r), false);
}
public static MockDirectoryWrapper newMockDirectory() {
@@ -1348,11 +1348,11 @@ public abstract class LuceneTestCase extends Assert {
}
public static MockDirectoryWrapper newMockDirectory(Random r) {
- return (MockDirectoryWrapper) wrapDirectory(r, newDirectoryImpl(r, TEST_DIRECTORY), false);
+ return (MockDirectoryWrapper) wrapDirectory(r, newDirectoryImpl(r, TEST_DIRECTORY), false, false);
}
public static MockDirectoryWrapper newMockDirectory(Random r, LockFactory lf) {
- return (MockDirectoryWrapper) wrapDirectory(r, newDirectoryImpl(r, TEST_DIRECTORY, lf), false);
+ return (MockDirectoryWrapper) wrapDirectory(r, newDirectoryImpl(r, TEST_DIRECTORY, lf), false, false);
}
public static MockDirectoryWrapper newMockFSDirectory(Path f) {
@@ -1416,10 +1416,7 @@ public abstract class LuceneTestCase extends Assert {
}
Directory fsdir = newFSDirectoryImpl(clazz, f, lf);
- if (rarely()) {
-
- }
- BaseDirectoryWrapper wrapped = wrapDirectory(random(), fsdir, bare);
+ BaseDirectoryWrapper wrapped = wrapDirectory(random(), fsdir, bare, true);
return wrapped;
} catch (Exception e) {
Rethrow.rethrow(e);
@@ -1447,11 +1444,13 @@ public abstract class LuceneTestCase extends Assert {
impl.copyFrom(d, file, file, newIOContext(r));
}
}
- return wrapDirectory(r, impl, rarely(r));
+ return wrapDirectory(r, impl, rarely(r), false);
}
- private static BaseDirectoryWrapper wrapDirectory(Random random, Directory directory, boolean bare) {
- if (rarely(random) && !bare) {
+ private static BaseDirectoryWrapper wrapDirectory(Random random, Directory directory, boolean bare, boolean filesystem) {
+ // IOContext randomization might make NRTCachingDirectory make bad decisions, so avoid
+ // using it if the user requested a filesystem directory.
+ if (rarely(random) && !bare && filesystem == false) {
directory = new NRTCachingDirectory(directory, random.nextDouble(), random.nextDouble());
}