From b2106c7c4fbc91f6cdfd8bf4371b723db6fdae2f Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Sat, 9 Mar 2024 13:30:12 +0100 Subject: [PATCH] Improve FileChannelJournalSegmentWriter.truncate() Reduce the dance around channel position by explicitly passing it down, thus reducing the number of syscalls we incur. Also inline FileChannelJournalSegmentWriter.zero() and add a FIXME to improve our zero-out strategy. JIRA: CONTROLLER-2095 Change-Id: I885947fef51132a294c900396d902cf5481e2bd8 Signed-off-by: Robert Varga --- .../FileChannelJournalSegmentWriter.java | 33 ++++++++----------- 1 file changed, 13 insertions(+), 20 deletions(-) diff --git a/atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentWriter.java b/atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentWriter.java index 5c7af3fdb4..b43aa3d443 100644 --- a/atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentWriter.java +++ b/atomix-storage/src/main/java/io/atomix/storage/journal/FileChannelJournalSegmentWriter.java @@ -244,39 +244,32 @@ class FileChannelJournalSegmentWriter implements JournalWriter { // Reset the last entry. lastEntry = null; - try { - // Truncate the index. - this.index.truncate(index); + // Truncate the index. + this.index.truncate(index); + try { if (index < segment.index()) { - channel.position(JournalSegmentDescriptor.BYTES); - channel.write(zero()); + // Reset the writer to the first entry. channel.position(JournalSegmentDescriptor.BYTES); } else { // Reset the writer to the given index. reset(index); + } - // Zero entries after the given index. - long position = channel.position(); - channel.write(zero()); - channel.position(position); + // Zero entries at the current position. + // FIXME: This is quite inefficient, we essentially want to zero-out part of the file, but it is not quite clear + // how much: this overwrites at least two entries. I believe we should be able to get by with wiping the + // header of the current entry. + memory.clear(); + for (int i = 0; i < memory.limit(); i++) { + memory.put(i, (byte) 0); } + channel.write(memory, channel.position()); } catch (IOException e) { throw new StorageException(e); } } - /** - * Returns a zeroed out byte buffer. - */ - private ByteBuffer zero() { - memory.clear(); - for (int i = 0; i < memory.limit(); i++) { - memory.put(i, (byte) 0); - } - return memory; - } - @Override public void flush() { try { -- 2.36.6