Search in sources :

Example 76 with ExponentialBackoffRetry

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

the class MasterService method setupZookeeperClient.

/**
 * Create a Zookeeper client and create the control and state nodes if needed.
 *
 * @param config The service configuration.
 *
 * @return A zookeeper client.
 */
private ZooKeeperClient setupZookeeperClient(final MasterConfig config) {
    ACLProvider aclProvider = null;
    List<AuthInfo> authorization = null;
    final String masterUser = config.getZookeeperAclMasterUser();
    final String masterPassword = config.getZooKeeperAclMasterPassword();
    final String agentUser = config.getZookeeperAclAgentUser();
    final String agentDigest = config.getZooKeeperAclAgentDigest();
    if (!isNullOrEmpty(masterPassword)) {
        if (isNullOrEmpty(masterUser)) {
            throw new HeliosRuntimeException("Master username must be set if a password is set");
        }
        authorization = Lists.newArrayList(new AuthInfo("digest", String.format("%s:%s", masterUser, masterPassword).getBytes()));
    }
    if (config.isZooKeeperEnableAcls()) {
        if (isNullOrEmpty(masterUser) || isNullOrEmpty(masterPassword)) {
            throw new HeliosRuntimeException("ZooKeeper ACLs enabled but master username and/or password not set");
        }
        if (isNullOrEmpty(agentUser) || isNullOrEmpty(agentDigest)) {
            throw new HeliosRuntimeException("ZooKeeper ACLs enabled but agent username and/or digest not set");
        }
        aclProvider = heliosAclProvider(masterUser, digest(masterUser, masterPassword), agentUser, agentDigest);
    }
    final RetryPolicy zooKeeperRetryPolicy = new ExponentialBackoffRetry(1000, 3);
    final CuratorFramework curator = curatorClientFactory.newClient(config.getZooKeeperConnectionString(), config.getZooKeeperSessionTimeoutMillis(), config.getZooKeeperConnectionTimeoutMillis(), zooKeeperRetryPolicy, aclProvider, authorization);
    final ZooKeeperClient client = new DefaultZooKeeperClient(curator, config.getZooKeeperClusterId());
    client.start();
    zkRegistrar = ZooKeeperRegistrarService.newBuilder().setZooKeeperClient(client).setZooKeeperRegistrar(new MasterZooKeeperRegistrar(config.getName())).build();
    // place where we have access to the ACL provider.
    if (aclProvider != null) {
        // effects are limited to a spurious log line.
        try {
            final List<ACL> curAcls = client.getAcl("/");
            final List<ACL> wantedAcls = aclProvider.getAclForPath("/");
            if (!Sets.newHashSet(curAcls).equals(Sets.newHashSet(wantedAcls))) {
                log.info("Current ACL's on the zookeeper root node differ from desired, updating: {} -> {}", curAcls, wantedAcls);
                client.getCuratorFramework().setACL().withACL(wantedAcls).forPath("/");
            }
        } catch (Exception e) {
            log.error("Failed to get/set ACLs on the zookeeper root node", e);
        }
    }
    return client;
}
Also used : ACLProvider(org.apache.curator.framework.api.ACLProvider) AuthInfo(org.apache.curator.framework.AuthInfo) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) ACL(org.apache.zookeeper.data.ACL) DefaultZooKeeperClient(com.spotify.helios.servicescommon.coordination.DefaultZooKeeperClient) HeliosRuntimeException(com.spotify.helios.common.HeliosRuntimeException) ConfigurationException(io.dropwizard.configuration.ConfigurationException) IOException(java.io.IOException) CuratorFramework(org.apache.curator.framework.CuratorFramework) ZooKeeperClient(com.spotify.helios.servicescommon.coordination.ZooKeeperClient) DefaultZooKeeperClient(com.spotify.helios.servicescommon.coordination.DefaultZooKeeperClient) RetryPolicy(org.apache.curator.RetryPolicy)

Example 77 with ExponentialBackoffRetry

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

the class DeploymentGroupTest method setUp.

@Before
public void setUp() throws Exception {
    MockitoAnnotations.initMocks(this);
    zkServer = new TestingServer(true);
    final CuratorFramework curatorFramework = CuratorFrameworkFactory.builder().connectString(zkServer.getConnectString()).retryPolicy(new ExponentialBackoffRetry(100, 3)).build();
    client = new DefaultZooKeeperClient(curatorFramework);
    client.start();
}
Also used : TestingServer(org.apache.curator.test.TestingServer) CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) DefaultZooKeeperClient(com.spotify.helios.servicescommon.coordination.DefaultZooKeeperClient) Before(org.junit.Before)

Example 78 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project exhibitor by soabase.

the class Exhibitor method getLocalConnection.

/**
 * Return a connection ot the ZK instance (creating it if needed)
 *
 * @return connection
 * @throws IOException errors
 */
public synchronized CuratorFramework getLocalConnection() throws IOException {
    if (localConnection == null) {
        CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString("localhost:" + configManager.getConfig().getInt(IntConfigs.CLIENT_PORT)).sessionTimeoutMs(arguments.connectionTimeOutMs * 10).connectionTimeoutMs(arguments.connectionTimeOutMs).retryPolicy(new ExponentialBackoffRetry(1000, 3));
        if (arguments.aclProvider != null) {
            builder = builder.aclProvider(arguments.aclProvider);
        }
        localConnection = builder.build();
        localConnection.start();
    }
    return localConnection;
}
Also used : CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry)

Example 79 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project dubbo by alibaba.

the class ZKTools method testTreeCache.

public static void testTreeCache() throws Exception {
    CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", 60 * 1000, 60 * 1000, new ExponentialBackoffRetry(1000, 3));
    client.start();
    CountDownLatch latch = new CountDownLatch(1);
    TreeCache treeCache = TreeCache.newBuilder(client, "/dubbo/config").setCacheData(true).build();
    treeCache.start();
    treeCache.getListenable().addListener(new TreeCacheListener() {

        @Override
        public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
            TreeCacheEvent.Type type = event.getType();
            ChildData data = event.getData();
            if (type == TreeCacheEvent.Type.INITIALIZED) {
                latch.countDown();
            }
            System.out.println(data.getPath() + "\n");
            if (data.getPath().split("/").length == 5) {
                byte[] value = data.getData();
                String stringValue = new String(value, StandardCharsets.UTF_8);
                // fire event to all listeners
                Map<String, Object> added = null;
                Map<String, Object> changed = null;
                Map<String, Object> deleted = null;
                switch(type) {
                    case NODE_ADDED:
                        added = new HashMap<>(1);
                        added.put(pathToKey(data.getPath()), stringValue);
                        added.forEach((k, v) -> System.out.println(k + "  " + v));
                        break;
                    case NODE_REMOVED:
                        deleted = new HashMap<>(1);
                        deleted.put(pathToKey(data.getPath()), stringValue);
                        deleted.forEach((k, v) -> System.out.println(k + "  " + v));
                        break;
                    case NODE_UPDATED:
                        changed = new HashMap<>(1);
                        changed.put(pathToKey(data.getPath()), stringValue);
                        changed.forEach((k, v) -> System.out.println(k + "  " + v));
                }
            }
        }
    });
    latch.await();
/* Map<String, ChildData> dataMap = treeCache.getCurrentChildren("/dubbo/config");
        dataMap.forEach((k, v) -> {
            System.out.println(k);
            treeCache.getCurrentChildren("/dubbo/config/" + k).forEach((ck, cv) -> {
                System.out.println(ck);
            });
        });*/
}
Also used : TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) CuratorListener(org.apache.curator.framework.api.CuratorListener) HashMap(java.util.HashMap) NamedThreadFactory(org.apache.dubbo.common.utils.NamedThreadFactory) StandardCharsets(java.nio.charset.StandardCharsets) Executors(java.util.concurrent.Executors) StringUtils(org.apache.dubbo.common.utils.StringUtils) ChildData(org.apache.curator.framework.recipes.cache.ChildData) CountDownLatch(java.util.concurrent.CountDownLatch) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) List(java.util.List) CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) Map(java.util.Map) CuratorEvent(org.apache.curator.framework.api.CuratorEvent) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) ExecutorService(java.util.concurrent.ExecutorService) TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener) HashMap(java.util.HashMap) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) CountDownLatch(java.util.concurrent.CountDownLatch) CuratorFramework(org.apache.curator.framework.CuratorFramework) ChildData(org.apache.curator.framework.recipes.cache.ChildData) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) HashMap(java.util.HashMap) Map(java.util.Map)

Example 80 with ExponentialBackoffRetry

use of org.apache.flink.shaded.curator5.org.apache.curator.retry.ExponentialBackoffRetry in project dubbo by alibaba.

the class ZKTools method main.

public static void main(String[] args) throws Exception {
    client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", 60 * 1000, 60 * 1000, new ExponentialBackoffRetry(1000, 3));
    client.start();
    client.getCuratorListenable().addListener(new CuratorListener() {

        @Override
        public void eventReceived(CuratorFramework client, CuratorEvent event) throws Exception {
            System.out.println("event notification: " + event.getPath());
            System.out.println(event);
        }
    }, executor);
    testMigrationRule();
// tesConditionRule();
// testStartupConfig();
// testProviderConfig();
// testPathCache();
// testTreeCache();
// testCuratorListener();
// Thread.sleep(100000);
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) CuratorListener(org.apache.curator.framework.api.CuratorListener) CuratorEvent(org.apache.curator.framework.api.CuratorEvent)

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