Search in sources :

Example 51 with DatasetAdmin

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());
    }
}
Also used : TableId(io.cdap.cdap.data2.util.TableId) HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) DatasetProperties(io.cdap.cdap.api.dataset.DatasetProperties) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) TransactionExecutor(org.apache.tephra.TransactionExecutor) DefaultTransactionExecutor(org.apache.tephra.DefaultTransactionExecutor) ScannerTimeoutException(org.apache.hadoop.hbase.client.ScannerTimeoutException) IOException(java.io.IOException) Put(io.cdap.cdap.api.dataset.table.Put) BufferingTable(io.cdap.cdap.data2.dataset2.lib.table.BufferingTable) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) TransactionSystemClient(org.apache.tephra.TransactionSystemClient) Get(io.cdap.cdap.api.dataset.table.Get) DefaultTransactionExecutor(org.apache.tephra.DefaultTransactionExecutor) DetachedTxSystemClient(org.apache.tephra.inmemory.DetachedTxSystemClient) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) BufferingTableTest(io.cdap.cdap.data2.dataset2.lib.table.BufferingTableTest) Test(org.junit.Test)

Example 52 with DatasetAdmin

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();
}
Also used : DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) Test(org.junit.Test)

Example 53 with DatasetAdmin

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();
    }
}
Also used : Table(io.cdap.cdap.api.dataset.table.Table) HBaseTable(io.cdap.cdap.data2.dataset2.lib.table.hbase.HBaseTable) Transaction(org.apache.tephra.Transaction) TransactionAware(org.apache.tephra.TransactionAware) DatasetProperties(io.cdap.cdap.api.dataset.DatasetProperties) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) TransactionConflictException(org.apache.tephra.TransactionConflictException)

Example 54 with DatasetAdmin

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();
    }
}
Also used : Table(io.cdap.cdap.api.dataset.table.Table) HBaseTable(io.cdap.cdap.data2.dataset2.lib.table.hbase.HBaseTable) Transaction(org.apache.tephra.Transaction) HashMap(java.util.HashMap) TransactionAware(org.apache.tephra.TransactionAware) DatasetProperties(io.cdap.cdap.api.dataset.DatasetProperties) Increment(io.cdap.cdap.api.dataset.table.Increment) Get(io.cdap.cdap.api.dataset.table.Get) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin)

Example 55 with DatasetAdmin

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();
    }
}
Also used : Table(io.cdap.cdap.api.dataset.table.Table) HBaseTable(io.cdap.cdap.data2.dataset2.lib.table.hbase.HBaseTable) Transaction(org.apache.tephra.Transaction) TransactionAware(org.apache.tephra.TransactionAware) DatasetProperties(io.cdap.cdap.api.dataset.DatasetProperties) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin)

Aggregations

DatasetAdmin (io.cdap.cdap.api.dataset.DatasetAdmin)112 Test (org.junit.Test)60 Table (io.cdap.cdap.api.dataset.table.Table)54 Transaction (org.apache.tephra.Transaction)54 TransactionAware (org.apache.tephra.TransactionAware)46 HBaseTable (io.cdap.cdap.data2.dataset2.lib.table.hbase.HBaseTable)42 DatasetSpecification (io.cdap.cdap.api.dataset.DatasetSpecification)20 DatasetProperties (io.cdap.cdap.api.dataset.DatasetProperties)16 Get (io.cdap.cdap.api.dataset.table.Get)16 Put (io.cdap.cdap.api.dataset.table.Put)14 IOException (java.io.IOException)14 Row (io.cdap.cdap.api.dataset.table.Row)12 DatasetType (io.cdap.cdap.data2.datafabric.dataset.DatasetType)10 TransactionConflictException (org.apache.tephra.TransactionConflictException)10 Scan (io.cdap.cdap.api.dataset.table.Scan)8 BufferingTableTest (io.cdap.cdap.data2.dataset2.lib.table.BufferingTableTest)8 AbstractModule (com.google.inject.AbstractModule)6 TypeLiteral (com.google.inject.TypeLiteral)6 DatasetContext (io.cdap.cdap.api.dataset.DatasetContext)6 DatasetManagementException (io.cdap.cdap.api.dataset.DatasetManagementException)6