Search in sources :

Example 1 with CoprocessorClassLoader

use of org.apache.hadoop.hbase.util.CoprocessorClassLoader in project hbase by apache.

the class CoprocessorHost method load.

/**
 * Load a coprocessor implementation into the host
 * @param path path to implementation jar
 * @param className the main class name
 * @param priority chaining priority
 * @param conf configuration for coprocessor
 * @param includedClassPrefixes class name prefixes to include
 * @throws java.io.IOException Exception
 */
public E load(Path path, String className, int priority, Configuration conf, String[] includedClassPrefixes) throws IOException {
    Class<?> implClass;
    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 {
            implClass = getClass().getClassLoader().loadClass(className);
        } catch (ClassNotFoundException e) {
            throw new IOException("No jar path specified for " + className);
        }
    } else {
        cl = CoprocessorClassLoader.getClassLoader(path, getClass().getClassLoader(), pathPrefix, conf);
        try {
            implClass = ((CoprocessorClassLoader) cl).loadClass(className, includedClassPrefixes);
        } catch (ClassNotFoundException e) {
            throw new IOException("Cannot load external coprocessor class " + className, e);
        }
    }
    // load custom code for coprocessor
    Thread currentThread = Thread.currentThread();
    ClassLoader hostClassLoader = currentThread.getContextClassLoader();
    try {
        // switch temporarily to the thread classloader for custom CP
        currentThread.setContextClassLoader(cl);
        E cpInstance = checkAndLoadInstance(implClass, priority, conf);
        return cpInstance;
    } finally {
        // restore the fresh (host) classloader
        currentThread.setContextClassLoader(hostClassLoader);
    }
}
Also used : CoprocessorClassLoader(org.apache.hadoop.hbase.util.CoprocessorClassLoader) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) IOException(java.io.IOException)

Example 2 with CoprocessorClassLoader

use of org.apache.hadoop.hbase.util.CoprocessorClassLoader in project hbase by apache.

the class RegionCoprocessorHost method testTableCoprocessorAttrs.

/**
 * Sanity check the table coprocessor attributes of the supplied schema. Will
 * throw an exception if there is a problem.
 * @param conf
 * @param htd
 * @throws IOException
 */
public static void testTableCoprocessorAttrs(final Configuration conf, final TableDescriptor htd) throws IOException {
    String pathPrefix = UUID.randomUUID().toString();
    for (TableCoprocessorAttribute attr : getTableCoprocessorAttrsFromSchema(conf, htd)) {
        if (attr.getPriority() < 0) {
            throw new IOException("Priority for coprocessor " + attr.getClassName() + " cannot be less than 0");
        }
        ClassLoader old = Thread.currentThread().getContextClassLoader();
        try {
            ClassLoader cl;
            if (attr.getPath() != null) {
                cl = CoprocessorClassLoader.getClassLoader(attr.getPath(), CoprocessorHost.class.getClassLoader(), pathPrefix, conf);
            } else {
                cl = CoprocessorHost.class.getClassLoader();
            }
            Thread.currentThread().setContextClassLoader(cl);
            if (cl instanceof CoprocessorClassLoader) {
                String[] includedClassPrefixes = null;
                if (conf.get(HConstants.CP_HTD_ATTR_INCLUSION_KEY) != null) {
                    String prefixes = attr.conf.get(HConstants.CP_HTD_ATTR_INCLUSION_KEY);
                    includedClassPrefixes = prefixes.split(";");
                }
                ((CoprocessorClassLoader) cl).loadClass(attr.getClassName(), includedClassPrefixes);
            } else {
                cl.loadClass(attr.getClassName());
            }
        } catch (ClassNotFoundException e) {
            throw new IOException("Class " + attr.getClassName() + " cannot be loaded", e);
        } finally {
            Thread.currentThread().setContextClassLoader(old);
        }
    }
}
Also used : CoprocessorHost(org.apache.hadoop.hbase.coprocessor.CoprocessorHost) CoprocessorClassLoader(org.apache.hadoop.hbase.util.CoprocessorClassLoader) IOException(java.io.IOException) CoprocessorClassLoader(org.apache.hadoop.hbase.util.CoprocessorClassLoader)

Aggregations

IOException (java.io.IOException)2 CoprocessorClassLoader (org.apache.hadoop.hbase.util.CoprocessorClassLoader)2 DoNotRetryIOException (org.apache.hadoop.hbase.DoNotRetryIOException)1 CoprocessorHost (org.apache.hadoop.hbase.coprocessor.CoprocessorHost)1