use of org.apache.helix.HelixAdmin 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;
}
use of org.apache.helix.HelixAdmin 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.HelixAdmin in project helix by apache.
the class ClusterAccessor method getClusters.
@GET
public Response getClusters() {
HelixAdmin helixAdmin = getHelixAdmin();
List<String> clusters = helixAdmin.getClusters();
Map<String, List<String>> dataMap = new HashMap<>();
dataMap.put(ClusterProperties.clusters.name(), clusters);
return JSONRepresentation(dataMap);
}
use of org.apache.helix.HelixAdmin in project helix by apache.
the class InstanceAccessor method updateInstances.
@POST
public Response updateInstances(@PathParam("clusterId") String clusterId, @QueryParam("command") String command, String content) {
Command cmd;
try {
cmd = Command.valueOf(command);
} catch (Exception e) {
return badRequest("Invalid command : " + command);
}
HelixAdmin admin = getHelixAdmin();
try {
JsonNode node = null;
if (content.length() != 0) {
node = OBJECT_MAPPER.readTree(content);
}
if (node == null) {
return badRequest("Invalid input for content : " + content);
}
List<String> enableInstances = OBJECT_MAPPER.readValue(node.get(InstanceProperties.instances.name()).toString(), OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, String.class));
switch(cmd) {
case enable:
admin.enableInstance(clusterId, enableInstances, true);
break;
case disable:
admin.enableInstance(clusterId, enableInstances, false);
break;
default:
_logger.error("Unsupported command :" + command);
return badRequest("Unsupported command :" + command);
}
} catch (Exception e) {
_logger.error("Failed in updating instances : " + content, e);
return badRequest(e.getMessage());
}
return OK();
}
use of org.apache.helix.HelixAdmin in project helix by apache.
the class InstanceAccessor method updateInstance.
@POST
@Path("{instanceName}")
public Response updateInstance(@PathParam("clusterId") String clusterId, @PathParam("instanceName") String instanceName, @QueryParam("command") String command, String content) {
Command cmd;
try {
cmd = Command.valueOf(command);
} catch (Exception e) {
return badRequest("Invalid command : " + command);
}
HelixAdmin admin = getHelixAdmin();
try {
JsonNode node = null;
if (content.length() != 0) {
node = OBJECT_MAPPER.readTree(content);
}
switch(cmd) {
case enable:
admin.enableInstance(clusterId, instanceName, true);
break;
case disable:
admin.enableInstance(clusterId, instanceName, false);
break;
case reset:
if (!validInstance(node, instanceName)) {
return badRequest("Instance names are not match!");
}
admin.resetPartition(clusterId, instanceName, node.get(InstanceProperties.resource.name()).toString(), (List<String>) OBJECT_MAPPER.readValue(node.get(InstanceProperties.partitions.name()).toString(), OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, String.class)));
break;
case addInstanceTag:
if (!validInstance(node, instanceName)) {
return badRequest("Instance names are not match!");
}
for (String tag : (List<String>) OBJECT_MAPPER.readValue(node.get(InstanceProperties.instanceTags.name()).toString(), OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, String.class))) {
admin.addInstanceTag(clusterId, instanceName, tag);
}
break;
case removeInstanceTag:
if (!validInstance(node, instanceName)) {
return badRequest("Instance names are not match!");
}
for (String tag : (List<String>) OBJECT_MAPPER.readValue(node.get(InstanceProperties.instanceTags.name()).toString(), OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, String.class))) {
admin.removeInstanceTag(clusterId, instanceName, tag);
}
break;
case enablePartitions:
admin.enablePartition(true, clusterId, instanceName, node.get(InstanceProperties.resource.name()).getTextValue(), (List<String>) OBJECT_MAPPER.readValue(node.get(InstanceProperties.partitions.name()).toString(), OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, String.class)));
break;
case disablePartitions:
admin.enablePartition(false, clusterId, instanceName, node.get(InstanceProperties.resource.name()).getTextValue(), (List<String>) OBJECT_MAPPER.readValue(node.get(InstanceProperties.partitions.name()).toString(), OBJECT_MAPPER.getTypeFactory().constructCollectionType(List.class, String.class)));
break;
default:
_logger.error("Unsupported command :" + command);
return badRequest("Unsupported command :" + command);
}
} catch (Exception e) {
_logger.error("Failed in updating instance : " + instanceName, e);
return badRequest(e.getMessage());
}
return OK();
}
Aggregations