Search in sources :

Example 41 with DatasetAdmin

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

the class TableTest method testReadOwnWrite.

@Test
public void testReadOwnWrite() throws Exception {
    final String tableName = "readOwnWrite";
    DatasetAdmin admin = getTableAdmin(CONTEXT1, tableName);
    admin.create();
    try (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);
        }
    } 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) Get(io.cdap.cdap.api.dataset.table.Get) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) Put(io.cdap.cdap.api.dataset.table.Put) Test(org.junit.Test)

Example 42 with DatasetAdmin

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

the class TableTest method testMultiGetWithTx.

@Test
public void testMultiGetWithTx() throws Exception {
    String testMultiGet = "testMultiGet";
    DatasetAdmin admin = getTableAdmin(CONTEXT1, testMultiGet);
    admin.create();
    try (Table table = getTable(CONTEXT1, testMultiGet)) {
        Transaction tx = txClient.startShort();
        ((TransactionAware) table).startTx(tx);
        for (int i = 0; i < 100; i++) {
            table.put(new Put(Bytes.toBytes("r" + i)).add(C1, V1).add(C2, V2));
        }
        txClient.canCommitOrThrow(tx, ((TransactionAware) table).getTxChanges());
        Assert.assertTrue(((TransactionAware) table).commitTx());
        txClient.commitOrThrow(tx);
        Transaction tx2 = txClient.startShort();
        ((TransactionAware) table).startTx(tx2);
        List<Get> gets = Lists.newArrayListWithCapacity(100);
        for (int i = 0; i < 100; i++) {
            gets.add(new Get(Bytes.toBytes("r" + i)));
        }
        List<Row> results = table.get(gets);
        txClient.commitOrThrow(tx2);
        for (int i = 0; i < 100; i++) {
            Row row = results.get(i);
            Assert.assertArrayEquals(Bytes.toBytes("r" + i), row.getRow());
            byte[] val = row.get(C1);
            Assert.assertNotNull(val);
            Assert.assertArrayEquals(V1, val);
            byte[] val2 = row.get(C2);
            Assert.assertNotNull(val2);
            Assert.assertArrayEquals(V2, val2);
        }
        Transaction tx3 = txClient.startShort();
        ((TransactionAware) table).startTx(tx3);
        gets = Lists.newArrayListWithCapacity(100);
        for (int i = 0; i < 100; i++) {
            gets.add(new Get("r" + i).add(C1));
        }
        results = table.get(gets);
        txClient.commitOrThrow(tx3);
        for (int i = 0; i < 100; i++) {
            Row row = results.get(i);
            Assert.assertArrayEquals(Bytes.toBytes("r" + i), row.getRow());
            byte[] val = row.get(C1);
            Assert.assertNotNull(val);
            Assert.assertArrayEquals(V1, val);
            // should have only returned column 1
            byte[] val2 = row.get(C2);
            Assert.assertNull(val2);
        }
        // retrieve different columns per row
        Transaction tx4 = txClient.startShort();
        ((TransactionAware) table).startTx(tx4);
        gets = Lists.newArrayListWithCapacity(100);
        for (int i = 0; i < 100; i++) {
            Get get = new Get("r" + i);
            // evens get C1, odds get C2
            get.add(i % 2 == 0 ? C1 : C2);
            gets.add(get);
        }
        results = table.get(gets);
        txClient.commitOrThrow(tx4);
        for (int i = 0; i < 100; i++) {
            Row row = results.get(i);
            Assert.assertArrayEquals(Bytes.toBytes("r" + i), row.getRow());
            byte[] val1 = row.get(C1);
            byte[] val2 = row.get(C2);
            if (i % 2 == 0) {
                Assert.assertNotNull(val1);
                Assert.assertArrayEquals(V1, val1);
                Assert.assertNull(val2);
            } else {
                Assert.assertNull(val1);
                Assert.assertNotNull(val2);
                Assert.assertArrayEquals(V2, val2);
            }
        }
    } 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) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) Row(io.cdap.cdap.api.dataset.table.Row) Put(io.cdap.cdap.api.dataset.table.Put) Test(org.junit.Test)

Example 43 with DatasetAdmin

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

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 (Table myTable1 = getTable(CONTEXT1, MY_TABLE);
        Table myTable2 = getTable(CONTEXT1, MY_TABLE)) {
        // write r1...r5 and commit
        Transaction tx1 = txClient.startShort();
        ((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();
        ((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();
    }
}
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 44 with DatasetAdmin

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

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

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

the class ObjectMappedTableDefinition method getDataset.

@Override
public ObjectMappedTableDataset<?> getDataset(DatasetContext datasetContext, DatasetSpecification spec, Map<String, String> arguments, ClassLoader classLoader) throws IOException {
    String keyName = ObjectMappedTableProperties.getRowKeyExploreName(spec.getProperties());
    DatasetSpecification tableSpec = spec.getSpecification(TABLE_NAME);
    // TODO: remove after CDAP-2122 is done
    if (!tableSpec.getProperties().containsKey(Table.PROPERTY_SCHEMA)) {
        tableSpec = DatasetSpecification.builder(tableSpec.getName(), tableSpec.getType()).properties(tableSpec.getProperties()).property(Table.PROPERTY_SCHEMA, spec.getProperty(Table.PROPERTY_SCHEMA)).property(Table.PROPERTY_SCHEMA_ROW_FIELD, keyName).datasets(tableSpec.getSpecifications().values()).build();
    }
    // reconstruct the table schema here because of backwards compatibility
    DatasetDefinition<Table, DatasetAdmin> tableDef = getDelegate(TABLE_NAME);
    Table table = tableDef.getDataset(datasetContext, tableSpec, arguments, classLoader);
    Map<String, String> properties = spec.getProperties();
    TypeRepresentation typeRep = GSON.fromJson(ObjectMappedTableProperties.getObjectTypeRepresentation(properties), TypeRepresentation.class);
    Schema objSchema = ObjectMappedTableProperties.getObjectSchema(properties);
    return new ObjectMappedTableDataset(spec.getName(), table, typeRep, objSchema, classLoader);
}
Also used : ObjectMappedTable(io.cdap.cdap.api.dataset.lib.ObjectMappedTable) Table(io.cdap.cdap.api.dataset.table.Table) TypeRepresentation(io.cdap.cdap.internal.io.TypeRepresentation) Schema(io.cdap.cdap.api.data.schema.Schema) DatasetSpecification(io.cdap.cdap.api.dataset.DatasetSpecification) 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