Remove JournalWriter.getLastEntry() 78/111478/2 master
authorRobert Varga <robert.varga@pantheon.tech>
Mon, 22 Apr 2024 10:49:02 +0000 (12:49 +0200)
committerRuslan Kashapov <ruslan.kashapov@pantheon.tech>
Mon, 22 Apr 2024 14:13:23 +0000 (17:13 +0300)
We really do not want to retain a written entry -- and there is only a
single real user. Remove this method and update its sole caller.

JIRA: CONTROLLER-2115
Change-Id: Ibea3d820fa7d1daa023be1cdcc1a513720744b6e
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
Signed-off-by: Ruslan Kashapov <ruslan.kashapov@pantheon.tech>
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/SegmentedJournalWriter.java
atomix-storage/src/test/java/io/atomix/storage/journal/AbstractJournalTest.java
opendaylight/md-sal/sal-akka-segmented-journal/src/main/java/org/opendaylight/controller/akka/segjournal/SegmentedJournalActor.java

index f00e0daddd542aa42d0db489b075f85d514dc198..e381bc25a7d167935b403098778a8d617fc33701 100644 (file)
@@ -38,7 +38,6 @@ final class JournalSegmentWriter {
 
     private int currentPosition;
     private Long lastIndex;
-    private ByteBuf lastWritten;
 
     JournalSegmentWriter(final FileWriter fileWriter, final JournalSegment segment, final int maxEntrySize,
             final JournalIndex index) {
@@ -56,7 +55,6 @@ final class JournalSegmentWriter {
         index = previous.index;
         maxSegmentSize = previous.maxSegmentSize;
         maxEntrySize = previous.maxEntrySize;
-        lastWritten = previous.lastWritten;
         lastIndex = previous.lastIndex;
         currentPosition = previous.currentPosition;
         this.fileWriter = requireNonNull(fileWriter);
@@ -67,25 +65,16 @@ final class JournalSegmentWriter {
      *
      * @return The last written index.
      */
-    final long getLastIndex() {
+    long getLastIndex() {
         return lastIndex != null ? lastIndex : segment.firstIndex() - 1;
     }
 
-    /**
-     * Returns the last data written.
-     *
-     * @return The last data written.
-     */
-    final ByteBuf getLastWritten() {
-        return lastWritten == null ? null : lastWritten.slice();
-    }
-
     /**
      * Returns the next index to be written.
      *
      * @return The next index to be written.
      */
-    final long getNextIndex() {
+    long getNextIndex() {
         return lastIndex != null ? lastIndex + 1 : segment.firstIndex();
     }
 
@@ -95,7 +84,7 @@ final class JournalSegmentWriter {
      * @param buf binary data to append
      * @return The index of appended data, or {@code null} if segment has no space
      */
-    final Long append(final ByteBuf buf) {
+    Long append(final ByteBuf buf) {
         final var length = buf.readableBytes();
         if (length > maxEntrySize) {
             throw new StorageException.TooLarge("Serialized entry size exceeds maximum allowed bytes ("
@@ -127,7 +116,6 @@ final class JournalSegmentWriter {
 
         // Update the last entry with the correct index/term/length.
         currentPosition = nextPosition;
-        lastWritten = buf;
         lastIndex = index;
         this.index.index(index, position);
 
@@ -139,7 +127,7 @@ final class JournalSegmentWriter {
      *
      * @param index the index to which to reset the head of the segment
      */
-    final void reset(final long index) {
+    void reset(final long index) {
         // acquire ownership of cache and make sure reader does not see anything we've done once we're done
         final var fileReader = fileWriter.reader();
         try {
@@ -164,7 +152,6 @@ final class JournalSegmentWriter {
                 break;
             }
 
-            lastWritten = buf;
             lastIndex = nextIndex;
             this.index.index(nextIndex, currentPosition);
             nextIndex++;
@@ -179,7 +166,7 @@ final class JournalSegmentWriter {
      *
      * @param index The index to which to truncate the log.
      */
-    final void truncate(final long index) {
+    void truncate(final long index) {
         // If the index is greater than or equal to the last index, skip the truncate.
         if (index >= getLastIndex()) {
             return;
@@ -187,7 +174,6 @@ final class JournalSegmentWriter {
 
         // Reset the last written
         lastIndex = null;
-        lastWritten = null;
 
         // Truncate the index.
         this.index.truncate(index);
index 1462463e9d68c8049f68f85ea34fdfbb4734144a..064fd019ecb6459f245d40bb2711976ab668961a 100644 (file)
@@ -30,13 +30,6 @@ public interface JournalWriter<E> {
      */
     long getLastIndex();
 
-    /**
-     * Returns the last entry written.
-     *
-     * @return The last entry written.
-     */
-    Indexed<E> getLastEntry();
-
     /**
      * Returns the next index to be written.
      *
index 9ff535284dc23b574ef54ff00f864fdcbeafa6a6..71120891a1514847ed44789c10e1f5b95692d0de 100644 (file)
@@ -36,16 +36,6 @@ final class SegmentedJournalWriter<E> implements JournalWriter<E> {
     return currentWriter.getLastIndex();
   }
 
-  @Override
-  public Indexed<E> getLastEntry() {
-    final var lastWritten = currentWriter.getLastWritten();
-    if (lastWritten == null) {
-      return null;
-    }
-    final E deserialized = journal.serializer().deserialize(lastWritten);
-    return new Indexed<>(currentWriter.getLastIndex(), deserialized, lastWritten.readableBytes()) ;
-  }
-
   @Override
   public long getNextIndex() {
     return currentWriter.getNextIndex();
index 97d7d54c20ad91a8b039f5fef861cac6b6495948..487c314141707bd4a60bdc4189aea2fce58baccc 100644 (file)
@@ -186,19 +186,18 @@ public abstract class AbstractJournalTest {
             assertEquals(1, indexed.index());
             writer.reset(1);
             assertEquals(0, writer.getLastIndex());
-            writer.append(ENTRY);
+            indexed = writer.append(ENTRY);
             assertEquals(1, writer.getLastIndex());
-            assertEquals(1, writer.getLastEntry().index());
+            assertEquals(1, indexed.index());
 
             indexed = assertNext(reader);
             assertEquals(1, indexed.index());
 
             writer.truncate(0);
             assertEquals(0, writer.getLastIndex());
-            assertNull(writer.getLastEntry());
-            writer.append(ENTRY);
+            indexed = writer.append(ENTRY);
             assertEquals(1, writer.getLastIndex());
-            assertEquals(1, writer.getLastEntry().index());
+            assertEquals(1, indexed.index());
 
             indexed = assertNext(reader);
             assertEquals(1, indexed.index());
index 9f63892d26fdfd949b46d0bb9213bd84df2deb80..a3c28cac8addc8c774d64ea6408f1c20251d6d38 100644 (file)
@@ -486,8 +486,9 @@ abstract sealed class SegmentedJournalActor extends AbstractActor {
         final var sw = Stopwatch.createStarted();
         deleteJournal = SegmentedJournal.<Long>builder().withDirectory(directory).withName("delete")
                 .withNamespace(DELETE_NAMESPACE).withMaxSegmentSize(DELETE_SEGMENT_SIZE).build();
-        final var lastEntry = deleteJournal.writer().getLastEntry();
-        lastDelete = lastEntry == null ? 0 : lastEntry.entry();
+        final var lastDeleteRecovered = deleteJournal.openReader(deleteJournal.writer().getLastIndex())
+            .tryNext((index, value, length) -> value);
+        lastDelete = lastDeleteRecovered == null ? 0 : lastDeleteRecovered.longValue();
 
         dataJournal = new DataJournalV0(persistenceId, messageSize, context().system(), storage, directory,
             maxEntrySize, maxSegmentSize);