Example usage for org.apache.zookeeper KeeperException create

List of usage examples for org.apache.zookeeper KeeperException create

Introduction

In this page you can find the example usage for org.apache.zookeeper KeeperException create.

Prototype

@Deprecated
public static KeeperException create(int code, String path) 

Source Link

Usage

From source file:org.apache.bookkeeper.meta.ZkLedgerIdGenerator.java

License:Apache License

@Override
public void generateLedgerId(final GenericCallback<Long> cb) {
    ZkUtils.asyncCreateFullPathOptimistic(zk, ledgerPrefix, new byte[0], zkAcls,
            CreateMode.EPHEMERAL_SEQUENTIAL, new StringCallback() {
                @Override//from   w  w  w  .j  a  v  a  2 s  .  c om
                public void processResult(int rc, String path, Object ctx, final String idPathName) {
                    if (rc != KeeperException.Code.OK.intValue()) {
                        LOG.error("Could not generate new ledger id",
                                KeeperException.create(KeeperException.Code.get(rc), path));
                        cb.operationComplete(BKException.Code.ZKException, null);
                        return;
                    }

                    /*
                     * Extract ledger id from generated path
                     */
                    long ledgerId;
                    try {
                        ledgerId = getLedgerIdFromGenPath(idPathName);
                        cb.operationComplete(BKException.Code.OK, ledgerId);
                    } catch (IOException e) {
                        LOG.error("Could not extract ledger-id from id gen path:" + path, e);
                        cb.operationComplete(BKException.Code.ZKException, null);
                        return;
                    }

                    // delete the znode for id generation
                    zk.delete(idPathName, -1, new AsyncCallback.VoidCallback() {
                        @Override
                        public void processResult(int rc, String path, Object ctx) {
                            if (rc != KeeperException.Code.OK.intValue()) {
                                LOG.warn("Exception during deleting znode for id generation : ",
                                        KeeperException.create(KeeperException.Code.get(rc), path));
                            } else {
                                LOG.debug("Deleting znode for id generation : {}", idPathName);
                            }
                        }
                    }, null);
                }
            }, null);
}

From source file:org.apache.bookkeeper.tools.BookKeeperTools.java

License:Apache License

/**
 * Async method to rebuild and recover the ledger fragments data that was
 * stored on the source bookie. That bookie could have failed completely and
 * now the ledger data that was stored on it is under replicated. An
 * optional destination bookie server could be given if we want to copy all
 * of the ledger fragments data on the failed source bookie to it.
 * Otherwise, we will just randomly distribute the ledger fragments to the
 * active set of bookies, perhaps based on load. All ZooKeeper ledger
 * metadata will be updated to point to the new bookie(s) that contain the
 * replicated ledger fragments.//  w ww.j  av  a 2s.  c  o  m
 * 
 * @param bookieSrc
 *            Source bookie that had a failure. We want to replicate the
 *            ledger fragments that were stored there.
 * @param bookieDest
 *            Optional destination bookie that if passed, we will copy all
 *            of the ledger fragments from the source bookie over to it.
 * @param cb
 *            RecoverCallback to invoke once all of the data on the dead
 *            bookie has been recovered and replicated.
 * @param context
 *            Context for the RecoverCallback to call.
 */
public void asyncRecoverBookieData(final InetSocketAddress bookieSrc, final InetSocketAddress bookieDest,
        final RecoverCallback cb, final Object context) {
    // Sync ZK to make sure we're reading the latest bookie/ledger data.
    zk.sync(LEDGERS_PATH, new AsyncCallback.VoidCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx) {
            if (rc != Code.OK.intValue()) {
                LOG.error("ZK error syncing: ", KeeperException.create(KeeperException.Code.get(rc), path));
                cb.recoverComplete(BKException.Code.ZKException, context);
                return;
            }
            getAvailableBookies(bookieSrc, bookieDest, cb, context);
        };
    }, null);
}

From source file:org.apache.bookkeeper.tools.BookKeeperTools.java

License:Apache License

/**
 * This method asynchronously gets the set of available Bookies that the
 * dead input bookie's data will be copied over into. If the user passed in
 * a specific destination bookie, then just use that one. Otherwise, we'll
 * randomly pick one of the other available bookies to use for each ledger
 * fragment we are replicating.//from   w ww .j  a  va  2  s.c  om
 * 
 * @param bookieSrc
 *            Source bookie that had a failure. We want to replicate the
 *            ledger fragments that were stored there.
 * @param bookieDest
 *            Optional destination bookie that if passed, we will copy all
 *            of the ledger fragments from the source bookie over to it.
 * @param cb
 *            RecoverCallback to invoke once all of the data on the dead
 *            bookie has been recovered and replicated.
 * @param context
 *            Context for the RecoverCallback to call.
 */
private void getAvailableBookies(final InetSocketAddress bookieSrc, final InetSocketAddress bookieDest,
        final RecoverCallback cb, final Object context) {
    final List<InetSocketAddress> availableBookies = new LinkedList<InetSocketAddress>();
    if (bookieDest != null) {
        availableBookies.add(bookieDest);
        // Now poll ZK to get the active ledgers
        getActiveLedgers(bookieSrc, bookieDest, cb, context, availableBookies);
    } else {
        zk.getChildren(BOOKIES_PATH, null, new AsyncCallback.ChildrenCallback() {
            @Override
            public void processResult(int rc, String path, Object ctx, List<String> children) {
                if (rc != Code.OK.intValue()) {
                    LOG.error("ZK error getting bookie nodes: ",
                            KeeperException.create(KeeperException.Code.get(rc), path));
                    cb.recoverComplete(BKException.Code.ZKException, context);
                    return;
                }
                for (String bookieNode : children) {
                    String parts[] = bookieNode.split(COLON);
                    if (parts.length < 2) {
                        LOG.error("Bookie Node retrieved from ZK has invalid name format: " + bookieNode);
                        cb.recoverComplete(BKException.Code.ZKException, context);
                        return;
                    }
                    availableBookies.add(new InetSocketAddress(parts[0], Integer.parseInt(parts[1])));
                }
                // Now poll ZK to get the active ledgers
                getActiveLedgers(bookieSrc, bookieDest, cb, context, availableBookies);
            }
        }, null);
    }
}

From source file:org.apache.bookkeeper.tools.BookKeeperTools.java

License:Apache License

/**
 * This method asynchronously polls ZK to get the current set of active
 * ledgers. From this, we can open each ledger and look at the metadata to
 * determine if any of the ledger fragments for it were stored at the dead
 * input bookie./*from w w w .  ja  v a 2  s  .  com*/
 * 
 * @param bookieSrc
 *            Source bookie that had a failure. We want to replicate the
 *            ledger fragments that were stored there.
 * @param bookieDest
 *            Optional destination bookie that if passed, we will copy all
 *            of the ledger fragments from the source bookie over to it.
 * @param cb
 *            RecoverCallback to invoke once all of the data on the dead
 *            bookie has been recovered and replicated.
 * @param context
 *            Context for the RecoverCallback to call.
 * @param availableBookies
 *            List of Bookie Servers that are available to use for
 *            replicating data on the failed bookie. This could contain a
 *            single bookie server if the user explicitly chose a bookie
 *            server to replicate data to.
 */
private void getActiveLedgers(final InetSocketAddress bookieSrc, final InetSocketAddress bookieDest,
        final RecoverCallback cb, final Object context, final List<InetSocketAddress> availableBookies) {
    zk.getChildren(LEDGERS_PATH, null, new AsyncCallback.ChildrenCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx, List<String> children) {
            if (rc != Code.OK.intValue()) {
                LOG.error("ZK error getting ledger nodes: ",
                        KeeperException.create(KeeperException.Code.get(rc), path));
                cb.recoverComplete(BKException.Code.ZKException, context);
                return;
            }
            // Wrapper class around the RecoverCallback so it can be used
            // as the final VoidCallback to invoke within the MultiCallback.
            class RecoverCallbackWrapper implements AsyncCallback.VoidCallback {
                final RecoverCallback cb;

                RecoverCallbackWrapper(RecoverCallback cb) {
                    this.cb = cb;
                }

                @Override
                public void processResult(int rc, String path, Object ctx) {
                    cb.recoverComplete(rc, ctx);
                }
            }
            // Recover each of the ledgers asynchronously
            MultiCallback ledgerMcb = new MultiCallback(children.size(), new RecoverCallbackWrapper(cb),
                    context);
            for (final String ledgerNode : children) {
                recoverLedger(bookieSrc, ledgerNode, ledgerMcb, availableBookies);
            }
        }
    }, null);
}

From source file:org.apache.bookkeeper.tools.BookKeeperTools.java

License:Apache License

/**
 * This method asynchronously recovers a given ledger if any of the ledger
 * entries were stored on the failed bookie.
 * //from   w  w  w . j  av a2s .  co  m
 * @param bookieSrc
 *            Source bookie that had a failure. We want to replicate the
 *            ledger fragments that were stored there.
 * @param ledgerNode
 *            Ledger Node name as retrieved from ZooKeeper we want to
 *            recover.
 * @param ledgerMcb
 *            MultiCallback to invoke once we've recovered the current
 *            ledger.
 * @param availableBookies
 *            List of Bookie Servers that are available to use for
 *            replicating data on the failed bookie. This could contain a
 *            single bookie server if the user explicitly chose a bookie
 *            server to replicate data to.
 */
private void recoverLedger(final InetSocketAddress bookieSrc, final String ledgerNode,
        final MultiCallback ledgerMcb, final List<InetSocketAddress> availableBookies) {
    /*
     * The available node is also stored in this path so ignore that. That
     * node is the path for the set of available Bookie Servers.
     */
    if (ledgerNode.equals(AVAILABLE_NODE)) {
        ledgerMcb.processResult(BKException.Code.OK, null, null);
        return;
    }
    // Parse out the ledgerId from the ZK ledger node.
    String parts[] = ledgerNode.split(LEDGER_NODE_PREFIX);
    if (parts.length < 2) {
        LOG.error("Ledger Node retrieved from ZK has invalid name format: " + ledgerNode);
        ledgerMcb.processResult(BKException.Code.ZKException, null, null);
        return;
    }
    final long lId;
    try {
        lId = Long.parseLong(parts[parts.length - 1]);
    } catch (NumberFormatException e) {
        LOG.error("Error retrieving ledgerId from ledgerNode: " + ledgerNode, e);
        ledgerMcb.processResult(BKException.Code.ZKException, null, null);
        return;
    }
    /*
     * For the current ledger, open it to retrieve the LedgerHandle. This
     * will contain the LedgerMetadata indicating which bookie servers the
     * ledger fragments are stored on. Check if any of the ledger fragments
     * for the current ledger are stored on the input dead bookie.
     */
    DigestType digestType = getLedgerDigestType(lId);
    byte[] passwd = getLedgerPasswd(lId);
    bkc.asyncOpenLedger(lId, digestType, passwd, new OpenCallback() {
        @Override
        public void openComplete(int rc, final LedgerHandle lh, Object ctx) {
            if (rc != Code.OK.intValue()) {
                LOG.error("BK error opening ledger: " + lId, BKException.create(rc));
                ledgerMcb.processResult(rc, null, null);
                return;
            }
            /*
             * This List stores the ledger fragments to recover indexed by
             * the start entry ID for the range. The ensembles TreeMap is
             * keyed off this.
             */
            final List<Long> ledgerFragmentsToRecover = new LinkedList<Long>();
            /*
             * This Map will store the start and end entry ID values for
             * each of the ledger fragment ranges. The only exception is the
             * current active fragment since it has no end yet. In the event
             * of a bookie failure, a new ensemble is created so the current
             * ensemble should not contain the dead bookie we are trying to
             * recover.
             */
            Map<Long, Long> ledgerFragmentsRange = new HashMap<Long, Long>();
            Long curEntryId = null;
            for (Map.Entry<Long, ArrayList<InetSocketAddress>> entry : lh.getLedgerMetadata().getEnsembles()
                    .entrySet()) {
                if (curEntryId != null)
                    ledgerFragmentsRange.put(curEntryId, entry.getKey() - 1);
                curEntryId = entry.getKey();
                if (entry.getValue().contains(bookieSrc)) {
                    /*
                     * Current ledger fragment has entries stored on the
                     * dead bookie so we'll need to recover them.
                     */
                    ledgerFragmentsToRecover.add(entry.getKey());
                }
            }
            /*
             * See if this current ledger contains any ledger fragment that
             * needs to be re-replicated. If not, then just invoke the
             * multiCallback and return.
             */
            if (ledgerFragmentsToRecover.size() == 0) {
                ledgerMcb.processResult(BKException.Code.OK, null, null);
                return;
            }
            /*
             * We have ledger fragments that need to be re-replicated to a
             * new bookie. Choose one randomly from the available set of
             * bookies.
             */
            final InetSocketAddress newBookie = availableBookies.get(rand.nextInt(availableBookies.size()));

            /*
             * Wrapper class around the ledger MultiCallback. Once all
             * ledger fragments for the ledger have been replicated to a new
             * bookie, we need to update ZK with this new metadata to point
             * to the new bookie instead of the old dead one. That should be
             * done at the end prior to invoking the ledger MultiCallback.
             */
            class LedgerMultiCallbackWrapper implements AsyncCallback.VoidCallback {
                final MultiCallback ledgerMcb;

                LedgerMultiCallbackWrapper(MultiCallback ledgerMcb) {
                    this.ledgerMcb = ledgerMcb;
                }

                @Override
                public void processResult(int rc, String path, Object ctx) {
                    if (rc != Code.OK.intValue()) {
                        LOG.error("BK error replicating ledger fragments for ledger: " + lId,
                                BKException.create(rc));
                        ledgerMcb.processResult(rc, null, null);
                        return;
                    }
                    /*
                     * Update the ledger metadata's ensemble info to point
                     * to the new bookie.
                     */
                    for (final Long startEntryId : ledgerFragmentsToRecover) {
                        ArrayList<InetSocketAddress> ensemble = lh.getLedgerMetadata().getEnsembles()
                                .get(startEntryId);
                        int deadBookieIndex = ensemble.indexOf(bookieSrc);
                        ensemble.remove(deadBookieIndex);
                        ensemble.add(deadBookieIndex, newBookie);
                    }
                    lh.writeLedgerConfig(new AsyncCallback.StatCallback() {
                        @Override
                        public void processResult(int rc, String path, Object ctx, Stat stat) {
                            if (rc != Code.OK.intValue()) {
                                LOG.error(
                                        "ZK error updating ledger config metadata for ledgerId: " + lh.getId(),
                                        KeeperException.create(KeeperException.Code.get(rc), path));
                            } else {
                                LOG.info("Updated ZK for ledgerId: (" + lh.getId()
                                        + ") to point ledger fragments from old dead bookie: (" + bookieSrc
                                        + ") to new bookie: (" + newBookie + ")");
                            }
                            /*
                             * Pass the return code result up the chain with
                             * the parent callback.
                             */
                            ledgerMcb.processResult(rc, null, null);
                        }
                    }, null);
                }
            }

            /*
             * Now recover all of the necessary ledger fragments
             * asynchronously using a MultiCallback for every fragment.
             */
            MultiCallback ledgerFragmentMcb = new MultiCallback(ledgerFragmentsToRecover.size(),
                    new LedgerMultiCallbackWrapper(ledgerMcb), null);
            for (final Long startEntryId : ledgerFragmentsToRecover) {
                Long endEntryId = ledgerFragmentsRange.get(startEntryId);
                recoverLedgerFragment(bookieSrc, lh, startEntryId, endEntryId, ledgerFragmentMcb, newBookie);
            }
        }
    }, null);
}

From source file:org.apache.bookkeeper.util.ZkUtils.java

License:Apache License

/**
 * Async get direct children under single node
 *
 * @param zk//  w  w  w  .jav  a 2  s .  c om
 *          zookeeper client
 * @param node
 *          node path
 * @param cb
 *          callback function
 */
public static void getChildrenInSingleNode(final ZooKeeper zk, final String node,
        final GenericCallback<List<String>> cb) {
    zk.sync(node, new AsyncCallback.VoidCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx) {
            if (rc != Code.OK.intValue()) {
                LOG.error("ZK error syncing nodes when getting children: ",
                        KeeperException.create(KeeperException.Code.get(rc), path));
                cb.operationComplete(rc, null);
                return;
            }
            zk.getChildren(node, false, new AsyncCallback.ChildrenCallback() {
                @Override
                public void processResult(int rc, String path, Object ctx, List<String> nodes) {
                    if (rc != Code.OK.intValue()) {
                        LOG.error("Error polling ZK for the available nodes: ",
                                KeeperException.create(KeeperException.Code.get(rc), path));
                        cb.operationComplete(rc, null);
                        return;
                    }

                    cb.operationComplete(rc, nodes);

                }
            }, null);
        }
    }, null);
}

From source file:org.apache.curator.x.async.details.BackgroundProcs.java

License:Apache License

static <T> BackgroundProc<T> makeProc(Function<CuratorEvent, T> proc) {
    return (event, future) -> {
        if (event.getResultCode() == 0) {
            future.complete(proc.apply(event));
        } else {/*w w  w .j av  a 2  s. co m*/
            future.completeExceptionally(
                    KeeperException.create(KeeperException.Code.get(event.getResultCode()), event.getPath()));
        }
        return null;
    };
}

From source file:org.apache.distributedlog.impl.subscription.ZKSubscriptionsStore.java

License:Apache License

@Override
public CompletableFuture<Map<String, DLSN>> getLastCommitPositions() {
    final CompletableFuture<Map<String, DLSN>> result = new CompletableFuture<Map<String, DLSN>>();
    try {// w w w  .  ja  va 2 s. com
        this.zkc.get().getChildren(this.zkPath, false, new AsyncCallback.Children2Callback() {
            @Override
            public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
                if (KeeperException.Code.NONODE.intValue() == rc) {
                    result.complete(new HashMap<String, DLSN>());
                } else if (KeeperException.Code.OK.intValue() != rc) {
                    result.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc), path));
                } else {
                    getLastCommitPositions(result, children);
                }
            }
        }, null);
    } catch (ZooKeeperClient.ZooKeeperConnectionException zkce) {
        result.completeExceptionally(zkce);
    } catch (InterruptedException ie) {
        result.completeExceptionally(new DLInterruptedException("getLastCommitPositions was interrupted", ie));
    }
    return result;
}

From source file:org.apache.distributedlog.impl.subscription.ZKSubscriptionStateStore.java

License:Apache License

CompletableFuture<DLSN> getLastCommitPositionFromZK() {
    final CompletableFuture<DLSN> result = new CompletableFuture<DLSN>();
    try {//from www  .j  a v a2  s .  co m
        logger.debug("Reading last commit position from path {}", zkPath);
        zooKeeperClient.get().getData(zkPath, false, new AsyncCallback.DataCallback() {
            @Override
            public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
                logger.debug("Read last commit position from path {}: rc = {}", zkPath, rc);
                if (KeeperException.Code.NONODE.intValue() == rc) {
                    result.complete(DLSN.NonInclusiveLowerBound);
                } else if (KeeperException.Code.OK.intValue() != rc) {
                    result.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc), path));
                } else {
                    try {
                        DLSN dlsn = DLSN.deserialize(new String(data, Charsets.UTF_8));
                        result.complete(dlsn);
                    } catch (Exception t) {
                        logger.warn("Invalid last commit position found from path {}", zkPath, t);
                        // invalid dlsn recorded in subscription state store
                        result.complete(DLSN.NonInclusiveLowerBound);
                    }
                }
            }
        }, null);
    } catch (ZooKeeperClient.ZooKeeperConnectionException zkce) {
        result.completeExceptionally(zkce);
    } catch (InterruptedException ie) {
        result.completeExceptionally(new DLInterruptedException("getLastCommitPosition was interrupted", ie));
    }
    return result;
}

From source file:org.apache.distributedlog.util.Utils.java

License:Apache License

private static void handleKeeperExceptionCode(int rc, String pathOrMessage, CompletableFuture<Void> result) {
    if (KeeperException.Code.OK.intValue() == rc) {
        result.complete(null);/*from  w ww.j av  a2s.  c o m*/
    } else if (DistributedLogConstants.ZK_CONNECTION_EXCEPTION_RESULT_CODE == rc) {
        result.completeExceptionally(new ZooKeeperClient.ZooKeeperConnectionException(pathOrMessage));
    } else if (DistributedLogConstants.DL_INTERRUPTED_EXCEPTION_RESULT_CODE == rc) {
        result.completeExceptionally(new DLInterruptedException(pathOrMessage));
    } else {
        result.completeExceptionally(KeeperException.create(KeeperException.Code.get(rc), pathOrMessage));
    }
}