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