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