You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hbase.apache.org by ap...@apache.org on 2016/10/12 00:52:53 UTC

[2/4] hbase git commit: HBASE-16663 JMX ConnectorServer stopped when unauthorized user try to stop HM/RS/cluster

HBASE-16663 JMX ConnectorServer stopped when unauthorized user try to stop HM/RS/cluster

Signed-off-by: Andrew Purtell <ap...@apache.org>

Conflicts:

	hbase-server/src/main/java/org/apache/hadoop/hbase/master/MasterCoprocessorHost.java

Amending-Author: Andrew Purtell <ap...@apache.org>


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

Branch: refs/heads/0.98
Commit: 95a70c9249c7525c25b02e2ed5b10b0e345d1e51
Parents: 0cd6fce
Author: Pankaj Kumar <pa...@huawei.com>
Authored: Sat Oct 8 12:29:06 2016 +0800
Committer: Andrew Purtell <ap...@apache.org>
Committed: Tue Oct 11 16:28:35 2016 -0700

----------------------------------------------------------------------
 .../hbase/master/MasterCoprocessorHost.java     |  56 +++++-
 .../RegionServerCoprocessorHost.java            |  51 ++++-
 .../hadoop/hbase/TestJMXConnectorServer.java    | 201 +++++++++++++++++++
 3 files changed, 305 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/hbase/blob/95a70c92/hbase-server/src/main/java/org/apache/hadoop/hbase/master/MasterCoprocessorHost.java
----------------------------------------------------------------------
diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/MasterCoprocessorHost.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/MasterCoprocessorHost.java
index f585bb7..1fd2c0b 100644
--- a/hbase-server/src/main/java/org/apache/hadoop/hbase/master/MasterCoprocessorHost.java
+++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/master/MasterCoprocessorHost.java
@@ -738,7 +738,9 @@ public class MasterCoprocessorHost
   }
 
   public void preShutdown() throws IOException {
-    execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
+    // While stopping the cluster all coprocessors method should be executed first then the
+    // coprocessor should be cleaned up.
+    execShutdown(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
       @Override
       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
           throws IOException {
@@ -753,7 +755,9 @@ public class MasterCoprocessorHost
   }
 
   public void preStopMaster() throws IOException {
-    execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
+    // While stopping master all coprocessors method should be executed first then the coprocessor
+    // environment should be cleaned up.
+    execShutdown(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
       @Override
       public void call(MasterObserver oserver, ObserverContext<MasterCoprocessorEnvironment> ctx)
           throws IOException {
@@ -948,4 +952,52 @@ public class MasterCoprocessorHost
     }
     return bypass;
   }
+
+  /**
+   * Master coprocessor classes can be configured in any order, based on that priority is set and
+   * chained in a sorted order. For preStopMaster()/preShutdown(), coprocessor methods are invoked
+   * in call() and environment is shutdown in postEnvCall(). <br>
+   * Need to execute all coprocessor methods first then postEnvCall(), otherwise some coprocessors
+   * may remain shutdown if any exception occurs during next coprocessor execution which prevent
+   * Master stop or cluster shutdown. (Refer:
+   * <a href="https://issues.apache.org/jira/browse/HBASE-16663">HBASE-16663</a>
+   * @param ctx CoprocessorOperation
+   * @return true if bypaas coprocessor execution, false if not.
+   * @throws IOException
+   */
+  private boolean execShutdown(final CoprocessorOperation ctx) throws IOException {
+    if (ctx == null) return false;
+    boolean bypass = false;
+    List<MasterEnvironment> envs = coprocessors.get();
+    int envsSize = envs.size();
+    // Iterate the coprocessors and execute CoprocessorOperation's call()
+    for (int i = 0; i < envsSize; i++) {
+      MasterEnvironment env = envs.get(i);
+      if (env.getInstance() instanceof MasterObserver) {
+        ctx.prepare(env);
+        Thread currentThread = Thread.currentThread();
+        ClassLoader cl = currentThread.getContextClassLoader();
+        try {
+          currentThread.setContextClassLoader(env.getClassLoader());
+          ctx.call((MasterObserver) env.getInstance(), ctx);
+        } catch (Throwable e) {
+          handleCoprocessorThrowable(env, e);
+        } finally {
+          currentThread.setContextClassLoader(cl);
+        }
+        bypass |= ctx.shouldBypass();
+        if (ctx.shouldComplete()) {
+          break;
+        }
+      }
+    }
+
+    // Iterate the coprocessors and execute CoprocessorOperation's postEnvCall()
+    for (int i = 0; i < envsSize; i++) {
+      MasterEnvironment env = envs.get(i);
+      ctx.postEnvCall(env);
+    }
+    return bypass;
+  }
+
 }

http://git-wip-us.apache.org/repos/asf/hbase/blob/95a70c92/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/RegionServerCoprocessorHost.java
----------------------------------------------------------------------
diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/RegionServerCoprocessorHost.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/RegionServerCoprocessorHost.java
index dfe9818..cebae51 100644
--- a/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/RegionServerCoprocessorHost.java
+++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/RegionServerCoprocessorHost.java
@@ -77,7 +77,9 @@ public class RegionServerCoprocessorHost extends
   }
 
   public void preStop(String message) throws IOException {
-    execOperation(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
+    // While stopping the region server all coprocessors method should be executed first then the
+    // coprocessor should be cleaned up.
+    execShutdown(coprocessors.isEmpty() ? null : new CoprocessorOperation() {
       @Override
       public void call(RegionServerObserver oserver,
           ObserverContext<RegionServerCoprocessorEnvironment> ctx) throws IOException {
@@ -290,6 +292,53 @@ public class RegionServerCoprocessorHost extends
   }
 
   /**
+   * RegionServer coprocessor classes can be configured in any order, based on that priority is set
+   * and chained in a sorted order. For preStop(), coprocessor methods are invoked in call() and
+   * environment is shutdown in postEnvCall(). <br>
+   * Need to execute all coprocessor methods first then postEnvCall(), otherwise some coprocessors
+   * may remain shutdown if any exception occurs during next coprocessor execution which prevent
+   * RegionServer stop. (Refer:
+   * <a href="https://issues.apache.org/jira/browse/HBASE-16663">HBASE-16663</a>
+   * @param ctx CoprocessorOperation
+   * @return true if bypaas coprocessor execution, false if not.
+   * @throws IOException
+   */
+  private boolean execShutdown(final CoprocessorOperation ctx) throws IOException {
+    if (ctx == null) return false;
+    boolean bypass = false;
+    List<RegionServerEnvironment> envs = coprocessors.get();
+    int envsSize = envs.size();
+    // Iterate the coprocessors and execute CoprocessorOperation's call()
+    for (int i = 0; i < envsSize; i++) {
+      RegionServerEnvironment env = envs.get(i);
+      if (env.getInstance() instanceof RegionServerObserver) {
+        ctx.prepare(env);
+        Thread currentThread = Thread.currentThread();
+        ClassLoader cl = currentThread.getContextClassLoader();
+        try {
+          currentThread.setContextClassLoader(env.getClassLoader());
+          ctx.call((RegionServerObserver) env.getInstance(), ctx);
+        } catch (Throwable e) {
+          handleCoprocessorThrowable(env, e);
+        } finally {
+          currentThread.setContextClassLoader(cl);
+        }
+        bypass |= ctx.shouldBypass();
+        if (ctx.shouldComplete()) {
+          break;
+        }
+      }
+    }
+
+    // Iterate the coprocessors and execute CoprocessorOperation's postEnvCall()
+    for (int i = 0; i < envsSize; i++) {
+      RegionServerEnvironment env = envs.get(i);
+      ctx.postEnvCall(env);
+    }
+    return bypass;
+  }
+
+  /**
    * Coprocessor environment extension providing access to region server
    * related services.
    */

http://git-wip-us.apache.org/repos/asf/hbase/blob/95a70c92/hbase-server/src/test/java/org/apache/hadoop/hbase/TestJMXConnectorServer.java
----------------------------------------------------------------------
diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/TestJMXConnectorServer.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestJMXConnectorServer.java
new file mode 100644
index 0000000..4cc4ead
--- /dev/null
+++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/TestJMXConnectorServer.java
@@ -0,0 +1,201 @@
+/**
+ *
+ * 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;
+
+import java.io.IOException;
+
+import javax.management.remote.JMXConnector;
+import javax.management.remote.JMXConnectorFactory;
+import javax.naming.ServiceUnavailableException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hbase.client.HBaseAdmin;
+import org.apache.hadoop.hbase.coprocessor.CoprocessorHost;
+import org.apache.hadoop.hbase.coprocessor.MasterCoprocessorEnvironment;
+import org.apache.hadoop.hbase.coprocessor.ObserverContext;
+import org.apache.hadoop.hbase.coprocessor.RegionServerCoprocessorEnvironment;
+import org.apache.hadoop.hbase.security.AccessDeniedException;
+import org.apache.hadoop.hbase.security.access.AccessController;
+import org.apache.hadoop.hbase.testclassification.MediumTests;
+import org.apache.hadoop.hbase.testclassification.MiscTests;
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+
+/**
+ * Test case for JMX Connector Server.
+ */
+@Category(MediumTests.class)
+public class TestJMXConnectorServer {
+  private static final Log LOG = LogFactory.getLog(TestJMXConnectorServer.class);
+  private static HBaseTestingUtility UTIL = new HBaseTestingUtility();
+
+  private static Configuration conf = null;
+  // RMI registry port
+  private static int rmiRegistryPort = 61120;
+  // Switch for customized Accesscontroller to throw ACD exception while executing test case
+  static boolean hasAccess;
+
+  @Before
+  public void setUp() throws Exception {
+    UTIL = new HBaseTestingUtility();
+    conf = UTIL.getConfiguration();
+  }
+
+  @After
+  public void tearDown() throws Exception {
+    // Set to true while stopping cluster
+    hasAccess = true;
+    UTIL.shutdownMiniCluster();
+  }
+
+  /**
+   * This tests to validate the HMaster's ConnectorServer after unauthorised stopMaster call.
+   */
+  @Test(timeout = 180000)
+  public void testHMConnectorServerWhenStopMaster() throws Exception {
+    conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
+      JMXListener.class.getName() + "," + MyAccessController.class.getName());
+    conf.setInt("master.rmi.registry.port", rmiRegistryPort);
+    UTIL.startMiniCluster();
+
+    // try to stop master
+    boolean accessDenied = false;
+    try {
+      hasAccess = false;
+      LOG.info("Stopping HMaster...");
+      UTIL.getHBaseAdmin().stopMaster();
+    } catch (AccessDeniedException e) {
+      LOG.info("Exception occured while stopping HMaster. ", e);
+      accessDenied = true;
+    }
+    Assert.assertTrue(accessDenied);
+
+    // Check whether HMaster JMX Connector server can be connected
+    JMXConnector connector = null;
+    try {
+      connector = JMXConnectorFactory
+          .connect(JMXListener.buildJMXServiceURL(rmiRegistryPort, rmiRegistryPort));
+    } catch (IOException e) {
+      if (e.getCause() instanceof ServiceUnavailableException) {
+        Assert.fail("Can't connect to HMaster ConnectorServer.");
+      }
+    }
+    Assert.assertNotNull("JMXConnector should not be null.", connector);
+    connector.close();
+  }
+
+  /**
+   * This tests to validate the RegionServer's ConnectorServer after unauthorised stopRegionServer
+   * call.
+   */
+  @Test(timeout = 180000)
+  public void testRSConnectorServerWhenStopRegionServer() throws Exception {
+    conf.set(CoprocessorHost.REGIONSERVER_COPROCESSOR_CONF_KEY,
+      JMXListener.class.getName() + "," + MyAccessController.class.getName());
+    conf.setInt("regionserver.rmi.registry.port", rmiRegistryPort);
+    UTIL.startMiniCluster();
+
+    hasAccess = false;
+    ServerName serverName = UTIL.getHBaseCluster().getRegionServer(0).getServerName();
+    LOG.info("Stopping Region Server...");
+    UTIL.getHBaseAdmin().stopRegionServer(serverName.getHostname() + ":" + serverName.getPort());
+
+    // Check whether Region Sever JMX Connector server can be connected
+    JMXConnector connector = null;
+    try {
+      connector = JMXConnectorFactory
+          .connect(JMXListener.buildJMXServiceURL(rmiRegistryPort, rmiRegistryPort));
+    } catch (IOException e) {
+      if (e.getCause() instanceof ServiceUnavailableException) {
+        Assert.fail("Can't connect to Region Server ConnectorServer.");
+      }
+    }
+    Assert.assertNotNull("JMXConnector should not be null.", connector);
+    connector.close();
+  }
+
+  /**
+   * This tests to validate the HMaster's ConnectorServer after unauthorised shutdown call.
+   */
+  @Test(timeout = 180000)
+  public void testHMConnectorServerWhenShutdownCluster() throws Exception {
+    conf.set(CoprocessorHost.MASTER_COPROCESSOR_CONF_KEY,
+      JMXListener.class.getName() + "," + MyAccessController.class.getName());
+    conf.setInt("master.rmi.registry.port", rmiRegistryPort);
+
+    UTIL.startMiniCluster();
+
+    boolean accessDenied = false;
+    try {
+      hasAccess = false;
+      LOG.info("Stopping HMaster...");
+      UTIL.getHBaseAdmin().shutdown();
+    } catch (AccessDeniedException e) {
+      LOG.error("Exception occured while stopping HMaster. ", e);
+      accessDenied = true;
+    }
+    Assert.assertTrue(accessDenied);
+
+    // Check whether HMaster JMX Connector server can be connected
+    JMXConnector connector = null;
+    try {
+      connector = JMXConnectorFactory
+          .connect(JMXListener.buildJMXServiceURL(rmiRegistryPort, rmiRegistryPort));
+    } catch (IOException e) {
+      if (e.getCause() instanceof ServiceUnavailableException) {
+        Assert.fail("Can't connect to HMaster ConnectorServer.");
+      }
+    }
+    Assert.assertNotNull("JMXConnector should not be null.", connector);
+    connector.close();
+  }
+
+  /*
+   * Customized class for test case execution which will throw ACD exception while executing
+   * stopMaster/preStopRegionServer/preShutdown explicitly.
+   */
+  public static class MyAccessController extends AccessController {
+    @Override
+    public void preStopMaster(ObserverContext<MasterCoprocessorEnvironment> c) throws IOException {
+      if (!hasAccess) {
+        throw new AccessDeniedException("Insufficient permissions to stop master");
+      }
+    }
+
+    @Override
+    public void preStopRegionServer(ObserverContext<RegionServerCoprocessorEnvironment> ctx)
+        throws IOException {
+      if (!hasAccess) {
+        throw new AccessDeniedException("Insufficient permissions to stop region server.");
+      }
+    }
+
+    @Override
+    public void preShutdown(ObserverContext<MasterCoprocessorEnvironment> c) throws IOException {
+      if (!hasAccess) {
+        throw new AccessDeniedException("Insufficient permissions to shut down cluster.");
+      }
+    }
+  }
+}
\ No newline at end of file