本文整理匯總了Java中org.apache.zookeeper.KeeperException類的典型用法代碼示例。如果您正苦於以下問題:Java KeeperException類的具體用法?Java KeeperException怎麽用?Java KeeperException使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。
KeeperException類屬於org.apache.zookeeper包,在下文中一共展示了KeeperException類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: createPath
import org.apache.zookeeper.KeeperException; //導入依賴的package包/類
private boolean createPath(String path, CreateMode createMode) {
boolean success;
try {
zkClient.create()
.creatingParentsIfNeeded()
.withMode(createMode)
.forPath(path, "".getBytes());
success = true;
LOG.info("create path success, path={}", path);
} catch (KeeperException.NodeExistsException ex1) {
success = true;
LOG.debug("node exist, path={}", path);
} catch (Exception ex2) {
success = false;
LOG.debug("createPath exception:", ex2);
}
return success;
}
示例2: processRegionInTransitionAndBlockUntilAssigned
import org.apache.zookeeper.KeeperException; //導入依賴的package包/類
/**
* If region is up in zk in transition, then do fixup and block and wait until
* the region is assigned and out of transition. Used on startup for
* catalog regions.
* @param hri Region to look for.
* @return True if we processed a region in transition else false if region
* was not up in zk in transition.
* @throws InterruptedException
* @throws KeeperException
* @throws IOException
*/
boolean processRegionInTransitionAndBlockUntilAssigned(final HRegionInfo hri)
throws InterruptedException, KeeperException, IOException {
String encodedRegionName = hri.getEncodedName();
if (!processRegionInTransition(encodedRegionName, hri)) {
return false; // The region is not in transition
}
LOG.debug("Waiting on " + HRegionInfo.prettyPrint(encodedRegionName));
while (!this.server.isStopped() &&
this.regionStates.isRegionInTransition(encodedRegionName)) {
RegionState state = this.regionStates.getRegionTransitionState(encodedRegionName);
if (state == null || !serverManager.isServerOnline(state.getServerName())) {
// The region is not in transition, or not in transition on an online
// server. Doesn't help to block here any more. Caller need to
// verify the region is actually assigned.
break;
}
this.regionStates.waitForUpdate(100);
}
return true;
}
示例3: processRequest
import org.apache.zookeeper.KeeperException; //導入依賴的package包/類
@Override
public void processRequest(Request request)
throws RequestProcessorException {
// Check if this is a local session and we are trying to create
// an ephemeral node, in which case we upgrade the session
Request upgradeRequest = null;
try {
upgradeRequest = lzks.checkUpgradeSession(request);
} catch (KeeperException ke) {
if (request.getHdr() != null) {
LOG.debug("Updating header");
request.getHdr().setType(OpCode.error);
request.setTxn(new ErrorTxn(ke.code().intValue()));
}
request.setException(ke);
LOG.info("Error creating upgrade request " + ke.getMessage());
} catch (IOException ie) {
LOG.error("Unexpected error in upgrade", ie);
}
if (upgradeRequest != null) {
nextProcessor.processRequest(upgradeRequest);
}
nextProcessor.processRequest(request);
}
示例4: verifyRegionState
import org.apache.zookeeper.KeeperException; //導入依賴的package包/類
/**
* Verifies that the specified region is in the specified state in ZooKeeper.
* <p>
* Returns true if region is in transition and in the specified state in
* ZooKeeper. Returns false if the region does not exist in ZK or is in
* a different state.
* <p>
* Method synchronizes() with ZK so will yield an up-to-date result but is
* a slow read.
* @param zkw
* @param region
* @param expectedState
* @return true if region exists and is in expected state
* @throws DeserializationException
*/
static boolean verifyRegionState(ZooKeeperWatcher zkw, HRegionInfo region, EventType expectedState)
throws KeeperException, DeserializationException {
String encoded = region.getEncodedName();
String node = ZKAssign.getNodeName(zkw, encoded);
zkw.sync(node);
// Read existing data of the node
byte [] existingBytes = null;
try {
existingBytes = ZKUtil.getDataAndWatch(zkw, node);
} catch (KeeperException.NoNodeException nne) {
return false;
} catch (KeeperException e) {
throw e;
}
if (existingBytes == null) return false;
RegionTransition rt = RegionTransition.parseFrom(existingBytes);
return rt.getEventType().equals(expectedState);
}
示例5: createAndWatch
import org.apache.zookeeper.KeeperException; //導入依賴的package包/類
/**
* Creates the specified node with the specified data and watches it.
*
* <p>Throws an exception if the node already exists.
*
* <p>The node created is persistent and open access.
*
* <p>Returns the version number of the created node if successful.
*
* @param zkw zk reference
* @param znode path of node to create
* @param data data of node to create
* @return version of node created
* @throws KeeperException if unexpected zookeeper exception
* @throws KeeperException.NodeExistsException if node already exists
*/
public static int createAndWatch(ZooKeeperWatcher zkw,
String znode, byte [] data)
throws KeeperException, KeeperException.NodeExistsException {
try {
zkw.getRecoverableZooKeeper().create(znode, data, createACL(zkw, znode),
CreateMode.PERSISTENT);
Stat stat = zkw.getRecoverableZooKeeper().exists(znode, zkw);
if (stat == null){
// Likely a race condition. Someone deleted the znode.
throw KeeperException.create(KeeperException.Code.SYSTEMERROR,
"ZK.exists returned null (i.e.: znode does not exist) for znode=" + znode);
}
return stat.getVersion();
} catch (InterruptedException e) {
zkw.interruptedException(e);
return -1;
}
}
示例6: createZNodeTree
import org.apache.zookeeper.KeeperException; //導入依賴的package包/類
private void createZNodeTree(String rootZNode) throws KeeperException,
InterruptedException {
List<Op> opList = new ArrayList<Op>();
opList.add(Op.create(rootZNode, new byte[0], Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT));
int level = 0;
String parentZNode = rootZNode;
while (level < 10) {
// define parent node
parentZNode = parentZNode + "/" + level;
opList.add(Op.create(parentZNode, new byte[0], Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT));
int elements = 0;
// add elements to the parent node
while (elements < level) {
opList.add(Op.create(parentZNode + "/" + elements, new byte[0],
Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT));
elements++;
}
level++;
}
zkw.getRecoverableZooKeeper().multi(opList);
}
示例7: testReconfigEnabledWithAuthAndWrongACL
import org.apache.zookeeper.KeeperException; //導入依賴的package包/類
@Test(timeout = 10000)
public void testReconfigEnabledWithAuthAndWrongACL() throws InterruptedException {
resetZKAdmin();
try {
zkAdmin.addAuthInfo("digest", "super:test".getBytes());
// There is ACL however the permission is wrong - need WRITE permission at leaste.
ArrayList<ACL> acls = new ArrayList<ACL>(
Collections.singletonList(
new ACL(ZooDefs.Perms.READ,
new Id("digest", "user:tl+z3z0vO6PfPfEENfLF96E6pM0="/* password is test */))));
zkAdmin.setACL(ZooDefs.CONFIG_NODE, acls, -1);
resetZKAdmin();
zkAdmin.addAuthInfo("digest", "user:test".getBytes());
reconfigPort();
Assert.fail("Reconfig should fail with an ACL that is read only!");
} catch (KeeperException e) {
Assert.assertTrue(e.code() == KeeperException.Code.NOAUTH);
}
}
示例8: testZNodeDeletes
import org.apache.zookeeper.KeeperException; //導入依賴的package包/類
/**
* Create a bunch of znodes in a hierarchy, try deleting one that has childs (it will fail), then
* delete it recursively, then delete the last znode
* @throws Exception
*/
@Test
public void testZNodeDeletes() throws Exception {
ZooKeeperWatcher zkw = new ZooKeeperWatcher(
new Configuration(TEST_UTIL.getConfiguration()),
TestZooKeeper.class.getName(), null);
ZKUtil.createWithParents(zkw, "/l1/l2/l3/l4");
try {
ZKUtil.deleteNode(zkw, "/l1/l2");
fail("We should not be able to delete if znode has childs");
} catch (KeeperException ex) {
assertNotNull(ZKUtil.getDataNoWatch(zkw, "/l1/l2/l3/l4", null));
}
ZKUtil.deleteNodeRecursively(zkw, "/l1/l2");
// make sure it really is deleted
assertNull(ZKUtil.getDataNoWatch(zkw, "/l1/l2/l3/l4", null));
// do the same delete again and make sure it doesn't crash
ZKUtil.deleteNodeRecursively(zkw, "/l1/l2");
ZKUtil.deleteNode(zkw, "/l1");
assertNull(ZKUtil.getDataNoWatch(zkw, "/l1/l2", null));
}
示例9: deleteRecoveringRegionZNodes
import org.apache.zookeeper.KeeperException; //導入依賴的package包/類
public static void deleteRecoveringRegionZNodes(ZooKeeperWatcher watcher, List<String> regions) {
try {
if (regions == null) {
// remove all children under /home/recovering-regions
LOG.debug("Garbage collecting all recovering region znodes");
ZKUtil.deleteChildrenRecursively(watcher, watcher.recoveringRegionsZNode);
} else {
for (String curRegion : regions) {
String nodePath = ZKUtil.joinZNode(watcher.recoveringRegionsZNode, curRegion);
ZKUtil.deleteNodeRecursively(watcher, nodePath);
}
}
} catch (KeeperException e) {
LOG.warn("Cannot remove recovering regions from ZooKeeper", e);
}
}
示例10: deleteZNode
import org.apache.zookeeper.KeeperException; //導入依賴的package包/類
@DELETE
@Produces( { MediaType.APPLICATION_JSON, "application/javascript",
MediaType.APPLICATION_XML, MediaType.APPLICATION_OCTET_STREAM })
public void deleteZNode(@PathParam("path") String path,
@DefaultValue("-1") @QueryParam("version") String versionParam,
@Context UriInfo ui) throws InterruptedException, KeeperException {
ensurePathNotNull(path);
int version;
try {
version = Integer.parseInt(versionParam);
} catch (NumberFormatException e) {
throw new WebApplicationException(Response.status(
Response.Status.BAD_REQUEST).entity(
new ZError(ui.getRequestUri().toString(), path
+ " bad version " + versionParam)).build());
}
zk.delete(path, version);
}
示例11: testReconfigEnabledWithAuthAndACL
import org.apache.zookeeper.KeeperException; //導入依賴的package包/類
@Test(timeout = 10000)
public void testReconfigEnabledWithAuthAndACL() throws InterruptedException {
resetZKAdmin();
try {
zkAdmin.addAuthInfo("digest", "super:test".getBytes());
ArrayList<ACL> acls = new ArrayList<ACL>(
Collections.singletonList(
new ACL(ZooDefs.Perms.WRITE,
new Id("digest", "user:tl+z3z0vO6PfPfEENfLF96E6pM0="/* password is test */))));
zkAdmin.setACL(ZooDefs.CONFIG_NODE, acls, -1);
resetZKAdmin();
zkAdmin.addAuthInfo("digest", "user:test".getBytes());
Assert.assertTrue(reconfigPort());
} catch (KeeperException e) {
Assert.fail("Reconfig should not fail, but failed with exception : " + e.getMessage());
}
}
示例12: checkZnodePermsRecursive
import org.apache.zookeeper.KeeperException; //導入依賴的package包/類
private void checkZnodePermsRecursive(ZooKeeperWatcher watcher,
RecoverableZooKeeper zk, String znode) throws KeeperException, InterruptedException {
boolean expectedWorldReadable = watcher.isClientReadable(znode);
assertZnodePerms(zk, znode, expectedWorldReadable);
try {
List<String> children = zk.getChildren(znode, false);
for (String child : children) {
checkZnodePermsRecursive(watcher, zk, ZKUtil.joinZNode(znode, child));
}
} catch (KeeperException ke) {
// if we are not authenticated for listChildren, it is fine.
if (ke.code() != Code.NOAUTH) {
throw ke;
}
}
}
示例13: setData
import org.apache.zookeeper.KeeperException; //導入依賴的package包/類
@Override
public Stat setData(String path, byte[] data, int version)
throws KeeperException, InterruptedException {
int count = 0;
do {
try {
return super.setData(path, data, version);
} catch (KeeperException.ConnectionLossException e) {
LoggerFactory.getLogger().warn(
"ZooKeeper connection lost. Trying to reconnect.");
Stat s = exists(path, false);
if (s != null) {
if (getData(path, false, s) == data) {
return s;
}
} else {
return null;
}
}
} while (!closed && (limit == -1 || count++ < limit));
return null;
}
示例14: testBlankPath
import org.apache.zookeeper.KeeperException; //導入依賴的package包/類
/**
* Test verifies the multi calls with blank znode path
*/
@Test(timeout = 90000)
public void testBlankPath() throws Exception {
List<Integer> expectedResultCodes = new ArrayList<Integer>();
expectedResultCodes.add(KeeperException.Code.RUNTIMEINCONSISTENCY
.intValue());
expectedResultCodes.add(KeeperException.Code.BADARGUMENTS.intValue());
expectedResultCodes.add(KeeperException.Code.RUNTIMEINCONSISTENCY
.intValue());
expectedResultCodes.add(KeeperException.Code.BADARGUMENTS.intValue());
// delete
String expectedErr = "Path cannot be null";
List<Op> opList = Arrays.asList(Op.delete("/multi0", -1),
Op.delete(null, 100),
Op.delete("/multi2", 5),
Op.delete("", -1));
multiHavingErrors(zk, opList, expectedResultCodes, expectedErr);
}
示例15: testNodes3
import org.apache.zookeeper.KeeperException; //導入依賴的package包/類
@Test
public void testNodes3() throws IOException, InterruptedException,
KeeperException {
int testIterations = 3;
final CountDownLatch latch = new CountDownLatch(testIterations);
final AtomicInteger failureCounter = new AtomicInteger();
for (int i = 0; i < testIterations; i++) {
runElectionSupportThread(latch, failureCounter);
}
Assert.assertEquals(0, failureCounter.get());
if (!latch.await(10, TimeUnit.SECONDS)) {
logger
.info(
"Waited for all threads to start, but timed out. We had {} failures.",
failureCounter);
}
}