From d16d5f730e9d139d3e026805f21ac2c9b0bbb98b Mon Sep 17 00:00:00 2001 From: Yiqun Lin Date: Fri, 2 Nov 2018 10:50:32 +0800 Subject: [PATCH] HDDS-751. Replace usage of Guava Optional with Java Optional. --- .../org/apache/hadoop/hdds/HddsUtils.java | 29 +++++++++---------- .../hadoop/hdds/scm/HddsServerUtil.java | 17 ++++++----- .../hadoop/hdds/server/BaseHttpServer.java | 9 ++++-- .../java/org/apache/hadoop/ozone/OmUtils.java | 8 ++--- 4 files changed, 32 insertions(+), 31 deletions(-) diff --git a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/HddsUtils.java b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/HddsUtils.java index 89edfddd09..18637af7a8 100644 --- a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/HddsUtils.java +++ b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/HddsUtils.java @@ -18,7 +18,6 @@ package org.apache.hadoop.hdds; -import com.google.common.base.Optional; import com.google.common.base.Strings; import com.google.common.net.HostAndPort; import org.apache.hadoop.classification.InterfaceAudience; @@ -45,6 +44,7 @@ import java.util.Collection; import java.util.HashSet; import java.util.Map; +import java.util.Optional; import java.util.TimeZone; import static org.apache.hadoop.hdfs.DFSConfigKeys @@ -114,7 +114,7 @@ public static InetSocketAddress getScmAddressForClients(Configuration conf) { ScmConfigKeys.OZONE_SCM_CLIENT_ADDRESS_KEY); return NetUtils.createSocketAddr(host.get() + ":" + port - .or(ScmConfigKeys.OZONE_SCM_CLIENT_PORT_DEFAULT)); + .orElse(ScmConfigKeys.OZONE_SCM_CLIENT_PORT_DEFAULT)); } /** @@ -162,7 +162,7 @@ public static InetSocketAddress getScmAddressForBlockClients( ScmConfigKeys.OZONE_SCM_BLOCK_CLIENT_ADDRESS_KEY); return NetUtils.createSocketAddr(host.get() + ":" + port - .or(ScmConfigKeys.OZONE_SCM_BLOCK_CLIENT_PORT_DEFAULT)); + .orElse(ScmConfigKeys.OZONE_SCM_BLOCK_CLIENT_PORT_DEFAULT)); } /** @@ -186,7 +186,7 @@ public static Optional getHostNameFromConfigKeys(Configuration conf, return hostName; } } - return Optional.absent(); + return Optional.empty(); } /** @@ -196,7 +196,7 @@ public static Optional getHostNameFromConfigKeys(Configuration conf, */ public static Optional getHostName(String value) { if ((value == null) || value.isEmpty()) { - return Optional.absent(); + return Optional.empty(); } return Optional.of(HostAndPort.fromString(value).getHostText()); } @@ -208,11 +208,11 @@ public static Optional getHostName(String value) { */ public static Optional getHostPort(String value) { if ((value == null) || value.isEmpty()) { - return Optional.absent(); + return Optional.empty(); } int port = HostAndPort.fromString(value).getPortOrDefault(NO_PORT); if (port == NO_PORT) { - return Optional.absent(); + return Optional.empty(); } else { return Optional.of(port); } @@ -239,7 +239,7 @@ public static Optional getPortNumberFromConfigKeys( return hostPort; } } - return Optional.absent(); + return Optional.empty(); } /** @@ -261,20 +261,17 @@ public static Collection getSCMAddresses( + " Null or empty address list found."); } - final com.google.common.base.Optional - defaultPort = com.google.common.base.Optional.of(ScmConfigKeys - .OZONE_SCM_DEFAULT_PORT); + final Optional defaultPort = Optional + .of(ScmConfigKeys.OZONE_SCM_DEFAULT_PORT); for (String address : names) { - com.google.common.base.Optional hostname = - getHostName(address); + Optional hostname = getHostName(address); if (!hostname.isPresent()) { throw new IllegalArgumentException("Invalid hostname for SCM: " + hostname); } - com.google.common.base.Optional port = - getHostPort(address); + Optional port = getHostPort(address); InetSocketAddress addr = NetUtils.createSocketAddr(hostname.get(), - port.or(defaultPort.get())); + port.orElse(defaultPort.get())); addresses.add(addr); } return addresses; diff --git a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/hdds/scm/HddsServerUtil.java b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/hdds/scm/HddsServerUtil.java index 738a2ef510..40b3d7af42 100644 --- a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/hdds/scm/HddsServerUtil.java +++ b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/hdds/scm/HddsServerUtil.java @@ -17,7 +17,6 @@ package org.apache.hadoop.hdds.scm; -import com.google.common.base.Optional; import com.google.common.base.Strings; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hdds.HddsConfigKeys; @@ -32,6 +31,7 @@ import java.util.Collection; import java.util.HashMap; import java.util.Map; +import java.util.Optional; import java.util.concurrent.TimeUnit; import static org.apache.hadoop.hdds.HddsConfigKeys @@ -114,7 +114,7 @@ public static InetSocketAddress getScmAddressForDataNodes( ScmConfigKeys.OZONE_SCM_DATANODE_ADDRESS_KEY); InetSocketAddress addr = NetUtils.createSocketAddr(host.get() + ":" + - port.or(ScmConfigKeys.OZONE_SCM_DATANODE_PORT_DEFAULT)); + port.orElse(ScmConfigKeys.OZONE_SCM_DATANODE_PORT_DEFAULT)); return addr; } @@ -135,8 +135,8 @@ public static InetSocketAddress getScmClientBindAddress( ScmConfigKeys.OZONE_SCM_CLIENT_ADDRESS_KEY); return NetUtils.createSocketAddr( - host.or(ScmConfigKeys.OZONE_SCM_CLIENT_BIND_HOST_DEFAULT) + ":" + - port.or(ScmConfigKeys.OZONE_SCM_CLIENT_PORT_DEFAULT)); + host.orElse(ScmConfigKeys.OZONE_SCM_CLIENT_BIND_HOST_DEFAULT) + ":" + + port.orElse(ScmConfigKeys.OZONE_SCM_CLIENT_PORT_DEFAULT)); } /** @@ -155,8 +155,9 @@ public static InetSocketAddress getScmBlockClientBindAddress( ScmConfigKeys.OZONE_SCM_BLOCK_CLIENT_ADDRESS_KEY); return NetUtils.createSocketAddr( - host.or(ScmConfigKeys.OZONE_SCM_BLOCK_CLIENT_BIND_HOST_DEFAULT) + - ":" + port.or(ScmConfigKeys.OZONE_SCM_BLOCK_CLIENT_PORT_DEFAULT)); + host.orElse(ScmConfigKeys.OZONE_SCM_BLOCK_CLIENT_BIND_HOST_DEFAULT) + + ":" + + port.orElse(ScmConfigKeys.OZONE_SCM_BLOCK_CLIENT_PORT_DEFAULT)); } /** @@ -176,8 +177,8 @@ public static InetSocketAddress getScmDataNodeBindAddress( ScmConfigKeys.OZONE_SCM_DATANODE_ADDRESS_KEY); return NetUtils.createSocketAddr( - host.or(ScmConfigKeys.OZONE_SCM_DATANODE_BIND_HOST_DEFAULT) + ":" + - port.or(ScmConfigKeys.OZONE_SCM_DATANODE_PORT_DEFAULT)); + host.orElse(ScmConfigKeys.OZONE_SCM_DATANODE_BIND_HOST_DEFAULT) + ":" + + port.orElse(ScmConfigKeys.OZONE_SCM_DATANODE_PORT_DEFAULT)); } diff --git a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/BaseHttpServer.java b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/BaseHttpServer.java index da5d8dae4a..2726fc3c51 100644 --- a/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/BaseHttpServer.java +++ b/hadoop-hdds/framework/src/main/java/org/apache/hadoop/hdds/server/BaseHttpServer.java @@ -17,7 +17,6 @@ package org.apache.hadoop.hdds.server; -import com.google.common.base.Optional; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hdfs.DFSConfigKeys; import org.apache.hadoop.hdfs.DFSUtil; @@ -32,6 +31,7 @@ import javax.servlet.http.HttpServlet; import java.io.IOException; import java.net.InetSocketAddress; +import java.util.Optional; import static org.apache.hadoop.hdds.HddsUtils.getHostNameFromConfigKeys; import static org.apache.hadoop.hdds.HddsUtils.getPortNumberFromConfigKeys; @@ -118,10 +118,13 @@ protected InetSocketAddress getBindAddress(String bindHostKey, final Optional addresHost = getHostNameFromConfigKeys(conf, addressKey); - String hostName = bindHost.or(addresHost).or(bindHostDefault); + String hostName = bindHost.orElse(addresHost.get()); + if (hostName == null || hostName.isEmpty()) { + hostName = bindHostDefault; + } return NetUtils.createSocketAddr( - hostName + ":" + addressPort.or(bindPortdefault)); + hostName + ":" + addressPort.orElse(bindPortdefault)); } /** diff --git a/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/OmUtils.java b/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/OmUtils.java index d8608beed5..92806485c4 100644 --- a/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/OmUtils.java +++ b/hadoop-ozone/common/src/main/java/org/apache/hadoop/ozone/OmUtils.java @@ -20,13 +20,13 @@ import java.io.File; import java.net.InetSocketAddress; import java.util.Collection; +import java.util.Optional; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hdds.HddsConfigKeys; import org.apache.hadoop.hdds.server.ServerUtils; import org.apache.hadoop.net.NetUtils; -import com.google.common.base.Optional; import org.apache.hadoop.ozone.om.OMConfigKeys; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -60,7 +60,7 @@ public static InetSocketAddress getOmAddress( OZONE_OM_ADDRESS_KEY); return NetUtils.createSocketAddr( - host.or(OZONE_OM_BIND_HOST_DEFAULT) + ":" + + host.orElse(OZONE_OM_BIND_HOST_DEFAULT) + ":" + getOmRpcPort(conf)); } @@ -90,7 +90,7 @@ public static int getOmRpcPort(Configuration conf) { // If no port number is specified then we'll just try the defaultBindPort. final Optional port = getPortNumberFromConfigKeys(conf, OZONE_OM_ADDRESS_KEY); - return port.or(OZONE_OM_PORT_DEFAULT); + return port.orElse(OZONE_OM_PORT_DEFAULT); } public static int getOmRestPort(Configuration conf) { @@ -98,7 +98,7 @@ public static int getOmRestPort(Configuration conf) { // HTTP BindPort. final Optional port = getPortNumberFromConfigKeys(conf, OZONE_OM_HTTP_ADDRESS_KEY); - return port.or(OZONE_OM_HTTP_BIND_PORT_DEFAULT); + return port.orElse(OZONE_OM_HTTP_BIND_PORT_DEFAULT); } /**