void addTransactionAware(TransactionAware txAware) { if (this.txContext != null) { txContext.addTransactionAware(txAware); } else if (this.tx != null) { txAwares.add(txAware); assert (tx != null); txAware.startTx(tx); } }
/** * Force all transaction-aware datasets participating in this context to flush their writes. */ public void flushOperations() throws Exception { for (TransactionAware txAware : txAwares) { txAware.commitTx(); } }
@Override public void postTxCommit() { if (txAware != null) { txAware.postTxCommit(); } }
/** * If a dataset is a new transaction-aware, it starts the transaction and remembers the dataset. */ private <T extends Dataset> void startDatasetTransaction(T dataset) { if (dataset instanceof TransactionAware) { TransactionAware txAware = (TransactionAware) dataset; if (transaction == null) { throw new IllegalStateException( "Transaction is not supported in the current runtime. Cannot set transaction on dataset " + txAware.getTransactionAwareName()); } if (txAwares.add(txAware)) { txAware.startTx(transaction); } } }
((TransactionAware) cube1).startTx(tx); ((TransactionAware) cube1).commitTx(); txClient.abort(tx); ((TransactionAware) cube1).rollbackTx(); ((TransactionAware) cube1).startTx(tx); ((TransactionAware) cube1).commitTx(); txClient.commitOrThrow(tx); ((TransactionAware) cube1).postTxCommit(); ((TransactionAware) cube2).startTx(tx); ((TransactionAware) cube2).commitTx(); txClient.commitOrThrow(tx); ((TransactionAware) cube2).postTxCommit(); } finally { txManager.stopAndWait();
@Test public void testBatchWritableKeyIsIgnored() throws Exception { String tableName = "batchWritableTable"; getTableAdmin(CONTEXT1, tableName).create(); try { // write in a transaction, three times, with key = null, a, q, always Put with row = a Transaction tx = txClient.startShort(); Table table = getTable(CONTEXT1, tableName); ((TransactionAware) table).startTx(tx); table.write(null, new Put("a").add("x", "x")); table.write(new byte[]{'q'}, new Put("a").add("y", "y")); table.write(new byte[]{'a'}, new Put("a").add("z", "z")); txClient.canCommitOrThrow(tx, ((TransactionAware) table).getTxChanges()); ((TransactionAware) table).commitTx(); txClient.commitOrThrow(tx); // validate that all writes went to row a, and row q was not written tx = txClient.startShort(); ((TransactionAware) table).startTx(tx); Assert.assertTrue(table.get(new Get("q")).isEmpty()); Row row = table.get(new Get("a")); Assert.assertEquals(3, row.getColumns().size()); Assert.assertEquals("x", row.getString("x")); Assert.assertEquals("y", row.getString("y")); Assert.assertEquals("z", row.getString("z")); ((TransactionAware) table).commitTx(); txClient.abort(tx); } finally { getTableAdmin(CONTEXT1, tableName).drop(); } }
@Override public boolean rollbackTx() throws Exception { boolean success = true; for (TransactionAware txAware : this) { success = success && txAware.rollbackTx(); } return success; }
private void testStringPutGet(Table t, String key, String col, String val) throws Exception { Transaction tx = txClient.startShort(); ((TransactionAware) t).startTx(tx); t.put(new Put(key, col, val)); Row row = t.get(new Get(key)); Assert.assertTrue(!row.isEmpty()); Assert.assertEquals(key, Bytes.toString(row.getRow())); Assert.assertEquals(1, row.getColumns().size()); Assert.assertEquals(col, Bytes.toString(row.getColumns().entrySet().iterator().next().getKey())); Assert.assertEquals(val, Bytes.toString(row.getColumns().entrySet().iterator().next().getValue())); Assert.assertEquals(val, Bytes.toString(row.get(col))); Assert.assertEquals(val, row.getString(col)); ((TransactionAware) t).rollbackTx(); txClient.abort(tx); }
/** * Calls postTxCommit on all the transaction-aware datasets participating in this context. * If any Throwable is encountered, it is suppressed and logged. */ public void postTxCommit() throws Exception { if (transaction == null) { return; } for (TransactionAware txAware : txAwares) { try { txAware.postTxCommit(); } catch (Throwable t) { LOG.error("Unable to perform post-commit in transaction-aware '{}' for transaction {}.", txAware.getTransactionAwareName(), transaction.getTransactionId(), t); } } }
private void commitAndAssertSuccess(Transaction tx, TransactionAware txAware) throws Exception { txClient.canCommitOrThrow(tx, txAware.getTxChanges()); Assert.assertTrue(txAware.commitTx()); txClient.commitOrThrow(tx); }
@Override public String getTransactionAwareName() { return getClass().getSimpleName() + "(" + txAware.getTransactionAwareName() + ")"; }
@Override public Collection<byte[]> getTxChanges() { return txAwares.getTxChanges(); }
@Override public void updateTx(Transaction transaction) { if (txAware != null) { txAware.updateTx(transaction); } }
Transaction tx1 = txClient.startShort(); Table table = getTable(CONTEXT1, MY_TABLE); ((TransactionAware) table).startTx(tx1); txClient.canCommitOrThrow(tx1, ((TransactionAware) table).getTxChanges()); Assert.assertTrue(((TransactionAware) table).commitTx()); txClient.commitOrThrow(tx1); ((TransactionAware) table).postTxCommit(); ((TransactionAware) table).startTx(tx2); verifyScanWithFuzzyRowFilter(table);
/** * If a dataset is a new transaction-aware, it starts the transaction and remembers the dataset. */ private <T extends Dataset> void startDatasetTransaction(T dataset) { if (dataset instanceof TransactionAware) { TransactionAware txAware = (TransactionAware) dataset; if (transaction == null) { throw new IllegalStateException( "Transaction is not supported in the current runtime. Cannot set transaction on dataset " + txAware.getTransactionAwareName()); } if (txAwares.add(txAware)) { txAware.startTx(transaction); } } }
@Override public boolean rollbackTx() throws Exception { if (consumer instanceof TransactionAware) { return ((TransactionAware) consumer).rollbackTx(); } return true; }
/** * Calls postTxCommit on all the transaction-aware datasets participating in this context. * If any Throwable is encountered, it is suppressed and logged. */ public void postTxCommit() throws Exception { if (transaction == null) { return; } for (TransactionAware txAware : txAwares) { try { txAware.postTxCommit(); } catch (Throwable t) { LOG.error("Unable to perform post-commit in transaction-aware '{}' for transaction {}.", txAware.getTransactionAwareName(), transaction.getTransactionId(), t); } } }
@Override public String getTransactionAwareName() { // todo: will go away, see comment at TransactionAware StringBuilder sb = new StringBuilder("{"); for (TransactionAware txAware : this) { sb.append(txAware.getTransactionAwareName()).append(","); } sb.replace(sb.length() - 1, sb.length() - 1, "}"); return sb.toString(); } }
@Override public Collection<byte[]> getTxChanges() { if (consumer instanceof TransactionAware) { return ((TransactionAware) consumer).getTxChanges(); } return ImmutableList.of(); }
@Override public void updateTx(Transaction tx) { if (consumer instanceof TransactionAware) { ((TransactionAware) consumer).updateTx(tx); } }