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.apache.hadoop.hbase.coprocessor.CoprocessorHost.COPROCESSORS_ENABLED_CONF_KEY;
021import static org.apache.hadoop.hbase.coprocessor.CoprocessorHost.REGION_COPROCESSOR_CONF_KEY;
022import static org.apache.hadoop.hbase.coprocessor.CoprocessorHost.SKIP_LOAD_DUPLICATE_TABLE_COPROCESSOR;
023import static org.apache.hadoop.hbase.coprocessor.CoprocessorHost.USER_COPROCESSORS_ENABLED_CONF_KEY;
024import static org.junit.Assert.assertEquals;
025import static org.mockito.Mockito.mock;
026import static org.mockito.Mockito.when;
027
028import org.apache.hadoop.conf.Configuration;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.HBaseConfiguration;
031import org.apache.hadoop.hbase.TableName;
032import org.apache.hadoop.hbase.client.RegionInfo;
033import org.apache.hadoop.hbase.client.RegionInfoBuilder;
034import org.apache.hadoop.hbase.client.TableDescriptor;
035import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
036import org.apache.hadoop.hbase.regionserver.HRegion;
037import org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost;
038import org.apache.hadoop.hbase.regionserver.RegionServerServices;
039import org.apache.hadoop.hbase.testclassification.SmallTests;
040import org.junit.ClassRule;
041import org.junit.Test;
042import org.junit.experimental.categories.Category;
043
044@Category({SmallTests.class})
045public class TestRegionCoprocessorHost {
046
047  @ClassRule
048  public static final HBaseClassTestRule CLASS_RULE =
049      HBaseClassTestRule.forClass(TestRegionCoprocessorHost.class);
050
051  @Test
052  public void testLoadDuplicateCoprocessor() throws Exception {
053    Configuration conf = HBaseConfiguration.create();
054    conf.setBoolean(COPROCESSORS_ENABLED_CONF_KEY, true);
055    conf.setBoolean(USER_COPROCESSORS_ENABLED_CONF_KEY, true);
056    conf.setBoolean(SKIP_LOAD_DUPLICATE_TABLE_COPROCESSOR, true);
057    conf.set(REGION_COPROCESSOR_CONF_KEY, SimpleRegionObserver.class.getName());
058    TableName tableName = TableName.valueOf("testDoubleLoadingCoprocessor");
059    RegionInfo regionInfo = RegionInfoBuilder.newBuilder(tableName).build();
060    // config a same coprocessor with system coprocessor
061    TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tableName)
062        .setCoprocessor(SimpleRegionObserver.class.getName()).build();
063    HRegion region = mock(HRegion.class);
064    when(region.getRegionInfo()).thenReturn(regionInfo);
065    when(region.getTableDescriptor()).thenReturn(tableDesc);
066    RegionServerServices rsServices = mock(RegionServerServices.class);
067    RegionCoprocessorHost host = new RegionCoprocessorHost(region, rsServices, conf);
068    // Only one coprocessor SimpleRegionObserver loaded
069    assertEquals(1, host.coprocEnvironments.size());
070
071    // Allow to load duplicate coprocessor
072    conf.setBoolean(SKIP_LOAD_DUPLICATE_TABLE_COPROCESSOR, false);
073    host = new RegionCoprocessorHost(region, rsServices, conf);
074    // Two duplicate coprocessors loaded
075    assertEquals(2, host.coprocEnvironments.size());
076  }
077}