Search in sources :

Example 21 with DatasetAdmin

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

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

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

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

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

the class BufferingTableTest method testScanWithBuffering.

/**
 * Tests that writes being buffered in memory by the client are still visible during scans.
 */
@Test
public void testScanWithBuffering() throws Exception {
    String testScanWithBuffering = "testScanWithBuffering";
    DatasetAdmin admin = getTableAdmin(CONTEXT1, testScanWithBuffering);
    admin.create();
    try (Table table1 = getTable(CONTEXT1, testScanWithBuffering)) {
        // 
        Transaction tx1 = txClient.startShort();
        ((TransactionAware) table1).startTx(tx1);
        table1.put(Bytes.toBytes("1_01"), a(C1), a(V1));
        table1.put(Bytes.toBytes("1_02"), a(C1), a(V1));
        table1.put(Bytes.toBytes("1_03"), a(C1), a(V1));
        // written values should not yet be persisted
        TableAssert.assertScan(new byte[0][], new byte[0][][], ((BufferingTable) table1).scanPersisted(new Scan(Bytes.toBytes("1_"), Bytes.toBytes("2_"))));
        // buffered values should be visible in a scan
        TableAssert.assertScan(a(Bytes.toBytes("1_01"), Bytes.toBytes("1_02"), Bytes.toBytes("1_03")), aa(a(C1, V1), a(C1, V1), a(C1, V1)), table1.scan(Bytes.toBytes("1_"), Bytes.toBytes("2_")));
        txClient.canCommitOrThrow(tx1, ((TransactionAware) table1).getTxChanges());
        Assert.assertTrue(((TransactionAware) table1).commitTx());
        txClient.commitOrThrow(tx1);
        Transaction tx2 = txClient.startShort();
        ((TransactionAware) table1).startTx(tx2);
        // written values should be visible after commit
        TableAssert.assertScan(a(Bytes.toBytes("1_01"), Bytes.toBytes("1_02"), Bytes.toBytes("1_03")), aa(a(C1, V1), a(C1, V1), a(C1, V1)), table1.scan(Bytes.toBytes("1_"), Bytes.toBytes("2_")));
        txClient.commitOrThrow(tx2);
        Transaction tx3 = txClient.startShort();
        ((TransactionAware) table1).startTx(tx3);
        // test merging of buffered writes on existing rows
        table1.put(Bytes.toBytes("1_01"), a(C2), a(V2));
        table1.put(Bytes.toBytes("1_02"), a(C1), a(V2));
        table1.put(Bytes.toBytes("1_02a"), a(C1), a(V1));
        table1.put(Bytes.toBytes("1_02b"), a(C1), a(V1));
        table1.put(Bytes.toBytes("1_04"), a(C2), a(V2));
        // persisted values should be the same
        TableAssert.assertScan(a(Bytes.toBytes("1_01"), Bytes.toBytes("1_02"), Bytes.toBytes("1_03")), aa(a(C1, V1), a(C1, V1), a(C1, V1)), ((BufferingTable) table1).scanPersisted(new Scan(Bytes.toBytes("1_"), Bytes.toBytes("2_"))));
        // all values should be visible in buffered scan
        TableAssert.assertScan(a(Bytes.toBytes("1_01"), Bytes.toBytes("1_02"), Bytes.toBytes("1_02a"), Bytes.toBytes("1_02b"), Bytes.toBytes("1_03"), Bytes.toBytes("1_04")), aa(// 1_01
        a(C1, V1, C2, V2), // 1_02
        a(C1, V2), // 1_02a
        a(C1, V1), // 1_02b
        a(C1, V1), // 1_03
        a(C1, V1), // 1_04
        a(C2, V2)), table1.scan(Bytes.toBytes("1_"), Bytes.toBytes("2_")));
        txClient.canCommitOrThrow(tx3, ((TransactionAware) table1).getTxChanges());
        Assert.assertTrue(((TransactionAware) table1).commitTx());
        txClient.commitOrThrow(tx3);
        Transaction tx4 = txClient.startShort();
        ((TransactionAware) table1).startTx(tx4);
        // all values should be visible after commit
        TableAssert.assertScan(a(Bytes.toBytes("1_01"), Bytes.toBytes("1_02"), Bytes.toBytes("1_02a"), Bytes.toBytes("1_02b"), Bytes.toBytes("1_03"), Bytes.toBytes("1_04")), aa(// 1_01
        a(C1, V1, C2, V2), // 1_02
        a(C1, V2), // 1_02a
        a(C1, V1), // 1_02b
        a(C1, V1), // 1_03
        a(C1, V1), // 1_04
        a(C2, V2)), table1.scan(Bytes.toBytes("1_"), Bytes.toBytes("2_")));
        txClient.commitOrThrow(tx4);
    } 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) Scan(io.cdap.cdap.api.dataset.table.Scan) Test(org.junit.Test)

Example 24 with DatasetAdmin

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

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)

Example 25 with DatasetAdmin

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

the class IntegrationTestBaseTest method testSQLQuery.

@Test
public void testSQLQuery() throws Exception {
    getTestManager().deployDatasetModule(NamespaceId.DEFAULT.datasetModule("my-kv"), AppUsingCustomModule.Module.class);
    DatasetAdmin dsAdmin = getTestManager().addDatasetInstance("myKeyValueTable", NamespaceId.DEFAULT.dataset("myTable"));
    Assert.assertTrue(dsAdmin.exists());
    ApplicationManager appManager = deployApplication(NamespaceId.DEFAULT, AppUsingCustomModule.class);
    ServiceManager serviceManager = appManager.getServiceManager("MyService").start();
    serviceManager.waitForRun(ProgramRunStatus.RUNNING, 10, TimeUnit.SECONDS);
    put(serviceManager, "a", "1");
    put(serviceManager, "b", "2");
    put(serviceManager, "c", "1");
    try (Connection connection = getTestManager().getQueryClient(NamespaceId.DEFAULT);
        // the value (character) "1" corresponds to the decimal 49. In hex, that is 31.
        ResultSet results = connection.prepareStatement("select key from dataset_mytable where hex(value) = '31'").executeQuery()) {
        // run a query over the dataset
        Assert.assertTrue(results.next());
        Assert.assertEquals("a", results.getString(1));
        Assert.assertTrue(results.next());
        Assert.assertEquals("c", results.getString(1));
        Assert.assertFalse(results.next());
    }
    dsAdmin.drop();
    Assert.assertFalse(dsAdmin.exists());
}
Also used : Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) 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