Search in sources :

Example 26 with ExponentialBackoffRetry

use of org.apache.curator.retry.ExponentialBackoffRetry in project incubator-gobblin by apache.

the class ZookeeperBasedJobLock method initializeCuratorFramework.

private static synchronized void initializeCuratorFramework(Properties properties) {
    if (curatorFrameworkShutdownHook == null) {
        curatorFrameworkShutdownHook = new CuratorFrameworkShutdownHook();
        Runtime.getRuntime().addShutdownHook(curatorFrameworkShutdownHook);
    }
    if (curatorFramework == null) {
        CuratorFramework newCuratorFramework = CuratorFrameworkFactory.builder().connectString(properties.getProperty(CONNECTION_STRING, CONNECTION_STRING_DEFAULT)).connectionTimeoutMs(getMilliseconds(properties, CONNECTION_TIMEOUT_SECONDS, CONNECTION_TIMEOUT_SECONDS_DEFAULT)).sessionTimeoutMs(getMilliseconds(properties, SESSION_TIMEOUT_SECONDS, SESSION_TIMEOUT_SECONDS_DEFAULT)).retryPolicy(new ExponentialBackoffRetry(getMilliseconds(properties, RETRY_BACKOFF_SECONDS, RETRY_BACKOFF_SECONDS_DEFAULT), getInt(properties, MAX_RETRY_COUNT, MAX_RETRY_COUNT_DEFAULT))).build();
        newCuratorFramework.getConnectionStateListenable().addListener(new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework curatorFramework, ConnectionState connectionState) {
                switch(connectionState) {
                    case LOST:
                        log.warn("Lost connection with zookeeper");
                        for (Map.Entry<String, JobLockEventListener> lockEventListener : lockEventListeners.entrySet()) {
                            log.warn("Informing job {} that lock was lost", lockEventListener.getKey());
                            lockEventListener.getValue().onLost();
                        }
                        break;
                    case SUSPENDED:
                        log.warn("Suspended connection with zookeeper");
                        for (Map.Entry<String, JobLockEventListener> lockEventListener : lockEventListeners.entrySet()) {
                            log.warn("Informing job {} that lock was suspended", lockEventListener.getKey());
                            lockEventListener.getValue().onLost();
                        }
                        break;
                    case CONNECTED:
                        log.info("Connected with zookeeper");
                        break;
                    case RECONNECTED:
                        log.warn("Regained connection with zookeeper");
                        break;
                    case READ_ONLY:
                        log.warn("Zookeeper connection went into read-only mode");
                        break;
                }
            }
        });
        newCuratorFramework.start();
        try {
            if (!newCuratorFramework.blockUntilConnected(getInt(properties, CONNECTION_TIMEOUT_SECONDS, CONNECTION_TIMEOUT_SECONDS_DEFAULT), TimeUnit.SECONDS)) {
                throw new RuntimeException("Time out while waiting to connect to zookeeper");
            }
        } catch (InterruptedException e) {
            throw new RuntimeException("Interrupted while waiting to connect to zookeeper");
        }
        curatorFramework = newCuratorFramework;
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener)

Example 27 with ExponentialBackoffRetry

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

the class Factory method getCuratorClient.

public static CuratorFramework getCuratorClient() throws ApplicationSettingsException {
    if (curatorClient == null) {
        synchronized (Factory.class) {
            if (curatorClient == null) {
                String connectionSting = ServerSettings.getZookeeperConnection();
                RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 5);
                curatorClient = CuratorFrameworkFactory.newClient(connectionSting, retryPolicy);
            }
        }
    }
    return curatorClient;
}
Also used : ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) LoggerFactory(org.slf4j.LoggerFactory) MessagingFactory(org.apache.airavata.messaging.core.MessagingFactory) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) RegistryFactory(org.apache.airavata.registry.core.experiment.catalog.impl.RegistryFactory) RetryPolicy(org.apache.curator.RetryPolicy)

Example 28 with ExponentialBackoffRetry

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

the class DbEventManagerZkUtils method getCuratorClient.

/**
 *  Get curatorFramework instance
 * @return
 * @throws ApplicationSettingsException
 */
public static CuratorFramework getCuratorClient() throws ApplicationSettingsException {
    if (curatorClient == null) {
        synchronized (DbEventManagerZkUtils.class) {
            if (curatorClient == null) {
                String connectionSting = ServerSettings.getZookeeperConnection();
                RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 5);
                curatorClient = CuratorFrameworkFactory.newClient(connectionSting, retryPolicy);
            }
        }
    }
    return curatorClient;
}
Also used : ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) RetryPolicy(org.apache.curator.RetryPolicy)

Example 29 with ExponentialBackoffRetry

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

the class OrchestratorServerHandler method startCurator.

private void startCurator() throws ApplicationSettingsException {
    String connectionSting = ServerSettings.getZookeeperConnection();
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 5);
    curatorClient = CuratorFrameworkFactory.newClient(connectionSting, retryPolicy);
    curatorClient.start();
}
Also used : ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) RetryPolicy(org.apache.curator.RetryPolicy)

Example 30 with ExponentialBackoffRetry

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

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)

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