Search in sources :

Example 56 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project nifi by apache.

the class CuratorNodeProtocolSender method getServiceAddress.

@Override
protected synchronized InetSocketAddress getServiceAddress() throws IOException {
    if (coordinatorAddress != null) {
        return coordinatorAddress;
    }
    final RetryPolicy retryPolicy = new RetryNTimes(0, 0);
    final CuratorFramework curatorClient = CuratorFrameworkFactory.newClient(zkConfig.getConnectString(), zkConfig.getSessionTimeoutMillis(), zkConfig.getConnectionTimeoutMillis(), retryPolicy);
    curatorClient.start();
    try {
        // Get coordinator address and add watcher to change who we are heartbeating to if the value changes.
        final byte[] coordinatorAddressBytes = curatorClient.getData().usingWatcher(new Watcher() {

            @Override
            public void process(final WatchedEvent event) {
                coordinatorAddress = null;
            }
        }).forPath(coordinatorPath);
        if (coordinatorAddressBytes == null || coordinatorAddressBytes.length == 0) {
            throw new NoClusterCoordinatorException("No node has yet been elected Cluster Coordinator. Cannot establish connection to cluster yet.");
        }
        final String address = new String(coordinatorAddressBytes, StandardCharsets.UTF_8);
        final String[] splits = address.split(":");
        if (splits.length != 2) {
            final String message = String.format("Attempted to determine Cluster Coordinator address. Zookeeper indicates " + "that address is %s, but this is not in the expected format of <hostname>:<port>", address);
            logger.error(message);
            throw new ProtocolException(message);
        }
        logger.info("Determined that Cluster Coordinator is located at {}; will use this address for sending heartbeat messages", address);
        final String hostname = splits[0];
        final int port;
        try {
            port = Integer.parseInt(splits[1]);
            if (port < 1 || port > 65535) {
                throw new NumberFormatException("Port must be in the range of 1 - 65535 but got " + port);
            }
        } catch (final NumberFormatException nfe) {
            final String message = String.format("Attempted to determine Cluster Coordinator address. Zookeeper indicates " + "that address is %s, but the port is not a valid port number", address);
            logger.error(message);
            throw new ProtocolException(message);
        }
        final InetSocketAddress socketAddress = InetSocketAddress.createUnresolved(hostname, port);
        coordinatorAddress = socketAddress;
        return socketAddress;
    } catch (final NoNodeException nne) {
        logger.info("No node has yet been elected Cluster Coordinator. Cannot establish connection to cluster yet.");
        throw new NoClusterCoordinatorException("No node has yet been elected Cluster Coordinator. Cannot establish connection to cluster yet.");
    } catch (final NoClusterCoordinatorException ncce) {
        throw ncce;
    } catch (Exception e) {
        throw new IOException("Unable to determine Cluster Coordinator from ZooKeeper", e);
    } finally {
        curatorClient.close();
    }
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) InetSocketAddress(java.net.InetSocketAddress) Watcher(org.apache.zookeeper.Watcher) IOException(java.io.IOException) NoClusterCoordinatorException(org.apache.nifi.cluster.exception.NoClusterCoordinatorException) IOException(java.io.IOException) ProtocolException(org.apache.nifi.cluster.protocol.ProtocolException) NoNodeException(org.apache.zookeeper.KeeperException.NoNodeException) WatchedEvent(org.apache.zookeeper.WatchedEvent) CuratorFramework(org.apache.curator.framework.CuratorFramework) NoClusterCoordinatorException(org.apache.nifi.cluster.exception.NoClusterCoordinatorException) RetryPolicy(org.apache.curator.RetryPolicy)

Example 57 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project nifi by apache.

the class CuratorLeaderElectionManager method createClient.

private CuratorFramework createClient() {
    // Create a new client because we don't want to try indefinitely for this to occur.
    final RetryPolicy retryPolicy = new RetryNTimes(1, 100);
    final CuratorACLProviderFactory aclProviderFactory = new CuratorACLProviderFactory();
    final CuratorFramework client = CuratorFrameworkFactory.builder().connectString(zkConfig.getConnectString()).sessionTimeoutMs(zkConfig.getSessionTimeoutMillis()).connectionTimeoutMs(zkConfig.getConnectionTimeoutMillis()).retryPolicy(retryPolicy).aclProvider(aclProviderFactory.create(zkConfig)).defaultData(new byte[0]).build();
    client.start();
    return client;
}
Also used : RetryNTimes(org.apache.curator.retry.RetryNTimes) CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryPolicy(org.apache.curator.RetryPolicy)

Example 58 with CuratorFramework

use of org.apache.curator.framework.CuratorFramework in project nifi by apache.

the class CuratorLeaderElectionManager method determineLeaderExternal.

/**
 * Use a new Curator client to determine which node is the elected leader for the given role.
 *
 * @param roleName the name of the role
 * @return the id of the elected leader, or <code>null</code> if no leader has been selected or if unable to determine
 *         the leader from ZooKeeper
 */
private String determineLeaderExternal(final String roleName) {
    final CuratorFramework client = createClient();
    try {
        final LeaderSelectorListener electionListener = new LeaderSelectorListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
            }

            @Override
            public void takeLeadership(CuratorFramework client) throws Exception {
            }
        };
        final String electionPath = getElectionPath(roleName);
        // Note that we intentionally do not auto-requeue here, and we do not start the selector. We do not
        // want to join the leader election. We simply want to observe.
        final LeaderSelector selector = new LeaderSelector(client, electionPath, electionListener);
        try {
            final Participant leader = selector.getLeader();
            return leader == null ? null : leader.getId();
        } catch (final KeeperException.NoNodeException nne) {
            // If there is no ZNode, then there is no elected leader.
            return null;
        } catch (final Exception e) {
            logger.warn("Unable to determine the Elected Leader for role '{}' due to {}; assuming no leader has been elected", roleName, e.toString());
            if (logger.isDebugEnabled()) {
                logger.warn("", e);
            }
            return null;
        }
    } finally {
        client.close();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Participant(org.apache.curator.framework.recipes.leader.Participant) LeaderSelectorListener(org.apache.curator.framework.recipes.leader.LeaderSelectorListener) LeaderSelector(org.apache.curator.framework.recipes.leader.LeaderSelector) ConnectionState(org.apache.curator.framework.state.ConnectionState) KeeperException(org.apache.zookeeper.KeeperException) KeeperException(org.apache.zookeeper.KeeperException)

Example 59 with CuratorFramework

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

the class ZkConnection method build.

/**
 * Builds zk connector. Note that this method does not initiate a connection. {@link ZkConnection#connect()} must be called to connect
 * to cluster.
 * <p/>
 * This separation is provided so that callbacks can be setup separately prior to connection to cluster.
 */
public void build() {
    try {
        _zkConnection = CuratorFrameworkFactory.builder().connectString(_connectString).connectionTimeoutMs(DEFAULT_CONN_TIMEOUT).canBeReadOnly(true).sessionTimeoutMs(_timeoutMs).retryPolicy(new RetryUntilElapsed(_timeoutMs, RETRY_INTERVAL_MS)).build();
        _zkConnection.getUnhandledErrorListenable().addListener(new UnhandledErrorListener() {

            @Override
            public void unhandledError(String message, Throwable e) {
                _logger.warn("Unknown exception in curator stack", e);
            }
        });
        _zkConnection.getConnectionStateListenable().addListener(new ConnectionStateListener() {

            @Override
            public void stateChanged(CuratorFramework client, ConnectionState newState) {
                _logger.info("Current connection state {}", newState);
            }
        });
        if (FileUtils.exists(siteIdFile)) {
            siteId = new String(FileUtils.readDataFromFile(siteIdFile));
            siteId = siteId.trim();
            _logger.info("Current site id is {}", siteId);
        }
    } catch (Exception e) {
        throw CoordinatorException.fatals.failedToBuildZKConnector(e);
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) RetryUntilElapsed(org.apache.curator.retry.RetryUntilElapsed) UnhandledErrorListener(org.apache.curator.framework.api.UnhandledErrorListener) ConnectionState(org.apache.curator.framework.state.ConnectionState) ConnectionStateListener(org.apache.curator.framework.state.ConnectionStateListener) IOException(java.io.IOException) CoordinatorException(com.emc.storageos.coordinator.exceptions.CoordinatorException)

Example 60 with CuratorFramework

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

the class DistributedDoubleBarrierTest method testMultiClient.

/**
 * Test case port from Apache Curator
 */
@Test
public void testMultiClient() throws Exception {
    final Timing timing = new Timing();
    final CountDownLatch postEnterLatch = new CountDownLatch(QTY);
    final CountDownLatch postLeaveLatch = new CountDownLatch(QTY);
    final AtomicInteger count = new AtomicInteger(0);
    final AtomicInteger max = new AtomicInteger(0);
    List<Future<Void>> futures = Lists.newArrayList();
    ExecutorService service = Executors.newCachedThreadPool();
    for (int i = 0; i < QTY; ++i) {
        Future<Void> future = service.submit(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                ZkConnection zkConnection = createConnection(60 * 1000);
                CuratorFramework client = zkConnection.curator();
                try {
                    zkConnection.connect();
                    DistributedDoubleBarrier barrier = new DistributedDoubleBarrier(client, "/barrier/testMultiClient", QTY);
                    log.info("Entering with timeout {}", timing.seconds());
                    Assert.assertTrue("Return value of enter()", barrier.enter(timing.seconds(), TimeUnit.SECONDS));
                    log.info("Entered");
                    synchronized (DistributedDoubleBarrierTest.this) {
                        int thisCount = count.incrementAndGet();
                        if (thisCount > max.get()) {
                            max.set(thisCount);
                        }
                    }
                    postEnterLatch.countDown();
                    Assert.assertTrue("postEnterLatch", timing.awaitLatch(postEnterLatch));
                    Assert.assertEquals("entered count", count.get(), QTY);
                    log.info("Leaving timeout {}", timing.seconds());
                    Assert.assertTrue("Return value of leave()", barrier.leave(timing.seconds(), TimeUnit.SECONDS));
                    log.info("Left");
                    count.decrementAndGet();
                    postLeaveLatch.countDown();
                    Assert.assertTrue("postLeaveLatch", timing.awaitLatch(postLeaveLatch));
                } finally {
                    CloseableUtils.closeQuietly(client);
                }
                return null;
            }
        });
        futures.add(future);
    }
    for (Future<Void> f : futures) {
        f.get();
    }
    Assert.assertEquals(count.get(), 0);
    Assert.assertEquals(max.get(), QTY);
}
Also used : ZkConnection(com.emc.storageos.coordinator.common.impl.ZkConnection) CuratorFramework(org.apache.curator.framework.CuratorFramework) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Timing(org.apache.curator.test.Timing) Test(org.junit.Test)

Aggregations

CuratorFramework (org.apache.curator.framework.CuratorFramework)824 Test (org.testng.annotations.Test)290 RetryOneTime (org.apache.curator.retry.RetryOneTime)267 Test (org.junit.Test)168 Timing (org.apache.curator.test.Timing)147 CountDownLatch (java.util.concurrent.CountDownLatch)120 ExponentialBackoffRetry (org.apache.curator.retry.ExponentialBackoffRetry)98 KeeperException (org.apache.zookeeper.KeeperException)84 IOException (java.io.IOException)75 ConnectionState (org.apache.curator.framework.state.ConnectionState)70 CuratorEvent (org.apache.curator.framework.api.CuratorEvent)52 ConnectionStateListener (org.apache.curator.framework.state.ConnectionStateListener)50 RetryNTimes (org.apache.curator.retry.RetryNTimes)48 ExecutorService (java.util.concurrent.ExecutorService)47 ArrayList (java.util.ArrayList)44 RetryPolicy (org.apache.curator.RetryPolicy)36 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)35 Stat (org.apache.zookeeper.data.Stat)35 BackgroundCallback (org.apache.curator.framework.api.BackgroundCallback)33 CuratorFrameworkFactory (org.apache.curator.framework.CuratorFrameworkFactory)32