2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 package org
.apache
.hadoop
.hbase
;
20 import java
.io
.Serializable
;
21 import java
.util
.ArrayList
;
22 import java
.util
.List
;
23 import java
.util
.Locale
;
24 import java
.util
.regex
.Pattern
;
25 import org
.apache
.hadoop
.hbase
.net
.Address
;
26 import org
.apache
.hadoop
.hbase
.util
.Addressing
;
27 import org
.apache
.hadoop
.hbase
.util
.Bytes
;
28 import org
.apache
.yetus
.audience
.InterfaceAudience
;
29 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.collect
.Interner
;
30 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.collect
.Interners
;
31 import org
.apache
.hbase
.thirdparty
.com
.google
.common
.net
.InetAddresses
;
34 * Name of a particular incarnation of an HBase Server.
35 * A {@link ServerName} is used uniquely identifying a server instance in a cluster and is made
36 * of the combination of hostname, port, and startcode. The startcode distinguishes restarted
37 * servers on same hostname and port (startcode is usually timestamp of server startup). The
38 * {@link #toString()} format of ServerName is safe to use in the filesystem and as znode name
39 * up in ZooKeeper. Its format is:
40 * <code><hostname> '{@link #SERVERNAME_SEPARATOR}' <port>
41 * '{@link #SERVERNAME_SEPARATOR}' <startcode></code>.
42 * For example, if hostname is <code>www.example.org</code>, port is <code>1234</code>,
43 * and the startcode for the regionserver is <code>1212121212</code>, then
44 * the {@link #toString()} would be <code>www.example.org,1234,1212121212</code>.
46 * <p>You can obtain a versioned serialized form of this class by calling
47 * {@link #getVersionedBytes()}. To deserialize, call
48 * {@link #parseVersionedServerName(byte[])}.
50 * <p>Use {@link #getAddress()} to obtain the Server hostname + port
51 * (Endpoint/Socket Address).
55 @InterfaceAudience.Public
56 public class ServerName
implements Comparable
<ServerName
>, Serializable
{
57 private static final long serialVersionUID
= 1367463982557264981L;
60 * Version for this class.
61 * Its a short rather than a byte so I can for sure distinguish between this
62 * version of this class and the version previous to this which did not have
65 private static final short VERSION
= 0;
66 static final byte [] VERSION_BYTES
= Bytes
.toBytes(VERSION
);
69 * What to use if no startcode supplied.
71 public static final int NON_STARTCODE
= -1;
74 * This character is used as separator between server hostname, port and
77 public static final String SERVERNAME_SEPARATOR
= ",";
79 public static final Pattern SERVERNAME_PATTERN
=
80 Pattern
.compile("[^" + SERVERNAME_SEPARATOR
+ "]+" +
81 SERVERNAME_SEPARATOR
+ Addressing
.VALID_PORT_REGEX
+
82 SERVERNAME_SEPARATOR
+ Addressing
.VALID_PORT_REGEX
+ "$");
85 * What to use if server name is unknown.
87 public static final String UNKNOWN_SERVERNAME
= "#unknown#";
89 private final String servername
;
90 private final long startcode
;
91 private transient Address address
;
94 * Cached versioned bytes of this ServerName instance.
95 * @see #getVersionedBytes()
97 private byte [] bytes
;
98 public static final List
<ServerName
> EMPTY_SERVER_LIST
= new ArrayList
<>(0);
101 * Intern ServerNames. The Set of ServerNames is mostly-fixed changing slowly as Servers
102 * restart. Rather than create a new instance everytime, try and return existing instance
105 private static final Interner
<ServerName
> INTERN_POOL
= Interners
.newWeakInterner();
107 protected ServerName(final String hostname
, final int port
, final long startcode
) {
108 this(Address
.fromParts(hostname
, port
), startcode
);
111 private ServerName(final Address address
, final long startcode
) {
112 // Use HostAndPort to host port and hostname. Does validation and can do ipv6
113 this.address
= address
;
114 this.startcode
= startcode
;
115 this.servername
= getServerName(this.address
.getHostname(),
116 this.address
.getPort(), startcode
);
119 private ServerName(final String hostAndPort
, final long startCode
) {
120 this(Address
.fromString(hostAndPort
), startCode
);
124 * @param hostname the hostname string to get the actual hostname from
125 * @return hostname minus the domain, if there is one (will do pass-through on ip addresses)
127 private static String
getHostNameMinusDomain(final String hostname
) {
128 if (InetAddresses
.isInetAddress(hostname
)) {
131 String
[] parts
= hostname
.split("\\.");
132 if (parts
.length
== 0) {
140 * Retrieve an instance of ServerName.
141 * Callers should use the equals method to compare returned instances, though we may return
142 * a shared immutable object as an internal optimization.
144 public static ServerName
valueOf(final String hostname
, final int port
, final long startcode
) {
145 return INTERN_POOL
.intern(new ServerName(hostname
, port
, startcode
));
149 * Retrieve an instance of ServerName.
150 * Callers should use the equals method to compare returned instances, though we may return
151 * a shared immutable object as an internal optimization.
153 public static ServerName
valueOf(final String serverName
) {
154 final String hostname
= serverName
.substring(0, serverName
.indexOf(SERVERNAME_SEPARATOR
));
155 final int port
= Integer
.parseInt(serverName
.split(SERVERNAME_SEPARATOR
)[1]);
156 final long statuscode
=
157 Long
.parseLong(serverName
.substring(serverName
.lastIndexOf(SERVERNAME_SEPARATOR
) + 1));
158 return INTERN_POOL
.intern(new ServerName(hostname
, port
, statuscode
));
162 * Retrieve an instance of ServerName.
163 * Callers should use the equals method to compare returned instances, though we may return
164 * a shared immutable object as an internal optimization.
166 public static ServerName
valueOf(final String hostAndPort
, final long startCode
) {
167 return INTERN_POOL
.intern(new ServerName(hostAndPort
, startCode
));
171 * Retrieve an instance of {@link ServerName}. Callers should use the {@link #equals(Object)}
172 * method to compare returned instances, though we may return a shared immutable object as an
173 * internal optimization.
175 * @param address the {@link Address} to use for getting the {@link ServerName}
176 * @param startcode the startcode to use for getting the {@link ServerName}
177 * @return the constructed {@link ServerName}
178 * @see #valueOf(String, int, long)
180 public static ServerName
valueOf(final Address address
, final long startcode
) {
181 return valueOf(address
.getHostname(), address
.getPort(), startcode
);
185 public String
toString() {
186 return getServerName();
190 * @return Return a SHORT version of {@link #toString()}, one that has the host only,
191 * minus the domain, and the port only -- no start code; the String is for us internally mostly
192 * tying threads to their server. Not for external use. It is lossy and will not work in
195 public String
toShortString() {
196 return Addressing
.createHostAndPortStr(
197 getHostNameMinusDomain(this.address
.getHostname()),
198 this.address
.getPort());
202 * @return {@link #getServerName()} as bytes with a short-sized prefix with
203 * the {@link #VERSION} of this class.
205 public synchronized byte [] getVersionedBytes() {
206 if (this.bytes
== null) {
207 this.bytes
= Bytes
.add(VERSION_BYTES
, Bytes
.toBytes(getServerName()));
212 public String
getServerName() {
216 public String
getHostname() {
217 return this.address
.getHostname();
220 public String
getHostnameLowerCase() {
221 return this.address
.getHostname().toLowerCase(Locale
.ROOT
);
224 public int getPort() {
225 return this.address
.getPort();
228 public long getStartcode() {
233 * For internal use only.
234 * @param hostName the name of the host to use
235 * @param port the port on the host to use
236 * @param startcode the startcode to use for formatting
237 * @return Server name made of the concatenation of hostname, port and
238 * startcode formatted as <code><hostname> ',' <port> ',' <startcode></code>
240 private static String
getServerName(String hostName
, int port
, long startcode
) {
241 return hostName
.toLowerCase(Locale
.ROOT
) + SERVERNAME_SEPARATOR
+ port
242 + SERVERNAME_SEPARATOR
+ startcode
;
245 public Address
getAddress() {
250 public int compareTo(ServerName other
) {
255 if (this.getHostname() == null) {
256 if (other
.getHostname() != null) {
260 if (other
.getHostname() == null) {
263 compare
= this.getHostname().compareToIgnoreCase(other
.getHostname());
268 compare
= this.getPort() - other
.getPort();
272 return Long
.compare(this.getStartcode(), other
.getStartcode());
276 public int hashCode() {
277 return getServerName().hashCode();
281 public boolean equals(Object o
) {
288 if (!(o
instanceof ServerName
)) {
291 return this.compareTo((ServerName
)o
) == 0;
295 * @param left the first server address to compare
296 * @param right the second server address to compare
297 * @return {@code true} if {@code left} and {@code right} have the same hostname and port.
299 public static boolean isSameAddress(final ServerName left
, final ServerName right
) {
300 return left
.getAddress().equals(right
.getAddress());
304 * Use this method instantiating a {@link ServerName} from bytes
305 * gotten from a call to {@link #getVersionedBytes()}. Will take care of the
306 * case where bytes were written by an earlier version of hbase.
307 * @param versionedBytes Pass bytes gotten from a call to {@link #getVersionedBytes()}
308 * @return A ServerName instance.
309 * @see #getVersionedBytes()
311 public static ServerName
parseVersionedServerName(final byte [] versionedBytes
) {
312 // Version is a short.
313 short version
= Bytes
.toShort(versionedBytes
);
314 if (version
== VERSION
) {
315 int length
= versionedBytes
.length
- Bytes
.SIZEOF_SHORT
;
316 return valueOf(Bytes
.toString(versionedBytes
, Bytes
.SIZEOF_SHORT
, length
));
318 // Presume the bytes were written with an old version of hbase and that the
319 // bytes are actually a String of the form "'<hostname>' ':' '<port>'".
320 return valueOf(Bytes
.toString(versionedBytes
), NON_STARTCODE
);
324 * @param str Either an instance of {@link #toString()} or a
325 * "'<hostname>' ':' '<port>'".
326 * @return A ServerName instance.
328 public static ServerName
parseServerName(final String str
) {
329 return SERVERNAME_PATTERN
.matcher(str
).matches()?
valueOf(str
) :
330 valueOf(str
, NON_STARTCODE
);
334 * @return true if the String follows the pattern of {@link #toString()}, false
337 public static boolean isFullServerName(final String str
){
338 if (str
== null ||str
.isEmpty()) {
341 return SERVERNAME_PATTERN
.matcher(str
).matches();