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