org.apache.zookeeper.data.Stat Java Examples
The following examples show how to use
org.apache.zookeeper.data.Stat.
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SetACLBuilderImpl.java From xian with Apache License 2.0 | 6 votes |
@Override public Stat forPath(String path) throws Exception { path = client.fixForNamespace(path); Stat resultStat = null; if ( backgrounding.inBackground() ) { client.processBackgroundOperation(new OperationAndData<String>(this, path, backgrounding.getCallback(), null, backgrounding.getContext()), null); } else { resultStat = pathInForeground(path); } return resultStat; }
Example #2
Source File: TaskManager.java From Taroco-Scheduler with GNU General Public License v2.0 | 6 votes |
/** * 分配指定任务给指定server * * @param taskName 任务名称 * @param serverId server节点 */ private void assignTask2Server(final String taskName, final String serverId) { final String taskPath = zkClient.getTaskPath() + "/" + taskName; try { final List<String> taskServerIds = zkClient.getClient().getChildren().forPath(taskPath); if (!CollectionUtils.isEmpty(taskServerIds)) { // 任务已分配, 删除分配信息 for (String taskServerId : taskServerIds) { zkClient.getClient().delete().deletingChildrenIfNeeded() .forPath(taskPath + "/" + taskServerId); } } final String runningInfo = "0:" + System.currentTimeMillis(); final String path = taskPath + "/" + serverId; final Stat stat = zkClient.getClient().checkExists().forPath(path); if (stat == null) { zkClient.getClient() .create() .withMode(CreateMode.EPHEMERAL) .forPath(path, runningInfo.getBytes()); } log.info("成功分配任务 [" + taskPath + "]" + " 给 server [" + serverId + "]"); } catch (Exception e) { log.error("assignTask2Server failed: taskName={}, serverId={}", taskName, serverId, e); } }
Example #3
Source File: SharedCacheCoordinator.java From datawave with Apache License 2.0 | 6 votes |
/** * Sends an eviction message for {@code messagePath} to all other shared cache coordinators that are listening. */ public void sendEvictMessage(String messagePath) throws Exception { ArgumentChecker.notNull(messagePath); String rootPath = ZKPaths.makePath("/", "evictions"); String evictMessagePath = ZKPaths.makePath(rootPath, ZKPaths.makePath(messagePath, localName)); Stat nodeData = curatorClient.checkExists().forPath(evictMessagePath); boolean shouldCreate = true; if (nodeData != null) { long delta = System.currentTimeMillis() - nodeData.getCtime(); if (delta > EVICT_MESSAGE_TIMEOUT) { log.debug("Attempting to delete " + evictMessagePath + " since it was created " + delta + "ms ago and hasn't been cleaned up."); ZKUtil.deleteRecursive(curatorClient.getZookeeperClient().getZooKeeper(), evictMessagePath); } else { shouldCreate = false; } } if (shouldCreate) curatorClient.create().creatingParentsIfNeeded().forPath(evictMessagePath); }
Example #4
Source File: ZkHelper.java From AthenaServing with Apache License 2.0 | 6 votes |
/** * 增加或者更新节点数据 * * @param path * @param data * @return */ public boolean addOrUpdatePersistentNode(String path, byte[] data) { boolean flag = false; if (checkExists(path)) { Stat stat = update(path, data); if (stat != null) { flag = true; } } else { String result = addPersistent(path, data); if (null != result && result.length() > 0) { flag = true; } } return flag; }
Example #5
Source File: ZKSubscriptionsStore.java From distributedlog with Apache License 2.0 | 6 votes |
@Override public CompletableFuture<Map<String, DLSN>> getLastCommitPositions() { final CompletableFuture<Map<String, DLSN>> result = new CompletableFuture<Map<String, DLSN>>(); try { this.zkc.get().getChildren(this.zkPath, false, new AsyncCallback.Children2Callback() { @Override public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) { if (KeeperException.Code.NONODE.intValue() == rc) { result.complete(new HashMap<String, DLSN>()); } else if (KeeperException.Code.OK.intValue() != rc) { result.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc), path)); } else { getLastCommitPositions(result, children); } } }, null); } catch (ZooKeeperClient.ZooKeeperConnectionException zkce) { result.completeExceptionally(zkce); } catch (InterruptedException ie) { result.completeExceptionally(new DLInterruptedException("getLastCommitPositions was interrupted", ie)); } return result; }
Example #6
Source File: ZooKeeperService.java From blog with MIT License | 6 votes |
String createNode(String node, boolean ephemeral) { String createdNodePath; try { final Stat nodeStat = zooKeeper.exists(node, false); if (nodeStat == null) { createdNodePath = zooKeeper.create(node, new byte[0], Ids.OPEN_ACL_UNSAFE, (ephemeral ? CreateMode.EPHEMERAL_SEQUENTIAL : CreateMode.PERSISTENT)); } else { createdNodePath = node; } } catch (KeeperException | InterruptedException e) { throw new IllegalStateException(e); } return createdNodePath; }
Example #7
Source File: ActiveStandbyElector.java From big-c with Apache License 2.0 | 6 votes |
/** * Write the "ActiveBreadCrumb" node, indicating that this node may need * to be fenced on failover. * @param oldBreadcrumbStat */ private void writeBreadCrumbNode(Stat oldBreadcrumbStat) throws KeeperException, InterruptedException { Preconditions.checkState(appData != null, "no appdata"); LOG.info("Writing znode " + zkBreadCrumbPath + " to indicate that the local node is the most recent active..."); if (oldBreadcrumbStat == null) { // No previous active, just create the node createWithRetries(zkBreadCrumbPath, appData, zkAcl, CreateMode.PERSISTENT); } else { // There was a previous active, update the node setDataWithRetries(zkBreadCrumbPath, appData, oldBreadcrumbStat.getVersion()); } }
Example #8
Source File: ZkUtils.java From GoPush with GNU General Public License v2.0 | 6 votes |
/** * 设置子节点更改监听 * * @param path * @throws Exception */ public boolean listenerPathChildrenCache(String path, BiConsumer<CuratorFramework, PathChildrenCacheEvent> biConsumer) { if (!ObjectUtils.allNotNull(zkClient, path, biConsumer)) { return Boolean.FALSE; } try { Stat stat = exists(path); if (stat != null) { PathChildrenCache watcher = new PathChildrenCache(zkClient, path, true); watcher.start(PathChildrenCache.StartMode.POST_INITIALIZED_EVENT); //该模式下 watcher在重连的时候会自动 rebuild 否则需要重新rebuild watcher.getListenable().addListener(biConsumer::accept, pool); if (!pathChildrenCaches.contains(watcher)) { pathChildrenCaches.add(watcher); } // else{ // watcher.rebuild(); // } return Boolean.TRUE; } } catch (Exception e) { log.error("listen path children cache fail! path:{} , error:{}", path, e); } return Boolean.FALSE; }
Example #9
Source File: ZookeeperVerticle.java From examples with Apache License 2.0 | 6 votes |
private DataCallback getVersionCallback() { return new DataCallback() { @Override public void processResult(int rc, String path, Object ctx, byte[] rawData, Stat s) { ZookeeperVerticle zv = (ZookeeperVerticle) ctx; int version = -1; synchronized (zv.configVersion) { version = zv.configVersion.get(); } int fetchedVersion = new Integer(new String(rawData)).intValue(); if (fetchedVersion > version) { synchronized (zv.configVersion) { zv.configVersion.set(fetchedVersion); } zv.zk.getData(Constants.CONFIGURATION_PATH + "/" + fetchedVersion, false, getVersionDataCallback(), zv); } } }; }
Example #10
Source File: zkDumpZookeeper.java From zkTreeUtil with Apache License 2.0 | 6 votes |
private void dumpChild(String znodeParent, String znode, TreeNode<zNode> tree_node) throws Exception { String znodePath = (znodeParent.equals("/") ? "" : znodeParent) + "/" + znode; List<String> children = zk.getChildren(znodePath, false); Stat stat = new Stat(); byte[] data = zk.getData(znodePath, false, stat); zNode z = new zNode(znode, znodePath, data, stat, !children.isEmpty()); TreeNode<zNode> tnode; if (tree_node != null) { tnode = tree_node.addChild(z); } else { zktree = new TreeNode<>(z); tnode = zktree; } logger.debug("read znode path: " + znodePath); for (String c : children) { dumpChild(znodePath, c, tnode); } }
Example #11
Source File: ZkController.java From lucene-solr with Apache License 2.0 | 6 votes |
public void checkOverseerDesignate() { try { byte[] data = zkClient.getData(ZkStateReader.ROLES, null, new Stat(), true); if (data == null) return; @SuppressWarnings({"rawtypes"}) Map roles = (Map) Utils.fromJSON(data); if (roles == null) return; @SuppressWarnings({"rawtypes"}) List nodeList = (List) roles.get("overseer"); if (nodeList == null) return; if (nodeList.contains(getNodeName())) { ZkNodeProps props = new ZkNodeProps(Overseer.QUEUE_OPERATION, CollectionParams.CollectionAction.ADDROLE.toString().toLowerCase(Locale.ROOT), "node", getNodeName(), "role", "overseer"); log.info("Going to add role {} ", props); getOverseerCollectionQueue().offer(Utils.toJSON(props)); } } catch (NoNodeException nne) { return; } catch (Exception e) { log.warn("could not read the overseer designate ", e); } }
Example #12
Source File: PersistentEphemeralNodeTest.java From curator-extensions with Apache License 2.0 | 6 votes |
@Test public void testRecreatesNodeWhenSessionReconnects() throws Exception { PersistentEphemeralNode node = createNode(PATH); assertNodeExists(_curator, node.getActualPath()); WatchTrigger deletedWatchTrigger = WatchTrigger.deletionTrigger(); _curator.checkExists().usingWatcher(deletedWatchTrigger).forPath(node.getActualPath()); killSession(node.getCurator()); // Make sure the node got deleted... assertTrue(deletedWatchTrigger.firedWithin(10, TimeUnit.SECONDS)); // Check for it to be recreated... WatchTrigger createdWatchTrigger = WatchTrigger.creationTrigger(); Stat stat = _curator.checkExists().usingWatcher(createdWatchTrigger).forPath(node.getActualPath()); assertTrue(stat != null || createdWatchTrigger.firedWithin(10, TimeUnit.SECONDS)); }
Example #13
Source File: RecoverableZooKeeper.java From hbase with Apache License 2.0 | 6 votes |
/** * getAcl is an idempotent operation. Retry before throwing exception * @return list of ACLs */ public List<ACL> getAcl(String path, Stat stat) throws KeeperException, InterruptedException { try (TraceScope scope = TraceUtil.createTrace("RecoverableZookeeper.getAcl")) { RetryCounter retryCounter = retryCounterFactory.create(); while (true) { try { return checkZk().getACL(path, stat); } catch (KeeperException e) { switch (e.code()) { case CONNECTIONLOSS: retryOrThrow(retryCounter, e, "getAcl"); break; case OPERATIONTIMEOUT: retryOrThrow(retryCounter, e, "getAcl"); break; default: throw e; } } retryCounter.sleepUntilNextRetry(); } } }
Example #14
Source File: ZookeeperCoordinatorRepository.java From hmily with Apache License 2.0 | 6 votes |
@Override public int updateStatus(final String id, final Integer status) { final String path = RepositoryPathUtils.buildZookeeperRootPath(rootPathPrefix, id); try { byte[] content = zooKeeper.getData(path, false, new Stat()); if (content != null) { final CoordinatorRepositoryAdapter adapter = objectSerializer.deSerialize(content, CoordinatorRepositoryAdapter.class); adapter.setStatus(status); zooKeeper.setData(path, objectSerializer.serialize(adapter), -1); } return ROWS; } catch (Exception e) { e.printStackTrace(); return FAIL_ROWS; } }
Example #15
Source File: Writer.java From zkcopy with Apache License 2.0 | 6 votes |
/** * Updates or creates the given node. * * @param node * The node to copy * @throws KeeperException * If the server signals an error * @throws InterruptedException * If the server transaction is interrupted */ private void upsertNode(Node node) throws KeeperException, InterruptedException { String nodePath = node.getAbsolutePath(); // 1. Update or create current node Stat stat = zk.exists(nodePath, false); if (stat != null) { logger.debug("Attempting to update " + nodePath); transaction.setData(nodePath, node.getData(), -1); nodesUpdated++; } else { logger.debug("Attempting to create " + nodePath); transaction.create(nodePath, node.getData(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); nodesCreated++; } if (nodesUpdated % 100 == 0) { logger.debug(String.format("Updated: %s, current node mtime %s", nodesUpdated, node.getMtime())); } }
Example #16
Source File: ExecutePlanActionTest.java From lucene-solr with Apache License 2.0 | 6 votes |
@Before public void setUp() throws Exception { super.setUp(); configureCluster(NODE_COUNT) .addConfig("conf", configset("cloud-minimal")) .configure(); // clear any persisted auto scaling configuration Stat stat = zkClient().setData(SOLR_AUTOSCALING_CONF_PATH, Utils.toJSON(new ZkNodeProps()), true); cloudManager = cluster.getJettySolrRunner(0).getCoreContainer().getZkController().getSolrCloudManager(); finishedProcessing = new CountDownLatch(1); startedProcessing = new CountDownLatch(1); }
Example #17
Source File: SetACLBuilderImpl.java From xian with Apache License 2.0 | 6 votes |
private Stat pathInForeground(final String path) throws Exception { OperationTrace trace = client.getZookeeperClient().startAdvancedTracer("SetACLBuilderImpl-Foreground"); Stat resultStat = RetryLoop.callWithRetry ( client.getZookeeperClient(), new Callable<Stat>() { @Override public Stat call() throws Exception { return client.getZooKeeper().setACL(path, acling.getAclList(path), version); } } ); trace.setPath(path).setStat(resultStat).commit(); return resultStat; }
Example #18
Source File: TestActiveStandbyElector.java From big-c with Apache License 2.0 | 5 votes |
/** * verify error in exists() callback results in fatal error */ @Test public void testStatNodeError() { elector.joinElection(data); elector.processResult(Code.RUNTIMEINCONSISTENCY.intValue(), ZK_LOCK_NAME, mockZK, (Stat) null); Mockito.verify(mockApp, Mockito.times(0)).enterNeutralMode(); Mockito.verify(mockApp, Mockito.times(1)).notifyFatalError( "Received stat error from Zookeeper. code:RUNTIMEINCONSISTENCY"); }
Example #19
Source File: MockHelixPropertyStore.java From ambry with Apache License 2.0 | 5 votes |
/** * {@inheritDoc} * * This is not thread safe if some thread is setting {@link #readLatch}. */ @Override public T get(String path, Stat stat, int options) { if (readLatch != null) { readLatch.countDown(); } T result = pathToRecords.get(path); if (result != null && stat != null) { Stat resultStat = pathToStats.get(path); DataTree.copyStat(resultStat, stat); } return result; }
Example #20
Source File: TestZooKeeperACL.java From hbase with Apache License 2.0 | 5 votes |
/** * Finally, we check the ACLs of a node outside of the /hbase hierarchy and * verify that its ACL is simply 'hbase:Perms.ALL'. */ @Test public void testOutsideHBaseNodeACL() throws Exception { if (!secureZKAvailable) { return; } ZKUtil.createWithParents(zkw, "/testACLNode"); List<ACL> acls = zkw.getRecoverableZooKeeper().getZooKeeper() .getACL("/testACLNode", new Stat()); assertEquals(1, acls.size()); assertEquals("sasl", acls.get(0).getId().getScheme()); assertEquals("hbase", acls.get(0).getId().getId()); assertEquals(ZooDefs.Perms.ALL, acls.get(0).getPerms()); }
Example #21
Source File: ZkInstance.java From libevent with Apache License 2.0 | 5 votes |
private boolean checkNode(String clientId, String resource) throws KeeperException, InterruptedException { try { Stat stat = new Stat(); byte[] data = zk.getData(resource, false, stat); return clientId.equals(new String(data, Charset.forName("utf-8"))); } catch(KeeperException e){ if (e.code().equals(KeeperException.Code.NONODE)) { return this.tryLock(clientId, resource); } else if (e.code().equals(KeeperException.Code.CONNECTIONLOSS)) { return this.checkNode(clientId, resource); } else { throw e; } } }
Example #22
Source File: OverseerTaskProcessor.java From lucene-solr with Apache License 2.0 | 5 votes |
public static String getLeaderId(SolrZkClient zkClient) throws KeeperException,InterruptedException{ byte[] data = null; try { data = zkClient.getData(Overseer.OVERSEER_ELECT + "/leader", null, new Stat(), true); } catch (KeeperException.NoNodeException e) { return null; } @SuppressWarnings({"rawtypes"}) Map m = (Map) Utils.fromJSON(data); return (String) m.get(ID); }
Example #23
Source File: GlobalConfigServiceImplTest.java From metron with Apache License 2.0 | 5 votes |
@Test public void saveShouldReturnSameConfigThatIsPassedOnSuccessfulSave() throws Exception { SetDataBuilder setDataBuilder = mock(SetDataBuilder.class); when(setDataBuilder.forPath(ConfigurationType.GLOBAL.getZookeeperRoot(), "{ }".getBytes(StandardCharsets.UTF_8))).thenReturn(new Stat()); when(curatorFramework.setData()).thenReturn(setDataBuilder); assertEquals(new HashMap<>(), globalConfigService.save(new HashMap<>())); verify(setDataBuilder).forPath(eq(ConfigurationType.GLOBAL.getZookeeperRoot()), eq("{ }".getBytes(StandardCharsets.UTF_8))); }
Example #24
Source File: ZkClient.java From TakinRPC with Apache License 2.0 | 5 votes |
public Stat writeData(final String path, final byte[] data, final int expectedVersion) { return retryUntilConnected(new Callable<Stat>() { @Override public Stat call() throws Exception { return _connection.writeData(path, data, expectedVersion); } }); }
Example #25
Source File: TestLedgerAllocator.java From distributedlog with Apache License 2.0 | 5 votes |
@Test(timeout = 60000) public void testBadVersionOnTwoAllocators() throws Exception { String allocationPath = "/allocation-bad-version"; zkc.get().create(allocationPath, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); Stat stat = new Stat(); byte[] data = zkc.get().getData(allocationPath, false, stat); Versioned<byte[]> allocationData = new Versioned<byte[]>(data, new ZkVersion(stat.getVersion())); SimpleLedgerAllocator allocator1 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc); SimpleLedgerAllocator allocator2 = new SimpleLedgerAllocator(allocationPath, allocationData, newQuorumConfigProvider(dlConf), zkc, bkc); allocator1.allocate(); // wait until allocated ZKTransaction txn1 = newTxn(); LedgerHandle lh = FutureUtils.result(allocator1.tryObtain(txn1, NULL_LISTENER)); allocator2.allocate(); ZKTransaction txn2 = newTxn(); try { FutureUtils.result(allocator2.tryObtain(txn2, NULL_LISTENER)); fail("Should fail allocating on second allocator as allocator1 is starting allocating something."); } catch (ZKException zke) { assertEquals(KeeperException.Code.BADVERSION, zke.getKeeperExceptionCode()); } FutureUtils.result(txn1.execute()); Utils.close(allocator1); Utils.close(allocator2); long eid = lh.addEntry("hello world".getBytes()); lh.close(); LedgerHandle readLh = bkc.get().openLedger(lh.getId(), BookKeeper.DigestType.CRC32, dlConf.getBKDigestPW().getBytes()); Enumeration<LedgerEntry> entries = readLh.readEntries(eid, eid); int i = 0; while (entries.hasMoreElements()) { LedgerEntry entry = entries.nextElement(); assertEquals("hello world", new String(entry.getEntry(), UTF_8)); ++i; } assertEquals(1, i); }
Example #26
Source File: ZKRMStateStore.java From big-c with Apache License 2.0 | 5 votes |
private Stat existsWithRetries( final String path, final boolean watch) throws Exception { return new ZKAction<Stat>() { @Override Stat run() throws KeeperException, InterruptedException { return zkClient.exists(path, watch); } }.runWithRetries(); }
Example #27
Source File: TestZooKeeperConnection.java From util with Apache License 2.0 | 5 votes |
@Test public void testCreateIfNotExists() throws IOException, InterruptedException, KeeperException { ZooKeeperConnection zkc = new ZooKeeperConnection(zk.getZkNodes(), 30000); zkc.connect(); assertNull(zkc.exists("/zkc", false)); assertTrue(ZooKeeperConnection.createIfNotExists(zkc, "/zkc", new byte[]{1, 2, 3}, CreateMode.PERSISTENT)); assertFalse(ZooKeeperConnection.createIfNotExists(zkc, "/zkc", new byte[]{4, 5, 6}, CreateMode.PERSISTENT)); assertArrayEquals(new byte[]{1, 2, 3}, zkc.getData("/zkc", false, new Stat())); zkc.close(); }
Example #28
Source File: ActiveStandbyElector.java From big-c with Apache License 2.0 | 5 votes |
/** * If there is a breadcrumb node indicating that another node may need * fencing, try to fence that node. * @return the Stat of the breadcrumb node that was read, or null * if no breadcrumb node existed */ private Stat fenceOldActive() throws InterruptedException, KeeperException { final Stat stat = new Stat(); byte[] data; LOG.info("Checking for any old active which needs to be fenced..."); try { data = zkDoWithRetries(new ZKAction<byte[]>() { @Override public byte[] run() throws KeeperException, InterruptedException { return zkClient.getData(zkBreadCrumbPath, false, stat); } }); } catch (KeeperException ke) { if (isNodeDoesNotExist(ke.code())) { LOG.info("No old node to fence"); return null; } // If we failed to read for any other reason, then likely we lost // our session, or we don't have permissions, etc. In any case, // we probably shouldn't become active, and failing the whole // thing is the best bet. throw ke; } LOG.info("Old node exists: " + StringUtils.byteToHexString(data)); if (Arrays.equals(data, appData)) { LOG.info("But old node has our own data, so don't need to fence it."); } else { appClient.fenceOldActive(data); } return stat; }
Example #29
Source File: FlowRuleZookeeperPublisher.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Override public void publish(String app, List<FlowRuleEntity> rules) throws Exception { AssertUtil.notEmpty(app, "app name cannot be empty"); String path = ZookeeperConfigUtil.getPath(app); Stat stat = zkClient.checkExists().forPath(path); if (stat == null) { zkClient.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).forPath(path, null); } byte[] data = CollectionUtils.isEmpty(rules) ? "[]".getBytes() : converter.convert(rules).getBytes(); zkClient.setData().forPath(path, data); }
Example #30
Source File: MasterBasedDistributedLayoutFactoryTest.java From incubator-retired-blur with Apache License 2.0 | 5 votes |
private void rmr(ZooKeeper zooKeeper, String storagePath) throws KeeperException, InterruptedException { Stat stat = zooKeeper.exists(storagePath, false); if (stat == null) { return; } List<String> children = zooKeeper.getChildren(storagePath, false); for (String s : children) { rmr(zooKeeper, storagePath + "/" + s); } zooKeeper.delete(storagePath, -1); }