Properly format IPv6 addresses when logging JMX service URL

Patch by Sam Tunnicliffe; reviewed by Alex Petrov for CASSANDRA-12454


Project: http://git-wip-us.apache.org/repos/asf/cassandra/repo
Commit: http://git-wip-us.apache.org/repos/asf/cassandra/commit/bdd4a927
Tree: http://git-wip-us.apache.org/repos/asf/cassandra/tree/bdd4a927
Diff: http://git-wip-us.apache.org/repos/asf/cassandra/diff/bdd4a927

Branch: refs/heads/trunk
Commit: bdd4a927fa13e9beff3bdc342ea6fe762b9997dc
Parents: 20b085a
Author: Sam Tunnicliffe <[email protected]>
Authored: Wed Oct 26 17:34:50 2016 +0100
Committer: Sam Tunnicliffe <[email protected]>
Committed: Wed Oct 26 17:34:50 2016 +0100

----------------------------------------------------------------------
 CHANGES.txt                                     |  1 +
 .../apache/cassandra/utils/JMXServerUtils.java  | 25 ++++++++++++++++----
 2 files changed, 22 insertions(+), 4 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cassandra/blob/bdd4a927/CHANGES.txt
----------------------------------------------------------------------
diff --git a/CHANGES.txt b/CHANGES.txt
index 0f16a0e..847bae6 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -1,4 +1,5 @@
 3.10
+ * Properly format IPv6 addresses when logging JMX service URL 
(CASSANDRA-12454)
  * Optimize the vnode allocation for single replica per DC (CASSANDRA-12777)
  * Use non-token restrictions for bounds when token restrictions are 
overridden (CASSANDRA-12419)
  * Fix CQLSH auto completion for PER PARTITION LIMIT (CASSANDRA-12803)

http://git-wip-us.apache.org/repos/asf/cassandra/blob/bdd4a927/src/java/org/apache/cassandra/utils/JMXServerUtils.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/utils/JMXServerUtils.java 
b/src/java/org/apache/cassandra/utils/JMXServerUtils.java
index 84eb870..e78ed01 100644
--- a/src/java/org/apache/cassandra/utils/JMXServerUtils.java
+++ b/src/java/org/apache/cassandra/utils/JMXServerUtils.java
@@ -22,6 +22,7 @@ import java.io.IOException;
 import java.lang.management.ManagementFactory;
 import java.lang.reflect.InvocationHandler;
 import java.lang.reflect.Proxy;
+import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.rmi.*;
 import java.rmi.server.RMIClientSocketFactory;
@@ -45,7 +46,6 @@ import org.slf4j.LoggerFactory;
 import com.sun.jmx.remote.internal.RMIExporter;
 import com.sun.jmx.remote.security.JMXPluggableAuthenticator;
 import org.apache.cassandra.auth.jmx.AuthenticationProxy;
-import org.apache.cassandra.exceptions.ConfigurationException;
 import sun.rmi.registry.RegistryImpl;
 import sun.rmi.server.UnicastServerRef2;
 
@@ -64,7 +64,6 @@ public class JMXServerUtils
     {
         Map<String, Object> env = new HashMap<>();
 
-        String urlTemplate = 
"service:jmx:rmi://%1$s/jndi/rmi://%1$s:%2$d/jmxrmi";
         InetAddress serverAddress = null;
         if (local)
         {
@@ -91,7 +90,6 @@ public class JMXServerUtils
         // sun.rmi.dgc.server.gcInterval millis (default is 3600000ms/1 hour)
         env.put(RMIExporter.EXPORTER_ATTRIBUTE, new Exporter());
 
-        String url = String.format(urlTemplate, (serverAddress != null ? 
serverAddress.getHostAddress() : "0.0.0.0"), port);
 
         int rmiPort = 
Integer.getInteger("com.sun.management.jmxremote.rmi.port", 0);
         JMXConnectorServer jmxServer =
@@ -108,7 +106,7 @@ public class JMXServerUtils
         // use a custom Registry to avoid having to interact with it 
internally using the remoting interface
         configureRMIRegistry(port, env);
 
-        logger.info("Configured JMX server at: {}", url);
+        logJmxServiceUrl(serverAddress, port);
         return jmxServer;
     }
 
@@ -232,6 +230,25 @@ public class JMXServerUtils
         return env;
     }
 
+    private static void logJmxServiceUrl(InetAddress serverAddress, int port)
+    {
+        String urlTemplate = 
"service:jmx:rmi://%1$s/jndi/rmi://%1$s:%2$d/jmxrmi";
+        String hostName;
+        if (serverAddress == null)
+        {
+            hostName = FBUtilities.getBroadcastAddress() instanceof 
Inet6Address ? "[::]" : "0.0.0.0";
+        }
+        else
+        {
+            // hostnames based on IPv6 addresses must be wrapped in [ ]
+            hostName = serverAddress instanceof Inet6Address
+                       ? '[' + serverAddress.getHostAddress() + ']'
+                       : serverAddress.getHostAddress();
+        }
+        String url = String.format(urlTemplate, hostName, port);
+        logger.info("Configured JMX server at: {}", url);
+    }
+
     private static void logJmxSslConfig(SslRMIServerSocketFactory 
serverFactory)
     {
         logger.debug("JMX SSL configuration. { protocols: [{}], cipher_suites: 
[{}], require_client_auth: {} }",

Reply via email to