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.client.TestBookieWatcher.java

License:Apache License

private void expireZooKeeperSession(ZooKeeper zk, int timeout)
        throws IOException, InterruptedException, KeeperException {
    final CountDownLatch latch = new CountDownLatch(1);
    ZooKeeper newZk = new ZooKeeper(zkUtil.getZooKeeperConnectString(), timeout, new Watcher() {

        @Override/*from w  ww . j a v a 2s. c  o m*/
        public void process(WatchedEvent event) {
            if (event.getType() == EventType.None && event.getState() == KeeperState.SyncConnected) {
                latch.countDown();
            }
        }

    }, zk.getSessionId(), zk.getSessionPasswd());
    if (!latch.await(timeout, TimeUnit.MILLISECONDS)) {
        throw KeeperException.create(KeeperException.Code.CONNECTIONLOSS);
    }
    newZk.close();
}

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

License:Apache License

/**
 * Process hash nodes in a given path/*  w ww  .j  av a2 s. co m*/
 */
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

/**
 * Removes ledger metadata from ZooKeeper if version matches.
 *
 * @param   ledgerId    ledger identifier
 * @param   version     local version of metadata znode
 * @param   cb          callback object//from   w  w  w .  j  av  a 2s. c  o m
 */
@Override
public void removeLedgerMetadata(final long ledgerId, final Version version, final GenericCallback<Void> cb) {
    int znodeVersion = -1;
    if (Version.NEW == version) {
        LOG.error("Request to delete ledger {} metadata with version set to the initial one", ledgerId);
        cb.operationComplete(BKException.Code.MetadataVersionException, (Void) null);
        return;
    } else if (Version.ANY != version) {
        if (!(version instanceof ZkVersion)) {
            LOG.info("Not an instance of ZKVersion: {}", ledgerId);
            cb.operationComplete(BKException.Code.MetadataVersionException, (Void) null);
            return;
        } else {
            znodeVersion = ((ZkVersion) version).getZnodeVersion();
        }
    }

    zk.delete(getLedgerPath(ledgerId), znodeVersion, new VoidCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx) {
            int bkRc;
            if (rc == KeeperException.Code.NONODE.intValue()) {
                LOG.warn("Ledger node does not exist in ZooKeeper: ledgerId={}", ledgerId);
                bkRc = BKException.Code.NoSuchLedgerExistsException;
            } else if (rc == KeeperException.Code.OK.intValue()) {
                // removed listener on ledgerId
                Set<LedgerMetadataListener> listenerSet = listeners.remove(ledgerId);
                if (null != listenerSet) {
                    LOG.debug(
                            "Remove registered ledger metadata listeners on ledger {} after ledger is deleted.",
                            ledgerId, listenerSet);
                } else {
                    LOG.debug("No ledger metadata listeners to remove from ledger {} when it's being deleted.",
                            ledgerId);
                }
                bkRc = BKException.Code.OK;
            } else {
                bkRc = BKException.Code.ZKException;
            }
            cb.operationComplete(bkRc, (Void) null);
        }
    }, 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  ww.ja  va2  s . 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);
}

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

License:Apache License

@Override
public void writeLedgerMetadata(final long ledgerId, final LedgerMetadata metadata,
        final GenericCallback<Void> cb) {
    Version v = metadata.getVersion();//w  w  w.  j  av a  2s . c  om
    if (Version.NEW == v || !(v instanceof ZkVersion)) {
        cb.operationComplete(BKException.Code.MetadataVersionException, null);
        return;
    }
    final ZkVersion zv = (ZkVersion) v;
    zk.setData(getLedgerPath(ledgerId), metadata.serialize(), zv.getZnodeVersion(), new StatCallback() {
        @Override
        public void processResult(int rc, String path, Object ctx, Stat stat) {
            if (KeeperException.Code.BADVERSION.intValue() == rc) {
                cb.operationComplete(BKException.Code.MetadataVersionException, null);
            } else if (KeeperException.Code.OK.intValue() == rc) {
                // update metadata version
                metadata.setVersion(zv.setZnodeVersion(stat.getVersion()));
                cb.operationComplete(BKException.Code.OK, null);
            } else {
                LOG.warn("Conditional update ledger metadata failed: ", KeeperException.Code.get(rc));
                cb.operationComplete(BKException.Code.ZKException, null);
            }
        }
    }, null);
}

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

License:Apache License

@Test
public void testCreateLedgerMetadataSuccess() throws Exception {
    long ledgerId = System.currentTimeMillis();
    String ledgerStr = String.valueOf(ledgerId);
    mockZkUtilsAsyncCreateFullPathOptimistic(ledgerStr, CreateMode.PERSISTENT,
            KeeperException.Code.OK.intValue(), ledgerStr);

    Versioned<LedgerMetadata> result = ledgerManager.createLedgerMetadata(ledgerId, metadata).get();

    assertEquals(new LongVersion(0), result.getVersion());
}

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

License:Apache License

@Test
public void testCreateLedgerMetadataNodeExists() throws Exception {
    long ledgerId = System.currentTimeMillis();
    String ledgerStr = String.valueOf(ledgerId);
    mockZkUtilsAsyncCreateFullPathOptimistic(ledgerStr, CreateMode.PERSISTENT,
            KeeperException.Code.NODEEXISTS.intValue(), null);
    Stat stat = mock(Stat.class);
    when(stat.getVersion()).thenReturn(1234);
    when(stat.getCtime()).thenReturn(metadata.getCtime());
    /*/* w  w w. ja v a 2 s.c  o  m*/
     * this is needed because in AbstractZkLedgerManager.readLedgerMetadata
     * if MetadataFormatVersion is >2, then for createLedgerMetadata if we
     * get NODEEXISTS exception then it will try to read to make sure ledger
     * creation is robust to ZK connection loss. Please check Issue #1967.
     */
    mockZkGetData(ledgerStr, false, KeeperException.Code.OK.intValue(), serDe.serialize(metadata), stat);
    try {
        result(ledgerManager.createLedgerMetadata(ledgerId, metadata));
        fail("Should fail to create ledger metadata if the ledger already exists");
    } catch (Exception e) {
        assertTrue(e instanceof BKException);
        BKException bke = (BKException) e;
        assertEquals(Code.LedgerExistException, bke.getCode());
    }
}

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

License:Apache License

@Test
public void testCreateLedgerMetadataException() throws Exception {
    long ledgerId = System.currentTimeMillis();
    String ledgerStr = String.valueOf(ledgerId);
    mockZkUtilsAsyncCreateFullPathOptimistic(ledgerStr, CreateMode.PERSISTENT,
            KeeperException.Code.CONNECTIONLOSS.intValue(), null);

    try {// ww w  .  j  a v a 2s  . c o m
        result(ledgerManager.createLedgerMetadata(ledgerId, metadata));
        fail("Should fail to create ledger metadata when encountering zookeeper exception");
    } catch (Exception e) {
        assertTrue(e instanceof BKException);
        BKException bke = (BKException) e;
        assertEquals(Code.ZKException, bke.getCode());
    }
}

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

License:Apache License

@Test
public void testRemoveLedgerMetadataSuccess() throws Exception {
    long ledgerId = System.currentTimeMillis();
    String ledgerStr = String.valueOf(ledgerId);
    LongVersion version = new LongVersion(1234L);

    mockZkDelete(ledgerStr, (int) version.getLongVersion(), KeeperException.Code.OK.intValue());

    ledgerManager.removeLedgerMetadata(ledgerId, version).get();

    verify(mockZk, times(1)).delete(eq(ledgerStr), eq(1234), any(VoidCallback.class), eq(null));
}

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

License:Apache License

@Test
public void testRemoveLedgerMetadataVersionAny() throws Exception {
    long ledgerId = System.currentTimeMillis();
    String ledgerStr = String.valueOf(ledgerId);

    mockZkDelete(ledgerStr, -1, KeeperException.Code.OK.intValue());

    ledgerManager.removeLedgerMetadata(ledgerId, Version.ANY).get();

    verify(mockZk, times(1)).delete(eq(ledgerStr), eq(-1), any(VoidCallback.class), eq(null));
}