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: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 {/*from   w ww . ja va  2s .  com*/
        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 .j a  va 2s  . 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:crunch.MaxTemperature.java

License:Apache License

@Override
    public void process(WatchedEvent event) {
        if (event.getType() == EventType.NodeDataChanged) {
            try {
                displayConfig();/* ww  w .j a va 2 s.  c  o  m*/
            } catch (InterruptedException e) {
                System.err.println("Interrupted. Exiting.");
                Thread.currentThread().interrupt();
            } catch (KeeperException e) {
                System.err.printf("KeeperException: %s. Exiting.\n", e);
            }
        }
    }

From source file:dsync.synchronization.impl.zk.SessionManager.java

License:Apache License

public void process(WatchedEvent watchedEvent) {
    if (watchedEvent.getType() == Event.EventType.None) {
        switch (watchedEvent.getState()) {
        case Disconnected:
        case Expired:
            revocate();/*from w  w w  .j ava 2s.c o m*/
        }
    }

}

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();//from  w ww .  j  av  a 2s .  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  w  w  w .  ja  v a  2  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.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.j a  v a  2s .  co m*/
        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:gr.ntua.h2rdf.concurrent.SyncPrimitive.java

License:Open Source License

synchronized public void process(WatchedEvent event) {
    synchronized (mutex) {
        //System.out.println("Process: " + event.getType());
        mutex.notify();//w w w .j a  v  a  2s .c  o m
    }
    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();
                zk = new ZooKeeper(address, 3000, this);
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
            break;
        }
    }
}

From source file:io.confluent.admin.utils.ZookeeperConnectionWatcher.java

License:Apache License

@Override
public void process(WatchedEvent event) {
    if (event.getType() == Event.EventType.None) {
        switch (event.getState()) {
        case SyncConnected:
            // If SASL is enabled, we want to wait for the SaslAuthenticated event.
            if (!isSASLEnabled) {
                connectSignal.countDown();
            }//from  ww  w.  ja v  a2s.c  om
            break;
        case Expired:
            failureMessage = "Session expired.";
            isSuccessful = false;
            connectSignal.countDown();
            break;
        case Disconnected:
            failureMessage = "Disconnected from the server.";
            isSuccessful = false;
            connectSignal.countDown();
            break;
        case AuthFailed:
            failureMessage = "Authentication failed.";
            isSuccessful = false;
            connectSignal.countDown();
            break;
        case SaslAuthenticated:
            connectSignal.countDown();
            break;
        }
    }
}

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;/*from w  ww . j a  v  a  2s  . c  om*/
    }

    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);
    }
}