Search in sources :

Example 1 with TransactionExecutorFactory

use of org.apache.tephra.TransactionExecutorFactory in project cdap by caskdata.

the class UsageRegistryTest method testUsageRegistry.

@Test
public void testUsageRegistry() {
    // instantiate a usage registry
    UsageRegistry registry = new DefaultUsageRegistry(new TransactionExecutorFactory() {

        @Override
        public TransactionExecutor createExecutor(Iterable<TransactionAware> iterable) {
            return dsFrameworkUtil.newInMemoryTransactionExecutor(iterable);
        }
    }, new ForwardingDatasetFramework(dsFrameworkUtil.getFramework()) {

        @Nullable
        @Override
        public <T extends Dataset> T getDataset(DatasetId datasetInstanceId, Map<String, String> arguments, @Nullable ClassLoader classLoader) throws DatasetManagementException, IOException {
            T t = super.getDataset(datasetInstanceId, arguments, classLoader);
            if (t instanceof UsageDataset) {
                @SuppressWarnings("unchecked") T t1 = (T) new WrappedUsageDataset((UsageDataset) t);
                return t1;
            }
            return t;
        }
    });
    // register usage for a stream and a dataset for single and multiple "owners", including a non-program
    registry.register(flow11, datasetInstance1);
    registry.register(flow12, stream1);
    registry.registerAll(ImmutableList.of(flow21, flow22), datasetInstance2);
    registry.registerAll(ImmutableList.of(flow21, flow22), stream1);
    int count = WrappedUsageDataset.registerCount;
    // validate usage
    Assert.assertEquals(ImmutableSet.of(datasetInstance1), registry.getDatasets(flow11));
    Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow12));
    Assert.assertEquals(ImmutableSet.of(datasetInstance2), registry.getDatasets(flow21));
    Assert.assertEquals(ImmutableSet.of(datasetInstance2), registry.getDatasets(flow22));
    Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow21));
    Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow22));
    Assert.assertEquals(ImmutableSet.of(flow11), registry.getPrograms(datasetInstance1));
    Assert.assertEquals(ImmutableSet.of(flow21, flow22), registry.getPrograms(datasetInstance2));
    Assert.assertEquals(ImmutableSet.of(flow12, flow21, flow22), registry.getPrograms(stream1));
    // register datasets again
    registry.register(flow11, datasetInstance1);
    registry.registerAll(ImmutableList.of(flow21, flow22), datasetInstance2);
    // validate that this does re-register previous usages (DefaultUsageRegistry no longer avoids re-registration)
    count += 3;
    Assert.assertEquals(count, WrappedUsageDataset.registerCount);
    // validate usage
    Assert.assertEquals(ImmutableSet.of(datasetInstance1), registry.getDatasets(flow11));
    Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow12));
    Assert.assertEquals(ImmutableSet.of(datasetInstance2), registry.getDatasets(flow21));
    Assert.assertEquals(ImmutableSet.of(datasetInstance2), registry.getDatasets(flow22));
    Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow21));
    Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow22));
    Assert.assertEquals(ImmutableSet.of(flow11), registry.getPrograms(datasetInstance1));
    Assert.assertEquals(ImmutableSet.of(flow21, flow22), registry.getPrograms(datasetInstance2));
    Assert.assertEquals(ImmutableSet.of(flow12, flow21, flow22), registry.getPrograms(stream1));
    // unregister app
    registry.unregister(flow11.getParent());
    // validate usage for that app is gone
    Assert.assertEquals(ImmutableSet.of(), registry.getDatasets(flow11));
    Assert.assertEquals(ImmutableSet.of(), registry.getStreams(flow12));
    Assert.assertEquals(ImmutableSet.of(datasetInstance2), registry.getDatasets(flow21));
    Assert.assertEquals(ImmutableSet.of(datasetInstance2), registry.getDatasets(flow22));
    Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow21));
    Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow22));
    Assert.assertEquals(ImmutableSet.of(), registry.getPrograms(datasetInstance1));
    Assert.assertEquals(ImmutableSet.of(flow21, flow22), registry.getPrograms(datasetInstance2));
    Assert.assertEquals(ImmutableSet.of(flow21, flow22), registry.getPrograms(stream1));
    // register application 1 again
    registry.register(flow11, datasetInstance1);
    registry.register(flow12, stream1);
    // validate it was re-registered
    Assert.assertEquals(ImmutableSet.of(datasetInstance1), registry.getDatasets(flow11));
    Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow12));
    Assert.assertEquals(ImmutableSet.of(datasetInstance2), registry.getDatasets(flow21));
    Assert.assertEquals(ImmutableSet.of(datasetInstance2), registry.getDatasets(flow22));
    Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow21));
    Assert.assertEquals(ImmutableSet.of(stream1), registry.getStreams(flow22));
    Assert.assertEquals(ImmutableSet.of(flow11), registry.getPrograms(datasetInstance1));
    Assert.assertEquals(ImmutableSet.of(flow21, flow22), registry.getPrograms(datasetInstance2));
    Assert.assertEquals(ImmutableSet.of(flow12, flow21, flow22), registry.getPrograms(stream1));
    // validate that this actually re-registered previous usages (through code in wrapped usage dataset)
    Assert.assertEquals(count + 2, WrappedUsageDataset.registerCount);
}
Also used : TransactionExecutor(org.apache.tephra.TransactionExecutor) IOException(java.io.IOException) TransactionExecutorFactory(org.apache.tephra.TransactionExecutorFactory) DatasetId(co.cask.cdap.proto.id.DatasetId) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) TransactionAware(org.apache.tephra.TransactionAware) ForwardingDatasetFramework(co.cask.cdap.data2.dataset2.ForwardingDatasetFramework) Nullable(javax.annotation.Nullable) Test(org.junit.Test)

Example 2 with TransactionExecutorFactory

use of org.apache.tephra.TransactionExecutorFactory in project cdap by caskdata.

the class TableConcurrentTest method before.

@Before
public void before() {
    super.before();
    txExecutorFactory = new TransactionExecutorFactory() {

        @Override
        public TransactionExecutor createExecutor(Iterable<TransactionAware> txAwares) {
            return new DefaultTransactionExecutor(txClient, txAwares);
        }
    };
}
Also used : TransactionAware(org.apache.tephra.TransactionAware) DefaultTransactionExecutor(org.apache.tephra.DefaultTransactionExecutor) TransactionExecutor(org.apache.tephra.TransactionExecutor) DefaultTransactionExecutor(org.apache.tephra.DefaultTransactionExecutor) TransactionExecutorFactory(org.apache.tephra.TransactionExecutorFactory) Before(org.junit.Before)

Example 3 with TransactionExecutorFactory

use of org.apache.tephra.TransactionExecutorFactory in project cdap by caskdata.

the class ProgramLifecycleHttpHandlerTest method enqueue.

// TODO: Duplicate from AppFabricHttpHandlerTest, remove the AppFabricHttpHandlerTest method after deprecating v2 APIs
private void enqueue(QueueName queueName, final QueueEntry queueEntry) throws Exception {
    QueueClientFactory queueClientFactory = AppFabricTestBase.getInjector().getInstance(QueueClientFactory.class);
    final QueueProducer producer = queueClientFactory.createProducer(queueName);
    // doing inside tx
    TransactionExecutorFactory txExecutorFactory = AppFabricTestBase.getInjector().getInstance(TransactionExecutorFactory.class);
    txExecutorFactory.createExecutor(ImmutableList.of((TransactionAware) producer)).execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            // write more than one so that we can dequeue multiple times for multiple checks
            // we only dequeue twice, but ensure that the drop queues call drops the rest of the entries as well
            int numEntries = 0;
            while (numEntries++ < 5) {
                producer.enqueue(queueEntry);
            }
        }
    });
}
Also used : QueueProducer(co.cask.cdap.data2.queue.QueueProducer) QueueClientFactory(co.cask.cdap.data2.queue.QueueClientFactory) TransactionExecutor(org.apache.tephra.TransactionExecutor) IOException(java.io.IOException) NotFoundException(co.cask.cdap.common.NotFoundException) TransactionExecutorFactory(org.apache.tephra.TransactionExecutorFactory)

Example 4 with TransactionExecutorFactory

use of org.apache.tephra.TransactionExecutorFactory in project cdap by caskdata.

the class ProgramLifecycleHttpHandlerTest method dequeueOne.

private boolean dequeueOne(QueueName queueName) throws Exception {
    QueueClientFactory queueClientFactory = AppFabricTestBase.getInjector().getInstance(QueueClientFactory.class);
    final QueueConsumer consumer = queueClientFactory.createConsumer(queueName, new ConsumerConfig(1L, 0, 1, DequeueStrategy.ROUND_ROBIN, null), 1);
    // doing inside tx
    TransactionExecutorFactory txExecutorFactory = AppFabricTestBase.getInjector().getInstance(TransactionExecutorFactory.class);
    return txExecutorFactory.createExecutor(ImmutableList.of((TransactionAware) consumer)).execute(new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            return !consumer.dequeue(1).isEmpty();
        }
    });
}
Also used : QueueConsumer(co.cask.cdap.data2.queue.QueueConsumer) QueueClientFactory(co.cask.cdap.data2.queue.QueueClientFactory) ConsumerConfig(co.cask.cdap.data2.queue.ConsumerConfig) IOException(java.io.IOException) NotFoundException(co.cask.cdap.common.NotFoundException) TransactionExecutorFactory(org.apache.tephra.TransactionExecutorFactory)

Example 5 with TransactionExecutorFactory

use of org.apache.tephra.TransactionExecutorFactory 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

TransactionExecutorFactory (org.apache.tephra.TransactionExecutorFactory)5 IOException (java.io.IOException)4 TransactionExecutor (org.apache.tephra.TransactionExecutor)4 NotFoundException (co.cask.cdap.common.NotFoundException)2 QueueClientFactory (co.cask.cdap.data2.queue.QueueClientFactory)2 TransactionAware (org.apache.tephra.TransactionAware)2 Test (org.junit.Test)2 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)1 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)1 ProgramDescriptor (co.cask.cdap.app.program.ProgramDescriptor)1 ProgramController (co.cask.cdap.app.runtime.ProgramController)1 SystemDatasetInstantiator (co.cask.cdap.data.dataset.SystemDatasetInstantiator)1 DatasetFramework (co.cask.cdap.data2.dataset2.DatasetFramework)1 DynamicDatasetCache (co.cask.cdap.data2.dataset2.DynamicDatasetCache)1 ForwardingDatasetFramework (co.cask.cdap.data2.dataset2.ForwardingDatasetFramework)1 SingleThreadDatasetCache (co.cask.cdap.data2.dataset2.SingleThreadDatasetCache)1 ConsumerConfig (co.cask.cdap.data2.queue.ConsumerConfig)1 QueueConsumer (co.cask.cdap.data2.queue.QueueConsumer)1 QueueProducer (co.cask.cdap.data2.queue.QueueProducer)1 ApplicationWithPrograms (co.cask.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms)1