Search in sources :

Example 1 with RatisMetricRegistry

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

the class MetricRegistriesImpl method create.

@Override
public RatisMetricRegistry create(MetricRegistryInfo info) {
    return registries.put(info, () -> {
        if (reporterRegistrations.isEmpty()) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("First MetricRegistry has been created without registering reporters. " + "Hence registering JMX reporter by default.");
            }
            enableJmxReporter();
        }
        RatisMetricRegistry registry = factory.create(info);
        reporterRegistrations.forEach(reg -> reg.accept(registry));
        return registry;
    });
}
Also used : RatisMetricRegistry(org.apache.ratis.metrics.RatisMetricRegistry)

Example 2 with RatisMetricRegistry

use of org.apache.ratis.metrics.RatisMetricRegistry 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 RatisMetricRegistry

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

the class TestGrpcMessageMetrics method assertMessageCount.

static void assertMessageCount(RaftServer.Division server) {
    String serverId = server.getId().toString();
    GrpcService service = (GrpcService) RaftServerTestUtil.getServerRpc(server);
    RatisMetricRegistry registry = service.getServerInterceptor().getMetrics().getRegistry();
    String counter_prefix = serverId + "_" + "ratis.grpc.RaftServerProtocolService";
    Assert.assertTrue(registry.counter(counter_prefix + "_" + "requestVote" + "_OK_completed_total").getCount() > 0);
}
Also used : GrpcService(org.apache.ratis.grpc.server.GrpcService) RatisMetricRegistry(org.apache.ratis.metrics.RatisMetricRegistry)

Example 4 with RatisMetricRegistry

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

the class TestSegmentedRaftLog method testLoadLogSegments.

@Test
public void testLoadLogSegments() throws Exception {
    // first generate log files
    List<SegmentRange> ranges = prepareRanges(0, 5, 100, 0);
    LogEntryProto[] entries = prepareLog(ranges);
    // create RaftLog object and load log file
    try (SegmentedRaftLog raftLog = newSegmentedRaftLog()) {
        raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
        // check if log entries are loaded correctly
        for (LogEntryProto e : entries) {
            LogEntryProto entry = raftLog.get(e.getIndex());
            Assert.assertEquals(e, entry);
        }
        final LogEntryHeader[] termIndices = raftLog.getEntries(0, 500);
        LogEntryProto[] entriesFromLog = Arrays.stream(termIndices).map(ti -> {
            try {
                return raftLog.get(ti.getIndex());
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }).toArray(LogEntryProto[]::new);
        Assert.assertArrayEquals(entries, entriesFromLog);
        Assert.assertEquals(entries[entries.length - 1], getLastEntry(raftLog));
        final RatisMetricRegistry metricRegistryForLogWorker = RaftLogMetricsBase.getLogWorkerMetricRegistry(memberId);
        Timer raftLogSegmentLoadLatencyTimer = metricRegistryForLogWorker.timer("segmentLoadLatency");
        assertTrue(raftLogSegmentLoadLatencyTimer.getMeanRate() > 0);
        Timer raftLogReadLatencyTimer = metricRegistryForLogWorker.timer("readEntryLatency");
        assertTrue(raftLogReadLatencyTimer.getMeanRate() > 0);
    }
}
Also used : LogProtoUtils(org.apache.ratis.server.raftlog.LogProtoUtils) Arrays(java.util.Arrays) RetryCache(org.apache.ratis.server.RetryCache) TermIndex(org.apache.ratis.server.protocol.TermIndex) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) LongSupplier(java.util.function.LongSupplier) RaftGroupMemberId(org.apache.ratis.protocol.RaftGroupMemberId) RaftLog(org.apache.ratis.server.raftlog.RaftLog) RetryCacheTestUtil(org.apache.ratis.server.impl.RetryCacheTestUtil) CompletableFuture(java.util.concurrent.CompletableFuture) Supplier(java.util.function.Supplier) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) RaftLogMetricsBase(org.apache.ratis.server.metrics.RaftLogMetricsBase) Log4jUtils(org.apache.ratis.util.Log4jUtils) Level(org.apache.log4j.Level) After(org.junit.After) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) RaftStorage(org.apache.ratis.server.storage.RaftStorage) JavaUtils(org.apache.ratis.util.JavaUtils) SizeInBytes(org.apache.ratis.util.SizeInBytes) Before(org.junit.Before) StateMachine(org.apache.ratis.statemachine.StateMachine) 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) BaseTest(org.apache.ratis.BaseTest) RatisMetricRegistry(org.apache.ratis.metrics.RatisMetricRegistry) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) FileUtils(org.apache.ratis.util.FileUtils) ExecutionException(java.util.concurrent.ExecutionException) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) BaseStateMachine(org.apache.ratis.statemachine.impl.BaseStateMachine) RaftStorageTestUtils(org.apache.ratis.server.storage.RaftStorageTestUtils) RaftProperties(org.apache.ratis.conf.RaftProperties) LifeCycle(org.apache.ratis.util.LifeCycle) Timer(com.codahale.metrics.Timer) SimpleOperation(org.apache.ratis.RaftTestUtil.SimpleOperation) Assert(org.junit.Assert) Collections(java.util.Collections) TimeDuration(org.apache.ratis.util.TimeDuration) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) Timer(com.codahale.metrics.Timer) TimeoutIOException(org.apache.ratis.protocol.exceptions.TimeoutIOException) IOException(java.io.IOException) RatisMetricRegistry(org.apache.ratis.metrics.RatisMetricRegistry) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 5 with RatisMetricRegistry

use of org.apache.ratis.metrics.RatisMetricRegistry 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)

Aggregations

RatisMetricRegistry (org.apache.ratis.metrics.RatisMetricRegistry)13 Test (org.junit.Test)5 Timer (com.codahale.metrics.Timer)4 IOException (java.io.IOException)4 List (java.util.List)4 Level (org.apache.log4j.Level)4 RaftProperties (org.apache.ratis.conf.RaftProperties)4 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)4 RaftServerConfigKeys (org.apache.ratis.server.RaftServerConfigKeys)4 Log4jUtils (org.apache.ratis.util.Log4jUtils)4 Gauge (com.codahale.metrics.Gauge)3 TimeUnit (java.util.concurrent.TimeUnit)3 RaftServer (org.apache.ratis.server.RaftServer)3 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)3 StateMachine (org.apache.ratis.statemachine.StateMachine)3 JavaUtils (org.apache.ratis.util.JavaUtils)3 Assert (org.junit.Assert)3 ArrayList (java.util.ArrayList)2 Arrays (java.util.Arrays)2 SortedMap (java.util.SortedMap)2