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

public static KeeperException create(Code code) 

Source Link

Document

All non-specific keeper exceptions should be constructed via this factory method in order to guarantee consistency in error codes and such.

Usage

From source file:org.apache.bookkeeper.mledger.impl.MetaStoreImplZookeeper.java

License:Apache License

@Override
public void getManagedLedgerInfo(final String ledgerName, final MetaStoreCallback<ManagedLedgerInfo> callback) {
    // Try to get the content or create an empty node
    zk.getData(prefix + ledgerName, false, (DataCallback) (rc, path, ctx, readData, stat) -> {
        executor.submit(safeRun(() -> {
            if (rc == KeeperException.Code.OK.intValue()) {
                try {
                    ManagedLedgerInfo.Builder builder = ManagedLedgerInfo.newBuilder();
                    TextFormat.merge(new String(readData, Encoding), builder);
                    ManagedLedgerInfo info = builder.build();
                    info = updateMLInfoTimestamp(info);
                    callback.operationComplete(info, new ZKVersion(stat.getVersion()));
                } catch (ParseException e) {
                    callback.operationFailed(new MetaStoreException(e));
                }// ww  w.j a v  a  2 s  . co m
            } else if (rc == KeeperException.Code.NONODE.intValue()) {
                log.info("Creating '{}{}'", prefix, ledgerName);

                StringCallback createcb = new StringCallback() {
                    public void processResult(int rc, String path, Object ctx, String name) {
                        if (rc == KeeperException.Code.OK.intValue()) {
                            ManagedLedgerInfo info = ManagedLedgerInfo.getDefaultInstance();
                            callback.operationComplete(info, new ZKVersion(0));
                        } else {
                            callback.operationFailed(new MetaStoreException(
                                    KeeperException.create(KeeperException.Code.get(rc))));
                        }
                    }
                };

                ZkUtils.asyncCreateFullPathOptimistic(zk, prefix + ledgerName, new byte[0], Acl,
                        CreateMode.PERSISTENT, createcb, null);
            } else {
                callback.operationFailed(
                        new MetaStoreException(KeeperException.create(KeeperException.Code.get(rc))));
            }
        }));
    }, null);
}

From source file:org.apache.bookkeeper.mledger.impl.MetaStoreImplZookeeper.java

License:Apache License

@Override
public void asyncUpdateLedgerIds(String ledgerName, ManagedLedgerInfo mlInfo, Version version,
        final MetaStoreCallback<Void> callback) {

    ZKVersion zkVersion = (ZKVersion) version;
    if (log.isDebugEnabled()) {
        log.debug("[{}] Updating metadata version={} with content={}", ledgerName, zkVersion.version, mlInfo);
    }/*from   ww w  . ja v a  2 s  . c o m*/

    zk.setData(prefix + ledgerName, mlInfo.toString().getBytes(Encoding), zkVersion.version,
            new StatCallback() {
                public void processResult(int rc, String path, Object zkCtx, Stat stat) {
                    executor.submit(safeRun(() -> {
                        if (log.isDebugEnabled()) {
                            log.debug("[{}] UpdateLedgersIdsCallback.processResult rc={} newVersion={}",
                                    ledgerName, Code.get(rc), stat != null ? stat.getVersion() : "null");
                        }
                        MetaStoreException status = null;
                        if (rc == KeeperException.Code.BADVERSION.intValue()) {
                            // Content has been modified on ZK since our last read
                            status = new BadVersionException(
                                    KeeperException.create(KeeperException.Code.get(rc)));
                            callback.operationFailed(status);
                        } else if (rc != KeeperException.Code.OK.intValue()) {
                            status = new MetaStoreException(
                                    KeeperException.create(KeeperException.Code.get(rc)));
                            callback.operationFailed(status);
                        } else {
                            callback.operationComplete(null, new ZKVersion(stat.getVersion()));
                        }
                    }));
                }
            }, null);
}

From source file:org.apache.bookkeeper.mledger.impl.MetaStoreImplZookeeper.java

License:Apache License

@Override
public void getCursors(final String ledgerName, final MetaStoreCallback<List<String>> callback) {
    if (log.isDebugEnabled()) {
        log.debug("[{}] Get cursors list", ledgerName);
    }//  ww  w.ja  v a 2s .c om
    zk.getChildren(prefix + ledgerName, false, new Children2Callback() {
        public void processResult(int rc, String path, Object ctx, List<String> children, Stat stat) {
            executor.submit(safeRun(() -> {
                if (log.isDebugEnabled()) {
                    log.debug("[{}] getConsumers complete rc={} children={}", ledgerName, Code.get(rc),
                            children);
                }
                if (rc != KeeperException.Code.OK.intValue()) {
                    callback.operationFailed(
                            new MetaStoreException(KeeperException.create(KeeperException.Code.get(rc))));
                    return;
                }

                if (log.isDebugEnabled()) {
                    log.debug("[{}] Get childrend completed version={}", ledgerName, stat.getVersion());
                }
                ZKVersion version = new ZKVersion(stat.getVersion());
                callback.operationComplete(children, version);
            }));
        }
    }, null);
}

From source file:org.apache.bookkeeper.mledger.impl.MetaStoreImplZookeeper.java

License:Apache License

@Override
public void asyncGetCursorInfo(String ledgerName, String consumerName,
        final MetaStoreCallback<ManagedCursorInfo> callback) {
    String path = prefix + ledgerName + "/" + consumerName;
    if (log.isDebugEnabled()) {
        log.debug("Reading from {}", path);
    }/* w w  w  .  j a va  2s .co m*/

    zk.getData(path, false, (DataCallback) (rc, path1, ctx, data, stat) -> {
        executor.submit(safeRun(() -> {
            if (rc != KeeperException.Code.OK.intValue()) {
                callback.operationFailed(
                        new MetaStoreException(KeeperException.create(KeeperException.Code.get(rc))));
            } else {
                try {
                    ManagedCursorInfo.Builder info = ManagedCursorInfo.newBuilder();
                    TextFormat.merge(new String(data, Encoding), info);
                    callback.operationComplete(info.build(), new ZKVersion(stat.getVersion()));
                } catch (ParseException e) {
                    callback.operationFailed(new MetaStoreException(e));
                }
            }
        }));
    }, null);

    if (log.isDebugEnabled()) {
        log.debug("Reading from {} ok", path);
    }
}

From source file:org.apache.bookkeeper.mledger.impl.MetaStoreImplZookeeper.java

License:Apache License

@Override
public void asyncUpdateCursorInfo(final String ledgerName, final String cursorName,
        final ManagedCursorInfo info, Version version, final MetaStoreCallback<Void> callback) {
    log.info("[{}] [{}] Updating cursor info ledgerId={} mark-delete={}:{}", ledgerName, cursorName,
            info.getCursorsLedgerId(), info.getMarkDeleteLedgerId(), info.getMarkDeleteEntryId());

    String path = prefix + ledgerName + "/" + cursorName;
    byte[] content = info.toString().getBytes(Encoding);

    if (version == null) {
        if (log.isDebugEnabled()) {
            log.debug("[{}] Creating consumer {} on meta-data store with {}", ledgerName, cursorName, info);
        }//from   w  w w.  j a  v  a  2 s .c om
        zk.create(path, content, Acl, CreateMode.PERSISTENT, (rc, path1, ctx, name) -> {
            executor.submit(safeRun(() -> {
                if (rc != KeeperException.Code.OK.intValue()) {
                    log.warn("[{}] Error creating cosumer {} node on meta-data store with {}: ", ledgerName,
                            cursorName, info, KeeperException.Code.get(rc));
                    callback.operationFailed(
                            new MetaStoreException(KeeperException.create(KeeperException.Code.get(rc))));
                } else {
                    if (log.isDebugEnabled()) {
                        log.debug("[{}] Created consumer {} on meta-data store with {}", ledgerName, cursorName,
                                info);
                    }
                    callback.operationComplete(null, new ZKVersion(0));
                }
            }));
        }, null);
    } else {
        ZKVersion zkVersion = (ZKVersion) version;
        if (log.isDebugEnabled()) {
            log.debug("[{}] Updating consumer {} on meta-data store with {}", ledgerName, cursorName, info);
        }
        zk.setData(path, content, zkVersion.version, (rc, path1, ctx, stat) -> {
            executor.submit(safeRun(() -> {
                if (rc == KeeperException.Code.BADVERSION.intValue()) {
                    callback.operationFailed(
                            new BadVersionException(KeeperException.create(KeeperException.Code.get(rc))));
                } else if (rc != KeeperException.Code.OK.intValue()) {
                    callback.operationFailed(
                            new MetaStoreException(KeeperException.create(KeeperException.Code.get(rc))));
                } else {
                    callback.operationComplete(null, new ZKVersion(stat.getVersion()));
                }
            }));
        }, null);
    }
}

From source file:org.apache.bookkeeper.mledger.impl.MetaStoreImplZookeeper.java

License:Apache License

@Override
public void asyncRemoveCursor(final String ledgerName, final String consumerName,
        final MetaStoreCallback<Void> callback) {
    log.info("[{}] Remove consumer={}", ledgerName, consumerName);
    zk.delete(prefix + ledgerName + "/" + consumerName, -1, (rc, path, ctx) -> {
        executor.submit(safeRun(() -> {
            if (log.isDebugEnabled()) {
                log.debug("[{}] [{}] zk delete done. rc={}", ledgerName, consumerName, Code.get(rc));
            }// w  w  w. j  a  v  a 2  s.  c  o  m
            if (rc == KeeperException.Code.OK.intValue()) {
                callback.operationComplete(null, null);
            } else {
                callback.operationFailed(
                        new MetaStoreException(KeeperException.create(KeeperException.Code.get(rc))));
            }
        }));
    }, null);
}

From source file:org.apache.bookkeeper.mledger.impl.MetaStoreImplZookeeper.java

License:Apache License

@Override
public void removeManagedLedger(String ledgerName, MetaStoreCallback<Void> callback) {
    log.info("[{}] Remove ManagedLedger", ledgerName);
    zk.delete(prefix + ledgerName, -1, (rc, path, ctx) -> {
        executor.submit(safeRun(() -> {
            if (log.isDebugEnabled()) {
                log.debug("[{}] zk delete done. rc={}", ledgerName, Code.get(rc));
            }//w ww  .j  a va2 s  . co m
            if (rc == KeeperException.Code.OK.intValue()) {
                callback.operationComplete(null, null);
            } else {
                callback.operationFailed(
                        new MetaStoreException(KeeperException.create(KeeperException.Code.get(rc))));
            }
        }));
    }, null);
}

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

License:Apache License

/**
 * Create zookeeper path recursively and optimistically. This method can throw
 * any of the KeeperExceptions which can be thrown by ZooKeeper#create.
 * KeeperException.NodeExistsException will only be thrown if the full path specified
 * by _path_ already exists. The existence of any parent znodes is not an error
 * condition./*  w ww  .ja  v a2s  . c o  m*/
 *
 * @param zkc
 *            - ZK instance
 * @param path
 *            - znode path
 * @param data
 *            - znode data
 * @param acl
 *            - Acl of the zk path
 * @param createMode
 *            - Create mode of zk path
 * @throws KeeperException
 *             if the server returns a non-zero error code, or invalid ACL
 * @throws InterruptedException
 *             if the transaction is interrupted
 */
public static void createFullPathOptimistic(ZooKeeper zkc, String path, byte[] data, final List<ACL> acl,
        final CreateMode createMode) throws KeeperException, InterruptedException {
    final CountDownLatch latch = new CountDownLatch(1);
    final AtomicInteger rc = new AtomicInteger(Code.OK.intValue());
    asyncCreateFullPathOptimistic(zkc, path, data, acl, createMode, new StringCallback() {
        @Override
        public void processResult(int rc2, String path, Object ctx, String name) {
            rc.set(rc2);
            latch.countDown();
        }
    }, null);
    latch.await();
    if (rc.get() != Code.OK.intValue()) {
        throw KeeperException.create(Code.get(rc.get()));
    }
}

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

License:Apache License

/**
 * Waiting for the SyncConnected event from the ZooKeeper server
 *
 * @throws KeeperException/*w ww .  j ava 2 s  . c om*/
 *             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);
    }
}

From source file:org.apache.curator.framework.imps.CuratorFrameworkImpl.java

License:Apache License

@SuppressWarnings({ "ThrowableResultOfMethodCallIgnored" })
private <DATA_TYPE> boolean checkBackgroundRetry(OperationAndData<DATA_TYPE> operationAndData,
        CuratorEvent event) {/*from  w w w .j av a  2s  .  co  m*/
    boolean doRetry = false;
    if (client.getRetryPolicy().allowRetry(operationAndData.getThenIncrementRetryCount(),
            operationAndData.getElapsedTimeMs(), operationAndData)) {
        doRetry = true;
    } else {
        if (operationAndData.getErrorCallback() != null) {
            operationAndData.getErrorCallback().retriesExhausted(operationAndData);
        }

        if (operationAndData.getCallback() != null) {
            sendToBackgroundCallback(operationAndData, event);
        }

        KeeperException.Code code = KeeperException.Code.get(event.getResultCode());
        Exception e = null;
        try {
            e = (code != null) ? KeeperException.create(code) : null;
        } catch (Throwable t) {
            ThreadUtils.checkInterrupted(t);
        }
        if (e == null) {
            e = new Exception("Unknown result codegetResultCode()");
        }

        validateConnection(codeToState(code));
        logError("Background operation retry gave up", e);
    }
    return doRetry;
}