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 int overridePriority = Integer.MAX_VALUE - 1; 071 072 final String coprocessor_v3 = 073 SimpleRegionObserverV3.class.getName() + "|" + overridePriority; 074 075 // Try and load a coprocessor three times 076 conf.setStrings(key, coprocessor, coprocessor, coprocessor, 077 SimpleRegionObserverV2.class.getName(), coprocessor_v3); 078 host.loadSystemCoprocessors(conf, key); 079 080 // Three coprocessors(SimpleRegionObserver, SimpleRegionObserverV2, 081 // SimpleRegionObserverV3) loaded 082 Assert.assertEquals(3, host.coprocEnvironments.size()); 083 084 // Check the priority value 085 CoprocessorEnvironment<?> simpleEnv = host.findCoprocessorEnvironment( 086 SimpleRegionObserver.class.getName()); 087 CoprocessorEnvironment<?> simpleEnv_v2 = host.findCoprocessorEnvironment( 088 SimpleRegionObserverV2.class.getName()); 089 CoprocessorEnvironment<?> simpleEnv_v3 = host.findCoprocessorEnvironment( 090 SimpleRegionObserverV3.class.getName()); 091 092 assertNotNull(simpleEnv); 093 assertNotNull(simpleEnv_v2); 094 assertNotNull(simpleEnv_v3); 095 assertEquals(Coprocessor.PRIORITY_SYSTEM, simpleEnv.getPriority()); 096 assertEquals(Coprocessor.PRIORITY_SYSTEM + 1, simpleEnv_v2.getPriority()); 097 assertEquals(overridePriority, simpleEnv_v3.getPriority()); 098 } 099 100 public static class SimpleRegionObserverV2 extends SimpleRegionObserver { } 101 102 public static class SimpleRegionObserverV3 extends SimpleRegionObserver { 103 104 } 105 106 private static class CoprocessorHostForTest<E extends Coprocessor> extends 107 CoprocessorHost<E, CoprocessorEnvironment<E>> { 108 final Configuration cpHostConf; 109 110 public CoprocessorHostForTest(Configuration conf) { 111 super(new TestAbortable()); 112 cpHostConf = conf; 113 } 114 115 @Override 116 public E checkAndGetInstance(Class<?> implClass) 117 throws InstantiationException, IllegalAccessException { 118 try { 119 return (E) implClass.getDeclaredConstructor().newInstance(); 120 } catch (InvocationTargetException | NoSuchMethodException e) { 121 throw (InstantiationException) new InstantiationException().initCause(e); 122 } 123 } 124 125 @Override 126 public CoprocessorEnvironment<E> createEnvironment(final E instance, final int priority, 127 int sequence, Configuration conf) { 128 return new BaseEnvironment<>(instance, priority, 0, cpHostConf); 129 } 130 } 131}