1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.regex.Pattern;
29
30 import org.apache.hadoop.hbase.classification.InterfaceAudience;
31 import org.apache.hadoop.hbase.classification.InterfaceStability;
32 import org.apache.hadoop.hbase.exceptions.DeserializationException;
33 import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
34 import org.apache.hadoop.hbase.protobuf.generated.ZooKeeperProtos;
35 import org.apache.hadoop.hbase.util.Addressing;
36 import org.apache.hadoop.hbase.util.Bytes;
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
62
63
64
65
66 private static final short VERSION = 0;
67 static final byte [] VERSION_BYTES = Bytes.toBytes(VERSION);
68
69
70
71
72 public static final int NON_STARTCODE = -1;
73
74
75
76
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
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 private transient HostAndPort hostAndPort;
95
96
97
98
99
100 private byte [] bytes;
101 public static final List<ServerName> EMPTY_SERVER_LIST = new ArrayList<ServerName>(0);
102
103 private ServerName(final String hostname, final int port, final long startcode) {
104
105
106 this.hostnameOnly = hostname;
107 this.port = port;
108 this.startcode = startcode;
109 this.servername = getServerName(hostname, port, startcode);
110 }
111
112
113
114
115
116 static String getHostNameMinusDomain(final String hostname) {
117 if (InetAddresses.isInetAddress(hostname)) return hostname;
118 String [] parts = hostname.split("\\.");
119 if (parts == null || parts.length == 0) return hostname;
120 return parts[0];
121 }
122
123 private ServerName(final String serverName) {
124 this(parseHostname(serverName), parsePort(serverName),
125 parseStartcode(serverName));
126 }
127
128 private ServerName(final String hostAndPort, final long startCode) {
129 this(Addressing.parseHostname(hostAndPort),
130 Addressing.parsePort(hostAndPort), startCode);
131 }
132
133 public static String parseHostname(final String serverName) {
134 if (serverName == null || serverName.length() <= 0) {
135 throw new IllegalArgumentException("Passed hostname is null or empty");
136 }
137 if (!Character.isLetterOrDigit(serverName.charAt(0))) {
138 throw new IllegalArgumentException("Bad passed hostname, serverName=" + serverName);
139 }
140 int index = serverName.indexOf(SERVERNAME_SEPARATOR);
141 if (index < 0) {
142 return serverName;
143 }
144 return serverName.substring(0, index);
145 }
146
147 public static int parsePort(final String serverName) {
148 String [] split = serverName.split(SERVERNAME_SEPARATOR);
149 return Integer.parseInt(split[1]);
150 }
151
152 public static long parseStartcode(final String serverName) {
153 int index = serverName.lastIndexOf(SERVERNAME_SEPARATOR);
154 return Long.parseLong(serverName.substring(index + 1));
155 }
156
157
158
159
160
161
162 public static ServerName valueOf(final String hostname, final int port, final long startcode) {
163 return new ServerName(hostname, port, startcode);
164 }
165
166
167
168
169
170
171 public static ServerName valueOf(final String serverName) {
172 return new ServerName(serverName);
173 }
174
175
176
177
178
179
180 public static ServerName valueOf(final String hostAndPort, final long startCode) {
181 return new ServerName(hostAndPort, startCode);
182 }
183
184 @Override
185 public String toString() {
186 return getServerName();
187 }
188
189
190
191
192
193
194
195 public String toShortString() {
196 return Addressing.createHostAndPortStr(
197 getHostNameMinusDomain(hostnameOnly), port);
198 }
199
200
201
202
203
204 public synchronized byte [] getVersionedBytes() {
205 if (this.bytes == null) {
206 this.bytes = Bytes.add(VERSION_BYTES, Bytes.toBytes(getServerName()));
207 }
208 return this.bytes;
209 }
210
211 public String getServerName() {
212 return servername;
213 }
214
215 public String getHostname() {
216 return hostnameOnly;
217 }
218
219 public int getPort() {
220 return port;
221 }
222
223 public long getStartcode() {
224 return startcode;
225 }
226
227
228
229
230
231
232
233
234
235 static String getServerName(String hostName, int port, long startcode) {
236 final StringBuilder name = new StringBuilder(hostName.length() + 1 + 5 + 1 + 13);
237 name.append(hostName.toLowerCase());
238 name.append(SERVERNAME_SEPARATOR);
239 name.append(port);
240 name.append(SERVERNAME_SEPARATOR);
241 name.append(startcode);
242 return name.toString();
243 }
244
245
246
247
248
249
250
251 public static String getServerName(final String hostAndPort,
252 final long startcode) {
253 int index = hostAndPort.indexOf(":");
254 if (index <= 0) throw new IllegalArgumentException("Expected <hostname> ':' <port>");
255 return getServerName(hostAndPort.substring(0, index),
256 Integer.parseInt(hostAndPort.substring(index + 1)), startcode);
257 }
258
259
260
261
262
263 public String getHostAndPort() {
264 return Addressing.createHostAndPortStr(hostnameOnly, port);
265 }
266
267 public HostAndPort getHostPort() {
268 if (hostAndPort == null) {
269 hostAndPort = HostAndPort.fromParts(hostnameOnly, port);
270 }
271 return hostAndPort;
272 }
273
274
275
276
277
278 public static long getServerStartcodeFromServerName(final String serverName) {
279 int index = serverName.lastIndexOf(SERVERNAME_SEPARATOR);
280 return Long.parseLong(serverName.substring(index + 1));
281 }
282
283
284
285
286
287
288 public static String getServerNameLessStartCode(String inServerName) {
289 if (inServerName != null && inServerName.length() > 0) {
290 int index = inServerName.lastIndexOf(SERVERNAME_SEPARATOR);
291 if (index > 0) {
292 return inServerName.substring(0, index);
293 }
294 }
295 return inServerName;
296 }
297
298 @Override
299 public int compareTo(ServerName other) {
300 int compare = this.getHostname().compareToIgnoreCase(other.getHostname());
301 if (compare != 0) return compare;
302 compare = this.getPort() - other.getPort();
303 if (compare != 0) return compare;
304 return (int)(this.getStartcode() - other.getStartcode());
305 }
306
307 @Override
308 public int hashCode() {
309 return getServerName().hashCode();
310 }
311
312 @Override
313 public boolean equals(Object o) {
314 if (this == o) return true;
315 if (o == null) return false;
316 if (!(o instanceof ServerName)) return false;
317 return this.compareTo((ServerName)o) == 0;
318 }
319
320
321
322
323
324
325 public static boolean isSameHostnameAndPort(final ServerName left,
326 final ServerName right) {
327 if (left == null) return false;
328 if (right == null) return false;
329 return left.getHostname().compareToIgnoreCase(right.getHostname()) == 0 &&
330 left.getPort() == right.getPort();
331 }
332
333
334
335
336
337
338
339
340
341 public static ServerName parseVersionedServerName(final byte [] versionedBytes) {
342
343 short version = Bytes.toShort(versionedBytes);
344 if (version == VERSION) {
345 int length = versionedBytes.length - Bytes.SIZEOF_SHORT;
346 return valueOf(Bytes.toString(versionedBytes, Bytes.SIZEOF_SHORT, length));
347 }
348
349
350 return valueOf(Bytes.toString(versionedBytes), NON_STARTCODE);
351 }
352
353
354
355
356
357
358 public static ServerName parseServerName(final String str) {
359 return SERVERNAME_PATTERN.matcher(str).matches()? valueOf(str) :
360 valueOf(str, NON_STARTCODE);
361 }
362
363
364
365
366
367
368 public static boolean isFullServerName(final String str){
369 if (str == null ||str.isEmpty()) return false;
370 return SERVERNAME_PATTERN.matcher(str).matches();
371 }
372
373
374
375
376
377
378
379
380
381
382
383 public static ServerName parseFrom(final byte [] data) throws DeserializationException {
384 if (data == null || data.length <= 0) return null;
385 if (ProtobufUtil.isPBMagicPrefix(data)) {
386 int prefixLen = ProtobufUtil.lengthOfPBMagic();
387 try {
388 ZooKeeperProtos.Master rss =
389 ZooKeeperProtos.Master.PARSER.parseFrom(data, prefixLen, data.length - prefixLen);
390 org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.ServerName sn = rss.getMaster();
391 return valueOf(sn.getHostName(), sn.getPort(), sn.getStartCode());
392 } catch (InvalidProtocolBufferException e) {
393
394
395
396
397
398 throw new DeserializationException(e);
399 }
400 }
401
402
403
404 String str = Bytes.toString(data);
405 int index = str.indexOf(ServerName.SERVERNAME_SEPARATOR);
406 if (index != -1) {
407
408 return ServerName.parseVersionedServerName(data);
409 }
410
411 String hostname = Addressing.parseHostname(str);
412 int port = Addressing.parsePort(str);
413 return valueOf(hostname, port, -1L);
414 }
415 }