Search in sources :

Example 1 with Watcher

use of org.apache.zookeeper_voltpatches.Watcher in project voltdb by VoltDB.

the class DataTree method deleteNode.

/**
     * remove the path from the datatree
     *
     * @param path
     *            the path to of the node to be deleted
     * @param zxid
     *            the current zxid
     * @throws KeeperException.NoNodeException
     */
public void deleteNode(String path, long zxid) throws KeeperException.NoNodeException {
    int lastSlash = path.lastIndexOf('/');
    String parentName = path.substring(0, lastSlash);
    String childName = path.substring(lastSlash + 1);
    DataNode node = nodes.get(path);
    if (node == null) {
        throw new KeeperException.NoNodeException();
    }
    nodes.remove(path);
    DataNode parent = nodes.get(parentName);
    if (parent == null) {
        throw new KeeperException.NoNodeException();
    }
    synchronized (parent) {
        parent.removeChild(childName);
        parent.stat.setCversion(parent.stat.getCversion() + 1);
        parent.stat.setPzxid(zxid);
        long eowner = node.stat.getEphemeralOwner();
        if (eowner != 0) {
            HashSet<String> nodes = ephemerals.get(eowner);
            if (nodes != null) {
                synchronized (nodes) {
                    nodes.remove(path);
                }
            }
        }
        node.parent = null;
    }
    if (parentName.startsWith(procZookeeper)) {
        // delete the node in the trie.
        if (Quotas.limitNode.equals(childName)) {
            // we need to update the trie
            // as well
            pTrie.deletePath(parentName.substring(quotaZookeeper.length()));
        }
    }
    // also check to update the quotas for this node
    String lastPrefix = pTrie.findMaxPrefix(path);
    if (!rootZookeeper.equals(lastPrefix) && !("".equals(lastPrefix))) {
        // ok we have some match and need to update
        updateCount(lastPrefix, -1);
        int bytes = 0;
        synchronized (node) {
            bytes = (node.data == null ? 0 : -(node.data.length));
        }
        updateBytes(lastPrefix, bytes);
    }
    if (LOG.isTraceEnabled()) {
        ZooTrace.logTraceMessage(LOG, ZooTrace.EVENT_DELIVERY_TRACE_MASK, "dataWatches.triggerWatch " + path);
        ZooTrace.logTraceMessage(LOG, ZooTrace.EVENT_DELIVERY_TRACE_MASK, "childWatches.triggerWatch " + parentName);
    }
    Set<Watcher> processed = dataWatches.triggerWatch(path, EventType.NodeDeleted);
    childWatches.triggerWatch(path, EventType.NodeDeleted, processed);
    childWatches.triggerWatch(parentName.equals("") ? "/" : parentName, EventType.NodeChildrenChanged);
}
Also used : NoNodeException(org.apache.zookeeper_voltpatches.KeeperException.NoNodeException) DataNode(org.apache.zookeeper_voltpatches.server.DataNode) Watcher(org.apache.zookeeper_voltpatches.Watcher)

Example 2 with Watcher

use of org.apache.zookeeper_voltpatches.Watcher in project voltdb by VoltDB.

the class TestZK method testDataWatches.

@Test
public void testDataWatches() throws Exception {
    ZooKeeper zk = getClient(0);
    ZooKeeper zk2 = getClient(1);
    final Semaphore sem = new Semaphore(0);
    zk2.create("/foo", new byte[1], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk.getData("/foo", new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.NodeDataChanged) {
                sem.release();
                System.out.println(event);
            }
        }
    }, null);
    zk2.setData("/foo", new byte[2], -1);
    Stat stat = new Stat();
    zk.getData("/foo", false, stat);
    boolean threwException = false;
    try {
        zk2.setData("/foo", new byte[3], stat.getVersion());
        zk.setData("/foo", new byte[3], stat.getVersion());
    } catch (BadVersionException e) {
        threwException = true;
        e.printStackTrace();
    }
    assertTrue(threwException);
}
Also used : WatchedEvent(org.apache.zookeeper_voltpatches.WatchedEvent) ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) Stat(org.apache.zookeeper_voltpatches.data.Stat) Watcher(org.apache.zookeeper_voltpatches.Watcher) BadVersionException(org.apache.zookeeper_voltpatches.KeeperException.BadVersionException) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

Example 3 with Watcher

use of org.apache.zookeeper_voltpatches.Watcher in project voltdb by VoltDB.

the class ZKUtil method getClient.

public static final ZooKeeper getClient(String zkAddress, int timeout, Set<Long> verbotenThreads) throws Exception {
    final Semaphore zkConnect = new Semaphore(0);
    ZooKeeper zk = new ZooKeeper(zkAddress, 2000, new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getState() == KeeperState.SyncConnected) {
                zkConnect.release();
            }
        }
    }, verbotenThreads);
    if (!zkConnect.tryAcquire(timeout, TimeUnit.MILLISECONDS)) {
        return null;
    }
    return zk;
}
Also used : WatchedEvent(org.apache.zookeeper_voltpatches.WatchedEvent) ZooKeeper(org.apache.zookeeper_voltpatches.ZooKeeper) Watcher(org.apache.zookeeper_voltpatches.Watcher) Semaphore(java.util.concurrent.Semaphore)

Example 4 with Watcher

use of org.apache.zookeeper_voltpatches.Watcher in project voltdb by VoltDB.

the class SnapshotDaemon method processUserSnapshotRequestResponse.

void processUserSnapshotRequestResponse(final WatchedEvent event, final long clientHandle, final Connection c, final boolean notifyChanges) throws Exception {
    byte[] responseBytes = m_zk.getData(event.getPath(), false, null);
    try {
        m_zk.delete(event.getPath(), -1, null, null);
    } catch (Exception e) {
        SNAP_LOG.error("Error cleaning up user snapshot request response in ZK", e);
    }
    ByteBuffer buf = ByteBuffer.wrap(responseBytes);
    ClientResponseImpl response = new ClientResponseImpl();
    response.initFromBuffer(buf);
    response.setClientHandle(clientHandle);
    // Not sure if we need to preserve the original byte buffer here, playing it safe
    ByteBuffer buf2 = ByteBuffer.allocate(response.getSerializedSize() + 4);
    buf2.putInt(buf2.capacity() - 4);
    response.flattenToBuffer(buf2).flip();
    c.writeStream().enqueue(buf2);
    /*
         * If the caller wants to be notified of final results for the snapshot
         * request, set up a watcher only if the snapshot is queued.
         */
    if (notifyChanges && (response.getStatus() == ClientResponse.SUCCESS) && SnapshotUtil.isSnapshotQueued(response.getResults())) {
        Watcher watcher = new Watcher() {

            @Override
            public void process(final WatchedEvent event) {
                if (event.getState() == KeeperState.Disconnected)
                    return;
                switch(event.getType()) {
                    case NodeCreated:
                        m_es.submit(new Runnable() {

                            @Override
                            public void run() {
                                try {
                                    processUserSnapshotRequestResponse(event, clientHandle, c, false);
                                } catch (Exception e) {
                                    VoltDB.crashLocalVoltDB("Error retrieving user snapshot request response from ZK", true, e);
                                }
                            }
                        });
                        break;
                    default:
                }
            }
        };
        // Set the watcher
        if (m_zk.exists(event.getPath(), watcher) != null) {
            processUserSnapshotRequestResponse(event, clientHandle, c, false);
        }
    }
}
Also used : WatchedEvent(org.apache.zookeeper_voltpatches.WatchedEvent) Watcher(org.apache.zookeeper_voltpatches.Watcher) ByteBuffer(java.nio.ByteBuffer) JSONException(org.json_voltpatches.JSONException) NodeExistsException(org.apache.zookeeper_voltpatches.KeeperException.NodeExistsException) KeeperException(org.apache.zookeeper_voltpatches.KeeperException) ExecutionException(java.util.concurrent.ExecutionException)

Example 5 with Watcher

use of org.apache.zookeeper_voltpatches.Watcher in project voltdb by VoltDB.

the class WatchManager method dumpWatches.

/**
     * String representation of watches. Warning, may be large!
     * @param byPath iff true output watches by paths, otw output
     * watches by connection
     * @return string representation of watches
     */
public synchronized void dumpWatches(PrintWriter pwriter, boolean byPath) {
    if (byPath) {
        for (Entry<String, HashSet<Watcher>> e : watchTable.entrySet()) {
            pwriter.println(e.getKey());
            for (Watcher w : e.getValue()) {
                pwriter.print("\t0x");
                pwriter.print(Long.toHexString(((ServerCnxn) w).getSessionId()));
                pwriter.print("\n");
            }
        }
    } else {
        for (Entry<Watcher, HashSet<String>> e : watch2Paths.entrySet()) {
            pwriter.print("0x");
            pwriter.println(Long.toHexString(((ServerCnxn) e.getKey()).getSessionId()));
            for (String path : e.getValue()) {
                pwriter.print("\t");
                pwriter.println(path);
            }
        }
    }
}
Also used : ServerCnxn(org.apache.zookeeper_voltpatches.server.ServerCnxn) Watcher(org.apache.zookeeper_voltpatches.Watcher) HashSet(java.util.HashSet)

Aggregations

Watcher (org.apache.zookeeper_voltpatches.Watcher)12 WatchedEvent (org.apache.zookeeper_voltpatches.WatchedEvent)10 Semaphore (java.util.concurrent.Semaphore)4 ZooKeeper (org.apache.zookeeper_voltpatches.ZooKeeper)4 ExecutionException (java.util.concurrent.ExecutionException)3 KeeperException (org.apache.zookeeper_voltpatches.KeeperException)3 NoNodeException (org.apache.zookeeper_voltpatches.KeeperException.NoNodeException)3 NodeExistsException (org.apache.zookeeper_voltpatches.KeeperException.NodeExistsException)3 Stat (org.apache.zookeeper_voltpatches.data.Stat)3 JSONException (org.json_voltpatches.JSONException)3 Test (org.junit.Test)2 ByteBuffer (java.nio.ByteBuffer)1 HashSet (java.util.HashSet)1 TreeSet (java.util.TreeSet)1 BadVersionException (org.apache.zookeeper_voltpatches.KeeperException.BadVersionException)1 DataNode (org.apache.zookeeper_voltpatches.server.DataNode)1 ServerCnxn (org.apache.zookeeper_voltpatches.server.ServerCnxn)1