Search in sources :

Example 46 with TestingCluster

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

the class TestReconfiguration method testAddAsync.

@Test
public void testAddAsync() throws Exception {
    try (CuratorFramework client = newClient()) {
        client.start();
        QuorumVerifier oldConfig = toQuorumVerifier(client.getConfig().forEnsemble());
        assertConfig(oldConfig, cluster.getInstances());
        CountDownLatch latch = setChangeWaiter(client);
        try (TestingCluster newCluster = new TestingCluster(TestingCluster.makeSpecs(1, false))) {
            newCluster.start();
            final CountDownLatch callbackLatch = new CountDownLatch(1);
            BackgroundCallback callback = new BackgroundCallback() {

                @Override
                public void processResult(CuratorFramework client, CuratorEvent event) throws Exception {
                    if (event.getType() == CuratorEventType.RECONFIG) {
                        callbackLatch.countDown();
                    }
                }
            };
            client.reconfig().inBackground(callback).joining(toReconfigSpec(newCluster.getInstances())).fromConfig(oldConfig.getVersion()).forEnsemble();
            assertTrue(timing.awaitLatch(callbackLatch));
            assertTrue(timing.awaitLatch(latch));
            byte[] newConfigData = client.getConfig().forEnsemble();
            QuorumVerifier newConfig = toQuorumVerifier(newConfigData);
            List<InstanceSpec> newInstances = Lists.newArrayList(cluster.getInstances());
            newInstances.addAll(newCluster.getInstances());
            assertConfig(newConfig, newInstances);
            assertEquals(EnsembleTracker.configToConnectionString(newConfig), ensembleProvider.getConnectionString());
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) InstanceSpec(org.apache.curator.test.InstanceSpec) BackgroundCallback(org.apache.curator.framework.api.BackgroundCallback) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) CountDownLatch(java.util.concurrent.CountDownLatch) QuorumVerifier(org.apache.zookeeper.server.quorum.flexible.QuorumVerifier) Test(org.junit.jupiter.api.Test)

Example 47 with TestingCluster

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

the class TestWithCluster method testSessionSurvives.

@Test
public void testSessionSurvives() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = null;
    TestingCluster cluster = createAndStartCluster(3);
    try {
        client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
        client.start();
        final CountDownLatch reconnectedLatch = new CountDownLatch(1);
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.RECONNECTED) {
                    reconnectedLatch.countDown();
                    ;
                }
            }
        };
        client.getConnectionStateListenable().addListener(listener);
        client.create().withMode(CreateMode.EPHEMERAL).forPath("/temp", "value".getBytes());
        assertNotNull(client.checkExists().forPath("/temp"));
        for (InstanceSpec spec : cluster.getInstances()) {
            cluster.killServer(spec);
            timing.sleepABit();
            cluster.restartServer(spec);
            timing.sleepABit();
        }
        assertTrue(timing.awaitLatch(reconnectedLatch));
        assertNotNull(client.checkExists().forPath("/temp"));
    } finally {
        CloseableUtils.closeQuietly(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) ConnectionState(org.apache.curator.framework.state.ConnectionState) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.junit.jupiter.api.Test)

Example 48 with TestingCluster

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

the class TestReconfiguration method testAddAndRemove.

@Test
public void testAddAndRemove() throws Exception {
    try (CuratorFramework client = newClient()) {
        client.start();
        QuorumVerifier oldConfig = toQuorumVerifier(client.getConfig().forEnsemble());
        assertConfig(oldConfig, cluster.getInstances());
        CountDownLatch latch = setChangeWaiter(client);
        try (TestingCluster newCluster = new TestingCluster(TestingCluster.makeSpecs(1, false))) {
            newCluster.start();
            Collection<InstanceSpec> oldInstances = cluster.getInstances();
            InstanceSpec us = cluster.findConnectionInstance(client.getZookeeperClient().getZooKeeper());
            InstanceSpec removeSpec = oldInstances.iterator().next();
            if (us.equals(removeSpec)) {
                Iterator<InstanceSpec> iterator = oldInstances.iterator();
                iterator.next();
                removeSpec = iterator.next();
            }
            Collection<InstanceSpec> instances = newCluster.getInstances();
            client.reconfig().leaving(Integer.toString(removeSpec.getServerId())).joining(toReconfigSpec(instances)).fromConfig(oldConfig.getVersion()).forEnsemble();
            assertTrue(timing.awaitLatch(latch));
            byte[] newConfigData = client.getConfig().forEnsemble();
            QuorumVerifier newConfig = toQuorumVerifier(newConfigData);
            ArrayList<InstanceSpec> newInstances = Lists.newArrayList(oldInstances);
            newInstances.addAll(instances);
            newInstances.remove(removeSpec);
            assertConfig(newConfig, newInstances);
            assertEquals(EnsembleTracker.configToConnectionString(newConfig), ensembleProvider.getConnectionString());
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) InstanceSpec(org.apache.curator.test.InstanceSpec) CountDownLatch(java.util.concurrent.CountDownLatch) QuorumVerifier(org.apache.zookeeper.server.quorum.flexible.QuorumVerifier) Test(org.junit.jupiter.api.Test)

Example 49 with TestingCluster

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

the class TestReconfiguration method testAddWithoutEnsembleTracker.

@Test
public void testAddWithoutEnsembleTracker() throws Exception {
    final String initialClusterCS = cluster.getConnectString();
    try (CuratorFramework client = newClient(cluster.getConnectString(), false)) {
        assertEquals(((CuratorFrameworkImpl) client).getEnsembleTracker(), null);
        client.start();
        QuorumVerifier oldConfig = toQuorumVerifier(client.getConfig().forEnsemble());
        assertConfig(oldConfig, cluster.getInstances());
        CountDownLatch latch = setChangeWaiter(client);
        try (TestingCluster newCluster = new TestingCluster(TestingCluster.makeSpecs(1, false))) {
            newCluster.start();
            client.reconfig().joining(toReconfigSpec(newCluster.getInstances())).fromConfig(oldConfig.getVersion()).forEnsemble();
            assertTrue(timing.awaitLatch(latch));
            byte[] newConfigData = client.getConfig().forEnsemble();
            QuorumVerifier newConfig = toQuorumVerifier(newConfigData);
            List<InstanceSpec> newInstances = Lists.newArrayList(cluster.getInstances());
            newInstances.addAll(newCluster.getInstances());
            assertConfig(newConfig, newInstances);
            assertEquals(ensembleProvider.getConnectionString(), initialClusterCS);
            assertNotEquals(EnsembleTracker.configToConnectionString(newConfig), ensembleProvider.getConnectionString());
            assertEquals(client.getZookeeperClient().getCurrentConnectionString(), initialClusterCS);
            final CountDownLatch reconnectLatch = new CountDownLatch(1);
            client.getConnectionStateListenable().addListener((cfClient, newState) -> {
                if (newState == ConnectionState.RECONNECTED)
                    reconnectLatch.countDown();
            });
            client.getZookeeperClient().getZooKeeper().getTestable().injectSessionExpiration();
            assertTrue(reconnectLatch.await(2, TimeUnit.SECONDS));
            assertEquals(client.getZookeeperClient().getCurrentConnectionString(), initialClusterCS);
            assertEquals(ensembleProvider.getConnectionString(), initialClusterCS);
            newConfigData = client.getConfig().forEnsemble();
            assertNotEquals(EnsembleTracker.configToConnectionString(newConfig), ensembleProvider.getConnectionString());
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) InstanceSpec(org.apache.curator.test.InstanceSpec) CountDownLatch(java.util.concurrent.CountDownLatch) QuorumVerifier(org.apache.zookeeper.server.quorum.flexible.QuorumVerifier) Test(org.junit.jupiter.api.Test)

Example 50 with TestingCluster

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

the class TestCuratorCacheConsistency method testConsistencyAfterSimulation.

@Test
public void testConsistencyAfterSimulation() throws Exception {
    int clientQty = random.nextInt(10, 20);
    int maxDepth = random.nextInt(5, 10);
    log.info("clientQty: {}, maxDepth: {}", clientQty, maxDepth);
    List<Client> clients = Collections.emptyList();
    Map<String, String> actualTree;
    AtomicReference<Exception> errorSignal = new AtomicReference<>();
    try (TestingCluster cluster = new TestingCluster(clusterSize)) {
        cluster.start();
        initializeBasePath(cluster);
        try {
            clients = buildClients(cluster, clientQty, errorSignal);
            workLoop(cluster, clients, maxDepth, errorSignal);
            log.info("Test complete - sleeping to allow events to complete");
            timing.sleepABit();
        } finally {
            clients.forEach(Client::close);
        }
        actualTree = buildActual(cluster);
    }
    log.info("client qty: {}", clientQty);
    Map<Integer, List<String>> errorsList = clients.stream().map(client -> findErrors(client, actualTree)).filter(errorsEntry -> !errorsEntry.getValue().isEmpty()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue));
    if (!errorsList.isEmpty()) {
        log.error("{} clients had errors", errorsList.size());
        errorsList.forEach((index, errorList) -> {
            log.error("Client {}", index);
            errorList.forEach(log::error);
            log.error("");
        });
        fail("Errors found");
    }
}
Also used : Assertions.fail(org.junit.jupiter.api.Assertions.fail) IntStream(java.util.stream.IntStream) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) Arrays(java.util.Arrays) LoggerFactory(org.slf4j.LoggerFactory) DO_NOT_CLEAR_ON_CLOSE(org.apache.curator.framework.recipes.cache.CuratorCache.Options.DO_NOT_CLEAR_ON_CLOSE) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) ArrayList(java.util.ArrayList) ZKPaths(org.apache.curator.utils.ZKPaths) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Duration(java.time.Duration) Map(java.util.Map) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) Tag(org.junit.jupiter.api.Tag) Logger(org.slf4j.Logger) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) AbstractMap(java.util.AbstractMap) List(java.util.List) CuratorFramework(org.apache.curator.framework.CuratorFramework) InstanceSpec(org.apache.curator.test.InstanceSpec) Closeable(java.io.Closeable) TestingCluster(org.apache.curator.test.TestingCluster) CuratorCacheListener.builder(org.apache.curator.framework.recipes.cache.CuratorCacheListener.builder) CuratorTestBase(org.apache.curator.test.compatibility.CuratorTestBase) Collections(java.util.Collections) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestingCluster(org.apache.curator.test.TestingCluster) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap) Test(org.junit.jupiter.api.Test)

Aggregations

TestingCluster (org.apache.curator.test.TestingCluster)94 CuratorFramework (org.apache.curator.framework.CuratorFramework)46 InstanceSpec (org.apache.curator.test.InstanceSpec)40 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)39 Before (org.junit.Before)30 Timing (org.apache.curator.test.Timing)21 CountDownLatch (java.util.concurrent.CountDownLatch)20 Test (org.junit.jupiter.api.Test)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.testng.annotations.Test)10 ArrayList (java.util.ArrayList)9 Test (org.junit.Test)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