Search in sources :

Example 1 with ConstraintType

use of org.apache.helix.model.ClusterConstraints.ConstraintType 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 2 with ConstraintType

use of org.apache.helix.model.ClusterConstraints.ConstraintType in project helix by apache.

the class ClusterSetup method removeConstraint.

/**
 * remove constraint
 * @param clusterName
 * @param constraintType
 * @param constraintId
 */
public void removeConstraint(String clusterName, String constraintType, String constraintId) {
    if (clusterName == null || constraintType == null || constraintId == null) {
        throw new IllegalArgumentException("fail to remove constraint. missing clusterName|constraintType|constraintId");
    }
    ConstraintType type = ConstraintType.valueOf(constraintType);
    _admin.removeConstraint(clusterName, type, constraintId);
}
Also used : ConstraintType(org.apache.helix.model.ClusterConstraints.ConstraintType)

Example 3 with ConstraintType

use of org.apache.helix.model.ClusterConstraints.ConstraintType in project helix by apache.

the class ClusterSetup method setConstraint.

/**
 * set constraint
 * @param clusterName
 * @param constraintType
 * @param constraintId
 * @param constraintAttributesMap : csv-formated constraint key-value pairs
 */
public void setConstraint(String clusterName, String constraintType, String constraintId, String constraintAttributesMap) {
    if (clusterName == null || constraintType == null || constraintId == null || constraintAttributesMap == null) {
        throw new IllegalArgumentException("fail to set constraint. missing clusterName|constraintType|constraintId|constraintAttributesMap");
    }
    ConstraintType type = ConstraintType.valueOf(constraintType);
    ConstraintItemBuilder builder = new ConstraintItemBuilder();
    Map<String, String> constraintAttributes = HelixUtil.parseCsvFormatedKeyValuePairs(constraintAttributesMap);
    ConstraintItem constraintItem = builder.addConstraintAttributes(constraintAttributes).build();
    _admin.setConstraint(clusterName, type, constraintId, constraintItem);
}
Also used : ConstraintItem(org.apache.helix.model.ConstraintItem) ConstraintType(org.apache.helix.model.ClusterConstraints.ConstraintType) ConstraintItemBuilder(org.apache.helix.model.builder.ConstraintItemBuilder)

Example 4 with ConstraintType

use of org.apache.helix.model.ClusterConstraints.ConstraintType in project helix by apache.

the class ClusterSetup method getConstraints.

/**
 * get constraints associated with given type
 * @param constraintType : constraint-type. e.g. MESSAGE_CONSTRAINT
 * @return json-formated constraints
 */
public String getConstraints(String clusterName, String constraintType) {
    if (clusterName == null || constraintType == null) {
        throw new IllegalArgumentException("fail to get constraint. missing clusterName|constraintType");
    }
    ConstraintType type = ConstraintType.valueOf(constraintType);
    ClusterConstraints constraints = _admin.getConstraints(clusterName, type);
    return new String(constraints.serialize(new ZNRecordSerializer()));
}
Also used : ClusterConstraints(org.apache.helix.model.ClusterConstraints) ConstraintType(org.apache.helix.model.ClusterConstraints.ConstraintType) ZNRecordSerializer(org.apache.helix.manager.zk.ZNRecordSerializer)

Aggregations

ConstraintType (org.apache.helix.model.ClusterConstraints.ConstraintType)4 HelixAdmin (org.apache.helix.HelixAdmin)1 ZNRecord (org.apache.helix.ZNRecord)1 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)1 ZNRecordSerializer (org.apache.helix.manager.zk.ZNRecordSerializer)1 ZkClient (org.apache.helix.manager.zk.ZkClient)1 ClusterConstraints (org.apache.helix.model.ClusterConstraints)1 ConstraintItem (org.apache.helix.model.ConstraintItem)1 ConstraintItemBuilder (org.apache.helix.model.builder.ConstraintItemBuilder)1 StringRepresentation (org.restlet.representation.StringRepresentation)1