Search in sources :

Example 6 with RaftPeerId

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

the class RetryCacheTests method testBasicRetry.

/**
 * make sure the retry cache can correct capture the retry from a client,
 * and returns the result from the previous request
 */
@Test
public void testBasicRetry() throws Exception {
    final MiniRaftCluster cluster = getCluster();
    RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId leaderId = cluster.getLeaderAndSendFirstMessage().getId();
    long oldLastApplied = cluster.getLeader().getState().getLastAppliedIndex();
    final RaftClient client = cluster.createClient(leaderId);
    final RaftClientRpc rpc = client.getClientRpc();
    final long callId = 999;
    final long seqNum = 111;
    RaftClientRequest r = cluster.newRaftClientRequest(client.getId(), leaderId, callId, seqNum, new SimpleMessage("message"));
    RaftClientReply reply = rpc.sendRequest(r);
    Assert.assertEquals(callId, reply.getCallId());
    Assert.assertTrue(reply.isSuccess());
    // retry with the same callId
    for (int i = 0; i < 5; i++) {
        reply = rpc.sendRequest(r);
        Assert.assertEquals(client.getId(), reply.getClientId());
        Assert.assertEquals(callId, reply.getCallId());
        Assert.assertTrue(reply.isSuccess());
    }
    long leaderApplied = cluster.getLeader().getState().getLastAppliedIndex();
    // make sure retry cache has the entry
    for (RaftServerImpl server : cluster.iterateServerImpls()) {
        LOG.info("check server " + server.getId());
        if (server.getState().getLastAppliedIndex() < leaderApplied) {
            Thread.sleep(1000);
        }
        Assert.assertEquals(2, RaftServerTestUtil.getRetryCacheSize(server));
        Assert.assertNotNull(RaftServerTestUtil.getRetryEntry(server, client.getId(), callId));
        // make sure there is only one log entry committed
        Assert.assertEquals(oldLastApplied + 1, server.getState().getLastAppliedIndex());
    }
    client.close();
}
Also used : RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient) RaftClientRpc(org.apache.ratis.client.RaftClientRpc) Test(org.junit.Test)

Example 7 with RaftPeerId

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

the class TestBatchAppend method testAppend.

@Test
public void testAppend() throws Exception {
    final int numMsgs = 10;
    final int numClients = 5;
    cluster.start();
    RaftTestUtil.waitForLeader(cluster);
    final RaftPeerId leaderId = cluster.getLeader().getId();
    // start several clients and write concurrently
    CountDownLatch latch = new CountDownLatch(1);
    final List<Sender> senders = Stream.iterate(0, i -> i + 1).limit(numClients).map(i -> new Sender(leaderId, latch, numMsgs)).collect(Collectors.toList());
    senders.forEach(Thread::start);
    latch.countDown();
    senders.forEach(sender -> {
        try {
            sender.join();
        } catch (InterruptedException ignored) {
        }
    });
    for (Sender s : senders) {
        Assert.assertTrue(s.succeed.get());
    }
    Assert.assertEquals(6 * numMsgs * numClients, cluster.getLeader().getState().getLastAppliedIndex());
}
Also used : Arrays(java.util.Arrays) RunWith(org.junit.runner.RunWith) ParameterizedBaseTest(org.apache.ratis.examples.ParameterizedBaseTest) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Level(org.apache.log4j.Level) After(org.junit.After) SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) SizeInBytes(org.apache.ratis.util.SizeInBytes) Parameterized(org.junit.runners.Parameterized) LogUtils(org.apache.ratis.util.LogUtils) StateMachine(org.apache.ratis.statemachine.StateMachine) RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) Collection(java.util.Collection) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Stream(java.util.stream.Stream) RaftProperties(org.apache.ratis.conf.RaftProperties) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) RaftClient(org.apache.ratis.client.RaftClient) Assert(org.junit.Assert) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) CountDownLatch(java.util.concurrent.CountDownLatch) ParameterizedBaseTest(org.apache.ratis.examples.ParameterizedBaseTest) Test(org.junit.Test)

Example 8 with RaftPeerId

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

the class LeaderElectionTests method testChangeLeader.

@Test
public void testChangeLeader() throws Exception {
    RaftStorageTestUtils.setRaftLogWorkerLogLevel(Level.TRACE);
    LOG.info("Running testChangeLeader");
    final MiniRaftCluster cluster = newCluster(3);
    cluster.start();
    RaftPeerId leader = RaftTestUtil.waitForLeader(cluster).getId();
    for (int i = 0; i < 10; i++) {
        leader = RaftTestUtil.changeLeader(cluster, leader);
        ExitUtils.assertNotTerminated();
    }
    RaftStorageTestUtils.setRaftLogWorkerLogLevel(Level.INFO);
    cluster.shutdown();
}
Also used : MiniRaftCluster(org.apache.ratis.MiniRaftCluster) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Example 9 with RaftPeerId

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

the class TestRaftServerJmx method runRegister.

static void runRegister(boolean expectToSucceed, String name, JmxRegister jmx) {
    final RaftServerMXBean mBean = new RaftServerMXBean() {

        @Override
        public String getId() {
            return null;
        }

        @Override
        public String getLeaderId() {
            return null;
        }

        @Override
        public long getCurrentTerm() {
            return 0;
        }

        @Override
        public String getGroupId() {
            return null;
        }

        @Override
        public String getRole() {
            return null;
        }

        @Override
        public List<String> getFollowers() {
            return null;
        }
    };
    final RaftPeerId id = RaftPeerId.valueOf(name);
    final RaftGroupId groupId = RaftGroupId.randomId();
    final boolean succeeded = RaftServerImpl.registerMBean(id, groupId, mBean, jmx);
    Assert.assertEquals(expectToSucceed, succeeded);
}
Also used : RaftGroupId(org.apache.ratis.protocol.RaftGroupId) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerMXBean(org.apache.ratis.server.RaftServerMXBean)

Example 10 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId 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 int maxCachedNum = RaftServerConfigKeys.Log.maxCachedSegmentNum(prop);
    File storageDir = getTestDir();
    RaftServerConfigKeys.setStorageDir(prop, storageDir);
    RaftStorage storage = new RaftStorage(storageDir, RaftServerConstants.StartupOption.REGULAR);
    RaftServerImpl server = Mockito.mock(RaftServerImpl.class);
    ServerState state = Mockito.mock(ServerState.class);
    Mockito.when(server.getState()).thenReturn(state);
    Mockito.when(server.getFollowerNextIndices()).thenReturn(new long[] {});
    Mockito.when(state.getLastAppliedIndex()).thenReturn(0L);
    SegmentedRaftLog raftLog = new SegmentedRaftLog(peerId, server, storage, -1, prop);
    raftLog.open(RaftServerConstants.INVALID_LOG_INDEX, null);
    List<SegmentRange> slist = TestSegmentedRaftLog.prepareRanges(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(server.getFollowerNextIndices()).thenReturn(new long[] { 21, 40, 40 });
    Mockito.when(state.getLastAppliedIndex()).thenReturn(35L);
    slist = TestSegmentedRaftLog.prepareRanges(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.storage.TestSegmentedRaftLog.SegmentRange) ServerState(org.apache.ratis.server.impl.ServerState) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftServerImpl(org.apache.ratis.server.impl.RaftServerImpl) CompletableFuture(java.util.concurrent.CompletableFuture) LogEntryProto(org.apache.ratis.shaded.proto.RaftProtos.LogEntryProto) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) File(java.io.File) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

RaftPeerId (org.apache.ratis.protocol.RaftPeerId)29 Test (org.junit.Test)16 RaftClient (org.apache.ratis.client.RaftClient)15 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)14 IOException (java.io.IOException)13 RaftProperties (org.apache.ratis.conf.RaftProperties)9 Collectors (java.util.stream.Collectors)8 RaftPeer (org.apache.ratis.protocol.RaftPeer)8 List (java.util.List)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 BaseTest (org.apache.ratis.BaseTest)7 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)6 RaftServerConfigKeys (org.apache.ratis.server.RaftServerConfigKeys)6 Assert (org.junit.Assert)6 Logger (org.slf4j.Logger)6 TimeUnit (java.util.concurrent.TimeUnit)5 Level (org.apache.log4j.Level)5 RaftServerImpl (org.apache.ratis.server.impl.RaftServerImpl)5 LogUtils (org.apache.ratis.util.LogUtils)5 TimeDuration (org.apache.ratis.util.TimeDuration)5