use of io.cdap.cdap.app.program.ProgramDescriptor in project cdap by caskdata.
the class AbstractProgramTwillRunnable method doInitialize.
/**
* Prepares this instance to execute a program.
*
* @param programOptionFile a json file containing the serialized {@link ProgramOptions}
* @throws Exception if failed to initialize
*/
private void doInitialize(File programOptionFile) throws Exception {
controllerFuture = new CompletableFuture<>();
programCompletion = new CompletableFuture<>();
// Setup process wide settings
Thread.setDefaultUncaughtExceptionHandler(new UncaughtExceptionHandler());
System.setSecurityManager(new ProgramContainerSecurityManager(System.getSecurityManager()));
SLF4JBridgeHandler.removeHandlersForRootLogger();
SLF4JBridgeHandler.install();
// Create the ProgramOptions
programOptions = createProgramOptions(programOptionFile);
programRunId = programOptions.getProgramId().run(ProgramRunners.getRunId(programOptions));
Arguments systemArgs = programOptions.getArguments();
LoggingContextAccessor.setLoggingContext(LoggingContextHelper.getLoggingContextWithRunId(programRunId, systemArgs.asMap()));
ClusterMode clusterMode = ProgramRunners.getClusterMode(programOptions);
// Loads configurations
Configuration hConf = new Configuration();
if (clusterMode == ClusterMode.ON_PREMISE) {
hConf.clear();
hConf.addResource(new File(systemArgs.getOption(ProgramOptionConstants.HADOOP_CONF_FILE)).toURI().toURL());
}
UserGroupInformation.setConfiguration(hConf);
CConfiguration cConf = CConfiguration.create();
cConf.clear();
cConf.addResource(new File(systemArgs.getOption(ProgramOptionConstants.CDAP_CONF_FILE)).toURI().toURL());
maxStopSeconds = cConf.getLong(io.cdap.cdap.common.conf.Constants.AppFabric.PROGRAM_MAX_STOP_SECONDS);
Injector injector = Guice.createInjector(createModule(cConf, hConf, programOptions, programRunId));
// Initialize log appender
logAppenderInitializer = injector.getInstance(LogAppenderInitializer.class);
logAppenderInitializer.initialize();
SystemArguments.setLogLevel(programOptions.getUserArguments(), logAppenderInitializer);
// Setup the proxy selector for in active monitoring mode
oldProxySelector = ProxySelector.getDefault();
if (clusterMode == ClusterMode.ISOLATED) {
RuntimeMonitors.setupMonitoring(injector, programOptions);
}
// Create list of core services. They'll will be started in the run method and shutdown when the run
// method completed
coreServices = createCoreServices(injector, programOptions);
// Create the ProgramRunner
programRunner = createProgramRunner(injector);
// Create the Program instance
Location programJarLocation = Locations.toLocation(new File(systemArgs.getOption(ProgramOptionConstants.PROGRAM_JAR)));
ApplicationSpecification appSpec = readJsonFile(new File(systemArgs.getOption(ProgramOptionConstants.APP_SPEC_FILE)), ApplicationSpecification.class);
// Expand the program jar for creating classloader
ClassLoaderFolder classLoaderFolder = BundleJarUtil.prepareClassLoaderFolder(programJarLocation, () -> new File("expanded." + System.currentTimeMillis() + programJarLocation.getName()));
program = Programs.create(cConf, programRunner, new ProgramDescriptor(programOptions.getProgramId(), appSpec), programJarLocation, classLoaderFolder.getDir());
}
use of io.cdap.cdap.app.program.ProgramDescriptor in project cdap by caskdata.
the class DefaultStoreTest method testLoadingProgram.
@Test
public void testLoadingProgram() throws Exception {
ApplicationSpecification appSpec = Specifications.from(new FooApp());
ApplicationId appId = NamespaceId.DEFAULT.app(appSpec.getName());
store.addApplication(appId, appSpec);
ProgramDescriptor descriptor = store.loadProgram(appId.mr("mrJob1"));
Assert.assertNotNull(descriptor);
MapReduceSpecification mrSpec = descriptor.getSpecification();
Assert.assertEquals("mrJob1", mrSpec.getName());
Assert.assertEquals(FooMapReduceJob.class.getName(), mrSpec.getClassName());
}
use of io.cdap.cdap.app.program.ProgramDescriptor in project cdap by caskdata.
the class ProgramGenerationStage method process.
@Override
public void process(final ApplicationDeployable input) throws Exception {
List<ProgramDescriptor> programDescriptors = new ArrayList<>();
final ApplicationSpecification appSpec = input.getSpecification();
// Now, we iterate through all ProgramSpecification and generate programs
Iterable<ProgramSpecification> specifications = Iterables.concat(appSpec.getMapReduce().values(), appSpec.getWorkflows().values(), appSpec.getServices().values(), appSpec.getSpark().values(), appSpec.getWorkers().values());
for (ProgramSpecification spec : specifications) {
ProgramType type = ProgramTypes.fromSpecification(spec);
ProgramId programId = input.getApplicationId().program(type, spec.getName());
programDescriptors.add(new ProgramDescriptor(programId, appSpec));
}
emit(new ApplicationWithPrograms(input, programDescriptors));
}
use of io.cdap.cdap.app.program.ProgramDescriptor in project cdap by caskdata.
the class AbstractProgramRuntimeService method run.
@Override
public final RuntimeInfo run(ProgramDescriptor programDescriptor, ProgramOptions options, RunId runId) {
ProgramId programId = programDescriptor.getProgramId();
ProgramRunId programRunId = programId.run(runId);
ClusterMode clusterMode = ProgramRunners.getClusterMode(options);
// Creates the ProgramRunner based on the cluster mode
ProgramRunner runner = (clusterMode == ClusterMode.ON_PREMISE ? programRunnerFactory : Optional.ofNullable(remoteProgramRunnerFactory).orElseThrow(UnsupportedOperationException::new)).create(programId.getType());
File tempDir = createTempDirectory(programId, runId);
AtomicReference<Runnable> cleanUpTaskRef = new AtomicReference<>(createCleanupTask(tempDir, runner));
DelayedProgramController controller = new DelayedProgramController(programRunId);
RuntimeInfo runtimeInfo = createRuntimeInfo(controller, programId, () -> cleanUpTaskRef.get().run());
updateRuntimeInfo(runtimeInfo);
executor.execute(() -> {
try {
// Get the artifact details and save it into the program options.
ArtifactId artifactId = programDescriptor.getArtifactId();
ArtifactDetail artifactDetail = getArtifactDetail(artifactId);
ApplicationSpecification appSpec = programDescriptor.getApplicationSpecification();
ProgramDescriptor newProgramDescriptor = programDescriptor;
boolean isPreview = Boolean.valueOf(options.getArguments().getOption(ProgramOptionConstants.IS_PREVIEW, "false"));
// for preview we already have a resolved app spec, so no need to regenerate the app spec again
if (!isPreview && appSpec != null && ClusterMode.ON_PREMISE.equals(clusterMode)) {
try {
ApplicationSpecification generatedAppSpec = regenerateAppSpec(artifactDetail, programId, artifactId, appSpec, options);
appSpec = generatedAppSpec != null ? generatedAppSpec : appSpec;
newProgramDescriptor = new ProgramDescriptor(programDescriptor.getProgramId(), appSpec);
} catch (Exception e) {
LOG.warn("Failed to regenerate the app spec for program {}, using the existing app spec", programId);
}
}
ProgramOptions runtimeProgramOptions = updateProgramOptions(artifactId, programId, options, runId, clusterMode, Iterables.getFirst(artifactDetail.getMeta().getClasses().getApps(), null));
// Take a snapshot of all the plugin artifacts used by the program
ProgramOptions optionsWithPlugins = createPluginSnapshot(runtimeProgramOptions, programId, tempDir, newProgramDescriptor.getApplicationSpecification());
// Create and run the program
Program executableProgram = createProgram(cConf, runner, newProgramDescriptor, artifactDetail, tempDir);
cleanUpTaskRef.set(createCleanupTask(cleanUpTaskRef.get(), executableProgram));
controller.setProgramController(runner.run(executableProgram, optionsWithPlugins));
} catch (Exception e) {
controller.failed(e);
programStateWriter.error(programRunId, e);
LOG.error("Exception while trying to run program", e);
}
});
return runtimeInfo;
}
use of io.cdap.cdap.app.program.ProgramDescriptor in project cdap by caskdata.
the class ProvisioningServiceTest method createTaskInfo.
private TaskFields createTaskInfo(ProvisionerInfo provisionerInfo) {
ProgramRunId programRunId = NamespaceId.DEFAULT.app("app").workflow("wf").run(RunIds.generate());
Map<String, String> systemArgs = new HashMap<>();
Map<String, String> userArgs = new HashMap<>();
Profile profile = new Profile(ProfileId.NATIVE.getProfile(), "label", "desc", provisionerInfo);
SystemArguments.addProfileArgs(systemArgs, profile);
systemArgs.put(Constants.APP_CDAP_VERSION, APP_CDAP_VERSION);
ProgramOptions programOptions = new SimpleProgramOptions(programRunId.getParent(), new BasicArguments(systemArgs), new BasicArguments(userArgs));
ArtifactId artifactId = NamespaceId.DEFAULT.artifact("testArtifact", "1.0").toApiArtifactId();
ApplicationSpecification appSpec = new DefaultApplicationSpecification("name", "1.0.0", APP_CDAP_VERSION, "desc", null, artifactId, Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap(), Collections.emptyMap());
ProgramDescriptor programDescriptor = new ProgramDescriptor(programRunId.getParent(), appSpec);
return new TaskFields(programDescriptor, programOptions, programRunId);
}
Aggregations