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 committed by GitHub
parent da261344cc
commit 71d0862d84
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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 = ",";
/**
* 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<ServerName> parseMasterAddrs(Configuration conf) throws UnknownHostException {
Set<ServerName> masterAddrs = new HashSet<>();
String configuredMasters = getMasterAddr(conf);
final int defaultPort = getDefaultMasterPort(conf);
final Set<ServerName> 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");

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
public void testRegistryRPCs() throws Exception {
Configuration conf = new Configuration(TEST_UTIL.getConfiguration());