Search in sources :

Example 36 with RaftClient

use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.

the class MiniRaftCluster method setConfiguration.

public void setConfiguration(RaftPeer... peers) throws IOException {
    try (RaftClient client = createClient()) {
        LOG.info("Start changing the configuration: {}", Arrays.asList(peers));
        final RaftClientReply reply = client.admin().setConfiguration(peers);
        Preconditions.assertTrue(reply.isSuccess());
    }
}
Also used : RaftClientReply(org.apache.ratis.protocol.RaftClientReply) RaftClient(org.apache.ratis.client.RaftClient)

Example 37 with RaftClient

use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.

the class RaftReconfigurationBaseTest method runTestOverlappedSetConfRequests.

void runTestOverlappedSetConfRequests(CLUSTER cluster) throws Exception {
    try {
        RaftTestUtil.waitForLeader(cluster);
        final RaftPeerId leaderId = cluster.getLeader().getId();
        RaftPeer[] newPeers = cluster.addNewPeers(2, true).allPeersInNewConf;
        // delay every peer's logSync so that the setConf request is delayed
        cluster.getPeers().forEach(peer -> logSyncDelay.setDelayMs(peer.getId().toString(), 1000));
        final CountDownLatch latch = new CountDownLatch(1);
        final RaftPeer[] peersInRequest2 = cluster.getPeers().toArray(new RaftPeer[0]);
        AtomicBoolean caughtException = new AtomicBoolean(false);
        new Thread(() -> {
            try (final RaftClient client2 = cluster.createClient(leaderId)) {
                latch.await();
                LOG.info("client2 starts to change conf");
                final RaftClientRpc sender2 = client2.getClientRpc();
                sender2.sendRequest(cluster.newSetConfigurationRequest(client2.getId(), leaderId, peersInRequest2));
            } catch (ReconfigurationInProgressException e) {
                caughtException.set(true);
            } catch (Exception e) {
                LOG.warn("Got unexpected exception when client2 changes conf", e);
            }
        }).start();
        AtomicBoolean confChanged = new AtomicBoolean(false);
        new Thread(() -> {
            try (final RaftClient client1 = cluster.createClient(leaderId)) {
                LOG.info("client1 starts to change conf");
                confChanged.set(client1.admin().setConfiguration(newPeers).isSuccess());
            } catch (IOException e) {
                LOG.warn("Got unexpected exception when client1 changes conf", e);
            }
        }).start();
        Thread.sleep(100);
        latch.countDown();
        for (int i = 0; i < 10 && !confChanged.get(); i++) {
            Thread.sleep(1000);
        }
        Assert.assertTrue(confChanged.get());
        Assert.assertTrue(caughtException.get());
    } finally {
        logSyncDelay.clear();
    }
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) IOException(java.io.IOException) RaftPeer(org.apache.ratis.protocol.RaftPeer) CountDownLatch(java.util.concurrent.CountDownLatch) ReconfigurationInProgressException(org.apache.ratis.protocol.exceptions.ReconfigurationInProgressException) RaftClient(org.apache.ratis.client.RaftClient) LeaderNotReadyException(org.apache.ratis.protocol.exceptions.LeaderNotReadyException) TimeoutException(java.util.concurrent.TimeoutException) ReconfigurationInProgressException(org.apache.ratis.protocol.exceptions.ReconfigurationInProgressException) ReconfigurationTimeoutException(org.apache.ratis.protocol.exceptions.ReconfigurationTimeoutException) IOException(java.io.IOException) RaftClientRpc(org.apache.ratis.client.RaftClientRpc)

Example 38 with RaftClient

use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.

the class RaftReconfigurationBaseTest method runTestReconfTimeout.

void runTestReconfTimeout(CLUSTER cluster) throws Exception {
    final RaftPeerId leaderId = RaftTestUtil.waitForLeader(cluster).getId();
    try (final RaftClient client = cluster.createClient(leaderId)) {
        PeerChanges c1 = cluster.addNewPeers(2, false);
        LOG.info("Start changing the configuration: {}", asList(c1.allPeersInNewConf));
        Assert.assertFalse(((RaftConfigurationImpl) cluster.getLeader().getRaftConf()).isTransitional());
        final RaftClientRpc sender = client.getClientRpc();
        final SetConfigurationRequest request = cluster.newSetConfigurationRequest(client.getId(), leaderId, c1.allPeersInNewConf);
        try {
            sender.sendRequest(request);
            Assert.fail("did not get expected exception");
        } catch (IOException e) {
            Assert.assertTrue("Got exception " + e, e instanceof ReconfigurationTimeoutException);
        }
        // the two new peers have not started yet, the bootstrapping must timeout
        LOG.info(cluster.printServers());
        // state so that we still get timeout instead of in-progress exception
        try {
            sender.sendRequest(request);
            Assert.fail("did not get expected exception");
        } catch (IOException e) {
            Assert.assertTrue("Got exception " + e, e instanceof ReconfigurationTimeoutException);
        }
        // start the two new peers
        LOG.info("Start new peers");
        for (RaftPeer np : c1.newPeers) {
            cluster.restartServer(np.getId(), false);
        }
        Assert.assertTrue(client.admin().setConfiguration(c1.allPeersInNewConf).isSuccess());
    }
}
Also used : ReconfigurationTimeoutException(org.apache.ratis.protocol.exceptions.ReconfigurationTimeoutException) PeerChanges(org.apache.ratis.server.impl.MiniRaftCluster.PeerChanges) RaftPeerId(org.apache.ratis.protocol.RaftPeerId) IOException(java.io.IOException) RaftPeer(org.apache.ratis.protocol.RaftPeer) SetConfigurationRequest(org.apache.ratis.protocol.SetConfigurationRequest) RaftClient(org.apache.ratis.client.RaftClient) RaftClientRpc(org.apache.ratis.client.RaftClientRpc)

Example 39 with RaftClient

use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.

the class WatchRequestTests method runSingleTest.

static void runSingleTest(CheckedConsumer<TestParameters, Exception> testCase, MiniRaftCluster cluster, Logger LOG) throws Exception {
    try (final RaftClient client = cluster.createClient(RaftTestUtil.waitForLeader(cluster).getId())) {
        final int[] numMessages = { 1 };
        for (int n : numMessages) {
            final TestParameters p = new TestParameters(n, client, cluster, LOG);
            LOG.info("{}) {}, {}", n, p, cluster.printServers());
            testCase.accept(p);
        }
    }
}
Also used : RaftClient(org.apache.ratis.client.RaftClient)

Example 40 with RaftClient

use of org.apache.ratis.client.RaftClient in project incubator-ratis by apache.

the class RaftSnapshotBaseTest method testInstallSnapshotDuringBootstrap.

/**
 * Test for install snapshot during a peer bootstrap: start a one node cluster
 * and let it generate a snapshot. Add another node and verify that the new
 * node installs a snapshot from the old node.
 */
@Test
public void testInstallSnapshotDuringBootstrap() throws Exception {
    int i = 0;
    try {
        RaftTestUtil.waitForLeader(cluster);
        final RaftPeerId leaderId = cluster.getLeader().getId();
        try (final RaftClient client = cluster.createClient(leaderId)) {
            for (; i < SNAPSHOT_TRIGGER_THRESHOLD * 2 - 1; i++) {
                RaftClientReply reply = client.io().send(new SimpleMessage("m" + i));
                Assert.assertTrue(reply.isSuccess());
            }
        }
        // wait for the snapshot to be done
        final long nextIndex = cluster.getLeader().getRaftLog().getNextIndex();
        LOG.info("nextIndex = {}", nextIndex);
        final List<File> snapshotFiles = getSnapshotFiles(cluster, nextIndex - SNAPSHOT_TRIGGER_THRESHOLD, nextIndex);
        JavaUtils.attemptRepeatedly(() -> {
            Assert.assertTrue(snapshotFiles.stream().anyMatch(RaftSnapshotBaseTest::exists));
            return null;
        }, 10, ONE_SECOND, "snapshotFile.exist", LOG);
        verifyTakeSnapshotMetric(cluster.getLeader());
        assertLeaderContent(cluster);
        // add two more peers
        String[] newPeers = new String[] { "s3", "s4" };
        MiniRaftCluster.PeerChanges change = cluster.addNewPeers(newPeers, true, false);
        // trigger setConfiguration
        cluster.setConfiguration(change.allPeersInNewConf);
        for (String newPeer : newPeers) {
            final RaftServer.Division s = cluster.getDivision(RaftPeerId.valueOf(newPeer));
            SimpleStateMachine4Testing simpleStateMachine = SimpleStateMachine4Testing.get(s);
            Assert.assertSame(LifeCycle.State.RUNNING, simpleStateMachine.getLifeCycleState());
        }
        // Verify installSnapshot counter on leader
        verifyInstallSnapshotMetric(cluster.getLeader());
        RaftServerTestUtil.waitAndCheckNewConf(cluster, change.allPeersInNewConf, 0, null);
    } finally {
        cluster.shutdown();
    }
}
Also used : RaftServer(org.apache.ratis.server.RaftServer) SimpleMessage(org.apache.ratis.RaftTestUtil.SimpleMessage) 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) Test(org.junit.Test) BaseTest(org.apache.ratis.BaseTest)

Aggregations

RaftClient (org.apache.ratis.client.RaftClient)134 RaftClientReply (org.apache.ratis.protocol.RaftClientReply)66 RaftPeerId (org.apache.ratis.protocol.RaftPeerId)54 SimpleMessage (org.apache.ratis.RaftTestUtil.SimpleMessage)46 RaftServer (org.apache.ratis.server.RaftServer)46 Test (org.junit.Test)44 IOException (java.io.IOException)41 RaftPeer (org.apache.ratis.protocol.RaftPeer)33 BaseTest (org.apache.ratis.BaseTest)27 RaftTestUtil (org.apache.ratis.RaftTestUtil)22 ArrayList (java.util.ArrayList)20 RaftClientRpc (org.apache.ratis.client.RaftClientRpc)20 RaftGroup (org.apache.ratis.protocol.RaftGroup)16 CompletableFuture (java.util.concurrent.CompletableFuture)14 RaftProperties (org.apache.ratis.conf.RaftProperties)14 File (java.io.File)13 SimpleStateMachine4Testing (org.apache.ratis.statemachine.SimpleStateMachine4Testing)13 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)11 RaftClientRequest (org.apache.ratis.protocol.RaftClientRequest)11 MiniRaftCluster (org.apache.ratis.server.impl.MiniRaftCluster)11