View Javadoc

1   /**
2    * Copyright 2011 The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase;
21  
22  import java.util.Collection;
23  import java.util.regex.Pattern;
24  
25  import org.apache.hadoop.hbase.util.Addressing;
26  import org.apache.hadoop.hbase.util.Bytes;
27  
28  /**
29   * Instance of an HBase ServerName.
30   * A server name is used uniquely identifying a server instance and is made
31   * of the combination of hostname, port, and startcode. The startcode
32   * distingushes restarted servers on same hostname and port (startcode is
33   * usually timestamp of server startup). The {@link #toString()} format of
34   * ServerName is safe to use in the  filesystem and as znode name up in
35   * ZooKeeper.  Its format is:
36   * <code>&lt;hostname> '{@link #SERVERNAME_SEPARATOR}' &lt;port> '{@link #SERVERNAME_SEPARATOR}' &lt;startcode></code>.
37   * For example, if hostname is <code>example.org</code>, port is <code>1234</code>,
38   * and the startcode for the regionserver is <code>1212121212</code>, then
39   * the {@link #toString()} would be <code>example.org,1234,1212121212</code>.
40   * 
41   * <p>You can obtain a versioned serialized form of this class by calling
42   * {@link #getVersionedBytes()}.  To deserialize, call {@link #parseVersionedServerName(byte[])}
43   * 
44   * <p>Immutable.
45   */
46  public class ServerName implements Comparable<ServerName> {
47    /**
48     * Version for this class.
49     * Its a short rather than a byte so I can for sure distinguish between this
50     * version of this class and the version previous to this which did not have
51     * a version.
52     */
53    private static final short VERSION = 0;
54    static final byte [] VERSION_BYTES = Bytes.toBytes(VERSION);
55  
56    /**
57     * What to use if no startcode supplied.
58     */
59    public static final int NON_STARTCODE = -1;
60  
61    /**
62     * This character is used as separator between server hostname, port and
63     * startcode.
64     */
65    public static final String SERVERNAME_SEPARATOR = ",";
66  
67    public static Pattern SERVERNAME_PATTERN =
68      Pattern.compile("[^" + SERVERNAME_SEPARATOR + "]+" +
69        SERVERNAME_SEPARATOR + Addressing.VALID_PORT_REGEX +
70        SERVERNAME_SEPARATOR + Addressing.VALID_PORT_REGEX + "$");
71  
72    /**
73     * What to use if server name is unknown.
74     */
75    public static final String UNKNOWN_SERVERNAME = "#unknown#";
76  
77    private final String servername;
78    private final String hostname;
79    private final int port;
80    private final long startcode;
81  
82    /**
83     * Cached versioned bytes of this ServerName instance.
84     * @see #getVersionedBytes()
85     */
86    private byte [] bytes;
87  
88    public ServerName(final String hostname, final int port, final long startcode) {
89      this.hostname = hostname;
90      this.port = port;
91      this.startcode = startcode;
92      this.servername = getServerName(hostname, port, startcode);
93    }
94  
95    public ServerName(final String serverName) {
96      this(parseHostname(serverName), parsePort(serverName),
97        parseStartcode(serverName));
98    }
99  
100   public ServerName(final String hostAndPort, final long startCode) {
101     this(Addressing.parseHostname(hostAndPort),
102       Addressing.parsePort(hostAndPort), startCode);
103   }
104 
105   public static String parseHostname(final String serverName) {
106     if (serverName == null || serverName.length() <= 0) {
107       throw new IllegalArgumentException("Passed hostname is null or empty");
108     }
109     int index = serverName.indexOf(SERVERNAME_SEPARATOR);
110     return serverName.substring(0, index);
111   }
112 
113   public static int parsePort(final String serverName) {
114     String [] split = serverName.split(SERVERNAME_SEPARATOR);
115     return Integer.parseInt(split[1]);
116   }
117 
118   public static long parseStartcode(final String serverName) {
119     int index = serverName.lastIndexOf(SERVERNAME_SEPARATOR);
120     return Long.parseLong(serverName.substring(index + 1));
121   }
122 
123   @Override
124   public String toString() {
125     return getServerName();
126   }
127 
128   /**
129    * @return {@link #getServerName()} as bytes with a short-sized prefix with
130    * the ServerName#VERSION of this class.
131    */
132   public synchronized byte [] getVersionedBytes() {
133     if (this.bytes == null) {
134       this.bytes = Bytes.add(VERSION_BYTES, Bytes.toBytes(getServerName()));
135     }
136     return this.bytes;
137   }
138 
139   public String getServerName() {
140     return servername;
141   }
142 
143   public String getHostname() {
144     return hostname;
145   }
146 
147   public int getPort() {
148     return port;
149   }
150 
151   public long getStartcode() {
152     return startcode;
153   }
154 
155   /**
156    * @param hostName
157    * @param port
158    * @param startcode
159    * @return Server name made of the concatenation of hostname, port and
160    * startcode formatted as <code>&lt;hostname> ',' &lt;port> ',' &lt;startcode></code>
161    */
162   public static String getServerName(String hostName, int port, long startcode) {
163     final StringBuilder name = new StringBuilder(hostName.length() + 1 + 5 + 1 + 13);
164     name.append(hostName);
165     name.append(SERVERNAME_SEPARATOR);
166     name.append(port);
167     name.append(SERVERNAME_SEPARATOR);
168     name.append(startcode);
169     return name.toString();
170   }
171 
172   /**
173    * @param hostAndPort String in form of &lt;hostname> ':' &lt;port>
174    * @param startcode
175    * @return Server name made of the concatenation of hostname, port and
176    * startcode formatted as <code>&lt;hostname> ',' &lt;port> ',' &lt;startcode></code>
177    */
178   public static String getServerName(final String hostAndPort,
179       final long startcode) {
180     int index = hostAndPort.indexOf(":");
181     if (index <= 0) throw new IllegalArgumentException("Expected <hostname> ':' <port>");
182     return getServerName(hostAndPort.substring(0, index),
183       Integer.parseInt(hostAndPort.substring(index + 1)), startcode);
184   }
185 
186   /**
187    * @return Hostname and port formatted as described at
188    * {@link Addressing#createHostAndPortStr(String, int)}
189    */
190   public String getHostAndPort() {
191     return Addressing.createHostAndPortStr(this.hostname, this.port);
192   }
193 
194   /**
195    * @param serverName ServerName in form specified by {@link #getServerName()}
196    * @return The server start code parsed from <code>servername</code>
197    */
198   public static long getServerStartcodeFromServerName(final String serverName) {
199     int index = serverName.lastIndexOf(SERVERNAME_SEPARATOR);
200     return Long.parseLong(serverName.substring(index + 1));
201   }
202 
203   /**
204    * Utility method to excise the start code from a server name
205    * @param inServerName full server name
206    * @return server name less its start code
207    */
208   public static String getServerNameLessStartCode(String inServerName) {
209     if (inServerName != null && inServerName.length() > 0) {
210       int index = inServerName.lastIndexOf(SERVERNAME_SEPARATOR);
211       if (index > 0) {
212         return inServerName.substring(0, index);
213       }
214     }
215     return inServerName;
216   }
217 
218   @Override
219   public int compareTo(ServerName other) {
220     int compare = this.getHostname().toLowerCase().
221       compareTo(other.getHostname().toLowerCase());
222     if (compare != 0) return compare;
223     compare = this.getPort() - other.getPort();
224     if (compare != 0) return compare;
225     return (int)(this.getStartcode() - other.getStartcode());
226   }
227 
228   @Override
229   public int hashCode() {
230     return getServerName().hashCode();
231   }
232 
233   @Override
234   public boolean equals(Object o) {
235     if (this == o) return true;
236     if (o == null) return false;
237     if (!(o instanceof ServerName)) return false;
238     return this.compareTo((ServerName)o) == 0;
239   }
240 
241 
242   /**
243    * @return ServerName with matching hostname and port.
244    */
245   public static ServerName findServerWithSameHostnamePort(final Collection<ServerName> names,
246       final ServerName serverName) {
247     for (ServerName sn: names) {
248       if (isSameHostnameAndPort(serverName, sn)) return sn;
249     }
250     return null;
251   }
252 
253   /**
254    * @param left
255    * @param right
256    * @return True if <code>other</code> has same hostname and port.
257    */
258   public static boolean isSameHostnameAndPort(final ServerName left,
259       final ServerName right) {
260     if (left == null) return false;
261     if (right == null) return false;
262     return left.getHostname().equals(right.getHostname()) &&
263       left.getPort() == right.getPort();
264   }
265 
266   /**
267    * Use this method instantiating a {@link ServerName} from bytes
268    * gotten from a call to {@link #getVersionedBytes()}.  Will take care of the
269    * case where bytes were written by an earlier version of hbase.
270    * @param versionedBytes Pass bytes gotten from a call to {@link #getVersionedBytes()}
271    * @return A ServerName instance.
272    * @see #getVersionedBytes()
273    */
274   public static ServerName parseVersionedServerName(final byte [] versionedBytes) {
275     // Version is a short.
276     short version = Bytes.toShort(versionedBytes);
277     if (version == VERSION) {
278       int length = versionedBytes.length - Bytes.SIZEOF_SHORT;
279       return new ServerName(Bytes.toString(versionedBytes, Bytes.SIZEOF_SHORT, length));
280     }
281     // Presume the bytes were written with an old version of hbase and that the
282     // bytes are actually a String of the form "'<hostname>' ':' '<port>'".
283     return new ServerName(Bytes.toString(versionedBytes), NON_STARTCODE);
284   }
285 
286   /**
287    * @param str Either an instance of {@link ServerName#toString()} or a
288    * "'<hostname>' ':' '<port>'".
289    * @return A ServerName instance.
290    */
291   public static ServerName parseServerName(final String str) {
292     return SERVERNAME_PATTERN.matcher(str).matches()? new ServerName(str):
293       new ServerName(str, NON_STARTCODE);
294   }
295 }