Example usage for org.apache.zookeeper WatchedEvent getState

List of usage examples for org.apache.zookeeper WatchedEvent getState

Introduction

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

Prototype

public KeeperState getState() 

Source Link

Usage

From source file:org.apache.ambari.datastore.ZookeeperDS.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    if (event.getType() == Event.EventType.None) {
        // We are are being told that the state of the
        // connection has changed
        switch (event.getState()) {
        case SyncConnected:
            // In this particular example we don't need to do anything
            // here - watches are automatically re-registered with 
            // server and any watches triggered while the client was 
            // disconnected will be delivered (in order of course)
            this.zkCoonected = true;
            break;
        case Expired:
            // It's all over
            //running = false;
            //commandHandler.stop();
            break;
        }/* ww  w  . ja  va 2  s  . c  o m*/
    }

}

From source file:org.apache.aries.rsa.discovery.zookeeper.ZooKeeperDiscovery.java

License:Apache License

public void process(WatchedEvent event) {
    LOG.debug("got ZooKeeper event " + event);
    switch (event.getState()) {
    case SyncConnected:
        LOG.info("Connection to ZooKeeper established");
        // this event can be triggered more than once in a row (e.g. after Disconnected event),
        // so we must be re-entrant here
        start();//from w  w  w . j  av a  2  s. com
        break;

    case Expired:
        LOG.info("Connection to ZooKeeper expired. Trying to create a new connection");
        stop(false);
        try {
            createZookeeper(curConfiguration);
        } catch (IOException e) {
            LOG.error("Error starting zookeeper client", e);
        }
        break;

    default:
        // ignore other events
        break;
    }
}

From source file:org.apache.asterix.event.service.ZooKeeperService.java

License:Apache License

@Override
public void process(WatchedEvent wEvent) {
    if (wEvent.getState() == KeeperState.SyncConnected) {
        msgQ.add("connected");
    }/*from w  ww.  j av a  2s  .  c  om*/
}

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);/*from w w  w.j  a  v a  2s.  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.  j av a2s  .c o  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;//  w w  w  .  j a va 2s. c om
    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.client.TestBookieWatcher.java

License:Apache License

private void expireZooKeeperSession(ZooKeeper zk, int timeout)
        throws IOException, InterruptedException, KeeperException {
    final CountDownLatch latch = new CountDownLatch(1);
    ZooKeeper newZk = new ZooKeeper(zkUtil.getZooKeeperConnectString(), timeout, new Watcher() {

        @Override/*from w ww.  j a  v  a 2 s  .c  o  m*/
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.None && event.getState() == KeeperState.SyncConnected) {
                latch.countDown();
            }
        }

    }, zk.getSessionId(), zk.getSessionPasswd());
    if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
        throw KeeperException.create(KeeperException.Code.CONNECTIONLOSS);
    }
    newZk.close();
}

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

License:Apache License

@Test(timeout = 10000)
public void testBookieWatcherDieWhenSessionExpired() throws Exception {
    final int timeout = 2000;
    final CountDownLatch connectLatch = new CountDownLatch(1);
    ZooKeeper zk = new ZooKeeper(zkUtil.getZooKeeperConnectString(), timeout, new Watcher() {
        @Override/*w w w. ja  v a2 s .c  om*/
        public void process(WatchedEvent watchedEvent) {
            if (EventType.None == watchedEvent.getType()
                    && KeeperState.SyncConnected == watchedEvent.getState()) {
                connectLatch.countDown();
            }
        }
    });
    connectLatch.await();
    try {
        runBookieWatcherWhenSessionExpired(zk, timeout, false);
    } finally {
        zk.close();
    }
}

From source file:org.apache.bookkeeper.meta.ZkLedgerUnderreplicationManager.java

License:Apache License

@Override
public long getLedgerToRereplicate() throws ReplicationException.UnavailableException {
    LOG.debug("getLedgerToRereplicate()");
    try {/* ww  w.  j  a v a2 s. c  o  m*/
        while (true) {
            waitIfLedgerReplicationDisabled();
            final CountDownLatch changedLatch = new CountDownLatch(1);
            Watcher w = new Watcher() {
                public void process(WatchedEvent e) {
                    if (e.getType() == Watcher.Event.EventType.NodeChildrenChanged
                            || e.getType() == Watcher.Event.EventType.NodeDeleted
                            || e.getType() == Watcher.Event.EventType.NodeCreated
                            || e.getState() == Watcher.Event.KeeperState.Expired
                            || e.getState() == Watcher.Event.KeeperState.Disconnected) {
                        changedLatch.countDown();
                    }
                }
            };
            long ledger = getLedgerToRereplicateFromHierarchy(urLedgerPath, 0, w);
            if (ledger != -1) {
                return ledger;
            }
            // nothing found, wait for a watcher to trigger
            changedLatch.await();
        }
    } catch (KeeperException ke) {
        throw new ReplicationException.UnavailableException("Error contacting zookeeper", ke);
    } catch (InterruptedException ie) {
        Thread.currentThread().interrupt();
        throw new ReplicationException.UnavailableException("Interrupted while connecting zookeeper", ie);
    }
}

From source file:org.apache.bookkeeper.replication.AutoRecoveryMain.java

License:Apache License

public AutoRecoveryMain(ServerConfiguration conf, StatsLogger statsLogger) throws IOException,
        InterruptedException, KeeperException, UnavailableException, CompatibilityException {
    this.conf = conf;
    Set<Watcher> watchers = new HashSet<Watcher>();
    // TODO: better session handling for auto recovery daemon  https://issues.apache.org/jira/browse/BOOKKEEPER-594
    //       since {@link org.apache.bookkeeper.meta.ZkLedgerUnderreplicationManager}
    //       use Watcher, need to ensure the logic works correctly after recreating
    //       a new zookeeper client when session expired.
    //       for now just shutdown it.
    watchers.add(new Watcher() {
        @Override//from   www.j  av a 2  s .c  o m
        public void process(WatchedEvent event) {
            // Check for expired connection.
            if (event.getState().equals(Watcher.Event.KeeperState.Expired)) {
                LOG.error("ZK client connection to the ZK server has expired!");
                shutdown(ExitCode.ZK_EXPIRED);
            }
        }
    });
    zk = ZooKeeperClient.newBuilder().connectString(conf.getZkServers()).sessionTimeoutMs(conf.getZkTimeout())
            .watchers(watchers).build();
    auditorElector = new AuditorElector(Bookie.getBookieAddress(conf).toString(), conf, zk,
            statsLogger.scope(AUDITOR_SCOPE));
    replicationWorker = new ReplicationWorker(zk, conf, Bookie.getBookieAddress(conf),
            statsLogger.scope(REPLICATION_WORKER_SCOPE));
    deathWatcher = new AutoRecoveryDeathWatcher(this);
}