001/** 002 * 003 * Licensed to the Apache Software Foundation (ASF) under one 004 * or more contributor license agreements. See the NOTICE file 005 * distributed with this work for additional information 006 * regarding copyright ownership. The ASF licenses this file 007 * to you under the Apache License, Version 2.0 (the 008 * "License"); you may not use this file except in compliance 009 * with the License. You may obtain a copy of the License at 010 * 011 * http://www.apache.org/licenses/LICENSE-2.0 012 * 013 * Unless required by applicable law or agreed to in writing, software 014 * distributed under the License is distributed on an "AS IS" BASIS, 015 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 016 * See the License for the specific language governing permissions and 017 * limitations under the License. 018 */ 019package org.apache.hadoop.hbase; 020 021import java.io.Serializable; 022import java.util.ArrayList; 023import java.util.List; 024import java.util.Locale; 025import java.util.regex.Pattern; 026 027import org.apache.hadoop.hbase.net.Address; 028import org.apache.hadoop.hbase.util.Addressing; 029import org.apache.hadoop.hbase.util.Bytes; 030import org.apache.yetus.audience.InterfaceAudience; 031 032import org.apache.hbase.thirdparty.com.google.common.net.InetAddresses; 033 034 035/** 036 * Name of a particular incarnation of an HBase Server. 037 * A {@link ServerName} is used uniquely identifying a server instance in a cluster and is made 038 * of the combination of hostname, port, and startcode. The startcode distinguishes restarted 039 * servers on same hostname and port (startcode is usually timestamp of server startup). The 040 * {@link #toString()} format of ServerName is safe to use in the filesystem and as znode name 041 * up in ZooKeeper. Its format is: 042 * <code><hostname> '{@link #SERVERNAME_SEPARATOR}' <port> 043 * '{@link #SERVERNAME_SEPARATOR}' <startcode></code>. 044 * For example, if hostname is <code>www.example.org</code>, port is <code>1234</code>, 045 * and the startcode for the regionserver is <code>1212121212</code>, then 046 * the {@link #toString()} would be <code>www.example.org,1234,1212121212</code>. 047 * 048 * <p>You can obtain a versioned serialized form of this class by calling 049 * {@link #getVersionedBytes()}. To deserialize, call 050 * {@link #parseVersionedServerName(byte[])}. 051 * 052 * <p>Use {@link #getAddress()} to obtain the Server hostname + port 053 * (Endpoint/Socket Address). 054 * 055 * <p>Immutable. 056 */ 057@InterfaceAudience.Public 058public class ServerName implements Comparable<ServerName>, Serializable { 059 private static final long serialVersionUID = 1367463982557264981L; 060 061 /** 062 * Version for this class. 063 * Its a short rather than a byte so I can for sure distinguish between this 064 * version of this class and the version previous to this which did not have 065 * a version. 066 */ 067 private static final short VERSION = 0; 068 static final byte [] VERSION_BYTES = Bytes.toBytes(VERSION); 069 070 /** 071 * What to use if no startcode supplied. 072 */ 073 public static final int NON_STARTCODE = -1; 074 075 /** 076 * This character is used as separator between server hostname, port and 077 * startcode. 078 */ 079 public static final String SERVERNAME_SEPARATOR = ","; 080 081 public static final Pattern SERVERNAME_PATTERN = 082 Pattern.compile("[^" + SERVERNAME_SEPARATOR + "]+" + 083 SERVERNAME_SEPARATOR + Addressing.VALID_PORT_REGEX + 084 SERVERNAME_SEPARATOR + Addressing.VALID_PORT_REGEX + "$"); 085 086 /** 087 * What to use if server name is unknown. 088 */ 089 public static final String UNKNOWN_SERVERNAME = "#unknown#"; 090 091 private final String servername; 092 private final long startcode; 093 private transient Address address; 094 095 /** 096 * Cached versioned bytes of this ServerName instance. 097 * @see #getVersionedBytes() 098 */ 099 private byte [] bytes; 100 public static final List<ServerName> EMPTY_SERVER_LIST = new ArrayList<>(0); 101 102 protected ServerName(final String hostname, final int port, final long startcode) { 103 this(Address.fromParts(hostname, port), startcode); 104 } 105 106 private ServerName(final Address address, final long startcode) { 107 // Use HostAndPort to host port and hostname. Does validation and can do ipv6 108 this.address = address; 109 this.startcode = startcode; 110 this.servername = getServerName(this.address.getHostname(), 111 this.address.getPort(), startcode); 112 } 113 114 private ServerName(final String serverName) { 115 this(parseHostname(serverName), parsePort(serverName), 116 parseStartcode(serverName)); 117 } 118 119 private ServerName(final String hostAndPort, final long startCode) { 120 this(Address.fromString(hostAndPort), startCode); 121 } 122 123 /** 124 * @param hostname 125 * @return hostname minus the domain, if there is one (will do pass-through on ip addresses) 126 * @deprecated Since 2.0. This is for internal use only. 127 */ 128 @Deprecated 129 // Make this private in hbase-3.0. 130 static String getHostNameMinusDomain(final String hostname) { 131 if (InetAddresses.isInetAddress(hostname)) return hostname; 132 String [] parts = hostname.split("\\."); 133 if (parts == null || parts.length == 0) return hostname; 134 return parts[0]; 135 } 136 137 /** 138 * @deprecated Since 2.0. Use {@link #valueOf(String)} 139 */ 140 @Deprecated 141 // This is unused. Get rid of it. 142 public static String parseHostname(final String serverName) { 143 if (serverName == null || serverName.length() <= 0) { 144 throw new IllegalArgumentException("Passed hostname is null or empty"); 145 } 146 if (!Character.isLetterOrDigit(serverName.charAt(0))) { 147 throw new IllegalArgumentException("Bad passed hostname, serverName=" + serverName); 148 } 149 int index = serverName.indexOf(SERVERNAME_SEPARATOR); 150 return serverName.substring(0, index); 151 } 152 153 /** 154 * @deprecated Since 2.0. Use {@link #valueOf(String)} 155 */ 156 @Deprecated 157 // This is unused. Get rid of it. 158 public static int parsePort(final String serverName) { 159 String [] split = serverName.split(SERVERNAME_SEPARATOR); 160 return Integer.parseInt(split[1]); 161 } 162 163 /** 164 * @deprecated Since 2.0. Use {@link #valueOf(String)} 165 */ 166 @Deprecated 167 // This is unused. Get rid of it. 168 public static long parseStartcode(final String serverName) { 169 int index = serverName.lastIndexOf(SERVERNAME_SEPARATOR); 170 return Long.parseLong(serverName.substring(index + 1)); 171 } 172 173 /** 174 * Retrieve an instance of ServerName. 175 * Callers should use the equals method to compare returned instances, though we may return 176 * a shared immutable object as an internal optimization. 177 */ 178 public static ServerName valueOf(final String hostname, final int port, final long startcode) { 179 return new ServerName(hostname, port, startcode); 180 } 181 182 /** 183 * Retrieve an instance of ServerName. 184 * Callers should use the equals method to compare returned instances, though we may return 185 * a shared immutable object as an internal optimization. 186 */ 187 public static ServerName valueOf(final String serverName) { 188 return new ServerName(serverName); 189 } 190 191 /** 192 * Retrieve an instance of ServerName. 193 * Callers should use the equals method to compare returned instances, though we may return 194 * a shared immutable object as an internal optimization. 195 */ 196 public static ServerName valueOf(final String hostAndPort, final long startCode) { 197 return new ServerName(hostAndPort, startCode); 198 } 199 200 @Override 201 public String toString() { 202 return getServerName(); 203 } 204 205 /** 206 * @return Return a SHORT version of {@link ServerName#toString()}, one that has the host only, 207 * minus the domain, and the port only -- no start code; the String is for us internally mostly 208 * tying threads to their server. Not for external use. It is lossy and will not work in 209 * in compares, etc. 210 */ 211 public String toShortString() { 212 return Addressing.createHostAndPortStr( 213 getHostNameMinusDomain(this.address.getHostname()), 214 this.address.getPort()); 215 } 216 217 /** 218 * @return {@link #getServerName()} as bytes with a short-sized prefix with 219 * the ServerName#VERSION of this class. 220 */ 221 public synchronized byte [] getVersionedBytes() { 222 if (this.bytes == null) { 223 this.bytes = Bytes.add(VERSION_BYTES, Bytes.toBytes(getServerName())); 224 } 225 return this.bytes; 226 } 227 228 public String getServerName() { 229 return servername; 230 } 231 232 public String getHostname() { 233 return this.address.getHostname(); 234 } 235 236 public String getHostnameLowerCase() { 237 return this.address.getHostname().toLowerCase(Locale.ROOT); 238 } 239 240 public int getPort() { 241 return this.address.getPort(); 242 } 243 244 public long getStartcode() { 245 return startcode; 246 } 247 248 /** 249 * For internal use only. 250 * @param hostName 251 * @param port 252 * @param startcode 253 * @return Server name made of the concatenation of hostname, port and 254 * startcode formatted as <code><hostname> ',' <port> ',' <startcode></code> 255 * @deprecated Since 2.0. Use {@link ServerName#valueOf(String, int, long)} instead. 256 */ 257 @Deprecated 258 // TODO: Make this private in hbase-3.0. 259 static String getServerName(String hostName, int port, long startcode) { 260 final StringBuilder name = new StringBuilder(hostName.length() + 1 + 5 + 1 + 13); 261 name.append(hostName.toLowerCase(Locale.ROOT)); 262 name.append(SERVERNAME_SEPARATOR); 263 name.append(port); 264 name.append(SERVERNAME_SEPARATOR); 265 name.append(startcode); 266 return name.toString(); 267 } 268 269 /** 270 * @param hostAndPort String in form of <hostname> ':' <port> 271 * @param startcode 272 * @return Server name made of the concatenation of hostname, port and 273 * startcode formatted as <code><hostname> ',' <port> ',' <startcode></code> 274 * @deprecated Since 2.0. Use {@link ServerName#valueOf(String, long)} instead. 275 */ 276 @Deprecated 277 public static String getServerName(final String hostAndPort, 278 final long startcode) { 279 int index = hostAndPort.indexOf(":"); 280 if (index <= 0) throw new IllegalArgumentException("Expected <hostname> ':' <port>"); 281 return getServerName(hostAndPort.substring(0, index), 282 Integer.parseInt(hostAndPort.substring(index + 1)), startcode); 283 } 284 285 /** 286 * @return Hostname and port formatted as described at 287 * {@link Addressing#createHostAndPortStr(String, int)} 288 * @deprecated Since 2.0. Use {@link #getAddress()} instead. 289 */ 290 @Deprecated 291 public String getHostAndPort() { 292 return this.address.toString(); 293 } 294 295 public Address getAddress() { 296 return this.address; 297 } 298 299 /** 300 * @param serverName ServerName in form specified by {@link #getServerName()} 301 * @return The server start code parsed from <code>servername</code> 302 * @deprecated Since 2.0. Use instance of ServerName to pull out start code. 303 */ 304 @Deprecated 305 public static long getServerStartcodeFromServerName(final String serverName) { 306 int index = serverName.lastIndexOf(SERVERNAME_SEPARATOR); 307 return Long.parseLong(serverName.substring(index + 1)); 308 } 309 310 /** 311 * Utility method to excise the start code from a server name 312 * @param inServerName full server name 313 * @return server name less its start code 314 * @deprecated Since 2.0. Use {@link #getAddress()} 315 */ 316 @Deprecated 317 public static String getServerNameLessStartCode(String inServerName) { 318 if (inServerName != null && inServerName.length() > 0) { 319 int index = inServerName.lastIndexOf(SERVERNAME_SEPARATOR); 320 if (index > 0) { 321 return inServerName.substring(0, index); 322 } 323 } 324 return inServerName; 325 } 326 327 @Override 328 public int compareTo(ServerName other) { 329 int compare; 330 if (other == null) { 331 return -1; 332 } 333 if (this.getHostname() == null) { 334 if (other.getHostname() != null) { 335 return 1; 336 } 337 } else { 338 if (other.getHostname() == null) { 339 return -1; 340 } 341 compare = this.getHostname().compareToIgnoreCase(other.getHostname()); 342 if (compare != 0) { 343 return compare; 344 } 345 } 346 compare = this.getPort() - other.getPort(); 347 if (compare != 0) { 348 return compare; 349 } 350 return Long.compare(this.getStartcode(), other.getStartcode()); 351 } 352 353 @Override 354 public int hashCode() { 355 return getServerName().hashCode(); 356 } 357 358 @Override 359 public boolean equals(Object o) { 360 if (this == o) return true; 361 if (o == null) return false; 362 if (!(o instanceof ServerName)) return false; 363 return this.compareTo((ServerName)o) == 0; 364 } 365 366 /** 367 * @param left 368 * @param right 369 * @return True if <code>other</code> has same hostname and port. 370 */ 371 public static boolean isSameAddress(final ServerName left, 372 final ServerName right) { 373 // TODO: Make this left.getAddress().equals(right.getAddress()) 374 if (left == null) return false; 375 if (right == null) return false; 376 return left.getHostname().compareToIgnoreCase(right.getHostname()) == 0 && 377 left.getPort() == right.getPort(); 378 } 379 380 /** 381 * Use this method instantiating a {@link ServerName} from bytes 382 * gotten from a call to {@link #getVersionedBytes()}. Will take care of the 383 * case where bytes were written by an earlier version of hbase. 384 * @param versionedBytes Pass bytes gotten from a call to {@link #getVersionedBytes()} 385 * @return A ServerName instance. 386 * @see #getVersionedBytes() 387 */ 388 public static ServerName parseVersionedServerName(final byte [] versionedBytes) { 389 // Version is a short. 390 short version = Bytes.toShort(versionedBytes); 391 if (version == VERSION) { 392 int length = versionedBytes.length - Bytes.SIZEOF_SHORT; 393 return valueOf(Bytes.toString(versionedBytes, Bytes.SIZEOF_SHORT, length)); 394 } 395 // Presume the bytes were written with an old version of hbase and that the 396 // bytes are actually a String of the form "'<hostname>' ':' '<port>'". 397 return valueOf(Bytes.toString(versionedBytes), NON_STARTCODE); 398 } 399 400 /** 401 * @param str Either an instance of {@link ServerName#toString()} or a 402 * "'<hostname>' ':' '<port>'". 403 * @return A ServerName instance. 404 */ 405 public static ServerName parseServerName(final String str) { 406 return SERVERNAME_PATTERN.matcher(str).matches()? valueOf(str) : 407 valueOf(str, NON_STARTCODE); 408 } 409 410 411 /** 412 * @return true if the String follows the pattern of {@link ServerName#toString()}, false 413 * otherwise. 414 */ 415 public static boolean isFullServerName(final String str){ 416 if (str == null ||str.isEmpty()) return false; 417 return SERVERNAME_PATTERN.matcher(str).matches(); 418 } 419}