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