3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 package org
.apache
.hadoop
.hbase
;
21 import java
.io
.Serializable
;
22 import java
.util
.ArrayList
;
23 import java
.util
.List
;
24 import java
.util
.Locale
;
25 import java
.util
.regex
.Pattern
;
27 import org
.apache
.hadoop
.hbase
.net
.Address
;
28 import org
.apache
.hadoop
.hbase
.util
.Addressing
;
29 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
30 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.collect
.Interner
;
31 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.collect
.Interners
;
32 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.net
.InetAddresses
;
33 import org
.apache
.yetus
.audience
.InterfaceAudience
;
38 * Name of a particular incarnation of an HBase Server.
39 * A {@link ServerName} is used uniquely identifying a server instance in a cluster and is made
40 * of the combination of hostname, port, and startcode. The startcode distinguishes restarted
41 * servers on same hostname and port (startcode is usually timestamp of server startup). The
42 * {@link #toString()} format of ServerName is safe to use in the filesystem and as znode name
43 * up in ZooKeeper. Its format is:
44 * <code><hostname> '{@link #SERVERNAME_SEPARATOR}' <port>
45 * '{@link #SERVERNAME_SEPARATOR}' <startcode></code>.
46 * For example, if hostname is <code>www.example.org</code>, port is <code>1234</code>,
47 * and the startcode for the regionserver is <code>1212121212</code>, then
48 * the {@link #toString()} would be <code>www.example.org,1234,1212121212</code>.
50 * <p>You can obtain a versioned serialized form of this class by calling
51 * {@link #getVersionedBytes()}. To deserialize, call
52 * {@link #parseVersionedServerName(byte[])}.
54 * <p>Use {@link #getAddress()} to obtain the Server hostname + port
55 * (Endpoint/Socket Address).
59 @InterfaceAudience.Public
60 public class ServerName
implements Comparable
<ServerName
>, Serializable
{
61 private static final long serialVersionUID
= 1367463982557264981L;
64 * Version for this class.
65 * Its a short rather than a byte so I can for sure distinguish between this
66 * version of this class and the version previous to this which did not have
69 private static final short VERSION
= 0;
70 static final byte [] VERSION_BYTES
= Bytes
.toBytes(VERSION
);
73 * What to use if no startcode supplied.
75 public static final int NON_STARTCODE
= -1;
78 * This character is used as separator between server hostname, port and
81 public static final String SERVERNAME_SEPARATOR
= ",";
83 public static final Pattern SERVERNAME_PATTERN
=
84 Pattern
.compile("[^" + SERVERNAME_SEPARATOR
+ "]+" +
85 SERVERNAME_SEPARATOR
+ Addressing
.VALID_PORT_REGEX
+
86 SERVERNAME_SEPARATOR
+ Addressing
.VALID_PORT_REGEX
+ "$");
89 * What to use if server name is unknown.
91 public static final String UNKNOWN_SERVERNAME
= "#unknown#";
93 private final String servername
;
94 private final long startcode
;
95 private transient Address address
;
98 * Cached versioned bytes of this ServerName instance.
99 * @see #getVersionedBytes()
101 private byte [] bytes
;
102 public static final List
<ServerName
> EMPTY_SERVER_LIST
= new ArrayList
<>(0);
105 * Intern ServerNames. The Set of ServerNames is mostly-fixed changing slowly as Servers
106 * restart. Rather than create a new instance everytime, try and return existing instance
109 private static final Interner
<ServerName
> INTERN_POOL
= Interners
.newWeakInterner();
111 protected ServerName(final String hostname
, final int port
, final long startcode
) {
112 this(Address
.fromParts(hostname
, port
), startcode
);
115 private ServerName(final Address address
, final long startcode
) {
116 // Use HostAndPort to host port and hostname. Does validation and can do ipv6
117 this.address
= address
;
118 this.startcode
= startcode
;
119 this.servername
= getServerName(this.address
.getHostname(),
120 this.address
.getPort(), startcode
);
123 private ServerName(final String serverName
) {
124 this(parseHostname(serverName
), parsePort(serverName
),
125 parseStartcode(serverName
));
128 private ServerName(final String hostAndPort
, final long startCode
) {
129 this(Address
.fromString(hostAndPort
), startCode
);
134 * @return hostname minus the domain, if there is one (will do pass-through on ip addresses)
135 * @deprecated Since 2.0. This is for internal use only.
138 // Make this private in hbase-3.0.
139 static String
getHostNameMinusDomain(final String hostname
) {
140 if (InetAddresses
.isInetAddress(hostname
)) return hostname
;
141 String
[] parts
= hostname
.split("\\.");
142 if (parts
== null || parts
.length
== 0) return hostname
;
147 * @deprecated Since 2.0. Use {@link #valueOf(String)}
150 // This is unused. Get rid of it.
151 public static String
parseHostname(final String serverName
) {
152 if (serverName
== null || serverName
.length() <= 0) {
153 throw new IllegalArgumentException("Passed hostname is null or empty");
155 if (!Character
.isLetterOrDigit(serverName
.charAt(0))) {
156 throw new IllegalArgumentException("Bad passed hostname, serverName=" + serverName
);
158 int index
= serverName
.indexOf(SERVERNAME_SEPARATOR
);
159 return serverName
.substring(0, index
);
163 * @deprecated Since 2.0. Use {@link #valueOf(String)}
166 // This is unused. Get rid of it.
167 public static int parsePort(final String serverName
) {
168 String
[] split
= serverName
.split(SERVERNAME_SEPARATOR
);
169 return Integer
.parseInt(split
[1]);
173 * @deprecated Since 2.0. Use {@link #valueOf(String)}
176 // This is unused. Get rid of it.
177 public static long parseStartcode(final String serverName
) {
178 int index
= serverName
.lastIndexOf(SERVERNAME_SEPARATOR
);
179 return Long
.parseLong(serverName
.substring(index
+ 1));
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.
187 public static ServerName
valueOf(final String hostname
, final int port
, final long startcode
) {
188 return INTERN_POOL
.intern(new ServerName(hostname
, port
, startcode
));
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.
196 public static ServerName
valueOf(final String serverName
) {
197 return INTERN_POOL
.intern(new ServerName(serverName
));
201 * Retrieve an instance of ServerName.
202 * Callers should use the equals method to compare returned instances, though we may return
203 * a shared immutable object as an internal optimization.
205 public static ServerName
valueOf(final String hostAndPort
, final long startCode
) {
206 return INTERN_POOL
.intern(new ServerName(hostAndPort
, startCode
));
210 public String
toString() {
211 return getServerName();
215 * @return Return a SHORT version of {@link ServerName#toString()}, one that has the host only,
216 * minus the domain, and the port only -- no start code; the String is for us internally mostly
217 * tying threads to their server. Not for external use. It is lossy and will not work in
220 public String
toShortString() {
221 return Addressing
.createHostAndPortStr(
222 getHostNameMinusDomain(this.address
.getHostname()),
223 this.address
.getPort());
227 * @return {@link #getServerName()} as bytes with a short-sized prefix with
228 * the ServerName#VERSION of this class.
230 public synchronized byte [] getVersionedBytes() {
231 if (this.bytes
== null) {
232 this.bytes
= Bytes
.add(VERSION_BYTES
, Bytes
.toBytes(getServerName()));
237 public String
getServerName() {
241 public String
getHostname() {
242 return this.address
.getHostname();
245 public String
getHostnameLowerCase() {
246 return this.address
.getHostname().toLowerCase(Locale
.ROOT
);
249 public int getPort() {
250 return this.address
.getPort();
253 public long getStartcode() {
258 * For internal use only.
262 * @return Server name made of the concatenation of hostname, port and
263 * startcode formatted as <code><hostname> ',' <port> ',' <startcode></code>
264 * @deprecated Since 2.0. Use {@link ServerName#valueOf(String, int, long)} instead.
267 // TODO: Make this private in hbase-3.0.
268 static String
getServerName(String hostName
, int port
, long startcode
) {
269 final StringBuilder name
= new StringBuilder(hostName
.length() + 1 + 5 + 1 + 13);
270 name
.append(hostName
.toLowerCase(Locale
.ROOT
));
271 name
.append(SERVERNAME_SEPARATOR
);
273 name
.append(SERVERNAME_SEPARATOR
);
274 name
.append(startcode
);
275 return name
.toString();
279 * @param hostAndPort String in form of <hostname> ':' <port>
281 * @return Server name made of the concatenation of hostname, port and
282 * startcode formatted as <code><hostname> ',' <port> ',' <startcode></code>
283 * @deprecated Since 2.0. Use {@link ServerName#valueOf(String, long)} instead.
286 public static String
getServerName(final String hostAndPort
,
287 final long startcode
) {
288 int index
= hostAndPort
.indexOf(":");
289 if (index
<= 0) throw new IllegalArgumentException("Expected <hostname> ':' <port>");
290 return getServerName(hostAndPort
.substring(0, index
),
291 Integer
.parseInt(hostAndPort
.substring(index
+ 1)), startcode
);
295 * @return Hostname and port formatted as described at
296 * {@link Addressing#createHostAndPortStr(String, int)}
297 * @deprecated Since 2.0. Use {@link #getAddress()} instead.
300 public String
getHostAndPort() {
301 return this.address
.toString();
304 public Address
getAddress() {
309 * @param serverName ServerName in form specified by {@link #getServerName()}
310 * @return The server start code parsed from <code>servername</code>
311 * @deprecated Since 2.0. Use instance of ServerName to pull out start code.
314 public static long getServerStartcodeFromServerName(final String serverName
) {
315 int index
= serverName
.lastIndexOf(SERVERNAME_SEPARATOR
);
316 return Long
.parseLong(serverName
.substring(index
+ 1));
320 * Utility method to excise the start code from a server name
321 * @param inServerName full server name
322 * @return server name less its start code
323 * @deprecated Since 2.0. Use {@link #getAddress()}
326 public static String
getServerNameLessStartCode(String inServerName
) {
327 if (inServerName
!= null && inServerName
.length() > 0) {
328 int index
= inServerName
.lastIndexOf(SERVERNAME_SEPARATOR
);
330 return inServerName
.substring(0, index
);
337 public int compareTo(ServerName other
) {
342 if (this.getHostname() == null) {
343 if (other
.getHostname() != null) {
347 if (other
.getHostname() == null) {
350 compare
= this.getHostname().compareToIgnoreCase(other
.getHostname());
355 compare
= this.getPort() - other
.getPort();
359 return Long
.compare(this.getStartcode(), other
.getStartcode());
363 public int hashCode() {
364 return getServerName().hashCode();
368 public boolean equals(Object o
) {
369 if (this == o
) return true;
370 if (o
== null) return false;
371 if (!(o
instanceof ServerName
)) return false;
372 return this.compareTo((ServerName
)o
) == 0;
378 * @return True if <code>other</code> has same hostname and port.
380 public static boolean isSameAddress(final ServerName left
,
381 final ServerName right
) {
382 // TODO: Make this left.getAddress().equals(right.getAddress())
383 if (left
== null) return false;
384 if (right
== null) return false;
385 return left
.getHostname().compareToIgnoreCase(right
.getHostname()) == 0 &&
386 left
.getPort() == right
.getPort();
390 * Use this method instantiating a {@link ServerName} from bytes
391 * gotten from a call to {@link #getVersionedBytes()}. Will take care of the
392 * case where bytes were written by an earlier version of hbase.
393 * @param versionedBytes Pass bytes gotten from a call to {@link #getVersionedBytes()}
394 * @return A ServerName instance.
395 * @see #getVersionedBytes()
397 public static ServerName
parseVersionedServerName(final byte [] versionedBytes
) {
398 // Version is a short.
399 short version
= Bytes
.toShort(versionedBytes
);
400 if (version
== VERSION
) {
401 int length
= versionedBytes
.length
- Bytes
.SIZEOF_SHORT
;
402 return valueOf(Bytes
.toString(versionedBytes
, Bytes
.SIZEOF_SHORT
, length
));
404 // Presume the bytes were written with an old version of hbase and that the
405 // bytes are actually a String of the form "'<hostname>' ':' '<port>'".
406 return valueOf(Bytes
.toString(versionedBytes
), NON_STARTCODE
);
410 * @param str Either an instance of {@link ServerName#toString()} or a
411 * "'<hostname>' ':' '<port>'".
412 * @return A ServerName instance.
414 public static ServerName
parseServerName(final String str
) {
415 return SERVERNAME_PATTERN
.matcher(str
).matches()?
valueOf(str
) :
416 valueOf(str
, NON_STARTCODE
);
421 * @return true if the String follows the pattern of {@link ServerName#toString()}, false
424 public static boolean isFullServerName(final String str
){
425 if (str
== null ||str
.isEmpty()) return false;
426 return SERVERNAME_PATTERN
.matcher(str
).matches();