Search in sources :

Example 36 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project sofa-ark by alipay.

the class ZookeeperConfigActivator method start.

@Override
public void start(final PluginContext context) {
    if (!enableZkServer) {
        LOGGER.warn("config server is disabled.");
        return;
    }
    String config = ArkConfigs.getStringValue(Constants.CONFIG_SERVER_ADDRESS);
    RegistryConfig registryConfig = ZookeeperConfigurator.buildConfig(config);
    String address = registryConfig.getAddress();
    int idx = address.indexOf(Constants.ZOOKEEPER_CONTEXT_SPLIT);
    if (idx != -1) {
        rootPath = address.substring(idx);
        if (!rootPath.endsWith(Constants.ZOOKEEPER_CONTEXT_SPLIT)) {
            rootPath += Constants.ZOOKEEPER_CONTEXT_SPLIT;
        }
        address = address.substring(0, idx);
    }
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFrameworkFactory.Builder zkClientBuilder = CuratorFrameworkFactory.builder().connectString(address).sessionTimeoutMs(3 * registryConfig.getConnectTimeout()).connectionTimeoutMs(registryConfig.getConnectTimeout()).canBeReadOnly(false).retryPolicy(retryPolicy).defaultData(null);
    List<AuthInfo> authInfos = buildAuthInfo(registryConfig);
    if (!authInfos.isEmpty()) {
        zkClientBuilder = zkClientBuilder.aclProvider(getDefaultAclProvider()).authorization(authInfos);
    }
    zkClient = zkClientBuilder.build();
    zkClient.getConnectionStateListenable().addListener(new ConnectionStateListener() {

        @Override
        public void stateChanged(CuratorFramework client, ConnectionState newState) {
            LOGGER.info("Reconnect to zookeeper, re-register config resource.");
            if (newState == ConnectionState.RECONNECTED) {
                unSubscribeIpConfig();
                registryResource(ipResourcePath, CreateMode.EPHEMERAL);
                subscribeIpConfig();
            }
        }
    });
    zkClient.start();
    registryResource(bizResourcePath, CreateMode.PERSISTENT);
    registryResource(ipResourcePath, CreateMode.EPHEMERAL);
    subscribeIpConfig();
    subscribeBizConfig();
    registerEventHandler(context);
}
Also used : RegistryConfig(com.alipay.sofa.ark.config.RegistryConfig) AuthInfo(org.apache.curator.framework.AuthInfo) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) CuratorFramework(org.apache.curator.framework.CuratorFramework) ConnectionState(org.apache.curator.framework.state.ConnectionState) RetryPolicy(org.apache.curator.RetryPolicy) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener)

Example 37 with ExponentialBackoffRetry

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

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

use of org.apache.flink.shaded.curator5.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)

Example 40 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.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)

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