use of org.apache.ratis.server.impl.RaftServerProxy in project incubator-ratis by apache.
the class MiniRaftCluster method putNewServer.
public RaftServerProxy putNewServer(RaftPeerId id, RaftGroup group, boolean format) {
final RaftServerProxy s = newRaftServer(id, group, format);
Preconditions.assertTrue(servers.put(id, s) == null);
return s;
}
use of org.apache.ratis.server.impl.RaftServerProxy in project incubator-ratis by apache.
the class TestStateMachine method testTransactionContextIsPassedBack.
@Test
public void testTransactionContextIsPassedBack() throws Throwable {
// tests that the TrxContext set by the StateMachine in Leader is passed back to the SM
properties.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SMTransactionContext.class, StateMachine.class);
startCluster();
int numTrx = 100;
final RaftTestUtil.SimpleMessage[] messages = RaftTestUtil.SimpleMessage.create(numTrx);
try (final RaftClient client = cluster.createClient()) {
for (RaftTestUtil.SimpleMessage message : messages) {
client.send(message);
}
}
// TODO: there eshould be a better way to ensure all data is replicated and applied
Thread.sleep(cluster.getMaxTimeout() + 100);
for (RaftServerProxy raftServer : cluster.getServers()) {
final SMTransactionContext sm = SMTransactionContext.get(raftServer);
sm.rethrowIfException();
assertEquals(numTrx, sm.numApplied.get());
}
// check leader
RaftServerImpl raftServer = cluster.getLeader();
// assert every transaction has obtained context in leader
final SMTransactionContext sm = SMTransactionContext.get(raftServer.getProxy());
List<Long> ll = sm.applied.stream().collect(Collectors.toList());
Collections.sort(ll);
assertEquals(ll.toString(), ll.size(), numTrx);
for (int i = 0; i < numTrx; i++) {
assertEquals(ll.toString(), Long.valueOf(i + 1), ll.get(i));
}
}
use of org.apache.ratis.server.impl.RaftServerProxy in project incubator-ratis by apache.
the class RaftAsyncTests method testAsyncRequestSemaphore.
@Test
public void testAsyncRequestSemaphore() throws InterruptedException, IOException {
LOG.info("Running testAsyncRequestSemaphore");
CLUSTER cluster = getFactory().newCluster(NUM_SERVERS, properties);
Assert.assertNull(cluster.getLeader());
cluster.start();
waitForLeader(cluster);
int numMessages = RaftClientConfigKeys.Async.maxOutstandingRequests(properties);
CompletableFuture[] futures = new CompletableFuture[numMessages + 1];
final RaftTestUtil.SimpleMessage[] messages = RaftTestUtil.SimpleMessage.create(numMessages);
final RaftClient client = cluster.createClient();
// Set blockTransaction flag so that transaction blocks
for (RaftServerProxy server : cluster.getServers()) {
((SimpleStateMachine4Testing) server.getStateMachine()).setBlockTransaction(true);
}
// Send numMessages which are blocked and do not release the client semaphore permits
AtomicInteger blockedRequestsCount = new AtomicInteger();
for (int i = 0; i < numMessages; i++) {
blockedRequestsCount.getAndIncrement();
futures[i] = client.sendAsync(messages[i]);
blockedRequestsCount.decrementAndGet();
}
Assert.assertTrue(blockedRequestsCount.get() == 0);
ExecutorService threadPool = Executors.newFixedThreadPool(1);
futures[numMessages] = CompletableFuture.supplyAsync(() -> {
blockedRequestsCount.incrementAndGet();
client.sendAsync(new RaftTestUtil.SimpleMessage("n1"));
blockedRequestsCount.decrementAndGet();
return null;
}, threadPool);
// Allow the last msg to be sent
while (blockedRequestsCount.get() != 1) {
Thread.sleep(1000);
}
Assert.assertTrue(blockedRequestsCount.get() == 1);
// Since all semaphore permits are acquired the last message sent is in queue
RaftClientTestUtil.assertAsyncRequestSemaphore(client, 0, 1);
// Unset the blockTransaction flag so that semaphore permits can be released
for (RaftServerProxy server : cluster.getServers()) {
((SimpleStateMachine4Testing) server.getStateMachine()).setBlockTransaction(false);
}
for (int i = 0; i <= numMessages; i++) {
futures[i].join();
}
Assert.assertTrue(blockedRequestsCount.get() == 0);
cluster.shutdown();
}
use of org.apache.ratis.server.impl.RaftServerProxy in project incubator-ratis by apache.
the class TestRaftLogMetrics method testFlushMetric.
@Test
public void testFlushMetric() throws Exception {
int numMsg = 2;
final RaftTestUtil.SimpleMessage[] messages = RaftTestUtil.SimpleMessage.create(numMsg);
try (final RaftClient client = cluster.createClient()) {
for (RaftTestUtil.SimpleMessage message : messages) {
client.send(message);
}
}
for (RaftServerProxy rsp : cluster.getServers()) {
String flushTimeMetric = getLogFlushTimeMetric(rsp.getId().toString());
Timer tm = RatisMetricsRegistry.getRegistry().getTimers().get(flushTimeMetric);
Assert.assertNotNull(tm);
// Number of log entries expected = numMsg + 1 entry for start-log-segment
int numExpectedLogEntries = numMsg + 1;
Assert.assertEquals(numExpectedLogEntries, tm.getCount());
Assert.assertTrue(tm.getMeanRate() > 0);
// Test jmx
ObjectName oname = new ObjectName("metrics", "name", flushTimeMetric);
Assert.assertEquals(numExpectedLogEntries, ((Long) ManagementFactory.getPlatformMBeanServer().getAttribute(oname, "Count")).intValue());
}
}
Aggregations