SOLR-4088: New and improved auto host detection strategy for SolrCloud.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1411466 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Mark Robert Miller 2012-11-19 22:38:28 +00:00
parent 1464045268
commit bdff3aa4a4
2 changed files with 26 additions and 1 deletions

View File

@ -70,6 +70,9 @@ New Features
* SOLR-4084: Add FuzzyLookupFactory, which is like AnalyzingSuggester except that
it can tolerate typos in the input. (Areek Zillur via Robert Muir)
* SOLR-4088: New and improved auto host detection strategy for SolrCloud.
(Raintung Li via Mark Miller)
Optimizations
----------------------

View File

@ -20,7 +20,9 @@ package org.apache.solr.cloud;
import java.io.File;
import java.io.IOException;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
@ -358,7 +360,27 @@ public final class ZkController {
private String getHostAddress(String host) throws IOException {
if (host == null) {
host = "http://" + InetAddress.getLocalHost().getHostName();
String hostaddress = InetAddress.getLocalHost().getHostAddress();
//Re-get the IP again for "127.0.0.1", the other case we trust the hosts file is right.
if("127.0.0.1".equals(hostaddress)){
Enumeration<NetworkInterface> netInterfaces = null;
try {
netInterfaces = NetworkInterface.getNetworkInterfaces();
while (netInterfaces.hasMoreElements()) {
NetworkInterface ni = netInterfaces.nextElement();
Enumeration<InetAddress> ips = ni.getInetAddresses();
while (ips.hasMoreElements()) {
InetAddress ip = ips.nextElement();
if (ip.isSiteLocalAddress()) {
hostaddress = ip.getHostAddress();
}
}
}
} catch (Throwable e) {
SolrException.log(log, "Error while looking for a better host name than 127.0.0.1", e);
}
}
host = "http://" + hostaddress;
} else {
Matcher m = URL_PREFIX.matcher(host);
if (m.matches()) {