Clean up SegmentedByteBufWriter.reset(long) 61/111661/1
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 8 May 2024 14:05:25 +0000 (16:05 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 8 May 2024 14:33:55 +0000 (16:33 +0200)
The logic in writer reset needs to be a tad updated:

- it makes absolutely no sense to seek past last written index, unless
  we want to magically sync across multiply-open journals. We do not
  support that
- we need to realize when we are already at the correct index

JIRA: CONTROLLER-2100
Change-Id: I20b2a084c782069b893059f8d5bf535ecbbf33e3
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
atomix-storage/src/main/java/io/atomix/storage/journal/SegmentedByteBufWriter.java
atomix-storage/src/test/java/io/atomix/storage/journal/AbstractJournalTest.java

index c51e8a2ffae870306ce0dcb952fb019349c7f7a0..6d50ef642f2b5d9ab4a7a2c782b9ef3de5d20335 100644 (file)
@@ -67,20 +67,24 @@ final class SegmentedByteBufWriter implements ByteBufWriter {
 
     @Override
     public void reset(final long index) {
-        final long commitIndex = journal.getCommitIndex();
+        final var commitIndex = journal.getCommitIndex();
         if (index <= commitIndex) {
-          // also catches index == 0, which is not a valid next index
-          throw new IndexOutOfBoundsException("Cannot reset to: " + index + ", committed index: " + commitIndex);
+            // also catches index == 0, which is not a valid next index
+            throw new IndexOutOfBoundsException("Cannot reset to: " + index + ", committed index: " + commitIndex);
         }
 
-        if (index > currentSegment.firstIndex()) {
-            currentSegment.releaseWriter();
-            currentSegment = journal.resetSegments(index);
-            currentWriter = currentSegment.acquireWriter();
+        final var lastIndex = currentSegment.lastIndex();
+        final var prevIndex = index - 1;
+        if (prevIndex == lastIndex) {
+            // already at the correct position: no-op
+        } else if (prevIndex < lastIndex) {
+            // move back
+            checkedTruncate(prevIndex);
+            journal.resetHead(index);
         } else {
-            checkedTruncate(index - 1);
+            // cannot seek past last written entry
+            throw new IndexOutOfBoundsException("Cannot reset to: " + index + ", lastIndex: " + lastIndex);
         }
-        journal.resetHead(index);
     }
 
     @Override
index 026e58df7b13b407775b277a89eec4b3d7dedfca..8abc2b2779ccac2df3da6ca20a87996533bc7721 100644 (file)
@@ -180,9 +180,15 @@ public abstract class AbstractJournalTest {
             assertEquals(1, writer.getNextIndex());
             writer.append(ENTRY);
             writer.append(ENTRY);
+
             writer.reset(1);
             assertEquals(0, journal.lastIndex());
             assertEquals(1, writer.getNextIndex());
+            // Repeat to assert this is a no-op
+            writer.reset(1);
+            assertEquals(0, journal.lastIndex());
+            assertEquals(1, writer.getNextIndex());
+
             writer.append(ENTRY);
 
             var indexed = assertNext(reader);