Search in sources :

Example 1 with BatchProgramResult

use of co.cask.cdap.proto.BatchProgramResult in project cdap by caskdata.

the class StartProgramsCommand method runBatchCommand.

@Override
protected void runBatchCommand(PrintStream printStream, Args<BatchProgramStart> args) throws Exception {
    List<BatchProgramResult> results = programClient.start(args.appId.getParent(), args.programs);
    Table table = Table.builder().setHeader("name", "type", "error").setRows(results, new RowMaker<BatchProgramResult>() {

        @Override
        public List<?> makeRow(BatchProgramResult result) {
            return Lists.newArrayList(result.getProgramId(), result.getProgramType(), result.getError());
        }
    }).build();
    cliConfig.getTableRenderer().render(cliConfig, printStream, table);
}
Also used : Table(co.cask.cdap.cli.util.table.Table) RowMaker(co.cask.cdap.cli.util.RowMaker) BatchProgramResult(co.cask.cdap.proto.BatchProgramResult)

Example 2 with BatchProgramResult

use of co.cask.cdap.proto.BatchProgramResult in project cdap by caskdata.

the class StopProgramsCommand method runBatchCommand.

@Override
protected void runBatchCommand(PrintStream printStream, Args<BatchProgram> args) throws Exception {
    List<BatchProgramResult> results = programClient.stop(args.appId.getParent(), args.programs);
    Table table = Table.builder().setHeader("name", "type", "error").setRows(results, new RowMaker<BatchProgramResult>() {

        @Override
        public List<?> makeRow(BatchProgramResult result) {
            return Lists.newArrayList(result.getProgramId(), result.getProgramType(), result.getError());
        }
    }).build();
    cliConfig.getTableRenderer().render(cliConfig, printStream, table);
}
Also used : Table(co.cask.cdap.cli.util.table.Table) RowMaker(co.cask.cdap.cli.util.RowMaker) BatchProgramResult(co.cask.cdap.proto.BatchProgramResult)

Example 3 with BatchProgramResult

use of co.cask.cdap.proto.BatchProgramResult in project cdap by caskdata.

the class ProgramClient method stop.

/**
   * Stops a batch of programs in the same call.
   *
   * @param namespace the namespace of the programs
   * @param programs the programs to stop
   * @return the result of stopping each program
   * @throws IOException if a network error occurred
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   */
public List<BatchProgramResult> stop(NamespaceId namespace, List<BatchProgram> programs) throws IOException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(namespace, "stop");
    HttpRequest request = HttpRequest.builder(HttpMethod.POST, url).withBody(GSON.toJson(programs), Charsets.UTF_8).build();
    HttpResponse response = restClient.execute(request, config.getAccessToken());
    return ObjectResponse.<List<BatchProgramResult>>fromJsonBody(response, BATCH_RESULTS_TYPE, GSON).getResponseObject();
}
Also used : HttpRequest(co.cask.common.http.HttpRequest) BatchProgramResult(co.cask.cdap.proto.BatchProgramResult) HttpResponse(co.cask.common.http.HttpResponse) URL(java.net.URL)

Example 4 with BatchProgramResult

use of co.cask.cdap.proto.BatchProgramResult in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method startPrograms.

/**
   * Starts 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.). In additional, each object can contain an optional runtimeargs element,
   * which is a map of arguments to start the program with.
   * <p>
   * Example input:
   * <pre><code>
   * [{"appId": "App1", "programType": "Service", "programId": "Service1"},
   * {"appId": "App1", "programType": "Mapreduce", "programId": "MapReduce2", "runtimeargs":{"arg1":"val1"}},
   * {"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 a "statusCode" field 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},
   * {"appId": "App1", "programType": "Mapreduce", "programId": "Mapreduce2", "statusCode": 200},
   * {"appId":"App2", "programType":"Flow", "programId":"Flow1", "statusCode":404, "error": "App: App2 not found"}]
   * </code></pre>
   */
@POST
@Path("/start")
@AuditPolicy({ AuditDetail.REQUEST_BODY, AuditDetail.RESPONSE_BODY })
public void startPrograms(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
    List<BatchProgramStart> programs = validateAndGetBatchInput(request, BATCH_STARTS_TYPE);
    List<BatchProgramResult> output = new ArrayList<>(programs.size());
    for (BatchProgramStart program : programs) {
        ProgramId programId = new ProgramId(namespaceId, program.getAppId(), program.getProgramType(), program.getProgramId());
        try {
            ProgramController programController = lifecycleService.start(programId, program.getRuntimeargs(), false);
            output.add(new BatchProgramResult(program, HttpResponseStatus.OK.getCode(), null, programController.getRunId().getId()));
        } catch (NotFoundException e) {
            output.add(new BatchProgramResult(program, HttpResponseStatus.NOT_FOUND.getCode(), e.getMessage()));
        } catch (BadRequestException e) {
            output.add(new BatchProgramResult(program, HttpResponseStatus.BAD_REQUEST.getCode(), e.getMessage()));
        } catch (ConflictException e) {
            output.add(new BatchProgramResult(program, HttpResponseStatus.CONFLICT.getCode(), e.getMessage()));
        }
    }
    responder.sendJson(HttpResponseStatus.OK, output);
}
Also used : ProgramController(co.cask.cdap.app.runtime.ProgramController) ConflictException(co.cask.cdap.common.ConflictException) ArrayList(java.util.ArrayList) BatchProgramResult(co.cask.cdap.proto.BatchProgramResult) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) BadRequestException(co.cask.cdap.common.BadRequestException) BatchProgramStart(co.cask.cdap.proto.BatchProgramStart) ProgramId(co.cask.cdap.proto.id.ProgramId) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST)

Example 5 with BatchProgramResult

use of co.cask.cdap.proto.BatchProgramResult in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method stopPrograms.

/**
   * Stops 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 a "statusCode" field 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},
   * {"appId": "App1", "programType": "Mapreduce", "programId": "Mapreduce2", "statusCode": 200},
   * {"appId":"App2", "programType":"Flow", "programId":"Flow1", "statusCode":404, "error": "App: App2 not found"}]
   * </code></pre>
   */
@POST
@Path("/stop")
@AuditPolicy({ AuditDetail.REQUEST_BODY, AuditDetail.RESPONSE_BODY })
public void stopPrograms(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
    List<BatchProgram> programs = validateAndGetBatchInput(request, BATCH_PROGRAMS_TYPE);
    List<ListenableFuture<BatchProgramResult>> issuedStops = new ArrayList<>(programs.size());
    for (final BatchProgram program : programs) {
        ProgramId programId = new ProgramId(namespaceId, program.getAppId(), program.getProgramType(), program.getProgramId());
        try {
            List<ListenableFuture<ProgramController>> stops = lifecycleService.issueStop(programId, null);
            for (ListenableFuture<ProgramController> stop : stops) {
                ListenableFuture<BatchProgramResult> issuedStop = Futures.transform(stop, new Function<ProgramController, BatchProgramResult>() {

                    @Override
                    public BatchProgramResult apply(ProgramController input) {
                        return new BatchProgramResult(program, HttpResponseStatus.OK.getCode(), null, input.getRunId().getId());
                    }
                });
                issuedStops.add(issuedStop);
            }
        } catch (NotFoundException e) {
            issuedStops.add(Futures.immediateFuture(new BatchProgramResult(program, HttpResponseStatus.NOT_FOUND.getCode(), e.getMessage())));
        } catch (BadRequestException e) {
            issuedStops.add(Futures.immediateFuture(new BatchProgramResult(program, HttpResponseStatus.BAD_REQUEST.getCode(), e.getMessage())));
        }
    }
    List<BatchProgramResult> output = new ArrayList<>(programs.size());
    // need to keep this index in case there is an exception getting the future, since we won't have the program
    // information in that scenario
    int i = 0;
    for (ListenableFuture<BatchProgramResult> issuedStop : issuedStops) {
        try {
            output.add(issuedStop.get());
        } catch (Throwable t) {
            LOG.warn(t.getMessage(), t);
            output.add(new BatchProgramResult(programs.get(i), HttpResponseStatus.INTERNAL_SERVER_ERROR.getCode(), t.getMessage()));
        }
        i++;
    }
    responder.sendJson(HttpResponseStatus.OK, output);
}
Also used : ProgramController(co.cask.cdap.app.runtime.ProgramController) ArrayList(java.util.ArrayList) BatchProgramResult(co.cask.cdap.proto.BatchProgramResult) NamespaceNotFoundException(co.cask.cdap.common.NamespaceNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) ProgramId(co.cask.cdap.proto.id.ProgramId) Constraint(co.cask.cdap.internal.schedule.constraint.Constraint) ProtoConstraint(co.cask.cdap.proto.ProtoConstraint) ListenableFuture(com.google.common.util.concurrent.ListenableFuture) BadRequestException(co.cask.cdap.common.BadRequestException) BatchProgram(co.cask.cdap.proto.BatchProgram) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST)

Aggregations

BatchProgramResult (co.cask.cdap.proto.BatchProgramResult)7 NotFoundException (co.cask.cdap.common.NotFoundException)3 ProgramController (co.cask.cdap.app.runtime.ProgramController)2 RowMaker (co.cask.cdap.cli.util.RowMaker)2 Table (co.cask.cdap.cli.util.table.Table)2 BadRequestException (co.cask.cdap.common.BadRequestException)2 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)2 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)2 BatchProgram (co.cask.cdap.proto.BatchProgram)2 BatchProgramStart (co.cask.cdap.proto.BatchProgramStart)2 ProgramId (co.cask.cdap.proto.id.ProgramId)2 HttpRequest (co.cask.common.http.HttpRequest)2 HttpResponse (co.cask.common.http.HttpResponse)2 URL (java.net.URL)2 ArrayList (java.util.ArrayList)2 POST (javax.ws.rs.POST)2 Path (javax.ws.rs.Path)2 FakeApp (co.cask.cdap.client.app.FakeApp)1 ConflictException (co.cask.cdap.common.ConflictException)1 Constraint (co.cask.cdap.internal.schedule.constraint.Constraint)1