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:com.alibaba.wasp.zookeeper.RecoverableZooKeeper.java

License:Apache License

/**
 * getData is an idemnpotent operation. Retry before throwing exception
 * @return Data/*from   ww  w .  j av  a2s  .c  o m*/
 */
public byte[] getData(String path, boolean watch, Stat stat) throws KeeperException, InterruptedException {
    RetryCounter retryCounter = retryCounterFactory.create();
    while (true) {
        try {
            byte[] revData = zk.getData(path, watch, stat);
            return this.removeMetaData(revData);
        } catch (KeeperException e) {
            switch (e.code()) {
            case CONNECTIONLOSS:
            case SESSIONEXPIRED:
            case OPERATIONTIMEOUT:
                retryOrThrow(retryCounter, e, "getData");
                break;

            default:
                throw e;
            }
        }
        retryCounter.sleepUntilNextRetry();
        retryCounter.useRetry();
    }
}

From source file:com.alibaba.wasp.zookeeper.RecoverableZooKeeper.java

License:Apache License

/**
 * setData is NOT an idempotent operation. Retry may cause BadVersion
 * Exception Adding an identifier field into the data to check whether
 * badversion is caused by the result of previous correctly setData
 * @return Stat instance/*from   w  w  w.j  av a2  s  .c  o  m*/
 */
public Stat setData(String path, byte[] data, int version) throws KeeperException, InterruptedException {
    RetryCounter retryCounter = retryCounterFactory.create();
    byte[] newData = appendMetaData(data);
    boolean isRetry = false;
    while (true) {
        try {
            return zk.setData(path, newData, version);
        } catch (KeeperException e) {
            switch (e.code()) {
            case CONNECTIONLOSS:
            case SESSIONEXPIRED:
            case OPERATIONTIMEOUT:
                retryOrThrow(retryCounter, e, "setData");
                break;
            case BADVERSION:
                if (isRetry) {
                    // try to verify whether the previous setData success or not
                    try {
                        Stat stat = new Stat();
                        byte[] revData = zk.getData(path, false, stat);
                        if (Bytes.compareTo(revData, newData) == 0) {
                            // the bad version is caused by previous successful setData
                            return stat;
                        }
                    } catch (KeeperException keeperException) {
                        // the ZK is not reliable at this moment. just throwing
                        // exception
                        throw keeperException;
                    }
                }
                // throw other exceptions and verified bad version exceptions
            default:
                throw e;
            }
        }
        retryCounter.sleepUntilNextRetry();
        retryCounter.useRetry();
        isRetry = true;
    }
}

From source file:com.alibaba.wasp.zookeeper.RecoverableZooKeeper.java

License:Apache License

private String createNonSequential(String path, byte[] data, List<ACL> acl, CreateMode createMode)
        throws KeeperException, InterruptedException {
    RetryCounter retryCounter = retryCounterFactory.create();
    boolean isRetry = false; // False for first attempt, true for all retries.
    while (true) {
        try {/*from  w w  w.j av  a 2 s .c  o m*/
            return zk.create(path, data, acl, createMode);
        } catch (KeeperException e) {
            switch (e.code()) {
            case NODEEXISTS:
                if (isRetry) {
                    // If the connection was lost, there is still a possibility that
                    // we have successfully created the node at our previous attempt,
                    // so we read the node and compare.
                    byte[] currentData = zk.getData(path, false, null);
                    if (currentData != null && Bytes.compareTo(currentData, data) == 0) {
                        // We successfully created a non-sequential node
                        return path;
                    }
                    LOG.error("Node " + path + " already exists with " + Bytes.toStringBinary(currentData)
                            + ", could not write " + Bytes.toStringBinary(data));
                    throw e;
                }
                LOG.error("Node " + path + " already exists and this is not a " + "retry");
                throw e;

            case CONNECTIONLOSS:
            case SESSIONEXPIRED:
            case OPERATIONTIMEOUT:
                retryOrThrow(retryCounter, e, "create");
                break;

            default:
                throw e;
            }
        }
        retryCounter.sleepUntilNextRetry();
        retryCounter.useRetry();
        isRetry = true;
    }
}

From source file:com.alibaba.wasp.zookeeper.RecoverableZooKeeper.java

License:Apache License

private String createSequential(String path, byte[] data, List<ACL> acl, CreateMode createMode)
        throws KeeperException, InterruptedException {
    RetryCounter retryCounter = retryCounterFactory.create();
    boolean first = true;
    String newPath = path + this.identifier;
    while (true) {
        try {//from   w  w w .  j a v a 2  s  .  c  om
            if (!first) {
                // Check if we succeeded on a previous attempt
                String previousResult = findPreviousSequentialNode(newPath);
                if (previousResult != null) {
                    return previousResult;
                }
            }
            first = false;
            return zk.create(newPath, data, acl, createMode);
        } catch (KeeperException e) {
            switch (e.code()) {
            case CONNECTIONLOSS:
            case SESSIONEXPIRED:
            case OPERATIONTIMEOUT:
                retryOrThrow(retryCounter, e, "create");
                break;

            default:
                throw e;
            }
        }
        retryCounter.sleepUntilNextRetry();
        retryCounter.useRetry();
    }
}

From source file:com.andyadc.menagerie.latches.ZkCountDownLatchTest.java

License:Apache License

@BeforeClass
public static void setupBeforeClass() throws Exception {

    zk = newZooKeeper();//w ww  .jav  a2s .  c om

    //be sure that the lock-place is created
    try {
        zk.create(baseBarrierPath, new byte[] {}, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } catch (KeeperException ke) {
        if (ke.code() != KeeperException.Code.NODEEXISTS)
            throw ke;
    }
}

From source file:com.andyadc.menagerie.latches.ZkCountDownLatchTest.java

License:Apache License

@AfterClass
public static void tearDownAfterClass() throws Exception {
    try {//from   w w  w .j  a va2  s . c o  m
        List<String> children = zk.getChildren(baseBarrierPath, false);
        for (String child : children) {
            zk.delete(baseBarrierPath + "/" + child, -1);
        }
        zk.delete(baseBarrierPath, -1);

    } catch (KeeperException ke) {
        if (ke.code() != KeeperException.Code.NONODE)
            throw ke;
    } finally {
        zk.close();
    }
}

From source file:com.andyadc.menagerie.latches.ZkCyclicBarrier.java

License:Apache License

private void breakBarrier(String message) {
    if (!isBroken()) {
        try {/*  ww  w . j  a  v a  2s.c  om*/
            zkSessionManager.getZooKeeper().create(getBrokenPath(), message.getBytes(), privileges,
                    CreateMode.PERSISTENT);
        } catch (KeeperException e) {
            //if the node already exists, someone else broke it first, so don't worry about it
            if (e.code() != KeeperException.Code.NODEEXISTS)
                throw new RuntimeException(e);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
    }
}

From source file:com.andyadc.menagerie.latches.ZkCyclicBarrierTest.java

License:Apache License

@Before
public void setup() throws Exception {
    zk = newZooKeeper();//  w  ww  .  ja  v a  2 s .c om

    //be sure that the lock-place is created
    try {
        zk.create(baseBarrierPath, new byte[] {}, ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    } catch (KeeperException ke) {
        if (ke.code() != KeeperException.Code.NODEEXISTS)
            throw ke;
    }
}

From source file:com.andyadc.menagerie.latches.ZkCyclicBarrierTest.java

License:Apache License

@After
public void tearDown() throws Exception {
    try {//from w  w  w . j  a v a2s .  co m
        List<String> children = zk.getChildren(baseBarrierPath, false);
        for (String child : children) {
            ZkUtils.recursiveSafeDelete(zk, baseBarrierPath + "/" + child, -1);
        }
        zk.delete(baseBarrierPath, -1);

    } catch (KeeperException ke) {
        if (ke.code() != KeeperException.Code.NONODE)
            throw ke;
    } finally {
        zk.close();
    }
}

From source file:com.andyadc.menagerie.ZkPrimitive.java

License:Apache License

/**
 * Ensures that the base node exists in ZooKeeper.
 * <p>//  ww w  .  j av  a  2  s. c  o  m
 * Note: This method does NOT create elements recursively--if the base node is a sub-node of a
 * node which doesn't exist, a NoNode Exception will be thrown.
 *
 * @throws RuntimeException wrapping a KeeperException if something goes wrong communicating with the ZooKeeper server
 *                          RuntimeException wrapping an InterruptedException if something goes wrong communicating with the ZooKeeper
 *                          Server.
 */
protected final void ensureNodeExists() {
    try {
        ZooKeeper zooKeeper = zkSessionManager.getZooKeeper();

        Stat stat = zooKeeper.exists(baseNode, false);
        if (stat == null) {
            zooKeeper.create(baseNode, emptyNode, privileges, CreateMode.PERSISTENT);
        }
    } catch (KeeperException e) {
        //if the node already exists, then we are happy, so ignore those exceptions
        if (e.code() != KeeperException.Code.NODEEXISTS)
            throw new RuntimeException(e);
    } catch (InterruptedException e) {
        throw new RuntimeException(e);
    }
}