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;
}
};
}
};
}
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;
}
}
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");
}
}
}
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);
}
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);
}
Aggregations