Search in sources :

Example 16 with SimpleProgramOptions

use of co.cask.cdap.internal.app.runtime.SimpleProgramOptions in project cdap by caskdata.

the class InMemoryFlowProgramRunner method createFlowletOptions.

private ProgramOptions createFlowletOptions(int instanceId, int instances, ProgramOptions options) {
    Map<String, String> systemArgs = new HashMap<>();
    systemArgs.putAll(options.getArguments().asMap());
    systemArgs.put(ProgramOptionConstants.INSTANCE_ID, Integer.toString(instanceId));
    systemArgs.put(ProgramOptionConstants.INSTANCES, Integer.toString(instances));
    return new SimpleProgramOptions(options.getProgramId(), new BasicArguments(systemArgs), options.getUserArguments());
}
Also used : HashMap(java.util.HashMap) SimpleProgramOptions(co.cask.cdap.internal.app.runtime.SimpleProgramOptions) BasicArguments(co.cask.cdap.internal.app.runtime.BasicArguments)

Example 17 with SimpleProgramOptions

use of co.cask.cdap.internal.app.runtime.SimpleProgramOptions in project cdap by caskdata.

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

Example 18 with SimpleProgramOptions

use of co.cask.cdap.internal.app.runtime.SimpleProgramOptions in project cdap by caskdata.

the class AbstractProgramRuntimeServiceTest method testDeadlock.

@Test(timeout = 5000)
public void testDeadlock() throws IOException, ExecutionException, InterruptedException, TimeoutException {
    // This test is for testing condition in (CDAP-3579)
    // The race condition is if a program finished very fast such that inside the AbstractProgramRuntimeService is
    // still in the run method, it holds the object lock, making the callback from the listener block forever.
    ProgramRunnerFactory runnerFactory = createProgramRunnerFactory();
    final Program program = createDummyProgram();
    final ProgramRuntimeService runtimeService = new AbstractProgramRuntimeService(CConfiguration.create(), runnerFactory, null, new NoOpProgramStateWriter()) {

        @Override
        public ProgramLiveInfo getLiveInfo(ProgramId programId) {
            return new ProgramLiveInfo(programId, "runtime") {
            };
        }

        @Override
        protected Program createProgram(CConfiguration cConf, ProgramRunner programRunner, ProgramDescriptor programDescriptor, ArtifactDetail artifactDetail, File tempDir) throws IOException {
            return program;
        }

        @Override
        protected ArtifactDetail getArtifactDetail(ArtifactId artifactId) throws IOException, ArtifactNotFoundException {
            co.cask.cdap.api.artifact.ArtifactId id = new co.cask.cdap.api.artifact.ArtifactId("dummy", new ArtifactVersion("1.0"), ArtifactScope.USER);
            return new ArtifactDetail(new ArtifactDescriptor(id, Locations.toLocation(TEMP_FOLDER.newFile())), new ArtifactMeta(ArtifactClasses.builder().build()));
        }
    };
    runtimeService.startAndWait();
    try {
        ProgramDescriptor descriptor = new ProgramDescriptor(program.getId(), null, NamespaceId.DEFAULT.artifact("test", "1.0"));
        final ProgramController controller = runtimeService.run(descriptor, new SimpleProgramOptions(program.getId())).getController();
        Tasks.waitFor(ProgramController.State.COMPLETED, new Callable<ProgramController.State>() {

            @Override
            public ProgramController.State call() throws Exception {
                return controller.getState();
            }
        }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
        Tasks.waitFor(true, new Callable<Boolean>() {

            @Override
            public Boolean call() throws Exception {
                return runtimeService.list(ProgramType.WORKER).isEmpty();
            }
        }, 5, TimeUnit.SECONDS, 100, TimeUnit.MICROSECONDS);
    } finally {
        runtimeService.stopAndWait();
    }
}
Also used : ArtifactId(co.cask.cdap.proto.id.ArtifactId) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactDescriptor(co.cask.cdap.internal.app.runtime.artifact.ArtifactDescriptor) ProgramDescriptor(co.cask.cdap.app.program.ProgramDescriptor) ArtifactMeta(co.cask.cdap.internal.app.runtime.artifact.ArtifactMeta) Program(co.cask.cdap.app.program.Program) ProgramId(co.cask.cdap.proto.id.ProgramId) CConfiguration(co.cask.cdap.common.conf.CConfiguration) TimeoutException(java.util.concurrent.TimeoutException) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SimpleProgramOptions(co.cask.cdap.internal.app.runtime.SimpleProgramOptions) ProgramLiveInfo(co.cask.cdap.proto.ProgramLiveInfo) File(java.io.File) ArtifactDetail(co.cask.cdap.internal.app.runtime.artifact.ArtifactDetail) Test(org.junit.Test)

Example 19 with SimpleProgramOptions

use of co.cask.cdap.internal.app.runtime.SimpleProgramOptions in project cdap by caskdata.

the class AbstractProgramRuntimeServiceTest method testScopingRuntimeArguments.

@Test
public void testScopingRuntimeArguments() throws Exception {
    Map<ProgramId, Arguments> argumentsMap = new ConcurrentHashMap<>();
    ProgramRunnerFactory runnerFactory = createProgramRunnerFactory(argumentsMap);
    final Program program = createDummyProgram();
    final ProgramRuntimeService runtimeService = new AbstractProgramRuntimeService(CConfiguration.create(), runnerFactory, null, new NoOpProgramStateWriter()) {

        @Override
        public ProgramLiveInfo getLiveInfo(ProgramId programId) {
            return new ProgramLiveInfo(programId, "runtime") {
            };
        }

        @Override
        protected Program createProgram(CConfiguration cConf, ProgramRunner programRunner, ProgramDescriptor programDescriptor, ArtifactDetail artifactDetail, File tempDir) throws IOException {
            return program;
        }

        @Override
        protected ArtifactDetail getArtifactDetail(ArtifactId artifactId) throws IOException, ArtifactNotFoundException {
            co.cask.cdap.api.artifact.ArtifactId id = new co.cask.cdap.api.artifact.ArtifactId("dummy", new ArtifactVersion("1.0"), ArtifactScope.USER);
            return new ArtifactDetail(new ArtifactDescriptor(id, Locations.toLocation(TEMP_FOLDER.newFile())), new ArtifactMeta(ArtifactClasses.builder().build()));
        }
    };
    runtimeService.startAndWait();
    try {
        try {
            ProgramDescriptor descriptor = new ProgramDescriptor(program.getId(), null, NamespaceId.DEFAULT.artifact("test", "1.0"));
            // Set of scopes to test
            String programScope = program.getType().getScope();
            String clusterName = "c1";
            List<String> scopes = Arrays.asList("cluster.*.", "cluster." + clusterName + ".", "cluster." + clusterName + ".app.*.", "app.*.", "app." + program.getApplicationId() + ".", "app." + program.getApplicationId() + "." + programScope + ".*.", "app." + program.getApplicationId() + "." + programScope + "." + program.getName() + ".", programScope + ".*.", programScope + "." + program.getName() + ".", "");
            for (String scope : scopes) {
                ProgramOptions programOptions = new SimpleProgramOptions(program.getId(), new BasicArguments(Collections.singletonMap(Constants.CLUSTER_NAME, clusterName)), new BasicArguments(Collections.singletonMap(scope + "size", Integer.toString(scope.length()))));
                final ProgramController controller = runtimeService.run(descriptor, programOptions).getController();
                Tasks.waitFor(ProgramController.State.COMPLETED, new Callable<ProgramController.State>() {

                    @Override
                    public ProgramController.State call() throws Exception {
                        return controller.getState();
                    }
                }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
                // Should get an argument
                Arguments args = argumentsMap.get(program.getId());
                Assert.assertNotNull(args);
                Assert.assertEquals(scope.length(), Integer.parseInt(args.getOption("size")));
            }
        } finally {
            runtimeService.stopAndWait();
        }
    } finally {
        runtimeService.stopAndWait();
    }
}
Also used : ArtifactId(co.cask.cdap.proto.id.ArtifactId) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactDescriptor(co.cask.cdap.internal.app.runtime.artifact.ArtifactDescriptor) ProgramDescriptor(co.cask.cdap.app.program.ProgramDescriptor) BasicArguments(co.cask.cdap.internal.app.runtime.BasicArguments) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ArtifactMeta(co.cask.cdap.internal.app.runtime.artifact.ArtifactMeta) Program(co.cask.cdap.app.program.Program) BasicArguments(co.cask.cdap.internal.app.runtime.BasicArguments) ProgramId(co.cask.cdap.proto.id.ProgramId) CConfiguration(co.cask.cdap.common.conf.CConfiguration) SimpleProgramOptions(co.cask.cdap.internal.app.runtime.SimpleProgramOptions) TimeoutException(java.util.concurrent.TimeoutException) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) SimpleProgramOptions(co.cask.cdap.internal.app.runtime.SimpleProgramOptions) ProgramLiveInfo(co.cask.cdap.proto.ProgramLiveInfo) File(java.io.File) ArtifactDetail(co.cask.cdap.internal.app.runtime.artifact.ArtifactDetail) Test(org.junit.Test)

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