Skip to content

Commit 8975a6e

Browse files
Tom McCormickmccormickt12
authored andcommitted
HDFS-16896 clear ignoredNodes list when we clear deadnode list on ref… (apache#5322) (apache#5444) (apache#99)
Cherry picked from: 162288b Co-authored-by: Tom McCormick <[email protected]> ACLOVERRIDE for oss email Co-authored-by: Tom <[email protected]>
1 parent f37ab11 commit 8975a6e

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

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

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ boolean deadNodesContain(DatanodeInfo nodeInfo) {
224224
}
225225

226226
/**
227-
* Grab the open-file info from namenode
227+
* Grab the open-file info from namenode.
228228
* @param refreshLocatedBlocks whether to re-fetch locatedblocks
229229
*/
230230
void openInfo(boolean refreshLocatedBlocks) throws IOException {
@@ -940,7 +940,8 @@ private DNAddrPair chooseDataNode(LocatedBlock block,
940940
* @return Returns chosen DNAddrPair; Can be null if refetchIfRequired is
941941
* false.
942942
*/
943-
private DNAddrPair chooseDataNode(LocatedBlock block,
943+
@VisibleForTesting
944+
DNAddrPair chooseDataNode(LocatedBlock block,
944945
Collection<DatanodeInfo> ignoredNodes, boolean refetchIfRequired)
945946
throws IOException {
946947
while (true) {
@@ -955,6 +956,14 @@ private DNAddrPair chooseDataNode(LocatedBlock block,
955956
}
956957
}
957958

959+
/**
960+
* RefetchLocations should only be called when there are no active requests
961+
* to datanodes. In the hedged read case this means futures should be empty.
962+
* @param block The locatedBlock to get new datanode locations for.
963+
* @param ignoredNodes A list of ignored nodes. This list can be null and can be cleared.
964+
* @return the locatedBlock with updated datanode locations.
965+
* @throws IOException
966+
*/
958967
private LocatedBlock refetchLocations(LocatedBlock block,
959968
Collection<DatanodeInfo> ignoredNodes) throws IOException {
960969
String errMsg = getBestNodeDNAddrPairErrorString(block.getLocations(),
@@ -999,13 +1008,24 @@ private LocatedBlock refetchLocations(LocatedBlock block,
9991008
throw new InterruptedIOException(
10001009
"Interrupted while choosing DataNode for read.");
10011010
}
1002-
clearLocalDeadNodes(); //2nd option is to remove only nodes[blockId]
1011+
clearCachedNodeState(ignoredNodes);
10031012
openInfo(true);
10041013
block = refreshLocatedBlock(block);
10051014
failures++;
10061015
return block;
10071016
}
10081017

1018+
/**
1019+
* Clear both the dead nodes and the ignored nodes
1020+
* @param ignoredNodes is cleared
1021+
*/
1022+
private void clearCachedNodeState(Collection<DatanodeInfo> ignoredNodes) {
1023+
clearLocalDeadNodes(); //2nd option is to remove only nodes[blockId]
1024+
if (ignoredNodes != null) {
1025+
ignoredNodes.clear();
1026+
}
1027+
}
1028+
10091029
/**
10101030
* Get the best node from which to stream the data.
10111031
* @param block LocatedBlock, containing nodes in priority order.
@@ -1337,8 +1357,12 @@ private void hedgedFetchBlockByteRange(LocatedBlock block, long start,
13371357
} catch (InterruptedException ie) {
13381358
// Ignore and retry
13391359
}
1340-
if (refetch) {
1341-
refetchLocations(block, ignored);
1360+
// If refetch is true, then all nodes are in deadNodes or ignoredNodes.
1361+
// We should loop through all futures and remove them, so we do not
1362+
// have concurrent requests to the same node.
1363+
// Once all futures are cleared, we can clear the ignoredNodes and retry.
1364+
if (refetch && futures.isEmpty()) {
1365+
block = refetchLocations(block, ignored);
13421366
}
13431367
// We got here if exception. Ignore this node on next go around IFF
13441368
// we found a chosenNode to hedge read against.

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestDFSInputStreamBlockLocations.java

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727

2828
import java.io.IOException;
2929
import java.net.InetSocketAddress;
30+
import java.util.ArrayList;
3031
import java.util.Arrays;
3132
import java.util.Collection;
3233
import java.util.HashMap;
@@ -35,11 +36,14 @@
3536
import org.apache.hadoop.fs.FSDataOutputStream;
3637
import org.apache.hadoop.fs.Path;
3738
import org.apache.hadoop.hdfs.client.HdfsClientConfigKeys;
39+
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
3840
import org.apache.hadoop.hdfs.protocol.HdfsConstants.DatanodeReportType;
41+
import org.apache.hadoop.hdfs.protocol.LocatedBlock;
3942
import org.apache.hadoop.hdfs.protocol.LocatedBlocks;
4043
import org.apache.hadoop.hdfs.server.datanode.DataNode;
4144
import org.apache.hadoop.util.Time;
4245
import org.junit.After;
46+
import org.junit.Assert;
4347
import org.junit.Before;
4448
import org.junit.Test;
4549
import org.junit.runner.RunWith;
@@ -200,6 +204,25 @@ public void testDeferredRegistrationGetAllBlocks() throws IOException {
200204
testWithRegistrationMethod(DFSInputStream::getAllBlocks);
201205
}
202206

207+
/**
208+
* If the ignoreList contains all datanodes, the ignoredList should be cleared to take advantage
209+
* of retries built into chooseDataNode. This is needed for hedged reads
210+
* @throws IOException
211+
*/
212+
@Test
213+
public void testClearIgnoreListChooseDataNode() throws IOException {
214+
final String fileName = "/test_cache_locations";
215+
filePath = createFile(fileName);
216+
217+
try (DFSInputStream fin = dfsClient.open(fileName)) {
218+
LocatedBlocks existing = fin.locatedBlocks;
219+
LocatedBlock block = existing.getLastLocatedBlock();
220+
ArrayList<DatanodeInfo> ignoreList = new ArrayList<>(Arrays.asList(block.getLocations()));
221+
Assert.assertNotNull(fin.chooseDataNode(block, ignoreList, true));
222+
Assert.assertEquals(0, ignoreList.size());
223+
}
224+
}
225+
203226
@FunctionalInterface
204227
interface ThrowingConsumer {
205228
void accept(DFSInputStream fin) throws IOException;

hadoop-hdfs-project/hadoop-hdfs/src/test/java/org/apache/hadoop/hdfs/TestPread.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -603,7 +603,9 @@ public Void answer(InvocationOnMock invocation) throws Throwable {
603603
input.read(0, buffer, 0, 1024);
604604
Assert.fail("Reading the block should have thrown BlockMissingException");
605605
} catch (BlockMissingException e) {
606-
assertEquals(3, input.getHedgedReadOpsLoopNumForTesting());
606+
// The result of 9 is due to 2 blocks by 4 iterations plus one because
607+
// hedgedReadOpsLoopNumForTesting is incremented at start of the loop.
608+
assertEquals(9, input.getHedgedReadOpsLoopNumForTesting());
607609
assertTrue(metrics.getHedgedReadOps() == 0);
608610
} finally {
609611
Mockito.reset(injector);

0 commit comments

Comments
 (0)