Search in sources :

Example 91 with DatasetAdmin

use of io.cdap.cdap.api.dataset.DatasetAdmin in project cdap by cdapio.

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();
    }
}
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) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) Put(io.cdap.cdap.api.dataset.table.Put) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 92 with DatasetAdmin

use of io.cdap.cdap.api.dataset.DatasetAdmin in project cdap by cdapio.

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 93 with DatasetAdmin

use of io.cdap.cdap.api.dataset.DatasetAdmin in project cdap by cdapio.

the class TableTest method testStringPutGet.

/**
 * Test that tables convert String to byte[] and back inversely. That is, get(put(s)) == s.
 */
@Test
public void testStringPutGet() throws Exception {
    DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE);
    admin.create();
    try (Table t = getTable(CONTEXT1, MY_TABLE)) {
        // ascii
        testStringPutGet(t, "a", "b", "c");
        // row is latin
        testStringPutGet(t, "ä", "b", "c");
        // column is latin
        testStringPutGet(t, "a", "ä", "c");
        // value is latin
        testStringPutGet(t, "a", "b", "ä");
        // row is non-printable
        testStringPutGet(t, "\u009F", "b", "c");
        // column is non-printable
        testStringPutGet(t, "a", "\u009F", "c");
        // value is non-printable
        testStringPutGet(t, "a", "b", "\u009F");
    } finally {
        admin.drop();
    }
}
Also used : Table(io.cdap.cdap.api.dataset.table.Table) HBaseTable(io.cdap.cdap.data2.dataset2.lib.table.hbase.HBaseTable) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) Test(org.junit.Test)

Example 94 with DatasetAdmin

use of io.cdap.cdap.api.dataset.DatasetAdmin in project cdap by cdapio.

the class TableTest method testClientSurvivesTableReset.

// this test ensures that an existing client survives the truncating or dropping and recreating of a table
@Test
public void testClientSurvivesTableReset() throws Exception {
    final String tableName = "survive";
    DatasetAdmin admin = getTableAdmin(CONTEXT1, tableName);
    admin.create();
    try (Table table = getTable(CONTEXT1, tableName);
        Table table2 = getTable(CONTEXT1, tableName)) {
        // write some values
        Transaction tx0 = txClient.startShort();
        ((TransactionAware) table).startTx(tx0);
        table.put(R1, a(C1), a(V1));
        txClient.canCommitOrThrow(tx0, ((TransactionAware) table).getTxChanges());
        Assert.assertTrue(((TransactionAware) table).commitTx());
        txClient.commitOrThrow(tx0);
        ((TransactionAware) table).postTxCommit();
        // TableAssert.verify
        Transaction tx1 = txClient.startShort();
        ((TransactionAware) table).startTx(tx1);
        TableAssert.assertRow(a(C1, V1), table.get(R1));
        // drop table and recreate
        admin.drop();
        admin.create();
        // TableAssert.verify can read but nothing there
        TableAssert.assertRow(a(), table.get(R1));
        // only did read, safe to abort
        txClient.abort(tx1);
        // create a new client and write another value
        Transaction tx2 = txClient.startShort();
        ((TransactionAware) table2).startTx(tx2);
        table2.put(R1, a(C2), a(V2));
        txClient.canCommitOrThrow(tx2, ((TransactionAware) table2).getTxChanges());
        Assert.assertTrue(((TransactionAware) table2).commitTx());
        txClient.commitOrThrow(tx2);
        ((TransactionAware) table2).postTxCommit();
        // TableAssert.verify it is visible
        Transaction tx3 = txClient.startShort();
        ((TransactionAware) table).startTx(tx3);
        TableAssert.assertRow(a(C2, V2), table.get(R1));
        // truncate table
        admin.truncate();
        // TableAssert.verify can read but nothing there
        TableAssert.assertRow(a(), table.get(R1));
        // only did read, safe to abort
        txClient.abort(tx3);
        // write again with other client
        Transaction tx4 = txClient.startShort();
        ((TransactionAware) table2).startTx(tx4);
        table2.put(R1, a(C3), a(V3));
        txClient.canCommitOrThrow(tx4, ((TransactionAware) table2).getTxChanges());
        Assert.assertTrue(((TransactionAware) table2).commitTx());
        txClient.commitOrThrow(tx4);
        ((TransactionAware) table2).postTxCommit();
        // TableAssert.verify it is visible
        Transaction tx5 = txClient.startShort();
        ((TransactionAware) table).startTx(tx5);
        TableAssert.assertRow(a(C3, V3), table.get(R1));
        // only did read, safe to abort
        txClient.abort(tx5);
    } 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) TransactionAware(org.apache.tephra.TransactionAware) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) Test(org.junit.Test)

Example 95 with DatasetAdmin

use of io.cdap.cdap.api.dataset.DatasetAdmin in project cdap by cdapio.

the class BufferingTableTest method testTxChangePrefix.

// This test is in Buffering table because it needs to test the transaction change prefix
@Test
public void testTxChangePrefix() throws Exception {
    String tableName = "same";
    DatasetAdmin admin1 = getTableAdmin(CONTEXT1, tableName);
    DatasetAdmin admin2 = getTableAdmin(CONTEXT2, tableName);
    admin1.create();
    admin2.create();
    try (BufferingTable table1 = getTable(CONTEXT1, tableName);
        BufferingTable table2 = getTable(CONTEXT2, tableName)) {
        // write some values in table1
        Transaction tx1 = txClient.startShort();
        table1.startTx(tx1);
        table1.put(R1, a(C1), a(V1));
        Collection<byte[]> tx1Changes = table1.getTxChanges();
        txClient.canCommitOrThrow(tx1, tx1Changes);
        Assert.assertTrue(table1.commitTx());
        txClient.commitOrThrow(tx1);
        table1.postTxCommit();
        // write some values in table2
        Transaction tx2 = txClient.startShort();
        table2.startTx(tx2);
        table2.put(R1, a(C1), a(V1));
        Collection<byte[]> tx2Changes = table2.getTxChanges();
        txClient.canCommitOrThrow(tx2, tx2Changes);
        Assert.assertTrue(table2.commitTx());
        txClient.commitOrThrow(tx2);
        table1.postTxCommit();
        String tx1ChangePrefix = new String(table1.getNameAsTxChangePrefix());
        String tx2ChangePrefix = new String(table2.getNameAsTxChangePrefix());
        String tx1Change = new String(((ArrayList<byte[]>) tx1Changes).get(0));
        String tx2Change = new String(((ArrayList<byte[]>) tx2Changes).get(0));
        Assert.assertNotEquals(tx1ChangePrefix, tx2ChangePrefix);
        Assert.assertTrue(tx1ChangePrefix.contains(NAMESPACE1.getEntityName()));
        Assert.assertTrue(tx2ChangePrefix.contains(NAMESPACE2.getEntityName()));
        Assert.assertTrue(tx1Change.startsWith(tx1ChangePrefix));
        Assert.assertTrue(tx2Change.startsWith(tx2ChangePrefix));
    } finally {
        admin1.drop();
        admin2.drop();
    }
}
Also used : Transaction(org.apache.tephra.Transaction) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) Test(org.junit.Test)

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