List of usage examples for org.apache.zookeeper WatchedEvent WatchedEvent
public WatchedEvent(EventType eventType, KeeperState keeperState, String path)
From source file:org.apache.niolex.zookeeper.watcher.CommonRecoverableWatcherTest.java
License:Apache License
@Test public void testProcessa() throws Exception { WatchedEvent event = new WatchedEvent(EventType.NodeChildrenChanged, KeeperState.AuthFailed, "/a/ab/cc"); wda.process(event);/* www . jav a 2 s . co m*/ verify(listn, never()).onDataChange(any(byte[].class)); verify(listn, never()).onChildrenChange(any(List.class)); verify(zk, never()).getData(any(String.class), any(Watcher.class), any(Stat.class)); verify(zk, never()).getChildren(any(String.class), any(Watcher.class)); }
From source file:org.apache.niolex.zookeeper.watcher.CommonRecoverableWatcherTest.java
License:Apache License
@Test public void testReconnected() throws Exception { WatchedEvent event = new WatchedEvent(EventType.NodeDataChanged, KeeperState.AuthFailed, "/a/ab/cc"); wcl.process(event);/*ww w . ja v a 2 s . com*/ verify(listn, never()).onDataChange(any(byte[].class)); verify(listn, never()).onChildrenChange(any(List.class)); verify(zk, never()).getData(any(String.class), any(Watcher.class), any(Stat.class)); verify(zk, never()).getChildren(any(String.class), any(Watcher.class)); }
From source file:org.apache.niolex.zookeeper.watcher.CommonRecoverableWatcherTest.java
License:Apache License
@Test public void testReconnecteda() throws Exception { WatchedEvent event = new WatchedEvent(EventType.NodeDataChanged, KeeperState.AuthFailed, "/a/ab/cc"); wda.process(event);//from ww w.j a v a 2s .c om verify(listn, times(1)).onDataChange(any(byte[].class)); verify(listn, never()).onChildrenChange(any(List.class)); verify(zk, times(1)).getData(any(String.class), any(Watcher.class), any(Stat.class)); verify(zk, never()).getChildren(any(String.class), any(Watcher.class)); }
From source file:org.apache.pulsar.zookeeper.ZookeeperCacheTest.java
License:Apache License
@Test void testSimpleCache() throws Exception { OrderedSafeExecutor executor = new OrderedSafeExecutor(1, "test"); ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(); ZooKeeperCache zkCacheService = new LocalZooKeeperCache(zkClient, executor, scheduledExecutor); ZooKeeperDataCache<String> zkCache = new ZooKeeperDataCache<String>(zkCacheService) { @Override/*w w w .j a va2 s. com*/ public String deserialize(String key, byte[] content) throws Exception { return new String(content); } }; String value = "test"; zkClient.create("/my_test", value.getBytes(), null, null); assertEquals(zkCache.get("/my_test").get(), value); String newValue = "test2"; zkClient.setData("/my_test", newValue.getBytes(), -1); // Wait for the watch to be triggered Thread.sleep(100); assertEquals(zkCache.get("/my_test").get(), newValue); zkCacheService.process(new WatchedEvent(Event.EventType.None, KeeperState.Expired, null)); assertEquals(zkCache.get("/my_test").get(), newValue); zkClient.failNow(Code.SESSIONEXPIRED); assertEquals(zkCache.get("/my_test").get(), newValue); try { zkCache.get("/other"); fail("shuld have thrown exception"); } catch (Exception e) { // Ok } executor.shutdown(); scheduledExecutor.shutdown(); }
From source file:org.apache.pulsar.zookeeper.ZookeeperCacheTest.java
License:Apache License
@Test void testChildrenCache() throws Exception { OrderedSafeExecutor executor = new OrderedSafeExecutor(1, "test"); ScheduledExecutorService scheduledExecutor = Executors.newSingleThreadScheduledExecutor(); zkClient.create("/test", new byte[0], null, null); ZooKeeperCache zkCacheService = new LocalZooKeeperCache(zkClient, executor, scheduledExecutor); ZooKeeperChildrenCache cache = new ZooKeeperChildrenCache(zkCacheService, "/test"); // Create callback counter AtomicInteger notificationCount = new AtomicInteger(0); ZooKeeperCacheListener<Set<String>> counter = (path, data, stat) -> { notificationCount.incrementAndGet(); };// ww w. jav a2 s . c o m // Register counter twice and unregister once, so callback should be counted correctly cache.registerListener(counter); cache.registerListener(counter); cache.unregisterListener(counter); assertEquals(notificationCount.get(), 0); assertEquals(cache.get(), Sets.newTreeSet()); zkClient.create("/test/z1", new byte[0], null, null); zkClient.create("/test/z2", new byte[0], null, null); // Wait for cache to be updated in background while (notificationCount.get() < 2) { Thread.sleep(1); } assertEquals(cache.get(), new TreeSet<String>(Lists.newArrayList("z1", "z2"))); assertEquals(cache.get("/test"), new TreeSet<String>(Lists.newArrayList("z1", "z2"))); assertEquals(notificationCount.get(), 2); zkClient.delete("/test/z2", -1); while (notificationCount.get() < 3) { Thread.sleep(1); } assertEquals(cache.get(), new TreeSet<String>(Lists.newArrayList("z1"))); assertEquals(cache.get(), new TreeSet<String>(Lists.newArrayList("z1"))); zkCacheService.process(new WatchedEvent(Event.EventType.None, KeeperState.Expired, null)); zkClient.failNow(Code.SESSIONEXPIRED); try { cache.get(); fail("shuld have thrown exception"); } catch (Exception e) { // Ok } assertEquals(notificationCount.get(), 3); executor.shutdown(); scheduledExecutor.shutdown(); }
From source file:org.apache.pulsar.zookeeper.ZookeeperCacheTest.java
License:Apache License
@Test void testGlobalZooKeeperCache() throws Exception { OrderedSafeExecutor executor = new OrderedSafeExecutor(1, "test"); ScheduledExecutorService scheduledExecutor = new ScheduledThreadPoolExecutor(1); MockZooKeeper zkc = MockZooKeeper.newInstance(); ZooKeeperClientFactory zkClientfactory = new ZooKeeperClientFactory() { @Override/*from www . j a va2 s .c o m*/ public CompletableFuture<ZooKeeper> create(String serverList, SessionType sessionType, int zkSessionTimeoutMillis) { return CompletableFuture.completedFuture(zkc); } }; GlobalZooKeeperCache zkCacheService = new GlobalZooKeeperCache(zkClientfactory, -1, "", executor, scheduledExecutor); zkCacheService.start(); zkClient = (MockZooKeeper) zkCacheService.getZooKeeper(); ZooKeeperDataCache<String> zkCache = new ZooKeeperDataCache<String>(zkCacheService) { @Override public String deserialize(String key, byte[] content) throws Exception { return new String(content); } }; // Create callback counter AtomicInteger notificationCount = new AtomicInteger(0); ZooKeeperCacheListener<String> counter = (path, data, stat) -> { notificationCount.incrementAndGet(); }; // Register counter twice and unregister once, so callback should be counted correctly zkCache.registerListener(counter); zkCache.registerListener(counter); zkCache.unregisterListener(counter); String value = "test"; zkClient.create("/my_test", value.getBytes(), null, null); assertEquals(zkCache.get("/my_test").get(), value); String newValue = "test2"; // case 1: update and create znode directly and verify that the cache is retrieving the correct data assertEquals(notificationCount.get(), 0); zkClient.setData("/my_test", newValue.getBytes(), -1); zkClient.create("/my_test2", value.getBytes(), null, null); // Wait for the watch to be triggered while (notificationCount.get() < 1) { Thread.sleep(1); } // retrieve the data from the cache and verify it is the updated/new data assertEquals(zkCache.get("/my_test").get(), newValue); assertEquals(zkCache.get("/my_test2").get(), value); // The callback method should be called just only once assertEquals(notificationCount.get(), 1); // case 2: force the ZooKeeper session to be expired and verify that the data is still accessible zkCacheService.process(new WatchedEvent(Event.EventType.None, KeeperState.Expired, null)); assertEquals(zkCache.get("/my_test").get(), newValue); assertEquals(zkCache.get("/my_test2").get(), value); // case 3: update the znode directly while the client session is marked as expired. Verify that the new updates // is not seen in the cache zkClient.create("/other", newValue.getBytes(), null, null); zkClient.failNow(Code.SESSIONEXPIRED); assertEquals(zkCache.get("/my_test").get(), newValue); assertEquals(zkCache.get("/my_test2").get(), value); try { zkCache.get("/other"); fail("shuld have thrown exception"); } catch (Exception e) { // Ok } // case 4: directly delete the znode while the session is not re-connected yet. Verify that the deletion is not // seen by the cache zkClient.failAfter(-1, Code.OK); zkClient.delete("/my_test2", -1); zkCacheService.process(new WatchedEvent(Event.EventType.None, KeeperState.SyncConnected, null)); assertEquals(zkCache.get("/other").get(), newValue); // Make sure that the value is now directly from ZK and deleted assertFalse(zkCache.get("/my_test2").isPresent()); // case 5: trigger a ZooKeeper disconnected event and make sure the cache content is not changed. zkCacheService.process(new WatchedEvent(Event.EventType.None, KeeperState.Disconnected, null)); zkClient.create("/other2", newValue.getBytes(), null, null); // case 6: trigger a ZooKeeper SyncConnected event and make sure that the cache content is invalidated s.t. we // can see the updated content now zkCacheService.process(new WatchedEvent(Event.EventType.None, KeeperState.SyncConnected, null)); // make sure that we get it assertEquals(zkCache.get("/other2").get(), newValue); zkCacheService.close(); executor.shutdown(); scheduledExecutor.shutdown(); // Update shouldn't happen after the last check assertEquals(notificationCount.get(), 1); }
From source file:org.apache.solr.cloud.ConnectionManagerTest.java
License:Apache License
public void testLikelyExpired() throws Exception { // setup a SolrZkClient to do some getBaseUrlForNodeName testing String zkDir = createTempDir("zkData").getAbsolutePath(); ZkTestServer server = new ZkTestServer(zkDir); try {/*w w w.ja va 2s. co m*/ server.run(); AbstractZkTestCase.tryCleanSolrZkNode(server.getZkHost()); AbstractZkTestCase.makeSolrZkNode(server.getZkHost()); SolrZkClient zkClient = new SolrZkClient(server.getZkAddress(), TIMEOUT); ConnectionManager cm = zkClient.getConnectionManager(); try { assertFalse(cm.isLikelyExpired()); assertTrue(cm.isConnected()); cm.process(new WatchedEvent(EventType.None, KeeperState.Disconnected, "")); // disconnect shouldn't immediately set likelyExpired assertFalse(cm.isConnected()); assertFalse(cm.isLikelyExpired()); // but it should after the timeout Thread.sleep((long) (zkClient.getZkClientTimeout() * 1.5)); assertFalse(cm.isConnected()); assertTrue(cm.isLikelyExpired()); // even if we disconnect immediately again cm.process(new WatchedEvent(EventType.None, KeeperState.Disconnected, "")); assertFalse(cm.isConnected()); assertTrue(cm.isLikelyExpired()); // reconnect -- should no longer be likely expired cm.process(new WatchedEvent(EventType.None, KeeperState.SyncConnected, "")); assertFalse(cm.isLikelyExpired()); assertTrue(cm.isConnected()); } finally { cm.close(); zkClient.close(); } } finally { server.shutdown(); } }
From source file:org.apache.solr.schema.SchemaWatcherTest.java
License:Apache License
@Test public void testProcess() throws Exception { schemaWatcher.process(new WatchedEvent(EventType.NodeDataChanged, KeeperState.SyncConnected, "/test")); verify(mockSchemaReader).updateSchema(schemaWatcher, -1); }
From source file:org.apache.solr.schema.SchemaWatcherTest.java
License:Apache License
@Test public void testDiscardReaderReference() throws Exception { schemaWatcher.discardReaderReference(); schemaWatcher.process(new WatchedEvent(EventType.NodeDataChanged, KeeperState.SyncConnected, "/test")); // after discardReaderReference, SchemaWatcher should no longer hold a ref to the reader verifyZeroInteractions(mockSchemaReader); }