Search in sources :

Example 66 with TestingCluster

use of org.apache.curator.test.TestingCluster in project knox by apache.

the class KafkaZookeeperURLManagerTest method setup.

@Before
public void setup() throws Exception {
    cluster = new TestingCluster(3);
    cluster.start();
    CuratorFramework zooKeeperClient = CuratorFrameworkFactory.builder().connectString(cluster.getConnectString()).retryPolicy(new ExponentialBackoffRetry(1000, 3)).build();
    zooKeeperClient.start();
    zooKeeperClient.create().forPath("/brokers");
    zooKeeperClient.create().forPath("/brokers/ids");
    zooKeeperClient.close();
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Before(org.junit.Before)

Example 67 with TestingCluster

use of org.apache.curator.test.TestingCluster in project xian by happyyangyuan.

the class TestLeaderLatchCluster method testInCluster.

@Test
public void testInCluster() throws Exception {
    final int PARTICIPANT_QTY = 3;
    List<ClientAndLatch> clients = Lists.newArrayList();
    Timing timing = new Timing();
    TestingCluster cluster = new TestingCluster(PARTICIPANT_QTY);
    try {
        cluster.start();
        List<InstanceSpec> instances = Lists.newArrayList(cluster.getInstances());
        for (int i = 0; i < PARTICIPANT_QTY; ++i) {
            CuratorFramework client = CuratorFrameworkFactory.newClient(instances.get(i).getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
            LeaderLatch latch = new LeaderLatch(client, "/latch");
            clients.add(new ClientAndLatch(client, latch, i));
            client.start();
            latch.start();
        }
        ClientAndLatch leader = waitForALeader(clients, timing);
        Assert.assertNotNull(leader);
        cluster.killServer(instances.get(leader.index));
        Thread.sleep(timing.multiple(2).session());
        leader = waitForALeader(clients, timing);
        Assert.assertNotNull(leader);
        Assert.assertEquals(getLeaders(clients).size(), 1);
    } finally {
        for (ClientAndLatch client : clients) {
            CloseableUtils.closeQuietly(client.latch);
            CloseableUtils.closeQuietly(client.client);
        }
        CloseableUtils.closeQuietly(cluster);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) InstanceSpec(org.apache.curator.test.InstanceSpec) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Timing(org.apache.curator.test.Timing) Test(org.testng.annotations.Test)

Example 68 with TestingCluster

use of org.apache.curator.test.TestingCluster in project xian by happyyangyuan.

the class TestLeaderSelectorCluster method testRestart.

@Test
public void testRestart() throws Exception {
    final Timing timing = new Timing();
    CuratorFramework client = null;
    TestingCluster cluster = new TestingCluster(3);
    cluster.start();
    try {
        client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(1));
        client.start();
        final Semaphore semaphore = new Semaphore(0);
        LeaderSelectorListener listener = new LeaderSelectorListener() {

            @Override
            public void takeLeadership(CuratorFramework client) throws Exception {
                List<String> names = client.getChildren().forPath("/leader");
                Assert.assertTrue(names.size() > 0);
                semaphore.release();
            }

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
            }
        };
        LeaderSelector selector = new LeaderSelector(client, "/leader", listener);
        selector.autoRequeue();
        selector.start();
        Assert.assertTrue(timing.acquireSemaphore(semaphore));
        InstanceSpec connectionInstance = cluster.findConnectionInstance(client.getZookeeperClient().getZooKeeper());
        cluster.killServer(connectionInstance);
        Assert.assertTrue(timing.multiple(4).acquireSemaphore(semaphore));
    } finally {
        CloseableUtils.closeQuietly(client);
        CloseableUtils.closeQuietly(cluster);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) RetryOneTime(org.apache.curator.retry.RetryOneTime) InstanceSpec(org.apache.curator.test.InstanceSpec) Timing(org.apache.curator.test.Timing) Semaphore(java.util.concurrent.Semaphore) ConnectionState(org.apache.curator.framework.state.ConnectionState) Test(org.testng.annotations.Test)

Example 69 with TestingCluster

use of org.apache.curator.test.TestingCluster in project xian by happyyangyuan.

the class TestInterProcessSemaphoreCluster method testCluster.

@Test
public void testCluster() throws Exception {
    final int QTY = 20;
    final int OPERATION_TIME_MS = 1000;
    final String PATH = "/foo/bar/lock";
    ExecutorService executorService = Executors.newFixedThreadPool(QTY);
    ExecutorCompletionService<Void> completionService = new ExecutorCompletionService<Void>(executorService);
    final Timing timing = new Timing();
    TestingCluster cluster = new TestingCluster(3);
    List<SemaphoreClient> semaphoreClients = Lists.newArrayList();
    try {
        cluster.start();
        final AtomicInteger opCount = new AtomicInteger(0);
        for (int i = 0; i < QTY; ++i) {
            SemaphoreClient semaphoreClient = new SemaphoreClient(cluster.getConnectString(), PATH, new Callable<Void>() {

                @Override
                public Void call() throws Exception {
                    opCount.incrementAndGet();
                    Thread.sleep(OPERATION_TIME_MS);
                    return null;
                }
            });
            completionService.submit(semaphoreClient);
            semaphoreClients.add(semaphoreClient);
        }
        timing.forWaiting().sleepABit();
        Assert.assertNotNull(SemaphoreClient.getActiveClient());
        final CountDownLatch latch = new CountDownLatch(1);
        CuratorFramework client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.LOST) {
                    latch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(listener);
        client.start();
        try {
            client.getZookeeperClient().blockUntilConnectedOrTimedOut();
            cluster.stop();
            latch.await();
        } finally {
            CloseableUtils.closeQuietly(client);
        }
        long startTicks = System.currentTimeMillis();
        for (; ; ) {
            int thisOpCount = opCount.get();
            Thread.sleep(2 * OPERATION_TIME_MS);
            if (thisOpCount == opCount.get()) {
                // checking that the op count isn't increasing
                break;
            }
            Assert.assertTrue((System.currentTimeMillis() - startTicks) < timing.forWaiting().milliseconds());
        }
        int thisOpCount = opCount.get();
        Iterator<InstanceSpec> iterator = cluster.getInstances().iterator();
        cluster = new TestingCluster(iterator.next(), iterator.next());
        cluster.start();
        timing.forWaiting().sleepABit();
        startTicks = System.currentTimeMillis();
        for (; ; ) {
            Thread.sleep(2 * OPERATION_TIME_MS);
            if (opCount.get() > thisOpCount) {
                // checking that semaphore has started working again
                break;
            }
            Assert.assertTrue((System.currentTimeMillis() - startTicks) < timing.forWaiting().milliseconds());
        }
    } finally {
        for (SemaphoreClient semaphoreClient : semaphoreClients) {
            CloseableUtils.closeQuietly(semaphoreClient);
        }
        CloseableUtils.closeQuietly(cluster);
        executorService.shutdownNow();
    }
}
Also used : InstanceSpec(org.apache.curator.test.InstanceSpec) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ExecutorCompletionService(java.util.concurrent.ExecutorCompletionService) CountDownLatch(java.util.concurrent.CountDownLatch) IOException(java.io.IOException) CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ExecutorService(java.util.concurrent.ExecutorService) Timing(org.apache.curator.test.Timing) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.Test)

Example 70 with TestingCluster

use of org.apache.curator.test.TestingCluster in project activemq-artemis by apache.

the class CuratorDistributedLockTest method setupEnv.

@Override
public void setupEnv() throws Throwable {
    clusterSpecs = new InstanceSpec[zkNodes];
    for (int i = 0; i < zkNodes; i++) {
        clusterSpecs[i] = new InstanceSpec(tmpFolder.newFolder(), BASE_SERVER_PORT + i, -1, -1, true, -1, SERVER_TICK_MS, -1);
    }
    testingServer = new TestingCluster(clusterSpecs);
    testingServer.start();
    // start waits for quorumPeer!=null but not that it has started...
    Wait.waitFor(this::ensembleHasLeader);
    connectString = testingServer.getConnectString();
    super.setupEnv();
}
Also used : InstanceSpec(org.apache.curator.test.InstanceSpec) TestingCluster(org.apache.curator.test.TestingCluster)

Aggregations

TestingCluster (org.apache.curator.test.TestingCluster)97 CuratorFramework (org.apache.curator.framework.CuratorFramework)48 InstanceSpec (org.apache.curator.test.InstanceSpec)41 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)39 Before (org.junit.Before)30 Timing (org.apache.curator.test.Timing)21 Test (org.junit.jupiter.api.Test)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 ConnectionState (org.apache.curator.framework.state.ConnectionState)16 RetryOneTime (org.apache.curator.retry.RetryOneTime)15 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)12 HashMap (java.util.HashMap)10 PotentiallyGzippedCompressionProvider (org.apache.druid.curator.PotentiallyGzippedCompressionProvider)10 BeforeClass (org.junit.BeforeClass)10 Test (org.junit.Test)10 Test (org.testng.annotations.Test)10 ArrayList (java.util.ArrayList)9 TestBroker (org.apache.druid.indexing.kafka.test.TestBroker)8 ZkPathsConfig (org.apache.druid.server.initialization.ZkPathsConfig)8 GatewayConfig (org.apache.knox.gateway.config.GatewayConfig)8