HADOOP-14773. Extend ZKCuratorManager API for more reusability. (Íñigo Goiri via Subru).
(cherry picked from commit 75dd866bfb8b63cb9f13179d4365b05c48e0907d)
This commit is contained in:
parent
895a35eac0
commit
ad46a90872
@ -33,9 +33,12 @@
|
|||||||
import org.apache.hadoop.util.ZKUtil;
|
import org.apache.hadoop.util.ZKUtil;
|
||||||
import org.apache.zookeeper.CreateMode;
|
import org.apache.zookeeper.CreateMode;
|
||||||
import org.apache.zookeeper.data.ACL;
|
import org.apache.zookeeper.data.ACL;
|
||||||
|
import org.apache.zookeeper.data.Stat;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
import com.google.common.base.Preconditions;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Helper class that provides utility methods specific to ZK operations.
|
* Helper class that provides utility methods specific to ZK operations.
|
||||||
*/
|
*/
|
||||||
@ -179,7 +182,6 @@ public List<ACL> getACL(final String path) throws Exception {
|
|||||||
/**
|
/**
|
||||||
* Get the data in a ZNode.
|
* Get the data in a ZNode.
|
||||||
* @param path Path of the ZNode.
|
* @param path Path of the ZNode.
|
||||||
* @param stat Output statistics of the ZNode.
|
|
||||||
* @return The data in the ZNode.
|
* @return The data in the ZNode.
|
||||||
* @throws Exception If it cannot contact Zookeeper.
|
* @throws Exception If it cannot contact Zookeeper.
|
||||||
*/
|
*/
|
||||||
@ -187,6 +189,28 @@ public byte[] getData(final String path) throws Exception {
|
|||||||
return curator.getData().forPath(path);
|
return curator.getData().forPath(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the data in a ZNode.
|
||||||
|
* @param path Path 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 getStringData(final String path) throws Exception {
|
||||||
|
byte[] bytes = getData(path);
|
||||||
|
return new String(bytes, Charset.forName("UTF-8"));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the data in a ZNode.
|
* Get the data in a ZNode.
|
||||||
* @param path Path of the ZNode.
|
* @param path Path of the ZNode.
|
||||||
@ -194,8 +218,8 @@ public byte[] getData(final String path) throws Exception {
|
|||||||
* @return The data in the ZNode.
|
* @return The data in the ZNode.
|
||||||
* @throws Exception If it cannot contact Zookeeper.
|
* @throws Exception If it cannot contact Zookeeper.
|
||||||
*/
|
*/
|
||||||
public String getSringData(final String path) throws Exception {
|
public String getStringData(final String path, Stat stat) throws Exception {
|
||||||
byte[] bytes = getData(path);
|
byte[] bytes = getData(path, stat);
|
||||||
return new String(bytes, Charset.forName("UTF-8"));
|
return new String(bytes, Charset.forName("UTF-8"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -271,15 +295,37 @@ public boolean create(final String path, List<ACL> zkAcl) throws Exception {
|
|||||||
return created;
|
return created;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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.
|
* Delete a ZNode.
|
||||||
* @param path Path of the ZNode.
|
* @param path Path of the ZNode.
|
||||||
|
* @return If the znode was deleted.
|
||||||
* @throws Exception If it cannot contact ZooKeeper.
|
* @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)) {
|
if (exists(path)) {
|
||||||
curator.delete().deletingChildrenIfNeeded().forPath(path);
|
curator.delete().deletingChildrenIfNeeded().forPath(path);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -67,7 +67,7 @@ public void testReadWriteData() throws Exception {
|
|||||||
curator.create(testZNode);
|
curator.create(testZNode);
|
||||||
assertTrue(curator.exists(testZNode));
|
assertTrue(curator.exists(testZNode));
|
||||||
curator.setData(testZNode, expectedString, -1);
|
curator.setData(testZNode, expectedString, -1);
|
||||||
String testString = curator.getSringData("/test");
|
String testString = curator.getStringData("/test");
|
||||||
assertEquals(expectedString, testString);
|
assertEquals(expectedString, testString);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -19,7 +19,6 @@
|
|||||||
package org.apache.hadoop.yarn.server.resourcemanager.recovery;
|
package org.apache.hadoop.yarn.server.resourcemanager.recovery;
|
||||||
|
|
||||||
import com.google.common.annotations.VisibleForTesting;
|
import com.google.common.annotations.VisibleForTesting;
|
||||||
import com.google.common.base.Preconditions;
|
|
||||||
import org.apache.commons.logging.Log;
|
import org.apache.commons.logging.Log;
|
||||||
import org.apache.commons.logging.LogFactory;
|
import org.apache.commons.logging.LogFactory;
|
||||||
import org.apache.curator.framework.CuratorFramework;
|
import org.apache.curator.framework.CuratorFramework;
|
||||||
@ -338,7 +337,7 @@ public synchronized void initInternal(Configuration conf)
|
|||||||
@Override
|
@Override
|
||||||
public synchronized void startInternal() throws Exception {
|
public synchronized void startInternal() throws Exception {
|
||||||
// ensure root dirs exist
|
// ensure root dirs exist
|
||||||
createRootDirRecursively(znodeWorkingPath);
|
zkManager.createRootDirRecursively(znodeWorkingPath);
|
||||||
create(zkRootNodePath);
|
create(zkRootNodePath);
|
||||||
setRootNodeAcls();
|
setRootNodeAcls();
|
||||||
delete(fencingNodePath);
|
delete(fencingNodePath);
|
||||||
@ -1147,22 +1146,6 @@ private void addOrUpdateReservationState(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Utility function to ensure that the configured base znode exists.
|
|
||||||
* This recursively creates the znode as well as all of its parents.
|
|
||||||
*/
|
|
||||||
private 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());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get alternate path for app id if path according to configured split index
|
* Get alternate path for app id if path according to configured split index
|
||||||
* does not exist. We look for path based on all possible split indices.
|
* does not exist. We look for path based on all possible split indices.
|
||||||
|
Loading…
x
Reference in New Issue
Block a user