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