Search in sources :

Example 1 with SimpleProgramOptions

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());
}
Also used : HashMap(java.util.HashMap) SimpleProgramOptions(io.cdap.cdap.internal.app.runtime.SimpleProgramOptions) BasicArguments(io.cdap.cdap.internal.app.runtime.BasicArguments) SimpleProgramOptions(io.cdap.cdap.internal.app.runtime.SimpleProgramOptions) ProgramOptions(io.cdap.cdap.app.runtime.ProgramOptions)

Example 2 with SimpleProgramOptions

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());
}
Also used : HashMap(java.util.HashMap) FileOutputStream(java.io.FileOutputStream) Arguments(io.cdap.cdap.app.runtime.Arguments) SystemArguments(io.cdap.cdap.internal.app.runtime.SystemArguments) BasicArguments(io.cdap.cdap.internal.app.runtime.BasicArguments) JarOutputStream(java.util.jar.JarOutputStream) SimpleProgramOptions(io.cdap.cdap.internal.app.runtime.SimpleProgramOptions) BasicArguments(io.cdap.cdap.internal.app.runtime.BasicArguments) File(java.io.File)

Example 3 with SimpleProgramOptions

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

Example 4 with SimpleProgramOptions

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);
}
Also used : Node(io.cdap.cdap.runtime.spi.provisioner.Node) Arguments(io.cdap.cdap.app.runtime.Arguments) SystemArguments(io.cdap.cdap.internal.app.runtime.SystemArguments) BasicArguments(io.cdap.cdap.internal.app.runtime.BasicArguments) Cluster(io.cdap.cdap.runtime.spi.provisioner.Cluster) CConfiguration(io.cdap.cdap.common.conf.CConfiguration) LocalLocationFactory(org.apache.twill.filesystem.LocalLocationFactory) LocationFactory(org.apache.twill.filesystem.LocationFactory) NoopTwillRunnerService(io.cdap.cdap.common.twill.NoopTwillRunnerService) Injector(com.google.inject.Injector) RuntimeJobEnvironment(io.cdap.cdap.runtime.spi.runtimejob.RuntimeJobEnvironment) BasicArguments(io.cdap.cdap.internal.app.runtime.BasicArguments) ProgramRunId(io.cdap.cdap.proto.id.ProgramRunId) SimpleProgramOptions(io.cdap.cdap.internal.app.runtime.SimpleProgramOptions) LocalLocationFactory(org.apache.twill.filesystem.LocalLocationFactory) Test(org.junit.Test)

Example 5 with SimpleProgramOptions

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());
}
Also used : HashMap(java.util.HashMap) SimpleProgramOptions(io.cdap.cdap.internal.app.runtime.SimpleProgramOptions) BasicArguments(io.cdap.cdap.internal.app.runtime.BasicArguments) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) HashMap(java.util.HashMap)

Aggregations

SimpleProgramOptions (io.cdap.cdap.internal.app.runtime.SimpleProgramOptions)30 BasicArguments (io.cdap.cdap.internal.app.runtime.BasicArguments)28 ProgramOptions (io.cdap.cdap.app.runtime.ProgramOptions)18 ProgramDescriptor (io.cdap.cdap.app.program.ProgramDescriptor)15 ProgramRunId (io.cdap.cdap.proto.id.ProgramRunId)15 HashMap (java.util.HashMap)15 CConfiguration (io.cdap.cdap.common.conf.CConfiguration)12 ProgramId (io.cdap.cdap.proto.id.ProgramId)11 Test (org.junit.Test)10 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)9 Map (java.util.Map)9 Program (io.cdap.cdap.app.program.Program)8 SystemArguments (io.cdap.cdap.internal.app.runtime.SystemArguments)8 ImmutableMap (com.google.common.collect.ImmutableMap)6 ProgramStateWriter (io.cdap.cdap.app.runtime.ProgramStateWriter)6 Constants (io.cdap.cdap.common.conf.Constants)6 IOException (java.io.IOException)6 Collections (java.util.Collections)6 List (java.util.List)6 Injector (com.google.inject.Injector)5