Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,24 @@ public void testMasterAddressParsing() throws IOException {
}
}

@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());
Expand Down