private static <T extends RegionObserver> T find(final TableName tableName, Class<T> clz) throws IOException, InterruptedException { HRegion region = find(tableName); Coprocessor cp = region.getCoprocessorHost().findCoprocessor(clz.getName()); assertTrue("The cp instance should be " + clz.getName() + ", current instance is " + cp.getClass().getName(), clz.isInstance(cp)); return clz.cast(cp); }
RegionScanner checkScannerOpen(final Scan scan) throws IOException { RegionScanner scanner; if (region.getCoprocessorHost() == null) { scanner = region.getScanner(scan); } else { region.getCoprocessorHost().preScannerOpen(scan); scanner = region.getScanner(scan); scanner = region.getCoprocessorHost().postScannerOpen(scan, scanner); } if (scanner == null) { throw new IOException("Failed to open region scanner"); } return scanner; }
void checkScannerClose(final InternalScanner s) throws IOException { if (s == null) { return; } if (region.getCoprocessorHost() == null) { s.close(); return; } region.getCoprocessorHost().preScannerClose(s); try { s.close(); } finally { region.getCoprocessorHost().postScannerClose(s); } }
HRegion reopenRegion(final HRegion closedRegion, Class<?> ... implClasses) throws IOException { //RegionInfo info = new RegionInfo(tableName, null, null, false); HRegion r = HRegion.openHRegion(closedRegion, null); // this following piece is a hack. currently a coprocessorHost // is secretly loaded at OpenRegionHandler. we don't really // start a region server here, so just manually create cphost // and set it to region. Configuration conf = TEST_UTIL.getConfiguration(); RegionCoprocessorHost host = new RegionCoprocessorHost(r, Mockito.mock(RegionServerServices.class), conf); r.setCoprocessorHost(host); for (Class<?> implClass : implClasses) { host.load((Class<? extends RegionCoprocessor>) implClass, Coprocessor.PRIORITY_USER, conf); } // we need to manually call pre- and postOpen here since the // above load() is not the real case for CP loading. A CP is // expected to be loaded by default from 1) configuration; or 2) // HTableDescriptor. If it's loaded after HRegion initialized, // the pre- and postOpen() won't be triggered automatically. // Here we have to call pre and postOpen explicitly. host.preOpen(); host.postOpen(); return r; }
scanner = region.getCoprocessorHost().preScannerOpen(scan); scanner = region.getCoprocessorHost().postScannerOpen(scan, scanner); Boolean bypass = region.getCoprocessorHost().preScannerNext( scanner, results, rows); if (!results.isEmpty()) { region.getCoprocessorHost().postScannerNext(scanner, results, rows, true); moreResults = false; if (region != null && region.getCoprocessorHost() != null) { if (region.getCoprocessorHost().preScannerClose(scanner)) { return builder.build(); // bypass regionServer.leases.cancelLease(scannerName); if (region != null && region.getCoprocessorHost() != null) { region.getCoprocessorHost().postScannerClose(scanner);
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); region.getCoprocessorHost().findCoprocessorEnvironment(cpName6); if (env != null) { found_6 = true;
@Test public void testCoprocessorInterface() throws IOException { TableName tableName = TableName.valueOf(name.getMethodName()); byte [][] families = { fam1, fam2, fam3 }; Configuration hc = initConfig(); HRegion region = initHRegion(tableName, name.getMethodName(), hc, new Class<?>[]{CoprocessorImpl.class}, families); for (int i = 0; i < 3; i++) { HBaseTestCase.addContent(region, fam3); region.flush(true); } region.compact(false); // HBASE-4197 Scan s = new Scan(); RegionScanner scanner = region.getCoprocessorHost().postScannerOpen(s, region.getScanner(s)); assertTrue(scanner instanceof CustomScanner); // this would throw an exception before HBASE-4197 scanner.next(new ArrayList<>()); HBaseTestingUtility.closeRegionAndWAL(region); Coprocessor c = region.getCoprocessorHost().findCoprocessor(CoprocessorImpl.class); assertTrue("Coprocessor not started", ((CoprocessorImpl)c).wasStarted()); assertTrue("Coprocessor not stopped", ((CoprocessorImpl)c).wasStopped()); assertTrue(((CoprocessorImpl)c).wasOpened()); assertTrue(((CoprocessorImpl)c).wasClosed()); assertTrue(((CoprocessorImpl)c).wasFlushed()); assertTrue(((CoprocessorImpl)c).wasCompacted()); }
public void testRegionObserverStacking() throws Exception { byte[] ROW = Bytes.toBytes("testRow"); byte[] TABLE = Bytes.toBytes(this.getClass().getSimpleName()); byte[] A = Bytes.toBytes("A"); byte[][] FAMILIES = new byte[][] { A } ; Configuration conf = TEST_UTIL.getConfiguration(); HRegion region = initHRegion(TABLE, getClass().getName(), conf, FAMILIES); RegionCoprocessorHost h = region.getCoprocessorHost(); h.load(ObserverA.class, Coprocessor.PRIORITY_HIGHEST, conf); h.load(ObserverB.class, Coprocessor.PRIORITY_USER, conf); h.load(ObserverC.class, Coprocessor.PRIORITY_LOWEST, conf); Put put = new Put(ROW); put.addColumn(A, A, A); region.put(put); Coprocessor c = h.findCoprocessor(ObserverA.class.getName()); long idA = ((ObserverA)c).id; c = h.findCoprocessor(ObserverB.class.getName()); long idB = ((ObserverB)c).id; c = h.findCoprocessor(ObserverC.class.getName()); long idC = ((ObserverC)c).id; assertTrue(idA < idB); assertTrue(idB < idC); HBaseTestingUtility.closeRegionAndWAL(region); } }
/** * Rough test that Coprocessor Environment is Read-Only. * Just check a random CP and see that it returns a read-only config. */ @Test public void testReadOnlyConfiguration() throws Exception { Configuration conf = new Configuration(CONF); HRegion region = mock(HRegion.class); when(region.getRegionInfo()).thenReturn(REGIONINFO); when(region.getTableDescriptor()).thenReturn(TABLEDESC); RegionServerServices rsServices = mock(RegionServerServices.class); RegionCoprocessorHost rcp = new RegionCoprocessorHost(region, rsServices, conf); boolean found = false; for (String cpStr: rcp.getCoprocessors()) { CoprocessorEnvironment cpenv = rcp.findCoprocessorEnvironment(cpStr); if (cpenv != null) { found = true; } Configuration c = cpenv.getConfiguration(); thrown.expect(UnsupportedOperationException.class); c.set("one.two.three", "four.five.six"); } assertTrue("Should be at least one CP found", found); } }
@Test public void testRegionCoprocessorHostAllDisabled() throws Exception { Configuration conf = new Configuration(CONF); conf.setBoolean(CoprocessorHost.COPROCESSORS_ENABLED_CONF_KEY, false); HRegion region = mock(HRegion.class); when(region.getRegionInfo()).thenReturn(REGIONINFO); when(region.getTableDescriptor()).thenReturn(TABLEDESC); RegionServerServices rsServices = mock(RegionServerServices.class); systemCoprocessorLoaded.set(false); tableCoprocessorLoaded.set(false); new RegionCoprocessorHost(region, rsServices, conf); assertFalse("System coprocessors should not have been loaded", systemCoprocessorLoaded.get()); assertFalse("Table coprocessors should not have been loaded", tableCoprocessorLoaded.get()); }
Boolean bypass = region.getCoprocessorHost().preScannerNext(s, results, nbRows); if (!results.isEmpty()) { region.getCoprocessorHost().postScannerNext(s, results, nbRows, true);
if (region.getRegionInfo().getRegionNameAsString().startsWith(tableName.getNameAsString())) { CoprocessorEnvironment env; env = region.getCoprocessorHost().findCoprocessorEnvironment(cpName1); if (env != null) { found1 = true; env = region.getCoprocessorHost().findCoprocessorEnvironment(cpName2); if (env != null) { found2 = true;
public String[] getRegionServerCoprocessors() { TreeSet<String> coprocessors = new TreeSet<>(); try { coprocessors.addAll(getWAL(null).getCoprocessorHost().getCoprocessors()); } catch (IOException exception) { LOG.warn("Exception attempting to fetch wal coprocessor information for the common wal; " + "skipping."); LOG.debug("Exception details for failure to fetch wal coprocessor information.", exception); } Collection<HRegion> regions = getOnlineRegionsLocalContext(); for (HRegion region: regions) { coprocessors.addAll(region.getCoprocessorHost().getCoprocessors()); try { coprocessors.addAll(getWAL(region.getRegionInfo()).getCoprocessorHost().getCoprocessors()); } catch (IOException exception) { LOG.warn("Exception attempting to fetch wal coprocessor information for region " + region + "; skipping."); LOG.debug("Exception details for failure to fetch wal coprocessor information.", exception); } } coprocessors.addAll(rsHost.getCoprocessors()); return coprocessors.toArray(new String[coprocessors.size()]); }
boolean preScannerNext(final InternalScanner s, final List<Result> results, final int limit) throws IOException { if (region.getCoprocessorHost() == null) { return false; } else { Boolean bypass = region.getCoprocessorHost().preScannerNext(s, results, limit); return bypass == null ? false : bypass; } }
boolean postScannerNext(final InternalScanner s, final List<Result> results, final int limit, boolean hasMore) throws IOException { if (region.getCoprocessorHost() == null) { return false; } else { return region.getCoprocessorHost().postScannerNext(s, results, limit, hasMore); } } }
scan.setAuthorizations(new Authorizations(authorizations)); env.getRegion().getCoprocessorHost().preScannerOpen(scan); try (InternalScanner scanner = env.getRegion().getScanner(scan)) { final List<Cell> results = new ArrayList<Cell>();
HRegion initHRegion (TableName tableName, String callingMethod, Configuration conf, Class<?> [] implClasses, byte [][] families) throws IOException { HTableDescriptor htd = new HTableDescriptor(tableName); for(byte [] family : families) { htd.addFamily(new HColumnDescriptor(family)); } ChunkCreator.initialize(MemStoreLABImpl.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null); RegionInfo info = RegionInfoBuilder.newBuilder(tableName) .setStartKey(null) .setEndKey(null) .setSplit(false) .build(); Path path = new Path(DIR + callingMethod); HRegion r = HBaseTestingUtility.createRegionAndWAL(info, path, conf, htd); // this following piece is a hack. RegionCoprocessorHost host = new RegionCoprocessorHost(r, Mockito.mock(RegionServerServices.class), conf); r.setCoprocessorHost(host); for (Class<?> implClass : implClasses) { host.load((Class<? extends RegionCoprocessor>) implClass, Coprocessor.PRIORITY_USER, conf); Coprocessor c = host.findCoprocessor(implClass.getName()); assertNotNull(c); } // Here we have to call pre and postOpen explicitly. host.preOpen(); host.postOpen(); return r; }
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); region.getCoprocessorHost().findCoprocessorEnvironment(cpName6); if (env != null) { found_6 = true;
@Test public void testCoprocessorInterface() throws IOException { TableName tableName = TableName.valueOf(name.getMethodName()); byte [][] families = { fam1, fam2, fam3 }; Configuration hc = initConfig(); HRegion region = initHRegion(tableName, name.getMethodName(), hc, new Class<?>[]{CoprocessorImpl.class}, families); for (int i = 0; i < 3; i++) { HBaseTestCase.addContent(region, fam3); region.flush(true); } region.compact(false); // HBASE-4197 Scan s = new Scan(); RegionScanner scanner = region.getCoprocessorHost().postScannerOpen(s, region.getScanner(s)); assertTrue(scanner instanceof CustomScanner); // this would throw an exception before HBASE-4197 scanner.next(new ArrayList<>()); HBaseTestingUtility.closeRegionAndWAL(region); Coprocessor c = region.getCoprocessorHost().findCoprocessor(CoprocessorImpl.class); assertTrue("Coprocessor not started", ((CoprocessorImpl)c).wasStarted()); assertTrue("Coprocessor not stopped", ((CoprocessorImpl)c).wasStopped()); assertTrue(((CoprocessorImpl)c).wasOpened()); assertTrue(((CoprocessorImpl)c).wasClosed()); assertTrue(((CoprocessorImpl)c).wasFlushed()); assertTrue(((CoprocessorImpl)c).wasCompacted()); }
public void testRegionObserverStacking() throws Exception { byte[] ROW = Bytes.toBytes("testRow"); byte[] TABLE = Bytes.toBytes(this.getClass().getSimpleName()); byte[] A = Bytes.toBytes("A"); byte[][] FAMILIES = new byte[][] { A } ; Configuration conf = TEST_UTIL.getConfiguration(); HRegion region = initHRegion(TABLE, getClass().getName(), conf, FAMILIES); RegionCoprocessorHost h = region.getCoprocessorHost(); h.load(ObserverA.class, Coprocessor.PRIORITY_HIGHEST, conf); h.load(ObserverB.class, Coprocessor.PRIORITY_USER, conf); h.load(ObserverC.class, Coprocessor.PRIORITY_LOWEST, conf); Put put = new Put(ROW); put.addColumn(A, A, A); region.put(put); Coprocessor c = h.findCoprocessor(ObserverA.class.getName()); long idA = ((ObserverA)c).id; c = h.findCoprocessor(ObserverB.class.getName()); long idB = ((ObserverB)c).id; c = h.findCoprocessor(ObserverC.class.getName()); long idC = ((ObserverC)c).id; assertTrue(idA < idB); assertTrue(idB < idC); HBaseTestingUtility.closeRegionAndWAL(region); } }