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