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.assertNotNull;
022
023import java.lang.reflect.InvocationTargetException;
024
025import org.apache.hadoop.conf.Configuration;
026import org.apache.hadoop.hbase.Abortable;
027import org.apache.hadoop.hbase.Coprocessor;
028import org.apache.hadoop.hbase.CoprocessorEnvironment;
029import org.apache.hadoop.hbase.HBaseClassTestRule;
030import org.apache.hadoop.hbase.HBaseConfiguration;
031import org.apache.hadoop.hbase.testclassification.SmallTests;
032import org.junit.Assert;
033import org.junit.ClassRule;
034import org.junit.Test;
035import org.junit.experimental.categories.Category;
036
037@Category({SmallTests.class})
038public class TestCoprocessorHost {
039
040  @ClassRule
041  public static final HBaseClassTestRule CLASS_RULE =
042      HBaseClassTestRule.forClass(TestCoprocessorHost.class);
043
044  /**
045   * An {@link Abortable} implementation for tests.
046   */
047  private static class TestAbortable implements Abortable {
048    private volatile boolean aborted = false;
049
050    @Override
051    public void abort(String why, Throwable e) {
052      this.aborted = true;
053      Assert.fail();
054    }
055
056    @Override
057    public boolean isAborted() {
058      return this.aborted;
059    }
060  }
061
062  @Test
063  public void testDoubleLoadingAndPriorityValue() {
064    final Configuration conf = HBaseConfiguration.create();
065    final String key = "KEY";
066    final String coprocessor = "org.apache.hadoop.hbase.coprocessor.SimpleRegionObserver";
067
068    CoprocessorHost<RegionCoprocessor, CoprocessorEnvironment<RegionCoprocessor>> host;
069    host = new CoprocessorHostForTest<>(conf);
070
071    // Try and load a coprocessor three times
072    conf.setStrings(key, coprocessor, coprocessor, coprocessor,
073        SimpleRegionObserverV2.class.getName());
074    host.loadSystemCoprocessors(conf, key);
075
076    // Two coprocessors(SimpleRegionObserver and SimpleRegionObserverV2) loaded
077    Assert.assertEquals(2, host.coprocEnvironments.size());
078
079    // Check the priority value
080    CoprocessorEnvironment<?> simpleEnv = host.findCoprocessorEnvironment(
081        SimpleRegionObserver.class.getName());
082    CoprocessorEnvironment<?> simpleEnv_v2 = host.findCoprocessorEnvironment(
083        SimpleRegionObserverV2.class.getName());
084
085    assertNotNull(simpleEnv);
086    assertNotNull(simpleEnv_v2);
087    assertEquals(Coprocessor.PRIORITY_SYSTEM, simpleEnv.getPriority());
088    assertEquals(Coprocessor.PRIORITY_SYSTEM + 1, simpleEnv_v2.getPriority());
089  }
090
091  public static class SimpleRegionObserverV2 extends SimpleRegionObserver { }
092
093  private static class CoprocessorHostForTest<E extends Coprocessor> extends
094      CoprocessorHost<E, CoprocessorEnvironment<E>> {
095    final Configuration cpHostConf;
096
097    public CoprocessorHostForTest(Configuration conf) {
098      super(new TestAbortable());
099      cpHostConf = conf;
100    }
101
102    @Override
103    public E checkAndGetInstance(Class<?> implClass)
104        throws InstantiationException, IllegalAccessException {
105      try {
106        return (E) implClass.getDeclaredConstructor().newInstance();
107      } catch (InvocationTargetException | NoSuchMethodException e) {
108        throw (InstantiationException) new InstantiationException().initCause(e);
109      }
110    }
111
112    @Override
113    public CoprocessorEnvironment<E> createEnvironment(final E instance, final int priority,
114        int sequence, Configuration conf) {
115      return new BaseEnvironment<>(instance, priority, 0, cpHostConf);
116    }
117  }
118}