Class CoprocessorClassLoader
- All Implemented Interfaces:
- Closeable,- AutoCloseable
This ClassLoader always tries to load classes from the specified coprocessor jar first actually using URLClassLoader logic before delegating to the parent ClassLoader, thus avoiding dependency conflicts between HBase's classpath and classes in the coprocessor jar.
Certain classes are exempt from being loaded by this ClassLoader because it would prevent them from being cast to the equivalent classes in the region server. For example, the Coprocessor interface needs to be loaded by the region server's ClassLoader to prevent a ClassCastException when casting the coprocessor implementation.
A HDFS path can be used to specify the coprocessor jar. In this case, the jar will be copied to local at first under some folder under ${hbase.local.dir}/jars/tmp/. The local copy will be removed automatically when the HBase server instance is stopped.
This ClassLoader also handles resource loading. In most cases this ClassLoader will attempt to load resources from the coprocessor jar first before delegating to the parent. However, like in class loading, some resources need to be handled differently. For all of the Hadoop default configurations (e.g. hbase-default.xml) we will check the parent ClassLoader first to prevent issues such as failing the HBase default configuration version check.
- 
Field SummaryFieldsModifier and TypeFieldDescriptionprivate static final String[]If the class being loaded starts with any of these strings, we will skip trying to load it from the coprocessor jar and instead delegate directly to the parent ClassLoader.private static final ConcurrentMap<org.apache.hadoop.fs.Path,CoprocessorClassLoader> External class loaders cache keyed by external jar path.private static final PatternA locker used to synchronize class loader initialization per coprocessor jar fileprivate static final org.slf4j.LoggerA set used to synchronized parent path clean up.private static final Pattern[]If the resource being loaded matches any of these patterns, we will first attempt to load the resource with the parent ClassLoader.private static final StringFields inherited from class org.apache.hadoop.hbase.util.ClassLoaderBaseDEFAULT_LOCAL_DIR, LOCAL_DIR_KEY, parent
- 
Constructor SummaryConstructorsModifierConstructorDescriptionprivateCoprocessorClassLoader(ClassLoader parent) Creates a JarClassLoader that loads classes from the given paths.
- 
Method SummaryModifier and TypeMethodDescriptionstatic voidstatic Collection<? extends ClassLoader>static CoprocessorClassLoadergetClassLoader(org.apache.hadoop.fs.Path path, ClassLoader parent, String pathPrefix, org.apache.hadoop.conf.Configuration conf) Get a CoprocessorClassLoader for a coprocessor jar path from cache.static CoprocessorClassLoadergetIfCached(org.apache.hadoop.fs.Path path) getResource(String name) private voidinit(org.apache.hadoop.fs.Path pathPattern, String pathPrefix, org.apache.hadoop.conf.Configuration conf) protected booleanisClassExempt(String name, String[] includedClassPrefixes) Determines whether the given class should be exempt from being loaded by this ClassLoader.Class<?>Class<?>protected booleanDetermines whether we should attempt to load the given resource using the parent first before attempting to load the resource using this ClassLoader.Methods inherited from class java.net.URLClassLoaderaddURL, close, definePackage, findClass, findResource, findResources, getPermissions, getResourceAsStream, getURLs, newInstance, newInstanceMethods inherited from class java.security.SecureClassLoaderdefineClass, defineClassMethods inherited from class java.lang.ClassLoaderclearAssertionStatus, defineClass, defineClass, defineClass, defineClass, definePackage, findClass, findLibrary, findLoadedClass, findResource, findSystemClass, getClassLoadingLock, getDefinedPackage, getDefinedPackages, getName, getPackage, getPackages, getParent, getPlatformClassLoader, getResources, getSystemClassLoader, getSystemResource, getSystemResourceAsStream, getSystemResources, getUnnamedModule, isRegisteredAsParallelCapable, loadClass, registerAsParallelCapable, resolveClass, resources, setClassAssertionStatus, setDefaultAssertionStatus, setPackageAssertionStatus, setSigners
- 
Field Details- 
LOG
- 
TMP_JARS_DIR
- 
classLoadersCacheprivate static final ConcurrentMap<org.apache.hadoop.fs.Path,CoprocessorClassLoader> classLoadersCacheExternal class loaders cache keyed by external jar path. ClassLoader instance is stored as a weak-reference to allow GC'ing when it is not used (@see HBASE-7205)
- 
CLASS_PREFIX_EXEMPTIONSIf the class being loaded starts with any of these strings, we will skip trying to load it from the coprocessor jar and instead delegate directly to the parent ClassLoader.
- 
RESOURCE_LOAD_PARENT_FIRST_PATTERNSIf the resource being loaded matches any of these patterns, we will first attempt to load the resource with the parent ClassLoader. Only if the resource is not found by the parent do we attempt to load it from the coprocessor jar.
- 
libJarPattern
- 
lockerA locker used to synchronize class loader initialization per coprocessor jar file
- 
parentDirLockSetA set used to synchronized parent path clean up. Generally, there should be only one parent path, but using a set so that we can support more.
 
- 
- 
Constructor Details- 
CoprocessorClassLoaderCreates a JarClassLoader that loads classes from the given paths.
 
- 
- 
Method Details- 
initprivate void init(org.apache.hadoop.fs.Path pathPattern, String pathPrefix, org.apache.hadoop.conf.Configuration conf) throws IOException - Throws:
- IOException
 
- 
getIfCached
- 
getAllCached
- 
clearCache
- 
getClassLoaderpublic static CoprocessorClassLoader getClassLoader(org.apache.hadoop.fs.Path path, ClassLoader parent, String pathPrefix, org.apache.hadoop.conf.Configuration conf) throws IOException Get a CoprocessorClassLoader for a coprocessor jar path from cache. If not in cache, create one.- Parameters:
- path- the path to the coprocessor jar file to load classes from
- parent- the parent class loader for exempted classes
- pathPrefix- a prefix used in temp path name to store the jar file locally
- conf- the configuration used to create the class loader, if needed
- Returns:
- a CoprocessorClassLoader for the coprocessor jar path
- Throws:
- IOException
 
- 
loadClass- Overrides:
- loadClassin class- ClassLoader
- Throws:
- ClassNotFoundException
 
- 
loadClasspublic Class<?> loadClass(String name, String[] includedClassPrefixes) throws ClassNotFoundException - Throws:
- ClassNotFoundException
 
- 
getResource- Overrides:
- getResourcein class- ClassLoader
 
- 
isClassExemptDetermines whether the given class should be exempt from being loaded by this ClassLoader.- Parameters:
- name- the name of the class to test.
- Returns:
- true if the class should *not* be loaded by this ClassLoader; false otherwise.
 
- 
loadResourceUsingParentFirstDetermines whether we should attempt to load the given resource using the parent first before attempting to load the resource using this ClassLoader.- Parameters:
- name- the name of the resource to test.
- Returns:
- true if we should attempt to load the resource using the parent first; false if we should attempt to load the resource using this ClassLoader first.
 
 
-