Search in sources :

Example 81 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project metron by apache.

the class MaaSHandler method start.

public void start() throws Exception {
    if (client == null) {
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        client = CuratorFrameworkFactory.newClient(zkQuorum, retryPolicy);
        client.start();
    }
    config = ConfigUtil.INSTANCE.read(client, root, new MaaSConfig(), MaaSConfig.class);
    cache = new NodeCache(client, root);
    cache.getListenable().addListener(() -> {
        byte[] data = cache.getCurrentData().getData();
        Lock wLock = lock.writeLock();
        wLock.lock();
        try {
            config = _mapper.readValue(data, MaaSConfig.class);
        } finally {
            wLock.unlock();
        }
    });
    discoverer = new ServiceDiscoverer(client, config.getServiceRoot());
    discoverer.start();
}
Also used : ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) MaaSConfig(org.apache.metron.maas.config.MaaSConfig) RetryPolicy(org.apache.curator.RetryPolicy) ServiceDiscoverer(org.apache.metron.maas.discovery.ServiceDiscoverer) Lock(java.util.concurrent.locks.Lock) ReentrantReadWriteLock(java.util.concurrent.locks.ReentrantReadWriteLock) ReadWriteLock(java.util.concurrent.locks.ReadWriteLock)

Example 82 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project Saturn by vipshop.

the class ZookeeperRegistryCenter method buildZkClient.

private CuratorFramework buildZkClient() {
    if (zkConfig.isUseNestedZookeeper()) {
        NestedZookeeperServers.getInstance().startServerIfNotStarted(zkConfig.getNestedPort(), zkConfig.getNestedDataDir());
    }
    Builder builder = CuratorFrameworkFactory.builder().connectString(zkConfig.getServerLists()).retryPolicy(new ExponentialBackoffRetry(zkConfig.getBaseSleepTimeMilliseconds(), zkConfig.getMaxRetries(), zkConfig.getMaxSleepTimeMilliseconds())).namespace(zkConfig.getNamespace());
    if (0 != zkConfig.getSessionTimeoutMilliseconds()) {
        sessionTimeout = zkConfig.getSessionTimeoutMilliseconds();
    } else {
        sessionTimeout = calculateSessionTimeout();
    }
    builder.sessionTimeoutMs(sessionTimeout);
    int connectionTimeout;
    if (0 != zkConfig.getConnectionTimeoutMilliseconds()) {
        connectionTimeout = zkConfig.getConnectionTimeoutMilliseconds();
    } else {
        connectionTimeout = calculateConnectionTimeout();
    }
    builder.connectionTimeoutMs(connectionTimeout);
    if (!Strings.isNullOrEmpty(zkConfig.getDigest())) {
        builder.authorization("digest", zkConfig.getDigest().getBytes(Charset.forName(Constant.CHARSET_UTF8))).aclProvider(new ACLProvider() {

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

            @Override
            public List<ACL> getAclForPath(final String path) {
                return ZooDefs.Ids.CREATOR_ALL_ACL;
            }
        });
    }
    LogUtils.info(log, LogEvents.ExecutorEvent.COMMON, "Saturn job: zookeeper registry center init, server lists is: {}, connection_timeout: {}, session_timeout: {}, retry_times: {}", zkConfig.getServerLists(), connectionTimeout, sessionTimeout, zkConfig.getMaxRetries());
    return builder.build();
}
Also used : ACLProvider(org.apache.curator.framework.api.ACLProvider) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Builder(org.apache.curator.framework.CuratorFrameworkFactory.Builder) List(java.util.List)

Example 83 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project Saturn by vipshop.

the class ZookeeperRegistryCenterTest method testZkClientConfig.

@Test
public void testZkClientConfig() throws Exception {
    // default settings
    CuratorFramework client = initZk("127.0.0.1:" + PORT, "ut-ns");
    assertEquals(20000L, client.getZookeeperClient().getConnectionTimeoutMs());
    assertEquals(20000L, client.getZookeeperClient().getZooKeeper().getSessionTimeout());
    ExponentialBackoffRetry retryPolicy = (ExponentialBackoffRetry) client.getZookeeperClient().getRetryPolicy();
    assertEquals(3, retryPolicy.getN());
    // set VIP_SATURN_ZK_CLIENT_CONNECTION_TIMEOUT = true
    System.setProperty("VIP_SATURN_USE_UNSTABLE_NETWORK_SETTING", "true");
    SystemEnvProperties.loadProperties();
    client = initZk("127.0.0.1:" + PORT, "ut-ns");
    assertEquals(40000L, client.getZookeeperClient().getConnectionTimeoutMs());
    assertEquals(40000L, client.getZookeeperClient().getZooKeeper().getSessionTimeout());
    retryPolicy = (ExponentialBackoffRetry) client.getZookeeperClient().getRetryPolicy();
    assertEquals(9, retryPolicy.getN());
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Test(org.junit.Test)

Example 84 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project Saturn by vipshop.

the class CuratorRepositoryImpl method connect.

@Override
public CuratorFramework connect(final String connectString, final String namespace, final String digest) {
    Builder builder = CuratorFrameworkFactory.builder().connectString(connectString).sessionTimeoutMs(SESSION_TIMEOUT).connectionTimeoutMs(CONNECTION_TIMEOUT).retryPolicy(new ExponentialBackoffRetry(1000, 3, 3000));
    if (namespace != null) {
        builder.namespace(namespace);
    }
    if (!Strings.isNullOrEmpty(digest)) {
        builder.authorization("digest", digest.getBytes(Charset.forName("UTF-8"))).aclProvider(new ACLProvider() {

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

            @Override
            public List<ACL> getAclForPath(final String path) {
                return ZooDefs.Ids.CREATOR_ALL_ACL;
            }
        });
    }
    CuratorFramework client = builder.build();
    client.start();
    boolean established = false;
    try {
        established = client.blockUntilConnected(WAITING_SECONDS, TimeUnit.SECONDS);
    } catch (final InterruptedException ex) {
        Thread.currentThread().interrupt();
    }
    if (established) {
        return client;
    }
    CloseableUtils.closeQuietly(client);
    return null;
}
Also used : ACLProvider(org.apache.curator.framework.api.ACLProvider) CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Builder(org.apache.curator.framework.CuratorFrameworkFactory.Builder) List(java.util.List)

Example 85 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project Saturn by vipshop.

the class DemoJavaJob method main.

/**
 * @param args
 * @throws Exception
 */
public static void main(String[] args) throws Exception {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFramework client = CuratorFrameworkFactory.builder().connectString("localhost:2181").namespace("mydomain").sessionTimeoutMs(10000).retryPolicy(retryPolicy).build();
    client.start();
    addJavaJob(client, "demoJavaJob");
    System.out.println("done add a java-job.");
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) RetryPolicy(org.apache.curator.RetryPolicy)

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