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.apache.distributedlog.zk.TestZKWatcherManager.java

License:Apache License

@Test(timeout = 60000)
public void testRegisterUnregisterWatcher() throws Exception {
    ZKWatcherManager watcherManager = ZKWatcherManager.newBuilder().name("test-register-unregister-watcher")
            .zkc(null).statsLogger(NullStatsLogger.INSTANCE).build();
    String path = "/test-register-unregister-watcher";
    final List<WatchedEvent> events = new LinkedList<WatchedEvent>();
    final CountDownLatch latch = new CountDownLatch(2);
    Watcher watcher = new Watcher() {
        @Override//from   w w  w.  j a  va2  s. com
        public void process(WatchedEvent event) {
            events.add(event);
            latch.countDown();
        }
    };
    watcherManager.registerChildWatcher(path, watcher);

    // fire the event
    WatchedEvent event0 = new WatchedEvent(Watcher.Event.EventType.NodeCreated,
            Watcher.Event.KeeperState.SyncConnected, path);
    WatchedEvent event1 = new WatchedEvent(Watcher.Event.EventType.None,
            Watcher.Event.KeeperState.SyncConnected, path);
    WatchedEvent event2 = new WatchedEvent(Watcher.Event.EventType.NodeChildrenChanged,
            Watcher.Event.KeeperState.SyncConnected, path);
    watcher.process(event1);
    watcher.process(event2);

    latch.await();

    assertEquals(2, events.size());
    assertEquals(event1, events.get(0));
    assertEquals(event2, events.get(1));

    // unregister watcher
    watcherManager.unregisterChildWatcher(path, watcher, true);
    // unregister gauges
    watcherManager.unregisterGauges();
    assertEquals(0, watcherManager.childWatches.size());
}

From source file:org.apache.flume.agent.SmartAvroSource.java

License:Apache License

@Override
public void start() {
    logger.info("Starting {}...", this);

    Responder responder = new SpecificResponder(AvroSourceProtocol.class, this);

    NioServerSocketChannelFactory socketChannelFactory = initSocketChannelFactory();

    ChannelPipelineFactory pipelineFactory = initChannelPipelineFactory();

    server = new NettyServer(responder, new InetSocketAddress(bindAddress, port), socketChannelFactory,
            pipelineFactory, null);//w w  w  . j  a  v  a2s .  c o m

    connectionCountUpdater = Executors.newSingleThreadScheduledExecutor();
    server.start();
    sourceCounter.start();
    super.start();
    final NettyServer srv = (NettyServer) server;
    connectionCountUpdater.scheduleWithFixedDelay(new Runnable() {

        @Override
        public void run() {
            sourceCounter.setOpenConnectionCount(Long.valueOf(srv.getNumActiveConnections()));
        }
    }, 0, 60, TimeUnit.SECONDS);

    logger.info("Avro source {} started.", getName());

    if (isMonitor) {
        try {
            String configInfo = FileUtils.readFileByLines(configFilePath, -1);
            createConfigNodes(configInfo, congZkUtils);
            createConfigNodes(configInfo, zhuZkUtils, hostname); // zk?
        } catch (KeeperException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } catch (InterruptedException e1) {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } // zk?
        try {
            createLogNodes(heartInfo, congZkUtils);// zk
            createLogNodes(heartInfo, zhuZkUtils, hostname);// zk
        } catch (KeeperException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        heart_sender_future_ = executor_service_.scheduleWithFixedDelay(heart_sender_runnable_, 0L, 4,
                TimeUnit.SECONDS);
        try {
            zhuZk = new ZooKeeper(senderIpAndPort, 3000, new Watcher() {
                public void process(WatchedEvent event) {
                    logger.info("--> zk???");
                }
            });
            congZk = new ZooKeeper(ipAndPort, 3000, new Watcher() {
                public void process(WatchedEvent event) {
                    logger.info("--> zk???");
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        } // 
          //6.zk?,?,?zk?,
        monitorLocalNodeData(FlumeConstants.AGENT_LOG_ROOT + "/" + systemProperties.getIp_(), 0);
        //7.zk?????,?zk?
        monitorLocalChildNodeDataChange(FlumeConstants.AGENT_LOG_ROOT + "/" + systemProperties.getIp_());
        //8.zk??,?,?zk?
        monitorLocalNodeData(FlumeConstants.AGENT_CONFIG_ROOT + "/" + systemProperties.getIp_(), 0);

        //4.zk???,???,?.
        //5.zk???,???,?zk?.
        monitorSenderNodeData(
                FlumeConstants.AGENT_CONFIG_ROOT + "/" + hostname + "/" + systemProperties.getIp_());
    }
}

From source file:org.apache.hadoop.contrib.bkjournal.BKJMUtil.java

License:Apache License

static ZooKeeper connectZooKeeper() throws IOException, KeeperException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);

    ZooKeeper zkc = new ZooKeeper(zkEnsemble, 3600, new Watcher() {
        public void process(WatchedEvent event) {
            if (event.getState() == Watcher.Event.KeeperState.SyncConnected) {
                latch.countDown();/*from www. j a va 2s  .com*/
            }
        }
    });
    if (!latch.await(3, TimeUnit.SECONDS)) {
        throw new IOException("Zookeeper took too long to connect");
    }
    return zkc;
}

From source file:org.apache.hadoop.contrib.bkjournal.TestBookKeeperConfiguration.java

License:Apache License

private static ZooKeeper connectZooKeeper(String ensemble)
        throws IOException, KeeperException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);

    ZooKeeper zkc = new ZooKeeper(HOSTPORT, ZK_SESSION_TIMEOUT, new Watcher() {
        public void process(WatchedEvent event) {
            if (event.getState() == Watcher.Event.KeeperState.SyncConnected) {
                latch.countDown();//from w  ww. j a  v a2  s .c  o m
            }
        }
    });
    if (!latch.await(ZK_SESSION_TIMEOUT, TimeUnit.MILLISECONDS)) {
        throw new IOException("Zookeeper took too long to connect");
    }
    return zkc;
}

From source file:org.apache.hadoop.contrib.bkjournal.TestCurrentInprogress.java

License:Apache License

private static ZooKeeper connectZooKeeper(String ensemble)
        throws IOException, KeeperException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);

    ZooKeeper zkc = new ZooKeeper(HOSTPORT, 3600, new Watcher() {
        public void process(WatchedEvent event) {
            if (event.getState() == Watcher.Event.KeeperState.SyncConnected) {
                latch.countDown();//from  ww w.  ja  v  a2 s  .  c om
            }
        }
    });
    if (!latch.await(10, TimeUnit.SECONDS)) {
        throw new IOException("Zookeeper took too long to connect");
    }
    return zkc;
}

From source file:org.apache.hadoop.eclipse.release.ZooKeeperClientRelease.java

License:Apache License

@Override
public void connect() throws IOException, InterruptedException {
    if (client == null) {
        if (logger.isDebugEnabled())
            logger.debug("connect(" + serverLocation + "): Connecting begin");
        client = new ZooKeeper(serverLocation, 5000, new Watcher() {

            @Override//from   w  w w.jav a  2  s.  c o m
            public void process(WatchedEvent event) {
            }
        });
        int waitCount = 0;
        while (client.getState() == States.CONNECTING && waitCount++ < 5) {
            if (logger.isDebugEnabled())
                logger.debug("connect(" + serverLocation + "): Still connecting... sleep for 1s");
            Thread.sleep(1000);
        }
        if (logger.isDebugEnabled())
            logger.debug("connect(" + serverLocation + "): Connecting finish with state: " + client.getState());
    }
}

From source file:org.apache.hadoop.hbase.stargate.auth.ZooKeeperAuthenticator.java

License:Apache License

/**
 * Constructor/*  w  ww . j av  a  2 s. co m*/
 * @param conf
 * @throws IOException
 */
public ZooKeeperAuthenticator(HBaseConfiguration conf) throws IOException {
    this(conf, new ZooKeeperWrapper(conf, new Watcher() {
        public void process(WatchedEvent event) {
        }
    }));
    ensureExists(USERS_ZNODE_ROOT);
}

From source file:org.apache.hama.bsp.sync.ZooKeeperSyncClientImpl.java

License:Apache License

@Override
public void leaveBarrier(final BSPJobID jobId, final TaskAttemptID taskId, final long superstep)
        throws SyncException {
    try {/*from  www .  j  a  v a2s  .  c om*/
        // final String pathToSuperstepZnode = bspRoot + "/"
        // + taskId.getJobID().toString() + "/" + superstep;
        final String pathToSuperstepZnode = constructKey(taskId.getJobID(), "sync", "" + superstep);
        while (true) {
            List<String> znodes = zk.getChildren(pathToSuperstepZnode, false);
            LOG.debug("leaveBarrier() !!! checking znodes contnains /ready node or not: at superstep:"
                    + superstep + " znode:" + znodes);
            if (znodes.contains("ready")) {
                znodes.remove("ready");
            }
            final int size = znodes.size();

            LOG.debug("leaveBarrier() at superstep:" + superstep + " znode size: (" + size + ") znodes:"
                    + znodes);

            if (null == znodes || znodes.isEmpty())
                return;
            if (1 == size) {
                try {
                    zk.delete(getNodeName(taskId, superstep), 0);
                } catch (KeeperException.NoNodeException nne) {
                    LOG.debug("+++ (znode size is 1). Ignore because znode may disconnect.", nne);
                }
                return;
            }
            Collections.sort(znodes);

            final String lowest = znodes.get(0);
            final String highest = znodes.get(size - 1);

            synchronized (mutex) {

                if (getNodeName(taskId, superstep).equals(pathToSuperstepZnode + "/" + lowest)) {
                    Stat s = zk.exists(pathToSuperstepZnode + "/" + highest, new Watcher() {
                        @Override
                        public void process(WatchedEvent event) {
                            synchronized (mutex) {
                                LOG.debug("leaveBarrier() at superstep: " + superstep + " taskid:"
                                        + taskId.toString() + " highest notify lowest.");
                                mutex.notifyAll();
                            }
                        }
                    });

                    if (null != s) {
                        LOG.debug("leaveBarrier(): superstep:" + superstep + " taskid:" + taskId.toString()
                                + " wait for higest notify.");
                        mutex.wait();
                    }
                } else {
                    Stat s1 = zk.exists(getNodeName(taskId, superstep), false);

                    if (null != s1) {
                        try {
                            zk.delete(getNodeName(taskId, superstep), 0);
                        } catch (KeeperException.NoNodeException nne) {
                            LOG.debug("++++ Ignore because node may be dleted.", nne);
                        }
                    }

                    Stat s2 = zk.exists(pathToSuperstepZnode + "/" + lowest, new Watcher() {
                        @Override
                        public void process(WatchedEvent event) {
                            synchronized (mutex) {
                                LOG.debug("leaveBarrier() at superstep: " + superstep + " taskid:"
                                        + taskId.toString() + " lowest notify other nodes.");
                                mutex.notifyAll();
                            }
                        }
                    });
                    if (null != s2) {
                        LOG.debug("leaveBarrier(): superstep:" + superstep + " taskid:" + taskId.toString()
                                + " wait for lowest notify.");
                        mutex.wait();
                    }
                }
            }
        }
    } catch (Exception e) {
        throw new SyncException(e.getMessage());
    }
}

From source file:org.apache.hcatalog.templeton.tool.ZooKeeperStorage.java

License:Apache License

/**
 * Open a ZooKeeper connection for the JobState.
 *//*w  w  w  . j  a  va2 s  .  com*/
public static ZooKeeper zkOpen(String zkHosts, int zkSessionTimeout) throws IOException {
    return new ZooKeeper(zkHosts, zkSessionTimeout, new Watcher() {
        @Override
        synchronized public void process(WatchedEvent event) {
        }
    });
}

From source file:org.apache.hedwig.server.netty.PubSubServer.java

License:Apache License

protected void instantiateZookeeperClient() throws Exception {
    if (!conf.isStandalone()) {
        final CountDownLatch signalZkReady = new CountDownLatch(1);

        zk = new ZooKeeper(conf.getZkHost(), conf.getZkTimeout(), new Watcher() {
            @Override//from  w w w . j  av a  2 s. c  om
            public void process(WatchedEvent event) {
                if (Event.KeeperState.SyncConnected.equals(event.getState())) {
                    signalZkReady.countDown();
                }
            }
        });
        // wait until connection is effective
        if (!signalZkReady.await(conf.getZkTimeout() * 2, TimeUnit.MILLISECONDS)) {
            logger.error("Could not establish connection with ZooKeeper after zk_timeout*2 = "
                    + conf.getZkTimeout() * 2 + " ms. (Default value for zk_timeout is 2000).");
            throw new Exception("Could not establish connection with ZooKeeper after zk_timeout*2 = "
                    + conf.getZkTimeout() * 2 + " ms. (Default value for zk_timeout is 2000).");
        }
    }
}