001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.coprocessor; 019 020import static org.junit.Assert.assertEquals; 021import static org.junit.Assert.assertFalse; 022import static org.junit.Assert.assertTrue; 023 024import java.io.IOException; 025import org.apache.hadoop.hbase.HBaseClassTestRule; 026import org.apache.hadoop.hbase.HBaseTestingUtil; 027import org.apache.hadoop.hbase.MockRegionServerServices; 028import org.apache.hadoop.hbase.TableName; 029import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; 030import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 031import org.apache.hadoop.hbase.client.RegionInfo; 032import org.apache.hadoop.hbase.client.RegionInfoBuilder; 033import org.apache.hadoop.hbase.client.TableDescriptor; 034import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 035import org.apache.hadoop.hbase.regionserver.ChunkCreator; 036import org.apache.hadoop.hbase.regionserver.HRegion; 037import org.apache.hadoop.hbase.regionserver.MemStoreLAB; 038import org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost; 039import org.apache.hadoop.hbase.regionserver.RegionServerServices; 040import org.apache.hadoop.hbase.testclassification.CoprocessorTests; 041import org.apache.hadoop.hbase.testclassification.SmallTests; 042import org.apache.hadoop.hbase.util.Bytes; 043import org.junit.After; 044import org.junit.Before; 045import org.junit.ClassRule; 046import org.junit.Rule; 047import org.junit.Test; 048import org.junit.experimental.categories.Category; 049import org.junit.rules.TestName; 050 051/** 052 * Test CoreCoprocessor Annotation works giving access to facility not usually available. Test 053 * RegionCoprocessor. 054 */ 055@Category({ CoprocessorTests.class, SmallTests.class }) 056public class TestCoreRegionCoprocessor { 057 058 @ClassRule 059 public static final HBaseClassTestRule CLASS_RULE = 060 HBaseClassTestRule.forClass(TestCoreRegionCoprocessor.class); 061 062 @Rule 063 public TestName name = new TestName(); 064 HBaseTestingUtil HTU = new HBaseTestingUtil(); 065 private HRegion region = null; 066 private RegionServerServices rss; 067 068 @Before 069 public void before() throws IOException { 070 String methodName = this.name.getMethodName(); 071 TableName tn = TableName.valueOf(methodName); 072 ColumnFamilyDescriptor cfd = 073 ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(methodName)).build(); 074 TableDescriptor td = TableDescriptorBuilder.newBuilder(tn).setColumnFamily(cfd).build(); 075 RegionInfo ri = RegionInfoBuilder.newBuilder(tn).build(); 076 this.rss = new MockRegionServerServices(HTU.getConfiguration()); 077 ChunkCreator.initialize(MemStoreLAB.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null, 078 MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_DEFAULT); 079 this.region = HRegion.openHRegion(ri, td, null, HTU.getConfiguration(), this.rss, null); 080 } 081 082 @After 083 public void after() throws IOException { 084 this.region.close(); 085 } 086 087 /** 088 * No annotation with CoreCoprocessor. This should make it so I can NOT get at instance of a 089 * RegionServerServices instance after some gymnastics. 090 */ 091 public static class NotCoreRegionCoprocessor implements RegionCoprocessor { 092 public NotCoreRegionCoprocessor() { 093 } 094 } 095 096 /** 097 * Annotate with CoreCoprocessor. This should make it so I can get at instance of a 098 * RegionServerServices instance after some gymnastics. 099 */ 100 @org.apache.hadoop.hbase.coprocessor.CoreCoprocessor 101 public static class CoreRegionCoprocessor implements RegionCoprocessor { 102 public CoreRegionCoprocessor() { 103 } 104 } 105 106 /** 107 * Assert that when a Coprocessor is annotated with CoreCoprocessor, then it is possible to access 108 * a RegionServerServices instance. Assert the opposite too. Do it to RegionCoprocessors. 109 */ 110 @Test 111 public void testCoreRegionCoprocessor() throws IOException { 112 RegionCoprocessorHost rch = region.getCoprocessorHost(); 113 RegionCoprocessorEnvironment env = 114 rch.load(null, NotCoreRegionCoprocessor.class.getName(), 0, HTU.getConfiguration()); 115 assertFalse(env instanceof HasRegionServerServices); 116 env = rch.load(null, CoreRegionCoprocessor.class.getName(), 1, HTU.getConfiguration()); 117 assertTrue(env instanceof HasRegionServerServices); 118 assertEquals(this.rss, ((HasRegionServerServices) env).getRegionServerServices()); 119 } 120}