From 98bdd5df13e8f55b093799ee960987761613a40b Mon Sep 17 00:00:00 2001 From: Michael Stack Date: Wed, 14 Nov 2012 08:42:52 +0000 Subject: [PATCH] HBASE-6945 Compilation errors when using non-Sun JDKs to build HBase-0.94 git-svn-id: https://svn.apache.org/repos/asf/hbase/trunk@1409115 13f79535-47bb-0310-9956-ffa450edef68 --- .../hbase/util/{OSMXBean.java => JVM.java} | 51 ++++++++++--------- .../hbase/ResourceCheckerJUnitListener.java | 47 ++++++----------- 2 files changed, 41 insertions(+), 57 deletions(-) rename hbase-common/src/main/java/org/apache/hadoop/hbase/util/{OSMXBean.java => JVM.java} (83%) diff --git a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/OSMXBean.java b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/JVM.java similarity index 83% rename from hbase-common/src/main/java/org/apache/hadoop/hbase/util/OSMXBean.java rename to hbase-common/src/main/java/org/apache/hadoop/hbase/util/JVM.java index 2569a83331f..8b0b329629c 100644 --- a/hbase-common/src/main/java/org/apache/hadoop/hbase/util/OSMXBean.java +++ b/hbase-common/src/main/java/org/apache/hadoop/hbase/util/JVM.java @@ -34,17 +34,20 @@ import java.lang.reflect.Method; import org.apache.hadoop.classification.InterfaceAudience; import org.apache.hadoop.classification.InterfaceStability; +import org.apache.hadoop.util.Shell; + /** * This class is a wrapper for the implementation of * com.sun.management.UnixOperatingSystemMXBean - * It will decide to use Oracle Java api or its own implementation + * It will decide to use the sun api or its own implementation * depending on the runtime (vendor) used. */ @InterfaceAudience.Public @InterfaceStability.Evolving -public class OSMXBean { - static final Logger LOG = LoggerFactory.getLogger(OSMXBean.class); +public class JVM +{ + static final Logger LOG = LoggerFactory.getLogger(JVM.class); private OperatingSystemMXBean osMbean; @@ -58,7 +61,7 @@ public class OSMXBean { /** * Constructor. Get the running Operating System instance */ - public OSMXBean () { + public JVM () { this.osMbean = ManagementFactory.getOperatingSystemMXBean(); } @@ -80,8 +83,8 @@ public class OSMXBean { * @param mBeanMethodName : method to run from the interface UnixOperatingSystemMXBean * @return the method result */ - private Long runUnixMXBeanMethod (String mBeanMethodName) - { + private Long runUnixMXBeanMethod (String mBeanMethodName) { + Object unixos; Class classRef; Method mBeanMethod; @@ -107,8 +110,8 @@ public class OSMXBean { * Otherwise, this methods implements it (linux only). * @return number of open file descriptors for the jvm */ - public long getOpenFileDescriptorCount() - { + public long getOpenFileDescriptorCount() { + Long ofdc; if (!ibmvendor) { @@ -123,18 +126,17 @@ public class OSMXBean { //using linux bash commands to retrieve info Process p = Runtime.getRuntime().exec( - new String[] { "bash", "-c", + new String[] { "bash", "-c", "ls /proc/" + pidhost[0] + "/fdinfo | wc -l" }); InputStream in = p.getInputStream(); BufferedReader output = new BufferedReader( - new InputStreamReader(in)); + new InputStreamReader(in)); String openFileDesCount; - if ((openFileDesCount = output.readLine()) != null) { - return Long.parseLong(openFileDesCount); - } - } catch (IOException ie) { - LOG.warn("Not able to get the number of open file descriptors", ie); + if ((openFileDesCount = output.readLine()) != null) + return Long.parseLong(openFileDesCount); + } catch (IOException ie) { + LOG.warn("Not able to get the number of open file descriptors", ie); } return -1; } @@ -145,8 +147,8 @@ public class OSMXBean { * Otherwise, this methods implements it (linux only). * @return max number of file descriptors the operating system can use. */ - public long getMaxFileDescriptorCount() - { + public long getMaxFileDescriptorCount() { + Long mfdc; if (!ibmvendor) { @@ -157,19 +159,18 @@ public class OSMXBean { //using linux bash commands to retrieve info Process p = Runtime.getRuntime().exec( - new String[] { "bash", "-c", - "ulimit -n" }); + new String[] { "bash", "-c", + "ulimit -n" }); InputStream in = p.getInputStream(); BufferedReader output = new BufferedReader( new InputStreamReader(in)); String maxFileDesCount; - if ((maxFileDesCount = output.readLine()) != null) { - return Long.parseLong(maxFileDesCount); - } - } catch (IOException ie) { - LOG.warn("Not able to get the max number of file descriptors", ie); + if ((maxFileDesCount = output.readLine()) != null) + return Long.parseLong(maxFileDesCount); + } catch (IOException ie) { + LOG.warn("Not able to get the max number of file descriptors", ie); } return -1; - } + } } diff --git a/hbase-common/src/test/java/org/apache/hadoop/hbase/ResourceCheckerJUnitListener.java b/hbase-common/src/test/java/org/apache/hadoop/hbase/ResourceCheckerJUnitListener.java index 591f364cedf..3ed9c749e83 100644 --- a/hbase-common/src/test/java/org/apache/hadoop/hbase/ResourceCheckerJUnitListener.java +++ b/hbase-common/src/test/java/org/apache/hadoop/hbase/ResourceCheckerJUnitListener.java @@ -32,7 +32,7 @@ import java.util.concurrent.ConcurrentHashMap; import org.apache.hadoop.hbase.ResourceChecker.Phase; import org.junit.runner.notification.RunListener; -import com.sun.management.UnixOperatingSystemMXBean; +import org.apache.hadoop.hbase.util.JVM; /** * Listen to the test progress and check the usage of: @@ -85,32 +85,15 @@ public class ResourceCheckerJUnitListener extends RunListener { } } - /** - * On unix, we know how to get the number of open file descriptor. This class allow to share - * the MXBeans code. - */ - abstract static class OSResourceAnalyzer extends ResourceChecker.ResourceAnalyzer { - protected static final OperatingSystemMXBean osStats; - protected static final UnixOperatingSystemMXBean unixOsStats; - static { - osStats = ManagementFactory.getOperatingSystemMXBean(); - if (osStats instanceof UnixOperatingSystemMXBean) { - unixOsStats = (UnixOperatingSystemMXBean) osStats; - } else { - unixOsStats = null; - } - } - } - - static class OpenFileDescriptorResourceAnalyzer extends OSResourceAnalyzer { + static class OpenFileDescriptorResourceAnalyzer extends ResourceChecker.ResourceAnalyzer { @Override public int getVal(Phase phase) { - if (unixOsStats == null) { - return 0; - } else { - return (int) unixOsStats.getOpenFileDescriptorCount(); - } + JVM jvm = new JVM(); + if (jvm != null && jvm.isUnix() == true) + return (int)jvm.getOpenFileDescriptorCount(); + else + return 0; } @Override @@ -119,16 +102,16 @@ public class ResourceCheckerJUnitListener extends RunListener { } } - static class MaxFileDescriptorResourceAnalyzer extends OSResourceAnalyzer { + static class MaxFileDescriptorResourceAnalyzer extends ResourceChecker.ResourceAnalyzer { @Override public int getVal(Phase phase) { - if (unixOsStats == null) { - return 0; - } else { - return (int) unixOsStats.getMaxFileDescriptorCount(); - } - } - } + JVM jvm = new JVM(); + if (jvm != null && jvm.isUnix() == true) + return (int)jvm.getMaxFileDescriptorCount(); + else + return 0; + } + } /**