Search in sources :

Example 1 with SimpleProgramOptions

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());
}
Also used : SimpleProgramOptions(co.cask.cdap.internal.app.runtime.SimpleProgramOptions) BasicArguments(co.cask.cdap.internal.app.runtime.BasicArguments) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with SimpleProgramOptions

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());
}
Also used : SimpleProgramOptions(co.cask.cdap.internal.app.runtime.SimpleProgramOptions) BasicArguments(co.cask.cdap.internal.app.runtime.BasicArguments)

Example 3 with SimpleProgramOptions

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());
}
Also used : RunRecord(co.cask.cdap.proto.RunRecord) MessagingProgramStateWriter(co.cask.cdap.internal.app.program.MessagingProgramStateWriter) ProgramStateWriter(co.cask.cdap.app.runtime.ProgramStateWriter) TopicId(co.cask.cdap.proto.id.TopicId) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) ProgramRunId(co.cask.cdap.proto.id.ProgramRunId) SimpleProgramOptions(co.cask.cdap.internal.app.runtime.SimpleProgramOptions) MessagingProgramStateWriter(co.cask.cdap.internal.app.program.MessagingProgramStateWriter) CConfiguration(co.cask.cdap.common.conf.CConfiguration) WorkflowTokenDetail(co.cask.cdap.proto.WorkflowTokenDetail) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 4 with SimpleProgramOptions

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);
}
Also used : Arguments(co.cask.cdap.app.runtime.Arguments) JsonObject(com.google.gson.JsonObject) SimpleProgramOptions(co.cask.cdap.internal.app.runtime.SimpleProgramOptions) ProgramId(co.cask.cdap.proto.id.ProgramId)

Example 5 with SimpleProgramOptions

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());
}
Also used : SimpleProgramOptions(co.cask.cdap.internal.app.runtime.SimpleProgramOptions) BasicArguments(co.cask.cdap.internal.app.runtime.BasicArguments) ProgramId(co.cask.cdap.proto.id.ProgramId) SimpleProgramOptions(co.cask.cdap.internal.app.runtime.SimpleProgramOptions) ProgramOptions(co.cask.cdap.app.runtime.ProgramOptions)

Aggregations

SimpleProgramOptions (co.cask.cdap.internal.app.runtime.SimpleProgramOptions)19 BasicArguments (co.cask.cdap.internal.app.runtime.BasicArguments)15 ProgramOptions (co.cask.cdap.app.runtime.ProgramOptions)5 CConfiguration (co.cask.cdap.common.conf.CConfiguration)5 ProgramId (co.cask.cdap.proto.id.ProgramId)5 Program (co.cask.cdap.app.program.Program)4 ProgramDescriptor (co.cask.cdap.app.program.ProgramDescriptor)4 File (java.io.File)4 Test (org.junit.Test)4 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)3 ProgramRunId (co.cask.cdap.proto.id.ProgramRunId)3 ImmutableMap (com.google.common.collect.ImmutableMap)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 ExecutionException (java.util.concurrent.ExecutionException)3 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)2 Arguments (co.cask.cdap.app.runtime.Arguments)2 ProgramStateWriter (co.cask.cdap.app.runtime.ProgramStateWriter)2 DatasetFramework (co.cask.cdap.data2.dataset2.DatasetFramework)2 ArtifactDescriptor (co.cask.cdap.internal.app.runtime.artifact.ArtifactDescriptor)2