Search in sources :

Example 26 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(1);
    cluster.start();
    try (CuratorFramework zooKeeperClient = CuratorFrameworkFactory.builder().connectString(cluster.getConnectString()).retryPolicy(new ExponentialBackoffRetry(1000, 3)).build()) {
        zooKeeperClient.start();
        assertTrue(zooKeeperClient.blockUntilConnected(10, TimeUnit.SECONDS));
        zooKeeperClient.create().forPath("/brokers");
        zooKeeperClient.create().forPath("/brokers/ids");
    }
}
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 27 with TestingCluster

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

the class AtlasZookeeperURLManagerTest method setUp.

@Before
public void setUp() throws Exception {
    cluster = new TestingCluster(1);
    cluster.start();
    try (CuratorFramework zooKeeperClient = CuratorFrameworkFactory.builder().connectString(cluster.getConnectString()).retryPolicy(new ExponentialBackoffRetry(1000, 3)).build()) {
        zooKeeperClient.start();
        assertTrue(zooKeeperClient.blockUntilConnected(10, TimeUnit.SECONDS));
        zooKeeperClient.create().forPath("/apache_atlas");
        zooKeeperClient.create().forPath("/apache_atlas/active_server_info");
        zooKeeperClient.setData().forPath("/apache_atlas/active_server_info", atlasNode1.getBytes(StandardCharsets.UTF_8));
    }
    setAtlasActiveHostURLInZookeeper(atlasNode1);
    manager = new AtlasZookeeperURLManager();
    HaServiceConfig config = new DefaultHaServiceConfig("ATLAS-API");
    config.setEnabled(true);
    config.setZookeeperEnsemble(cluster.getConnectString());
    config.setZookeeperNamespace("apache_atlas");
    manager.setConfig(config);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) HaServiceConfig(org.apache.knox.gateway.ha.provider.HaServiceConfig) Before(org.junit.Before)

Example 28 with TestingCluster

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

the class HBaseZookeeperURLManagerTest method setUp.

@Before
public void setUp() throws Exception {
    cluster = new TestingCluster(1);
    cluster.start();
}
Also used : TestingCluster(org.apache.curator.test.TestingCluster) Before(org.junit.Before)

Example 29 with TestingCluster

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

the class TestReadOnly method testReadOnly.

@Test
public void testReadOnly() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = null;
    TestingCluster cluster = new TestingCluster(2);
    try {
        cluster.start();
        client = CuratorFrameworkFactory.builder().connectString(cluster.getConnectString()).canBeReadOnly(true).connectionTimeoutMs(timing.connection()).sessionTimeoutMs(timing.session()).retryPolicy(new ExponentialBackoffRetry(100, 3)).build();
        client.start();
        client.create().forPath("/test");
        final CountDownLatch readOnlyLatch = new CountDownLatch(1);
        final CountDownLatch reconnectedLatch = new CountDownLatch(1);
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if (newState == ConnectionState.READ_ONLY) {
                    readOnlyLatch.countDown();
                } else if (newState == ConnectionState.RECONNECTED) {
                    reconnectedLatch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(listener);
        InstanceSpec ourInstance = cluster.findConnectionInstance(client.getZookeeperClient().getZooKeeper());
        Iterator<InstanceSpec> iterator = cluster.getInstances().iterator();
        InstanceSpec killInstance = iterator.next();
        if (killInstance.equals(ourInstance)) {
            // kill the instance we're not connected to
            killInstance = iterator.next();
        }
        cluster.killServer(killInstance);
        Assert.assertEquals(reconnectedLatch.getCount(), 1);
        Assert.assertTrue(timing.awaitLatch(readOnlyLatch));
        Assert.assertEquals(reconnectedLatch.getCount(), 1);
        cluster.restartServer(killInstance);
        Assert.assertTrue(timing.awaitLatch(reconnectedLatch));
    } 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.testng.annotations.Test)

Example 30 with TestingCluster

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

the class TestReadOnly method testConnectionStateNewClient.

@Test
public void testConnectionStateNewClient() throws Exception {
    Timing timing = new Timing();
    TestingCluster cluster = new TestingCluster(3);
    CuratorFramework client = null;
    try {
        cluster.start();
        client = CuratorFrameworkFactory.newClient(cluster.getConnectString(), timing.session(), timing.connection(), new RetryOneTime(100));
        client.start();
        client.checkExists().forPath("/");
        client.close();
        client = null;
        Iterator<InstanceSpec> iterator = cluster.getInstances().iterator();
        for (int i = 0; i < 2; ++i) {
            cluster.killServer(iterator.next());
        }
        client = CuratorFrameworkFactory.builder().connectString(cluster.getConnectString()).sessionTimeoutMs(timing.session()).connectionTimeoutMs(timing.connection()).retryPolicy(new RetryNTimes(3, timing.milliseconds())).canBeReadOnly(true).build();
        final BlockingQueue<ConnectionState> states = Queues.newLinkedBlockingQueue();
        client.getConnectionStateListenable().addListener(new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                states.add(newState);
            }
        });
        client.start();
        client.checkExists().forPath("/");
        ConnectionState state = states.poll(timing.forWaiting().milliseconds(), TimeUnit.MILLISECONDS);
        Assert.assertEquals(state, ConnectionState.READ_ONLY);
    } finally {
        CloseableUtils.closeQuietly(client);
        CloseableUtils.closeQuietly(cluster);
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) 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) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) Test(org.testng.annotations.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