View Javadoc

1   /**
2    *
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
10   *
11   *     http://www.apache.org/licenses/LICENSE-2.0
12   *
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.
18   */
19  package org.apache.hadoop.hbase;
20  
21  import com.google.common.net.InetAddresses;
22  import com.google.protobuf.InvalidProtocolBufferException;
23  
24  import org.apache.hadoop.hbase.classification.InterfaceAudience;
25  import org.apache.hadoop.hbase.classification.InterfaceStability;
26  import org.apache.hadoop.hbase.exceptions.DeserializationException;
27  import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
28  import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
29  import org.apache.hadoop.hbase.util.Addressing;
30  import org.apache.hadoop.hbase.util.Bytes;
31  
32  import java.io.Serializable;
33  import java.util.ArrayList;
34  import java.util.List;
35  import java.util.regex.Pattern;
36  
37  /**
38   * Instance of an HBase ServerName.
39   * A server name is used uniquely identifying a server instance in a cluster and is made
40   * of the combination of hostname, port, and startcode.  The startcode distingushes 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>&lt;hostname&gt; '{@link #SERVERNAME_SEPARATOR}' &lt;port&gt;
45   * '{@link #SERVERNAME_SEPARATOR}' &lt;startcode&gt;</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>.
49   * 
50   * <p>You can obtain a versioned serialized form of this class by calling
51   * {@link #getVersionedBytes()}.  To deserialize, call {@link #parseVersionedServerName(byte[])}
52   * 
53   * <p>Immutable.
54   */
55  @InterfaceAudience.Public
56  @InterfaceStability.Evolving
57  public class ServerName implements Comparable<ServerName>, Serializable {
58    private static final long serialVersionUID = 1367463982557264981L;
59  
60    /**
61     * Version for this class.
62     * Its a short rather than a byte so I can for sure distinguish between this
63     * version of this class and the version previous to this which did not have
64     * a version.
65     */
66    private static final short VERSION = 0;
67    static final byte [] VERSION_BYTES = Bytes.toBytes(VERSION);
68  
69    /**
70     * What to use if no startcode supplied.
71     */
72    public static final int NON_STARTCODE = -1;
73  
74    /**
75     * This character is used as separator between server hostname, port and
76     * startcode.
77     */
78    public static final String SERVERNAME_SEPARATOR = ",";
79  
80    public static final Pattern SERVERNAME_PATTERN =
81      Pattern.compile("[^" + SERVERNAME_SEPARATOR + "]+" +
82        SERVERNAME_SEPARATOR + Addressing.VALID_PORT_REGEX +
83        SERVERNAME_SEPARATOR + Addressing.VALID_PORT_REGEX + "$");
84  
85    /**
86     * What to use if server name is unknown.
87     */
88    public static final String UNKNOWN_SERVERNAME = "#unknown#";
89  
90    private final String servername;
91    private final String hostnameOnly;
92    private final int port;
93    private final long startcode;
94  
95    /**
96     * Cached versioned bytes of this ServerName instance.
97     * @see #getVersionedBytes()
98     */
99    private byte [] bytes;
100   public static final List<ServerName> EMPTY_SERVER_LIST = new ArrayList<ServerName>(0);
101 
102   private ServerName(final String hostname, final int port, final long startcode) {
103     // Drop the domain is there is one; no need of it in a local cluster.  With it, we get long
104     // unwieldy names.
105     this.hostnameOnly = hostname;
106     this.port = port;
107     this.startcode = startcode;
108     this.servername = getServerName(this.hostnameOnly, port, startcode);
109   }
110 
111   /**
112    * @param hostname
113    * @return hostname minus the domain, if there is one (will do pass-through on ip addresses)
114    */
115   static String getHostNameMinusDomain(final String hostname) {
116     if (InetAddresses.isInetAddress(hostname)) return hostname;
117     String [] parts = hostname.split("\\.");
118     if (parts == null || parts.length == 0) return hostname;
119     return parts[0];
120   }
121 
122   private ServerName(final String serverName) {
123     this(parseHostname(serverName), parsePort(serverName),
124       parseStartcode(serverName));
125   }
126 
127   private ServerName(final String hostAndPort, final long startCode) {
128     this(Addressing.parseHostname(hostAndPort),
129       Addressing.parsePort(hostAndPort), startCode);
130   }
131 
132   public static String parseHostname(final String serverName) {
133     if (serverName == null || serverName.length() <= 0) {
134       throw new IllegalArgumentException("Passed hostname is null or empty");
135     }
136     if (!Character.isLetterOrDigit(serverName.charAt(0))) {
137       throw new IllegalArgumentException("Bad passed hostname, serverName=" + serverName);
138     }
139     int index = serverName.indexOf(SERVERNAME_SEPARATOR);
140     return serverName.substring(0, index);
141   }
142 
143   public static int parsePort(final String serverName) {
144     String [] split = serverName.split(SERVERNAME_SEPARATOR);
145     return Integer.parseInt(split[1]);
146   }
147 
148   public static long parseStartcode(final String serverName) {
149     int index = serverName.lastIndexOf(SERVERNAME_SEPARATOR);
150     return Long.parseLong(serverName.substring(index + 1));
151   }
152 
153   /**
154    * Retrieve an instance of ServerName.
155    * Callers should use the equals method to compare returned instances, though we may return
156    * a shared immutable object as an internal optimization.
157    */
158   public static ServerName valueOf(final String hostname, final int port, final long startcode) {
159     return new ServerName(hostname, port, startcode);
160   }
161 
162   /**
163    * Retrieve an instance of ServerName.
164    * Callers should use the equals method to compare returned instances, though we may return
165    * a shared immutable object as an internal optimization.
166    */
167   public static ServerName valueOf(final String serverName) {
168     return new ServerName(serverName);
169   }
170 
171   /**
172    * Retrieve an instance of ServerName.
173    * Callers should use the equals method to compare returned instances, though we may return
174    * a shared immutable object as an internal optimization.
175    */
176   public static ServerName valueOf(final String hostAndPort, final long startCode) {
177     return new ServerName(hostAndPort, startCode);
178   }
179 
180   @Override
181   public String toString() {
182     return getServerName();
183   }
184 
185   /**
186    * @return Return a SHORT version of {@link ServerName#toString()}, one that has the host only,
187    * minus the domain, and the port only -- no start code; the String is for us internally mostly
188    * tying threads to their server.  Not for external use.  It is lossy and will not work in
189    * in compares, etc.
190    */
191   public String toShortString() {
192     return Addressing.createHostAndPortStr(getHostNameMinusDomain(this.hostnameOnly), this.port);
193   }
194 
195   /**
196    * @return {@link #getServerName()} as bytes with a short-sized prefix with
197    * the ServerName#VERSION of this class.
198    */
199   public synchronized byte [] getVersionedBytes() {
200     if (this.bytes == null) {
201       this.bytes = Bytes.add(VERSION_BYTES, Bytes.toBytes(getServerName()));
202     }
203     return this.bytes;
204   }
205 
206   public String getServerName() {
207     return servername;
208   }
209 
210   public String getHostname() {
211     return hostnameOnly;
212   }
213 
214   public int getPort() {
215     return port;
216   }
217 
218   public long getStartcode() {
219     return startcode;
220   }
221 
222   /**
223    * For internal use only.
224    * @param hostName
225    * @param port
226    * @param startcode
227    * @return Server name made of the concatenation of hostname, port and
228    * startcode formatted as <code>&lt;hostname&gt; ',' &lt;port&gt; ',' &lt;startcode&gt;</code>
229    */
230   static String getServerName(String hostName, int port, long startcode) {
231     final StringBuilder name = new StringBuilder(hostName.length() + 1 + 5 + 1 + 13);
232     name.append(hostName.toLowerCase());
233     name.append(SERVERNAME_SEPARATOR);
234     name.append(port);
235     name.append(SERVERNAME_SEPARATOR);
236     name.append(startcode);
237     return name.toString();
238   }
239 
240   /**
241    * @param hostAndPort String in form of &lt;hostname&gt; ':' &lt;port&gt;
242    * @param startcode
243    * @return Server name made of the concatenation of hostname, port and
244    * startcode formatted as <code>&lt;hostname&gt; ',' &lt;port&gt; ',' &lt;startcode&gt;</code>
245    */
246   public static String getServerName(final String hostAndPort,
247       final long startcode) {
248     int index = hostAndPort.indexOf(":");
249     if (index <= 0) throw new IllegalArgumentException("Expected <hostname> ':' <port>");
250     return getServerName(hostAndPort.substring(0, index),
251       Integer.parseInt(hostAndPort.substring(index + 1)), startcode);
252   }
253 
254   /**
255    * @return Hostname and port formatted as described at
256    * {@link Addressing#createHostAndPortStr(String, int)}
257    */
258   public String getHostAndPort() {
259     return Addressing.createHostAndPortStr(this.hostnameOnly, this.port);
260   }
261 
262   /**
263    * @param serverName ServerName in form specified by {@link #getServerName()}
264    * @return The server start code parsed from <code>servername</code>
265    */
266   public static long getServerStartcodeFromServerName(final String serverName) {
267     int index = serverName.lastIndexOf(SERVERNAME_SEPARATOR);
268     return Long.parseLong(serverName.substring(index + 1));
269   }
270 
271   /**
272    * Utility method to excise the start code from a server name
273    * @param inServerName full server name
274    * @return server name less its start code
275    */
276   public static String getServerNameLessStartCode(String inServerName) {
277     if (inServerName != null && inServerName.length() > 0) {
278       int index = inServerName.lastIndexOf(SERVERNAME_SEPARATOR);
279       if (index > 0) {
280         return inServerName.substring(0, index);
281       }
282     }
283     return inServerName;
284   }
285 
286   @Override
287   public int compareTo(ServerName other) {
288     int compare = this.getHostname().compareToIgnoreCase(other.getHostname());
289     if (compare != 0) return compare;
290     compare = this.getPort() - other.getPort();
291     if (compare != 0) return compare;
292 
293     return Long.compare(this.getStartcode(), other.getStartcode());
294   }
295 
296   @Override
297   public int hashCode() {
298     return getServerName().hashCode();
299   }
300 
301   @Override
302   public boolean equals(Object o) {
303     if (this == o) return true;
304     if (o == null) return false;
305     if (!(o instanceof ServerName)) return false;
306     return this.compareTo((ServerName)o) == 0;
307   }
308 
309   /**
310    * @param left
311    * @param right
312    * @return True if <code>other</code> has same hostname and port.
313    */
314   public static boolean isSameHostnameAndPort(final ServerName left,
315       final ServerName right) {
316     if (left == null) return false;
317     if (right == null) return false;
318     return left.getHostname().compareToIgnoreCase(right.getHostname()) == 0 &&
319       left.getPort() == right.getPort();
320   }
321 
322   /**
323    * Use this method instantiating a {@link ServerName} from bytes
324    * gotten from a call to {@link #getVersionedBytes()}.  Will take care of the
325    * case where bytes were written by an earlier version of hbase.
326    * @param versionedBytes Pass bytes gotten from a call to {@link #getVersionedBytes()}
327    * @return A ServerName instance.
328    * @see #getVersionedBytes()
329    */
330   public static ServerName parseVersionedServerName(final byte [] versionedBytes) {
331     // Version is a short.
332     short version = Bytes.toShort(versionedBytes);
333     if (version == VERSION) {
334       int length = versionedBytes.length - Bytes.SIZEOF_SHORT;
335       return valueOf(Bytes.toString(versionedBytes, Bytes.SIZEOF_SHORT, length));
336     }
337     // Presume the bytes were written with an old version of hbase and that the
338     // bytes are actually a String of the form "'<hostname>' ':' '<port>'".
339     return valueOf(Bytes.toString(versionedBytes), NON_STARTCODE);
340   }
341 
342   /**
343    * @param str Either an instance of {@link ServerName#toString()} or a
344    * "'&lt;hostname&gt;' ':' '&lt;port&gt;'".
345    * @return A ServerName instance.
346    */
347   public static ServerName parseServerName(final String str) {
348     return SERVERNAME_PATTERN.matcher(str).matches()? valueOf(str) :
349         valueOf(str, NON_STARTCODE);
350   }
351 
352 
353   /**
354    * @return true if the String follows the pattern of {@link ServerName#toString()}, false
355    *  otherwise.
356    */
357   public static boolean isFullServerName(final String str){
358     if (str == null ||str.isEmpty()) return false;
359     return SERVERNAME_PATTERN.matcher(str).matches();
360   }
361 
362   /**
363    * Get a ServerName from the passed in data bytes.
364    * @param data Data with a serialize server name in it; can handle the old style
365    * servername where servername was host and port.  Works too with data that
366    * begins w/ the pb 'PBUF' magic and that is then followed by a protobuf that
367    * has a serialized {@link ServerName} in it.
368    * @return Returns null if <code>data</code> is null else converts passed data
369    * to a ServerName instance.
370    * @throws DeserializationException 
371    */
372   public static ServerName parseFrom(final byte [] data) throws DeserializationException {
373     if (data == null || data.length <= 0) return null;
374     if (ProtobufUtil.isPBMagicPrefix(data)) {
375       int prefixLen = ProtobufUtil.lengthOfPBMagic();
376       try {
377         ZooKeeperProtos.Master rss =
378           ZooKeeperProtos.Master.PARSER.parseFrom(data, prefixLen, data.length - prefixLen);
379         org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.ServerName sn = rss.getMaster();
380         return valueOf(sn.getHostName(), sn.getPort(), sn.getStartCode());
381       } catch (InvalidProtocolBufferException e) {
382         // A failed parse of the znode is pretty catastrophic. Rather than loop
383         // retrying hoping the bad bytes will changes, and rather than change
384         // the signature on this method to add an IOE which will send ripples all
385         // over the code base, throw a RuntimeException.  This should "never" happen.
386         // Fail fast if it does.
387         throw new DeserializationException(e);
388       }
389     }
390     // The str returned could be old style -- pre hbase-1502 -- which was
391     // hostname and port seperated by a colon rather than hostname, port and
392     // startcode delimited by a ','.
393     String str = Bytes.toString(data);
394     int index = str.indexOf(ServerName.SERVERNAME_SEPARATOR);
395     if (index != -1) {
396       // Presume its ServerName serialized with versioned bytes.
397       return ServerName.parseVersionedServerName(data);
398     }
399     // Presume it a hostname:port format.
400     String hostname = Addressing.parseHostname(str);
401     int port = Addressing.parsePort(str);
402     return valueOf(hostname, port, -1L);
403   }
404 }