use of io.cdap.cdap.proto.id.ProgramId in project cdap by caskdata.
the class SystemProgramManagementServiceTest method deployTestApp.
private void deployTestApp() throws Exception {
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.SYSTEM, APP_NAME, VERSION);
Location appJar = AppJarHelper.createDeploymentJar(locationFactory, APP_CLASS);
File appJarFile = new File(tmpFolder.newFolder(), String.format("%s-%s.jar", artifactId.getName(), artifactId.getVersion().getVersion()));
Locations.linkOrCopyOverwrite(appJar, appJarFile);
appJar.delete();
artifactRepository.addArtifact(artifactId, appJarFile);
ArtifactSummary summary = new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion(), ArtifactScope.SYSTEM);
applicationLifecycleService.deployApp(NamespaceId.SYSTEM, APP_NAME, VERSION, summary, null, programId -> {
// no-op
}, null, false, false, Collections.emptyMap());
}
use of io.cdap.cdap.proto.id.ProgramId in project cdap by caskdata.
the class AppLifecycleHttpHandlerTest method testDelete.
/**
* Tests deleting applications with versioned and non-versioned API.
*/
@Test
public void testDelete() throws Exception {
// Delete an non-existing app
HttpResponse response = doDelete(getVersionedAPIPath("apps/XYZ", Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1));
Assert.assertEquals(404, response.getResponseCode());
// Start a service from the App
deploy(AllProgramsApp.class, 200, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1);
Id.Program program = Id.Program.from(TEST_NAMESPACE1, AllProgramsApp.NAME, ProgramType.SERVICE, AllProgramsApp.NoOpService.NAME);
startProgram(program);
waitState(program, "RUNNING");
// Try to delete an App while its service is running
response = doDelete(getVersionedAPIPath("apps/" + AllProgramsApp.NAME, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1));
Assert.assertEquals(409, response.getResponseCode());
Assert.assertEquals("'" + program.getApplication() + "' could not be deleted. Reason: The following programs are still running: " + program.getId(), response.getResponseBodyAsString());
stopProgram(program);
waitState(program, "STOPPED");
startProgram(program);
waitState(program, "RUNNING");
// Try to delete all Apps while service is running
response = doDelete(getVersionedAPIPath("apps", Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1));
Assert.assertEquals(409, response.getResponseCode());
Assert.assertEquals("'" + program.getNamespace() + "' could not be deleted. Reason: The following programs are still running: " + program.getApplicationId() + ": " + program.getId(), response.getResponseBodyAsString());
stopProgram(program);
waitState(program, "STOPPED");
// Delete the app in the wrong namespace
response = doDelete(getVersionedAPIPath("apps/" + AllProgramsApp.NAME, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE2));
Assert.assertEquals(404, response.getResponseCode());
// Delete an non-existing app with version
response = doDelete(getVersionedAPIPath("apps/XYZ/versions/" + VERSION1, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1));
Assert.assertEquals(404, response.getResponseCode());
// Deploy an app with version
Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, AllProgramsApp.class.getSimpleName(), VERSION1);
addAppArtifact(artifactId, AllProgramsApp.class);
AppRequest<? extends Config> appRequest = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()));
ApplicationId appId = NamespaceId.DEFAULT.app(AllProgramsApp.NAME, VERSION1);
Assert.assertEquals(200, deploy(appId, appRequest).getResponseCode());
// Start a service for the App
ProgramId program1 = appId.program(ProgramType.SERVICE, AllProgramsApp.NoOpService.NAME);
startProgram(program1, 200);
waitState(program1, "RUNNING");
// Try to delete an App while its service is running
response = doDelete(getVersionedAPIPath(String.format("apps/%s/versions/%s", appId.getApplication(), appId.getVersion()), Constants.Gateway.API_VERSION_3_TOKEN, appId.getNamespace()));
Assert.assertEquals(409, response.getResponseCode());
Assert.assertEquals("'" + program1.getParent() + "' could not be deleted. Reason: The following programs" + " are still running: " + program1.getProgram(), response.getResponseBodyAsString());
stopProgram(program1, null, 200, null);
waitState(program1, "STOPPED");
// Delete the app with version in the wrong namespace
response = doDelete(getVersionedAPIPath(String.format("apps/%s/versions/%s", appId.getApplication(), appId.getVersion()), Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE2));
Assert.assertEquals(404, response.getResponseCode());
// Delete the app with version after stopping the service
response = doDelete(getVersionedAPIPath(String.format("apps/%s/versions/%s", appId.getApplication(), appId.getVersion()), Constants.Gateway.API_VERSION_3_TOKEN, appId.getNamespace()));
Assert.assertEquals(200, response.getResponseCode());
response = doDelete(getVersionedAPIPath(String.format("apps/%s/versions/%s", appId.getApplication(), appId.getVersion()), Constants.Gateway.API_VERSION_3_TOKEN, appId.getNamespace()));
Assert.assertEquals(404, response.getResponseCode());
// Delete the App after stopping the service
response = doDelete(getVersionedAPIPath("apps/" + AllProgramsApp.NAME, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1));
Assert.assertEquals(200, response.getResponseCode());
response = doDelete(getVersionedAPIPath("apps/" + AllProgramsApp.NAME, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1));
Assert.assertEquals(404, response.getResponseCode());
// deleting the app should not delete the artifact
response = doGet(getVersionedAPIPath("artifacts/" + artifactId.getName(), Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1));
Assert.assertEquals(200, response.getResponseCode());
List<ArtifactSummary> summaries = readResponse(response, new TypeToken<List<ArtifactSummary>>() {
}.getType());
Assert.assertFalse(summaries.isEmpty());
// cleanup
deleteNamespace(NamespaceId.DEFAULT.getNamespace());
}
use of io.cdap.cdap.proto.id.ProgramId 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.id.ProgramId 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.id.ProgramId in project cdap by caskdata.
the class ProgramNotificationSubscriberServiceTest method testWorkflowInnerPrograms.
@Test
public void testWorkflowInnerPrograms() throws Exception {
AppFabricTestHelper.deployApplication(Id.Namespace.DEFAULT, ProgramStateWorkflowApp.class, null, cConf);
ProgramRunId workflowRunId = NamespaceId.DEFAULT.app(ProgramStateWorkflowApp.class.getSimpleName()).workflow(ProgramStateWorkflowApp.ProgramStateWorkflow.class.getSimpleName()).run(RunIds.generate());
ApplicationSpecification appSpec = TransactionRunners.run(transactionRunner, context -> {
return AppMetadataStore.create(context).getApplication(workflowRunId.getParent().getParent()).getSpec();
});
ProgramDescriptor programDescriptor = new ProgramDescriptor(workflowRunId.getParent(), appSpec);
// Start and run the workflow
Map<String, String> systemArgs = new HashMap<>();
systemArgs.put(ProgramOptionConstants.SKIP_PROVISIONING, Boolean.TRUE.toString());
systemArgs.put(SystemArguments.PROFILE_NAME, ProfileId.NATIVE.getScopedName());
programStateWriter.start(workflowRunId, new SimpleProgramOptions(workflowRunId.getParent(), new BasicArguments(systemArgs), new BasicArguments()), null, programDescriptor);
programStateWriter.running(workflowRunId, null);
ProgramRunId mrRunId = workflowRunId.getParent().getParent().mr(ProgramStateWorkflowApp.ProgramStateMR.class.getSimpleName()).run(RunIds.generate());
ProgramRunId sparkRunId = workflowRunId.getParent().getParent().spark(ProgramStateWorkflowApp.ProgramStateSpark.class.getSimpleName()).run(RunIds.generate());
ProgramId sparkId2 = workflowRunId.getParent().getParent().spark(ProgramStateWorkflowApp.ProgramStateSpark2.class.getSimpleName());
// Start and run the MR and Spark inside
for (ProgramRunId programRunId : Arrays.asList(mrRunId, sparkRunId)) {
workflowStateWriter.addWorkflowNodeState(workflowRunId, new WorkflowNodeStateDetail(programRunId.getProgram(), NodeStatus.STARTING));
workflowStateWriter.addWorkflowNodeState(workflowRunId, new WorkflowNodeStateDetail(programRunId.getProgram(), NodeStatus.RUNNING));
systemArgs = new HashMap<>(systemArgs);
systemArgs.put(ProgramOptionConstants.RUN_ID, programRunId.getRun());
systemArgs.put(ProgramOptionConstants.WORKFLOW_NAME, workflowRunId.getProgram());
systemArgs.put(ProgramOptionConstants.WORKFLOW_RUN_ID, workflowRunId.getRun());
systemArgs.put(ProgramOptionConstants.WORKFLOW_NODE_ID, programRunId.getProgram());
systemArgs.put(ProgramOptionConstants.PROGRAM_NAME_IN_WORKFLOW, programRunId.getProgram());
programStateWriter.start(programRunId, new SimpleProgramOptions(programRunId.getParent(), new BasicArguments(systemArgs), new BasicArguments()), null, programDescriptor);
programStateWriter.running(programRunId, null);
// Wait for the inner program running
Tasks.waitFor(ProgramRunStatus.RUNNING, () -> TransactionRunners.run(transactionRunner, context -> {
AppMetadataStore metadataStoreDataset = AppMetadataStore.create(context);
RunRecordDetail meta = metadataStoreDataset.getRun(programRunId);
if (meta == null) {
return null;
}
return meta.getStatus();
}), 10, TimeUnit.SECONDS);
}
// Stop the Spark normally
programStateWriter.completed(sparkRunId);
// Error out the Workflow without stopping the MR
programStateWriter.error(workflowRunId, new IllegalStateException("Explicitly error out"));
// Wait for the Workflow state changed to failed
Tasks.waitFor(ProgramRunStatus.FAILED, () -> TransactionRunners.run(transactionRunner, context -> {
AppMetadataStore metadataStoreDataset = AppMetadataStore.create(context);
RunRecordDetail meta = metadataStoreDataset.getRun(workflowRunId);
if (meta == null) {
return null;
}
return meta.getStatus();
}), 10000, TimeUnit.SECONDS);
// The MR run record should be changed to ERROR state as well (without race)
TransactionRunners.run(transactionRunner, context -> {
AppMetadataStore metadataStoreDataset = AppMetadataStore.create(context);
RunRecordDetail meta = metadataStoreDataset.getRun(mrRunId);
Assert.assertNotNull(meta);
Assert.assertEquals(ProgramRunStatus.FAILED, meta.getStatus());
});
// The Spark run record should stay as COMPLETED
TransactionRunners.run(transactionRunner, context -> {
AppMetadataStore metadataStoreDataset = AppMetadataStore.create(context);
RunRecordDetail meta = metadataStoreDataset.getRun(sparkRunId);
Assert.assertNotNull(meta);
Assert.assertEquals(ProgramRunStatus.COMPLETED, meta.getStatus());
});
// Since the Spark2 program hasn't been executed, there should be no run record
TransactionRunners.run(transactionRunner, context -> {
AppMetadataStore metadataStoreDataset = AppMetadataStore.create(context);
Map<ProgramRunId, RunRecordDetail> runs = metadataStoreDataset.getRuns(sparkId2, ProgramRunStatus.ALL, 0, Long.MAX_VALUE, 100, null);
Assert.assertTrue(runs.isEmpty());
});
}
Aggregations