gortiz commented on code in PR #10528: URL: https://github.com/apache/pinot/pull/10528#discussion_r1218402845
########## pinot-segment-spi/src/main/java/org/apache/pinot/segment/spi/memory/unsafe/MmapMemory.java: ########## @@ -0,0 +1,377 @@ +/** + * 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; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +/** + * A {@link Memory} that whose bytes are mapped on a file. + */ +public class MmapMemory implements Memory { + private static final Logger LOGGER = LoggerFactory.getLogger(MmapMemory.class); + + private static final MapFun MAP_FUN; + + /** + * The address that correspond to the offset given at creation time. + * + * The actual mapping address may be smaller than this value, as usually memory map must start on an address that is + * page aligned. + */ + private final long _address; + /** + * 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; + private boolean _closed = false; + + 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; + + 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(_address, _size, mode); + } + + @Override + public void close() + throws IOException { + try { + if (!_closed) { + synchronized (this) { + if (!_closed) { + _section._unmapFun.unmap(); + _closed = true; + } + } + } + } catch (InvocationTargetException | IllegalAccessException e) { + throw new RuntimeException("Error while calling unmap", e); + } + } + + @Override + protected void finalize() + throws Throwable { + if (!_closed) { + LOGGER.warn("Mmap section of " + _size + " wasn't explicitly closed"); + close(); + } + super.finalize(); + } + + 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; + } + } + + /** + * This is a factory method that can be used to create {@link MapSection}s. + * + * Each JVM may provide different method to map files in memory. + */ + 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(), + new Map0Fun.Java11(), + new Map0Fun.Java17(), + new Map0Fun.Java20() + ); + + for (Finder<? extends MapFun> candidate : candidates) { + try { + return candidate.tryFind(); + } catch (NoSuchMethodException | ClassNotFoundException | AssertionError e) { + // IGNORE + } + } + throw new NoSuchMethodException("Cannot find how to create memory map files in Java " + JavaVersion.VERSION); + } + } + + /** + * As defined by POSIX, the map0 method requires that the offset is page aligned. Failing to do that may produce + * segfault errors. This interface is a {@link MapFun} that does some sanitation before calling the map method. + * They include: + * <ul> + * <li>Grow the file if the last mapped byte is larger than the file length.</li> + * <li>Align the offset with the previous page. This means that we need to correct the actual mapped address.</li> + * </ul> + */ + interface Map0Fun extends MapFun { + + /** + * @param pageAlignedOffset It has to be a positive value that is page aligned. + */ + MapSection map0(FileChannel fc, boolean readOnly, long pageAlignedOffset, long size) Review Comment: hahaha, good question! There are 4 implementations of this interface: Map0Fun.ChronicleCore, Map0Fun.Java11, Map0Fun.Java17 and Map0Fun.Java20. All these implementations are lambda functions. After Java 8, interfaces with a single abstract method can be implemented by using an lambda with the same number of arguments and same return value. That is how, for example, you can implement a standard `java.util.function.Consumer` with a lambda. For example, a Consumer is defined as: ``` @FunctionalInterface public interface Consumer<T> { /** * Performs this operation on the given argument. * * @param t the input argument */ void accept(T t); /** * Returns a composed {@code Consumer} that performs, in sequence, this * operation followed by the {@code after} operation. If performing either * operation throws an exception, it is relayed to the caller of the * composed operation. If performing this operation throws an exception, * the {@code after} operation will not be performed. * * @param after the operation to perform after this operation * @return a composed {@code Consumer} that performs in sequence this * operation followed by the {@code after} operation * @throws NullPointerException if {@code after} is null */ default Consumer<T> andThen(Consumer<? super T> after) { Objects.requireNonNull(after); return (T t) -> { accept(t); after.accept(t); }; } } ``` Then you can say something like: ``` Consumer<Instant> aConsumer = (Instant i) -> System.out.println(i.toString()) ``` and it is almost the same as writing: ``` Consumer<Instant> aConsumer = new Consumer<Instant> { void accept(Instant i) { System.out.println(i.toString()); } }; ``` It is not exactly the same because internally Java can do other optimizations. For example, in the case where we use `new Consumer<Instant>` Java needs to create a new instance each time the code executes that line, but in the lambda case the compiler can see that the lambda is not capturing and therefore create a single instance that will be shared between all executions. BTW, you can see that Consumer interface declaration is decorated with `@FunctionalInterface`. This is an informative annotation that can be used to indicate that the interface is intended to be implemented by lambdas. It is not a requirement (and you can see I didn't use it in Map0Fun) but if added, the compiler can verify that the interface can actually be implemented as a lambda (in other words, it verifies that the interface has exactly one single not default method) -- 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