Example usage for org.apache.zookeeper Watcher Watcher

List of usage examples for org.apache.zookeeper Watcher Watcher

Introduction

In this page you can find the example usage for org.apache.zookeeper Watcher Watcher.

Prototype

Watcher

Source Link

Usage

From source file:org.midonet.midolman.state.ZkNatBlockAllocatorTest.java

License:Apache License

private void freeBlock(NatBlock natBlock) throws Exception {
    String path = paths.getNatBlockOwnershipPath(natBlock.deviceId, natBlock.ip, natBlock.blockIndex);
    final CountDownLatch latch = new CountDownLatch(1);
    zk.getZooKeeper().exists(path, new Watcher() {
        @Override/*from w  ww. jav a  2  s . co  m*/
        public void process(WatchedEvent event) {
            latch.countDown();
        }
    });
    allocator.freeBlock(natBlock);
    latch.await();
}

From source file:org.reborndb.reborn.TestRoundRobinJedisPool.java

License:Open Source License

public ZooKeeper connect(String host, int timeout) throws Exception {
    final CountDownLatch connectedLatch = new CountDownLatch(1);

    try {// w ww .  java  2s.c om
        ZooKeeper zk = new ZooKeeper(host, timeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                if (event.getState() == Event.KeeperState.SyncConnected) {
                    connectedLatch.countDown();
                }
            }
        });

        if (ZooKeeper.States.CONNECTING == zk.getState()) {
            connectedLatch.await();
        }

        return zk;
    } catch (Exception e) {
        return null;
    }
}

From source file:org.springframework.xd.yarn.XdYarnAdminInfoCommand.java

License:Apache License

private void init() throws IOException {
    YamlPropertiesFactoryBean ypfb = new YamlPropertiesFactoryBean();
    ypfb.setResources(new FileSystemResource(System.getProperty("spring.config.location")));
    Properties conf = ypfb.getObject();
    if (conf.containsKey(ZK_NAMESPACE)) {
        zkNamespace = (String) conf.get(ZK_NAMESPACE);
    }/*w w  w  . jav a 2s  .  c  o  m*/
    if (conf.containsKey(ZK_CLIENT_CONNECT)) {
        zkClientConnect = (String) conf.get(ZK_CLIENT_CONNECT);
    }
    zk = new ZooKeeper(zkClientConnect, 5000, new Watcher() {
        @Override
        public void process(WatchedEvent event) {
        }
    });
}

From source file:org.wso2.andes.server.cluster.ClusterManager.java

License:Open Source License

/**
 * Initialize the Cluster manager. This will create ZNodes related to nodes and assign node ids
 *
 * @throws CoordinationException in a Error when communicating with Zookeeper
 *//* w w  w.j  ava 2  s. c o m*/
public void init() throws CoordinationException {
    final ClusterConfiguration config = ClusterResourceHolder.getInstance().getClusterConfiguration();

    /**
     * do following if clustering is disabled. Here no Zookeeper is involved
     * so nodeID will be always 0
     */
    if (!config.isClusteringEnabled()) {

        //update node information in durable store
        List<String> nodeList = subscriptionStore.getStoredNodeIDList();
        for (String node : nodeList) {
            subscriptionStore.deleteNodeData(node);
        }
        clearAllPersistedStatesOfDissapearedNode(nodeId);
        subscriptionStore.addNodeDetails("" + nodeId, config.getBindIpAddress());
        //start all global queue workers on the node
        startAllGlobalQueueWorkers();
        return;
    }

    /**
     * do following if cluster is enabled
     */
    try {

        // create a new node with a generated randomId
        // get the node name and id

        zkAgent = new ZooKeeperAgent(connectionString);
        zkAgent.initQueueWorkerCoordination();
        final String nodeName = CoordinationConstants.QUEUE_WORKER_NODE
                + (UUID.randomUUID()).toString().replace("-", "_");
        String path = CoordinationConstants.QUEUE_WORKER_COORDINATION_PARENT + nodeName;
        //Register the watcher for zoo keeper parent to be fired when children changed
        zkAgent.getZooKeeper().getChildren(CoordinationConstants.QUEUE_WORKER_COORDINATION_PARENT,
                new Watcher() {

                    @Override
                    public void process(WatchedEvent watchedEvent) {
                        if (Event.EventType.NodeChildrenChanged == watchedEvent.getType()) {
                            try {
                                List<String> nodeListFromZK = zkAgent.getZooKeeper().getChildren(
                                        CoordinationConstants.QUEUE_WORKER_COORDINATION_PARENT, false);

                                //identify and register this node
                                for (String node : nodeListFromZK) {
                                    if ((CoordinationConstants.NODE_SEPARATOR + node).contains(nodeName)) {

                                        zkNode = node;
                                        nodeId = getNodeIdFromZkNode(node);
                                        log.info("Initializing Cluster Manager , " + "Selected Node id : "
                                                + nodeId);

                                        //add node information to durable store
                                        subscriptionStore.addNodeDetails("" + nodeId,
                                                config.getBindIpAddress());

                                        //register a listener for changes on my node data only
                                        zkAgent.getZooKeeper().getData(
                                                CoordinationConstants.QUEUE_WORKER_COORDINATION_PARENT
                                                        + CoordinationConstants.NODE_SEPARATOR + node,
                                                new NodeDataChangeListener(), null);
                                        break;
                                    }

                                }

                                for (String node : nodeListFromZK) {

                                    //update in-memory node list
                                    int id = getNodeIdFromZkNode(node);
                                    clusterNodeIDList.add(id);
                                }

                                List<String> storedNodes = subscriptionStore.getStoredNodeIDList();

                                /**
                                 * If nodeList size is one, this is the first node joining to cluster. Here we check if there has been
                                 * any nodes that lived before and somehow suddenly got killed. If there are such nodes clear the state of them and
                                 * copy back node queue messages of them back to global queue.
                                 * If node was the same machine:ip and zookeeper assigned a different id this logic will handle the confusion
                                 * We need to clear up current node's state as well as there might have been a node with same id and it was killed
                                 */
                                clearAllPersistedStatesOfDissapearedNode(nodeId);

                                for (String storedNode : storedNodes) {
                                    int storedNodeId = Integer.parseInt(storedNode);
                                    if (!clusterNodeIDList.contains(storedNodeId)) {
                                        clearAllPersistedStatesOfDissapearedNode(storedNodeId);
                                        checkAndCopyMessagesOfNodeQueueBackToGlobalQueue(nodeId,
                                                AndesUtils.getNodeQueueNameForNodeId(storedNodeId));
                                    }

                                }
                            } catch (Exception e) {
                                log.error(
                                        "Error while coordinating cluster information while joining to cluster",
                                        e);
                                throw new RuntimeException(e);
                            }
                        }
                    }
                });
        // Once this method called above defined watcher will be fired
        zkAgent.getZooKeeper().create(path, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.EPHEMERAL_SEQUENTIAL);

        //wait until above task is completed
        Thread.sleep(4000);
        //update global queue synchronizing ID
        reAssignGlobalQueueSyncId();
        //handle global queue addition for this node
        handleGlobalQueueAddition();

        //notify node addition to all nodes and register node existence listeners for all nodes on this node
        List<String> nodeList = zkAgent.getZooKeeper()
                .getChildren(CoordinationConstants.QUEUE_WORKER_COORDINATION_PARENT, false);

        for (String node : nodeList) {
            String currentNodePath = CoordinationConstants.QUEUE_WORKER_COORDINATION_PARENT
                    + CoordinationConstants.NODE_SEPARATOR + node;

            String data = CoordinationConstants.NODE_CHANGED_PREFIX + zkNode;
            //notify all other nodes that a new node joined with node ID
            zkAgent.getZooKeeper().setData(currentNodePath, data.getBytes(), -1);
            //Add Listener for node existence of any node in the cluster
            zkAgent.getZooKeeper().exists(currentNodePath, new NodeExistenceListener(node));

        }

    } catch (Exception e) {
        e.printStackTrace();
        String msg = "Error while initializing the zookeeper coordination ";
        log.error("Error while initializing the zookeeper coordination ", e);
        throw new CoordinationException(msg, e);
    }
}

From source file:perLucene.ZooTiger.java

License:Open Source License

ZooTiger() {
    String[] line = Config.readLocalConfig();

    super.initZookeeper(line[0], Integer.parseInt(line[1]), new Watcher() {
        @Override//  w  w w.ja  v  a2s  .  c o  m
        public void process(WatchedEvent e) {
            if (!(e.getState().equals(Watcher.Event.KeeperState.SyncConnected))) {
                System.out.println("Disconnected.. exiting");
                System.exit(0);
            }
        }
    });

    interval = Integer.parseInt(line[2]);
    replica = Integer.parseInt(line[3]);

    broker = new Broker();

}

From source file:perLucene.ZooTigerRegister.java

License:Open Source License

ZooTigerRegister() {

    String[] line = Config.readLocalConfig();

    super.initZookeeper(line[0], Integer.parseInt(line[1]), new Watcher() {
        @Override//from   ww w . ja  v a2s  .c  o m
        public void process(WatchedEvent e) {
        }
    });

}

From source file:sleet.generators.reservedinstance.ZooKeeperReservedInstanceIdGenerator.java

License:Apache License

@Override
public void beginIdSession(Properties config) throws SleetException {
    if (this.zk != null) {
        throw new GeneratorSessionException(
                "Session was already started.  Stop session by calling endIdSession() then start session by calling beginIdSession()");
    }//from  w  w  w  .  j a  v  a2  s . c o  m

    String zkStr = config.getProperty(ZK_SERVER_KEY);
    if (zkStr == null) {
        throw new GeneratorConfigException(
                "Missing ZooKeeper server string, must be specified in configuration properties key \""
                        + ZK_SERVER_KEY + "\".");
    }

    String zkPathStr = config.getProperty(ZK_PATH_KEY);
    if (zkPathStr == null) {
        throw new GeneratorConfigException(
                "Missing ZooKeeper path string, must be specified in configuration properties key \""
                        + ZK_PATH_KEY + "\".");
    }

    this.path = zkPathStr;

    String zkTimeoutStr = config.getProperty(ZK_TIMEOUT_KEY);
    if (zkTimeoutStr == null) {
        throw new GeneratorConfigException(
                "Missing ZooKeeper timeout, must be specified in configuration properties key \""
                        + ZK_TIMEOUT_KEY + "\".");
    }
    int zkTimeout = -1;
    try {
        zkTimeout = Integer.valueOf(zkTimeoutStr);
    } catch (NumberFormatException e) {
        throw new GeneratorConfigException("Failed to parse ZooKeeper timeout from value \"" + zkTimeoutStr
                + "\".  The value must be an integer.");
    }

    try {
        this.zk = new ZooKeeperClient(zkStr, zkTimeout, new Watcher() {
            @Override
            public void process(WatchedEvent event) {

            }
        });
    } catch (IOException e) {
        throw new SleetException(e);
    }

    String bitsStr = config.getProperty(BITS_IN_INSTANCE_ID_KEY);
    if (bitsStr == null) {
        throw new GeneratorConfigException(
                "Missing number of bits, must be specified in configuration properties key \""
                        + BITS_IN_INSTANCE_ID_KEY + "\".");
    }
    int bits = -1;
    try {
        bits = Integer.valueOf(bitsStr);
    } catch (NumberFormatException e) {
        throw new GeneratorConfigException("Failed to parse number of bits from value \"" + bitsStr
                + "\".  The value must be an integer.");
    }
    this.bitsInInstanceValue = bits;
    this.maxInstanceValue = (1 << this.bitsInInstanceValue) - 1;

    String msTimeoutStr = config.getProperty(MILLISECONDS_TO_WAIT_FOR_ID);
    if (msTimeoutStr == null) {
        throw new GeneratorConfigException(
                "Missing number of milliseconds to wait on startup, must be specified in configuration properties key \""
                        + MILLISECONDS_TO_WAIT_FOR_ID + "\".");
    }
    int msTimeout = -1;
    try {
        msTimeout = Integer.valueOf(msTimeoutStr);
    } catch (NumberFormatException e) {
        throw new GeneratorConfigException(
                "Failed to parse number of milliseconds to wait on startup from value \"" + msTimeoutStr
                        + "\".  The value must be an integer.");
    }

    try {
        this.instMgr = new InstanceIdManagerRaceZooKeeper(this.zk, this.path, this.maxInstanceValue + 1);
        long underlyingid = this.instMgr.tryToGetId(msTimeout);
        if (underlyingid == -1) {
            throw new ReservedInstanceTimeoutException(
                    "Unable to allocate an id instance within the timeout allotted (" + msTimeout + ")");
        } else {
            this.id = new LongId(underlyingid, null, this.bitsInInstanceValue);
        }
    } catch (IOException e) {
        throw new SleetException(e);
    }
}

From source file:sleet.utils.zookeeper.InstanceIdManagerZooKeeper.java

License:Apache License

@Override
public int tryToGetId(long millisToWait) throws IOException {
    final long initialTime = System.currentTimeMillis();
    final Object watchLock = new Object();
    final Watcher watcher = new Watcher() {
        @Override/*from  w  w  w .jav a 2 s  .  c o  m*/
        public void process(WatchedEvent event) {
            synchronized (watchLock) {
                watchLock.notify();
            }
        }
    };
    String newPath = null;
    try {
        newPath = _zooKeeper.create(_idPath + "/id_", null, Ids.OPEN_ACL_UNSAFE,
                CreateMode.EPHEMERAL_SEQUENTIAL);
    } catch (KeeperException e) {
        throw new IOException(e);
    } catch (InterruptedException e) {
        throw new IOException(e);
    }
    String lock = null;
    try {
        int count = -1;
        while (count == -1 || count >= _maxInstances) {
            lock = lock();
            List<String> children = _zooKeeper.getChildren(_idPath, watcher);
            int[] childNumbers = new int[children.size()];
            int childCounter = 0;
            for (String child : children) {
                childNumbers[childCounter++] = Integer.parseInt(child.substring(child.lastIndexOf('_') + 1));
            }
            Arrays.sort(childNumbers);
            int myNumber = Integer.parseInt(newPath.substring(newPath.lastIndexOf('_') + 1));
            String newNode = newPath.substring(newPath.lastIndexOf('/') + 1);
            count = -1;
            for (int childNumber : childNumbers) {
                count++;
                if (myNumber == childNumber) {
                    break;
                }
            }
            // new node is in the top children, assign a new id.
            if (count < _maxInstances) {
                return assignNode(newNode);
            } else {
                if (millisToWait != -1 && (System.currentTimeMillis() - initialTime >= millisToWait)) {
                    _zooKeeper.delete(newPath, -1);
                    LOG.info("Waited more than " + millisToWait + "ms.  Returning -1.");
                    return -1;
                }
                unlock(lock);
                lock = null;
                synchronized (watchLock) {
                    watchLock.wait(_waitForIdQueueTimeout);
                }
            }
        }
    } catch (KeeperException e) {
        throw new IOException(e);
    } catch (InterruptedException e) {
        throw new IOException(e);
    } finally {
        if (lock != null) {
            unlock(lock);
        }
    }
    LOG.info("Escaped loop to allocate an instance id.  Returning -1.");
    return -1;
}

From source file:sleet.utils.zookeeper.InstanceIdManagerZooKeeper.java

License:Apache License

private String lock() throws IOException {
    try {//w  w  w  . j a va2s. c  om
        String path = _zooKeeper.create(_lockPath + "/lock_", null, Ids.OPEN_ACL_UNSAFE,
                CreateMode.EPHEMERAL_SEQUENTIAL);
        String lockNode = path.substring(path.lastIndexOf('/') + 1);
        int lockNumber = Integer.parseInt(lockNode.substring(lockNode.lastIndexOf('_') + 1));
        final Object lock = new Object();
        Watcher watcher = new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                synchronized (lock) {
                    lock.notifyAll();
                }
            }
        };
        while (true) {
            List<String> children = new ArrayList<String>(_zooKeeper.getChildren(_lockPath, watcher));
            if (children.size() < 1) {
                throw new IOException("Children of path [" + _lockPath + "] should never be 0.");
            }
            int[] childNumbers = new int[children.size()];
            int childCounter = 0;
            for (String child : children) {
                childNumbers[childCounter++] = Integer.parseInt(child.substring(child.lastIndexOf('_') + 1));
            }
            Arrays.sort(childNumbers);
            int lockOwnerNumber = childNumbers[0];
            if (lockNumber == lockOwnerNumber) {
                return lockNode;
            }
            synchronized (lock) {
                lock.wait(TimeUnit.SECONDS.toMillis(1));
            }
        }
    } catch (KeeperException e) {
        throw new IOException(e);
    } catch (InterruptedException e) {
        throw new IOException(e);
    }
}

From source file:sleet.utils.zookeeper.TestInstanceIdManagerZooKeeper.java

License:Apache License

public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
    ZkMiniCluster cluster = new ZkMiniCluster();
    File temp = File.createTempFile("instanceidmanager", "");
    temp.delete();/*  ww  w.ja v  a  2s  . c om*/
    temp.mkdir();
    System.out.println("zk temp path: " + temp.getAbsolutePath());
    cluster.startZooKeeper(temp.getAbsolutePath());
    String zkconn = cluster.getZkConnectionString();
    zkconn = "localhost:" + zkconn.substring(zkconn.lastIndexOf(':') + 1);
    System.out.println("ZK connection string: " + zkconn);
    ZooKeeper zooKeeper = new ZooKeeper(zkconn, 10000, new Watcher() {
        @Override
        public void process(WatchedEvent event) {

        }
    });
    String path = "/id-manager";
    try {
        rmr(zooKeeper, path);
    } catch (Exception e) {
        // ignore
    }
    final InstanceIdManagerZooKeeper idManagerZooKeeper = new InstanceIdManagerZooKeeper(zooKeeper, path, 10);
    {
        int id1 = idManagerZooKeeper.tryToGetId(1000L);
        System.out.println(id1);
        idManagerZooKeeper.releaseId(id1);
    }
    {
        int id1 = idManagerZooKeeper.tryToGetId(1000L);
        System.out.println(id1);
        idManagerZooKeeper.releaseId(id1);
    }
    {
        int[] ids = new int[12];
        for (int i = 0; i < ids.length; i++) {
            ids[i] = idManagerZooKeeper.tryToGetId(1000L);
        }

        for (int i = 0; i < ids.length; i++) {
            System.out.println(ids[i]);
        }

        for (int i = 0; i < ids.length; i++) {
            idManagerZooKeeper.releaseId(ids[i]);
        }
    }

    {
        ExecutorService service = Executors.newCachedThreadPool();
        Random random = new Random();
        final AtomicInteger count = new AtomicInteger();
        int maxWorkers = 100;
        List<Future<Void>> futures = new ArrayList<Future<Void>>();
        for (int i = 0; i < maxWorkers; i++) {
            final int secondToWork = random.nextInt(2) + 1;
            final int index = i;
            futures.add(service.submit(new Callable<Void>() {
                @Override
                public Void call() throws Exception {
                    int id;
                    int attempts = 0;
                    do {
                        if (attempts > 0) {
                            Thread.sleep(100);
                        }
                        id = idManagerZooKeeper.tryToGetId(1000L);
                        attempts++;
                    } while (id < 0);
                    try {
                        System.out.println("[" + index + "] Working for [" + secondToWork
                                + "] seconds with id [" + id + "]");
                        Thread.sleep(TimeUnit.SECONDS.toMillis(secondToWork));
                    } finally {
                        System.out.println("[" + index + "] Releasing with id [" + id + "]");
                        idManagerZooKeeper.releaseId(id);
                        count.incrementAndGet();
                    }
                    return null;
                }
            }));
        }
        for (Future<Void> future : futures) {
            try {
                future.get();
            } catch (ExecutionException e) {
                Throwable cause = e.getCause();
                cause.printStackTrace();
            }
        }
        while (count.get() < maxWorkers) {
            System.out.println(count.get() + " " + maxWorkers);
            Thread.sleep(TimeUnit.SECONDS.toMillis(1));
        }
        service.shutdown();
        service.awaitTermination(1, TimeUnit.DAYS);
    }

    zooKeeper.close();
    cluster.shutdownZooKeeper();
}