Search in sources :

Example 91 with RaftClientReply

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

the class InstallSnapshotNotificationTests method testInstallSnapshotNotificationCount.

private void testInstallSnapshotNotificationCount(CLUSTER cluster) throws Exception {
    leaderSnapshotInfoRef.set(null);
    numSnapshotRequests.set(0);
    int i = 0;
    try {
        RaftTestUtil.waitForLeader(cluster);
        final RaftPeerId leaderId = cluster.getLeader().getId();
        // Let a few heartbeats pass.
        ONE_SECOND.sleep();
        Assert.assertEquals(0, numSnapshotRequests.get());
        // Generate data.
        try (final RaftClient client = cluster.createClient(leaderId)) {
            for (; i < 10; i++) {
                RaftClientReply reply = client.io().send(new RaftTestUtil.SimpleMessage("m" + i));
                Assert.assertTrue(reply.isSuccess());
            }
        }
        // Wait until index has been updated
        RaftTestUtil.waitFor(() -> cluster.getLeader().getStateMachine().getLastAppliedTermIndex().getIndex() == 20, 300, 15000);
        // Take snapshot and check result.
        long snapshotIndex = cluster.getLeader().getStateMachine().takeSnapshot();
        Assert.assertEquals(20, snapshotIndex);
        final SnapshotInfo leaderSnapshotInfo = cluster.getLeader().getStateMachine().getLatestSnapshot();
        Assert.assertEquals(20, leaderSnapshotInfo.getIndex());
        final boolean set = leaderSnapshotInfoRef.compareAndSet(null, leaderSnapshotInfo);
        Assert.assertTrue(set);
        // Wait for the snapshot to be done.
        final RaftServer.Division leader = cluster.getLeader();
        final long nextIndex = leader.getRaftLog().getNextIndex();
        Assert.assertEquals(21, nextIndex);
        // End index is exclusive.
        final List<File> snapshotFiles = RaftSnapshotBaseTest.getSnapshotFiles(cluster, 0, nextIndex);
        JavaUtils.attemptRepeatedly(() -> {
            Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
            return null;
        }, 10, ONE_SECOND, "snapshotFile.exist", LOG);
        // Clear all log files and reset cached log start index.
        long snapshotInstallIndex = leader.getRaftLog().onSnapshotInstalled(leader.getRaftLog().getLastCommittedIndex()).get();
        Assert.assertEquals(20, snapshotInstallIndex);
        // Check that logs are gone.
        Assert.assertEquals(0, LogSegmentPath.getLogSegmentPaths(leader.getRaftStorage()).size());
        Assert.assertEquals(RaftLog.INVALID_LOG_INDEX, leader.getRaftLog().getStartIndex());
        // Allow some heartbeats to go through, then make sure none of them had
        // snapshot requests.
        ONE_SECOND.sleep();
        Assert.assertEquals(0, numSnapshotRequests.get());
        // Make sure leader and followers are still up to date.
        for (RaftServer.Division follower : cluster.getFollowers()) {
            Assert.assertEquals(leader.getRaftLog().getNextIndex(), follower.getRaftLog().getNextIndex());
        }
        // Add two more peers who will need snapshots from the leader.
        final MiniRaftCluster.PeerChanges change = cluster.addNewPeers(2, true, true);
        // trigger setConfiguration
        cluster.setConfiguration(change.allPeersInNewConf);
        RaftServerTestUtil.waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null);
        // Generate more data.
        try (final RaftClient client = cluster.createClient(leader.getId())) {
            Assert.assertTrue(client.io().send(new RaftTestUtil.SimpleMessage("m" + i)).isSuccess());
        }
        // Make sure leader and followers are still up to date.
        for (RaftServer.Division follower : cluster.getFollowers()) {
            // Give follower slightly time to catch up
            RaftTestUtil.waitFor(() -> leader.getRaftLog().getNextIndex() == follower.getRaftLog().getNextIndex(), 300, 15000);
        }
        // Make sure each new peer got one snapshot notification.
        Assert.assertEquals(2, numSnapshotRequests.get());
    } finally {
        cluster.shutdown();
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) SnapshotInfo(org.apache.ratis.statemachine.SnapshotInfo) SingleFileSnapshotInfo(org.apache.ratis.statemachine.impl.SingleFileSnapshotInfo) MiniRaftCluster(org.apache.ratis.server.impl.MiniRaftCluster) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) File(java.io.File) RaftClient(org.apache.ratis.client.RaftClient)

Example 92 with RaftClientReply

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

the class MessageStreamApiTests method runTestStreamAsync.

void runTestStreamAsync(CLUSTER cluster) throws Exception {
    RaftTestUtil.waitForLeader(cluster);
    ByteString bytes = ByteString.EMPTY;
    for (int i = 0; i < 10; ) {
        final String s = (char) ('A' + i) + "1234567";
        LOG.info("s=" + s);
        final ByteString b = ByteString.copyFrom(s, StandardCharsets.UTF_8);
        Assert.assertEquals(8, b.size());
        for (int j = 0; j < 128; j++) {
            bytes = bytes.concat(b);
        }
        i++;
        Assert.assertEquals(i * SUBMESSAGE_SIZE.getSizeInt(), bytes.size());
    }
    try (RaftClient client = cluster.createClient()) {
        final RaftClientReply reply = client.getMessageStreamApi().streamAsync(Message.valueOf(bytes)).get();
        Assert.assertTrue(reply.isSuccess());
    }
    // check if all the parts are streamed as a single message.
    try (RaftClient client = cluster.createClient()) {
        final RaftClientReply reply = client.io().sendReadOnly(new SimpleMessage(bytes.toString(StandardCharsets.UTF_8)));
        Assert.assertTrue(reply.isSuccess());
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) RaftClient(org.apache.ratis.client.RaftClient)

Example 93 with RaftClientReply

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

the class MessageStreamApiTests method runTestStream.

void runTestStream(CLUSTER cluster) throws Exception {
    RaftTestUtil.waitForLeader(cluster);
    // stream multiple parts
    final int numParts = 9;
    final int endOfRequest = 6;
    final StringBuilder key = new StringBuilder();
    try (RaftClient client = cluster.createClient();
        MessageOutputStream out = client.getMessageStreamApi().stream()) {
        for (int i = 1; i <= numParts; i++) {
            key.append(i);
            out.sendAsync(new SimpleMessage(i + ""), i == endOfRequest);
        }
    }
    // check if all the parts are streamed as a single message.
    final String k = key.toString();
    try (RaftClient client = cluster.createClient()) {
        final String k1 = k.substring(0, endOfRequest);
        final RaftClientReply r1 = client.io().sendReadOnly(new SimpleMessage(k1));
        Assert.assertTrue(r1.isSuccess());
        final String k2 = k.substring(endOfRequest);
        final RaftClientReply r2 = client.io().sendReadOnly(new SimpleMessage(k2));
        Assert.assertTrue(r2.isSuccess());
    }
}
Also used : MessageOutputStream(org.apache.ratis.client.api.MessageOutputStream) RaftClientReply(org.apache.ratis.protocol.RaftClientReply) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) ByteString(org.apache.ratis.thirdparty.com.google.protobuf.ByteString) RaftClient(org.apache.ratis.client.RaftClient)

Example 94 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 95 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)

Aggregations

RaftClientReply (org.apache.ratis.protocol.RaftClientReply)96 RaftClient (org.apache.ratis.client.RaftClient)71 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)43 RaftServer (org.apache.ratis.server.RaftServer)40 IOException (java.io.IOException)32 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)25 CompletableFuture (java.util.concurrent.CompletableFuture)22 RaftPeer (org.apache.ratis.protocol.RaftPeer)22 Test (org.junit.Test)22 ArrayList (java.util.ArrayList)20 TimeDuration (org.apache.ratis.util.TimeDuration)18 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)14 RaftTestUtil (org.apache.ratis.RaftTestUtil)13 RaftProperties (org.apache.ratis.conf.RaftProperties)13 MiniRaftCluster (org.apache.ratis.server.impl.MiniRaftCluster)13 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)13 List (java.util.List)12 RetryPolicy (org.apache.ratis.retry.RetryPolicy)12 CompletionException (java.util.concurrent.CompletionException)11 ExecutionException (java.util.concurrent.ExecutionException)11