Search in sources :

Example 76 with TableId

use of co.cask.cdap.data2.util.TableId in project cdap by caskdata.

the class AbstractHBaseTableUtilTest method testBackwardCompatibility.

@Test
public void testBackwardCompatibility() throws IOException, InterruptedException {
    HBaseTableUtil tableUtil = getTableUtil();
    String tablePrefix = cConf.get(Constants.Dataset.TABLE_PREFIX);
    TableId tableId = TableId.from("default", "my.dataset");
    TableId hTableId = tableUtil.createHTableId(new NamespaceId(tableId.getNamespace()), tableId.getTableName());
    create(tableId);
    TableId resultTableId = getTableId("default", "my.dataset");
    Assert.assertNotNull(resultTableId);
    Assert.assertEquals("default", resultTableId.getNamespace());
    Assert.assertEquals("cdap.user.my.dataset", HTableNameConverter.toHBaseTableName(tablePrefix, resultTableId));
    Assert.assertEquals(getTableNameAsString(tableId), Bytes.toString(tableUtil.createHTable(TEST_HBASE.getConfiguration(), hTableId).getTableName()));
    drop(tableId);
    tableId = TableId.from("default", "system.queue.config");
    hTableId = tableUtil.createHTableId(new NamespaceId(tableId.getNamespace()), tableId.getTableName());
    create(tableId);
    resultTableId = getTableId("default", "system.queue.config");
    Assert.assertEquals("default", resultTableId.getNamespace());
    Assert.assertEquals("cdap.system.queue.config", HTableNameConverter.toHBaseTableName(tablePrefix, resultTableId));
    Assert.assertEquals(getTableNameAsString(tableId), Bytes.toString(tableUtil.createHTable(TEST_HBASE.getConfiguration(), hTableId).getTableName()));
    drop(tableId);
    tableId = TableId.from("myspace", "could.be.any.table.name");
    hTableId = tableUtil.createHTableId(new NamespaceId(tableId.getNamespace()), tableId.getTableName());
    createNamespace("myspace");
    create(tableId);
    resultTableId = getTableId("myspace", "could.be.any.table.name");
    Assert.assertEquals("cdap_myspace", resultTableId.getNamespace());
    Assert.assertEquals("could.be.any.table.name", HTableNameConverter.toHBaseTableName(tablePrefix, resultTableId));
    Assert.assertEquals(getTableNameAsString(hTableId), Bytes.toString(tableUtil.createHTable(TEST_HBASE.getConfiguration(), hTableId).getTableName()));
    drop(tableId);
    deleteNamespace("myspace");
}
Also used : TableId(co.cask.cdap.data2.util.TableId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Test(org.junit.Test)

Example 77 with TableId

use of co.cask.cdap.data2.util.TableId in project cdap by caskdata.

the class AbstractHBaseTableUtilTest method testGrant.

@Test
public void testGrant() throws Exception {
    String namespace = "perm";
    TableId tableId = TableId.from("perm", "priv");
    // create a namespace and table
    if (namespacesSupported()) {
        createNamespace(namespace);
    }
    create(tableId);
    Assert.assertTrue(exists(tableId));
    // attempt to assign invalid permissions to the namespace or the table
    try {
        ddlExecutor.grantPermissions(tableId.getNamespace(), null, ImmutableMap.of("joe", "iii"));
        Assert.fail("Grant should have failed with invalid permissions");
    } catch (IOException e) {
        Assert.assertTrue(e.getMessage().contains("Unknown Action"));
    }
    try {
        getTableUtil().grantPermissions(ddlExecutor, tableId, ImmutableMap.of("@readers", "RXT"));
        Assert.fail("Grant should have failed with invalid permissions");
    } catch (IOException e) {
        Assert.assertTrue(e.getMessage().contains("Unknown Action"));
    }
    // assign some privileges to the namespace
    ddlExecutor.grantPermissions(tableId.getNamespace(), null, ImmutableMap.of("joe", "RX", "@readers", "CA"));
    // assign some privileges to the table
    getTableUtil().grantPermissions(ddlExecutor, tableId, ImmutableMap.of("joe", "RWX", "@readers", "RX"));
    // clean up
    drop(tableId);
    deleteNamespace(namespace);
}
Also used : TableId(co.cask.cdap.data2.util.TableId) IOException(java.io.IOException) Test(org.junit.Test)

Example 78 with TableId

use of co.cask.cdap.data2.util.TableId in project cdap by caskdata.

the class AbstractHBaseTableUtilTest method testTableSizeMetrics.

@Test
public void testTableSizeMetrics() throws Exception {
    HBaseTableUtil tableUtil = getTableUtil();
    // namespace should not exist
    if (namespacesSupported()) {
        Assert.assertFalse(tableUtil.hasNamespace(hAdmin, tableUtil.getHBaseNamespace(new NamespaceId("namespace"))));
    }
    Assert.assertNull(getTableStats("namespace", "table1"));
    Assert.assertNull(getTableStats("namespace", "table2"));
    Assert.assertNull(getTableStats("namespace", "table3"));
    if (namespacesSupported()) {
        createNamespace("namespace");
        createNamespace("namespace2");
        Assert.assertTrue(tableUtil.hasNamespace(hAdmin, tableUtil.getHBaseNamespace(new NamespaceId("namespace"))));
    }
    Futures.allAsList(createAsync(TableId.from("namespace", "table1")), createAsync(TableId.from("namespace2", "table1")), createAsync(TableId.from("namespace", "table2")), createAsync(TableId.from("namespace", "table3"))).get(60, TimeUnit.SECONDS);
    Assert.assertTrue(exists("namespace", "table1"));
    Assert.assertTrue(exists("namespace2", "table1"));
    Assert.assertTrue(exists("namespace", "table2"));
    Assert.assertTrue(exists("namespace", "table3"));
    Tasks.waitFor(true, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            try {
                Assert.assertEquals(0, getTableStats("namespace", "table1").getTotalSizeMB());
                Assert.assertEquals(0, getTableStats("namespace2", "table1").getTotalSizeMB());
                Assert.assertEquals(0, getTableStats("namespace", "table2").getTotalSizeMB());
                Assert.assertEquals(0, getTableStats("namespace", "table3").getTotalSizeMB());
                return true;
            } catch (Throwable t) {
                return false;
            }
        }
    }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    writeSome("namespace2", "table1");
    writeSome("namespace", "table2");
    writeSome("namespace", "table3");
    Tasks.waitFor(true, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            try {
                Assert.assertEquals(0, getTableStats("namespace", "table1").getTotalSizeMB());
                Assert.assertTrue(getTableStats("namespace2", "table1").getTotalSizeMB() > 0);
                Assert.assertTrue(getTableStats("namespace", "table2").getTotalSizeMB() > 0);
                Assert.assertTrue(getTableStats("namespace", "table3").getTotalSizeMB() > 0);
                return true;
            } catch (Throwable t) {
                return false;
            }
        }
    }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    drop("namespace", "table1");
    Assert.assertFalse(exists("namespace", "table1"));
    TableId hTableId = tableUtil.createHTableId(new NamespaceId("namespace"), "table2");
    //TODO: TestHBase methods should eventually accept namespace as a param, but will add them incrementally
    TEST_HBASE.forceRegionFlush(Bytes.toBytes(getTableNameAsString(hTableId)));
    truncate("namespace", "table3");
    Tasks.waitFor(true, new Callable<Boolean>() {

        @Override
        public Boolean call() throws Exception {
            try {
                Assert.assertNull(getTableStats("namespace", "table1"));
                Assert.assertTrue(getTableStats("namespace", "table2").getTotalSizeMB() > 0);
                Assert.assertTrue(getTableStats("namespace", "table2").getStoreFileSizeMB() > 0);
                Assert.assertEquals(0, getTableStats("namespace", "table3").getTotalSizeMB());
                return true;
            } catch (Throwable t) {
                return false;
            }
        }
    }, 5, TimeUnit.SECONDS, 100, TimeUnit.MILLISECONDS);
    // modify
    HTableDescriptor desc = getTableDescriptor("namespace2", "table1");
    HTableDescriptorBuilder newDesc = getTableUtil().buildHTableDescriptor(desc);
    newDesc.setValue("mykey", "myvalue");
    disable("namespace2", "table1");
    getTableUtil().modifyTable(ddlExecutor, newDesc.build());
    desc = getTableDescriptor("namespace2", "table1");
    Assert.assertTrue(desc.getValue("mykey").equals("myvalue"));
    enable("namespace2", "table1");
    desc = getTableDescriptor("namespace", "table2");
    Assert.assertNull(desc.getValue("myKey"));
    if (namespacesSupported()) {
        try {
            deleteNamespace("namespace");
            Assert.fail("Should not be able to delete a non-empty namespace.");
        } catch (ConstraintException e) {
        // Expected exception
        }
    }
    Futures.allAsList(dropAsync(TableId.from("namespace2", "table1")), dropAsync(TableId.from("namespace", "table2")), dropAsync(TableId.from("namespace", "table3"))).get(60, TimeUnit.SECONDS);
    if (namespacesSupported()) {
        deleteNamespace("namespace");
        deleteNamespace("namespace2");
        Assert.assertFalse(tableUtil.hasNamespace(hAdmin, "namespace"));
        Assert.assertFalse(tableUtil.hasNamespace(hAdmin, "namespace2"));
    }
}
Also used : TableId(co.cask.cdap.data2.util.TableId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) IOException(java.io.IOException) ConstraintException(org.apache.hadoop.hbase.constraint.ConstraintException) ConstraintException(org.apache.hadoop.hbase.constraint.ConstraintException) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) Test(org.junit.Test)

Example 79 with TableId

use of co.cask.cdap.data2.util.TableId in project cdap by caskdata.

the class AbstractHBaseTableUtilTest method testCustomNamespaceMap.

@Test
public void testCustomNamespaceMap() throws Exception {
    final String tableName = "mytable";
    Assert.assertEquals(HBASE_NS, getTableUtil().getHBaseNamespace(new NamespaceId(CDAP_NS)));
    Assert.assertFalse(getTableUtil().hasNamespace(hAdmin, HBASE_NS));
    createNamespace(CDAP_NS);
    // Check if namespace was created - in actual run, custom namespaces' lifecycle is managed by the user and hence
    // we won't create the namespace
    Assert.assertTrue(getTableUtil().hasNamespace(hAdmin, HBASE_NS));
    TableId tableId = TableId.from(CDAP_NS, tableName);
    create(tableId);
    List<TableId> actualTableIds = getTableUtil().listTablesInNamespace(hAdmin, HBASE_NS);
    Assert.assertEquals(1, actualTableIds.size());
    getTableUtil().deleteAllInNamespace(ddlExecutor, HBASE_NS, hAdmin.getConfiguration());
    actualTableIds = getTableUtil().listTablesInNamespace(hAdmin, HBASE_NS);
    Assert.assertTrue(actualTableIds.isEmpty());
    deleteNamespace(CDAP_NS);
    Assert.assertFalse(getTableUtil().hasNamespace(hAdmin, HBASE_NS));
}
Also used : TableId(co.cask.cdap.data2.util.TableId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) Test(org.junit.Test)

Example 80 with TableId

use of co.cask.cdap.data2.util.TableId in project cdap by caskdata.

the class IncrementHandlerTest method createTable.

@Override
public HTable createTable(TableId tableId) throws Exception {
    HBaseTableUtil tableUtil = new HBaseTableUtilFactory(cConf).get();
    HTableDescriptorBuilder tableDesc = tableUtil.buildHTableDescriptor(tableId);
    HColumnDescriptor columnDesc = new HColumnDescriptor(FAMILY);
    columnDesc.setMaxVersions(Integer.MAX_VALUE);
    columnDesc.setValue(IncrementHandlerState.PROPERTY_TRANSACTIONAL, "false");
    tableDesc.addFamily(columnDesc);
    tableDesc.addCoprocessor(IncrementHandler.class.getName());
    HTableDescriptor htd = tableDesc.build();
    TEST_HBASE.getHBaseAdmin().createTable(htd);
    TEST_HBASE.waitUntilTableAvailable(htd.getName(), 5000);
    return tableUtil.createHTable(conf, tableId);
}
Also used : HTableDescriptorBuilder(co.cask.cdap.data2.util.hbase.HTableDescriptorBuilder) HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) HBaseTableUtilFactory(co.cask.cdap.data2.util.hbase.HBaseTableUtilFactory) HBaseTableUtil(co.cask.cdap.data2.util.hbase.HBaseTableUtil) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor)

Aggregations

TableId (co.cask.cdap.data2.util.TableId)100 Test (org.junit.Test)49 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)40 HRegion (org.apache.hadoop.hbase.regionserver.HRegion)39 Put (org.apache.hadoop.hbase.client.Put)34 NamespaceId (co.cask.cdap.proto.id.NamespaceId)26 Cell (org.apache.hadoop.hbase.Cell)24 Scan (org.apache.hadoop.hbase.client.Scan)24 HTableDescriptorBuilder (co.cask.cdap.data2.util.hbase.HTableDescriptorBuilder)18 RegionScanner (org.apache.hadoop.hbase.regionserver.RegionScanner)17 HBaseTableUtil (co.cask.cdap.data2.util.hbase.HBaseTableUtil)15 HBaseTableUtilFactory (co.cask.cdap.data2.util.hbase.HBaseTableUtilFactory)15 IOException (java.io.IOException)13 HTable (org.apache.hadoop.hbase.client.HTable)11 Delete (org.apache.hadoop.hbase.client.Delete)9 HBaseAdmin (org.apache.hadoop.hbase.client.HBaseAdmin)9 HBaseDDLExecutor (co.cask.cdap.spi.hbase.HBaseDDLExecutor)8 Path (org.apache.hadoop.fs.Path)8 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)8 HRegionInfo (org.apache.hadoop.hbase.HRegionInfo)8