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 java.io.IOException;
21  import java.util.concurrent.ExecutorService;
22  
23  import org.apache.hadoop.conf.Configuration;
24  import org.apache.hadoop.hbase.Abortable;
25  import org.apache.hadoop.hbase.Coprocessor;
26  import org.apache.hadoop.hbase.CoprocessorEnvironment;
27  import org.apache.hadoop.hbase.HBaseConfiguration;
28  import org.apache.hadoop.hbase.TableName;
29  import org.apache.hadoop.hbase.client.HTableInterface;
30  import org.apache.hadoop.hbase.testclassification.SmallTests;
31  import org.junit.Assert;
32  import static org.junit.Assert.assertEquals;
33  import static org.junit.Assert.assertNotNull;
34  import org.junit.Test;
35  import org.junit.experimental.categories.Category;
36  
37  @Category({SmallTests.class})
38  public class TestCoprocessorHost {
39    /**
40     * An {@link Abortable} implementation for tests.
41     */
42    class TestAbortable implements Abortable {
43      private volatile boolean aborted = false;
44  
45      @Override
46      public void abort(String why, Throwable e) {
47        this.aborted = true;
48        Assert.fail();
49      }
50  
51      @Override
52      public boolean isAborted() {
53        return this.aborted;
54      }
55    }
56    @Test
57    public void testDoubleLoadingAndPriorityValue() {
58      final Configuration conf = HBaseConfiguration.create();
59      CoprocessorHost<CoprocessorEnvironment> host =
60          new CoprocessorHost<CoprocessorEnvironment>(new TestAbortable()) {
61        final Configuration cpHostConf = conf;
62  
63        @Override
64        public CoprocessorEnvironment createEnvironment(Class<?> implClass,
65            final Coprocessor instance, final int priority, int sequence, Configuration conf) {
66          return new CoprocessorEnvironment() {
67            final Coprocessor envInstance = instance;
68  
69            @Override
70            public int getVersion() {
71              return 0;
72            }
73  
74            @Override
75            public String getHBaseVersion() {
76              return "0.0.0";
77            }
78  
79            @Override
80            public Coprocessor getInstance() {
81              return envInstance;
82            }
83  
84            @Override
85            public int getPriority() {
86              return priority;
87            }
88  
89            @Override
90            public int getLoadSequence() {
91              return 0;
92            }
93  
94            @Override
95            public Configuration getConfiguration() {
96              return cpHostConf;
97            }
98  
99            @Override
100           public HTableInterface getTable(TableName tableName) throws IOException {
101             return null;
102           }
103 
104           @Override
105           public HTableInterface getTable(TableName tableName, ExecutorService service)
106           throws IOException {
107             return null;
108           }
109 
110           @Override
111           public ClassLoader getClassLoader() {
112             return null;
113           }
114         };
115       }
116     };
117     final String key = "KEY";
118     final String coprocessor = "org.apache.hadoop.hbase.coprocessor.SimpleRegionObserver";
119     // Try and load a coprocessor three times
120     conf.setStrings(key, coprocessor, coprocessor, coprocessor, SimpleRegionObserverV2.class.getName());
121     host.loadSystemCoprocessors(conf, key);
122     // Two coprocessors(SimpleRegionObserver and SimpleRegionObserverV2) loaded
123     Assert.assertEquals(2, host.coprocessors.size());
124     // Check the priority value
125     CoprocessorEnvironment simpleEnv = host.findCoprocessorEnvironment(SimpleRegionObserver.class.getName());
126     CoprocessorEnvironment simpleEnv_v2 = host.findCoprocessorEnvironment(SimpleRegionObserverV2.class.getName());
127     assertNotNull(simpleEnv);
128     assertNotNull(simpleEnv_v2);
129     assertEquals(Coprocessor.PRIORITY_SYSTEM, simpleEnv.getPriority());
130     assertEquals(Coprocessor.PRIORITY_SYSTEM + 1, simpleEnv_v2.getPriority());
131   }
132   public static class SimpleRegionObserverV2 extends SimpleRegionObserver {
133   }
134 }