use of io.cdap.cdap.proto.id.ApplicationId in project cdap by caskdata.
the class LineageAdmin method extractAndAddInnerPrograms.
/**
* Extract inner programs and runs from the workflow run record, the run record's properties have all the
* inner program run ids. The workflow spec can then be used to determine what the inner programs are and
* create the program run ids for them
*/
private void extractAndAddInnerPrograms(Set<ProgramId> toVisitPrograms, Map<ProgramRunId, ProgramRunId> programWorkflowMap, Map<ApplicationId, ApplicationSpecification> appSpecs, ProgramRunId programRunId, RunRecordDetail wfRunRecord) {
ApplicationId appId = programRunId.getParent().getParent();
WorkflowSpecification workflowSpec = appSpecs.get(appId).getWorkflows().get(programRunId.getProgram());
Map<String, WorkflowNode> nodeIdMap = workflowSpec.getNodeIdMap();
wfRunRecord.getProperties().forEach((key, value) -> {
if (nodeIdMap.containsKey(key)) {
WorkflowActionNode node = (WorkflowActionNode) nodeIdMap.get(key);
ProgramType type = ProgramType.valueOf(node.getProgram().getProgramType().name());
ProgramId program = appId.program(type, key);
programWorkflowMap.put(program.run(value), programRunId);
toVisitPrograms.add(program);
}
});
}
use of io.cdap.cdap.proto.id.ApplicationId in project cdap by caskdata.
the class RemotePreferencesFetcherInternal method getPreferencesURI.
/**
* Construct URI to fetch preferences depending on the type of supplied entity
*/
private String getPreferencesURI(EntityId entityId, boolean resolved) {
String uri;
switch(entityId.getEntityType()) {
case INSTANCE:
uri = "preferences";
break;
case NAMESPACE:
NamespaceId namespaceId = (NamespaceId) entityId;
uri = String.format("namespaces/%s/preferences", namespaceId.getNamespace());
break;
case APPLICATION:
ApplicationId appId = (ApplicationId) entityId;
uri = String.format("namespaces/%s/apps/%s/preferences", appId.getNamespace(), appId.getApplication());
break;
case PROGRAM:
ProgramId programId = (ProgramId) entityId;
uri = String.format("namespaces/%s/apps/%s/%s/%s/preferences", programId.getNamespace(), programId.getApplication(), programId.getType().getCategoryName(), programId.getProgram());
break;
default:
throw new UnsupportedOperationException(String.format("Preferences cannot be used on this entity type: %s", entityId.getEntityType()));
}
if (resolved) {
uri += "?resolved=true";
}
return uri;
}
use of io.cdap.cdap.proto.id.ApplicationId in project cdap by caskdata.
the class ScheduledRunTimeTest method testGetNextRun.
@Test
public void testGetNextRun() throws Exception {
ApplicationId appId = NamespaceId.DEFAULT.app("test");
deploy(appId, new AppRequest<>(new ArtifactSummary(ARTIFACT_ID.getName(), ARTIFACT_ID.getVersion().getVersion())));
String scheduleName = "schedule1";
// Add a schedule. Use a constraint to make it not going to be executed
ProgramId programId = appId.workflow(WorkflowApp.FunWorkflow.NAME);
Constraint constraint = new DelayConstraint(1, TimeUnit.HOURS);
ScheduleProgramInfo scheduleProgramInfo = new ScheduleProgramInfo(programId.getType().getSchedulableType(), programId.getProgram());
addSchedule(appId.getNamespace(), appId.getApplication(), appId.getVersion(), scheduleName, new ScheduleDetail(scheduleName, null, scheduleProgramInfo, null, new TimeTrigger("0 0 * * * "), Collections.singletonList(constraint), null));
long now = System.currentTimeMillis();
HttpResponse response = enableSchedule(programId.getNamespace(), programId.getApplication(), programId.getVersion(), scheduleName);
Assert.assertEquals(200, response.getResponseCode());
// Get the next run time
List<ScheduledRuntime> scheduledRunTimes = getScheduledRunTimes(programId, true);
Assert.assertEquals(1, scheduledRunTimes.size());
long nextTime = scheduledRunTimes.get(0).getTime();
Assert.assertTrue(nextTime >= now);
}
use of io.cdap.cdap.proto.id.ApplicationId in project cdap by caskdata.
the class ScheduledRunTimeTest method testBatchGetNextRun.
@Test
public void testBatchGetNextRun() throws Exception {
// deploys 5 apps and create schedules for each of them
long now = System.currentTimeMillis();
List<ProgramId> programIds = new ArrayList<>();
// Use a constraint to make it not going to be executed
Constraint constraint = new DelayConstraint(1, TimeUnit.HOURS);
for (int i = 0; i < 5; i++) {
ApplicationId appId = NamespaceId.DEFAULT.app("test" + i);
deploy(appId, new AppRequest<>(new ArtifactSummary(ARTIFACT_ID.getName(), ARTIFACT_ID.getVersion().getVersion())));
String scheduleName = "schedule" + i;
// Add a schedule
ProgramId programId = appId.workflow(WorkflowApp.FunWorkflow.NAME);
programIds.add(programId);
ScheduleProgramInfo scheduleProgramInfo = new ScheduleProgramInfo(programId.getType().getSchedulableType(), programId.getProgram());
addSchedule(appId.getNamespace(), appId.getApplication(), appId.getVersion(), scheduleName, new ScheduleDetail(scheduleName, null, scheduleProgramInfo, null, new TimeTrigger("0 0 * * * "), Collections.singletonList(constraint), null));
HttpResponse response = enableSchedule(programId.getNamespace(), programId.getApplication(), programId.getVersion(), scheduleName);
Assert.assertEquals(200, response.getResponseCode());
}
// Add programs that the app or the program doesn't exist
programIds.add(NamespaceId.DEFAULT.app("not-exist").workflow("not-exist"));
programIds.add(NamespaceId.DEFAULT.app("test1").workflow("not-exist"));
List<BatchProgramSchedule> schedules = getScheduledRunTimes(NamespaceId.DEFAULT.getNamespace(), programIds, true);
Assert.assertEquals(programIds.size(), schedules.size());
// For the first 5 programs, they should have a next run
for (int i = 0; i < 5; i++) {
BatchProgramSchedule schedule = schedules.get(i);
Assert.assertEquals(200, schedule.getStatusCode());
List<ScheduledRuntime> nextRuns = schedule.getSchedules();
Assert.assertNotNull(nextRuns);
Assert.assertEquals(1, nextRuns.size());
long nextTime = nextRuns.get(0).getTime();
Assert.assertTrue(nextTime >= now);
}
// The last two should be a not found
Assert.assertEquals(404, schedules.get(5).getStatusCode());
Assert.assertEquals(404, schedules.get(6).getStatusCode());
}
use of io.cdap.cdap.proto.id.ApplicationId in project cdap by caskdata.
the class DefaultStore method scanApplications.
@Override
public boolean scanApplications(ScanApplicationsRequest request, int txBatchSize, BiConsumer<ApplicationId, ApplicationSpecification> consumer) {
AtomicReference<ScanApplicationsRequest> requestRef = new AtomicReference<>(request);
AtomicReference<ApplicationId> lastKey = new AtomicReference<>();
AtomicInteger currentLimit = new AtomicInteger(request.getLimit());
while (currentLimit.get() > 0) {
AtomicInteger count = new AtomicInteger();
try {
TransactionRunners.run(transactionRunner, context -> {
getAppMetadataStore(context).scanApplications(requestRef.get(), entry -> {
lastKey.set(entry.getKey());
currentLimit.decrementAndGet();
consumer.accept(entry.getKey(), entry.getValue().getSpec());
return count.incrementAndGet() < txBatchSize && currentLimit.get() > 0;
});
});
} catch (UnsupportedOperationException e) {
if (requestRef.get().getSortOrder() != SortOrder.DESC || count.get() != 0) {
throw e;
}
scanApplicationwWithReorder(requestRef.get(), txBatchSize, consumer);
}
if (lastKey.get() == null) {
break;
}
ScanApplicationsRequest nextBatchRequest = ScanApplicationsRequest.builder(requestRef.get()).setScanFrom(lastKey.get()).setLimit(currentLimit.get()).build();
requestRef.set(nextBatchRequest);
lastKey.set(null);
}
return currentLimit.get() == 0;
}
Aggregations