/** * Constructs a {@link org.apache.hadoop.hbase.client.RequestController}. * @param conf The {@link Configuration} to use. * @return A RequestController which is built according to the configuration. */ public static RequestController create(Configuration conf) { Class<? extends RequestController> clazz= conf.getClass(REQUEST_CONTROLLER_IMPL_CONF_KEY, SimpleRequestController.class, RequestController.class); return ReflectionUtils.newInstance(clazz, conf); } }
public static ClientBackoffPolicy create(Configuration conf) { // create the backoff policy String className = conf.get(ClientBackoffPolicy.BACKOFF_POLICY_CLASS, NoBackoffPolicy.class .getName()); return ReflectionUtils.instantiateWithCustomCtor(className, new Class<?>[] { Configuration.class }, new Object[] { conf }); }
public static <T> T newInstance(Class<T> type, Object... params) { return instantiate(type.getName(), findConstructor(type, params), params); }
/** * Get the storage policy of the source path (directory/file). * @param path The source path (directory/file). * @return Storage policy name, or {@code null} if not using {@link DistributedFileSystem} or * exception thrown when trying to get policy */ @Nullable public String getStoragePolicyName(Path path) { try { Object blockStoragePolicySpi = ReflectionUtils.invokeMethod(this.fs, "getStoragePolicy", path); return (String) ReflectionUtils.invokeMethod(blockStoragePolicySpi, "getName"); } catch (Exception e) { // Maybe fail because of using old HDFS version, try the old way if (LOG.isTraceEnabled()) { LOG.trace("Failed to get policy directly", e); } return getStoragePolicyForOldHDFSVersion(path); } }
@SuppressWarnings("unchecked") public static <T> T instantiateWithCustomCtor(String className, Class<? >[] ctorArgTypes, Object[] ctorArgs) { try { Class<? extends T> resultType = (Class<? extends T>) Class.forName(className); Constructor<? extends T> ctor = resultType.getDeclaredConstructor(ctorArgTypes); return instantiate(className, ctor, ctorArgs); } catch (ClassNotFoundException e) { throw new UnsupportedOperationException( "Unable to find " + className, e); } catch (NoSuchMethodException e) { throw new UnsupportedOperationException( "Unable to find suitable constructor for class " + className, e); } }
@Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (!HttpServer.isInstrumentationAccessAllowed(getServletContext(), request, response)) { return; } response.setContentType("text/plain; charset=UTF-8"); try (PrintStream out = new PrintStream( response.getOutputStream(), false, "UTF-8")) { Threads.printThreadInfo(out, ""); out.flush(); } ReflectionUtils.logThreadInfo(LOG, "jsp requested", 1); } }
/** * Log the current thread stacks at INFO level. * @param log the logger that logs the stack trace * @param title a descriptive title for the call stacks * @param minInterval the minimum time from the last */ public static void logThreadInfo(Logger log, String title, long minInterval) { boolean dumpStack = false; if (log.isInfoEnabled()) { synchronized (ReflectionUtils.class) { long now = System.currentTimeMillis(); if (now - previousLogTime >= minInterval * 1000) { previousLogTime = now; dumpStack = true; } } if (dumpStack) { try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); printThreadInfo(new PrintStream(buffer, false, "UTF-8"), title); log.info(buffer.toString(Charset.defaultCharset().name())); } catch (UnsupportedEncodingException ignored) { log.warn("Could not write thread info about '" + title + "' due to a string encoding issue."); } } } }
getTaskName(info.getThreadId(), info.getThreadName()) + ":"); Thread.State state = info.getThreadState(); stream.println(" Blocked on " + info.getLockName()); stream.println(" Blocked by " + getTaskName(info.getLockOwnerId(), info.getLockOwnerName()));
private String getStoragePolicyName(FileSystem fs, Path path) { try { Object blockStoragePolicySpi = ReflectionUtils.invokeMethod(fs, "getStoragePolicy", path); return (String) ReflectionUtils.invokeMethod(blockStoragePolicySpi, "getName"); } catch (Exception e) { // Maybe fail because of using old HDFS version, try the old way if (LOG.isTraceEnabled()) { LOG.trace("Failed to get policy directly", e); } String policy = getStoragePolicyNameForOldHDFSVersion(fs, path); return policy == null ? "HOT" : policy;// HOT by default } }
@SuppressWarnings("unchecked") public static <T> T instantiateWithCustomCtor(String className, Class<? >[] ctorArgTypes, Object[] ctorArgs) { try { Class<? extends T> resultType = (Class<? extends T>) Class.forName(className); Constructor<? extends T> ctor = resultType.getDeclaredConstructor(ctorArgTypes); return instantiate(className, ctor, ctorArgs); } catch (ClassNotFoundException e) { throw new UnsupportedOperationException( "Unable to find " + className, e); } catch (NoSuchMethodException e) { throw new UnsupportedOperationException( "Unable to find suitable constructor for class " + className, e); } }
@Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (!HttpServer.isInstrumentationAccessAllowed(getServletContext(), request, response)) { return; } response.setContentType("text/plain; charset=UTF-8"); try (PrintStream out = new PrintStream( response.getOutputStream(), false, "UTF-8")) { Threads.printThreadInfo(out, ""); out.flush(); } ReflectionUtils.logThreadInfo(LOG, "jsp requested", 1); } }
/** * Log the current thread stacks at INFO level. * @param log the logger that logs the stack trace * @param title a descriptive title for the call stacks * @param minInterval the minimum time from the last */ public static void logThreadInfo(Logger log, String title, long minInterval) { boolean dumpStack = false; if (log.isInfoEnabled()) { synchronized (ReflectionUtils.class) { long now = System.currentTimeMillis(); if (now - previousLogTime >= minInterval * 1000) { previousLogTime = now; dumpStack = true; } } if (dumpStack) { try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); printThreadInfo(new PrintStream(buffer, false, "UTF-8"), title); log.info(buffer.toString(Charset.defaultCharset().name())); } catch (UnsupportedEncodingException ignored) { log.warn("Could not write thread info about '" + title + "' due to a string encoding issue."); } } } }
getTaskName(info.getThreadId(), info.getThreadName()) + ":"); Thread.State state = info.getThreadState(); stream.println(" Blocked on " + info.getLockName()); stream.println(" Blocked by " + getTaskName(info.getLockOwnerId(), info.getLockOwnerName()));
/** * @return The cluster registry implementation to use. */ static AsyncRegistry getRegistry(Configuration conf) { Class<? extends AsyncRegistry> clazz = conf.getClass(REGISTRY_IMPL_CONF_KEY, ZKAsyncRegistry.class, AsyncRegistry.class); return ReflectionUtils.newInstance(clazz, conf); } }
public RackManager(Configuration conf) { switchMapping = ReflectionUtils.instantiateWithCustomCtor( conf.getClass("hbase.util.ip.to.rack.determiner", ScriptBasedMapping.class, DNSToSwitchMapping.class).getName(), new Class<?>[]{Configuration.class}, new Object[]{conf}); }
public static <T> T newInstance(Class<T> type, Object... params) { return instantiate(type.getName(), findConstructor(type, params), params); }
private String getStoragePolicyName(FileSystem fs, Path path) { try { Object blockStoragePolicySpi = ReflectionUtils.invokeMethod(fs, "getStoragePolicy", path); return (String) ReflectionUtils.invokeMethod(blockStoragePolicySpi, "getName"); } catch (Exception e) { // Maybe fail because of using old HDFS version, try the old way if (LOG.isTraceEnabled()) { LOG.trace("Failed to get policy directly", e); } String policy = getStoragePolicyNameForOldHDFSVersion(fs, path); return policy == null ? "HOT" : policy;// HOT by default } }
@SuppressWarnings("unchecked") public static <T> T instantiateWithCustomCtor(String className, Class<? >[] ctorArgTypes, Object[] ctorArgs) { try { Class<? extends T> resultType = (Class<? extends T>) Class.forName(className); Constructor<? extends T> ctor = resultType.getDeclaredConstructor(ctorArgTypes); return instantiate(className, ctor, ctorArgs); } catch (ClassNotFoundException e) { throw new UnsupportedOperationException( "Unable to find " + className, e); } catch (NoSuchMethodException e) { throw new UnsupportedOperationException( "Unable to find suitable constructor for class " + className, e); } }
@Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { if (!HttpServer.isInstrumentationAccessAllowed(getServletContext(), request, response)) { return; } response.setContentType("text/plain; charset=UTF-8"); try (PrintStream out = new PrintStream( response.getOutputStream(), false, "UTF-8")) { Threads.printThreadInfo(out, ""); out.flush(); } ReflectionUtils.logThreadInfo(LOG, "jsp requested", 1); } }
/** * Log the current thread stacks at INFO level. * @param log the logger that logs the stack trace * @param title a descriptive title for the call stacks * @param minInterval the minimum time from the last */ public static void logThreadInfo(Logger log, String title, long minInterval) { boolean dumpStack = false; if (log.isInfoEnabled()) { synchronized (ReflectionUtils.class) { long now = System.currentTimeMillis(); if (now - previousLogTime >= minInterval * 1000) { previousLogTime = now; dumpStack = true; } } if (dumpStack) { try { ByteArrayOutputStream buffer = new ByteArrayOutputStream(); printThreadInfo(new PrintStream(buffer, false, "UTF-8"), title); log.info(buffer.toString(Charset.defaultCharset().name())); } catch (UnsupportedEncodingException ignored) { log.warn("Could not write thread info about '" + title + "' due to a string encoding issue."); } } } }