You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by tb...@apache.org on 2013/02/19 21:59:07 UTC

svn commit: r1447921 - in /incubator/ambari/trunk/ambari-server/src: main/java/org/apache/ambari/server/controller/jmx/ test/java/org/apache/ambari/server/controller/jmx/

Author: tbeerbower
Date: Tue Feb 19 20:59:07 2013
New Revision: 1447921

URL: http://svn.apache.org/r1447921
Log:
AMBARI-1443 - Should use defaults when missing JMX port info from configuration

Modified:
    incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/jmx/JMXHostProvider.java
    incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/jmx/JMXPropertyProvider.java
    incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/jmx/JMXPropertyProviderTest.java

Modified: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/jmx/JMXHostProvider.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/jmx/JMXHostProvider.java?rev=1447921&r1=1447920&r2=1447921&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/jmx/JMXHostProvider.java (original)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/jmx/JMXHostProvider.java Tue Feb 19 20:59:07 2013
@@ -19,8 +19,6 @@ package org.apache.ambari.server.control
 
 import org.apache.ambari.server.controller.spi.SystemException;
 
-import java.util.Map;
-
 /**
  * Provider of JMX host information.
  */
@@ -34,18 +32,21 @@ public interface JMXHostProvider {
    *
    * @return the JMX host name
    *
-   * @throws SystemException of unable to ge the JMX host name
+   * @throws SystemException if unable to get the JMX host name
    */
-  public String getHostName(String clusterName, String componentName) throws SystemException;
+  public String getHostName(String clusterName, String componentName)
+      throws SystemException;
 
   /**
-   * Get the port for specified cluster name and component
+   * Get the port for the specified cluster name and component.
+   *
+   * @param clusterName    the cluster name
+   * @param componentName  the component name
+   *
+   * @return the port for the specified cluster name and component
    *
-   * @param clusterName
-   * @param componentName
-   * @return
-   * @throws SystemException
+   * @throws SystemException if unable to get the JMX port
    */
-  public String getPort(String clusterName, String componentName) throws
-    SystemException;
+  public String getPort(String clusterName, String componentName)
+      throws SystemException;
 }

Modified: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/jmx/JMXPropertyProvider.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/jmx/JMXPropertyProvider.java?rev=1447921&r1=1447920&r2=1447921&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/jmx/JMXPropertyProvider.java (original)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/jmx/JMXPropertyProvider.java Tue Feb 19 20:59:07 2013
@@ -49,6 +49,8 @@ public class JMXPropertyProvider extends
 
   private final JMXHostProvider jmxHostProvider;
 
+  private static final Map<String, String> DEFAULT_JMX_PORTS = new HashMap<String, String>();
+
   private final String clusterNamePropertyId;
 
   private final String hostNamePropertyId;
@@ -59,6 +61,12 @@ public class JMXPropertyProvider extends
 
 
   static {
+    DEFAULT_JMX_PORTS.put("NAMENODE",     "50070");
+    DEFAULT_JMX_PORTS.put("DATANODE",     "50075");
+    DEFAULT_JMX_PORTS.put("JOBTRACKER",   "50030");
+    DEFAULT_JMX_PORTS.put("TASKTRACKER",  "50060");
+    DEFAULT_JMX_PORTS.put("HBASE_MASTER", "60010");
+
     ObjectMapper objectMapper = new ObjectMapper();
     objectMapper.configure(DeserializationConfig.Feature.USE_ANNOTATIONS, false);
     objectReader = objectMapper.reader(JMXMetricHolder.class);
@@ -117,6 +125,18 @@ public class JMXPropertyProvider extends
   // ----- helper methods ----------------------------------------------------
 
   /**
+   * Get the spec to locate the JMX stream from the given host and port
+   *
+   * @param hostName  the host name
+   * @param port      the port
+   *
+   * @return the spec
+   */
+  protected String getSpec(String hostName, String port) {
+    return "http://" + hostName + ":" + port + "/jmx";
+  }
+
+  /**
    * Populate a resource by obtaining the requested JMX properties.
    *
    * @param resource  the resource to be populated
@@ -133,25 +153,31 @@ public class JMXPropertyProvider extends
       return true;
     }
 
-    String clusterName   = (String) resource.getPropertyValue(clusterNamePropertyId);
     String componentName = (String) resource.getPropertyValue(componentNamePropertyId);
-    String port          = jmxHostProvider.getPort(clusterName, componentName);
 
-    String hostName;
-    if (hostNamePropertyId == null) {
-      hostName = jmxHostProvider.getHostName(clusterName, componentName);
+    if (getComponentMetrics().get(componentName) == null) {
+      // If there are no metrics defined for the given component then there is nothing to do.
+      return true;
     }
-    else {
-      hostName = (String) resource.getPropertyValue(hostNamePropertyId);
+
+    String clusterName = (String) resource.getPropertyValue(clusterNamePropertyId);
+
+    String port = getPort(clusterName, componentName);
+    if (port == null) {
+      String error = "Unable to get JMX metrics.  No port value for " + componentName;
+      logError(error, null);
+      throw new SystemException(error, null);
     }
 
-    if (getComponentMetrics().get(componentName) == null ||
-        hostName == null || port == null) {
-      return true;
+    String hostName = getHost(resource, clusterName, componentName);
+    if (hostName == null) {
+      String error = "Unable to get JMX metrics.  No host name for " + componentName;
+      logError(error, null);
+      throw new SystemException(error, null);
     }
 
-    String spec = getSpec(hostName + ":" + port);
-    InputStream in = null;
+    String      spec = getSpec(hostName, port);
+    InputStream in   = null;
     try {
       in = streamProvider.readFrom(spec);
       JMXMetricHolder metricHolder = objectReader.readValue(in);
@@ -192,7 +218,6 @@ public class JMXPropertyProvider extends
               }
             }
 
-
             int dotIndex = property.lastIndexOf('.', firstKeyIndex - 1);
             if (dotIndex != -1){
               category = property.substring(0, dotIndex);
@@ -220,17 +245,13 @@ public class JMXPropertyProvider extends
         }
       }
     } catch (IOException e) {
-      if (LOG.isErrorEnabled()) {
-        LOG.error("Caught exception getting JMX metrics : spec=" + spec, e);
-      }
+      logError(spec, e);
     } finally {
       if (in != null) {
         try {
           in.close();
         } catch (IOException e) {
-          if (LOG.isWarnEnabled()) {
-            LOG.warn("Unable to close http input steam : spec=" + spec, e);
-          }
+            logError("Unable to close http input steam : spec=" + spec, e);
         }
       }
     }
@@ -238,6 +259,17 @@ public class JMXPropertyProvider extends
     return true;
   }
 
+  private String getPort(String clusterName, String componentName) throws SystemException {
+    String port = jmxHostProvider.getPort(clusterName, componentName);
+    return port == null ? DEFAULT_JMX_PORTS.get(componentName) : port;
+  }
+
+  private String getHost(Resource resource, String clusterName, String componentName) throws SystemException {
+    return hostNamePropertyId == null ?
+        jmxHostProvider.getHostName(clusterName, componentName) :
+        (String) resource.getPropertyValue(hostNamePropertyId);
+  }
+
   private String getCategory(Map<String, Object> bean) {
     if (bean.containsKey(NAME_KEY)) {
       String name = (String) bean.get(NAME_KEY);
@@ -251,15 +283,13 @@ public class JMXPropertyProvider extends
     return null;
   }
 
-  /**
-   * Get the spec to locate the JMX stream from the given source
-   *
-   * @param jmxSource  the source (host and port)
-   *
-   * @return the spec
-   */
-  protected String getSpec(String jmxSource) {
-    return "http://" + jmxSource + "/jmx";
+  private static void logError(String error, IOException e) {
+    if (LOG.isErrorEnabled()) {
+      if (e == null) {
+        LOG.error("Caught exception getting JMX metrics : spec=" + error);
+      } else {
+        LOG.error("Caught exception getting JMX metrics : spec=" + error, e);
+      }
+    }
   }
-
 }

Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/jmx/JMXPropertyProviderTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/jmx/JMXPropertyProviderTest.java?rev=1447921&r1=1447920&r2=1447921&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/jmx/JMXPropertyProviderTest.java (original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/jmx/JMXPropertyProviderTest.java Tue Feb 19 20:59:07 2013
@@ -41,7 +41,7 @@ public class JMXPropertyProviderTest {
   @Test
   public void testGetResources() throws Exception {
     TestStreamProvider  streamProvider = new TestStreamProvider();
-    TestJMXHostProvider hostProvider = new TestJMXHostProvider();
+    TestJMXHostProvider hostProvider = new TestJMXHostProvider(false);
 
     JMXPropertyProvider propertyProvider = new JMXPropertyProvider(
         PropertyHelper.getJMXPropertyIds(Resource.Type.HostComponent),
@@ -59,7 +59,7 @@ public class JMXPropertyProviderTest {
 
     Assert.assertEquals(1, propertyProvider.populateResources(Collections.singleton(resource), request, null).size());
 
-    Assert.assertEquals(propertyProvider.getSpec("domu-12-31-39-0e-34-e1.compute-1.internal:50070"), streamProvider.getLastSpec());
+    Assert.assertEquals(propertyProvider.getSpec("domu-12-31-39-0e-34-e1.compute-1.internal", "50070"), streamProvider.getLastSpec());
 
     // see test/resources/hdfs_namenode_jmx.json for values
     Assert.assertEquals(13670605,  resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/rpc", "ReceivedBytes")));
@@ -81,7 +81,7 @@ public class JMXPropertyProviderTest {
 
     propertyProvider.populateResources(Collections.singleton(resource), request, null);
 
-    Assert.assertEquals(propertyProvider.getSpec("domu-12-31-39-14-ee-b3.compute-1.internal:50075"), streamProvider.getLastSpec());
+    Assert.assertEquals(propertyProvider.getSpec("domu-12-31-39-14-ee-b3.compute-1.internal", "50075"), streamProvider.getLastSpec());
 
     // see test/resources/hdfs_datanode_jmx.json for values
     Assert.assertEquals(856,  resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/rpc", "ReceivedBytes")));
@@ -111,7 +111,7 @@ public class JMXPropertyProviderTest {
 
     propertyProvider.populateResources(Collections.singleton(resource), request, null);
 
-    Assert.assertEquals(propertyProvider.getSpec("domu-12-31-39-14-ee-b3.compute-1.internal:50030"), streamProvider.getLastSpec());
+    Assert.assertEquals(propertyProvider.getSpec("domu-12-31-39-14-ee-b3.compute-1.internal", "50030"), streamProvider.getLastSpec());
 
     // see test/resources/mapreduce_jobtracker_jmx.json for values
     Assert.assertEquals(9, PropertyHelper.getProperties(resource).size());
@@ -146,7 +146,7 @@ public class JMXPropertyProviderTest {
 
     propertyProvider.populateResources(Collections.singleton(resource), request, null);
 
-    Assert.assertEquals(propertyProvider.getSpec("domu-12-31-39-14-ee-b3.compute-1.internal:50060"), streamProvider.getLastSpec());
+    Assert.assertEquals(propertyProvider.getSpec("domu-12-31-39-14-ee-b3.compute-1.internal", "50060"), streamProvider.getLastSpec());
 
     Assert.assertEquals(10, PropertyHelper.getProperties(resource).size());
     Assert.assertEquals(954466304, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryMax")));
@@ -177,7 +177,7 @@ public class JMXPropertyProviderTest {
 
     propertyProvider.populateResources(Collections.singleton(resource), request, null);
 
-    Assert.assertEquals(propertyProvider.getSpec("domu-12-31-39-14-ee-b3.compute-1.internal:60010"), streamProvider.getLastSpec());
+    Assert.assertEquals(propertyProvider.getSpec("domu-12-31-39-14-ee-b3.compute-1.internal", "60010"), streamProvider.getLastSpec());
 
     Assert.assertEquals(7, PropertyHelper.getProperties(resource).size());
     Assert.assertEquals(1069416448, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryMax")));
@@ -189,7 +189,45 @@ public class JMXPropertyProviderTest {
     Assert.assertNull(resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "gcCount")));
   }
 
+  @Test
+  public void testGetResourcesWithUnknownPort() throws Exception {
+    TestStreamProvider  streamProvider = new TestStreamProvider();
+    TestJMXHostProvider hostProvider = new TestJMXHostProvider(true);
+
+    JMXPropertyProvider propertyProvider = new JMXPropertyProvider(
+        PropertyHelper.getJMXPropertyIds(Resource.Type.HostComponent),
+        streamProvider,
+        hostProvider, PropertyHelper.getPropertyId("HostRoles", "cluster_name"), PropertyHelper.getPropertyId("HostRoles", "host_name"), PropertyHelper.getPropertyId("HostRoles", "component_name"));
+
+    // namenode
+    Resource resource = new ResourceImpl(Resource.Type.HostComponent);
+
+    resource.setProperty(HOST_COMPONENT_HOST_NAME_PROPERTY_ID, "domu-12-31-39-0e-34-e1.compute-1.internal");
+    resource.setProperty(HOST_COMPONENT_COMPONENT_NAME_PROPERTY_ID, "NAMENODE");
+
+    // request with an empty set should get all supported properties
+    Request request = PropertyHelper.getReadRequest(Collections.<String>emptySet());
+
+    Assert.assertEquals(1, propertyProvider.populateResources(Collections.singleton(resource), request, null).size());
+
+    Assert.assertEquals(propertyProvider.getSpec("domu-12-31-39-0e-34-e1.compute-1.internal", "50070"), streamProvider.getLastSpec());
+
+    // see test/resources/hdfs_namenode_jmx.json for values
+    Assert.assertEquals(13670605,  resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/rpc", "ReceivedBytes")));
+    Assert.assertEquals(28,      resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/dfs/namenode", "CreateFileOps")));
+    Assert.assertEquals(1006632960, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryMax")));
+    Assert.assertEquals(473433016, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "HeapMemoryUsed")));
+    Assert.assertEquals(136314880, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryMax")));
+    Assert.assertEquals(23634400, resource.getPropertyValue(PropertyHelper.getPropertyId("metrics/jvm", "NonHeapMemoryUsed")));
+  }
+
   private static class TestJMXHostProvider implements JMXHostProvider {
+    private final boolean unknownPort;
+
+    private TestJMXHostProvider(boolean unknownPort) {
+      this.unknownPort = unknownPort;
+    }
+
     @Override
     public String getHostName(String clusterName, String componentName) {
       return null;
@@ -198,6 +236,10 @@ public class JMXPropertyProviderTest {
     @Override
     public String getPort(String clusterName, String componentName) throws
       SystemException {
+
+      if (unknownPort) {
+        return null;
+      }
       if (componentName.equals("NAMENODE"))
         return "50070";
       else if (componentName.equals("DATANODE"))