Search in sources :

Example 16 with ZkClient

use of org.apache.helix.manager.zk.ZkClient in project helix by apache.

the class ConstraintResource method post.

/**
 * Set constraints
 * <p>
 * Usage:
 * <code>curl -d 'jsonParameters={"constraintAttributes":"RESOURCE={resource},CONSTRAINT_VALUE={1}"}'
 * -H "Content-Type: application/json" http://{host:port}/clusters/{cluster}/constraints/MESSAGE_CONSTRAINT/{constraintId}
 */
@Override
public Representation post(Representation entity) {
    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 {
        ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
        ClusterSetup setupTool = new ClusterSetup(zkClient);
        JsonParameters jsonParameters = new JsonParameters(entity);
        String constraintAttrStr = jsonParameters.getParameter(JsonParameters.CONSTRAINT_ATTRIBUTES);
        setupTool.setConstraint(clusterName, constraintTypeStr, constraintId, constraintAttrStr);
    } catch (Exception e) {
        LOG.error("Error in posting " + entity, e);
        getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e), MediaType.APPLICATION_JSON);
        getResponse().setStatus(Status.SUCCESS_OK);
    }
    return null;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) ClusterSetup(org.apache.helix.tools.ClusterSetup)

Example 17 with ZkClient

use of org.apache.helix.manager.zk.ZkClient in project helix by apache.

the class ConstraintResource method delete.

/**
 * Remove constraints
 * <p>
 * Usage:
 * <code>curl -X DELETE http://{host:port}/clusters/{cluster}/constraints/MESSAGE_CONSTRAINT/{constraintId}
 */
@Override
public Representation delete() {
    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 {
        ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
        ClusterSetup setupTool = new ClusterSetup(zkClient);
        setupTool.removeConstraint(clusterName, constraintTypeStr, constraintId);
    } catch (Exception e) {
        LOG.error("Error in delete constraint", e);
        getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e), MediaType.APPLICATION_JSON);
        getResponse().setStatus(Status.SUCCESS_OK);
    }
    return null;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) ClusterSetup(org.apache.helix.tools.ClusterSetup)

Example 18 with ZkClient

use of org.apache.helix.manager.zk.ZkClient 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 19 with ZkClient

use of org.apache.helix.manager.zk.ZkClient in project helix by apache.

the class ControllerResource method post.

/**
 * Enable/disable Helix controller
 * <p>
 * Usage:
 * <code>curl -d 'jsonParameters={"command":"enableCluster","enabled":"{true/false}"}'
 * -H "Content-Type: application/json" http://{host:port}/clusters/{cluster}/Controller
 */
@Override
public Representation post(Representation entity) {
    try {
        String clusterName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CLUSTER_NAME);
        ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
        ClusterSetup setupTool = new ClusterSetup(zkClient);
        JsonParameters jsonParameters = new JsonParameters(entity);
        String command = jsonParameters.getCommand();
        if (command == null) {
            throw new HelixException("Could NOT find 'command' in parameterMap: " + jsonParameters._parameterMap);
        } else if (command.equalsIgnoreCase(ClusterSetup.enableCluster)) {
            boolean enabled = Boolean.parseBoolean(jsonParameters.getParameter(JsonParameters.ENABLED));
            setupTool.getClusterManagementTool().enableCluster(clusterName, enabled);
        } else {
            throw new HelixException("Unsupported command: " + command + ". Should be one of [" + ClusterSetup.enableCluster + "]");
        }
        getResponse().setEntity(getControllerRepresentation(clusterName));
        getResponse().setStatus(Status.SUCCESS_OK);
    } catch (Exception e) {
        getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e), MediaType.APPLICATION_JSON);
        getResponse().setStatus(Status.SUCCESS_OK);
    }
    return null;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) HelixException(org.apache.helix.HelixException) ClusterSetup(org.apache.helix.tools.ClusterSetup) HelixException(org.apache.helix.HelixException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException)

Example 20 with ZkClient

use of org.apache.helix.manager.zk.ZkClient 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)

Aggregations

ZkClient (org.apache.helix.manager.zk.ZkClient)109 ZNRecordSerializer (org.apache.helix.manager.zk.ZNRecordSerializer)39 ZNRecord (org.apache.helix.ZNRecord)29 StringRepresentation (org.restlet.representation.StringRepresentation)29 ClusterSetup (org.apache.helix.tools.ClusterSetup)26 HelixException (org.apache.helix.HelixException)23 Builder (org.apache.helix.PropertyKey.Builder)18 IOException (java.io.IOException)17 HelixDataAccessor (org.apache.helix.HelixDataAccessor)15 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)15 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)15 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)12 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)10 Test (org.testng.annotations.Test)10 Date (java.util.Date)9 BeforeClass (org.testng.annotations.BeforeClass)9 PropertyKey (org.apache.helix.PropertyKey)8 IdealState (org.apache.helix.model.IdealState)8 InstanceConfig (org.apache.helix.model.InstanceConfig)8 StateModelDefinition (org.apache.helix.model.StateModelDefinition)7