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);
}
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))));
}
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()));
}
}
}
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;
}
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));
}
Aggregations