Example usage for org.apache.zookeeper WatchedEvent getType

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

Introduction

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

Prototype

public EventType getType() 

Source Link

Usage

From source file:org.apache.airavata.gfac.impl.watcher.CancelRequestWatcherImpl.java

License:Apache License

@Override
public void process(WatchedEvent watchedEvent) throws Exception {
    // this watcher change data in cancel listener node in the experiment node
    String path = watchedEvent.getPath();
    Watcher.Event.EventType type = watchedEvent.getType();
    CuratorFramework curatorClient = Factory.getCuratorClient();
    log.info("cancel watcher triggered process id {}.", processId);
    switch (type) {
    case NodeDataChanged:
        byte[] bytes = curatorClient.getData().forPath(path);
        String action = new String(bytes);
        if (action.equalsIgnoreCase(ZkConstants.ZOOKEEPER_CANCEL_REQEUST)) {
            cancelProcess(0);// w  w  w. j  a va 2s  .  com
        } else {
            curatorClient.getData().usingWatcher(this).forPath(path);
        }
        break;
    case NodeDeleted:
        //end of experiment execution, ignore this event
        log.info("expId: {}, cancel watcher trigger for process {} with event type {}", experimentId, processId,
                type.name());
        break;
    case NodeCreated:
    case NodeChildrenChanged:
    case None:
        log.info("expId: {}, Cancel watcher trigger for process {} with event type {}", experimentId, processId,
                type.name());
        if (path != null) {
            curatorClient.getData().usingWatcher(this).forPath(path);
        }
        break;
    default:
        log.info("expId: {}, Cancel watcher trigger for process {} with event type {}", experimentId, processId,
                type.name());
        if (path != null) {
            curatorClient.getData().usingWatcher(this).forPath(path);
        }
        break;
    }
}

From source file:org.apache.airavata.gfac.impl.watcher.RedeliveryRequestWatcherImpl.java

License:Apache License

@Override
public void process(WatchedEvent watchedEvent) throws Exception {
    String path = watchedEvent.getPath();
    Watcher.Event.EventType eventType = watchedEvent.getType();
    log.info("Redelivery request came for zk path {} event type {} ", path, eventType.name());
    CuratorFramework curatorClient = Factory.getCuratorClient();
    switch (eventType) {
    case NodeDataChanged:
        byte[] bytes = curatorClient.getData().forPath(path);
        String serverName = new String(bytes);
        if (ServerSettings.getGFacServerName().trim().equals(serverName)) {
            curatorClient.getData().usingWatcher(this).forPath(path);
            log.info("processId: {},event type {}, change data with same server name : {}", processId,
                    eventType, serverName);
        } else {//ww w . j  ava2s  .  c  o  m
            ProcessContext processContext = Factory.getGfacContext().getProcess(processId);
            if (processContext != null) {
                processContext.setHandOver(true);
                log.info("processId : {}, event type {}, handing over to new server instance : {}", processId,
                        eventType, serverName);
            } else {
                log.info("Redelivery request came for processId {}, with event type {}, but couldn't find "
                        + "process context", processId, eventType.name());
            }
        }
        break;
    case NodeDeleted:
        //end of experiment execution, ignore this event
        log.info("Redelivery watcher trigger for process {} with event type {}", processId, eventType.name());
        break;
    case NodeCreated:
    case NodeChildrenChanged:
    case None:
        if (path != null) {
            curatorClient.getData().usingWatcher(this).forPath(path);
            log.info("Redelivery watcher trigger for process {} with event type {}", processId,
                    eventType.name());
        }
        break;
    // not yet implemented
    default:
        if (path != null) {
            curatorClient.getData().usingWatcher(this).forPath(path);
            log.info("Redelivery watcher trigger for process {} with event type {}", processId,
                    eventType.name());
        }
        break;
    }
}

From source file:org.apache.airavata.gfac.monitor.handlers.GridPullMonitorHandler.java

License:Apache License

public void process(WatchedEvent watchedEvent) {
    if (Event.EventType.NodeDataChanged.equals(watchedEvent.getType())) {
        // node data is changed, this means node is cancelled.
        logger.info("Experiment is cancelled with this path:" + watchedEvent.getPath());

        String[] split = watchedEvent.getPath().split("/");
        for (String element : split) {
            if (element.contains("+")) {
                logger.info("Adding experimentID+TaskID to be removed from monitoring:" + element);
                hpcPullMonitor.getCancelJobList().add(element);
            }/*  w  ww.j a v a2s .  c o m*/
        }
    }
}

From source file:org.apache.airavata.orchestrator.core.impl.GFACPassiveJobSubmitter.java

License:Apache License

synchronized public void process(WatchedEvent event) {
    logger.info(getClass().getName() + event.getPath());
    logger.info(getClass().getName() + event.getType());
    synchronized (mutex) {
        switch (event.getState()) {
        case SyncConnected:
            mutex.notify();/*from ww w .ja  v a2s. com*/
        }
        switch (event.getType()) {
        case NodeCreated:
            mutex.notify();
            break;
        }
    }
}

From source file:org.apache.airavata.orchestrator.core.impl.GFACRPCJobSubmitter.java

License:Apache License

synchronized public void process(WatchedEvent event) {
    synchronized (mutex) {
        switch (event.getState()) {
        case SyncConnected:
            mutex.notify();//from   w w w .  j a va 2 s  . c o  m
        }
        switch (event.getType()) {
        case NodeCreated:
            mutex.notify();
            break;
        }
    }
}

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;
        }//from w  w w .  j  av a  2s.  co  m
    }

}

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 av  a  2 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  w  w w .  j a  va  2 s. 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.bookie.Bookie.java

License:Apache License

private void instantiateZookeeperClient(int port, String zkServers) throws IOException {
    if (zkServers == null) {
        LOG.warn(/*from   ww  w  . ja va  2s  .  co m*/
                "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.
 * /*  w w w.j  a v  a  2  s. c om*/
 * @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;
}