use of org.apache.tephra.TransactionAware in project cdap by caskdata.
the class TableTest method testBasicCompareAndSwapWithTx.
@Test
public void testBasicCompareAndSwapWithTx() throws Exception {
DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE);
admin.create();
try {
Transaction tx1 = txClient.startShort();
Table myTable1 = getTable(CONTEXT1, MY_TABLE);
((TransactionAware) myTable1).startTx(tx1);
// write r1->c1,v1 but not commit
myTable1.put(R1, a(C1), a(V1));
// write r1->c2,v2 but not commit
Assert.assertTrue(myTable1.compareAndSwap(R1, C2, null, V5));
// TableAssert.verify compare and swap result visible inside tx before commit
TableAssert.assertRow(a(C1, V1, C2, V5), myTable1.get(R1, a(C1, C2)));
Assert.assertArrayEquals(V1, myTable1.get(R1, C1));
Assert.assertArrayEquals(V5, myTable1.get(R1, C2));
Assert.assertArrayEquals(null, myTable1.get(R1, C3));
TableAssert.assertRow(a(C1, V1, C2, V5), myTable1.get(R1));
// these should fail
Assert.assertFalse(myTable1.compareAndSwap(R1, C1, null, V1));
Assert.assertFalse(myTable1.compareAndSwap(R1, C1, V2, V1));
Assert.assertFalse(myTable1.compareAndSwap(R1, C2, null, V2));
Assert.assertFalse(myTable1.compareAndSwap(R1, C2, V2, V1));
// but this should succeed
Assert.assertTrue(myTable1.compareAndSwap(R1, C2, V5, V2));
// start new tx (doesn't see changes of the tx1)
Transaction tx2 = txClient.startShort();
// committing tx1 in stages to check races are handled well
// * first, flush operations of table
Assert.assertTrue(txClient.canCommit(tx1, ((TransactionAware) myTable1).getTxChanges()));
Assert.assertTrue(((TransactionAware) myTable1).commitTx());
// check that tx2 doesn't see changes (even though they were flushed) of tx1 by trying to compareAndSwap
// assuming current value is null
Table myTable2 = getTable(CONTEXT1, MY_TABLE);
((TransactionAware) myTable2).startTx(tx2);
Assert.assertTrue(myTable2.compareAndSwap(R1, C1, null, V3));
// start tx3 and TableAssert.verify same thing again
Transaction tx3 = txClient.startShort();
Table myTable3 = getTable(CONTEXT1, MY_TABLE);
((TransactionAware) myTable3).startTx(tx3);
Assert.assertTrue(myTable3.compareAndSwap(R1, C1, null, V2));
// * second, make tx visible
Assert.assertTrue(txClient.commit(tx1));
// TableAssert.verify that tx2 cannot commit because of the conflicts...
Assert.assertFalse(txClient.canCommit(tx2, ((TransactionAware) myTable2).getTxChanges()));
((TransactionAware) myTable2).rollbackTx();
txClient.abort(tx2);
// start tx4 and TableAssert.verify that changes of tx1 are now visible
Transaction tx4 = txClient.startShort();
Table myTable4 = getTable(CONTEXT1, MY_TABLE);
((TransactionAware) myTable4).startTx(tx4);
TableAssert.assertRow(a(C1, V1, C2, V2), myTable4.get(R1, a(C1, C2)));
Assert.assertArrayEquals(V1, myTable4.get(R1, C1));
Assert.assertArrayEquals(V2, myTable4.get(R1, C2));
Assert.assertArrayEquals(null, myTable4.get(R1, C3));
TableAssert.assertRow(a(C2, V2), myTable4.get(R1, a(C2)));
TableAssert.assertRow(a(C1, V1, C2, V2), myTable4.get(R1));
// tx3 still cannot see tx1 changes
Assert.assertTrue(myTable3.compareAndSwap(R1, C2, null, V5));
// and it cannot commit because its changes cause conflicts
Assert.assertFalse(txClient.canCommit(tx3, ((TransactionAware) myTable3).getTxChanges()));
((TransactionAware) myTable3).rollbackTx();
txClient.abort(tx3);
// TableAssert.verify we can do some ops with tx4 based on data written with tx1
Assert.assertFalse(myTable4.compareAndSwap(R1, C1, null, V4));
Assert.assertFalse(myTable4.compareAndSwap(R1, C2, null, V5));
Assert.assertTrue(myTable4.compareAndSwap(R1, C1, V1, V3));
Assert.assertTrue(myTable4.compareAndSwap(R1, C2, V2, V4));
myTable4.delete(R1, a(C1));
// committing tx4
Assert.assertTrue(txClient.canCommit(tx4, ((TransactionAware) myTable3).getTxChanges()));
Assert.assertTrue(((TransactionAware) myTable4).commitTx());
Assert.assertTrue(txClient.commit(tx4));
// TableAssert.verifying the result contents in next transaction
Transaction tx5 = txClient.startShort();
// NOTE: table instance can be re-used in series of transactions
((TransactionAware) myTable4).startTx(tx5);
TableAssert.assertRow(a(C2, V4), myTable4.get(R1, a(C1, C2)));
Assert.assertArrayEquals(null, myTable4.get(R1, C1));
Assert.assertArrayEquals(V4, myTable4.get(R1, C2));
TableAssert.assertRow(a(C2, V4), myTable4.get(R1));
Assert.assertTrue(txClient.canCommit(tx5, ((TransactionAware) myTable3).getTxChanges()));
Assert.assertTrue(((TransactionAware) myTable3).commitTx());
Assert.assertTrue(txClient.commit(tx5));
} finally {
admin.drop();
}
}
use of org.apache.tephra.TransactionAware in project cdap by caskdata.
the class TableTest method testMetrics.
private void testMetrics(boolean readless) throws Exception {
final String tableName = "survive";
DatasetProperties props = TableProperties.builder().setReadlessIncrementSupport(readless).build();
DatasetAdmin admin = getTableAdmin(CONTEXT1, tableName, props);
admin.create();
Table table = getTable(CONTEXT1, tableName, props);
final Map<String, Long> metrics = Maps.newHashMap();
((MeteredDataset) table).setMetricsCollector(new MetricsCollector() {
@Override
public void increment(String metricName, long value) {
Long old = metrics.get(metricName);
metrics.put(metricName, old == null ? value : old + value);
}
@Override
public void gauge(String metricName, long value) {
metrics.put(metricName, value);
}
});
// Note that we don't need to finish tx for metrics to be reported
Transaction tx0 = txClient.startShort();
((TransactionAware) table).startTx(tx0);
int writes = 0;
int reads = 0;
table.put(new Put(R1, C1, V1));
verifyDatasetMetrics(metrics, ++writes, reads);
table.compareAndSwap(R1, C1, V1, V2);
verifyDatasetMetrics(metrics, ++writes, ++reads);
// note: will not write anything as expected value will not match
table.compareAndSwap(R1, C1, V1, V2);
verifyDatasetMetrics(metrics, writes, ++reads);
table.increment(new Increment(R2, C2, 1L));
if (readless) {
verifyDatasetMetrics(metrics, ++writes, reads);
} else {
verifyDatasetMetrics(metrics, ++writes, ++reads);
}
table.incrementAndGet(new Increment(R2, C2, 1L));
verifyDatasetMetrics(metrics, ++writes, ++reads);
table.get(new Get(R1, C1, V1));
verifyDatasetMetrics(metrics, writes, ++reads);
Scanner scanner = table.scan(new Scan(null, null));
while (scanner.next() != null) {
verifyDatasetMetrics(metrics, writes, ++reads);
}
table.delete(new Delete(R1, C1, V1));
verifyDatasetMetrics(metrics, ++writes, reads);
// drop table
admin.drop();
}
use of org.apache.tephra.TransactionAware in project cdap by caskdata.
the class TableTest method testBasicIncrementWriteWithTxSmall.
private void testBasicIncrementWriteWithTxSmall(boolean readless) throws Exception {
DatasetProperties props = TableProperties.builder().setReadlessIncrementSupport(readless).build();
DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE, props);
admin.create();
Table myTable = getTable(CONTEXT1, MY_TABLE, props);
// start 1st tx
Transaction tx = txClient.startShort();
((TransactionAware) myTable).startTx(tx);
myTable.increment(R1, a(C1), la(-3L));
// we'll use this one to test that we can delete increment and increment again
myTable.increment(R2, a(C2), la(5L));
commitAndAssertSuccess(tx, (TransactionAware) myTable);
// start 2nd tx
tx = txClient.startShort();
((TransactionAware) myTable).startTx(tx);
Assert.assertArrayEquals(Bytes.toBytes(-3L), myTable.get(R1, C1));
myTable.increment(R1, a(C1), la(-3L));
Assert.assertArrayEquals(Bytes.toBytes(-6L), myTable.get(R1, C1));
Assert.assertArrayEquals(Bytes.toBytes(5L), myTable.get(R2, C2));
myTable.delete(R2, C2);
Assert.assertArrayEquals(null, myTable.get(R2, C2));
commitAndAssertSuccess(tx, (TransactionAware) myTable);
// start 3rd tx
tx = txClient.startShort();
((TransactionAware) myTable).startTx(tx);
Assert.assertArrayEquals(Bytes.toBytes(-6L), myTable.get(R1, C1));
Assert.assertArrayEquals(null, myTable.get(R2, C2));
myTable.increment(R2, a(C2), la(7L));
Assert.assertArrayEquals(Bytes.toBytes(7L), myTable.get(R2, C2));
commitAndAssertSuccess(tx, (TransactionAware) myTable);
// start 4rd tx
tx = txClient.startShort();
((TransactionAware) myTable).startTx(tx);
Assert.assertArrayEquals(Bytes.toBytes(7L), myTable.get(R2, C2));
commitAndAssertSuccess(tx, (TransactionAware) myTable);
admin.drop();
}
use of org.apache.tephra.TransactionAware in project cdap by caskdata.
the class TableTest method testBatchWritableKeyIsIgnored.
@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"));
Assert.assertTrue(txClient.canCommit(tx, ((TransactionAware) table).getTxChanges()));
((TransactionAware) table).commitTx();
Assert.assertTrue(txClient.commit(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();
}
}
use of org.apache.tephra.TransactionAware in project cdap by caskdata.
the class TableTest method testReadOwnWrite.
@Test
public void testReadOwnWrite() throws Exception {
final String tableName = "readOwnWrite";
DatasetAdmin admin = getTableAdmin(CONTEXT1, tableName);
admin.create();
Table table = getTable(CONTEXT1, tableName);
Transaction tx = txClient.startShort();
try {
((TransactionAware) table).startTx(tx);
// Write some data, then flush it by calling commitTx.
table.put(new Put(R1, C1, V1));
((TransactionAware) table).commitTx();
// Try to read the previous write.
Assert.assertArrayEquals(V1, table.get(new Get(R1, C1)).get(C1));
} finally {
txClient.commit(tx);
}
// drop table
admin.drop();
}
Aggregations