Search in sources :

Example 31 with ZkClient

use of org.apache.helix.manager.zk.ZkClient in project helix by apache.

the class ErrorsResource method getInstanceErrorsRepresentation.

StringRepresentation getInstanceErrorsRepresentation(String clusterName, String instanceName) throws JsonGenerationException, JsonMappingException, IOException {
    ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
    ;
    String instanceSessionId = ClusterRepresentationUtil.getInstanceSessionId(zkClient, clusterName, instanceName);
    String message = ClusterRepresentationUtil.getInstancePropertyNameListAsString(zkClient, clusterName, instanceName, PropertyType.CURRENTSTATES, instanceSessionId, MediaType.APPLICATION_JSON);
    StringRepresentation representation = new StringRepresentation(message, MediaType.APPLICATION_JSON);
    return representation;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) StringRepresentation(org.restlet.representation.StringRepresentation)

Example 32 with ZkClient

use of org.apache.helix.manager.zk.ZkClient in project helix by apache.

the class ExternalViewResource method getExternalViewRepresentation.

StringRepresentation getExternalViewRepresentation(String clusterName, String resourceName) throws JsonGenerationException, JsonMappingException, IOException {
    Builder keyBuilder = new PropertyKey.Builder(clusterName);
    ZkClient zkclient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.RAW_ZKCLIENT);
    String extViewStr = ResourceUtil.readZkAsBytes(zkclient, keyBuilder.externalView(resourceName));
    StringRepresentation representation = new StringRepresentation(extViewStr, 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 33 with ZkClient

use of org.apache.helix.manager.zk.ZkClient in project helix by apache.

the class IdealStateResource 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 34 with ZkClient

use of org.apache.helix.manager.zk.ZkClient in project helix by apache.

the class InstancesResource method getInstancesRepresentation.

StringRepresentation getInstancesRepresentation(String clusterName) throws JsonGenerationException, JsonMappingException, IOException {
    ZkClient zkClient = (ZkClient) getContext().getAttributes().get(RestAdminApplication.ZKCLIENT);
    HelixDataAccessor accessor = ClusterRepresentationUtil.getClusterDataAccessor(zkClient, clusterName);
    Map<String, LiveInstance> liveInstancesMap = accessor.getChildValuesMap(accessor.keyBuilder().liveInstances());
    Map<String, InstanceConfig> instanceConfigsMap = accessor.getChildValuesMap(accessor.keyBuilder().instanceConfigs());
    Map<String, List<String>> tagInstanceLists = new TreeMap<String, List<String>>();
    for (String instanceName : instanceConfigsMap.keySet()) {
        boolean isAlive = liveInstancesMap.containsKey(instanceName);
        instanceConfigsMap.get(instanceName).getRecord().setSimpleField("Alive", isAlive + "");
        InstanceConfig config = instanceConfigsMap.get(instanceName);
        for (String tag : config.getTags()) {
            if (!tagInstanceLists.containsKey(tag)) {
                tagInstanceLists.put(tag, new LinkedList<String>());
            }
            if (!tagInstanceLists.get(tag).contains(instanceName)) {
                tagInstanceLists.get(tag).add(instanceName);
            }
        }
    }
    // Wrap raw data into an object, then serialize it
    List<ZNRecord> recordList = Lists.newArrayList();
    for (InstanceConfig instanceConfig : instanceConfigsMap.values()) {
        recordList.add(instanceConfig.getRecord());
    }
    ListInstancesWrapper wrapper = new ListInstancesWrapper();
    wrapper.instanceInfo = recordList;
    wrapper.tagInfo = tagInstanceLists;
    StringRepresentation representation = new StringRepresentation(ClusterRepresentationUtil.ObjectToJson(wrapper), MediaType.APPLICATION_JSON);
    return representation;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) TreeMap(java.util.TreeMap) HelixDataAccessor(org.apache.helix.HelixDataAccessor) LiveInstance(org.apache.helix.model.LiveInstance) InstanceConfig(org.apache.helix.model.InstanceConfig) StringRepresentation(org.restlet.representation.StringRepresentation) List(java.util.List) LinkedList(java.util.LinkedList) ZNRecord(org.apache.helix.ZNRecord)

Example 35 with ZkClient

use of org.apache.helix.manager.zk.ZkClient in project helix by apache.

the class JobQueueResource method post.

/**
 * Start a new job in a job queue, or stop/resume/flush/delete a job queue
 * <p>
 * Usage:
 * <p>
 * <li>Start a new job in a job queue:
 * <code>curl -d @'./{input.txt}' -H 'Content-Type: application/json'
 * http://{host:port}/clusters/{clusterName}/jobQueues/{jobQueue}
 * <p>
 * input.txt: <code>jsonParameters={"command":"start"}&newJob={newJobConfig.yaml}
 * <p>
 * For newJobConfig.yaml, see {@link Workflow#parse(String)}
 * <li>Stop/resume/flush/delete a job queue:
 * <code>curl -d 'jsonParameters={"command":"{stop/resume/flush/delete}"}'
 * -H "Content-Type: application/json" http://{host:port}/clusters/{clusterName}/jobQueues/{jobQueue}
 */
@Override
public Representation post(Representation entity) {
    String clusterName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.CLUSTER_NAME);
    String jobQueueName = ResourceUtil.getAttributeFromRequest(getRequest(), ResourceUtil.RequestKey.JOB_QUEUE);
    ZkClient zkClient = ResourceUtil.getAttributeFromCtx(getContext(), ResourceUtil.ContextKey.ZKCLIENT);
    try {
        TaskDriver driver = new TaskDriver(zkClient, clusterName);
        Form form = new Form(entity);
        JsonParameters jsonParameters = new JsonParameters(form);
        TaskDriver.DriverCommand cmd = TaskDriver.DriverCommand.valueOf(jsonParameters.getCommand());
        switch(cmd) {
            case start:
                {
                    // Get the job queue and submit it
                    String yamlPayload = ResourceUtil.getYamlParameters(form, ResourceUtil.YamlParamKey.NEW_JOB);
                    if (yamlPayload == null) {
                        throw new HelixException("Yaml job config is required!");
                    }
                    Workflow workflow = Workflow.parse(yamlPayload);
                    for (String jobName : workflow.getJobConfigs().keySet()) {
                        Map<String, String> jobCfgMap = workflow.getJobConfigs().get(jobName);
                        JobConfig.Builder jobCfgBuilder = JobConfig.Builder.fromMap(jobCfgMap);
                        if (workflow.getTaskConfigs() != null && workflow.getTaskConfigs().containsKey(jobName)) {
                            jobCfgBuilder.addTaskConfigs(workflow.getTaskConfigs().get(jobName));
                        }
                        driver.enqueueJob(jobQueueName, TaskUtil.getDenamespacedJobName(jobQueueName, jobName), jobCfgBuilder);
                    }
                    break;
                }
            case stop:
                {
                    driver.stop(jobQueueName);
                    break;
                }
            case resume:
                {
                    driver.resume(jobQueueName);
                    break;
                }
            case flush:
                {
                    driver.flushQueue(jobQueueName);
                    break;
                }
            case delete:
                {
                    driver.delete(jobQueueName);
                    break;
                }
            case clean:
                {
                    driver.cleanupQueue(jobQueueName);
                    break;
                }
            default:
                throw new HelixException("Unsupported job queue command: " + cmd);
        }
        getResponse().setEntity(getHostedEntitiesRepresentation(clusterName, jobQueueName));
        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 posting job queue: " + entity, e);
    }
    return null;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) HelixException(org.apache.helix.HelixException) Form(org.restlet.data.Form) TaskDriver(org.apache.helix.task.TaskDriver) Workflow(org.apache.helix.task.Workflow) Map(java.util.Map) HelixException(org.apache.helix.HelixException)

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