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