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:fr.eurecom.hybris.mds.ZkRmds.java

License:Apache License

public void markOrphanKey(final String key, final Timestamp ts, final List<Kvs> savedReplicas) {
    new Thread(new Runnable() {
        public void run() {
            // create ZNode <root>-gc/orphans/<KvsKey>
            String path = gcOrphansDir + "/" + Utils.getKvsKey(key, ts);
            byte[] value = new Metadata(ts, null, 0, savedReplicas, null).serialize();
            try {
                ZkRmds.this.zkCli.create().forPath(path, value);
                logger.debug("GcMarker: marked {} as orphan", path);
            } catch (KeeperException e) {
                if (e.code() != KeeperException.Code.NODEEXISTS)
                    logger.warn("GcMarker: could not create orphan node " + path, e);
            } catch (Exception e) {
                logger.warn("GcMarker: could not create orphan node " + path, e);
            }//from ww w .  j  a  va2 s  .c o  m
        }
    }).start();
}

From source file:fr.eurecom.hybris.mds.ZkRmds.java

License:Apache License

public void removeOrphanKeys(Set<String> orphanKeys) {

    for (String key : orphanKeys) {
        String znodePath = this.gcOrphansDir + "/" + key;
        try {//from  w w  w .  ja va 2 s.  c  om
            this.zkCli.delete().forPath(znodePath);
        } catch (KeeperException e) {
            if (e.code() != KeeperException.Code.NONODE)
                logger.warn("Could not delete orphan ZNode " + znodePath, e);
        } catch (Exception e) {
            logger.warn("Could not delete orphan ZNode " + znodePath, e);
        }
    }
}

From source file:fr.eurecom.hybris.mds.ZkRmds.java

License:Apache License

public void removeStaleKey(String staleKey) {

    String znodePath = this.gcStaleDir + "/" + staleKey;
    try {/*from  w  w w  .j a v  a2 s .  c o  m*/
        this.zkCli.delete().forPath(znodePath);
    } catch (KeeperException e) {
        if (e.code() != KeeperException.Code.NONODE)
            logger.warn("Could not delete orphan ZNode " + znodePath, e);
    } catch (Exception e) {
        logger.warn("Could not delete orphan ZNode " + znodePath, e);
    }
}

From source file:fr.eurecom.hybris.test.mds.ConsulRmdsTest.java

License:Apache License

@Ignore
@Test//from w w  w .  j  a  v  a  2 s . c o  m
public void testTransactionalWrite() {

    LinkedHashMap<String, Metadata> map = new LinkedHashMap<String, Metadata>();
    LinkedHashMap<String, Stat> statMap = new LinkedHashMap<String, Stat>();
    int numKeys = 5, i = 0;
    byte[] hash = new byte[Utils.HASH_LENGTH];
    this.random.nextBytes(hash);
    List<Kvs> replicas = new ArrayList<Kvs>();
    replicas.add(new TransientKvs("transient", "d", true, 20));
    Metadata retrieved;
    for (i = 0; i < numKeys; i++) {
        String key = this.TEST_KEY_PREFIX + new BigInteger(50, this.random).toString(32);
        map.put(key, new Metadata(new Timestamp(this.random.nextInt(10), "qwe"), hash, 2, replicas, null));
        Stat st = new Stat();
        st.setVersion(-1);
        statMap.put(key, st);
    }

    // successful transactional write creating znodes
    try {
        mds.tsMultiWrite(map, statMap);
        for (Entry<String, Metadata> entry : map.entrySet()) {
            retrieved = mds.tsRead(entry.getKey(), new Stat());
            assertEquals(entry.getValue(), retrieved);
        }
    } catch (HybrisException e) {
        fail(e.getMessage());
    }

    // fails for mismatching versions of all the keys
    try {
        mds.tsMultiWrite(map, statMap);
        fail();
    } catch (HybrisException e) {
        KeeperException ke = (KeeperException) e.getCause();
        assertEquals(KeeperException.Code.NODEEXISTS, ke.code());
    }

    // successful transactional write modifying all znodes
    for (Entry<String, Stat> entry : statMap.entrySet())
        entry.getValue().setVersion(entry.getValue().getVersion() + 1);
    try {
        for (Entry<String, Metadata> entry : map.entrySet())
            map.put(entry.getKey(),
                    new Metadata(new Timestamp(this.random.nextInt(10), "NEW"), hash, 10, replicas, null));
        mds.tsMultiWrite(map, statMap);
        for (Entry<String, Metadata> entry : map.entrySet()) {
            retrieved = mds.tsRead(entry.getKey(), new Stat());
            assertEquals(entry.getValue(), retrieved);
        }
    } catch (HybrisException e) {
        fail(e.getMessage());
    }

    // successful transactional write with mix of modify and create nodes
    for (Entry<String, Stat> entry : statMap.entrySet())
        entry.getValue().setVersion(entry.getValue().getVersion() + 1);
    int numNewKeys = 3;
    for (i = 0; i < numNewKeys; i++) {
        String key = this.TEST_KEY_PREFIX + new BigInteger(50, this.random).toString(32);
        map.put(key, new Metadata(new Timestamp(this.random.nextInt(10), "qwe"), hash, 2, replicas, null));
        Stat st = new Stat();
        st.setVersion(-1);
        statMap.put(key, st);
    }
    try {
        mds.tsMultiWrite(map, statMap);
        for (Entry<String, Metadata> entry : map.entrySet()) {
            retrieved = mds.tsRead(entry.getKey(), new Stat());
            assertEquals(entry.getValue(), retrieved);
        }
    } catch (HybrisException e) {
        fail(e.getMessage());
    }

    // fails for mismatching version of one key
    for (Entry<String, Stat> entry : statMap.entrySet()) {
        if (this.random.nextInt(10) > 5)
            entry.getValue().setVersion(entry.getValue().getVersion() + 1);
        else
            entry.getValue().setVersion(123);
    }
    try {
        mds.tsMultiWrite(map, statMap);
        fail();
    } catch (HybrisException e) {
        KeeperException ke = (KeeperException) e.getCause();
        assertEquals(KeeperException.Code.BADVERSION, ke.code());
    }
}

From source file:fr.eurecom.hybris.test.mds.MdsManagerTest.java

License:Apache License

@Test
public void testDeleteNotExistingKey() {

    String key = this.TEST_KEY_PREFIX + new BigInteger(50, this.random).toString(32);
    try {/*from   www . java  2 s .c o  m*/
        Timestamp ts1 = new Timestamp(1, "BBBB");
        Timestamp ts2 = new Timestamp(1, "AAAA");
        Timestamp ts3 = new Timestamp(1, "CCCC");

        mds.delete(key, Metadata.getTombstone(ts1), -1); // writes the tombstone no matter which znode version
        mds.delete(key, Metadata.getTombstone(ts2), 5); // finds a smaller znode version, retries (because ts is greater) and eventually succeeds
        mds.delete(key, Metadata.getTombstone(ts3), 5); // finds a smaller znode version, silently fails

        Stat stat = new Stat();
        Metadata md = mds.tsRead(key, stat);
        assertNotNull(md);
        assertEquals(md.getTs(), ts2);
        assertNull(md.getChunksLst());
        assertNull(md.getHash());
        assertEquals(0, md.getSize());
        assertTrue(md.isTombstone());
    } catch (HybrisException e) {
        e.printStackTrace();
        fail();
    }

    key = this.TEST_KEY_PREFIX + new BigInteger(50, this.random).toString(32);
    try {
        mds.delete(key, Metadata.getTombstone(new Timestamp(1, "QWERTY")), 7); // writes the tombstone anyway
        fail();
    } catch (HybrisException e) {
        KeeperException ke = (KeeperException) e.getCause();
        assertEquals(KeeperException.Code.NONODE, ke.code());
    }
}

From source file:fr.eurecom.hybris.test.mds.ZkRmdsTest.java

License:Apache License

@Test
public void testTransactionalWrite() {

    LinkedHashMap<String, Metadata> map = new LinkedHashMap<String, Metadata>();
    LinkedHashMap<String, Stat> statMap = new LinkedHashMap<String, Stat>();
    int numKeys = 5, i = 0;
    byte[] hash = new byte[Utils.HASH_LENGTH];
    this.random.nextBytes(hash);
    List<Kvs> replicas = new ArrayList<Kvs>();
    replicas.add(new TransientKvs("transient", "d", true, 20));
    Metadata retrieved;//  www .j  av  a2 s . c om
    for (i = 0; i < numKeys; i++) {
        String key = this.TEST_KEY_PREFIX + new BigInteger(50, this.random).toString(32);
        map.put(key, new Metadata(new Timestamp(this.random.nextInt(10), "qwe"), hash, 2, replicas, null));
        Stat st = new Stat();
        st.setVersion(-1);
        statMap.put(key, st);
    }

    // successful transactional write creating znodes
    try {
        mds.tsMultiWrite(map, statMap);
        for (Entry<String, Metadata> entry : map.entrySet()) {
            retrieved = mds.tsRead(entry.getKey(), new Stat());
            assertEquals(entry.getValue(), retrieved);
        }
    } catch (HybrisException e) {
        fail(e.getMessage());
    }

    // fails for mismatching versions of all the keys
    try {
        mds.tsMultiWrite(map, statMap);
        fail();
    } catch (HybrisException e) {
        KeeperException ke = (KeeperException) e.getCause();
        assertEquals(KeeperException.Code.NODEEXISTS, ke.code());
    }

    // successful transactional write modifying all znodes
    for (Entry<String, Stat> entry : statMap.entrySet())
        entry.getValue().setVersion(entry.getValue().getVersion() + 1);
    try {
        for (Entry<String, Metadata> entry : map.entrySet())
            map.put(entry.getKey(),
                    new Metadata(new Timestamp(this.random.nextInt(10), "NEW"), hash, 10, replicas, null));
        mds.tsMultiWrite(map, statMap);
        for (Entry<String, Metadata> entry : map.entrySet()) {
            retrieved = mds.tsRead(entry.getKey(), new Stat());
            assertEquals(entry.getValue(), retrieved);
        }
    } catch (HybrisException e) {
        fail(e.getMessage());
    }

    // successful transactional write with mix of modify and create nodes
    for (Entry<String, Stat> entry : statMap.entrySet())
        entry.getValue().setVersion(entry.getValue().getVersion() + 1);
    int numNewKeys = 3;
    for (i = 0; i < numNewKeys; i++) {
        String key = this.TEST_KEY_PREFIX + new BigInteger(50, this.random).toString(32);
        map.put(key, new Metadata(new Timestamp(this.random.nextInt(10), "qwe"), hash, 2, replicas, null));
        Stat st = new Stat();
        st.setVersion(-1);
        statMap.put(key, st);
    }
    try {
        mds.tsMultiWrite(map, statMap);
        for (Entry<String, Metadata> entry : map.entrySet()) {
            retrieved = mds.tsRead(entry.getKey(), new Stat());
            assertEquals(entry.getValue(), retrieved);
        }
    } catch (HybrisException e) {
        fail(e.getMessage());
    }

    // fails for mismatching version of one key
    for (Entry<String, Stat> entry : statMap.entrySet()) {
        if (this.random.nextInt(10) > 5)
            entry.getValue().setVersion(entry.getValue().getVersion() + 1);
        else
            entry.getValue().setVersion(123);
    }
    try {
        mds.tsMultiWrite(map, statMap);
        fail();
    } catch (HybrisException e) {
        KeeperException ke = (KeeperException) e.getCause();
        assertEquals(KeeperException.Code.BADVERSION, ke.code());
    }
}

From source file:fr.eurecom.hybris.test.mds.ZkRmdsTest.java

License:Apache License

@Test
public void testDeleteNotExistingKey() {

    String key = this.TEST_KEY_PREFIX + new BigInteger(50, this.random).toString(32);
    try {//from   ww w  .j  a v a2 s.  co  m
        Timestamp ts1 = new Timestamp(1, "BBBB");
        Timestamp ts2 = new Timestamp(1, "AAAA");
        Timestamp ts3 = new Timestamp(1, "CCCC");

        mds.delete(key, Metadata.getTombstone(ts1), -1); // writes the tombstone no matter which znode version
        mds.delete(key, Metadata.getTombstone(ts2), 5); // finds a smaller znode version, retries (because ts is greater) and eventually succeeds
        mds.delete(key, Metadata.getTombstone(ts3), 5); // finds a smaller znode version, silently fails

        Stat stat = new Stat();
        Metadata md = mds.tsRead(key, stat);
        assertNotNull(md);
        assertEquals(md.getTs(), ts2);
        assertNull(md.getReplicasLst());
        assertNull(md.getHash());
        assertEquals(0, md.getSize());
        assertTrue(md.isTombstone());
    } catch (HybrisException e) {
        e.printStackTrace();
        fail();
    }

    key = this.TEST_KEY_PREFIX + new BigInteger(50, this.random).toString(32);
    try {
        mds.delete(key, Metadata.getTombstone(new Timestamp(1, "QWERTY")), 7); // writes the tombstone anyway
        fail();
    } catch (HybrisException e) {
        KeeperException ke = (KeeperException) e.getCause();
        assertEquals(KeeperException.Code.NONODE, ke.code());
    }
}

From source file:hws.util.ZkDataMonitor.java

License:Apache License

public void deleteAll(String path) throws KeeperException, InterruptedException {
    List<String> children = null;
    try {//from  w w w.  j a va 2  s .  com
        children = this.zk.getChildren(path, false);
    } catch (KeeperException e) {
        if (e.code() != KeeperException.Code.NONODE) {
            throw e;
        }
    }
    if (children != null) {
        for (String child : children) {
            deleteAll((new File(path)).getPath() + "/" + child);
        }
    }
    this.zk.delete(path, -1);
}

From source file:io.fabric8.groups.internal.ZooKeeperGroup.java

License:Apache License

void getDataAndStat(final String fullPath) throws Exception {
    Stat stat = new Stat();
    byte[] data = client.getData().storingStatIn(stat).usingWatcher(dataWatcher).forPath(fullPath);
    applyNewData(fullPath, KeeperException.Code.OK.intValue(), stat, data);
}

From source file:io.fabric8.groups.internal.ZooKeeperGroup.java

License:Apache License

private void applyNewData(String fullPath, int resultCode, Stat stat, byte[] bytes) {
    if (resultCode == KeeperException.Code.OK.intValue()) {
        // otherwise - node must have dropped or something - we should be getting another event
        ChildData<T> data = new ChildData<T>(fullPath, stat, bytes, decode(bytes));
        ChildData<T> previousData = currentData.put(fullPath, data);
        if (previousData == null || previousData.getStat().getVersion() != stat.getVersion()) {
            offerOperation(new EventOperation(this, GroupListener.GroupEvent.CHANGED));
        }/*w ww.j a v a 2  s.  c  o m*/
    }
}