private void FindPrefixInChildren(String prefix, IZooKeeper zookeeper, String dir) { var names = Zookeeper.GetChildren(dir, false); foreach (string name in names) { if (name.StartsWith(prefix)) { id = name; if (LOG.IsDebugEnabled) { LOG.DebugFormat("Found id created last time: {0}", id); } break; } } if (id == null) { id = zookeeper.Create(dir.Combine(prefix), data, Acl, CreateMode.EphemeralSequential); if (LOG.IsDebugEnabled) { LOG.DebugFormat("Created id: {0}", id); } } }
/** * Make sure all the nodes in the path are created. NOTE: Unlike File.mkdirs(), Zookeeper doesn't distinguish * between directories and files. So, every node in the path is created. The data for each node is an empty blob * * @param zookeeper the client * @param path path to ensure * @param makeLastNode if true, all nodes are created. If false, only the parent nodes are created * @throws InterruptedException thread interruption * @throws org.apache.zookeeper.KeeperException * Zookeeper errors */ public static void Mkdirs(IZooKeeper zookeeper, String path, bool makeLastNode) { PathUtils.ValidatePath(path); var pos = 1; // skip first slash, root is guaranteed to exist do { pos = path.IndexOf(PathUtils.PathSeparatorChar, pos + 1); if (pos == -1) { if (makeLastNode) { pos = path.Length; } else { break; } } var subPath = path.Substring(0, pos); if (zookeeper.Exists(subPath, false) == null) { try { zookeeper.Create(subPath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } catch (KeeperException.NodeExistsException e) { // ignore... someone else has created it since we checked } } }while (pos < path.Length); }
/** * Make sure all the nodes in the path are created. NOTE: Unlike File.mkdirs(), Zookeeper doesn't distinguish * between directories and files. So, every node in the path is created. The data for each node is an empty blob * * @param zookeeper the client * @param path path to ensure * @param makeLastNode if true, all nodes are created. If false, only the parent nodes are created * @throws InterruptedException thread interruption * @throws org.apache.zookeeper.KeeperException * Zookeeper errors */ public static void Mkdirs(IZooKeeper zookeeper, String path, bool makeLastNode) { PathUtils.ValidatePath(path); var pos = 1; // skip first slash, root is guaranteed to exist do { pos = path.IndexOf(PathUtils.PathSeparatorChar, pos + 1); if (pos == -1) { if (makeLastNode) { pos = path.Length; } else { break; } } var subPath = path.Substring(0, pos); if (zookeeper.Exists(subPath, false) == null) { try { zookeeper.Create(subPath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateMode.Persistent); } catch (KeeperException.NodeExistsException e) { // ignore... someone else has created it since we checked } } } while (pos < path.Length); }