Skip to content

Commit

Permalink
Implement MMapDirectory with Java 21 Project Panama Preview API (apac…
Browse files Browse the repository at this point in the history
  • Loading branch information
uschindler authored Jun 12, 2023
1 parent 41baf23 commit c8e05c8
Show file tree
Hide file tree
Showing 11 changed files with 729 additions and 14 deletions.
2 changes: 1 addition & 1 deletion buildSrc/scriptDepVersions.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
ext {
scriptDepVersions = [
"apache-rat": "0.14",
"asm": "9.4",
"asm": "9.5",
"commons-codec": "1.13",
"ecj": "3.30.0",
"flexmark": "0.61.24",
Expand Down
2 changes: 1 addition & 1 deletion gradle/generation/extract-jdk-apis.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ configure(rootProject) {
configure(project(":lucene:core")) {
ext {
apijars = file('src/generated/jdk');
mrjarJavaVersions = [ 19, 20 ]
mrjarJavaVersions = [ 19, 20, 21 ]
}

configurations {
Expand Down
3 changes: 2 additions & 1 deletion gradle/generation/extract-jdk-apis/ExtractJdkApis.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,8 @@ public final class ExtractJdkApis {

static final Map<Integer,List<String>> CLASSFILE_PATTERNS = Map.of(
19, List.of(PATTERN_PANAMA_FOREIGN),
20, List.of(PATTERN_PANAMA_FOREIGN, PATTERN_VECTOR_VM_INTERNALS, PATTERN_VECTOR_INCUBATOR)
20, List.of(PATTERN_PANAMA_FOREIGN, PATTERN_VECTOR_VM_INTERNALS, PATTERN_VECTOR_INCUBATOR),
21, List.of(PATTERN_PANAMA_FOREIGN)
);

public static void main(String... args) throws IOException {
Expand Down
2 changes: 1 addition & 1 deletion gradle/template.gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -102,5 +102,5 @@ tests.jvms=@TEST_JVMS@
org.gradle.java.installations.auto-download=true

# Set these to enable automatic JVM location discovery.
org.gradle.java.installations.fromEnv=JAVA17_HOME,JAVA19_HOME,JAVA20_HOME,JAVA21_HOME,RUNTIME_JAVA_HOME
org.gradle.java.installations.fromEnv=JAVA17_HOME,JAVA19_HOME,JAVA20_HOME,JAVA21_HOME,JAVA22_HOME,RUNTIME_JAVA_HOME
#org.gradle.java.installations.paths=(custom paths)
6 changes: 6 additions & 0 deletions lucene/CHANGES.txt
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,12 @@ New Features
thoroughly and report bugs/slowness to Lucene's mailing list.
(Chris Hegarty, Robert Muir, Uwe Schindler)

* GITHUB#12294: Add support for Java 21 foreign memory API. If Java 19 up to 21 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. To disable this feature,
pass the following sysprop on Java command line:
"-Dorg.apache.lucene.store.MMapDirectory.enableMemorySegments=false" (Uwe Schindler)

Improvements
---------------------

Expand Down
Binary file added lucene/core/src/generated/jdk/jdk21.apijar
Binary file not shown.
14 changes: 7 additions & 7 deletions lucene/core/src/java/org/apache/lucene/store/MMapDirectory.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@
* <li>{@code permission java.lang.RuntimePermission "accessClassInPackage.sun.misc";}
* </ul>
*
* <p>On exactly <b>Java 19</b> and <b>Java 20</b> this class will use the modern {@code
* MemorySegment} API which allows to safely unmap (if you discover any problems with this preview
* API, you can disable it by using system property {@link #ENABLE_MEMORY_SEGMENTS_SYSPROP}).
* <p>On exactly <b>Java 19 / 20 / 21</b> this class will use the modern {@code MemorySegment} API
* which allows to safely unmap (if you discover any problems with this preview API, you can disable
* it by using system property {@link #ENABLE_MEMORY_SEGMENTS_SYSPROP}).
*
* <p><b>NOTE:</b> Accessing this class either directly or indirectly from a thread while it's
* interrupted can close the underlying channel immediately if at the same time the thread is
Expand Down Expand Up @@ -123,7 +123,7 @@ public class MMapDirectory extends FSDirectory {
* Default max chunk size:
*
* <ul>
* <li>16 GiBytes for 64 bit <b>Java 19</b> and <b>Java 20</b> JVMs
* <li>16 GiBytes for 64 bit <b>Java 19 / 20 / 21</b> JVMs
* <li>1 GiBytes for other 64 bit JVMs
* <li>256 MiBytes for 32 bit JVMs
* </ul>
Expand Down Expand Up @@ -346,7 +346,7 @@ private static MMapIndexInputProvider lookupProvider() {
}
final var lookup = MethodHandles.lookup();
final int runtimeVersion = Runtime.version().feature();
if (runtimeVersion == 19 || runtimeVersion == 20) {
if (runtimeVersion >= 19 && runtimeVersion <= 21) {
try {
final var cls = lookup.findClass("org.apache.lucene.store.MemorySegmentIndexInputProvider");
// we use method handles, so we do not need to deal with setAccessible as we have private
Expand All @@ -366,9 +366,9 @@ private static MMapIndexInputProvider lookupProvider() {
throw new LinkageError(
"MemorySegmentIndexInputProvider is missing in Lucene JAR file", cnfe);
}
} else if (runtimeVersion >= 21) {
} else if (runtimeVersion >= 22) {
LOG.warning(
"You are running with Java 21 or later. To make full use of MMapDirectory, please update Apache Lucene.");
"You are running with Java 22 or later. To make full use of MMapDirectory, please update Apache Lucene.");
}
return new MappedByteBufferIndexInputProvider();
}
Expand Down
Loading

0 comments on commit c8e05c8

Please sign in to comment.