Search in sources :

Example 46 with ProgramType

use of co.cask.cdap.proto.ProgramType in project cdap by caskdata.

the class AbstractProgramRuntimeServiceTest method createProgramRunnerFactory.

/**
   * Creates a {@link ProgramRunnerFactory} for creating {@link ProgramRunner}
   * that always run with a {@link FastService}.
   *
   * @param argumentsMap the map to be populated with the user arguments for each run.
   */
private ProgramRunnerFactory createProgramRunnerFactory(final Map<ProgramId, Arguments> argumentsMap) {
    return new ProgramRunnerFactory() {

        @Override
        public ProgramRunner create(ProgramType programType) {
            return new ProgramRunner() {

                @Override
                public ProgramController run(Program program, ProgramOptions options) {
                    argumentsMap.put(program.getId(), options.getUserArguments());
                    Service service = new FastService();
                    ProgramController controller = new ProgramControllerServiceAdapter(service, program.getId(), RunIds.generate());
                    service.start();
                    return controller;
                }
            };
        }
    };
}
Also used : Program(co.cask.cdap.app.program.Program) ProgramControllerServiceAdapter(co.cask.cdap.internal.app.runtime.ProgramControllerServiceAdapter) AbstractExecutionThreadService(com.google.common.util.concurrent.AbstractExecutionThreadService) AbstractIdleService(com.google.common.util.concurrent.AbstractIdleService) Service(com.google.common.util.concurrent.Service) ProgramType(co.cask.cdap.proto.ProgramType) SimpleProgramOptions(co.cask.cdap.internal.app.runtime.SimpleProgramOptions)

Example 47 with ProgramType

use of co.cask.cdap.proto.ProgramType in project cdap by caskdata.

the class WorkerProgramRunner method run.

@Override
public ProgramController run(Program program, ProgramOptions options) {
    ApplicationSpecification appSpec = program.getApplicationSpecification();
    Preconditions.checkNotNull(appSpec, "Missing application specification.");
    int instanceId = Integer.parseInt(options.getArguments().getOption(ProgramOptionConstants.INSTANCE_ID, "-1"));
    Preconditions.checkArgument(instanceId >= 0, "Missing instance Id");
    int instanceCount = Integer.parseInt(options.getArguments().getOption(ProgramOptionConstants.INSTANCES, "0"));
    Preconditions.checkArgument(instanceCount > 0, "Invalid or missing instance count");
    RunId runId = ProgramRunners.getRunId(options);
    ProgramType programType = program.getType();
    Preconditions.checkNotNull(programType, "Missing processor type.");
    Preconditions.checkArgument(programType == ProgramType.WORKER, "Only Worker process type is supported.");
    WorkerSpecification workerSpec = appSpec.getWorkers().get(program.getName());
    Preconditions.checkArgument(workerSpec != null, "Missing Worker specification for %s", program.getId());
    String instances = options.getArguments().getOption(ProgramOptionConstants.INSTANCES, String.valueOf(workerSpec.getInstances()));
    WorkerSpecification newWorkerSpec = new WorkerSpecification(workerSpec.getClassName(), workerSpec.getName(), workerSpec.getDescription(), workerSpec.getProperties(), workerSpec.getDatasets(), workerSpec.getResources(), Integer.valueOf(instances));
    // Setup dataset framework context, if required
    if (datasetFramework instanceof ProgramContextAware) {
        ProgramId programId = program.getId();
        ((ProgramContextAware) datasetFramework).setContext(new BasicProgramContext(programId.run(runId)));
    }
    final PluginInstantiator pluginInstantiator = createPluginInstantiator(options, program.getClassLoader());
    try {
        BasicWorkerContext context = new BasicWorkerContext(newWorkerSpec, program, options, cConf, instanceId, instanceCount, metricsCollectionService, datasetFramework, txClient, discoveryServiceClient, streamWriterFactory, pluginInstantiator, secureStore, secureStoreManager, messagingService);
        WorkerDriver worker = new WorkerDriver(program, newWorkerSpec, context);
        // Add a service listener to make sure the plugin instantiator is closed when the worker driver finished.
        worker.addListener(new ServiceListenerAdapter() {

            @Override
            public void terminated(Service.State from) {
                Closeables.closeQuietly(pluginInstantiator);
            }

            @Override
            public void failed(Service.State from, Throwable failure) {
                Closeables.closeQuietly(pluginInstantiator);
            }
        }, Threads.SAME_THREAD_EXECUTOR);
        ProgramController controller = new WorkerControllerServiceAdapter(worker, program.getId(), runId, workerSpec.getName() + "-" + instanceId);
        worker.start();
        return controller;
    } catch (Throwable t) {
        Closeables.closeQuietly(pluginInstantiator);
        throw t;
    }
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) ProgramController(co.cask.cdap.app.runtime.ProgramController) WorkerSpecification(co.cask.cdap.api.worker.WorkerSpecification) MessagingService(co.cask.cdap.messaging.MessagingService) MetricsCollectionService(co.cask.cdap.api.metrics.MetricsCollectionService) Service(com.google.common.util.concurrent.Service) ServiceListenerAdapter(org.apache.twill.internal.ServiceListenerAdapter) ProgramId(co.cask.cdap.proto.id.ProgramId) BasicProgramContext(co.cask.cdap.internal.app.runtime.BasicProgramContext) PluginInstantiator(co.cask.cdap.internal.app.runtime.plugin.PluginInstantiator) ProgramType(co.cask.cdap.proto.ProgramType) RunId(org.apache.twill.api.RunId) ProgramContextAware(co.cask.cdap.data.ProgramContextAware)

Example 48 with ProgramType

use of co.cask.cdap.proto.ProgramType in project cdap by caskdata.

the class ProgramLifecycleServiceAuthorizationTest method testProgramList.

@Test
public void testProgramList() throws Exception {
    SecurityRequestContext.setUserId(ALICE.getName());
    // AppFabricTestHelper tries to create a namespace if it does not already exist
    authorizer.grant(new InstanceId(cConf.get(Constants.INSTANCE_NAME)), ALICE, Collections.singleton(Action.ADMIN));
    authorizer.grant(NamespaceId.DEFAULT, ALICE, Collections.singleton(Action.WRITE));
    AppFabricTestHelper.deployApplication(Id.Namespace.DEFAULT, AllProgramsApp.class, null, cConf);
    for (ProgramType type : ProgramType.values()) {
        if (!type.equals(ProgramType.CUSTOM_ACTION) && !type.equals(ProgramType.WEBAPP)) {
            Assert.assertFalse(programLifecycleService.list(NamespaceId.DEFAULT, type).isEmpty());
            SecurityRequestContext.setUserId("bob");
            Assert.assertTrue(programLifecycleService.list(NamespaceId.DEFAULT, type).isEmpty());
            SecurityRequestContext.setUserId("alice");
        }
    }
}
Also used : InstanceId(co.cask.cdap.proto.id.InstanceId) ProgramType(co.cask.cdap.proto.ProgramType) Test(org.junit.Test)

Example 49 with ProgramType

use of co.cask.cdap.proto.ProgramType in project cdap by caskdata.

the class BaseBatchCommand method readArgs.

/**
   * Reads arguments to get app id, program types, and list of input programs.
   */
protected Args<T> readArgs(Arguments arguments) throws ApplicationNotFoundException, UnauthenticatedException, IOException, UnauthorizedException {
    String appName = arguments.get(ArgumentName.APP.getName());
    ApplicationId appId = cliConfig.getCurrentNamespace().app(appName);
    Set<ProgramType> programTypes = getDefaultProgramTypes();
    if (arguments.hasArgument(ArgumentName.PROGRAM_TYPES.getName())) {
        programTypes.clear();
        String programTypesStr = arguments.get(ArgumentName.PROGRAM_TYPES.getName());
        for (String programTypeStr : Splitter.on(',').trimResults().split(programTypesStr)) {
            ProgramType programType = ProgramType.valueOf(programTypeStr.toUpperCase());
            programTypes.add(programType);
        }
    }
    List<T> programs = new ArrayList<>();
    Map<ProgramType, List<ProgramRecord>> appPrograms = appClient.listProgramsByType(appId);
    for (ProgramType programType : programTypes) {
        List<ProgramRecord> programRecords = appPrograms.get(programType);
        if (programRecords != null) {
            for (ProgramRecord programRecord : programRecords) {
                programs.add(createProgram(programRecord));
            }
        }
    }
    return new Args<>(appId, programTypes, programs);
}
Also used : ProgramRecord(co.cask.cdap.proto.ProgramRecord) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) ProgramType(co.cask.cdap.proto.ProgramType) ApplicationId(co.cask.cdap.proto.id.ApplicationId)

Example 50 with ProgramType

use of co.cask.cdap.proto.ProgramType in project cdap by caskdata.

the class ProgramLifecycleHttpHandler method saveProgramRuntimeArgs.

/**
   * Save program runtime args.
   */
@PUT
@Path("/apps/{app-name}/{program-type}/{program-name}/runtimeargs")
@AuditPolicy(AuditDetail.REQUEST_BODY)
public void saveProgramRuntimeArgs(HttpRequest request, HttpResponder responder, @PathParam("namespace-id") String namespaceId, @PathParam("app-name") String appName, @PathParam("program-type") String type, @PathParam("program-name") String programName) throws Exception {
    ProgramType programType = getProgramType(type);
    ProgramId programId = new ProgramId(namespaceId, appName, programType, programName);
    saveProgramIdRuntimeArgs(programId, request, responder);
}
Also used : ProgramType(co.cask.cdap.proto.ProgramType) ProgramId(co.cask.cdap.proto.id.ProgramId) Path(javax.ws.rs.Path) AuditPolicy(co.cask.cdap.common.security.AuditPolicy) PUT(javax.ws.rs.PUT)

Aggregations

ProgramType (co.cask.cdap.proto.ProgramType)59 ProgramId (co.cask.cdap.proto.id.ProgramId)25 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)19 ApplicationId (co.cask.cdap.proto.id.ApplicationId)14 Path (javax.ws.rs.Path)13 RunId (org.apache.twill.api.RunId)12 GET (javax.ws.rs.GET)11 ArrayList (java.util.ArrayList)8 BadRequestException (co.cask.cdap.common.BadRequestException)7 ProgramController (co.cask.cdap.app.runtime.ProgramController)6 NamespaceNotFoundException (co.cask.cdap.common.NamespaceNotFoundException)6 NotFoundException (co.cask.cdap.common.NotFoundException)6 ProgramContextAware (co.cask.cdap.data.ProgramContextAware)6 BasicProgramContext (co.cask.cdap.internal.app.runtime.BasicProgramContext)6 Service (com.google.common.util.concurrent.Service)6 ProgramSpecification (co.cask.cdap.api.ProgramSpecification)5 FlowSpecification (co.cask.cdap.api.flow.FlowSpecification)5 MetricsCollectionService (co.cask.cdap.api.metrics.MetricsCollectionService)5 MessagingService (co.cask.cdap.messaging.MessagingService)5 WorkflowSpecification (co.cask.cdap.api.workflow.WorkflowSpecification)4