You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by ap...@apache.org on 2023/01/17 22:08:34 UTC

[hbase] branch branch-2 updated: HBASE-27561 hbase.master.port is ignored in processing of hbase.masters (#4952)

This is an automated email from the ASF dual-hosted git repository.

apurtell pushed a commit to branch branch-2
in repository https://gitbox.apache.org/repos/asf/hbase.git


The following commit(s) were added to refs/heads/branch-2 by this push:
     new 28da1546e23 HBASE-27561 hbase.master.port is ignored in processing of hbase.masters (#4952)
28da1546e23 is described below

commit 28da1546e2329f62717f94dd107dc2043b6c694a
Author: Andrew Purtell <ap...@apache.org>
AuthorDate: Tue Jan 17 13:20:03 2023 -0800

    HBASE-27561 hbase.master.port is ignored in processing of hbase.masters (#4952)
    
    Signed-off-by: Duo Zhang <zh...@apache.org>
    Signed-off-by: Reid Chan <re...@apache.org>
---
 .../apache/hadoop/hbase/client/MasterRegistry.java | 22 ++++++++++++++++++----
 .../hadoop/hbase/client/TestMasterRegistry.java    | 18 ++++++++++++++++++
 2 files changed, 36 insertions(+), 4 deletions(-)

diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/MasterRegistry.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/MasterRegistry.java
index 28ac5bb4528..b6f81c30f0b 100644
--- a/hbase-client/src/main/java/org/apache/hadoop/hbase/client/MasterRegistry.java
+++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/client/MasterRegistry.java
@@ -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");
diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestMasterRegistry.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestMasterRegistry.java
index 6301b053739..9fcc9ea88cc 100644
--- a/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestMasterRegistry.java
+++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestMasterRegistry.java
@@ -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());