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;
}
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);
}
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);
}
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()));
}
Aggregations