use of io.cdap.cdap.proto.BatchProgram 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);
}
use of io.cdap.cdap.proto.BatchProgram in project cdap by caskdata.
the class AppFabricTestBase method getScheduledRunTimes.
/**
* Returns a list of {@link BatchProgramSchedule}.
*
* @param namespace the namespace to query in
* @param programIds list of programs to query for scheuled run time
* @param next if true, fetch the list of future run times. If false, fetch the list of past run times.
* @return a list of {@link BatchProgramSchedule}
*/
protected List<BatchProgramSchedule> getScheduledRunTimes(String namespace, Collection<? extends ProgramId> programIds, boolean next) throws Exception {
Assert.assertTrue(programIds.stream().map(ProgramId::getNamespace).allMatch(namespace::equals));
String url = String.format("%sruntime", next ? "next" : "previous");
String versionedUrl = getVersionedAPIPath(url, Constants.Gateway.API_VERSION_3_TOKEN, namespace);
List<BatchProgram> batchPrograms = programIds.stream().map(id -> new BatchProgram(id.getApplication(), id.getType(), id.getProgram())).collect(Collectors.toList());
HttpResponse response = doPost(versionedUrl, GSON.toJson(batchPrograms));
assertResponseCode(200, response);
return readResponse(response, new TypeToken<List<BatchProgramSchedule>>() {
}.getType());
}
use of io.cdap.cdap.proto.BatchProgram in project cdap by caskdata.
the class AppFabricTestBase method getProgramRuns.
protected List<BatchProgramHistory> getProgramRuns(NamespaceId namespace, List<ProgramId> programs) throws Exception {
List<BatchProgram> request = programs.stream().map(program -> new BatchProgram(program.getApplication(), program.getType(), program.getProgram())).collect(Collectors.toList());
HttpResponse response = doPost(getVersionedAPIPath("runs", namespace.getNamespace()), GSON.toJson(request));
assertResponseCode(200, response);
return GSON.fromJson(response.getResponseBodyAsString(), BATCH_PROGRAM_RUNS_TYPE);
}
use of io.cdap.cdap.proto.BatchProgram 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);
}
}
}
use of io.cdap.cdap.proto.BatchProgram 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"}]
* </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"}]
* </code></pre>
*/
@POST
@Path("/status")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void getStatuses(FullHttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId) throws Exception {
List<BatchProgram> batchPrograms = validateAndGetBatchInput(request, BATCH_PROGRAMS_TYPE);
List<ProgramId> programs = batchPrograms.stream().map(p -> new ProgramId(namespaceId, p.getAppId(), p.getProgramType(), p.getProgramId())).collect(Collectors.toList());
Map<ProgramId, ProgramStatus> statuses = lifecycleService.getProgramStatuses(programs);
List<BatchProgramStatus> result = new ArrayList<>(programs.size());
for (BatchProgram program : batchPrograms) {
ProgramId programId = new ProgramId(namespaceId, program.getAppId(), program.getProgramType(), program.getProgramId());
ProgramStatus status = statuses.get(programId);
if (status == null) {
result.add(new BatchProgramStatus(program, HttpResponseStatus.NOT_FOUND.code(), new NotFoundException(programId).getMessage(), null));
} else {
result.add(new BatchProgramStatus(program, HttpResponseStatus.OK.code(), null, status.name()));
}
}
responder.sendJson(HttpResponseStatus.OK, GSON.toJson(result));
}
Aggregations