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