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.accumulo.fate.zookeeper.ZooReaderWriter.java

License:Apache License

@Override
public void delete(String path, int version) throws InterruptedException, KeeperException {
    final Retry retry = getRetryFactory().create();
    while (true) {
        try {//w  w w.j av a 2s.  com
            getZooKeeper().delete(path, version);
            return;
        } catch (KeeperException e) {
            final Code code = e.code();
            if (code == Code.NONODE) {
                if (retry.hasRetried()) {
                    // A retried delete could have deleted the node, assume that was the case
                    log.debug("Delete saw no node on a retry. Assuming node was deleted");
                    return;
                }

                throw e;
            } else if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT
                    || code == Code.SESSIONEXPIRED) {
                // retry if we have more attempts to do so
                retryOrThrow(retry, e);
            } else {
                throw e;
            }
        }

        retry.waitForNextAttempt();
    }
}

From source file:org.apache.accumulo.fate.zookeeper.ZooReaderWriter.java

License:Apache License

@Override
public byte[] mutate(String zPath, byte[] createValue, List<ACL> acl, Mutator mutator) throws Exception {
    if (createValue != null) {
        while (true) {
            final Retry retry = getRetryFactory().create();
            try {
                getZooKeeper().create(zPath, createValue, acl, CreateMode.PERSISTENT);
                return createValue;
            } catch (KeeperException ex) {
                final Code code = ex.code();
                if (code == Code.NODEEXISTS) {
                    // expected
                    break;
                } else if (code == Code.OPERATIONTIMEOUT || code == Code.CONNECTIONLOSS
                        || code == Code.SESSIONEXPIRED) {
                    retryOrThrow(retry, ex);
                } else {
                    throw ex;
                }/*  w  ww.j a v a  2 s  .co  m*/
            }

            retry.waitForNextAttempt();
        }
    }
    do {
        final Retry retry = getRetryFactory().create();
        Stat stat = new Stat();
        byte[] data = getData(zPath, false, stat);
        data = mutator.mutate(data);
        if (data == null)
            return data;
        try {
            getZooKeeper().setData(zPath, data, stat.getVersion());
            return data;
        } catch (KeeperException ex) {
            final Code code = ex.code();
            if (code == Code.BADVERSION) {
                // Retry, but don't increment. This makes it backwards compatible with the infinite
                // loop that previously happened. I'm not sure if that's really desirable though.
            } else if (code == Code.OPERATIONTIMEOUT || code == Code.CONNECTIONLOSS
                    || code == Code.SESSIONEXPIRED) {
                retryOrThrow(retry, ex);
                retry.waitForNextAttempt();
            } else {
                throw ex;
            }
        }
    } while (true);
}

From source file:org.apache.accumulo.fate.zookeeper.ZooUtil.java

License:Apache License

/**
 * This method will delete a node and all its children from zookeeper
 *
 * @param zPath/* ww  w  .  j a  v a  2s .  c o  m*/
 *          the path to delete
 */
static void recursiveDelete(ZooKeeperConnectionInfo info, String zPath, NodeMissingPolicy policy)
        throws KeeperException, InterruptedException {
    if (policy.equals(NodeMissingPolicy.CREATE))
        throw new IllegalArgumentException(policy.name() + " is invalid for this operation");
    try {
        List<String> children;
        final Retry retry = RETRY_FACTORY.create();
        while (true) {
            try {
                children = getZooKeeper(info).getChildren(zPath, false);
                break;
            } catch (KeeperException e) {
                final Code c = e.code();
                if (c == Code.CONNECTIONLOSS || c == Code.OPERATIONTIMEOUT || c == Code.SESSIONEXPIRED) {
                    retryOrThrow(retry, e);
                } else {
                    throw e;
                }
            }
            retry.waitForNextAttempt();
        }
        for (String child : children)
            recursiveDelete(info, zPath + "/" + child, NodeMissingPolicy.SKIP);

        Stat stat;
        while (true) {
            try {
                stat = getZooKeeper(info).exists(zPath, null);
                // Node exists
                if (stat != null) {
                    try {
                        // Try to delete it. We don't care if there was an update to the node
                        // since we got the Stat, just delete all versions (-1).
                        getZooKeeper(info).delete(zPath, -1);
                        return;
                    } catch (NoNodeException e) {
                        // If the node is gone now, it's ok if we have SKIP
                        if (policy.equals(NodeMissingPolicy.SKIP)) {
                            return;
                        }
                        throw e;
                    }
                    // Let other KeeperException bubble to the outer catch
                } else {
                    // If the stat is null, the node is now gone which is fine.
                    return;
                }
            } catch (KeeperException e) {
                final Code c = e.code();
                if (c == Code.CONNECTIONLOSS || c == Code.OPERATIONTIMEOUT || c == Code.SESSIONEXPIRED) {
                    retryOrThrow(retry, e);
                } else {
                    throw e;
                }
            }

            retry.waitForNextAttempt();
        }
    } catch (KeeperException e) {
        if (policy.equals(NodeMissingPolicy.SKIP) && e.code().equals(KeeperException.Code.NONODE))
            return;
        throw e;
    }
}

From source file:org.apache.accumulo.fate.zookeeper.ZooUtil.java

License:Apache License

private static boolean putData(ZooKeeperConnectionInfo info, String zPath, byte[] data, CreateMode mode,
        int version, NodeExistsPolicy policy, List<ACL> acls) throws KeeperException, InterruptedException {
    if (policy == null)
        policy = NodeExistsPolicy.FAIL;//from  ww w.jav  a  2s.  c  o  m

    final Retry retry = RETRY_FACTORY.create();
    while (true) {
        try {
            getZooKeeper(info).create(zPath, data, acls, mode);
            return true;
        } catch (KeeperException e) {
            final Code code = e.code();
            if (code == Code.NODEEXISTS) {
                switch (policy) {
                case SKIP:
                    return false;
                case OVERWRITE:
                    // overwrite the data in the node when it already exists
                    try {
                        getZooKeeper(info).setData(zPath, data, version);
                        return true;
                    } catch (KeeperException e2) {
                        final Code code2 = e2.code();
                        if (code2 == Code.NONODE) {
                            // node delete between create call and set data, so try create call again
                            continue;
                        } else if (code2 == Code.CONNECTIONLOSS || code2 == Code.OPERATIONTIMEOUT
                                || code2 == Code.SESSIONEXPIRED) {
                            retryOrThrow(retry, e2);
                            break;
                        } else {
                            // unhandled exception on setData()
                            throw e2;
                        }
                    }
                default:
                    throw e;
                }
            } else if (code == Code.CONNECTIONLOSS || code == Code.OPERATIONTIMEOUT
                    || code == Code.SESSIONEXPIRED) {
                retryOrThrow(retry, e);
            } else {
                // unhandled exception on create()
                throw e;
            }
        }

        // Catch all to wait before retrying
        retry.waitForNextAttempt();
    }
}

From source file:org.apache.accumulo.fate.zookeeper.ZooUtil.java

License:Apache License

public static byte[] getData(ZooKeeperConnectionInfo info, String zPath, Stat stat)
        throws KeeperException, InterruptedException {
    final Retry retry = RETRY_FACTORY.create();
    while (true) {
        try {/*from w  w  w.j  a v  a 2 s  . c  o  m*/
            return getZooKeeper(info).getData(zPath, false, stat);
        } catch (KeeperException e) {
            final Code c = e.code();
            if (c == Code.CONNECTIONLOSS || c == Code.OPERATIONTIMEOUT || c == Code.SESSIONEXPIRED) {
                retryOrThrow(retry, e);
            } else {
                throw e;
            }
        }

        retry.waitForNextAttempt();
    }
}

From source file:org.apache.accumulo.fate.zookeeper.ZooUtil.java

License:Apache License

public static Stat getStatus(ZooKeeperConnectionInfo info, String zPath)
        throws KeeperException, InterruptedException {
    final Retry retry = RETRY_FACTORY.create();
    while (true) {
        try {/*from w  w  w.  j  ava 2  s .  co  m*/
            return getZooKeeper(info).exists(zPath, false);
        } catch (KeeperException e) {
            final Code c = e.code();
            if (c == Code.CONNECTIONLOSS || c == Code.OPERATIONTIMEOUT || c == Code.SESSIONEXPIRED) {
                retryOrThrow(retry, e);
            } else {
                throw e;
            }
        }

        retry.waitForNextAttempt();
    }
}

From source file:org.apache.accumulo.fate.zookeeper.ZooUtil.java

License:Apache License

public static void recursiveCopyPersistent(ZooKeeperConnectionInfo info, String source, String destination,
        NodeExistsPolicy policy) throws KeeperException, InterruptedException {
    Stat stat = null;/*ww w .ja  v  a  2s  .  c o m*/
    if (!exists(info, source))
        throw KeeperException.create(Code.NONODE, source);
    if (exists(info, destination)) {
        switch (policy) {
        case OVERWRITE:
            break;
        case SKIP:
            return;
        case FAIL:
        default:
            throw KeeperException.create(Code.NODEEXISTS, source);
        }
    }

    stat = new Stat();
    byte[] data = getData(info, source, stat);

    if (stat.getEphemeralOwner() == 0) {
        if (data == null)
            throw KeeperException.create(Code.NONODE, source);
        putPersistentData(info, destination, data, policy);
        if (stat.getNumChildren() > 0) {
            List<String> children;
            final Retry retry = RETRY_FACTORY.create();
            while (true) {
                try {
                    children = getZooKeeper(info).getChildren(source, false);
                    break;
                } catch (KeeperException e) {
                    final Code c = e.code();
                    if (c == Code.CONNECTIONLOSS || c == Code.OPERATIONTIMEOUT || c == Code.SESSIONEXPIRED) {
                        retryOrThrow(retry, e);
                    } else {
                        throw e;
                    }
                }
                retry.waitForNextAttempt();
            }
            for (String child : children) {
                recursiveCopyPersistent(info, source + "/" + child, destination + "/" + child, policy);
            }
        }
    }
}

From source file:org.apache.accumulo.fate.zookeeper.ZooUtil.java

License:Apache License

public static String putPersistentSequential(ZooKeeperConnectionInfo info, String zPath, byte[] data)
        throws KeeperException, InterruptedException {
    final Retry retry = RETRY_FACTORY.create();
    while (true) {
        try {//www . ja  v a  2 s.com
            return getZooKeeper(info).create(zPath, data, ZooUtil.PUBLIC, CreateMode.PERSISTENT_SEQUENTIAL);
        } catch (KeeperException e) {
            final Code c = e.code();
            if (c == Code.CONNECTIONLOSS || c == Code.OPERATIONTIMEOUT || c == Code.SESSIONEXPIRED) {
                retryOrThrow(retry, e);
            } else {
                throw e;
            }
        }

        retry.waitForNextAttempt();
    }
}

From source file:org.apache.accumulo.fate.zookeeper.ZooUtil.java

License:Apache License

public static String putEphemeralData(ZooKeeperConnectionInfo info, String zPath, byte[] data)
        throws KeeperException, InterruptedException {
    final Retry retry = RETRY_FACTORY.create();
    while (true) {
        try {//from ww  w .j a va 2  s .c o  m
            return getZooKeeper(info).create(zPath, data, ZooUtil.PUBLIC, CreateMode.EPHEMERAL);
        } catch (KeeperException e) {
            final Code c = e.code();
            if (c == Code.CONNECTIONLOSS || c == Code.OPERATIONTIMEOUT || c == Code.SESSIONEXPIRED) {
                retryOrThrow(retry, e);
            } else {
                throw e;
            }
        }

        retry.waitForNextAttempt();
    }
}

From source file:org.apache.accumulo.fate.zookeeper.ZooUtil.java

License:Apache License

public static String putEphemeralSequential(ZooKeeperConnectionInfo info, String zPath, byte[] data)
        throws KeeperException, InterruptedException {
    final Retry retry = RETRY_FACTORY.create();
    while (true) {
        try {// www. j av a  2  s.  com
            return getZooKeeper(info).create(zPath, data, ZooUtil.PUBLIC, CreateMode.EPHEMERAL_SEQUENTIAL);
        } catch (KeeperException e) {
            final Code c = e.code();
            if (c == Code.CONNECTIONLOSS || c == Code.OPERATIONTIMEOUT || c == Code.SESSIONEXPIRED) {
                retryOrThrow(retry, e);
            } else {
                throw e;
            }
        }

        retry.waitForNextAttempt();
    }
}