Search in sources :

Example 1 with Code

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);
}
Also used : Code(org.apache.zookeeper.KeeperException.Code)

Example 2 with Code

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);
    }
}
Also used : Stat(org.apache.zookeeper.data.Stat) StateMap(org.apache.nifi.components.state.StateMap) StandardStateMap(org.apache.nifi.controller.state.StandardStateMap) IOException(java.io.IOException) Code(org.apache.zookeeper.KeeperException.Code) StandardStateMap(org.apache.nifi.controller.state.StandardStateMap) KeeperException(org.apache.zookeeper.KeeperException)

Example 3 with Code

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;
        }
    });
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Code(org.apache.zookeeper.KeeperException.Code) NotNull(javax.validation.constraints.NotNull)

Example 4 with Code

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;
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) Code(org.apache.zookeeper.KeeperException.Code) NotNull(javax.validation.constraints.NotNull)

Example 5 with Code

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;
}
Also used : ArrayList(java.util.ArrayList) CreateCallbackHandler(org.apache.helix.manager.zk.ZkAsyncCallbacks.CreateCallbackHandler) Code(org.apache.zookeeper.KeeperException.Code) CreateMode(org.apache.zookeeper.CreateMode)

Aggregations

Code (org.apache.zookeeper.KeeperException.Code)18 KeeperException (org.apache.zookeeper.KeeperException)10 Stat (org.apache.zookeeper.data.Stat)10 ArrayList (java.util.ArrayList)4 Retry (org.apache.accumulo.fate.util.Retry)4 IOException (java.io.IOException)3 ZooKeeper (org.apache.zookeeper.ZooKeeper)3 CompletableFuture (java.util.concurrent.CompletableFuture)2 NotNull (javax.validation.constraints.NotNull)2 CreateCallbackHandler (org.apache.helix.manager.zk.ZkAsyncCallbacks.CreateCallbackHandler)2 CreateMode (org.apache.zookeeper.CreateMode)2 Op (org.apache.zookeeper.Op)2 OpCode (org.apache.zookeeper.ZooDefs.OpCode)2 InputStream (java.io.InputStream)1 ByteBuffer (java.nio.ByteBuffer)1 List (java.util.List)1 Set (java.util.Set)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1 SetDataCallbackHandler (org.apache.helix.manager.zk.ZkAsyncCallbacks.SetDataCallbackHandler)1