HBASE-26215 The backup master status page should use ActiveMasterManager instead of MasterAddressTracker (#3621)

Signed-off-by: Duo Zhang <zhangduo@apache.org>
This commit is contained in:
GeorryHuang 2021-08-25 16:41:42 +08:00 committed by GitHub
parent 2ce2f9336f
commit 4b0a64033a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 80 additions and 14 deletions

View File

@ -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;
</%import>
<%java>
MasterAddressTracker masterAddressTracker = master.getMasterAddressTracker();
</%java>
<%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();
</%java>
<div class="row inner_header">
<div class="page-header">
<h1>Backup Master <small><% master.getServerName().getHostname() %></small></h1>
</div>
</div>
<h4>Current Active Master: <a href="//<% active_master.getHostname() %>:<% infoPort %>/master-status"
<h4>Current Active Master: <a href="//<% active_master.getHostname() %>:<% activeInfoPort %>/master-status"
target="_blank"><% active_master.getHostname() %></a><h4>
<%else>
<h2>Backup Masters</h2>
@ -54,13 +48,11 @@ MasterAddressTracker masterAddressTracker = master.getMasterAddressTracker();
<th>Start Time</th>
</tr>
<%java>
Collection<ServerName> backup_masters = master.getClusterMetricsWithoutCoprocessor(
EnumSet.of(ClusterMetrics.Option.BACKUP_MASTERS)).getBackupMasterNames();
Collection<ServerName> 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);
</%java>
<tr>
<td><a href="//<% serverName.getHostname() %>:<% infoPort %>/master-status"

View File

@ -158,6 +158,24 @@ public class ActiveMasterManager extends ZKListener {
return Optional.ofNullable(activeMasterServerName);
}
public int getActiveMasterInfoPort() {
try {
return MasterAddressTracker.getMasterInfoPort(watcher);
} catch (Exception e) {
LOG.warn("Failed to get active master's info port.", e);
return 0;
}
}
public int getBackupMasterInfoPort(final ServerName sn) {
try {
return MasterAddressTracker.getBackupMasterInfoPort(watcher, sn);
} catch (Exception e) {
LOG.warn("Failed to get backup master: " + sn + "'s info port.", e);
return 0;
}
}
/**
* Handle a change in the master node. Doesn't matter whether this was called
* from a nodeCreated or nodeDeleted event because there are no guarantees

View File

@ -2710,6 +2710,21 @@ public class HMaster extends HRegionServer implements MasterServices {
return activeMasterManager.getBackupMasters();
}
/**
* @return info port of active master or 0 if any exception occurs.
*/
public int getActiveMasterInfoPort() {
return activeMasterManager.getActiveMasterInfoPort();
}
/**
* @param sn is ServerName of the backup master
* @return info port of backup master or 0 if any exception occurs.
*/
public int getBackupMasterInfoPort(final ServerName sn) {
return activeMasterManager.getBackupMasterInfoPort(sn);
}
@Override
public List<ServerName> getRegionServers() {
return serverManager.getOnlineServersList();

View File

@ -23,6 +23,7 @@ import java.util.ArrayList;
import java.util.Arrays;
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;
@ -116,6 +117,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();

View File

@ -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 <code>master</code> znode or into the backup
* subdirectory of backup masters; switch off the passed in <code>znode</code>