org.apache.zookeeper.Watcher Java Examples
The following examples show how to use
org.apache.zookeeper.Watcher.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: ParameterDynamicZookeeper.java From DDMQ with Apache License 2.0 | 7 votes |
@Override public void handleStateChanged(Watcher.Event.KeeperState state) throws Exception { LOGGER.info("ParameterDynamicZookeeper - zkClient WatchedEvent:{}, isConfigCentre:{}", state, isConfigCentre); try { switch (state) { case Disconnected: useZooKeeper = false; break; case SyncConnected: useZooKeeper = true; if (!isConfigCentre) { syncFromZooKeeper(); } break; case Expired: useZooKeeper = false; break; } } catch (Exception e) { LOGGER.error("ParameterDynamicZookeeper Exception", e); } latch.countDown(); }
Example #2
Source File: Holder.java From opensharding-spi-impl with Apache License 2.0 | 6 votes |
private Watcher startWatcher() { return new Watcher() { @Override public void process(final WatchedEvent event) { processConnection(event); if (!isConnected()) { return; } processGlobalListener(event); // TODO filter event type or path if (event.getType() == Event.EventType.None) { return; } if (Event.EventType.NodeDeleted == event.getType() || checkPath(event.getPath())) { processUsualListener(event); } } }; }
Example #3
Source File: Holder.java From opensharding-spi-impl with Apache License 2.0 | 6 votes |
protected void processConnection(final WatchedEvent event) { if (Watcher.Event.EventType.None == event.getType()) { if (Watcher.Event.KeeperState.SyncConnected == event.getState()) { connectLatch.countDown(); connected = true; } else if (Watcher.Event.KeeperState.Expired == event.getState()) { connected = false; try { reset(); } catch (final IOException | InterruptedException ex) { log.error("event state Expired: {}", ex.getMessage(), ex); } } else if (Watcher.Event.KeeperState.Disconnected == event.getState()) { connected = false; } } }
Example #4
Source File: ZooKeeperClient.java From incubator-retired-blur with Apache License 2.0 | 6 votes |
@Override public List<String> getChildren(final String path, final Watcher watcher) throws KeeperException, InterruptedException { return execute(new ZKExecutor<List<String>>("getChildren") { @Override List<String> execute() throws KeeperException, InterruptedException { LOG.debug("ZK Call - getChildren [{0}] [{1}]", path, watcher); return ZooKeeperClient.super.getChildren(path, watcher); } @Override public String toString() { return "path=" + path + " watcher=" + watcher; } }); }
Example #5
Source File: ZooKeeperCache.java From pulsar with Apache License 2.0 | 6 votes |
@SuppressWarnings("deprecation") public CompletableFuture<Boolean> existsAsync(String path, Watcher watcher) { return existsCache.get(path, (p, executor) -> { ZooKeeper zk = zkSession.get(); if (zk == null) { return FutureUtil.failedFuture(new IOException("ZK session not ready")); } CompletableFuture<Boolean> future = new CompletableFuture<>(); zk.exists(path, watcher, (rc, path1, ctx, stat) -> { if (rc == Code.OK.intValue()) { future.complete(true); } else if (rc == Code.NONODE.intValue()) { future.complete(false); } else { future.completeExceptionally(KeeperException.create(rc)); } }, null); return future; }); }
Example #6
Source File: TestSimClusterStateProvider.java From lucene-solr with Apache License 2.0 | 6 votes |
@Test public void testAutoScalingConfig() throws Exception { final CountDownLatch triggered = new CountDownLatch(1); Watcher w = ev -> { if (triggered.getCount() == 0) { fail("already triggered once!"); } triggered.countDown(); }; AutoScalingConfig cfg = cloudManager.getDistribStateManager().getAutoScalingConfig(w); assertEquals(autoScalingConfig, cfg); Preference p = new Preference(Collections.singletonMap("maximize", "freedisk")); cfg = cfg.withPolicy(cfg.getPolicy().withClusterPreferences(Collections.singletonList(p))); setAutoScalingConfig(cfg); if (!triggered.await(10, TimeUnit.SECONDS)) { fail("Watch should be triggered on update!"); } AutoScalingConfig cfg1 = cloudManager.getDistribStateManager().getAutoScalingConfig(null); assertEquals(cfg, cfg1); // restore setAutoScalingConfig(autoScalingConfig); cfg1 = cloudManager.getDistribStateManager().getAutoScalingConfig(null); assertEquals(autoScalingConfig, cfg1); }
Example #7
Source File: ZkInstance.java From libevent with Apache License 2.0 | 6 votes |
private boolean listenLock(String resource) throws InterruptedException, KeeperException { s = new Semaphore(0); try { Stat stat = zk.exists(resource, (event) -> { if (event.getType().equals(Watcher.Event.EventType.NodeDeleted)) { s.release(); } }); if (null != stat) { s.acquire(); } return !expired; } catch (KeeperException e) { if (e.code().equals(KeeperException.Code.CONNECTIONLOSS)) { return this.listenLock(resource); } else { throw e; } } }
Example #8
Source File: ZooKeeperDDLWatchChecker.java From spliceengine with GNU Affero General Public License v3.0 | 6 votes |
@Override public boolean initialize(final CommunicationListener changeIdListener) throws IOException{ this.id = zkClient.registerThisServer(); if(id==null) return false; //not a server, so inform the world if(id.startsWith("/")) id = id.substring(1); //strip the leading / to make sure that we register properly changeIdWatcher=new Watcher(){ @Override public void process(WatchedEvent watchedEvent){ if(watchedEvent.getType().equals(Event.EventType.NodeChildrenChanged)){ if(LOG.isTraceEnabled()) LOG.trace("Received watch event, signalling refresh"); changeIdListener.onCommunicationEvent(watchedEvent.getPath()); } } }; return true; }
Example #9
Source File: SafeModeTest.java From incubator-retired-blur with Apache License 2.0 | 6 votes |
@Test public void testSecondNodeStartup() throws IOException, InterruptedException, KeeperException { ZooKeeper zk = new ZooKeeper(miniCluster.getZkConnectionString(), 20000, new Watcher() { @Override public void process(WatchedEvent event) { } }); SafeMode setupSafeMode = new SafeMode(zk, "/testing/safemode", "/testing/nodepath", TimeUnit.SECONDS, 5, TimeUnit.SECONDS, 60, 0); setupSafeMode.registerNode("node10", null); SafeMode safeMode = new SafeMode(zk, "/testing/safemode", "/testing/nodepath", TimeUnit.SECONDS, 5, TimeUnit.SECONDS, 15, 0); try { safeMode.registerNode("node10", null); fail("should throw exception."); } catch (Exception e) { } zk.close(); }
Example #10
Source File: ZkUtilsTest.java From incubator-retired-blur with Apache License 2.0 | 6 votes |
@Before public void setUp() throws IOException, InterruptedException { final Object lock = new Object(); synchronized (lock) { _zooKeeper = new ZooKeeper(_zkMiniCluster.getZkConnectionString(), 10000, new Watcher() { @Override public void process(WatchedEvent event) { synchronized (lock) { lock.notifyAll(); } } }); lock.wait(); } }
Example #11
Source File: BlurClient.java From incubator-retired-blur with Apache License 2.0 | 6 votes |
ZooKeeperConntrollerWatchInfo(BlurConfiguration conf) throws IOException, KeeperException, InterruptedException { _zooKeeperConnectionStr = conf.getExpected(BLUR_ZOOKEEPER_CONNECTION); _zkSessionTimeout = conf.getInt(BLUR_ZOOKEEPER_TIMEOUT, BLUR_ZOOKEEPER_TIMEOUT_DEFAULT); _zooKeeper = new ZooKeeperClient(_zooKeeperConnectionStr, _zkSessionTimeout, new Watcher() { @Override public void process(WatchedEvent event) { } }); setConnections(_zooKeeper.getChildren(ZookeeperPathConstants.getOnlineControllersPath(), false)); _watchConntrollers = new WatchChildren(_zooKeeper, ZookeeperPathConstants.getOnlineControllersPath()); _watchConntrollers.watch(new OnChange() { @Override public void action(List<String> children) { setConnections(children); } }); }
Example #12
Source File: ServiceStarter.java From pravega with Apache License 2.0 | 6 votes |
@Override public ZooKeeper newZooKeeper(String connectString, int sessionTimeout, Watcher watcher, boolean canBeReadOnly) throws Exception { Exceptions.checkNotNullOrEmpty(connectString, "connectString"); Preconditions.checkArgument(sessionTimeout > 0, "sessionTimeout should be a positive integer"); synchronized (this) { if (client == null) { this.connectString = connectString; this.sessionTimeout = sessionTimeout; this.canBeReadOnly = canBeReadOnly; } log.info("Creating new Zookeeper client with arguments: {}, {}, {}.", this.connectString, this.sessionTimeout, this.canBeReadOnly); this.client = new ZooKeeper(this.connectString, this.sessionTimeout, watcher, this.canBeReadOnly); return this.client; } }
Example #13
Source File: ZookeeperMetadataStorageManager.java From herddb with Apache License 2.0 | 5 votes |
private synchronized void restartZooKeeper() throws IOException, InterruptedException { ZooKeeper old = zooKeeper; if (old != null) { old.close(); } CountDownLatch firstConnectionLatch = new CountDownLatch(1); ZooKeeper zk = new ZooKeeper(zkAddress, zkSessionTimeout, new Watcher() { @Override public void process(WatchedEvent event) { switch (event.getState()) { case SyncConnected: case SaslAuthenticated: firstConnectionLatch.countDown(); notifyMetadataChanged("zkevent " + event.getState() + " " + event.getType() + " " + event.getPath()); break; default: // ignore break; } } }); if (!firstConnectionLatch.await(zkSessionTimeout, TimeUnit.SECONDS)) { zk.close(); throw new IOException("Could not connect to zookeeper at " + zkAddress + " within " + zkSessionTimeout + " ms"); } this.zooKeeper = zk; LOGGER.info("Connected to ZK " + zk); }
Example #14
Source File: TestZkJobCoordinator.java From samza with Apache License 2.0 | 5 votes |
@Test public void testZookeeperSessionMetricsAreUpdatedCoorrectly() { ZkKeyBuilder keyBuilder = Mockito.mock(ZkKeyBuilder.class); ZkClient mockZkClient = Mockito.mock(ZkClient.class); when(keyBuilder.getJobModelVersionBarrierPrefix()).thenReturn(TEST_BARRIER_ROOT); ZkUtils zkUtils = Mockito.mock(ZkUtils.class); when(zkUtils.getKeyBuilder()).thenReturn(keyBuilder); when(zkUtils.getZkClient()).thenReturn(mockZkClient); when(zkUtils.getJobModel(TEST_JOB_MODEL_VERSION)).thenReturn(new JobModel(new MapConfig(), new HashMap<>())); ScheduleAfterDebounceTime mockDebounceTimer = Mockito.mock(ScheduleAfterDebounceTime.class); ZkJobCoordinator zkJobCoordinator = Mockito.spy(new ZkJobCoordinator("TEST_PROCESSOR_ID", new MapConfig(), new NoOpMetricsRegistry(), zkUtils, zkMetadataStore, coordinatorStreamStore)); zkJobCoordinator.debounceTimer = mockDebounceTimer; zkJobCoordinator.zkSessionMetrics = new ZkSessionMetrics(new MetricsRegistryMap()); final ZkSessionStateChangedListener zkSessionStateChangedListener = zkJobCoordinator.new ZkSessionStateChangedListener(); zkSessionStateChangedListener.handleStateChanged(Watcher.Event.KeeperState.Disconnected); zkSessionStateChangedListener.handleStateChanged(Watcher.Event.KeeperState.SyncConnected); zkSessionStateChangedListener.handleStateChanged(Watcher.Event.KeeperState.AuthFailed); Assert.assertEquals(1, zkJobCoordinator.zkSessionMetrics.zkSessionErrors.getCount()); zkSessionStateChangedListener.handleSessionEstablishmentError(new SamzaException("Test exception")); Assert.assertEquals(1, zkJobCoordinator.zkSessionMetrics.zkSessionDisconnects.getCount()); Assert.assertEquals(1, zkJobCoordinator.zkSessionMetrics.zkSyncConnected.getCount()); Assert.assertEquals(2, zkJobCoordinator.zkSessionMetrics.zkSessionErrors.getCount()); }
Example #15
Source File: TestWatchesBuilder.java From curator with Apache License 2.0 | 5 votes |
/** * Test the case where we try and remove an unregistered watcher but have the quietly flag set. In this case we expect success. * @throws Exception */ @Test public void testRemoveUnregisteredWatcherQuietly() throws Exception { Timing timing = new Timing(); CuratorFramework client = CuratorFrameworkFactory.builder(). connectString(server.getConnectString()). retryPolicy(new RetryOneTime(1)). build(); try { client.start(); final AtomicBoolean watcherRemoved = new AtomicBoolean(false); final String path = "/"; Watcher watcher = new BooleanWatcher(path, watcherRemoved, EventType.DataWatchRemoved); client.watches().remove(watcher).quietly().forPath(path); timing.sleepABit(); //There should be no watcher removed as none were registered. Assert.assertEquals(watcherRemoved.get(), false); } finally { CloseableUtils.closeQuietly(client); } }
Example #16
Source File: ZkConfigCenterServiceImpl.java From jim-framework with Apache License 2.0 | 5 votes |
private Watcher getPathWatcher() { return new Watcher() { @Override public void process(WatchedEvent event) { if (event != null) { logger.debug(event.toString()); logger.info("thread id={} even={} path={}", new Object[]{Thread.currentThread().getId(), event.getType().name(), event.getPath()}); try { boolean isDelete = false; if (event.getState() == Event.KeeperState.SyncConnected) { String path = event.getPath(); if (path == null || path.equals("/")) return; switch (event.getType()) { case NodeDeleted: postRemovePath(event.getPath()); isDelete = true; break; case NodeDataChanged: postDataChangeEvent(event); break; default: break; } if (!isDelete) { watchPathDataChange(event.getPath()); } } } catch (Exception e) { logger.info("zk data changed error:",e); } } } }; }
Example #17
Source File: TestZooKeeperClient.java From distributedlog with Apache License 2.0 | 5 votes |
private CountDownLatch awaitConnectionEvent(final KeeperState state, final ZooKeeperClient zkc) { final CountDownLatch connected = new CountDownLatch(1); Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { if (event.getType() == EventType.None && event.getState() == state) { connected.countDown(); } } }; zkc.register(watcher); return connected; }
Example #18
Source File: BlurUtil.java From incubator-retired-blur with Apache License 2.0 | 5 votes |
public static String lockForSafeMode(ZooKeeper zookeeper, String nodeName, String cluster) throws KeeperException, InterruptedException { LOG.info("Getting safe mode lock."); final Object lock = new Object(); String blurSafemodePath = ZookeeperPathConstants.getSafemodePath(cluster); String newPath = zookeeper.create(blurSafemodePath + "/safemode-", nodeName.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { synchronized (lock) { lock.notifyAll(); } } }; while (true) { synchronized (lock) { List<String> children = new ArrayList<String>(zookeeper.getChildren(blurSafemodePath, watcher)); Collections.sort(children); if (newPath.equals(blurSafemodePath + "/" + children.get(0))) { LOG.info("Lock aquired."); return newPath; } else { lock.wait(BlurConstants.ZK_WAIT_TIME); } } } }
Example #19
Source File: TestZooKeeperClient.java From distributedlog with Apache License 2.0 | 5 votes |
@Test(timeout = 60000) public void testExceptionOnWatchers() throws Exception { TestWatcher w1 = new TestWatcher(); TestWatcher w2 = new TestWatcher(); final CountDownLatch latch = new CountDownLatch(2); w1.setLatch(latch); w2.setLatch(latch); zkc.register(w1); zkc.register(w2); // register bad watcher zkc.register(new Watcher() { @Override public void process(WatchedEvent event) { throw new NullPointerException("bad watcher returning null"); } }); assertEquals(3, zkc.watchers.size()); final String zkPath = "/test-exception-on-watchers"; zkc.get().create(zkPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); zkc.get().getData(zkPath, true, new Stat()); zkc.get().setData(zkPath, "first-set".getBytes(), -1); latch.await(); assertEquals(1, w1.receivedEvents.size()); assertEquals(zkPath, w1.receivedEvents.get(0).getPath()); assertEquals(Watcher.Event.EventType.NodeDataChanged, w1.receivedEvents.get(0).getType()); assertEquals(1, w2.receivedEvents.size()); assertEquals(zkPath, w2.receivedEvents.get(0).getPath()); assertEquals(Watcher.Event.EventType.NodeDataChanged, w2.receivedEvents.get(0).getType()); }
Example #20
Source File: ZKOperations.java From twill with Apache License 2.0 | 5 votes |
public static void watchDeleted(final ZKClient zkClient, final String path, final SettableFuture<String> completion) { Futures.addCallback(zkClient.exists(path, new Watcher() { @Override public void process(WatchedEvent event) { if (!completion.isDone()) { if (event.getType() == Event.EventType.NodeDeleted) { completion.set(path); } else { watchDeleted(zkClient, path, completion); } } } }), new FutureCallback<Stat>() { @Override public void onSuccess(Stat result) { if (result == null) { completion.set(path); } } @Override public void onFailure(Throwable t) { completion.setException(t); } }); }
Example #21
Source File: ZKConnection.java From tutorials with MIT License | 5 votes |
public ZooKeeper connect(String host) throws IOException, InterruptedException { zoo = new ZooKeeper(host, 2000, new Watcher() { public void process(WatchedEvent we) { if (we.getState() == KeeperState.SyncConnected) { connectionLatch.countDown(); } } }); connectionLatch.await(); return zoo; }
Example #22
Source File: ZookeeperTemplate.java From easyooo-framework with Apache License 2.0 | 5 votes |
/** * Get children and set the given watcher on the node. * @param path * @param watcher * @return * @throws ZookeeperExpcetion */ public List<String> watchedGetChildren(final String path, final Watcher watcher)throws ZookeeperExpcetion{ return doTemplate(new ZkCallback<List<String>>() { @Override public List<String> callback() throws Exception { return zkClient.getChildren().usingWatcher(watcher).forPath(path); } }); }
Example #23
Source File: Zk.java From rdf-delta with Apache License 2.0 | 5 votes |
public static JsonObject zkFetchJson(CuratorFramework client, Watcher watcher, String path) { byte[] x = zkFetch(client, watcher, path); if ( x == null ) return null; if ( x.length == 0 ) return null; return JSONX.fromBytes(x); }
Example #24
Source File: HandleHolder.java From xian with Apache License 2.0 | 5 votes |
HandleHolder(ZookeeperFactory zookeeperFactory, Watcher watcher, EnsembleProvider ensembleProvider, int sessionTimeout, boolean canBeReadOnly) { this.zookeeperFactory = zookeeperFactory; this.watcher = watcher; this.ensembleProvider = ensembleProvider; this.sessionTimeout = sessionTimeout; this.canBeReadOnly = canBeReadOnly; }
Example #25
Source File: ConnectionState.java From xian with Apache License 2.0 | 5 votes |
@Override public void process(WatchedEvent event) { if ( LOG_EVENTS ) { log.debug("ConnectState watcher: " + event); } final boolean eventTypeNone = event.getType() == Watcher.Event.EventType.None; if ( eventTypeNone ) { boolean wasConnected = isConnected.get(); boolean newIsConnected = checkState(event.getState(), wasConnected); if ( newIsConnected != wasConnected ) { isConnected.set(newIsConnected); connectionStartMs = System.currentTimeMillis(); } } // only wait during tests if (debugWaitOnExpiredEvent && event.getState() == Event.KeeperState.Expired) { waitOnExpiredEvent(); } for ( Watcher parentWatcher : parentWatchers ) { OperationTrace trace = new OperationTrace("connection-state-parent-process", tracer.get(), getSessionId()); parentWatcher.process(event); trace.commit(); } if (eventTypeNone) handleState(event.getState()); }
Example #26
Source File: ZkClientTest.java From zkclient with Apache License 2.0 | 5 votes |
@Test public void listener() throws ZkClientException { zk.listenData("/zk/test/1", new Listener() { public void listen(String path, Watcher.Event.EventType eventType, byte[] data) throws ZkClientException, SocketException { System.out.println(path + " " + new String(data) + " " + eventType.name()); } }); }
Example #27
Source File: AsyncRemoveWatchesBuilderImpl.java From curator with Apache License 2.0 | 5 votes |
@Override public AsyncPathable<AsyncStage<Void>> removing(Watcher watcher, Watcher.WatcherType watcherType) { this.watcher = Objects.requireNonNull(watcher, "watcher cannot be null"); this.watcherType = Objects.requireNonNull(watcherType, "watcherType cannot be null"); this.curatorWatcher = null; return this; }
Example #28
Source File: ZkUtils.java From DBus with Apache License 2.0 | 5 votes |
public void usingWatcher(String path, Watcher watcher) { CuratorFramework curator = CuratorContainer.getInstance().getCurator(); try { curator.getData().usingWatcher(watcher).forPath(ZKPaths.makePath(path, null)); } catch (Exception e) { e.printStackTrace(); } }
Example #29
Source File: WorkerCoordinator.java From DBus with Apache License 2.0 | 5 votes |
public void registerWorkerChangeListener() { Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { if (event.getType() == Event.EventType.NodeChildrenChanged) { Map<Worker, TopicAssignor.Assignment> assignment = topicAssignor.assign(zkUtils.getCluster(), null); // 将leader分配的结果写回zk } } }; zkUtils.usingWatcher(zkUtils.workersIdsPath, watcher); }
Example #30
Source File: ZkClientTest.java From zkclient with Apache License 2.0 | 5 votes |
@Test public void listenState(){ zk.listenState(Watcher.Event.KeeperState.Disconnected, new StateListener() { @Override public void listen(Watcher.Event.KeeperState state) { System.out.println("--------------Disconnected-----------"); } }); }