use of io.cdap.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.
the class HBaseTableTest method testColumnFamily.
@Test
public void testColumnFamily() throws Exception {
DatasetProperties props = TableProperties.builder().setColumnFamily("t").build();
String tableName = "testcf";
DatasetAdmin admin = getTableAdmin(CONTEXT1, tableName, props);
admin.create();
try (BufferingTable table = getTable(CONTEXT1, tableName, props);
BufferingTable table2 = getTable(CONTEXT1, tableName, props)) {
TransactionSystemClient txClient = new DetachedTxSystemClient();
TransactionExecutor executor = new DefaultTransactionExecutor(txClient, table);
executor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
table.put(new Put("row", "column", "testValue"));
}
});
executor = new DefaultTransactionExecutor(txClient, table2);
executor.execute(new TransactionExecutor.Subroutine() {
@Override
public void apply() throws Exception {
Assert.assertEquals("testValue", table2.get(new Get("row", "column")).getString("column"));
}
});
// Verify the column family name
TableId hTableId = hBaseTableUtil.createHTableId(new NamespaceId(CONTEXT1.getNamespaceId()), tableName);
HTableDescriptor htd = hBaseTableUtil.getHTableDescriptor(TEST_HBASE.getHBaseAdmin(), hTableId);
HColumnDescriptor hcd = htd.getFamily(Bytes.toBytes("t"));
Assert.assertNotNull(hcd);
Assert.assertEquals("t", hcd.getNameAsString());
}
}
use of io.cdap.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.
the class TableTest method testCreate.
@Test
public void testCreate() throws Exception {
DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE);
Assert.assertFalse(admin.exists());
admin.create();
Assert.assertTrue(admin.exists());
// creation of non-existing table should do nothing
admin.create();
admin.drop();
}
use of io.cdap.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.
the class TableTest method testBasicIncrementWithTx.
private void testBasicIncrementWithTx(boolean doIncrAndGet, boolean readless) throws Exception {
DatasetProperties props = TableProperties.builder().setReadlessIncrementSupport(readless).build();
DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE, props);
admin.create();
try (Table myTable1 = getTable(CONTEXT1, MY_TABLE, props);
Table myTable2 = getTable(CONTEXT1, MY_TABLE, props);
Table myTable3 = getTable(CONTEXT1, MY_TABLE, props);
Table myTable4 = getTable(CONTEXT1, MY_TABLE, props)) {
Transaction tx1 = txClient.startShort();
((TransactionAware) myTable1).startTx(tx1);
myTable1.put(R1, a(C1), a(L4));
doIncrement(doIncrAndGet, myTable1, R1, a(C1), la(-3L), lb(1L));
doIncrement(doIncrAndGet, myTable1, R1, a(C2), la(2L), lb(2L));
// TableAssert.verify increment result visible inside tx before commit
TableAssert.assertRow(a(C1, L1, C2, L2), myTable1.get(R1, a(C1, C2)));
Assert.assertArrayEquals(L1, myTable1.get(R1, C1));
Assert.assertArrayEquals(L2, myTable1.get(R1, C2));
Assert.assertArrayEquals(null, myTable1.get(R1, C3));
TableAssert.assertRow(a(C1, L1), myTable1.get(R1, a(C1)));
TableAssert.assertRow(a(C1, L1, C2, L2), myTable1.get(R1));
// incrementing non-long value should fail
myTable1.put(R1, a(C5), a(V5));
try {
doIncrement(doIncrAndGet, myTable1, R1, a(C5), la(5L), lb(-1L));
Assert.fail("increment should have failed with NumberFormatException");
} catch (NumberFormatException e) {
// Expected
}
// previous increment should not do any change
TableAssert.assertRow(a(C5, V5), myTable1.get(R1, a(C5)));
Assert.assertArrayEquals(V5, myTable1.get(R1, C5));
// 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
txClient.canCommitOrThrow(tx1, ((TransactionAware) myTable1).getTxChanges());
Assert.assertTrue(((TransactionAware) myTable1).commitTx());
// check that tx2 doesn't see changes (even though they were flushed) of tx1
// assuming current value is null
((TransactionAware) myTable2).startTx(tx2);
TableAssert.assertRow(a(), myTable2.get(R1, a(C1, C2, C5)));
Assert.assertArrayEquals(null, myTable2.get(R1, C1));
Assert.assertArrayEquals(null, myTable2.get(R1, C2));
Assert.assertArrayEquals(null, myTable2.get(R1, C5));
TableAssert.assertRow(a(), myTable2.get(R1));
doIncrement(doIncrAndGet, myTable2, R1, a(C1), la(55L), lb(55L));
// start tx3 and TableAssert.verify same thing again
Transaction tx3 = txClient.startShort();
((TransactionAware) myTable3).startTx(tx3);
TableAssert.assertRow(a(), myTable3.get(R1, a(C1, C2, C5)));
Assert.assertArrayEquals(null, myTable3.get(R1, C1));
Assert.assertArrayEquals(null, myTable3.get(R1, C2));
Assert.assertArrayEquals(null, myTable3.get(R1, C5));
TableAssert.assertRow(a(), myTable3.get(R1));
doIncrement(doIncrAndGet, myTable3, R1, a(C1), la(4L), lb(4L));
// * second, make tx visible
txClient.commitOrThrow(tx1);
// TableAssert.verify that tx2 cannot commit because of the conflicts...
try {
txClient.canCommitOrThrow(tx2, ((TransactionAware) myTable2).getTxChanges());
Assert.fail("Conflict not detected!");
} catch (TransactionConflictException e) {
// expected
}
((TransactionAware) myTable2).rollbackTx();
txClient.abort(tx2);
// start tx4 and TableAssert.verify that changes of tx1 are now visible
Transaction tx4 = txClient.startShort();
((TransactionAware) myTable4).startTx(tx4);
TableAssert.assertRow(a(C1, L1, C2, L2, C5, V5), myTable4.get(R1, a(C1, C2, C3, C4, C5)));
TableAssert.assertRow(a(C2, L2), myTable4.get(R1, a(C2)));
Assert.assertArrayEquals(L1, myTable4.get(R1, C1));
Assert.assertArrayEquals(L2, myTable4.get(R1, C2));
Assert.assertArrayEquals(null, myTable4.get(R1, C3));
Assert.assertArrayEquals(V5, myTable4.get(R1, C5));
TableAssert.assertRow(a(C1, L1, C5, V5), myTable4.get(R1, a(C1, C5)));
TableAssert.assertRow(a(C1, L1, C2, L2, C5, V5), myTable4.get(R1));
// tx3 still cannot see tx1 changes, only its own
TableAssert.assertRow(a(C1, L4), myTable3.get(R1, a(C1, C2, C5)));
Assert.assertArrayEquals(L4, myTable3.get(R1, C1));
Assert.assertArrayEquals(null, myTable3.get(R1, C2));
Assert.assertArrayEquals(null, myTable3.get(R1, C5));
TableAssert.assertRow(a(C1, L4), myTable3.get(R1));
// and it cannot commit because its changes cause conflicts
try {
txClient.canCommitOrThrow(tx3, ((TransactionAware) myTable3).getTxChanges());
Assert.fail("Conflict not detected!");
} catch (TransactionConflictException e) {
// expected
}
((TransactionAware) myTable3).rollbackTx();
txClient.abort(tx3);
// TableAssert.verify we can do some ops with tx4 based on data written with tx1
doIncrement(doIncrAndGet, myTable4, R1, a(C1, C2, C3), la(2L, 1L, 5L), lb(3L, 3L, 5L));
myTable4.delete(R1, a(C2));
doIncrement(doIncrAndGet, myTable4, R1, a(C4), la(3L), lb(3L));
myTable4.delete(R1, a(C1));
// committing tx4
txClient.canCommitOrThrow(tx4, ((TransactionAware) myTable3).getTxChanges());
Assert.assertTrue(((TransactionAware) myTable4).commitTx());
txClient.commitOrThrow(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(C3, L5, C4, L3, C5, V5), myTable4.get(R1, a(C1, C2, C3, C4, C5)));
Assert.assertArrayEquals(null, myTable4.get(R1, C1));
Assert.assertArrayEquals(null, myTable4.get(R1, C2));
Assert.assertArrayEquals(L5, myTable4.get(R1, C3));
Assert.assertArrayEquals(L3, myTable4.get(R1, C4));
Assert.assertArrayEquals(V5, myTable4.get(R1, C5));
TableAssert.assertRow(a(C3, L5, C4, L3, C5, V5), myTable4.get(R1));
txClient.canCommitOrThrow(tx5, ((TransactionAware) myTable3).getTxChanges());
Assert.assertTrue(((TransactionAware) myTable3).commitTx());
txClient.commitOrThrow(tx5);
} finally {
admin.drop();
}
}
use of io.cdap.cdap.api.dataset.DatasetAdmin in project cdap by caskdata.
the class TableTest method testMultiIncrementWithFlush.
private void testMultiIncrementWithFlush(boolean readless) throws Exception {
final String tableName = "incrFlush";
DatasetProperties props = TableProperties.builder().setReadlessIncrementSupport(readless).build();
DatasetAdmin admin = getTableAdmin(CONTEXT1, tableName, props);
admin.create();
Map<String, String> args = new HashMap<>();
if (readless) {
args.put(HBaseTable.SAFE_INCREMENTS, "true");
}
try (Table table = getTable(CONTEXT1, tableName, props, args);
Table table2 = getTable(CONTEXT1, tableName, props, args)) {
Transaction tx = txClient.startShort();
try {
((TransactionAware) table).startTx(tx);
// Write an increment, then flush it by calling commitTx.
table.increment(new Increment(R1, C1, 10L));
((TransactionAware) table).commitTx();
} finally {
// invalidate the tx, leaving an excluded write in the table
txClient.invalidate(tx.getTransactionId());
}
// validate the first write is not visible
tx = txClient.startShort();
try {
((TransactionAware) table).startTx(tx);
Assert.assertEquals(null, table.get(new Get(R1, C1)).getLong(C1));
} finally {
txClient.commitOrThrow(tx);
}
tx = txClient.startShort();
try {
((TransactionAware) table).startTx(tx);
// Write an increment, then flush it by calling commitTx.
table.increment(new Increment(R1, C1, 1L));
((TransactionAware) table).commitTx();
// Write another increment, from both table instances
table.increment(new Increment(R1, C1, 1L));
if (readless) {
((TransactionAware) table2).startTx(tx);
table2.increment(new Increment(R1, C1, 1L));
((TransactionAware) table2).commitTx();
}
((TransactionAware) table).commitTx();
} finally {
txClient.commitOrThrow(tx);
}
// validate all increments are visible to a new tx
tx = txClient.startShort();
try {
((TransactionAware) table).startTx(tx);
Assert.assertEquals(new Long(readless ? 3L : 2L), table.get(new Get(R1, C1)).getLong(C1));
} finally {
txClient.commitOrThrow(tx);
}
} finally {
// drop table
admin.drop();
}
}
use of io.cdap.cdap.api.dataset.DatasetAdmin 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();
try (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);
} finally {
admin.drop();
}
}
Aggregations