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