HBASE-27561 hbase.master.port is ignored in processing of hbase.masters (#4952)

Signed-off-by: Duo Zhang <zhangduo@apache.org>
Signed-off-by: Reid Chan <reidchan@apache.org>
This commit is contained in:
Andrew Purtell 2023-01-17 13:20:03 -08:00
parent 9f81c78a1c
commit e124b31815
2 changed files with 36 additions and 4 deletions

View File

@ -71,18 +71,32 @@ public class MasterRegistry extends AbstractRpcBasedConnectionRegistry {
private static final String MASTER_ADDRS_CONF_SEPARATOR = ","; 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 * 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. * separated host[:port] values. If no port number if specified, default master port is assumed.
* @param conf Configuration to parse from. * @param conf Configuration to parse from.
*/ */
public static Set<ServerName> parseMasterAddrs(Configuration conf) throws UnknownHostException { public static Set<ServerName> parseMasterAddrs(Configuration conf) throws UnknownHostException {
Set<ServerName> masterAddrs = new HashSet<>(); final int defaultPort = getDefaultMasterPort(conf);
String configuredMasters = getMasterAddr(conf); final Set<ServerName> masterAddrs = new HashSet<>();
final String configuredMasters = getMasterAddr(conf);
for (String masterAddr : Splitter.onPattern(MASTER_ADDRS_CONF_SEPARATOR) for (String masterAddr : Splitter.onPattern(MASTER_ADDRS_CONF_SEPARATOR)
.split(configuredMasters)) { .split(configuredMasters)) {
HostAndPort masterHostPort = final HostAndPort masterHostPort =
HostAndPort.fromString(masterAddr.trim()).withDefaultPort(HConstants.DEFAULT_MASTER_PORT); HostAndPort.fromString(masterAddr.trim()).withDefaultPort(defaultPort);
masterAddrs.add(ServerName.valueOf(masterHostPort.toString(), ServerName.NON_STARTCODE)); masterAddrs.add(ServerName.valueOf(masterHostPort.toString(), ServerName.NON_STARTCODE));
} }
Preconditions.checkArgument(!masterAddrs.isEmpty(), "At least one master address is needed"); Preconditions.checkArgument(!masterAddrs.isEmpty(), "At least one master address is needed");

View File

@ -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<ServerName> 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<ServerName> parsedMasters = new ArrayList<>(registry.getParsedServers());
ServerName sn = parsedMasters.get(0);
assertEquals(CUSTOM_MASTER_PORT, sn.getPort());
}
}
@Test @Test
public void testRegistryRPCs() throws Exception { public void testRegistryRPCs() throws Exception {
Configuration conf = new Configuration(TEST_UTIL.getConfiguration()); Configuration conf = new Configuration(TEST_UTIL.getConfiguration());