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