private <I, O> O executeOnce(Function<I, O> function, I input) throws TransactionFailureException { TransactionContext txContext = new TransactionContext(txClient, txAwares); 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; } }
@Override public void addTransactionAware(TransactionAware transactionAware) { transactionAwares.add(transactionAware); if (transactionContext != null) { transactionContext.addTransactionAware(transactionAware); } }
public Result[] get(List<Get> gets) throws IOException { try { transactionContext.start(); Result[] result = transactionAwareHTable.get(gets); transactionContext.finish(); return result; } catch (Exception e) { try { transactionContext.abort(); } catch (TransactionFailureException e1) { throw new IOException("Could not rollback transaction", e1); } } return null; }
try { TransactionAwareHTable table = new TransactionAwareHTable(conn.getTable(TABLE)); TransactionContext context = new TransactionContext(txClient, table); context.start(); long totalBalance = 0; ResultScanner scanner = table.getScanner(new Scan()); LOG.info("FAILED! Total balance should be 0 but was {}", totalBalance); context.finish(); } catch (Exception e) { LOG.error("Failed verification check", e);
/** * Sets up any resources needed by the individual client. * * @param txClient the transaction client to use in accessing the transaciton service * @param table the HBase table instance to use for accessing storage */ public void init(TransactionSystemClient txClient, HTableInterface table) { txTable = new TransactionAwareHTable(table); txContext = new TransactionContext(txClient, txTable); }
@Test public void testAndThenRemoveOnFailure() throws TransactionFailureException { ds1.failCommitTxOnce = InduceFailure.ThrowException; TransactionContext context = newTransactionContext(); context.start(); Assert.assertTrue(context.addTransactionAware(ds1)); ds1.addChange(A); try { context.finish(); Assert.fail("Persist should have failed - exception should be thrown"); } catch (TransactionFailureException e) { Assert.assertEquals("persist failure", e.getCause().getMessage()); } Assert.assertTrue(context.removeTransactionAware(ds1)); // Verify ds1 is rolled back Assert.assertTrue(ds1.started); Assert.assertTrue(ds1.checked); Assert.assertTrue(ds1.committed); Assert.assertFalse(ds1.postCommitted); Assert.assertTrue(ds1.rolledBack); Assert.assertEquals(txClient.state, DummyTxClient.CommitState.Aborted); }
@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); }
/** * Aborts the given transaction, and rolls back all data set changes. If rollback fails, * the transaction is invalidated. If an exception is caught during rollback, the exception * is rethrown wrapped in a TransactionFailureException, after all remaining TransactionAwares have * completed rollback. * * @throws TransactionFailureException for any exception that is encountered. */ public void abort() throws TransactionFailureException { abort(null); }
byte[] fenceId = "test_table".getBytes(Charsets.UTF_8); final TransactionContext fence1 = new TransactionContext(new InMemoryTxSystemClient(txManager), VisibilityFence.create(fenceId)); fence1.start();
@Override public Transaction startShort() { Transaction transaction = super.startShort(); try { switch (attempts.getAndIncrement()) { case 0: fence1.finish(); break; case 1: fence2.finish(); break; case 2: break; default: throw new IllegalStateException("Unexpected state"); } } catch (TransactionFailureException e) { Throwables.propagate(e); } return transaction; } };
@Test public void testStartAndRollbackFailure() throws TransactionFailureException, InterruptedException { ds1.failStartTxOnce = InduceFailure.ThrowException; TransactionContext context = newTransactionContext(ds1, ds2); // start transaction try { context.start(); Assert.fail("start failed - exception should be thrown"); } catch (TransactionFailureException e) { Assert.assertEquals("start failure", e.getCause().getMessage()); } // verify both are not rolled back and tx is aborted Assert.assertTrue(ds1.started); Assert.assertFalse(ds2.started); Assert.assertFalse(ds1.checked); Assert.assertFalse(ds2.checked); Assert.assertFalse(ds1.committed); Assert.assertFalse(ds2.committed); Assert.assertFalse(ds1.postCommitted); Assert.assertFalse(ds2.postCommitted); Assert.assertFalse(ds1.rolledBack); Assert.assertFalse(ds2.rolledBack); Assert.assertEquals(txClient.state, DummyTxClient.CommitState.Aborted); }
public Result[] get(List<Get> gets) throws IOException { try { transactionContext.start(); Result[] result = transactionAwareHTable.get(gets); transactionContext.finish(); return result; } catch (Exception e) { try { transactionContext.abort(); } catch (TransactionFailureException e1) { throw new IOException("Could not rollback transaction", e1); } } return null; }
@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); }
try { TransactionAwareHTable table = new TransactionAwareHTable(conn.getTable(TABLE)); TransactionContext context = new TransactionContext(txClient, table); context.start(); long totalBalance = 0; ResultScanner scanner = table.getScanner(new Scan()); LOG.info("FAILED! Total balance should be 0 but was {}", totalBalance); context.finish(); } catch (Exception e) { LOG.error("Failed verification check", e);
/** * Sets up any resources needed by the individual client. * * @param txClient the transaction client to use in accessing the transaciton service * @param table the HBase table instance to use for accessing storage */ public void init(TransactionSystemClient txClient, HTableInterface table) { txTable = new TransactionAwareHTable(table); txContext = new TransactionContext(txClient, txTable); }
@Test public void testAddThenRemoveSuccess() throws TransactionFailureException { TransactionContext context = newTransactionContext(); context.start(); Assert.assertTrue(context.addTransactionAware(ds1)); ds1.addChange(A); try { context.removeTransactionAware(ds1); Assert.fail("Removal of TransactionAware should fails when there is active transaction."); } catch (IllegalStateException e) { // Expected } context.finish(); Assert.assertTrue(context.removeTransactionAware(ds1)); // Removing a TransactionAware not added before should returns false Assert.assertFalse(context.removeTransactionAware(ds2)); // Verify ds1 is committed and post-committed Assert.assertTrue(ds1.started); Assert.assertTrue(ds1.checked); Assert.assertTrue(ds1.committed); Assert.assertTrue(ds1.postCommitted); Assert.assertFalse(ds1.rolledBack); // Verify nothing happen to ds2 Assert.assertFalse(ds2.started); Assert.assertFalse(ds2.checked); Assert.assertFalse(ds2.committed); Assert.assertFalse(ds2.postCommitted); Assert.assertFalse(ds2.rolledBack); Assert.assertEquals(txClient.state, DummyTxClient.CommitState.Committed); }
context.start(); context.addTransactionAware(ds2); context.finish(); Assert.fail("Persist should have failed - exception should be thrown"); } catch (TransactionFailureException e) {