Search in sources :

Example 1 with InvalidContentException

use of com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InvalidContentException in project vespa by vespa-engine.

the class SetNodeStateRequest method getRequestedNodeState.

static NodeState getRequestedNodeState(Map<String, UnitState> newStates, Node n) throws StateRestApiException {
    UnitState newState = newStates.get("user");
    if (newState == null)
        throw new InvalidContentException("No new user state given in request");
    State state;
    switch(newState.getId().toLowerCase()) {
        case "up":
            state = State.UP;
            break;
        case "retired":
            state = State.RETIRED;
            break;
        case "maintenance":
            state = State.MAINTENANCE;
            break;
        case "down":
            state = State.DOWN;
            break;
        default:
            throw new InvalidContentException("Invalid user state '" + newState.getId() + "' given.");
    }
    return new NodeState(n.getType(), state).setDescription(newState.getReason());
}
Also used : InvalidContentException(com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InvalidContentException) NodeState(com.yahoo.vdslib.state.NodeState) UnitState(com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitState) ClusterState(com.yahoo.vdslib.state.ClusterState) NodeState(com.yahoo.vdslib.state.NodeState) State(com.yahoo.vdslib.state.State) UnitState(com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitState)

Example 2 with InvalidContentException

use of com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InvalidContentException in project vespa by vespa-engine.

the class DummyStateApi method setUnitState.

@Override
public SetResponse setUnitState(SetUnitStateRequest request) throws StateRestApiException {
    checkForInducedException();
    String[] path = request.getUnitPath();
    if (path.length != 2) {
        throw new OperationNotSupportedForUnitException(path, "You can only set states on nodes");
    }
    DummyBackend.Node n = null;
    DummyBackend.Cluster c = backend.getClusters().get(path[0]);
    if (c != null) {
        n = c.nodes.get(path[1]);
    }
    if (n == null)
        throw new MissingUnitException(path, 2);
    Map<String, UnitState> newState = request.getNewState();
    if (newState.size() != 1 || !newState.containsKey("current")) {
        throw new InvalidContentException("Only state of type 'current' is allowed to be set.");
    }
    n.state = newState.get("current").getId();
    n.reason = newState.get("current").getReason();
    String reason = String.format("DummyStateAPI %s call", request.getResponseWait().getName());
    return new SetResponse(reason, true);
}
Also used : InvalidContentException(com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InvalidContentException) OperationNotSupportedForUnitException(com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.OperationNotSupportedForUnitException) MissingUnitException(com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.MissingUnitException)

Example 3 with InvalidContentException

use of com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InvalidContentException in project vespa by vespa-engine.

the class SetNodeStatesForClusterRequest method calculateResult.

@Override
public SetResponse calculateResult(RemoteClusterControllerTask.Context context) throws StateRestApiException {
    if (condition != SetUnitStateRequest.Condition.FORCE) {
        // go down, etc. This is prohibited in Condition.SAFE.
        throw new InvalidContentException("Setting all nodes in a cluster to a state is only supported with FORCE");
    }
    for (ConfiguredNode configuredNode : context.cluster.getConfiguredNodes().values()) {
        Node node = new Node(NodeType.STORAGE, configuredNode.index());
        SetResponse setResponse = SetNodeStateRequest.setWantedState(context.cluster, condition, newStates, node, context.nodeStateOrHostInfoChangeHandler, context.currentConsolidatedState);
        if (!setResponse.getWasModified()) {
            throw new InternalFailure("We have not yet implemented the meaning of " + "failing to set the wanted state for a subset of nodes: " + "condition = " + condition + ", newStates = " + newStates + ", currentConsolidatedState = " + context.currentConsolidatedState);
        }
    }
    // 'true' here means the current state now equals the request's wanted state.
    return new SetResponse("ok", true);
}
Also used : SetResponse(com.yahoo.vespa.clustercontroller.utils.staterestapi.response.SetResponse) InvalidContentException(com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InvalidContentException) InternalFailure(com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InternalFailure) ConfiguredNode(com.yahoo.vdslib.distribution.ConfiguredNode) ConfiguredNode(com.yahoo.vdslib.distribution.ConfiguredNode)

Example 4 with InvalidContentException

use of com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InvalidContentException in project vespa by vespa-engine.

the class JsonReader method getStateRequestData.

public SetRequestData getStateRequestData(HttpRequest request) throws Exception {
    JSONObject json = new JSONObject(request.getPostContent().toString());
    final SetUnitStateRequest.Condition condition;
    if (json.has("condition")) {
        condition = SetUnitStateRequest.Condition.fromString(json.getString("condition"));
    } else {
        condition = SetUnitStateRequest.Condition.FORCE;
    }
    final SetUnitStateRequest.ResponseWait responseWait = json.has("response-wait") ? SetUnitStateRequest.ResponseWait.fromString(json.getString("response-wait")) : SetUnitStateRequest.ResponseWait.WAIT_UNTIL_CLUSTER_ACKED;
    Map<String, UnitState> stateMap = new HashMap<>();
    if (!json.has("state")) {
        throw new InvalidContentException("Set state requests must contain a state object");
    }
    Object o = json.get("state");
    if (!(o instanceof JSONObject)) {
        throw new InvalidContentException("value of state is not a json object");
    }
    JSONObject state = (JSONObject) o;
    JSONArray stateTypes = state.names();
    for (int i = 0; i < stateTypes.length(); ++i) {
        o = stateTypes.get(i);
        String type = (String) o;
        o = state.get(type);
        if (!(o instanceof JSONObject)) {
            throw new InvalidContentException("value of state->" + type + " is not a json object");
        }
        JSONObject userState = (JSONObject) o;
        String code = "up";
        if (userState.has("state")) {
            o = userState.get("state");
            if (!(o instanceof String)) {
                throw new InvalidContentException("value of state->" + type + "->state is not a string");
            }
            code = o.toString();
        }
        String reason = "";
        if (userState.has("reason")) {
            o = userState.get("reason");
            if (!(o instanceof String)) {
                throw new InvalidContentException("value of state->" + type + "->reason is not a string");
            }
            reason = o.toString();
        }
        stateMap.put(type, new UnitStateImpl(code, reason));
    }
    return new SetRequestData(stateMap, condition, responseWait);
}
Also used : HashMap(java.util.HashMap) JSONArray(org.codehaus.jettison.json.JSONArray) UnitState(com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitState) InvalidContentException(com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InvalidContentException) JSONObject(org.codehaus.jettison.json.JSONObject) JSONObject(org.codehaus.jettison.json.JSONObject) SetUnitStateRequest(com.yahoo.vespa.clustercontroller.utils.staterestapi.requests.SetUnitStateRequest)

Aggregations

InvalidContentException (com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InvalidContentException)4 UnitState (com.yahoo.vespa.clustercontroller.utils.staterestapi.response.UnitState)2 ConfiguredNode (com.yahoo.vdslib.distribution.ConfiguredNode)1 ClusterState (com.yahoo.vdslib.state.ClusterState)1 NodeState (com.yahoo.vdslib.state.NodeState)1 State (com.yahoo.vdslib.state.State)1 InternalFailure (com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.InternalFailure)1 MissingUnitException (com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.MissingUnitException)1 OperationNotSupportedForUnitException (com.yahoo.vespa.clustercontroller.utils.staterestapi.errors.OperationNotSupportedForUnitException)1 SetUnitStateRequest (com.yahoo.vespa.clustercontroller.utils.staterestapi.requests.SetUnitStateRequest)1 SetResponse (com.yahoo.vespa.clustercontroller.utils.staterestapi.response.SetResponse)1 HashMap (java.util.HashMap)1 JSONArray (org.codehaus.jettison.json.JSONArray)1 JSONObject (org.codehaus.jettison.json.JSONObject)1