use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class JobQueuesResource method getHostedEntitiesRepresentation.
StringRepresentation getHostedEntitiesRepresentation(String clusterName) throws JsonGenerationException, JsonMappingException, IOException {
// Get all resources
ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
PropertyKey.Builder keyBuilder = accessor.keyBuilder();
Map<String, HelixProperty> resourceConfigMap = accessor.getChildValuesMap(keyBuilder.resourceConfigs());
// Create the result
ZNRecord hostedEntitiesRecord = new ZNRecord("JobQueues");
// Filter out non-workflow resources
Iterator<Map.Entry<String, HelixProperty>> it = resourceConfigMap.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, HelixProperty> e = it.next();
HelixProperty resource = e.getValue();
Map<String, String> simpleFields = resource.getRecord().getSimpleFields();
boolean isTerminable = resource.getRecord().getBooleanField(WorkflowConfig.WorkflowConfigProperty.Terminable.name(), true);
if (!simpleFields.containsKey(WorkflowConfig.WorkflowConfigProperty.TargetState.name()) || !simpleFields.containsKey(WorkflowConfig.WorkflowConfigProperty.Dag.name()) || isTerminable) {
it.remove();
}
}
// Populate the result
List<String> allResources = Lists.newArrayList(resourceConfigMap.keySet());
hostedEntitiesRecord.setListField("JobQueues", allResources);
StringRepresentation representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(hostedEntitiesRecord), MediaType.APPLICATION_JSON);
return representation;
}
use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class ResourceGroupResource method delete.
@Override
public Representation delete() {
try {
String clusterName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CLUSTER_NAME);
String resourceName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.RESOURCE_NAME);
ZkClient zkclient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
ClusterSetup setupTool = new ClusterSetup(zkclient);
setupTool.dropResourceFromCluster(clusterName, resourceName);
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 ResourceGroupResource method getIdealStateRepresentation.
StringRepresentation getIdealStateRepresentation(String clusterName, String resourceName) throws JsonGenerationException, JsonMappingException, IOException {
Builder keyBuilder = new PropertyKey.Builder(clusterName);
ZkClient zkclient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.RAW_ZKCLIENT);
String idealStateStr = ResourceUtil.readZkAsBytes(zkclient, keyBuilder.idealStates(resourceName));
StringRepresentation representation = new StringRepresentation(idealStateStr, MediaType.APPLICATION_JSON);
return representation;
}
use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class ResourceGroupsResource method getHostedEntitiesRepresentation.
StringRepresentation getHostedEntitiesRepresentation(String clusterName) throws JsonGenerationException, JsonMappingException, IOException {
// Get all resources
ZkClient zkclient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.RAW_ZKCLIENT);
PropertyKey.Builder keyBuilder = new PropertyKey.Builder(clusterName);
Map<String, String> idealStateMap = ResourceUtil.readZkChildrenAsBytesMap(zkclient, keyBuilder.idealStates());
// Create the result
ZNRecord hostedEntitiesRecord = new ZNRecord("ResourceGroups");
// Figure out which tags are present on which resources
Map<String, String> tagMap = Maps.newHashMap();
for (String resourceName : idealStateMap.keySet()) {
String idealStateStr = idealStateMap.get(resourceName);
String tag = ResourceUtil.extractSimpleFieldFromZNRecord(idealStateStr, IdealState.IdealStateProperty.INSTANCE_GROUP_TAG.toString());
if (tag != null) {
tagMap.put(resourceName, tag);
}
}
// Populate the result
List<String> allResources = Lists.newArrayList(idealStateMap.keySet());
hostedEntitiesRecord.setListField("ResourceGroups", allResources);
if (!tagMap.isEmpty()) {
hostedEntitiesRecord.setMapField("ResourceTags", tagMap);
}
StringRepresentation representation = new StringRepresentation(ClusterRepresentationUtil.ZNRecordToJson(hostedEntitiesRecord), MediaType.APPLICATION_JSON);
return representation;
}
use of org.apache.helix.manager.zk.ZkClient in project helix by apache.
the class SchedulerTasksResource method post.
@Override
public Representation post(Representation entity) {
try {
String clusterName = (String) getRequest().getAttributes().get("clusterName");
Form form = new Form(entity);
ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
String msgTemplateString = ClusterRepresentationUtil.getFormJsonParameterString(form, MESSAGETEMPLATE);
if (msgTemplateString == null) {
throw new HelixException("SchedulerTasksResource need to have MessageTemplate specified.");
}
Map<String, String> messageTemplate = ClusterRepresentationUtil.getFormJsonParameters(form, MESSAGETEMPLATE);
String criteriaString = ClusterRepresentationUtil.getFormJsonParameterString(form, CRITERIA);
if (criteriaString == null) {
throw new HelixException("SchedulerTasksResource need to have Criteria specified.");
}
HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
LiveInstance leader = accessor.getProperty(accessor.keyBuilder().controllerLeader());
if (leader == null) {
throw new HelixException("There is no leader for the cluster " + clusterName);
}
Message schedulerMessage = new Message(MessageType.SCHEDULER_MSG, UUID.randomUUID().toString());
schedulerMessage.getRecord().getSimpleFields().put(CRITERIA, criteriaString);
schedulerMessage.getRecord().getMapFields().put(MESSAGETEMPLATE, messageTemplate);
schedulerMessage.setTgtSessionId(leader.getSessionId());
schedulerMessage.setTgtName("CONTROLLER");
schedulerMessage.setSrcInstanceType(InstanceType.CONTROLLER);
String taskQueueName = ClusterRepresentationUtil.getFormJsonParameterString(form, TASKQUEUENAME);
if (taskQueueName != null && taskQueueName.length() > 0) {
schedulerMessage.getRecord().setSimpleField(DefaultSchedulerMessageHandlerFactory.SCHEDULER_TASK_QUEUE, taskQueueName);
}
accessor.setProperty(accessor.keyBuilder().controllerMessage(schedulerMessage.getMsgId()), schedulerMessage);
Map<String, String> resultMap = new HashMap<String, String>();
resultMap.put("StatusUpdatePath", PropertyPathBuilder.controllerStatusUpdate(clusterName, MessageType.SCHEDULER_MSG.name(), schedulerMessage.getMsgId()));
resultMap.put("MessageType", Message.MessageType.SCHEDULER_MSG.name());
resultMap.put("MsgId", schedulerMessage.getMsgId());
// Assemble the rest URL for task status update
String ipAddress = InetAddress.getLocalHost().getCanonicalHostName();
String url = "http://" + ipAddress + ":" + getContext().getAttributes().get(RestAdminApplication.PORT) + "/clusters/" + clusterName + "/Controller/statusUpdates/SCHEDULER_MSG/" + schedulerMessage.getMsgId();
resultMap.put("statusUpdateUrl", url);
getResponse().setEntity(ClusterRepresentationUtil.ObjectToJson(resultMap), MediaType.APPLICATION_JSON);
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;
}
Aggregations