use of co.cask.cdap.data2.util.TableId in project cdap by caskdata.
the class HBaseTableFactory method createMetadataTable.
@Override
public MetadataTable createMetadataTable(String tableName) throws IOException {
TableId tableId = tableUtil.createHTableId(NamespaceId.SYSTEM, tableName);
HTable hTable = null;
// If the table descriptor is in the cache, we assume the table exists.
if (!tableDescriptors.containsKey(tableId)) {
synchronized (this) {
if (!tableDescriptors.containsKey(tableId)) {
try (HBaseDDLExecutor ddlExecutor = ddlExecutorFactory.get()) {
ColumnFamilyDescriptorBuilder cfdBuilder = HBaseTableUtil.getColumnFamilyDescriptorBuilder(Bytes.toString(COLUMN_FAMILY), hConf);
TableDescriptorBuilder tdBuilder = HBaseTableUtil.getTableDescriptorBuilder(tableId, cConf).addColumnFamily(cfdBuilder.build());
ddlExecutor.createTableIfNotExists(tdBuilder.build(), null);
hTable = tableUtil.createHTable(hConf, tableId);
tableDescriptors.put(tableId, hTable.getTableDescriptor());
}
}
}
}
if (hTable == null) {
hTable = tableUtil.createHTable(hConf, tableId);
}
return new HBaseMetadataTable(tableUtil, hTable, COLUMN_FAMILY, cConf.getInt(Constants.MessagingSystem.HBASE_SCAN_CACHE_ROWS), createExceptionHandler(tableId));
}
use of co.cask.cdap.data2.util.TableId 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);
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 co.cask.cdap.data2.util.TableId in project cdap by caskdata.
the class HBaseTableFactory method createTable.
/**
* Creates a new instance of {@link HTable} 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.
HTable hTable = 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);
hTable = tableUtil.createHTable(hConf, tableId);
htd = hTable.getTableDescriptor();
tableDescriptors.put(tableId, htd);
} else {
hTable = tableUtil.createHTable(hConf, tableId);
htd = hTable.getTableDescriptor();
tableDescriptors.put(tableId, htd);
}
}
}
}
}
if (hTable == null) {
hTable = tableUtil.createHTable(hConf, tableId);
}
hTable.setAutoFlushTo(false);
return new HTableWithRowKeyDistributor(hTable, new RowKeyDistributorByHashPrefix(new OneByteSimpleHash(getKeyDistributorBuckets(tableId, htd))));
}
use of co.cask.cdap.data2.util.TableId in project cdap by caskdata.
the class LevelDBDatasetMetricsReporter method report.
private void report(Map<TableId, LevelDBTableService.TableStats> datasetStat) throws DatasetManagementException {
for (Map.Entry<TableId, LevelDBTableService.TableStats> statEntry : datasetStat.entrySet()) {
String namespace = statEntry.getKey().getNamespace();
// emit metrics for only user datasets, tables in system namespace are ignored
if (NamespaceId.SYSTEM.getNamespace().equals(namespace)) {
continue;
}
String tableName = statEntry.getKey().getTableName();
Collection<DatasetSpecificationSummary> instances = dsFramework.getInstances(new NamespaceId(namespace));
for (DatasetSpecificationSummary spec : instances) {
DatasetSpecification specification = dsFramework.getDatasetSpec(new DatasetId(namespace, spec.getName()));
if (specification.isParent(tableName)) {
MetricsContext collector = metricsService.getContext(ImmutableMap.of(Constants.Metrics.Tag.NAMESPACE, namespace, Constants.Metrics.Tag.DATASET, spec.getName()));
int sizeInMb = (int) (statEntry.getValue().getDiskSizeBytes() / BYTES_IN_MB);
collector.gauge("dataset.size.mb", sizeInMb);
break;
}
}
}
}
use of co.cask.cdap.data2.util.TableId in project cdap by caskdata.
the class HBaseQueueTest method testQueueTableNameFormat.
// TODO: CDAP-1177 Should move to QueueTest after making getApplicationName() etc instance methods in a base class
@Test
public void testQueueTableNameFormat() throws Exception {
QueueName queueName = QueueName.fromFlowlet(NamespaceId.DEFAULT.getEntityName(), "application1", "flow1", "flowlet1", "output1");
HBaseQueueAdmin hbaseQueueAdmin = (HBaseQueueAdmin) queueAdmin;
TableId tableId = hbaseQueueAdmin.getDataTableId(queueName);
Assert.assertEquals(NamespaceId.DEFAULT.getEntityName(), tableId.getNamespace());
Assert.assertEquals("system." + hbaseQueueAdmin.getType() + ".application1.flow1", tableId.getTableName());
String tableName = tableUtil.buildHTableDescriptor(tableId).build().getNameAsString();
Assert.assertEquals("application1", HBaseQueueAdmin.getApplicationName(tableName));
Assert.assertEquals("flow1", HBaseQueueAdmin.getFlowName(tableName));
queueName = QueueName.fromFlowlet("testNamespace", "application1", "flow1", "flowlet1", "output1");
tableId = hbaseQueueAdmin.getDataTableId(queueName);
Assert.assertEquals(String.format("%s_testNamespace", TABLE_PREFIX), tableId.getNamespace());
Assert.assertEquals("system." + hbaseQueueAdmin.getType() + ".application1.flow1", tableId.getTableName());
tableName = tableUtil.buildHTableDescriptor(tableId).build().getNameAsString();
Assert.assertEquals("application1", HBaseQueueAdmin.getApplicationName(tableName));
Assert.assertEquals("flow1", HBaseQueueAdmin.getFlowName(tableName));
}
Aggregations