Search in sources :

Example 26 with DatasetContext

use of co.cask.cdap.api.data.DatasetContext in project cdap by caskdata.

the class DefaultStore method setWorkerInstances.

@Override
public void setWorkerInstances(final ProgramId id, final int instances) {
    Preconditions.checkArgument(instances > 0, "Cannot change number of worker instances to %s", instances);
    Transactions.executeUnchecked(transactional, new TxRunnable() {

        @Override
        public void run(DatasetContext context) throws Exception {
            AppMetadataStore metaStore = getAppMetadataStore(context);
            ApplicationSpecification appSpec = getAppSpecOrFail(metaStore, id);
            WorkerSpecification workerSpec = getWorkerSpecOrFail(id, appSpec);
            WorkerSpecification newSpecification = new WorkerSpecification(workerSpec.getClassName(), workerSpec.getName(), workerSpec.getDescription(), workerSpec.getProperties(), workerSpec.getDatasets(), workerSpec.getResources(), instances);
            ApplicationSpecification newAppSpec = replaceWorkerInAppSpec(appSpec, id, newSpecification);
            metaStore.updateAppSpec(id.getNamespace(), id.getApplication(), id.getVersion(), newAppSpec);
        }
    });
    LOG.trace("Setting program instances: namespace: {}, application: {}, worker: {}, new instances count: {}", id.getNamespaceId(), id.getApplication(), id.getProgram(), instances);
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) ForwardingApplicationSpecification(co.cask.cdap.internal.app.ForwardingApplicationSpecification) TxRunnable(co.cask.cdap.api.TxRunnable) WorkerSpecification(co.cask.cdap.api.worker.WorkerSpecification) DatasetContext(co.cask.cdap.api.data.DatasetContext) TransactionFailureException(org.apache.tephra.TransactionFailureException) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) ApplicationNotFoundException(co.cask.cdap.common.ApplicationNotFoundException) TransactionNotInProgressException(org.apache.tephra.TransactionNotInProgressException) TransactionConflictException(org.apache.tephra.TransactionConflictException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) NoSuchElementException(java.util.NoSuchElementException) IOException(java.io.IOException)

Example 27 with DatasetContext

use of co.cask.cdap.api.data.DatasetContext in project cdap by caskdata.

the class DStreamCollection method compute.

@Override
public <U> SparkCollection<U> compute(final StageInfo stageInfo, SparkCompute<T, U> compute) throws Exception {
    final SparkCompute<T, U> wrappedCompute = new DynamicSparkCompute<>(new DynamicDriverContext(stageInfo, sec), compute);
    Transactionals.execute(sec, new TxRunnable() {

        @Override
        public void run(DatasetContext datasetContext) throws Exception {
            SparkExecutionPluginContext sparkPluginContext = new BasicSparkExecutionPluginContext(sec, JavaSparkContext.fromSparkContext(stream.context().sparkContext()), datasetContext, stageInfo);
            wrappedCompute.initialize(sparkPluginContext);
        }
    }, Exception.class);
    return wrap(stream.transform(new ComputeTransformFunction<>(sec, stageInfo, wrappedCompute)));
}
Also used : DynamicSparkCompute(co.cask.cdap.etl.spark.streaming.function.DynamicSparkCompute) BasicSparkExecutionPluginContext(co.cask.cdap.etl.spark.batch.BasicSparkExecutionPluginContext) SparkExecutionPluginContext(co.cask.cdap.etl.api.batch.SparkExecutionPluginContext) BasicSparkExecutionPluginContext(co.cask.cdap.etl.spark.batch.BasicSparkExecutionPluginContext) ComputeTransformFunction(co.cask.cdap.etl.spark.streaming.function.ComputeTransformFunction) TxRunnable(co.cask.cdap.api.TxRunnable) DatasetContext(co.cask.cdap.api.data.DatasetContext) TransactionFailureException(org.apache.tephra.TransactionFailureException)

Example 28 with DatasetContext

use of co.cask.cdap.api.data.DatasetContext in project cdap by caskdata.

the class DynamicSparkCompute method lazyInit.

// when checkpointing is enabled, and Spark is loading DStream operations from an existing checkpoint,
// delegate will be null and the initialize() method won't have been called. So we need to instantiate
// the delegate and initialize it.
private void lazyInit(final JavaSparkContext jsc) throws Exception {
    if (delegate == null) {
        PluginFunctionContext pluginFunctionContext = dynamicDriverContext.getPluginFunctionContext();
        delegate = pluginFunctionContext.createPlugin();
        final StageInfo stageInfo = pluginFunctionContext.getStageInfo();
        final JavaSparkExecutionContext sec = dynamicDriverContext.getSparkExecutionContext();
        Transactionals.execute(sec, new TxRunnable() {

            @Override
            public void run(DatasetContext datasetContext) throws Exception {
                SparkExecutionPluginContext sparkPluginContext = new BasicSparkExecutionPluginContext(sec, jsc, datasetContext, stageInfo);
                delegate.initialize(sparkPluginContext);
            }
        }, Exception.class);
    }
}
Also used : BasicSparkExecutionPluginContext(co.cask.cdap.etl.spark.batch.BasicSparkExecutionPluginContext) PluginFunctionContext(co.cask.cdap.etl.spark.function.PluginFunctionContext) SparkExecutionPluginContext(co.cask.cdap.etl.api.batch.SparkExecutionPluginContext) BasicSparkExecutionPluginContext(co.cask.cdap.etl.spark.batch.BasicSparkExecutionPluginContext) StageInfo(co.cask.cdap.etl.planner.StageInfo) TxRunnable(co.cask.cdap.api.TxRunnable) JavaSparkExecutionContext(co.cask.cdap.api.spark.JavaSparkExecutionContext) DatasetContext(co.cask.cdap.api.data.DatasetContext) TransactionFailureException(org.apache.tephra.TransactionFailureException)

Example 29 with DatasetContext

use of co.cask.cdap.api.data.DatasetContext in project cdap by caskdata.

the class MockAction method run.

@Override
public void run(ActionContext context) throws Exception {
    context.execute(new TxRunnable() {

        @Override
        public void run(DatasetContext context) throws Exception {
            Table table = context.getDataset(config.tableName);
            Put put = new Put(config.rowKey);
            put.add(config.columnKey, config.value);
            table.put(put);
        }
    });
    // Set the same value in the arguments as well.
    context.getArguments().set(config.rowKey + config.columnKey, config.value);
}
Also used : Table(co.cask.cdap.api.dataset.table.Table) TxRunnable(co.cask.cdap.api.TxRunnable) DatasetContext(co.cask.cdap.api.data.DatasetContext) Put(co.cask.cdap.api.dataset.table.Put)

Example 30 with DatasetContext

use of co.cask.cdap.api.data.DatasetContext in project cdap by caskdata.

the class NotificationTest method useTransactionTest.

@Test
public void useTransactionTest() throws Exception {
    // Performing admin operations to create dataset instance
    // keyValueTable is a system dataset module
    namespaceAdmin.create(new NamespaceMeta.Builder().setName(namespace).build());
    DatasetId myTableInstance = namespace.dataset("myTable");
    dsFramework.addInstance("keyValueTable", myTableInstance, DatasetProperties.EMPTY);
    final CountDownLatch receivedLatch = new CountDownLatch(1);
    Assert.assertTrue(feedManager.createFeed(FEED1_INFO));
    try {
        Cancellable cancellable = notificationService.subscribe(FEED1, new NotificationHandler<String>() {

            private int received = 0;

            @Override
            public Type getNotificationType() {
                return String.class;
            }

            @Override
            public void received(final String notification, NotificationContext notificationContext) {
                notificationContext.execute(new TxRunnable() {

                    @Override
                    public void run(DatasetContext context) throws Exception {
                        KeyValueTable table = context.getDataset("myTable");
                        table.write("foo", String.format("%s-%d", notification, received++));
                        receivedLatch.countDown();
                    }
                }, TxRetryPolicy.maxRetries(5));
            }
        });
        // Short delay for the subscriber to setup the subscription.
        TimeUnit.MILLISECONDS.sleep(500);
        try {
            notificationService.publish(FEED1, "foobar");
            // Waiting for the subscriber to receive that notification
            Assert.assertTrue(receivedLatch.await(5, TimeUnit.SECONDS));
            // Read the KeyValueTable for the value updated from the subscriber.
            // Need to poll it couple times since after the received method returned,
            // the tx may not yet committed when we try to read it here.
            final KeyValueTable table = dsFramework.getDataset(myTableInstance, DatasetDefinition.NO_ARGUMENTS, null);
            Assert.assertNotNull(table);
            final TransactionContext txContext = new TransactionContext(txClient, table);
            Tasks.waitFor(true, new Callable<Boolean>() {

                @Override
                public Boolean call() throws Exception {
                    txContext.start();
                    try {
                        return "foobar-0".equals(Bytes.toString(table.read("foo")));
                    } finally {
                        txContext.finish();
                    }
                }
            }, 5, TimeUnit.SECONDS);
        } finally {
            cancellable.cancel();
        }
    } finally {
        dsFramework.deleteInstance(myTableInstance);
        feedManager.deleteFeed(FEED1);
        namespaceAdmin.delete(namespace);
    }
}
Also used : Cancellable(org.apache.twill.common.Cancellable) CountDownLatch(java.util.concurrent.CountDownLatch) NotificationFeedNotFoundException(co.cask.cdap.notifications.feeds.NotificationFeedNotFoundException) DatasetId(co.cask.cdap.proto.id.DatasetId) NotificationContext(co.cask.cdap.notifications.service.NotificationContext) Type(java.lang.reflect.Type) NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) TxRunnable(co.cask.cdap.api.TxRunnable) KeyValueTable(co.cask.cdap.api.dataset.lib.KeyValueTable) TransactionContext(org.apache.tephra.TransactionContext) DatasetContext(co.cask.cdap.api.data.DatasetContext) Test(org.junit.Test)

Aggregations

DatasetContext (co.cask.cdap.api.data.DatasetContext)43 TxRunnable (co.cask.cdap.api.TxRunnable)37 IOException (java.io.IOException)22 TransactionFailureException (org.apache.tephra.TransactionFailureException)22 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)15 TransactionConflictException (org.apache.tephra.TransactionConflictException)15 ApplicationNotFoundException (co.cask.cdap.common.ApplicationNotFoundException)10 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)10 NoSuchElementException (java.util.NoSuchElementException)10 TransactionNotInProgressException (org.apache.tephra.TransactionNotInProgressException)10 TransactionControl (co.cask.cdap.api.annotation.TransactionControl)8 Table (co.cask.cdap.api.dataset.table.Table)8 ProgramLifecycle (co.cask.cdap.api.ProgramLifecycle)4 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)4 KeyValueTable (co.cask.cdap.api.dataset.lib.KeyValueTable)4 ForwardingApplicationSpecification (co.cask.cdap.internal.app.ForwardingApplicationSpecification)4 HashMap (java.util.HashMap)4 AtomicReference (java.util.concurrent.atomic.AtomicReference)4 Put (co.cask.cdap.api.dataset.table.Put)3 Map (java.util.Map)3