From d8383c687c95dbb37effa307ab2d41497da1cfc2 Mon Sep 17 00:00:00 2001 From: Walter Su Date: Mon, 28 Mar 2016 15:44:25 +0800 Subject: [PATCH] HDFS-10182. Hedged read might overwrite user's buf. Contributed by zhouyingchao. --- .../org/apache/hadoop/hdfs/DFSInputStream.java | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSInputStream.java b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSInputStream.java index 7661e82b8a..2689841444 100644 --- a/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSInputStream.java +++ b/hadoop-hdfs-project/hadoop-hdfs-client/src/main/java/org/apache/hadoop/hdfs/DFSInputStream.java @@ -1279,7 +1279,7 @@ private void hedgedFetchBlockByteRange(LocatedBlock block, long start, // chooseDataNode is a commitment. If no node, we go to // the NN to reget block locations. Only go here on first read. chosenNode = chooseDataNode(block, ignored); - bb = ByteBuffer.wrap(buf, offset, len); + bb = ByteBuffer.allocate(len); Callable getFromDataNodeCallable = getFromOneDataNode( chosenNode, block, start, end, bb, corruptedBlocks, hedgedReadId++); @@ -1290,7 +1290,9 @@ private void hedgedFetchBlockByteRange(LocatedBlock block, long start, Future future = hedgedService.poll( conf.getHedgedReadThresholdMillis(), TimeUnit.MILLISECONDS); if (future != null) { - future.get(); + ByteBuffer result = future.get(); + System.arraycopy(result.array(), result.position(), buf, offset, + len); return; } DFSClient.LOG.debug("Waited {}ms to read from {}; spawning hedged " @@ -1328,13 +1330,9 @@ private void hedgedFetchBlockByteRange(LocatedBlock block, long start, ByteBuffer result = getFirstToComplete(hedgedService, futures); // cancel the rest. cancelAll(futures); - if (result.array() != buf) { // compare the array pointers - dfsClient.getHedgedReadMetrics().incHedgedReadWins(); - System.arraycopy(result.array(), result.position(), buf, offset, - len); - } else { - dfsClient.getHedgedReadMetrics().incHedgedReadOps(); - } + dfsClient.getHedgedReadMetrics().incHedgedReadWins(); + System.arraycopy(result.array(), result.position(), buf, offset, + len); return; } catch (InterruptedException ie) { // Ignore and retry