Search in sources :

Example 51 with CuratorFramework

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

the class ZooKeeperHiveClientHelper method getDirectParamsList.

static List<JdbcConnectionParams> getDirectParamsList(JdbcConnectionParams connParams) throws ZooKeeperHiveClientException {
    CuratorFramework zooKeeperClient = null;
    try {
        zooKeeperClient = getZkClient(connParams);
        List<String> serverHosts = getServerHosts(connParams, zooKeeperClient);
        final List<JdbcConnectionParams> directParamsList = new ArrayList<>();
        // For each node
        for (String serverNode : serverHosts) {
            JdbcConnectionParams directConnParams = new JdbcConnectionParams(connParams);
            directParamsList.add(directConnParams);
            updateParamsWithZKServerNode(directConnParams, zooKeeperClient, serverNode);
        }
        return directParamsList;
    } catch (Exception e) {
        throw new ZooKeeperHiveClientException("Unable to read HiveServer2 configs from ZooKeeper", e);
    } finally {
        // Close the client connection with ZooKeeper
        if (zooKeeperClient != null) {
            zooKeeperClient.close();
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) JdbcConnectionParams(org.apache.hive.jdbc.Utils.JdbcConnectionParams) ArrayList(java.util.ArrayList)

Example 52 with CuratorFramework

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

the class ZooKeeperHiveClientHelper method configureConnParams.

static void configureConnParams(JdbcConnectionParams connParams) throws ZooKeeperHiveClientException {
    if (isZkHADynamicDiscoveryMode(connParams.getSessionVars())) {
        configureConnParamsHA(connParams);
    } else {
        CuratorFramework zooKeeperClient = null;
        try {
            zooKeeperClient = getZkClient(connParams);
            List<String> serverHosts = getServerHosts(connParams, zooKeeperClient);
            // Now pick a server node randomly
            String serverNode = serverHosts.get(new Random().nextInt(serverHosts.size()));
            updateParamsWithZKServerNode(connParams, zooKeeperClient, serverNode);
        } catch (Exception e) {
            throw new ZooKeeperHiveClientException("Unable to read HiveServer2 configs from ZooKeeper", e);
        } finally {
            // Close the client connection with ZooKeeper
            if (zooKeeperClient != null) {
                zooKeeperClient.close();
            }
        }
    }
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Random(java.util.Random)

Example 53 with CuratorFramework

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

the class FileCenter method main.

public static void main(String[] args) {
    id = new Random().nextInt(10);
    System.out.println("id = " + id);
    System.out.println("hahahahha");
    RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
    CuratorFramework client = CuratorFrameworkFactory.builder().namespace(ROOT).connectString(zk_address).retryPolicy(retryPolicy).build();
    client.start();
    try {
        Stat stat = client.checkExists().forPath(DUPS);
        System.out.println("stat =" + stat);
        if (stat == null) {
            System.out.println("create--" + client.create().forPath(DUPS));
        }
        ExecutorService pool = Executors.newFixedThreadPool(5);
        PathChildrenCache pathCache = new PathChildrenCache(client, DUPS, true, false, pool);
        pathCache.getListenable().addListener(new PathNodeListener());
        pathCache.start();
    // TreeCache cache = new TreeCache(client, DUPS);
    // cache.getListenable().addListener(new TreeNodeListener(), pool);
    // cache.start();
    } catch (Exception e) {
        e.printStackTrace();
    }
    synchronized (client) {
        try {
            client.wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    client.close();
}
Also used : CuratorFramework(org.apache.curator.framework.CuratorFramework) Stat(org.apache.zookeeper.data.Stat) Random(java.util.Random) ExponentialBackoffRetry(org.apache.curator.retry.ExponentialBackoffRetry) PathChildrenCache(org.apache.curator.framework.recipes.cache.PathChildrenCache) ExecutorService(java.util.concurrent.ExecutorService) RetryPolicy(org.apache.curator.RetryPolicy)

Example 54 with CuratorFramework

use of 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 55 with CuratorFramework

use of 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)

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