List of usage examples for org.apache.zookeeper WatchedEvent getType
public EventType getType()
From source file:net.sf.katta.zk.ZKClient.java
License:Apache License
private void processDataOrChildChange(WatchedEvent event) { // ZkEventType eventType = ZkEventType.getMappedType(event.getType()); final String path = event.getPath(); if (event.getType() == Watcher.Event.EventType.NodeChildrenChanged) { Set<IZkChildListener> childListeners = _path2ChildListenersMap.get(path); if (childListeners != null && !childListeners.isEmpty()) { // since resubscribeChildPath might remove listener from listeners // because they throw exeception we need to copy the listener first to // make sure we also notify listener we could not re subscribe to the // given path HashSet<IZkChildListener> copiedSet = new HashSet<IZkChildListener>(); copiedSet.addAll(childListeners); List<String> children = resubscribeChildPath(path, childListeners); for (final IZkChildListener listener : copiedSet) { try { listener.handleChildChange(event.getPath(), children); } catch (final Throwable e) { LOG.error("Faild to process event with listener: " + listener, e); }//from w ww .j av a 2 s . c o m } } } else { Set<IZkDataListener> listeners = _path2DataListenersMap.get(path); if (listeners != null && !listeners.isEmpty()) { HashSet<IZkDataListener> copiedSet = new HashSet<IZkDataListener>(); copiedSet.addAll(listeners); byte[] data = resubscribeDataPath(path, listeners); for (final IZkDataListener listener : copiedSet) { try { if (event.getType() == Watcher.Event.EventType.NodeCreated) { listener.handleDataAdded(event.getPath(), readWritable(listener.createWritable(), data)); } else if (event.getType() == Watcher.Event.EventType.NodeDataChanged) { listener.handleDataChange(event.getPath(), readWritable(listener.createWritable(), data)); } else if (event.getType() == Watcher.Event.EventType.NodeDeleted) { listener.handleDataDeleted(event.getPath()); } else { LOG.error("Received a unknown event, ignoring: " + event.getType()); } } catch (final Throwable e) { LOG.error("Faild to process event " + event.getType() + " with listener: " + listener, e); } } } } }
From source file:net.spy.memcached.CacheManager.java
License:Apache License
/*************************************************************************** * We do process only child node change event ourselves, we just need to * forward them on.//from ww w .ja v a 2 s . c o m * */ public void process(WatchedEvent event) { if (event.getType() == Event.EventType.None) { switch (event.getState()) { case SyncConnected: getLogger().info("Connected to Arcus admin. (%s@%s)", serviceCode, hostPort); zkInitLatch.countDown(); } } if (cacheMonitor != null) { cacheMonitor.process(event); } else { getLogger().debug("cm is null, servicecode : %s, state:%s, type:%s", serviceCode, event.getState(), event.getType()); } }
From source file:net.spy.memcached.CacheMonitor.java
License:Apache License
/** * Processes every events from the ZooKeeper. *//* w ww . j a va 2 s . com*/ public void process(WatchedEvent event) { if (event.getType() == Event.EventType.None) { // Processes session events switch (event.getState()) { case SyncConnected: getLogger().warn("Reconnected to the Arcus admin. " + getInfo()); return; case Disconnected: getLogger().warn("Disconnected from the Arcus admin. Trying to reconnect. " + getInfo()); return; case Expired: // If the session was expired, just shutdown this client to be re-initiated. getLogger().warn("Session expired. Trying to reconnect to the Arcus admin." + getInfo()); shutdown(); return; } } else { // Set a new watch on the znode when there are any changes in it. if (event.getType() == Event.EventType.NodeChildrenChanged) { asyncGetCacheList(); } } }
From source file:org.apache.accumulo.fate.zookeeper.ZooLock.java
License:Apache License
private synchronized void lockAsync(final String myLock, final AsyncLockWatcher lw) throws KeeperException, InterruptedException { if (asyncLock == null) { throw new IllegalStateException("Called lockAsync() when asyncLock == null"); }/* www. ja v a 2 s .com*/ List<String> children = zooKeeper.getChildren(path); if (!children.contains(myLock)) { throw new RuntimeException("Lock attempt ephemeral node no longer exist " + myLock); } Collections.sort(children); if (log.isTraceEnabled()) { log.trace("Candidate lock nodes"); for (String child : children) { log.trace("- " + child); } } if (children.get(0).equals(myLock)) { log.trace("First candidate is my lock, acquiring"); if (!watchingParent) { throw new IllegalStateException("Can not acquire lock, no longer watching parent : " + path); } this.lockWatcher = lw; this.lock = myLock; asyncLock = null; lockWasAcquired = true; lw.acquiredLock(); return; } String prev = null; for (String child : children) { if (child.equals(myLock)) { break; } prev = child; } final String lockToWatch = path + "/" + prev; log.trace("Establishing watch on " + lockToWatch); Stat stat = zooKeeper.getStatus(lockToWatch, new Watcher() { @Override public void process(WatchedEvent event) { if (log.isTraceEnabled()) { log.trace("Processing event:"); log.trace("- type " + event.getType()); log.trace("- path " + event.getPath()); log.trace("- state " + event.getState()); } boolean renew = true; if (event.getType() == EventType.NodeDeleted && event.getPath().equals(lockToWatch)) { log.trace("Detected deletion of " + lockToWatch + ", attempting to acquire lock"); synchronized (ZooLock.this) { try { if (asyncLock != null) { lockAsync(myLock, lw); } else if (log.isTraceEnabled()) { log.trace("While waiting for another lock " + lockToWatch + " " + myLock + " was deleted"); } } catch (Exception e) { if (lock == null) { // have not acquired lock yet lw.failedToAcquireLock(e); } } } renew = false; } if (event.getState() == KeeperState.Expired || event.getState() == KeeperState.Disconnected) { synchronized (ZooLock.this) { if (lock == null) { lw.failedToAcquireLock(new Exception("Zookeeper Session expired / disconnected")); } } renew = false; } if (renew) { log.trace("Renewing watch on " + lockToWatch); try { Stat restat = zooKeeper.getStatus(lockToWatch, this); if (restat == null) { lockAsync(myLock, lw); } } catch (KeeperException e) { lw.failedToAcquireLock(new Exception("Failed to renew watch on other master node")); } catch (InterruptedException e) { lw.failedToAcquireLock(new Exception("Failed to renew watch on other master node")); } } } }); if (stat == null) lockAsync(myLock, lw); }
From source file:org.apache.accumulo.fate.zookeeper.ZooLock.java
License:Apache License
public synchronized void lockAsync(final AsyncLockWatcher lw, byte data[]) { if (lockWatcher != null || lock != null || asyncLock != null) { throw new IllegalStateException(); }//from w ww .ja va 2 s . c om lockWasAcquired = false; try { final String asyncLockPath = zooKeeper.putEphemeralSequential(path + "/" + LOCK_PREFIX, data); log.trace("Ephemeral node " + asyncLockPath + " created"); Stat stat = zooKeeper.getStatus(asyncLockPath, new Watcher() { private void failedToAcquireLock() { lw.failedToAcquireLock(new Exception("Lock deleted before acquired")); asyncLock = null; } @Override public void process(WatchedEvent event) { synchronized (ZooLock.this) { if (lock != null && event.getType() == EventType.NodeDeleted && event.getPath().equals(path + "/" + lock)) { lostLock(LockLossReason.LOCK_DELETED); } else if (asyncLock != null && event.getType() == EventType.NodeDeleted && event.getPath().equals(path + "/" + asyncLock)) { failedToAcquireLock(); } else if (event.getState() != KeeperState.Disconnected && event.getState() != KeeperState.Expired && (lock != null || asyncLock != null)) { log.debug("Unexpected event watching lock node " + event + " " + asyncLockPath); try { Stat stat2 = zooKeeper.getStatus(asyncLockPath, this); if (stat2 == null) { if (lock != null) lostLock(LockLossReason.LOCK_DELETED); else if (asyncLock != null) failedToAcquireLock(); } } catch (Throwable e) { lockWatcher.unableToMonitorLockNode(e); log.error("Failed to stat lock node " + asyncLockPath, e); } } } } }); if (stat == null) { lw.failedToAcquireLock(new Exception("Lock does not exist after create")); return; } asyncLock = asyncLockPath.substring(path.length() + 1); lockAsync(asyncLock, lw); } catch (KeeperException e) { lw.failedToAcquireLock(e); } catch (InterruptedException e) { lw.failedToAcquireLock(e); } }
From source file:org.apache.accumulo.fate.zookeeper.ZooLock.java
License:Apache License
@Override public synchronized void process(WatchedEvent event) { log.debug("event " + event.getPath() + " " + event.getType() + " " + event.getState()); watchingParent = false;/*from w w w. ja va 2s.com*/ if (event.getState() == KeeperState.Expired && lock != null) { lostLock(LockLossReason.SESSION_EXPIRED); } else { try { // set the watch on the parent node again zooKeeper.getStatus(path, this); watchingParent = true; } catch (KeeperException.ConnectionLossException ex) { // we can't look at the lock because we aren't connected, but our session is still good log.warn("lost connection to zookeeper"); } catch (Exception ex) { if (lock != null || asyncLock != null) { lockWatcher.unableToMonitorLockNode(ex); log.error("Error resetting watch on ZooLock " + lock == null ? asyncLock : lock + " " + event, ex); } } } }
From source file:org.apache.accumulo.master.Master.java
License:Apache License
public void run() throws IOException, InterruptedException, KeeperException { final String zroot = ZooUtil.getRoot(getInstance()); // ACCUMULO-4424 Put up the Thrift servers before getting the lock as a sign of process health when a hot-standby ////from w ww . j a va 2 s. co m // Start the Master's Client service clientHandler = new MasterClientServiceHandler(this); // Ensure that calls before the master gets the lock fail Iface haProxy = HighlyAvailableServiceWrapper.service(clientHandler, this); Iface rpcProxy = RpcWrapper.service(haProxy, new Processor<Iface>(clientHandler)); final Processor<Iface> processor; if (ThriftServerType.SASL == getThriftServerType()) { Iface tcredsProxy = TCredentialsUpdatingWrapper.service(rpcProxy, clientHandler.getClass(), getConfiguration()); processor = new Processor<>(tcredsProxy); } else { processor = new Processor<>(rpcProxy); } ServerAddress sa = TServerUtils.startServer(this, hostname, Property.MASTER_CLIENTPORT, processor, "Master", "Master Client Service Handler", null, Property.MASTER_MINTHREADS, Property.MASTER_THREADCHECK, Property.GENERAL_MAX_MESSAGE_SIZE); clientService = sa.server; log.info("Started Master client service at {}", sa.address); // Start the replication coordinator which assigns tservers to service replication requests MasterReplicationCoordinator impl = new MasterReplicationCoordinator(this); ReplicationCoordinator.Iface haReplicationProxy = HighlyAvailableServiceWrapper.service(impl, this); ReplicationCoordinator.Processor<ReplicationCoordinator.Iface> replicationCoordinatorProcessor = new ReplicationCoordinator.Processor<>( RpcWrapper.service(impl, new ReplicationCoordinator.Processor<>(haReplicationProxy))); ServerAddress replAddress = TServerUtils.startServer(this, hostname, Property.MASTER_REPLICATION_COORDINATOR_PORT, replicationCoordinatorProcessor, "Master Replication Coordinator", "Replication Coordinator", null, Property.MASTER_REPLICATION_COORDINATOR_MINTHREADS, Property.MASTER_REPLICATION_COORDINATOR_THREADCHECK, Property.GENERAL_MAX_MESSAGE_SIZE); log.info("Started replication coordinator service at " + replAddress.address); // block until we can obtain the ZK lock for the master getMasterLock(zroot + Constants.ZMASTER_LOCK); recoveryManager = new RecoveryManager(this); TableManager.getInstance().addObserver(this); StatusThread statusThread = new StatusThread(); statusThread.start(); MigrationCleanupThread migrationCleanupThread = new MigrationCleanupThread(); migrationCleanupThread.start(); tserverSet.startListeningForTabletServerChanges(); ZooReaderWriter zReaderWriter = ZooReaderWriter.getInstance(); zReaderWriter.getChildren(zroot + Constants.ZRECOVERY, new Watcher() { @Override public void process(WatchedEvent event) { nextEvent.event("Noticed recovery changes", event.getType()); try { // watcher only fires once, add it back ZooReaderWriter.getInstance().getChildren(zroot + Constants.ZRECOVERY, this); } catch (Exception e) { log.error("Failed to add log recovery watcher back", e); } } }); watchers.add(new TabletGroupWatcher(this, new MetaDataStateStore(this, this), null) { @Override boolean canSuspendTablets() { // Always allow user data tablets to enter suspended state. return true; } }); watchers.add(new TabletGroupWatcher(this, new RootTabletStateStore(this, this), watchers.get(0)) { @Override boolean canSuspendTablets() { // Allow metadata tablets to enter suspended state only if so configured. Generally we'll want metadata tablets to // be immediately reassigned, even if there's a global table.suspension.duration setting. return getConfiguration().getBoolean(Property.MASTER_METADATA_SUSPENDABLE); } }); watchers.add(new TabletGroupWatcher(this, new ZooTabletStateStore(new ZooStore(zroot)), watchers.get(1)) { @Override boolean canSuspendTablets() { // Never allow root tablet to enter suspended state. return false; } }); for (TabletGroupWatcher watcher : watchers) { watcher.start(); } // Once we are sure the upgrade is complete, we can safely allow fate use. waitForMetadataUpgrade.await(); try { final AgeOffStore<Master> store = new AgeOffStore<>( new org.apache.accumulo.fate.ZooStore<Master>(ZooUtil.getRoot(getInstance()) + Constants.ZFATE, ZooReaderWriter.getInstance()), 1000 * 60 * 60 * 8); int threads = getConfiguration().getCount(Property.MASTER_FATE_THREADPOOL_SIZE); fate = new Fate<>(this, store); fate.startTransactionRunners(threads); SimpleTimer.getInstance(getConfiguration()).schedule(new Runnable() { @Override public void run() { store.ageOff(); } }, 63000, 63000); } catch (KeeperException e) { throw new IOException(e); } catch (InterruptedException e) { throw new IOException(e); } ZooKeeperInitialization.ensureZooKeeperInitialized(zReaderWriter, zroot); // Make sure that we have a secret key (either a new one or an old one from ZK) before we start // the master client service. if (null != authenticationTokenKeyManager && null != keyDistributor) { log.info("Starting delegation-token key manager"); keyDistributor.initialize(); authenticationTokenKeyManager.start(); boolean logged = false; while (!authenticationTokenKeyManager.isInitialized()) { // Print out a status message when we start waiting for the key manager to get initialized if (!logged) { log.info("Waiting for AuthenticationTokenKeyManager to be initialized"); logged = true; } sleepUninterruptibly(200, TimeUnit.MILLISECONDS); } // And log when we are initialized log.info("AuthenticationTokenSecretManager is initialized"); } String address = sa.address.toString(); log.info("Setting master lock data to " + address); masterLock.replaceLockData(address.getBytes()); while (!clientService.isServing()) { sleepUninterruptibly(100, TimeUnit.MILLISECONDS); } // Start the daemon to scan the replication table and make units of work replicationWorkDriver = new ReplicationDriver(this); replicationWorkDriver.start(); // Start the daemon to assign work to tservers to replicate to our peers try { replicationWorkAssigner = new WorkDriver(this); } catch (AccumuloException | AccumuloSecurityException e) { log.error("Caught exception trying to initialize replication WorkDriver", e); throw new RuntimeException(e); } replicationWorkAssigner.start(); // Advertise that port we used so peers don't have to be told what it is ZooReaderWriter.getInstance().putPersistentData( ZooUtil.getRoot(getInstance()) + Constants.ZMASTER_REPLICATION_COORDINATOR_ADDR, replAddress.address.toString().getBytes(UTF_8), NodeExistsPolicy.OVERWRITE); // Register replication metrics MasterMetricsFactory factory = new MasterMetricsFactory(getConfiguration(), this); Metrics replicationMetrics = factory.createReplicationMetrics(); try { replicationMetrics.register(); } catch (Exception e) { log.error("Failed to register replication metrics", e); } while (clientService.isServing()) { sleepUninterruptibly(500, TimeUnit.MILLISECONDS); } log.info("Shutting down fate."); fate.shutdown(); log.info("Shutting down timekeeping."); timeKeeper.shutdown(); final long deadline = System.currentTimeMillis() + MAX_CLEANUP_WAIT_TIME; statusThread.join(remaining(deadline)); replicationWorkAssigner.join(remaining(deadline)); replicationWorkDriver.join(remaining(deadline)); replAddress.server.stop(); // Signal that we want it to stop, and wait for it to do so. if (authenticationTokenKeyManager != null) { authenticationTokenKeyManager.gracefulStop(); authenticationTokenKeyManager.join(remaining(deadline)); } // quit, even if the tablet servers somehow jam up and the watchers // don't stop for (TabletGroupWatcher watcher : watchers) { watcher.join(remaining(deadline)); } log.info("exiting"); }
From source file:org.apache.accumulo.server.conf.NamespaceConfWatcher.java
License:Apache License
static String toString(WatchedEvent event) { return new StringBuilder("{path=").append(event.getPath()).append(",state=").append(event.getState()) .append(",type=").append(event.getType()).append("}").toString(); }
From source file:org.apache.accumulo.server.conf.NamespaceConfWatcher.java
License:Apache License
@Override public void process(WatchedEvent event) { String path = event.getPath(); if (log.isTraceEnabled()) log.trace("WatchedEvent : " + toString(event)); String namespaceId = null;//from w w w . ja v a 2 s.c om String key = null; if (path != null) { if (path.startsWith(namespacesPrefix)) { namespaceId = path.substring(namespacesPrefixLength); if (namespaceId.contains("/")) { namespaceId = namespaceId.substring(0, namespaceId.indexOf('/')); if (path.startsWith(namespacesPrefix + namespaceId + Constants.ZNAMESPACE_CONF + "/")) key = path.substring( (namespacesPrefix + namespaceId + Constants.ZNAMESPACE_CONF + "/").length()); } } if (namespaceId == null) { log.warn("Zookeeper told me about a path I was not watching: " + path + ", event " + toString(event)); return; } } switch (event.getType()) { case NodeDataChanged: if (log.isTraceEnabled()) log.trace("EventNodeDataChanged " + event.getPath()); if (key != null) scf.getNamespaceConfiguration(namespaceId).propertyChanged(key); break; case NodeChildrenChanged: scf.getNamespaceConfiguration(namespaceId).propertiesChanged(); break; case NodeDeleted: if (key == null) { ServerConfigurationFactory.removeCachedNamespaceConfiguration(instance.getInstanceID(), namespaceId); } break; case None: switch (event.getState()) { case Expired: ServerConfigurationFactory.expireAllTableObservers(); break; case SyncConnected: break; case Disconnected: break; default: log.warn("EventNone event not handled " + toString(event)); } break; case NodeCreated: switch (event.getState()) { case SyncConnected: break; default: log.warn("Event NodeCreated event not handled " + toString(event)); } break; default: log.warn("Event not handled " + toString(event)); } }
From source file:org.apache.accumulo.server.conf.TableConfWatcher.java
License:Apache License
@Override public void process(WatchedEvent event) { String path = event.getPath(); if (log.isTraceEnabled()) log.trace("WatchedEvent : " + toString(event)); String tableId = null;// w ww . j a v a 2 s .c o m String key = null; if (path != null) { if (path.startsWith(tablesPrefix)) { tableId = path.substring(tablesPrefix.length()); if (tableId.contains("/")) { tableId = tableId.substring(0, tableId.indexOf('/')); if (path.startsWith(tablesPrefix + tableId + Constants.ZTABLE_CONF + "/")) key = path.substring((tablesPrefix + tableId + Constants.ZTABLE_CONF + "/").length()); } } if (tableId == null) { log.warn("Zookeeper told me about a path I was not watching: " + path + ", event " + toString(event)); return; } } switch (event.getType()) { case NodeDataChanged: if (log.isTraceEnabled()) log.trace("EventNodeDataChanged " + event.getPath()); if (key != null) scf.getTableConfiguration(tableId).propertyChanged(key); break; case NodeChildrenChanged: scf.getTableConfiguration(tableId).propertiesChanged(); break; case NodeDeleted: if (key == null) { // only remove the AccumuloConfiguration object when a // table node is deleted, not when a tables property is // deleted. ServerConfigurationFactory.removeCachedTableConfiguration(instance.getInstanceID(), tableId); } break; case None: switch (event.getState()) { case Expired: ServerConfigurationFactory.expireAllTableObservers(); break; case SyncConnected: break; case Disconnected: break; default: log.warn("EventNone event not handled " + toString(event)); } break; case NodeCreated: switch (event.getState()) { case SyncConnected: break; default: log.warn("Event NodeCreated event not handled " + toString(event)); } break; default: log.warn("Event not handled " + toString(event)); } }