Search in sources :

Example 16 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply in project alluxio by Alluxio.

the class RaftJournalSystem method addQuorumServer.

/**
 * Adds a server to the quorum.
 *
 * @param serverNetAddress the address of the server
 * @throws IOException if error occurred while performing the operation
 */
public synchronized void addQuorumServer(NetAddress serverNetAddress) throws IOException {
    InetSocketAddress serverAddress = InetSocketAddress.createUnresolved(serverNetAddress.getHost(), serverNetAddress.getRpcPort());
    RaftPeerId peerId = RaftJournalUtils.getPeerId(serverAddress);
    Collection<RaftPeer> peers = mServer.getGroups().iterator().next().getPeers();
    if (peers.stream().anyMatch((peer) -> peer.getId().equals(peerId))) {
        return;
    }
    RaftPeer newPeer = RaftPeer.newBuilder().setId(peerId).setAddress(serverAddress).build();
    List<RaftPeer> newPeers = new ArrayList<>(peers);
    newPeers.add(newPeer);
    RaftClientReply reply = mServer.setConfiguration(new SetConfigurationRequest(mRawClientId, mPeerId, RAFT_GROUP_ID, nextCallId(), newPeers));
    if (reply.getException() != null) {
        throw reply.getException();
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) InetSocketAddress(java.net.InetSocketAddress) ArrayList(java.util.ArrayList) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftPeer(org.apache.ratis.protocol.RaftPeer) SetConfigurationRequest(org.apache.ratis.protocol.SetConfigurationRequest)

Example 17 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply in project alluxio by Alluxio.

the class SnapshotReplicationManagerTest method before.

private void before(int numFollowers) throws Exception {
    mLeader = Mockito.mock(RaftJournalSystem.class);
    Mockito.when(mLeader.isLeader()).thenReturn(true);
    Mockito.when(mLeader.getLocalPeerId()).thenReturn(RaftPeerId.getRaftPeerId("leader"));
    mLeaderStore = getSimpleStateMachineStorage();
    mLeaderSnapshotManager = Mockito.spy(new SnapshotReplicationManager(mLeader, mLeaderStore));
    String serverName = InProcessServerBuilder.generateName();
    mServer = InProcessServerBuilder.forName(serverName).directExecutor().addService(new RaftJournalServiceHandler(mLeaderSnapshotManager)).build();
    mServer.start();
    ManagedChannel channel = InProcessChannelBuilder.forName(serverName).directExecutor().build();
    RaftJournalServiceGrpc.RaftJournalServiceStub stub = RaftJournalServiceGrpc.newStub(channel);
    // mock RaftJournalServiceClient
    mClient = Mockito.mock(RaftJournalServiceClient.class);
    Mockito.doNothing().when(mClient).close();
    // download rpc mock
    Mockito.when(mClient.downloadSnapshot(any())).thenAnswer((args) -> {
        StreamObserver responseObserver = args.getArgument(0, StreamObserver.class);
        return stub.downloadSnapshot(responseObserver);
    });
    // upload rpc mock
    Mockito.when(mClient.uploadSnapshot(any())).thenAnswer((args) -> {
        StreamObserver responseObserver = args.getArgument(0, StreamObserver.class);
        return stub.uploadSnapshot(responseObserver);
    });
    Mockito.doReturn(mClient).when(mLeaderSnapshotManager).getJournalServiceClient();
    for (int i = 0; i < numFollowers; i++) {
        Follower follower = new Follower(mClient);
        mFollowers.put(follower.getRaftPeerId(), follower);
    }
    List<QuorumServerInfo> quorumServerInfos = mFollowers.values().stream().map(follower -> {
        return QuorumServerInfo.newBuilder().setServerAddress(NetAddress.newBuilder().setHost(follower.mHost).setRpcPort(follower.mRpcPort)).build();
    }).collect(Collectors.toList());
    Mockito.when(mLeader.getQuorumServerInfoList()).thenReturn(quorumServerInfos);
    Mockito.when(mLeader.sendMessageAsync(any(), any())).thenAnswer((args) -> {
        RaftPeerId peerId = args.getArgument(0, RaftPeerId.class);
        Message message = args.getArgument(1, Message.class);
        JournalQueryRequest queryRequest = JournalQueryRequest.parseFrom(message.getContent().asReadOnlyByteBuffer());
        Message response = mFollowers.get(peerId).mSnapshotManager.handleRequest(queryRequest);
        RaftClientReply reply = Mockito.mock(RaftClientReply.class);
        Mockito.when(reply.getMessage()).thenReturn(response);
        return CompletableFuture.completedFuture(reply);
    });
}
Also used : StreamObserver(io.grpc.stub.StreamObserver) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) BufferUtils(alluxio.util.io.BufferUtils) UploadSnapshotPResponse(alluxio.grpc.UploadSnapshotPResponse) TermIndex(org.apache.ratis.server.protocol.TermIndex) ManagedChannel(io.grpc.ManagedChannel) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) RandomString(net.bytebuddy.utility.RandomString) RaftJournalServiceGrpc(alluxio.grpc.RaftJournalServiceGrpc) PropertyKey(alluxio.conf.PropertyKey) InProcessServerBuilder(io.grpc.inprocess.InProcessServerBuilder) ArrayList(java.util.ArrayList) WaitForOptions(alluxio.util.WaitForOptions) Message(org.apache.ratis.protocol.Message) StreamObserver(io.grpc.stub.StreamObserver) ConfigurationRule(alluxio.ConfigurationRule) JournalQueryRequest(alluxio.grpc.JournalQueryRequest) After(org.junit.After) Map(java.util.Map) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) RaftStorage(org.apache.ratis.server.storage.RaftStorage) Status(io.grpc.Status) Server(io.grpc.Server) InProcessChannelBuilder(io.grpc.inprocess.InProcessChannelBuilder) SingleFileSnapshotInfo(org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo) QuorumServerInfo(alluxio.grpc.QuorumServerInfo) ServerConfiguration(alluxio.conf.ServerConfiguration) NetAddress(alluxio.grpc.NetAddress) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) RaftServerConfigKeys(org.apache.ratis.server.RaftServerConfigKeys) FileUtils(org.apache.commons.io.FileUtils) Test(org.junit.Test) IOException(java.io.IOException) SimpleStateMachineStorage(org.apache.ratis.statemachine.impl.SimpleStateMachineStorage) Collectors(java.util.stream.Collectors) StatusRuntimeException(io.grpc.StatusRuntimeException) Mockito(org.mockito.Mockito) RaftStorageImpl(org.apache.ratis.server.storage.RaftStorageImpl) List(java.util.List) Rule(org.junit.Rule) UploadSnapshotPRequest(alluxio.grpc.UploadSnapshotPRequest) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) Assert(org.junit.Assert) CommonUtils(alluxio.util.CommonUtils) TemporaryFolder(org.junit.rules.TemporaryFolder) SnapshotData(alluxio.grpc.SnapshotData) Message(org.apache.ratis.protocol.Message) QuorumServerInfo(alluxio.grpc.QuorumServerInfo) JournalQueryRequest(alluxio.grpc.JournalQueryRequest) RandomString(net.bytebuddy.utility.RandomString) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) ManagedChannel(io.grpc.ManagedChannel) RaftJournalServiceGrpc(alluxio.grpc.RaftJournalServiceGrpc) RaftPeerId(org.apache.ratis.protocol.RaftPeerId)

Example 18 with RaftClientReply

use of org.apache.ratis.protocol.RaftClientReply in project alluxio by Alluxio.

the class RaftJournalWriterTest method setupRaftJournalWriter.

private void setupRaftJournalWriter() throws IOException {
    mClient = mock(RaftJournalAppender.class);
    RaftClientReply reply = RaftClientReply.newBuilder().setClientId(ClientId.randomId()).setServerId(RaftGroupMemberId.valueOf(RaftJournalUtils.getPeerId(new InetSocketAddress(1)), RaftGroupId.valueOf(UUID.fromString("02511d47-d67c-49a3-9011-abb3109a44c1")))).setCallId(1L).setSuccess(true).setMessage(Message.valueOf("mp")).setException(null).setLogIndex(1L).setCommitInfos(null).build();
    CompletableFuture<RaftClientReply> future = new CompletableFuture<RaftClientReply>() {

        @Override
        public boolean cancel(boolean mayInterruptIfRunning) {
            return false;
        }

        @Override
        public boolean isCancelled() {
            return false;
        }

        @Override
        public boolean isDone() {
            return false;
        }

        @Override
        public RaftClientReply get() {
            return reply;
        }

        @Override
        public RaftClientReply get(long timeout, TimeUnit unit) {
            return reply;
        }
    };
    when(mClient.sendAsync(any(), any())).thenReturn(future);
    mRaftJournalWriter = new RaftJournalWriter(1, mClient);
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) InetSocketAddress(java.net.InetSocketAddress) TimeUnit(java.util.concurrent.TimeUnit)

Aggregations

RaftClientReply (org.apache.ratis.protocol.RaftClientReply)18 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)11 RaftClient (org.apache.ratis.client.RaftClient)9 IOException (java.io.IOException)8 InetSocketAddress (java.net.InetSocketAddress)5 ArrayList (java.util.ArrayList)5 RaftPeer (org.apache.ratis.protocol.RaftPeer)5 Test (org.junit.Test)5 File (java.io.File)4 CompletableFuture (java.util.concurrent.CompletableFuture)4 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)4 ServerConfiguration (alluxio.conf.ServerConfiguration)3 JournalQueryRequest (alluxio.grpc.JournalQueryRequest)3 Map (java.util.Map)3 CompletionException (java.util.concurrent.CompletionException)3 Collectors (java.util.stream.Collectors)3 Message (org.apache.ratis.protocol.Message)3 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)3 AbortedException (alluxio.exception.status.AbortedException)2 AlluxioStatusException (alluxio.exception.status.AlluxioStatusException)2