Search in sources :

Example 96 with ZkClient

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;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) HelixDataAccessor(org.apache.helix.HelixDataAccessor) HelixProperty(org.apache.helix.HelixProperty) StringRepresentation(org.restlet.representation.StringRepresentation) Map(java.util.Map) PropertyKey(org.apache.helix.PropertyKey) ZNRecord(org.apache.helix.ZNRecord)

Example 97 with ZkClient

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;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) ClusterSetup(org.apache.helix.tools.ClusterSetup) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException) HelixException(org.apache.helix.HelixException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException)

Example 98 with ZkClient

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;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) StringRepresentation(org.restlet.representation.StringRepresentation) Builder(org.apache.helix.PropertyKey.Builder)

Example 99 with ZkClient

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;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) StringRepresentation(org.restlet.representation.StringRepresentation) PropertyKey(org.apache.helix.PropertyKey) ZNRecord(org.apache.helix.ZNRecord)

Example 100 with ZkClient

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;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) HelixException(org.apache.helix.HelixException) HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) Message(org.apache.helix.model.Message) Form(org.restlet.data.Form) HashMap(java.util.HashMap) HelixException(org.apache.helix.HelixException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException)

Aggregations

ZkClient (org.apache.helix.manager.zk.ZkClient)109 ZNRecordSerializer (org.apache.helix.manager.zk.ZNRecordSerializer)39 ZNRecord (org.apache.helix.ZNRecord)29 StringRepresentation (org.restlet.representation.StringRepresentation)29 ClusterSetup (org.apache.helix.tools.ClusterSetup)26 HelixException (org.apache.helix.HelixException)23 Builder (org.apache.helix.PropertyKey.Builder)18 IOException (java.io.IOException)17 HelixDataAccessor (org.apache.helix.HelixDataAccessor)15 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)15 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)15 ZKHelixAdmin (org.apache.helix.manager.zk.ZKHelixAdmin)12 ZKHelixDataAccessor (org.apache.helix.manager.zk.ZKHelixDataAccessor)10 Test (org.testng.annotations.Test)10 Date (java.util.Date)9 BeforeClass (org.testng.annotations.BeforeClass)9 PropertyKey (org.apache.helix.PropertyKey)8 IdealState (org.apache.helix.model.IdealState)8 InstanceConfig (org.apache.helix.model.InstanceConfig)8 StateModelDefinition (org.apache.helix.model.StateModelDefinition)7