Search in sources :

Example 1 with RaftServerMetricsImpl

use of org.apache.ratis.server.metrics.RaftServerMetricsImpl in project incubator-ratis by apache.

the class RaftSnapshotBaseTest method verifyInstallSnapshotMetric.

protected void verifyInstallSnapshotMetric(RaftServer.Division leader) {
    final Counter installSnapshotCounter = ((RaftServerMetricsImpl) leader.getRaftServerMetrics()).getCounter(RATIS_SERVER_INSTALL_SNAPSHOT_COUNT);
    Assert.assertNotNull(installSnapshotCounter);
    Assert.assertTrue(installSnapshotCounter.getCount() >= 1);
}
Also used : Counter(com.codahale.metrics.Counter) RaftServerMetricsImpl(org.apache.ratis.server.metrics.RaftServerMetricsImpl)

Example 2 with RaftServerMetricsImpl

use of org.apache.ratis.server.metrics.RaftServerMetricsImpl in project incubator-ratis by apache.

the class TestRaftServerSlownessDetection method testSlownessDetection.

@Test
public void testSlownessDetection() throws Exception {
    RaftServer.Division leaderServer = RaftTestUtil.waitForLeader(cluster);
    long slownessTimeout = RaftServerConfigKeys.Rpc.slownessTimeout(cluster.getProperties()).toIntExact(TimeUnit.MILLISECONDS);
    RaftServer.Division failedFollower = cluster.getFollowers().get(0);
    final RatisMetricRegistry ratisMetricRegistry = ((RaftServerMetricsImpl) leaderServer.getRaftServerMetrics()).getRegistry();
    SortedMap<String, Gauge> heartbeatElapsedTimeGauges = ratisMetricRegistry.getGauges((s, metric) -> s.contains("lastHeartbeatElapsedTime"));
    String followerId = failedFollower.getId().toString();
    Gauge metric = heartbeatElapsedTimeGauges.entrySet().parallelStream().filter(e -> e.getKey().contains(followerId)).iterator().next().getValue();
    long followerHeartBeatElapsedMetric = (long) metric.getValue();
    // fail the node and wait for the callback to be triggered
    cluster.killServer(failedFollower.getId());
    Thread.sleep(slownessTimeout * 2);
    long followerHeartBeatElapsedMetricNew = (long) metric.getValue();
    Assert.assertTrue(followerHeartBeatElapsedMetricNew > followerHeartBeatElapsedMetric);
    // Followers should not get any failed not notification
    for (RaftServer.Division followerServer : cluster.getFollowers()) {
        Assert.assertNull(SimpleStateMachine4Testing.get(followerServer).getSlownessInfo());
    }
    // the leader should get notification that the follower has failed now
    RaftProtos.RoleInfoProto roleInfoProto = SimpleStateMachine4Testing.get(cluster.getLeader()).getSlownessInfo();
    Assert.assertNotNull(roleInfoProto);
    List<RaftProtos.ServerRpcProto> followers = roleInfoProto.getLeaderInfo().getFollowerInfoList();
    // Assert that the node shutdown is lagging behind
    for (RaftProtos.ServerRpcProto serverProto : followers) {
        if (RaftPeerId.valueOf(serverProto.getId().getId()).equals(failedFollower.getId())) {
            Assert.assertTrue(serverProto.getLastRpcElapsedTimeMs() > slownessTimeout);
        }
    }
}
Also used : Log4jUtils(org.apache.ratis.util.Log4jUtils) RaftServerTestUtil(org.apache.ratis.server.impl.RaftServerTestUtil) Level(org.apache.log4j.Level) After(org.junit.After) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) RaftServerMetricsImpl(org.apache.ratis.server.metrics.RaftServerMetricsImpl) MiniRaftClusterWithSimulatedRpc(org.apache.ratis.server.simulation.MiniRaftClusterWithSimulatedRpc) Before(org.junit.Before) StateMachine(org.apache.ratis.statemachine.StateMachine) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) RaftProtos(org.apache.ratis.proto.RaftProtos) Test(org.junit.Test) IOException(java.io.IOException) RatisMetricRegistry(org.apache.ratis.metrics.RatisMetricRegistry) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) Ignore(org.junit.Ignore) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftServer(org.apache.ratis.server.RaftServer) Gauge(com.codahale.metrics.Gauge) Assert(org.junit.Assert) SortedMap(java.util.SortedMap) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) TimeDuration(org.apache.ratis.util.TimeDuration) RaftServer(org.apache.ratis.server.RaftServer) RaftServerMetricsImpl(org.apache.ratis.server.metrics.RaftServerMetricsImpl) Gauge(com.codahale.metrics.Gauge) RaftProtos(org.apache.ratis.proto.RaftProtos) RatisMetricRegistry(org.apache.ratis.metrics.RatisMetricRegistry) Test(org.junit.Test)

Example 3 with RaftServerMetricsImpl

use of org.apache.ratis.server.metrics.RaftServerMetricsImpl in project incubator-ratis by apache.

the class TestRetryCacheMetrics method setUp.

@BeforeClass
public static void setUp() {
    RaftGroupId raftGroupId = RaftGroupId.randomId();
    RaftPeerId raftPeerId = RaftPeerId.valueOf("TestId");
    RaftGroupMemberId raftGroupMemberId = RaftGroupMemberId.valueOf(raftPeerId, raftGroupId);
    retryCache = new RetryCacheImpl(RaftServerConfigKeys.RetryCache.EXPIRY_TIME_DEFAULT, null);
    final RaftServerMetricsImpl raftServerMetrics = RaftServerMetricsImpl.computeIfAbsentRaftServerMetrics(raftGroupMemberId, () -> null, retryCache::getStatistics);
    ratisMetricRegistry = raftServerMetrics.getRegistry();
}
Also used : RaftGroupId(org.apache.ratis.protocol.RaftGroupId) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftGroupMemberId(org.apache.ratis.protocol.RaftGroupMemberId) RaftServerMetricsImpl(org.apache.ratis.server.metrics.RaftServerMetricsImpl) BeforeClass(org.junit.BeforeClass)

Example 4 with RaftServerMetricsImpl

use of org.apache.ratis.server.metrics.RaftServerMetricsImpl in project incubator-ratis by apache.

the class LogAppenderTests method testFollowerHeartbeatMetric.

@Test
public void testFollowerHeartbeatMetric() throws IOException, InterruptedException {
    // Start a 3 node Ratis ring.
    final MiniRaftCluster cluster = newCluster(3);
    cluster.start();
    final RaftServer.Division leaderServer = waitForLeader(cluster);
    // Write 10 messages to leader.
    try (RaftClient client = cluster.createClient(leaderServer.getId())) {
        for (int i = 1; i <= 10; i++) {
            client.io().send(new RaftTestUtil.SimpleMessage("Msg to make leader ready " + i));
        }
    } catch (IOException e) {
        throw e;
    }
    final RatisMetricRegistry ratisMetricRegistry = ((RaftServerMetricsImpl) leaderServer.getRaftServerMetrics()).getRegistry();
    // Get all last_heartbeat_elapsed_time metric gauges. Should be equal to number of followers.
    SortedMap<String, Gauge> heartbeatElapsedTimeGauges = ratisMetricRegistry.getGauges((s, metric) -> s.contains("lastHeartbeatElapsedTime"));
    assertTrue(heartbeatElapsedTimeGauges.size() == 2);
    for (RaftServer.Division followerServer : cluster.getFollowers()) {
        String followerId = followerServer.getId().toString();
        Gauge metric = heartbeatElapsedTimeGauges.entrySet().parallelStream().filter(e -> e.getKey().contains(followerId)).iterator().next().getValue();
        // Metric for this follower exists.
        assertTrue(metric != null);
        // Metric in nanos > 0.
        assertTrue((long) metric.getValue() > 0);
        // Try to get Heartbeat metrics for follower.
        final RaftServerMetricsImpl followerMetrics = (RaftServerMetricsImpl) followerServer.getRaftServerMetrics();
        // Metric should not exist. It only exists in leader.
        assertTrue(followerMetrics.getRegistry().getGauges((s, m) -> s.contains("lastHeartbeatElapsedTime")).isEmpty());
        for (boolean heartbeat : new boolean[] { true, false }) {
            assertTrue(followerMetrics.getFollowerAppendEntryTimer(heartbeat).getMeanRate() > 0.0d);
            assertTrue(followerMetrics.getFollowerAppendEntryTimer(heartbeat).getCount() > 0L);
        }
    }
}
Also used : LogProtoUtils(org.apache.ratis.server.raftlog.LogProtoUtils) Arrays(java.util.Arrays) RaftLog(org.apache.ratis.server.raftlog.RaftLog) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) Log4jUtils(org.apache.ratis.util.Log4jUtils) Level(org.apache.log4j.Level) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) RaftServerMetricsImpl(org.apache.ratis.server.metrics.RaftServerMetricsImpl) JavaUtils(org.apache.ratis.util.JavaUtils) SizeInBytes(org.apache.ratis.util.SizeInBytes) RaftTestUtil.waitForLeader(org.apache.ratis.RaftTestUtil.waitForLeader) StateMachine(org.apache.ratis.statemachine.StateMachine) EnumMap(java.util.EnumMap) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) 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) RatisMetricRegistry(org.apache.ratis.metrics.RatisMetricRegistry) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) LogEntryBodyCase(org.apache.ratis.proto.RaftProtos.LogEntryProto.LogEntryBodyCase) LogAppender(org.apache.ratis.server.leader.LogAppender) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftServer(org.apache.ratis.server.RaftServer) Gauge(com.codahale.metrics.Gauge) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) SortedMap(java.util.SortedMap) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) IOException(java.io.IOException) RaftServerMetricsImpl(org.apache.ratis.server.metrics.RaftServerMetricsImpl) Gauge(com.codahale.metrics.Gauge) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) RatisMetricRegistry(org.apache.ratis.metrics.RatisMetricRegistry) RaftClient(org.apache.ratis.client.RaftClient) Test(org.junit.Test)

Example 5 with RaftServerMetricsImpl

use of org.apache.ratis.server.metrics.RaftServerMetricsImpl in project incubator-ratis by apache.

the class TestRatisServerMetricsBase method runTestClientFailedRequest.

void runTestClientFailedRequest(CLUSTER cluster) throws InterruptedException, IOException, ExecutionException {
    final RaftServer.Division leaderImpl = RaftTestUtil.waitForLeader(cluster);
    ClientId clientId = ClientId.randomId();
    // StaleRead with Long.MAX_VALUE minIndex will fail.
    RaftClientRequest r = RaftClientRequest.newBuilder().setClientId(clientId).setLeaderId(leaderImpl.getId()).setGroupId(cluster.getGroupId()).setCallId(0).setMessage(Message.EMPTY).setType(RaftClientRequest.staleReadRequestType(Long.MAX_VALUE)).build();
    final CompletableFuture<RaftClientReply> f = leaderImpl.getRaftServer().submitClientRequestAsync(r);
    Assert.assertTrue(!f.get().isSuccess());
    assertEquals(1L, ((RaftServerMetricsImpl) leaderImpl.getRaftServerMetrics()).getCounter(RATIS_SERVER_FAILED_CLIENT_STALE_READ_COUNT).getCount());
}
Also used : RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftServer(org.apache.ratis.server.RaftServer) ClientId(org.apache.ratis.protocol.ClientId) RaftServerMetricsImpl(org.apache.ratis.server.metrics.RaftServerMetricsImpl)

Aggregations

RaftServerMetricsImpl (org.apache.ratis.server.metrics.RaftServerMetricsImpl)6 RaftServer (org.apache.ratis.server.RaftServer)4 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)3 Gauge (com.codahale.metrics.Gauge)2 IOException (java.io.IOException)2 List (java.util.List)2 SortedMap (java.util.SortedMap)2 Level (org.apache.log4j.Level)2 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)2 RaftClient (org.apache.ratis.client.RaftClient)2 RaftProperties (org.apache.ratis.conf.RaftProperties)2 RatisMetricRegistry (org.apache.ratis.metrics.RatisMetricRegistry)2 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)2 RaftServerConfigKeys (org.apache.ratis.server.RaftServerConfigKeys)2 MiniRaftCluster (org.apache.ratis.server.impl.MiniRaftCluster)2 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)2 StateMachine (org.apache.ratis.statemachine.StateMachine)2 Log4jUtils (org.apache.ratis.util.Log4jUtils)2 Assert (org.junit.Assert)2 Test (org.junit.Test)2