Search in sources :

Example 46 with ProgramId

use of co.cask.cdap.proto.id.ProgramId in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method setWorkerInstances.

/**
   * Sets the number of instances of a worker.
   */
@PUT
@Path("/apps/{app-id}/workers/{worker-id}/instances")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void setWorkerInstances(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("worker-id") String workerId) throws Exception {
    int instances = getInstances(request);
    try {
        lifecycleService.setInstances(new ProgramId(namespaceId, appId, ProgramType.WORKER, workerId), instances);
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (SecurityException e) {
        responder.sendStatus(HttpResponseStatus.UNAUTHORIZED);
    } catch (Throwable e) {
        if (respondIfElementNotFound(e, responder)) {
            return;
        }
        throw e;
    }
}
Also used : ProgramId(co.cask.cdap.proto.id.ProgramId) Constraint(co.cask.cdap.internal.schedule.constraint.Constraint) ProtoConstraint(co.cask.cdap.proto.ProtoConstraint) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Example 47 with ProgramId

use of co.cask.cdap.proto.id.ProgramId in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method programRunRecord.

/**
   * Returns run record for a particular run of a program of an app version.
   */
@GET
@Path("/apps/{app-name}/versions/{app-version}/{program-type}/{program-name}/runs/{run-id}")
public void programRunRecord(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-name") String appName, @PathParam("app-version") String appVersion, @PathParam("program-type") String type, @PathParam("program-name") String programName, @PathParam("run-id") String runid) throws NotFoundException {
    ProgramType programType = getProgramType(type);
    if (programType == null || programType == ProgramType.WEBAPP) {
        throw new NotFoundException(String.format("Program run record is not supported for program type '%s'.", programType));
    }
    ProgramId progId = new ApplicationId(namespaceId, appName, appVersion).program(programType, programName);
    RunRecordMeta runRecordMeta = store.getRun(progId, runid);
    if (runRecordMeta != null) {
        RunRecord runRecord = CONVERT_TO_RUN_RECORD.apply(runRecordMeta);
        responder.sendJson(HttpResponseStatus.OK, runRecord);
        return;
    }
    throw new NotFoundException(progId.run(runid));
}
Also used : RunRecord(co.cask.cdap.proto.RunRecord) RunRecordMeta(co.cask.cdap.internal.app.store.RunRecordMeta) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) ProgramType(co.cask.cdap.proto.ProgramType) ProgramId(co.cask.cdap.proto.id.ProgramId) ApplicationId(co.cask.cdap.proto.id.ApplicationId) Path(javax.ws.rs.Path) GET(javax.ws.rs.GET)

Example 48 with ProgramId

use of co.cask.cdap.proto.id.ProgramId in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method doPerformAction.

private void doPerformAction(HttpRequest request, HttpResponder responder, String namespaceId, String appId, String appVersion, String type, String programId, String action) throws Exception {
    ApplicationId applicationId = new ApplicationId(namespaceId, appId, appVersion);
    if (SCHEDULES.equals(type)) {
        ScheduleId scheduleId = applicationId.schedule(programId);
        lifecycleService.suspendResumeSchedule(scheduleId, action);
        responder.sendJson(HttpResponseStatus.OK, "OK");
        return;
    }
    ProgramType programType;
    try {
        programType = ProgramType.valueOfCategoryName(type);
    } catch (IllegalArgumentException e) {
        throw new BadRequestException(String.format("Unknown program type '%s'", type), e);
    }
    ProgramId program = applicationId.program(programType, programId);
    Map<String, String> args = decodeArguments(request);
    // we have already validated that the action is valid
    switch(action.toLowerCase()) {
        case "start":
            lifecycleService.start(program, args, false);
            break;
        case "debug":
            if (!isDebugAllowed(programType)) {
                throw new NotImplementedException(String.format("debug action is not implemented for program type %s", programType));
            }
            lifecycleService.start(program, args, true);
            break;
        case "stop":
            lifecycleService.stop(program);
            break;
        default:
            throw new NotFoundException(String.format("%s action was not found", action));
    }
    responder.sendStatus(HttpResponseStatus.OK);
}
Also used : NotImplementedException(co.cask.cdap.common.NotImplementedException) BadRequestException(co.cask.cdap.common.BadRequestException) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) ProgramType(co.cask.cdap.proto.ProgramType) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ScheduleId(co.cask.cdap.proto.id.ScheduleId) ProgramId(co.cask.cdap.proto.id.ProgramId)

Example 49 with ProgramId

use of co.cask.cdap.proto.id.ProgramId in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method getStatuses.

/**
   * Returns the status for all programs that are passed into the data. The data is an array of JSON objects
   * where each object must contain the following three elements: appId, programType, and programId
   * (flow name, service name, etc.).
   * <p>
   * Example input:
   * <pre><code>
   * [{"appId": "App1", "programType": "Service", "programId": "Service1"},
   * {"appId": "App1", "programType": "Mapreduce", "programId": "MapReduce2"},
   * {"appId": "App2", "programType": "Flow", "programId": "Flow1"}]
   * </code></pre>
   * </p><p>
   * The response will be an array of JsonObjects each of which will contain the three input parameters
   * as well as 2 fields, "status" which maps to the status of the program and "statusCode" which maps to the
   * status code for the data in that JsonObjects.
   * </p><p>
   * If an error occurs in the input (for the example above, App2 does not exist), then all JsonObjects for which the
   * parameters have a valid status will have the status field but all JsonObjects for which the parameters do not have
   * a valid status will have an error message and statusCode.
   * </p><p>
   * For example, if there is no App2 in the data above, then the response would be 200 OK with following possible data:
   * </p>
   * <pre><code>
   * [{"appId": "App1", "programType": "Service", "programId": "Service1", "statusCode": 200, "status": "RUNNING"},
   * {"appId": "App1", "programType": "Mapreduce", "programId": "Mapreduce2", "statusCode": 200, "status": "STOPPED"},
   * {"appId":"App2", "programType":"Flow", "programId":"Flow1", "statusCode":404, "error": "App: App2 not found"}]
   * </code></pre>
   */
@POST
@Path("/status")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void getStatuses(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
    List<BatchProgram> programs = validateAndGetBatchInput(request, BATCH_PROGRAMS_TYPE);
    List<BatchProgramStatus> statuses = new ArrayList<>(programs.size());
    for (BatchProgram program : programs) {
        ProgramId programId = new ProgramId(namespaceId, program.getAppId(), program.getProgramType(), program.getProgramId());
        try {
            ProgramStatus programStatus = lifecycleService.getProgramStatus(programId);
            statuses.add(new BatchProgramStatus(program, HttpResponseStatus.OK.getCode(), null, programStatus.name()));
        } catch (NotFoundException e) {
            statuses.add(new BatchProgramStatus(program, HttpResponseStatus.NOT_FOUND.getCode(), e.getMessage(), null));
        }
    }
    responder.sendJson(HttpResponseStatus.OK, statuses);
}
Also used : BatchProgramStatus(co.cask.cdap.proto.BatchProgramStatus) ArrayList(java.util.ArrayList) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) ProgramId(co.cask.cdap.proto.id.ProgramId) BatchProgram(co.cask.cdap.proto.BatchProgram) ProgramStatus(co.cask.cdap.proto.ProgramStatus) BatchProgramStatus(co.cask.cdap.proto.BatchProgramStatus) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST)

Example 50 with ProgramId

use of co.cask.cdap.proto.id.ProgramId in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method setServiceInstances.

/**
   * Set instances of a service.
   */
@PUT
@Path("/apps/{app-id}/services/{service-id}/instances")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void setServiceInstances(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-id") String appId, @PathParam("service-id") String serviceId) throws Exception {
    try {
        ProgramId programId = new ProgramId(namespaceId, appId, ProgramType.SERVICE, serviceId);
        if (!store.programExists(programId)) {
            responder.sendString(HttpResponseStatus.NOT_FOUND, "Service not found");
            return;
        }
        int instances = getInstances(request);
        lifecycleService.setInstances(programId, instances);
        responder.sendStatus(HttpResponseStatus.OK);
    } catch (SecurityException e) {
        responder.sendStatus(HttpResponseStatus.UNAUTHORIZED);
    } catch (Throwable throwable) {
        if (respondIfElementNotFound(throwable, responder)) {
            return;
        }
        throw throwable;
    }
}
Also used : ProgramId(co.cask.cdap.proto.id.ProgramId) Constraint(co.cask.cdap.internal.schedule.constraint.Constraint) ProtoConstraint(co.cask.cdap.proto.ProtoConstraint) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Aggregations

ProgramId (co.cask.cdap.proto.id.ProgramId)209 Test (org.junit.Test)89 ApplicationId (co.cask.cdap.proto.id.ApplicationId)69 Path (javax.ws.rs.Path)45 StreamId (co.cask.cdap.proto.id.StreamId)35 DatasetId (co.cask.cdap.proto.id.DatasetId)34 RunId (org.apache.twill.api.RunId)34 ProgramRunId (co.cask.cdap.proto.id.ProgramRunId)31 NamespaceId (co.cask.cdap.proto.id.NamespaceId)29 ProgramType (co.cask.cdap.proto.ProgramType)25 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)24 IOException (java.io.IOException)24 NotFoundException (co.cask.cdap.common.NotFoundException)22 HttpResponse (org.apache.http.HttpResponse)19 ArrayList (java.util.ArrayList)18 GET (javax.ws.rs.GET)18 Id (co.cask.cdap.proto.Id)16 File (java.io.File)15 POST (javax.ws.rs.POST)15 ArtifactId (co.cask.cdap.proto.id.ArtifactId)13