[24/50] [abbrv] hadoop git commit: HADOOP-14773. Extend ZKCuratorManager API for more reusability. (Íñigo Goiri via Subru).
HADOOP-14773. Extend ZKCuratorManager API for more reusability. (Ãñigo Goiri via Subru). Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/75dd866b Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/75dd866b Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/75dd866b Branch: refs/heads/HDFS-7240 Commit: 75dd866bfb8b63cb9f13179d4365b05c48e0907d Parents: f34646d Author: Subru Krishnan Authored: Tue Aug 15 16:53:59 2017 -0700 Committer: Subru Krishnan Committed: Tue Aug 15 16:53:59 2017 -0700 -- .../hadoop/util/curator/ZKCuratorManager.java | 54 ++-- .../util/curator/TestZKCuratorManager.java | 2 +- .../recovery/ZKRMStateStore.java| 19 +-- 3 files changed, 52 insertions(+), 23 deletions(-) -- http://git-wip-us.apache.org/repos/asf/hadoop/blob/75dd866b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/curator/ZKCuratorManager.java -- diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/curator/ZKCuratorManager.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/curator/ZKCuratorManager.java index 3adf028..9a031af 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/curator/ZKCuratorManager.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/curator/ZKCuratorManager.java @@ -33,9 +33,12 @@ import org.apache.hadoop.fs.CommonConfigurationKeys; import org.apache.hadoop.util.ZKUtil; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.base.Preconditions; + /** * Helper class that provides utility methods specific to ZK operations. */ @@ -179,7 +182,6 @@ public final class ZKCuratorManager { /** * Get the data in a ZNode. * @param path Path of the ZNode. - * @param stat Output statistics of the ZNode. * @return The data in the ZNode. * @throws Exception If it cannot contact Zookeeper. */ @@ -190,16 +192,38 @@ public final class ZKCuratorManager { /** * Get the data in a ZNode. * @param path Path of the ZNode. - * @param stat Output statistics of the ZNode. + * @param stat + * @return The data in the ZNode. + * @throws Exception If it cannot contact Zookeeper. + */ + public byte[] getData(final String path, Stat stat) throws Exception { +return curator.getData().storingStatIn(stat).forPath(path); + } + + /** + * Get the data in a ZNode. + * @param path Path of the ZNode. * @return The data in the ZNode. * @throws Exception If it cannot contact Zookeeper. */ - public String getSringData(final String path) throws Exception { + public String getStringData(final String path) throws Exception { byte[] bytes = getData(path); return new String(bytes, Charset.forName("UTF-8")); } /** + * Get the data in a ZNode. + * @param path Path of the ZNode. + * @param stat Output statistics of the ZNode. + * @return The data in the ZNode. + * @throws Exception If it cannot contact Zookeeper. + */ + public String getStringData(final String path, Stat stat) throws Exception { +byte[] bytes = getData(path, stat); +return new String(bytes, Charset.forName("UTF-8")); + } + + /** * Set data into a ZNode. * @param path Path of the ZNode. * @param data Data to set. @@ -272,14 +296,36 @@ public final class ZKCuratorManager { } /** + * Utility function to ensure that the configured base znode exists. + * This recursively creates the znode as well as all of its parents. + * @param path Path of the znode to create. + * @throws Exception If it cannot create the file. + */ + public void createRootDirRecursively(String path) throws Exception { +String[] pathParts = path.split("/"); +Preconditions.checkArgument( +pathParts.length >= 1 && pathParts[0].isEmpty(), +"Invalid path: %s", path); +StringBuilder sb = new StringBuilder(); + +for (int i = 1; i < pathParts.length; i++) { + sb.append("/").append(pathParts[i]); + create(sb.toString()); +} + } + + /** * Delete a ZNode. * @param path Path of the ZNode. + * @return If the znode was deleted. * @throws Exception If it cannot contact ZooKeeper. */ - public void delete(final String path) throws Exception { + public boolean delete(final String path) throws Exception { if (exists(path)) { curator.delete().deletingChildrenIfNeeded().forPath(path); + return true; } +return false; } /** http://git-wip-us.apache.org/repos/
[24/50] [abbrv] hadoop git commit: HADOOP-14773. Extend ZKCuratorManager API for more reusability. (Íñigo Goiri via Subru).
HADOOP-14773. Extend ZKCuratorManager API for more reusability. (Ãñigo Goiri via Subru). Project: http://git-wip-us.apache.org/repos/asf/hadoop/repo Commit: http://git-wip-us.apache.org/repos/asf/hadoop/commit/75dd866b Tree: http://git-wip-us.apache.org/repos/asf/hadoop/tree/75dd866b Diff: http://git-wip-us.apache.org/repos/asf/hadoop/diff/75dd866b Branch: refs/heads/YARN-3926 Commit: 75dd866bfb8b63cb9f13179d4365b05c48e0907d Parents: f34646d Author: Subru Krishnan Authored: Tue Aug 15 16:53:59 2017 -0700 Committer: Subru Krishnan Committed: Tue Aug 15 16:53:59 2017 -0700 -- .../hadoop/util/curator/ZKCuratorManager.java | 54 ++-- .../util/curator/TestZKCuratorManager.java | 2 +- .../recovery/ZKRMStateStore.java| 19 +-- 3 files changed, 52 insertions(+), 23 deletions(-) -- http://git-wip-us.apache.org/repos/asf/hadoop/blob/75dd866b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/curator/ZKCuratorManager.java -- diff --git a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/curator/ZKCuratorManager.java b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/curator/ZKCuratorManager.java index 3adf028..9a031af 100644 --- a/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/curator/ZKCuratorManager.java +++ b/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/util/curator/ZKCuratorManager.java @@ -33,9 +33,12 @@ import org.apache.hadoop.fs.CommonConfigurationKeys; import org.apache.hadoop.util.ZKUtil; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.data.ACL; +import org.apache.zookeeper.data.Stat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import com.google.common.base.Preconditions; + /** * Helper class that provides utility methods specific to ZK operations. */ @@ -179,7 +182,6 @@ public final class ZKCuratorManager { /** * Get the data in a ZNode. * @param path Path of the ZNode. - * @param stat Output statistics of the ZNode. * @return The data in the ZNode. * @throws Exception If it cannot contact Zookeeper. */ @@ -190,16 +192,38 @@ public final class ZKCuratorManager { /** * Get the data in a ZNode. * @param path Path of the ZNode. - * @param stat Output statistics of the ZNode. + * @param stat + * @return The data in the ZNode. + * @throws Exception If it cannot contact Zookeeper. + */ + public byte[] getData(final String path, Stat stat) throws Exception { +return curator.getData().storingStatIn(stat).forPath(path); + } + + /** + * Get the data in a ZNode. + * @param path Path of the ZNode. * @return The data in the ZNode. * @throws Exception If it cannot contact Zookeeper. */ - public String getSringData(final String path) throws Exception { + public String getStringData(final String path) throws Exception { byte[] bytes = getData(path); return new String(bytes, Charset.forName("UTF-8")); } /** + * Get the data in a ZNode. + * @param path Path of the ZNode. + * @param stat Output statistics of the ZNode. + * @return The data in the ZNode. + * @throws Exception If it cannot contact Zookeeper. + */ + public String getStringData(final String path, Stat stat) throws Exception { +byte[] bytes = getData(path, stat); +return new String(bytes, Charset.forName("UTF-8")); + } + + /** * Set data into a ZNode. * @param path Path of the ZNode. * @param data Data to set. @@ -272,14 +296,36 @@ public final class ZKCuratorManager { } /** + * Utility function to ensure that the configured base znode exists. + * This recursively creates the znode as well as all of its parents. + * @param path Path of the znode to create. + * @throws Exception If it cannot create the file. + */ + public void createRootDirRecursively(String path) throws Exception { +String[] pathParts = path.split("/"); +Preconditions.checkArgument( +pathParts.length >= 1 && pathParts[0].isEmpty(), +"Invalid path: %s", path); +StringBuilder sb = new StringBuilder(); + +for (int i = 1; i < pathParts.length; i++) { + sb.append("/").append(pathParts[i]); + create(sb.toString()); +} + } + + /** * Delete a ZNode. * @param path Path of the ZNode. + * @return If the znode was deleted. * @throws Exception If it cannot contact ZooKeeper. */ - public void delete(final String path) throws Exception { + public boolean delete(final String path) throws Exception { if (exists(path)) { curator.delete().deletingChildrenIfNeeded().forPath(path); + return true; } +return false; } /** http://git-wip-us.apache.org/repos/