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:io.vertx.config.zookeeper.ZookeeperConfigStore.java

License:Apache License

private void retrieve(CuratorEvent event, Handler<AsyncResult<Buffer>> completionHandler) {
    KeeperException.Code code = KeeperException.Code.get(event.getResultCode());
    if (code == KeeperException.Code.OK) {
        completionHandler.handle(Future.succeededFuture(Buffer.buffer(event.getData())));
    } else if (code == KeeperException.Code.NONODE) {
        completionHandler.handle(Future.succeededFuture(Buffer.buffer("{}")));
    } else {// www. j  a v  a 2  s .c o  m
        completionHandler.handle(Future.failedFuture(KeeperException.create(code, path)));
    }
}

From source file:org.apache.accumulo.core.zookeeper.ZooUtil.java

License:Apache License

public static void recursiveCopyPersistent(ZooKeeper zk, String source, String destination,
        NodeExistsPolicy policy) throws KeeperException, InterruptedException {
    Stat stat = null;/*from w w  w.j  a v  a  2  s. c  o m*/
    if (!exists(zk, source))
        throw KeeperException.create(Code.NONODE, source);
    if (exists(zk, destination)) {
        switch (policy) {
        case OVERWRITE:
            break;
        case SKIP:
            return;
        case FAIL:
        default:
            throw KeeperException.create(Code.NODEEXISTS, source);
        }
    }

    stat = new Stat();
    byte[] data = zk.getData(source, false, stat);
    if (stat.getEphemeralOwner() == 0) {
        if (data == null)
            throw KeeperException.create(Code.NONODE, source);
        putPersistentData(zk, destination, data, policy);
        if (stat.getNumChildren() > 0)
            for (String child : zk.getChildren(source, false))
                recursiveCopyPersistent(zk, source + "/" + child, destination + "/" + child, policy);
    }
}

From source file:org.apache.accumulo.fate.zookeeper.ZooUtil.java

License:Apache License

public static void recursiveCopyPersistent(ZooKeeperConnectionInfo info, String source, String destination,
        NodeExistsPolicy policy) throws KeeperException, InterruptedException {
    Stat stat = null;//  w  w w.j  av a2 s  .com
    if (!exists(info, source))
        throw KeeperException.create(Code.NONODE, source);
    if (exists(info, destination)) {
        switch (policy) {
        case OVERWRITE:
            break;
        case SKIP:
            return;
        case FAIL:
        default:
            throw KeeperException.create(Code.NODEEXISTS, source);
        }
    }

    stat = new Stat();
    byte[] data = getData(info, source, stat);

    if (stat.getEphemeralOwner() == 0) {
        if (data == null)
            throw KeeperException.create(Code.NONODE, source);
        putPersistentData(info, destination, data, policy);
        if (stat.getNumChildren() > 0) {
            List<String> children;
            final Retry retry = RETRY_FACTORY.create();
            while (true) {
                try {
                    children = getZooKeeper(info).getChildren(source, false);
                    break;
                } catch (KeeperException e) {
                    final Code c = e.code();
                    if (c == Code.CONNECTIONLOSS || c == Code.OPERATIONTIMEOUT || c == Code.SESSIONEXPIRED) {
                        retryOrThrow(retry, e);
                    } else {
                        throw e;
                    }
                }
                retry.waitForNextAttempt();
            }
            for (String child : children) {
                recursiveCopyPersistent(info, source + "/" + child, destination + "/" + child, policy);
            }
        }
    }
}

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./*  w  ww  .ja  v  a  2 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 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  w  w  .j  av a2  s . co 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.
 * //  w w w .  ja  v a  2 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.LedgerOpenOp.java

License:Apache License

/**
 * Implements ZooKeeper data callback./*  ww  w.j  av  a  2  s .  co  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);
            }
        }
    });
}

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

License:Apache License

/**
 * Process hash nodes in a given path/*from   www .j  ava 2s  . c  om*/
 */
void asyncProcessLevelNodes(final String path, final Processor<String> processor,
        final AsyncCallback.VoidCallback finalCb, final Object context, final int successRc,
        final int failureRc) {
    zk.sync(path, new AsyncCallback.VoidCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx) {
            if (rc != Code.OK.intValue()) {
                LOG.error("Error syncing path " + path + " when getting its chidren: ",
                        KeeperException.create(KeeperException.Code.get(rc), path));
                finalCb.processResult(failureRc, null, context);
                return;
            }

            zk.getChildren(path, false, new AsyncCallback.ChildrenCallback() {
                @Override
                public void processResult(int rc, String path, Object ctx, List<String> levelNodes) {
                    if (rc != Code.OK.intValue()) {
                        LOG.error("Error polling hash nodes of " + path,
                                KeeperException.create(KeeperException.Code.get(rc), path));
                        finalCb.processResult(failureRc, null, context);
                        return;
                    }
                    AsyncListProcessor<String> listProcessor = new AsyncListProcessor<String>(scheduler);
                    // process its children
                    listProcessor.process(levelNodes, processor, finalCb, context, successRc, failureRc);
                }
            }, null);
        }
    }, null);
}

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

License:Apache License

@Override
public void createLedgerMetadata(final long ledgerId, final LedgerMetadata metadata,
        final GenericCallback<Void> ledgerCb) {
    String ledgerPath = getLedgerPath(ledgerId);
    StringCallback scb = new StringCallback() {
        @Override/*from  ww w  .  j  a  va2s.  c  o m*/
        public void processResult(int rc, String path, Object ctx, String name) {
            if (rc == Code.OK.intValue()) {
                // update version
                metadata.setVersion(new ZkVersion(0));
                ledgerCb.operationComplete(BKException.Code.OK, null);
            } else if (rc == Code.NODEEXISTS.intValue()) {
                LOG.warn("Failed to create ledger metadata for {} which already exist", ledgerId);
                ledgerCb.operationComplete(BKException.Code.LedgerExistException, null);
            } else {
                LOG.error("Could not create node for ledger {}", ledgerId,
                        KeeperException.create(Code.get(rc), path));
                ledgerCb.operationComplete(BKException.Code.ZKException, null);
            }
        }
    };
    List<ACL> zkAcls = ZkUtils.getACLs(conf);
    ZkUtils.asyncCreateFullPathOptimistic(zk, ledgerPath, metadata.serialize(), zkAcls, CreateMode.PERSISTENT,
            scb, null);
}

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

License:Apache License

protected void readLedgerMetadata(final long ledgerId, final GenericCallback<LedgerMetadata> readCb,
        Watcher watcher) {//from w w w . j av a 2s  .c o  m
    zk.getData(getLedgerPath(ledgerId), watcher, new DataCallback() {
        @Override
        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));
                }
                readCb.operationComplete(BKException.Code.NoSuchLedgerExistsException, null);
                return;
            }
            if (rc != KeeperException.Code.OK.intValue()) {
                LOG.error("Could not read metadata for ledger: " + ledgerId,
                        KeeperException.create(KeeperException.Code.get(rc), path));
                readCb.operationComplete(BKException.Code.ZKException, null);
                return;
            }
            if (stat == null) {
                LOG.error("Could not parse ledger metadata for ledger: " + ledgerId + ". Stat object is null");
                readCb.operationComplete(BKException.Code.ZKException, null);
                return;
            }
            LedgerMetadata metadata;
            try {
                metadata = LedgerMetadata.parseConfig(data, new ZkVersion(stat.getVersion()),
                        Optional.of(stat.getCtime()));
            } catch (IOException e) {
                LOG.error("Could not parse ledger metadata for ledger: " + ledgerId, e);
                readCb.operationComplete(BKException.Code.ZKException, null);
                return;
            }
            readCb.operationComplete(BKException.Code.OK, metadata);
        }
    }, null);
}