Search in sources :

Example 71 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project Mycat-Server by MyCATApache.

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 72 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project Mycat-Server by MyCATApache.

the class ZKUtils method createConnection.

private static CuratorFramework createConnection() {
    String url = ZkConfig.getInstance().getZkURL();
    CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(url, new ExponentialBackoffRetry(100, 6));
    // start connection
    curatorFramework.start();
    // wait 3 second to establish connect
    try {
        curatorFramework.blockUntilConnected(3, TimeUnit.SECONDS);
        if (curatorFramework.getZookeeperClient().isConnected()) {
            return curatorFramework;
        }
    } catch (InterruptedException ignored) {
        Thread.currentThread().interrupt();
    }
    // fail situation
    curatorFramework.close();
    throw new RuntimeException("failed to connect to zookeeper service : " + url);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry)

Example 73 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project helios by spotify.

the class ZooKeeperAclInitializer method initializeAcl.

static void initializeAcl(final String zooKeeperConnectionString, final String zooKeeperClusterId, final String masterUser, final String masterPassword, final String agentUser, final String agentPassword) throws KeeperException {
    final ACLProvider aclProvider = heliosAclProvider(masterUser, digest(masterUser, masterPassword), agentUser, digest(agentUser, agentPassword));
    final List<AuthInfo> authorization = Lists.newArrayList(new AuthInfo("digest", String.format("%s:%s", masterUser, masterPassword).getBytes()));
    final RetryPolicy zooKeeperRetryPolicy = new ExponentialBackoffRetry(1000, 3);
    final CuratorFramework curator = new CuratorClientFactoryImpl().newClient(zooKeeperConnectionString, (int) TimeUnit.SECONDS.toMillis(60), (int) TimeUnit.SECONDS.toMillis(15), zooKeeperRetryPolicy, aclProvider, authorization);
    final ZooKeeperClient client = new DefaultZooKeeperClient(curator, zooKeeperClusterId);
    try {
        client.start();
        initializeAclRecursive(client, "/", aclProvider);
    } finally {
        client.close();
    }
}
Also used : ACLProvider(org.apache.curator.framework.api.ACLProvider) AuthInfo(org.apache.curator.framework.AuthInfo) CuratorFramework(org.apache.curator.framework.CuratorFramework) ZooKeeperClient(com.spotify.helios.servicescommon.coordination.ZooKeeperClient) DefaultZooKeeperClient(com.spotify.helios.servicescommon.coordination.DefaultZooKeeperClient) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) CuratorClientFactoryImpl(com.spotify.helios.servicescommon.coordination.CuratorClientFactoryImpl) DefaultZooKeeperClient(com.spotify.helios.servicescommon.coordination.DefaultZooKeeperClient) RetryPolicy(org.apache.curator.RetryPolicy)

Example 74 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project helios by spotify.

the class ZooKeeperClusterIdTest method testZooKeeperClient.

@Test
public void testZooKeeperClient() throws Exception {
    // Create the cluster ID node
    zk().curatorWithSuperAuth().newNamespaceAwareEnsurePath(Paths.configId(zkClusterId)).ensure(zk().curatorWithSuperAuth().getZookeeperClient());
    // We need to create a new curator because ZooKeeperClient will try to start it,
    // and zk().curator() has already been started.
    final ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3);
    final CuratorFramework curator = CuratorFrameworkFactory.builder().retryPolicy(retryPolicy).connectString(zk().connectString()).build();
    final ZooKeeperClient client = new DefaultZooKeeperClient(curator, zkClusterId);
    client.start();
    // This should work since the cluster ID exists
    client.create("/test");
    // Now let's remove the cluster ID
    client.delete(Paths.configId(zkClusterId));
    // Sleep so the watcher thread in ZooKeeperClient has a chance to update state
    Thread.sleep(500);
    // Try the same operation again, and it should fail this time
    try {
        client.ensurePath(Paths.configJobs());
        fail("ZooKeeper operation should have failed because cluster ID was removed");
    } catch (IllegalStateException ignore) {
    // ignored
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ZooKeeperClient(com.spotify.helios.servicescommon.coordination.ZooKeeperClient) DefaultZooKeeperClient(com.spotify.helios.servicescommon.coordination.DefaultZooKeeperClient) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) DefaultZooKeeperClient(com.spotify.helios.servicescommon.coordination.DefaultZooKeeperClient) Test(org.junit.Test)

Example 75 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project helios by spotify.

the class ZooKeeperTestingClusterManager method createCurator.

private CuratorFramework createCurator(final String connectString) {
    final ExponentialBackoffRetry retryPolicy = new ExponentialBackoffRetry(1000, 3);
    final CuratorFramework curator = CuratorFrameworkFactory.builder().connectString(connectString).retryPolicy(retryPolicy).authorization("digest", (SUPER_USER + ":" + SUPER_PASSWORD).getBytes()).build();
    curator.start();
    return curator;
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry)

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