Search in sources :

Example 96 with CuratorFramework

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework in project knox by apache.

the class RemoteConfigurationRegistryClientServiceTest method testZooKeeperWithSimpleRegistryConfig.

/**
 * Test a configuration for a secure remote registry, included in the gateway configuration.
 */
@Test
public void testZooKeeperWithSimpleRegistryConfig() throws Exception {
    final String AUTH_TYPE = "digest";
    final String REGISTRY_CLIENT_NAME = "zk-registry-name";
    final String PRINCIPAL = "knox";
    final String PWD = "knoxtest";
    final String CRED_ALIAS = "zkCredential";
    // Configure and start a secure ZK cluster
    TestingCluster zkCluster = setupAndStartSecureTestZooKeeper(PRINCIPAL, PWD);
    try {
        // Create the setup client for the test cluster, and initialize the test znodes
        CuratorFramework setupClient = initializeTestClientAndZNodes(zkCluster, PRINCIPAL);
        // Mock configuration
        GatewayConfig config = EasyMock.createNiceMock(GatewayConfig.class);
        final String registryConfigValue = GatewayConfig.REMOTE_CONFIG_REGISTRY_TYPE + "=" + ZooKeeperClientService.TYPE + ";" + GatewayConfig.REMOTE_CONFIG_REGISTRY_ADDRESS + "=" + zkCluster.getConnectString() + ";" + GatewayConfig.REMOTE_CONFIG_REGISTRY_AUTH_TYPE + "=" + AUTH_TYPE + ";" + GatewayConfig.REMOTE_CONFIG_REGISTRY_PRINCIPAL + "=" + PRINCIPAL + ";" + GatewayConfig.REMOTE_CONFIG_REGISTRY_CREDENTIAL_ALIAS + "=" + CRED_ALIAS;
        EasyMock.expect(config.getRemoteRegistryConfiguration(REGISTRY_CLIENT_NAME)).andReturn(registryConfigValue).anyTimes();
        EasyMock.expect(config.getRemoteRegistryConfigurationNames()).andReturn(Collections.singletonList(REGISTRY_CLIENT_NAME)).anyTimes();
        EasyMock.replay(config);
        doTestZooKeeperClient(setupClient, REGISTRY_CLIENT_NAME, config, CRED_ALIAS, PWD);
    } finally {
        zkCluster.stop();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) TestingCluster(org.apache.curator.test.TestingCluster) GatewayConfig(org.apache.knox.gateway.config.GatewayConfig) Test(org.junit.Test)

Example 97 with CuratorFramework

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework in project twister2 by DSC-SPIDAL.

the class ZKUtil method connectToServer.

/**
 * connect to ZooKeeper server
 * @param config
 * @return
 */
public static CuratorFramework connectToServer(Config config) {
    String zkServerAddress = ZKContext.zooKeeperServerIP(config);
    String zkServerPort = ZKContext.zooKeeperServerPort(config);
    String zkServer = zkServerAddress + ":" + zkServerPort;
    try {
        CuratorFramework client = CuratorFrameworkFactory.newClient(zkServer, new ExponentialBackoffRetry(1000, 3));
        client.start();
        LOG.log(Level.INFO, "Connected to ZooKeeper server: " + zkServer);
        return client;
    } catch (Exception e) {
        LOG.log(Level.SEVERE, "Could not connect to ZooKeeper server" + zkServer, e);
        throw new RuntimeException(e);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry)

Example 98 with CuratorFramework

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework in project fabric8 by jboss-fuse.

the class FabricDiscoveryAgent method call.

@Override
public Object call() throws Exception {
    LOG.info("Using local ZKClient");
    managedZkClient = true;
    CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder().connectString(System.getProperty("zookeeper.url", "localhost:2181")).retryPolicy(new RetryOneTime(1000)).connectionTimeoutMs(10000);
    String password = System.getProperty("zookeeper.password", "admin");
    if (password != null && !password.isEmpty()) {
        builder.aclProvider(new CuratorACLManager());
        builder.authorization("digest", ("fabric:" + password).getBytes());
    }
    CuratorFramework client = builder.build();
    client.start();
    client.getZookeeperClient().blockUntilConnectedOrTimedOut();
    return client;
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryOneTime(org.apache.curator.retry.RetryOneTime) CuratorFrameworkFactory(org.apache.curator.framework.CuratorFrameworkFactory) CuratorACLManager(io.fabric8.zookeeper.curator.CuratorACLManager)

Example 99 with CuratorFramework

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework in project fabric8 by jboss-fuse.

the class FabricDiscoveryServlet method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    CuratorFramework curator = this.curator;
    if (curator == null) {
        resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Not attached to Fabric");
        return;
    }
    try {
        String groupName = req.getPathInfo();
        if (groupName == null) {
            groupName = "";
        }
        if (groupName.startsWith("/")) {
            int start = 0;
            while (groupName.charAt(start) == '/') {
                start++;
            }
            int end = groupName.length();
            while (groupName.charAt(end - 1) == '/') {
                end--;
            }
            groupName = groupName.substring(start, end);
        }
        LOG.debug("discovery request for group name={}", groupName);
        // To avoid hammering ZooKeeper if we get to many HTTP requests back to back,
        // lets cache results.
        CacheEntry cacheEntry = cache.get(groupName);
        long now = System.currentTimeMillis();
        if (cacheEntry == null || cacheEntry.timestamp + cacheTimeout < now) {
            try {
                Map<String, ActiveMQNode> members = ZooKeeperGroup.members(MAPPER, curator, "/fabric/registry/clusters/amq/" + groupName, ActiveMQNode.class);
                HashSet<String> masters = new HashSet<String>();
                StringBuilder buff = new StringBuilder();
                for (ActiveMQNode node : members.values()) {
                    if (!masters.contains(node.getId())) {
                        for (int i = 0; i < node.services.length; i++) {
                            String url = node.services[i];
                            url = ZooKeeperUtils.getSubstitutedData(curator, url);
                            buff.append(url);
                            buff.append('\n');
                        }
                        masters.add(node.getId());
                    }
                }
                cacheEntry = new CacheEntry(buff.toString(), now);
            } catch (Exception e) {
                cacheEntry = new CacheEntry(null, now);
            }
            cache.put(groupName, cacheEntry);
        }
        if (cacheEntry.result != null) {
            resp.getWriter().print(cacheEntry.result.toString());
        } else {
            resp.sendError(HttpServletResponse.SC_NOT_FOUND, "Group not found");
        }
    } catch (Exception e) {
        resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Error occurred: " + e);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) HashSet(java.util.HashSet)

Example 100 with CuratorFramework

use of org.apache.flink.shaded.curator5.org.apache.curator.framework.CuratorFramework in project coprhd-controller by CoprHD.

the class CoordinatorClientImpl method start.

@Override
public void start() throws IOException {
    if (_zkConnection.curator().isStarted()) {
        return;
    }
    _zkConnection.curator().getConnectionStateListenable().addListener(new org.apache.curator.framework.state.ConnectionStateListener() {

        @Override
        public void stateChanged(CuratorFramework client, final ConnectionState newState) {
            log.info("Entering stateChanged method : {}", newState);
            _connectionStateWorker.submit(new Callable<Object>() {

                @Override
                public Object call() throws Exception {
                    Iterator<ConnectionStateListener> it = _listener.iterator();
                    while (it.hasNext()) {
                        ConnectionStateListener listener = it.next();
                        try {
                            switch(newState) {
                                case RECONNECTED:
                                case CONNECTED:
                                    {
                                        listener.connectionStateChanged(ConnectionStateListener.State.CONNECTED);
                                        break;
                                    }
                                case LOST:
                                case SUSPENDED:
                                    {
                                        listener.connectionStateChanged(ConnectionStateListener.State.DISCONNECTED);
                                        break;
                                    }
                            }
                        } catch (Exception e) {
                            log.warn("Connection listener threw", e);
                        }
                    }
                    return null;
                }
            });
        }
    });
    _zkConnection.connect();
    // writing local node to zk
    initInetAddressEntry();
    try {
        checkAndCreateSiteSpecificSection();
        String servicePath = getServicePath();
        EnsurePath path = new EnsurePath(servicePath);
        path.ensure(_zkConnection.curator().getZookeeperClient());
    } catch (Exception e) {
        // desired
        if ((_connectionStateWorker != null) && (!_connectionStateWorker.isShutdown())) {
            _connectionStateWorker.shutdownNow();
        }
        if (nodeChangeWorker != null && !nodeChangeWorker.isShutdown()) {
            nodeChangeWorker.shutdownNow();
        }
        throw CoordinatorException.fatals.errorConnectingCoordinatorService(e);
    }
}
Also used : PropertyInfoMapper.decodeFromString(com.emc.storageos.coordinator.mapper.PropertyInfoMapper.decodeFromString) Callable(java.util.concurrent.Callable) CoordinatorException(com.emc.storageos.coordinator.exceptions.CoordinatorException) RetryableCoordinatorException(com.emc.storageos.coordinator.exceptions.RetryableCoordinatorException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) CuratorFramework(org.apache.curator.framework.CuratorFramework) EnsurePath(org.apache.curator.utils.EnsurePath) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(com.emc.storageos.coordinator.client.service.ConnectionStateListener)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)924 Test (org.testng.annotations.Test)290 RetryOneTime (org.apache.curator.retry.RetryOneTime)271 Test (org.junit.Test)199 Timing (org.apache.curator.test.Timing)147 CountDownLatch (java.util.concurrent.CountDownLatch)124 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)114 KeeperException (org.apache.zookeeper.KeeperException)93 IOException (java.io.IOException)79 ConnectionState (org.apache.curator.framework.state.ConnectionState)71 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)58 ExecutorService (java.util.concurrent.ExecutorService)55 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)53 ArrayList (java.util.ArrayList)51 RetryNTimes (org.apache.curator.retry.RetryNTimes)51 RetryPolicy (org.apache.curator.RetryPolicy)41 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)38 Cleanup (lombok.Cleanup)37 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)37 Stat (org.apache.zookeeper.data.Stat)36