Skip to content

Commit

Permalink
Ya memory improvement: queryMBeansqueryNames
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
yannmh committed Aug 25, 2015
1 parent 3044e39 commit 0a586bd
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 33 deletions.
6 changes: 3 additions & 3 deletions src/main/java/org/datadog/jmxfetch/Connection.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,10 @@ public MBeanAttributeInfo[] getAttributesForBean(ObjectName bean_name)
return mbs.getMBeanInfo(bean_name).getAttributes();
}

public Set<ObjectInstance> queryMBeans(ObjectName name) throws IOException {
public Set<ObjectName> 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 {
Expand Down
16 changes: 7 additions & 9 deletions src/main/java/org/datadog/jmxfetch/Instance.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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<ObjectInstance> beans;
private Set<ObjectName> beans;
private LinkedList<String> beanScopes;
private LinkedList<Configuration> configurationList = new LinkedList<Configuration>();
private LinkedList<JMXAttribute> matchingAttributes;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -271,17 +269,17 @@ public LinkedList<String> getBeansScopes(){
}

private void refreshBeansList() throws IOException {
this.beans = new HashSet<ObjectInstance>();
this.beans = new HashSet<ObjectName>();
try {
LinkedList<String> 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();
}
Expand Down
29 changes: 14 additions & 15 deletions src/main/java/org/datadog/jmxfetch/JMXAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -31,29 +31,28 @@ 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<String, String> beanParameters;
private String attributeName;
private LinkedHashMap<Object, Object> valueConversions;
protected String[] tags;
private Configuration matchingConf;
private LinkedList<String> defaultTagsList;

JMXAttribute(MBeanAttributeInfo attribute, ObjectInstance jmxInstance, String instanceName,
JMXAttribute(MBeanAttributeInfo attribute, ObjectName beanName, String instanceName,
Connection connection, HashMap<String, String> 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<String> beanParametersList = getBeanParametersList(instanceName, domain, beanParameters, instanceTags);
Expand Down Expand Up @@ -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();
}
Expand All @@ -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) {
Expand Down Expand Up @@ -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;
}
}
Expand All @@ -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;
}

Expand Down Expand Up @@ -260,7 +259,7 @@ private boolean excludeMatchBeanName(Configuration conf) {
Filter exclude = conf.getExclude();
ArrayList<String> beanNames = exclude.getBeanNames();

if(beanNames.contains(beanName)){
if(beanNames.contains(beanStringName)){
return true;
}

Expand Down Expand Up @@ -349,7 +348,7 @@ protected String[] getTags() {
}

String getBeanName() {
return beanName;
return beanStringName;
}

String getAttributeName() {
Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/datadog/jmxfetch/JMXComplexAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -21,9 +21,9 @@ public class JMXComplexAttribute extends JMXAttribute {

private HashMap<String, HashMap<String, Object>> subAttributeList;

public JMXComplexAttribute(MBeanAttributeInfo attribute, ObjectInstance instance, String instanceName,
public JMXComplexAttribute(MBeanAttributeInfo attribute, ObjectName beanName, String instanceName,
Connection connection, HashMap<String, String> instanceTags) {
super(attribute, instance, instanceName, connection, instanceTags);
super(attribute, beanName, instanceName, connection, instanceTags);
this.subAttributeList = new HashMap<String, HashMap<String, Object>>();
}

Expand Down
6 changes: 3 additions & 3 deletions src/main/java/org/datadog/jmxfetch/JMXSimpleAttribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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<String, String> instanceTags) {
super(attribute, instance, instanceName, connection, instanceTags);
super(attribute, beanName, instanceName, connection, instanceTags);
}

@Override
Expand Down

0 comments on commit 0a586bd

Please sign in to comment.