Search in sources :

Example 71 with RaftPeerId

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

the class Server method run.

@Override
public void run() throws Exception {
    JVMMetrics.initJvmMetrics(TimeDuration.valueOf(10, TimeUnit.SECONDS));
    RaftPeerId peerId = RaftPeerId.valueOf(id);
    RaftProperties properties = new RaftProperties();
    // Avoid leader change affect the performance
    RaftServerConfigKeys.Rpc.setTimeoutMin(properties, TimeDuration.valueOf(2, TimeUnit.SECONDS));
    RaftServerConfigKeys.Rpc.setTimeoutMax(properties, TimeDuration.valueOf(3, TimeUnit.SECONDS));
    final int port = NetUtils.createSocketAddr(getPeer(peerId).getAddress()).getPort();
    GrpcConfigKeys.Server.setPort(properties, port);
    Optional.ofNullable(getPeer(peerId).getClientAddress()).ifPresent(address -> GrpcConfigKeys.Client.setPort(properties, NetUtils.createSocketAddr(address).getPort()));
    Optional.ofNullable(getPeer(peerId).getAdminAddress()).ifPresent(address -> GrpcConfigKeys.Admin.setPort(properties, NetUtils.createSocketAddr(address).getPort()));
    String dataStreamAddress = getPeer(peerId).getDataStreamAddress();
    if (dataStreamAddress != null) {
        final int dataStreamport = NetUtils.createSocketAddr(dataStreamAddress).getPort();
        NettyConfigKeys.DataStream.setPort(properties, dataStreamport);
        RaftConfigKeys.DataStream.setType(properties, SupportedDataStreamType.NETTY);
    }
    RaftServerConfigKeys.setStorageDir(properties, storageDir);
    RaftServerConfigKeys.Write.setElementLimit(properties, 40960);
    RaftServerConfigKeys.Write.setByteLimit(properties, SizeInBytes.valueOf("1000MB"));
    ConfUtils.setFiles(properties::setFiles, FileStoreCommon.STATEMACHINE_DIR_KEY, storageDir);
    RaftServerConfigKeys.DataStream.setAsyncRequestThreadPoolSize(properties, writeThreadNum);
    RaftServerConfigKeys.DataStream.setAsyncWriteThreadPoolSize(properties, writeThreadNum);
    ConfUtils.setInt(properties::setInt, FileStoreCommon.STATEMACHINE_WRITE_THREAD_NUM, writeThreadNum);
    ConfUtils.setInt(properties::setInt, FileStoreCommon.STATEMACHINE_READ_THREAD_NUM, readThreadNum);
    ConfUtils.setInt(properties::setInt, FileStoreCommon.STATEMACHINE_COMMIT_THREAD_NUM, commitThreadNum);
    ConfUtils.setInt(properties::setInt, FileStoreCommon.STATEMACHINE_DELETE_THREAD_NUM, deleteThreadNum);
    StateMachine stateMachine = new FileStoreStateMachine(properties);
    final RaftGroup raftGroup = RaftGroup.valueOf(RaftGroupId.valueOf(ByteString.copyFromUtf8(getRaftGroupId())), getPeers());
    RaftServer raftServer = RaftServer.newBuilder().setServerId(RaftPeerId.valueOf(id)).setStateMachine(stateMachine).setProperties(properties).setGroup(raftGroup).build();
    raftServer.start();
    for (; raftServer.getLifeCycleState() != LifeCycle.State.CLOSED; ) {
        TimeUnit.SECONDS.sleep(1);
    }
}
Also used : StateMachine(org.apache.ratis.statemachine.StateMachine) FileStoreStateMachine(org.apache.ratis.examples.filestore.FileStoreStateMachine) RaftServer(org.apache.ratis.server.RaftServer) RaftProperties(org.apache.ratis.conf.RaftProperties) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) RaftGroup(org.apache.ratis.protocol.RaftGroup) FileStoreStateMachine(org.apache.ratis.examples.filestore.FileStoreStateMachine)

Example 72 with RaftPeerId

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

the class GrpcService method requestVote.

@Override
public RequestVoteReplyProto requestVote(RequestVoteRequestProto request) throws IOException {
    CodeInjectionForTesting.execute(GRPC_SEND_SERVER_REQUEST, getId(), null, request);
    final RaftPeerId target = RaftPeerId.valueOf(request.getServerRequest().getReplyId());
    return getProxies().getProxy(target).requestVote(request);
}
Also used : RaftPeerId(org.apache.ratis.protocol.RaftPeerId)

Example 73 with RaftPeerId

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

the class DataStreamAsyncClusterTests method runTestDataStream.

void runTestDataStream(CLUSTER cluster, boolean stepDownLeader) throws Exception {
    RaftTestUtil.waitForLeader(cluster);
    final List<CompletableFuture<Long>> futures = new ArrayList<>();
    futures.add(CompletableFuture.supplyAsync(() -> runTestDataStream(cluster, 5, 10, 1_000_000, 10, stepDownLeader), executor));
    futures.add(CompletableFuture.supplyAsync(() -> runTestDataStream(cluster, 2, 20, 1_000, 5_000, stepDownLeader), executor));
    final long maxIndex = futures.stream().map(CompletableFuture::join).max(Long::compareTo).orElseThrow(IllegalStateException::new);
    if (stepDownLeader) {
        final RaftPeerId oldLeader = cluster.getLeader().getId();
        final CompletableFuture<RaftPeerId> changeLeader = futures.get(0).thenApplyAsync(dummy -> {
            try {
                return RaftTestUtil.changeLeader(cluster, oldLeader);
            } catch (Exception e) {
                throw new CompletionException("Failed to change leader from " + oldLeader, e);
            }
        });
        LOG.info("Changed leader from {} to {}", oldLeader, changeLeader.join());
    }
    // wait for all servers to catch up
    try (RaftClient client = cluster.createClient()) {
        RaftClientReply reply = client.async().watch(maxIndex, ReplicationLevel.ALL).join();
        Assert.assertTrue(reply.isSuccess());
    }
    // assert all streams are linked
    for (RaftServer proxy : cluster.getServers()) {
        final RaftServer.Division impl = proxy.getDivision(cluster.getGroupId());
        final MultiDataStreamStateMachine stateMachine = (MultiDataStreamStateMachine) impl.getStateMachine();
        for (SingleDataStream s : stateMachine.getStreams()) {
            Assert.assertFalse(s.getDataChannel().isOpen());
            DataStreamTestUtils.assertLogEntry(impl, s);
        }
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) ArrayList(java.util.ArrayList) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) CompletableFuture(java.util.concurrent.CompletableFuture) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) MultiDataStreamStateMachine(org.apache.ratis.datastream.DataStreamTestUtils.MultiDataStreamStateMachine) CompletionException(java.util.concurrent.CompletionException) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) SingleDataStream(org.apache.ratis.datastream.DataStreamTestUtils.SingleDataStream) RaftClient(org.apache.ratis.client.RaftClient)

Example 74 with RaftPeerId

use of org.apache.ratis.protocol.RaftPeerId 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 75 with RaftPeerId

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

the class RemoveCommand method run.

@Override
public int run(CommandLine cl) throws IOException {
    super.run(cl);
    final List<RaftPeerId> ids = getIds(cl.getOptionValue(ADDRESS_OPTION_NAME).split(","), (a, b) -> {
    });
    try (RaftClient client = RaftUtils.createClient(getRaftGroup())) {
        final List<RaftPeer> remaining = getRaftGroup().getPeers().stream().filter(raftPeer -> !ids.contains(raftPeer.getId())).collect(Collectors.toList());
        System.out.println("New peer list: " + remaining);
        RaftClientReply reply = client.admin().setConfiguration(remaining);
        processReply(reply, () -> "Failed to change raft peer");
    }
    return 0;
}
Also used : RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) Options(org.apache.commons.cli.Options) Context(org.apache.ratis.shell.cli.sh.command.Context) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) List(java.util.List) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) AbstractRatisCommand(org.apache.ratis.shell.cli.sh.command.AbstractRatisCommand) CommandLine(org.apache.commons.cli.CommandLine) RaftUtils(org.apache.ratis.shell.cli.RaftUtils) RaftClient(org.apache.ratis.client.RaftClient) Option(org.apache.commons.cli.Option) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftPeer(org.apache.ratis.protocol.RaftPeer) RaftClient(org.apache.ratis.client.RaftClient)

Aggregations

RaftPeerId (org.apache.ratis.protocol.RaftPeerId)101 RaftClient (org.apache.ratis.client.RaftClient)58 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)45 IOException (java.io.IOException)36 Test (org.junit.Test)32 RaftPeer (org.apache.ratis.protocol.RaftPeer)31 RaftServer (org.apache.ratis.server.RaftServer)31 BaseTest (org.apache.ratis.BaseTest)21 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)20 CompletableFuture (java.util.concurrent.CompletableFuture)18 RaftProperties (org.apache.ratis.conf.RaftProperties)18 File (java.io.File)17 ArrayList (java.util.ArrayList)15 RaftGroupId (org.apache.ratis.protocol.RaftGroupId)15 RaftGroup (org.apache.ratis.protocol.RaftGroup)14 TimeDuration (org.apache.ratis.util.TimeDuration)14 List (java.util.List)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)12 Collectors (java.util.stream.Collectors)11 RaftLog (org.apache.ratis.server.raftlog.RaftLog)11