use of org.apache.tephra.TransactionContext in project cdap by caskdata.
the class TransactionContextTest method testAddThenSuccess.
@Test
public void testAddThenSuccess() throws TransactionFailureException, InterruptedException {
TransactionContext context = newTransactionContext(ds1);
// start transaction
context.start();
// add a change to ds1
ds1.addChange(A);
// add ds2 to the tx
context.addTransactionAware(ds2);
// add a change to ds2
ds2.addChange(B);
// commit transaction
context.finish();
// verify both are committed and post-committed
Assert.assertTrue(ds1.started);
Assert.assertTrue(ds2.started);
Assert.assertTrue(ds1.checked);
Assert.assertTrue(ds2.checked);
Assert.assertTrue(ds1.committed);
Assert.assertTrue(ds2.committed);
Assert.assertTrue(ds1.postCommitted);
Assert.assertTrue(ds2.postCommitted);
Assert.assertFalse(ds1.rolledBack);
Assert.assertFalse(ds2.rolledBack);
Assert.assertEquals(txClient.state, DummyTxClient.CommitState.Committed);
}
use of org.apache.tephra.TransactionContext in project cdap by caskdata.
the class TransactionContextTest method testAddThenFailure.
@Test
public void testAddThenFailure() throws TransactionFailureException, InterruptedException {
ds2.failCommitTxOnce = InduceFailure.ThrowException;
TransactionContext context = newTransactionContext(ds1);
// start transaction
context.start();
// add a change to ds1
ds1.addChange(A);
// add ds2 to the tx
context.addTransactionAware(ds2);
// add a change to ds2
ds2.addChange(B);
// commit transaction should fail and cause rollback
try {
context.finish();
Assert.fail("Persist should have failed - exception should be thrown");
} catch (TransactionFailureException e) {
Assert.assertEquals("persist failure", e.getCause().getMessage());
}
// verify both are rolled back and tx is aborted
Assert.assertTrue(ds1.started);
Assert.assertTrue(ds2.started);
Assert.assertTrue(ds1.checked);
Assert.assertTrue(ds2.checked);
Assert.assertTrue(ds1.committed);
Assert.assertTrue(ds2.committed);
Assert.assertFalse(ds1.postCommitted);
Assert.assertFalse(ds2.postCommitted);
Assert.assertTrue(ds1.rolledBack);
Assert.assertTrue(ds2.rolledBack);
Assert.assertEquals(txClient.state, DummyTxClient.CommitState.Aborted);
}
use of org.apache.tephra.TransactionContext in project cdap by caskdata.
the class TransactionContextTest method testSuccessful.
@Test
public void testSuccessful() throws TransactionFailureException, InterruptedException {
TransactionContext context = newTransactionContext(ds1, ds2);
// start transaction
context.start();
// add a change to ds1 and ds2
ds1.addChange(A);
ds2.addChange(B);
// commit transaction
context.finish();
// verify both are committed and post-committed
Assert.assertTrue(ds1.started);
Assert.assertTrue(ds2.started);
Assert.assertTrue(ds1.checked);
Assert.assertTrue(ds2.checked);
Assert.assertTrue(ds1.committed);
Assert.assertTrue(ds2.committed);
Assert.assertTrue(ds1.postCommitted);
Assert.assertTrue(ds2.postCommitted);
Assert.assertFalse(ds1.rolledBack);
Assert.assertFalse(ds2.rolledBack);
Assert.assertEquals(txClient.state, DummyTxClient.CommitState.Committed);
}
use of org.apache.tephra.TransactionContext in project cdap by caskdata.
the class TransactionContextTest method testPersistFailure.
@Test
public void testPersistFailure() throws TransactionFailureException, InterruptedException {
ds1.failCommitTxOnce = InduceFailure.ThrowException;
TransactionContext context = newTransactionContext(ds1, ds2);
// start transaction
context.start();
// add a change to ds1 and ds2
ds1.addChange(A);
ds2.addChange(B);
// commit transaction should fail and cause rollback
try {
context.finish();
Assert.fail("Persist should have failed - exception should be thrown");
} catch (TransactionFailureException e) {
Assert.assertEquals("persist failure", e.getCause().getMessage());
}
// verify both are rolled back and tx is aborted
Assert.assertTrue(ds1.started);
Assert.assertTrue(ds2.started);
Assert.assertTrue(ds1.checked);
Assert.assertTrue(ds2.checked);
Assert.assertTrue(ds1.committed);
Assert.assertFalse(ds2.committed);
Assert.assertFalse(ds1.postCommitted);
Assert.assertFalse(ds2.postCommitted);
Assert.assertTrue(ds1.rolledBack);
Assert.assertTrue(ds2.rolledBack);
Assert.assertEquals(txClient.state, DummyTxClient.CommitState.Aborted);
}
use of org.apache.tephra.TransactionContext in project cdap by caskdata.
the class DynamicTransactionExecutor method executeOnce.
private <I, O> O executeOnce(Function<I, O> function, I input) throws TransactionFailureException {
TransactionContext txContext;
try {
txContext = txContextSupplier.get();
} catch (RuntimeException e) {
// and this would represent a nested transaction.
if (e.getCause() instanceof TransactionFailureException) {
throw (TransactionFailureException) e.getCause();
} else {
throw e;
}
}
txContext.start();
O o = null;
try {
o = function.apply(input);
} catch (Throwable e) {
txContext.abort(new TransactionFailureException("Transaction function failure for transaction. ", e));
// abort will throw
}
// will throw if smth goes wrong
txContext.finish();
return o;
}
Aggregations