use of org.apache.ratis.server.metrics.LeaderElectionMetrics.LAST_LEADER_ELECTION_ELAPSED_TIME in project incubator-ratis by apache.
the class LeaderElectionTests method testLeaderElectionMetrics.
@Test
public void testLeaderElectionMetrics() throws IOException, InterruptedException {
Timestamp timestamp = Timestamp.currentTime();
final MiniRaftCluster cluster = newCluster(3);
cluster.start();
final RaftServer.Division leaderServer = waitForLeader(cluster);
final RatisMetricRegistry ratisMetricRegistry = LeaderElectionMetrics.getMetricRegistryForLeaderElection(leaderServer.getMemberId());
// Verify each metric individually.
long numLeaderElections = ratisMetricRegistry.counter(LEADER_ELECTION_COUNT_METRIC).getCount();
assertTrue(numLeaderElections > 0);
long numLeaderElectionTimeout = ratisMetricRegistry.counter(LEADER_ELECTION_TIMEOUT_COUNT_METRIC).getCount();
assertTrue(numLeaderElectionTimeout > 0);
Timer timer = ratisMetricRegistry.timer(LEADER_ELECTION_TIME_TAKEN);
double meanTimeNs = timer.getSnapshot().getMean();
long elapsedNs = timestamp.elapsedTime().toLong(TimeUnit.NANOSECONDS);
assertTrue(timer.getCount() > 0 && meanTimeNs < elapsedNs);
Long leaderElectionLatency = (Long) ratisMetricRegistry.getGauges((s, metric) -> s.contains(LAST_LEADER_ELECTION_ELAPSED_TIME)).values().iterator().next().getValue();
assertTrue(leaderElectionLatency > 0L);
}
Aggregations