use of co.cask.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
* @return the copy of the program options with RunId included in them
*/
private ProgramOptions updateProgramOptions(ProgramId programId, ProgramOptions options, RunId runId) {
// Build the system arguments
ImmutableMap.Builder<String, String> builder = ImmutableMap.builder();
builder.putAll(options.getArguments().asMap());
builder.putAll(getExtraProgramOptions());
builder.put(ProgramOptionConstants.RUN_ID, runId.getId());
// 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.getName(), new BasicArguments(builder.build()), new BasicArguments(userArguments), options.isDebug());
}
use of co.cask.cdap.internal.app.runtime.SimpleProgramOptions in project cdap by caskdata.
the class AbstractInMemoryProgramRunner method createComponentOptions.
private ProgramOptions createComponentOptions(String name, 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(name, new BasicArguments(systemOptions), options.getUserArguments());
}
use of co.cask.cdap.internal.app.runtime.SimpleProgramOptions in project cdap by caskdata.
the class CoreSchedulerServiceTest method testProgramEvents.
@Test
@Category(XSlowTests.class)
public void testProgramEvents() throws Exception {
// Deploy the app
deploy(AppWithMultipleSchedules.class);
CConfiguration cConf = getInjector().getInstance(CConfiguration.class);
TopicId programEventTopic = NamespaceId.SYSTEM.topic(cConf.get(Constants.AppFabric.PROGRAM_STATUS_RECORD_EVENT_TOPIC));
ProgramStateWriter programStateWriter = new MessagingProgramStateWriter(cConf, messagingService);
// These notifications should not trigger the program
ProgramRunId anotherWorkflowRun = ANOTHER_WORKFLOW.run(RunIds.generate());
programStateWriter.start(anotherWorkflowRun, new SimpleProgramOptions(anotherWorkflowRun.getParent()), null);
programStateWriter.running(anotherWorkflowRun, null);
long lastProcessed = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
programStateWriter.error(anotherWorkflowRun, null);
waitUntilProcessed(programEventTopic, lastProcessed);
ProgramRunId someWorkflowRun = SOME_WORKFLOW.run(RunIds.generate());
programStateWriter.start(someWorkflowRun, new SimpleProgramOptions(someWorkflowRun.getParent()), null);
programStateWriter.running(someWorkflowRun, null);
lastProcessed = TimeUnit.MILLISECONDS.toSeconds(System.currentTimeMillis());
programStateWriter.killed(someWorkflowRun);
waitUntilProcessed(programEventTopic, lastProcessed);
Assert.assertEquals(0, getRuns(TRIGGERED_WORKFLOW, ProgramRunStatus.ALL));
// Enable the schedule
scheduler.enableSchedule(APP_MULT_ID.schedule(AppWithMultipleSchedules.WORKFLOW_COMPLETED_SCHEDULE));
// Start a program with user arguments
startProgram(ANOTHER_WORKFLOW, ImmutableMap.of(AppWithMultipleSchedules.ANOTHER_RUNTIME_ARG_KEY, AppWithMultipleSchedules.ANOTHER_RUNTIME_ARG_VALUE), 200);
// Wait for a completed run record
waitForCompleteRuns(1, TRIGGERED_WORKFLOW);
assertProgramRuns(TRIGGERED_WORKFLOW, ProgramRunStatus.COMPLETED, 1);
RunRecord run = getProgramRuns(TRIGGERED_WORKFLOW, ProgramRunStatus.COMPLETED).get(0);
Map<String, List<WorkflowTokenDetail.NodeValueDetail>> tokenData = getWorkflowToken(TRIGGERED_WORKFLOW, run.getPid(), null, null).getTokenData();
// There should be 2 entries in tokenData
Assert.assertEquals(2, tokenData.size());
// The value of TRIGGERED_RUNTIME_ARG_KEY should be ANOTHER_RUNTIME_ARG_VALUE from the triggering workflow
Assert.assertEquals(AppWithMultipleSchedules.ANOTHER_RUNTIME_ARG_VALUE, tokenData.get(AppWithMultipleSchedules.TRIGGERED_RUNTIME_ARG_KEY).get(0).getValue());
// The value of TRIGGERED_TOKEN_KEY should be ANOTHER_TOKEN_VALUE from the triggering workflow
Assert.assertEquals(AppWithMultipleSchedules.ANOTHER_TOKEN_VALUE, tokenData.get(AppWithMultipleSchedules.TRIGGERED_TOKEN_KEY).get(0).getValue());
}
use of co.cask.cdap.internal.app.runtime.SimpleProgramOptions in project cdap by caskdata.
the class ProgramOptionsCodec method deserialize.
@Override
public ProgramOptions deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
JsonObject jsonObj = json.getAsJsonObject();
ProgramId programId = context.deserialize(jsonObj.get("programId"), ProgramId.class);
Arguments arguments = context.deserialize(jsonObj.get("arguments"), Arguments.class);
Arguments userArguments = context.deserialize(jsonObj.get("userArguments"), Arguments.class);
boolean debug = jsonObj.get("debug").getAsBoolean();
return new SimpleProgramOptions(programId, arguments, userArguments, debug);
}
use of co.cask.cdap.internal.app.runtime.SimpleProgramOptions in project cdap by caskdata.
the class AbstractProgramTwillRunnable method createProgramOptions.
/**
* Creates program options. It contains program and user arguments as passed form the distributed program runner.
* Extra program arguments are inserted based on the environment information (e.g. host, instance id). Also all
* configs available through the TwillRunnable configs are also available through program arguments.
*/
private ProgramOptions createProgramOptions(CommandLine cmdLine, TwillContext context, Map<String, String> configs) {
ProgramOptions original = GSON.fromJson(cmdLine.getOptionValue(RunnableOptions.PROGRAM_OPTIONS), ProgramOptions.class);
// Overwrite them with environmental information
Map<String, String> arguments = Maps.newHashMap(original.getArguments().asMap());
arguments.put(ProgramOptionConstants.INSTANCE_ID, Integer.toString(context.getInstanceId()));
arguments.put(ProgramOptionConstants.INSTANCES, Integer.toString(context.getInstanceCount()));
arguments.put(ProgramOptionConstants.TWILL_RUN_ID, context.getApplicationRunId().getId());
arguments.put(ProgramOptionConstants.HOST, context.getHost().getCanonicalHostName());
arguments.putAll(configs);
// Use the name passed in by the constructor as the program name to construct the ProgramId
ProgramId originalProgramId = original.getProgramId();
ProgramId runnableProgramId = originalProgramId.getParent().program(originalProgramId.getType(), name);
return new SimpleProgramOptions(runnableProgramId, new BasicArguments(arguments), resolveScope(original.getUserArguments()), original.isDebug());
}
Aggregations