Search in sources :

Example 1 with Transactional

use of io.cdap.cdap.api.Transactional in project cdap by caskdata.

the class SearchHelper method createTransactional.

/**
 * Create a transactional for metadata datasets.
 *
 * @param txClient transaction client
 * @return transactional for the metadata tables
 */
private Transactional createTransactional(TransactionSystemClient txClient) {
    return new Transactional() {

        @Override
        public void execute(io.cdap.cdap.api.TxRunnable runnable) throws TransactionFailureException {
            TransactionContext txContext = new TransactionContext(txClient);
            try (MetaOnlyDatasetContext datasetContext = new MetaOnlyDatasetContext(txContext)) {
                txContext.start();
                finishExecute(txContext, datasetContext, runnable);
            } catch (Exception e) {
                Throwables.propagateIfPossible(e, TransactionFailureException.class);
            }
        }

        @Override
        public void execute(int timeout, io.cdap.cdap.api.TxRunnable runnable) throws TransactionFailureException {
            TransactionContext txContext = new TransactionContext(txClient);
            try (MetaOnlyDatasetContext datasetContext = new MetaOnlyDatasetContext(txContext)) {
                txContext.start(timeout);
                finishExecute(txContext, datasetContext, runnable);
            } catch (Exception e) {
                Throwables.propagateIfPossible(e, TransactionFailureException.class);
            }
        }

        private void finishExecute(TransactionContext txContext, io.cdap.cdap.api.data.DatasetContext dsContext, io.cdap.cdap.api.TxRunnable runnable) throws TransactionFailureException {
            try {
                runnable.run(dsContext);
            } catch (Exception e) {
                txContext.abort(new TransactionFailureException("Exception raised from TxRunnable.run() " + runnable, e));
            }
            // The call the txContext.abort above will always have exception thrown
            // Hence we'll only reach here if and only if the runnable.run() returns normally.
            txContext.finish();
        }
    };
}
Also used : TransactionFailureException(org.apache.tephra.TransactionFailureException) TransactionContext(org.apache.tephra.TransactionContext) DatasetContext(io.cdap.cdap.api.dataset.DatasetContext) TransactionFailureException(org.apache.tephra.TransactionFailureException) DatasetInstantiationException(io.cdap.cdap.api.data.DatasetInstantiationException) IOException(java.io.IOException) Transactional(io.cdap.cdap.api.Transactional)

Example 2 with Transactional

use of io.cdap.cdap.api.Transactional in project cdap by caskdata.

the class NoSQLTransactionals method createTransactional.

/**
 * Create a transactional for an entity table. The regular {@link io.cdap.cdap.api.Transactionals} class cannot be
 * used due to cyclic dependency between dataset service and NoSQL StructuredTable.
 *
 * @param txClient transaction client
 * @param datasetSupplier supplies the dataset for the entity table
 * @return transactional for the entity table
 */
public static Transactional createTransactional(TransactionSystemClient txClient, TableDatasetSupplier datasetSupplier) {
    return new Transactional() {

        @Override
        public void execute(io.cdap.cdap.api.TxRunnable runnable) throws TransactionFailureException {
            TransactionContext txContext = new TransactionContext(txClient);
            try (EntityTableDatasetContext datasetContext = new EntityTableDatasetContext(txContext, datasetSupplier)) {
                txContext.start();
                finishExecute(txContext, datasetContext, runnable);
            } catch (Exception e) {
                Throwables.propagateIfPossible(e, TransactionFailureException.class);
            }
        }

        @Override
        public void execute(int timeout, io.cdap.cdap.api.TxRunnable runnable) throws TransactionFailureException {
            TransactionContext txContext = new TransactionContext(txClient);
            try (EntityTableDatasetContext datasetContext = new EntityTableDatasetContext(txContext, datasetSupplier)) {
                txContext.start(timeout);
                finishExecute(txContext, datasetContext, runnable);
            } catch (Exception e) {
                Throwables.propagateIfPossible(e, TransactionFailureException.class);
            }
        }

        private void finishExecute(TransactionContext txContext, DatasetContext dsContext, io.cdap.cdap.api.TxRunnable runnable) throws TransactionFailureException {
            try {
                runnable.run(dsContext);
            } catch (Exception e) {
                txContext.abort(new TransactionFailureException("Exception raised from TxRunnable.run() " + runnable, e));
            }
            // The call the txContext.abort above will always have exception thrown
            // Hence we'll only reach here if and only if the runnable.run() returns normally.
            txContext.finish();
        }
    };
}
Also used : TransactionFailureException(org.apache.tephra.TransactionFailureException) TransactionContext(org.apache.tephra.TransactionContext) DatasetContext(io.cdap.cdap.api.data.DatasetContext) TransactionFailureException(org.apache.tephra.TransactionFailureException) Transactional(io.cdap.cdap.api.Transactional)

Example 3 with Transactional

use of io.cdap.cdap.api.Transactional in project cdap by caskdata.

the class AbstractContext method execute.

/**
 * Execute in a transaction with optional retry on conflict.
 */
public void execute(final TxRunnable runnable, boolean retryOnConflict) throws TransactionFailureException {
    ClassLoader oldClassLoader = ClassLoaders.setContextClassLoader(getClass().getClassLoader());
    try {
        Transactional txnl = retryOnConflict ? Transactions.createTransactionalWithRetry(transactional, RetryStrategies.retryOnConflict(20, 100)) : transactional;
        txnl.execute(context -> {
            ClassLoader oldClassLoader1 = ClassLoaders.setContextClassLoader(getProgramInvocationClassLoader());
            try {
                runnable.run(context);
            } finally {
                ClassLoaders.setContextClassLoader(oldClassLoader1);
            }
        });
    } finally {
        ClassLoaders.setContextClassLoader(oldClassLoader);
    }
}
Also used : CombineClassLoader(io.cdap.cdap.common.lang.CombineClassLoader) Transactional(io.cdap.cdap.api.Transactional)

Aggregations

Transactional (io.cdap.cdap.api.Transactional)3 TransactionContext (org.apache.tephra.TransactionContext)2 TransactionFailureException (org.apache.tephra.TransactionFailureException)2 DatasetContext (io.cdap.cdap.api.data.DatasetContext)1 DatasetInstantiationException (io.cdap.cdap.api.data.DatasetInstantiationException)1 DatasetContext (io.cdap.cdap.api.dataset.DatasetContext)1 CombineClassLoader (io.cdap.cdap.common.lang.CombineClassLoader)1 IOException (java.io.IOException)1