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