@Test
  public void testSimpleMasterFailover() throws Exception {
    final int NUM_MASTERS = 3;
    final int NUM_RS = 3;

    // Start the cluster
    HBaseTestingUtil TEST_UTIL = new HBaseTestingUtil();
    try {
      StartTestingClusterOption option = StartTestingClusterOption.builder().numMasters(NUM_MASTERS)
        .numRegionServers(NUM_RS).numDataNodes(NUM_RS).build();
      TEST_UTIL.startMiniCluster(option);
      SingleProcessHBaseCluster cluster = TEST_UTIL.getHBaseCluster();

      // get all the master threads
      List<MasterThread> masterThreads = cluster.getMasterThreads();

      // wait for each to come online
      for (MasterThread mt : masterThreads) {
        assertTrue(mt.isAlive());
      }

      // verify only one is the active master and we have right number
      int numActive = 0;
      int activeIndex = -1;
      ServerName activeName = null;
      HMaster active = null;
      for (int i = 0; i < masterThreads.size(); i++) {
        if (masterThreads.get(i).getMaster().isActiveMaster()) {
          numActive++;
          activeIndex = i;
          active = masterThreads.get(activeIndex).getMaster();
          activeName = active.getServerName();
        }
      }
      assertEquals(1, numActive);
      assertEquals(NUM_MASTERS, masterThreads.size());
      LOG.info("Active master " + activeName);

      // Check that ClusterStatus reports the correct active and backup masters
      assertNotNull(active);
      ClusterMetrics status = active.getClusterMetrics();
      assertEquals(activeName, status.getMasterName());
      assertEquals(2, status.getBackupMasterNames().size());

      // attempt to stop one of the inactive masters
      int backupIndex = (activeIndex == 0 ? 1 : activeIndex - 1);
      HMaster master = cluster.getMaster(backupIndex);
      LOG.debug("\n\nStopping a backup master: " + master.getServerName() + "\n");
      cluster.stopMaster(backupIndex, false);
      cluster.waitOnMaster(backupIndex);

      // Verify still one active master and it's the same
      for (int i = 0; i < masterThreads.size(); i++) {
        if (masterThreads.get(i).getMaster().isActiveMaster()) {
          assertEquals(activeName, masterThreads.get(i).getMaster().getServerName());
          activeIndex = i;
          active = masterThreads.get(activeIndex).getMaster();
        }
      }
      assertEquals(1, numActive);
      assertEquals(2, masterThreads.size());
      int rsCount = masterThreads.get(activeIndex).getMaster().getClusterMetrics()
        .getLiveServerMetrics().size();
      LOG.info(
        "Active master " + active.getServerName() + " managing " + rsCount + " regions servers");
      assertEquals(3, rsCount);

      // wait for the active master to acknowledge loss of the backup from ZK
      final HMaster activeFinal = active;
      TEST_UTIL.waitFor(TimeUnit.MINUTES.toMillis(5),
        () -> activeFinal.getBackupMasters().size() == 1);

      // Check that ClusterStatus reports the correct active and backup masters
      assertNotNull(active);
      status = active.getClusterMetrics();
      assertEquals(activeName, status.getMasterName());
      assertEquals(1, status.getBackupMasterNames().size());

      // kill the active master
      LOG.debug("\n\nStopping the active master " + active.getServerName() + "\n");
      cluster.stopMaster(activeIndex, false);
      cluster.waitOnMaster(activeIndex);

      // wait for an active master to show up and be ready
      assertTrue(cluster.waitForActiveAndReadyMaster());

      LOG.debug("\n\nVerifying backup master is now active\n");
      // should only have one master now
      assertEquals(1, masterThreads.size());

      // and he should be active
      active = masterThreads.get(0).getMaster();
      assertNotNull(active);
      status = active.getClusterMetrics();
      ServerName masterName = status.getMasterName();
      assertNotNull(masterName);
      assertEquals(active.getServerName(), masterName);
      assertTrue(active.isActiveMaster());
      assertEquals(0, status.getBackupMasterNames().size());
      int rss = status.getLiveServerMetrics().size();
      LOG.info("Active master {} managing {} region servers", masterName.getServerName(), rss);
      assertEquals(3, rss);
    } finally {
      // Stop the cluster
      TEST_UTIL.shutdownMiniCluster();
    }
  }
Simple test of master failover.
<p>
Starts with three masters. Kills a backup master. Then kills the active master. Ensures the
final master becomes active and we can still contact the cluster.
hbase-server/src/test/java/org/apache/hadoop/hbase/master/TestMasterFailover.java