From cd676023cc1989bbe565d806a5d3df3267519a51 Mon Sep 17 00:00:00 2001 From: Matt Pavlovich Date: Mon, 1 May 2023 20:08:16 -0500 Subject: [PATCH] [#9254] DataFile readRecord fallback to OS file.length in rare edge case (cherry picked from commit 3e61a200d07c4f788542e3e0a434e3e7db25cd81) --- .../store/kahadb/disk/journal/DataFileAccessor.java | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/DataFileAccessor.java b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/DataFileAccessor.java index 57df143b56..c9671f2820 100644 --- a/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/DataFileAccessor.java +++ b/activemq-kahadb-store/src/main/java/org/apache/activemq/store/kahadb/disk/journal/DataFileAccessor.java @@ -17,7 +17,6 @@ package org.apache.activemq.store.kahadb.disk.journal; import java.io.IOException; -import java.io.RandomAccessFile; import java.util.Map; import org.apache.activemq.util.ByteSequence; @@ -85,7 +84,17 @@ final class DataFileAccessor { file.seek(location.getOffset() + Journal.RECORD_HEAD_SPACE); } if ((long)location.getOffset() + location.getSize() > dataFile.length) { - throw new IOException("Invalid location size: " + location + ", size: " + location.getSize()); + /** + * AMQ-9254 if the read request is outside expected dataFile length, + * perform expensive OS file length lookup operation + * to allow read operation if it will succeed + */ + long osFileLength = dataFile.getFile().length(); + if((long)location.getOffset() + location.getSize() > osFileLength) { + throw new IOException("Invalid location size: " + location + ", size: " + location.getSize()); + } else { + LOG.warn("DataFile:{} actual length:{} larger than expected:{} for readRecord location:{} size:{}", dataFile.file.getName(), osFileLength, dataFile.length, location, location.getSize()); + } } byte[] data = new byte[location.getSize() - Journal.RECORD_HEAD_SPACE]; file.readFully(data);