Search in sources :

Example 81 with Transaction

use of org.apache.tephra.Transaction in project cdap by caskdata.

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 {
        // write r1...r5 and commit
        Transaction tx1 = txClient.startShort();
        Table myTable1 = getTable(CONTEXT1, MY_TABLE);
        ((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();
        Table myTable2 = getTable(CONTEXT1, MY_TABLE);
        ((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(co.cask.cdap.api.dataset.table.Table) HBaseTable(co.cask.cdap.data2.dataset2.lib.table.hbase.HBaseTable) Transaction(org.apache.tephra.Transaction) TransactionAware(org.apache.tephra.TransactionAware) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) Scan(co.cask.cdap.api.dataset.table.Scan) Test(org.junit.Test)

Example 82 with Transaction

use of org.apache.tephra.Transaction 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();
    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);
    }
    // drop table
    admin.drop();
}
Also used : Table(co.cask.cdap.api.dataset.table.Table) HBaseTable(co.cask.cdap.data2.dataset2.lib.table.hbase.HBaseTable) Transaction(org.apache.tephra.Transaction) TransactionAware(org.apache.tephra.TransactionAware) Get(co.cask.cdap.api.dataset.table.Get) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) Put(co.cask.cdap.api.dataset.table.Put) Test(org.junit.Test)

Example 83 with Transaction

use of org.apache.tephra.Transaction in project cdap by caskdata.

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(co.cask.cdap.api.dataset.table.Table) HBaseTable(co.cask.cdap.data2.dataset2.lib.table.hbase.HBaseTable) Transaction(org.apache.tephra.Transaction) DatasetAdmin(co.cask.cdap.api.dataset.DatasetAdmin) Put(co.cask.cdap.api.dataset.table.Put) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 84 with Transaction

use of org.apache.tephra.Transaction in project cdap by caskdata.

the class InMemoryTableServiceTest method testInternalsNotLeaking.

@Test
public void testInternalsNotLeaking() {
    // Test that there's no way to break the state of InMemoryTableService by changing parameters of update
    // methods (after method invocation) or by changing returned values "in-place"
    InMemoryTableService.create("table");
    // verify writing thru merge is guarded
    NavigableMap<byte[], NavigableMap<byte[], Update>> updates = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    NavigableMap<byte[], Update> rowUpdate = Maps.newTreeMap(Bytes.BYTES_COMPARATOR);
    byte[] rowParam = new byte[] { 1 };
    byte[] columnParam = new byte[] { 2 };
    byte[] valParam = new byte[] { 3 };
    rowUpdate.put(columnParam, new PutValue(valParam));
    updates.put(rowParam, rowUpdate);
    InMemoryTableService.merge("table", updates, 1L);
    verify123();
    updates.remove(rowParam);
    rowUpdate.remove(columnParam);
    rowParam[0]++;
    columnParam[0]++;
    valParam[0]++;
    verify123();
    // verify changing returned data from get doesn't affect the stored data
    NavigableMap<byte[], NavigableMap<Long, byte[]>> rowFromGet = InMemoryTableService.get("table", new byte[] { 1 }, new Transaction(1L, 2L, new long[0], new long[0], 1L));
    Assert.assertEquals(1, rowFromGet.size());
    byte[] columnFromGet = rowFromGet.firstEntry().getKey();
    Assert.assertArrayEquals(new byte[] { 2 }, columnFromGet);
    byte[] valFromGet = rowFromGet.firstEntry().getValue().get(1L);
    Assert.assertArrayEquals(new byte[] { 3 }, valFromGet);
    rowFromGet.firstEntry().getValue().remove(1L);
    rowFromGet.remove(columnFromGet);
    columnFromGet[0]++;
    valFromGet[0]++;
    verify123();
    // verify changing returned data doesn't affect the stored data
    NavigableMap<byte[], NavigableMap<byte[], NavigableMap<Long, byte[]>>> fromGetRange = InMemoryTableService.getRowRange("table", null, null, new Transaction(1L, 2L, new long[0], new long[0], 1L));
    Assert.assertEquals(1, fromGetRange.size());
    byte[] keyFromGetRange = fromGetRange.firstEntry().getKey();
    Assert.assertArrayEquals(new byte[] { 1 }, keyFromGetRange);
    NavigableMap<byte[], NavigableMap<Long, byte[]>> rowFromGetRange = fromGetRange.get(new byte[] { 1 });
    Assert.assertEquals(1, rowFromGetRange.size());
    byte[] columnFromGetRange = rowFromGetRange.firstEntry().getKey();
    Assert.assertArrayEquals(new byte[] { 2 }, columnFromGetRange);
    byte[] valFromGetRange = rowFromGetRange.firstEntry().getValue().get(1L);
    Assert.assertArrayEquals(new byte[] { 3 }, valFromGetRange);
    rowFromGetRange.firstEntry().getValue().remove(1L);
    rowFromGetRange.remove(columnFromGetRange);
    fromGetRange.remove(keyFromGetRange);
    keyFromGetRange[0]++;
    columnFromGetRange[0]++;
    valFromGet[0]++;
    verify123();
}
Also used : PutValue(co.cask.cdap.data2.dataset2.lib.table.PutValue) NavigableMap(java.util.NavigableMap) Transaction(org.apache.tephra.Transaction) Update(co.cask.cdap.data2.dataset2.lib.table.Update) Test(org.junit.Test)

Example 85 with Transaction

use of org.apache.tephra.Transaction in project cdap by caskdata.

the class InMemoryTableServiceTest method verify123.

private void verify123() {
    NavigableMap<byte[], NavigableMap<Long, byte[]>> rowFromGet = InMemoryTableService.get("table", new byte[] { 1 }, new Transaction(1L, 2L, new long[0], new long[0], 1L));
    Assert.assertEquals(1, rowFromGet.size());
    Assert.assertArrayEquals(new byte[] { 2 }, rowFromGet.firstEntry().getKey());
    Assert.assertArrayEquals(new byte[] { 3 }, rowFromGet.firstEntry().getValue().get(1L));
}
Also used : NavigableMap(java.util.NavigableMap) Transaction(org.apache.tephra.Transaction)

Aggregations

Transaction (org.apache.tephra.Transaction)99 Test (org.junit.Test)54 TransactionAware (org.apache.tephra.TransactionAware)34 Table (co.cask.cdap.api.dataset.table.Table)29 DatasetAdmin (co.cask.cdap.api.dataset.DatasetAdmin)27 HBaseTable (co.cask.cdap.data2.dataset2.lib.table.hbase.HBaseTable)22 Put (co.cask.cdap.api.dataset.table.Put)12 DatasetProperties (co.cask.cdap.api.dataset.DatasetProperties)11 Get (co.cask.cdap.api.dataset.table.Get)10 TransactionSystemClient (org.apache.tephra.TransactionSystemClient)10 Row (co.cask.cdap.api.dataset.table.Row)8 ConsumerConfig (co.cask.cdap.data2.queue.ConsumerConfig)8 KeyStructValueTableDefinition (co.cask.cdap.explore.service.datasets.KeyStructValueTableDefinition)8 Scan (co.cask.cdap.api.dataset.table.Scan)7 ArrayList (java.util.ArrayList)7 CConfiguration (co.cask.cdap.common.conf.CConfiguration)6 ExploreExecutionResult (co.cask.cdap.explore.client.ExploreExecutionResult)6 DatasetId (co.cask.cdap.proto.id.DatasetId)6 IOException (java.io.IOException)6 BufferingTableTest (co.cask.cdap.data2.dataset2.lib.table.BufferingTableTest)5