From dacd3404a46e8ffb628662d0416c5dd2eacd21d2 Mon Sep 17 00:00:00 2001 From: GeorryHuang <215175212@qq.com> Date: Wed, 25 Aug 2021 16:41:42 +0800 Subject: [PATCH] HBASE-26215 The backup master status page should use ActiveMasterManager instead of MasterAddressTracker (#3621) Signed-off-by: Duo Zhang --- .../tmpl/master/BackupMasterStatusTmpl.jamon | 20 +++------- .../hbase/master/ActiveMasterManager.java | 18 +++++++++ .../apache/hadoop/hbase/master/HMaster.java | 15 ++++++++ .../master/http/TestMasterStatusServlet.java | 4 ++ .../hbase/zookeeper/MasterAddressTracker.java | 37 +++++++++++++++++++ 5 files changed, 80 insertions(+), 14 deletions(-) diff --git a/hbase-server/src/main/jamon/org/apache/hadoop/hbase/tmpl/master/BackupMasterStatusTmpl.jamon b/hbase-server/src/main/jamon/org/apache/hadoop/hbase/tmpl/master/BackupMasterStatusTmpl.jamon index a49a5fae2b4..190f1dd857f 100644 --- a/hbase-server/src/main/jamon/org/apache/hadoop/hbase/tmpl/master/BackupMasterStatusTmpl.jamon +++ b/hbase-server/src/main/jamon/org/apache/hadoop/hbase/tmpl/master/BackupMasterStatusTmpl.jamon @@ -24,25 +24,19 @@ java.util.*; org.apache.hadoop.hbase.ServerName; org.apache.hadoop.hbase.ClusterMetrics; org.apache.hadoop.hbase.master.HMaster; -org.apache.hadoop.hbase.zookeeper.MasterAddressTracker; -<%java> -MasterAddressTracker masterAddressTracker = master.getMasterAddressTracker(); - - <%if (!master.isActiveMaster()) %> <%java> - ServerName active_master = - (masterAddressTracker == null) ? null : masterAddressTracker.getMasterAddress(); - assert active_master != null : "Failed to retrieve master's ServerName!"; - int infoPort = (masterAddressTracker == null) ? 0 : masterAddressTracker.getMasterInfoPort(); + ServerName active_master = master.getActiveMaster().orElse(null); + assert active_master != null : "Failed to retrieve active master's ServerName!"; + int activeInfoPort = active_master == null ? 0 : master.getActiveMasterInfoPort();
-

Current Active Master: Current Active Master: <% active_master.getHostname() %>

<%else>

Backup Masters

@@ -54,13 +48,11 @@ MasterAddressTracker masterAddressTracker = master.getMasterAddressTracker(); Start Time <%java> - Collection backup_masters = master.getClusterMetricsWithoutCoprocessor( - EnumSet.of(ClusterMetrics.Option.BACKUP_MASTERS)).getBackupMasterNames(); + Collection backup_masters = master.getBackupMasters(); ServerName [] backupServerNames = backup_masters.toArray(new ServerName[backup_masters.size()]); Arrays.sort(backupServerNames); for (ServerName serverName : backupServerNames) { - int infoPort = (masterAddressTracker == null) ? 0 : masterAddressTracker - .getBackupMasterInfoPort(serverName); + int infoPort = master.getBackupMasterInfoPort(serverName); getRegionServers() { return serverManager.getOnlineServersList(); diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/http/TestMasterStatusServlet.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/http/TestMasterStatusServlet.java index 3c9d6c20834..0ad9fbc783a 100644 --- a/hbase-server/src/test/java/org/apache/hadoop/hbase/master/http/TestMasterStatusServlet.java +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/master/http/TestMasterStatusServlet.java @@ -22,6 +22,7 @@ import java.io.StringWriter; import java.util.ArrayList; import java.util.HashSet; import java.util.List; +import java.util.Optional; import java.util.Set; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HBaseClassTestRule; @@ -113,6 +114,9 @@ public class TestMasterStatusServlet { Mockito.doReturn(tracker).when(master).getMasterAddressTracker(); Mockito.doReturn(FAKE_HOST).when(tracker).getMasterAddress(); + // Fake ActiveMaster + Mockito.doReturn(Optional.of(FAKE_HOST)).when(master).getActiveMaster(); + MetricsRegionServer rms = Mockito.mock(MetricsRegionServer.class); Mockito.doReturn(new MetricsRegionServerWrapperStub()).when(rms).getRegionServerWrapper(); Mockito.doReturn(rms).when(master).getMetrics(); diff --git a/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/MasterAddressTracker.java b/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/MasterAddressTracker.java index 54385f81f75..34b5cdf1100 100644 --- a/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/MasterAddressTracker.java +++ b/hbase-zookeeper/src/main/java/org/apache/hadoop/hbase/zookeeper/MasterAddressTracker.java @@ -222,6 +222,43 @@ public class MasterAddressTracker extends ZKNodeTracker { } } + /** + * Get backup master info port. + * Use this instead of {@link #getBackupMasterInfoPort(ServerName)} if you do not have an + * instance of this tracker in your context. + * + * @param zkw ZKWatcher to use + * @param sn ServerName of the backup master + * @return backup master info port in the the master address znode or 0 if no + * znode present. + * @throws KeeperException if a ZooKeeper operation fails + * @throws IOException if the address of the ZooKeeper master cannot be retrieved + */ + public static int getBackupMasterInfoPort(ZKWatcher zkw, final ServerName sn) + throws KeeperException, IOException { + byte[] data; + try { + data = ZKUtil.getData(zkw, + ZNodePaths.joinZNode(zkw.getZNodePaths().backupMasterAddressesZNode, sn.toString())); + } catch (InterruptedException e) { + throw new InterruptedIOException(); + } + if (data == null) { + throw new IOException("Can't get backup master address from ZooKeeper; znode data == null"); + } + try { + final ZooKeeperProtos.Master backup = parse(data); + if (backup == null) { + return 0; + } + return backup.getInfoPort(); + } catch (DeserializationException e) { + KeeperException ke = new KeeperException.DataInconsistencyException(); + ke.initCause(e); + throw ke; + } + } + /** * Set master address into the master znode or into the backup * subdirectory of backup masters; switch off the passed in znode