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