Search in sources :

Example 81 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project dubbo by alibaba.

the class ZookeeperDynamicConfigurationTest method setUp.

@BeforeEach
public void setUp() throws Exception {
    zkServer = new TestingServer(zkServerPort, true);
    client = CuratorFrameworkFactory.newClient("127.0.0.1:" + zkServerPort, 60 * 1000, 60 * 1000, new ExponentialBackoffRetry(1000, 3));
    client.start();
    try {
        setData("/dubbo/config/dubbo/dubbo.properties", "The content from dubbo.properties");
        setData("/dubbo/config/appname", "The content from higher level node");
        setData("/dubbo/config/dubbo/never.change.DemoService.configurators", "Never change value from configurators");
    } catch (Exception e) {
        e.printStackTrace();
    }
    configUrl = URL.valueOf("zookeeper://127.0.0.1:" + zkServerPort);
    configuration = ExtensionLoader.getExtensionLoader(DynamicConfigurationFactory.class).getExtension(configUrl.getProtocol()).getDynamicConfiguration(configUrl);
}
Also used : TestingServer(org.apache.curator.test.TestingServer) DynamicConfigurationFactory(org.apache.dubbo.common.config.configcenter.DynamicConfigurationFactory) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) BeforeEach(org.junit.jupiter.api.BeforeEach)

Example 82 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry 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 83 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project xian by happyyangyuan.

the class TestWithCluster method testSessionSurvives.

@Test
public void testSessionSurvives() throws Exception {
    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 ExponentialBackoffRetry(100, 3));
        client.start();
        client.create().withMode(CreateMode.EPHEMERAL).forPath("/temp", "value".getBytes());
        Assert.assertNotNull(client.checkExists().forPath("/temp"));
        for (InstanceSpec spec : cluster.getInstances()) {
            cluster.killServer(spec);
            timing.forWaiting().sleepABit();
            cluster.restartServer(spec);
            timing.sleepABit();
        }
        timing.sleepABit();
        Assert.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) Test(org.testng.annotations.Test)

Example 84 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project xian by happyyangyuan.

the class TestFailedDeleteManager method testLostSession.

@Test
public void testLostSession() throws Exception {
    Timing timing = new Timing();
    CuratorFramework client = CuratorFrameworkFactory.newClient(server.getConnectString(), timing.session(), timing.connection(), new ExponentialBackoffRetry(100, 3));
    try {
        client.start();
        client.create().forPath("/test-me");
        final CountDownLatch latch = new CountDownLatch(1);
        final Semaphore semaphore = new Semaphore(0);
        ConnectionStateListener listener = new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                if ((newState == ConnectionState.LOST) || (newState == ConnectionState.SUSPENDED)) {
                    semaphore.release();
                } else if (newState == ConnectionState.RECONNECTED) {
                    latch.countDown();
                }
            }
        };
        client.getConnectionStateListenable().addListener(listener);
        server.stop();
        Assert.assertTrue(timing.acquireSemaphore(semaphore));
        try {
            client.delete().guaranteed().forPath("/test-me");
            Assert.fail();
        } catch (KeeperException.ConnectionLossException e) {
        // expected
        }
        Assert.assertTrue(timing.acquireSemaphore(semaphore));
        timing.sleepABit();
        server.restart();
        Assert.assertTrue(timing.awaitLatch(latch));
        timing.sleepABit();
        Assert.assertNull(client.checkExists().forPath("/test-me"));
    } finally {
        CloseableUtils.closeQuietly(client);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Timing(org.apache.curator.test.Timing) Semaphore(java.util.concurrent.Semaphore) ConnectionState(org.apache.curator.framework.state.ConnectionState) CountDownLatch(java.util.concurrent.CountDownLatch) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) KeeperException(org.apache.zookeeper.KeeperException) Test(org.testng.annotations.Test)

Example 85 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project xian by happyyangyuan.

the class TestLeaderAcls method testAclErrorWithLeader.

@Test(description = "Validation test for CURATOR-365")
public void testAclErrorWithLeader() throws Exception {
    ACLProvider provider = new ACLProvider() {

        @Override
        public List<ACL> getDefaultAcl() {
            return ZooDefs.Ids.OPEN_ACL_UNSAFE;
        }

        @Override
        public List<ACL> getAclForPath(String path) {
            if (path.equals("/base")) {
                try {
                    String testDigest = DigestAuthenticationProvider.generateDigest("test:test");
                    return Collections.singletonList(new ACL(ZooDefs.Perms.ALL, new Id("digest", testDigest)));
                } catch (NoSuchAlgorithmException e) {
                    e.printStackTrace();
                }
            }
            return getDefaultAcl();
        }
    };
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(timing.milliseconds(), 3);
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString(server.getConnectString()).retryPolicy(retryPolicy).aclProvider(provider).authorization("digest", "test:test".getBytes());
    CuratorFramework client = builder.build();
    LeaderLatch latch = null;
    try {
        client.start();
        latch = new LeaderLatch(client, "/base");
        latch.start();
        Assert.assertTrue(latch.await(timing.forWaiting().seconds(), TimeUnit.SECONDS));
        latch.close();
        latch = null;
        CuratorFramework noAuthClient = CuratorFrameworkFactory.newClient(server.getConnectString(), retryPolicy);
        try {
            noAuthClient.start();
            final CountDownLatch noAuthLatch = new CountDownLatch(1);
            UnhandledErrorListener listener = new UnhandledErrorListener() {

                @Override
                public void unhandledError(String message, Throwable e) {
                    if (e instanceof KeeperException.NoAuthException) {
                        noAuthLatch.countDown();
                    }
                }
            };
            noAuthClient.getUnhandledErrorListenable().addListener(listener);
            // use a path below "base" as noAuthClient is not authorized to create nodes in "/base"
            // but also making sure that the code goes through the backgroundCreateParentsThenNode() codepath
            latch = new LeaderLatch(noAuthClient, "/base/second");
            latch.start();
            Assert.assertTrue(timing.awaitLatch(noAuthLatch));
        } finally {
            CloseableUtils.closeQuietly(noAuthClient);
        }
    } finally {
        CloseableUtils.closeQuietly(latch);
        CloseableUtils.closeQuietly(client);
    }
}
Also used : ACLProvider(org.apache.curator.framework.api.ACLProvider) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ACL(org.apache.zookeeper.data.ACL) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) CountDownLatch(java.util.concurrent.CountDownLatch) CuratorFramework(org.apache.curator.framework.CuratorFramework) Id(org.apache.zookeeper.data.Id) UnhandledErrorListener(org.apache.curator.framework.api.UnhandledErrorListener) RetryPolicy(org.apache.curator.RetryPolicy) Test(org.testng.annotations.Test)

Aggregations

ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)189 CuratorFramework (org.apache.curator.framework.CuratorFramework)113 RetryPolicy (org.apache.curator.RetryPolicy)46 Before (org.junit.Before)31 TestingCluster (org.apache.curator.test.TestingCluster)28 Test (org.testng.annotations.Test)23 TestingServer (org.apache.curator.test.TestingServer)19 IOException (java.io.IOException)18 Timing (org.apache.curator.test.Timing)16 CountDownLatch (java.util.concurrent.CountDownLatch)15 ArrayList (java.util.ArrayList)14 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)12 ACLProvider (org.apache.curator.framework.api.ACLProvider)12 Test (org.junit.Test)12 ConnectionState (org.apache.curator.framework.state.ConnectionState)11 ExecutorService (java.util.concurrent.ExecutorService)10 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)10 TestingServerStarter (io.pravega.test.common.TestingServerStarter)9 HashMap (java.util.HashMap)8 KeeperException (org.apache.zookeeper.KeeperException)8