Remove MappableJournalSegmentWriter 91/110591/4
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 11 Mar 2024 10:56:22 +0000 (11:56 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Mon, 11 Mar 2024 12:51:31 +0000 (13:51 +0100)
MappableJournalSegmentWriter is now just mediating implementation access
-- which is always is now guarded by a reference.

This means we can ditch this indirection and give out
SegmentedJournalWriter directly.

JIRA: CONTROLLER-2098
Change-Id: If15bdb10f9a317960aaf41aa80c57e929923ed21
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
atomix-storage/src/main/java/io/atomix/storage/journal/JournalSegment.java
atomix-storage/src/main/java/io/atomix/storage/journal/MappableJournalSegmentWriter.java [deleted file]
atomix-storage/src/main/java/io/atomix/storage/journal/SegmentedJournalWriter.java

index 992c56ddfced1cfcbfd13af262d0b9151bde11ee..3c27109a22b9b8dc504430eb38411b2cdd1c416d 100644 (file)
@@ -40,10 +40,11 @@ final class JournalSegment<E> implements AutoCloseable {
   private final int maxEntrySize;
   private final JournalIndex index;
   private final JournalSerdes namespace;
-  private final MappableJournalSegmentWriter<E> writer;
   private final Set<JournalSegmentReader<E>> readers = ConcurrentHashMap.newKeySet();
   private final AtomicInteger references = new AtomicInteger();
   private final FileChannel channel;
+
+  private JournalSegmentWriter<E> writer;
   private boolean open = true;
 
   JournalSegment(
@@ -65,7 +66,7 @@ final class JournalSegment<E> implements AutoCloseable {
     } catch (IOException e) {
       throw new StorageException(e);
     }
-    writer = new MappableJournalSegmentWriter<>(channel, this, maxEntrySize, index, namespace);
+    writer = new FileChannelJournalSegmentWriter<>(channel, this, maxEntrySize, index, namespace);
   }
 
   /**
@@ -158,7 +159,7 @@ final class JournalSegment<E> implements AutoCloseable {
    */
   private void acquire() {
     if (references.getAndIncrement() == 0 && storageLevel == StorageLevel.MAPPED) {
-      writer.map();
+      writer = writer.toMapped();
     }
   }
 
@@ -168,7 +169,7 @@ final class JournalSegment<E> implements AutoCloseable {
   private void release() {
     if (references.decrementAndGet() == 0) {
       if (storageLevel == StorageLevel.MAPPED) {
-        writer.unmap();
+        writer = writer.toFileChannel();
       }
       if (!open) {
         finishClose();
@@ -181,7 +182,7 @@ final class JournalSegment<E> implements AutoCloseable {
    *
    * @return The segment writer.
    */
-  MappableJournalSegmentWriter<E> acquireWriter() {
+  JournalSegmentWriter<E> acquireWriter() {
     checkOpen();
     acquire();
 
diff --git a/atomix-storage/src/main/java/io/atomix/storage/journal/MappableJournalSegmentWriter.java b/atomix-storage/src/main/java/io/atomix/storage/journal/MappableJournalSegmentWriter.java
deleted file mode 100644 (file)
index 71f0589..0000000
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * Copyright 2018-present Open Networking Foundation
- *
- * Licensed 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 io.atomix.storage.journal;
-
-import io.atomix.storage.journal.index.JournalIndex;
-import java.nio.MappedByteBuffer;
-import java.nio.channels.FileChannel;
-
-/**
- * Mappable log segment writer.
- */
-final class MappableJournalSegmentWriter<E> implements JournalWriter<E> {
-  private JournalSegmentWriter<E> writer;
-
-  MappableJournalSegmentWriter(
-      FileChannel channel,
-      JournalSegment<E> segment,
-      int maxEntrySize,
-      JournalIndex index,
-      JournalSerdes namespace) {
-    this.writer = new FileChannelJournalSegmentWriter<>(channel, segment, maxEntrySize, index, namespace);
-  }
-
-  /**
-   * Maps the segment writer into memory, returning the mapped buffer.
-   *
-   * @return the buffer that was mapped into memory
-   */
-  MappedByteBuffer map() {
-    final var mapped = writer.toMapped();
-    writer = mapped;
-    return mapped.buffer();
-  }
-
-  /**
-   * Unmaps the mapped buffer.
-   */
-  void unmap() {
-    writer = writer.toFileChannel();
-  }
-
-  MappedByteBuffer buffer() {
-    return writer.buffer();
-  }
-
-  @Override
-  public long getLastIndex() {
-    return writer.getLastIndex();
-  }
-
-  @Override
-  public Indexed<E> getLastEntry() {
-    return writer.getLastEntry();
-  }
-
-  @Override
-  public long getNextIndex() {
-    return writer.getNextIndex();
-  }
-
-  @Override
-  public <T extends E> Indexed<T> append(T entry) {
-    return writer.append(entry);
-  }
-
-  @Override
-  public void append(Indexed<E> entry) {
-    writer.append(entry);
-  }
-
-  @Override
-  public void commit(long index) {
-    writer.commit(index);
-  }
-
-  @Override
-  public void reset(long index) {
-    writer.reset(index);
-  }
-
-  @Override
-  public void truncate(long index) {
-    writer.truncate(index);
-  }
-
-  @Override
-  public void flush() {
-    writer.flush();
-  }
-
-  @Override
-  public void close() {
-    writer.close();
-  }
-}
index 4d13b2e5cc0b691d6f0fd8e42cb7c293211bd18a..35f7540b9e50231d665617a57bf64ff35d75cf28 100644 (file)
@@ -23,7 +23,7 @@ import java.nio.BufferOverflowException;
 public final class SegmentedJournalWriter<E> implements JournalWriter<E> {
   private final SegmentedJournal<E> journal;
   private JournalSegment<E> currentSegment;
-  private MappableJournalSegmentWriter<E> currentWriter;
+  private JournalSegmentWriter<E> currentWriter;
 
   SegmentedJournalWriter(SegmentedJournal<E> journal) {
     this.journal = journal;