From 0a586bdafd7c23aa46151d752486d6b0bc317caa Mon Sep 17 00:00:00 2001 From: Yann Mahe Date: Tue, 25 Aug 2015 14:26:34 -0400 Subject: [PATCH] =?UTF-8?q?Ya=20memory=20improvement:=20`queryMBeans`?= =?UTF-8?q?=E2=86=92`queryNames`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit JMXFetch was unnecessarily querying full bean objects: query bean names instead (`queryMBeans`→`queryNames`). As it queries one bean attributes at a time (`getAttributesForBean`), extra memory is saved. --- .../java/org/datadog/jmxfetch/Connection.java | 6 ++-- .../java/org/datadog/jmxfetch/Instance.java | 16 +++++----- .../org/datadog/jmxfetch/JMXAttribute.java | 29 +++++++++---------- .../datadog/jmxfetch/JMXComplexAttribute.java | 6 ++-- .../datadog/jmxfetch/JMXSimpleAttribute.java | 6 ++-- 5 files changed, 30 insertions(+), 33 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/Connection.java b/src/main/java/org/datadog/jmxfetch/Connection.java index ed2141d0e..bfab57be1 100644 --- a/src/main/java/org/datadog/jmxfetch/Connection.java +++ b/src/main/java/org/datadog/jmxfetch/Connection.java @@ -48,10 +48,10 @@ public MBeanAttributeInfo[] getAttributesForBean(ObjectName bean_name) return mbs.getMBeanInfo(bean_name).getAttributes(); } - public Set queryMBeans(ObjectName name) throws IOException { + public Set queryNames(ObjectName name) throws IOException { String scope = (name != null) ? name.toString() : "*:*"; - LOGGER.debug("Querying beans on scope: " + scope); - return mbs.queryMBeans(name, null); + LOGGER.debug("Querying bean names on scope: " + scope); + return mbs.queryNames(name, null); } protected void createConnection() throws IOException { diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index c6d05ce0c..46594f14a 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -12,7 +12,6 @@ import java.util.Set; import javax.management.MBeanAttributeInfo; -import javax.management.ObjectInstance; import javax.management.ObjectName; import javax.security.auth.login.FailedLoginException; @@ -29,7 +28,7 @@ public class Instance { private final static int MAX_RETURNED_METRICS = 350; private final static int DEFAULT_REFRESH_BEANS_PERIOD = 600; - private Set beans; + private Set beans; private LinkedList beanScopes; private LinkedList configurationList = new LinkedList(); private LinkedList matchingAttributes; @@ -181,14 +180,13 @@ private void getMatchingAttributes() { reporter.displayInstanceName(this); } - for (ObjectInstance bean : beans) { + for (ObjectName beanName : beans) { if (limitReached) { LOGGER.debug("Limit reached"); if (action.equals(AppConfig.ACTION_COLLECT)) { break; } } - ObjectName beanName = bean.getObjectName(); MBeanAttributeInfo[] attributeInfos; try { @@ -218,10 +216,10 @@ private void getMatchingAttributes() { String attributeType = attributeInfo.getType(); if (SIMPLE_TYPES.contains(attributeType)) { LOGGER.debug("Attribute: " + beanName + " : " + attributeInfo + " has attributeInfo simple type"); - jmxAttribute = new JMXSimpleAttribute(attributeInfo, bean, instanceName, connection, tags); + jmxAttribute = new JMXSimpleAttribute(attributeInfo, beanName, instanceName, connection, tags); } else if (COMPOSED_TYPES.contains(attributeType)) { LOGGER.debug("Attribute: " + beanName + " : " + attributeInfo + " has attributeInfo complex type"); - jmxAttribute = new JMXComplexAttribute(attributeInfo, bean, instanceName, connection, tags); + jmxAttribute = new JMXComplexAttribute(attributeInfo, beanName, instanceName, connection, tags); } else { try { LOGGER.debug("Attribute: " + beanName + " : " + attributeInfo + " has an unsupported type: " + attributeType); @@ -271,17 +269,17 @@ public LinkedList getBeansScopes(){ } private void refreshBeansList() throws IOException { - this.beans = new HashSet(); + this.beans = new HashSet(); try { LinkedList beanScopes = getBeansScopes(); for (String scope : beanScopes) { ObjectName name = new ObjectName(scope); - this.beans.addAll(connection.queryMBeans(name)); + this.beans.addAll(connection.queryNames(name)); } } catch (Exception e) { LOGGER.error("Unable to compute a common bean scope, querying all beans as a fallback", e); - this.beans = connection.queryMBeans(null); + this.beans = connection.queryNames(null); } this.lastRefreshTime = System.currentTimeMillis(); } diff --git a/src/main/java/org/datadog/jmxfetch/JMXAttribute.java b/src/main/java/org/datadog/jmxfetch/JMXAttribute.java index 961f2ff59..6db723809 100644 --- a/src/main/java/org/datadog/jmxfetch/JMXAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JMXAttribute.java @@ -16,7 +16,7 @@ import javax.management.InstanceNotFoundException; import javax.management.MBeanAttributeInfo; import javax.management.MBeanException; -import javax.management.ObjectInstance; +import javax.management.ObjectName; import javax.management.ReflectionException; import org.apache.log4j.Logger; @@ -31,9 +31,9 @@ public abstract class JMXAttribute { private static final String DOT_UNDERSCORE = "_*\\._*"; private MBeanAttributeInfo attribute; private Connection connection; - private ObjectInstance jmxInstance; + private ObjectName beanName; private String domain; - private String beanName; + private String beanStringName; private HashMap beanParameters; private String attributeName; private LinkedHashMap valueConversions; @@ -41,19 +41,18 @@ public abstract class JMXAttribute { private Configuration matchingConf; private LinkedList defaultTagsList; - JMXAttribute(MBeanAttributeInfo attribute, ObjectInstance jmxInstance, String instanceName, + JMXAttribute(MBeanAttributeInfo attribute, ObjectName beanName, String instanceName, Connection connection, HashMap instanceTags) { this.attribute = attribute; - this.jmxInstance = jmxInstance; + this.beanName = beanName; this.matchingConf = null; this.connection = connection; - - this.beanName = jmxInstance.getObjectName().toString(); this.attributeName = attribute.getName(); + this.beanStringName = beanName.toString(); // A bean name is formatted like that: org.apache.cassandra.db:type=Caches,keyspace=system,cache=HintsColumnFamilyKeyCache // i.e. : domain:bean_parameter1,bean_parameter2 - String[] splitBeanName = this.beanName.split(":"); + String[] splitBeanName = beanStringName.split(":"); String domain = splitBeanName[0]; String beanParameters = splitBeanName[1]; LinkedList beanParametersList = getBeanParametersList(instanceName, domain, beanParameters, instanceTags); @@ -120,7 +119,7 @@ static String convertMetricName(String metricName) { @Override public String toString() { - return "Bean name: " + beanName + + return "Bean name: " + beanStringName + " - Attribute name: " + attributeName + " - Attribute type: " + attribute.getType(); } @@ -139,13 +138,13 @@ public int getMetricsCount() { try { return this.getMetrics().size(); } catch (Exception e) { - LOGGER.warn("Unable to get metrics from " + beanName); + LOGGER.warn("Unable to get metrics from " + beanStringName); return 0; } } Object getJmxValue() throws AttributeNotFoundException, InstanceNotFoundException, MBeanException, ReflectionException, IOException { - return this.connection.getAttribute(this.jmxInstance.getObjectName(), this.attribute.getName()); + return this.connection.getAttribute(this.beanName, this.attribute.getName()); } boolean matchDomain(Configuration conf) { @@ -214,7 +213,7 @@ private boolean matchBeanRegex(Filter filter, boolean matchIfNoRegex) { } for (Pattern beanRegex : beanRegexes) { - if(beanRegex.matcher(beanName).matches()) { + if(beanRegex.matcher(beanStringName).matches()) { return true; } } @@ -226,7 +225,7 @@ private boolean matchBeanName(Configuration configuration) { boolean matchBeanAttr = true; Filter include = configuration.getInclude(); - if (!include.isEmptyBeanName() && !include.getBeanNames().contains(beanName)) { + if (!include.isEmptyBeanName() && !include.getBeanNames().contains(beanStringName)) { return false; } @@ -260,7 +259,7 @@ private boolean excludeMatchBeanName(Configuration conf) { Filter exclude = conf.getExclude(); ArrayList beanNames = exclude.getBeanNames(); - if(beanNames.contains(beanName)){ + if(beanNames.contains(beanStringName)){ return true; } @@ -349,7 +348,7 @@ protected String[] getTags() { } String getBeanName() { - return beanName; + return beanStringName; } String getAttributeName() { diff --git a/src/main/java/org/datadog/jmxfetch/JMXComplexAttribute.java b/src/main/java/org/datadog/jmxfetch/JMXComplexAttribute.java index 3a1ad325d..474e17623 100644 --- a/src/main/java/org/datadog/jmxfetch/JMXComplexAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JMXComplexAttribute.java @@ -12,7 +12,7 @@ import javax.management.InstanceNotFoundException; import javax.management.MBeanAttributeInfo; import javax.management.MBeanException; -import javax.management.ObjectInstance; +import javax.management.ObjectName; import javax.management.ReflectionException; import javax.management.openmbean.CompositeData; @@ -21,9 +21,9 @@ public class JMXComplexAttribute extends JMXAttribute { private HashMap> subAttributeList; - public JMXComplexAttribute(MBeanAttributeInfo attribute, ObjectInstance instance, String instanceName, + public JMXComplexAttribute(MBeanAttributeInfo attribute, ObjectName beanName, String instanceName, Connection connection, HashMap instanceTags) { - super(attribute, instance, instanceName, connection, instanceTags); + super(attribute, beanName, instanceName, connection, instanceTags); this.subAttributeList = new HashMap>(); } diff --git a/src/main/java/org/datadog/jmxfetch/JMXSimpleAttribute.java b/src/main/java/org/datadog/jmxfetch/JMXSimpleAttribute.java index 43a33ad3f..f2a0eef9a 100644 --- a/src/main/java/org/datadog/jmxfetch/JMXSimpleAttribute.java +++ b/src/main/java/org/datadog/jmxfetch/JMXSimpleAttribute.java @@ -10,7 +10,7 @@ import javax.management.InstanceNotFoundException; import javax.management.MBeanAttributeInfo; import javax.management.MBeanException; -import javax.management.ObjectInstance; +import javax.management.ObjectName; import javax.management.ReflectionException; @SuppressWarnings("unchecked") @@ -19,9 +19,9 @@ public class JMXSimpleAttribute extends JMXAttribute { private String alias; private String metricType; - public JMXSimpleAttribute(MBeanAttributeInfo attribute, ObjectInstance instance, String instanceName, + public JMXSimpleAttribute(MBeanAttributeInfo attribute, ObjectName beanName, String instanceName, Connection connection, HashMap instanceTags) { - super(attribute, instance, instanceName, connection, instanceTags); + super(attribute, beanName, instanceName, connection, instanceTags); } @Override