Author: mc
Date: Sat Mar 4 12:47:01 2006
New Revision: 383197
URL: http://svn.apache.org/viewcvs?rev=383197&view=rev
Log:
This code makes sure we read from a local block,
if available. I thought this code had already been
committed some time ago, but the workspace doesn't
have it.
Modified:
lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/DFSClient.java
Modified: lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/DFSClient.java
URL:
http://svn.apache.org/viewcvs/lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/DFSClient.java?rev=383197&r1=383196&r2=383197&view=diff
==============================================================================
--- lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/DFSClient.java (original)
+++ lucene/hadoop/trunk/src/java/org/apache/hadoop/dfs/DFSClient.java Sat Mar
4 12:47:01 2006
@@ -42,6 +42,7 @@
public static final Logger LOG =
LogFormatter.getLogger("org.apache.hadoop.fs.DFSClient");
static int MAX_BLOCK_ACQUIRE_FAILURES = 10;
ClientProtocol namenode;
+ String localName;
boolean running = true;
Random r = new Random();
String clientName;
@@ -53,6 +54,11 @@
*/
public DFSClient(InetSocketAddress nameNodeAddr, Configuration conf) {
this.namenode = (ClientProtocol) RPC.getProxy(ClientProtocol.class,
nameNodeAddr, conf);
+ try {
+ this.localName = InetAddress.getLocalHost().getHostName();
+ } catch (UnknownHostException uhe) {
+ this.localName = "";
+ }
this.clientName = "DFSClient_" + r.nextInt();
this.leaseChecker = new Daemon(new LeaseChecker());
this.leaseChecker.start();
@@ -188,8 +194,8 @@
}
/**
- * Pick the best/closest node which to stream the data.
- * For now, just pick the first on the list.
+ * Pick the best node from which to stream the data.
+ * That's the local one, if available.
*/
private DatanodeInfo bestNode(DatanodeInfo nodes[], TreeSet deadNodes)
throws IOException {
if ((nodes == null) ||
@@ -197,9 +203,25 @@
throw new IOException("No live nodes contain current block");
}
DatanodeInfo chosenNode = null;
- do {
- chosenNode = nodes[Math.abs(r.nextInt()) % nodes.length];
- } while (deadNodes.contains(chosenNode));
+ for (int i = 0; i < nodes.length; i++) {
+ if (deadNodes.contains(nodes[i])) {
+ continue;
+ }
+ String nodename = nodes[i].getName().toString();
+ int colon = nodename.indexOf(':');
+ if (colon >= 0) {
+ nodename = nodename.substring(0, colon);
+ }
+ if (localName.equals(nodename)) {
+ chosenNode = nodes[i];
+ break;
+ }
+ }
+ if (chosenNode == null) {
+ do {
+ chosenNode = nodes[Math.abs(r.nextInt()) % nodes.length];
+ } while (deadNodes.contains(chosenNode));
+ }
return chosenNode;
}