sashapolo commented on code in PR #7299:
URL: https://github.com/apache/ignite-3/pull/7299#discussion_r2643910958


##########
modules/raft/src/main/java/org/apache/ignite/internal/raft/storage/segstore/MappedByteBufferSyncer.java:
##########
@@ -0,0 +1,125 @@
+/*
+ * 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.ignite.internal.raft.storage.segstore;
+
+import static org.apache.ignite.internal.util.IgniteUtils.jdkVersion;
+import static org.apache.ignite.internal.util.IgniteUtils.majorJavaVersion;
+import static org.apache.ignite.lang.ErrorGroups.Common.INTERNAL_ERR;
+
+import java.io.FileDescriptor;
+import java.lang.reflect.Field;
+import java.lang.reflect.InvocationTargetException;
+import java.lang.reflect.Method;
+import java.nio.MappedByteBuffer;
+import org.apache.ignite.internal.lang.IgniteInternalException;
+import org.apache.ignite.internal.util.GridUnsafe;
+
+/**
+ * Class that encapsulates different strategies of syncing a region of a 
memory-mapped byte buffer to the underlying storage for
+ * different JDK versions.
+ */
+abstract class MappedByteBufferSyncer {
+    /**
+     * Forces any changes made to a region of given buffer's content to be 
written to the storage device containing the mapped file. The
+     * region starts at the given {@code index} in this buffer and is {@code 
length} bytes.
+     *
+     * @param buf mmap-ed byte buffer which content should be synced.
+     * @param index The index of the first byte in the buffer region that is 
to be written back to storage; must be non-negative and
+     *         less than {@code capacity()}.
+     * @param length The length of the region in bytes; must be non-negative 
and no larger than {@code capacity() - index}.
+     */
+    abstract void force(MappedByteBuffer buf, int index, int length);
+
+    static MappedByteBufferSyncer createSyncer() {
+        return majorJavaVersion(jdkVersion()) >= 13 ? new Jdk13Syncer() : new 
LegacySyncer();
+    }
+
+    private static class Jdk13Syncer extends MappedByteBufferSyncer {
+        /** {@code MappedByteBuffer#force(int, int)}. */
+        private static final Method force = findMethod("force", int.class, 
int.class);
+
+        @Override
+        public void force(MappedByteBuffer buf, int index, int length) {
+            try {
+                force.invoke(buf, index, length);
+            } catch (IllegalAccessException | InvocationTargetException e) {
+                throw new IgniteInternalException(INTERNAL_ERR, e);
+            }
+        }
+    }
+
+    private static class LegacySyncer extends MappedByteBufferSyncer {

Review Comment:
   Note to reviewer: this code has been ported from Ignite 2



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to