use of io.cdap.cdap.internal.app.runtime.SimpleProgramOptions in project cdap by caskdata.
the class AbstractProgramTwillRunnable method createProgramOptions.
/**
* Creates a {@link ProgramOptions} by deserializing the given json file.
*/
private ProgramOptions createProgramOptions(File programOptionsFile) throws IOException {
ProgramOptions original = readJsonFile(programOptionsFile, ProgramOptions.class);
// Overwrite them with environmental information
Map<String, String> arguments = new HashMap<>(original.getArguments().asMap());
arguments.putAll(getExtraSystemArguments());
// Use the name passed in by the constructor as the program name to construct the ProgramId
return new SimpleProgramOptions(original.getProgramId(), new BasicArguments(arguments), original.getUserArguments(), original.isDebug());
}
use of io.cdap.cdap.internal.app.runtime.SimpleProgramOptions in project cdap by caskdata.
the class DistributedProgramRunner method updateProgramOptions.
/**
* Creates a new instance of {@link ProgramOptions} with artifact localization information and with
* extra system arguments, while maintaining other fields of the given {@link ProgramOptions}.
*
* @param options the original {@link ProgramOptions}.
* @param localizeResources a {@link Map} of {@link LocalizeResource} to be localized to the remote container
* @param tempDir a local temporary directory for creating files for artifact localization.
* @param extraSystemArgs a set of extra system arguments to be added/updated
* @return a new instance of {@link ProgramOptions}
* @throws IOException if failed to create local copy of artifact files
*/
private ProgramOptions updateProgramOptions(ProgramOptions options, Map<String, LocalizeResource> localizeResources, File tempDir, Map<String, String> extraSystemArgs) throws IOException {
Arguments systemArgs = options.getArguments();
Map<String, String> newSystemArgs = new HashMap<>(systemArgs.asMap());
newSystemArgs.putAll(extraSystemArgs);
if (systemArgs.hasOption(ProgramOptionConstants.PLUGIN_ARCHIVE)) {
// If the archive already exists locally, we just need to re-localize it to remote containers
File archiveFile = new File(systemArgs.getOption(ProgramOptionConstants.PLUGIN_ARCHIVE));
// Localize plugins to two files, one expanded into a directory, one not.
localizeResources.put(PLUGIN_DIR, new LocalizeResource(archiveFile, true));
localizeResources.put(PLUGIN_ARCHIVE, new LocalizeResource(archiveFile, false));
} else if (systemArgs.hasOption(ProgramOptionConstants.PLUGIN_DIR)) {
// If there is a plugin directory, then we need to create an archive and localize it to remote containers
File localDir = new File(systemArgs.getOption(ProgramOptionConstants.PLUGIN_DIR));
File archiveFile = new File(tempDir, PLUGIN_DIR + ".jar");
// Store all artifact jars into a new jar file for localization without compression
try (JarOutputStream jarOut = new JarOutputStream(new FileOutputStream(archiveFile))) {
jarOut.setLevel(0);
BundleJarUtil.addToArchive(localDir, jarOut);
}
// Localize plugins to two files, one expanded into a directory, one not.
localizeResources.put(PLUGIN_DIR, new LocalizeResource(archiveFile, true));
localizeResources.put(PLUGIN_ARCHIVE, new LocalizeResource(archiveFile, false));
}
// Add/rename the entries in the system arguments
if (localizeResources.containsKey(PLUGIN_DIR)) {
newSystemArgs.put(ProgramOptionConstants.PLUGIN_DIR, PLUGIN_DIR);
}
if (localizeResources.containsKey(PLUGIN_ARCHIVE)) {
newSystemArgs.put(ProgramOptionConstants.PLUGIN_ARCHIVE, PLUGIN_ARCHIVE);
}
return new SimpleProgramOptions(options.getProgramId(), new BasicArguments(newSystemArgs), options.getUserArguments(), options.isDebug());
}
use of io.cdap.cdap.internal.app.runtime.SimpleProgramOptions in project cdap by caskdata.
the class AbstractInMemoryProgramRunner method createComponentOptions.
private ProgramOptions createComponentOptions(int instanceId, int instances, RunId runId, ProgramOptions options) {
Map<String, String> systemOptions = Maps.newHashMap();
systemOptions.putAll(options.getArguments().asMap());
systemOptions.put(ProgramOptionConstants.INSTANCE_ID, Integer.toString(instanceId));
systemOptions.put(ProgramOptionConstants.INSTANCES, Integer.toString(instances));
systemOptions.put(ProgramOptionConstants.RUN_ID, runId.getId());
systemOptions.put(ProgramOptionConstants.HOST, host);
return new SimpleProgramOptions(options.getProgramId(), new BasicArguments(systemOptions), options.getUserArguments());
}
use of io.cdap.cdap.internal.app.runtime.SimpleProgramOptions in project cdap by caskdata.
the class DefaultRuntimeJobTest method testInjector.
@Test
public void testInjector() throws Exception {
CConfiguration cConf = CConfiguration.create();
cConf.set(Constants.CFG_LOCAL_DATA_DIR, TEMP_FOLDER.newFolder().toString());
LocationFactory locationFactory = new LocalLocationFactory(TEMP_FOLDER.newFile());
DefaultRuntimeJob defaultRuntimeJob = new DefaultRuntimeJob();
Arguments systemArgs = new BasicArguments(Collections.singletonMap(SystemArguments.PROFILE_NAME, "test"));
Node node = new Node("test", Node.Type.MASTER, "127.0.0.1", System.currentTimeMillis(), Collections.emptyMap());
Cluster cluster = new Cluster("test", ClusterStatus.RUNNING, Collections.singleton(node), Collections.emptyMap());
ProgramRunId programRunId = NamespaceId.DEFAULT.app("app").workflow("workflow").run(RunIds.generate());
SimpleProgramOptions programOpts = new SimpleProgramOptions(programRunId.getParent(), systemArgs, new BasicArguments());
Injector injector = Guice.createInjector(defaultRuntimeJob.createModules(new RuntimeJobEnvironment() {
@Override
public LocationFactory getLocationFactory() {
return locationFactory;
}
@Override
public TwillRunner getTwillRunner() {
return new NoopTwillRunnerService();
}
@Override
public Map<String, String> getProperties() {
return Collections.emptyMap();
}
}, cConf, programRunId, programOpts));
injector.getInstance(LogAppenderInitializer.class);
defaultRuntimeJob.createCoreServices(injector, systemArgs, cluster);
}
use of io.cdap.cdap.internal.app.runtime.SimpleProgramOptions in project cdap by caskdata.
the class AbstractProgramRuntimeService method updateProgramOptions.
/**
* Updates the given {@link ProgramOptions} and return a new instance.
* It copies the {@link ProgramOptions}. Then it adds all entries returned by {@link #getExtraProgramOptions()}
* followed by adding the {@link RunId} to the system arguments.
*
* Also scope resolution will be performed on the user arguments on the application and program.
*
* @param programId the program id
* @param options The {@link ProgramOptions} in which the RunId to be included
* @param runId The RunId to be included
* @param clusterMode clustermode for the program run
* @param applicationClass application class for the program
* @return the copy of the program options with RunId included in them
*/
private ProgramOptions updateProgramOptions(ArtifactId artifactId, ProgramId programId, ProgramOptions options, RunId runId, ClusterMode clusterMode, ApplicationClass applicationClass) {
// Build the system arguments
Map<String, String> systemArguments = new HashMap<>(options.getArguments().asMap());
// this can happen if this is a program within a workflow, and the workflow already added these arguments
for (Map.Entry<String, String> extraOption : getExtraProgramOptions().entrySet()) {
systemArguments.putIfAbsent(extraOption.getKey(), extraOption.getValue());
}
systemArguments.putIfAbsent(ProgramOptionConstants.RUN_ID, runId.getId());
systemArguments.putIfAbsent(ProgramOptionConstants.ARTIFACT_ID, Joiner.on(':').join(artifactId.toIdParts()));
if (clusterMode == ClusterMode.ISOLATED) {
systemArguments.putIfAbsent(ProgramOptionConstants.APPLICATION_CLASS, applicationClass.getClassName());
}
// Resolves the user arguments
// First resolves at the cluster scope if the cluster.name is not empty
String clusterName = options.getArguments().getOption(Constants.CLUSTER_NAME);
Map<String, String> userArguments = options.getUserArguments().asMap();
if (!Strings.isNullOrEmpty(clusterName)) {
userArguments = RuntimeArguments.extractScope(CLUSTER_SCOPE, clusterName, userArguments);
}
// Then resolves at the application scope
userArguments = RuntimeArguments.extractScope(APPLICATION_SCOPE, programId.getApplication(), userArguments);
// Then resolves at the program level
userArguments = RuntimeArguments.extractScope(programId.getType().getScope(), programId.getProgram(), userArguments);
return new SimpleProgramOptions(options.getProgramId(), new BasicArguments(systemArguments), new BasicArguments(userArguments), options.isDebug());
}
Aggregations