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.emc.storageos.coordinator.client.service.impl.CoordinatorClientImpl.java

License:Open Source License

@Override
public <T extends CoordinatorSerializable> void persistRuntimeState(String key, T state)
        throws CoordinatorException {
    String path = String.format("%s/%s", ZkPath.STATE, key);

    try {/*w  w  w. ja v a 2  s  .c  o  m*/
        int lastSlash = path.lastIndexOf('/');
        String parentPath = path.substring(0, lastSlash);
        EnsurePath ensurePath = new EnsurePath(parentPath);
        ensurePath.ensure(_zkConnection.curator().getZookeeperClient());
    } catch (Exception e) {
        log.error(String.format("Failed to ensure path to key: %s", path), e);
    }

    try {
        byte[] data = state.encodeAsString().getBytes("UTF-8");
        // This is reported because the for loop's stop condition and incrementer don't act on the same variable to make sure loop ends
        // Here the loop can end (break or throw Exception) from inside, safe to suppress
        for (boolean exist = _zkConnection.curator().checkExists().forPath(path) != null;; exist = !exist) { // NOSONAR("squid:S1994")
            try {
                if (exist) {
                    _zkConnection.curator().setData().forPath(path, data);
                } else {
                    _zkConnection.curator().create().forPath(path, data);
                }
                break;
            } catch (KeeperException ex) {
                if (exist && ex.code() == KeeperException.Code.NONODE
                        || !exist && ex.code() == KeeperException.Code.NODEEXISTS) {
                    continue;
                }

                throw ex;
            }
        }
    } catch (Exception e) {
        log.info("Failed to persist runtime state e=", e);
        throw CoordinatorException.fatals.unableToPersistTheState(e);
    }
}

From source file:com.ery.estorm.zk.RecoverableZooKeeper.java

License:Apache License

/**
 * delete is an idempotent operation. Retry before throwing exception. This
 * function will not throw NoNodeException if the path does not exist.
 * /*from  w  w  w.j a  va2 s  .  co  m*/
 * @throws IOException
 */
public void delete(String path, int version) throws InterruptedException, KeeperException, IOException {
    try {
        RetryCounter retryCounter = retryCounterFactory.create();
        boolean isRetry = false; // False for first attempt, true for all
        // retries.
        while (true) {
            try {
                zk.delete(path, version);
                return;
            } catch (KeeperException e) {
                switch (e.code()) {
                case NONODE:
                    if (isRetry) {
                        LOG.info("Node " + path + " already deleted. Assuming a "
                                + "previous attempt succeeded.");
                        return;
                    }
                    LOG.warn("Node " + path + " already deleted, retry=" + isRetry);
                    throw e;

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

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

From source file:com.ery.estorm.zk.RecoverableZooKeeper.java

License:Apache License

/**
 * exists is an idempotent operation. Retry before throwing exception
 * //from   www.  ja  v a 2 s.  c o  m
 * @return A Stat instance
 * @throws IOException
 */
public Stat exists(String path, Watcher watcher) throws KeeperException, InterruptedException, IOException {
    try {
        RetryCounter retryCounter = retryCounterFactory.create();
        while (true) {
            try {
                return zk.exists(path, watcher);
            } catch (KeeperException e) {
                switch (e.code()) {
                case CONNECTIONLOSS:
                case SESSIONEXPIRED:
                    reconnectAfterExpiration();
                case OPERATIONTIMEOUT:
                    retryOrThrow(retryCounter, e, "exists");
                    break;

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

From source file:com.ery.estorm.zk.RecoverableZooKeeper.java

License:Apache License

/**
 * exists is an idempotent operation. Retry before throwing exception
 * //from  ww  w.j a  va 2s.  co  m
 * @return A Stat instance
 * @throws IOException
 */
public Stat exists(String path, boolean watch) throws KeeperException, InterruptedException, IOException {
    TraceScope traceScope = null;
    try {
        traceScope = Trace.startSpan("RecoverableZookeeper.exists");
        RetryCounter retryCounter = retryCounterFactory.create();
        while (true) {
            try {
                return zk.exists(path, watch);
            } catch (KeeperException e) {
                switch (e.code()) {
                case CONNECTIONLOSS:
                case SESSIONEXPIRED:
                    reconnectAfterExpiration();
                case OPERATIONTIMEOUT:
                    retryOrThrow(retryCounter, e, "exists");
                    break;

                default:
                    throw e;
                }
            }
            retryCounter.sleepUntilNextRetry();
            retryCounter.useRetry();
        }
    } finally {
        if (traceScope != null)
            traceScope.close();
    }
}

From source file:com.ery.estorm.zk.RecoverableZooKeeper.java

License:Apache License

/**
 * getChildren is an idempotent operation. Retry before throwing exception
 * /*from www.j  av a 2  s.c  o m*/
 * @return List of children znodes
 * @throws IOException
 */
public List<String> getChildren(String path, Watcher watcher)
        throws KeeperException, InterruptedException, IOException {
    TraceScope traceScope = null;
    try {
        traceScope = Trace.startSpan("RecoverableZookeeper.getChildren");
        RetryCounter retryCounter = retryCounterFactory.create();
        while (true) {
            try {
                return zk.getChildren(path, watcher);
            } catch (KeeperException e) {
                switch (e.code()) {
                case CONNECTIONLOSS:
                case SESSIONEXPIRED:
                    reconnectAfterExpiration();
                case OPERATIONTIMEOUT:
                    retryOrThrow(retryCounter, e, "getChildren");
                    break;

                default:
                    throw e;
                }
            }
            retryCounter.sleepUntilNextRetry();
            retryCounter.useRetry();
        }
    } finally {
        if (traceScope != null)
            traceScope.close();
    }
}

From source file:com.ery.estorm.zk.RecoverableZooKeeper.java

License:Apache License

/**
 * getChildren is an idempotent operation. Retry before throwing exception
 * /*from   ww w .java 2s  . c  om*/
 * @return List of children znodes
 * @throws IOException
 */
public List<String> getChildren(String path, boolean watch)
        throws KeeperException, InterruptedException, IOException {
    TraceScope traceScope = null;
    try {
        traceScope = Trace.startSpan("RecoverableZookeeper.getChildren");
        RetryCounter retryCounter = retryCounterFactory.create();
        while (true) {
            try {
                return zk.getChildren(path, watch);
            } catch (KeeperException e) {
                switch (e.code()) {
                case CONNECTIONLOSS:
                case SESSIONEXPIRED:
                    reconnectAfterExpiration();
                case OPERATIONTIMEOUT:
                    retryOrThrow(retryCounter, e, "getChildren");
                    break;

                default:
                    throw e;
                }
            }
            retryCounter.sleepUntilNextRetry();
            retryCounter.useRetry();
        }
    } finally {
        if (traceScope != null)
            traceScope.close();
    }
}

From source file:com.ery.estorm.zk.RecoverableZooKeeper.java

License:Apache License

/**
 * getData is an idempotent operation. Retry before throwing exception
 * //from   ww w.j  a  v  a  2 s . c o m
 * @return Data
 * @throws IOException
 */
public byte[] getData(String path, Watcher watcher, Stat stat)
        throws KeeperException, InterruptedException, IOException {
    TraceScope traceScope = null;
    try {
        traceScope = Trace.startSpan("RecoverableZookeeper.getData");
        RetryCounter retryCounter = retryCounterFactory.create();
        while (true) {
            try {
                byte[] revData = zk.getData(path, watcher, stat);
                return this.removeMetaData(revData);
            } catch (KeeperException e) {
                switch (e.code()) {
                case CONNECTIONLOSS:
                case SESSIONEXPIRED:
                    reconnectAfterExpiration();
                case OPERATIONTIMEOUT:
                    retryOrThrow(retryCounter, e, "getData");
                    break;

                default:
                    throw e;
                }
            }
            retryCounter.sleepUntilNextRetry();
            retryCounter.useRetry();
        }
    } finally {
        if (traceScope != null)
            traceScope.close();
    }
}

From source file:com.ery.estorm.zk.RecoverableZooKeeper.java

License:Apache License

/**
 * getData is an idemnpotent operation. Retry before throwing exception
 * /*w  ww .  j  a  v a2 s . co m*/
 * @return Data
 * @throws IOException
 */
public byte[] getData(String path, boolean watch, Stat stat)
        throws KeeperException, InterruptedException, IOException {
    TraceScope traceScope = null;
    try {
        traceScope = Trace.startSpan("RecoverableZookeeper.getData");
        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:
                    reconnectAfterExpiration();
                case OPERATIONTIMEOUT:
                    retryOrThrow(retryCounter, e, "getData");
                    break;

                default:
                    throw e;
                }
            }
            retryCounter.sleepUntilNextRetry();
            retryCounter.useRetry();
        }
    } finally {
        if (traceScope != null)
            traceScope.close();
    }
}

From source file:com.ery.estorm.zk.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
 * /*  w w w. j a  v a2  s  .c  om*/
 * @return Stat instance
 * @throws IOException
 */
public Stat setData(String path, byte[] data, int version)
        throws KeeperException, InterruptedException, IOException {
    TraceScope traceScope = null;
    try {
        traceScope = Trace.startSpan("RecoverableZookeeper.setData");
        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:
                    reconnectAfterExpiration();
                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;
        }
    } finally {
        if (traceScope != null)
            traceScope.close();
    }
}

From source file:com.ery.estorm.zk.RecoverableZooKeeper.java

License:Apache License

private String createNonSequential(String path, byte[] data, List<ACL> acl, CreateMode createMode)
        throws KeeperException, InterruptedException, IOException {
    RetryCounter retryCounter = retryCounterFactory.create();
    boolean isRetry = false; // False for first attempt, true for all
    // retries.//  w  ww  .  j a v  a2s  . c om
    while (true) {
        try {
            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.info("Node " + path + " already exists and this is not a " + "retry");
                throw e;

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

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