@Test
  public void testRemovePeerMetricsCleanup() throws Exception {
    MetricsReplicationSourceSource globalSource = getGlobalSource();
    int globalLogQueueSizeInitial = globalSource.getSizeOfLogQueue();
    String peerId = "DummyPeer";
    addPeerAndWait(peerId, "hbase", false);
    // there is no latestPaths so the size of log queue should not change
    assertEquals(globalLogQueueSizeInitial, globalSource.getSizeOfLogQueue());

    ReplicationSourceInterface source = manager.getSource(peerId);
    // Sanity check
    assertNotNull(source);
    int sizeOfSingleLogQueue = source.getSourceMetrics().getSizeOfLogQueue();
    // Enqueue log and check if metrics updated
    Path serverLogDir = new Path(logDir, server.getServerName().toString());
    source.enqueueLog(new Path(serverLogDir, server.getServerName() + ".1"));
    assertEquals(1 + sizeOfSingleLogQueue, source.getSourceMetrics().getSizeOfLogQueue());
    assertEquals(source.getSourceMetrics().getSizeOfLogQueue() + globalLogQueueSizeInitial,
      globalSource.getSizeOfLogQueue());

    // Removing the peer should reset the global metrics
    removePeerAndWait(peerId);
    assertEquals(globalLogQueueSizeInitial, globalSource.getSizeOfLogQueue());

    // Adding the same peer back again should reset the single source metrics
    addPeerAndWait(peerId, "hbase", false);
    source = manager.getSource(peerId);
    assertNotNull(source);
    assertEquals(source.getSourceMetrics().getSizeOfLogQueue() + globalLogQueueSizeInitial,
      globalSource.getSizeOfLogQueue());
  }
Remove a peer and wait for it to get cleaned up
hbase-server/src/test/java/org/apache/hadoop/hbase/replication/regionserver/TestReplicationSourceManager.java