List of usage examples for org.apache.zookeeper WatchedEvent getState
public KeeperState getState()
From source file:org.apache.storm.zookeeper.Zookeeper.java
License:Apache License
public CuratorFramework mkClientImpl(Map conf, List<String> servers, Object port, String root, final WatcherCallBack watcher, Map authConf) { CuratorFramework fk;//from ww w . jav a2 s . co m if (authConf != null) { fk = Utils.newCurator(conf, servers, port, root, new ZookeeperAuthInfo(authConf)); } else { fk = Utils.newCurator(conf, servers, port, root); } fk.getCuratorListenable().addListener(new CuratorListener() { @Override public void eventReceived(CuratorFramework _fk, CuratorEvent e) throws Exception { if (e.getType().equals(CuratorEventType.WATCHED)) { WatchedEvent event = e.getWatchedEvent(); watcher.execute(event.getState(), event.getType(), event.getPath()); } } }); fk.start(); return fk; }
From source file:org.apache.tephra.distributed.ThriftTransactionServerTest.java
License:Apache License
private void expireZkSession(ZKClientService zkClientService) throws Exception { ZooKeeper zooKeeper = zkClientService.getZooKeeperSupplier().get(); final SettableFuture<?> connectFuture = SettableFuture.create(); Watcher watcher = new Watcher() { @Override//from w w w . j a v a 2 s .c o m public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.SyncConnected) { connectFuture.set(null); } } }; // Create another Zookeeper session with the same sessionId so that the original one expires. ZooKeeper dupZookeeper = new ZooKeeper(zkClientService.getConnectString(), zooKeeper.getSessionTimeout(), watcher, zooKeeper.getSessionId(), zooKeeper.getSessionPasswd()); connectFuture.get(30, TimeUnit.SECONDS); Assert.assertEquals("Failed to re-create current session", dupZookeeper.getState(), ZooKeeper.States.CONNECTED); dupZookeeper.close(); }
From source file:org.apache.twill.discovery.ZKDiscoveryService.java
License:Apache License
private Watcher createConnectionWatcher() { return new Watcher() { // Watcher is invoked from single event thread, hence safe to use normal mutable variable. private boolean expired; @Override//w ww . j a v a 2 s . c o m public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.Expired) { LOG.warn("ZK Session expired: {}", zkClient.getConnectString()); expired = true; } else if (event.getState() == Event.KeeperState.SyncConnected && expired) { LOG.info("Reconnected after expiration: {}", zkClient.getConnectString()); expired = false; // Re-register all services lock.lock(); try { for (final Map.Entry<Discoverable, DiscoveryCancellable> entry : discoverables.entries()) { LOG.info("Re-registering service: {}", entry.getKey()); // Must be non-blocking in here. Futures.addCallback(doRegister(entry.getKey()), new FutureCallback<String>() { @Override public void onSuccess(String result) { // Updates the cancellable to the newly created sequential node. entry.getValue().setPath(result); LOG.debug("Service re-registered: {} {}", entry.getKey(), result); } @Override public void onFailure(Throwable t) { // When failed to create the node, there would be no retry and simply make the cancellable do nothing. entry.getValue().setPath(null); LOG.error("Failed to re-register service: {}", entry.getKey(), t); } }, Threads.SAME_THREAD_EXECUTOR); } } finally { lock.unlock(); } } } }; }
From source file:org.apache.twill.internal.AbstractTwillService.java
License:Apache License
@Override protected final void startUp() throws Exception { // Single thread executor that will discard task silently if it is already terminated, which only // happens when this service is shutting down. messageCallbackExecutor = new ThreadPoolExecutor(1, 1, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>(), Threads.createDaemonThreadFactory("message-callback"), new ThreadPoolExecutor.DiscardPolicy()); // Watch for session expiration, recreate the live node if reconnected after expiration. zkClient.addConnectionWatcher(new Watcher() { private boolean expired = false; @Override/* www . j ava2 s .c om*/ public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.Expired) { LOG.warn("ZK Session expired for service {} with runId {}.", getServiceName(), runId.getId()); expired = true; } else if (event.getState() == Event.KeeperState.SyncConnected && expired) { LOG.info("Reconnected after expiration for service {} with runId {}", getServiceName(), runId.getId()); expired = false; logIfFailed(createLiveNode()); } } }); // Create the live node, if succeeded, start the service, otherwise fail out. createLiveNode().get(); // Create node for messaging ZKOperations.ignoreError(zkClient.create(getZKPath("messages"), null, CreateMode.PERSISTENT), KeeperException.NodeExistsException.class, null).get(); doStart(); // Starts watching for messages watchMessages(); }
From source file:org.apache.twill.internal.ZKServiceDecorator.java
License:Apache License
@Override protected void doStart() { callbackExecutor = Executors.newSingleThreadExecutor(Threads.createDaemonThreadFactory("message-callback")); // Create the live node, if succeeded, start the decorated service, otherwise fail out. Futures.addCallback(createLiveNode(), new FutureCallback<String>() { @Override/*from w w w .j a v a2s .c o m*/ public void onSuccess(String result) { // Create nodes for states and messaging StateNode stateNode = new StateNode(ServiceController.State.STARTING); final ListenableFuture<List<String>> createFuture = Futures.allAsList( ZKOperations.ignoreError( zkClient.create(getZKPath("messages"), null, CreateMode.PERSISTENT), KeeperException.NodeExistsException.class, null), zkClient.create(getZKPath("state"), encodeStateNode(stateNode), CreateMode.PERSISTENT)); createFuture.addListener(new Runnable() { @Override public void run() { try { createFuture.get(); // Starts the decorated service decoratedService.addListener(createListener(), Threads.SAME_THREAD_EXECUTOR); decoratedService.start(); } catch (Exception e) { notifyFailed(e); } } }, Threads.SAME_THREAD_EXECUTOR); } @Override public void onFailure(Throwable t) { notifyFailed(t); } }); // Watch for session expiration, recreate the live node if reconnected after expiration. zkClient.addConnectionWatcher(new Watcher() { private boolean expired = false; @Override public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.Expired) { LOG.warn("ZK Session expired for service {} with runId {}.", decoratedService, id.getId()); expired = true; } else if (event.getState() == Event.KeeperState.SyncConnected && expired) { LOG.info("Reconnected after expiration for service {} with runId {}", decoratedService, id.getId()); expired = false; Futures.addCallback(createLiveNode(), new FutureCallback<String>() { @Override public void onSuccess(String result) { // All good, no-op } @Override public void onFailure(Throwable t) { notifyFailed(t); } }, Threads.SAME_THREAD_EXECUTOR); } } }); }
From source file:org.apache.twill.internal.zookeeper.ReentrantDistributedLock.java
License:Apache License
/** * Acquires a distributed lock through ZooKeeper. * * @param interruptible true if acquisition of lock can be interrupted * @param waitForLock true if wants to wait for the lock when not able to acquire it * @param timeout time to wait for the lock before giving up * @param unit unit for the timeout/*from w w w.ja v a 2s .c om*/ * @throws InterruptedException if {@code interruptible} is set to {@code true} and the current thread is interrupted * while acquiring the lock * @throws ExecutionException if there is failure while trying to acquire the lock */ private boolean acquire(boolean interruptible, final boolean waitForLock, long timeout, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { Preconditions.checkState(lock.isHeldByCurrentThread(), "Not owner of local lock."); if (lock.getHoldCount() > 1) { // Already owner of the lock, simply return. return true; } // Use a Future to help deal with different variants of locking // (lock, lockInterruptibly, tryLock, tryLock with timeout) // When the completion future is completed successfully, it means the lock is acquired and the future contains // the ZK node path to the ephemeral node that is representing this lock. // If it is failed, it means there is exception while trying to acquire the lock // If it is cancelled, it means to abort the acquisition logic (due to timeout / interrupt). final SettableFuture<String> completion = SettableFuture.create(); // If the connection expired, fail the locking process if it is still in progress final Cancellable watcherCancellable = zkClient.addConnectionWatcher(new Watcher() { @Override public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.Expired) { completion.setException(new IllegalStateException("ZK session expired")); } } }); // Always remove the watcher on completion completion.addListener(new Runnable() { @Override public void run() { watcherCancellable.cancel(); } }, Threads.SAME_THREAD_EXECUTOR); // Step 1. Create a ephemeral sequential node final String guid = UUID.randomUUID().toString(); final String lockPath = String.format("%s/%s-", path, guid); OperationFuture<String> future = zkClient.create(lockPath, null, CreateMode.EPHEMERAL_SEQUENTIAL, true); Futures.addCallback(future, new FutureCallback<String>() { @Override public void onSuccess(final String lockNode) { // If lock failed due to whatever reason, delete the lock node. deleteNodeOnFailure(completion, lockNode); // If the lock is completed (mainly due to cancellation), simply abort the lock acquisition logic. if (completion.isDone()) { return; } // Step 2-5. Try to determine who is the lock owner and watch for ZK node changes if itself is not the owner. doAcquire(completion, waitForLock, guid, lockNode); } @Override public void onFailure(Throwable t) { if (t instanceof KeeperException.ConnectionLossException) { // Ignore connection exception in create. Going to handle it in next step. // See the ZK receipt for details about the possible failure situation that can cause this. doAcquire(completion, waitForLock, guid, null); } else { LOG.error("Exception raised when creating lock node at {}", lockPath, t); completion.setException(t); } } }); // Gets the result from the completion try { if (interruptible) { localLockNode.set(completion.get(timeout, unit)); } else { localLockNode.set(Uninterruptibles.getUninterruptibly(completion, timeout, unit)); } return true; } catch (InterruptedException e) { completion.cancel(true); throw e; } catch (TimeoutException e) { completion.cancel(true); throw e; } catch (CancellationException e) { // If the completion get cancelled, meaning the lock acquisition is aborted. return false; } }
From source file:org.apache.twill.zookeeper.ZKClientTest.java
License:Apache License
@Test public void testExpireRewatch() throws InterruptedException, IOException, ExecutionException { InMemoryZKServer zkServer = InMemoryZKServer.builder().setTickTime(1000).build(); zkServer.startAndWait();// ww w .j av a 2 s .c om try { final CountDownLatch expireReconnectLatch = new CountDownLatch(1); final AtomicBoolean expired = new AtomicBoolean(false); final ZKClientService client = ZKClientServices .delegate(ZKClients.reWatchOnExpire(ZKClientService.Builder.of(zkServer.getConnectionStr()) .setSessionTimeout(2000).setConnectionWatcher(new Watcher() { @Override public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.Expired) { expired.set(true); } else if (event.getState() == Event.KeeperState.SyncConnected && expired.compareAndSet(true, true)) { expireReconnectLatch.countDown(); } } }).build())); client.startAndWait(); try { final BlockingQueue<Watcher.Event.EventType> events = new LinkedBlockingQueue<>(); client.exists("/expireRewatch", new Watcher() { @Override public void process(final WatchedEvent event) { Futures.addCallback(client.exists("/expireRewatch", this), new FutureCallback<Stat>() { @Override public void onSuccess(Stat result) { events.add(event.getType()); } @Override public void onFailure(Throwable t) { LOG.error("Failed to call exists on /expireRewatch", t); } }); } }); client.create("/expireRewatch", null, CreateMode.PERSISTENT); Assert.assertEquals(Watcher.Event.EventType.NodeCreated, events.poll(60, TimeUnit.SECONDS)); KillZKSession.kill(client.getZooKeeperSupplier().get(), zkServer.getConnectionStr(), 10000); Assert.assertTrue(expireReconnectLatch.await(60, TimeUnit.SECONDS)); // Keep trying to delete the node until it succeed while (ZKOperations.ignoreError(client.delete("/expireRewatch"), KeeperException.class, null) .get() == null) { LOG.info("Delete failed. Retrying to delete /expireRewatch"); TimeUnit.MILLISECONDS.sleep(10); } Assert.assertEquals(Watcher.Event.EventType.NodeDeleted, events.poll(60, TimeUnit.SECONDS)); } finally { client.stopAndWait(); } } finally { zkServer.stopAndWait(); } }
From source file:org.apache.twill.zookeeper.ZKClientTest.java
License:Apache License
@Test public void testRetry() throws ExecutionException, InterruptedException, TimeoutException, IOException { File dataDir = tmpFolder.newFolder(); InMemoryZKServer zkServer = InMemoryZKServer.builder().setDataDir(dataDir).setTickTime(1000).build(); zkServer.startAndWait();/*from w ww . ja v a 2s.c o m*/ int port = zkServer.getLocalAddress().getPort(); final CountDownLatch disconnectLatch = new CountDownLatch(1); ZKClientService client = ZKClientServices.delegate(ZKClients.retryOnFailure( ZKClientService.Builder.of(zkServer.getConnectionStr()).setConnectionWatcher(new Watcher() { @Override public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.Disconnected) { disconnectLatch.countDown(); } } }).build(), RetryStrategies.fixDelay(0, TimeUnit.SECONDS))); final CountDownLatch createLatch = new CountDownLatch(1); client.startAndWait(); try { zkServer.stopAndWait(); Assert.assertTrue(disconnectLatch.await(1, TimeUnit.SECONDS)); Futures.addCallback(client.create("/testretry/test", null, CreateMode.PERSISTENT), new FutureCallback<String>() { @Override public void onSuccess(String result) { createLatch.countDown(); } @Override public void onFailure(Throwable t) { t.printStackTrace(System.out); } }); TimeUnit.SECONDS.sleep(2); zkServer = InMemoryZKServer.builder().setDataDir(dataDir).setAutoCleanDataDir(true).setPort(port) .setTickTime(1000).build(); zkServer.startAndWait(); try { Assert.assertTrue(createLatch.await(10, TimeUnit.SECONDS)); } finally { zkServer.stopAndWait(); } } finally { client.stopAndWait(); } }
From source file:org.apache.usergrid.locking.zookeeper.ZooPut.java
License:Apache License
@Override public void process(WatchedEvent event) { // nocommit: consider how we want to accomplish this if (event.getState() == KeeperState.SyncConnected) { synchronized (this) { connected = true;/* w w w . java 2s .c o m*/ this.notify(); } } }
From source file:org.apache.whirr.service.cdh.integration.Cdh3ZooKeeperServiceTest.java
License:Apache License
@Test public void test() throws Exception { class ConnectionWatcher implements Watcher { private ZooKeeper zk; private CountDownLatch latch = new CountDownLatch(1); public void connect(String hosts) throws IOException, InterruptedException { zk = new ZooKeeper(hosts, 5000, this); latch.await();/*from w w w .j a v a 2s. c om*/ } public ZooKeeper getZooKeeper() { return zk; } @Override public void process(WatchedEvent event) { if (event.getState() == KeeperState.SyncConnected) { latch.countDown(); } } public void close() throws InterruptedException { if (zk != null) { zk.close(); } } } String path = "/data"; String data = "Hello"; ConnectionWatcher watcher = new ConnectionWatcher(); watcher.connect(hosts); watcher.getZooKeeper().create(path, data.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); watcher.close(); watcher = new ConnectionWatcher(); watcher.connect(hosts); byte[] actualData = watcher.getZooKeeper().getData(path, false, null); assertEquals(data, new String(actualData)); watcher.close(); }