Search in sources :

Example 1 with ApplicationSpecification

use of co.cask.cdap.api.app.ApplicationSpecification in project cdap by caskdata.

the class ProgramScheduleStoreDataset method migrateFromAppMetadataStore.

/**
   * Migrate schedules in a given namespace from app metadata store to ProgramScheduleStoreDataset
   *
   * @param namespaceId  the namespace with schedules to be migrated
   * @param appMetaStore app metadata store with schedules to be migrated
   * @return the lexicographically largest namespace id String with schedule migration completed
   */
public String migrateFromAppMetadataStore(NamespaceId namespaceId, Store appMetaStore) {
    String completedNamespace = getMigrationCompleteNamespace();
    if (completedNamespace != null && completedNamespace.compareTo(namespaceId.toString()) > 0) {
        return completedNamespace;
    }
    for (ApplicationSpecification appSpec : appMetaStore.getAllApplications(namespaceId)) {
        ApplicationId appId = namespaceId.app(appSpec.getName(), appSpec.getAppVersion());
        for (ScheduleSpecification scheduleSpec : appSpec.getSchedules().values()) {
            ProgramSchedule schedule = Schedulers.toProgramSchedule(appId, scheduleSpec);
            try {
                addSchedule(schedule);
            } catch (AlreadyExistsException e) {
                // This should never happen since no schedule with the same schedule key should exist before migration
                LOG.warn("Schedule {} already exists before schedule migration", schedule.getScheduleId(), e);
            }
        }
    }
    store.put(MIGRATION_COMPLETE_NAMESPACE_ROW_BYTES, MIGRATION_COMPLETE_NAMESPACE_COLUMN_BYTES, Bytes.toBytes(namespaceId.toString()));
    return namespaceId.toString();
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) AlreadyExistsException(co.cask.cdap.common.AlreadyExistsException) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ScheduleSpecification(co.cask.cdap.api.schedule.ScheduleSpecification)

Example 2 with ApplicationSpecification

use of co.cask.cdap.api.app.ApplicationSpecification in project cdap by caskdata.

the class ScheduleSpecificationCodecTest method testAppConfigurerRoute.

@Test
public void testAppConfigurerRoute() throws Exception {
    Application app = new AbstractApplication() {

        @Override
        public void configure() {
            // intentionally use the deprecated scheduleWorkflow method to for timeSchedule
            // to test TimeSchedule deserialization
            scheduleWorkflow(Schedules.builder("timeSchedule").createTimeSchedule("0 * * * *"), "workflow");
            scheduleWorkflow(Schedules.builder("streamSizeSchedule").createDataSchedule(Schedules.Source.STREAM, "stream", 1), "workflow");
        }
    };
    ApplicationSpecification specification = Specifications.from(app);
    ApplicationSpecificationAdapter gsonAdapater = ApplicationSpecificationAdapter.create(new ReflectionSchemaGenerator());
    String jsonStr = gsonAdapater.toJson(specification);
    ApplicationSpecification deserializedSpec = gsonAdapater.fromJson(jsonStr);
    Assert.assertEquals(new TimeSchedule("timeSchedule", "", "0 * * * *"), deserializedSpec.getSchedules().get("timeSchedule").getSchedule());
    Assert.assertEquals(new StreamSizeSchedule("streamSizeSchedule", "", "stream", 1), deserializedSpec.getSchedules().get("streamSizeSchedule").getSchedule());
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) AbstractApplication(co.cask.cdap.api.app.AbstractApplication) TimeSchedule(co.cask.cdap.internal.schedule.TimeSchedule) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) StreamSizeSchedule(co.cask.cdap.internal.schedule.StreamSizeSchedule) Application(co.cask.cdap.api.app.Application) AbstractApplication(co.cask.cdap.api.app.AbstractApplication) Test(org.junit.Test)

Example 3 with ApplicationSpecification

use of co.cask.cdap.api.app.ApplicationSpecification in project cdap by caskdata.

the class FlowProgramRunner method run.

@Override
public ProgramController run(Program program, ProgramOptions options) {
    // Extract and verify parameters
    ApplicationSpecification appSpec = program.getApplicationSpecification();
    Preconditions.checkNotNull(appSpec, "Missing application specification.");
    ProgramType processorType = program.getType();
    Preconditions.checkNotNull(processorType, "Missing processor type.");
    Preconditions.checkArgument(processorType == ProgramType.FLOW, "Only FLOW process type is supported.");
    FlowSpecification flowSpec = appSpec.getFlows().get(program.getName());
    Preconditions.checkNotNull(flowSpec, "Missing FlowSpecification for %s", program.getName());
    try {
        // Launch flowlet program runners
        RunId runId = ProgramRunners.getRunId(options);
        Multimap<String, QueueName> consumerQueues = FlowUtils.configureQueue(program, flowSpec, streamAdmin, queueAdmin, txExecutorFactory);
        final Table<String, Integer, ProgramController> flowlets = createFlowlets(program, options, flowSpec);
        return new FlowProgramController(flowlets, program, options, flowSpec, consumerQueues);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    }
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) ProgramController(co.cask.cdap.app.runtime.ProgramController) AbstractProgramController(co.cask.cdap.internal.app.runtime.AbstractProgramController) FlowSpecification(co.cask.cdap.api.flow.FlowSpecification) ProgramType(co.cask.cdap.proto.ProgramType) RunId(org.apache.twill.api.RunId) QueueName(co.cask.cdap.common.queue.QueueName) ExecutionException(java.util.concurrent.ExecutionException)

Example 4 with ApplicationSpecification

use of co.cask.cdap.api.app.ApplicationSpecification in project cdap by caskdata.

the class ProgramLifecycleService method getScheduleStatus.

/**
   * Gets the state of the given schedule
   *
   * @return the status of the given schedule
   * @throws Exception if failed to get the state of the schedule
   */
public ProgramScheduleStatus getScheduleStatus(ScheduleId scheduleId) throws Exception {
    ApplicationId applicationId = scheduleId.getParent();
    ApplicationSpecification appSpec = store.getApplication(applicationId);
    if (appSpec == null) {
        throw new NotFoundException(applicationId);
    }
    ProgramSchedule schedule = scheduler.getSchedule(scheduleId);
    ensureAccess(schedule.getProgramId());
    return scheduler.getScheduleStatus(scheduleId);
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) ProgramSchedule(co.cask.cdap.internal.app.runtime.schedule.ProgramSchedule) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) NotFoundException(co.cask.cdap.common.NotFoundException) ApplicationId(co.cask.cdap.proto.id.ApplicationId)

Example 5 with ApplicationSpecification

use of co.cask.cdap.api.app.ApplicationSpecification in project cdap by caskdata.

the class ProgramLifecycleService method retrieveProgramIdForRunRecord.

/**
   * Helper method to get {@link ProgramId} for a RunRecord for type of program
   *
   * @param programType Type of program to search
   * @param runId The target id of the {@link RunRecord} to find
   * @return the program id of the run record or {@code null} if does not exist.
   */
@Nullable
private ProgramId retrieveProgramIdForRunRecord(ProgramType programType, String runId) {
    // Get list of namespaces (borrow logic from AbstractAppFabricHttpHandler#listPrograms)
    List<NamespaceMeta> namespaceMetas = nsStore.list();
    // For each, get all programs under it
    ProgramId targetProgramId = null;
    for (NamespaceMeta nm : namespaceMetas) {
        NamespaceId namespace = Ids.namespace(nm.getName());
        Collection<ApplicationSpecification> appSpecs = store.getAllApplications(namespace);
        // For each application get the programs checked against run records
        for (ApplicationSpecification appSpec : appSpecs) {
            switch(programType) {
                case FLOW:
                    for (String programName : appSpec.getFlows().keySet()) {
                        ProgramId programId = validateProgramForRunRecord(nm.getName(), appSpec.getName(), appSpec.getAppVersion(), programType, programName, runId);
                        if (programId != null) {
                            targetProgramId = programId;
                            break;
                        }
                    }
                    break;
                case MAPREDUCE:
                    for (String programName : appSpec.getMapReduce().keySet()) {
                        ProgramId programId = validateProgramForRunRecord(nm.getName(), appSpec.getName(), appSpec.getAppVersion(), programType, programName, runId);
                        if (programId != null) {
                            targetProgramId = programId;
                            break;
                        }
                    }
                    break;
                case SPARK:
                    for (String programName : appSpec.getSpark().keySet()) {
                        ProgramId programId = validateProgramForRunRecord(nm.getName(), appSpec.getName(), appSpec.getAppVersion(), programType, programName, runId);
                        if (programId != null) {
                            targetProgramId = programId;
                            break;
                        }
                    }
                    break;
                case SERVICE:
                    for (String programName : appSpec.getServices().keySet()) {
                        ProgramId programId = validateProgramForRunRecord(nm.getName(), appSpec.getName(), appSpec.getAppVersion(), programType, programName, runId);
                        if (programId != null) {
                            targetProgramId = programId;
                            break;
                        }
                    }
                    break;
                case WORKER:
                    for (String programName : appSpec.getWorkers().keySet()) {
                        ProgramId programId = validateProgramForRunRecord(nm.getName(), appSpec.getName(), appSpec.getAppVersion(), programType, programName, runId);
                        if (programId != null) {
                            targetProgramId = programId;
                            break;
                        }
                    }
                    break;
                case WORKFLOW:
                    for (String programName : appSpec.getWorkflows().keySet()) {
                        ProgramId programId = validateProgramForRunRecord(nm.getName(), appSpec.getName(), appSpec.getAppVersion(), programType, programName, runId);
                        if (programId != null) {
                            targetProgramId = programId;
                            break;
                        }
                    }
                    break;
                case CUSTOM_ACTION:
                case WEBAPP:
                    // no-op
                    break;
                default:
                    LOG.debug("Unknown program type: " + programType.name());
                    break;
            }
            if (targetProgramId != null) {
                break;
            }
        }
        if (targetProgramId != null) {
            break;
        }
    }
    return targetProgramId;
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) NamespaceId(co.cask.cdap.proto.id.NamespaceId) ProgramId(co.cask.cdap.proto.id.ProgramId) Nullable(javax.annotation.Nullable)

Aggregations

ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)104 ApplicationId (co.cask.cdap.proto.id.ApplicationId)47 ProgramId (co.cask.cdap.proto.id.ProgramId)28 Test (org.junit.Test)27 ProgramType (co.cask.cdap.proto.ProgramType)22 FlowSpecification (co.cask.cdap.api.flow.FlowSpecification)16 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)15 NotFoundException (co.cask.cdap.common.NotFoundException)14 ReflectionSchemaGenerator (co.cask.cdap.internal.io.ReflectionSchemaGenerator)12 ProgramRunId (co.cask.cdap.proto.id.ProgramRunId)12 ApplicationSpecificationAdapter (co.cask.cdap.internal.app.ApplicationSpecificationAdapter)11 NamespaceId (co.cask.cdap.proto.id.NamespaceId)11 ArrayList (java.util.ArrayList)11 HashMap (java.util.HashMap)10 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)9 WordCountApp (co.cask.cdap.WordCountApp)8 ServiceSpecification (co.cask.cdap.api.service.ServiceSpecification)8 IOException (java.io.IOException)8 RunId (org.apache.twill.api.RunId)8 Map (java.util.Map)7