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();
}
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());
}
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();
}
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);
}
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());
}
Aggregations