List of usage examples for org.apache.zookeeper WatchedEvent getPath
public String getPath()
From source file:org.apache.cassandra.mutex.ClusterMutex.java
License:Apache License
/** * process any events from ZooKeeper. We simply wake up any clients that are waiting for * file deletion. Number of clients is usually very small (most likely just one), so no need * for any complex logic.//w w w . j a va 2 s . c om */ public void process(WatchedEvent event) { if (logger.isTraceEnabled()) logger.trace("Got event " + event.getType() + ", keeper state " + event.getState() + ", path " + event.getPath()); synchronized (mutex) { mutex.notifyAll(); } }
From source file:org.apache.cassandra.service.StorageService.java
License:Apache License
private void reportToZookeeper() throws Throwable { try {//w w w . jav a 2 s . c o m zk_ = new ZooKeeper(DatabaseDescriptor.getZkAddress(), DatabaseDescriptor.getZkSessionTimeout(), new Watcher() { public void process(WatchedEvent we) { String path = "/Cassandra/" + DatabaseDescriptor.getClusterName() + "/Leader"; String eventPath = we.getPath(); logger_.debug("PROCESS EVENT : " + eventPath); if (eventPath != null && (eventPath.contains(path))) { logger_.debug("Signalling the leader instance ..."); LeaderElector.instance().signal(); } } }); Stat stat = zk_.exists("/", false); if (stat != null) { stat = zk_.exists("/Cassandra", false); if (stat == null) { logger_.debug("Creating the Cassandra znode ..."); zk_.create("/Cassandra", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } String path = "/Cassandra/" + DatabaseDescriptor.getClusterName(); stat = zk_.exists(path, false); if (stat == null) { logger_.debug("Creating the cluster znode " + path); zk_.create(path, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } /* Create the Leader, Locks and Misc znode */ stat = zk_.exists(path + "/Leader", false); if (stat == null) { logger_.debug("Creating the leader znode " + path); zk_.create(path + "/Leader", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } stat = zk_.exists(path + "/Locks", false); if (stat == null) { logger_.debug("Creating the locks znode " + path); zk_.create(path + "/Locks", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } stat = zk_.exists(path + "/Misc", false); if (stat == null) { logger_.debug("Creating the misc znode " + path); zk_.create(path + "/Misc", new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); } } } catch (KeeperException ke) { LogUtil.throwableToString(ke); /* do the re-initialize again. */ reportToZookeeper(); } }
From source file:org.apache.cassandra.service.ZookeeperWatcher.java
License:Apache License
public void process(WatchedEvent we) { String eventPath = we.getPath(); logger_.debug("PROCESS EVENT : " + eventPath); if (eventPath != null && (eventPath.contains(leader_))) { logger_.debug("Signalling the leader instance ..."); LeaderElector.instance().signal(); }/* w w w . j a v a 2s . co m*/ }
From source file:org.apache.curator.framework.imps.CuratorFrameworkImpl.java
License:Apache License
public CuratorFrameworkImpl(CuratorFrameworkFactory.Builder builder) { ZookeeperFactory localZookeeperFactory = makeZookeeperFactory(builder.getZookeeperFactory()); this.client = new CuratorZookeeperClient(localZookeeperFactory, builder.getEnsembleProvider(), builder.getSessionTimeoutMs(), builder.getConnectionTimeoutMs(), new Watcher() { @Override//from ww w . j a v a 2 s . c om public void process(WatchedEvent watchedEvent) { CuratorEvent event = new CuratorEventImpl(CuratorFrameworkImpl.this, CuratorEventType.WATCHED, watchedEvent.getState().getIntValue(), unfixForNamespace(watchedEvent.getPath()), null, null, null, null, null, watchedEvent, null); processEvent(event); } }, builder.getRetryPolicy(), builder.canBeReadOnly()); listeners = new ListenerContainer<CuratorListener>(); unhandledErrorListeners = new ListenerContainer<UnhandledErrorListener>(); backgroundOperations = new DelayQueue<OperationAndData<?>>(); namespace = new NamespaceImpl(this, builder.getNamespace()); threadFactory = getThreadFactory(builder); maxCloseWaitMs = builder.getMaxCloseWaitMs(); connectionStateManager = new ConnectionStateManager(this, builder.getThreadFactory()); compressionProvider = builder.getCompressionProvider(); aclProvider = builder.getAclProvider(); state = new AtomicReference<CuratorFrameworkState>(CuratorFrameworkState.LATENT); useContainerParentsIfAvailable = builder.useContainerParentsIfAvailable(); byte[] builderDefaultData = builder.getDefaultData(); defaultData = (builderDefaultData != null) ? Arrays.copyOf(builderDefaultData, builderDefaultData.length) : new byte[0]; authInfos = buildAuths(builder); failedDeleteManager = new FailedDeleteManager(this); namespaceFacadeCache = new NamespaceFacadeCache(this); }
From source file:org.apache.curator.framework.imps.TestFramework.java
License:Apache License
@Test public void testNamespaceWithWatcher() throws Exception { CuratorFrameworkFactory.Builder builder = CuratorFrameworkFactory.builder(); CuratorFramework client = builder.connectString(server.getConnectString()).namespace("aisa") .retryPolicy(new RetryOneTime(1)).build(); client.start();/*from w ww . ja va2s.co m*/ try { final BlockingQueue<String> queue = new LinkedBlockingQueue<String>(); Watcher watcher = new Watcher() { @Override public void process(WatchedEvent event) { try { queue.put(event.getPath()); } catch (InterruptedException e) { throw new Error(e); } } }; client.create().forPath("/base"); client.getChildren().usingWatcher(watcher).forPath("/base"); client.create().forPath("/base/child"); String path = queue.take(); Assert.assertEquals(path, "/base"); } finally { CloseableUtils.closeQuietly(client); } }
From source file:org.apache.curator.framework.imps.TestRemoveWatches.java
License:Apache License
@Test public void testRemoveCuratorWatch() throws Exception { Timing timing = new Timing(); CuratorFrameworkImpl client = (CuratorFrameworkImpl) CuratorFrameworkFactory.builder() .connectString(server.getConnectString()).retryPolicy(new RetryOneTime(1)).build(); try {/* w ww . jav a 2s.c o m*/ client.start(); final CountDownLatch removedLatch = new CountDownLatch(1); final String path = "/"; CuratorWatcher watcher = new CuratorWatcher() { @Override public void process(WatchedEvent event) throws Exception { if (event.getPath().equals(path) && event.getType() == EventType.DataWatchRemoved) { removedLatch.countDown(); } } }; client.checkExists().usingWatcher(watcher).forPath(path); client.watches().remove(watcher).forPath(path); Assert.assertTrue(timing.awaitLatch(removedLatch), "Timed out waiting for watch removal"); } finally { CloseableUtils.closeQuietly(client); } }
From source file:org.apache.curator.x.rpc.idl.structs.RpcCuratorEvent.java
License:Apache License
public RpcCuratorEvent(WatchedEvent event) { this.type = RpcCuratorEventType.WATCHED; this.resultCode = 0; this.path = event.getPath(); this.context = null; this.stat = null; this.data = null; this.name = null; this.children = null; this.aclList = null; this.watchedEvent = new RpcWatchedEvent(RpcKeeperState.valueOf(event.getState().name()), RpcEventType.valueOf(event.getType().name()), event.getPath()); this.leaderEvent = null; this.childrenCacheEvent = null; }
From source file:org.apache.curator.x.rpc.idl.structs.RpcWatchedEvent.java
License:Apache License
public RpcWatchedEvent(WatchedEvent watchedEvent) { keeperState = RpcKeeperState.valueOf(watchedEvent.getState().name()); eventType = RpcEventType.valueOf(watchedEvent.getType().name()); path = watchedEvent.getPath(); }
From source file:org.apache.distributedlog.impl.ZKLogSegmentMetadataStore.java
License:Apache License
/** * Process the watched events for registered listeners. *//* w ww. j a va 2s . c om*/ @Override public void process(WatchedEvent event) { if (Event.EventType.None == event.getType() && Event.KeeperState.Expired == event.getState()) { Set<String> keySet = new HashSet<String>(listeners.keySet()); for (String logSegmentsPath : keySet) { scheduleTask(logSegmentsPath, new ReadLogSegmentsTask(logSegmentsPath, this), 0L); } return; } String path = event.getPath(); if (null == path) { return; } switch (event.getType()) { case NodeDeleted: notifyLogStreamDeleted(path, listeners.remove(path)); break; case NodeChildrenChanged: new ReadLogSegmentsTask(path, this).run(); break; default: break; } }
From source file:org.apache.flume.agent.SmartAvroSource.java
License:Apache License
public void aaa(WatchedEvent event, String path) { String getpchangeDathPath = event.getPath(); String changeLogDataInfo = congZkUtils.readData(getpchangeDathPath); String zhuPath = getTargetPath(getpchangeDathPath, hostname); zhuZkUtils.writeData(zhuPath, changeLogDataInfo); monitorLocalNodeData(getpchangeDathPath, 1); }