From 71d0862d845486495b991a31beea3d8a1bb1d4fd Mon Sep 17 00:00:00 2001 From: Andrew Purtell Date: Tue, 17 Jan 2023 13:20:03 -0800 Subject: [PATCH] HBASE-27561 hbase.master.port is ignored in processing of hbase.masters (#4952) Signed-off-by: Duo Zhang Signed-off-by: Reid Chan --- .../hadoop/hbase/client/MasterRegistry.java | 22 +++++++++++++++---- .../hbase/client/TestMasterRegistry.java | 18 +++++++++++++++ 2 files changed, 36 insertions(+), 4 deletions(-) diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/MasterRegistry.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/MasterRegistry.java index 28ac5bb4528..b6f81c30f0b 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/MasterRegistry.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/MasterRegistry.java @@ -71,18 +71,32 @@ public class MasterRegistry extends AbstractRpcBasedConnectionRegistry { private static final String MASTER_ADDRS_CONF_SEPARATOR = ","; + /** + * Supplies the default master port we should use given the provided configuration. + * @param conf Configuration to parse from. + */ + private static int getDefaultMasterPort(Configuration conf) { + final int port = conf.getInt(HConstants.MASTER_PORT, HConstants.DEFAULT_MASTER_PORT); + if (port == 0) { + // Master port may be set to 0. We should substitute the default port in that case. + return HConstants.DEFAULT_MASTER_PORT; + } + return port; + } + /** * Parses the list of master addresses from the provided configuration. Supported format is comma * separated host[:port] values. If no port number if specified, default master port is assumed. * @param conf Configuration to parse from. */ public static Set parseMasterAddrs(Configuration conf) throws UnknownHostException { - Set masterAddrs = new HashSet<>(); - String configuredMasters = getMasterAddr(conf); + final int defaultPort = getDefaultMasterPort(conf); + final Set masterAddrs = new HashSet<>(); + final String configuredMasters = getMasterAddr(conf); for (String masterAddr : Splitter.onPattern(MASTER_ADDRS_CONF_SEPARATOR) .split(configuredMasters)) { - HostAndPort masterHostPort = - HostAndPort.fromString(masterAddr.trim()).withDefaultPort(HConstants.DEFAULT_MASTER_PORT); + final HostAndPort masterHostPort = + HostAndPort.fromString(masterAddr.trim()).withDefaultPort(defaultPort); masterAddrs.add(ServerName.valueOf(masterHostPort.toString(), ServerName.NON_STARTCODE)); } Preconditions.checkArgument(!masterAddrs.isEmpty(), "At least one master address is needed"); diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestMasterRegistry.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestMasterRegistry.java index 1cc176e2951..c9238bc9997 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestMasterRegistry.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestMasterRegistry.java @@ -108,6 +108,24 @@ public class TestMasterRegistry { } } + @Test + public void testMasterPortDefaults() throws IOException { + Configuration conf = new Configuration(TEST_UTIL.getConfiguration()); + conf.set(HConstants.MASTER_ADDRS_KEY, "localhost"); + try (MasterRegistry registry = new MasterRegistry(conf)) { + List parsedMasters = new ArrayList<>(registry.getParsedServers()); + ServerName sn = parsedMasters.get(0); + assertEquals(HConstants.DEFAULT_MASTER_PORT, sn.getPort()); + } + final int CUSTOM_MASTER_PORT = 9999; + conf.setInt(HConstants.MASTER_PORT, CUSTOM_MASTER_PORT); + try (MasterRegistry registry = new MasterRegistry(conf)) { + List parsedMasters = new ArrayList<>(registry.getParsedServers()); + ServerName sn = parsedMasters.get(0); + assertEquals(CUSTOM_MASTER_PORT, sn.getPort()); + } + } + @Test public void testRegistryRPCs() throws Exception { Configuration conf = new Configuration(TEST_UTIL.getConfiguration());