From ac277cc850d78c906250263ca9ae196672fa438b Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Mon, 11 Mar 2024 09:08:57 +0100 Subject: [PATCH] Clean up SegmentedJournalWriter We have duplicated code in append() methods and their recovery is not obvious. Move common code into a private method and reduce the size of catch blocks. JIRA: CONTROLLER-2098 Change-Id: I58e6ff19cf2eaa2e97c56499d70171cb93da6174 Signed-off-by: Robert Varga --- .../journal/SegmentedJournalWriter.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/atomix-storage/src/main/java/io/atomix/storage/journal/SegmentedJournalWriter.java b/atomix-storage/src/main/java/io/atomix/storage/journal/SegmentedJournalWriter.java index d4a1a68261..64a4c70c7a 100644 --- a/atomix-storage/src/main/java/io/atomix/storage/journal/SegmentedJournalWriter.java +++ b/atomix-storage/src/main/java/io/atomix/storage/journal/SegmentedJournalWriter.java @@ -20,12 +20,12 @@ import java.nio.BufferOverflowException; /** * Raft log writer. */ -public class SegmentedJournalWriter implements JournalWriter { +public final class SegmentedJournalWriter implements JournalWriter { private final SegmentedJournal journal; private JournalSegment currentSegment; private MappableJournalSegmentWriter currentWriter; - public SegmentedJournalWriter(SegmentedJournal journal) { + SegmentedJournalWriter(SegmentedJournal journal) { this.journal = journal; this.currentSegment = journal.getLastSegment(); currentSegment.acquire(); @@ -78,30 +78,33 @@ public class SegmentedJournalWriter implements JournalWriter { if (currentSegment.index() == currentWriter.getNextIndex()) { throw e; } - currentWriter.flush(); - currentSegment.release(); - currentSegment = journal.getNextSegment(); - currentSegment.acquire(); - currentWriter = currentSegment.writer(); - return currentWriter.append(entry); } + + moveToNextSegment(); + return currentWriter.append(entry); } @Override public void append(Indexed entry) { try { currentWriter.append(entry); + return; } catch (BufferOverflowException e) { if (currentSegment.index() == currentWriter.getNextIndex()) { throw e; } - currentWriter.flush(); - currentSegment.release(); - currentSegment = journal.getNextSegment(); - currentSegment.acquire(); - currentWriter = currentSegment.writer(); - currentWriter.append(entry); } + + moveToNextSegment(); + currentWriter.append(entry); + } + + private void moveToNextSegment() { + currentWriter.flush(); + currentSegment.release(); + currentSegment = journal.getNextSegment(); + currentSegment.acquire(); + currentWriter = currentSegment.writer(); } @Override -- 2.36.6