List of usage examples for org.apache.zookeeper WatchedEvent getType
public EventType getType()
From source file:org.apache.solr.codecs.onsql.ONSQLCodecFactory.java
License:Apache License
@Override public void inform(SolrCore core) { log.info("=>>>>>>>>>>>>>codecfactory inform was called"); final String dir = Util.tidyIndexDir(core.getIndexDir()); log.info("inform.dir=" + dir); //Util.printStackTrace(); final CloudDescriptor cldesc = core.getCoreDescriptor().getCloudDescriptor(); if (cldesc != null) { log.info("cloud config available"); ONSQLKVstoreHandler.getInstance().setAllowWriting(dir, cldesc.isLeader()); ONSQLKVstoreHandler.getInstance().setShardId(dir, cldesc.getShardId()); final SolrZkClient zkclient = core.getCoreDescriptor().getCoreContainer().getZkController() .getZkClient();/*from w w w. j a v a2 s . c o m*/ ZkSolrResourceLoader loader = (ZkSolrResourceLoader) core.getResourceLoader(); String zkconfigpath = loader.getCollectionZkPath(); try { // load NoSQL config from ZKregistry byte[] content = zkclient.getData(zkconfigpath + "/kvstore.properties", null, null, true); ByteArrayInputStream is = new ByteArrayInputStream(content); Properties kvstore_props = new Properties(); kvstore_props.load(is); is.close(); ONSQLKVstoreHandler.getInstance().setKVStore(dir, kvstore_props); // hook watcher to the cluster update zkclient.exists(ZkStateReader.CLUSTER_STATE, new Watcher() { @Override public void process(WatchedEvent event) { if (EventType.None.equals(event.getType())) return; log.info("got event type=" + event.getType()); try { final Watcher thisWatch = this; zkclient.exists(ZkStateReader.CLUSTER_STATE, thisWatch, true); //clstate.getReplica(arg0, arg1) ONSQLKVstoreHandler.getInstance().setAllowWriting(dir, cldesc.isLeader()); } catch (KeeperException e) { if (e.code() == KeeperException.Code.SESSIONEXPIRED || e.code() == KeeperException.Code.CONNECTIONLOSS) { log.warn("we have been disconnected from registry"); // TODO add code for stopping all the jobs in case it was a network failure in the cluster return; } throw new ZooKeeperException(SolrException.ErrorCode.SERVER_ERROR, "", e); } catch (InterruptedException e) { log.error(" we have been interrupted", e); // Restore the interrupted status Thread.currentThread().interrupt(); return; } } }, true); } catch (IOException e) { log.error("error while reading key-value store properties", e); throw new IllegalStateException("error while reading key-value store properties"); } catch (KeeperException e) { log.error("we have been disconnected from Zookeeper registry", e); throw new IllegalStateException("error while reading key-value store properties"); } catch (InterruptedException e) { log.error(" we have been interrupted", e); // Restore the interrupted status Thread.currentThread().interrupt(); } } else { log.info("no cloud available, using local configs from filesystem, collection name=" + core.getName()); ONSQLKVstoreHandler.getInstance().setAllowWriting(dir, false); ONSQLKVstoreHandler.getInstance().setShardId(dir, "shard1"); Properties props = new Properties(); try { FileInputStream propstream = new FileInputStream( core.getResourceLoader().getConfigDir().concat("/kvstore.properties")); props.load(propstream); propstream.close(); } catch (FileNotFoundException fnex) { throw new IllegalStateException( "kvstore.properties file is missing or non-readable in core config directory"); } catch (IOException fnex) { throw new IllegalStateException( "kvstore.properties file is missing or non-readable in core config directory"); } ONSQLKVstoreHandler.getInstance().setKVStore(dir, props); } }
From source file:org.apache.solr.common.cloud.ConnectionManager.java
License:Apache License
@Override public synchronized void process(WatchedEvent event) { if (log.isInfoEnabled()) { log.info("Watcher " + this + " name:" + name + " got event " + event + " path:" + event.getPath() + " type:" + event.getType()); }/*from ww w . java2s . c o m*/ if (isClosed) { log.info("Client->ZooKeeper status change trigger but we are already closed"); return; } state = event.getState(); if (state == KeeperState.SyncConnected) { connected = true; clientConnected.countDown(); connectionStrategy.connected(); } else if (state == KeeperState.Expired) { connected = false; log.info( "Our previous ZooKeeper session was expired. Attempting to reconnect to recover relationship with ZooKeeper..."); try { connectionStrategy.reconnect(zkServerAddress, zkClientTimeout, this, new ZkClientConnectionStrategy.ZkUpdate() { @Override public void update(SolrZooKeeper keeper) { try { waitForConnected(Long.MAX_VALUE); } catch (Exception e1) { closeKeeper(keeper); throw new RuntimeException(e1); } log.info("Connection with ZooKeeper reestablished."); try { client.updateKeeper(keeper); } catch (InterruptedException e) { closeKeeper(keeper); Thread.currentThread().interrupt(); // we must have been asked to stop throw new RuntimeException(e); } catch (Throwable t) { closeKeeper(keeper); throw new RuntimeException(t); } if (onReconnect != null) { onReconnect.command(); } synchronized (ConnectionManager.this) { ConnectionManager.this.connected = true; } } }); } catch (Exception e) { SolrException.log(log, "", e); } log.info("Connected:" + connected); } else if (state == KeeperState.Disconnected) { log.info("zkClient has disconnected"); connected = false; connectionStrategy.disconnected(); } else { connected = false; } notifyAll(); }
From source file:org.apache.solr.common.cloud.ZkStateReader.java
License:Apache License
public synchronized void createClusterStateWatchersAndUpdate() throws KeeperException, InterruptedException { // We need to fetch the current cluster state and the set of live nodes synchronized (getUpdateLock()) { cmdExecutor.ensureExists(CLUSTER_STATE, zkClient); cmdExecutor.ensureExists(ALIASES, zkClient); log.info("Updating cluster state from ZooKeeper... "); zkClient.exists(CLUSTER_STATE, new Watcher() { @Override//from w ww. java 2s . c o m public void process(WatchedEvent event) { // session events are not change events, // and do not remove the watcher if (EventType.None.equals(event.getType())) { return; } log.info("A cluster state change: {}, has occurred - updating... (live nodes size: {})", (event), ZkStateReader.this.clusterState == null ? 0 : ZkStateReader.this.clusterState.getLiveNodes().size()); try { // delayed approach // ZkStateReader.this.updateClusterState(false, false); synchronized (ZkStateReader.this.getUpdateLock()) { // remake watch final Watcher thisWatch = this; Stat stat = new Stat(); byte[] data = zkClient.getData(CLUSTER_STATE, thisWatch, stat, true); Set<String> ln = ZkStateReader.this.clusterState.getLiveNodes(); ClusterState clusterState = ClusterState.load(stat.getVersion(), data, ln); // update volatile ZkStateReader.this.clusterState = clusterState; } } 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(SolrException.ErrorCode.SERVER_ERROR, "", e); } catch (InterruptedException e) { // Restore the interrupted status Thread.currentThread().interrupt(); log.warn("", e); return; } } }, true); } synchronized (ZkStateReader.this.getUpdateLock()) { List<String> liveNodes = zkClient.getChildren(LIVE_NODES_ZKNODE, new Watcher() { @Override public void process(WatchedEvent event) { // session events are not change events, // and do not remove the watcher if (EventType.None.equals(event.getType())) { return; } try { // delayed approach // ZkStateReader.this.updateClusterState(false, true); synchronized (ZkStateReader.this.getUpdateLock()) { List<String> liveNodes = zkClient.getChildren(LIVE_NODES_ZKNODE, this, true); log.info("Updating live nodes... ({})", liveNodes.size()); Set<String> liveNodesSet = new HashSet<String>(); liveNodesSet.addAll(liveNodes); ClusterState clusterState = new ClusterState( ZkStateReader.this.clusterState.getZkClusterStateVersion(), liveNodesSet, ZkStateReader.this.clusterState.getCollectionStates()); ZkStateReader.this.clusterState = clusterState; } } 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(SolrException.ErrorCode.SERVER_ERROR, "", e); } catch (InterruptedException e) { // Restore the interrupted status Thread.currentThread().interrupt(); log.warn("", e); return; } } }, true); Set<String> liveNodeSet = new HashSet<String>(); liveNodeSet.addAll(liveNodes); ClusterState clusterState = ClusterState.load(zkClient, liveNodeSet); this.clusterState = clusterState; zkClient.exists(ALIASES, new Watcher() { @Override public void process(WatchedEvent event) { // session events are not change events, // and do not remove the watcher if (EventType.None.equals(event.getType())) { return; } try { synchronized (ZkStateReader.this.getUpdateLock()) { log.info("Updating aliases... "); // remake watch final Watcher thisWatch = this; Stat stat = new Stat(); byte[] data = zkClient.getData(ALIASES, thisWatch, stat, true); Aliases aliases = ClusterState.load(data); ZkStateReader.this.aliases = aliases; } } 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(SolrException.ErrorCode.SERVER_ERROR, "", e); } catch (InterruptedException e) { // Restore the interrupted status Thread.currentThread().interrupt(); log.warn("", e); return; } } }, true); } updateAliases(); }
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 w w w . j av a 2 s . c o m 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.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 .j av a 2 s . c o 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.twill.internal.AbstractTwillService.java
License:Apache License
/** * Watches for messages that are sent through ZK messages node. */// www. j a v a2 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/*from w w w . j a v a2 s .c o 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 www.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(); }//w ww . ja v a 2s . co 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 ww w. j a v a 2 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); } }; }