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 int getPort() {
217 return port;
218 }
219
220 public long getStartcode() {
221 return startcode;
222 }
223
224
225
226
227
228
229
230
231
232 static String getServerName(String hostName, int port, long startcode) {
233 final StringBuilder name = new StringBuilder(hostName.length() + 1 + 5 + 1 + 13);
234 name.append(hostName.toLowerCase(Locale.ROOT));
235 name.append(SERVERNAME_SEPARATOR);
236 name.append(port);
237 name.append(SERVERNAME_SEPARATOR);
238 name.append(startcode);
239 return name.toString();
240 }
241
242
243
244
245
246
247
248 public static String getServerName(final String hostAndPort,
249 final long startcode) {
250 int index = hostAndPort.indexOf(":");
251 if (index <= 0) throw new IllegalArgumentException("Expected <hostname> ':' <port>");
252 return getServerName(hostAndPort.substring(0, index),
253 Integer.parseInt(hostAndPort.substring(index + 1)), startcode);
254 }
255
256
257
258
259
260 public String getHostAndPort() {
261 return Addressing.createHostAndPortStr(this.hostnameOnly, this.port);
262 }
263
264
265
266
267
268 public static long getServerStartcodeFromServerName(final String serverName) {
269 int index = serverName.lastIndexOf(SERVERNAME_SEPARATOR);
270 return Long.parseLong(serverName.substring(index + 1));
271 }
272
273
274
275
276
277
278 public static String getServerNameLessStartCode(String inServerName) {
279 if (inServerName != null && inServerName.length() > 0) {
280 int index = inServerName.lastIndexOf(SERVERNAME_SEPARATOR);
281 if (index > 0) {
282 return inServerName.substring(0, index);
283 }
284 }
285 return inServerName;
286 }
287
288 @Override
289 public int compareTo(ServerName other) {
290 int compare;
291 if (other == null) {
292 return -1;
293 }
294 if (this.getHostname() == null) {
295 if (other.getHostname() != null) {
296 return 1;
297 }
298 } else {
299 if (other.getHostname() == null) {
300 return -1;
301 }
302 compare = this.getHostname().compareToIgnoreCase(other.getHostname());
303 if (compare != 0) {
304 return compare;
305 }
306 }
307 compare = this.getPort() - other.getPort();
308 if (compare != 0) {
309 return compare;
310 }
311 return Long.compare(this.getStartcode(), other.getStartcode());
312 }
313
314 @Override
315 public int hashCode() {
316 return getServerName().hashCode();
317 }
318
319 @Override
320 public boolean equals(Object o) {
321 if (this == o) return true;
322 if (o == null) return false;
323 if (!(o instanceof ServerName)) return false;
324 return this.compareTo((ServerName)o) == 0;
325 }
326
327
328
329
330
331
332 public static boolean isSameHostnameAndPort(final ServerName left,
333 final ServerName right) {
334 if (left == null) return false;
335 if (right == null) return false;
336 return left.getHostname().compareToIgnoreCase(right.getHostname()) == 0 &&
337 left.getPort() == right.getPort();
338 }
339
340
341
342
343
344
345
346
347
348 public static ServerName parseVersionedServerName(final byte [] versionedBytes) {
349
350 short version = Bytes.toShort(versionedBytes);
351 if (version == VERSION) {
352 int length = versionedBytes.length - Bytes.SIZEOF_SHORT;
353 return valueOf(Bytes.toString(versionedBytes, Bytes.SIZEOF_SHORT, length));
354 }
355
356
357 return valueOf(Bytes.toString(versionedBytes), NON_STARTCODE);
358 }
359
360
361
362
363
364
365 public static ServerName parseServerName(final String str) {
366 return SERVERNAME_PATTERN.matcher(str).matches()? valueOf(str) :
367 valueOf(str, NON_STARTCODE);
368 }
369
370
371
372
373
374
375 public static boolean isFullServerName(final String str){
376 if (str == null ||str.isEmpty()) return false;
377 return SERVERNAME_PATTERN.matcher(str).matches();
378 }
379
380
381
382
383
384
385
386
387
388
389
390 public static ServerName parseFrom(final byte [] data) throws DeserializationException {
391 if (data == null || data.length <= 0) return null;
392 if (ProtobufUtil.isPBMagicPrefix(data)) {
393 int prefixLen = ProtobufUtil.lengthOfPBMagic();
394 try {
395 ZooKeeperProtos.Master rss =
396 ZooKeeperProtos.Master.PARSER.parseFrom(data, prefixLen, data.length - prefixLen);
397 org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.ServerName sn = rss.getMaster();
398 return valueOf(sn.getHostName(), sn.getPort(), sn.getStartCode());
399 } catch (InvalidProtocolBufferException e) {
400
401
402
403
404
405 throw new DeserializationException(e);
406 }
407 }
408
409
410
411 String str = Bytes.toString(data);
412 int index = str.indexOf(ServerName.SERVERNAME_SEPARATOR);
413 if (index != -1) {
414
415 return ServerName.parseVersionedServerName(data);
416 }
417
418 String hostname = Addressing.parseHostname(str);
419 int port = Addressing.parsePort(str);
420 return valueOf(hostname, port, -1L);
421 }
422
423
424
425
426 public Address getAddress() {
427 return Address.fromParts(getHostname(), getPort());
428 }
429
430
431
432
433
434
435 public static boolean isSameAddress(final ServerName left,
436 final ServerName right) {
437
438 if (left == null) return false;
439 if (right == null) return false;
440 return left.getHostname().compareToIgnoreCase(right.getHostname()) == 0 &&
441 left.getPort() == right.getPort();
442 }
443
444 }