public static String createNode(CuratorFramework zk, String path, byte[] data, CreateMode mode, List<ACL> acls) { String ret = null; try { String npath = normalizePath(path); ret = zk.create().creatingParentsIfNeeded().withMode(mode).withACL(acls).forPath(npath, data); } catch (Exception e) { throw Utils.wrapInRuntime(e); } return ret; }
private static void verifyAclStrict(CuratorFramework zk, List<ACL> strictAcl, String path, boolean fixUp) throws Exception { try { List<ACL> foundAcl = zk.getACL().forPath(path); if (!equivalent(foundAcl, strictAcl)) { if (fixUp) { LOG.warn("{} expected to have ACL {}, but has {}. Fixing...", path, strictAcl, foundAcl); zk.setACL().withACL(strictAcl).forPath(path); } else { throw new IllegalStateException(path + " did not have the correct ACL found " + foundAcl + " expected " + strictAcl); } } } catch (KeeperException.NoNodeException ne) { LOG.debug("{} removed in the middle of checking it", ne); } }
private static void verifyParentWithTopoChildrenDeleteDead(CuratorFramework zk, ACL superUserAcl, String path, Map<String, Id> topoToZkCreds, boolean fixUp, int perms) throws Exception { if (zk.checkExists().forPath(path) != null) { verifyAclStrict(zk, Arrays.asList(superUserAcl), path, fixUp); Set<String> possiblyBadIds = new HashSet<>(); for (String topoId : zk.getChildren().forPath(path)) { String childPath = path + ClusterUtils.ZK_SEPERATOR + topoId; if (!topoToZkCreds.containsKey(topoId)) { //Save it to try again later... possiblyBadIds.add(topoId); } else { List<ACL> rwAcl = getTopoAcl(path, topoId, topoToZkCreds, superUserAcl, fixUp, perms); verifyAclStrictRecursive(zk, rwAcl, childPath, fixUp); } } if (!possiblyBadIds.isEmpty()) { //Lets reread the children in STORMS as the source of truth and see if a new one was created in the background possiblyBadIds.removeAll(zk.getChildren().forPath(ClusterUtils.STORMS_SUBTREE)); for (String topoId : possiblyBadIds) { //Now we know for sure that this is a bad id String childPath = path + ClusterUtils.ZK_SEPERATOR + topoId; zk.delete().deletingChildrenIfNeeded().forPath(childPath); } } } }
public static Integer getVersion(CuratorFramework zk, String path, boolean watch) throws Exception { String npath = normalizePath(path); Stat stat = null; if (existsNode(zk, npath, watch)) { if (watch) { stat = zk.checkExists().watched().forPath(npath); } else { stat = zk.checkExists().forPath(npath); } } return stat == null ? null : Integer.valueOf(stat.getVersion()); }
public static List<String> getChildren(CuratorFramework zk, String path, boolean watch) { try { String npath = normalizePath(path); if (watch) { return zk.getChildren().watched().forPath(npath); } else { return zk.getChildren().forPath(npath); } } catch (Exception e) { throw Utils.wrapInRuntime(e); } }
public static byte[] getData(CuratorFramework zk, String path, boolean watch) { try { String npath = normalizePath(path); if (existsNode(zk, npath, watch)) { if (watch) { return zk.getData().watched().forPath(npath); } else { return zk.getData().forPath(npath); } } } catch (Exception e) { if (Utils.exceptionCauseIsInstanceOf(KeeperException.NoNodeException.class, e)) { // this is fine b/c we still have a watch from the successful exists call } else { throw Utils.wrapInRuntime(e); } } return null; }
@Test public void testNimbodesWithLatestVersionOfBlob() throws Exception { try (TestingServer server = new TestingServer(); CuratorFramework zkClient = CuratorFrameworkFactory .newClient(server.getConnectString(), new ExponentialBackoffRetry(1000, 3))) { zkClient.start(); // Creating nimbus hosts containing latest version of blob zkClient.create().creatingParentContainersIfNeeded().forPath("/blobstore/key1/nimbus1:7800-1"); zkClient.create().creatingParentContainersIfNeeded().forPath("/blobstore/key1/nimbus2:7800-2"); Set<NimbusInfo> set = BlobStoreUtils.getNimbodesWithLatestSequenceNumberOfBlob(zkClient, "key1"); assertEquals("Failed to get the correct nimbus hosts with latest blob version", (set.iterator().next()).getHost(), "nimbus2"); zkClient.delete().deletingChildrenIfNeeded().forPath("/blobstore/key1/nimbus1:7800-1"); zkClient.delete().deletingChildrenIfNeeded().forPath("/blobstore/key1/nimbus2:7800-2"); } }
public List<String> list(String path) { path = "/" + path; try { List<String> children; if (_curator.checkExists().forPath(path) == null) { children = new ArrayList<>(); } else { children = _curator.getChildren().forPath(path); } LOG.debug("List [path = {}], [children = {}]", path, children); return children; } catch (Exception e) { throw new RuntimeException(e); } }
protected static void createNode(CuratorFramework curator, String path, byte[] data, List<ACL> acls, CreateMode mode) throws Exception { ProtectACLCreateModePathAndBytesable<String> builder = curator.create().creatingParentsIfNeeded(); LOG.debug("Creating node [path = {}], [data = {}], [acls = {}], [mode = {}]", path, asString(data), acls, mode); if (acls == null) { if (mode == null) { TransactionalState.forPath(builder, path, data); } else { TransactionalState.forPath(builder.withMode(mode), path, data); } return; } TransactionalState.forPath(builder.withACL(acls), path, data); }
public static void deleteNode(CuratorFramework zk, String path) { try { String npath = normalizePath(path); if (existsNode(zk, npath, false)) { zk.delete().deletingChildrenIfNeeded().forPath(normalizePath(path)); } } catch (Exception e) { if (Utils.exceptionCauseIsInstanceOf(KeeperException.NodeExistsException.class, e)) { // do nothing LOG.info("delete {} failed.", path, e); } else { throw Utils.wrapInRuntime(e); } } }
private void incrementMaxSequenceNumber(CuratorFramework zkClient, int count) throws Exception { zkClient.setData().forPath(BLOBSTORE_MAX_KEY_SEQUENCE_SUBTREE + "/" + key, ByteBuffer.allocate(INT_CAPACITY).putInt(count + 1).array()); }
private int getMaxSequenceNumber(CuratorFramework zkClient) throws Exception { return ByteBuffer.wrap(zkClient.getData() .forPath(BLOBSTORE_MAX_KEY_SEQUENCE_SUBTREE + "/" + key)).getInt(); } }
private static void verifyAclStrictRecursive(CuratorFramework zk, List<ACL> strictAcl, String path, boolean fixUp) throws Exception { verifyAclStrict(zk, strictAcl, path, fixUp); for (String child : zk.getChildren().forPath(path)) { String newPath = path + ClusterUtils.ZK_SEPERATOR + child; verifyAclStrictRecursive(zk, strictAcl, newPath, fixUp); } }
public GetChildrenBuilder withGetChildren(String path, List<String> returnValue) { when(getChildrenBuilder.watched()).thenReturn(getChildrenBuilder); return mockForPath(getChildrenBuilder, path, returnValue); }
public void delete(String path) { path = "/" + path; try { _curator.delete().forPath(path); } catch (KeeperException.NoNodeException nne) { LOG.warn("Path {} already deleted."); } catch (Exception e) { throw new RuntimeException(e); } LOG.debug("Deleted [path = {}]", path); }
public static void syncPath(CuratorFramework zk, String path) { try { zk.sync().forPath(normalizePath(path)); } catch (Exception e) { throw Utils.wrapInRuntime(e); } }
public static boolean existsNode(CuratorFramework zk, String path, boolean watch) { Stat stat = null; try { if (watch) { stat = zk.checkExists().watched().forPath(normalizePath(path)); } else { stat = zk.checkExists().forPath(normalizePath(path)); } } catch (Exception e) { throw Utils.wrapInRuntime(e); } return stat != null; }
private static void verifyParentWithTopoChildren(CuratorFramework zk, ACL superUserAcl, String path, Map<String, Id> topoToZkCreds, boolean fixUp, int perms) throws Exception { if (zk.checkExists().forPath(path) != null) { verifyAclStrict(zk, Arrays.asList(superUserAcl), path, fixUp); for (String topoId : zk.getChildren().forPath(path)) { String childPath = path + ClusterUtils.ZK_SEPERATOR + topoId; List<ACL> rwAcl = getTopoAcl(path, topoId, topoToZkCreds, superUserAcl, fixUp, perms); verifyAclStrictRecursive(zk, rwAcl, childPath, fixUp); } } }
public static Stat setData(CuratorFramework zk, String path, byte[] data) { try { String npath = normalizePath(path); return zk.setData().forPath(npath, data); } catch (Exception e) { throw Utils.wrapInRuntime(e); } }