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.bookie.BookieInitializationTest.java

License:Apache License

/**
 * Verify the bookie reg. Restarting bookie server will wait for the session
 * timeout when previous reg node exists in zk. On zNode delete event,
 * should continue startup/* www  .  jav a 2 s . c om*/
 */
@Test(timeout = 20000)
public void testBookieRegistration() throws Exception {
    File tmpDir = createTempDir("bookie", "test");

    final ServerConfiguration conf = TestBKConfiguration.newServerConfiguration().setZkServers(null)
            .setJournalDirName(tmpDir.getPath()).setLedgerDirNames(new String[] { tmpDir.getPath() });

    final String bkRegPath = conf.getZkAvailableBookiesPath() + "/"
            + InetAddress.getLocalHost().getHostAddress() + ":" + conf.getBookiePort();

    MockBookie b = new MockBookie(conf);
    b.zk = zkc;
    b.testRegisterBookie(conf);
    Stat bkRegNode1 = zkc.exists(bkRegPath, false);
    Assert.assertNotNull("Bookie registration node doesn't exists!", bkRegNode1);

    // simulating bookie restart, on restart bookie will create new
    // zkclient and doing the registration.
    createNewZKClient();
    b.zk = newzk;

    // deleting the znode, so that the bookie registration should
    // continue successfully on NodeDeleted event
    new Thread() {
        @Override
        public void run() {
            try {
                Thread.sleep(conf.getZkTimeout() / 3);
                zkc.delete(bkRegPath, -1);
            } catch (Exception e) {
                // Not handling, since the testRegisterBookie will fail
                LOG.error("Failed to delete the znode :" + bkRegPath, e);
            }
        }
    }.start();
    try {
        b.testRegisterBookie(conf);
    } catch (IOException e) {
        Throwable t = e.getCause();
        if (t instanceof KeeperException) {
            KeeperException ke = (KeeperException) t;
            Assert.assertTrue("ErrorCode:" + ke.code() + ", Registration node exists",
                    ke.code() != KeeperException.Code.NODEEXISTS);
        }
        throw e;
    }

    // verify ephemeral owner of the bkReg znode
    Stat bkRegNode2 = newzk.exists(bkRegPath, false);
    Assert.assertNotNull("Bookie registration has been failed", bkRegNode2);
    Assert.assertTrue(
            "Bookie is referring to old registration znode:" + bkRegNode1 + ", New ZNode:" + bkRegNode2,
            bkRegNode1.getEphemeralOwner() != bkRegNode2.getEphemeralOwner());
}

From source file:org.apache.bookkeeper.bookie.BookieInitializationTest.java

License:Apache License

/**
 * Verify the bookie registration, it should throw
 * KeeperException.NodeExistsException if the znode still exists even after
 * the zk session timeout./*from   www  .  j a  va  2 s.  c o  m*/
 */
@Test(timeout = 30000)
public void testRegNodeExistsAfterSessionTimeOut() throws Exception {
    File tmpDir = createTempDir("bookie", "test");

    ServerConfiguration conf = TestBKConfiguration.newServerConfiguration().setZkServers(null)
            .setJournalDirName(tmpDir.getPath()).setLedgerDirNames(new String[] { tmpDir.getPath() });

    String bkRegPath = conf.getZkAvailableBookiesPath() + "/" + InetAddress.getLocalHost().getHostAddress()
            + ":" + conf.getBookiePort();

    MockBookie b = new MockBookie(conf);
    b.zk = zkc;
    b.testRegisterBookie(conf);
    Stat bkRegNode1 = zkc.exists(bkRegPath, false);
    Assert.assertNotNull("Bookie registration node doesn't exists!", bkRegNode1);

    // simulating bookie restart, on restart bookie will create new
    // zkclient and doing the registration.
    createNewZKClient();
    b.zk = newzk;
    try {
        b.testRegisterBookie(conf);
        fail("Should throw NodeExistsException as the znode is not getting expired");
    } catch (IOException e) {
        Throwable t = e.getCause();
        if (t instanceof KeeperException) {
            KeeperException ke = (KeeperException) t;
            Assert.assertTrue("ErrorCode:" + ke.code() + ", Registration node doesn't exists",
                    ke.code() == KeeperException.Code.NODEEXISTS);

            // verify ephemeral owner of the bkReg znode
            Stat bkRegNode2 = newzk.exists(bkRegPath, false);
            Assert.assertNotNull("Bookie registration has been failed", bkRegNode2);
            Assert.assertTrue("Bookie wrongly registered. Old registration znode:" + bkRegNode1 + ", New znode:"
                    + bkRegNode2, bkRegNode1.getEphemeralOwner() == bkRegNode2.getEphemeralOwner());
            return;
        }
        throw e;
    }
}

From source file:org.apache.bookkeeper.client.BookieWatcher.java

License:Apache License

@Override
public void processResult(int rc, String path, Object ctx, List<String> children) {

    if (rc != KeeperException.Code.OK.intValue()) {
        //logger.error("Error while reading bookies", KeeperException.create(Code.get(rc), path));
        // try the read after a second again
        scheduler.schedule(reReadTask, ZK_CONNECT_BACKOFF_SEC, TimeUnit.SECONDS);
        return;//  w  w  w .jav  a 2 s . co m
    }

    // Read the bookie addresses into a set for efficient lookup
    Set<InetSocketAddress> newBookieAddrs = new HashSet<InetSocketAddress>();
    for (String bookieAddrString : children) {
        InetSocketAddress bookieAddr;
        try {
            bookieAddr = StringUtils.parseAddr(bookieAddrString);
        } catch (IOException e) {
            logger.error("Could not parse bookie address: " + bookieAddrString + ", ignoring this bookie");
            continue;
        }
        newBookieAddrs.add(bookieAddr);
    }

    synchronized (this) {
        knownBookies = newBookieAddrs;
    }
}

From source file:org.apache.bookkeeper.client.BookieWatcher.java

License:Apache License

/**
 * Blocks until bookies are read from zookeeper, used in the {@link BookKeeper} constructor.
 * @throws InterruptedException/*from w  w w  . jav a  2 s . c  om*/
 * @throws KeeperException
 */
public void readBookiesBlocking() throws InterruptedException, KeeperException {
    final LinkedBlockingQueue<Integer> queue = new LinkedBlockingQueue<Integer>();
    readBookies(new ChildrenCallback() {
        public void processResult(int rc, String path, Object ctx, List<String> children) {
            try {
                BookieWatcher.this.processResult(rc, path, ctx, children);
                queue.put(rc);
            } catch (InterruptedException e) {
                logger.error("Interruped when trying to read bookies in a blocking fashion");
                throw new RuntimeException(e);
            }
        }
    });
    int rc = queue.take();

    if (rc != KeeperException.Code.OK.intValue()) {
        throw KeeperException.create(Code.get(rc));
    }
}

From source file:org.apache.bookkeeper.client.BookKeeperAdmin.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.//from  ww w . j a  v 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 BookieSocketAddress bookieSrc, final BookieSocketAddress bookieDest,
        final RecoverCallback cb, final Object context) {
    // Sync ZK to make sure we're reading the latest bookie data.
    zk.sync(bookiesPath, 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.client.BookKeeperAdmin.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 av 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.
 */
private void getAvailableBookies(final BookieSocketAddress bookieSrc, final BookieSocketAddress bookieDest,
        final RecoverCallback cb, final Object context) {
    final List<BookieSocketAddress> availableBookies = new LinkedList<BookieSocketAddress>();
    if (bookieDest != null) {
        availableBookies.add(bookieDest);
        // Now poll ZK to get the active ledgers
        getActiveLedgers(bookieSrc, bookieDest, cb, context, availableBookies);
    } else {
        zk.getChildren(bookiesPath, 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) {
                    if (BookKeeperConstants.READONLY.equals(bookieNode)) {
                        // exclude the readonly node from available bookies.
                        continue;
                    }
                    BookieSocketAddress addr;
                    try {
                        addr = new BookieSocketAddress(bookieNode);
                    } catch (UnknownHostException nhe) {
                        LOG.error("Bookie Node retrieved from ZK has invalid name format: " + bookieNode);
                        cb.recoverComplete(BKException.Code.ZKException, context);
                        return;
                    }
                    availableBookies.add(addr);
                }
                // Now poll ZK to get the active ledgers
                getActiveLedgers(bookieSrc, null, cb, context, availableBookies);
            }
        }, null);
    }
}

From source file:org.apache.bookkeeper.client.LedgerCreateOp.java

License:Apache License

/**
 * Implements ZooKeeper string callback.
 * /*from w w w.ja  va2  s  .c o m*/
 * @see org.apache.zookeeper.AsyncCallback.StringCallback#processResult(int, java.lang.String, java.lang.Object, java.lang.String)
 */
public void processResult(int rc, String path, Object ctx, String name) {

    if (rc != KeeperException.Code.OK.intValue()) {
        LOG.error("Could not create node for ledger",
                KeeperException.create(KeeperException.Code.get(rc), path));
        cb.createComplete(BKException.Code.ZKException, null, this.ctx);
        return;
    }

    /*
     * Extract ledger id.
     */
    long ledgerId;
    try {
        ledgerId = StringUtils.getLedgerId(name);
    } catch (IOException e) {
        LOG.error("Could not extract ledger-id from path:" + path, e);
        cb.createComplete(BKException.Code.ZKException, null, this.ctx);
        return;
    }

    /*
     * Adding bookies to ledger handle
     */

    ArrayList<InetSocketAddress> ensemble;
    try {
        ensemble = bk.bookieWatcher.getNewBookies(metadata.ensembleSize);
    } catch (BKNotEnoughBookiesException e) {
        LOG.error("Not enough bookies to create ledger" + ledgerId);
        cb.createComplete(e.getCode(), null, this.ctx);
        return;
    }

    /*
     * Add ensemble to the configuration
     */
    metadata.addEnsemble(new Long(0), ensemble);
    try {
        lh = new LedgerHandle(bk, ledgerId, metadata, digestType, passwd);
    } catch (GeneralSecurityException e) {
        LOG.error("Security exception while creating ledger: " + ledgerId, e);
        cb.createComplete(BKException.Code.DigestNotInitializedException, null, this.ctx);
        return;
    }

    lh.writeLedgerConfig(this, null);

}

From source file:org.apache.bookkeeper.client.LedgerHandle.java

License:Apache License

/**
 * Same as public version of asynClose except that this one takes an
 * additional parameter which is the return code to hand to all the pending
 * add ops// w  w w. ja va2 s . c  om
 * 
 * @param cb
 * @param ctx
 * @param rc
 */
private void asyncClose(final CloseCallback cb, final Object ctx, final int rc) {

    bk.mainWorkerPool.submitOrdered(ledgerId, new SafeRunnable() {

        @Override
        public void safeRun() {
            metadata.length = length;
            // Close operation is idempotent, so no need to check if we are
            // already closed
            metadata.close(lastAddConfirmed);
            errorOutPendingAdds(rc);
            lastAddPushed = lastAddConfirmed;

            if (LOG.isDebugEnabled()) {
                LOG.debug("Closing ledger: " + ledgerId + " at entryId: " + metadata.close
                        + " with this many bytes: " + metadata.length);
            }

            writeLedgerConfig(new StatCallback() {
                @Override
                public void processResult(int rc, String path, Object subctx, Stat stat) {
                    if (rc != KeeperException.Code.OK.intValue()) {
                        cb.closeComplete(BKException.Code.ZKException, LedgerHandle.this, ctx);
                    } else {
                        cb.closeComplete(BKException.Code.OK, LedgerHandle.this, ctx);
                    }
                }
            }, null);

        }
    });
}

From source file:org.apache.bookkeeper.client.LedgerHandle.java

License:Apache License

void handleBookieFailure(InetSocketAddress addr, final int bookieIndex) {
    InetSocketAddress newBookie;/*  www .j  a va 2s  .  c  o  m*/

    if (LOG.isDebugEnabled()) {
        LOG.debug("Handling failure of bookie: " + addr + " index: " + bookieIndex);
    }

    try {
        newBookie = bk.bookieWatcher.getAdditionalBookie(metadata.currentEnsemble);
    } catch (BKNotEnoughBookiesException e) {
        LOG.error("Could not get additional bookie to remake ensemble, closing ledger: " + ledgerId);
        handleUnrecoverableErrorDuringAdd(e.getCode());
        return;
    }

    final ArrayList<InetSocketAddress> newEnsemble = new ArrayList<InetSocketAddress>(metadata.currentEnsemble);
    newEnsemble.set(bookieIndex, newBookie);

    if (LOG.isDebugEnabled()) {
        LOG.debug("Changing ensemble from: " + metadata.currentEnsemble + " to: " + newEnsemble
                + " for ledger: " + ledgerId + " starting at entry: " + (lastAddConfirmed + 1));
    }

    metadata.addEnsemble(lastAddConfirmed + 1, newEnsemble);

    writeLedgerConfig(new StatCallback() {
        @Override
        public void processResult(final int rc, String path, Object ctx, Stat stat) {

            bk.mainWorkerPool.submitOrdered(ledgerId, new SafeRunnable() {
                @Override
                public void safeRun() {
                    if (rc != KeeperException.Code.OK.intValue()) {
                        LOG.error("Could not persist ledger metadata while changing ensemble to: " + newEnsemble
                                + " , closing ledger");
                        handleUnrecoverableErrorDuringAdd(BKException.Code.ZKException);
                        return;
                    }

                    for (PendingAddOp pendingAddOp : pendingAddOps) {
                        pendingAddOp.unsetSuccessAndSendWriteRequest(bookieIndex);
                    }
                }
            });

        }
    }, null);

}

From source file:org.apache.bookkeeper.client.LedgerOpenOp.java

License:Apache License

/**
 * Implements ZooKeeper data callback.//from w  w w.j a  va2  s  .c o m
 * @see org.apache.zookeeper.AsyncCallback.DataCallback#processResult(int, String, Object, byte[], Stat)
 */
public void processResult(int rc, String path, Object ctx, byte[] data, Stat stat) {

    if (rc == KeeperException.Code.NONODE.intValue()) {
        if (LOG.isDebugEnabled()) {
            LOG.debug("No such ledger: " + ledgerId,
                    KeeperException.create(KeeperException.Code.get(rc), path));
        }
        cb.openComplete(BKException.Code.NoSuchLedgerExistsException, null, this.ctx);
        return;
    }
    if (rc != KeeperException.Code.OK.intValue()) {
        LOG.error("Could not read metadata for ledger: " + ledgerId,
                KeeperException.create(KeeperException.Code.get(rc), path));
        cb.openComplete(BKException.Code.ZKException, null, this.ctx);
        return;
    }

    LedgerMetadata metadata;
    try {
        metadata = LedgerMetadata.parseConfig(data);
    } catch (IOException e) {
        LOG.error("Could not parse ledger metadata for ledger: " + ledgerId, e);
        cb.openComplete(BKException.Code.ZKException, null, this.ctx);
        return;
    }

    try {
        lh = new LedgerHandle(bk, ledgerId, metadata, digestType, passwd);
    } catch (GeneralSecurityException e) {
        LOG.error("Security exception while opening ledger: " + ledgerId, e);
        cb.openComplete(BKException.Code.DigestNotInitializedException, null, this.ctx);
        return;
    }

    if (metadata.close != LedgerMetadata.NOTCLOSED) {
        // Ledger was closed properly
        cb.openComplete(BKException.Code.OK, lh, this.ctx);
        return;
    }

    lh.recover(new GenericCallback<Void>() {
        @Override
        public void operationComplete(int rc, Void result) {
            if (rc != BKException.Code.OK) {
                cb.openComplete(BKException.Code.LedgerRecoveryException, null, LedgerOpenOp.this.ctx);
            } else {
                cb.openComplete(BKException.Code.OK, lh, LedgerOpenOp.this.ctx);
            }
        }
    });
}