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 public static ServerName valueOf(final String hostname, final int port, final long startcode) {
151 return new ServerName(hostname, port, startcode);
152 }
153
154 public static ServerName valueOf(final String serverName) {
155 return new ServerName(serverName);
156 }
157
158 public static ServerName valueOf(final String hostAndPort, final long startCode) {
159 return new ServerName(hostAndPort, startCode);
160 }
161
162 @Override
163 public String toString() {
164 return getServerName();
165 }
166
167
168
169
170
171
172
173 public String toShortString() {
174 return Addressing.createHostAndPortStr(getHostNameMinusDomain(this.hostnameOnly), this.port);
175 }
176
177
178
179
180
181 public synchronized byte [] getVersionedBytes() {
182 if (this.bytes == null) {
183 this.bytes = Bytes.add(VERSION_BYTES, Bytes.toBytes(getServerName()));
184 }
185 return this.bytes;
186 }
187
188 public String getServerName() {
189 return servername;
190 }
191
192 public String getHostname() {
193 return hostnameOnly;
194 }
195
196 public int getPort() {
197 return port;
198 }
199
200 public long getStartcode() {
201 return startcode;
202 }
203
204
205
206
207
208
209
210
211
212 static String getServerName(String hostName, int port, long startcode) {
213 final StringBuilder name = new StringBuilder(hostName.length() + 1 + 5 + 1 + 13);
214 name.append(hostName);
215 name.append(SERVERNAME_SEPARATOR);
216 name.append(port);
217 name.append(SERVERNAME_SEPARATOR);
218 name.append(startcode);
219 return name.toString();
220 }
221
222
223
224
225
226
227
228 public static String getServerName(final String hostAndPort,
229 final long startcode) {
230 int index = hostAndPort.indexOf(":");
231 if (index <= 0) throw new IllegalArgumentException("Expected <hostname> ':' <port>");
232 return getServerName(hostAndPort.substring(0, index),
233 Integer.parseInt(hostAndPort.substring(index + 1)), startcode);
234 }
235
236
237
238
239
240 public String getHostAndPort() {
241 return Addressing.createHostAndPortStr(this.hostnameOnly, this.port);
242 }
243
244
245
246
247
248 public static long getServerStartcodeFromServerName(final String serverName) {
249 int index = serverName.lastIndexOf(SERVERNAME_SEPARATOR);
250 return Long.parseLong(serverName.substring(index + 1));
251 }
252
253
254
255
256
257
258 public static String getServerNameLessStartCode(String inServerName) {
259 if (inServerName != null && inServerName.length() > 0) {
260 int index = inServerName.lastIndexOf(SERVERNAME_SEPARATOR);
261 if (index > 0) {
262 return inServerName.substring(0, index);
263 }
264 }
265 return inServerName;
266 }
267
268 @Override
269 public int compareTo(ServerName other) {
270 int compare = this.getHostname().compareToIgnoreCase(other.getHostname());
271 if (compare != 0) return compare;
272 compare = this.getPort() - other.getPort();
273 if (compare != 0) return compare;
274 return (int)(this.getStartcode() - other.getStartcode());
275 }
276
277 @Override
278 public int hashCode() {
279 return getServerName().hashCode();
280 }
281
282 @Override
283 public boolean equals(Object o) {
284 if (this == o) return true;
285 if (o == null) return false;
286 if (!(o instanceof ServerName)) return false;
287 return this.compareTo((ServerName)o) == 0;
288 }
289
290
291
292
293
294
295 public static boolean isSameHostnameAndPort(final ServerName left,
296 final ServerName right) {
297 if (left == null) return false;
298 if (right == null) return false;
299 return left.getHostname().equals(right.getHostname()) &&
300 left.getPort() == right.getPort();
301 }
302
303
304
305
306
307
308
309
310
311 public static ServerName parseVersionedServerName(final byte [] versionedBytes) {
312
313 short version = Bytes.toShort(versionedBytes);
314 if (version == VERSION) {
315 int length = versionedBytes.length - Bytes.SIZEOF_SHORT;
316 return valueOf(Bytes.toString(versionedBytes, Bytes.SIZEOF_SHORT, length));
317 }
318
319
320 return valueOf(Bytes.toString(versionedBytes), NON_STARTCODE);
321 }
322
323
324
325
326
327
328 public static ServerName parseServerName(final String str) {
329 return SERVERNAME_PATTERN.matcher(str).matches()? valueOf(str) :
330 valueOf(str, NON_STARTCODE);
331 }
332
333
334
335
336
337
338 public static boolean isFullServerName(final String str){
339 if (str == null ||str.isEmpty()) return false;
340 return SERVERNAME_PATTERN.matcher(str).matches();
341 }
342
343
344
345
346
347
348
349
350
351
352
353 public static ServerName parseFrom(final byte [] data) throws DeserializationException {
354 if (data == null || data.length <= 0) return null;
355 if (ProtobufUtil.isPBMagicPrefix(data)) {
356 int prefixLen = ProtobufUtil.lengthOfPBMagic();
357 try {
358 MetaRegionServer rss =
359 MetaRegionServer.PARSER.parseFrom(data, prefixLen, data.length - prefixLen);
360 org.apache.hadoop.hbase.protobuf.generated.HBaseProtos.ServerName sn = rss.getServer();
361 return valueOf(sn.getHostName(), sn.getPort(), sn.getStartCode());
362 } catch (InvalidProtocolBufferException e) {
363
364
365
366
367
368 throw new DeserializationException(e);
369 }
370 }
371
372
373
374 String str = Bytes.toString(data);
375 int index = str.indexOf(ServerName.SERVERNAME_SEPARATOR);
376 if (index != -1) {
377
378 return ServerName.parseVersionedServerName(data);
379 }
380
381 String hostname = Addressing.parseHostname(str);
382 int port = Addressing.parsePort(str);
383 return valueOf(hostname, port, -1L);
384 }
385 }