use of org.apache.tephra.Transaction in project cdap by caskdata.
the class TableTest method testBasicScanWithTx.
@Test
public void testBasicScanWithTx() throws Exception {
// todo: make work with tx well (merge with buffer, conflicts) and add tests for that
DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE);
admin.create();
try {
// write r1...r5 and commit
Transaction tx1 = txClient.startShort();
Table myTable1 = getTable(CONTEXT1, MY_TABLE);
((TransactionAware) myTable1).startTx(tx1);
myTable1.put(R1, a(C1), a(V1));
myTable1.put(R2, a(C2), a(V2));
myTable1.put(R3, a(C3, C4), a(V3, V4));
myTable1.put(R4, a(C4), a(V4));
myTable1.put(R5, a(C5), a(V5));
txClient.canCommitOrThrow(tx1, ((TransactionAware) myTable1).getTxChanges());
Assert.assertTrue(((TransactionAware) myTable1).commitTx());
txClient.commitOrThrow(tx1);
// Now, we will test scans
// currently not testing races/conflicts/etc as this logic is not there for scans yet; so using one same tx
Transaction tx2 = txClient.startShort();
Table myTable2 = getTable(CONTEXT1, MY_TABLE);
((TransactionAware) myTable2).startTx(tx2);
// bounded scan
TableAssert.assertScan(a(R2, R3, R4), aa(a(C2, V2), a(C3, V3, C4, V4), a(C4, V4)), myTable2, new Scan(R2, R5));
// open start scan
TableAssert.assertScan(a(R1, R2, R3), aa(a(C1, V1), a(C2, V2), a(C3, V3, C4, V4)), myTable2, new Scan(null, R4));
// open end scan
TableAssert.assertScan(a(R3, R4, R5), aa(a(C3, V3, C4, V4), a(C4, V4), a(C5, V5)), myTable2, new Scan(R3, null));
// open ends scan
TableAssert.assertScan(a(R1, R2, R3, R4, R5), aa(a(C1, V1), a(C2, V2), a(C3, V3, C4, V4), a(C4, V4), a(C5, V5)), myTable2, new Scan(null, null));
// adding/changing/removing some columns
myTable2.put(R2, a(C1, C2, C3), a(V4, V3, V2));
myTable2.delete(R3, 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.assertScan(a(R2, R3, R4), aa(a(C1, V4, C2, V3, C3, V2), a(C3, V3), a(C4, V4)), myTable1, new Scan(R2, R5));
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 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.commitOrThrow(tx);
}
// drop table
admin.drop();
}
use of org.apache.tephra.Transaction in project cdap by caskdata.
the class TableTest method testEmptyValuePut.
// TODO figure out what to do with this. As long as ObjectMappedTable writes empty values, we cannot
// throw exceptions, and this test is pointless.
@Ignore
@Test
public void testEmptyValuePut() throws Exception {
DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE);
admin.create();
Transaction tx = txClient.startShort();
try {
Table myTable = getTable(CONTEXT1, MY_TABLE);
try {
myTable.put(R1, C1, MT);
Assert.fail("Put with empty value should fail.");
} catch (IllegalArgumentException e) {
// expected
}
try {
myTable.put(R1, a(C1, C2), a(V1, MT));
Assert.fail("Put with empty value should fail.");
} catch (IllegalArgumentException e) {
// expected
}
try {
myTable.put(new Put(R1).add(C1, V1).add(C2, MT));
Assert.fail("Put with empty value should fail.");
} catch (IllegalArgumentException e) {
// expected
}
try {
myTable.compareAndSwap(R1, C1, V1, MT);
Assert.fail("CompareAndSwap with empty value should fail.");
} catch (IllegalArgumentException e) {
// expected
}
} finally {
txClient.abort(tx);
admin.drop();
}
}
use of org.apache.tephra.Transaction in project cdap by caskdata.
the class InMemoryTableServiceTest method testInternalsNotLeaking.
@Test
public void testInternalsNotLeaking() {
// Test that there's no way to break the state of InMemoryTableService by changing parameters of update
// methods (after method invocation) or by changing returned values "in-place"
InMemoryTableService.create("table");
// verify writing thru merge is guarded
NavigableMap<byte[], NavigableMap<byte[], Update>> updates = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
NavigableMap<byte[], Update> rowUpdate = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
byte[] rowParam = new byte[] { 1 };
byte[] columnParam = new byte[] { 2 };
byte[] valParam = new byte[] { 3 };
rowUpdate.put(columnParam, new PutValue(valParam));
updates.put(rowParam, rowUpdate);
InMemoryTableService.merge("table", updates, 1L);
verify123();
updates.remove(rowParam);
rowUpdate.remove(columnParam);
rowParam[0]++;
columnParam[0]++;
valParam[0]++;
verify123();
// verify changing returned data from get doesn't affect the stored data
NavigableMap<byte[], NavigableMap<Long, byte[]>> rowFromGet = InMemoryTableService.get("table", new byte[] { 1 }, new Transaction(1L, 2L, new long[0], new long[0], 1L));
Assert.assertEquals(1, rowFromGet.size());
byte[] columnFromGet = rowFromGet.firstEntry().getKey();
Assert.assertArrayEquals(new byte[] { 2 }, columnFromGet);
byte[] valFromGet = rowFromGet.firstEntry().getValue().get(1L);
Assert.assertArrayEquals(new byte[] { 3 }, valFromGet);
rowFromGet.firstEntry().getValue().remove(1L);
rowFromGet.remove(columnFromGet);
columnFromGet[0]++;
valFromGet[0]++;
verify123();
// verify changing returned data doesn't affect the stored data
NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> fromGetRange = InMemoryTableService.getRowRange("table", null, null, new Transaction(1L, 2L, new long[0], new long[0], 1L));
Assert.assertEquals(1, fromGetRange.size());
byte[] keyFromGetRange = fromGetRange.firstEntry().getKey();
Assert.assertArrayEquals(new byte[] { 1 }, keyFromGetRange);
NavigableMap<byte[], NavigableMap<Long, byte[]>> rowFromGetRange = fromGetRange.get(new byte[] { 1 });
Assert.assertEquals(1, rowFromGetRange.size());
byte[] columnFromGetRange = rowFromGetRange.firstEntry().getKey();
Assert.assertArrayEquals(new byte[] { 2 }, columnFromGetRange);
byte[] valFromGetRange = rowFromGetRange.firstEntry().getValue().get(1L);
Assert.assertArrayEquals(new byte[] { 3 }, valFromGetRange);
rowFromGetRange.firstEntry().getValue().remove(1L);
rowFromGetRange.remove(columnFromGetRange);
fromGetRange.remove(keyFromGetRange);
keyFromGetRange[0]++;
columnFromGetRange[0]++;
valFromGet[0]++;
verify123();
}
use of org.apache.tephra.Transaction in project cdap by caskdata.
the class InMemoryTableServiceTest method verify123.
private void verify123() {
NavigableMap<byte[], NavigableMap<Long, byte[]>> rowFromGet = InMemoryTableService.get("table", new byte[] { 1 }, new Transaction(1L, 2L, new long[0], new long[0], 1L));
Assert.assertEquals(1, rowFromGet.size());
Assert.assertArrayEquals(new byte[] { 2 }, rowFromGet.firstEntry().getKey());
Assert.assertArrayEquals(new byte[] { 3 }, rowFromGet.firstEntry().getValue().get(1L));
}
Aggregations