Search in sources :

Example 41 with RaftProperties

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);
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) AsyncRpcApi(org.apache.ratis.client.AsyncRpcApi) ArrayList(java.util.ArrayList) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftGroupId(org.apache.ratis.protocol.RaftGroupId) Answer(org.mockito.stubbing.Answer) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) ClientId(org.apache.ratis.protocol.ClientId) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftClient(org.apache.ratis.client.RaftClient)

Example 42 with RaftProperties

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));
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) StateMachine(org.apache.ratis.statemachine.StateMachine) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftPeerId(org.apache.ratis.protocol.RaftPeerId)

Example 43 with RaftProperties

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);
    }
}
Also used : RaftProperties(org.apache.ratis.conf.RaftProperties) BaseTest(org.apache.ratis.BaseTest) Test(org.junit.Test)

Example 44 with RaftProperties

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();
}
Also used : SimpleStateMachine4Testing(org.apache.ratis.statemachine.SimpleStateMachine4Testing) RaftServer(org.apache.ratis.server.RaftServer) RaftProperties(org.apache.ratis.conf.RaftProperties) ResourceUnavailableException(org.apache.ratis.protocol.exceptions.ResourceUnavailableException) IOException(java.io.IOException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RaftClientRequest(org.apache.ratis.protocol.RaftClientRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) ResourceUnavailableException(org.apache.ratis.protocol.exceptions.ResourceUnavailableException) ClientId(org.apache.ratis.protocol.ClientId) Test(org.junit.Test)

Example 45 with RaftProperties

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();
}
Also used : ExponentialBackoffRetry(org.apache.ratis.retry.ExponentialBackoffRetry) RaftProperties(org.apache.ratis.conf.RaftProperties)

Aggregations

RaftProperties (org.apache.ratis.conf.RaftProperties)54 Test (org.junit.Test)22 BaseTest (org.apache.ratis.BaseTest)14 Before (org.junit.Before)12 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)9 RaftServer (org.apache.ratis.server.RaftServer)8 File (java.io.File)7 CompletableFuture (java.util.concurrent.CompletableFuture)7 RaftClient (org.apache.ratis.client.RaftClient)7 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Parameters (org.apache.ratis.conf.Parameters)4 LogEntryProto (org.apache.ratis.proto.RaftProtos.LogEntryProto)4 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)4 RaftGroupMemberId (org.apache.ratis.protocol.RaftGroupMemberId)4 RaftGroup (org.apache.ratis.protocol.RaftGroup)3 StateMachine (org.apache.ratis.statemachine.StateMachine)3 TimeDuration (org.apache.ratis.util.TimeDuration)3 List (java.util.List)2 UUID (java.util.UUID)2