Search in sources :

Example 1 with RevealingTransaction

use of io.cdap.cdap.test.RevealingTxSystemClient.RevealingTransaction in project cdap by caskdata.

the class AppWithCustomTx method recordTransaction.

/**
 * If in a transaction, records the timeout that the current transaction was given, or "default" if no explicit
 * timeout was given. Otherwise does nothing.
 *
 * Note: we know whether and what explicit timeout was given, because we inject a {@link RevealingTxSystemClient},
 *       which returns a {@link RevealingTransaction} for {@link TransactionSystemClient#startShort(int)} only.
 */
static void recordTransaction(DatasetContext context, String row, String column) {
    TransactionCapturingTable capture = context.getDataset(CAPTURE);
    Transaction tx = capture.getTx();
    // we cannot cast because the RevealingTransaction is not visible in the program class loader
    String value = DEFAULT;
    if (tx == null) {
        try {
            capture.getTable().put(new Put(row, column, value));
            throw new RuntimeException("put to table without transaction should have failed.");
        } catch (DataSetException e) {
        // expected
        }
        return;
    }
    if ("RevealingTransaction".equals(tx.getClass().getSimpleName())) {
        int txTimeout;
        try {
            txTimeout = (int) tx.getClass().getField("timeout").get(tx);
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
        value = String.valueOf(txTimeout);
    }
    capture.getTable().put(new Put(row, column, value));
}
Also used : Transaction(org.apache.tephra.Transaction) RevealingTransaction(io.cdap.cdap.test.RevealingTxSystemClient.RevealingTransaction) DataSetException(io.cdap.cdap.api.dataset.DataSetException) Put(io.cdap.cdap.api.dataset.table.Put) TransactionFailureException(org.apache.tephra.TransactionFailureException) DataSetException(io.cdap.cdap.api.dataset.DataSetException) IOException(java.io.IOException)

Example 2 with RevealingTransaction

use of io.cdap.cdap.test.RevealingTxSystemClient.RevealingTransaction in project cdap by cdapio.

the class AppWithCustomTx method recordTransaction.

/**
 * If in a transaction, records the timeout that the current transaction was given, or "default" if no explicit
 * timeout was given. Otherwise does nothing.
 *
 * Note: we know whether and what explicit timeout was given, because we inject a {@link RevealingTxSystemClient},
 *       which returns a {@link RevealingTransaction} for {@link TransactionSystemClient#startShort(int)} only.
 */
static void recordTransaction(DatasetContext context, String row, String column) {
    TransactionCapturingTable capture = context.getDataset(CAPTURE);
    Transaction tx = capture.getTx();
    // we cannot cast because the RevealingTransaction is not visible in the program class loader
    String value = DEFAULT;
    if (tx == null) {
        try {
            capture.getTable().put(new Put(row, column, value));
            throw new RuntimeException("put to table without transaction should have failed.");
        } catch (DataSetException e) {
        // expected
        }
        return;
    }
    if ("RevealingTransaction".equals(tx.getClass().getSimpleName())) {
        int txTimeout;
        try {
            txTimeout = (int) tx.getClass().getField("timeout").get(tx);
        } catch (Exception e) {
            throw Throwables.propagate(e);
        }
        value = String.valueOf(txTimeout);
    }
    capture.getTable().put(new Put(row, column, value));
}
Also used : Transaction(org.apache.tephra.Transaction) RevealingTransaction(io.cdap.cdap.test.RevealingTxSystemClient.RevealingTransaction) DataSetException(io.cdap.cdap.api.dataset.DataSetException) Put(io.cdap.cdap.api.dataset.table.Put) TransactionFailureException(org.apache.tephra.TransactionFailureException) DataSetException(io.cdap.cdap.api.dataset.DataSetException) IOException(java.io.IOException)

Aggregations

DataSetException (io.cdap.cdap.api.dataset.DataSetException)2 Put (io.cdap.cdap.api.dataset.table.Put)2 RevealingTransaction (io.cdap.cdap.test.RevealingTxSystemClient.RevealingTransaction)2 IOException (java.io.IOException)2 Transaction (org.apache.tephra.Transaction)2 TransactionFailureException (org.apache.tephra.TransactionFailureException)2