Centralize IO buffer allocation 32/111632/1
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 6 May 2024 00:09:32 +0000 (02:09 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 6 May 2024 00:11:00 +0000 (02:11 +0200)
JournalSegmentFile.maxSize() goes into the algorithm to choose I/O size.
Let's move the method there.

JIRA: CONTROLLER-2115
Change-Id: I9f0810cb9769217965375ee70c7e3e231adbf1d9
Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
atomix-storage/src/main/java/io/atomix/storage/journal/DiskFileReader.java
atomix-storage/src/main/java/io/atomix/storage/journal/DiskFileWriter.java
atomix-storage/src/main/java/io/atomix/storage/journal/JournalSegmentFile.java

index a934a903085c28f76a06f504610ec0ee1f63be6d..55f95f4d51a8b933c8c147b4b7c928b537f5e1b4 100644 (file)
@@ -26,11 +26,6 @@ import org.eclipse.jdt.annotation.NonNull;
  * A {@link StorageLevel#DISK} implementation of {@link FileReader}. Maintains an internal buffer.
  */
 final class DiskFileReader extends FileReader {
-    /**
-     * Just do not bother with IO smaller than this many bytes.
-     */
-    private static final int MIN_IO_SIZE = 8192;
-
     private final FileChannel channel;
     private final ByteBuffer buffer;
 
@@ -38,7 +33,7 @@ final class DiskFileReader extends FileReader {
     private int bufferPosition;
 
     DiskFileReader(final JournalSegmentFile file, final int maxEntrySize) {
-        this(file, allocateBuffer(file.maxSize(), maxEntrySize));
+        this(file, file.allocateBuffer(maxEntrySize));
     }
 
     // Note: take ownership of the buffer
@@ -49,21 +44,6 @@ final class DiskFileReader extends FileReader {
         bufferPosition = 0;
     }
 
-    static ByteBuffer allocateBuffer(final int maxSegmentSize, final int maxEntrySize) {
-        return ByteBuffer.allocate(chooseBufferSize(maxSegmentSize, maxEntrySize));
-    }
-
-    private static int chooseBufferSize(final int maxSegmentSize, final int maxEntrySize) {
-        if (maxSegmentSize <= MIN_IO_SIZE) {
-            // just buffer the entire segment
-            return maxSegmentSize;
-        }
-
-        // one full entry plus its header, or MIN_IO_SIZE, which benefits the read of many small entries
-        final int minBufferSize = maxEntrySize + SegmentEntry.HEADER_BYTES;
-        return minBufferSize <= MIN_IO_SIZE ? MIN_IO_SIZE : minBufferSize;
-    }
-
     @Override
     void invalidateCache() {
         buffer.clear().flip();
index 74fb2d8be84b152899be4fdb518ec3389ec36229..0cc6454c2b314ed85ea8e14af7b30ee415dfeb65 100644 (file)
@@ -35,7 +35,7 @@ final class DiskFileWriter extends FileWriter {
     DiskFileWriter(final JournalSegmentFile file, final int maxEntrySize) {
         super(file, maxEntrySize);
         channel = file.channel();
-        buffer = DiskFileReader.allocateBuffer(file.maxSize(), maxEntrySize);
+        buffer = file.allocateBuffer(maxEntrySize);
         reader = new DiskFileReader(file, buffer);
     }
 
index 04e22a1659787792c87d7767f3505f60f7518f84..d096b33fd88846eb3d4bd57f461cf963182ec66c 100644 (file)
@@ -21,6 +21,7 @@ import com.google.common.base.MoreObjects;
 import java.io.File;
 import java.io.IOException;
 import java.io.RandomAccessFile;
+import java.nio.ByteBuffer;
 import java.nio.channels.FileChannel;
 import java.nio.file.Path;
 import org.eclipse.jdt.annotation.NonNull;
@@ -34,6 +35,10 @@ final class JournalSegmentFile {
     private static final char PART_SEPARATOR = '-';
     private static final char EXTENSION_SEPARATOR = '.';
     private static final String EXTENSION = "log";
+    /**
+     * Just do not bother with IO smaller than this many bytes.
+     */
+    private static final int MIN_IO_SIZE = 8192;
 
     private final @NonNull JournalSegmentDescriptor descriptor;
     private final @NonNull Path path;
@@ -126,11 +131,27 @@ final class JournalSegmentFile {
         file.close();
     }
 
+    ByteBuffer allocateBuffer(final int maxEntrySize) {
+        return ByteBuffer.allocate(chooseBufferSize(maxEntrySize));
+    }
+
     @Override
     public String toString() {
         return MoreObjects.toStringHelper(this).add("path", path).add("descriptor", descriptor).toString();
     }
 
+    private int chooseBufferSize(final int maxEntrySize) {
+        final int maxSegmentSize = maxSize();
+        if (maxSegmentSize <= MIN_IO_SIZE) {
+            // just buffer the entire segment
+            return maxSegmentSize;
+        }
+
+        // one full entry plus its header, or MIN_IO_SIZE, which benefits the read of many small entries
+        final int minBufferSize = maxEntrySize + SegmentEntry.HEADER_BYTES;
+        return minBufferSize <= MIN_IO_SIZE ? MIN_IO_SIZE : minBufferSize;
+    }
+
     /**
      * Returns a boolean value indicating whether the given file appears to be a parsable segment file.
      *