use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.
the class DefaultPreviewRunner method startPreview.
@Override
public void startPreview(PreviewRequest<?> previewRequest) throws Exception {
namespaceAdmin.create(new NamespaceMeta.Builder().setName(previewRequest.getProgram().getNamespaceId()).build());
programId = previewRequest.getProgram();
AppRequest<?> request = previewRequest.getAppRequest();
ArtifactSummary artifactSummary = request.getArtifact();
ApplicationId preview = programId.getParent();
DataTracerFactoryProvider.setDataTracerFactory(preview, dataTracerFactory);
String config = request.getConfig() == null ? null : GSON.toJson(request.getConfig());
try {
applicationLifecycleService.deployApp(preview.getParent(), preview.getApplication(), preview.getVersion(), artifactSummary, config, NOOP_PROGRAM_TERMINATOR, null, request.canUpdateSchedules());
} catch (Exception e) {
this.status = new PreviewStatus(PreviewStatus.Status.DEPLOY_FAILED, new BasicThrowable(e), null, null);
throw e;
}
final PreviewConfig previewConfig = previewRequest.getAppRequest().getPreview();
ProgramController controller = programLifecycleService.start(programId, previewConfig == null ? Collections.<String, String>emptyMap() : previewConfig.getRuntimeArgs(), false);
controller.addListener(new AbstractListener() {
@Override
public void init(ProgramController.State currentState, @Nullable Throwable cause) {
setStatus(new PreviewStatus(PreviewStatus.Status.RUNNING, null, System.currentTimeMillis(), null));
// Only have timer if there is a timeout setting.
if (previewConfig.getTimeout() != null) {
timer = new Timer();
final int timeOutMinutes = previewConfig.getTimeout();
timer.schedule(new TimerTask() {
@Override
public void run() {
try {
LOG.info("Stopping the preview since it has reached running time: {} mins.", timeOutMinutes);
stopPreview();
killedByTimer = true;
} catch (Exception e) {
LOG.debug("Error shutting down the preview run with id: {}", programId);
}
}
}, timeOutMinutes * 60 * 1000);
}
}
@Override
public void completed() {
setStatus(new PreviewStatus(PreviewStatus.Status.COMPLETED, null, status.getStartTime(), System.currentTimeMillis()));
shutDownUnrequiredServices();
}
@Override
public void killed() {
if (!killedByTimer) {
setStatus(new PreviewStatus(PreviewStatus.Status.KILLED, null, status.getStartTime(), System.currentTimeMillis()));
} else {
setStatus(new PreviewStatus(PreviewStatus.Status.KILLED_BY_TIMER, null, status.getStartTime(), System.currentTimeMillis()));
}
shutDownUnrequiredServices();
}
@Override
public void error(Throwable cause) {
setStatus(new PreviewStatus(PreviewStatus.Status.RUN_FAILED, new BasicThrowable(cause), status.getStartTime(), System.currentTimeMillis()));
shutDownUnrequiredServices();
}
}, Threads.SAME_THREAD_EXECUTOR);
runId = controller.getProgramRunId();
}
use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.
the class ProgramLifecycleHttpHandlerTest method testMultipleWorkflowSchedules.
@Test
public void testMultipleWorkflowSchedules() throws Exception {
// Deploy the app
NamespaceId testNamespace2 = new NamespaceId(TEST_NAMESPACE2);
Id.Namespace idTestNamespace2 = testNamespace2.toId();
Id.Artifact artifactId = Id.Artifact.from(idTestNamespace2, "appwithmultiplescheduledworkflows", VERSION1);
addAppArtifact(artifactId, AppWithMultipleScheduledWorkflows.class);
AppRequest<? extends Config> appRequest = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()));
Id.Application appDefault = new Id.Application(idTestNamespace2, APP_WITH_MULTIPLE_WORKFLOWS_APP_NAME);
ApplicationId app1 = testNamespace2.app(APP_WITH_MULTIPLE_WORKFLOWS_APP_NAME, VERSION1);
ApplicationId app2 = testNamespace2.app(APP_WITH_MULTIPLE_WORKFLOWS_APP_NAME, VERSION2);
Assert.assertEquals(200, deploy(appDefault, appRequest).getStatusLine().getStatusCode());
Assert.assertEquals(200, deploy(app1, appRequest).getStatusLine().getStatusCode());
Assert.assertEquals(200, deploy(app2, appRequest).getStatusLine().getStatusCode());
// Schedule spec from non-versioned API
List<ScheduleDetail> someSchedules = getSchedules(TEST_NAMESPACE2, APP_WITH_MULTIPLE_WORKFLOWS_APP_NAME, APP_WITH_MULTIPLE_WORKFLOWS_SOMEWORKFLOW);
Assert.assertEquals(2, someSchedules.size());
Assert.assertEquals(APP_WITH_MULTIPLE_WORKFLOWS_SOMEWORKFLOW, someSchedules.get(0).getProgram().getProgramName());
Assert.assertEquals(APP_WITH_MULTIPLE_WORKFLOWS_SOMEWORKFLOW, someSchedules.get(1).getProgram().getProgramName());
// Schedule spec from non-versioned API
List<ScheduleDetail> anotherSchedules = getSchedules(TEST_NAMESPACE2, APP_WITH_MULTIPLE_WORKFLOWS_APP_NAME, APP_WITH_MULTIPLE_WORKFLOWS_ANOTHERWORKFLOW);
Assert.assertEquals(3, anotherSchedules.size());
Assert.assertEquals(APP_WITH_MULTIPLE_WORKFLOWS_ANOTHERWORKFLOW, anotherSchedules.get(0).getProgram().getProgramName());
Assert.assertEquals(APP_WITH_MULTIPLE_WORKFLOWS_ANOTHERWORKFLOW, anotherSchedules.get(1).getProgram().getProgramName());
Assert.assertEquals(APP_WITH_MULTIPLE_WORKFLOWS_ANOTHERWORKFLOW, anotherSchedules.get(2).getProgram().getProgramName());
deleteApp(appDefault, 200);
// Schedule of app1 from versioned API
List<ScheduleDetail> someSchedules1 = getSchedules(TEST_NAMESPACE2, APP_WITH_MULTIPLE_WORKFLOWS_APP_NAME, VERSION1, APP_WITH_MULTIPLE_WORKFLOWS_SOMEWORKFLOW);
Assert.assertEquals(2, someSchedules1.size());
Assert.assertEquals(APP_WITH_MULTIPLE_WORKFLOWS_SOMEWORKFLOW, someSchedules1.get(0).getProgram().getProgramName());
Assert.assertEquals(APP_WITH_MULTIPLE_WORKFLOWS_SOMEWORKFLOW, someSchedules1.get(1).getProgram().getProgramName());
// validate backward-compatible API
List<ScheduleSpecification> someSpecs1 = getScheduleSpecs(TEST_NAMESPACE2, APP_WITH_MULTIPLE_WORKFLOWS_APP_NAME, VERSION1, APP_WITH_MULTIPLE_WORKFLOWS_SOMEWORKFLOW);
Assert.assertEquals(2, someSpecs1.size());
Assert.assertEquals(APP_WITH_MULTIPLE_WORKFLOWS_SOMEWORKFLOW, someSpecs1.get(0).getProgram().getProgramName());
Assert.assertEquals(APP_WITH_MULTIPLE_WORKFLOWS_SOMEWORKFLOW, someSpecs1.get(1).getProgram().getProgramName());
deleteApp(app1, 200);
// Schedule spec of app2 from versioned API
List<ScheduleDetail> anotherSchedules2 = getSchedules(TEST_NAMESPACE2, APP_WITH_MULTIPLE_WORKFLOWS_APP_NAME, VERSION2, APP_WITH_MULTIPLE_WORKFLOWS_ANOTHERWORKFLOW);
Assert.assertEquals(3, anotherSchedules2.size());
Assert.assertEquals(APP_WITH_MULTIPLE_WORKFLOWS_ANOTHERWORKFLOW, anotherSchedules2.get(0).getProgram().getProgramName());
Assert.assertEquals(APP_WITH_MULTIPLE_WORKFLOWS_ANOTHERWORKFLOW, anotherSchedules2.get(1).getProgram().getProgramName());
Assert.assertEquals(APP_WITH_MULTIPLE_WORKFLOWS_ANOTHERWORKFLOW, anotherSchedules2.get(2).getProgram().getProgramName());
deleteApp(app2, 200);
}
use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.
the class ProgramLifecycleHttpHandlerTest method testUpdateSchedulesFlag.
@Test
public void testUpdateSchedulesFlag() throws Exception {
// deploy an app with schedule
AppWithSchedule.AppConfig config = new AppWithSchedule.AppConfig(true, true, true);
Id.Artifact artifactId = Id.Artifact.from(TEST_NAMESPACE_META2.getNamespaceId().toId(), AppWithSchedule.NAME, VERSION1);
addAppArtifact(artifactId, AppWithSchedule.class);
AppRequest<? extends Config> request = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()), config, null, null, false);
ApplicationId defaultAppId = TEST_NAMESPACE_META2.getNamespaceId().app(AppWithSchedule.NAME);
Assert.assertEquals(200, deploy(defaultAppId, request).getStatusLine().getStatusCode());
List<ScheduleDetail> actualSchedules = listSchedules(TEST_NAMESPACE_META2.getNamespaceId().getNamespace(), defaultAppId.getApplication(), defaultAppId.getVersion());
// none of the schedules will be added as we have set update schedules to be false
Assert.assertEquals(0, actualSchedules.size());
request = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()), config, null, null, true);
Assert.assertEquals(200, deploy(defaultAppId, request).getStatusLine().getStatusCode());
actualSchedules = listSchedules(TEST_NAMESPACE_META2.getNamespaceId().getNamespace(), defaultAppId.getApplication(), defaultAppId.getVersion());
Assert.assertEquals(2, actualSchedules.size());
// with workflow, without schedule
config = new AppWithSchedule.AppConfig(true, false, false);
request = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()), config, null, null, false);
Assert.assertEquals(200, deploy(defaultAppId, request).getStatusLine().getStatusCode());
// schedule should not be updated
actualSchedules = listSchedules(TEST_NAMESPACE_META2.getNamespaceId().getNamespace(), defaultAppId.getApplication(), defaultAppId.getVersion());
Assert.assertEquals(2, actualSchedules.size());
// without workflow and schedule, schedule should be deleted
config = new AppWithSchedule.AppConfig(false, false, false);
request = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()), config, null, null, false);
Assert.assertEquals(200, deploy(defaultAppId, request).getStatusLine().getStatusCode());
actualSchedules = listSchedules(TEST_NAMESPACE_META2.getNamespaceId().getNamespace(), defaultAppId.getApplication(), defaultAppId.getVersion());
Assert.assertEquals(0, actualSchedules.size());
// with workflow and one schedule, schedule should be added
config = new AppWithSchedule.AppConfig(true, true, false);
request = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()), config, null, null, true);
Assert.assertEquals(200, deploy(defaultAppId, request).getStatusLine().getStatusCode());
actualSchedules = listSchedules(TEST_NAMESPACE_META2.getNamespaceId().getNamespace(), defaultAppId.getApplication(), defaultAppId.getVersion());
Assert.assertEquals(1, actualSchedules.size());
Assert.assertEquals("SampleSchedule", actualSchedules.get(0).getName());
// with workflow and two schedules, but update-schedules is false, so 2nd schedule should not get added
config = new AppWithSchedule.AppConfig(true, true, true);
request = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()), config, null, null, false);
Assert.assertEquals(200, deploy(defaultAppId, request).getStatusLine().getStatusCode());
actualSchedules = listSchedules(TEST_NAMESPACE_META2.getNamespaceId().getNamespace(), defaultAppId.getApplication(), defaultAppId.getVersion());
Assert.assertEquals(1, actualSchedules.size());
Assert.assertEquals("SampleSchedule", actualSchedules.get(0).getName());
// same config, but update-schedule flag is true now, so 2 schedules should be available now
request = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()), config, null, null, true);
Assert.assertEquals(200, deploy(defaultAppId, request).getStatusLine().getStatusCode());
actualSchedules = listSchedules(TEST_NAMESPACE_META2.getNamespaceId().getNamespace(), defaultAppId.getApplication(), defaultAppId.getVersion());
Assert.assertEquals(2, actualSchedules.size());
}
use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.
the class ProgramLifecycleHttpHandlerTest method testSchedules.
@Test
public void testSchedules() throws Exception {
// deploy an app with schedule
Id.Artifact artifactId = Id.Artifact.from(TEST_NAMESPACE_META1.getNamespaceId().toId(), AppWithSchedule.NAME, VERSION1);
addAppArtifact(artifactId, AppWithSchedule.class);
AppRequest<? extends Config> request = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()));
ApplicationId defaultAppId = TEST_NAMESPACE_META1.getNamespaceId().app(AppWithSchedule.NAME);
Assert.assertEquals(200, deploy(defaultAppId, request).getStatusLine().getStatusCode());
// deploy another version of the app
ApplicationId appV2Id = TEST_NAMESPACE_META1.getNamespaceId().app(AppWithSchedule.NAME, VERSION2);
Assert.assertEquals(200, deploy(appV2Id, request).getStatusLine().getStatusCode());
// list schedules for default version app, for the workflow and for the app, they should be same
List<ScheduleDetail> schedules = getSchedules(TEST_NAMESPACE1, AppWithSchedule.NAME, AppWithSchedule.WORKFLOW_NAME);
Assert.assertEquals(1, schedules.size());
ScheduleDetail schedule = schedules.get(0);
Assert.assertEquals(SchedulableProgramType.WORKFLOW, schedule.getProgram().getProgramType());
Assert.assertEquals(AppWithSchedule.WORKFLOW_NAME, schedule.getProgram().getProgramName());
Assert.assertEquals(new TimeTrigger("0/15 * * * * ?"), schedule.getTrigger());
// there should be two schedules now
List<ScheduleDetail> schedulesForApp = listSchedules(TEST_NAMESPACE1, AppWithSchedule.NAME, null);
Assert.assertEquals(1, schedulesForApp.size());
Assert.assertEquals(schedules, schedulesForApp);
// validate backward compatible api
List<ScheduleSpecification> specsForApp = listScheduleSpecs(TEST_NAMESPACE1, AppWithSchedule.NAME, null);
Assert.assertEquals(1, specsForApp.size());
ScheduleSpecification spec = specsForApp.get(0);
Assert.assertEquals(AppWithSchedule.WORKFLOW_NAME, spec.getProgram().getProgramName());
Assert.assertTrue(spec.getSchedule() instanceof TimeSchedule);
Assert.assertEquals("0/15 * * * * ?", ((TimeSchedule) spec.getSchedule()).getCronEntry());
List<ScheduleDetail> schedules2 = getSchedules(TEST_NAMESPACE1, AppWithSchedule.NAME, VERSION2, AppWithSchedule.WORKFLOW_NAME);
Assert.assertEquals(1, schedules2.size());
ScheduleDetail schedule2 = schedules2.get(0);
Assert.assertEquals(SchedulableProgramType.WORKFLOW, schedule2.getProgram().getProgramType());
Assert.assertEquals(AppWithSchedule.WORKFLOW_NAME, schedule2.getProgram().getProgramName());
Assert.assertEquals(new TimeTrigger("0/15 * * * * ?"), schedule2.getTrigger());
String newSchedule = "newTimeSchedule";
testAddSchedule(newSchedule);
testDeleteSchedule(appV2Id, newSchedule);
testUpdateSchedule(appV2Id);
}
use of co.cask.cdap.api.artifact.ArtifactSummary in project cdap by caskdata.
the class RouteConfigHttpHandlerTest method testRouteStore.
@Test
public void testRouteStore() throws Exception {
// deploy, check the status
Id.Artifact artifactId = Id.Artifact.from(new Id.Namespace(TEST_NAMESPACE1), "wordcountapp", VERSION1);
addAppArtifact(artifactId, WordCountApp.class).getStatusLine().getStatusCode();
ApplicationId appIdV1 = new ApplicationId(TEST_NAMESPACE1, WORDCOUNT_APP_NAME, "v1");
ApplicationId appIdV2 = new ApplicationId(TEST_NAMESPACE1, WORDCOUNT_APP_NAME, "v2");
AppRequest<ConfigTestApp.ConfigClass> request = new AppRequest<>(new ArtifactSummary(artifactId.getName(), artifactId.getVersion().getVersion()), null);
Assert.assertEquals(200, deploy(appIdV1, request).getStatusLine().getStatusCode());
Assert.assertEquals(200, deploy(appIdV2, request).getStatusLine().getStatusCode());
HttpResponse response = deploy(WordCountApp.class, Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
Map<String, Integer> routes = ImmutableMap.<String, Integer>builder().put("v1", 30).put("v2", 70).build();
String routeAPI = getVersionedAPIPath(String.format("apps/%s/services/%s/routeconfig", WORDCOUNT_APP_NAME, WORDCOUNT_SERVICE_NAME), Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1);
doPut(routeAPI, GSON.toJson(routes));
String getResult = EntityUtils.toString(doGet(routeAPI).getEntity());
JsonObject jsonObject = GSON.fromJson(getResult, JsonObject.class);
Assert.assertNotNull(jsonObject);
Assert.assertEquals(30, jsonObject.get("v1").getAsInt());
Assert.assertEquals(70, jsonObject.get("v2").getAsInt());
doDelete(routeAPI);
HttpResponse getResponse = doGet(routeAPI);
getResult = EntityUtils.toString(getResponse.getEntity());
Assert.assertEquals(200, getResponse.getStatusLine().getStatusCode());
Assert.assertEquals("{}", getResult);
Assert.assertEquals(404, doDelete(routeAPI).getStatusLine().getStatusCode());
// Invalid Routes should return 400
routes = ImmutableMap.<String, Integer>builder().put("v1", 50).build();
response = doPut(routeAPI, GSON.toJson(routes));
Assert.assertEquals(400, response.getStatusLine().getStatusCode());
// Valid Routes but non-existing services should return 400
routes = ImmutableMap.<String, Integer>builder().put("v1", 30).put("v2", 70).build();
String nonexistNamespaceRouteAPI = getVersionedAPIPath(String.format("apps/%s/services/%s/routeconfig", WORDCOUNT_APP_NAME, WORDCOUNT_SERVICE_NAME), Constants.Gateway.API_VERSION_3_TOKEN, "_NONEXIST_NAMESPACE_");
response = doPut(nonexistNamespaceRouteAPI, GSON.toJson(routes));
Assert.assertEquals(400, response.getStatusLine().getStatusCode());
String nonexistAppRouteAPI = getVersionedAPIPath(String.format("apps/%s/services/%s/routeconfig", "_NONEXIST_APP", WORDCOUNT_SERVICE_NAME), Constants.Gateway.API_VERSION_3_TOKEN, TEST_NAMESPACE1);
response = doPut(nonexistAppRouteAPI, GSON.toJson(routes));
Assert.assertEquals(400, response.getStatusLine().getStatusCode());
routes = ImmutableMap.<String, Integer>builder().put("_NONEXIST_v1", 30).put("_NONEXIST_v2", 70).build();
response = doPut(routeAPI, GSON.toJson(routes));
Assert.assertEquals(400, response.getStatusLine().getStatusCode());
// Delete apps
deleteApp(appIdV1, 200);
deleteApp(appIdV2, 200);
}
Aggregations