Example usage for org.apache.zookeeper Watcher Watcher

List of usage examples for org.apache.zookeeper Watcher Watcher

Introduction

In this page you can find the example usage for org.apache.zookeeper Watcher Watcher.

Prototype

Watcher

Source Link

Usage

From source file:org.apache.solr.schema.ZkIndexSchemaReader.java

License:Apache License

public void createSchemaWatcher() {
    log.info("Creating ZooKeeper watch for the managed schema at " + managedSchemaPath + " ...");

    try {//from ww  w  . j  av a  2 s .com
        zkClient.exists(managedSchemaPath, new Watcher() {
            @Override
            public void process(WatchedEvent event) {
                // session events are not change events, and do not remove the watcher
                if (Event.EventType.None.equals(event.getType())) {
                    return;
                }
                log.info("A schema change: {}, has occurred - updating schema from ZooKeeper ...", event);
                try {
                    updateSchema(this);
                } catch (KeeperException e) {
                    if (e.code() == KeeperException.Code.SESSIONEXPIRED
                            || e.code() == KeeperException.Code.CONNECTIONLOSS) {
                        log.warn("ZooKeeper watch triggered, but Solr cannot talk to ZK");
                        return;
                    }
                    log.error("", e);
                    throw new ZooKeeperException(ErrorCode.SERVER_ERROR, "", e);
                } catch (InterruptedException e) {
                    // Restore the interrupted status
                    Thread.currentThread().interrupt();
                    log.warn("", e);
                }
            }
        }, true);
    } catch (KeeperException e) {
        final String msg = "Error creating ZooKeeper watch for the managed schema";
        log.error(msg, e);
        throw new ZooKeeperException(ErrorCode.SERVER_ERROR, msg, e);
    } catch (InterruptedException e) {
        // Restore the interrupted status
        Thread.currentThread().interrupt();
        log.warn("", e);
    }
}

From source file:org.apache.solr.security.hadoop.TestZkAclsWithHadoopAuth.java

License:Apache License

@Test
public void testZkAcls() throws Exception {
    ZooKeeper keeper = null;//  ww w.j a  v a  2 s .  c om
    try {
        keeper = new ZooKeeper(cluster.getZkServer().getZkAddress(), (int) TimeUnit.MINUTES.toMillis(1),
                new Watcher() {
                    @Override
                    public void process(WatchedEvent arg0) {
                        // Do nothing
                    }
                });

        keeper.addAuthInfo("digest", ("solr:" + SOLR_PASSWD).getBytes(StandardCharsets.UTF_8));

        // Test well known paths.
        checkNonSecurityACLs(keeper, "/solr.xml");
        checkSecurityACLs(keeper, "/security/token");
        checkSecurityACLs(keeper, "/security");

        // Now test all ZK tree.
        String zkHost = cluster.getSolrClient().getZkHost();
        String zkChroot = zkHost.contains("/") ? zkHost.substring(zkHost.indexOf("/")) : null;
        walkZkTree(keeper, zkChroot, "/");

    } finally {
        if (keeper != null) {
            keeper.close();
        }
    }
}

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  www  .j a va2s .  co  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 a2  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//from  ww w.j a v  a 2 s.  com
        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.AbstractTwillService.java

License:Apache License

/**
 * Watches for messages that are sent through ZK messages node.
 *//*www  .  j a  va  2  s.  c o  m*/
private void watchMessages() {
    final String messagesPath = getZKPath("messages");
    Futures.addCallback(zkClient.getChildren(messagesPath, new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            if (event.getType() == Event.EventType.NodeChildrenChanged && isRunning()) {
                watchMessages();
            }
        }
    }), new FutureCallback<NodeChildren>() {
        @Override
        public void onSuccess(NodeChildren result) {
            // Sort by the name, which is the messageId. Assumption is that message ids is ordered by time.
            List<String> messages = Lists.newArrayList(result.getChildren());
            Collections.sort(messages);
            for (String messageId : messages) {
                processMessage(messagesPath + "/" + messageId, messageId);
            }
        }

        @Override
        public void onFailure(Throwable t) {
            // TODO: what could be done besides just logging?
            LOG.error("Failed to watch messages.", t);
        }
    }, Threads.SAME_THREAD_EXECUTOR);
}

From source file:org.apache.twill.internal.AbstractZKServiceController.java

License:Apache License

private void actOnExists(final String path, final Runnable action) {
    // Watch for node existence.
    final AtomicBoolean nodeExists = new AtomicBoolean(false);
    Futures.addCallback(zkClient.exists(path, new Watcher() {
        @Override/*w  w w  .j a  va 2  s.co m*/
        public void process(WatchedEvent event) {
            if (!shouldProcessZKEvent()) {
                return;
            }
            // When node is created, call the action.
            // Other event type would be handled by the action.
            if (event.getType() == Event.EventType.NodeCreated && nodeExists.compareAndSet(false, true)) {
                action.run();
            }
        }
    }), new FutureCallback<Stat>() {
        @Override
        public void onSuccess(Stat result) {
            if (result != null && nodeExists.compareAndSet(false, true)) {
                action.run();
            }
        }

        @Override
        public void onFailure(Throwable t) {
            LOG.error("Failed in exists call to {}. Shutting down service.", path, t);
            forceShutDown();
        }
    }, Threads.SAME_THREAD_EXECUTOR);
}

From source file:org.apache.twill.internal.AbstractZKServiceController.java

License:Apache License

protected final void watchInstanceNode() {
    if (!shouldProcessZKEvent()) {
        return;//from   w  w w. j a  v  a 2  s . co  m
    }
    Futures.addCallback(zkClient.getData(getInstancePath(), new Watcher() {
        @Override
        public void process(WatchedEvent event) {
            if (!shouldProcessZKEvent()) {
                return;
            }
            switch (event.getType()) {
            case NodeDataChanged:
                watchInstanceNode();
                break;
            case NodeDeleted:
                instanceNodeFailed(KeeperException.create(KeeperException.Code.NONODE, getInstancePath()));
                break;
            default:
                LOG.info("Ignore ZK event for instance node: {}", event);
            }
        }
    }), instanceNodeDataCallback, Threads.SAME_THREAD_EXECUTOR);
}

From source file:org.apache.twill.internal.kafka.client.ZKBrokerService.java

License:Apache License

@Override
public synchronized Iterable<BrokerInfo> getBrokers() {
    Preconditions.checkState(isRunning(), "BrokerService is not running.");

    if (brokerList != null) {
        return brokerList.get();
    }/*from  w  w w .  jav  a 2 s  . c o  m*/

    final SettableFuture<?> readerFuture = SettableFuture.create();
    final AtomicReference<Iterable<BrokerInfo>> brokers = new AtomicReference<Iterable<BrokerInfo>>(
            ImmutableList.<BrokerInfo>of());

    actOnExists(BROKER_IDS_PATH, new Runnable() {
        @Override
        public void run() {
            // Callback for fetching children list. This callback should be executed in the executorService.
            final FutureCallback<NodeChildren> childrenCallback = new FutureCallback<NodeChildren>() {
                @Override
                public void onSuccess(NodeChildren result) {
                    try {
                        // For each children node, get the BrokerInfo from the brokerInfo cache.
                        brokers.set(ImmutableList.copyOf(Iterables.transform(brokerInfos
                                .getAll(Iterables.transform(result.getChildren(), BROKER_ID_TRANSFORMER))
                                .values(), Suppliers.<BrokerInfo>supplierFunction())));
                        readerFuture.set(null);

                        for (ListenerExecutor listener : listeners) {
                            listener.changed(ZKBrokerService.this);
                        }
                    } catch (ExecutionException e) {
                        readerFuture.setException(e.getCause());
                    }
                }

                @Override
                public void onFailure(Throwable t) {
                    readerFuture.setException(t);
                }
            };

            // Fetch list of broker ids
            Futures.addCallback(zkClient.getChildren(BROKER_IDS_PATH, new Watcher() {
                @Override
                public void process(WatchedEvent event) {
                    if (!isRunning()) {
                        return;
                    }
                    if (event.getType() == Event.EventType.NodeChildrenChanged) {
                        Futures.addCallback(zkClient.getChildren(BROKER_IDS_PATH, this), childrenCallback,
                                executorService);
                    }
                }
            }), childrenCallback, executorService);
        }
    }, readerFuture, FAILURE_RETRY_SECONDS, TimeUnit.SECONDS);

    brokerList = createSupplier(brokers);
    try {
        readerFuture.get();
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
    return brokerList.get();
}

From source file:org.apache.twill.internal.kafka.client.ZKBrokerService.java

License:Apache License

/**
 * Creates a cache loader for the given path to supply data with the data node.
 *//*from  www.j a v  a2 s  .c  om*/
private <K extends KeyPath, T> CacheLoader<K, Supplier<T>> createCacheLoader(
        final CacheInvalidater<K> invalidater, final Class<T> resultType) {
    return new CacheLoader<K, Supplier<T>>() {

        @Override
        public Supplier<T> load(final K key) throws Exception {
            // A future to tell if the result is ready, even it is failure.
            final SettableFuture<T> readyFuture = SettableFuture.create();
            final AtomicReference<T> resultValue = new AtomicReference<T>();

            // Fetch for node data when it exists.
            final String path = key.getPath();
            actOnExists(path, new Runnable() {
                @Override
                public void run() {
                    // Callback for getData call
                    final FutureCallback<NodeData> dataCallback = new FutureCallback<NodeData>() {
                        @Override
                        public void onSuccess(NodeData result) {
                            // Update with latest data
                            T value = decodeNodeData(result, resultType);
                            resultValue.set(value);
                            readyFuture.set(value);
                        }

                        @Override
                        public void onFailure(Throwable t) {
                            LOG.error("Failed to fetch node data on {}", path, t);
                            if (t instanceof KeeperException.NoNodeException) {
                                resultValue.set(null);
                                readyFuture.set(null);
                                return;
                            }

                            // On error, simply invalidate the key so that it'll be fetched next time.
                            invalidater.invalidate(key);
                            readyFuture.setException(t);
                        }
                    };

                    // Fetch node data
                    Futures.addCallback(zkClient.getData(path, new Watcher() {
                        @Override
                        public void process(WatchedEvent event) {
                            if (!isRunning()) {
                                return;
                            }
                            if (event.getType() == Event.EventType.NodeDataChanged) {
                                // If node data changed, fetch it again.
                                Futures.addCallback(zkClient.getData(path, this), dataCallback,
                                        executorService);
                            } else if (event.getType() == Event.EventType.NodeDeleted) {
                                // If node removed, invalidate the cached value.
                                brokerInfos.invalidate(key);
                            }
                        }
                    }), dataCallback, executorService);
                }
            }, readyFuture, FAILURE_RETRY_SECONDS, TimeUnit.SECONDS);

            readyFuture.get();
            return createSupplier(resultValue);
        }
    };
}