Search in sources :

Example 31 with ApplicationId

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);
        }
    });
}
Also used : WorkflowActionNode(io.cdap.cdap.api.workflow.WorkflowActionNode) WorkflowSpecification(io.cdap.cdap.api.workflow.WorkflowSpecification) ProgramType(io.cdap.cdap.proto.ProgramType) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) WorkflowNode(io.cdap.cdap.api.workflow.WorkflowNode)

Example 32 with ApplicationId

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;
}
Also used : NamespaceId(io.cdap.cdap.proto.id.NamespaceId) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId)

Example 33 with ApplicationId

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);
}
Also used : TimeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint) DelayConstraint(io.cdap.cdap.internal.app.runtime.schedule.constraint.DelayConstraint) HttpResponse(io.cdap.common.http.HttpResponse) ProgramId(io.cdap.cdap.proto.id.ProgramId) DelayConstraint(io.cdap.cdap.internal.app.runtime.schedule.constraint.DelayConstraint) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ScheduleDetail(io.cdap.cdap.proto.ScheduleDetail) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ScheduleProgramInfo(io.cdap.cdap.api.workflow.ScheduleProgramInfo) ScheduledRuntime(io.cdap.cdap.proto.ScheduledRuntime) Test(org.junit.Test)

Example 34 with ApplicationId

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());
}
Also used : BatchProgramSchedule(io.cdap.cdap.proto.BatchProgramSchedule) TimeTrigger(io.cdap.cdap.internal.app.runtime.schedule.trigger.TimeTrigger) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint) DelayConstraint(io.cdap.cdap.internal.app.runtime.schedule.constraint.DelayConstraint) ArrayList(java.util.ArrayList) HttpResponse(io.cdap.common.http.HttpResponse) ProgramId(io.cdap.cdap.proto.id.ProgramId) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint) DelayConstraint(io.cdap.cdap.internal.app.runtime.schedule.constraint.DelayConstraint) DelayConstraint(io.cdap.cdap.internal.app.runtime.schedule.constraint.DelayConstraint) ArtifactSummary(io.cdap.cdap.api.artifact.ArtifactSummary) ScheduleDetail(io.cdap.cdap.proto.ScheduleDetail) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ScheduleProgramInfo(io.cdap.cdap.api.workflow.ScheduleProgramInfo) ScheduledRuntime(io.cdap.cdap.proto.ScheduledRuntime) Test(org.junit.Test)

Example 35 with ApplicationId

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;
}
Also used : ScanApplicationsRequest(io.cdap.cdap.app.store.ScanApplicationsRequest) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicReference(java.util.concurrent.atomic.AtomicReference) ApplicationId(io.cdap.cdap.proto.id.ApplicationId)

Aggregations

ApplicationId (io.cdap.cdap.proto.id.ApplicationId)789 Test (org.junit.Test)410 AppRequest (io.cdap.cdap.proto.artifact.AppRequest)279 ProgramId (io.cdap.cdap.proto.id.ProgramId)263 ApplicationManager (io.cdap.cdap.test.ApplicationManager)225 ETLStage (io.cdap.cdap.etl.proto.v2.ETLStage)223 ETLBatchConfig (io.cdap.cdap.etl.proto.v2.ETLBatchConfig)196 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)180 Table (io.cdap.cdap.api.dataset.table.Table)178 WorkflowManager (io.cdap.cdap.test.WorkflowManager)169 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)154 Schema (io.cdap.cdap.api.data.schema.Schema)147 ArrayList (java.util.ArrayList)129 HashSet (java.util.HashSet)126 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)124 HashMap (java.util.HashMap)109 KeyValueTable (io.cdap.cdap.api.dataset.lib.KeyValueTable)107 ArtifactSummary (io.cdap.cdap.api.artifact.ArtifactSummary)88 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)88 Path (javax.ws.rs.Path)75