use of co.cask.cdap.api.schedule.ScheduleSpecification in project cdap by caskdata.
the class ScheduleClientTestRun method testAll.
@Test
public void testAll() throws Exception {
List<ScheduleDetail> list = scheduleClient.listSchedules(workflow);
List<ScheduleSpecification> specs = scheduleClient.list(workflow);
Assert.assertEquals(2, list.size());
Assert.assertEquals(specs, ScheduleDetail.toScheduleSpecs(list));
ScheduleDetail timeSchedule;
ScheduleDetail streamSchedule;
if (list.get(0).getTrigger() instanceof ProtoTrigger.TimeTrigger) {
timeSchedule = list.get(0);
streamSchedule = list.get(1);
} else {
streamSchedule = list.get(0);
timeSchedule = list.get(1);
}
ProtoTrigger.TimeTrigger timeTrigger = (ProtoTrigger.TimeTrigger) timeSchedule.getTrigger();
ProtoTrigger.StreamSizeTrigger streamSizeTrigger = (ProtoTrigger.StreamSizeTrigger) streamSchedule.getTrigger();
Assert.assertEquals(FakeApp.TIME_SCHEDULE_NAME, timeSchedule.getName());
Assert.assertEquals(FakeApp.SCHEDULE_CRON, timeTrigger.getCronExpression());
Assert.assertEquals(FakeApp.STREAM_SCHEDULE_NAME, streamSchedule.getName());
Assert.assertEquals(FakeApp.STREAM_NAME, streamSizeTrigger.getStreamId().getStream());
Assert.assertEquals(FakeApp.STREAM_TRIGGER_MB, streamSizeTrigger.getTriggerMB());
String status = scheduleClient.getStatus(schedule);
Assert.assertEquals("SUSPENDED", status);
status = scheduleClient.getStatus(schedule);
Assert.assertEquals("SUSPENDED", status);
scheduleClient.resume(schedule);
status = scheduleClient.getStatus(schedule);
Assert.assertEquals("SCHEDULED", status);
scheduleClient.suspend(schedule);
status = scheduleClient.getStatus(schedule);
Assert.assertEquals("SUSPENDED", status);
scheduleClient.resume(schedule);
status = scheduleClient.getStatus(schedule);
Assert.assertEquals("SCHEDULED", status);
scheduleClient.suspend(schedule);
status = scheduleClient.getStatus(schedule);
Assert.assertEquals("SUSPENDED", status);
scheduleClient.resume(schedule);
List<ScheduledRuntime> scheduledRuntimes = scheduleClient.nextRuntimes(workflow);
scheduleClient.suspend(schedule);
Assert.assertEquals(1, scheduledRuntimes.size());
// simply assert that its scheduled for some time in the future (or scheduled for now, but hasn't quite
// executed yet
Assert.assertTrue(scheduledRuntimes.get(0).getTime() >= System.currentTimeMillis() - TimeUnit.MINUTES.toMillis(1));
try {
scheduleClient.nextRuntimes(app.workflow("nonexistentWorkflow"));
Assert.fail("Expected not to be able to retrieve next run times for a nonexistent workflow.");
} catch (NotFoundException expected) {
// expected
}
}
use of co.cask.cdap.api.schedule.ScheduleSpecification 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.schedule.ScheduleSpecification 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.schedule.ScheduleSpecification in project cdap by caskdata.
the class ApplicationRegistrationStage method getApplicationSpecificationWithExistingSchedules.
/**
* uses the existing app spec schedules, however if workflows are deleted in the new app spec,
* we want to remove the schedules assigned to those deleted workflow from the existing app spec schedules.
* construct and return a app spec based on this filtered schedules and programs from new app spec.
* @param input
* @return {@link ApplicationSpecification} updated spec.
*/
private ApplicationSpecification getApplicationSpecificationWithExistingSchedules(ApplicationWithPrograms input) {
Map<String, ScheduleSpecification> filteredExistingSchedules = new HashMap<>();
if (input.getExistingAppSpec() != null) {
final Set<String> deletedWorkflows = Maps.difference(input.getExistingAppSpec().getWorkflows(), input.getSpecification().getWorkflows()).entriesOnlyOnLeft().keySet();
// predicate to filter schedules if their workflow is deleted
Predicate<Map.Entry<String, ScheduleSpecification>> byScheduleProgramStatus = new Predicate<Map.Entry<String, ScheduleSpecification>>() {
@Override
public boolean apply(Map.Entry<String, ScheduleSpecification> input) {
return !deletedWorkflows.contains(input.getValue().getProgram().getProgramName());
}
};
filteredExistingSchedules = Maps.filterEntries(input.getExistingAppSpec().getSchedules(), byScheduleProgramStatus);
}
ApplicationSpecification newSpecification = input.getSpecification();
return new DefaultApplicationSpecification(newSpecification.getName(), newSpecification.getAppVersion(), newSpecification.getDescription(), newSpecification.getConfiguration(), newSpecification.getArtifactId(), newSpecification.getStreams(), newSpecification.getDatasetModules(), newSpecification.getDatasets(), newSpecification.getFlows(), newSpecification.getMapReduce(), newSpecification.getSpark(), newSpecification.getWorkflows(), newSpecification.getServices(), filteredExistingSchedules, newSpecification.getProgramSchedules(), newSpecification.getWorkers(), newSpecification.getPlugins());
}
use of co.cask.cdap.api.schedule.ScheduleSpecification in project cdap by caskdata.
the class ApplicationSpecificationCodec method deserialize.
@Override
public ApplicationSpecification deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObj = json.getAsJsonObject();
String name = jsonObj.get("name").getAsString();
String appVersion = ApplicationId.DEFAULT_VERSION;
if (jsonObj.has("appVersion")) {
appVersion = jsonObj.get("appVersion").getAsString();
}
String description = jsonObj.get("description").getAsString();
String configuration = null;
if (jsonObj.has("configuration")) {
configuration = jsonObj.get("configuration").getAsString();
}
ArtifactId artifactId = context.deserialize(jsonObj.get("artifactId"), ArtifactId.class);
Map<String, StreamSpecification> streams = deserializeMap(jsonObj.get("streams"), context, StreamSpecification.class);
Map<String, String> datasetModules = deserializeMap(jsonObj.get("datasetModules"), context, String.class);
Map<String, DatasetCreationSpec> datasetInstances = deserializeMap(jsonObj.get("datasetInstances"), context, DatasetCreationSpec.class);
Map<String, FlowSpecification> flows = deserializeMap(jsonObj.get("flows"), context, FlowSpecification.class);
Map<String, MapReduceSpecification> mapReduces = deserializeMap(jsonObj.get("mapReduces"), context, MapReduceSpecification.class);
Map<String, SparkSpecification> sparks = deserializeMap(jsonObj.get("sparks"), context, SparkSpecification.class);
Map<String, WorkflowSpecification> workflows = deserializeMap(jsonObj.get("workflows"), context, WorkflowSpecification.class);
Map<String, ServiceSpecification> services = deserializeMap(jsonObj.get("services"), context, ServiceSpecification.class);
Map<String, ScheduleSpecification> schedules = deserializeMap(jsonObj.get("schedules"), context, ScheduleSpecification.class);
Map<String, ScheduleCreationSpec> programSchedules = deserializeMap(jsonObj.get("programSchedules"), context, ScheduleCreationSpec.class);
Map<String, WorkerSpecification> workers = deserializeMap(jsonObj.get("workers"), context, WorkerSpecification.class);
Map<String, Plugin> plugins = deserializeMap(jsonObj.get("plugins"), context, Plugin.class);
return new DefaultApplicationSpecification(name, appVersion, description, configuration, artifactId, streams, datasetModules, datasetInstances, flows, mapReduces, sparks, workflows, services, schedules, programSchedules, workers, plugins);
}
Aggregations