|
@@ -17,6 +17,9 @@
|
|
*/
|
|
*/
|
|
package org.apache.hadoop.net;
|
|
package org.apache.hadoop.net;
|
|
|
|
|
|
|
|
+import java.net.InetAddress;
|
|
|
|
+import java.net.SocketException;
|
|
|
|
+import java.net.UnknownHostException;
|
|
import java.util.ArrayList;
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.List;
|
|
import java.util.Map;
|
|
import java.util.Map;
|
|
@@ -37,16 +40,11 @@ public class CachedDNSToSwitchMapping implements DNSToSwitchMapping {
|
|
this.rawMapping = rawMapping;
|
|
this.rawMapping = rawMapping;
|
|
}
|
|
}
|
|
|
|
|
|
- public List<String> resolve(List<String> names) {
|
|
|
|
- // normalize all input names to be in the form of IP addresses
|
|
|
|
- names = NetUtils.normalizeHostNames(names);
|
|
|
|
-
|
|
|
|
- List <String> result = new ArrayList<String>(names.size());
|
|
|
|
- if (names.isEmpty()) {
|
|
|
|
- return result;
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
-
|
|
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Returns the hosts from 'names' that have not been cached previously
|
|
|
|
+ */
|
|
|
|
+ private List<String> getUncachedHosts(List<String> names) {
|
|
// find out all names without cached resolved location
|
|
// find out all names without cached resolved location
|
|
List<String> unCachedHosts = new ArrayList<String>(names.size());
|
|
List<String> unCachedHosts = new ArrayList<String>(names.size());
|
|
for (String name : names) {
|
|
for (String name : names) {
|
|
@@ -54,27 +52,81 @@ public class CachedDNSToSwitchMapping implements DNSToSwitchMapping {
|
|
unCachedHosts.add(name);
|
|
unCachedHosts.add(name);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- // Resolve those names
|
|
|
|
- List<String> rNames = rawMapping.resolve(unCachedHosts);
|
|
|
|
-
|
|
|
|
|
|
+ return unCachedHosts;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Caches the resolved hosts
|
|
|
|
+ */
|
|
|
|
+ private void cacheResolvedHosts(List<String> uncachedHosts,
|
|
|
|
+ List<String> resolvedHosts) {
|
|
// Cache the result
|
|
// Cache the result
|
|
- if (rNames != null) {
|
|
|
|
- for (int i=0; i<unCachedHosts.size(); i++) {
|
|
|
|
- cache.put(unCachedHosts.get(i), rNames.get(i));
|
|
|
|
|
|
+ if (resolvedHosts != null) {
|
|
|
|
+ for (int i=0; i<uncachedHosts.size(); i++) {
|
|
|
|
+ cache.put(uncachedHosts.get(i), resolvedHosts.get(i));
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Returns the cached resolution of the list of hostnames/addresses.
|
|
|
|
+ * Returns null if any of the names are not currently in the cache
|
|
|
|
+ */
|
|
|
|
+ private List<String> getCachedHosts(List<String> names) {
|
|
|
|
+ List<String> result = new ArrayList<String>(names.size());
|
|
// Construct the result
|
|
// Construct the result
|
|
for (String name : names) {
|
|
for (String name : names) {
|
|
- //now everything is in the cache
|
|
|
|
String networkLocation = cache.get(name);
|
|
String networkLocation = cache.get(name);
|
|
if (networkLocation != null) {
|
|
if (networkLocation != null) {
|
|
result.add(networkLocation);
|
|
result.add(networkLocation);
|
|
- } else { //resolve all or nothing
|
|
|
|
|
|
+ } else {
|
|
return null;
|
|
return null;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * Resolves host names and adds them to the cache.
|
|
|
|
+ * Unlike the 'resolve" method, this won't hide UnknownHostExceptions
|
|
|
|
+ *
|
|
|
|
+ * @param names to resolve
|
|
|
|
+ * @return List of resolved names
|
|
|
|
+ * @throws UnknownHostException if any hosts cannot be resolved
|
|
|
|
+ */
|
|
|
|
+ public List<String> resolveValidHosts(List<String> names)
|
|
|
|
+ throws UnknownHostException {
|
|
|
|
+ if (names.isEmpty()) {
|
|
|
|
+ return new ArrayList<String>();
|
|
|
|
+ }
|
|
|
|
+ List<String> addresses = new ArrayList<String>(names.size());
|
|
|
|
+ for (String name : names) {
|
|
|
|
+ addresses.add(InetAddress.getByName(name).getHostAddress());
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<String> uncachedHosts = this.getUncachedHosts(names);
|
|
|
|
+
|
|
|
|
+ // Resolve the uncached hosts
|
|
|
|
+ List<String> resolvedHosts = rawMapping.resolveValidHosts(uncachedHosts);
|
|
|
|
+ this.cacheResolvedHosts(uncachedHosts, resolvedHosts);
|
|
|
|
+ return this.getCachedHosts(addresses);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ public List<String> resolve(List<String> names) {
|
|
|
|
+ // normalize all input names to be in the form of IP addresses
|
|
|
|
+ names = NetUtils.normalizeHostNames(names);
|
|
|
|
+
|
|
|
|
+ List <String> result = new ArrayList<String>(names.size());
|
|
|
|
+ if (names.isEmpty()) {
|
|
|
|
+ return result;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ List<String> uncachedHosts = this.getUncachedHosts(names);
|
|
|
|
+
|
|
|
|
+ // Resolve the uncached hosts
|
|
|
|
+ List<String> resolvedHosts = rawMapping.resolve(uncachedHosts);
|
|
|
|
+ this.cacheResolvedHosts(uncachedHosts, resolvedHosts);
|
|
|
|
+ return this.getCachedHosts(names);
|
|
|
|
+
|
|
|
|
+ }
|
|
}
|
|
}
|