Search in sources :

Example 1 with BatchProgramResult

use of io.cdap.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(io.cdap.cdap.cli.util.table.Table) RowMaker(io.cdap.cdap.cli.util.RowMaker) BatchProgramResult(io.cdap.cdap.proto.BatchProgramResult)

Example 2 with BatchProgramResult

use of io.cdap.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(io.cdap.cdap.cli.util.table.Table) RowMaker(io.cdap.cdap.cli.util.RowMaker) BatchProgramResult(io.cdap.cdap.proto.BatchProgramResult)

Example 3 with BatchProgramResult

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

the class ProgramClient method start.

/**
 * Starts a batch of programs in the same call.
 *
 * @param namespace the namespace of the programs
 * @param programs the programs to start, including any runtime arguments to start them with
 * @return the result of starting 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> start(NamespaceId namespace, List<BatchProgramStart> programs) throws IOException, UnauthenticatedException, UnauthorizedException {
    URL url = config.resolveNamespacedURLV3(namespace, "start");
    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(io.cdap.common.http.HttpRequest) BatchProgramResult(io.cdap.cdap.proto.BatchProgramResult) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL)

Example 4 with BatchProgramResult

use of io.cdap.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(io.cdap.common.http.HttpRequest) BatchProgramResult(io.cdap.cdap.proto.BatchProgramResult) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL)

Example 5 with BatchProgramResult

use of io.cdap.cdap.proto.BatchProgramResult 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)

Aggregations

BatchProgramResult (io.cdap.cdap.proto.BatchProgramResult)6 RowMaker (io.cdap.cdap.cli.util.RowMaker)2 Table (io.cdap.cdap.cli.util.table.Table)2 BadRequestException (io.cdap.cdap.common.BadRequestException)2 ConflictException (io.cdap.cdap.common.ConflictException)2 NamespaceNotFoundException (io.cdap.cdap.common.NamespaceNotFoundException)2 NotFoundException (io.cdap.cdap.common.NotFoundException)2 AuditPolicy (io.cdap.cdap.common.security.AuditPolicy)2 BatchProgramStart (io.cdap.cdap.proto.BatchProgramStart)2 HttpRequest (io.cdap.common.http.HttpRequest)2 HttpResponse (io.cdap.common.http.HttpResponse)2 URL (java.net.URL)2 Charsets (com.google.common.base.Charsets)1 Function (com.google.common.base.Function)1 Joiner (com.google.common.base.Joiner)1 Objects (com.google.common.base.Objects)1 Throwables (com.google.common.base.Throwables)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 Futures (com.google.common.util.concurrent.Futures)1