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.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);
    }/*from  ww w  .  j a v  a  2s .  c  o m*/
    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);
    }/*from  w w  w .  j  a v  a2  s  .  c o 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);
        }/* www . j  a v a 2s  .  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 .jav a 2s  . com*/
            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));
            }//  ww w.  jav  a2 s  .c om
            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.stream.resolver.ZKStreamResolver.java

License:Apache License

@VisibleForTesting
ZooKeeper getZk() throws StreamException, InterruptedException {
    if (null == this.zk) {
        try {/*from  w  w  w . ja  v  a  2 s . co  m*/
            this.zk = ZooKeeperClient.createConnectedZooKeeper(zkServers, conf.getZkSessionTimeoutMs());
        } catch (KeeperException e) {
            throw new ZKException(e.code().intValue(), "Failed to create zookeeper client to " + zkServers);
        } catch (IOException e) {
            throw new MetadataException("Encountered IO Exception on creating zookeeper client to " + zkServers,
                    e);
        }
    }
    return this.zk;
}

From source file:org.apache.bookkeeper.stream.resolver.ZKStreamResolver.java

License:Apache License

private StreamFactoryMetadata resolve(String zkPath) throws StreamException, InterruptedException {
    byte[] data;/*  w  ww. ja  va 2s. co m*/
    try {
        data = getZk().getData(zkPath, false, new Stat());
    } catch (KeeperException.NoNodeException nne) {
        return null;
    } catch (KeeperException e) {
        throw new ZKException(e.code().intValue(), "Failed to resolve stream factory metadata from " + zkPath);
    }
    if (null == data || 0 == data.length) {
        return null;
    }
    try {
        return StreamFactoryMetadata.deserialize(data);
    } catch (StreamException e) {
        logger.warn("Invalid stream factory metadata found in {}", zkPath, e);
    }
    return null;
}

From source file:org.apache.bookkeeper.stream.resolver.ZKStreamResolver.java

License:Apache License

@Override
public void bind(StreamFactoryMetadata metadata, URI uri) throws StreamException, InterruptedException {
    String zkPath = uri.getPath();
    byte[] data = metadata.serialize();
    try {/*from  www  .  jav  a  2  s .c om*/
        ZkUtils.createFullPathOptimistic(getZk(), zkPath, data, ZooDefs.Ids.OPEN_ACL_UNSAFE,
                CreateMode.PERSISTENT);
    } catch (KeeperException.NodeExistsException nee) {
        try {
            getZk().setData(zkPath, data, -1);
        } catch (KeeperException e) {
            throw new ZKException(e.code().intValue(),
                    "Failed to bind stream factory metadata to zookeeper path " + zkPath);
        }
    } catch (KeeperException e) {
        throw new ZKException(e.code().intValue(),
                "Failed to create zookeeper path " + zkPath + " to bind metadata");
    }
}

From source file:org.apache.bookkeeper.stream.resolver.ZKStreamResolver.java

License:Apache License

@Override
public void unbind(URI uri) throws StreamException, InterruptedException {
    String zkPath = uri.getPath();
    byte[] data = new byte[0];
    try {/*  w  w w .ja va  2s. c o  m*/
        getZk().setData(zkPath, data, -1);
    } catch (KeeperException.NoNodeException nne) {
        return;
    } catch (KeeperException e) {
        throw new ZKException(e.code().intValue(),
                "Failed to unbind stream factory metadata in zookeeper path " + zkPath);
    }
}

From source file:org.apache.bookkeeper.stream.storage.impl.cluster.ZkClusterMetadataStoreTest.java

License:Apache License

@Test
public void testUnitialized() {
    ZkClusterMetadataStore newStore = new ZkClusterMetadataStore(curatorClient, zkServers,
            "/" + runtime.getMethodName() + "-new");

    try {//from w w w . j av a 2 s  .c o  m
        newStore.getClusterMetadata();
        fail("Should fail to get cluster metadata if not initialized");
    } catch (StorageRuntimeException sre) {
        assertTrue(sre.getCause() instanceof KeeperException);
        KeeperException cause = (KeeperException) sre.getCause();
        assertEquals(Code.NONODE, cause.code());
    }

    try {
        newStore.getClusterAssignmentData();
        fail("Should fail to get cluster assignment data if not initialized");
    } catch (StorageRuntimeException sre) {
        assertTrue(sre.getCause() instanceof KeeperException);
        KeeperException cause = (KeeperException) sre.getCause();
        assertEquals(Code.NONODE, cause.code());
    }
}