Search in sources :

Example 11 with HTableDescriptorBuilder

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

the class HBaseTableFactory method upgradeCoProcessor.

private void upgradeCoProcessor(TableId tableId, Class<? extends Coprocessor> coprocessor) throws IOException {
    try (HBaseDDLExecutor ddlExecutor = ddlExecutorFactory.get()) {
        HTableDescriptor tableDescriptor;
        try (HBaseAdmin admin = new HBaseAdmin(hConf)) {
            // If table doesn't exist, then skip upgrading coprocessor
            if (!tableUtil.tableExists(admin, tableId)) {
                LOG.debug("TMS Table {} was not found. Skip upgrading coprocessor.", tableId);
                return;
            }
            tableDescriptor = tableUtil.getHTableDescriptor(admin, tableId);
        }
        // Get cdap version from the table
        ProjectInfo.Version version = HBaseTableUtil.getVersion(tableDescriptor);
        if (version.compareTo(ProjectInfo.getVersion()) >= 0) {
            // If cdap has version has not changed or is greater, no need to update. Just enable it, in case
            // it has been disabled by the upgrade tool, and return
            LOG.info("Table '{}' has not changed and its version '{}' is same or greater than current CDAP version '{}'", tableId, version, ProjectInfo.getVersion());
            enableTable(ddlExecutor, tableId);
            return;
        }
        // create a new descriptor for the table update
        HTableDescriptorBuilder newDescriptor = tableUtil.buildHTableDescriptor(tableDescriptor);
        // Remove old coprocessor
        Map<String, HBaseTableUtil.CoprocessorInfo> coprocessorInfo = HBaseTableUtil.getCoprocessorInfo(tableDescriptor);
        for (Map.Entry<String, HBaseTableUtil.CoprocessorInfo> coprocessorEntry : coprocessorInfo.entrySet()) {
            newDescriptor.removeCoprocessor(coprocessorEntry.getValue().getClassName());
        }
        // Add new coprocessor
        CoprocessorDescriptor coprocessorDescriptor = coprocessorManager.getCoprocessorDescriptor(coprocessor, Coprocessor.PRIORITY_USER);
        Path path = coprocessorDescriptor.getPath() == null ? null : new Path(coprocessorDescriptor.getPath());
        newDescriptor.addCoprocessor(coprocessorDescriptor.getClassName(), path, coprocessorDescriptor.getPriority(), coprocessorDescriptor.getProperties());
        // Update CDAP version, table prefix
        HBaseTableUtil.setVersion(newDescriptor);
        HBaseTableUtil.setTablePrefix(newDescriptor, cConf);
        // Disable Table
        disableTable(ddlExecutor, tableId);
        tableUtil.modifyTable(ddlExecutor, newDescriptor.build());
        LOG.debug("Enabling table '{}'...", tableId);
        enableTable(ddlExecutor, tableId);
    }
    LOG.info("Table '{}' update completed.", tableId);
}
Also used : HBaseDDLExecutor(co.cask.cdap.spi.hbase.HBaseDDLExecutor) Path(org.apache.hadoop.fs.Path) HTableDescriptorBuilder(co.cask.cdap.data2.util.hbase.HTableDescriptorBuilder) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) ProjectInfo(co.cask.cdap.common.utils.ProjectInfo) CoprocessorDescriptor(co.cask.cdap.spi.hbase.CoprocessorDescriptor) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 12 with HTableDescriptorBuilder

use of co.cask.cdap.data2.util.hbase.HTableDescriptorBuilder 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 13 with HTableDescriptorBuilder

use of co.cask.cdap.data2.util.hbase.HTableDescriptorBuilder 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)

Example 14 with HTableDescriptorBuilder

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

the class IncrementSummingScannerTest method createRegion.

static HRegion createRegion(Configuration hConf, CConfiguration cConf, TableId tableId, HColumnDescriptor cfd) throws Exception {
    HBaseTableUtil tableUtil = new HBaseTableUtilFactory(cConf).get();
    HTableDescriptorBuilder htd = tableUtil.buildHTableDescriptor(tableId);
    cfd.setMaxVersions(Integer.MAX_VALUE);
    cfd.setKeepDeletedCells(true);
    htd.addFamily(cfd);
    htd.addCoprocessor(IncrementHandler.class.getName());
    HTableDescriptor desc = htd.build();
    String tableName = desc.getNameAsString();
    Path tablePath = new Path("/tmp/" + tableName);
    Path hlogPath = new Path("/tmp/hlog-" + tableName);
    FileSystem fs = FileSystem.get(hConf);
    assertTrue(fs.mkdirs(tablePath));
    WALFactory walFactory = new WALFactory(hConf, null, hlogPath.toString());
    WAL hLog = walFactory.getWAL(new byte[] { 1 });
    HRegionInfo regionInfo = new HRegionInfo(desc.getTableName());
    HRegionFileSystem regionFS = HRegionFileSystem.createRegionOnFileSystem(hConf, fs, tablePath, regionInfo);
    return new HRegion(regionFS, hLog, hConf, desc, new LocalRegionServerServices(hConf, ServerName.valueOf(InetAddress.getLocalHost().getHostName(), 0, System.currentTimeMillis())));
}
Also used : Path(org.apache.hadoop.fs.Path) HTableDescriptorBuilder(co.cask.cdap.data2.util.hbase.HTableDescriptorBuilder) WAL(org.apache.hadoop.hbase.wal.WAL) HBaseTableUtil(co.cask.cdap.data2.util.hbase.HBaseTableUtil) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) HRegion(org.apache.hadoop.hbase.regionserver.HRegion) HRegionFileSystem(org.apache.hadoop.hbase.regionserver.HRegionFileSystem) FileSystem(org.apache.hadoop.fs.FileSystem) HRegionFileSystem(org.apache.hadoop.hbase.regionserver.HRegionFileSystem) HBaseTableUtilFactory(co.cask.cdap.data2.util.hbase.HBaseTableUtilFactory) WALFactory(org.apache.hadoop.hbase.wal.WALFactory)

Example 15 with HTableDescriptorBuilder

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

the class IncrementSummingScannerTest method createRegion.

static HRegion createRegion(Configuration hConf, CConfiguration cConf, TableId tableId, HColumnDescriptor cfd) throws Exception {
    HBaseTableUtil tableUtil = new HBaseTableUtilFactory(cConf).get();
    HTableDescriptorBuilder htd = tableUtil.buildHTableDescriptor(tableId);
    cfd.setMaxVersions(Integer.MAX_VALUE);
    cfd.setKeepDeletedCells(true);
    htd.addFamily(cfd);
    htd.addCoprocessor(IncrementHandler.class.getName());
    HTableDescriptor desc = htd.build();
    String tableName = desc.getNameAsString();
    Path tablePath = new Path("/tmp/" + tableName);
    Path hlogPath = new Path("/tmp/hlog-" + tableName);
    FileSystem fs = FileSystem.get(hConf);
    assertTrue(fs.mkdirs(tablePath));
    WALFactory walFactory = new WALFactory(hConf, null, hlogPath.toString());
    WAL hLog = walFactory.getWAL(new byte[] { 1 });
    HRegionInfo regionInfo = new HRegionInfo(desc.getTableName());
    HRegionFileSystem regionFS = HRegionFileSystem.createRegionOnFileSystem(hConf, fs, tablePath, regionInfo);
    return new HRegion(regionFS, hLog, hConf, desc, new LocalRegionServerServices(hConf, ServerName.valueOf(InetAddress.getLocalHost().getHostName(), 0, System.currentTimeMillis())));
}
Also used : Path(org.apache.hadoop.fs.Path) HTableDescriptorBuilder(co.cask.cdap.data2.util.hbase.HTableDescriptorBuilder) WAL(org.apache.hadoop.hbase.wal.WAL) HBaseTableUtil(co.cask.cdap.data2.util.hbase.HBaseTableUtil) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) HRegion(org.apache.hadoop.hbase.regionserver.HRegion) HRegionFileSystem(org.apache.hadoop.hbase.regionserver.HRegionFileSystem) FileSystem(org.apache.hadoop.fs.FileSystem) HRegionFileSystem(org.apache.hadoop.hbase.regionserver.HRegionFileSystem) HBaseTableUtilFactory(co.cask.cdap.data2.util.hbase.HBaseTableUtilFactory) WALFactory(org.apache.hadoop.hbase.wal.WALFactory)

Aggregations

HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)17 HTableDescriptorBuilder (co.cask.cdap.data2.util.hbase.HTableDescriptorBuilder)16 HBaseTableUtil (co.cask.cdap.data2.util.hbase.HBaseTableUtil)15 HBaseTableUtilFactory (co.cask.cdap.data2.util.hbase.HBaseTableUtilFactory)14 Path (org.apache.hadoop.fs.Path)8 FileSystem (org.apache.hadoop.fs.FileSystem)7 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)7 HRegionInfo (org.apache.hadoop.hbase.HRegionInfo)7 HRegion (org.apache.hadoop.hbase.regionserver.HRegion)7 HRegionFileSystem (org.apache.hadoop.hbase.regionserver.HRegionFileSystem)7 WAL (org.apache.hadoop.hbase.wal.WAL)5 WALFactory (org.apache.hadoop.hbase.wal.WALFactory)5 ProjectInfo (co.cask.cdap.common.utils.ProjectInfo)2 MockRegionServerServices (co.cask.cdap.data2.util.hbase.MockRegionServerServices)2 HBaseDDLExecutor (co.cask.cdap.spi.hbase.HBaseDDLExecutor)2 HBaseAdmin (org.apache.hadoop.hbase.client.HBaseAdmin)2 HLog (org.apache.hadoop.hbase.regionserver.wal.HLog)2 TableId (co.cask.cdap.data2.util.TableId)1 NamespaceId (co.cask.cdap.proto.id.NamespaceId)1 CoprocessorDescriptor (co.cask.cdap.spi.hbase.CoprocessorDescriptor)1