use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class InstanceResource method post.
@Override
public Representation post(Representation entity) {
try {
String clusterName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CLUSTER_NAME);
String instanceName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.INSTANCE_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.equalsIgnoreCase(ClusterSetup.enableInstance)) {
jsonParameters.verifyCommand(ClusterSetup.enableInstance);
boolean enabled = Boolean.parseBoolean(jsonParameters.getParameter(JsonParameters.ENABLED));
setupTool.getClusterManagementTool().enableInstance(clusterName, instanceName, enabled);
} else if (command.equalsIgnoreCase(ClusterSetup.enablePartition)) {
jsonParameters.verifyCommand(ClusterSetup.enablePartition);
boolean enabled = Boolean.parseBoolean(jsonParameters.getParameter(JsonParameters.ENABLED));
String[] partitions = jsonParameters.getParameter(JsonParameters.PARTITION).split(";");
String resource = jsonParameters.getParameter(JsonParameters.RESOURCE);
setupTool.getClusterManagementTool().enablePartition(enabled, clusterName, instanceName, resource, Arrays.asList(partitions));
} else if (command.equalsIgnoreCase(ClusterSetup.resetPartition)) {
jsonParameters.verifyCommand(ClusterSetup.resetPartition);
String resource = jsonParameters.getParameter(JsonParameters.RESOURCE);
String[] partitionNames = jsonParameters.getParameter(JsonParameters.PARTITION).split("\\s+");
setupTool.getClusterManagementTool().resetPartition(clusterName, instanceName, resource, Arrays.asList(partitionNames));
} else if (command.equalsIgnoreCase(ClusterSetup.resetInstance)) {
jsonParameters.verifyCommand(ClusterSetup.resetInstance);
setupTool.getClusterManagementTool().resetInstance(clusterName, Arrays.asList(instanceName));
} else if (command.equalsIgnoreCase(ClusterSetup.addInstanceTag)) {
jsonParameters.verifyCommand(ClusterSetup.addInstanceTag);
String tag = jsonParameters.getParameter(ClusterSetup.instanceGroupTag);
setupTool.getClusterManagementTool().addInstanceTag(clusterName, instanceName, tag);
} else if (command.equalsIgnoreCase(ClusterSetup.removeInstanceTag)) {
jsonParameters.verifyCommand(ClusterSetup.removeInstanceTag);
String tag = jsonParameters.getParameter(ClusterSetup.instanceGroupTag);
setupTool.getClusterManagementTool().removeInstanceTag(clusterName, instanceName, tag);
} else {
throw new HelixException("Unsupported command: " + command + ". Should be one of [" + ClusterSetup.enableInstance + ", " + ClusterSetup.enablePartition + ", " + ClusterSetup.resetInstance + "]");
}
getResponse().setEntity(getInstanceRepresentation());
getResponse().setStatus(Status.SUCCESS_OK);
} catch (Exception e) {
getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e), MediaType.APPLICATION_JSON);
getResponse().setStatus(Status.SUCCESS_OK);
LOG.error("Exception in post instance", e);
}
return null;
}
use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class InstanceResource method delete.
@Override
public Representation delete() {
try {
String clusterName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CLUSTER_NAME);
String instanceName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.INSTANCE_NAME);
ZkClient zkclient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
ClusterSetup setupTool = new ClusterSetup(zkclient);
setupTool.dropInstanceFromCluster(clusterName, instanceName);
getResponse().setStatus(Status.SUCCESS_OK);
} catch (Exception e) {
getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e), MediaType.APPLICATION_JSON);
getResponse().setStatus(Status.SUCCESS_OK);
LOG.error("Error in delete instance", e);
}
return null;
}
use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class InstanceResource method getInstanceRepresentation.
StringRepresentation getInstanceRepresentation() throws JsonGenerationException, JsonMappingException, IOException {
String clusterName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CLUSTER_NAME);
String instanceName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.INSTANCE_NAME);
Builder keyBuilder = new PropertyKey.Builder(clusterName);
ZkClient zkclient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.RAW_ZKCLIENT);
String instanceCfgStr = ResourceUtil.readZkAsBytes(zkclient, keyBuilder.instanceConfig(instanceName));
StringRepresentation representation = new StringRepresentation(instanceCfgStr, MediaType.APPLICATION_JSON);
return representation;
}
use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class InstancesResource method post.
@Override
public Representation post(Representation entity) {
try {
String clusterName = (String) getRequest().getAttributes().get("clusterName");
JsonParameters jsonParameters = new JsonParameters(entity);
String command = jsonParameters.getCommand();
ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
ClusterSetup setupTool = new ClusterSetup(zkClient);
if (command.equalsIgnoreCase(ClusterSetup.addInstance) || JsonParameters.CLUSTERSETUP_COMMAND_ALIASES.get(ClusterSetup.addInstance).contains(command)) {
if (jsonParameters.getParameter(JsonParameters.INSTANCE_NAME) != null) {
setupTool.addInstanceToCluster(clusterName, jsonParameters.getParameter(JsonParameters.INSTANCE_NAME));
} else if (jsonParameters.getParameter(JsonParameters.INSTANCE_NAMES) != null) {
setupTool.addInstancesToCluster(clusterName, jsonParameters.getParameter(JsonParameters.INSTANCE_NAMES).split(";"));
} else {
throw new HelixException("Missing Json paramaters: '" + JsonParameters.INSTANCE_NAME + "' or '" + JsonParameters.INSTANCE_NAMES + "' ");
}
} else if (command.equalsIgnoreCase(ClusterSetup.swapInstance)) {
if (jsonParameters.getParameter(JsonParameters.NEW_INSTANCE) == null || jsonParameters.getParameter(JsonParameters.OLD_INSTANCE) == null) {
throw new HelixException("Missing Json paramaters: '" + JsonParameters.NEW_INSTANCE + "' or '" + JsonParameters.OLD_INSTANCE + "' ");
}
setupTool.swapInstance(clusterName, jsonParameters.getParameter(JsonParameters.OLD_INSTANCE), jsonParameters.getParameter(JsonParameters.NEW_INSTANCE));
} else {
throw new HelixException("Unsupported command: " + command + ". Should be one of [" + ClusterSetup.addInstance + ", " + ClusterSetup.swapInstance + "]");
}
getResponse().setEntity(getInstancesRepresentation(clusterName));
getResponse().setStatus(Status.SUCCESS_OK);
} catch (Exception e) {
getResponse().setEntity(ClusterRepresentationUtil.getErrorAsJsonStringFromException(e), MediaType.APPLICATION_JSON);
getResponse().setStatus(Status.SUCCESS_OK);
LOG.error("", e);
}
return null;
}
use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class JobQueueResource method getHostedEntitiesRepresentation.
StringRepresentation getHostedEntitiesRepresentation(String clusterName, String jobQueueName) throws Exception {
ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
TaskDriver taskDriver = new TaskDriver(zkClient, clusterName);
// Get job queue config
// TODO: fix this to use workflowConfig.
ResourceConfig jobQueueConfig = accessor.getProperty(keyBuilder.resourceConfig(jobQueueName));
// Get job queue context
WorkflowContext ctx = taskDriver.getWorkflowContext(jobQueueName);
// Create the result
ZNRecord hostedEntitiesRecord = new ZNRecord(jobQueueName);
if (jobQueueConfig != null) {
hostedEntitiesRecord.merge(jobQueueConfig.getRecord());
}
if (ctx != null) {
hostedEntitiesRecord.merge(ctx.getRecord());
}
StringRepresentation representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(hostedEntitiesRecord), MediaType.APPLICATION_JSON);
return representation;
}
Aggregations