Search in sources :

Example 26 with TaskDriver

use of org.apache.helix.task.TaskDriver in project helix by apache.

the class JobAccessor method addJob.

@PUT
@Path("{jobName}")
public Response addJob(@PathParam("clusterId") String clusterId, @PathParam("workflowName") String workflowName, @PathParam("jobName") String jobName, String content) {
    ZNRecord record;
    TaskDriver driver = getTaskDriver(clusterId);
    try {
        record = toZNRecord(content);
        JobConfig.Builder jobConfig = JobAccessor.getJobConfig(record);
        driver.enqueueJob(workflowName, jobName, jobConfig);
    } catch (HelixException e) {
        return badRequest(String.format("Failed to enqueue job %s for reason : %s", jobName, e.getMessage()));
    } catch (IOException e) {
        return badRequest(String.format("Invalid input for Job Config of Job : %s", jobName));
    }
    return OK();
}
Also used : HelixException(org.apache.helix.HelixException) TaskDriver(org.apache.helix.task.TaskDriver) IOException(java.io.IOException) ZNRecord(org.apache.helix.ZNRecord) JobConfig(org.apache.helix.task.JobConfig) Path(javax.ws.rs.Path) PUT(javax.ws.rs.PUT)

Example 27 with TaskDriver

use of org.apache.helix.task.TaskDriver in project helix by apache.

the class JobAccessor method getJob.

@GET
@Path("{jobName}")
public Response getJob(@PathParam("clusterId") String clusterId, @PathParam("workflowName") String workflowName, @PathParam("jobName") String jobName) {
    TaskDriver driver = getTaskDriver(clusterId);
    Map<String, ZNRecord> jobMap = new HashMap<>();
    JobConfig jobConfig = driver.getJobConfig(jobName);
    if (jobConfig != null) {
        jobMap.put(JobProperties.JobConfig.name(), jobConfig.getRecord());
    } else {
        return badRequest(String.format("Job config for %s does not exists", jobName));
    }
    JobContext jobContext = driver.getJobContext(jobName);
    jobMap.put(JobProperties.JobContext.name(), null);
    if (jobContext != null) {
        jobMap.put(JobProperties.JobContext.name(), jobContext.getRecord());
    }
    return JSONRepresentation(jobMap);
}
Also used : HashMap(java.util.HashMap) TaskDriver(org.apache.helix.task.TaskDriver) JobContext(org.apache.helix.task.JobContext) ZNRecord(org.apache.helix.ZNRecord) JobConfig(org.apache.helix.task.JobConfig) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 28 with TaskDriver

use of org.apache.helix.task.TaskDriver in project helix by apache.

the class JobAccessor method getJobs.

@GET
public Response getJobs(@PathParam("clusterId") String clusterId, @PathParam("workflowName") String workflowName) {
    TaskDriver driver = getTaskDriver(clusterId);
    WorkflowConfig workflowConfig = driver.getWorkflowConfig(workflowName);
    ObjectNode root = JsonNodeFactory.instance.objectNode();
    if (workflowConfig == null) {
        return badRequest(String.format("Workflow %s is not found!", workflowName));
    }
    Set<String> jobs = workflowConfig.getJobDag().getAllNodes();
    root.put(Properties.id.name(), JobProperties.Jobs.name());
    ArrayNode jobsNode = root.putArray(JobProperties.Jobs.name());
    if (jobs != null) {
        jobsNode.addAll((ArrayNode) OBJECT_MAPPER.valueToTree(jobs));
    }
    return JSONRepresentation(root);
}
Also used : WorkflowConfig(org.apache.helix.task.WorkflowConfig) ObjectNode(org.codehaus.jackson.node.ObjectNode) TaskDriver(org.apache.helix.task.TaskDriver) ArrayNode(org.codehaus.jackson.node.ArrayNode) GET(javax.ws.rs.GET)

Example 29 with TaskDriver

use of org.apache.helix.task.TaskDriver 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;
}
Also used : ZkClient(org.apache.helix.manager.zk.ZkClient) HelixDataAccessor(org.apache.helix.HelixDataAccessor) StringRepresentation(org.restlet.representation.StringRepresentation) TaskDriver(org.apache.helix.task.TaskDriver) WorkflowContext(org.apache.helix.task.WorkflowContext) ResourceConfig(org.apache.helix.model.ResourceConfig) PropertyKey(org.apache.helix.PropertyKey) ZNRecord(org.apache.helix.ZNRecord)

Example 30 with TaskDriver

use of org.apache.helix.task.TaskDriver in project helix by apache.

the class WorkflowsResource method post.

@Override
public Representation post(Representation entity) {
    try {
        String clusterName = (String) getRequest().getAttributes().get("clusterName");
        Form form = new Form(entity);
        // Get the workflow and submit it
        if (form.size() < 1) {
            throw new HelixException("yaml workflow is required!");
        }
        Parameter payload = form.get(0);
        String yamlPayload = payload.getName();
        if (yamlPayload == null) {
            throw new HelixException("yaml workflow is required!");
        }
        String zkAddr = (String) getContext().getAttributes().get(RestAdminApplication.ZKSERVERADDRESS);
        HelixManager manager = HelixManagerFactory.getZKHelixManager(clusterName, null, InstanceType.ADMINISTRATOR, zkAddr);
        manager.connect();
        try {
            Workflow workflow = Workflow.parse(yamlPayload);
            TaskDriver driver = new TaskDriver(manager);
            driver.start(workflow);
        } finally {
            manager.disconnect();
        }
        getResponse().setEntity(getHostedEntitiesRepresentation(clusterName));
        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 " + entity, e);
    }
    return null;
}
Also used : HelixException(org.apache.helix.HelixException) HelixManager(org.apache.helix.HelixManager) Form(org.restlet.data.Form) TaskDriver(org.apache.helix.task.TaskDriver) Parameter(org.restlet.data.Parameter) Workflow(org.apache.helix.task.Workflow) HelixException(org.apache.helix.HelixException) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException)

Aggregations

TaskDriver (org.apache.helix.task.TaskDriver)31 WorkflowConfig (org.apache.helix.task.WorkflowConfig)11 Path (javax.ws.rs.Path)9 GET (javax.ws.rs.GET)8 IOException (java.io.IOException)6 HashMap (java.util.HashMap)6 HelixException (org.apache.helix.HelixException)6 ZkClient (org.apache.helix.manager.zk.ZkClient)6 JobConfig (org.apache.helix.task.JobConfig)6 Workflow (org.apache.helix.task.Workflow)6 Test (org.testng.annotations.Test)6 ZNRecord (org.apache.helix.ZNRecord)5 JobQueue (org.apache.helix.task.JobQueue)4 WorkflowContext (org.apache.helix.task.WorkflowContext)4 ObjectNode (org.codehaus.jackson.node.ObjectNode)4 ArrayList (java.util.ArrayList)3 Map (java.util.Map)3 Entity (javax.ws.rs.client.Entity)3 HelixManager (org.apache.helix.HelixManager)3 ClusterControllerManager (org.apache.helix.integration.manager.ClusterControllerManager)3