Search in sources :

Example 26 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project helix by apache.

the class ConfigResource method get.

/**
 * Get scoped configs
 * <p>
 * Usage:
 * <p>
 * <li>Get cluster-level configs:
 * <code>curl http://{host:port}/clusters/{clusterName}/configs/cluster
 * <li>Get instance-level configs:
 * <code>curl http://{host:port}/clusters/{clusterName}/configs/participant/{instanceName}
 * <li>Get resource-level configs:
 * <code>curl http://{host:port}/clusters/{clusterName}/configs/resource/{resourceName}
 */
@Override
public Representation get() {
    StringRepresentation representation = null;
    String clusterName = getValue("clusterName");
    String scopeStr = getValue("scope");
    try {
        if (scopeStr == null) {
            // path is "/clusters/{clusterName}/configs"
            return getConfigScopes();
        }
        scopeStr = scopeStr.toUpperCase();
        ConfigScopeProperty scopeProperty = ConfigScopeProperty.valueOf(scopeStr);
        switch(scopeProperty) {
            case CLUSTER:
            case PARTICIPANT:
            case RESOURCE:
                String scopeKey1 = getValue("scopeKey1");
                if (scopeKey1 == null) {
                    // path is "/clusters/{clusterName}/configs/cluster|participant|resource"
                    representation = getConfigKeys(scopeProperty, clusterName);
                } else {
                    // path is "/clusters/{clusterName}/configs/cluster|participant|resource/
                    // {clusterName}|{participantName}|{resourceName}"
                    representation = getConfigs(scopeProperty, clusterName, scopeKey1);
                }
                break;
            case PARTITION:
                scopeKey1 = getValue("scopeKey1");
                String scopeKey2 = getValue("scopeKey2");
                if (scopeKey1 == null) {
                    // path is "/clusters/{clusterName}/configs/partition"
                    throw new HelixException("Missing resourceName");
                } else if (scopeKey2 == null) {
                    // path is "/clusters/{clusterName}/configs/partition/resourceName"
                    representation = getConfigKeys(scopeProperty, clusterName, scopeKey1);
                } else {
                    // path is
                    // "/clusters/{clusterName}/configs/partition/resourceName/partitionName"
                    representation = getConfigs(scopeProperty, clusterName, scopeKey1, scopeKey2);
                }
                break;
            default:
                break;
        }
    } catch (Exception e) {
        String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
        representation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
        LOG.error("", e);
    }
    return representation;
}
Also used : HelixException(org.apache.helix.HelixException) StringRepresentation(org.restlet.representation.StringRepresentation) ConfigScopeProperty(org.apache.helix.model.HelixConfigScope.ConfigScopeProperty) HelixException(org.apache.helix.HelixException)

Example 27 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project helix by apache.

the class ConfigResource method getConfigs.

StringRepresentation getConfigs(ConfigScopeProperty scopeProperty, String... keys) throws Exception {
    StringRepresentation representation = null;
    ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
    ClusterSetup setupTool = new ClusterSetup(zkClient);
    HelixAdmin admin = setupTool.getClusterManagementTool();
    ZNRecord record = new ZNRecord(scopeProperty + " Config");
    HelixConfigScope scope = new HelixConfigScopeBuilder(scopeProperty, keys).build();
    List<String> configKeys = admin.getConfigKeys(scope);
    Map<String, String> configs = admin.getConfig(scope, configKeys);
    record.setSimpleFields(configs);
    representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(record), MediaType.APPLICATION_JSON);
    return representation;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) StringRepresentation(org.restlet.representation.StringRepresentation) HelixConfigScopeBuilder(org.apache.helix.model.builder.HelixConfigScopeBuilder) HelixConfigScope(org.apache.helix.model.HelixConfigScope) ClusterSetup(org.apache.helix.tools.ClusterSetup) HelixAdmin(org.apache.helix.HelixAdmin) ZNRecord(org.apache.helix.ZNRecord)

Example 28 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project helix by apache.

the class ConstraintResource method get.

/**
 * List all constraints
 * <p>
 * Usage: <code>curl http://{host:port}/clusters/{clusterName}/constraints/MESSAGE_CONSTRAINT
 */
@Override
public Representation get() {
    StringRepresentation representation = null;
    String clusterName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CLUSTER_NAME);
    String constraintTypeStr = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CONSTRAINT_TYPE);
    String constraintId = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CONSTRAINT_ID);
    try {
        ConstraintType constraintType = ConstraintType.valueOf(constraintTypeStr);
        ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
        HelixAdmin admin = new ZKHelixAdmin(zkClient);
        ZNRecord record = admin.getConstraints(clusterName, constraintType).getRecord();
        if (constraintId == null) {
            // get all message constraints
            representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(record), MediaType.APPLICATION_JSON);
        } else {
            // get a specific constraint
            Map<String, String> constraint = record.getMapField(constraintId);
            if (constraint == null) {
                representation = new StringRepresentation("No constraint of type: " + constraintType + " associated with id: " + constraintId, MediaType.APPLICATION_JSON);
            } else {
                ZNRecord subRecord = new ZNRecord(record.getId());
                subRecord.setMapField(constraintId, constraint);
                representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(subRecord), MediaType.APPLICATION_JSON);
            }
        }
    } catch (IllegalArgumentException e) {
        representation = new StringRepresentation("constraint-type: " + constraintTypeStr + " not recognized.", MediaType.APPLICATION_JSON);
    } catch (Exception e) {
        String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
        representation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
        LOG.error("Exception get constraints", e);
    }
    return representation;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) StringRepresentation(org.restlet.representation.StringRepresentation) ConstraintType(org.apache.helix.model.ClusterConstraints.ConstraintType) HelixAdmin(org.apache.helix.HelixAdmin) ZKHelixAdmin(org.apache.helix.manager.zk.ZKHelixAdmin) ZNRecord(org.apache.helix.ZNRecord)

Example 29 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project helix by apache.

the class ControllerResource method getControllerRepresentation.

StringRepresentation getControllerRepresentation(String clusterName) throws JsonGenerationException, JsonMappingException, IOException {
    Builder keyBuilder = new PropertyKey.Builder(clusterName);
    ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
    ZKHelixDataAccessor accessor = new ZKHelixDataAccessor(clusterName, new ZkBaseDataAccessor<ZNRecord>(zkClient));
    ZNRecord record = null;
    LiveInstance leader = accessor.getProperty(keyBuilder.controllerLeader());
    if (leader != null) {
        record = leader.getRecord();
    } else {
        record = new ZNRecord("");
        DateFormat formatter = new SimpleDateFormat("yyyyMMdd-HHmmss.SSSSSS");
        String time = formatter.format(new Date());
        Map<String, String> contentMap = new TreeMap<String, String>();
        contentMap.put("AdditionalInfo", "No leader exists");
        record.setMapField(Level.HELIX_INFO + "-" + time, contentMap);
    }
    boolean paused = (accessor.getProperty(keyBuilder.pause()) == null ? false : true);
    record.setSimpleField(PropertyType.PAUSE.toString(), "" + paused);
    String retVal = ClusterRepresentationUtil.ZNRecordToJson(record);
    StringRepresentation representation = new StringRepresentation(retVal, MediaType.APPLICATION_JSON);
    return representation;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) Builder(org.apache.helix.PropertyKey.Builder) TreeMap(java.util.TreeMap) Date(java.util.Date) LiveInstance(org.apache.helix.model.LiveInstance) StringRepresentation(org.restlet.representation.StringRepresentation) SimpleDateFormat(java.text.SimpleDateFormat) DateFormat(java.text.DateFormat) SimpleDateFormat(java.text.SimpleDateFormat) ZNRecord(org.apache.helix.ZNRecord) ZKHelixDataAccessor(org.apache.helix.manager.zk.ZKHelixDataAccessor)

Example 30 with StringRepresentation

use of org.restlet.representation.StringRepresentation in project helix by apache.

the class ControllerResource method get.

/**
 * List Helix controller info
 * <p>
 * Usage: <code>curl http://{host:port}/clusters/{cluster}/Controller
 */
@Override
public Representation get() {
    StringRepresentation presentation = null;
    try {
        String clusterName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CLUSTER_NAME);
        presentation = getControllerRepresentation(clusterName);
    } catch (Exception e) {
        LOG.error("Exception get controller info", e);
        String error = ClusterRepresentationUtil.getErrorAsJsonStringFromException(e);
        presentation = new StringRepresentation(error, MediaType.APPLICATION_JSON);
    }
    return presentation;
}
Also used : StringRepresentation(org.restlet.representation.StringRepresentation) HelixException(org.apache.helix.HelixException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException)

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