Search in sources :

Example 6 with Watcher

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

the class SnapshotCompletionMonitor method processSnapshotDataChangedEvent.

private void processSnapshotDataChangedEvent(final WatchedEvent event) {
    try {
        byte[] data = m_zk.getData(event.getPath(), 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) Watcher(org.apache.zookeeper_voltpatches.Watcher) NoNodeException(org.apache.zookeeper_voltpatches.KeeperException.NoNodeException)

Example 7 with Watcher

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

the class SnapshotDaemon method leaderElection.

/**
     * Leader election for snapshots.
     * Leader will watch for truncation and user snapshot requests
     */
private void leaderElection() {
    loggingLog.info("Starting leader election for snapshot truncation daemon");
    try {
        while (true) {
            Stat stat = m_zk.exists(VoltZK.snapshot_truncation_master, new Watcher() {

                @Override
                public void process(WatchedEvent event) {
                    switch(event.getType()) {
                        case NodeDeleted:
                            loggingLog.info("Detected the snapshot truncation leader's ephemeral node deletion");
                            m_es.execute(new Runnable() {

                                @Override
                                public void run() {
                                    leaderElection();
                                }
                            });
                            break;
                        default:
                            break;
                    }
                }
            });
            if (stat == null) {
                try {
                    m_zk.create(VoltZK.snapshot_truncation_master, null, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
                    m_isAutoSnapshotLeader = true;
                    if (m_lastKnownSchedule != null) {
                        makeActivePrivate(m_lastKnownSchedule);
                    }
                    electedTruncationLeader();
                    return;
                } catch (NodeExistsException e) {
                }
            } else {
                loggingLog.info("Leader election concluded, a leader already exists");
                break;
            }
        }
    } catch (Exception e) {
        VoltDB.crashLocalVoltDB("Exception in snapshot daemon electing master via ZK", true, e);
    }
}
Also used : WatchedEvent(org.apache.zookeeper_voltpatches.WatchedEvent) Stat(org.apache.zookeeper_voltpatches.data.Stat) NodeExistsException(org.apache.zookeeper_voltpatches.KeeperException.NodeExistsException) Watcher(org.apache.zookeeper_voltpatches.Watcher) JSONException(org.json_voltpatches.JSONException) NodeExistsException(org.apache.zookeeper_voltpatches.KeeperException.NodeExistsException) KeeperException(org.apache.zookeeper_voltpatches.KeeperException) ExecutionException(java.util.concurrent.ExecutionException)

Example 8 with Watcher

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

the class SnapshotDaemon method registerUserSnapshotResponseWatch.

private void registerUserSnapshotResponseWatch(final String requestId, final long clientHandle, final Connection c, final boolean notifyChanges) throws Exception {
    final String responseNode = VoltZK.user_snapshot_response + requestId;
    Stat exists = m_zk.exists(responseNode, 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, notifyChanges);
                            } catch (Exception e) {
                                VoltDB.crashLocalVoltDB("Error retrieving user snapshot request response from ZK", true, e);
                            }
                        }
                    });
                    break;
                default:
            }
        }
    });
    if (exists != null) {
        processUserSnapshotRequestResponse(new WatchedEvent(EventType.NodeCreated, KeeperState.SyncConnected, responseNode), clientHandle, c, notifyChanges);
    }
}
Also used : WatchedEvent(org.apache.zookeeper_voltpatches.WatchedEvent) Stat(org.apache.zookeeper_voltpatches.data.Stat) Watcher(org.apache.zookeeper_voltpatches.Watcher) JSONException(org.json_voltpatches.JSONException) NodeExistsException(org.apache.zookeeper_voltpatches.KeeperException.NodeExistsException) KeeperException(org.apache.zookeeper_voltpatches.KeeperException) ExecutionException(java.util.concurrent.ExecutionException)

Example 9 with Watcher

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

the class TestZK method testChildWatches.

@Test
public void testChildWatches() throws Exception {
    ZooKeeper zk = getClient(0);
    ZooKeeper zk2 = getClient(1);
    final Semaphore sem = new Semaphore(0);
    zk.exists("/foo", new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.NodeCreated) {
                sem.release();
                System.out.println(event);
            }
        }
    });
    zk2.create("/foo", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    sem.tryAcquire(5, TimeUnit.SECONDS);
    zk.create("/foo2", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk2.exists("/foo2", new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.NodeDeleted) {
                sem.release();
                System.out.println(event);
            }
        }
    });
    zk.delete("/foo2", -1);
    sem.acquire();
}
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) Test(org.junit.Test)

Example 10 with Watcher

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

the class ZKTestBase method getClient.

protected ZooKeeper getClient(int site) throws Exception {
    final Semaphore permit = new Semaphore(0);
    int clientPort = m_siteIdToZKPort.get(site);
    ZooKeeper keeper = new ZooKeeper("127.0.0.1:" + Integer.toString(clientPort), 4000, new Watcher() {

        @Override
        public void process(WatchedEvent event) {
            if (event.getState() == KeeperState.SyncConnected) {
                permit.release();
            }
            System.out.println(event);
        }
    }, Sets.<Long>newHashSet());
    m_clients.add(keeper);
    permit.acquire();
    return keeper;
}
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)

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