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    private final String servername;
73    private final String hostname;
74    private final int port;
75    private final long startcode;
76  
77    /**
78     * Cached versioned bytes of this ServerName instance.
79     * @see #getVersionedBytes()
80     */
81    private byte [] bytes;
82  
83    public ServerName(final String hostname, final int port, final long startcode) {
84      this.hostname = hostname;
85      this.port = port;
86      this.startcode = startcode;
87      this.servername = getServerName(hostname, port, startcode);
88    }
89  
90    public ServerName(final String serverName) {
91      this(parseHostname(serverName), parsePort(serverName),
92        parseStartcode(serverName));
93    }
94  
95    public ServerName(final String hostAndPort, final long startCode) {
96      this(Addressing.parseHostname(hostAndPort),
97        Addressing.parsePort(hostAndPort), startCode);
98    }
99  
100   public static String parseHostname(final String serverName) {
101     if (serverName == null || serverName.length() <= 0) {
102       throw new IllegalArgumentException("Passed hostname is null or empty");
103     }
104     int index = serverName.indexOf(SERVERNAME_SEPARATOR);
105     return serverName.substring(0, index);
106   }
107 
108   public static int parsePort(final String serverName) {
109     String [] split = serverName.split(SERVERNAME_SEPARATOR);
110     return Integer.parseInt(split[1]);
111   }
112 
113   public static long parseStartcode(final String serverName) {
114     int index = serverName.lastIndexOf(SERVERNAME_SEPARATOR);
115     return Long.parseLong(serverName.substring(index + 1));
116   }
117 
118   @Override
119   public String toString() {
120     return getServerName();
121   }
122 
123   /**
124    * @return {@link #getServerName()} as bytes with a short-sized prefix with
125    * the ServerName#VERSION of this class.
126    */
127   public synchronized byte [] getVersionedBytes() {
128     if (this.bytes == null) {
129       this.bytes = Bytes.add(VERSION_BYTES, Bytes.toBytes(getServerName()));
130     }
131     return this.bytes;
132   }
133 
134   public String getServerName() {
135     return servername;
136   }
137 
138   public String getHostname() {
139     return hostname;
140   }
141 
142   public int getPort() {
143     return port;
144   }
145 
146   public long getStartcode() {
147     return startcode;
148   }
149 
150   /**
151    * @param hostName
152    * @param port
153    * @param startcode
154    * @return Server name made of the concatenation of hostname, port and
155    * startcode formatted as <code>&lt;hostname> ',' &lt;port> ',' &lt;startcode></code>
156    */
157   public static String getServerName(String hostName, int port, long startcode) {
158     final StringBuilder name = new StringBuilder(hostName.length() + 1 + 5 + 1 + 13);
159     name.append(hostName);
160     name.append(SERVERNAME_SEPARATOR);
161     name.append(port);
162     name.append(SERVERNAME_SEPARATOR);
163     name.append(startcode);
164     return name.toString();
165   }
166 
167   /**
168    * @param hostAndPort String in form of &lt;hostname> ':' &lt;port>
169    * @param startcode
170    * @return Server name made of the concatenation of hostname, port and
171    * startcode formatted as <code>&lt;hostname> ',' &lt;port> ',' &lt;startcode></code>
172    */
173   public static String getServerName(final String hostAndPort,
174       final long startcode) {
175     int index = hostAndPort.indexOf(":");
176     if (index <= 0) throw new IllegalArgumentException("Expected <hostname> ':' <port>");
177     return getServerName(hostAndPort.substring(0, index),
178       Integer.parseInt(hostAndPort.substring(index + 1)), startcode);
179   }
180 
181   /**
182    * @return Hostname and port formatted as described at
183    * {@link Addressing#createHostAndPortStr(String, int)}
184    */
185   public String getHostAndPort() {
186     return Addressing.createHostAndPortStr(this.hostname, this.port);
187   }
188 
189   /**
190    * @param serverName ServerName in form specified by {@link #getServerName()}
191    * @return The server start code parsed from <code>servername</code>
192    */
193   public static long getServerStartcodeFromServerName(final String serverName) {
194     int index = serverName.lastIndexOf(SERVERNAME_SEPARATOR);
195     return Long.parseLong(serverName.substring(index + 1));
196   }
197 
198   /**
199    * Utility method to excise the start code from a server name
200    * @param inServerName full server name
201    * @return server name less its start code
202    */
203   public static String getServerNameLessStartCode(String inServerName) {
204     if (inServerName != null && inServerName.length() > 0) {
205       int index = inServerName.lastIndexOf(SERVERNAME_SEPARATOR);
206       if (index > 0) {
207         return inServerName.substring(0, index);
208       }
209     }
210     return inServerName;
211   }
212 
213   @Override
214   public int compareTo(ServerName other) {
215     int compare = this.getHostname().toLowerCase().
216       compareTo(other.getHostname().toLowerCase());
217     if (compare != 0) return compare;
218     compare = this.getPort() - other.getPort();
219     if (compare != 0) return compare;
220     return (int)(this.getStartcode() - other.getStartcode());
221   }
222 
223   @Override
224   public int hashCode() {
225     return getServerName().hashCode();
226   }
227 
228   @Override
229   public boolean equals(Object o) {
230     if (this == o) return true;
231     if (o == null) return false;
232     if (!(o instanceof ServerName)) return false;
233     return this.compareTo((ServerName)o) == 0;
234   }
235 
236 
237   /**
238    * @return ServerName with matching hostname and port.
239    */
240   public static ServerName findServerWithSameHostnamePort(final Collection<ServerName> names,
241       final ServerName serverName) {
242     for (ServerName sn: names) {
243       if (isSameHostnameAndPort(serverName, sn)) return sn;
244     }
245     return null;
246   }
247 
248   /**
249    * @param left
250    * @param right
251    * @return True if <code>other</code> has same hostname and port.
252    */
253   public static boolean isSameHostnameAndPort(final ServerName left,
254       final ServerName right) {
255     if (left == null) return false;
256     if (right == null) return false;
257     return left.getHostname().equals(right.getHostname()) &&
258       left.getPort() == right.getPort();
259   }
260 
261   /**
262    * Use this method instantiating a {@link ServerName} from bytes
263    * gotten from a call to {@link #getVersionedBytes()}.  Will take care of the
264    * case where bytes were written by an earlier version of hbase.
265    * @param versionedBytes Pass bytes gotten from a call to {@link #getVersionedBytes()}
266    * @return A ServerName instance.
267    * @see #getVersionedBytes()
268    */
269   public static ServerName parseVersionedServerName(final byte [] versionedBytes) {
270     // Version is a short.
271     short version = Bytes.toShort(versionedBytes);
272     if (version == VERSION) {
273       int length = versionedBytes.length - Bytes.SIZEOF_SHORT;
274       return new ServerName(Bytes.toString(versionedBytes, Bytes.SIZEOF_SHORT, length));
275     }
276     // Presume the bytes were written with an old version of hbase and that the
277     // bytes are actually a String of the form "'<hostname>' ':' '<port>'".
278     return new ServerName(Bytes.toString(versionedBytes), NON_STARTCODE);
279   }
280 
281   /**
282    * @param str Either an instance of {@link ServerName#toString()} or a
283    * "'<hostname>' ':' '<port>'".
284    * @return A ServerName instance.
285    */
286   public static ServerName parseServerName(final String str) {
287     return SERVERNAME_PATTERN.matcher(str).matches()? new ServerName(str):
288       new ServerName(str, NON_STARTCODE);
289   }
290 }