Search in sources :

Example 31 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project Mycat_plus by coderczp.

the class DistributedSequenceHandler method initializeZK.

public void initializeZK(String zkAddress) {
    this.client = CuratorFrameworkFactory.newClient(zkAddress, new ExponentialBackoffRetry(1000, 3));
    this.client.start();
    try {
        if (client.checkExists().forPath(PATH.concat(INSTANCE_PATH)) == null) {
            client.create().creatingParentContainersIfNeeded().forPath(PATH.concat(INSTANCE_PATH));
        }
    } catch (Exception e) {
    // do nothing
    }
    this.leaderSelector = new LeaderSelector(client, PATH.concat(LEADER_PATH), this);
    this.leaderSelector.autoRequeue();
    this.leaderSelector.start();
    Runnable runnable = new Runnable() {

        @Override
        public void run() {
            try {
                while (leaderSelector.getLeader() == null) {
                    Thread.currentThread().yield();
                }
                if (!leaderSelector.hasLeadership()) {
                    isLeader = false;
                    if (slavePath != null && client.checkExists().forPath(slavePath) != null) {
                        return;
                    }
                    slavePath = client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL_SEQUENTIAL).forPath(PATH.concat("/instance/node"), "ready".getBytes());
                    while ("ready".equals(new String(client.getData().forPath(slavePath)))) {
                        Thread.currentThread().yield();
                    }
                    instanceId = Long.parseLong(new String(client.getData().forPath(slavePath)));
                    ready = true;
                }
            } catch (Exception e) {
                LOGGER.warn("Caught exception while handling zk!", e);
            }
        }
    };
    timerExecutor.scheduleAtFixedRate(runnable, 1L, 10L, TimeUnit.SECONDS);
}
Also used : ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) IOException(java.io.IOException) CancelLeadershipException(org.apache.curator.framework.recipes.leader.CancelLeadershipException)

Example 32 with ExponentialBackoffRetry

use of 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 33 with ExponentialBackoffRetry

use of 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 34 with ExponentialBackoffRetry

use of 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 35 with ExponentialBackoffRetry

use of 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)181 CuratorFramework (org.apache.curator.framework.CuratorFramework)107 RetryPolicy (org.apache.curator.RetryPolicy)46 Before (org.junit.Before)26 TestingCluster (org.apache.curator.test.TestingCluster)23 Test (org.testng.annotations.Test)23 IOException (java.io.IOException)18 TestingServer (org.apache.curator.test.TestingServer)18 Timing (org.apache.curator.test.Timing)16 CountDownLatch (java.util.concurrent.CountDownLatch)15 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)12 ACLProvider (org.apache.curator.framework.api.ACLProvider)12 Test (org.junit.Test)12 ArrayList (java.util.ArrayList)11 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 KeeperException (org.apache.zookeeper.KeeperException)8 HashMap (java.util.HashMap)7