Example usage for org.apache.zookeeper KeeperException code

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

Introduction

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

Prototype

Code code

To view the source code for org.apache.zookeeper KeeperException code.

Click Source Link

Usage

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./* ww w .  j  a v a2  s . 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./*  ww  w .  j  a  va  2s. 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./*ww w.  j a  va  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.
 * @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   www .j a  v  a2s . c o  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//from w ww  .  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.bookkeeper.zookeeper.BkZooWorker.java

License:Apache License

/**
 * Check whether the given exception is recoverable by retry.
 *
 * @param exception given exception/*from  w  w w . j av a  2 s . c o  m*/
 * @return true if given exception is recoverable.
 */
public static boolean isRecoverableException(KeeperException exception) {
    return isRecoverableException(exception.code().intValue());
}

From source file:org.apache.bookkeeper.zookeeper.BkZooWorker.java

License:Apache License

public boolean allowRetry(int rc) {
    elapsedTimeMs = MathUtils.elapsedMSec(startTimeNanos);
    if (!BkZooWorker.isRecoverableException(rc)) {
        if (KeeperException.Code.OK.intValue() == rc) {
            statsLogger.registerSuccessfulEvent(MathUtils.elapsedMicroSec(startTimeNanos),
                    TimeUnit.MICROSECONDS);
        } else {// w ww  .  j  av  a  2 s. c o m
            statsLogger.registerFailedEvent(MathUtils.elapsedMicroSec(startTimeNanos), TimeUnit.MICROSECONDS);
        }
        return false;
    }
    ++attempts;
    return retryPolicy.allowRetry(attempts, elapsedTimeMs);
}

From source file:org.apache.bookkeeper.zookeeper.BkZooWorker.java

License:Apache License

/**
 * Check whether the given result code is recoverable by retry.
 *
 * @param rc result code// w w  w .  ja  v  a 2s. c o m
 * @return true if given result code is recoverable.
 */
public static boolean isRecoverableException(int rc) {
    return KeeperException.Code.CONNECTIONLOSS.intValue() == rc
            || KeeperException.Code.OPERATIONTIMEOUT.intValue() == rc
            || KeeperException.Code.SESSIONMOVED.intValue() == rc
            || KeeperException.Code.SESSIONEXPIRED.intValue() == rc;
}

From source file:org.apache.bookkeeper.zookeeper.TestZooKeeperClient.java

License:Apache License

@Test(timeout = 60000)
public void testRetryAsyncOperations() throws Exception {
    final int timeout = 2000;
    ZooKeeperClient client = ZooKeeperClient.createConnectedZooKeeperClient(zkUtil.getZooKeeperConnectString(),
            timeout, new HashSet<Watcher>(),
            new BoundExponentialBackoffRetryPolicy(timeout, timeout, Integer.MAX_VALUE));
    Assert.assertTrue("Client failed to connect an alive ZooKeeper.", client.getState().isConnected());

    String path = "/a";
    byte[] data = "test".getBytes();

    expireZooKeeperSession(client, timeout);
    logger.info("Create znode " + path);
    final CountDownLatch createLatch = new CountDownLatch(1);
    client.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new StringCallback() {

        @Override/*  w ww  .j a v  a2 s .c  o  m*/
        public void processResult(int rc, String path, Object ctx, String name) {
            if (KeeperException.Code.OK.intValue() == rc) {
                createLatch.countDown();
            }
        }

    }, null);
    createLatch.await();
    logger.info("Created znode " + path);

    expireZooKeeperSession(client, timeout);
    logger.info("Create znode " + path);
    final CountDownLatch create2Latch = new CountDownLatch(1);
    client.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new Create2Callback() {

        @Override
        public void processResult(int rc, String path, Object ctx, String name, Stat stat) {
            if (KeeperException.Code.NODEEXISTS.intValue() == rc) {
                create2Latch.countDown();
            }
        }

    }, null);
    create2Latch.await();
    logger.info("Created znode " + path);

    expireZooKeeperSession(client, timeout);
    logger.info("Exists znode " + path);
    final CountDownLatch existsLatch = new CountDownLatch(1);
    client.exists(path, false, new StatCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            if (KeeperException.Code.OK.intValue() == rc) {
                existsLatch.countDown();
            }
        }

    }, null);
    existsLatch.await();

    expireZooKeeperSession(client, timeout);
    final CountDownLatch getLatch = new CountDownLatch(1);
    logger.info("Get data from znode " + path);
    client.getData(path, false, new DataCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {
            if (KeeperException.Code.OK.intValue() == rc) {
                getLatch.countDown();
            }
        }

    }, null);
    getLatch.await();

    expireZooKeeperSession(client, timeout);
    logger.info("Create children under znode " + path);
    final CountDownLatch createChildLatch = new CountDownLatch(1);
    client.create(path + "/children", data, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new StringCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx, String name) {
            if (KeeperException.Code.OK.intValue() == rc) {
                createChildLatch.countDown();
            }
        }

    }, null);
    createChildLatch.await();

    expireZooKeeperSession(client, timeout);
    final CountDownLatch getChildLatch = new CountDownLatch(1);
    final AtomicReference<List<String>> children = new AtomicReference<List<String>>();
    client.getChildren(path, false, new Children2Callback() {

        @Override
        public void processResult(int rc, String path, Object ctx, List<String> childList, Stat stat) {
            if (KeeperException.Code.OK.intValue() == rc) {
                children.set(childList);
                getChildLatch.countDown();
            }
        }

    }, null);
    getChildLatch.await();
    Assert.assertNotNull(children.get());
    Assert.assertEquals(1, children.get().size());
    Assert.assertEquals("children", children.get().get(0));
    logger.info("Get children under znode " + path);

    expireZooKeeperSession(client, timeout);
    final CountDownLatch deleteChildLatch = new CountDownLatch(1);
    client.delete(path + "/children", -1, new VoidCallback() {

        @Override
        public void processResult(int rc, String path, Object ctx) {
            if (KeeperException.Code.OK.intValue() == rc) {
                deleteChildLatch.countDown();
            }
        }

    }, null);
    deleteChildLatch.await();
    logger.info("Delete children from znode " + path);
}

From source file:org.apache.bookkeeper.zookeeper.ZooKeeperWatcherBase.java

License:Apache License

/**
 * Waiting for the SyncConnected event from the ZooKeeper server
 *
 * @throws KeeperException//from   ww  w.java 2s.  com
 *             when there is no connection
 * @throws InterruptedException
 *             interrupted while waiting for connection
 */
public void waitForConnection() throws KeeperException, InterruptedException {
    if (!clientConnectLatch.await(zkSessionTimeOut, TimeUnit.MILLISECONDS)) {
        throw KeeperException.create(KeeperException.Code.CONNECTIONLOSS);
    }
}