}
//
// Compute desired block.
//
LocatedBlock targetBlock = getBlockAt(target, true, throwWhenNotFound);
// Given target<= fileLength, when and only whenallowSeektoEnd is true and
// there is no block for the file yet, getBlockAt() returns null, in this
// case we should simply return null.
//
if (targetBlock == null) {
assert target == 0;
return null;
}
assert (target==this.pos) : "Wrong postion " + pos + " expect " + target;
long offsetIntoBlock = target - targetBlock.getStartOffset();
//
// Connect to best DataNode for desired Block, with potential offset
//
DatanodeInfo chosenNode = null;
while (s == null) {
DNAddrPair retval = chooseDataNode(targetBlock);
chosenNode = retval.info;
InetSocketAddress targetAddr = retval.addr;
// try reading the block locally. if this fails, then go via
// the datanode
Block blk = targetBlock.getBlock();
try {
if (DFSClient.LOG.isDebugEnabled()) {
DFSClient.LOG.warn("blockSeekTo shortCircuitLocalReads "
+ dfsClient.shortCircuitLocalReads +
" localhost " + dfsClient.localHost +
" targetAddr " + targetAddr);
}
if (dfsClient.shortCircuitLocalReads && dfsClient.localHost != null &&
(targetAddr.equals(dfsClient.localHost) ||
targetAddr.getHostName().startsWith("localhost"))) {
blockReader = BlockReaderLocal.newBlockReader(dfsClient.conf, src,
namespaceId, blk,
chosenNode,
offsetIntoBlock,
blk.getNumBytes() - offsetIntoBlock,
dfsClient.metrics,
this.verifyChecksum,
this.clearOsBuffer);
blockReader.setReadLocal(true);
blockReader.setFsStats(dfsClient.stats);
return chosenNode;
}
} catch (IOException ex) {
DFSClient.LOG.info("Failed to read block " + targetBlock.getBlock() +
" on local machine " + dfsClient.localHost +
". Try via the datanode on " + targetAddr + ":"
+ StringUtils.stringifyException(ex));
}
try {
s = dfsClient.socketFactory.createSocket();
NetUtils.connect(s, targetAddr, dfsClient.socketTimeout,
dfsClient.ipTosValue);
s.setSoTimeout(dfsClient.socketTimeout);
long minReadSpeedBps = (dfsClient.numNodeLeft(targetBlock.getLocations(),
deadNodes) > 1) ? dfsClient.minReadSpeedBps : -1;
blockReader = BlockReader.newBlockReader(
dfsClient.getDataTransferProtocolVersion(), namespaceId,
s, src, blk.getBlockId(),
blk.getGenerationStamp(),