Skip to content

Commit 0a88314

Browse files
committed
HBASE-27561 hbase.master.port is ignored in processing of hbase.masters (#4952)
Signed-off-by: Duo Zhang <[email protected]> Signed-off-by: Reid Chan <[email protected]> Conflicts: hbase-client/src/main/java/org/apache/hadoop/hbase/client/MasterRegistry.java
1 parent 181ffe7 commit 0a88314

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

hbase-client/src/main/java/org/apache/hadoop/hbase/client/MasterRegistry.java

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,17 +100,31 @@ public class MasterRegistry implements ConnectionRegistry {
100100

101101
protected final MasterAddressRefresher masterAddressRefresher;
102102

103+
/**
104+
* Supplies the default master port we should use given the provided configuration.
105+
* @param conf Configuration to parse from.
106+
*/
107+
private static int getDefaultMasterPort(Configuration conf) {
108+
final int port = conf.getInt(HConstants.MASTER_PORT, HConstants.DEFAULT_MASTER_PORT);
109+
if (port == 0) {
110+
// Master port may be set to 0. We should substitute the default port in that case.
111+
return HConstants.DEFAULT_MASTER_PORT;
112+
}
113+
return port;
114+
}
115+
103116
/**
104117
* Parses the list of master addresses from the provided configuration. Supported format is comma
105118
* separated host[:port] values. If no port number if specified, default master port is assumed.
106119
* @param conf Configuration to parse from.
107120
*/
108121
private static Set<ServerName> parseMasterAddrs(Configuration conf) throws UnknownHostException {
109-
Set<ServerName> masterAddrs = new HashSet<>();
110-
String configuredMasters = getMasterAddr(conf);
122+
final int defaultPort = getDefaultMasterPort(conf);
123+
final Set<ServerName> masterAddrs = new HashSet<>();
124+
final String configuredMasters = getMasterAddr(conf);
111125
for (String masterAddr : configuredMasters.split(MASTER_ADDRS_CONF_SEPARATOR)) {
112-
HostAndPort masterHostPort =
113-
HostAndPort.fromString(masterAddr.trim()).withDefaultPort(HConstants.DEFAULT_MASTER_PORT);
126+
final HostAndPort masterHostPort =
127+
HostAndPort.fromString(masterAddr.trim()).withDefaultPort(defaultPort);
114128
masterAddrs.add(ServerName.valueOf(masterHostPort.toString(), ServerName.NON_STARTCODE));
115129
}
116130
Preconditions.checkArgument(!masterAddrs.isEmpty(), "At least one master address is needed");

hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestMasterRegistry.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,24 @@ public void testMasterAddressParsing() throws IOException {
108108
}
109109
}
110110

111+
@Test
112+
public void testMasterPortDefaults() throws IOException {
113+
Configuration conf = new Configuration(TEST_UTIL.getConfiguration());
114+
conf.set(HConstants.MASTER_ADDRS_KEY, "localhost");
115+
try (MasterRegistry registry = new MasterRegistry(conf)) {
116+
List<ServerName> parsedMasters = new ArrayList<>(registry.getParsedMasterServers());
117+
ServerName sn = parsedMasters.get(0);
118+
assertEquals(HConstants.DEFAULT_MASTER_PORT, sn.getPort());
119+
}
120+
final int CUSTOM_MASTER_PORT = 9999;
121+
conf.setInt(HConstants.MASTER_PORT, CUSTOM_MASTER_PORT);
122+
try (MasterRegistry registry = new MasterRegistry(conf)) {
123+
List<ServerName> parsedMasters = new ArrayList<>(registry.getParsedMasterServers());
124+
ServerName sn = parsedMasters.get(0);
125+
assertEquals(CUSTOM_MASTER_PORT, sn.getPort());
126+
}
127+
}
128+
111129
@Test
112130
public void testRegistryRPCs() throws Exception {
113131
Configuration conf = new Configuration(TEST_UTIL.getConfiguration());

0 commit comments

Comments
 (0)