Do not store JournalSegment in JournalSegmentWriter 82/110582/3
authorRobert Varga <robert.varga@pantheon.tech>
Sun, 10 Mar 2024 22:01:42 +0000 (23:01 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 11 Mar 2024 12:51:31 +0000 (13:51 +0100)
We are only accessing index() and maxSegmentSize. We already copy
index() to firstIndex, so let's do the same maxSegmentSize and use
firstIndex instead of accessing segment.index().

JIRA: CONTROLLER-2098
Change-Id: Ia8a24c7f73187a5803d279309775f361a39c91b0
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentWriter.java
atomix-storage/src/main/java/io/atomix/storage/journal/JournalSegmentWriter.java
atomix-storage/src/main/java/io/atomix/storage/journal/MappedJournalSegmentWriter.java

index 037c20d3293e1c2a14bb2f530de9b4c6c4ff384d..d5addb28ab06583bdfe5bfe909027f6cc2c3abb4 100644 (file)
@@ -150,7 +150,7 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
 
   @Override
   public long getLastIndex() {
-    return lastEntry != null ? lastEntry.index() : segment.index() - 1;
+    return lastEntry != null ? lastEntry.index() : firstIndex - 1;
   }
 
   @Override
@@ -202,7 +202,7 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
     final int length = memory.limit() - (Integer.BYTES + Integer.BYTES);
 
     // Ensure there's enough space left in the buffer to store the entry.
-    if (segment.descriptor().maxSegmentSize() - currentPosition < length + Integer.BYTES + Integer.BYTES) {
+    if (maxSegmentSize - currentPosition < length + Integer.BYTES + Integer.BYTES) {
       throw new BufferOverflowException();
     }
 
@@ -248,7 +248,7 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
     this.index.truncate(index);
 
     try {
-      if (index < segment.index()) {
+      if (index < firstIndex) {
         // Reset the writer to the first entry.
         currentPosition = JournalSegmentDescriptor.BYTES;
       } else {
index 54661a2b47ec3bc1195a85e00a05a99e618c5d51..23b0992d4fbd00a0b9076f109e1d8a814f9c0f80 100644 (file)
@@ -18,28 +18,28 @@ import org.eclipse.jdt.annotation.Nullable;
 abstract sealed class JournalSegmentWriter<E> implements JournalWriter<E>
         permits FileChannelJournalSegmentWriter, MappedJournalSegmentWriter {
     final @NonNull FileChannel channel;
-    final @NonNull JournalSegment<E> segment;
-    final int maxEntrySize;
     final @NonNull JournalIndex index;
     final @NonNull JournalSerdes namespace;
+    final int maxSegmentSize;
+    final int maxEntrySize;
     final long firstIndex;
 
     JournalSegmentWriter(final FileChannel channel, final JournalSegment<E> segment, final int maxEntrySize,
             final JournalIndex index, final JournalSerdes namespace) {
         this.channel = requireNonNull(channel);
-        this.segment = requireNonNull(segment);
-        this.maxEntrySize = maxEntrySize;
         this.index = requireNonNull(index);
         this.namespace = requireNonNull(namespace);
+        this.maxSegmentSize = segment.descriptor().maxSegmentSize();
+        this.maxEntrySize = maxEntrySize;
         this.firstIndex = segment.index();
     }
 
     JournalSegmentWriter(JournalSegmentWriter<E> previous) {
         this.channel = previous.channel;
-        this.segment = previous.segment;
-        this.maxEntrySize = previous.maxEntrySize;
         this.index = previous.index;
         this.namespace = previous.namespace;
+        this.maxSegmentSize = previous.maxSegmentSize;
+        this.maxEntrySize = previous.maxEntrySize;
         this.firstIndex = previous.firstIndex;
     }
 
index 2a479ed192e3d5e0b95403b3f7ede07ce4602a59..705f98bc93ed482c3c56c0573d94c2f61ada3f8c 100644 (file)
@@ -55,14 +55,14 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
       JournalIndex index,
       JournalSerdes namespace) {
     super(channel, segment, maxEntrySize, index, namespace);
-    mappedBuffer = mapBuffer(channel, segment.descriptor().maxSegmentSize());
+    mappedBuffer = mapBuffer(channel, maxSegmentSize);
     buffer = mappedBuffer.slice();
     reset(0);
   }
 
   MappedJournalSegmentWriter(JournalSegmentWriter<E> previous, int position) {
     super(previous);
-    mappedBuffer = mapBuffer(channel, segment.descriptor().maxSegmentSize());
+    mappedBuffer = mapBuffer(channel, maxSegmentSize);
     buffer = mappedBuffer.slice();
     lastEntry = previous.getLastEntry();
     buffer.position(position);
@@ -151,7 +151,7 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
 
   @Override
   public long getLastIndex() {
-    return lastEntry != null ? lastEntry.index() : segment.index() - 1;
+    return lastEntry != null ? lastEntry.index() : firstIndex - 1;
   }
 
   @Override
@@ -248,7 +248,7 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
     // Truncate the index.
     this.index.truncate(index);
 
-    if (index < segment.index()) {
+    if (index < firstIndex) {
       // Reset the writer to the first entry.
       buffer.position(JournalSegmentDescriptor.BYTES);
     } else {