cheeeee commented on code in PR #23445:
URL: https://github.com/apache/kafka/pull/23445#discussion_r4266592796


##########
clients/src/main/java/org/apache/kafka/common/record/internal/FileRecords.java:
##########
@@ -300,8 +301,35 @@ public int writeTo(TransferableChannel destChannel, int 
offset, int length) thro
 
         long position = start + offset;
         int count = Math.min(length, oldSize - offset);
-        // safe to cast to int since `count` is an int
-        return (int) destChannel.transferFrom(channel, position, count);
+        try {
+            // safe to cast to int since `count` is an int
+            return (int) destChannel.transferFrom(channel, position, count);
+        } catch (IOException e) {
+            // Sent zero copy, so the disk read happens here and a broken 
medium looks exactly
+            // like a broken socket: both arrive as a bare IOException. 
Re-read the region to
+            // tell them apart; costs one read, on the error path only. The 
message text is not
+            // inspected: it is the platform's strerror output and differs 
between platforms.
+            if (regionIsReadable(position))
+                throw e;
+            throw new KafkaStorageException("Failed to read " + count + " 
bytes at position "
+                    + position + " of " + file.getAbsolutePath() + " while 
writing a response", e);
+        }
+    }
+
+    /**
+     * Whether the given position of the backing file can still be read, used 
by {@link #writeTo}
+     * to tell a storage failure from a socket failure. Anything but a 
successful read means
+     * unreadable; a position at or past the end of the file means readable, 
since nothing can be
+     * concluded about the medium from it.
+     */
+    private boolean regionIsReadable(long position) {
+        try {
+            if (position >= channel.size())
+                return true;
+            return channel.read(ByteBuffer.allocate(1), position) >= 0;

Review Comment:
   Thank you very much for explaining this. I hadn't taken into account that 
the log can be truncated while a send is
   in flight. An end of file from the re-read says nothing about the medium, 
and the size check cannot close the
   window. The re-read now treats an end of file, a read that makes no 
progress, and a channel closed concurrently as
   inconclusive, and keeps the original exception 
(`testWriteToPreservesExceptionWhenTruncatedDuringReRead`).
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to