ndimiduk commented on a change in pull request #78: URL: https://github.com/apache/hbase-thirdparty/pull/78#discussion_r820720521
########## File path: hbase-unsafe/src/main/java/org/apache/hadoop/hbase/unsafe/HBaseUnsafe.java ########## @@ -0,0 +1,562 @@ +/** + * 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.unsafe; + +import java.lang.reflect.Field; +import java.lang.reflect.Method; +import java.security.AccessController; +import java.security.PrivilegedAction; +import java.security.ProtectionDomain; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Delegate all methods of {@link HBaseUnsafeInternal}, so we will not touch the actual + * {@code sun.misc.Unsafe} class until we actually call the methods. + */ +public final class HBaseUnsafe { + + private static final String CLASS_NAME = "sun.misc.Unsafe"; + private static final Logger LOG = LoggerFactory.getLogger(HBaseUnsafe.class); + private static final boolean AVAIL; + private static final boolean UNALIGNED; + + static { + AVAIL = AccessController.doPrivileged(new PrivilegedAction<Boolean>() { + + @Override + public Boolean run() { + return checkAvailable(); + } + }); + UNALIGNED = checkUnaligned(); + } + + private static boolean checkAvailable() { + try { + Class<?> clazz = Class.forName(CLASS_NAME); + Field f = clazz.getDeclaredField("theUnsafe"); + f.setAccessible(true); + Object theUnsafe = f.get(null); + if (theUnsafe == null) { + LOG.warn("Could not get static instance from sun.misc.Unsafe"); + return false; + } + // Check for availability of all methods used by UnsafeAccess + Method m; + try { + m = clazz.getDeclaredMethod("arrayBaseOffset", Class.class); + if (m == null) { Review comment: Hmm good point, this class implementation could be JDK dependent. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
