Search in sources :

Example 1 with BatchProgramStart

use of io.cdap.cdap.proto.BatchProgramStart in project cdap by caskdata.

the class RestartProgramsCommand method runBatchCommand.

@Override
protected void runBatchCommand(PrintStream printStream, Args<BatchProgram> args) throws Exception {
    NamespaceId namespace = args.appId.getParent();
    printStream.print("Stopping programs...\n");
    programClient.stop(namespace, args.programs);
    printStream.print("Starting programs...\n");
    List<BatchProgramStart> startList = new ArrayList<>(args.programs.size());
    for (BatchProgram program : args.programs) {
        startList.add(new BatchProgramStart(program));
    }
    programClient.start(namespace, startList);
}
Also used : ArrayList(java.util.ArrayList) BatchProgramStart(io.cdap.cdap.proto.BatchProgramStart) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) BatchProgram(io.cdap.cdap.proto.BatchProgram)

Example 2 with BatchProgramStart

use of io.cdap.cdap.proto.BatchProgramStart in project cdap by caskdata.

the class ProgramClientTestRun method testBatchProgramCalls.

@Test
public void testBatchProgramCalls() throws Exception {
    final NamespaceId namespace = NamespaceId.DEFAULT;
    final ApplicationId appId = namespace.app(FakeApp.NAME);
    BatchProgram pingService = new BatchProgram(FakeApp.NAME, ProgramType.SERVICE, PingService.NAME);
    BatchProgram writerService = new BatchProgram(FakeApp.NAME, ProgramType.SERVICE, DatasetWriterService.NAME);
    BatchProgram missing = new BatchProgram(FakeApp.NAME, ProgramType.SERVICE, "not" + PingService.NAME);
    appClient.deploy(namespace, createAppJarFile(FakeApp.class));
    try {
        // make a batch call to start multiple programs, one of which does not exist
        List<BatchProgramStart> programStarts = ImmutableList.of(new BatchProgramStart(pingService), new BatchProgramStart(writerService), new BatchProgramStart(missing));
        List<BatchProgramResult> results = programClient.start(namespace, programStarts);
        // check that we got a 200 for programs that exist, and a 404 for the one that doesn't
        for (BatchProgramResult result : results) {
            if (missing.getProgramId().equals(result.getProgramId())) {
                Assert.assertEquals(404, result.getStatusCode());
            } else {
                Assert.assertEquals(200, result.getStatusCode());
            }
        }
        // wait for all programs to be in RUNNING status
        assertProgramRuns(programClient, namespace.app(pingService.getAppId()).service(pingService.getProgramId()), ProgramRunStatus.RUNNING, 1, 10);
        assertProgramRuns(programClient, namespace.app(writerService.getAppId()).service(writerService.getProgramId()), ProgramRunStatus.RUNNING, 1, 10);
        // make a batch call for status of programs, one of which does not exist
        List<BatchProgram> programs = ImmutableList.of(pingService, writerService, missing);
        List<BatchProgramStatus> statusList = programClient.getStatus(namespace, programs);
        // check status is running for programs that exist, and that we get a 404 for the one that doesn't
        for (BatchProgramStatus status : statusList) {
            if (missing.getProgramId().equals(status.getProgramId())) {
                Assert.assertEquals(404, status.getStatusCode());
            } else {
                Assert.assertEquals(200, status.getStatusCode());
                Assert.assertEquals("RUNNING", status.getStatus());
            }
        }
        // make a batch call to stop programs, one of which does not exist
        results = programClient.stop(namespace, programs);
        // check that we got a 200 for programs that exist, and a 404 for the one that doesn't
        for (BatchProgramResult result : results) {
            if (missing.getProgramId().equals(result.getProgramId())) {
                Assert.assertEquals(404, result.getStatusCode());
            } else {
                Assert.assertEquals(200, result.getStatusCode());
            }
        }
        // check programs are in stopped state
        final List<BatchProgram> stoppedPrograms = ImmutableList.of(pingService, writerService);
        Tasks.waitFor(true, new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                List<BatchProgramStatus> statusList = programClient.getStatus(namespace, stoppedPrograms);
                for (BatchProgramStatus status : statusList) {
                    if (status.getStatusCode() != 200) {
                        return false;
                    }
                    if (status.getStatus().equals("RUNNING")) {
                        return false;
                    }
                }
                return true;
            }
        }, 10, TimeUnit.SECONDS);
    } finally {
        try {
            appClient.delete(appId);
        } catch (Exception e) {
            LOG.error("Error deleting app {} during test cleanup.", appId, e);
        }
    }
}
Also used : FakeApp(io.cdap.cdap.client.app.FakeApp) BatchProgramResult(io.cdap.cdap.proto.BatchProgramResult) BatchProgramStatus(io.cdap.cdap.proto.BatchProgramStatus) BatchProgramStart(io.cdap.cdap.proto.BatchProgramStart) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) BatchProgram(io.cdap.cdap.proto.BatchProgram) Test(org.junit.Test)

Example 3 with BatchProgramStart

use of io.cdap.cdap.proto.BatchProgramStart 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"}}]
 * </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": "App2", "programType": "Mapreduce", "programId": "Mapreduce2",
 *  "statusCode":404, "error": "App: App2 not found"}]
 * </code></pre>
 */
@POST
@Path("/start")
@AuditPolicy({ AuditDetail.REQUEST_BODY, AuditDetail.RESPONSE_BODY })
public void startPrograms(FullHttpRequest 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 {
            String runId = lifecycleService.run(programId, program.getRuntimeargs(), false).getId();
            output.add(new BatchProgramResult(program, HttpResponseStatus.OK.code(), null, runId));
        } catch (NotFoundException e) {
            output.add(new BatchProgramResult(program, HttpResponseStatus.NOT_FOUND.code(), e.getMessage()));
        } catch (BadRequestException e) {
            output.add(new BatchProgramResult(program, HttpResponseStatus.BAD_REQUEST.code(), e.getMessage()));
        } catch (ConflictException e) {
            output.add(new BatchProgramResult(program, HttpResponseStatus.CONFLICT.code(), e.getMessage()));
        }
    }
    responder.sendJson(HttpResponseStatus.OK, GSON.toJson(output));
}
Also used : ConflictException(io.cdap.cdap.common.ConflictException) BatchProgramResult(io.cdap.cdap.proto.BatchProgramResult) ArrayList(java.util.ArrayList) NamespaceNotFoundException(io.cdap.cdap.common.NamespaceNotFoundException) NotFoundException(io.cdap.cdap.common.NotFoundException) BadRequestException(io.cdap.cdap.common.BadRequestException) BatchProgramStart(io.cdap.cdap.proto.BatchProgramStart) ProgramId(io.cdap.cdap.proto.id.ProgramId) Path(javax.ws.rs.Path) AuditPolicy(io.cdap.cdap.common.security.AuditPolicy) POST(javax.ws.rs.POST)

Aggregations

BatchProgramStart (io.cdap.cdap.proto.BatchProgramStart)3 BatchProgram (io.cdap.cdap.proto.BatchProgram)2 BatchProgramResult (io.cdap.cdap.proto.BatchProgramResult)2 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)2 ArrayList (java.util.ArrayList)2 ImmutableList (com.google.common.collect.ImmutableList)1 FakeApp (io.cdap.cdap.client.app.FakeApp)1 BadRequestException (io.cdap.cdap.common.BadRequestException)1 ConflictException (io.cdap.cdap.common.ConflictException)1 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)1 NotFoundException (io.cdap.cdap.common.NotFoundException)1 AuditPolicy (io.cdap.cdap.common.security.AuditPolicy)1 BatchProgramStatus (io.cdap.cdap.proto.BatchProgramStatus)1 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)1 ProgramId (io.cdap.cdap.proto.id.ProgramId)1 List (java.util.List)1 POST (javax.ws.rs.POST)1 Path (javax.ws.rs.Path)1 Test (org.junit.Test)1