From 5d8688fc3d268ff051c361eb716f39d8fb4a9e8e Mon Sep 17 00:00:00 2001 From: Jean-Daniel Cryans Date: Thu, 10 Dec 2009 23:17:35 +0000 Subject: [PATCH] HBASE-2031 When starting HQuorumPeer, try to match on more than 1 address git-svn-id: https://svn.apache.org/repos/asf/hadoop/hbase/trunk@889461 13f79535-47bb-0310-9956-ffa450edef68 --- CHANGES.txt | 1 + .../hadoop/hbase/zookeeper/HQuorumPeer.java | 39 ++++++++++++------- 2 files changed, 27 insertions(+), 13 deletions(-) diff --git a/CHANGES.txt b/CHANGES.txt index 2b2c2527b9c..1396c5306bc 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -224,6 +224,7 @@ Release 0.21.0 - Unreleased (Dave Latham via Stack) HBASE-2013 Add useful helpers to HBaseTestingUtility.java (Lars George via J-D) + HBASE-2031 When starting HQuorumPeer, try to match on more than 1 address NEW FEATURES HBASE-1901 "General" partitioner for "hbase-48" bulk (behind the api, write diff --git a/src/java/org/apache/hadoop/hbase/zookeeper/HQuorumPeer.java b/src/java/org/apache/hadoop/hbase/zookeeper/HQuorumPeer.java index c7cc69b15fd..2da4bd85a9c 100644 --- a/src/java/org/apache/hadoop/hbase/zookeeper/HQuorumPeer.java +++ b/src/java/org/apache/hadoop/hbase/zookeeper/HQuorumPeer.java @@ -24,7 +24,12 @@ import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.net.UnknownHostException; +import java.net.InetAddress; +import java.net.NetworkInterface; import java.util.Properties; +import java.util.List; +import java.util.ArrayList; +import java.util.Enumeration; import java.util.Map.Entry; import org.apache.commons.logging.Log; @@ -90,22 +95,32 @@ public class HQuorumPeer implements HConstants { return address.equals("localhost") || address.equals("127.0.0.1"); } - private static boolean hostEquals(String addrA, String addrB) { - if (addrA.contains(".") && addrB.contains(".")) { - return addrA.equals(addrB); - } - String hostA = StringUtils.simpleHostname(addrA); - String hostB = StringUtils.simpleHostname(addrB); - return hostA.equals(hostB); - } + private static void writeMyID(Properties properties) throws IOException { + long myId = -1; - private static void writeMyID(Properties properties) throws UnknownHostException, IOException { HBaseConfiguration conf = new HBaseConfiguration(); String myAddress = DNS.getDefaultHost( conf.get("hbase.zookeeper.dns.interface","default"), conf.get("hbase.zookeeper.dns.nameserver","default")); - long myId = -1; + List ips = new ArrayList(); + + // Add what could be the best (configured) match + ips.add(myAddress.contains(".") ? + myAddress : + StringUtils.simpleHostname(myAddress)); + + // For all nics get all hostnames and IPs + Enumeration nics = NetworkInterface.getNetworkInterfaces(); + while(nics.hasMoreElements()) { + Enumeration rawAdrs = + ((NetworkInterface)nics.nextElement()).getInetAddresses(); + while(rawAdrs.hasMoreElements()) { + InetAddress inet = (InetAddress) rawAdrs.nextElement(); + ips.add(StringUtils.simpleHostname(inet.getHostName())); + ips.add(inet.getHostAddress()); + } + } for (Entry entry : properties.entrySet()) { String key = entry.getKey().toString().trim(); @@ -115,9 +130,7 @@ public class HQuorumPeer implements HConstants { long id = Long.parseLong(key.substring(dot + 1)); String[] parts = value.split(":"); String address = parts[0]; - if (addressIsLocalHost(address) || hostEquals(myAddress, address)) { - LOG.debug("found my address: " + myAddress + ", in list: " + address + - ", setting myId to " + id); + if (addressIsLocalHost(address) || ips.contains(address)) { myId = id; break; }