本文整理汇总了Java中org.apache.zookeeper.KeeperException.code方法的典型用法代码示例。如果您正苦于以下问题:Java KeeperException.code方法的具体用法?Java KeeperException.code怎么用?Java KeeperException.code使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.zookeeper.KeeperException
的用法示例。
在下文中一共展示了KeeperException.code方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getActiveData
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
/**
* get data set by the active leader
*
* @return data set by the active instance
* @throws ActiveNotFoundException
* when there is no active leader
* @throws KeeperException
* other zookeeper operation errors
* @throws InterruptedException
* @throws IOException
* when ZooKeeper connection could not be established
*/
public synchronized byte[] getActiveData() throws ActiveNotFoundException,
KeeperException, InterruptedException, IOException {
try {
if (zkClient == null) {
createConnection();
}
Stat stat = new Stat();
return getDataWithRetries(zkLockFilePath, false, stat);
} catch(KeeperException e) {
Code code = e.code();
if (isNodeDoesNotExist(code)) {
// handle the commonly expected cases that make sense for us
throw new ActiveNotFoundException();
} else {
throw e;
}
}
}
示例2: grabTicket
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
static boolean grabTicket(ZooKeeper zookeeper, String lockNode,
String ticket) throws InterruptedException, KeeperException {
try {
zookeeper.create(lockNode + "/" + ticket, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.EPHEMERAL);
} catch (KeeperException e) {
if (e.code() == KeeperException.Code.NODEEXISTS) {
logger.debug("Failed to claim ticket {}.", ticket);
return false;
} else {
throw e;
}
}
logger.debug("Claimed ticket {}.", ticket);
return true;
}
示例3: multi
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
/**
* Run multiple operations in a transactional manner. Retry before throwing exception
*/
public List<OpResult> multi(Iterable<Op> ops)
throws KeeperException, InterruptedException {
TraceScope traceScope = null;
try {
traceScope = Trace.startSpan("RecoverableZookeeper.multi");
RetryCounter retryCounter = retryCounterFactory.create();
Iterable<Op> multiOps = prepareZKMulti(ops);
while (true) {
try {
return checkZk().multi(multiOps);
} catch (KeeperException e) {
switch (e.code()) {
case CONNECTIONLOSS:
case SESSIONEXPIRED:
case OPERATIONTIMEOUT:
retryOrThrow(retryCounter, e, "multi");
break;
default:
throw e;
}
}
retryCounter.sleepUntilNextRetry();
}
} finally {
if (traceScope != null) traceScope.close();
}
}
示例4: getData
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
/**
* getData is an idempotent operation. Retry before throwing exception
* @return Data
*/
public byte[] getData(String path, Watcher watcher, Stat stat)
throws KeeperException, InterruptedException {
TraceScope traceScope = null;
try {
traceScope = Trace.startSpan("RecoverableZookeeper.getData");
RetryCounter retryCounter = retryCounterFactory.create();
while (true) {
try {
byte[] revData = checkZk().getData(path, watcher, stat);
return this.removeMetaData(revData);
} catch (KeeperException e) {
switch (e.code()) {
case CONNECTIONLOSS:
case SESSIONEXPIRED:
case OPERATIONTIMEOUT:
retryOrThrow(retryCounter, e, "getData");
break;
default:
throw e;
}
}
retryCounter.sleepUntilNextRetry();
}
} finally {
if (traceScope != null) traceScope.close();
}
}
示例5: getChildren
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
/**
* getChildren is an idempotent operation. Retry before throwing exception
* @return List of children znodes
*/
public List<String> getChildren(String path, boolean watch)
throws KeeperException, InterruptedException {
TraceScope traceScope = null;
try {
traceScope = Trace.startSpan("RecoverableZookeeper.getChildren");
RetryCounter retryCounter = retryCounterFactory.create();
while (true) {
try {
return checkZk().getChildren(path, watch);
} catch (KeeperException e) {
switch (e.code()) {
case CONNECTIONLOSS:
case SESSIONEXPIRED:
case OPERATIONTIMEOUT:
retryOrThrow(retryCounter, e, "getChildren");
break;
default:
throw e;
}
}
retryCounter.sleepUntilNextRetry();
}
} finally {
if (traceScope != null) traceScope.close();
}
}
示例6: createIfNotThere
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
static void createIfNotThere(ZooKeeper zookeeper, String znode) throws KeeperException,
InterruptedException {
try {
create(zookeeper, znode);
} catch (KeeperException e) {
if (e.code() != KeeperException.Code.NODEEXISTS) {
throw e;
}
}
}
示例7: releaseTicket
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
static void releaseTicket(ZooKeeper zookeeper, String lockNode,
String ticket) throws KeeperException, InterruptedException {
logger.debug("Releasing ticket {}.", ticket);
try {
zookeeper.delete(lockNode + "/" + ticket, -1);
} catch (KeeperException e) {
if (e.code() != KeeperException.Code.NONODE) {
// If it the node is already gone, than that is fine, otherwise:
throw e;
}
}
}
示例8: checkException
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
private static ZkException checkException(String defaultMessage, Throwable e) {
if (e instanceof KeeperException) {
KeeperException zkException = (KeeperException) e;
if (zkException.code() == KeeperException.Code.NONODE) {
return new NotExitException(defaultMessage, e);
}
if (zkException.code() == KeeperException.Code.BADVERSION) {
return new BadVersion(e);
}
}
return new ZkException(defaultMessage, e);
}
示例9: createSequential
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
private String createSequential(String path, byte[] data,
List<ACL> acl, CreateMode createMode)
throws KeeperException, InterruptedException {
RetryCounter retryCounter = retryCounterFactory.create();
boolean first = true;
String newPath = path+this.identifier;
while (true) {
try {
if (!first) {
// Check if we succeeded on a previous attempt
String previousResult = findPreviousSequentialNode(newPath);
if (previousResult != null) {
return previousResult;
}
}
first = false;
return checkZk().create(newPath, data, acl, createMode);
} catch (KeeperException e) {
switch (e.code()) {
case CONNECTIONLOSS:
case SESSIONEXPIRED:
case OPERATIONTIMEOUT:
retryOrThrow(retryCounter, e, "create");
break;
default:
throw e;
}
}
retryCounter.sleepUntilNextRetry();
}
}
示例10: create
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
public static ZkException create(KeeperException e) {
switch (e.code()) {
// case DATAINCONSISTENCY:
// return new DataInconsistencyException();
// case CONNECTIONLOSS:
// return new ConnectionLossException();
case NONODE:
return new ZkNoNodeException(e);
// case NOAUTH:
// return new ZkNoAuthException();
case BADVERSION:
return new ZkBadVersionException(e);
// case NOCHILDRENFOREPHEMERALS:
// return new NoChildrenForEphemeralsException();
case NODEEXISTS:
return new ZkNodeExistsException(e);
// case INVALIDACL:
// return new ZkInvalidACLException();
// case AUTHFAILED:
// return new AuthFailedException();
// case NOTEMPTY:
// return new NotEmptyException();
// case SESSIONEXPIRED:
// return new SessionExpiredException();
// case INVALIDCALLBACK:
// return new InvalidCallbackException();
default:
return new ZkException(e);
}
}
示例11: getAcl
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
/**
* getAcl is an idempotent operation. Retry before throwing exception
* @return list of ACLs
*/
public List<ACL> getAcl(String path, Stat stat)
throws KeeperException, InterruptedException {
TraceScope traceScope = null;
try {
traceScope = Trace.startSpan("RecoverableZookeeper.getAcl");
RetryCounter retryCounter = retryCounterFactory.create();
while (true) {
try {
return checkZk().getACL(path, stat);
} catch (KeeperException e) {
switch (e.code()) {
case CONNECTIONLOSS:
case SESSIONEXPIRED:
case OPERATIONTIMEOUT:
retryOrThrow(retryCounter, e, "getAcl");
break;
default:
throw e;
}
}
retryCounter.sleepUntilNextRetry();
}
} finally {
if (traceScope != null) traceScope.close();
}
}
示例12: delete
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
/**
* delete is an idempotent operation. Retry before throwing exception.
* This function will not throw NoNodeException if the path does not
* exist.
*/
public void delete(String path, int version)
throws InterruptedException, KeeperException {
TraceScope traceScope = null;
try {
traceScope = Trace.startSpan("RecoverableZookeeper.delete");
RetryCounter retryCounter = retryCounterFactory.create();
boolean isRetry = false; // False for first attempt, true for all retries.
while (true) {
try {
checkZk().delete(path, version);
return;
} catch (KeeperException e) {
switch (e.code()) {
case NONODE:
if (isRetry) {
LOG.debug("Node " + path + " already deleted. Assuming a " +
"previous attempt succeeded.");
return;
}
LOG.debug("Node " + path + " already deleted, retry=" + isRetry);
throw e;
case CONNECTIONLOSS:
case SESSIONEXPIRED:
case OPERATIONTIMEOUT:
retryOrThrow(retryCounter, e, "delete");
break;
default:
throw e;
}
}
retryCounter.sleepUntilNextRetry();
isRetry = true;
}
} finally {
if (traceScope != null) traceScope.close();
}
}
示例13: setAcl
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
/**
* setAcl is an idempotent operation. Retry before throwing exception
* @return list of ACLs
*/
public Stat setAcl(String path, List<ACL> acls, int version)
throws KeeperException, InterruptedException {
TraceScope traceScope = null;
try {
traceScope = Trace.startSpan("RecoverableZookeeper.setAcl");
RetryCounter retryCounter = retryCounterFactory.create();
while (true) {
try {
return checkZk().setACL(path, acls, version);
} catch (KeeperException e) {
switch (e.code()) {
case CONNECTIONLOSS:
case SESSIONEXPIRED:
case OPERATIONTIMEOUT:
retryOrThrow(retryCounter, e, "setAcl");
break;
default:
throw e;
}
}
retryCounter.sleepUntilNextRetry();
}
} finally {
if (traceScope != null) traceScope.close();
}
}
示例14: toResponse
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
public Response toResponse(KeeperException e) {
Response.Status status;
String message;
String path = e.getPath();
switch(e.code()) {
case AUTHFAILED:
status = Response.Status.UNAUTHORIZED;
message = path + " not authorized";
break;
case BADARGUMENTS:
status = Response.Status.BAD_REQUEST;
message = path + " bad arguments";
break;
case BADVERSION:
status = Response.Status.PRECONDITION_FAILED;
message = path + " bad version";
break;
case INVALIDACL:
status = Response.Status.BAD_REQUEST;
message = path + " invalid acl";
break;
case NODEEXISTS:
status = Response.Status.CONFLICT;
message = path + " already exists";
break;
case NONODE:
status = Response.Status.NOT_FOUND;
message = path + " not found";
break;
case NOTEMPTY:
status = Response.Status.CONFLICT;
message = path + " not empty";
break;
default:
status = Response.Status.fromStatusCode(502); // bad gateway
message = "Error processing request for " + path
+ " : " + e.getMessage();
}
return Response.status(status).entity(
new ZError(ui.getRequestUri().toString(), message)).build();
}
示例15: claimResource
import org.apache.zookeeper.KeeperException; //导入方法依赖的package包/类
int claimResource(ZooKeeper zookeeper, String poolNode, int poolSize) throws KeeperException,
InterruptedException {
logger.debug("Trying to claim a resource.");
List<String> claimedResources = zookeeper.getChildren(poolNode, false);
if (claimedResources.size() >= poolSize) {
logger.debug("No resources available at the moment (pool size: {}), waiting.",
poolSize);
final CountDownLatch latch = new CountDownLatch(1);
zookeeper.getChildren(poolNode, event -> latch.countDown());
latch.await();
return claimResource(zookeeper, poolNode, poolSize);
}
for (int i = 0; i < poolSize; i++) {
String resourcePath = Integer.toString(i);
if (!claimedResources.contains(resourcePath)) {
String node;
try {
logger.debug("Trying to claim seemingly available resource {}.", resourcePath);
node = zookeeper.create(poolNode + "/" + resourcePath, new byte[0],
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
} catch (KeeperException e) {
if (e.code() == KeeperException.Code.NODEEXISTS) {
continue;
} else {
throw e;
}
}
zookeeper.exists(node, event -> {
if (event.getType() == EventType.NodeDeleted) {
logger.debug("Resource-claim node unexpectedly deleted ({})", resource);
close(true);
}
});
logger.debug("Successfully claimed resource {}.", resourcePath);
return i;
}
}
return claimResource(zookeeper, poolNode, poolSize);
}