use of org.apache.ratis.conf.RaftProperties in project incubator-ratis by apache.
the class TestNettyDataStreamWithMock method testMockCluster.
private void testMockCluster(int numServers, RaftException leaderException, IllegalStateException submitException, IOException getStateMachineException) throws Exception {
List<RaftServer> raftServers = new ArrayList<>();
ClientId clientId = ClientId.randomId();
RaftGroupId groupId = RaftGroupId.randomId();
for (int i = 0; i < numServers; i++) {
RaftServer raftServer = mock(RaftServer.class);
RaftPeerId peerId = RaftPeerId.valueOf("s" + i);
RaftProperties properties = new RaftProperties();
NettyConfigKeys.DataStream.setPort(properties, NetUtils.createLocalServerAddress().getPort());
RaftConfigKeys.DataStream.setType(properties, SupportedDataStreamType.NETTY);
when(raftServer.getProperties()).thenReturn(properties);
when(raftServer.getId()).thenReturn(peerId);
when(raftServer.getPeer()).thenReturn(RaftPeer.newBuilder().setId(peerId).build());
if (getStateMachineException == null) {
RaftClient client = Mockito.mock(RaftClient.class);
when(client.getId()).thenReturn(clientId);
AsyncRpcApi asyncRpcApi = Mockito.mock(AsyncRpcApi.class);
when(client.async()).thenReturn(asyncRpcApi);
final RaftServer.Division myDivision = mockDivision(raftServer, client);
when(raftServer.getDivision(Mockito.any(RaftGroupId.class))).thenReturn(myDivision);
if (submitException != null) {
when(asyncRpcApi.sendForward(Mockito.any(RaftClientRequest.class))).thenThrow(submitException);
} else if (i == 0) {
// primary
when(asyncRpcApi.sendForward(Mockito.any(RaftClientRequest.class))).thenAnswer((Answer<CompletableFuture<RaftClientReply>>) invocation -> {
final RaftClientRequest r = (RaftClientRequest) invocation.getArguments()[0];
final RaftClientReply.Builder b = RaftClientReply.newBuilder().setRequest(r);
final RaftClientReply reply = leaderException != null ? b.setException(leaderException).build() : b.setSuccess().setMessage(() -> DataStreamTestUtils.MOCK).build();
return CompletableFuture.completedFuture(reply);
});
}
} else {
when(raftServer.getDivision(Mockito.any(RaftGroupId.class))).thenThrow(getStateMachineException);
}
raftServers.add(raftServer);
}
runTestMockCluster(groupId, raftServers, clientId, 1_000_000, 10, submitException != null ? submitException : leaderException, getStateMachineException);
}
use of org.apache.ratis.conf.RaftProperties in project incubator-ratis by apache.
the class TestRaftServerWithGrpc method runTestServerRestartOnException.
void runTestServerRestartOnException(MiniRaftClusterWithGrpc cluster) throws Exception {
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
final RaftPeerId leaderId = leader.getId();
final RaftProperties p = getProperties();
GrpcConfigKeys.Server.setPort(p, RaftServerTestUtil.getServerRpc(leader).getInetSocketAddress().getPort());
// Create a raft server proxy with server rpc bound to a different address
// compared to leader. This helps in locking the raft storage directory to
// be used by next raft server proxy instance.
final StateMachine stateMachine = cluster.getLeader().getStateMachine();
RaftServerConfigKeys.setStorageDir(p, Collections.singletonList(cluster.getStorageDir(leaderId)));
newRaftServer(cluster, leaderId, stateMachine, p);
// Close the server rpc for leader so that new raft server can be bound to it.
RaftServerTestUtil.getServerRpc(cluster.getLeader()).close();
// Create a raft server proxy with server rpc bound to same address as
// the leader. This step would fail as the raft storage has been locked by
// the raft server proxy created earlier. Raft server proxy should close
// the rpc server on failure.
RaftServerConfigKeys.setStorageDir(p, Collections.singletonList(cluster.getStorageDir(leaderId)));
testFailureCase("start a new server with the same address", () -> newRaftServer(cluster, leaderId, stateMachine, p).start(), IOException.class, OverlappingFileLockException.class);
// Try to start a raft server rpc at the leader address.
cluster.getServerFactory(leaderId).newRaftServerRpc(cluster.getServer(leaderId));
}
use of org.apache.ratis.conf.RaftProperties in project incubator-ratis by apache.
the class TestRaftServerWithGrpc method testRaftServerMetrics.
@Test
public void testRaftServerMetrics() throws Exception {
final RaftProperties p = getProperties();
RaftServerConfigKeys.Write.setElementLimit(p, 10);
RaftServerConfigKeys.Write.setByteLimit(p, SizeInBytes.valueOf("1MB"));
try {
runWithNewCluster(3, this::testRequestMetrics);
} finally {
RaftServerConfigKeys.Write.setElementLimit(p, RaftServerConfigKeys.Write.ELEMENT_LIMIT_DEFAULT);
RaftServerConfigKeys.Write.setByteLimit(p, RaftServerConfigKeys.Write.BYTE_LIMIT_DEFAULT);
}
}
use of org.apache.ratis.conf.RaftProperties in project incubator-ratis by apache.
the class TestRetryCacheWithGrpc method testRetryOnResourceUnavailableException.
@Test(timeout = 10000)
public void testRetryOnResourceUnavailableException() throws InterruptedException, IOException {
RaftProperties properties = new RaftProperties();
properties.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY, SimpleStateMachine4Testing.class, StateMachine.class);
RaftServerConfigKeys.Write.setElementLimit(properties, 1);
MiniRaftClusterWithGrpc cluster = getFactory().newCluster(NUM_SERVERS, properties);
cluster.start();
final RaftServer.Division leader = RaftTestUtil.waitForLeader(cluster);
final RaftServer leaderProxy = leader.getRaftServer();
for (RaftServer.Division follower : cluster.getFollowers()) {
// block followers to trigger ResourceUnavailableException
((SimpleStateMachine4Testing) follower.getStateMachine()).blockWriteStateMachineData();
}
AtomicBoolean failure = new AtomicBoolean(false);
long callId = 1;
ClientId clientId = ClientId.randomId();
RaftClientRequest r = null;
while (!failure.get()) {
long cid = callId;
r = cluster.newRaftClientRequest(clientId, leaderProxy.getId(), callId++, new RaftTestUtil.SimpleMessage("message"));
CompletableFuture<RaftClientReply> f = leaderProxy.submitClientRequestAsync(r);
f.exceptionally(e -> {
if (e.getCause() instanceof ResourceUnavailableException) {
RetryCacheTestUtil.isFailed(RetryCacheTestUtil.get(leader, clientId, cid));
failure.set(true);
}
return null;
});
}
for (RaftServer.Division follower : cluster.getFollowers()) {
// unblock followers
((SimpleStateMachine4Testing) follower.getStateMachine()).unblockWriteStateMachineData();
}
while (failure.get()) {
try {
// retry until the request failed with ResourceUnavailableException succeeds.
RaftClientReply reply = leaderProxy.submitClientRequestAsync(r).get();
if (reply.isSuccess()) {
failure.set(false);
}
} catch (Exception e) {
// Ignore the exception
}
}
cluster.shutdown();
}
use of org.apache.ratis.conf.RaftProperties in project incubator-ratis by apache.
the class RaftUtils method createClient.
/**
* Create a raft client to communicate to ratis server.
* @param raftGroup the raft group
* @return return a raft client
*/
public static RaftClient createClient(RaftGroup raftGroup) {
RaftProperties properties = new RaftProperties();
RaftClientConfigKeys.Rpc.setRequestTimeout(properties, TimeDuration.valueOf(15, TimeUnit.SECONDS));
ExponentialBackoffRetry retryPolicy = ExponentialBackoffRetry.newBuilder().setBaseSleepTime(TimeDuration.valueOf(1000, TimeUnit.MILLISECONDS)).setMaxAttempts(10).setMaxSleepTime(TimeDuration.valueOf(100_000, TimeUnit.MILLISECONDS)).build();
return RaftClient.newBuilder().setRaftGroup(raftGroup).setProperties(properties).setRetryPolicy(retryPolicy).build();
}
Aggregations