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.jupiter.api.Assertions.assertEquals; 021import static org.junit.jupiter.api.Assertions.assertFalse; 022import static org.junit.jupiter.api.Assertions.assertTrue; 023 024import java.io.IOException; 025import org.apache.hadoop.hbase.HBaseTestingUtil; 026import org.apache.hadoop.hbase.MockRegionServerServices; 027import org.apache.hadoop.hbase.TableName; 028import org.apache.hadoop.hbase.client.ColumnFamilyDescriptor; 029import org.apache.hadoop.hbase.client.ColumnFamilyDescriptorBuilder; 030import org.apache.hadoop.hbase.client.RegionInfo; 031import org.apache.hadoop.hbase.client.RegionInfoBuilder; 032import org.apache.hadoop.hbase.client.TableDescriptor; 033import org.apache.hadoop.hbase.client.TableDescriptorBuilder; 034import org.apache.hadoop.hbase.regionserver.ChunkCreator; 035import org.apache.hadoop.hbase.regionserver.HRegion; 036import org.apache.hadoop.hbase.regionserver.MemStoreLAB; 037import org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost; 038import org.apache.hadoop.hbase.regionserver.RegionServerServices; 039import org.apache.hadoop.hbase.testclassification.CoprocessorTests; 040import org.apache.hadoop.hbase.testclassification.SmallTests; 041import org.apache.hadoop.hbase.util.Bytes; 042import org.junit.jupiter.api.AfterEach; 043import org.junit.jupiter.api.BeforeEach; 044import org.junit.jupiter.api.Tag; 045import org.junit.jupiter.api.Test; 046import org.junit.jupiter.api.TestInfo; 047 048/** 049 * Test CoreCoprocessor Annotation works giving access to facility not usually available. Test 050 * RegionCoprocessor. 051 */ 052@Tag(CoprocessorTests.TAG) 053@Tag(SmallTests.TAG) 054public class TestCoreRegionCoprocessor { 055 056 private String currentTestName; 057 HBaseTestingUtil HTU = new HBaseTestingUtil(); 058 private HRegion region = null; 059 private RegionServerServices rss; 060 061 @BeforeEach 062 public void before(TestInfo testInfo) throws IOException { 063 currentTestName = testInfo.getTestMethod().get().getName(); 064 String methodName = this.currentTestName; 065 TableName tn = TableName.valueOf(methodName); 066 ColumnFamilyDescriptor cfd = 067 ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(methodName)).build(); 068 TableDescriptor td = TableDescriptorBuilder.newBuilder(tn).setColumnFamily(cfd).build(); 069 RegionInfo ri = RegionInfoBuilder.newBuilder(tn).build(); 070 this.rss = new MockRegionServerServices(HTU.getConfiguration()); 071 ChunkCreator.initialize(MemStoreLAB.CHUNK_SIZE_DEFAULT, false, 0, 0, 0, null, 072 MemStoreLAB.INDEX_CHUNK_SIZE_PERCENTAGE_DEFAULT); 073 HTU.createRegionDir(ri); 074 this.region = HRegion.openHRegion(ri, td, null, HTU.getConfiguration(), this.rss, null); 075 } 076 077 @AfterEach 078 public void after() throws IOException { 079 this.region.close(); 080 HTU.cleanupTestDir(); 081 } 082 083 /** 084 * No annotation with CoreCoprocessor. This should make it so I can NOT get at instance of a 085 * RegionServerServices instance after some gymnastics. 086 */ 087 public static class NotCoreRegionCoprocessor implements RegionCoprocessor { 088 public NotCoreRegionCoprocessor() { 089 } 090 } 091 092 /** 093 * Annotate with CoreCoprocessor. This should make it so I can get at instance of a 094 * RegionServerServices instance after some gymnastics. 095 */ 096 @org.apache.hadoop.hbase.coprocessor.CoreCoprocessor 097 public static class CoreRegionCoprocessor implements RegionCoprocessor { 098 public CoreRegionCoprocessor() { 099 } 100 } 101 102 /** 103 * Assert that when a Coprocessor is annotated with CoreCoprocessor, then it is possible to access 104 * a RegionServerServices instance. Assert the opposite too. Do it to RegionCoprocessors. 105 */ 106 @Test 107 public void testCoreRegionCoprocessor() throws IOException { 108 RegionCoprocessorHost rch = region.getCoprocessorHost(); 109 RegionCoprocessorEnvironment env = 110 rch.load(null, NotCoreRegionCoprocessor.class.getName(), 0, HTU.getConfiguration()); 111 assertFalse(env instanceof HasRegionServerServices); 112 env = rch.load(null, CoreRegionCoprocessor.class.getName(), 1, HTU.getConfiguration()); 113 assertTrue(env instanceof HasRegionServerServices); 114 assertEquals(this.rss, ((HasRegionServerServices) env).getRegionServerServices()); 115 } 116}