From: Robert Varga Date: Tue, 26 Mar 2024 20:59:30 +0000 (+0100) Subject: Unify JournalSegmentWriter.truncate() X-Git-Tag: v9.0.2~5 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?p=controller.git;a=commitdiff_plain;h=de63490af457bf0f97c25cbe5048c981e932d6ee Unify JournalSegmentWriter.truncate() Implementations of truncate are practically identical, merge them into a common method. Also eliminate JournalSegmentWriter.firstIndex, as we can get it from the segment at any time. JIRA: CONTROLLER-2100 Change-Id: I019978461b67693f109cd6e90c8fba2ed8b18a3f Signed-off-by: Robert Varga --- diff --git a/atomix-storage/src/main/java/io/atomix/storage/journal/DiskJournalSegmentWriter.java b/atomix-storage/src/main/java/io/atomix/storage/journal/DiskJournalSegmentWriter.java index 7120158839..d3aa3332c7 100644 --- a/atomix-storage/src/main/java/io/atomix/storage/journal/DiskJournalSegmentWriter.java +++ b/atomix-storage/src/main/java/io/atomix/storage/journal/DiskJournalSegmentWriter.java @@ -134,29 +134,9 @@ final class DiskJournalSegmentWriter extends JournalSegmentWriter { } @Override - void truncate(final long index) { - // If the index is greater than or equal to the last index, skip the truncate. - if (index >= getLastIndex()) { - return; - } - - // Reset the last entry. - lastEntry = null; - - // Truncate the index. - this.index.truncate(index); - + void writeEmptyHeader(final int position) { try { - if (index < firstIndex) { - // Reset the writer to the first entry. - currentPosition = JournalSegmentDescriptor.BYTES; - } else { - // Reset the writer to the given index. - reset(index); - } - - // Zero the entry header at current channel position. - channel.write(ZERO_ENTRY_HEADER.asReadOnlyBuffer(), currentPosition); + channel.write(ZERO_ENTRY_HEADER.asReadOnlyBuffer(), position); } catch (IOException e) { throw new StorageException(e); } diff --git a/atomix-storage/src/main/java/io/atomix/storage/journal/JournalSegmentWriter.java b/atomix-storage/src/main/java/io/atomix/storage/journal/JournalSegmentWriter.java index f6a7e94987..e08e50de9e 100644 --- a/atomix-storage/src/main/java/io/atomix/storage/journal/JournalSegmentWriter.java +++ b/atomix-storage/src/main/java/io/atomix/storage/journal/JournalSegmentWriter.java @@ -31,7 +31,6 @@ abstract sealed class JournalSegmentWriter permits DiskJournalSegmentWriter, final @NonNull JournalSerdes namespace; final int maxSegmentSize; final int maxEntrySize; - final long firstIndex; // FIXME: hide these two fields Indexed lastEntry; @@ -45,7 +44,6 @@ abstract sealed class JournalSegmentWriter permits DiskJournalSegmentWriter, this.namespace = requireNonNull(namespace); maxSegmentSize = segment.descriptor().maxSegmentSize(); this.maxEntrySize = maxEntrySize; - firstIndex = segment.index(); } JournalSegmentWriter(final JournalSegmentWriter previous) { @@ -55,7 +53,6 @@ abstract sealed class JournalSegmentWriter permits DiskJournalSegmentWriter, namespace = previous.namespace; maxSegmentSize = previous.maxSegmentSize; maxEntrySize = previous.maxEntrySize; - firstIndex = previous.firstIndex; lastEntry = previous.lastEntry; currentPosition = previous.currentPosition; } @@ -66,7 +63,7 @@ abstract sealed class JournalSegmentWriter permits DiskJournalSegmentWriter, * @return The last written index. */ final long getLastIndex() { - return lastEntry != null ? lastEntry.index() : firstIndex - 1; + return lastEntry != null ? lastEntry.index() : segment.index() - 1; } /** @@ -84,7 +81,7 @@ abstract sealed class JournalSegmentWriter permits DiskJournalSegmentWriter, * @return The next index to be written. */ final long getNextIndex() { - return lastEntry != null ? lastEntry.index() + 1 : firstIndex; + return lastEntry != null ? lastEntry.index() + 1 : segment.index(); } /** @@ -115,7 +112,7 @@ abstract sealed class JournalSegmentWriter permits DiskJournalSegmentWriter, abstract JournalSegmentReader reader(); private void resetWithBuffer(final JournalSegmentReader reader, final long index) { - long nextIndex = firstIndex; + long nextIndex = segment.index(); // Clear the buffer indexes and acquire ownership of the buffer currentPosition = JournalSegmentDescriptor.BYTES; @@ -141,7 +138,36 @@ abstract sealed class JournalSegmentWriter permits DiskJournalSegmentWriter, * * @param index The index to which to truncate the log. */ - abstract void truncate(long index); + final void truncate(final long index) { + // If the index is greater than or equal to the last index, skip the truncate. + if (index >= getLastIndex()) { + return; + } + + // Reset the last entry. + lastEntry = null; + + // Truncate the index. + this.index.truncate(index); + + if (index < segment.index()) { + // Reset the writer to the first entry. + currentPosition = JournalSegmentDescriptor.BYTES; + } else { + // Reset the writer to the given index. + reset(index); + } + + // Zero the entry header at current channel position. + writeEmptyHeader(currentPosition); + } + + /** + * Write {@link SegmentEntry#HEADER_BYTES} worth of zeroes at specified position. + * + * @param position position to write to + */ + abstract void writeEmptyHeader(int position); /** * Flushes written entries to disk. diff --git a/atomix-storage/src/main/java/io/atomix/storage/journal/MappedJournalSegmentWriter.java b/atomix-storage/src/main/java/io/atomix/storage/journal/MappedJournalSegmentWriter.java index 9f437b6125..ffb08d29e1 100644 --- a/atomix-storage/src/main/java/io/atomix/storage/journal/MappedJournalSegmentWriter.java +++ b/atomix-storage/src/main/java/io/atomix/storage/journal/MappedJournalSegmentWriter.java @@ -142,29 +142,9 @@ final class MappedJournalSegmentWriter extends JournalSegmentWriter { } @Override - void truncate(final long index) { - // If the index is greater than or equal to the last index, skip the truncate. - if (index >= getLastIndex()) { - return; - } - - // Reset the last entry. - lastEntry = null; - - // Truncate the index. - this.index.truncate(index); - - if (index < firstIndex) { - // Reset the writer to the first entry. - currentPosition = JournalSegmentDescriptor.BYTES; - } else { - // Reset the writer to the given index. - reset(index); - } - - // Zero the entry header at current buffer position. - // Note: we issue a single putLong() instead of two putInt()s. - buffer.putLong(currentPosition, 0L); + void writeEmptyHeader(final int position) { + // Note: we issue a single putLong() instead of two putInt()s. + buffer.putLong(position, 0L); } @Override