Disconnect JournalSegmentWriter from JournalWriter 93/110593/3
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 11 Mar 2024 11:08:05 +0000 (12:08 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 11 Mar 2024 12:51:31 +0000 (13:51 +0100)
JournalSegmentWriter is really the internal API, so it should not have a
public API straightjacket. Disconnect the implementation, making method
properly package-private.

This also makes it very clear that JournalWriter is not something users
should close() -- because doing so would wreck internal machinery rather
thoroughly, rendering the segment writer inoperable -- and that should
only be possible via JournalSegment's lifecycle.

JIRA: CONTROLLER-2098
Change-Id: Ia18b0dc640cba11689bb36cf338c8cef3ae4e6f8
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/JournalWriter.java
atomix-storage/src/main/java/io/atomix/storage/journal/MappedJournalSegmentWriter.java
atomix-storage/src/main/java/io/atomix/storage/journal/SegmentedJournalWriter.java

index d5addb28ab06583bdfe5bfe909027f6cc2c3abb4..7e9b81035d1420242bf6da21de68a05d3881b937 100644 (file)
@@ -88,7 +88,7 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
   }
 
   @Override
-  public void reset(long index) {
+  void reset(long index) {
     long nextIndex = firstIndex;
 
     // Clear the buffer indexes.
@@ -149,17 +149,17 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
   }
 
   @Override
-  public long getLastIndex() {
+  long getLastIndex() {
     return lastEntry != null ? lastEntry.index() : firstIndex - 1;
   }
 
   @Override
-  public Indexed<E> getLastEntry() {
+  Indexed<E> getLastEntry() {
     return lastEntry;
   }
 
   @Override
-  public long getNextIndex() {
+  long getNextIndex() {
     if (lastEntry != null) {
       return lastEntry.index() + 1;
     } else {
@@ -168,7 +168,7 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
   }
 
   @Override
-  public void append(Indexed<E> entry) {
+  void append(Indexed<E> entry) {
     final long nextIndex = getNextIndex();
 
     // If the entry's index is greater than the next index in the segment, skip some entries.
@@ -185,7 +185,7 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
 
   @Override
   @SuppressWarnings("unchecked")
-  public <T extends E> Indexed<T> append(T entry) {
+  <T extends E> Indexed<T> append(T entry) {
     // Store the entry index.
     final long index = getNextIndex();
 
@@ -235,7 +235,7 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
   }
 
   @Override
-  public void truncate(long index) {
+  void truncate(long index) {
     // If the index is greater than or equal to the last index, skip the truncate.
     if (index >= getLastIndex()) {
       return;
@@ -264,7 +264,7 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
   }
 
   @Override
-  public void flush() {
+  void flush() {
     try {
       if (channel.isOpen()) {
         channel.force(true);
@@ -275,7 +275,7 @@ final class FileChannelJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
   }
 
   @Override
-  public void close() {
+  void close() {
     flush();
   }
 }
index 23b0992d4fbd00a0b9076f109e1d8a814f9c0f80..d552d84be84b944172c714cab27db92f3b19fd10 100644 (file)
@@ -15,8 +15,7 @@ import java.nio.channels.FileChannel;
 import org.eclipse.jdt.annotation.NonNull;
 import org.eclipse.jdt.annotation.Nullable;
 
-abstract sealed class JournalSegmentWriter<E> implements JournalWriter<E>
-        permits FileChannelJournalSegmentWriter, MappedJournalSegmentWriter {
+abstract sealed class JournalSegmentWriter<E> permits FileChannelJournalSegmentWriter, MappedJournalSegmentWriter {
     final @NonNull FileChannel channel;
     final @NonNull JournalIndex index;
     final @NonNull JournalSerdes namespace;
@@ -43,10 +42,65 @@ abstract sealed class JournalSegmentWriter<E> implements JournalWriter<E>
         this.firstIndex = previous.firstIndex;
     }
 
-    @Override
-    public final void commit(final long index) {
-        // FIXME: CONTROLLER-2098: eliminate the need for this method
-    }
+    /**
+     * Returns the last written index.
+     *
+     * @return The last written index.
+     */
+    abstract long getLastIndex();
+
+    /**
+     * Returns the last entry written.
+     *
+     * @return The last entry written.
+     */
+    abstract Indexed<E> getLastEntry();
+
+    /**
+     * Returns the next index to be written.
+     *
+     * @return The next index to be written.
+     */
+    abstract long getNextIndex();
+
+    /**
+     * Appends an entry to the journal.
+     *
+     * @param entry The entry to append.
+     * @return The appended indexed entry.
+     */
+    abstract <T extends E> Indexed<T> append(T entry);
+
+    /**
+     * Appends an indexed entry to the log.
+     *
+     * @param entry The indexed entry to append.
+     */
+    abstract void append(Indexed<E> entry);
+
+    /**
+     * Resets the head of the segment to the given index.
+     *
+     * @param index the index to which to reset the head of the segment
+     */
+    abstract void reset(long index);
+
+    /**
+     * Truncates the log to the given index.
+     *
+     * @param index The index to which to truncate the log.
+     */
+    abstract void truncate(long index);
+
+    /**
+     * Flushes written entries to disk.
+     */
+    abstract void flush();
+
+    /**
+     * Closes this writer.
+     */
+    abstract void close();
 
     /**
      * Returns the mapped buffer underlying the segment writer, or {@code null} if the writer does not have such a
index efb566efa9d5de8dc06183e28b319b914327649d..f5ee18b2e6a573927e51c347fc33457056303aef 100644 (file)
@@ -20,8 +20,7 @@ package io.atomix.storage.journal;
  *
  * @author <a href="http://github.com/kuujo">Jordan Halterman</a>
  */
-public interface JournalWriter<E> extends AutoCloseable {
-
+public interface JournalWriter<E> {
   /**
    * Returns the last written index.
    *
@@ -83,7 +82,4 @@ public interface JournalWriter<E> extends AutoCloseable {
    * Flushes written entries to disk.
    */
   void flush();
-
-  @Override
-  void close();
 }
index 705f98bc93ed482c3c56c0573d94c2f61ada3f8c..22c03be39f6ffb1f91ecab4dbd453437750fd9fb 100644 (file)
@@ -94,7 +94,7 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
   }
 
   @Override
-  public void reset(long index) {
+  void reset(long index) {
     long nextIndex = firstIndex;
 
     // Clear the buffer indexes.
@@ -150,17 +150,17 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
   }
 
   @Override
-  public long getLastIndex() {
+  long getLastIndex() {
     return lastEntry != null ? lastEntry.index() : firstIndex - 1;
   }
 
   @Override
-  public Indexed<E> getLastEntry() {
+  Indexed<E> getLastEntry() {
     return lastEntry;
   }
 
   @Override
-  public long getNextIndex() {
+  long getNextIndex() {
     if (lastEntry != null) {
       return lastEntry.index() + 1;
     } else {
@@ -169,7 +169,7 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
   }
 
   @Override
-  public void append(Indexed<E> entry) {
+  void append(Indexed<E> entry) {
     final long nextIndex = getNextIndex();
 
     // If the entry's index is greater than the next index in the segment, skip some entries.
@@ -186,7 +186,7 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
 
   @Override
   @SuppressWarnings("unchecked")
-  public <T extends E> Indexed<T> append(T entry) {
+  <T extends E> Indexed<T> append(T entry) {
     // Store the entry index.
     final long index = getNextIndex();
 
@@ -234,9 +234,8 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
     return (Indexed<T>) indexedEntry;
   }
 
-
   @Override
-  public void truncate(long index) {
+  void truncate(long index) {
     // If the index is greater than or equal to the last index, skip the truncate.
     if (index >= getLastIndex()) {
       return;
@@ -264,12 +263,12 @@ final class MappedJournalSegmentWriter<E> extends JournalSegmentWriter<E> {
   }
 
   @Override
-  public void flush() {
+  void flush() {
     mappedBuffer.force();
   }
 
   @Override
-  public void close() {
+  void close() {
     flush();
     try {
       BufferCleaner.freeBuffer(mappedBuffer);
index 35f7540b9e50231d665617a57bf64ff35d75cf28..3d831da55f0e8dfa0dad2709a32745380f4f3b4d 100644 (file)
@@ -129,9 +129,4 @@ public final class SegmentedJournalWriter<E> implements JournalWriter<E> {
   public void flush() {
     currentWriter.flush();
   }
-
-  @Override
-  public void close() {
-    currentWriter.close();
-  }
 }