gortiz commented on code in PR #10528:
URL: https://github.com/apache/pinot/pull/10528#discussion_r1212713174


##########
pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/memory/unsafe/MmapMemory.java:
##########
@@ -0,0 +1,348 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.pinot.segment.spi.memory.unsafe;
+
+import com.google.common.collect.Lists;
+import java.io.File;
+import java.io.IOException;
+import java.io.RandomAccessFile;
+import java.io.UncheckedIOException;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.nio.channels.FileChannel;
+import java.util.List;
+import java.util.function.BiConsumer;
+import net.openhft.chronicle.core.Jvm;
+import net.openhft.chronicle.core.OS;
+import net.openhft.posix.MSyncFlag;
+import net.openhft.posix.PosixAPI;
+import org.apache.pinot.segment.spi.utils.JavaVersion;
+
+
+public class MmapMemory implements Memory {
+
+  private static final MapFun MAP_FUN;
+
+  /**
+   * The address actually mapped. It has to be page aligned.
+   *
+   * {@code _address = _offset - offset % pageSize}
+   */
+  private final long _address;
+  /**
+   * The offset requested to map.
+   *
+   * {@code _address = _offset - offset % pageSize}
+   */
+  private final long _offset;
+  /**
+   * How many bytes have been requested to be mapped.
+   * The actual mapped size may be larger (up to the next page), but the 
actual mapped size
+   * is stored by {@link #_section}.
+   */
+  private final long _size;
+  private final MapSection _section;
+
+  static {
+    try {
+      Jvm.init();
+      MAP_FUN = MapFun.find();
+    } catch (ClassNotFoundException | NoSuchMethodException e) {
+      throw new RuntimeException(e);
+    }
+  }
+
+  public MmapMemory(File file, boolean readOnly, long offset, long size) {
+    _size = size;
+    _offset = offset;
+
+    try {
+      _section = MAP_FUN.map(file, readOnly, offset, size);
+    } catch (IOException e) {
+      throw new RuntimeException(e);
+    }
+    _address = _section.getAddress();
+  }
+
+  @Override
+  public long getAddress() {
+    return _address;
+  }
+
+  @Override
+  public long getSize() {
+    return _size;
+  }
+
+  @Override
+  public void flush() {
+    MSyncFlag mode = MSyncFlag.MS_SYNC;
+    PosixAPI.posix().msync(_offset, _size, mode);
+  }
+
+  @Override
+  public void close()
+      throws IOException {
+    try {
+      _section._unmapFun.unmap();
+    } catch (InvocationTargetException | IllegalAccessException e) {
+      throw new RuntimeException("Error while calling unmap", e);
+    }
+  }
+
+  private static class MapSection {
+    public static final MapSection EMPTY = new MapSection(0, () -> {
+    });
+    private final long _address;
+    private final UnmapFun _unmapFun;
+
+    public MapSection(long address, UnmapFun unmapFun) {
+      _address = address;
+      _unmapFun = unmapFun;
+    }
+
+    public long getAddress() {
+      return _address;
+    }
+
+    public UnmapFun getUnmapFun() {
+      return _unmapFun;
+    }
+  }
+
+  interface MapFun {
+
+    /**
+     * @param file The file to be mapped. If its length is lower than offset + 
size and the mode is not read only,
+     *            the file will be resized to that size.
+     * @param offset The offset in the file. Any positive value is valid, even 
if it is larger than the file size.
+     * @param size How many bytes to map.
+     * @throws IOException in several situations. For example, if the offset + 
size is larger than file length and the
+     * mode is read only or if the process doesn't have permission to read or 
modify the file.
+     */
+    MapSection map(File file, boolean readOnly, long offset, long size) throws 
IOException;
+
+    static MapFun find()
+        throws ClassNotFoundException, NoSuchMethodException {
+      List<Finder<? extends MapFun>> candidates = Lists.newArrayList(
+          new Map0Fun.ChronicleCore(),

Review Comment:
   > shouldn't the options after ChronicleCore be in reverse order i.e. Java20 
first, followed by 17 then 11?
   
   Option 2 is prioritize Chronicle Core. Therefore it should be the first one. 
If that one fails (the internal methods cannot be found) then it will try to 
use my code. Option 3 would be to place Chronicle Core at the end, so it will 
be used as fallback
   
   >  If we're running on Java 20, won't the older java methods and classes 
still be found?
   
   No. The reason why we need to add different ways to look for the method is 
that these methods are removed/renamed/changed on each JVM. So the method used 
in Java 8 will not be found in Java 20 (and the other way around).
   
   The JVM can break compatibility with these methods because they are private 
and therefore by changing them they do not break backward compatibility.



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

To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org

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


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

Reply via email to