use of org.apache.hadoop.hbase.client.CoprocessorDescriptor in project hbase by apache.
the class CoprocessorValidator method validateTables.
@InterfaceAudience.Private
protected void validateTables(ClassLoader classLoader, Admin admin, Pattern pattern, List<CoprocessorViolation> violations) throws IOException {
List<TableDescriptor> tableDescriptors = admin.listTableDescriptors(pattern);
for (TableDescriptor tableDescriptor : tableDescriptors) {
LOG.debug("Validating table {}", tableDescriptor.getTableName());
Collection<CoprocessorDescriptor> coprocessorDescriptors = tableDescriptor.getCoprocessorDescriptors();
for (CoprocessorDescriptor coprocessorDescriptor : coprocessorDescriptors) {
String className = coprocessorDescriptor.getClassName();
Optional<String> jarPath = coprocessorDescriptor.getJarPath();
if (jarPath.isPresent()) {
org.apache.hadoop.fs.Path path = new org.apache.hadoop.fs.Path(jarPath.get());
try (ResolverUrlClassLoader cpClassLoader = createClassLoader(classLoader, path)) {
validate(cpClassLoader, className, violations);
} catch (IOException e) {
CoprocessorViolation violation = new CoprocessorViolation(className, Severity.ERROR, "could not validate jar file '" + path + "'", e);
violations.add(violation);
}
} else {
validate(classLoader, className, violations);
}
}
}
}
use of org.apache.hadoop.hbase.client.CoprocessorDescriptor in project hbase by apache.
the class TestClassLoading method testHBase3810.
@Test
public // less strict
void testHBase3810() throws Exception {
// allowed value pattern: [path] | class name | [priority] | [key values]
File jarFile1 = buildCoprocessorJar(cpName1);
File jarFile2 = buildCoprocessorJar(cpName2);
File jarFile5 = buildCoprocessorJar(cpName5);
File jarFile6 = buildCoprocessorJar(cpName6);
String cpKey1 = "COPROCESSOR$1";
String cpKey2 = " Coprocessor$2 ";
String cpKey3 = " coprocessor$03 ";
String cpValue1 = getLocalPath(jarFile1) + "|" + cpName1 + "|" + Coprocessor.PRIORITY_USER;
String cpValue2 = getLocalPath(jarFile2) + " | " + cpName2 + " | ";
// load from default class loader
String cpValue3 = " | org.apache.hadoop.hbase.coprocessor.SimpleRegionObserver | | k=v ";
// create a table that references the jar
TableDescriptorBuilder tdb = TableDescriptorBuilder.newBuilder(tableName);
tdb.setColumnFamily(ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes("test")).build());
// add 3 coprocessors by setting htd attributes directly.
tdb.setValue(cpKey1, cpValue1);
tdb.setValue(cpKey2, cpValue2);
tdb.setValue(cpKey3, cpValue3);
// add 2 coprocessor by using new htd.setCoprocessor() api
CoprocessorDescriptor coprocessorDescriptor = CoprocessorDescriptorBuilder.newBuilder(cpName5).setJarPath(new Path(getLocalPath(jarFile5)).toString()).setPriority(Coprocessor.PRIORITY_USER).setProperties(Collections.emptyMap()).build();
tdb.setCoprocessor(coprocessorDescriptor);
Map<String, String> kvs = new HashMap<>();
kvs.put("k1", "v1");
kvs.put("k2", "v2");
kvs.put("k3", "v3");
coprocessorDescriptor = CoprocessorDescriptorBuilder.newBuilder(cpName6).setJarPath(new Path(getLocalPath(jarFile6)).toString()).setPriority(Coprocessor.PRIORITY_USER).setProperties(kvs).build();
tdb.setCoprocessor(coprocessorDescriptor);
Admin admin = TEST_UTIL.getAdmin();
if (admin.tableExists(tableName)) {
if (admin.isTableEnabled(tableName)) {
admin.disableTable(tableName);
}
admin.deleteTable(tableName);
}
TableDescriptor tableDescriptor = tdb.build();
admin.createTable(tableDescriptor);
waitForTable(tableDescriptor.getTableName());
// verify that the coprocessor was loaded
boolean found_2 = false, found_1 = false, found_3 = false, found_5 = false, found_6 = false;
boolean found6_k1 = false, found6_k2 = false, found6_k3 = false, found6_k4 = false;
SingleProcessHBaseCluster hbase = TEST_UTIL.getHBaseCluster();
for (HRegion region : hbase.getRegionServer(0).getOnlineRegionsLocalContext()) {
if (region.getRegionInfo().getRegionNameAsString().startsWith(tableName.getNameAsString())) {
found_1 = found_1 || (region.getCoprocessorHost().findCoprocessor(cpName1) != null);
found_2 = found_2 || (region.getCoprocessorHost().findCoprocessor(cpName2) != null);
found_3 = found_3 || (region.getCoprocessorHost().findCoprocessor("SimpleRegionObserver") != null);
found_5 = found_5 || (region.getCoprocessorHost().findCoprocessor(cpName5) != null);
CoprocessorEnvironment env = region.getCoprocessorHost().findCoprocessorEnvironment(cpName6);
if (env != null) {
found_6 = true;
Configuration conf = env.getConfiguration();
found6_k1 = conf.get("k1") != null;
found6_k2 = conf.get("k2") != null;
found6_k3 = conf.get("k3") != null;
}
}
}
assertTrue("Class " + cpName1 + " was missing on a region", found_1);
assertTrue("Class " + cpName2 + " was missing on a region", found_2);
assertTrue("Class SimpleRegionObserver was missing on a region", found_3);
assertTrue("Class " + cpName5 + " was missing on a region", found_5);
assertTrue("Class " + cpName6 + " was missing on a region", found_6);
assertTrue("Configuration key 'k1' was missing on a region", found6_k1);
assertTrue("Configuration key 'k2' was missing on a region", found6_k2);
assertTrue("Configuration key 'k3' was missing on a region", found6_k3);
assertFalse("Configuration key 'k4' wasn't configured", found6_k4);
}
use of org.apache.hadoop.hbase.client.CoprocessorDescriptor in project hbase by apache.
the class CoprocessorValidatorTest method validateTable.
private List<CoprocessorViolation> validateTable(String jarFile, String className) throws IOException {
Pattern pattern = Pattern.compile(".*");
Admin admin = mock(Admin.class);
TableDescriptor tableDescriptor = mock(TableDescriptor.class);
List<TableDescriptor> tableDescriptors = Lists.newArrayList(tableDescriptor);
doReturn(tableDescriptors).when(admin).listTableDescriptors(pattern);
CoprocessorDescriptor coprocessorDescriptor = mock(CoprocessorDescriptor.class);
List<CoprocessorDescriptor> coprocessorDescriptors = Lists.newArrayList(coprocessorDescriptor);
doReturn(coprocessorDescriptors).when(tableDescriptor).getCoprocessorDescriptors();
doReturn(getFullClassName(className)).when(coprocessorDescriptor).getClassName();
doReturn(Optional.ofNullable(jarFile)).when(coprocessorDescriptor).getJarPath();
List<CoprocessorViolation> violations = new ArrayList<>();
validator.validateTables(getClassLoader(), admin, pattern, violations);
return violations;
}
Aggregations