use of io.cdap.cdap.internal.app.runtime.BasicArguments in project cdap by cdapio.
the class ProgramLifecycleService method createProgramOptions.
@VisibleForTesting
ProgramOptions createProgramOptions(ProgramId programId, Map<String, String> userArgs, Map<String, String> sysArgs, boolean debug) throws NotFoundException, ProfileConflictException {
ProfileId profileId = SystemArguments.getProfileIdForProgram(programId, userArgs);
Map<String, String> profileProperties = SystemArguments.getProfileProperties(userArgs);
Profile profile = profileService.getProfile(profileId, profileProperties);
if (profile.getStatus() == ProfileStatus.DISABLED) {
throw new ProfileConflictException(String.format("Profile %s in namespace %s is disabled. It cannot be " + "used to start the program %s", profileId.getProfile(), profileId.getNamespace(), programId.toString()), profileId);
}
ProvisionerDetail spec = provisioningService.getProvisionerDetail(profile.getProvisioner().getName());
if (spec == null) {
throw new NotFoundException(String.format("Provisioner '%s' not found.", profile.getProvisioner().getName()));
}
// get and add any user overrides for profile properties
Map<String, String> systemArgs = new HashMap<>(sysArgs);
// add profile properties to the system arguments
SystemArguments.addProfileArgs(systemArgs, profile);
// Set the ClusterMode. If it is NATIVE profile, then it is ON_PREMISE, otherwise is ISOLATED
// This should probably move into the provisioner later once we have a better contract for the
// provisioner to actually pick what launching mechanism it wants to use.
systemArgs.put(ProgramOptionConstants.CLUSTER_MODE, (ProfileId.NATIVE.equals(profileId) ? ClusterMode.ON_PREMISE : ClusterMode.ISOLATED).name());
ProgramSpecification programSpecification = getProgramSpecificationWithoutAuthz(programId);
if (programSpecification == null) {
throw new NotFoundException(programId);
}
addAppCDAPVersion(programId, systemArgs);
// put all the plugin requirements if any involved in the run
systemArgs.put(ProgramOptionConstants.PLUGIN_REQUIREMENTS, GSON.toJson(getPluginRequirements(programSpecification)));
return new SimpleProgramOptions(programId, new BasicArguments(systemArgs), new BasicArguments(userArgs), debug);
}
use of io.cdap.cdap.internal.app.runtime.BasicArguments in project cdap by cdapio.
the class DefaultProgramWorkflowRunner method getProgramRunnable.
/**
* Gets a {@link Runnable} for the {@link Program}.
*
* @param name name of the {@link Program}
* @param program the {@link Program}
* @return a {@link Runnable} for this {@link Program}
*/
private Runnable getProgramRunnable(String name, final ProgramRunner programRunner, final Program program) {
Map<String, String> systemArgumentsMap = new HashMap<>(workflowProgramOptions.getArguments().asMap());
// Generate the new RunId here for the program running under Workflow
systemArgumentsMap.put(ProgramOptionConstants.RUN_ID, RunIds.generate().getId());
// Add Workflow specific system arguments to be passed to the underlying program
systemArgumentsMap.put(ProgramOptionConstants.WORKFLOW_NAME, workflowSpec.getName());
systemArgumentsMap.put(ProgramOptionConstants.WORKFLOW_RUN_ID, ProgramRunners.getRunId(workflowProgramOptions).getId());
systemArgumentsMap.put(ProgramOptionConstants.WORKFLOW_NODE_ID, nodeId);
systemArgumentsMap.put(ProgramOptionConstants.PROGRAM_NAME_IN_WORKFLOW, name);
systemArgumentsMap.put(ProgramOptionConstants.WORKFLOW_TOKEN, GSON.toJson(token));
final ProgramOptions options = new SimpleProgramOptions(program.getId(), new BasicArguments(Collections.unmodifiableMap(systemArgumentsMap)), new BasicArguments(RuntimeArguments.extractScope(program.getType().getScope(), name, workflowProgramOptions.getUserArguments().asMap())));
return new Runnable() {
@Override
public void run() {
try {
runAndWait(programRunner, program, options);
} catch (Exception e) {
throw Throwables.propagate(e);
}
}
};
}
use of io.cdap.cdap.internal.app.runtime.BasicArguments in project cdap by cdapio.
the class MapReduceProgramRunnerTest method testMapreduceWithFile.
private void testMapreduceWithFile(String inputDatasetName, String inputPaths, String outputDatasetName, String outputPath, Class appClass, Class mrClass, Map<String, String> extraRuntimeArgs, @Nullable final String counterTableName, @Nullable final String outputSeparator) throws Exception {
final ApplicationWithPrograms app = deployApp(appClass, new AppWithMapReduceUsingFileSet.AppConfig(inputDatasetName, outputDatasetName));
Map<String, String> runtimeArguments = Maps.newHashMap();
Map<String, String> inputArgs = Maps.newHashMap();
Map<String, String> outputArgs = Maps.newHashMap();
FileSetArguments.setInputPaths(inputArgs, inputPaths);
FileSetArguments.setOutputPath(outputArgs, outputPath);
if (outputSeparator != null) {
outputArgs.put(FileSetProperties.OUTPUT_PROPERTIES_PREFIX + TextOutputFormat.SEPERATOR, "#");
}
runtimeArguments.putAll(RuntimeArguments.addScope(Scope.DATASET, inputDatasetName, inputArgs));
runtimeArguments.putAll(RuntimeArguments.addScope(Scope.DATASET, outputDatasetName, outputArgs));
if (extraRuntimeArgs != null) {
runtimeArguments.putAll(extraRuntimeArgs);
}
// clear the counters in case a previous test case left behind some values
if (counterTableName != null) {
Transactions.execute(datasetCache.newTransactionContext(), "countersVerify", () -> {
KeyValueTable counters = datasetCache.getDataset(counterTableName);
counters.delete(AppWithMapReduceUsingRuntimeDatasets.INPUT_RECORDS);
counters.delete(AppWithMapReduceUsingRuntimeDatasets.REDUCE_KEYS);
});
}
// write a handful of numbers to a file; compute their sum, too.
final long[] values = { 15L, 17L, 7L, 3L };
final FileSet input = datasetCache.getDataset(inputDatasetName, inputArgs);
long sum = 0L, count = 1;
long inputRecords = 0;
for (Location inputLocation : input.getInputLocations()) {
final PrintWriter writer = new PrintWriter(inputLocation.getOutputStream());
for (long value : values) {
value *= count;
writer.println(value);
sum += value;
inputRecords++;
}
writer.close();
count++;
}
runProgram(app, mrClass, new BasicArguments(runtimeArguments));
// output location in file system is a directory that contains a part file, a _SUCCESS file, and checksums
// (.<filename>.crc) for these files. Find the actual part file. Its name begins with "part". In this case,
// there should be only one part file (with this small data, we have a single reducer).
final FileSet results = datasetCache.getDataset(outputDatasetName, outputArgs);
Location resultLocation = results.getOutputLocation();
if (resultLocation.isDirectory()) {
for (Location child : resultLocation.list()) {
if (!child.isDirectory() && child.getName().startsWith("part")) {
resultLocation = child;
break;
}
}
}
Assert.assertFalse(resultLocation.isDirectory());
// read output and verify result
String line = CharStreams.readFirstLine(CharStreams.newReaderSupplier(Locations.newInputSupplier(resultLocation), Charsets.UTF_8));
Assert.assertNotNull(line);
String[] fields = line.split(outputSeparator == null ? ":" : outputSeparator);
Assert.assertEquals(2, fields.length);
Assert.assertEquals(AppWithMapReduceUsingFileSet.FileMapper.ONLY_KEY, fields[0]);
Assert.assertEquals(sum, Long.parseLong(fields[1]));
if (counterTableName != null) {
final long totalInputRecords = inputRecords;
Transactions.execute(datasetCache.newTransactionContext(), "countersVerify", () -> {
KeyValueTable counters = datasetCache.getDataset(counterTableName);
Assert.assertEquals(totalInputRecords, counters.incrementAndGet(AppWithMapReduceUsingRuntimeDatasets.INPUT_RECORDS, 0L));
Assert.assertEquals(1L, counters.incrementAndGet(AppWithMapReduceUsingRuntimeDatasets.REDUCE_KEYS, 0L));
});
}
}
use of io.cdap.cdap.internal.app.runtime.BasicArguments in project cdap by cdapio.
the class MapReduceProgramRunnerTest method testTransactionHandling.
/**
* Tests that initialize() and getSplits() are called in the same transaction,
* and with the same instance of the input dataset.
*/
@Test
public void testTransactionHandling() throws Exception {
final ApplicationWithPrograms app = deployApp(AppWithTxAware.class);
runProgram(app, AppWithTxAware.PedanticMapReduce.class, new BasicArguments(ImmutableMap.of("outputPath", TEMP_FOLDER_SUPPLIER.get().getPath() + "/output")));
}
use of io.cdap.cdap.internal.app.runtime.BasicArguments in project cdap by cdapio.
the class MapReduceProgramRunnerTest method testMapReduceWithLocalFiles.
@Test
public void testMapReduceWithLocalFiles() throws Exception {
ApplicationWithPrograms appWithPrograms = deployApp(AppWithLocalFiles.class);
URI stopWordsFile = createStopWordsFile();
final KeyValueTable kvTable = datasetCache.getDataset(AppWithLocalFiles.MR_INPUT_DATASET);
Transactions.createTransactionExecutor(txExecutorFactory, kvTable).execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() {
kvTable.write("2324", "a test record");
kvTable.write("43353", "the test table");
kvTable.write("34335", "an end record");
}
});
runProgram(appWithPrograms, AppWithLocalFiles.MapReduceWithLocalFiles.class, new BasicArguments(ImmutableMap.of(AppWithLocalFiles.MR_INPUT_DATASET, "input", AppWithLocalFiles.MR_OUTPUT_DATASET, "output", AppWithLocalFiles.STOPWORDS_FILE_ARG, stopWordsFile.toString())));
final KeyValueTable outputKvTable = datasetCache.getDataset(AppWithLocalFiles.MR_OUTPUT_DATASET);
Transactions.createTransactionExecutor(txExecutorFactory, outputKvTable).execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() {
Assert.assertNull(outputKvTable.read("a"));
Assert.assertNull(outputKvTable.read("the"));
Assert.assertNull(outputKvTable.read("an"));
Assert.assertEquals(2, Bytes.toInt(outputKvTable.read("test")));
Assert.assertEquals(2, Bytes.toInt(outputKvTable.read("record")));
Assert.assertEquals(1, Bytes.toInt(outputKvTable.read("table")));
Assert.assertEquals(1, Bytes.toInt(outputKvTable.read("end")));
}
});
}
Aggregations