Search in sources :

Example 1 with LAST_LEADER_ELECTION_ELAPSED_TIME

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);
}
Also used : LEADER_ELECTION_TIMEOUT_COUNT_METRIC(org.apache.ratis.server.metrics.LeaderElectionMetrics.LEADER_ELECTION_TIMEOUT_COUNT_METRIC) RaftGroupMemberId(org.apache.ratis.protocol.RaftGroupMemberId) TransferLeadershipException(org.apache.ratis.protocol.exceptions.TransferLeadershipException) CompletableFuture(java.util.concurrent.CompletableFuture) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) Log4jUtils(org.apache.ratis.util.Log4jUtils) Level(org.apache.log4j.Level) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Assert.fail(org.junit.Assert.fail) JavaUtils(org.apache.ratis.util.JavaUtils) RaftTestUtil.waitForLeader(org.apache.ratis.RaftTestUtil.waitForLeader) Timestamp(org.apache.ratis.util.Timestamp) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) RaftPeer(org.apache.ratis.protocol.RaftPeer) Assert.assertNotNull(org.junit.Assert.assertNotNull) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) IOException(java.io.IOException) Mockito.when(org.mockito.Mockito.when) BaseTest(org.apache.ratis.BaseTest) RatisMetricRegistry(org.apache.ratis.metrics.RatisMetricRegistry) RaftTestUtil(org.apache.ratis.RaftTestUtil) LeaderElectionMetrics(org.apache.ratis.server.metrics.LeaderElectionMetrics) TimeUnit(java.util.concurrent.TimeUnit) LEADER_ELECTION_TIME_TAKEN(org.apache.ratis.server.metrics.LeaderElectionMetrics.LEADER_ELECTION_TIME_TAKEN) List(java.util.List) LeaderSteppingDownException(org.apache.ratis.protocol.exceptions.LeaderSteppingDownException) LEADER_ELECTION_COUNT_METRIC(org.apache.ratis.server.metrics.LeaderElectionMetrics.LEADER_ELECTION_COUNT_METRIC) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftProperties(org.apache.ratis.conf.RaftProperties) DivisionInfo(org.apache.ratis.server.DivisionInfo) ExitUtils(org.apache.ratis.util.ExitUtils) LifeCycle(org.apache.ratis.util.LifeCycle) Optional(java.util.Optional) Timer(com.codahale.metrics.Timer) LAST_LEADER_ELECTION_ELAPSED_TIME(org.apache.ratis.server.metrics.LeaderElectionMetrics.LAST_LEADER_ELECTION_ELAPSED_TIME) RaftServer(org.apache.ratis.server.RaftServer) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) SegmentedRaftLogTestUtils(org.apache.ratis.server.raftlog.segmented.SegmentedRaftLogTestUtils) Assert.assertEquals(org.junit.Assert.assertEquals) TimeDuration(org.apache.ratis.util.TimeDuration) Mockito.mock(org.mockito.Mockito.mock) Timer(com.codahale.metrics.Timer) RaftServer(org.apache.ratis.server.RaftServer) Timestamp(org.apache.ratis.util.Timestamp) RatisMetricRegistry(org.apache.ratis.metrics.RatisMetricRegistry) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

Timer (com.codahale.metrics.Timer)1 IOException (java.io.IOException)1 Iterator (java.util.Iterator)1 List (java.util.List)1 Optional (java.util.Optional)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 ThreadLocalRandom (java.util.concurrent.ThreadLocalRandom)1 TimeUnit (java.util.concurrent.TimeUnit)1 Level (org.apache.log4j.Level)1 BaseTest (org.apache.ratis.BaseTest)1 RaftTestUtil (org.apache.ratis.RaftTestUtil)1 RaftTestUtil.waitForLeader (org.apache.ratis.RaftTestUtil.waitForLeader)1 RaftClient (org.apache.ratis.client.RaftClient)1 RaftProperties (org.apache.ratis.conf.RaftProperties)1 RatisMetricRegistry (org.apache.ratis.metrics.RatisMetricRegistry)1 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)1 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)1 RaftGroupMemberId (org.apache.ratis.protocol.RaftGroupMemberId)1 RaftPeer (org.apache.ratis.protocol.RaftPeer)1 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)1