Skip to content

Commit 49a5702

Browse files
committed
HDFS-17914. Prevent DFSInputStream from issuing 0-byte reads
1 parent f6182e5 commit 49a5702

2 files changed

Lines changed: 47 additions & 1 deletion

File tree

‎hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSInputStream.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1230,7 +1230,9 @@ void actualGetFromOneDataNode(final DNAddrPair datanode, final long startInBlk,
12301230
long beginReadMS = Time.monotonicNow();
12311231
int nread = 0;
12321232
int ret;
1233-
while (true) {
1233+
// Stop once the slice is filled; an extra read with remaining()==0
1234+
// can trigger wasted slow-lane I/O in SCR.
1235+
while (tmp.hasRemaining()) {
12341236
ret = reader.read(tmp);
12351237
if (ret <= 0) {
12361238
break;

‎hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSInputStream.java‎

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,16 @@
2323
import static org.junit.jupiter.api.Assertions.assertNotNull;
2424
import static org.junit.jupiter.api.Assertions.assertTrue;
2525
import static org.junit.jupiter.api.Assumptions.assumeTrue;
26+
import static org.mockito.AdditionalAnswers.delegatesTo;
27+
import static org.mockito.ArgumentMatchers.any;
28+
import static org.mockito.Mockito.doAnswer;
2629
import static org.mockito.Mockito.mock;
2730
import static org.mockito.Mockito.when;
2831

2932
import java.io.File;
3033
import java.io.IOException;
34+
import java.net.InetSocketAddress;
35+
import java.nio.ByteBuffer;
3136
import java.util.ArrayList;
3237
import java.util.List;
3338
import java.util.Random;
@@ -364,4 +369,43 @@ public Void answer(InvocationOnMock invocation) throws Throwable {
364369
IOUtils.closeStream(out);
365370
}
366371
}
372+
373+
@Test
374+
@Timeout(value = 60)
375+
public void testAvoidsEmptyBufferRead() throws IOException {
376+
Configuration conf = new Configuration();
377+
try (MiniDFSCluster cluster = new MiniDFSCluster.Builder(conf).build()) {
378+
DistributedFileSystem fs = cluster.getFileSystem();
379+
DFSClient client = fs.getClient();
380+
Path file = new Path("/preadFile");
381+
int fileLength = 8192;
382+
DFSTestUtil.createFile(fs, file, fileLength, (short) 1, 0L);
383+
384+
AtomicInteger emptyBufferReads = new AtomicInteger(0);
385+
try (DFSInputStream in = new DFSInputStream(client, file.toString(),
386+
true, null) {
387+
@Override
388+
protected BlockReader getBlockReader(LocatedBlock targetBlock,
389+
long offsetInBlock, long length, InetSocketAddress targetAddr,
390+
StorageType storageType, DatanodeInfo datanode)
391+
throws IOException {
392+
BlockReader real = super.getBlockReader(targetBlock, offsetInBlock,
393+
length, targetAddr, storageType, datanode);
394+
BlockReader wrapped = mock(BlockReader.class, delegatesTo(real));
395+
doAnswer(inv -> {
396+
if (!((ByteBuffer) inv.getArgument(0)).hasRemaining()) {
397+
emptyBufferReads.incrementAndGet();
398+
}
399+
return real.read(inv.getArgument(0));
400+
}).when(wrapped).read(any(ByteBuffer.class));
401+
return wrapped;
402+
}
403+
}) {
404+
byte[] dest = new byte[fileLength];
405+
assertEquals(fileLength, in.read(0L, dest, 0, dest.length));
406+
assertEquals(0, emptyBufferReads.get(),
407+
"DFSInputStream should not call BlockReader.read with an empty buffer");
408+
}
409+
}
410+
}
367411
}

0 commit comments

Comments
 (0)