Search in sources :

Example 16 with ProgramDescriptor

use of co.cask.cdap.app.program.ProgramDescriptor 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)

Example 17 with ProgramDescriptor

use of co.cask.cdap.app.program.ProgramDescriptor in project cdap by caskdata.

the class DefaultStoreTest method testSetFlowletInstances.

@Test
public void testSetFlowletInstances() throws Exception {
    ApplicationSpecification spec = Specifications.from(new WordCountApp());
    int initialInstances = spec.getFlows().get("WordCountFlow").getFlowlets().get("StreamSource").getInstances();
    ApplicationId appId = NamespaceId.DEFAULT.app(spec.getName());
    store.addApplication(appId, spec);
    ProgramId programId = appId.flow("WordCountFlow");
    store.setFlowletInstances(programId, "StreamSource", initialInstances + 5);
    // checking that app spec in store was adjusted
    ApplicationSpecification adjustedSpec = store.getApplication(appId);
    Assert.assertNotNull(adjustedSpec);
    Assert.assertEquals(initialInstances + 5, adjustedSpec.getFlows().get("WordCountFlow").getFlowlets().get("StreamSource").getInstances());
    // checking that program spec in program jar was adjusted
    ProgramDescriptor descriptor = store.loadProgram(programId);
    Assert.assertNotNull(descriptor);
    Assert.assertEquals(initialInstances + 5, descriptor.getApplicationSpecification().getFlows().get("WordCountFlow").getFlowlets().get("StreamSource").getInstances());
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) WordCountApp(co.cask.cdap.WordCountApp) ProgramDescriptor(co.cask.cdap.app.program.ProgramDescriptor) ApplicationId(co.cask.cdap.proto.id.ApplicationId) ProgramId(co.cask.cdap.proto.id.ProgramId) Test(org.junit.Test)

Example 18 with ProgramDescriptor

use of co.cask.cdap.app.program.ProgramDescriptor in project cdap by caskdata.

the class DefaultStoreTest method testLoadingProgram.

@Test
public void testLoadingProgram() throws Exception {
    ApplicationSpecification appSpec = Specifications.from(new ToyApp());
    ApplicationId appId = NamespaceId.DEFAULT.app(appSpec.getName());
    store.addApplication(appId, appSpec);
    ProgramDescriptor descriptor = store.loadProgram(appId.flow("ToyFlow"));
    Assert.assertNotNull(descriptor);
    FlowSpecification flowSpec = descriptor.getSpecification();
    Assert.assertEquals("ToyFlow", flowSpec.getName());
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) FlowSpecification(co.cask.cdap.api.flow.FlowSpecification) ToyApp(co.cask.cdap.ToyApp) ProgramDescriptor(co.cask.cdap.app.program.ProgramDescriptor) ApplicationId(co.cask.cdap.proto.id.ApplicationId) Test(org.junit.Test)

Example 19 with ProgramDescriptor

use of co.cask.cdap.app.program.ProgramDescriptor in project cdap by caskdata.

the class FlowTest method testFlowPendingMetric.

@Test
public void testFlowPendingMetric() throws Exception {
    final ApplicationWithPrograms app = AppFabricTestHelper.deployApplicationWithManager(PendingMetricTestApp.class, TEMP_FOLDER_SUPPLIER);
    File tempFolder = TEMP_FOLDER_SUPPLIER.get();
    ProgramController controller = null;
    for (ProgramDescriptor programDescriptor : app.getPrograms()) {
        // running mapreduce is out of scope of this tests (there's separate unit-test for that)
        if (programDescriptor.getProgramId().getType() == ProgramType.FLOW) {
            Arguments args = new BasicArguments(ImmutableMap.of("temp", tempFolder.getAbsolutePath(), "count", "4"));
            controller = AppFabricTestHelper.submit(app, programDescriptor.getSpecification().getClassName(), args, TEMP_FOLDER_SUPPLIER);
        }
    }
    Assert.assertNotNull(controller);
    Map<String, String> tagsForSourceToOne = metricTagsForQueue("source", "ints", "forward-one");
    Map<String, String> tagsForSourceToTwo = metricTagsForQueue("source", null, "forward-two");
    Map<String, String> tagsForSourceToTwoInts = metricTagsForQueue("source", "ints", "forward-two");
    Map<String, String> tagsForSourceToTwoStrings = metricTagsForQueue("source", "strings", "forward-two");
    Map<String, String> tagsForOneToSink = metricTagsForQueue("forward-one", "queue", "sink");
    Map<String, String> tagsForTwoToSink = metricTagsForQueue("forward-two", "queue", "sink");
    Map<String, String> tagsForAllToOne = metricTagsForQueue(null, null, "forward-one");
    Map<String, String> tagsForAllToTwo = metricTagsForQueue(null, null, "forward-two");
    Map<String, String> tagsForAllToSink = metricTagsForQueue(null, null, "sink");
    Map<String, String> tagsForAll = metricTagsForQueue(null, null, null);
    try {
        // source emits 4, then forward-one reads 1, hence 3 should be pending
        // wait a little longer as flow needs to start
        waitForPending(tagsForSourceToOne, 3, 5000);
        // wait a little longer as flow needs to start
        waitForPending(tagsForAllToOne, 3, 100);
        // forward-two receives each of the 4 as a string and an int, but could have read 1 at most per each queue
        // so there should be either 3 + 4 = 7 pending or 3 + 3 = 6 pending, or 4 + 4 = 8 pending
        // but we don't know whether the queue pending count will be 4, 3 or 3, 4 or 3, 3 or 4, 4
        long intPending = waitForPending(tagsForSourceToTwoInts, 3, 4L, 1000);
        long stringPending = waitForPending(tagsForSourceToTwoStrings, 3, 4L, 1000);
        long totalPending = intPending + stringPending;
        Assert.assertTrue(String.format("Expected the pending events count to be 6, 7 or 8. But it was %d", totalPending), totalPending == 6 || totalPending == 7 || totalPending == 8);
        waitForPending(tagsForSourceToTwo, 7, 6L, 500);
        waitForPending(tagsForAllToTwo, 7, 6L, 100);
        // neither one nor two have emitted, so the total pending should be = 12 - 1 (forward-one) - 1 or 2 (forward-two)
        // => 10 or 9 events
        waitForPending(tagsForAll, 10, 9L, 100);
        // kick on forward-one, it should now consume all its events
        Assert.assertTrue(new File(tempFolder, "one").createNewFile());
        waitForPending(tagsForSourceToOne, 0, 2000);
        waitForPending(tagsForAllToOne, 0, 100);
        // sink has received 4 but started to read 1, so it has 3 pending
        waitForPending(tagsForOneToSink, 3, 1000);
        waitForPending(tagsForAllToSink, 3, 100);
        // kick-off forward-two, it should now consume all its integer and string events
        Assert.assertTrue(new File(tempFolder, "two-i").createNewFile());
        Assert.assertTrue(new File(tempFolder, "two-s").createNewFile());
        // pending events for all of forward-two's queues should go to zero
        waitForPending(tagsForSourceToTwoInts, 0, 2000);
        waitForPending(tagsForSourceToTwoStrings, 0, 1000);
        waitForPending(tagsForSourceToTwo, 0, 1000);
        waitForPending(tagsForAllToTwo, 0, 100);
        // but now sink should have 8 more events waiting
        waitForPending(tagsForOneToSink, 3, 1000);
        waitForPending(tagsForTwoToSink, 8, 1000);
        waitForPending(tagsForAllToSink, 11, 100);
        // kick off sink, its pending events should now go to zero
        Assert.assertTrue(new File(tempFolder, "three").createNewFile());
        waitForPending(tagsForOneToSink, 0, 2000);
        waitForPending(tagsForTwoToSink, 0, 2000);
        waitForPending(tagsForAllToSink, 0, 100);
    } finally {
        controller.stop();
    }
}
Also used : ProgramController(co.cask.cdap.app.runtime.ProgramController) ApplicationWithPrograms(co.cask.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms) Arguments(co.cask.cdap.app.runtime.Arguments) BasicArguments(co.cask.cdap.internal.app.runtime.BasicArguments) ProgramDescriptor(co.cask.cdap.app.program.ProgramDescriptor) BasicArguments(co.cask.cdap.internal.app.runtime.BasicArguments) File(java.io.File) Test(org.junit.Test)

Example 20 with ProgramDescriptor

use of co.cask.cdap.app.program.ProgramDescriptor in project cdap by caskdata.

the class MultiConsumerTest method testMulti.

@Test
public void testMulti() throws Exception {
    // TODO: Fix this test case to really test with numGroups settings.
    final ApplicationWithPrograms app = AppFabricTestHelper.deployApplicationWithManager(MultiApp.class, TEMP_FOLDER_SUPPLIER);
    List<ProgramController> controllers = Lists.newArrayList();
    for (ProgramDescriptor programDescriptor : app.getPrograms()) {
        controllers.add(AppFabricTestHelper.submit(app, programDescriptor.getSpecification().getClassName(), new BasicArguments(), TEMP_FOLDER_SUPPLIER));
    }
    DatasetFramework datasetFramework = AppFabricTestHelper.getInjector().getInstance(DatasetFramework.class);
    DynamicDatasetCache datasetCache = new SingleThreadDatasetCache(new SystemDatasetInstantiator(datasetFramework, getClass().getClassLoader(), null), AppFabricTestHelper.getInjector().getInstance(TransactionSystemClient.class), NamespaceId.DEFAULT, DatasetDefinition.NO_ARGUMENTS, null, null);
    final KeyValueTable accumulated = datasetCache.getDataset("accumulated");
    TransactionExecutorFactory txExecutorFactory = AppFabricTestHelper.getInjector().getInstance(TransactionExecutorFactory.class);
    // Try to get accumulated result and verify it. Expect result appear in max of 60 seconds.
    int trial = 0;
    while (trial < 60) {
        try {
            Transactions.createTransactionExecutor(txExecutorFactory, accumulated).execute(new TransactionExecutor.Subroutine() {

                @Override
                public void apply() throws Exception {
                    byte[] value = accumulated.read(MultiApp.KEY);
                    // Sum(1..100) * 3
                    Assert.assertEquals(((1 + 99) * 99 / 2) * 3, Longs.fromByteArray(value));
                }
            });
            break;
        } catch (TransactionFailureException e) {
            // No-op
            trial++;
            TimeUnit.SECONDS.sleep(1);
        }
    }
    Assert.assertTrue(trial < 60);
    for (ProgramController controller : controllers) {
        controller.stop().get();
    }
}
Also used : ProgramController(co.cask.cdap.app.runtime.ProgramController) DynamicDatasetCache(co.cask.cdap.data2.dataset2.DynamicDatasetCache) TransactionExecutor(org.apache.tephra.TransactionExecutor) SingleThreadDatasetCache(co.cask.cdap.data2.dataset2.SingleThreadDatasetCache) TransactionFailureException(org.apache.tephra.TransactionFailureException) IOException(java.io.IOException) TransactionExecutorFactory(org.apache.tephra.TransactionExecutorFactory) DatasetFramework(co.cask.cdap.data2.dataset2.DatasetFramework) TransactionSystemClient(org.apache.tephra.TransactionSystemClient) TransactionFailureException(org.apache.tephra.TransactionFailureException) ApplicationWithPrograms(co.cask.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms) SystemDatasetInstantiator(co.cask.cdap.data.dataset.SystemDatasetInstantiator) KeyValueTable(co.cask.cdap.api.dataset.lib.KeyValueTable) ProgramDescriptor(co.cask.cdap.app.program.ProgramDescriptor) BasicArguments(co.cask.cdap.internal.app.runtime.BasicArguments) Test(org.junit.Test)

Aggregations

ProgramDescriptor (co.cask.cdap.app.program.ProgramDescriptor)20 Test (org.junit.Test)11 BasicArguments (co.cask.cdap.internal.app.runtime.BasicArguments)10 ProgramController (co.cask.cdap.app.runtime.ProgramController)9 File (java.io.File)9 IOException (java.io.IOException)8 ApplicationWithPrograms (co.cask.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms)7 ProgramId (co.cask.cdap.proto.id.ProgramId)7 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)5 SimpleProgramOptions (co.cask.cdap.internal.app.runtime.SimpleProgramOptions)5 Location (org.apache.twill.filesystem.Location)4 CConfiguration (co.cask.cdap.common.conf.CConfiguration)3 RandomEndpointStrategy (co.cask.cdap.common.discovery.RandomEndpointStrategy)3 Discoverable (org.apache.twill.discovery.Discoverable)3 DiscoveryServiceClient (org.apache.twill.discovery.DiscoveryServiceClient)3 AppWithAnonymousWorkflow (co.cask.cdap.AppWithAnonymousWorkflow)2 MissingMapReduceWorkflowApp (co.cask.cdap.MissingMapReduceWorkflowApp)2 MissingSparkWorkflowApp (co.cask.cdap.MissingSparkWorkflowApp)2 NonUniqueProgramsInWorkflowApp (co.cask.cdap.NonUniqueProgramsInWorkflowApp)2 NonUniqueProgramsInWorkflowWithForkApp (co.cask.cdap.NonUniqueProgramsInWorkflowWithForkApp)2