Example usage for org.apache.zookeeper WatchedEvent getPath

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

Introduction

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

Prototype

public String getPath() 

Source Link

Usage

From source file:com.yahoo.pulsar.zookeeper.ZooKeeperCache.java

License:Apache License

public <T> void process(WatchedEvent event, final CacheUpdater<T> updater) {
    final String path = event.getPath();
    if (path != null) {
        dataCache.synchronous().invalidate(path);
        childrenCache.invalidate(path);//from w  w w  . jav a  2  s. co m
        existsCache.invalidate(path);
        if (executor != null && updater != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Submitting reload cache task to the executor for path: {}, updater: {}", path,
                        updater);
            }
            try {
                executor.submitOrdered(path, new SafeRunnable() {
                    @Override
                    public void safeRun() {
                        updater.reloadCache(path);
                    }
                });
            } catch (RejectedExecutionException e) {
                // Ok, the service is shutting down
            }
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Cannot reload cache for path: {}, updater: {}", path, updater);
            }
        }
    }
}

From source file:com.yihaodian.architecture.zkclient.ZkClient.java

License:Apache License

public void process(WatchedEvent event) {
    LOG.debug("Received event: " + event);
    _zookeeperEventThread = Thread.currentThread();

    boolean stateChanged = event.getPath() == null;
    boolean znodeChanged = event.getPath() != null;
    boolean dataChanged = event.getType() == EventType.NodeDataChanged
            || event.getType() == EventType.NodeDeleted || event.getType() == EventType.NodeCreated
            || event.getType() == EventType.NodeChildrenChanged;

    getEventLock().lock();//  w  w  w.j av a2s  . c om
    try {

        // We might have to install child change event listener if a new
        // node was created
        if (getShutdownTrigger()) {
            LOG.debug("ignoring event '{" + event.getType() + " | " + event.getPath()
                    + "}' since shutdown triggered");
            return;
        }
        if (stateChanged) {
            processStateChanged(event);
        }
        if (dataChanged) {
            processDataOrChildChange(event);
        }
    } finally {
        if (stateChanged) {
            getEventLock().getStateChangedCondition().signalAll();

            // If the session expired we have to signal all conditions,
            // because watches might have been removed and
            // there is no guarantee that those
            // conditions will be signaled at all after an Expired event
            // TODO PVo write a test for this
            if (event.getState() == KeeperState.Expired) {
                getEventLock().getZNodeEventCondition().signalAll();
                getEventLock().getDataChangedCondition().signalAll();
                // We also have to notify all listeners that something might
                // have changed
                fireAllEvents();
            }
        }
        if (znodeChanged) {
            getEventLock().getZNodeEventCondition().signalAll();
        }
        if (dataChanged) {
            getEventLock().getDataChangedCondition().signalAll();
        }
        getEventLock().unlock();
        LOG.debug("Leaving process event");
    }
}

From source file:concurrent.Executor.java

License:Open Source License

public void process(WatchedEvent event) {
    String path = event.getPath();
    if (event.getType() == Event.EventType.None) {
        // We are are being told that the state of the
        // connection has changed
    } else {/*www  .ja v a2  s. co  m*/
        if (path.equals(filename) && (event.getType() == Event.EventType.NodeCreated)) {
            // It's all over
            System.out.println("Finished ");
            try {
                dead = true;
                synchronized (this) {
                    notifyAll();
                }
                zk.delete(filename, 0);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (KeeperException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /*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)
        break;
    case Expired:
        // It's all over
       dead=true;
        synchronized (this) {
            notifyAll();
        }
        break;
    }
    } else {
    if (path != null && path.equals(filename)) {
        // Something has changed on the node, let's find out
        zk.exists(filename, true, this, null);
    }
    }*/
}

From source file:concurrent.Producer.java

License:Open Source License

@Override
public void process(WatchedEvent event) {
    String path = event.getPath();
    if (event.getType() == Event.EventType.None) {
        // We are are being told that the state of the
        // connection has changed
    } else {//from  w w w  .ja  va2s . c o m
        if (path.startsWith("/out") && (event.getType() == Event.EventType.NodeCreated)) {
            // It's all over
            try {
                int temp = zk.getChildren("/out", true).size();
                if (temp - prevOut >= 400) {
                    long stop = new Date().getTime();
                    double t = (double) (temp - prevOut) * 1000 / (double) (stop - startTime);
                    System.out.println("start: " + startTime + " stop: " + stop + " throughput: " + t);
                    startTime = new Date().getTime();
                    prevOut = temp;
                    //finished=0;
                }
                /*finished++;
                if(finished>=400){
                   long stop = new Date().getTime();
                   double t = (double) 400*1000/(double)(stop-startTime);
                   System.out.println("start: "+ startTime+" stop: "+stop+" throughput: "+t);
                   startTime = new Date().getTime();
                   finished=0;
                }*/

                //zk.delete(path, 0);
                //fs.delete("output/"+Join, arg1)

                if (finished >= total) {
                    System.out.println("Number of Queries: " + total);
                    dead = true;
                    synchronized (this) {
                        notifyAll();
                    }
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (KeeperException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

From source file:fr.eurecom.rs.clouds.ZookeeperLab.watches.Configuration.java

License:Apache License

synchronized public void process(WatchedEvent event) {
    if (event == null || event.getPath() == null)
        return;// w w  w .j  a v a  2s  . c o  m
    if (event.getPath().equals(root)) {
        //TODO complete the Configuration code         
        try {
            Stat s1 = new Stat();
            zk.getData(this.root, true, s1);
            this.printStat(s1);
        } catch (KeeperException e) {
            System.out.println("keeper exception when instantiating consensus: " + e.toString());
            System.exit(2);
        } catch (InterruptedException e) {
            System.out.println("interrupted exception");
            System.exit(2);
        } // try/catch
    }
}

From source file:gr.ntua.h2rdf.client.Executor.java

License:Open Source License

public void process(WatchedEvent event) {
    //System.out.println("message");
    String path = event.getPath();
    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)
            break;
        case Expired:
            try {
                zk.close();//  ww  w . ja  v a2 s .c o m
                zk = new ZooKeeper(conf.getAddress(), 3000, this);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
    } else {
        if (path.equals(filename) && (event.getType() == Event.EventType.NodeCreated)) {
            // It's all over
            System.out.println("Finished ");
            try {
                dead = true;
                Stat stat = null;
                outfile = Bytes.toString(zk.getData(filename, false, stat));
                //System.out.println(outfile +"dfsdfsd");
                zk.delete(filename, 0);
                synchronized (this) {
                    notifyAll();
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (KeeperException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /*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)
        break;
    case Expired:
        // It's all over
       dead=true;
        synchronized (this) {
            notifyAll();
        }
        break;
    }
    } else {
    if (path != null && path.equals(filename)) {
        // Something has changed on the node, let's find out
        zk.exists(filename, true, this, null);
    }
    }*/
}

From source file:gr.ntua.h2rdf.client.ExecutorOpenRdf.java

License:Open Source License

public void process(WatchedEvent event) {
    //System.out.println("message");
    String path = event.getPath();
    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)
            break;
        case Expired:
            try {
                zk.close();/*from ww w.  j a  va 2  s. com*/
                zk = new ZooKeeper(conf.getAddress(), 3000, this);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
    } else {
        if (path.equals(filename) && (event.getType() == Event.EventType.NodeCreated)) {
            // It's all over
            //System.out.println("Finished ");
            try {
                dead = true;
                Stat stat = null;
                outfile = Bytes.toString(zk.getData(filename, false, stat));
                //System.out.println(outfile +"dfsdfsd");
                zk.delete(filename, 0);
                synchronized (this) {
                    notifyAll();
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (KeeperException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

    /*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)
           break;
       case Expired:
           // It's all over
          dead=true;
           synchronized (this) {
               notifyAll();
           }
           break;
       }
    } else {
       if (path != null && path.equals(filename)) {
           // Something has changed on the node, let's find out
           zk.exists(filename, true, this, null);
       }
    }*/
}

From source file:gr.ntua.h2rdf.client.JavaApiCall.java

License:Open Source License

public void process(WatchedEvent event) {
    //System.out.println("message");
    String path = event.getPath();
    if (event.getType() == Event.EventType.None) {
        // We are are being told that the state of the
        // connection has changed
    } else {//from  w w w . java  2 s  . c  om
        if (path.equals(filename) && (event.getType() == Event.EventType.NodeCreated)) {
            // It's all over
            //System.out.println("Finished ");
            try {
                dead = true;
                Stat stat = null;
                outfile = zk.getData(filename, false, stat);
                //System.out.println(outfile +"dfsdfsd");
                zk.delete(filename, 0);
                synchronized (this) {
                    notifyAll();
                }
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (KeeperException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    }

}

From source file:io.druid.server.coordinator.CuratorLoadQueuePeon.java

License:Apache License

private void processSegmentChangeRequest() {
    if (currentlyProcessing != null) {
        log.debug("Server[%s] skipping processSegmentChangeRequest because something is currently loading[%s].",
                basePath, currentlyProcessing.getSegmentIdentifier());

        return;/*  www .  ja  v  a 2s.  c  o  m*/
    }

    if (!segmentsToDrop.isEmpty()) {
        currentlyProcessing = segmentsToDrop.firstEntry().getValue();
        log.info("Server[%s] dropping [%s]", basePath, currentlyProcessing.getSegmentIdentifier());
    } else if (!segmentsToLoad.isEmpty()) {
        currentlyProcessing = segmentsToLoad.firstEntry().getValue();
        log.info("Server[%s] loading [%s]", basePath, currentlyProcessing.getSegmentIdentifier());
    } else {
        return;
    }

    try {
        if (currentlyProcessing == null) {
            if (!stopped) {
                log.makeAlert("Crazy race condition! server[%s]", basePath).emit();
            }
            actionCompleted();
            return;
        }

        log.info("Server[%s] processing segment[%s]", basePath, currentlyProcessing.getSegmentIdentifier());
        final String path = ZKPaths.makePath(basePath, currentlyProcessing.getSegmentIdentifier());
        final byte[] payload = jsonMapper.writeValueAsBytes(currentlyProcessing.getChangeRequest());
        curator.create().withMode(CreateMode.EPHEMERAL).forPath(path, payload);

        processingExecutor.schedule(new Runnable() {
            @Override
            public void run() {
                try {
                    if (curator.checkExists().forPath(path) != null) {
                        failAssign(new ISE("%s was never removed! Failing this operation!", path));
                    }
                } catch (Exception e) {
                    failAssign(e);
                }
            }
        }, config.getLoadTimeoutDelay().getMillis(), TimeUnit.MILLISECONDS);

        final Stat stat = curator.checkExists().usingWatcher(new CuratorWatcher() {
            @Override
            public void process(WatchedEvent watchedEvent) throws Exception {
                switch (watchedEvent.getType()) {
                case NodeDeleted:
                    entryRemoved(watchedEvent.getPath());
                    break;
                default:
                    // do nothing
                }
            }
        }).forPath(path);

        if (stat == null) {
            final byte[] noopPayload = jsonMapper.writeValueAsBytes(new SegmentChangeRequestNoop());

            // Create a node and then delete it to remove the registered watcher.  This is a work-around for
            // a zookeeper race condition.  Specifically, when you set a watcher, it fires on the next event
            // that happens for that node.  If no events happen, the watcher stays registered foreverz.
            // Couple that with the fact that you cannot set a watcher when you create a node, but what we
            // want is to create a node and then watch for it to get deleted.  The solution is that you *can*
            // set a watcher when you check to see if it exists so, we first create the node and then set a
            // watcher on its existence.  However, if already does not exist by the time the existence check
            // returns, then the watcher that was set will never fire (nobody will ever create the node
            // again) and thus lead to a slow, but real, memory leak.  So, we create another node to cause
            // that watcher to fire and delete it right away.
            //
            // We do not create the existence watcher first, because then it will fire when we create the
            // node and we'll have the same race when trying to refresh that watcher.
            curator.create().withMode(CreateMode.EPHEMERAL).forPath(path, noopPayload);

            entryRemoved(path);
        }
    } catch (Exception e) {
        failAssign(e);
    }
}

From source file:io.druid.server.coordinator.LoadQueuePeon.java

License:Apache License

private void doNext() {
    synchronized (lock) {
        if (currentlyProcessing == null) {
            if (!segmentsToDrop.isEmpty()) {
                currentlyProcessing = segmentsToDrop.firstEntry().getValue();
                log.info("Server[%s] dropping [%s]", basePath, currentlyProcessing.getSegmentIdentifier());
            } else if (!segmentsToLoad.isEmpty()) {
                currentlyProcessing = segmentsToLoad.firstEntry().getValue();
                log.info("Server[%s] loading [%s]", basePath, currentlyProcessing.getSegmentIdentifier());
            } else {
                return;
            }/*from w  w w.  jav  a  2  s  .c om*/

            zkWritingExecutor.execute(new Runnable() {
                @Override
                public void run() {
                    synchronized (lock) {
                        try {
                            // expected when the coordinator looses leadership and LoadQueuePeon is stopped.
                            if (currentlyProcessing == null) {
                                if (!stopped) {
                                    log.makeAlert("Crazy race condition! server[%s]", basePath).emit();
                                }
                                actionCompleted();
                                doNext();
                                return;
                            }
                            log.info("Server[%s] processing segment[%s]", basePath,
                                    currentlyProcessing.getSegmentIdentifier());
                            final String path = ZKPaths.makePath(basePath,
                                    currentlyProcessing.getSegmentIdentifier());
                            final byte[] payload = jsonMapper
                                    .writeValueAsBytes(currentlyProcessing.getChangeRequest());
                            curator.create().withMode(CreateMode.EPHEMERAL).forPath(path, payload);

                            zkWritingExecutor.schedule(new Runnable() {
                                @Override
                                public void run() {
                                    try {
                                        if (curator.checkExists().forPath(path) != null) {
                                            failAssign(new ISE("%s was never removed! Failing this operation!",
                                                    path));
                                        }
                                    } catch (Exception e) {
                                        failAssign(e);
                                    }
                                }
                            }, config.getLoadTimeoutDelay().getMillis(), TimeUnit.MILLISECONDS);

                            final Stat stat = curator.checkExists().usingWatcher(new CuratorWatcher() {
                                @Override
                                public void process(WatchedEvent watchedEvent) throws Exception {
                                    switch (watchedEvent.getType()) {
                                    case NodeDeleted:
                                        entryRemoved(watchedEvent.getPath());
                                    }
                                }
                            }).forPath(path);

                            if (stat == null) {
                                final byte[] noopPayload = jsonMapper
                                        .writeValueAsBytes(new SegmentChangeRequestNoop());

                                // Create a node and then delete it to remove the registered watcher.  This is a work-around for
                                // a zookeeper race condition.  Specifically, when you set a watcher, it fires on the next event
                                // that happens for that node.  If no events happen, the watcher stays registered foreverz.
                                // Couple that with the fact that you cannot set a watcher when you create a node, but what we
                                // want is to create a node and then watch for it to get deleted.  The solution is that you *can*
                                // set a watcher when you check to see if it exists so, we first create the node and then set a
                                // watcher on its existence.  However, if already does not exist by the time the existence check
                                // returns, then the watcher that was set will never fire (nobody will ever create the node
                                // again) and thus lead to a slow, but real, memory leak.  So, we create another node to cause
                                // that watcher to fire and delete it right away.
                                //
                                // We do not create the existence watcher first, because then it will fire when we create the
                                // node and we'll have the same race when trying to refresh that watcher.
                                curator.create().withMode(CreateMode.EPHEMERAL).forPath(path, noopPayload);

                                entryRemoved(path);
                            }
                        } catch (Exception e) {
                            failAssign(e);
                        }
                    }
                }
            });
        } else {
            log.info("Server[%s] skipping doNext() because something is currently loading[%s].", basePath,
                    currentlyProcessing.getSegmentIdentifier());
        }
    }
}