Skip to content

Commit 03e5a14

Browse files
authored
HBASE-24305 Removed deprecations in ServerName (#1666)
Signed-off-by: Duo Zhang <zhangduo@apache.org> Signed-off-by: stack <stack@apache.org>
1 parent fddb2dd commit 03e5a14

File tree

19 files changed

+117
-217
lines changed

19 files changed

+117
-217
lines changed

hbase-backup/src/main/java/org/apache/hadoop/hbase/backup/util/BackupUtils.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,9 @@ public static String parseHostFromOldLog(Path p) {
367367
String n = p.getName();
368368
int idx = n.lastIndexOf(LOGNAME_SEPARATOR);
369369
String s = URLDecoder.decode(n.substring(0, idx), "UTF8");
370-
return ServerName.parseHostname(s) + ":" + ServerName.parsePort(s);
370+
return ServerName.valueOf(s).getAddress().toString();
371371
} catch (Exception e) {
372-
LOG.warn("Skip log file (can't parse): " + p);
372+
LOG.warn("Skip log file (can't parse): {}", p);
373373
return null;
374374
}
375375
}

hbase-balancer/src/main/java/org/apache/hadoop/hbase/favored/FavoredNodeAssignmentHelper.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -600,7 +600,7 @@ protected ServerName getOneRandomServer(String rack, Set<ServerName> skipServerS
600600
}
601601

602602
if (randomServer != null) {
603-
return ServerName.valueOf(randomServer.getHostAndPort(), randomServer.getStartcode());
603+
return ServerName.valueOf(randomServer.getAddress(), randomServer.getStartcode());
604604
} else {
605605
return null;
606606
}
@@ -628,7 +628,7 @@ public static String getFavoredNodesAsString(List<ServerName> nodes) {
628628
StringBuilder strBuf = new StringBuilder();
629629
int i = 0;
630630
for (ServerName node : nodes) {
631-
strBuf.append(node.getHostAndPort());
631+
strBuf.append(node.getAddress());
632632
if (++i != nodes.size()) strBuf.append(";");
633633
}
634634
return strBuf.toString();
@@ -772,7 +772,7 @@ public List<ServerName> generateFavoredNodes(RegionInfo hri) throws IOException
772772

773773
List<ServerName> favoredNodesForRegion = new ArrayList<>(FAVORED_NODES_NUM);
774774
ServerName primary = servers.get(random.nextInt(servers.size()));
775-
favoredNodesForRegion.add(ServerName.valueOf(primary.getHostAndPort(), ServerName.NON_STARTCODE));
775+
favoredNodesForRegion.add(ServerName.valueOf(primary.getAddress(), ServerName.NON_STARTCODE));
776776

777777
Map<RegionInfo, ServerName> primaryRSMap = new HashMap<>(1);
778778
primaryRSMap.put(hri, primary);
@@ -781,7 +781,7 @@ public List<ServerName> generateFavoredNodes(RegionInfo hri) throws IOException
781781
ServerName[] secondaryAndTertiaryNodes = secondaryAndTertiaryRSMap.get(hri);
782782
if (secondaryAndTertiaryNodes != null && secondaryAndTertiaryNodes.length == 2) {
783783
for (ServerName sn : secondaryAndTertiaryNodes) {
784-
favoredNodesForRegion.add(ServerName.valueOf(sn.getHostAndPort(), ServerName.NON_STARTCODE));
784+
favoredNodesForRegion.add(ServerName.valueOf(sn.getAddress(), ServerName.NON_STARTCODE));
785785
}
786786
return favoredNodesForRegion;
787787
} else {

hbase-balancer/src/main/java/org/apache/hadoop/hbase/favored/StartcodeAgnosticServerName.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public int compareTo(ServerName other) {
6161

6262
@Override
6363
public int hashCode() {
64-
return getHostAndPort().hashCode();
64+
return getAddress().hashCode();
6565
}
6666

6767
// Do not need @Override #equals() because super.equals() delegates to compareTo(), which ends

hbase-balancer/src/main/java/org/apache/hadoop/hbase/master/AssignmentVerificationReport.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ private void printHServerAddressSet(Set<ServerName> serverSet) {
619619
if ((i++) % 3 == 0) {
620620
System.out.print("\n\t\t\t");
621621
}
622-
System.out.print(addr.getHostAndPort() + " ; ");
622+
System.out.print(addr.getAddress() + " ; ");
623623
}
624624
System.out.println("\n");
625625
}

hbase-common/src/main/java/org/apache/hadoop/hbase/ServerName.java

Lines changed: 64 additions & 149 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
/**
2-
*
1+
/*
32
* Licensed to the Apache Software Foundation (ASF) under one
43
* or more contributor license agreements. See the NOTICE file
54
* distributed with this work for additional information
@@ -23,16 +22,13 @@
2322
import java.util.List;
2423
import java.util.Locale;
2524
import java.util.regex.Pattern;
26-
2725
import org.apache.hadoop.hbase.net.Address;
2826
import org.apache.hadoop.hbase.util.Addressing;
2927
import org.apache.hadoop.hbase.util.Bytes;
28+
import org.apache.yetus.audience.InterfaceAudience;
3029
import org.apache.hbase.thirdparty.com.google.common.collect.Interner;
3130
import org.apache.hbase.thirdparty.com.google.common.collect.Interners;
3231
import org.apache.hbase.thirdparty.com.google.common.net.InetAddresses;
33-
import org.apache.yetus.audience.InterfaceAudience;
34-
35-
3632

3733
/**
3834
* Name of a particular incarnation of an HBase Server.
@@ -120,63 +116,24 @@ private ServerName(final Address address, final long startcode) {
120116
this.address.getPort(), startcode);
121117
}
122118

123-
private ServerName(final String serverName) {
124-
this(parseHostname(serverName), parsePort(serverName),
125-
parseStartcode(serverName));
126-
}
127-
128119
private ServerName(final String hostAndPort, final long startCode) {
129120
this(Address.fromString(hostAndPort), startCode);
130121
}
131122

132123
/**
133-
* @param hostname
124+
* @param hostname the hostname string to get the actual hostname from
134125
* @return hostname minus the domain, if there is one (will do pass-through on ip addresses)
135-
* @deprecated Since 2.0. This is for internal use only.
136126
*/
137-
@Deprecated
138-
// Make this private in hbase-3.0.
139-
static String getHostNameMinusDomain(final String hostname) {
140-
if (InetAddresses.isInetAddress(hostname)) return hostname;
141-
String [] parts = hostname.split("\\.");
142-
if (parts == null || parts.length == 0) return hostname;
143-
return parts[0];
144-
}
145-
146-
/**
147-
* @deprecated Since 2.0. Use {@link #valueOf(String)}
148-
*/
149-
@Deprecated
150-
// This is unused. Get rid of it.
151-
public static String parseHostname(final String serverName) {
152-
if (serverName == null || serverName.length() <= 0) {
153-
throw new IllegalArgumentException("Passed hostname is null or empty");
127+
private static String getHostNameMinusDomain(final String hostname) {
128+
if (InetAddresses.isInetAddress(hostname)) {
129+
return hostname;
154130
}
155-
if (!Character.isLetterOrDigit(serverName.charAt(0))) {
156-
throw new IllegalArgumentException("Bad passed hostname, serverName=" + serverName);
131+
String[] parts = hostname.split("\\.");
132+
if (parts.length == 0) {
133+
return hostname;
157134
}
158-
int index = serverName.indexOf(SERVERNAME_SEPARATOR);
159-
return serverName.substring(0, index);
160-
}
161-
162-
/**
163-
* @deprecated Since 2.0. Use {@link #valueOf(String)}
164-
*/
165-
@Deprecated
166-
// This is unused. Get rid of it.
167-
public static int parsePort(final String serverName) {
168-
String [] split = serverName.split(SERVERNAME_SEPARATOR);
169-
return Integer.parseInt(split[1]);
170-
}
171135

172-
/**
173-
* @deprecated Since 2.0. Use {@link #valueOf(String)}
174-
*/
175-
@Deprecated
176-
// This is unused. Get rid of it.
177-
public static long parseStartcode(final String serverName) {
178-
int index = serverName.lastIndexOf(SERVERNAME_SEPARATOR);
179-
return Long.parseLong(serverName.substring(index + 1));
136+
return parts[0];
180137
}
181138

182139
/**
@@ -194,7 +151,11 @@ public static ServerName valueOf(final String hostname, final int port, final lo
194151
* a shared immutable object as an internal optimization.
195152
*/
196153
public static ServerName valueOf(final String serverName) {
197-
return INTERN_POOL.intern(new ServerName(serverName));
154+
final String hostname = serverName.substring(0, serverName.indexOf(SERVERNAME_SEPARATOR));
155+
final int port = Integer.parseInt(serverName.split(SERVERNAME_SEPARATOR)[1]);
156+
final long statuscode =
157+
Long.parseLong(serverName.substring(serverName.lastIndexOf(SERVERNAME_SEPARATOR) + 1));
158+
return INTERN_POOL.intern(new ServerName(hostname, port, statuscode));
198159
}
199160

200161
/**
@@ -206,26 +167,40 @@ public static ServerName valueOf(final String hostAndPort, final long startCode)
206167
return INTERN_POOL.intern(new ServerName(hostAndPort, startCode));
207168
}
208169

170+
/**
171+
* Retrieve an instance of {@link ServerName}. Callers should use the {@link #equals(Object)}
172+
* method to compare returned instances, though we may return a shared immutable object as an
173+
* internal optimization.
174+
*
175+
* @param address the {@link Address} to use for getting the {@link ServerName}
176+
* @param startcode the startcode to use for getting the {@link ServerName}
177+
* @return the constructed {@link ServerName}
178+
* @see #valueOf(String, int, long)
179+
*/
180+
public static ServerName valueOf(final Address address, final long startcode) {
181+
return valueOf(address.getHostname(), address.getPort(), startcode);
182+
}
183+
209184
@Override
210185
public String toString() {
211186
return getServerName();
212187
}
213188

214189
/**
215-
* @return Return a SHORT version of {@link ServerName#toString()}, one that has the host only,
216-
* minus the domain, and the port only -- no start code; the String is for us internally mostly
217-
* tying threads to their server. Not for external use. It is lossy and will not work in
218-
* in compares, etc.
190+
* @return Return a SHORT version of {@link #toString()}, one that has the host only,
191+
* minus the domain, and the port only -- no start code; the String is for us internally mostly
192+
* tying threads to their server. Not for external use. It is lossy and will not work in
193+
* in compares, etc.
219194
*/
220195
public String toShortString() {
221196
return Addressing.createHostAndPortStr(
222-
getHostNameMinusDomain(this.address.getHostname()),
223-
this.address.getPort());
197+
getHostNameMinusDomain(this.address.getHostname()),
198+
this.address.getPort());
224199
}
225200

226201
/**
227202
* @return {@link #getServerName()} as bytes with a short-sized prefix with
228-
* the ServerName#VERSION of this class.
203+
* the {@link #VERSION} of this class.
229204
*/
230205
public synchronized byte [] getVersionedBytes() {
231206
if (this.bytes == null) {
@@ -256,83 +231,21 @@ public long getStartcode() {
256231

257232
/**
258233
* For internal use only.
259-
* @param hostName
260-
* @param port
261-
* @param startcode
234+
* @param hostName the name of the host to use
235+
* @param port the port on the host to use
236+
* @param startcode the startcode to use for formatting
262237
* @return Server name made of the concatenation of hostname, port and
263-
* startcode formatted as <code>&lt;hostname&gt; ',' &lt;port&gt; ',' &lt;startcode&gt;</code>
264-
* @deprecated Since 2.0. Use {@link ServerName#valueOf(String, int, long)} instead.
238+
* startcode formatted as <code>&lt;hostname&gt; ',' &lt;port&gt; ',' &lt;startcode&gt;</code>
265239
*/
266-
@Deprecated
267-
// TODO: Make this private in hbase-3.0.
268-
static String getServerName(String hostName, int port, long startcode) {
269-
final StringBuilder name = new StringBuilder(hostName.length() + 1 + 5 + 1 + 13);
270-
name.append(hostName.toLowerCase(Locale.ROOT));
271-
name.append(SERVERNAME_SEPARATOR);
272-
name.append(port);
273-
name.append(SERVERNAME_SEPARATOR);
274-
name.append(startcode);
275-
return name.toString();
276-
}
277-
278-
/**
279-
* @param hostAndPort String in form of &lt;hostname&gt; ':' &lt;port&gt;
280-
* @param startcode
281-
* @return Server name made of the concatenation of hostname, port and
282-
* startcode formatted as <code>&lt;hostname&gt; ',' &lt;port&gt; ',' &lt;startcode&gt;</code>
283-
* @deprecated Since 2.0. Use {@link ServerName#valueOf(String, long)} instead.
284-
*/
285-
@Deprecated
286-
public static String getServerName(final String hostAndPort,
287-
final long startcode) {
288-
int index = hostAndPort.indexOf(":");
289-
if (index <= 0) throw new IllegalArgumentException("Expected <hostname> ':' <port>");
290-
return getServerName(hostAndPort.substring(0, index),
291-
Integer.parseInt(hostAndPort.substring(index + 1)), startcode);
292-
}
293-
294-
/**
295-
* @return Hostname and port formatted as described at
296-
* {@link Addressing#createHostAndPortStr(String, int)}
297-
* @deprecated Since 2.0. Use {@link #getAddress()} instead.
298-
*/
299-
@Deprecated
300-
public String getHostAndPort() {
301-
return this.address.toString();
240+
private static String getServerName(String hostName, int port, long startcode) {
241+
return hostName.toLowerCase(Locale.ROOT) + SERVERNAME_SEPARATOR + port
242+
+ SERVERNAME_SEPARATOR + startcode;
302243
}
303244

304245
public Address getAddress() {
305246
return this.address;
306247
}
307248

308-
/**
309-
* @param serverName ServerName in form specified by {@link #getServerName()}
310-
* @return The server start code parsed from <code>servername</code>
311-
* @deprecated Since 2.0. Use instance of ServerName to pull out start code.
312-
*/
313-
@Deprecated
314-
public static long getServerStartcodeFromServerName(final String serverName) {
315-
int index = serverName.lastIndexOf(SERVERNAME_SEPARATOR);
316-
return Long.parseLong(serverName.substring(index + 1));
317-
}
318-
319-
/**
320-
* Utility method to excise the start code from a server name
321-
* @param inServerName full server name
322-
* @return server name less its start code
323-
* @deprecated Since 2.0. Use {@link #getAddress()}
324-
*/
325-
@Deprecated
326-
public static String getServerNameLessStartCode(String inServerName) {
327-
if (inServerName != null && inServerName.length() > 0) {
328-
int index = inServerName.lastIndexOf(SERVERNAME_SEPARATOR);
329-
if (index > 0) {
330-
return inServerName.substring(0, index);
331-
}
332-
}
333-
return inServerName;
334-
}
335-
336249
@Override
337250
public int compareTo(ServerName other) {
338251
int compare;
@@ -366,24 +279,25 @@ public int hashCode() {
366279

367280
@Override
368281
public boolean equals(Object o) {
369-
if (this == o) return true;
370-
if (o == null) return false;
371-
if (!(o instanceof ServerName)) return false;
282+
if (this == o) {
283+
return true;
284+
}
285+
if (o == null) {
286+
return false;
287+
}
288+
if (!(o instanceof ServerName)) {
289+
return false;
290+
}
372291
return this.compareTo((ServerName)o) == 0;
373292
}
374293

375294
/**
376-
* @param left
377-
* @param right
378-
* @return True if <code>other</code> has same hostname and port.
295+
* @param left the first server address to compare
296+
* @param right the second server address to compare
297+
* @return {@code true} if {@code left} and {@code right} have the same hostname and port.
379298
*/
380-
public static boolean isSameAddress(final ServerName left,
381-
final ServerName right) {
382-
// TODO: Make this left.getAddress().equals(right.getAddress())
383-
if (left == null) return false;
384-
if (right == null) return false;
385-
return left.getHostname().compareToIgnoreCase(right.getHostname()) == 0 &&
386-
left.getPort() == right.getPort();
299+
public static boolean isSameAddress(final ServerName left, final ServerName right) {
300+
return left.getAddress().equals(right.getAddress());
387301
}
388302

389303
/**
@@ -407,22 +321,23 @@ public static ServerName parseVersionedServerName(final byte [] versionedBytes)
407321
}
408322

409323
/**
410-
* @param str Either an instance of {@link ServerName#toString()} or a
411-
* "'&lt;hostname&gt;' ':' '&lt;port&gt;'".
324+
* @param str Either an instance of {@link #toString()} or a
325+
* "'&lt;hostname&gt;' ':' '&lt;port&gt;'".
412326
* @return A ServerName instance.
413327
*/
414328
public static ServerName parseServerName(final String str) {
415329
return SERVERNAME_PATTERN.matcher(str).matches()? valueOf(str) :
416330
valueOf(str, NON_STARTCODE);
417331
}
418332

419-
420333
/**
421-
* @return true if the String follows the pattern of {@link ServerName#toString()}, false
422-
* otherwise.
334+
* @return true if the String follows the pattern of {@link #toString()}, false
335+
* otherwise.
423336
*/
424337
public static boolean isFullServerName(final String str){
425-
if (str == null ||str.isEmpty()) return false;
338+
if (str == null ||str.isEmpty()) {
339+
return false;
340+
}
426341
return SERVERNAME_PATTERN.matcher(str).matches();
427342
}
428343
}

0 commit comments

Comments
 (0)