View Javadoc

1   /**
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *     http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.hadoop.hbase.coprocessor;
19  
20  import static org.apache.hadoop.hbase.coprocessor.CoprocessorHost.COPROCESSORS_ENABLED_CONF_KEY;
21  import static org.apache.hadoop.hbase.coprocessor.CoprocessorHost.REGION_COPROCESSOR_CONF_KEY;
22  import static org.apache.hadoop.hbase.coprocessor.CoprocessorHost.SKIP_LOAD_DUPLICATE_TABLE_COPROCESSOR;
23  import static org.apache.hadoop.hbase.coprocessor.CoprocessorHost.USER_COPROCESSORS_ENABLED_CONF_KEY;
24  import static org.junit.Assert.assertEquals;
25  
26  import java.io.IOException;
27  import java.util.Arrays;
28  import java.util.List;
29  import java.util.concurrent.atomic.AtomicInteger;
30  
31  import org.apache.hadoop.conf.Configuration;
32  import org.apache.hadoop.hbase.CoprocessorEnvironment;
33  import org.apache.hadoop.hbase.HBaseTestingUtility;
34  import org.apache.hadoop.hbase.HColumnDescriptor;
35  import org.apache.hadoop.hbase.HTableDescriptor;
36  import org.apache.hadoop.hbase.TableName;
37  import org.apache.hadoop.hbase.testclassification.MediumTests;
38  import org.junit.After;
39  import org.junit.Before;
40  import org.junit.Test;
41  import org.junit.experimental.categories.Category;
42  import org.junit.runner.RunWith;
43  import org.junit.runners.Parameterized;
44  import org.junit.runners.Parameterized.Parameters;
45  
46  @Category({MediumTests.class})
47  @RunWith(Parameterized.class)
48  public class TestRegionCoprocessorHost {
49  
50    private static final HBaseTestingUtility TEST_UTIL = new HBaseTestingUtility();
51    private final boolean skip;
52  
53    @Parameters
54    public static List<Object> params() {
55      return Arrays.asList(new Object[] { new Boolean(true), new Boolean(false) });
56    }
57  
58    public TestRegionCoprocessorHost(boolean skip) {
59      this.skip = skip;
60    }
61  
62    @Before
63    public void setUp() throws Exception {
64      Configuration conf = TEST_UTIL.getConfiguration();
65      conf.setBoolean(COPROCESSORS_ENABLED_CONF_KEY, true);
66      conf.setBoolean(USER_COPROCESSORS_ENABLED_CONF_KEY, true);
67      conf.setBoolean(SKIP_LOAD_DUPLICATE_TABLE_COPROCESSOR, skip);
68      conf.set(REGION_COPROCESSOR_CONF_KEY, SimpleRegionObserver.class.getName());
69      TEST_UTIL.startMiniCluster();
70    }
71  
72    @After
73    public void tearDown() throws Exception {
74      TEST_UTIL.shutdownMiniCluster();
75    }
76  
77    @Test
78    public void testLoadDuplicateCoprocessor() throws Exception {
79      TableName table = TableName.valueOf("testLoadDuplicateCoprocessor");
80      HTableDescriptor tableDesc = new HTableDescriptor(table);
81      tableDesc.addFamily(new HColumnDescriptor("cf"));
82      tableDesc.addCoprocessor(TestRegionObserver.class.getName());
83  
84      // Only one coprocessor SimpleRegionObserver loaded if skipping, two otherwise
85      TEST_UTIL.getHBaseAdmin().createTable(tableDesc);
86      assertEquals(skip ? 1 : 2, TestRegionObserver.getNumInstances());
87    }
88  
89    public static final class TestRegionObserver extends SimpleRegionObserver {
90      public static final AtomicInteger numInstances = new AtomicInteger(0);
91  
92      @Override
93      public void start(CoprocessorEnvironment e) throws IOException {
94        super.start(e);
95        numInstances.incrementAndGet();
96      }
97  
98      public static int getNumInstances() {
99        return numInstances.get();
100     }
101   }
102 }