Skip to content

Commit e124b31

Browse files
committed
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>
1 parent 9f81c78 commit e124b31

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
@@ -71,18 +71,32 @@ public class MasterRegistry extends AbstractRpcBasedConnectionRegistry {
7171

7272
private static final String MASTER_ADDRS_CONF_SEPARATOR = ",";
7373

74+
/**
75+
* Supplies the default master port we should use given the provided configuration.
76+
* @param conf Configuration to parse from.
77+
*/
78+
private static int getDefaultMasterPort(Configuration conf) {
79+
final int port = conf.getInt(HConstants.MASTER_PORT, HConstants.DEFAULT_MASTER_PORT);
80+
if (port == 0) {
81+
// Master port may be set to 0. We should substitute the default port in that case.
82+
return HConstants.DEFAULT_MASTER_PORT;
83+
}
84+
return port;
85+
}
86+
7487
/**
7588
* Parses the list of master addresses from the provided configuration. Supported format is comma
7689
* separated host[:port] values. If no port number if specified, default master port is assumed.
7790
* @param conf Configuration to parse from.
7891
*/
7992
public static Set<ServerName> parseMasterAddrs(Configuration conf) throws UnknownHostException {
80-
Set<ServerName> masterAddrs = new HashSet<>();
81-
String configuredMasters = getMasterAddr(conf);
93+
final int defaultPort = getDefaultMasterPort(conf);
94+
final Set<ServerName> masterAddrs = new HashSet<>();
95+
final String configuredMasters = getMasterAddr(conf);
8296
for (String masterAddr : Splitter.onPattern(MASTER_ADDRS_CONF_SEPARATOR)
8397
.split(configuredMasters)) {
84-
HostAndPort masterHostPort =
85-
HostAndPort.fromString(masterAddr.trim()).withDefaultPort(HConstants.DEFAULT_MASTER_PORT);
98+
final HostAndPort masterHostPort =
99+
HostAndPort.fromString(masterAddr.trim()).withDefaultPort(defaultPort);
86100
masterAddrs.add(ServerName.valueOf(masterHostPort.toString(), ServerName.NON_STARTCODE));
87101
}
88102
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.getParsedServers());
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.getParsedServers());
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)