Search in sources :

Example 1 with WatchedEvent

use of org.apache.zookeeper_voltpatches.WatchedEvent 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 2 with WatchedEvent

use of org.apache.zookeeper_voltpatches.WatchedEvent 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 3 with WatchedEvent

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

the class SnapshotDaemon method truncationRequestExistenceCheck.

/*
     * Set the watch in ZK on the node that represents an internal request
     * for a truncation snapshot
     */
void truncationRequestExistenceCheck() throws KeeperException, InterruptedException {
    loggingLog.info("Checking for existence of snapshot truncation request");
    m_currentTruncationWatcher.cancel();
    m_currentTruncationWatcher = new TruncationRequestExistenceWatcher();
    // TRAIL [TruncSnap:2] checking for zk node existence
    List<String> requests = m_zk.getChildren(VoltZK.request_truncation_snapshot, m_currentTruncationWatcher);
    if (!requests.isEmpty()) {
        loggingLog.info("A truncation request node already existed, processing truncation request event");
        m_currentTruncationWatcher.cancel();
        // TRAIL [TruncSnap:3] fake a node created event (req ZK node already there)
        processTruncationRequestEvent(new WatchedEvent(EventType.NodeChildrenChanged, KeeperState.SyncConnected, VoltZK.request_truncation_snapshot));
    }
}
Also used : WatchedEvent(org.apache.zookeeper_voltpatches.WatchedEvent)

Example 4 with WatchedEvent

use of org.apache.zookeeper_voltpatches.WatchedEvent 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 5 with WatchedEvent

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

the class SnapshotCompletionMonitor method processSnapshotChildrenChanged.

private void processSnapshotChildrenChanged(final WatchedEvent event) {
    try {
        TreeSet<String> children = new TreeSet<String>(m_zk.getChildren(VoltZK.completed_snapshots, m_newSnapshotWatcher));
        TreeSet<String> newChildren = new TreeSet<String>(children);
        newChildren.removeAll(m_lastKnownSnapshots);
        m_lastKnownSnapshots = children;
        for (String newSnapshot : newChildren) {
            String path = VoltZK.completed_snapshots + "/" + newSnapshot;
            try {
                byte[] data = m_zk.getData(path, new Watcher() {

                    @Override
                    public void process(final WatchedEvent event) {
                        switch(event.getType()) {
                            case NodeDataChanged:
                                m_es.execute(new Runnable() {

                                    @Override
                                    public void run() {
                                        processSnapshotDataChangedEvent(event);
                                    }
                                });
                                break;
                            default:
                                break;
                        }
                    }
                }, null);
                processSnapshotData(data);
            } catch (NoNodeException e) {
            }
        }
    } catch (Exception e) {
        VoltDB.crashLocalVoltDB("Exception in snapshot completion monitor", true, e);
    }
}
Also used : WatchedEvent(org.apache.zookeeper_voltpatches.WatchedEvent) NoNodeException(org.apache.zookeeper_voltpatches.KeeperException.NoNodeException) TreeSet(java.util.TreeSet) Watcher(org.apache.zookeeper_voltpatches.Watcher) NoNodeException(org.apache.zookeeper_voltpatches.KeeperException.NoNodeException)

Aggregations

WatchedEvent (org.apache.zookeeper_voltpatches.WatchedEvent)12 Watcher (org.apache.zookeeper_voltpatches.Watcher)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 NodeExistsException (org.apache.zookeeper_voltpatches.KeeperException.NodeExistsException)3 Stat (org.apache.zookeeper_voltpatches.data.Stat)3 JSONException (org.json_voltpatches.JSONException)3 NoNodeException (org.apache.zookeeper_voltpatches.KeeperException.NoNodeException)2 Test (org.junit.Test)2 ByteBuffer (java.nio.ByteBuffer)1 TreeSet (java.util.TreeSet)1 BadVersionException (org.apache.zookeeper_voltpatches.KeeperException.BadVersionException)1 DataNode (org.apache.zookeeper_voltpatches.server.DataNode)1