|
@@ -1584,10 +1584,12 @@ public class DFSClient implements FSConstants, java.io.Closeable {
|
|
|
* Fetch it from the namenode if not cached.
|
|
|
*
|
|
|
* @param offset
|
|
|
+ * @param updatePosition whether to update current position
|
|
|
* @return located block
|
|
|
* @throws IOException
|
|
|
*/
|
|
|
- private LocatedBlock getBlockAt(long offset) throws IOException {
|
|
|
+ private synchronized LocatedBlock getBlockAt(long offset,
|
|
|
+ boolean updatePosition) throws IOException {
|
|
|
assert (locatedBlocks != null) : "locatedBlocks is null";
|
|
|
// search cached blocks first
|
|
|
int targetBlockIdx = locatedBlocks.findBlock(offset);
|
|
@@ -1601,9 +1603,11 @@ public class DFSClient implements FSConstants, java.io.Closeable {
|
|
|
}
|
|
|
LocatedBlock blk = locatedBlocks.get(targetBlockIdx);
|
|
|
// update current position
|
|
|
- this.pos = offset;
|
|
|
- this.blockEnd = blk.getStartOffset() + blk.getBlockSize() - 1;
|
|
|
- this.currentBlock = blk.getBlock();
|
|
|
+ if (updatePosition) {
|
|
|
+ this.pos = offset;
|
|
|
+ this.blockEnd = blk.getStartOffset() + blk.getBlockSize() - 1;
|
|
|
+ this.currentBlock = blk.getBlock();
|
|
|
+ }
|
|
|
return blk;
|
|
|
}
|
|
|
|
|
@@ -1670,7 +1674,7 @@ public class DFSClient implements FSConstants, java.io.Closeable {
|
|
|
//
|
|
|
// Compute desired block
|
|
|
//
|
|
|
- LocatedBlock targetBlock = getBlockAt(target);
|
|
|
+ LocatedBlock targetBlock = getBlockAt(target, true);
|
|
|
assert (target==this.pos) : "Wrong postion " + pos + " expect " + target;
|
|
|
long offsetIntoBlock = target - targetBlock.getStartOffset();
|
|
|
|
|
@@ -1859,13 +1863,16 @@ public class DFSClient implements FSConstants, java.io.Closeable {
|
|
|
if (nodes == null || nodes.length == 0) {
|
|
|
LOG.info("No node available for block: " + blockInfo);
|
|
|
}
|
|
|
- LOG.info("Could not obtain block " + block.getBlock() + " from any node: " + ie);
|
|
|
+ LOG.info("Could not obtain block " + block.getBlock()
|
|
|
+ + " from any node: " + ie
|
|
|
+ + ". Will get new block locations from namenode and retry...");
|
|
|
try {
|
|
|
Thread.sleep(3000);
|
|
|
} catch (InterruptedException iex) {
|
|
|
}
|
|
|
deadNodes.clear(); //2nd option is to remove only nodes[blockId]
|
|
|
openInfo();
|
|
|
+ block = getBlockAt(block.getStartOffset(), false);
|
|
|
failures++;
|
|
|
continue;
|
|
|
}
|
|
@@ -1878,10 +1885,13 @@ public class DFSClient implements FSConstants, java.io.Closeable {
|
|
|
// Connect to best DataNode for desired Block, with potential offset
|
|
|
//
|
|
|
Socket dn = null;
|
|
|
- int numAttempts = block.getLocations().length;
|
|
|
- IOException ioe = null;
|
|
|
-
|
|
|
- while (dn == null && numAttempts-- > 0 ) {
|
|
|
+
|
|
|
+ while (true) {
|
|
|
+ // cached block locations may have been updated by chooseDataNode()
|
|
|
+ // or fetchBlockAt(). Always get the latest list of locations at the
|
|
|
+ // start of the loop.
|
|
|
+ block = getBlockAt(block.getStartOffset(), false);
|
|
|
+
|
|
|
DNAddrPair retval = chooseDataNode(block);
|
|
|
DatanodeInfo chosenNode = retval.info;
|
|
|
InetSocketAddress targetAddr = retval.addr;
|
|
@@ -1906,13 +1916,11 @@ public class DFSClient implements FSConstants, java.io.Closeable {
|
|
|
}
|
|
|
return;
|
|
|
} catch (ChecksumException e) {
|
|
|
- ioe = e;
|
|
|
LOG.warn("fetchBlockByteRange(). Got a checksum exception for " +
|
|
|
src + " at " + block.getBlock() + ":" +
|
|
|
e.getPos() + " from " + chosenNode.getName());
|
|
|
reportChecksumFailure(src, block.getBlock(), chosenNode);
|
|
|
} catch (IOException e) {
|
|
|
- ioe = e;
|
|
|
LOG.warn("Failed to connect to " + targetAddr +
|
|
|
" for file " + src +
|
|
|
" for block " + block.getBlock().getBlockId() + ":" +
|
|
@@ -1920,12 +1928,10 @@ public class DFSClient implements FSConstants, java.io.Closeable {
|
|
|
} finally {
|
|
|
IOUtils.closeStream(reader);
|
|
|
IOUtils.closeSocket(dn);
|
|
|
- dn = null;
|
|
|
}
|
|
|
// Put chosen node into dead list, continue
|
|
|
addToDeadNodes(chosenNode);
|
|
|
}
|
|
|
- throw (ioe == null) ? new IOException("Could not read data") : ioe;
|
|
|
}
|
|
|
|
|
|
/**
|