Search in sources :

Example 11 with Timestamp

use of org.apache.ratis.util.Timestamp 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

Timestamp (org.apache.ratis.util.Timestamp)11 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)5 IOException (java.io.IOException)4 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)4 TimeDuration (org.apache.ratis.util.TimeDuration)4 ArrayList (java.util.ArrayList)3 RaftClient (org.apache.ratis.client.RaftClient)3 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)3 HashMap (java.util.HashMap)2 List (java.util.List)2 Optional (java.util.Optional)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 TimeUnit (java.util.concurrent.TimeUnit)2 RaftProperties (org.apache.ratis.conf.RaftProperties)2 RaftPeer (org.apache.ratis.protocol.RaftPeer)2 Timer (com.codahale.metrics.Timer)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1 Collections (java.util.Collections)1 Iterator (java.util.Iterator)1