Example usage for org.apache.zookeeper KeeperException.BadVersionException getMessage

List of usage examples for org.apache.zookeeper KeeperException.BadVersionException getMessage

Introduction

In this page you can find the example usage for org.apache.zookeeper KeeperException.BadVersionException getMessage.

Prototype

@Override
    public String getMessage() 

Source Link

Usage

From source file:org.apache.hms.controller.CommandHandler.java

License:Apache License

private void updateSystemState(String statusPath) throws InterruptedException, KeeperException, IOException {
    LOG.info("status path is: " + statusPath);
    Stat stat = zk.exists(statusPath, false);
    if (stat == null) {
        /* status has been previously processed by either this or another controller
         * delete the status lock if it exists 
         *//*from w  w w .  j  a v  a  2  s.c o  m*/
        LOG.info("status has been previously processed: " + statusPath);
        statusCleanup(statusPath, null);
        return;
    }
    ActionStatus actionStat = JAXBUtil.read(zk.getData(statusPath, false, null), ActionStatus.class);
    if (actionStat.getStatus() != Status.SUCCEEDED && actionStat.getStatus() != Status.FAILED)
        throw new IOException("Invalid action status: " + actionStat.getStatus() + " from action "
                + actionStat.getActionPath());
    String actionPath = actionStat.getActionPath();
    stat = zk.exists(actionPath, false);
    if (stat == null) {
        /* status has been previously processed by either this or another controller
         * delete the status znode, plus action and status locks 
         */
        statusCleanup(statusPath, actionPath);
        return;
    }

    String actionQueue = actionPath.substring(0, actionPath.lastIndexOf('/'));
    String hostNode = actionQueue.substring(0, actionQueue.lastIndexOf('/'));
    // update system status
    if (actionStat.getStatus() == Status.SUCCEEDED) {
        Action action = JAXBUtil.read(zk.getData(actionPath, false, null), Action.class);
        MachineState machineState = JAXBUtil.read(zk.getData(hostNode, false, stat), MachineState.class);
        boolean retry = true;
        while (retry) {
            retry = false;
            Set<StateEntry> states = machineState.getStates();
            if (states == null) {
                states = new HashSet<StateEntry>();
            }
            if (action.getExpectedResults() != null) {
                states.addAll(action.getExpectedResults());
            }
            machineState.setStates(states);
            try {
                stat = zk.setData(hostNode, JAXBUtil.write(machineState), stat.getVersion());
            } catch (KeeperException.BadVersionException e) {
                LOG.info("version mismatch: expected=" + stat.getVersion() + " msg: " + e.getMessage());
                machineState = JAXBUtil.read(zk.getData(hostNode, false, stat), MachineState.class);
                LOG.info("new version is " + stat.getVersion());
                retry = true;
            }
        }
    }

    // update cmd status
    if (actionStat.getStatus() != Status.SUCCEEDED) {
        try {
            zk.create(
                    actionStat.getCmdPath() + COMMAND_STATUS + "/" + actionStat.getHost().replace('/', '.')
                            + "-" + actionStat.getActionId(),
                    JAXBUtil.write(actionStat), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        } catch (KeeperException.NodeExistsException e) {
        }
    }

    String host = hostNode.substring(hostNode.lastIndexOf('/') + 1);
    String cmdStatusPath = actionStat.getCmdPath() + COMMAND_STATUS;
    CommandStatus cmdStatus = JAXBUtil.read(zk.getData(cmdStatusPath, false, stat), CommandStatus.class);
    boolean retry = true;
    while (retry) {
        retry = false;
        boolean found = false;
        boolean needUpdate = false;
        for (ActionEntry actionEntry : cmdStatus.getActionEntries()) {
            if (actionEntry.getAction().getActionId() == actionStat.getActionId()) {
                int failCount = 0;
                if (actionStat.getStatus() == Status.FAILED) {
                    // Count current action status if it has failed.
                    failCount++;
                }
                for (HostStatusPair hsp : actionEntry.getHostStatus()) {
                    if (hsp.getStatus() == Status.FAILED) {
                        // Walk through existing hosts, and count number of failed
                        // actions.
                        failCount++;
                    }
                    if (host.equals(hsp.getHost())) {
                        found = true;
                        Status status = hsp.getStatus();
                        if (status == Status.UNQUEUED || status == Status.QUEUED || status == Status.STARTED) {
                            hsp.setStatus(actionStat.getStatus());
                            cmdStatus.setCompletedActions(cmdStatus.getCompletedActions() + 1);
                            if (cmdStatus.getCompletedActions() == cmdStatus.getTotalActions()) {
                                Status overallStatus = Status.SUCCEEDED;
                                for (ActionEntry aEntry : cmdStatus.getActionEntries()) {
                                    boolean shouldBreak = false;
                                    for (HostStatusPair hspair : aEntry.getHostStatus()) {
                                        if (hspair.getStatus() != Status.SUCCEEDED) {
                                            overallStatus = Status.FAILED;
                                            shouldBreak = true;
                                            break;
                                        }
                                    }
                                    if (shouldBreak)
                                        break;
                                }
                                cmdStatus.setStatus(overallStatus);
                                cmdStatus.setEndTime(new Date(System.currentTimeMillis()).toString());
                                updateClusterStatus(actionStat.getCmdPath());
                            } else if (failCount == actionEntry.getHostStatus().size()) {
                                // If all nodes failed the action, set the command to fail.
                                cmdStatus.setStatus(Status.FAILED);
                                cmdStatus.setEndTime(new Date(System.currentTimeMillis()).toString());
                                updateClusterStatus(actionStat.getCmdPath());
                            }
                            needUpdate = true;
                            LOG.info("Fail count:" + failCount);
                            break;
                        } else if (status == actionStat.getStatus()) {
                            // duplicate status update, nothing to be done
                        } else {
                            throw new IOException("UNEXPECTED action status: " + actionStat.getStatus()
                                    + " from action " + actionPath + ", current host status is " + status);
                        }
                    }
                }
                if (found) {
                    break;
                }
            }
        }
        if (!found) {
            throw new IOException("UNEXPECTED: can't find action " + actionPath);
        }
        if (needUpdate) {
            try {
                stat = zk.setData(cmdStatusPath, JAXBUtil.write(cmdStatus), stat.getVersion());
                if (cmdStatus.getStatus() == Status.SUCCEEDED || cmdStatus.getStatus() == Status.FAILED) {
                    unlockCommand(actionStat.getCmdPath());
                }
            } catch (KeeperException.BadVersionException e) {
                LOG.info("version mismatch: expected=" + stat.getVersion() + " msg: " + e.getMessage());
                cmdStatus = JAXBUtil.read(zk.getData(cmdStatusPath, false, stat), CommandStatus.class);
                LOG.info("new version is " + stat.getVersion());
                retry = true;
            }
        }
    }

    statusCleanup(statusPath, actionPath);
    LOG.info("Deleted action:" + actionPath + ", status:" + statusPath);
}

From source file:org.apache.hms.controller.CommandHandler.java

License:Apache License

private void queueActions(String cmdStatusPath) throws IOException, KeeperException, InterruptedException {
    LOG.info("try to queue actions for cmd " + cmdStatusPath);
    Stat stat = new Stat();
    CommandStatus cmdStatus = JAXBUtil.read(zk.getData(cmdStatusPath, false, stat), CommandStatus.class);
    // we queue actions and update their status one at a time. After each
    // action is queued, we try to update its status (retry if necessary).
    // If retry happens, we start over again and try to find actions that
    // need to be issued.
    boolean startOver = true;
    while (startOver) {
        startOver = false;/* w  w w  . ja va  2 s.  c o  m*/
        for (ActionEntry actionEntry : cmdStatus.getActionEntries()) {
            //TODO needs to check if an actionEntry is already done
            if (!isDependencySatisfied(cmdStatusPath, actionEntry.getAction().getDependencies())) {
                LOG.info("dependency is not satified for actionId=" + actionEntry.getAction().getActionId());
                return;
            }
            int actionId = actionEntry.getAction().getActionId();
            for (HostStatusPair hsp : actionEntry.getHostStatus()) {
                if (hsp.getStatus() == Status.UNQUEUED) {
                    // queue action
                    String actionNode = CommonConfigurationKeys.ZOOKEEPER_CLUSTER_ROOT_DEFAULT + "/"
                            + cmdStatus.getClusterName() + "/" + hsp.getHost() + AGENT_ACTION + "/" + "action-";
                    actionNode = zk.create(actionNode, JAXBUtil.write(actionEntry.getAction()),
                            Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);

                    // update status for queued action
                    String host = hsp.getHost();
                    hsp.setStatus(Status.QUEUED);
                    boolean retry = true;
                    while (retry) {
                        retry = false;
                        try {
                            stat = zk.setData(cmdStatusPath, JAXBUtil.write(cmdStatus), stat.getVersion());
                        } catch (KeeperException.BadVersionException e) {
                            LOG.info("version mismatch: expected=" + stat.getVersion() + " msg: "
                                    + e.getMessage());
                            // our copy is stale, we need to start over again after
                            // updating the current status
                            startOver = true;
                            cmdStatus = JAXBUtil.read(zk.getData(cmdStatusPath, false, stat),
                                    CommandStatus.class);
                            LOG.info("new version is " + stat.getVersion());
                            // find the item we want to update and check if it needs to be
                            // updated
                            boolean found = false;
                            for (ActionEntry actEntry : cmdStatus.getActionEntries()) {
                                if (actEntry.getAction().getActionId() == actionId) {
                                    for (HostStatusPair hostStat : actEntry.getHostStatus()) {
                                        if (hostStat.getHost().equals(host)) {
                                            // only update the status when we are in unqueued
                                            // state
                                            if (hostStat.getStatus() == Status.UNQUEUED) {
                                                hostStat.setStatus(Status.QUEUED);
                                                retry = true;
                                            }
                                            found = true;
                                            break;
                                        }
                                    }
                                    if (found)
                                        break;
                                }
                            }
                        }
                    }
                    LOG.info("Queued action " + actionNode);
                    if (startOver)
                        break;
                }
            }
            if (startOver)
                break;
        }
    }
}

From source file:org.apache.solr.handler.admin.EditFileRequestHandler.java

License:Apache License

private void writeToZooKeeper(SolrQueryRequest req, SolrQueryResponse rsp)
        throws KeeperException, InterruptedException, IOException {

    CoreContainer coreContainer = req.getCore().getCoreDescriptor().getCoreContainer();
    SolrZkClient zkClient = coreContainer.getZkController().getZkClient();

    String adminFile = ShowFileRequestHandler.getAdminFileFromZooKeeper(req, rsp, zkClient, hiddenFiles);
    String fname = req.getParams().get("file", null);
    if (OP_TEST.equals(req.getParams().get(OP_PARAM))) {
        testReloadSuccess(req, rsp);//from   w ww.j  a  v  a 2 s  .com
        return;
    }
    // Persist the managed schema
    try {
        // Assumption: the path exists
        zkClient.setData(adminFile, data, true);
        log.info("Saved " + fname + " to ZooKeeper successfully.");
    } catch (KeeperException.BadVersionException e) {
        log.error("Cannot save file: " + fname + " to Zookeeper, " + "ZooKeeper error: " + e.getMessage());
        rsp.setException(new SolrException(ErrorCode.SERVER_ERROR,
                "Cannot save file: " + fname + " to Zookeeper, " + "ZooKeeper error: " + e.getMessage()));
    }
}