use of org.apache.tephra.Transaction in project cdap by caskdata.
the class QueueTest method testRollback.
@Test(timeout = TIMEOUT_MS)
public void testRollback() throws Exception {
QueueName queueName = QueueName.fromFlowlet(NamespaceId.DEFAULT.getEntityName(), "app", "flow", "flowlet", "queuerollback");
ConsumerConfig consumerConfig = new ConsumerConfig(0, 0, 1, DequeueStrategy.FIFO, null);
configureGroups(queueName, ImmutableList.of(consumerConfig));
try (QueueProducer producer = queueClientFactory.createProducer(queueName);
QueueConsumer consumer = queueClientFactory.createConsumer(queueName, consumerConfig, 1)) {
TransactionContext txContext = createTxContext(producer, consumer, new TransactionAware() {
boolean canCommit = false;
@Override
public void startTx(Transaction tx) {
}
@Override
public void updateTx(Transaction tx) {
}
@Override
public Collection<byte[]> getTxChanges() {
return ImmutableList.of();
}
@Override
public boolean commitTx() throws Exception {
// Flip-flop between commit success/failure.
boolean res = canCommit;
canCommit = !canCommit;
return res;
}
@Override
public void postTxCommit() {
}
@Override
public boolean rollbackTx() throws Exception {
return true;
}
@Override
public String getTransactionAwareName() {
return "test";
}
});
// First, try to enqueue and commit would fail
txContext.start();
try {
producer.enqueue(new QueueEntry(Bytes.toBytes(1)));
txContext.finish();
// If reaches here, it's wrong, as exception should be thrown.
Assert.assertTrue(false);
} catch (TransactionFailureException e) {
txContext.abort();
}
// Try to enqueue again. Within the same transaction, dequeue should be empty.
txContext.start();
producer.enqueue(new QueueEntry(Bytes.toBytes(1)));
Assert.assertTrue(consumer.dequeue().isEmpty());
txContext.finish();
// This time, enqueue has been committed, dequeue would see the item
txContext.start();
try {
Assert.assertEquals(1, Bytes.toInt(consumer.dequeue().iterator().next()));
txContext.finish();
// If reaches here, it's wrong, as exception should be thrown.
Assert.assertTrue(false);
} catch (TransactionFailureException e) {
txContext.abort();
}
// Dequeue again, since last tx was rollback, this dequeue should see the item again.
txContext.start();
Assert.assertEquals(1, Bytes.toInt(consumer.dequeue().iterator().next()));
txContext.finish();
}
}
use of org.apache.tephra.Transaction in project cdap by caskdata.
the class TableTest method testBasicColumnRangeWithTx.
@Test
public void testBasicColumnRangeWithTx() throws Exception {
// todo: test more tx logic (or add to get/put unit-test)
DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE);
admin.create();
try {
// write test data and commit
Transaction tx1 = txClient.startShort();
Table myTable1 = getTable(CONTEXT1, MY_TABLE);
((TransactionAware) myTable1).startTx(tx1);
myTable1.put(R1, a(C1, C2, C3, C4, C5), a(V1, V2, V3, V4, V5));
myTable1.put(R2, a(C1), a(V2));
txClient.canCommitOrThrow(tx1, ((TransactionAware) myTable1).getTxChanges());
Assert.assertTrue(((TransactionAware) myTable1).commitTx());
txClient.commitOrThrow(tx1);
// Now, we will test column range get
Transaction tx2 = txClient.startShort();
Table myTable2 = getTable(CONTEXT1, MY_TABLE);
((TransactionAware) myTable2).startTx(tx2);
// bounded range
TableAssert.assertRow(a(C2, V2, C3, V3, C4, V4), myTable2.get(R1, C2, C5, Integer.MAX_VALUE));
// open start range
TableAssert.assertRow(a(C1, V1, C2, V2, C3, V3), myTable2.get(R1, null, C4, Integer.MAX_VALUE));
// open end range
TableAssert.assertRow(a(C3, V3, C4, V4, C5, V5), myTable2.get(R1, C3, null, Integer.MAX_VALUE));
// open ends range
TableAssert.assertRow(a(C1, V1, C2, V2, C3, V3, C4, V4, C5, V5), myTable2.get(R1, null, null, Integer.MAX_VALUE));
// same with limit
// bounded range with limit
TableAssert.assertRow(a(C2, V2), myTable2.get(R1, C2, C5, 1));
// open start range with limit
TableAssert.assertRow(a(C1, V1, C2, V2), myTable2.get(R1, null, C4, 2));
// open end range with limit
TableAssert.assertRow(a(C3, V3, C4, V4), myTable2.get(R1, C3, null, 2));
// open ends range with limit
TableAssert.assertRow(a(C1, V1, C2, V2, C3, V3, C4, V4), myTable2.get(R1, null, null, 4));
// adding/changing/removing some columns
myTable2.put(R1, a(C1, C2, C3), a(V4, V3, V2));
myTable2.delete(R1, a(C4));
txClient.canCommitOrThrow(tx2, ((TransactionAware) myTable2).getTxChanges());
Assert.assertTrue(((TransactionAware) myTable2).commitTx());
txClient.commitOrThrow(tx2);
// Checking that changes are reflected in new scans in new tx
Transaction tx3 = txClient.startShort();
// NOTE: table can be re-used betweet tx
((TransactionAware) myTable1).startTx(tx3);
TableAssert.assertRow(a(C2, V3, C3, V2), myTable1.get(R1, C2, C5, Integer.MAX_VALUE));
txClient.canCommitOrThrow(tx3, ((TransactionAware) myTable1).getTxChanges());
Assert.assertTrue(((TransactionAware) myTable1).commitTx());
txClient.commitOrThrow(tx3);
} finally {
admin.drop();
}
}
use of org.apache.tephra.Transaction in project cdap by caskdata.
the class TableTest method testEmptyGet.
@Test
public void testEmptyGet() throws Exception {
DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE);
admin.create();
try {
Transaction tx = txClient.startShort();
Table myTable = getTable(CONTEXT1, MY_TABLE);
((TransactionAware) myTable).startTx(tx);
myTable.put(R1, C1, V1);
myTable.put(R1, C2, V2);
// to be used for validation later
TreeMap<byte[], byte[]> expectedColumns = new TreeMap<>(Bytes.BYTES_COMPARATOR);
expectedColumns.put(C1, V1);
expectedColumns.put(C2, V2);
Result expectedResult = new Result(R1, expectedColumns);
Result emptyResult = new Result(R1, ImmutableMap.<byte[], byte[]>of());
((TransactionAware) myTable).commitTx();
txClient.commitOrThrow(tx);
// start another transaction, so that the buffering table doesn't cache the values; the underlying Table
// implementations are tested this way.
tx = txClient.startShort();
((TransactionAware) myTable).startTx(tx);
Row row = myTable.get(R1, new byte[][] { C1, C2 });
assertEquals(expectedResult, row);
// passing in empty columns returns empty result
row = myTable.get(R1, new byte[][] {});
assertEquals(emptyResult, row);
// test all the Get constructors and their behavior
// constructors specifying only rowkey retrieve all columns
Get get = new Get(R1);
Assert.assertNull(get.getColumns());
assertEquals(expectedResult, myTable.get(get));
get = new Get(Bytes.toString(R1));
Assert.assertNull(get.getColumns());
assertEquals(expectedResult, myTable.get(get));
get.add(C1);
get.add(Bytes.toString(C2));
assertEquals(expectedResult, myTable.get(get));
// constructor specifying columns, but with an empty array/collection retrieve 0 columns
get = new Get(R1, new byte[][] {});
Assert.assertNotNull(get.getColumns());
assertEquals(emptyResult, myTable.get(get));
get = new Get(R1, ImmutableList.<byte[]>of());
Assert.assertNotNull(get.getColumns());
assertEquals(emptyResult, myTable.get(get));
get = new Get(Bytes.toString(R1), new String[] {});
Assert.assertNotNull(get.getColumns());
assertEquals(emptyResult, myTable.get(get));
get = new Get(Bytes.toString(R1), ImmutableList.<String>of());
Assert.assertNotNull(get.getColumns());
assertEquals(emptyResult, myTable.get(get));
row = myTable.get(R1, new byte[][] {});
assertEquals(emptyResult, row);
txClient.abort(tx);
} finally {
admin.drop();
}
}
use of org.apache.tephra.Transaction in project cdap by caskdata.
the class TableTest method testBasicDeleteWithTx.
@Test
public void testBasicDeleteWithTx() throws Exception {
// we will test 3 different delete column ops and one delete row op
// * delete column with delete
// * delete column with put null value
// * delete column with put byte[0] value
// * delete row with delete
DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE);
admin.create();
try {
// write smth and commit
Transaction tx1 = txClient.startShort();
Table myTable1 = getTable(CONTEXT1, MY_TABLE);
((TransactionAware) myTable1).startTx(tx1);
myTable1.put(R1, a(C1, C2), a(V1, V2));
myTable1.put(R2, a(C1, C2), a(V2, V3));
myTable1.put(R3, a(C1, C2), a(V3, V4));
myTable1.put(R4, a(C1, C2), a(V4, V5));
txClient.canCommitOrThrow(tx1, ((TransactionAware) myTable1).getTxChanges());
Assert.assertTrue(((TransactionAware) myTable1).commitTx());
txClient.commitOrThrow(tx1);
// Now, we will test delete ops
// start new tx2
Transaction tx2 = txClient.startShort();
Table myTable2 = getTable(CONTEXT1, MY_TABLE);
((TransactionAware) myTable2).startTx(tx2);
// TableAssert.verify tx2 sees changes of tx1
TableAssert.assertRow(a(C1, V1, C2, V2), myTable2.get(R1, a(C1, C2)));
// TableAssert.verify tx2 sees changes of tx1
TableAssert.assertRow(a(C1, V2, C2, V3), myTable2.get(R2));
// delete c1, r2
myTable2.delete(R1, a(C1));
myTable2.delete(R2);
// same as delete a column
myTable2.put(R3, C1, null);
// TableAssert.verify can see deletes in own changes before commit
TableAssert.assertRow(a(C2, V2), myTable2.get(R1, a(C1, C2)));
Assert.assertArrayEquals(null, myTable2.get(R1, C1));
Assert.assertArrayEquals(V2, myTable2.get(R1, C2));
TableAssert.assertRow(a(), myTable2.get(R2));
TableAssert.assertRow(a(C2, V4), myTable2.get(R3));
// overwrite c2 and write new value to c1
myTable2.put(R1, a(C1, C2), a(V3, V4));
myTable2.put(R2, a(C1, C2), a(V4, V5));
myTable2.put(R3, a(C1, C2), a(V1, V2));
myTable2.put(R4, a(C1, C2), a(V2, V3));
// TableAssert.verify can see changes in own changes before commit
TableAssert.assertRow(a(C1, V3, C2, V4), myTable2.get(R1, a(C1, C2, C3)));
Assert.assertArrayEquals(V3, myTable2.get(R1, C1));
Assert.assertArrayEquals(V4, myTable2.get(R1, C2));
Assert.assertArrayEquals(null, myTable2.get(R1, C3));
TableAssert.assertRow(a(C1, V4, C2, V5), myTable2.get(R2));
TableAssert.assertRow(a(C1, V1, C2, V2), myTable2.get(R3));
TableAssert.assertRow(a(C1, V2, C2, V3), myTable2.get(R4));
// delete c2 and r2
myTable2.delete(R1, a(C2));
myTable2.delete(R2);
myTable2.put(R1, C2, null);
// TableAssert.verify that delete is there (i.e. not reverted to whatever was persisted before)
TableAssert.assertRow(a(C1, V3), myTable2.get(R1, a(C1, C2)));
Assert.assertArrayEquals(V3, myTable2.get(R1, C1));
Assert.assertArrayEquals(null, myTable2.get(R1, C2));
TableAssert.assertRow(a(), myTable2.get(R2));
Assert.assertArrayEquals(V1, myTable2.get(R3, C1));
Assert.assertArrayEquals(V2, myTable2.get(R4, C1));
// start tx3 and TableAssert.verify that changes of tx2 are not visible yet
Transaction tx3 = txClient.startShort();
// NOTE: table instance can be re-used between tx
((TransactionAware) myTable1).startTx(tx3);
TableAssert.assertRow(a(C1, V1, C2, V2), myTable1.get(R1, a(C1, C2)));
Assert.assertArrayEquals(V1, myTable1.get(R1, C1));
Assert.assertArrayEquals(V2, myTable1.get(R1, C2));
Assert.assertArrayEquals(null, myTable1.get(R1, C3));
TableAssert.assertRow(a(C1, V1, C2, V2), myTable1.get(R1));
TableAssert.assertRow(a(C1, V2, C2, V3), myTable1.get(R2));
TableAssert.assertRow(a(C1, V3, C2, V4), myTable1.get(R3));
TableAssert.assertRow(a(C1, V4, C2, V5), myTable1.get(R4));
txClient.canCommitOrThrow(tx3, ((TransactionAware) myTable1).getTxChanges());
Assert.assertTrue(((TransactionAware) myTable1).commitTx());
txClient.commitOrThrow(tx3);
// starting tx4 before committing tx2 so that we can check conflicts are detected wrt deletes
Transaction tx4 = txClient.startShort();
// starting tx5 before committing tx2 so that we can check conflicts are detected wrt deletes
Transaction tx5 = txClient.startShort();
// commit tx2 in stages to see how races are handled wrt delete ops
txClient.canCommitOrThrow(tx2, ((TransactionAware) myTable2).getTxChanges());
Assert.assertTrue(((TransactionAware) myTable2).commitTx());
// start tx6 and TableAssert.verify that changes of tx2 are not visible yet (even though they are flushed)
Transaction tx6 = txClient.startShort();
// NOTE: table instance can be re-used between tx
((TransactionAware) myTable1).startTx(tx6);
TableAssert.assertRow(a(C1, V1, C2, V2), myTable1.get(R1, a(C1, C2)));
Assert.assertArrayEquals(V1, myTable1.get(R1, C1));
Assert.assertArrayEquals(V2, myTable1.get(R1, C2));
Assert.assertArrayEquals(null, myTable1.get(R1, C3));
TableAssert.assertRow(a(C1, V1, C2, V2), myTable1.get(R1));
TableAssert.assertRow(a(C1, V2, C2, V3), myTable1.get(R2));
TableAssert.assertRow(a(C1, V3, C2, V4), myTable1.get(R3));
TableAssert.assertRow(a(C1, V4, C2, V5), myTable1.get(R4));
txClient.canCommitOrThrow(tx6, ((TransactionAware) myTable1).getTxChanges());
Assert.assertTrue(((TransactionAware) myTable1).commitTx());
txClient.commitOrThrow(tx6);
// make tx2 visible
txClient.commitOrThrow(tx2);
// start tx7 and TableAssert.verify that changes of tx2 are now visible
Transaction tx7 = txClient.startShort();
// NOTE: table instance can be re-used between tx
((TransactionAware) myTable1).startTx(tx7);
TableAssert.assertRow(a(C1, V3), myTable1.get(R1, a(C1, C2)));
TableAssert.assertRow(a(C1, V3), myTable1.get(R1));
Assert.assertArrayEquals(V3, myTable1.get(R1, C1));
Assert.assertArrayEquals(null, myTable1.get(R1, C2));
TableAssert.assertRow(a(C1, V3), myTable1.get(R1, a(C1, C2)));
TableAssert.assertRow(a(), myTable1.get(R2));
Assert.assertArrayEquals(V1, myTable1.get(R3, C1));
Assert.assertArrayEquals(V2, myTable1.get(R4, C1));
txClient.canCommitOrThrow(tx6, ((TransactionAware) myTable1).getTxChanges());
Assert.assertTrue(((TransactionAware) myTable1).commitTx());
txClient.commitOrThrow(tx7);
// but not visible to tx4 that we started earlier than tx2 became visible
// NOTE: table instance can be re-used between tx
((TransactionAware) myTable1).startTx(tx4);
TableAssert.assertRow(a(C1, V1, C2, V2), myTable1.get(R1, a(C1, C2)));
Assert.assertArrayEquals(V1, myTable1.get(R1, C1));
Assert.assertArrayEquals(V2, myTable1.get(R1, C2));
Assert.assertArrayEquals(null, myTable1.get(R1, C3));
TableAssert.assertRow(a(C1, V1, C2, V2), myTable1.get(R1));
TableAssert.assertRow(a(C1, V2, C2, V3), myTable1.get(R2));
TableAssert.assertRow(a(C1, V3, C2, V4), myTable1.get(R3));
TableAssert.assertRow(a(C1, V4, C2, V5), myTable1.get(R4));
// writing to deleted column, to check conflicts are detected (delete-write conflict)
myTable1.put(R1, a(C2), a(V5));
try {
txClient.canCommitOrThrow(tx4, ((TransactionAware) myTable1).getTxChanges());
Assert.fail("Conflict not detected!");
} catch (TransactionConflictException e) {
// expected
}
((TransactionAware) myTable1).rollbackTx();
txClient.abort(tx4);
// deleting changed column, to check conflicts are detected (write-delete conflict)
// NOTE: table instance can be re-used between tx
((TransactionAware) myTable1).startTx(tx5);
TableAssert.assertRow(a(C1, V1, C2, V2), myTable1.get(R1, a(C1, C2)));
Assert.assertArrayEquals(V1, myTable1.get(R1, C1));
Assert.assertArrayEquals(V2, myTable1.get(R1, C2));
Assert.assertArrayEquals(null, myTable1.get(R1, C3));
TableAssert.assertRow(a(C1, V1, C2, V2), myTable1.get(R1));
TableAssert.assertRow(a(C1, V2, C2, V3), myTable1.get(R2));
TableAssert.assertRow(a(C1, V3, C2, V4), myTable1.get(R3));
TableAssert.assertRow(a(C1, V4, C2, V5), myTable1.get(R4));
// NOTE: we are TableAssert.verifying conflict in one operation only. We may want to test each...
myTable1.delete(R1, a(C1));
try {
txClient.canCommitOrThrow(tx5, ((TransactionAware) myTable1).getTxChanges());
Assert.fail("Conflict not detected!");
} catch (TransactionConflictException e) {
// expected
}
((TransactionAware) myTable1).rollbackTx();
txClient.abort(tx5);
} finally {
admin.drop();
}
}
use of org.apache.tephra.Transaction in project cdap by caskdata.
the class TableTest method testTxUsingMultipleTables.
@Test
public void testTxUsingMultipleTables() throws Exception {
String table1 = "table1";
String table2 = "table2";
String table3 = "table3";
String table4 = "table4";
getTableAdmin(CONTEXT1, table1).create();
getTableAdmin(CONTEXT1, table2).create();
getTableAdmin(CONTEXT1, table3).create();
getTableAdmin(CONTEXT1, table4).create();
try {
// We will be changing:
// * table1 and table2 in tx1
// * table2 and table3 in tx2 << will conflict with first one
// * table3 and table4 in tx3
Transaction tx1 = txClient.startShort();
Transaction tx2 = txClient.startShort();
Transaction tx3 = txClient.startShort();
// Write data in tx1 and commit
Table table11 = getTable(CONTEXT1, table1);
((TransactionAware) table11).startTx(tx1);
// write r1->c1,v1 but not commit
table11.put(R1, a(C1), a(V1));
Table table21 = getTable(CONTEXT1, table2);
((TransactionAware) table21).startTx(tx1);
// write r1->c1,v2 but not commit
table21.put(R1, a(C1), a(V2));
// TableAssert.verify writes inside same tx
TableAssert.assertRow(a(C1, V1), table11.get(R1, a(C1)));
TableAssert.assertRow(a(C1, V2), table21.get(R1, a(C1)));
// commit tx1
txClient.canCommitOrThrow(tx1, ImmutableList.copyOf(Iterables.concat(((TransactionAware) table11).getTxChanges(), ((TransactionAware) table21).getTxChanges())));
Assert.assertTrue(((TransactionAware) table11).commitTx());
Assert.assertTrue(((TransactionAware) table21).commitTx());
txClient.commitOrThrow(tx1);
// Write data in tx2 and check that cannot commit because of conflicts
Table table22 = getTable(CONTEXT1, table2);
((TransactionAware) table22).startTx(tx2);
// write r1->c1,v1 but not commit
table22.put(R1, a(C1), a(V2));
Table table32 = getTable(CONTEXT1, table3);
((TransactionAware) table32).startTx(tx2);
// write r1->c1,v2 but not commit
table32.put(R1, a(C1), a(V3));
// TableAssert.verify writes inside same tx
TableAssert.assertRow(a(C1, V2), table22.get(R1, a(C1)));
TableAssert.assertRow(a(C1, V3), table32.get(R1, a(C1)));
// try commit tx2
try {
txClient.canCommitOrThrow(tx2, ImmutableList.copyOf(Iterables.concat(((TransactionAware) table22).getTxChanges(), ((TransactionAware) table32).getTxChanges())));
Assert.fail("Conflict not detected!");
} catch (TransactionConflictException e) {
// expected
}
Assert.assertTrue(((TransactionAware) table22).rollbackTx());
Assert.assertTrue(((TransactionAware) table32).rollbackTx());
txClient.abort(tx2);
// Write data in tx3 and check that can commit (no conflicts)
Table table33 = getTable(CONTEXT1, table3);
((TransactionAware) table33).startTx(tx3);
// write r1->c1,v1 but not commit
table33.put(R1, a(C1), a(V3));
Table table43 = getTable(CONTEXT1, table4);
((TransactionAware) table43).startTx(tx3);
// write r1->c1,v2 but not commit
table43.put(R1, a(C1), a(V4));
// TableAssert.verify writes inside same tx
TableAssert.assertRow(a(C1, V3), table33.get(R1, a(C1)));
TableAssert.assertRow(a(C1, V4), table43.get(R1, a(C1)));
// commit tx3
txClient.canCommitOrThrow(tx3, ImmutableList.copyOf(Iterables.concat(((TransactionAware) table33).getTxChanges(), ((TransactionAware) table43).getTxChanges())));
Assert.assertTrue(((TransactionAware) table33).commitTx());
Assert.assertTrue(((TransactionAware) table43).commitTx());
txClient.commitOrThrow(tx3);
} finally {
getTableAdmin(CONTEXT1, table1).drop();
getTableAdmin(CONTEXT1, table2).drop();
getTableAdmin(CONTEXT1, table3).drop();
getTableAdmin(CONTEXT1, table4).drop();
}
}
Aggregations