use of org.apache.zookeeper.KeeperException.Code in project hadoop by apache.
the class ActiveStandbyElector method processResult.
/**
* interface implementation of Zookeeper callback for monitor (exists)
*/
@Override
public synchronized void processResult(int rc, String path, Object ctx, Stat stat) {
if (isStaleClient(ctx))
return;
monitorLockNodePending = false;
assert wantToBeInElection : "Got a StatNode result after quitting election";
if (LOG.isDebugEnabled()) {
LOG.debug("StatNode result: " + rc + " for path: " + path + " connectionState: " + zkConnectionState + " for " + this);
}
Code code = Code.get(rc);
if (isSuccess(code)) {
// creation was retried
if (stat.getEphemeralOwner() == zkClient.getSessionId()) {
// we own the lock znode. so we are the leader
if (!becomeActive()) {
reJoinElectionAfterFailureToBecomeActive();
}
} else {
// we dont own the lock znode. so we are a standby.
becomeStandby();
}
// the watch set by us will notify about changes
return;
}
if (isNodeDoesNotExist(code)) {
// the lock znode disappeared before we started monitoring it
enterNeutralMode();
joinElectionInternal();
return;
}
String errorMessage = "Received stat error from Zookeeper. code:" + code.toString();
LOG.debug(errorMessage);
if (shouldRetry(code)) {
if (statRetryCount < maxRetryNum) {
++statRetryCount;
monitorLockNodeAsync();
return;
}
errorMessage = errorMessage + ". Not retrying further znode monitoring connection errors.";
} else if (isSessionExpired(code)) {
// This isn't fatal - the client Watcher will re-join the election
LOG.warn("Lock monitoring failed because session was lost");
return;
}
fatalError(errorMessage);
}
use of org.apache.zookeeper.KeeperException.Code in project nifi by apache.
the class ZooKeeperStateProvider method getState.
@Override
public StateMap getState(final String componentId) throws IOException {
verifyEnabled();
try {
final Stat stat = new Stat();
final String path = getComponentPath(componentId);
final byte[] data = getZooKeeper().getData(path, false, stat);
final StateMap stateMap = deserialize(data, stat.getVersion(), componentId);
return stateMap;
} catch (final InterruptedException e) {
Thread.currentThread().interrupt();
throw new IOException("Failed to obtain value from ZooKeeper for component with ID " + componentId + ", due to interruption", e);
} catch (final KeeperException ke) {
final Code exceptionCode = ke.code();
if (Code.NONODE == exceptionCode) {
return new StandardStateMap(null, -1L);
}
if (Code.SESSIONEXPIRED == exceptionCode) {
invalidateClient();
return getState(componentId);
}
throw new IOException("Failed to obtain value from ZooKeeper for component with ID " + componentId + " with exception code " + exceptionCode, ke);
} catch (final IOException ioe) {
// provide more context in the error message
throw new IOException("Failed to obtain value from ZooKeeper for component with ID " + componentId, ioe);
}
}
use of org.apache.zookeeper.KeeperException.Code in project incubator-pulsar by apache.
the class BookkeeperSchemaStorage method getOrCreateSchemaLocator.
@NotNull
private CompletableFuture<LocatorEntry> getOrCreateSchemaLocator(String schema) {
return getSchemaLocator(schema).thenCompose(schemaLocatorStatEntry -> {
if (schemaLocatorStatEntry.isPresent()) {
return completedFuture(schemaLocatorStatEntry.get());
} else {
SchemaStorageFormat.SchemaLocator locator = SchemaStorageFormat.SchemaLocator.newBuilder().setInfo(SchemaStorageFormat.IndexEntry.newBuilder().setVersion(-1L).setHash(ByteString.EMPTY).setPosition(SchemaStorageFormat.PositionInfo.newBuilder().setEntryId(-1L).setLedgerId(-1L))).build();
CompletableFuture<LocatorEntry> future = new CompletableFuture<>();
ZkUtils.asyncCreateFullPathOptimistic(zooKeeper, schema, locator.toByteArray(), Acl, CreateMode.PERSISTENT, (rc, path, ctx, name) -> {
Code code = Code.get(rc);
if (code != Code.OK) {
future.completeExceptionally(KeeperException.create(code));
} else {
future.complete(new LocatorEntry(locator, -1));
}
}, null);
return future;
}
});
}
use of org.apache.zookeeper.KeeperException.Code in project incubator-pulsar by apache.
the class BookkeeperSchemaStorage method updateSchemaLocator.
@NotNull
private CompletableFuture<Void> updateSchemaLocator(String id, SchemaStorageFormat.SchemaLocator schema, int version) {
CompletableFuture<Void> future = new CompletableFuture<>();
zooKeeper.setData(id, schema.toByteArray(), version, (rc, path, ctx, stat) -> {
Code code = Code.get(rc);
if (code != Code.OK) {
future.completeExceptionally(KeeperException.create(code));
} else {
future.complete(null);
}
}, null);
return future;
}
use of org.apache.zookeeper.KeeperException.Code in project helix by apache.
the class ZkBaseDataAccessor method create.
/**
* async create. give up on error other than NONODE
*/
CreateCallbackHandler[] create(List<String> paths, List<T> records, boolean[] needCreate, List<List<String>> pathsCreated, int options) {
if ((records != null && records.size() != paths.size()) || needCreate.length != paths.size() || (pathsCreated != null && pathsCreated.size() != paths.size())) {
throw new IllegalArgumentException("paths, records, needCreate, and pathsCreated should be of same size");
}
CreateCallbackHandler[] cbList = new CreateCallbackHandler[paths.size()];
CreateMode mode = AccessOption.getMode(options);
if (mode == null) {
LOG.error("Invalid async set mode. options: " + options);
return cbList;
}
boolean retry;
do {
retry = false;
for (int i = 0; i < paths.size(); i++) {
if (!needCreate[i])
continue;
String path = paths.get(i);
T record = records == null ? null : records.get(i);
cbList[i] = new CreateCallbackHandler();
_zkClient.asyncCreate(path, record, mode, cbList[i]);
}
List<String> parentPaths = new ArrayList<String>(Collections.<String>nCopies(paths.size(), null));
boolean failOnNoNode = false;
for (int i = 0; i < paths.size(); i++) {
if (!needCreate[i])
continue;
CreateCallbackHandler cb = cbList[i];
cb.waitForSuccess();
String path = paths.get(i);
if (Code.get(cb.getRc()) == Code.NONODE) {
String parentPath = HelixUtil.getZkParentPath(path);
parentPaths.set(i, parentPath);
failOnNoNode = true;
} else {
// if create succeed or fail on error other than NONODE,
// give up
needCreate[i] = false;
// if succeeds, record what paths we've created
if (Code.get(cb.getRc()) == Code.OK && pathsCreated != null) {
if (pathsCreated.get(i) == null) {
pathsCreated.set(i, new ArrayList<String>());
}
pathsCreated.get(i).add(path);
}
}
}
if (failOnNoNode) {
boolean[] needCreateParent = Arrays.copyOf(needCreate, needCreate.length);
CreateCallbackHandler[] parentCbList = create(parentPaths, null, needCreateParent, pathsCreated, AccessOption.PERSISTENT);
for (int i = 0; i < parentCbList.length; i++) {
CreateCallbackHandler parentCb = parentCbList[i];
if (parentCb == null)
continue;
Code rc = Code.get(parentCb.getRc());
// if parent is created, retry create child
if (rc == Code.OK || rc == Code.NODEEXISTS) {
retry = true;
break;
}
}
}
} while (retry);
return cbList;
}
Aggregations