Repository: hbase
Updated Branches:
  refs/heads/branch-2 a237d97f7 -> ba9a49b8a


HBASE-21659 Avoid to load duplicate coprocessors in system config and table 
descriptor


Project: http://git-wip-us.apache.org/repos/asf/hbase/repo
Commit: http://git-wip-us.apache.org/repos/asf/hbase/commit/ba9a49b8
Tree: http://git-wip-us.apache.org/repos/asf/hbase/tree/ba9a49b8
Diff: http://git-wip-us.apache.org/repos/asf/hbase/diff/ba9a49b8

Branch: refs/heads/branch-2
Commit: ba9a49b8a5314ba4f91fa5f043c2f587d397b922
Parents: a237d97
Author: Guanghao Zhang <zg...@apache.org>
Authored: Sat Dec 29 11:51:32 2018 +0800
Committer: Guanghao Zhang <zg...@apache.org>
Committed: Wed Jan 2 11:45:21 2019 +0800

----------------------------------------------------------------------
 .../hbase/coprocessor/CoprocessorHost.java      | 11 +++
 .../coprocessor/TestRegionCoprocessorHost.java  | 77 ++++++++++++++++++++
 2 files changed, 88 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hbase/blob/ba9a49b8/hbase-server/src/main/java/org/apache/hadoop/hbase/coprocessor/CoprocessorHost.java
----------------------------------------------------------------------
diff --git 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/coprocessor/CoprocessorHost.java
 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/coprocessor/CoprocessorHost.java
index 4c56056..4243d2f 100644
--- 
a/hbase-server/src/main/java/org/apache/hadoop/hbase/coprocessor/CoprocessorHost.java
+++ 
b/hbase-server/src/main/java/org/apache/hadoop/hbase/coprocessor/CoprocessorHost.java
@@ -74,6 +74,9 @@ public abstract class CoprocessorHost<C extends Coprocessor, 
E extends Coprocess
   public static final String USER_COPROCESSORS_ENABLED_CONF_KEY =
     "hbase.coprocessor.user.enabled";
   public static final boolean DEFAULT_USER_COPROCESSORS_ENABLED = true;
+  public static final String SKIP_LOAD_DUPLICATE_TABLE_COPROCESSOR =
+      "hbase.skip.load.duplicate.table.coprocessor";
+  public static final boolean DEFAULT_SKIP_LOAD_DUPLICATE_TABLE_COPROCESSOR = 
false;
 
   private static final Logger LOG = 
LoggerFactory.getLogger(CoprocessorHost.class);
   protected Abortable abortable;
@@ -200,6 +203,14 @@ public abstract class CoprocessorHost<C extends 
Coprocessor, E extends Coprocess
     LOG.debug("Loading coprocessor class " + className + " with path " +
         path + " and priority " + priority);
 
+    boolean skipLoadDuplicateCoprocessor = 
conf.getBoolean(SKIP_LOAD_DUPLICATE_TABLE_COPROCESSOR,
+      DEFAULT_SKIP_LOAD_DUPLICATE_TABLE_COPROCESSOR);
+    if (skipLoadDuplicateCoprocessor && findCoprocessor(className) != null) {
+      // If already loaded will just continue
+      LOG.warn("Attempted duplicate loading of {}; skipped", className);
+      return null;
+    }
+
     ClassLoader cl = null;
     if (path == null) {
       try {

http://git-wip-us.apache.org/repos/asf/hbase/blob/ba9a49b8/hbase-server/src/test/java/org/apache/hadoop/hbase/coprocessor/TestRegionCoprocessorHost.java
----------------------------------------------------------------------
diff --git 
a/hbase-server/src/test/java/org/apache/hadoop/hbase/coprocessor/TestRegionCoprocessorHost.java
 
b/hbase-server/src/test/java/org/apache/hadoop/hbase/coprocessor/TestRegionCoprocessorHost.java
new file mode 100644
index 0000000..ee6e216
--- /dev/null
+++ 
b/hbase-server/src/test/java/org/apache/hadoop/hbase/coprocessor/TestRegionCoprocessorHost.java
@@ -0,0 +1,77 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.hadoop.hbase.coprocessor;
+
+import static 
org.apache.hadoop.hbase.coprocessor.CoprocessorHost.COPROCESSORS_ENABLED_CONF_KEY;
+import static 
org.apache.hadoop.hbase.coprocessor.CoprocessorHost.REGION_COPROCESSOR_CONF_KEY;
+import static 
org.apache.hadoop.hbase.coprocessor.CoprocessorHost.SKIP_LOAD_DUPLICATE_TABLE_COPROCESSOR;
+import static 
org.apache.hadoop.hbase.coprocessor.CoprocessorHost.USER_COPROCESSORS_ENABLED_CONF_KEY;
+import static org.junit.Assert.assertEquals;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.HBaseClassTestRule;
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.TableName;
+import org.apache.hadoop.hbase.client.RegionInfo;
+import org.apache.hadoop.hbase.client.RegionInfoBuilder;
+import org.apache.hadoop.hbase.client.TableDescriptor;
+import org.apache.hadoop.hbase.client.TableDescriptorBuilder;
+import org.apache.hadoop.hbase.regionserver.HRegion;
+import org.apache.hadoop.hbase.regionserver.RegionCoprocessorHost;
+import org.apache.hadoop.hbase.regionserver.RegionServerServices;
+import org.apache.hadoop.hbase.testclassification.SmallTests;
+import org.junit.ClassRule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+@Category({SmallTests.class})
+public class TestRegionCoprocessorHost {
+
+  @ClassRule
+  public static final HBaseClassTestRule CLASS_RULE =
+      HBaseClassTestRule.forClass(TestRegionCoprocessorHost.class);
+
+  @Test
+  public void testLoadDuplicateCoprocessor() throws Exception {
+    Configuration conf = HBaseConfiguration.create();
+    conf.setBoolean(COPROCESSORS_ENABLED_CONF_KEY, true);
+    conf.setBoolean(USER_COPROCESSORS_ENABLED_CONF_KEY, true);
+    conf.setBoolean(SKIP_LOAD_DUPLICATE_TABLE_COPROCESSOR, true);
+    conf.set(REGION_COPROCESSOR_CONF_KEY, 
SimpleRegionObserver.class.getName());
+    TableName tableName = TableName.valueOf("testDoubleLoadingCoprocessor");
+    RegionInfo regionInfo = RegionInfoBuilder.newBuilder(tableName).build();
+    // config a same coprocessor with system coprocessor
+    TableDescriptor tableDesc = TableDescriptorBuilder.newBuilder(tableName)
+        .setCoprocessor(SimpleRegionObserver.class.getName()).build();
+    HRegion region = mock(HRegion.class);
+    when(region.getRegionInfo()).thenReturn(regionInfo);
+    when(region.getTableDescriptor()).thenReturn(tableDesc);
+    RegionServerServices rsServices = mock(RegionServerServices.class);
+    RegionCoprocessorHost host = new RegionCoprocessorHost(region, rsServices, 
conf);
+    // Only one coprocessor SimpleRegionObserver loaded
+    assertEquals(1, host.coprocEnvironments.size());
+
+    // Allow to load duplicate coprocessor
+    conf.setBoolean(SKIP_LOAD_DUPLICATE_TABLE_COPROCESSOR, false);
+    host = new RegionCoprocessorHost(region, rsServices, conf);
+    // Two duplicate coprocessors loaded
+    assertEquals(2, host.coprocEnvironments.size());
+  }
+}
\ No newline at end of file

Reply via email to