Search in sources :

Example 46 with CuratorFramework

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

the class TestZKNode method main.

public static void main(String[] args) throws Exception {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFramework client = CuratorFrameworkFactory.newClient(zk_address, 5 * 1000, 30 * 1000, retryPolicy);
    client.start();
    client.blockUntilConnected();
    PathChildrenCache cache = new PathChildrenCache(client.usingNamespace("test"), "/fileCoordinator/big", true);
    cache.getListenable().addListener(new PathChildrenCacheListener() {

        @Override
        public void childEvent(CuratorFramework client, PathChildrenCacheEvent event) throws Exception {
            System.out.println("---" + event);
            ChildData data = event.getData();
            if (data != null) {
                switch(event.getType()) {
                    case CHILD_ADDED:
                        System.out.println("###PATH-" + data.getPath());
                        break;
                    default:
                        break;
                }
            }
        }
    });
    cache.start();
    PersistentNode node = new PersistentNode(client.usingNamespace("test"), CreateMode.EPHEMERAL, false, "/fileCoordinator/temp-1", "node1".getBytes());
    node.getListenable().addListener(new PersistentNodeListener() {

        @Override
        public void nodeCreated(String path) throws Exception {
            System.out.println("node1--created:" + path);
        }
    });
    node.start();
    PersistentNode node2 = new PersistentNode(client.usingNamespace("test"), CreateMode.EPHEMERAL, false, "/fileCoordinator/temp-1", "node2".getBytes());
    node2.getListenable().addListener(new PersistentNodeListener() {

        @Override
        public void nodeCreated(String path) throws Exception {
            System.out.println("node2--created:" + path);
        }
    });
    node2.start();
    Thread.sleep(2000);
    node2.close();
    synchronized (node) {
        node.wait();
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) PathChildrenCacheListener(org.apache.curator.framework.recipes.cache.PathChildrenCacheListener) PathChildrenCacheEvent(org.apache.curator.framework.recipes.cache.PathChildrenCacheEvent) PersistentNodeListener(org.apache.curator.framework.recipes.nodes.PersistentNodeListener) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ChildData(org.apache.curator.framework.recipes.cache.ChildData) PersistentNode(org.apache.curator.framework.recipes.nodes.PersistentNode) RetryPolicy(org.apache.curator.RetryPolicy)

Example 47 with CuratorFramework

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

the class TestStorer method main.

public static void main(String[] args) throws Exception {
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFramework client = CuratorFrameworkFactory.newClient("122.11.47.17:2181", 5 * 1000, 30 * 1000, retryPolicy);
    client.start();
    client.blockUntilConnected();
    FileNodeStorer storer = new ZkFileNodeStorer(client.usingNamespace("test"));
    FileNode node = new FileNode();
    node.setName("file_node_1");
    node.setServiceId("ser_1");
    node.setStorageName("sn_1");
    node.setDuplicates(new int[] { 1, 2, 3 });
    storer.save(node);
    FileNode node2 = storer.getFileNode("file_node_1");
    System.out.println("node2--" + node2.getName());
    node2.setServiceId("ser_2");
    storer.update("file_node_1", node2);
    List<FileNode> nodeList = storer.listFileNodes();
    for (FileNode nd : nodeList) {
        System.out.println("list--" + nd.getName() + ", " + nd.getServiceId());
        storer.delete(nd.getName());
    }
    synchronized (client) {
        client.wait();
    }
}
Also used : FileNodeStorer(com.bonree.brfs.duplication.coordinator.FileNodeStorer) CuratorFramework(org.apache.curator.framework.CuratorFramework) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) RetryPolicy(org.apache.curator.RetryPolicy) FileNode(com.bonree.brfs.duplication.coordinator.FileNode)

Example 48 with CuratorFramework

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

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

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

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