Skip to content

Apache Lucene open-source search software

Add a sysprop "org.apache.lucene.store.MMapDirectory.enableMemorySegments" (#12062)

Uwe Schindler2f602c01dd1c
diff --git a/lucene/CHANGES.txt b/lucene/CHANGES.txt
index 7b9d5b2acf3..8de313f43a0 100644
--- a/lucene/CHANGES.txt
+++ b/lucene/CHANGES.txt
@@ -162,7 +162,9 @@ New Features
* GITHUB#12033: Support for Java 19 foreign memory support is now enabled by default,
no need to pass "--enable-preview" on the command line. If exactly Java 19 is used,
MMapDirectory will mmap Lucene indexes in chunks of 16 GiB (instead of 1 GiB) and
- indexes closed while queries are running can no longer crash the JVM. (Uwe Schindler)
+ indexes closed while queries are running can no longer crash the JVM.
+ To disable this feature, pass the following sysprop on Java command line:
+ "-Dorg.apache.lucene.store.MMapDirectory.enableMemorySegments=false" (Uwe Schindler)
* GITHUB#11869: RangeOnRangeFacetCounts added, supporting numeric range "relationship" faceting over docvalue-stored
ranges. (Marc D'Mello)
diff --git a/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java b/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java
index 2fc3b9e4415..a1495fe33d4 100644
--- a/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java
+++ b/lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java
@@ -22,6 +22,7 @@ import java.lang.invoke.MethodType;
import java.nio.channels.ClosedChannelException; // javadoc @link
import java.nio.file.Path;
import java.util.Locale;
+import java.util.Optional;
import java.util.concurrent.Future;
import java.util.function.BiPredicate;
import java.util.logging.Logger;
@@ -113,6 +114,17 @@ public class MMapDirectory extends FSDirectory {
*/
public static final long DEFAULT_MAX_CHUNK_SIZE;
+ /**
+ * This sysprop allows to control if {@code MemorySegment} API should be used on supported Java
+ * versions. By default it is enabled; set to {@code false} to use legacy {@code ByteBuffer}
+ * implementation. On command line pass {@code
+ * -Dorg.apache.lucene.store.MMapDirectory.enableMemorySegments=false} to disable.
+ *
+ * @lucene.internal
+ */
+ public static final String ENABLE_MEMORY_SEGMENTS_SYSPROP =
+ "org.apache.lucene.store.MMapDirectory.enableMemorySegments";
+
final int chunkSizePower;
/**
@@ -347,7 +359,22 @@ public class MMapDirectory extends FSDirectory {
}
}
+ private static boolean checkMemorySegmentsSysprop() {
+ try {
+ return Optional.ofNullable(System.getProperty(ENABLE_MEMORY_SEGMENTS_SYSPROP))
+ .map(Boolean::valueOf)
+ .orElse(Boolean.TRUE);
+ } catch (
+ @SuppressWarnings("unused")
+ SecurityException ignored) {
+ return true;
+ }
+ }
+
private static MMapIndexInputProvider lookupProvider() {
+ if (checkMemorySegmentsSysprop() == false) {
+ return new MappedByteBufferIndexInputProvider();
+ }
final var lookup = MethodHandles.lookup();
final int runtimeVersion = Runtime.version().feature();
if (runtimeVersion == 19) {