diff --git a/src/ql/src/java/org/apache/hadoop/hive/ql/debug/Utils.java b/src/ql/src/java/org/apache/hadoop/hive/ql/debug/Utils.java index 439b46a..3a3bd18 100644 --- a/src/ql/src/java/org/apache/hadoop/hive/ql/debug/Utils.java +++ b/src/ql/src/java/org/apache/hadoop/hive/ql/debug/Utils.java @@ -21,19 +21,38 @@ package org.apache.hadoop.hive.ql.debug; import java.io.File; import java.io.UnsupportedEncodingException; import java.lang.management.ManagementFactory; +import java.lang.reflect.Method; import javax.management.MBeanServer; import org.apache.commons.lang.StringUtils; - -import com.sun.management.HotSpotDiagnosticMXBean; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; /** * Debug utility methods for Hive. */ public class Utils { + private static final Log LOG = LogFactory.getLog(Utils.class.getName()); private static final String HOTSPOT_BEAN_NAME = "com.sun.management:type=HotSpotDiagnostic"; - private static volatile HotSpotDiagnosticMXBean hotspotMBean; + private static volatile Object hotspotMBean; + private static final Method DUMP_HEAP_METHOD; + private static final Class HOTSPOT_MXBEAN_CLASS; + + static { + Class clazz; + Method method; + try{ + clazz = Class.forName("com.sun.management.HotSpotDiagnosticMXBean"); + method = clazz.getMethod("dumpHeap", String.class, boolean.class); + } catch (Exception ce){ + LOG.error("com.sun.management.HotSpotDiagnosticMXBean is not supported"); + clazz = null; + method = null; + } + HOTSPOT_MXBEAN_CLASS = clazz; + DUMP_HEAP_METHOD = method; + } /** * Dumps process heap to a file in temp directoty. @@ -62,19 +81,27 @@ public class Utils { try { MBeanServer server = ManagementFactory.getPlatformMBeanServer(); hotspotMBean = ManagementFactory.newPlatformMXBeanProxy(server, - HOTSPOT_BEAN_NAME, HotSpotDiagnosticMXBean.class); + HOTSPOT_BEAN_NAME, HOTSPOT_MXBEAN_CLASS); } catch (RuntimeException re) { + LOG.error("Failed to create HotSpotDiagnosticMXBean."); throw re; } catch (Exception exp) { + LOG.error("Failed to create HotSpotDiagnosticMXBean."); throw new RuntimeException(exp); } } - try { - hotspotMBean.dumpHeap(fileName, live); - } catch (RuntimeException re) { + if(DUMP_HEAP_METHOD != null) { + try { + DUMP_HEAP_METHOD.invoke(hotspotMBean, new Object[]{fileName, Boolean.valueOf(live)}); + } catch (RuntimeException re) { + LOG.error("Failed to execute dumpHeap."); throw re; - } catch (Exception exp) { + } catch (Exception exp) { + LOG.error("Failed to execute dumpHeap."); throw new RuntimeException(exp); + } + } else { + LOG.error("Cannot find method dumpHeap() in com.sun.management.HotSpotDiagnosticMXBean."); } }