Search in sources :

Example 11 with HBaseDDLExecutor

use of io.cdap.cdap.spi.hbase.HBaseDDLExecutor in project cdap by cdapio.

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);
        String hbaseVersion = HBaseTableUtil.getHBaseVersion(tableDescriptor);
        if (hbaseVersion != null && hbaseVersion.equals(HBaseVersion.getVersionString()) && 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 '{}'." + " The underlying HBase version {} has also not changed.", tableId, version, ProjectInfo.getVersion(), hbaseVersion);
            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.setHBaseVersion(newDescriptor);
        HBaseTableUtil.setTablePrefix(newDescriptor, cConf);
        // Disable auto-splitting
        newDescriptor.setValue(HTableDescriptor.SPLIT_POLICY, cConf.get(Constants.MessagingSystem.TABLE_HBASE_SPLIT_POLICY));
        // 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(io.cdap.cdap.spi.hbase.HBaseDDLExecutor) Path(org.apache.hadoop.fs.Path) HTableDescriptorBuilder(io.cdap.cdap.data2.util.hbase.HTableDescriptorBuilder) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) ProjectInfo(io.cdap.cdap.common.utils.ProjectInfo) CoprocessorDescriptor(io.cdap.cdap.spi.hbase.CoprocessorDescriptor) Map(java.util.Map) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 12 with HBaseDDLExecutor

use of io.cdap.cdap.spi.hbase.HBaseDDLExecutor in project cdap by cdapio.

the class HBaseTableFactory method createTable.

/**
 * Creates a new instance of {@link Table} for the given {@link TableId}. If the hbase table doesn't
 * exist, a new one will be created with the given number of splits.
 */
private HTableWithRowKeyDistributor createTable(TableId tableId, int splits, Class<? extends Coprocessor> coprocessor) throws IOException {
    // Lookup the table descriptor from the cache first. If it is there, we assume the HBase table exists
    // Otherwise, attempt to create it.
    Table table = null;
    HTableDescriptor htd = tableDescriptors.get(tableId);
    if (htd == null) {
        synchronized (this) {
            htd = tableDescriptors.get(tableId);
            if (htd == null) {
                boolean tableExists;
                try (HBaseAdmin admin = new HBaseAdmin(hConf)) {
                    tableExists = tableUtil.tableExists(admin, tableId);
                }
                // Create the table if the table doesn't exist
                try (HBaseDDLExecutor ddlExecutor = ddlExecutorFactory.get()) {
                    // If table exists, then skip creating coprocessor etc
                    if (!tableExists) {
                        TableId metadataTableId = tableUtil.createHTableId(NamespaceId.SYSTEM, cConf.get(Constants.MessagingSystem.METADATA_TABLE_NAME));
                        ColumnFamilyDescriptorBuilder cfdBuilder = HBaseTableUtil.getColumnFamilyDescriptorBuilder(Bytes.toString(COLUMN_FAMILY), hConf);
                        TableDescriptorBuilder tdBuilder = HBaseTableUtil.getTableDescriptorBuilder(tableId, cConf).addColumnFamily(cfdBuilder.build()).addProperty(Constants.MessagingSystem.HBASE_MESSAGING_TABLE_PREFIX_NUM_BYTES, Integer.toString(1)).addProperty(Constants.MessagingSystem.KEY_DISTRIBUTOR_BUCKETS_ATTR, Integer.toString(splits)).addProperty(Constants.MessagingSystem.HBASE_METADATA_TABLE_NAMESPACE, metadataTableId.getNamespace()).addProperty(HTableDescriptor.SPLIT_POLICY, cConf.get(Constants.MessagingSystem.TABLE_HBASE_SPLIT_POLICY)).addCoprocessor(coprocessorManager.getCoprocessorDescriptor(coprocessor, Coprocessor.PRIORITY_USER));
                        // Set the key distributor size the same as the initial number of splits,
                        // essentially one bucket per split.
                        byte[][] splitKeys = HBaseTableUtil.getSplitKeys(splits, splits, new RowKeyDistributorByHashPrefix(new OneByteSimpleHash(splits)));
                        ddlExecutor.createTableIfNotExists(tdBuilder.build(), splitKeys);
                        table = tableUtil.createTable(hConf, tableId);
                        htd = table.getTableDescriptor();
                        tableDescriptors.put(tableId, htd);
                    } else {
                        table = tableUtil.createTable(hConf, tableId);
                        htd = table.getTableDescriptor();
                        tableDescriptors.put(tableId, htd);
                    }
                }
            }
        }
    }
    if (table == null) {
        table = tableUtil.createTable(hConf, tableId);
    }
    return new HTableWithRowKeyDistributor(table, new RowKeyDistributorByHashPrefix(new OneByteSimpleHash(getKeyDistributorBuckets(tableId, htd))));
}
Also used : HBaseDDLExecutor(io.cdap.cdap.spi.hbase.HBaseDDLExecutor) TableId(io.cdap.cdap.data2.util.TableId) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) MetadataTable(io.cdap.cdap.messaging.store.MetadataTable) MessageTable(io.cdap.cdap.messaging.store.MessageTable) PayloadTable(io.cdap.cdap.messaging.store.PayloadTable) Table(org.apache.hadoop.hbase.client.Table) RowKeyDistributorByHashPrefix(io.cdap.cdap.hbase.wd.RowKeyDistributorByHashPrefix) ColumnFamilyDescriptorBuilder(io.cdap.cdap.data2.util.hbase.ColumnFamilyDescriptorBuilder) OneByteSimpleHash(io.cdap.cdap.hbase.wd.RowKeyDistributorByHashPrefix.OneByteSimpleHash) HTableDescriptorBuilder(io.cdap.cdap.data2.util.hbase.HTableDescriptorBuilder) TableDescriptorBuilder(io.cdap.cdap.data2.util.hbase.TableDescriptorBuilder) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor)

Example 13 with HBaseDDLExecutor

use of io.cdap.cdap.spi.hbase.HBaseDDLExecutor in project cdap by cdapio.

the class DistributedStorageProviderNamespaceAdmin method create.

@Override
public void create(NamespaceMeta namespaceMeta) throws IOException, ExploreException, SQLException {
    // create filesystem directory
    super.create(namespaceMeta);
    // skip namespace creation in HBase for default namespace
    if (NamespaceId.DEFAULT.equals(namespaceMeta.getNamespaceId())) {
        return;
    }
    // create HBase namespace and set group C(reate) permission if a group is configured
    String hbaseNamespace = tableUtil.getHBaseNamespace(namespaceMeta);
    if (Strings.isNullOrEmpty(namespaceMeta.getConfig().getHbaseNamespace())) {
        try (HBaseDDLExecutor executor = hBaseDDLExecutorFactory.get()) {
            boolean created = executor.createNamespaceIfNotExists(hbaseNamespace);
            if (namespaceMeta.getConfig().getGroupName() != null) {
                try {
                    executor.grantPermissions(hbaseNamespace, null, ImmutableMap.of("@" + namespaceMeta.getConfig().getGroupName(), "C"));
                } catch (IOException | RuntimeException e) {
                    // don't leave a partial state behind, as this fails the create(), the namespace should be removed
                    if (created) {
                        try {
                            executor.deleteNamespaceIfExists(hbaseNamespace);
                        } catch (Throwable t) {
                            e.addSuppressed(t);
                        }
                    }
                    throw e;
                }
            }
        } catch (Throwable t) {
            try {
                // if we failed to create a namespace in hbase then do clean up for above creations
                super.delete(namespaceMeta.getNamespaceId());
            } catch (Exception e) {
                t.addSuppressed(e);
            }
            throw t;
        }
    }
    try (HBaseAdmin admin = new HBaseAdmin(hConf)) {
        if (!tableUtil.hasNamespace(admin, hbaseNamespace)) {
            throw new IOException(String.format("HBase namespace '%s' specified for new namespace '%s' does not" + " exist. Please specify an existing HBase namespace.", hbaseNamespace, namespaceMeta.getName()));
        }
    }
}
Also used : HBaseDDLExecutor(io.cdap.cdap.spi.hbase.HBaseDDLExecutor) HBaseAdmin(org.apache.hadoop.hbase.client.HBaseAdmin) IOException(java.io.IOException) IOException(java.io.IOException) ExploreException(io.cdap.cdap.explore.service.ExploreException) SQLException(java.sql.SQLException)

Example 14 with HBaseDDLExecutor

use of io.cdap.cdap.spi.hbase.HBaseDDLExecutor in project cdap by cdapio.

the class HBaseDDLExecutorFactory method get.

@Override
public HBaseDDLExecutor get() {
    // Check if HBaseDDLExecutor extension is provided
    Map<String, HBaseDDLExecutor> extensions = hBaseDDLExecutorLoader.getAll();
    HBaseDDLExecutor executor;
    if (!extensions.isEmpty()) {
        // HBase DDL executor extension is provided.
        executor = extensions.values().iterator().next();
    } else {
        if (extensionDir != null) {
            // Extension directory is provided but the extension is not loaded
            throw new RuntimeException(String.format("HBaseDDLExecutor extension cannot be loaded from directory '%s'." + " Please make sure jar is available at that location with " + "appropriate permissions.", extensionDir));
        }
        // Return the version specific executor instance.
        executor = super.get();
    }
    executor.initialize(context);
    return executor;
}
Also used : HBaseDDLExecutor(io.cdap.cdap.spi.hbase.HBaseDDLExecutor)

Example 15 with HBaseDDLExecutor

use of io.cdap.cdap.spi.hbase.HBaseDDLExecutor in project cdap by caskdata.

the class MetricHBaseTableUtilTest method beforeClass.

@BeforeClass
public static void beforeClass() throws Exception {
    cConf = CConfiguration.create();
    hBaseTableUtil = new HBaseTableUtilFactory(cConf, new SimpleNamespaceQueryAdmin()).get();
    HBaseDDLExecutor executor = new HBaseDDLExecutorFactory(cConf, TEST_HBASE.getHBaseAdmin().getConfiguration()).get();
    executor.createNamespaceIfNotExists(hBaseTableUtil.getHBaseNamespace(NamespaceId.SYSTEM));
}
Also used : HBaseDDLExecutor(io.cdap.cdap.spi.hbase.HBaseDDLExecutor) SimpleNamespaceQueryAdmin(io.cdap.cdap.common.namespace.SimpleNamespaceQueryAdmin) HBaseDDLExecutorFactory(io.cdap.cdap.data2.util.hbase.HBaseDDLExecutorFactory) HBaseTableUtilFactory(io.cdap.cdap.data2.util.hbase.HBaseTableUtilFactory) BeforeClass(org.junit.BeforeClass)

Aggregations

HBaseDDLExecutor (io.cdap.cdap.spi.hbase.HBaseDDLExecutor)22 HTableDescriptorBuilder (io.cdap.cdap.data2.util.hbase.HTableDescriptorBuilder)10 IOException (java.io.IOException)8 HBaseAdmin (org.apache.hadoop.hbase.client.HBaseAdmin)8 TableId (io.cdap.cdap.data2.util.TableId)6 ColumnFamilyDescriptorBuilder (io.cdap.cdap.data2.util.hbase.ColumnFamilyDescriptorBuilder)6 TableDescriptorBuilder (io.cdap.cdap.data2.util.hbase.TableDescriptorBuilder)6 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)6 ProjectInfo (io.cdap.cdap.common.utils.ProjectInfo)4 ExploreException (io.cdap.cdap.explore.service.ExploreException)4 MessageTable (io.cdap.cdap.messaging.store.MessageTable)4 MetadataTable (io.cdap.cdap.messaging.store.MetadataTable)4 PayloadTable (io.cdap.cdap.messaging.store.PayloadTable)4 SQLException (java.sql.SQLException)4 Map (java.util.Map)4 Table (org.apache.hadoop.hbase.client.Table)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 SimpleNamespaceQueryAdmin (io.cdap.cdap.common.namespace.SimpleNamespaceQueryAdmin)2 HBaseDDLExecutorFactory (io.cdap.cdap.data2.util.hbase.HBaseDDLExecutorFactory)2 HBaseTableUtil (io.cdap.cdap.data2.util.hbase.HBaseTableUtil)2