diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/ServerName.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/ServerName.java index 91e4c9d..22586de 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/ServerName.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/ServerName.java @@ -18,6 +18,7 @@ */ package org.apache.hadoop.hbase; +import com.google.common.net.InetAddresses; import com.google.protobuf.InvalidProtocolBufferException; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; @@ -33,16 +34,16 @@ import java.util.regex.Pattern; /** * Instance of an HBase ServerName. - * A server name is used uniquely identifying a server instance and is made - * of the combination of hostname, port, and startcode. The startcode - * distingushes restarted servers on same hostname and port (startcode is - * usually timestamp of server startup). The {@link #toString()} format of - * ServerName is safe to use in the filesystem and as znode name up in - * ZooKeeper. Its format is: + * A server name is used uniquely identifying a server instance in a cluster and is made + * of the combination of hostname, port, and startcode. The startcode distingushes restarted + * servers on same hostname and port (startcode is usually timestamp of server startup). The + * {@link #toString()} format of ServerName is safe to use in the filesystem and as znode name + * up in ZooKeeper. Its format is: * <hostname> '{@link #SERVERNAME_SEPARATOR}' <port> '{@link #SERVERNAME_SEPARATOR}' <startcode>. - * For example, if hostname is example.org, port is 1234, + * For example, if hostname is www.example.org, port is 1234, * and the startcode for the regionserver is 1212121212, then - * the {@link #toString()} would be example.org,1234,1212121212. + * the {@link #toString()} would be www,1234,1212121212. Note how the hostname + * will have any domain portion stripped. * *

You can obtain a versioned serialized form of this class by calling * {@link #getVersionedBytes()}. To deserialize, call {@link #parseVersionedServerName(byte[])} @@ -83,7 +84,7 @@ public class ServerName implements Comparable { public static final String UNKNOWN_SERVERNAME = "#unknown#"; private final String servername; - private final String hostname; + private final String hostnameOnly; private final int port; private final long startcode; @@ -95,10 +96,23 @@ public class ServerName implements Comparable { public static final List EMPTY_SERVER_LIST = new ArrayList(0); public ServerName(final String hostname, final int port, final long startcode) { - this.hostname = hostname; + // Drop the domain is there is one; no need of it in a local cluster. With it, we get long + // unwieldy names. + this.hostnameOnly = getHostNameMinusDomain(hostname); this.port = port; this.startcode = startcode; - this.servername = getServerName(hostname, port, startcode); + this.servername = getServerName(this.hostnameOnly, port, startcode); + } + + /** + * @param hostname + * @return hostname minus the domain, if there is one (will do pass-through on ip addresses) + */ + static String getHostNameMinusDomain(final String hostname) { + if (InetAddresses.isInetAddress(hostname)) return hostname; + String [] parts = hostname.split("\\."); + if (parts == null || parts.length == 0) return hostname; + return parts[0]; } public ServerName(final String serverName) { @@ -116,7 +130,7 @@ public class ServerName implements Comparable { throw new IllegalArgumentException("Passed hostname is null or empty"); } int index = serverName.indexOf(SERVERNAME_SEPARATOR); - return serverName.substring(0, index); + return getHostNameMinusDomain(serverName.substring(0, index)); } public static int parsePort(final String serverName) { @@ -150,7 +164,7 @@ public class ServerName implements Comparable { } public String getHostname() { - return hostname; + return hostnameOnly; } public int getPort() { @@ -162,15 +176,17 @@ public class ServerName implements Comparable { } /** + * For internal use only. * @param hostName * @param port * @param startcode * @return Server name made of the concatenation of hostname, port and * startcode formatted as <hostname> ',' <port> ',' <startcode> */ - public static String getServerName(String hostName, int port, long startcode) { - final StringBuilder name = new StringBuilder(hostName.length() + 1 + 5 + 1 + 13); - name.append(hostName); + static String getServerName(String hostName, int port, long startcode) { + String hostnameOnly = getHostNameMinusDomain(hostName); + final StringBuilder name = new StringBuilder(hostnameOnly.length() + 1 + 5 + 1 + 13); + name.append(hostnameOnly); name.append(SERVERNAME_SEPARATOR); name.append(port); name.append(SERVERNAME_SEPARATOR); @@ -197,7 +213,7 @@ public class ServerName implements Comparable { * {@link Addressing#createHostAndPortStr(String, int)} */ public String getHostAndPort() { - return Addressing.createHostAndPortStr(this.hostname, this.port); + return Addressing.createHostAndPortStr(this.hostnameOnly, this.port); } /** diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/JVMClusterUtil.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/JVMClusterUtil.java index 7d0f775..b235dab 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/util/JVMClusterUtil.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/util/JVMClusterUtil.java @@ -48,7 +48,7 @@ public class JVMClusterUtil { private final HRegionServer regionServer; public RegionServerThread(final HRegionServer r, final int index) { - super(r, "RegionServer:" + index + ";" + r.getServerName()); + super(r, "RS:" + index + ";" + r.getServerName()); this.regionServer = r; } @@ -109,7 +109,7 @@ public class JVMClusterUtil { private final HMaster master; public MasterThread(final HMaster m, final int index) { - super(m, "Master:" + index + ";" + m.getServerName()); + super(m, "M:" + index + ";" + m.getServerName()); this.master = m; } diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/TestServerName.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestServerName.java index 52a7c1a..8f0b068 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/TestServerName.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestServerName.java @@ -32,6 +32,20 @@ import org.junit.experimental.categories.Category; @Category(SmallTests.class) public class TestServerName { @Test + public void testGetHostNameMinusDomain() { + assertEquals("2607:f0d0:1002:51::4", + ServerName.getHostNameMinusDomain("2607:f0d0:1002:51::4")); + assertEquals("2607:f0d0:1002:0051:0000:0000:0000:0004", + ServerName.getHostNameMinusDomain("2607:f0d0:1002:0051:0000:0000:0000:0004")); + assertEquals("1.1.1.1", ServerName.getHostNameMinusDomain("1.1.1.1")); + assertEquals("x", ServerName.getHostNameMinusDomain("x")); + assertEquals("x", ServerName.getHostNameMinusDomain("x.y.z")); + assertEquals("asf000", ServerName.getHostNameMinusDomain("asf000.sp2.ygridcore.net")); + ServerName sn = new ServerName("asf000.sp2.ygridcore.net", 1, 1); + assertEquals("asf000,1,1", sn.toString()); + } + + @Test public void testRegexPatterns() { assertTrue(Pattern.matches(Addressing.VALID_PORT_REGEX, "123")); assertFalse(Pattern.matches(Addressing.VALID_PORT_REGEX, "")); @@ -46,8 +60,8 @@ public class TestServerName { final String snStr = "www.example.org,1234,5678"; ServerName sn = new ServerName(snStr); byte [] versionedBytes = sn.getVersionedBytes(); - assertEquals(snStr, ServerName.parseVersionedServerName(versionedBytes).toString()); - final String hostnamePortStr = "www.example.org:1234"; + assertEquals(sn.toString(), ServerName.parseVersionedServerName(versionedBytes).toString()); + final String hostnamePortStr = sn.getHostAndPort(); byte [] bytes = Bytes.toBytes(hostnamePortStr); String expecting = hostnamePortStr.replace(":", ServerName.SERVERNAME_SEPARATOR) + @@ -69,8 +83,7 @@ public class TestServerName { assertEquals(sn.toString(), ServerName.getServerName("www.example.org:1234", 5678)); assertEquals(sn.toString(), - "www.example.org" + ServerName.SERVERNAME_SEPARATOR + - "1234" + ServerName.SERVERNAME_SEPARATOR + "5678"); + "www" + ServerName.SERVERNAME_SEPARATOR + "1234" + ServerName.SERVERNAME_SEPARATOR + "5678"); } @Test