@Test public void testRegionCoprocessorHostTableLoadingDisabled() throws Exception { Configuration conf = new Configuration(CONF); conf.setBoolean(CoprocessorHost.COPROCESSORS_ENABLED_CONF_KEY, true); // if defaults change conf.setBoolean(CoprocessorHost.USER_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); assertTrue("System coprocessors should have been loaded", systemCoprocessorLoaded.get()); assertFalse("Table coprocessors should not have been loaded", tableCoprocessorLoaded.get()); }
@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()); }
@Test public void testRegionCoprocessorHostDefaults() 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); systemCoprocessorLoaded.set(false); tableCoprocessorLoaded.set(false); new RegionCoprocessorHost(region, rsServices, conf); assertEquals("System coprocessors loading default was not honored", CoprocessorHost.DEFAULT_COPROCESSORS_ENABLED, systemCoprocessorLoaded.get()); assertEquals("Table coprocessors loading default was not honored", CoprocessorHost.DEFAULT_COPROCESSORS_ENABLED && CoprocessorHost.DEFAULT_USER_COPROCESSORS_ENABLED, tableCoprocessorLoaded.get()); }
private RegionCoprocessorHost getRegionCoprocessorHost() { // Make up an HRegion instance. Use the hbase:meta first region as our RegionInfo. Use // hbase:meta table name for building the TableDescriptor our mock returns when asked schema // down inside RegionCoprocessorHost. Pass in mocked RegionServerServices too. RegionInfo ri = RegionInfoBuilder.FIRST_META_REGIONINFO; HRegion mockedHRegion = Mockito.mock(HRegion.class); Mockito.when(mockedHRegion.getRegionInfo()).thenReturn(ri); TableDescriptor td = TableDescriptorBuilder.newBuilder(ri.getTable()).build(); Mockito.when(mockedHRegion.getTableDescriptor()).thenReturn(td); RegionServerServices mockedServices = Mockito.mock(RegionServerServices.class); Configuration conf = HBaseConfiguration.create(); // Load our test coprocessor defined above. conf.set(REGION_COPROCESSOR_CONF_KEY, TestRegionObserver.class.getName()); return new RegionCoprocessorHost(mockedHRegion, mockedServices, conf); } }
HRegion initHRegion (byte [] tableName, String callingMethod, Configuration conf, byte [] ... families) throws IOException { HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName)); for(byte [] family : families) { htd.addFamily(new HColumnDescriptor(family)); } ChunkCreator.initialize(MemStoreLABImpl.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null); HRegionInfo info = new HRegionInfo(htd.getTableName(), null, null, false); Path path = new Path(DIR + callingMethod); HRegion r = HBaseTestingUtility.createRegionAndWAL(info, path, conf, htd); // 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. RegionCoprocessorHost host = new RegionCoprocessorHost(r, Mockito.mock(RegionServerServices.class), conf); r.setCoprocessorHost(host); return r; }
this.coprocessorHost = new RegionCoprocessorHost(this, rsServices, conf); this.metricsRegionWrapper = new MetricsRegionWrapperImpl(this); this.metricsRegion = new MetricsRegion(this.metricsRegionWrapper);
@Test public void testLoadDuplicateCoprocessor() throws Exception { Configuration conf = HBaseConfiguration.create(); conf.setBoolean(COPROCESSORS_ENABLED_CONF_KEY, true); conf.setBoolean(USER_COPROCESSORS_ENABLED_CONF_KEY, true); conf.setBoolean(SKIP_LOAD_DUPLICATE_TABLE_COPROCESSOR, true); conf.set(REGION_COPROCESSOR_CONF_KEY, SimpleRegionObserver.class.getName()); TableName tableName = TableName.valueOf("testDoubleLoadingCoprocessor"); RegionInfo regionInfo = RegionInfoBuilder.newBuilder(tableName).build(); // config a same coprocessor with system coprocessor TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tableName) .setCoprocessor(SimpleRegionObserver.class.getName()).build(); HRegion region = mock(HRegion.class); when(region.getRegionInfo()).thenReturn(regionInfo); when(region.getTableDescriptor()).thenReturn(tableDesc); RegionServerServices rsServices = mock(RegionServerServices.class); RegionCoprocessorHost host = new RegionCoprocessorHost(region, rsServices, conf); // Only one coprocessor SimpleRegionObserver loaded assertEquals(1, host.coprocEnvironments.size()); // Allow to load duplicate coprocessor conf.setBoolean(SKIP_LOAD_DUPLICATE_TABLE_COPROCESSOR, false); host = new RegionCoprocessorHost(region, rsServices, conf); // Two duplicate coprocessors loaded assertEquals(2, host.coprocEnvironments.size()); } }
HRegion initHRegion(byte[] tableName, String callingMethod, Configuration conf, byte[]... families) throws IOException { HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName)); for (byte[] family : families) { htd.addFamily(new HColumnDescriptor(family)); } ChunkCreator.initialize(MemStoreLABImpl.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null); HRegionInfo info = new HRegionInfo(htd.getTableName(), null, null, false); Path path = new Path(DIR + callingMethod); WAL wal = HBaseTestingUtility.createWal(conf, path, info); HRegion r = HRegion.createHRegion(info, path, conf, htd, wal); // 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. RegionCoprocessorHost host = new RegionCoprocessorHost(r, null, conf); r.setCoprocessorHost(host); return r; }
/** * 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); } }
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; }
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; }
@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()); }
@Test public void testRegionCoprocessorHostDefaults() 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); systemCoprocessorLoaded.set(false); tableCoprocessorLoaded.set(false); new RegionCoprocessorHost(region, rsServices, conf); assertEquals("System coprocessors loading default was not honored", CoprocessorHost.DEFAULT_COPROCESSORS_ENABLED, systemCoprocessorLoaded.get()); assertEquals("Table coprocessors loading default was not honored", CoprocessorHost.DEFAULT_COPROCESSORS_ENABLED && CoprocessorHost.DEFAULT_USER_COPROCESSORS_ENABLED, tableCoprocessorLoaded.get()); }
@Test public void testRegionCoprocessorHostTableLoadingDisabled() throws Exception { Configuration conf = new Configuration(CONF); conf.setBoolean(CoprocessorHost.COPROCESSORS_ENABLED_CONF_KEY, true); // if defaults change conf.setBoolean(CoprocessorHost.USER_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); assertTrue("System coprocessors should have been loaded", systemCoprocessorLoaded.get()); assertFalse("Table coprocessors should not have been loaded", tableCoprocessorLoaded.get()); }
private RegionCoprocessorHost getRegionCoprocessorHost() { // Make up an HRegion instance. Use the hbase:meta first region as our RegionInfo. Use // hbase:meta table name for building the TableDescriptor our mock returns when asked schema // down inside RegionCoprocessorHost. Pass in mocked RegionServerServices too. RegionInfo ri = RegionInfoBuilder.FIRST_META_REGIONINFO; HRegion mockedHRegion = Mockito.mock(HRegion.class); Mockito.when(mockedHRegion.getRegionInfo()).thenReturn(ri); TableDescriptor td = TableDescriptorBuilder.newBuilder(ri.getTable()).build(); Mockito.when(mockedHRegion.getTableDescriptor()).thenReturn(td); RegionServerServices mockedServices = Mockito.mock(RegionServerServices.class); Configuration conf = HBaseConfiguration.create(); // Load our test coprocessor defined above. conf.set(REGION_COPROCESSOR_CONF_KEY, TestRegionObserver.class.getName()); return new RegionCoprocessorHost(mockedHRegion, mockedServices, conf); } }
HRegion initHRegion (byte [] tableName, String callingMethod, Configuration conf, byte [] ... families) throws IOException { HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName)); for(byte [] family : families) { htd.addFamily(new HColumnDescriptor(family)); } ChunkCreator.initialize(MemStoreLABImpl.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null); HRegionInfo info = new HRegionInfo(htd.getTableName(), null, null, false); Path path = new Path(DIR + callingMethod); HRegion r = HBaseTestingUtility.createRegionAndWAL(info, path, conf, htd); // 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. RegionCoprocessorHost host = new RegionCoprocessorHost(r, Mockito.mock(RegionServerServices.class), conf); r.setCoprocessorHost(host); return r; }
HRegion initHRegion(byte[] tableName, String callingMethod, Configuration conf, byte[]... families) throws IOException { HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName)); for (byte[] family : families) { htd.addFamily(new HColumnDescriptor(family)); } ChunkCreator.initialize(MemStoreLABImpl.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null); HRegionInfo info = new HRegionInfo(htd.getTableName(), null, null, false); Path path = new Path(DIR + callingMethod); WAL wal = HBaseTestingUtility.createWal(conf, path, info); HRegion r = HRegion.createHRegion(info, path, conf, htd, wal); // 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. RegionCoprocessorHost host = new RegionCoprocessorHost(r, null, conf); r.setCoprocessorHost(host); return r; }
/** * 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); } }
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; }
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; }