Search in sources :

Example 61 with DatasetAdmin

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

the class TableTest method testScanAndDelete.

@Test
public void testScanAndDelete() throws Exception {
    DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE);
    admin.create();
    try (Table myTable1 = getTable(CONTEXT1, MY_TABLE)) {
        Transaction tx1 = txClient.startShort();
        ((TransactionAware) myTable1).startTx(tx1);
        myTable1.put(Bytes.toBytes("1_09a"), a(C1), a(V1));
        txClient.canCommitOrThrow(tx1, ((TransactionAware) myTable1).getTxChanges());
        Assert.assertTrue(((TransactionAware) myTable1).commitTx());
        txClient.commitOrThrow(tx1);
        // 
        Transaction tx2 = txClient.startShort();
        ((TransactionAware) myTable1).startTx(tx2);
        myTable1.delete(Bytes.toBytes("1_09a"));
        myTable1.put(Bytes.toBytes("1_08a"), a(C1), a(V1));
        myTable1.put(Bytes.toBytes("1_09b"), a(C1), a(V1));
        txClient.canCommitOrThrow(tx2, ((TransactionAware) myTable1).getTxChanges());
        Assert.assertTrue(((TransactionAware) myTable1).commitTx());
        txClient.commitOrThrow(tx2);
        // 
        Transaction tx3 = txClient.startShort();
        ((TransactionAware) myTable1).startTx(tx3);
        TableAssert.assertScan(a(Bytes.toBytes("1_08a"), Bytes.toBytes("1_09b")), aa(a(C1, V1), a(C1, V1)), myTable1, new Scan(Bytes.toBytes("1_"), Bytes.toBytes("2_")));
        myTable1.delete(Bytes.toBytes("1_08a"));
        myTable1.put(Bytes.toBytes("1_07a"), a(C1), a(V1));
        myTable1.delete(Bytes.toBytes("1_09b"));
        myTable1.put(Bytes.toBytes("1_08b"), a(C1), a(V1));
        myTable1.put(Bytes.toBytes("1_09c"), a(C1), a(V1));
        txClient.canCommitOrThrow(tx3, ((TransactionAware) myTable1).getTxChanges());
        Assert.assertTrue(((TransactionAware) myTable1).commitTx());
        txClient.commitOrThrow(tx3);
        // Now, we will test scans
        Transaction tx4 = txClient.startShort();
        ((TransactionAware) myTable1).startTx(tx4);
        TableAssert.assertScan(a(Bytes.toBytes("1_07a"), Bytes.toBytes("1_08b"), Bytes.toBytes("1_09c")), aa(a(C1, V1), a(C1, V1), a(C1, V1)), myTable1, new Scan(Bytes.toBytes("1_"), Bytes.toBytes("2_")));
    } 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) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) Scan(io.cdap.cdap.api.dataset.table.Scan) Test(org.junit.Test)

Example 62 with DatasetAdmin

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

the class TableTest method testMultiGetWithEmpty.

@Test
public void testMultiGetWithEmpty() throws Exception {
    DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE);
    admin.create();
    try (Table myTable = getTable(CONTEXT1, MY_TABLE)) {
        Transaction tx = txClient.startShort();
        ((TransactionAware) myTable).startTx(tx);
        myTable.put(R1, C1, V1);
        myTable.put(R1, C2, V2);
        myTable.put(R1, C3, V3);
        myTable.put(R1, C4, V4);
        List<Get> gets = new ArrayList<>();
        // the second and fourth Gets are requesting 0 columns. This tests correctness of batch-get logic, when there
        // is/are empty Gets among them.
        gets.add(new Get(R1, C1));
        gets.add(new Get(R1, ImmutableList.<byte[]>of()));
        gets.add(new Get(R1, C2, C3));
        gets.add(new Get(R1, ImmutableList.<byte[]>of()));
        gets.add(new Get(R1, C4));
        List<Row> rows = myTable.get(gets);
        // first off, the Gets at index two and four should be empty
        Assert.assertEquals(0, rows.get(1).getColumns().size());
        Assert.assertEquals(0, rows.get(3).getColumns().size());
        // verify the results of the other Gets
        Assert.assertEquals(1, rows.get(0).getColumns().size());
        Assert.assertArrayEquals(V1, rows.get(0).get(C1));
        Assert.assertEquals(2, rows.get(2).getColumns().size());
        Assert.assertArrayEquals(V2, rows.get(2).get(C2));
        Assert.assertArrayEquals(V3, rows.get(2).get(C3));
        Assert.assertEquals(1, rows.get(4).getColumns().size());
        Assert.assertArrayEquals(V4, rows.get(4).get(C4));
        txClient.abort(tx);
    } 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) Get(io.cdap.cdap.api.dataset.table.Get) ArrayList(java.util.ArrayList) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) Row(io.cdap.cdap.api.dataset.table.Row) Test(org.junit.Test)

Example 63 with DatasetAdmin

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

the class TableTest method testBasicGetPutWithTx.

@Test
public void testBasicGetPutWithTx() throws Exception {
    DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE);
    admin.create();
    try (Table myTable1 = getTable(CONTEXT1, MY_TABLE);
        Table myTable2 = getTable(CONTEXT1, MY_TABLE);
        Table myTable3 = getTable(CONTEXT1, MY_TABLE)) {
        Transaction tx1 = txClient.startShort();
        ((TransactionAware) myTable1).startTx(tx1);
        // write r1->c1,v1 but not commit
        myTable1.put(R1, a(C1), a(V1));
        // TableAssert.verify can see changes inside tx
        TableAssert.assertRow(a(C1, V1), myTable1.get(R1, a(C1, C2)).getColumns());
        Assert.assertArrayEquals(V1, myTable1.get(R1, C1));
        Assert.assertArrayEquals(null, myTable1.get(R1, C2));
        Assert.assertArrayEquals(null, myTable1.get(R2, C1));
        TableAssert.assertRow(a(C1, V1), myTable1.get(R1).getColumns());
        // start new tx (doesn't see changes of the tx1)
        Transaction tx2 = txClient.startShort();
        ((TransactionAware) myTable2).startTx(tx2);
        // TableAssert.verify doesn't see changes of tx1
        TableAssert.assertRow(a(), myTable2.get(R1, a(C1, C2)));
        Assert.assertArrayEquals(null, myTable2.get(R1, C1));
        Assert.assertArrayEquals(null, myTable2.get(R1, C2));
        TableAssert.assertRow(a(), myTable2.get(R1));
        // write r2->c2,v2 in tx2
        myTable2.put(R2, a(C2), a(V2));
        // TableAssert.verify can see own changes
        TableAssert.assertRow(a(C2, V2), myTable2.get(R2, a(C1, C2)));
        Assert.assertArrayEquals(null, myTable2.get(R2, C1));
        Assert.assertArrayEquals(V2, myTable2.get(R2, C2));
        TableAssert.assertRow(a(C2, V2), myTable2.get(R2));
        // TableAssert.verify tx1 cannot see changes of tx2
        TableAssert.assertRow(a(), myTable1.get(R2, a(C1, C2)));
        Assert.assertArrayEquals(null, myTable1.get(R2, C1));
        Assert.assertArrayEquals(null, myTable1.get(R2, C2));
        TableAssert.assertRow(a(), myTable1.get(R2));
        // 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());
        // start tx3 and TableAssert.verify that changes of tx1 are not visible yet (even though they are flushed)
        Transaction tx3 = txClient.startShort();
        ((TransactionAware) myTable3).startTx(tx3);
        TableAssert.assertRow(a(), myTable3.get(R1, a(C1, C2)));
        Assert.assertArrayEquals(null, myTable3.get(R1, C1));
        Assert.assertArrayEquals(null, myTable3.get(R1, C2));
        TableAssert.assertRow(a(), myTable3.get(R1));
        txClient.canCommitOrThrow(tx3, ((TransactionAware) myTable3).getTxChanges());
        Assert.assertTrue(((TransactionAware) myTable3).commitTx());
        txClient.commitOrThrow(tx3);
        // * second, make tx visible
        txClient.commitOrThrow(tx1);
        // start tx4 and TableAssert.verify that changes of tx1 are now visible
        // NOTE: table instance can be re-used in series of transactions
        Transaction tx4 = txClient.startShort();
        ((TransactionAware) myTable3).startTx(tx4);
        TableAssert.assertRow(a(C1, V1), myTable3.get(R1, a(C1, C2)));
        Assert.assertArrayEquals(V1, myTable3.get(R1, C1));
        Assert.assertArrayEquals(null, myTable3.get(R1, C2));
        TableAssert.assertRow(a(C1, V1), myTable3.get(R1));
        // but tx2 still doesn't see committed changes of tx2
        TableAssert.assertRow(a(), myTable2.get(R1, a(C1, C2)));
        Assert.assertArrayEquals(null, myTable2.get(R1, C1));
        Assert.assertArrayEquals(null, myTable2.get(R1, C2));
        TableAssert.assertRow(a(), myTable2.get(R1));
        // and tx4 doesn't see changes of tx2
        TableAssert.assertRow(a(), myTable3.get(R2, a(C1, C2)));
        Assert.assertArrayEquals(null, myTable3.get(R2, C1));
        Assert.assertArrayEquals(null, myTable3.get(R2, C2));
        TableAssert.assertRow(a(), myTable3.get(R2));
        // committing tx4
        txClient.canCommitOrThrow(tx4, ((TransactionAware) myTable3).getTxChanges());
        Assert.assertTrue(((TransactionAware) myTable3).commitTx());
        txClient.commitOrThrow(tx4);
        // do change in tx2 that is conflicting with tx1
        myTable2.put(R1, a(C1), a(V2));
        // change is OK and visible inside tx2
        TableAssert.assertRow(a(C1, V2), myTable2.get(R1, a(C1, C2)));
        Assert.assertArrayEquals(V2, myTable2.get(R1, C1));
        Assert.assertArrayEquals(null, myTable2.get(R1, C2));
        TableAssert.assertRow(a(C1, V2), myTable2.get(R1));
        // cannot commit: conflict should be detected
        try {
            txClient.canCommitOrThrow(tx2, ((TransactionAware) myTable2).getTxChanges());
            Assert.fail("Conflict not detected!");
        } catch (TransactionConflictException e) {
        // expected
        }
        // rolling back tx2 changes and aborting tx
        ((TransactionAware) myTable2).rollbackTx();
        txClient.abort(tx2);
        // TableAssert.verifying that none of the changes of tx2 made it to be visible to other txs
        // NOTE: table instance can be re-used in series of transactions
        Transaction tx5 = txClient.startShort();
        ((TransactionAware) myTable3).startTx(tx5);
        TableAssert.assertRow(a(C1, V1), myTable3.get(R1, a(C1, C2)));
        Assert.assertArrayEquals(V1, myTable3.get(R1, C1));
        Assert.assertArrayEquals(null, myTable3.get(R1, C2));
        TableAssert.assertRow(a(C1, V1), myTable3.get(R1));
        TableAssert.assertRow(a(), myTable3.get(R2, a(C1, C2)));
        Assert.assertArrayEquals(null, myTable3.get(R2, C1));
        Assert.assertArrayEquals(null, myTable3.get(R2, C2));
        TableAssert.assertRow(a(), myTable3.get(R2));
        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) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) TransactionConflictException(org.apache.tephra.TransactionConflictException) Test(org.junit.Test)

Example 64 with DatasetAdmin

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

the class BufferingTableTest method testMultiGetIncludesBuffer.

@Test
public void testMultiGetIncludesBuffer() throws Exception {
    DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE);
    admin.create();
    try (BufferingTable table = getTable(CONTEXT1, MY_TABLE)) {
        // persist some data
        Transaction tx1 = txClient.startShort();
        table.startTx(tx1);
        // writing a couple rows
        // table should look like the following, with everything in the buffer
        // c1    c2    c3    c4
        // r1       1     2     3     -
        // r2       -     3     2     1
        table.put(R1, a(C1, C2, C3), lb(1, 2, 3));
        table.put(R2, a(C2, C3, C4), lb(3, 2, 1));
        // check that multi-get can see buffered writes
        List<Row> rows = table.get(Lists.newArrayList(new Get(R1), new Get(R2)));
        Assert.assertEquals(2, rows.size());
        TableAssert.assertRow(rows.get(0), R1, a(C1, C2, C3), lb(1, 2, 3));
        TableAssert.assertRow(rows.get(1), R2, a(C2, C3, C4), lb(3, 2, 1));
        // check multi-get with gets that specify columns, and one get that should return an empty row
        rows = table.get(Lists.newArrayList(new Get(R1, C2, C3), new Get(R2, C2, C3), new Get(R3)));
        Assert.assertEquals(3, rows.size());
        TableAssert.assertRow(rows.get(0), R1, a(C2, C3), lb(2, 3));
        TableAssert.assertRow(rows.get(1), R2, a(C2, C3), lb(3, 2));
        Assert.assertTrue(rows.get(2).isEmpty());
        // persist changes
        Collection<byte[]> txChanges = table.getTxChanges();
        txClient.canCommitOrThrow(tx1, txChanges);
        Assert.assertTrue(table.commitTx());
        txClient.commitOrThrow(tx1);
        table.postTxCommit();
        // start another transaction
        Transaction tx2 = txClient.startShort();
        table.startTx(tx2);
        // now add another row, delete a row, and change some column values
        // table should look like the following
        // c1    c2    c3    c4    c5
        // r1      -     -     3     2     -
        // r3      -     -     -     -     1
        table.put(R1, a(C2, C3, C4), lb(4, 3, 2));
        table.delete(R1, a(C1, C2));
        table.delete(R2);
        table.put(R3, C5, L1);
        // verify multi-get sees persisted data with buffer applied on top
        rows = table.get(Lists.newArrayList(new Get(R1), new Get(R2), new Get(R3)));
        Assert.assertEquals(3, rows.size());
        TableAssert.assertRow(rows.get(0), R1, a(C3, C4), lb(3, 2));
        Assert.assertTrue(rows.get(1).isEmpty());
        TableAssert.assertRow(rows.get(2), R3, a(C5), lb(1));
        // pretend there was a write conflict and rollback changes
        Assert.assertTrue(table.rollbackTx());
        txClient.abort(tx2);
        // start another transaction and make sure it can't see what was done before
        Transaction tx3 = txClient.startShort();
        table.startTx(tx3);
        rows = table.get(Lists.newArrayList(new Get(R1), new Get(R2)));
        Assert.assertEquals(2, rows.size());
        TableAssert.assertRow(rows.get(0), R1, a(C1, C2, C3), lb(1, 2, 3));
        TableAssert.assertRow(rows.get(1), R2, a(C2, C3, C4), lb(3, 2, 1));
    } finally {
        admin.drop();
    }
}
Also used : Transaction(org.apache.tephra.Transaction) Get(io.cdap.cdap.api.dataset.table.Get) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) Row(io.cdap.cdap.api.dataset.table.Row) Test(org.junit.Test)

Example 65 with DatasetAdmin

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

the class BufferingTableTest method testRollingBackAfterExceptionDuringPersist.

@Test
public void testRollingBackAfterExceptionDuringPersist() throws Exception {
    DatasetAdmin admin = getTableAdmin(CONTEXT1, MY_TABLE);
    admin.create();
    try (BufferingTable myTable1 = new BufferingTableWithPersistingFailure(getTable(CONTEXT1, MY_TABLE))) {
        Transaction tx1 = txClient.startShort();
        myTable1.startTx(tx1);
        // write some data but not commit
        myTable1.put(R1, a(C1), a(V1));
        myTable1.put(R2, a(C2), a(V2));
        // verify can see changes inside tx
        TableAssert.assertRow(a(C1, V1), myTable1.get(R1, a(C1)));
        TableAssert.assertRow(a(C2, V2), myTable1.get(R2, a(C2)));
        // persisting changes
        try {
            // should simulate exception
            myTable1.commitTx();
            Assert.assertFalse(true);
        } catch (Throwable th) {
        // Expected simulated exception
        }
        // let's pretend that after persisting changes we still got conflict when finalizing tx, so
        // rolling back changes
        Assert.assertTrue(myTable1.rollbackTx());
        // making tx visible
        txClient.abort(tx1);
        // start new tx
        Transaction tx2 = txClient.startShort();
        try (Table myTable2 = getTable(CONTEXT1, MY_TABLE)) {
            ((TransactionAware) myTable2).startTx(tx2);
            // verify don't see rolled back changes
            TableAssert.assertRow(a(), myTable2.get(R1, a(C1)));
            TableAssert.assertRow(a(), myTable2.get(R2, a(C2)));
        }
    } finally {
        admin.drop();
    }
}
Also used : Table(io.cdap.cdap.api.dataset.table.Table) Transaction(org.apache.tephra.Transaction) TransactionAware(org.apache.tephra.TransactionAware) 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