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.blur.zookeeper.WatchChildren.java

License:Apache License

public WatchChildren watch(final OnChange onChange, long fireAnywayTime, TimeUnit timeUnit) {
    if (_debug) {
        StringWriter writer = new StringWriter();
        PrintWriter printWriter = new PrintWriter(writer);
        new Throwable().printStackTrace(printWriter);
        printWriter.close();// www. j av a 2  s.c  o  m
        _debugStackTrace = writer.toString();
    }
    final long timeToFireAnywayTime = TimeUnit.MILLISECONDS.convert(fireAnywayTime, timeUnit);
    _watchThread = new Thread(new Runnable() {
        @Override
        public void run() {
            Watcher watcher = new Watcher() {
                @Override
                public void process(WatchedEvent event) {
                    synchronized (_lock) {
                        _lock.notify();
                    }
                }
            };
            startDoubleCheckThread();
            while (_running.get()) {
                synchronized (_lock) {
                    try {
                        List<String> children = _zooKeeper.getChildren(_path, watcher);
                        try {
                            onChange.action(children);
                            _children = children;
                        } catch (Throwable t) {
                            LOG.error("Unknown error during onchange action [" + this + "].", t);
                        }
                        _lock.wait(timeToFireAnywayTime);
                    } catch (KeeperException e) {
                        if (!_running.get()) {
                            LOG.info("Error [{0}]", e.getMessage());
                            return;
                        }
                        if (e.code() == Code.NONODE) {
                            if (_debug) {
                                LOG.debug("Path for watching not found [{0}], no longer watching, debug [{1}].",
                                        _path, _debugStackTrace);
                            } else {
                                LOG.debug("Path for watching not found [{0}], no longer watching.", _path);
                            }
                            close();
                            return;
                        }
                        if (_debug) {
                            LOG.error("Unknown error [{0}]", e, _debugStackTrace);
                        } else {
                            LOG.error("Unknown error", e);
                        }
                        throw new RuntimeException(e);
                    } catch (InterruptedException e) {
                        return;
                    }
                }
            }
            _running.set(false);
        }
    });
    _watchThread.setName("Watch Children [" + _path + "][" + instance + "]");
    _watchThread.setDaemon(true);
    _watchThread.start();
    return this;
}

From source file:org.apache.blur.zookeeper.WatchNodeData.java

License:Apache License

public WatchNodeData watch(final OnChange onChange) {
    _watchThread = new Thread(new Runnable() {

        @Override// w  w w  .jav  a 2 s  .  co  m
        public void run() {
            Watcher watcher = new Watcher() {
                @Override
                public void process(WatchedEvent event) {
                    synchronized (_lock) {
                        _lock.notify();
                    }
                }
            };
            startDoubleCheckThread();
            while (_running.get()) {
                synchronized (_lock) {
                    try {
                        Stat stat = _zooKeeper.exists(_path, false);
                        if (stat == null) {
                            LOG.debug("Path [{0}] not found.", _path);
                            return;
                        }
                        byte[] data = _zooKeeper.getData(_path, watcher, stat);
                        try {
                            onChange.action(data);
                            _data = data;
                        } catch (Throwable t) {
                            LOG.error("Unknown error during onchange action [" + this + "].", t);
                        }
                        _lock.wait();
                    } catch (KeeperException e) {
                        if (!_running.get()) {
                            LOG.info("Error [{0}]", e.getMessage());
                            return;
                        }
                        LOG.error("Unknown error", e);
                        throw new RuntimeException(e);
                    } catch (InterruptedException e) {
                        return;
                    }
                }
            }
        }
    });
    _watchThread.setName("Watch Data [" + _path + "][" + instance + "]");
    _watchThread.setDaemon(true);
    _watchThread.start();
    return this;
}

From source file:org.apache.blur.zookeeper.WatchNodeExistance.java

License:Apache License

public WatchNodeExistance watch(final OnChange onChange) {
    _watchThread = new Thread(new Runnable() {
        @Override//from w w w  .j  av  a 2 s  .com
        public void run() {
            Watcher watcher = new Watcher() {
                @Override
                public void process(WatchedEvent event) {
                    synchronized (_lock) {
                        _lock.notify();
                    }
                }
            };
            startDoubleCheckThread();
            while (_running.get()) {
                synchronized (_lock) {
                    try {
                        Stat stat = _zooKeeper.exists(_path, watcher);
                        try {
                            onChange.action(stat);
                            _stat = stat;
                        } catch (Throwable t) {
                            LOG.error("Unknown error during onchange action [" + this + "].", t);
                        }
                        _lock.wait();
                    } catch (KeeperException e) {
                        if (!_running.get()) {
                            LOG.info("Error [{0}]", e.getMessage());
                            return;
                        }
                        LOG.error("Unknown error", e);
                        throw new RuntimeException(e);
                    } catch (InterruptedException e) {
                        return;
                    }
                }
            }
        }
    });
    _watchThread.setName("Watch Existance [" + _path + "][" + instance + "]");
    _watchThread.setDaemon(true);
    _watchThread.start();
    return this;
}

From source file:org.apache.blur.zookeeper.ZkMiniCluster.java

License:Apache License

public void startZooKeeper(final Properties properties, boolean format, String path, final boolean randomPort) {
    String realPath = path + "/zk_test";
    properties.setProperty("dataDir", realPath);
    final ServerConfig serverConfig = new ServerConfig();
    QuorumPeerConfig config = new QuorumPeerConfig() {
        @Override//from  w w  w .j a v a2  s  .com
        public InetSocketAddress getClientPortAddress() {
            InetSocketAddress clientPortAddress = super.getClientPortAddress();
            if (randomPort) {
                return randomPort(clientPortAddress);
            }
            return clientPortAddress;
        }

        private InetSocketAddress randomPort(InetSocketAddress clientPortAddress) {
            return new InetSocketAddress(clientPortAddress.getAddress(), 0);
        }
    };
    try {
        config.parseProperties(properties);
    } catch (IOException e) {
        LOG.error(e);
        throw new RuntimeException(e);
    } catch (ConfigException e) {
        LOG.error(e);
        throw new RuntimeException(e);
    }
    serverConfig.readFrom(config);
    rm(new File(realPath));
    serverThread = new Thread(new Runnable() {

        @Override
        public void run() {
            try {
                zooKeeperServerMain = new ZooKeeperServerMainEmbedded();
                zooKeeperServerMain.runFromConfig(serverConfig);
            } catch (IOException e) {
                LOG.error(e);
            }
        }
    });
    serverThread.start();
    long s = System.nanoTime();
    while (s + 10000000000L > System.nanoTime()) {
        try {
            Thread.sleep(50);
        } catch (InterruptedException e) {
            LOG.error(e);
            throw new RuntimeException(e);
        }
        try {
            String zkConnectionString = getZkConnectionString();
            if (zkConnectionString == null) {
                continue;
            }
            ZooKeeper zk = new ZooKeeper(getZkConnectionString(), 30000, new Watcher() {
                @Override
                public void process(WatchedEvent event) {

                }
            });
            zk.close();
            break;
        } catch (IOException e) {
            LOG.error(e);
            throw new RuntimeException(e);
        } catch (InterruptedException e) {
            LOG.error(e);
            throw new RuntimeException(e);
        }
    }
}

From source file:org.apache.blur.zookeeper.ZkUtilsTest.java

License:Apache License

@Before
public void setUp() throws IOException, InterruptedException {
    final Object lock = new Object();
    synchronized (lock) {
        _zooKeeper = new ZooKeeper(_zkMiniCluster.getZkConnectionString(), 10000, new Watcher() {
            @Override/*from   w w  w . j ava 2s . com*/
            public void process(WatchedEvent event) {
                synchronized (lock) {
                    lock.notifyAll();
                }
            }
        });
        lock.wait();
    }

}

From source file:org.apache.bookkeeper.benchmark.BenchReadThroughputLatency.java

License:Apache License

@SuppressWarnings("deprecation")
public static void main(String[] args) throws Exception {
    Options options = new Options();
    options.addOption("ledger", true, "Ledger to read. If empty, read all ledgers which come available. "
            + " Cannot be used with -listen");
    options.addOption("listen", true, "Listen for creation of <arg> ledgers, and read each one fully");
    options.addOption("password", true, "Password used to access ledgers (default 'benchPasswd')");
    options.addOption("zookeeper", true, "Zookeeper ensemble, default \"localhost:2181\"");
    options.addOption("sockettimeout", true, "Socket timeout for bookkeeper client. In seconds. Default 5");
    options.addOption("help", false, "This message");

    CommandLineParser parser = new PosixParser();
    CommandLine cmd = parser.parse(options, args);

    if (cmd.hasOption("help")) {
        usage(options);//  w  ww .j a  va2  s  .co m
        System.exit(-1);
    }

    final String servers = cmd.getOptionValue("zookeeper", "localhost:2181");
    final byte[] passwd = cmd.getOptionValue("password", "benchPasswd").getBytes(UTF_8);
    final int sockTimeout = Integer.parseInt(cmd.getOptionValue("sockettimeout", "5"));
    if (cmd.hasOption("ledger") && cmd.hasOption("listen")) {
        LOG.error("Cannot used -ledger and -listen together");
        usage(options);
        System.exit(-1);
    }

    final AtomicInteger ledger = new AtomicInteger(0);
    final AtomicInteger numLedgers = new AtomicInteger(0);
    if (cmd.hasOption("ledger")) {
        ledger.set(Integer.parseInt(cmd.getOptionValue("ledger")));
    } else if (cmd.hasOption("listen")) {
        numLedgers.set(Integer.parseInt(cmd.getOptionValue("listen")));
    } else {
        LOG.error("You must use -ledger or -listen");
        usage(options);
        System.exit(-1);
    }

    final CountDownLatch shutdownLatch = new CountDownLatch(1);
    final CountDownLatch connectedLatch = new CountDownLatch(1);
    final String nodepath = String.format("/ledgers/L%010d", ledger.get());

    final ClientConfiguration conf = new ClientConfiguration();
    conf.setReadTimeout(sockTimeout).setZkServers(servers);

    final ZooKeeper zk = new ZooKeeper(servers, 3000, new Watcher() {
        public void process(WatchedEvent event) {
            if (event.getState() == Event.KeeperState.SyncConnected
                    && event.getType() == Event.EventType.None) {
                connectedLatch.countDown();
            }
        }
    });
    final Set<String> processedLedgers = new HashSet<String>();
    try {
        zk.register(new Watcher() {
            public void process(WatchedEvent event) {
                try {
                    if (event.getState() == Event.KeeperState.SyncConnected
                            && event.getType() == Event.EventType.None) {
                        connectedLatch.countDown();
                    } else if (event.getType() == Event.EventType.NodeCreated
                            && event.getPath().equals(nodepath)) {
                        readLedger(conf, ledger.get(), passwd);
                        shutdownLatch.countDown();
                    } else if (event.getType() == Event.EventType.NodeChildrenChanged) {
                        if (numLedgers.get() < 0) {
                            return;
                        }
                        List<String> children = zk.getChildren("/ledgers", true);
                        List<String> ledgers = new ArrayList<String>();
                        for (String child : children) {
                            if (LEDGER_PATTERN.matcher(child).find()) {
                                ledgers.add(child);
                            }
                        }
                        for (String ledger : ledgers) {
                            synchronized (processedLedgers) {
                                if (processedLedgers.contains(ledger)) {
                                    continue;
                                }
                                final Matcher m = LEDGER_PATTERN.matcher(ledger);
                                if (m.find()) {
                                    int ledgersLeft = numLedgers.decrementAndGet();
                                    final Long ledgerId = Long.valueOf(m.group(1));
                                    processedLedgers.add(ledger);
                                    Thread t = new Thread() {
                                        public void run() {
                                            readLedger(conf, ledgerId, passwd);
                                        }
                                    };
                                    t.start();
                                    if (ledgersLeft <= 0) {
                                        shutdownLatch.countDown();
                                    }
                                } else {
                                    LOG.error("Cant file ledger id in {}", ledger);
                                }
                            }
                        }
                    } else {
                        LOG.warn("Unknown event {}", event);
                    }
                } catch (Exception e) {
                    LOG.error("Exception in watcher", e);
                }
            }
        });
        connectedLatch.await();
        if (ledger.get() != 0) {
            if (zk.exists(nodepath, true) != null) {
                readLedger(conf, ledger.get(), passwd);
                shutdownLatch.countDown();
            } else {
                LOG.info("Watching for creation of" + nodepath);
            }
        } else {
            zk.getChildren("/ledgers", true);
        }
        shutdownLatch.await();
        LOG.info("Shutting down");
    } finally {
        zk.close();
    }
}

From source file:org.apache.bookkeeper.benchmark.BenchThroughputLatency.java

License:Apache License

@SuppressWarnings("deprecation")
public static void main(String[] args)
        throws KeeperException, IOException, InterruptedException, ParseException, BKException {
    Options options = new Options();
    options.addOption("time", true, "Running time (seconds), default 60");
    options.addOption("entrysize", true, "Entry size (bytes), default 1024");
    options.addOption("ensemble", true, "Ensemble size, default 3");
    options.addOption("quorum", true, "Quorum size, default 2");
    options.addOption("ackQuorum", true, "Ack quorum size, default is same as quorum");
    options.addOption("throttle", true, "Max outstanding requests, default 10000");
    options.addOption("ledgers", true, "Number of ledgers, default 1");
    options.addOption("zookeeper", true, "Zookeeper ensemble, default \"localhost:2181\"");
    options.addOption("password", true, "Password used to create ledgers (default 'benchPasswd')");
    options.addOption("coordnode", true, "Coordination znode for multi client benchmarks (optional)");
    options.addOption("timeout", true, "Number of seconds after which to give up");
    options.addOption("sockettimeout", true, "Socket timeout for bookkeeper client. In seconds. Default 5");
    options.addOption("skipwarmup", false, "Skip warm up, default false");
    options.addOption("sendlimit", true, "Max number of entries to send. Default 20000000");
    options.addOption("latencyFile", true, "File to dump latencies. Default is latencyDump.dat");
    options.addOption("help", false, "This message");

    CommandLineParser parser = new PosixParser();
    CommandLine cmd = parser.parse(options, args);

    if (cmd.hasOption("help")) {
        HelpFormatter formatter = new HelpFormatter();
        formatter.printHelp("BenchThroughputLatency <options>", options);
        System.exit(-1);//from  ww  w .ja v a  2s .co m
    }

    long runningTime = Long.parseLong(cmd.getOptionValue("time", "60"));
    String servers = cmd.getOptionValue("zookeeper", "localhost:2181");
    int entrysize = Integer.parseInt(cmd.getOptionValue("entrysize", "1024"));

    int ledgers = Integer.parseInt(cmd.getOptionValue("ledgers", "1"));
    int ensemble = Integer.parseInt(cmd.getOptionValue("ensemble", "3"));
    int quorum = Integer.parseInt(cmd.getOptionValue("quorum", "2"));
    int ackQuorum = quorum;
    if (cmd.hasOption("ackQuorum")) {
        ackQuorum = Integer.parseInt(cmd.getOptionValue("ackQuorum"));
    }
    int throttle = Integer.parseInt(cmd.getOptionValue("throttle", "10000"));
    int sendLimit = Integer.parseInt(cmd.getOptionValue("sendlimit", "20000000"));

    final int sockTimeout = Integer.parseInt(cmd.getOptionValue("sockettimeout", "5"));

    String coordinationZnode = cmd.getOptionValue("coordnode");
    final byte[] passwd = cmd.getOptionValue("password", "benchPasswd").getBytes(UTF_8);

    String latencyFile = cmd.getOptionValue("latencyFile", "latencyDump.dat");

    Timer timeouter = new Timer();
    if (cmd.hasOption("timeout")) {
        final long timeout = Long.parseLong(cmd.getOptionValue("timeout", "360")) * 1000;

        timeouter.schedule(new TimerTask() {
            public void run() {
                System.err.println("Timing out benchmark after " + timeout + "ms");
                System.exit(-1);
            }
        }, timeout);
    }

    LOG.warn("(Parameters received) running time: " + runningTime + ", entry size: " + entrysize
            + ", ensemble size: " + ensemble + ", quorum size: " + quorum + ", throttle: " + throttle
            + ", number of ledgers: " + ledgers + ", zk servers: " + servers + ", latency file: "
            + latencyFile);

    long totalTime = runningTime * 1000;

    // Do a warmup run
    Thread thread;

    byte data[] = new byte[entrysize];
    Arrays.fill(data, (byte) 'x');

    ClientConfiguration conf = new ClientConfiguration();
    conf.setThrottleValue(throttle).setReadTimeout(sockTimeout).setZkServers(servers);

    if (!cmd.hasOption("skipwarmup")) {
        long throughput;
        LOG.info("Starting warmup");

        throughput = warmUp(data, ledgers, ensemble, quorum, passwd, conf);
        LOG.info("Warmup tp: " + throughput);
        LOG.info("Warmup phase finished");
    }

    // Now do the benchmark
    BenchThroughputLatency bench = new BenchThroughputLatency(ensemble, quorum, ackQuorum, passwd, ledgers,
            sendLimit, conf);
    bench.setEntryData(data);
    thread = new Thread(bench);
    ZooKeeper zk = null;

    if (coordinationZnode != null) {
        final CountDownLatch connectLatch = new CountDownLatch(1);
        zk = new ZooKeeper(servers, 15000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                if (event.getState() == KeeperState.SyncConnected) {
                    connectLatch.countDown();
                }
            }
        });
        if (!connectLatch.await(10, TimeUnit.SECONDS)) {
            LOG.error("Couldn't connect to zookeeper at " + servers);
            zk.close();
            System.exit(-1);
        }

        final CountDownLatch latch = new CountDownLatch(1);
        LOG.info("Waiting for " + coordinationZnode);
        if (zk.exists(coordinationZnode, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                if (event.getType() == EventType.NodeCreated) {
                    latch.countDown();
                }
            }
        }) != null) {
            latch.countDown();
        }
        latch.await();
        LOG.info("Coordination znode created");
    }
    thread.start();
    Thread.sleep(totalTime);
    thread.interrupt();
    thread.join();

    LOG.info("Calculating percentiles");

    int numlat = 0;
    for (int i = 0; i < bench.latencies.length; i++) {
        if (bench.latencies[i] > 0) {
            numlat++;
        }
    }
    int numcompletions = numlat;
    numlat = Math.min(bench.sendLimit, numlat);
    long[] latency = new long[numlat];
    int j = 0;
    for (int i = 0; i < bench.latencies.length && j < numlat; i++) {
        if (bench.latencies[i] > 0) {
            latency[j++] = bench.latencies[i];
        }
    }
    Arrays.sort(latency);

    long tp = (long) ((double) (numcompletions * 1000.0) / (double) bench.getDuration());

    LOG.info(numcompletions + " completions in " + bench.getDuration() + " milliseconds: " + tp + " ops/sec");

    if (zk != null) {
        zk.create(coordinationZnode + "/worker-",
                ("tp " + tp + " duration " + bench.getDuration()).getBytes(UTF_8), ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT_SEQUENTIAL);
        zk.close();
    }

    // dump the latencies for later debugging (it will be sorted by entryid)
    OutputStream fos = new BufferedOutputStream(new FileOutputStream(latencyFile));

    for (Long l : latency) {
        fos.write((Long.toString(l) + "\t" + (l / 1000000) + "ms\n").getBytes(UTF_8));
    }
    fos.flush();
    fos.close();

    // now get the latencies
    LOG.info("99th percentile latency: {}", percentile(latency, 99));
    LOG.info("95th percentile latency: {}", percentile(latency, 95));

    bench.close();
    timeouter.cancel();
}

From source file:org.apache.bookkeeper.benchmark.BenchThroughputLatency.java

License:Apache License

private static long warmUp(byte[] data, int ledgers, int ensemble, int qSize, byte[] passwd,
        ClientConfiguration conf) throws KeeperException, IOException, InterruptedException, BKException {
    final CountDownLatch connectLatch = new CountDownLatch(1);
    final int bookies;
    String bookieRegistrationPath = conf.getZkAvailableBookiesPath();
    ZooKeeper zk = null;/*from  w w w . j a  v  a2  s .co  m*/
    try {
        final String servers = conf.getZkServers();
        zk = new ZooKeeper(servers, 15000, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                if (event.getState() == KeeperState.SyncConnected) {
                    connectLatch.countDown();
                }
            }
        });
        if (!connectLatch.await(10, TimeUnit.SECONDS)) {
            LOG.error("Couldn't connect to zookeeper at " + servers);
            throw new IOException("Couldn't connect to zookeeper " + servers);
        }
        bookies = zk.getChildren(bookieRegistrationPath, false).size();
    } finally {
        if (zk != null) {
            zk.close();
        }
    }

    BenchThroughputLatency warmup = new BenchThroughputLatency(bookies, bookies, bookies, passwd, ledgers,
            10000, conf);
    warmup.setEntryData(data);
    Thread thread = new Thread(warmup);
    thread.start();
    thread.join();
    warmup.close();
    return warmup.getThroughput();
}

From source file:org.apache.bookkeeper.bookie.Bookie.java

License:Apache License

private void instantiateZookeeperClient(int port, String zkServers) throws IOException {
    if (zkServers == null) {
        LOG.warn(/*from  w w w .  ja v a 2  s.c  om*/
                "No ZK servers passed to Bookie constructor so BookKeeper clients won't know about this server!");
        zk = null;
        return;
    }
    // Create the ZooKeeper client instance
    zk = new ZooKeeper(zkServers, 10000, new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            // TODO: handle session disconnects and expires
            if (LOG.isDebugEnabled()) {
                LOG.debug("Process: " + event.getType() + " " + event.getPath());
            }
        }
    });
    // Create the ZK ephemeral node for this Bookie.
    try {
        zk.create(BOOKIE_REGISTRATION_PATH + InetAddress.getLocalHost().getHostAddress() + ":" + port,
                new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    } catch (Exception e) {
        LOG.fatal("ZK exception registering ephemeral Znode for Bookie!", e);
        // Throw an IOException back up. This will cause the Bookie
        // constructor to error out. Alternatively, we could do a System
        // exit here as this is a fatal error.
        throw new IOException(e);
    }
}

From source file:org.apache.bookkeeper.client.BookKeeper.java

License:Apache License

/**
 * Create a bookkeeper client. A zookeeper client and a client socket factory
 * will be instantiated as part of this constructor.
 * //from w  w  w  .  ja  v a2 s  . c  o  m
 * @param servers
 *          A list of one of more servers on which zookeeper is running. The
 *          client assumes that the running bookies have been registered with
 *          zookeeper under the path
 *          {@link BookieWatcher#BOOKIE_REGISTRATION_PATH}
 * @throws IOException
 * @throws InterruptedException
 * @throws KeeperException
 */
public BookKeeper(String servers) throws IOException, InterruptedException, KeeperException {
    this(new ZooKeeper(servers, 10000, new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            // TODO: handle session disconnects and expires
            if (LOG.isDebugEnabled()) {
                LOG.debug("Process: " + event.getType() + " " + event.getPath());
            }
        }
    }), new NioClientSocketChannelFactory(Executors.newCachedThreadPool(), Executors.newCachedThreadPool()));

    ownZKHandle = true;
    ownChannelFactory = true;
}