From fcfaee59662dbfb3dd23e6ad52806bce9df14f8a Mon Sep 17 00:00:00 2001 From: Mike Drob Date: Thu, 8 Feb 2018 16:12:46 -0600 Subject: [PATCH] HBASE-19920 Lazy init for ProtobufUtil classloader --- .../apache/hadoop/hbase/protobuf/ProtobufUtil.java | 24 +++---- .../hadoop/hbase/shaded/protobuf/ProtobufUtil.java | 29 ++++++--- .../hadoop/hbase/security/token/TokenUtil.java | 15 ++++- .../hadoop/hbase/security/token/TestTokenUtil.java | 75 ++++++++++++++++++++++ 4 files changed, 120 insertions(+), 23 deletions(-) create mode 100644 hbase-server/src/test/java/org/apache/hadoop/hbase/security/token/TestTokenUtil.java diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/protobuf/ProtobufUtil.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/protobuf/ProtobufUtil.java index 29ff2a2a61..5b583ecb5a 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/protobuf/ProtobufUtil.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/protobuf/ProtobufUtil.java @@ -169,14 +169,16 @@ public final class ProtobufUtil { } /** - * Dynamic class loader to load filter/comparators - */ - private final static ClassLoader CLASS_LOADER; + * Dynamic class loader to load filter/comparators + */ + private final static class ClassLoaderHolder { + private final static ClassLoader CLASS_LOADER; - static { - ClassLoader parent = ProtobufUtil.class.getClassLoader(); - Configuration conf = HBaseConfiguration.create(); - CLASS_LOADER = new DynamicClassLoader(conf, parent); + static { + ClassLoader parent = ProtobufUtil.class.getClassLoader(); + Configuration conf = HBaseConfiguration.create(); + CLASS_LOADER = new DynamicClassLoader(conf, parent); + } } /** @@ -1431,8 +1433,7 @@ public final class ProtobufUtil { String funcName = "parseFrom"; byte [] value = proto.getSerializedComparator().toByteArray(); try { - Class c = - (Class)Class.forName(type, true, CLASS_LOADER); + Class c = Class.forName(type, true, ClassLoaderHolder.CLASS_LOADER); Method parseFrom = c.getMethod(funcName, byte[].class); if (parseFrom == null) { throw new IOException("Unable to locate function: " + funcName + " in type: " + type); @@ -1455,8 +1456,7 @@ public final class ProtobufUtil { final byte [] value = proto.getSerializedFilter().toByteArray(); String funcName = "parseFrom"; try { - Class c = - (Class)Class.forName(type, true, CLASS_LOADER); + Class c = Class.forName(type, true, ClassLoaderHolder.CLASS_LOADER); Method parseFrom = c.getMethod(funcName, byte[].class); if (parseFrom == null) { throw new IOException("Unable to locate function: " + funcName + " in type: " + type); @@ -1542,7 +1542,7 @@ public final class ProtobufUtil { String type = parameter.getName(); try { Class c = - (Class)Class.forName(type, true, CLASS_LOADER); + (Class)Class.forName(type, true, ClassLoaderHolder.CLASS_LOADER); Constructor cn = null; try { cn = c.getDeclaredConstructor(String.class); diff --git a/hbase-client/src/main/java/org/apache/hadoop/hbase/shaded/protobuf/ProtobufUtil.java b/hbase-client/src/main/java/org/apache/hadoop/hbase/shaded/protobuf/ProtobufUtil.java index 5bb3b4ba04..dbc365baa0 100644 --- a/hbase-client/src/main/java/org/apache/hadoop/hbase/shaded/protobuf/ProtobufUtil.java +++ b/hbase-client/src/main/java/org/apache/hadoop/hbase/shaded/protobuf/ProtobufUtil.java @@ -106,6 +106,7 @@ import org.apache.hadoop.hbase.util.VersionInfo; import org.apache.hadoop.ipc.RemoteException; import org.apache.yetus.audience.InterfaceAudience; +import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; import org.apache.hbase.thirdparty.com.google.common.io.ByteStreams; import org.apache.hbase.thirdparty.com.google.gson.JsonArray; import org.apache.hbase.thirdparty.com.google.gson.JsonElement; @@ -245,15 +246,25 @@ public final class ProtobufUtil { EMPTY_RESULT_PB_STALE = builder.build(); } + private static boolean classLoaderLoaded = false; + /** * Dynamic class loader to load filter/comparators */ - private final static ClassLoader CLASS_LOADER; + private final static class ClassLoaderHolder { + private final static ClassLoader CLASS_LOADER; - static { - ClassLoader parent = ProtobufUtil.class.getClassLoader(); - Configuration conf = HBaseConfiguration.create(); - CLASS_LOADER = new DynamicClassLoader(conf, parent); + static { + ClassLoader parent = ProtobufUtil.class.getClassLoader(); + Configuration conf = HBaseConfiguration.create(); + CLASS_LOADER = new DynamicClassLoader(conf, parent); + classLoaderLoaded = true; + } + } + + @VisibleForTesting + public static boolean isClassLoaderLoaded() { + return classLoaderLoaded; } /** @@ -1588,8 +1599,7 @@ public final class ProtobufUtil { String funcName = "parseFrom"; byte [] value = proto.getSerializedComparator().toByteArray(); try { - Class c = - (Class)Class.forName(type, true, CLASS_LOADER); + Class c = Class.forName(type, true, ClassLoaderHolder.CLASS_LOADER); Method parseFrom = c.getMethod(funcName, byte[].class); if (parseFrom == null) { throw new IOException("Unable to locate function: " + funcName + " in type: " + type); @@ -1612,8 +1622,7 @@ public final class ProtobufUtil { final byte [] value = proto.getSerializedFilter().toByteArray(); String funcName = "parseFrom"; try { - Class c = - (Class)Class.forName(type, true, CLASS_LOADER); + Class c = Class.forName(type, true, ClassLoaderHolder.CLASS_LOADER); Method parseFrom = c.getMethod(funcName, byte[].class); if (parseFrom == null) { throw new IOException("Unable to locate function: " + funcName + " in type: " + type); @@ -1699,7 +1708,7 @@ public final class ProtobufUtil { String type = parameter.getName(); try { Class c = - (Class)Class.forName(type, true, CLASS_LOADER); + (Class)Class.forName(type, true, ClassLoaderHolder.CLASS_LOADER); Constructor cn = null; try { cn = c.getDeclaredConstructor(String.class); diff --git a/hbase-server/src/main/java/org/apache/hadoop/hbase/security/token/TokenUtil.java b/hbase-server/src/main/java/org/apache/hadoop/hbase/security/token/TokenUtil.java index 5461760137..b4057ecc3d 100644 --- a/hbase-server/src/main/java/org/apache/hadoop/hbase/security/token/TokenUtil.java +++ b/hbase-server/src/main/java/org/apache/hadoop/hbase/security/token/TokenUtil.java @@ -26,7 +26,6 @@ import com.google.protobuf.ByteString; import com.google.protobuf.ServiceException; import org.apache.hadoop.hbase.zookeeper.ZKWatcher; -import org.apache.yetus.audience.InterfaceAudience; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hbase.HConstants; import org.apache.hadoop.hbase.TableName; @@ -41,10 +40,13 @@ import org.apache.hadoop.io.Text; import org.apache.hadoop.mapred.JobConf; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.security.token.Token; +import org.apache.yetus.audience.InterfaceAudience; import org.apache.zookeeper.KeeperException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.apache.hbase.thirdparty.com.google.common.annotations.VisibleForTesting; + /** * Utility methods for obtaining authentication tokens. */ @@ -53,6 +55,15 @@ public class TokenUtil { // This class is referenced indirectly by User out in common; instances are created by reflection private static final Logger LOG = LoggerFactory.getLogger(TokenUtil.class); + @VisibleForTesting + static boolean shouldInjectFault = false; + + private static void injectFault() throws ServiceException { + if (shouldInjectFault) { + throw new ServiceException("injected"); + } + } + /** * Obtain and return an authentication token for the current user. * @param conn The HBase cluster connection @@ -63,6 +74,8 @@ public class TokenUtil { Connection conn) throws IOException { Table meta = null; try { + injectFault(); + meta = conn.getTable(TableName.META_TABLE_NAME); CoprocessorRpcChannel rpcChannel = meta.coprocessorService(HConstants.EMPTY_START_ROW); AuthenticationProtos.AuthenticationService.BlockingInterface service = diff --git a/hbase-server/src/test/java/org/apache/hadoop/hbase/security/token/TestTokenUtil.java b/hbase-server/src/test/java/org/apache/hadoop/hbase/security/token/TestTokenUtil.java new file mode 100644 index 0000000000..cbecbb2703 --- /dev/null +++ b/hbase-server/src/test/java/org/apache/hadoop/hbase/security/token/TestTokenUtil.java @@ -0,0 +1,75 @@ +/** + * 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.security.token; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.fail; + +import java.lang.reflect.InvocationTargetException; +import java.net.URL; +import java.net.URLClassLoader; + +import org.apache.hadoop.hbase.HBaseClassTestRule; +import org.apache.hadoop.hbase.client.Connection; +import org.apache.hadoop.hbase.testclassification.SmallTests; +import org.junit.ClassRule; +import org.junit.Test; +import org.junit.experimental.categories.Category; + +import org.apache.hadoop.hbase.shaded.protobuf.ProtobufUtil; + +@Category(SmallTests.class) +public class TestTokenUtil { + @ClassRule + public static final HBaseClassTestRule CLASS_RULE = + HBaseClassTestRule.forClass(TestTokenUtil.class); + + @Test + public void testObtainToken() throws Exception { + URL urlPU = ProtobufUtil.class.getProtectionDomain().getCodeSource().getLocation(); + URL urlTU = TokenUtil.class.getProtectionDomain().getCodeSource().getLocation(); + + ClassLoader cl = new URLClassLoader(new URL[] { urlPU, urlTU }, getClass().getClassLoader()); + + Class tokenUtil = cl.loadClass(TokenUtil.class.getCanonicalName()); + tokenUtil.getDeclaredField("shouldInjectFault").setBoolean(null, true); + try { + tokenUtil.getMethod("obtainToken", Connection.class) + .invoke(null, new Object[] { null }); + fail("Should have injected exception."); + } catch (InvocationTargetException e) { + Throwable t = e; + boolean serviceExceptionFound = false; + while ((t = t.getCause()) != null) { + if (t instanceof com.google.protobuf.ServiceException) { + serviceExceptionFound = true; + break; + } + } + if (!serviceExceptionFound) { + // didn't get the right exception, fail the test + throw e; + } + } + + Boolean loaded = (Boolean) cl.loadClass(ProtobufUtil.class.getCanonicalName()) + .getDeclaredMethod("isClassLoaderLoaded") + .invoke(null); + assertFalse("Should not have loaded DynamicClassLoader", loaded); + } +} -- 2.15.1