From f768a6011af5c9027d6962a0e386e903f0a76226 Mon Sep 17 00:00:00 2001 From: Hippolyte HENRY Date: Thu, 23 Feb 2017 10:23:29 -0500 Subject: [PATCH 1/4] Store connection under the key instanceName in ConnectionManager Change the key under which a connection is stored in the ConnectionManager to avoid shared connections thus preventing entering a 'force new connection' loop. --- src/main/java/org/datadog/jmxfetch/ConnectionManager.java | 4 ++-- src/main/java/org/datadog/jmxfetch/Instance.java | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/ConnectionManager.java b/src/main/java/org/datadog/jmxfetch/ConnectionManager.java index 02ec90dd6..feec5aff2 100644 --- a/src/main/java/org/datadog/jmxfetch/ConnectionManager.java +++ b/src/main/java/org/datadog/jmxfetch/ConnectionManager.java @@ -52,8 +52,8 @@ private Connection createConnection(LinkedHashMap connectionPara } - public Connection getConnection(LinkedHashMap connectionParams, boolean forceNewConnection) throws IOException { - String key = generateKey(connectionParams); + public Connection getConnection(LinkedHashMap connectionParams, boolean forceNewConnection, String instanceName) throws IOException { + String key = instanceName; Connection existingConnection = cache.get(key); if (existingConnection == null || !existingConnection.isAlive()) { LOGGER.info("Connection closed or does not exist. Creating a new connection!"); diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index 0f84e32fe..67f448718 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -127,7 +127,7 @@ public Instance(LinkedHashMap yamlInstance, LinkedHashMap Date: Thu, 23 Feb 2017 16:21:09 -0500 Subject: [PATCH 2/4] Append the instance name to the old key instead of replacing --- src/main/java/org/datadog/jmxfetch/ConnectionManager.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/org/datadog/jmxfetch/ConnectionManager.java b/src/main/java/org/datadog/jmxfetch/ConnectionManager.java index feec5aff2..c1db2997b 100644 --- a/src/main/java/org/datadog/jmxfetch/ConnectionManager.java +++ b/src/main/java/org/datadog/jmxfetch/ConnectionManager.java @@ -53,7 +53,7 @@ private Connection createConnection(LinkedHashMap connectionPara } public Connection getConnection(LinkedHashMap connectionParams, boolean forceNewConnection, String instanceName) throws IOException { - String key = instanceName; + String key = generateKey(connectionParams) + ":" + instanceName; Connection existingConnection = cache.get(key); if (existingConnection == null || !existingConnection.isAlive()) { LOGGER.info("Connection closed or does not exist. Creating a new connection!"); From 0b880a717e14f7abe4aefa9ac75258987fb3b22f Mon Sep 17 00:00:00 2001 From: Hippolyte HENRY Date: Fri, 24 Feb 2017 11:22:35 -0500 Subject: [PATCH 3/4] Remove storage of connections in the connection manager --- .../datadog/jmxfetch/ConnectionManager.java | 32 ++++--------------- .../java/org/datadog/jmxfetch/Instance.java | 2 +- 2 files changed, 8 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/datadog/jmxfetch/ConnectionManager.java b/src/main/java/org/datadog/jmxfetch/ConnectionManager.java index c1db2997b..140f3bd9a 100644 --- a/src/main/java/org/datadog/jmxfetch/ConnectionManager.java +++ b/src/main/java/org/datadog/jmxfetch/ConnectionManager.java @@ -14,11 +14,8 @@ public class ConnectionManager { private final static Logger LOGGER = Logger.getLogger(ConnectionManager.class.getName()); public static final String PROCESS_NAME_REGEX = "process_name_regex"; private static ConnectionManager connectionManager = null; - private HashMap cache; - private ConnectionManager() { - cache = new HashMap(); - } + private ConnectionManager() {} public static ConnectionManager getInstance() { if (connectionManager == null) { @@ -27,15 +24,6 @@ public static ConnectionManager getInstance() { return connectionManager; } - private static String generateKey(LinkedHashMap connectionParams) { - if (connectionParams.get(PROCESS_NAME_REGEX) != null) { - return (String) connectionParams.get(PROCESS_NAME_REGEX); - } else if (connectionParams.get("jmx_url") != null) { - return (String) connectionParams.get("jmx_url"); - } - return connectionParams.get("host") + ":" + connectionParams.get("port") + ":" + connectionParams.get("user"); - } - private Connection createConnection(LinkedHashMap connectionParams) throws IOException { if (connectionParams.get(PROCESS_NAME_REGEX) != null) { try { @@ -52,22 +40,16 @@ private Connection createConnection(LinkedHashMap connectionPara } - public Connection getConnection(LinkedHashMap connectionParams, boolean forceNewConnection, String instanceName) throws IOException { - String key = generateKey(connectionParams) + ":" + instanceName; - Connection existingConnection = cache.get(key); + public Connection getConnection(LinkedHashMap connectionParams, boolean forceNewConnection, Connection existingConnection) throws IOException { if (existingConnection == null || !existingConnection.isAlive()) { LOGGER.info("Connection closed or does not exist. Creating a new connection!"); - cache.put(key, createConnection(connectionParams)); - } else { - if (forceNewConnection) { + return createConnection(connectionParams); + } else if (forceNewConnection) { LOGGER.info("Forcing the creation of a new connection"); - cache.get(key).closeConnector(); - cache.put(key, createConnection(connectionParams)); - } else { - LOGGER.info("Connection already exists for key: " + key + " . Using it..."); - } + existingConnection.closeConnector(); + return createConnection(connectionParams); } - return cache.get(key); + return existingConnection; } } \ No newline at end of file diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index 67f448718..5977cdcfe 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -127,7 +127,7 @@ public Instance(LinkedHashMap yamlInstance, LinkedHashMap Date: Fri, 24 Feb 2017 12:10:44 -0500 Subject: [PATCH 4/4] Change ConnectionManager into ConnectionFactory --- .../datadog/jmxfetch/ConnectionFactory.java | 33 +++++++++++ .../datadog/jmxfetch/ConnectionManager.java | 55 ------------------- .../java/org/datadog/jmxfetch/Instance.java | 14 ++++- 3 files changed, 46 insertions(+), 56 deletions(-) create mode 100644 src/main/java/org/datadog/jmxfetch/ConnectionFactory.java delete mode 100644 src/main/java/org/datadog/jmxfetch/ConnectionManager.java diff --git a/src/main/java/org/datadog/jmxfetch/ConnectionFactory.java b/src/main/java/org/datadog/jmxfetch/ConnectionFactory.java new file mode 100644 index 000000000..f76cb1490 --- /dev/null +++ b/src/main/java/org/datadog/jmxfetch/ConnectionFactory.java @@ -0,0 +1,33 @@ +package org.datadog.jmxfetch; + +import java.io.IOException; +import java.util.LinkedHashMap; + +import org.apache.log4j.Logger; + + +/** + * Singleton used to create connections to the MBeanServer + */ +public class ConnectionFactory { + private final static Logger LOGGER = Logger.getLogger(ConnectionFactory.class.getName()); + public static final String PROCESS_NAME_REGEX = "process_name_regex"; + private static ConnectionFactory connectionFactory = null; + + public static Connection createConnection(LinkedHashMap connectionParams) throws IOException { + if (connectionParams.get(PROCESS_NAME_REGEX) != null) { + try { + Class.forName( "com.sun.tools.attach.AttachNotSupportedException" ); + } catch (ClassNotFoundException e) { + throw new IOException("Unable to find tools.jar. Are you using a JDK and did you set the pass to tools.jar ?"); + } + LOGGER.info("Connecting using Attach API"); + return new AttachApiConnection(connectionParams); + + } + LOGGER.info("Connecting using JMX Remote"); + return new RemoteConnection(connectionParams); + + } + +} \ No newline at end of file diff --git a/src/main/java/org/datadog/jmxfetch/ConnectionManager.java b/src/main/java/org/datadog/jmxfetch/ConnectionManager.java deleted file mode 100644 index 140f3bd9a..000000000 --- a/src/main/java/org/datadog/jmxfetch/ConnectionManager.java +++ /dev/null @@ -1,55 +0,0 @@ -package org.datadog.jmxfetch; - -import java.io.IOException; -import java.util.HashMap; -import java.util.LinkedHashMap; - -import org.apache.log4j.Logger; - - -/** - * Singleton used to share connections across instances in case you have multiple instances configured to the save MBeanServer (e.g. Solr and Tomcat) - */ -public class ConnectionManager { - private final static Logger LOGGER = Logger.getLogger(ConnectionManager.class.getName()); - public static final String PROCESS_NAME_REGEX = "process_name_regex"; - private static ConnectionManager connectionManager = null; - - private ConnectionManager() {} - - public static ConnectionManager getInstance() { - if (connectionManager == null) { - connectionManager = new ConnectionManager(); - } - return connectionManager; - } - - private Connection createConnection(LinkedHashMap connectionParams) throws IOException { - if (connectionParams.get(PROCESS_NAME_REGEX) != null) { - try { - Class.forName( "com.sun.tools.attach.AttachNotSupportedException" ); - } catch (ClassNotFoundException e) { - throw new IOException("Unable to find tools.jar. Are you using a JDK and did you set the pass to tools.jar ?"); - } - LOGGER.info("Connecting using Attach API"); - return new AttachApiConnection(connectionParams); - - } - LOGGER.info("Connecting using JMX Remote"); - return new RemoteConnection(connectionParams); - - } - - public Connection getConnection(LinkedHashMap connectionParams, boolean forceNewConnection, Connection existingConnection) throws IOException { - if (existingConnection == null || !existingConnection.isAlive()) { - LOGGER.info("Connection closed or does not exist. Creating a new connection!"); - return createConnection(connectionParams); - } else if (forceNewConnection) { - LOGGER.info("Forcing the creation of a new connection"); - existingConnection.closeConnector(); - return createConnection(connectionParams); - } - return existingConnection; - } - -} \ No newline at end of file diff --git a/src/main/java/org/datadog/jmxfetch/Instance.java b/src/main/java/org/datadog/jmxfetch/Instance.java index 5977cdcfe..628f99e4d 100644 --- a/src/main/java/org/datadog/jmxfetch/Instance.java +++ b/src/main/java/org/datadog/jmxfetch/Instance.java @@ -125,9 +125,21 @@ public Instance(LinkedHashMap yamlInstance, LinkedHashMap) new YamlParser(this.getClass().getResourceAsStream("/jmx-2.yaml")).getParsedYaml())); } + public Connection getConnection(LinkedHashMap connectionParams, boolean forceNewConnection) throws IOException { + if (connection == null || !connection.isAlive()) { + LOGGER.info("Connection closed or does not exist. Creating a new connection!"); + return ConnectionFactory.createConnection(connectionParams); + } else if (forceNewConnection) { + LOGGER.info("Forcing the creation of a new connection"); + connection.closeConnector(); + return ConnectionFactory.createConnection(connectionParams); + } + return connection; + } + public void init(boolean forceNewConnection) throws IOException, FailedLoginException, SecurityException { LOGGER.info("Trying to connect to JMX Server at " + this.toString()); - connection = ConnectionManager.getInstance().getConnection(yaml, forceNewConnection, connection); + connection = getConnection(yaml, forceNewConnection); LOGGER.info("Connected to JMX Server at " + this.toString()); this.refreshBeansList(); this.getMatchingAttributes();