Skip to content

Commit

Permalink
HBASE-24305 Removed deprecations in ServerName (#1666)
Browse files Browse the repository at this point in the history
Signed-off-by: Duo Zhang <zhangduo@apache.org>
Signed-off-by: stack <stack@apache.org>
Rate limit · GitHub

Access has been restricted

You have triggered a rate limit.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

HorizonNet authored May 8, 2020
1 parent fddb2dd commit 03e5a14
Showing 19 changed files with 117 additions and 217 deletions.
Original file line number Diff line number Diff line change
@@ -367,9 +367,9 @@ public static String parseHostFromOldLog(Path p) {
String n = p.getName();
int idx = n.lastIndexOf(LOGNAME_SEPARATOR);
String s = URLDecoder.decode(n.substring(0, idx), "UTF8");
return ServerName.parseHostname(s) + ":" + ServerName.parsePort(s);
return ServerName.valueOf(s).getAddress().toString();
} catch (Exception e) {
LOG.warn("Skip log file (can't parse): " + p);
LOG.warn("Skip log file (can't parse): {}", p);
return null;
}
}
Original file line number Diff line number Diff line change
@@ -600,7 +600,7 @@ protected ServerName getOneRandomServer(String rack, Set<ServerName> skipServerS
}

if (randomServer != null) {
return ServerName.valueOf(randomServer.getHostAndPort(), randomServer.getStartcode());
return ServerName.valueOf(randomServer.getAddress(), randomServer.getStartcode());
} else {
return null;
}
@@ -628,7 +628,7 @@ public static String getFavoredNodesAsString(List<ServerName> nodes) {
StringBuilder strBuf = new StringBuilder();
int i = 0;
for (ServerName node : nodes) {
strBuf.append(node.getHostAndPort());
strBuf.append(node.getAddress());
if (++i != nodes.size()) strBuf.append(";");
}
return strBuf.toString();
@@ -772,7 +772,7 @@ public List<ServerName> generateFavoredNodes(RegionInfo hri) throws IOException

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

Map<RegionInfo, ServerName> primaryRSMap = new HashMap<>(1);
primaryRSMap.put(hri, primary);
@@ -781,7 +781,7 @@ public List<ServerName> generateFavoredNodes(RegionInfo hri) throws IOException
ServerName[] secondaryAndTertiaryNodes = secondaryAndTertiaryRSMap.get(hri);
if (secondaryAndTertiaryNodes != null && secondaryAndTertiaryNodes.length == 2) {
for (ServerName sn : secondaryAndTertiaryNodes) {
favoredNodesForRegion.add(ServerName.valueOf(sn.getHostAndPort(), ServerName.NON_STARTCODE));
favoredNodesForRegion.add(ServerName.valueOf(sn.getAddress(), ServerName.NON_STARTCODE));
}
return favoredNodesForRegion;
} else {
Original file line number Diff line number Diff line change
@@ -61,7 +61,7 @@ public int compareTo(ServerName other) {

@Override
public int hashCode() {
return getHostAndPort().hashCode();
return getAddress().hashCode();
}

// Do not need @Override #equals() because super.equals() delegates to compareTo(), which ends
Original file line number Diff line number Diff line change
@@ -619,7 +619,7 @@ private void printHServerAddressSet(Set<ServerName> serverSet) {
if ((i++) % 3 == 0) {
System.out.print("\n\t\t\t");
}
System.out.print(addr.getHostAndPort() + " ; ");
System.out.print(addr.getAddress() + " ; ");
}
System.out.println("\n");
}
213 changes: 64 additions & 149 deletions hbase-common/src/main/java/org/apache/hadoop/hbase/ServerName.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/**
*
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
@@ -23,16 +22,13 @@
import java.util.List;
import java.util.Locale;
import java.util.regex.Pattern;

import org.apache.hadoop.hbase.net.Address;
import org.apache.hadoop.hbase.util.Addressing;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.yetus.audience.InterfaceAudience;
import org.apache.hbase.thirdparty.com.google.common.collect.Interner;
import org.apache.hbase.thirdparty.com.google.common.collect.Interners;
import org.apache.hbase.thirdparty.com.google.common.net.InetAddresses;
import org.apache.yetus.audience.InterfaceAudience;



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

private ServerName(final String serverName) {
this(parseHostname(serverName), parsePort(serverName),
parseStartcode(serverName));
}

private ServerName(final String hostAndPort, final long startCode) {
this(Address.fromString(hostAndPort), startCode);
}

/**
* @param hostname
* @param hostname the hostname string to get the actual hostname from
* @return hostname minus the domain, if there is one (will do pass-through on ip addresses)
* @deprecated Since 2.0. This is for internal use only.
*/
@Deprecated
// Make this private in hbase-3.0.
static String getHostNameMinusDomain(final String hostname) {
if (InetAddresses.isInetAddress(hostname)) return hostname;
String [] parts = hostname.split("\\.");
if (parts == null || parts.length == 0) return hostname;
return parts[0];
}

/**
* @deprecated Since 2.0. Use {@link #valueOf(String)}
*/
@Deprecated
// This is unused. Get rid of it.
public static String parseHostname(final String serverName) {
if (serverName == null || serverName.length() <= 0) {
throw new IllegalArgumentException("Passed hostname is null or empty");
private static String getHostNameMinusDomain(final String hostname) {
if (InetAddresses.isInetAddress(hostname)) {
return hostname;
}
if (!Character.isLetterOrDigit(serverName.charAt(0))) {
throw new IllegalArgumentException("Bad passed hostname, serverName=" + serverName);
String[] parts = hostname.split("\\.");
if (parts.length == 0) {
return hostname;
}
int index = serverName.indexOf(SERVERNAME_SEPARATOR);
return serverName.substring(0, index);
}

/**
* @deprecated Since 2.0. Use {@link #valueOf(String)}
*/
@Deprecated
// This is unused. Get rid of it.
public static int parsePort(final String serverName) {
String [] split = serverName.split(SERVERNAME_SEPARATOR);
return Integer.parseInt(split[1]);
}

/**
* @deprecated Since 2.0. Use {@link #valueOf(String)}
*/
@Deprecated
// This is unused. Get rid of it.
public static long parseStartcode(final String serverName) {
int index = serverName.lastIndexOf(SERVERNAME_SEPARATOR);
return Long.parseLong(serverName.substring(index + 1));
return parts[0];
}

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

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

/**
* Retrieve an instance of {@link ServerName}. Callers should use the {@link #equals(Object)}
* method to compare returned instances, though we may return a shared immutable object as an
* internal optimization.
*
* @param address the {@link Address} to use for getting the {@link ServerName}
* @param startcode the startcode to use for getting the {@link ServerName}
* @return the constructed {@link ServerName}
* @see #valueOf(String, int, long)
*/
public static ServerName valueOf(final Address address, final long startcode) {
return valueOf(address.getHostname(), address.getPort(), startcode);
}

@Override
public String toString() {
return getServerName();
}

/**
* @return Return a SHORT version of {@link ServerName#toString()}, one that has the host only,
* minus the domain, and the port only -- no start code; the String is for us internally mostly
* tying threads to their server. Not for external use. It is lossy and will not work in
* in compares, etc.
* @return Return a SHORT version of {@link #toString()}, one that has the host only,
* minus the domain, and the port only -- no start code; the String is for us internally mostly
* tying threads to their server. Not for external use. It is lossy and will not work in
* in compares, etc.
*/
public String toShortString() {
return Addressing.createHostAndPortStr(
getHostNameMinusDomain(this.address.getHostname()),
this.address.getPort());
getHostNameMinusDomain(this.address.getHostname()),
this.address.getPort());
}

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

/**
* For internal use only.
* @param hostName
* @param port
* @param startcode
* @param hostName the name of the host to use
* @param port the port on the host to use
* @param startcode the startcode to use for formatting
* @return Server name made of the concatenation of hostname, port and
* startcode formatted as <code>&lt;hostname&gt; ',' &lt;port&gt; ',' &lt;startcode&gt;</code>
* @deprecated Since 2.0. Use {@link ServerName#valueOf(String, int, long)} instead.
* startcode formatted as <code>&lt;hostname&gt; ',' &lt;port&gt; ',' &lt;startcode&gt;</code>
*/
@Deprecated
// TODO: Make this private in hbase-3.0.
static String getServerName(String hostName, int port, long startcode) {
final StringBuilder name = new StringBuilder(hostName.length() + 1 + 5 + 1 + 13);
name.append(hostName.toLowerCase(Locale.ROOT));
name.append(SERVERNAME_SEPARATOR);
name.append(port);
name.append(SERVERNAME_SEPARATOR);
name.append(startcode);
return name.toString();
}

/**
* @param hostAndPort String in form of &lt;hostname&gt; ':' &lt;port&gt;
* @param startcode
* @return Server name made of the concatenation of hostname, port and
* startcode formatted as <code>&lt;hostname&gt; ',' &lt;port&gt; ',' &lt;startcode&gt;</code>
* @deprecated Since 2.0. Use {@link ServerName#valueOf(String, long)} instead.
*/
@Deprecated
public static String getServerName(final String hostAndPort,
final long startcode) {
int index = hostAndPort.indexOf(":");
if (index <= 0) throw new IllegalArgumentException("Expected <hostname> ':' <port>");
return getServerName(hostAndPort.substring(0, index),
Integer.parseInt(hostAndPort.substring(index + 1)), startcode);
}

/**
* @return Hostname and port formatted as described at
* {@link Addressing#createHostAndPortStr(String, int)}
* @deprecated Since 2.0. Use {@link #getAddress()} instead.
*/
@Deprecated
public String getHostAndPort() {
return this.address.toString();
private static String getServerName(String hostName, int port, long startcode) {
return hostName.toLowerCase(Locale.ROOT) + SERVERNAME_SEPARATOR + port
+ SERVERNAME_SEPARATOR + startcode;
}

public Address getAddress() {
return this.address;
}

/**
* @param serverName ServerName in form specified by {@link #getServerName()}
* @return The server start code parsed from <code>servername</code>
* @deprecated Since 2.0. Use instance of ServerName to pull out start code.
*/
@Deprecated
public static long getServerStartcodeFromServerName(final String serverName) {
int index = serverName.lastIndexOf(SERVERNAME_SEPARATOR);
return Long.parseLong(serverName.substring(index + 1));
}

/**
* Utility method to excise the start code from a server name
* @param inServerName full server name
* @return server name less its start code
* @deprecated Since 2.0. Use {@link #getAddress()}
*/
@Deprecated
public static String getServerNameLessStartCode(String inServerName) {
if (inServerName != null && inServerName.length() > 0) {
int index = inServerName.lastIndexOf(SERVERNAME_SEPARATOR);
if (index > 0) {
return inServerName.substring(0, index);
}
}
return inServerName;
}

@Override
public int compareTo(ServerName other) {
int compare;
@@ -366,24 +279,25 @@ public int hashCode() {

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null) return false;
if (!(o instanceof ServerName)) return false;
if (this == o) {
return true;
}
if (o == null) {
return false;
}
if (!(o instanceof ServerName)) {
return false;
}
return this.compareTo((ServerName)o) == 0;
}

/**
* @param left
* @param right
* @return True if <code>other</code> has same hostname and port.
* @param left the first server address to compare
* @param right the second server address to compare
* @return {@code true} if {@code left} and {@code right} have the same hostname and port.
*/
public static boolean isSameAddress(final ServerName left,
final ServerName right) {
// TODO: Make this left.getAddress().equals(right.getAddress())
if (left == null) return false;
if (right == null) return false;
return left.getHostname().compareToIgnoreCase(right.getHostname()) == 0 &&
left.getPort() == right.getPort();
public static boolean isSameAddress(final ServerName left, final ServerName right) {
return left.getAddress().equals(right.getAddress());
}

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

/**
* @param str Either an instance of {@link ServerName#toString()} or a
* "'&lt;hostname&gt;' ':' '&lt;port&gt;'".
* @param str Either an instance of {@link #toString()} or a
* "'&lt;hostname&gt;' ':' '&lt;port&gt;'".
* @return A ServerName instance.
*/
public static ServerName parseServerName(final String str) {
return SERVERNAME_PATTERN.matcher(str).matches()? valueOf(str) :
valueOf(str, NON_STARTCODE);
}


/**
* @return true if the String follows the pattern of {@link ServerName#toString()}, false
* otherwise.
* @return true if the String follows the pattern of {@link #toString()}, false
* otherwise.
*/
public static boolean isFullServerName(final String str){
if (str == null ||str.isEmpty()) return false;
if (str == null ||str.isEmpty()) {
return false;
}
return SERVERNAME_PATTERN.matcher(str).matches();
}
}
Rate limit · GitHub

Access has been restricted

You have triggered a rate limit.

Please wait a few minutes before you try again;
in some cases this may take up to an hour.

0 comments on commit 03e5a14

Please sign in to comment.