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.splicemachine.ddl.ZooKeeperDDLWatchChecker.java

License:Apache License

@Override
public void notifyProcessed(Collection<Pair<DDLMessage.DDLChange, String>> processedChanges)
        throws IOException {
    /*/*from   ww  w  . j  a  v  a  2  s.com*/
     * Notify the relevant controllers that their change has been processed
     */
    List<Op> ops = Lists.newArrayListWithExpectedSize(processedChanges.size());
    List<DDLMessage.DDLChange> changeList = Lists.newArrayList();
    for (Pair<DDLMessage.DDLChange, String> pair : processedChanges) {
        DDLMessage.DDLChange change = pair.getFirst();
        String errorMessage = pair.getSecond();
        // Tag Errors for handling on the client, will allow us to understand what node failed and why...
        String path = zkClient.changePath + "/" + change.getChangeId() + "/"
                + (errorMessage == null ? "" : DDLConfiguration.ERROR_TAG) + id;
        Op op = Op.create(path,
                (errorMessage == null ? new byte[] {}
                        : (String.format("server [%s] failed with error [%s]", id, errorMessage).getBytes())),
                ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
        ops.add(op);
        changeList.add(change);
    }
    try {
        List<OpResult> multi = ZkUtils.getRecoverableZooKeeper().getZooKeeper().multi(ops);
        for (int i = 0; i < multi.size(); i++) {
            OpResult result = multi.get(i);
            if (!(result instanceof OpResult.ErrorResult))
                processedChanges.remove(changeList.get(i));
            else {
                OpResult.ErrorResult err = (OpResult.ErrorResult) result;
                KeeperException.Code code = KeeperException.Code.get(err.getErr());
                switch (code) {
                case NODEEXISTS: //we may have already set the value, so ignore node exists issues
                case NONODE: // someone already removed the notification, it's obsolete
                    // ignore
                    break;
                default:
                    throw Exceptions.getIOException(KeeperException.create(code));
                }
            }
        }
    } catch (InterruptedException e) {
        throw Exceptions.getIOException(e);
    } catch (KeeperException e) {
        switch (e.code()) {
        case NODEEXISTS: //we may have already set the value, so ignore node exists issues
        case NONODE: // someone already removed the notification, it's obsolete
            // ignore
            break;
        default:
            throw Exceptions.getIOException(e);
        }
    }
}

From source file:com.splicemachine.hbase.SpliceZooKeeperManager.java

License:Apache License

public <T> T executeUnlessExpired(Command<T> command) throws InterruptedException, KeeperException {
    /*//from   w w  w. ja va  2s  .  co  m
     * What actually happens is that, in the event of a long network partition, ZooKeeper will throw
     * ConnectionLoss exceptions, but it will NOT throw a SessionExpired exception until it reconnects, even
     * if it's been disconnected for CLEARLY longer than the session timeout.
     *
     * To deal with this, we have to basically loop through our command repeatedly until we either
     *
     * 1. Succeed.
     * 2. Get a SessionExpired event from ZooKeeper
     * 3. Spent more than 2*sessionTimeout ms attempting the request
     * 4. Get some other kind of Zk error (NoNode, etc).
     */
    RecoverableZooKeeper rzk;
    try {
        rzk = getRecoverableZooKeeper();
    } catch (ZooKeeperConnectionException e) {
        throw new KeeperException.SessionExpiredException();
    }
    //multiple by 2 to make absolutely certain we're timed out.
    int sessionTimeout = 2 * rzk.getZooKeeper().getSessionTimeout();
    long nextTime = System.currentTimeMillis();
    long startTime = System.currentTimeMillis();
    while ((int) (nextTime - startTime) < sessionTimeout) {
        try {
            return command.execute(rzk);
        } catch (KeeperException ke) {
            switch (ke.code()) {
            case CONNECTIONLOSS:
            case OPERATIONTIMEOUT:
                LOG.warn("Detected a Connection issue(" + ke.code() + ") with ZooKeeper, retrying");
                nextTime = System.currentTimeMillis();
                break;
            default:
                throw ke;
            }
        }
    }

    //we've run out of time, our session has almost certainly expired. Give up and explode
    throw new KeeperException.SessionExpiredException();
}

From source file:com.splicemachine.hbase.ZkUtils.java

License:Apache License

/**
 * Creates a node in ZooKeeper if it does not already exist. If it already exists, this does nothing.
 *
 * @param path       the path to create//from  w w  w  .  j av  a 2s  . c o m
 * @param bytes      the bytes to set
 * @param acls       the privileges for the new node
 * @param createMode the create mode for that node
 * @throws IOException if something goes wrong and the node can't be added.
 */
public static boolean safeCreate(String path, byte[] bytes, List<ACL> acls, CreateMode createMode)
        throws KeeperException, InterruptedException {
    try {
        getRecoverableZooKeeper().create(path, bytes, acls, createMode);
        return true;
    } catch (KeeperException e) {
        if (e.code() == KeeperException.Code.NODEEXISTS) {
            //it's already been created, so nothing to do
            return true;
        }
        throw e;
    }
}

From source file:com.splicemachine.hbase.ZkUtils.java

License:Apache License

public static boolean safeDelete(String path, int version) throws KeeperException, InterruptedException {
    try {/*  ww  w  . j a va  2s .  c  om*/
        getRecoverableZooKeeper().delete(path, version);
        return true;
    } catch (KeeperException e) {
        if (e.code() != KeeperException.Code.NONODE)
            throw e;
        else
            return false;
    }
}

From source file:com.splicemachine.hbase.ZkUtils.java

License:Apache License

public static boolean recursiveSafeCreate(String path, byte[] bytes, List<ACL> acls, CreateMode createMode)
        throws InterruptedException, KeeperException {
    if (path == null || path.length() <= 0)
        return true; //nothing to do, we've gone all the way to the root
    RecoverableZooKeeper rzk = getRecoverableZooKeeper();
    try {//from  ww  w  . jav a  2s.  com
        return safeCreate(path, bytes, acls, createMode, rzk);
    } catch (KeeperException e) {
        if (e.code() == KeeperException.Code.NONODE) {
            //parent node doesn't exist, so recursively create it, and then try and create your node again
            String parent = path.substring(0, path.lastIndexOf('/'));
            recursiveSafeCreate(parent, new byte[] {}, acls, CreateMode.PERSISTENT);
            return safeCreate(path, bytes, acls, createMode);
        } else
            throw e;
    }
}

From source file:com.splicemachine.hbase.ZkUtils.java

License:Apache License

public static boolean safeCreate(String path, byte[] bytes, List<ACL> acls, CreateMode createMode,
        RecoverableZooKeeper zooKeeper) throws KeeperException, InterruptedException {
    try {/*  w w w.j a v  a2s  .c  o  m*/
        zooKeeper.create(path, bytes, acls, createMode);
        return true;
    } catch (KeeperException ke) {
        if (ke.code() != KeeperException.Code.NODEEXISTS)
            throw ke;
        else
            return true;
    }
}

From source file:com.splicemachine.hbase.ZkUtils.java

License:Apache License

/**
 *
 * Attempts to grab a squence numbered 1 - 1024 as an ephemeral node in zookeeper.  This is used to keep
 * the unqiue key number of bytes to a minimum.
 *
 * @return//from   w w w  . java 2s  . c  om
 * @throws Exception
 */
public static short assignSnowFlakeSequence() throws Exception {
    for (short i = 1; i <= 1024; i++) {
        try {
            // Notice the ephemeral node, will be gone once the process exits...
            // TODO -JL Can be more sophisticated when we get closer, multiple pass as well.
            ZkUtils.create(HConfiguration.getConfiguration().getSpliceRootPath()
                    + HBaseConfiguration.SNOWFLAKE_PATH + "/" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE,
                    CreateMode.EPHEMERAL);
            return i;
        } catch (KeeperException e) {
            if (e.code() != KeeperException.Code.NODEEXISTS)
                throw new Exception("Error assigning snowflake sequence, catastrophic", e);
        }
    }
    throw new Exception("Cannot assign snowflake sequence, catastrophic");
}

From source file:com.splicemachine.hbase.ZkUtils.java

License:Apache License

/**
 * Increments a counter stored in ZooKeeper
 *
 * @param counterNode the node to store the counter in
 * @return the next counter number/*from  w  w  w  .j  av  a  2s.  c  o  m*/
 * @throws IOException if something goes wrong and the counter can't be incremented.
 */
public static long nextSequenceId(String counterNode) throws IOException {
    /*
    * When generating a new, Monotonically increasing identifier
    * based off of zookeeper, you have essentially two choices:
    *
    * 1. Create a new sequential znode under a base znode, then
    * read off the assigned sequential identifier.
    * 2. Use optimistic versioning to push increments to a single znode
    * atomically.
    *
    * Case 1 is the simplest to implement, but it has some notable issues:
    *  1. There is no contiguity of numbers. ZooKeeper guarantees
    *  monotonically increasing sequence numbers(Up to Integer.MAX_VALUE
    *  at any rate), but does not guarantee contiguiity of those numbers.
    *  Thus, it's possible to see 1, then the next be 10
    * 2. Lots of znodes. If you have a few thousand conglomerates, then
    * you'll have a few thousand znodes, whose only purpose was to
    * grab the sequential identifier. Not only does this add to the
    * conceptual difficulty in understanding the zookeeper layout, but it
    * also adds additional load to the ZooKeeper cluster itself.
    * 3. Rollovers. The sequential identifier supplied by zookeeper is
    * limited to integers, which can cause a rollover eventually.
    * 4. Expensive to read. To get the highest sequence element (without
    * updating), you must get *all* the znode children, then sort them
    * by sequence number, and then pick out the highest (or lowest). This
    * is a large network operation when there are a few thousand znodes.
    * 5. It's difficult to watch. You can watch on children, but in
    * order to act on them, you must first
    *
    * this implementation uses the second option
    */

    RecoverableZooKeeper rzk = ZkUtils.getRecoverableZooKeeper();
    int maxTries = 10;
    int tries = 0;
    while (tries <= maxTries) {
        tries++;
        //get the current state of the counter
        Stat version = new Stat();
        long currentCount;
        byte[] data;
        try {
            data = rzk.getData(counterNode, false, version);
        } catch (KeeperException e) {
            //if we timed out trying to deal with ZooKeeper, retry
            switch (e.code()) {
            case CONNECTIONLOSS:
            case OPERATIONTIMEOUT:
                continue;
            default:
                throw new IOException(e);
            }
        } catch (InterruptedException e) {
            throw new IOException(e);
        }
        currentCount = Bytes.toLong(data) + 1;

        //try and write the next entry, but only if the versions haven't changed
        try {
            rzk.setData(counterNode, Bytes.toBytes(currentCount), version.getVersion());
            return currentCount;
        } catch (KeeperException e) {
            //if we timed out talking, or if the version didn't match, retry
            switch (e.code()) {
            case CONNECTIONLOSS:
            case OPERATIONTIMEOUT:
            case BADVERSION:
                continue;
            default:
                throw new IOException(e);
            }
        } catch (InterruptedException e) {
            //we were interrupted, which means we need to bail
            throw new IOException(e);
        }
    }
    /*
     * We've tried to get the latest sequence number a whole bunch of times,
     * without success. That means either a problem with ZooKeeper, or a LOT
     * of contention in getting the sequence. Need to back off and deal with it
     * at a higher level
     *
     * TODO -sf- we could fix this by putting in a non-optimistic lock at this point, but
     * it's probably best to see if it poses a problem as written first.
     */
    throw new IOException(
            "Unable to get next conglomerate sequence, there is an issue " + "speaking with ZooKeeper");
}

From source file:com.splicemachine.hbase.ZkUtils.java

License:Apache License

/**
 * Sets a counter stored in ZooKeeper/*  w  w w  .  j  a  va2  s.  c  o m*/
 *
 * @param counterNode the node to store the counter in
 * @throws IOException if something goes wrong and the counter can't be incremented.
 */
public static void setSequenceId(String counterNode, long count) throws IOException {
    RecoverableZooKeeper rzk = ZkUtils.getRecoverableZooKeeper();

    int maxTries = 10;
    int tries = 0;
    while (tries <= maxTries) {
        tries++;
        try {
            rzk.setData(counterNode, Bytes.toBytes(count), -1 /* any version */);
        } catch (KeeperException e) {
            //if we timed out trying to deal with ZooKeeper, retry
            switch (e.code()) {
            case CONNECTIONLOSS:
            case OPERATIONTIMEOUT:
                continue;
            default:
                throw new IOException(e);
            }
        } catch (InterruptedException e) {
            throw new IOException(e);
        }
        return;
    }
    /*
     * We've tried to set the sequence number a whole bunch of times,
     * without success. That means either a problem with ZooKeeper
     * Need to back off and deal with it
     * at a higher level
     */
    throw new IOException(
            "Unable to set next conglomerate sequence, there is an issue " + "speaking with ZooKeeper");
}

From source file:com.spotify.helios.master.ZooKeeperMasterModel.java

License:Apache License

@Override
public void stopDeploymentGroup(final String deploymentGroupName) throws DeploymentGroupDoesNotExistException {
    checkNotNull(deploymentGroupName, "name");

    log.info("stop deployment-group: name={}", deploymentGroupName);

    final ZooKeeperClient client = provider.get("stopDeploymentGroup");

    // Delete deployment group tasks (if any) and set DG state to FAILED
    final DeploymentGroupStatus status = DeploymentGroupStatus.newBuilder().setState(FAILED)
            .setError("Stopped by user").build();
    final String statusPath = Paths.statusDeploymentGroup(deploymentGroupName);
    final String tasksPath = Paths.statusDeploymentGroupTasks(deploymentGroupName);

    try {/*from  www  . j  a v  a 2 s  .co m*/
        client.ensurePath(Paths.statusDeploymentGroupTasks());

        final List<ZooKeeperOperation> operations = Lists.newArrayList();

        // NOTE: This remove operation is racey. If tasks exist and the rollout finishes before the
        // delete() is executed then this will fail. Conversely, if it doesn't exist but is created
        // before the transaction is executed it will also fail. This is annoying for users, but at
        // least means we won't have inconsistent state.
        //
        // That the set() is first in the list of operations is important because of the
        // kludgy error checking we do below to disambiguate "doesn't exist" failures from the race
        // condition mentioned below.
        operations.add(set(statusPath, status));

        final Stat tasksStat = client.exists(tasksPath);
        if (tasksStat != null) {
            operations.add(delete(tasksPath));
        } else {
            // There doesn't seem to be a "check that node doesn't exist" operation so we
            // do a create and a delete on the same path to emulate it.
            operations.add(create(tasksPath));
            operations.add(delete(tasksPath));
        }

        client.transaction(operations);
    } catch (final NoNodeException e) {
        // Either the statusPath didn't exist, in which case the DG does not exist. Or the tasks path
        // does not exist which can happen due to the race condition described above. In the latter
        // case make sure we don't return a "doesn't exist" error as that would be a lie.
        // Yes, the way you figure out which operation in a transaction failed is retarded.
        if (((OpResult.ErrorResult) e.getResults().get(0)).getErr() == KeeperException.Code.NONODE.intValue()) {
            throw new DeploymentGroupDoesNotExistException(deploymentGroupName);
        } else {
            throw new HeliosRuntimeException("stop deployment-group " + deploymentGroupName
                    + " failed due to a race condition, please retry", e);
        }
    } catch (final KeeperException e) {
        throw new HeliosRuntimeException("stop deployment-group " + deploymentGroupName + " failed", e);
    }
}