Search in sources :

Example 71 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project pinot by linkedin.

the class BasePinotControllerRestletResource method responseRepresentation.

protected StringRepresentation responseRepresentation(Status status, String jsonMsg) {
    setStatus(status);
    StringRepresentation repr = new StringRepresentation(jsonMsg);
    repr.setMediaType(MediaType.APPLICATION_JSON);
    return repr;
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation)

Example 72 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project pinot by linkedin.

the class PinotInstanceRestletResource method post.

@Override
@Post("json")
public Representation post(Representation entity) {
    StringRepresentation presentation;
    try {
        final String instanceName = (String) getRequest().getAttributes().get(INSTANCE_NAME);
        if (instanceName == null) {
            // This is a request to create an instance
            try {
                final Instance instance = mapper.readValue(ByteStreams.toByteArray(entity.getStream()), Instance.class);
                presentation = addInstance(instance);
            } catch (final Exception e) {
                presentation = new StringRepresentation(e.getMessage() + "\n" + ExceptionUtils.getStackTrace(e));
                LOGGER.error("Caught exception while processing post request", e);
                ControllerRestApplication.getControllerMetrics().addMeteredGlobalValue(ControllerMeter.CONTROLLER_INTERNAL_ERROR, 1L);
                setStatus(Status.SERVER_ERROR_INTERNAL);
            }
        } else {
            // This is a request to toggle the state of an instance
            if (_pinotHelixResourceManager.instanceExists(instanceName)) {
                final String state = getRequest().getEntityAsText().trim();
                if (isValidState(state)) {
                    presentation = toggleInstanceState(instanceName, state);
                } else {
                    LOGGER.error(INVALID_STATE_ERROR);
                    setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
                    return new StringRepresentation(INVALID_STATE_ERROR);
                }
            } else {
                setStatus(Status.CLIENT_ERROR_NOT_FOUND);
                presentation = new StringRepresentation("Error: Instance " + instanceName + " not found.");
            }
        }
    } catch (final Exception e) {
        presentation = new StringRepresentation(e.getMessage() + "\n" + ExceptionUtils.getStackTrace(e));
        LOGGER.error("Caught exception while processing post request", e);
        ControllerRestApplication.getControllerMetrics().addMeteredGlobalValue(ControllerMeter.CONTROLLER_INSTANCE_POST_ERROR, 1L);
        setStatus(Status.SERVER_ERROR_INTERNAL);
    }
    return presentation;
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) Instance(com.linkedin.pinot.controller.api.pojos.Instance) JSONException(org.json.JSONException) Post(org.restlet.resource.Post)

Example 73 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project pinot by linkedin.

the class PinotInstanceRestletResource method getInstanceInformation.

/**
   * Gets the information for an instance.
   *
   * @param instanceName The instance name
   */
@HttpVerb("get")
@Summary("Gets information for an instance")
@Tags({ "instance" })
@Paths({ "/instances/{instanceName}", "/instances/{instanceName}/" })
@Responses({ @Response(statusCode = "200", description = "Information about the specified instance"), @Response(statusCode = "404", description = "The specified instance does not exist"), @Response(statusCode = "500", description = "There was an error while fetching information for the given instance") })
private Representation getInstanceInformation(@Parameter(name = "instanceName", description = "The name of the instance (eg. Server_1.2.3.4_1234 or Broker_someHost.example.com_2345)", in = "path", required = true) String instanceName) {
    try {
        if (!_pinotHelixResourceManager.instanceExists(instanceName)) {
            setStatus(Status.CLIENT_ERROR_NOT_FOUND);
            return new StringRepresentation("Error: Instance " + instanceName + " not found.");
        }
        InstanceConfig instanceConfig = _pinotHelixResourceManager.getHelixInstanceConfig(instanceName);
        JSONObject response = new JSONObject();
        response.put("instanceName", instanceConfig.getInstanceName());
        response.put("hostName", instanceConfig.getHostName());
        response.put("enabled", instanceConfig.getInstanceEnabled());
        response.put("port", instanceConfig.getPort());
        response.put("tags", new JSONArray(instanceConfig.getTags()));
        return new StringRepresentation(response.toString());
    } catch (Exception e) {
        LOGGER.warn("Caught exception while fetching information for instance {}", instanceName, e);
        setStatus(Status.SERVER_ERROR_INTERNAL);
        return new StringRepresentation("{}");
    }
}
Also used : InstanceConfig(org.apache.helix.model.InstanceConfig) JSONObject(org.json.JSONObject) StringRepresentation(org.restlet.representation.StringRepresentation) JSONArray(org.json.JSONArray) JSONException(org.json.JSONException) Summary(com.linkedin.pinot.common.restlet.swagger.Summary) HttpVerb(com.linkedin.pinot.common.restlet.swagger.HttpVerb) Paths(com.linkedin.pinot.common.restlet.swagger.Paths) Tags(com.linkedin.pinot.common.restlet.swagger.Tags) Responses(com.linkedin.pinot.common.restlet.swagger.Responses)

Example 74 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project pinot by linkedin.

the class PinotInstanceRestletResource method addInstance.

@HttpVerb("post")
@Summary("Adds an instance")
@Tags({ "instance" })
@Paths({ "/instances", "/instances/" })
@Responses({ @Response(statusCode = "200", description = "The instance was created successfully"), @Response(statusCode = "409", description = "The instance already exists and no action was taken"), @Response(statusCode = "500", description = "Failed to create the instance") })
private StringRepresentation addInstance(@Parameter(name = "instance", in = "body", description = "The instance to add", required = true) Instance instance) throws JSONException {
    StringRepresentation presentation;
    LOGGER.info("Instance creation request received for instance " + instance.toInstanceId());
    final PinotResourceManagerResponse resp = _pinotHelixResourceManager.addInstance(instance);
    if (resp.status == PinotResourceManagerResponse.ResponseStatus.failure) {
        setStatus(Status.CLIENT_ERROR_CONFLICT);
    }
    presentation = new StringRepresentation(resp.toJSON().toString());
    return presentation;
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) PinotResourceManagerResponse(com.linkedin.pinot.controller.helix.core.PinotResourceManagerResponse) Summary(com.linkedin.pinot.common.restlet.swagger.Summary) HttpVerb(com.linkedin.pinot.common.restlet.swagger.HttpVerb) Paths(com.linkedin.pinot.common.restlet.swagger.Paths) Tags(com.linkedin.pinot.common.restlet.swagger.Tags) Responses(com.linkedin.pinot.common.restlet.swagger.Responses)

Example 75 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project pinot by linkedin.

the class PinotInstanceRestletResource method toggleInstanceState.

/**
   *
   * @param instanceName: Name of the instance to enable/disable/drop
   * @param state: One of '{enable|disable|drop}'
   * @return StringRepresentation of state after trying to enable/disable/drop instance.
   * @throws JSONException
   */
@HttpVerb("post")
@Summary("Enable, disable or drop an instance")
@Tags({ "instance" })
@Paths({ "/instances/{instanceName}/state", "/instances/{instanceName}/state" })
@Responses({ @Response(statusCode = "200", description = "The instance state was changed successfully"), @Response(statusCode = "400", description = "The state given was not enable, disable or drop"), @Response(statusCode = "404", description = "The instance was not found") })
private StringRepresentation toggleInstanceState(@Parameter(name = "instanceName", in = "path", description = "The name of the instance for which to toggle its state", required = true) String instanceName, @Parameter(name = "state", in = "body", description = "The desired instance state, either enable, disable or drop", required = true) String state) throws JSONException {
    if (StateType.ENABLE.name().equalsIgnoreCase(state)) {
        return new StringRepresentation(_pinotHelixResourceManager.enableInstance(instanceName).toJSON().toString());
    } else if (StateType.DISABLE.name().equalsIgnoreCase(state)) {
        return new StringRepresentation(_pinotHelixResourceManager.disableInstance(instanceName).toJSON().toString());
    } else if (StateType.DROP.name().equalsIgnoreCase(state)) {
        return new StringRepresentation(_pinotHelixResourceManager.dropInstance(instanceName).toJSON().toString());
    } else {
        LOGGER.error(INVALID_INSTANCE_URI_ERROR);
        setStatus(Status.CLIENT_ERROR_BAD_REQUEST);
        return new StringRepresentation(INVALID_INSTANCE_URI_ERROR);
    }
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) Summary(com.linkedin.pinot.common.restlet.swagger.Summary) HttpVerb(com.linkedin.pinot.common.restlet.swagger.HttpVerb) Paths(com.linkedin.pinot.common.restlet.swagger.Paths) Tags(com.linkedin.pinot.common.restlet.swagger.Tags) Responses(com.linkedin.pinot.common.restlet.swagger.Responses)

Aggregations

StringRepresentation (org.restlet.representation.StringRepresentation)130 HttpVerb (com.linkedin.pinot.common.restlet.swagger.HttpVerb)30 Paths (com.linkedin.pinot.common.restlet.swagger.Paths)30 Summary (com.linkedin.pinot.common.restlet.swagger.Summary)30 IOException (java.io.IOException)30 Tags (com.linkedin.pinot.common.restlet.swagger.Tags)29 ZkClient (org.apache.helix.manager.zk.ZkClient)29 JSONObject (org.json.JSONObject)23 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)19 ZNRecord (org.apache.helix.ZNRecord)17 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)17 JSONException (org.json.JSONException)17 Representation (org.restlet.representation.Representation)15 Responses (com.linkedin.pinot.common.restlet.swagger.Responses)14 HelixException (org.apache.helix.HelixException)12 JSONArray (org.json.JSONArray)12 Builder (org.apache.helix.PropertyKey.Builder)11 Get (org.restlet.resource.Get)10 Post (org.restlet.resource.Post)9 ResourceException (org.restlet.resource.ResourceException)9