Search in sources :

Example 6 with RaftGroupMemberId

use of org.apache.ratis.protocol.RaftGroupMemberId in project incubator-ratis by apache.

the class MemoryRaftLogTest method testEntryDoNotPerformTruncation.

@Test
public void testEntryDoNotPerformTruncation() throws Exception {
    final RaftProperties prop = new RaftProperties();
    prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class);
    final RaftPeerId peerId = RaftPeerId.valueOf("s0");
    final RaftGroupId groupId = RaftGroupId.randomId();
    final RaftGroupMemberId memberId = RaftGroupMemberId.valueOf(peerId, groupId);
    MemoryRaftLog raftLog = new MemoryRaftLog(memberId, () -> -1, prop);
    raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
    LogEntryProto[] entries1 = new LogEntryProto[2];
    entries1[0] = LogEntryProto.newBuilder().setIndex(0).setTerm(0).build();
    entries1[1] = LogEntryProto.newBuilder().setIndex(1).setTerm(0).build();
    raftLog.append(entries1).forEach(CompletableFuture::join);
    LogEntryProto[] entries2 = new LogEntryProto[1];
    entries2[0] = LogEntryProto.newBuilder().setIndex(0).setTerm(0).build();
    raftLog.append(entries2).forEach(CompletableFuture::join);
    final LogEntryHeader[] termIndices = raftLog.getEntries(0, 10);
    assertEquals(2, termIndices.length);
    for (int i = 0; i < 2; i++) {
        assertEquals(entries1[i].getIndex(), termIndices[i].getIndex());
        assertEquals(entries1[i].getTerm(), termIndices[i].getTerm());
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) LogEntryHeader(org.apache.ratis.server.raftlog.LogEntryHeader) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftGroupMemberId(org.apache.ratis.protocol.RaftGroupMemberId) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 7 with RaftGroupMemberId

use of org.apache.ratis.protocol.RaftGroupMemberId in project incubator-ratis by apache.

the class TestCacheEviction method testEvictionInSegmentedLog.

@Test
public void testEvictionInSegmentedLog() throws Exception {
    final RaftProperties prop = new RaftProperties();
    prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class);
    RaftServerConfigKeys.Log.setSegmentSizeMax(prop, SizeInBytes.valueOf("8KB"));
    RaftServerConfigKeys.Log.setPreallocatedSize(prop, SizeInBytes.valueOf("8KB"));
    final RaftPeerId peerId = RaftPeerId.valueOf("s0");
    final RaftGroupId groupId = RaftGroupId.randomId();
    final RaftGroupMemberId memberId = RaftGroupMemberId.valueOf(peerId, groupId);
    final int maxCachedNum = RaftServerConfigKeys.Log.segmentCacheNumMax(prop);
    File storageDir = getTestDir();
    RaftServerConfigKeys.setStorageDir(prop, Collections.singletonList(storageDir));
    RaftStorage storage = RaftStorageTestUtils.newRaftStorage(storageDir);
    final DivisionInfo info = Mockito.mock(DivisionInfo.class);
    Mockito.when(info.getLastAppliedIndex()).thenReturn(0L);
    Mockito.when(info.getFollowerNextIndices()).thenReturn(new long[] {});
    final SegmentedRaftLog raftLog = RaftServerTestUtil.newSegmentedRaftLog(memberId, info, storage, prop);
    raftLog.open(RaftLog.INVALID_LOG_INDEX, null);
    List<SegmentRange> slist = TestSegmentedRaftLog.prepareRanges(0, maxCachedNum, 7, 0);
    LogEntryProto[] entries = generateEntries(slist);
    raftLog.append(entries).forEach(CompletableFuture::join);
    // check the current cached segment number: the last segment is still open
    Assert.assertEquals(maxCachedNum - 1, raftLog.getRaftLogCache().getCachedSegmentNum());
    Mockito.when(info.getLastAppliedIndex()).thenReturn(35L);
    Mockito.when(info.getFollowerNextIndices()).thenReturn(new long[] { 21, 40, 40 });
    slist = TestSegmentedRaftLog.prepareRanges(maxCachedNum, maxCachedNum + 2, 7, 7 * maxCachedNum);
    entries = generateEntries(slist);
    raftLog.append(entries).forEach(CompletableFuture::join);
    // check the cached segment number again. since the slowest follower is on
    // index 21, the eviction should happen and evict 3 segments
    Assert.assertEquals(maxCachedNum + 1 - 3, raftLog.getRaftLogCache().getCachedSegmentNum());
}
Also used : SegmentRange(org.apache.ratis.server.raftlog.segmented.TestSegmentedRaftLog.SegmentRange) DivisionInfo(org.apache.ratis.server.DivisionInfo) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) RaftGroupMemberId(org.apache.ratis.protocol.RaftGroupMemberId) CompletableFuture(java.util.concurrent.CompletableFuture) LogEntryProto(org.apache.ratis.proto.RaftProtos.LogEntryProto) RaftStorage(org.apache.ratis.server.storage.RaftStorage) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) File(java.io.File) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 8 with RaftGroupMemberId

use of org.apache.ratis.protocol.RaftGroupMemberId in project incubator-ratis by apache.

the class TestGrpcServerMetrics method setUp.

@BeforeClass
public static void setUp() throws Exception {
    raftGroupId = RaftGroupId.randomId();
    raftPeerId = RaftPeerId.valueOf("TestId");
    followerId = RaftPeerId.valueOf("FollowerId");
    RaftGroupMemberId raftGroupMemberId = RaftGroupMemberId.valueOf(raftPeerId, raftGroupId);
    grpcServerMetrics = new GrpcServerMetrics(raftGroupMemberId.toString());
    ratisMetricRegistry = grpcServerMetrics.getRegistry();
}
Also used : RaftGroupMemberId(org.apache.ratis.protocol.RaftGroupMemberId) GrpcServerMetrics(org.apache.ratis.grpc.metrics.GrpcServerMetrics) BeforeClass(org.junit.BeforeClass)

Example 9 with RaftGroupMemberId

use of org.apache.ratis.protocol.RaftGroupMemberId in project incubator-ratis by apache.

the class RaftBasicTests method checkFollowerCommitLagsLeader.

private static void checkFollowerCommitLagsLeader(MiniRaftCluster cluster) {
    final List<RaftServer.Division> followers = cluster.getFollowers();
    final RaftGroupMemberId leader = cluster.getLeader().getMemberId();
    Gauge leaderCommitGauge = RaftServerMetricsImpl.getPeerCommitIndexGauge(leader, leader.getPeerId());
    for (RaftServer.Division f : followers) {
        final RaftGroupMemberId follower = f.getMemberId();
        Gauge followerCommitGauge = RaftServerMetricsImpl.getPeerCommitIndexGauge(leader, follower.getPeerId());
        Assert.assertTrue((Long) leaderCommitGauge.getValue() >= (Long) followerCommitGauge.getValue());
        Gauge followerMetric = RaftServerMetricsImpl.getPeerCommitIndexGauge(follower, follower.getPeerId());
        System.out.println(followerCommitGauge.getValue());
        System.out.println(followerMetric.getValue());
        Assert.assertTrue((Long) followerCommitGauge.getValue() <= (Long) followerMetric.getValue());
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) RaftGroupMemberId(org.apache.ratis.protocol.RaftGroupMemberId) Gauge(com.codahale.metrics.Gauge)

Aggregations

RaftGroupMemberId (org.apache.ratis.protocol.RaftGroupMemberId)9 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)6 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)5 RaftProperties (org.apache.ratis.conf.RaftProperties)4 CompletableFuture (java.util.concurrent.CompletableFuture)3 BaseTest (org.apache.ratis.BaseTest)3 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)3 BeforeClass (org.junit.BeforeClass)3 Test (org.junit.Test)3 DivisionInfo (org.apache.ratis.server.DivisionInfo)2 LogEntryHeader (org.apache.ratis.server.raftlog.LogEntryHeader)2 Gauge (com.codahale.metrics.Gauge)1 File (java.io.File)1 GrpcServerMetrics (org.apache.ratis.grpc.metrics.GrpcServerMetrics)1 RaftServer (org.apache.ratis.server.RaftServer)1 LeaderElectionMetrics (org.apache.ratis.server.metrics.LeaderElectionMetrics)1 LogAppenderMetrics (org.apache.ratis.server.metrics.LogAppenderMetrics)1 RaftServerMetricsImpl (org.apache.ratis.server.metrics.RaftServerMetricsImpl)1 SegmentRange (org.apache.ratis.server.raftlog.segmented.TestSegmentedRaftLog.SegmentRange)1 RaftStorage (org.apache.ratis.server.storage.RaftStorage)1