Search in sources :

Example 81 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.

the class WatchManager method triggerWatch.

@Override
public WatcherOrBitSet triggerWatch(String path, EventType type, WatcherOrBitSet supress) {
    WatchedEvent e = new WatchedEvent(type, KeeperState.SyncConnected, path);
    Set<Watcher> watchers = new HashSet<>();
    PathParentIterator pathParentIterator = getPathParentIterator(path);
    synchronized (this) {
        for (String localPath : pathParentIterator.asIterable()) {
            Set<Watcher> thisWatchers = watchTable.get(localPath);
            if (thisWatchers == null || thisWatchers.isEmpty()) {
                continue;
            }
            Iterator<Watcher> iterator = thisWatchers.iterator();
            while (iterator.hasNext()) {
                Watcher watcher = iterator.next();
                WatcherMode watcherMode = watcherModeManager.getWatcherMode(watcher, localPath);
                if (watcherMode.isRecursive()) {
                    if (type != EventType.NodeChildrenChanged) {
                        watchers.add(watcher);
                    }
                } else if (!pathParentIterator.atParentPath()) {
                    watchers.add(watcher);
                    if (!watcherMode.isPersistent()) {
                        iterator.remove();
                        Set<String> paths = watch2Paths.get(watcher);
                        if (paths != null) {
                            paths.remove(localPath);
                        }
                    }
                }
            }
            if (thisWatchers.isEmpty()) {
                watchTable.remove(localPath);
            }
        }
    }
    if (watchers.isEmpty()) {
        if (LOG.isTraceEnabled()) {
            ZooTrace.logTraceMessage(LOG, ZooTrace.EVENT_DELIVERY_TRACE_MASK, "No watchers for " + path);
        }
        return null;
    }
    for (Watcher w : watchers) {
        if (supress != null && supress.contains(w)) {
            continue;
        }
        w.process(e);
    }
    switch(type) {
        case NodeCreated:
            ServerMetrics.getMetrics().NODE_CREATED_WATCHER.add(watchers.size());
            break;
        case NodeDeleted:
            ServerMetrics.getMetrics().NODE_DELETED_WATCHER.add(watchers.size());
            break;
        case NodeDataChanged:
            ServerMetrics.getMetrics().NODE_CHANGED_WATCHER.add(watchers.size());
            break;
        case NodeChildrenChanged:
            ServerMetrics.getMetrics().NODE_CHILDREN_WATCHER.add(watchers.size());
            break;
        default:
            // Other types not logged.
            break;
    }
    return new WatcherOrBitSet(watchers);
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Set(java.util.Set) HashSet(java.util.HashSet) Watcher(org.apache.zookeeper.Watcher) HashSet(java.util.HashSet)

Example 82 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.

the class WatchManagerOptimized method triggerWatch.

@Override
public WatcherOrBitSet triggerWatch(String path, EventType type, WatcherOrBitSet suppress) {
    WatchedEvent e = new WatchedEvent(type, KeeperState.SyncConnected, path);
    BitHashSet watchers = remove(path);
    if (watchers == null) {
        return null;
    }
    int triggeredWatches = 0;
    // WatcherCleaner and iterating here
    synchronized (watchers) {
        for (Integer wBit : watchers) {
            if (suppress != null && suppress.contains(wBit)) {
                continue;
            }
            Watcher w = watcherBitIdMap.get(wBit);
            // skip dead watcher
            if (w == null || isDeadWatcher(w)) {
                continue;
            }
            w.process(e);
            triggeredWatches++;
        }
    }
    updateMetrics(type, triggeredWatches);
    return new WatcherOrBitSet(watchers);
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) BitHashSet(org.apache.zookeeper.server.util.BitHashSet) Watcher(org.apache.zookeeper.Watcher)

Example 83 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.

the class DisconnectedWatcherTest method testDefaultWatcherAutoResetWithChroot.

@Test
public void testDefaultWatcherAutoResetWithChroot() throws Exception {
    zk1.create("/ch1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    zk2 = createClient(watcher2, hostPort + "/ch1");
    zk2.getChildren("/", true);
    // this call shouldn't trigger any error or watch
    zk1.create("/youdontmatter1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    // this should trigger the watch
    zk1.create("/ch1/youshouldmatter1", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    WatchedEvent e = watcher2.events.poll(TIMEOUT, TimeUnit.MILLISECONDS);
    assertNotNull(e);
    assertEquals(EventType.NodeChildrenChanged, e.getType());
    assertEquals("/", e.getPath());
    zk2.getChildren("/", true);
    stopServer();
    watcher2.waitForDisconnected(3000);
    startServer();
    watcher2.waitForConnected(3000);
    watcher1.waitForConnected(3000);
    // this should trigger the watch
    zk1.create("/ch1/youshouldmatter2", null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    e = watcher2.events.poll(TIMEOUT, TimeUnit.MILLISECONDS);
    assertNotNull(e);
    assertEquals(EventType.NodeChildrenChanged, e.getType());
    assertEquals("/", e.getPath());
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) Test(org.junit.jupiter.api.Test)

Example 84 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.

the class BaseSysTest method setUp.

@Before
public void setUp() throws Exception {
    if (!fakeMachines) {
        zk = new ZooKeeper(zkHostPort, 15000, new Watcher() {

            public void process(WatchedEvent e) {
            }
        });
        im = new InstanceManager(zk, prefix);
    }
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ZooKeeper(org.apache.zookeeper.ZooKeeper) Watcher(org.apache.zookeeper.Watcher) Before(org.junit.Before)

Example 85 with WatchedEvent

use of org.apache.zookeeper.WatchedEvent in project zookeeper by apache.

the class ClientTest method testNonExistingOpCode.

/**
 * We create a perfectly valid 'exists' request, except that the opcode is wrong.
 * @throws Exception
 */
@Test
public void testNonExistingOpCode() throws Exception {
    final CountDownLatch clientDisconnected = new CountDownLatch(1);
    Watcher watcher = new Watcher() {

        @Override
        public synchronized void process(WatchedEvent event) {
            if (event.getState() == KeeperState.Disconnected) {
                clientDisconnected.countDown();
            }
        }
    };
    TestableZooKeeper zk = new TestableZooKeeper(hostPort, CONNECTION_TIMEOUT, watcher);
    final String path = "/m1";
    RequestHeader h = new RequestHeader();
    // This code does not exists
    h.setType(888);
    ExistsRequest request = new ExistsRequest();
    request.setPath(path);
    request.setWatch(false);
    ExistsResponse response = new ExistsResponse();
    ReplyHeader r = zk.submitRequest(h, request, response, null);
    assertEquals(r.getErr(), Code.UNIMPLEMENTED.intValue());
    // Sending a nonexisting opcode should cause the server to disconnect
    assertTrue(clientDisconnected.await(5000, TimeUnit.MILLISECONDS), "failed to disconnect");
    zk.close();
}
Also used : WatchedEvent(org.apache.zookeeper.WatchedEvent) ExistsRequest(org.apache.zookeeper.proto.ExistsRequest) ReplyHeader(org.apache.zookeeper.proto.ReplyHeader) TestableZooKeeper(org.apache.zookeeper.TestableZooKeeper) Watcher(org.apache.zookeeper.Watcher) RequestHeader(org.apache.zookeeper.proto.RequestHeader) CountDownLatch(java.util.concurrent.CountDownLatch) ExistsResponse(org.apache.zookeeper.proto.ExistsResponse) Test(org.junit.jupiter.api.Test)

Aggregations

WatchedEvent (org.apache.zookeeper.WatchedEvent)194 Watcher (org.apache.zookeeper.Watcher)123 ZooKeeper (org.apache.zookeeper.ZooKeeper)67 CountDownLatch (java.util.concurrent.CountDownLatch)64 KeeperException (org.apache.zookeeper.KeeperException)56 Test (org.junit.Test)38 Stat (org.apache.zookeeper.data.Stat)35 IOException (java.io.IOException)31 Test (org.testng.annotations.Test)27 Test (org.junit.jupiter.api.Test)18 CuratorFramework (org.apache.curator.framework.CuratorFramework)14 AsyncCallback (org.apache.zookeeper.AsyncCallback)14 List (java.util.List)10 KeeperState (org.apache.zookeeper.Watcher.Event.KeeperState)10 Set (java.util.Set)7 TimeoutException (java.util.concurrent.TimeoutException)7 HashSet (java.util.HashSet)6 CompletableFuture (java.util.concurrent.CompletableFuture)6 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)6 RetryOneTime (org.apache.curator.retry.RetryOneTime)6