You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kafka.apache.org by sh...@apache.org on 2023/05/16 02:31:46 UTC

[kafka] branch trunk updated: KAFKA-14997: Fix JmxToolTest failing on CI server (#13720)

This is an automated email from the ASF dual-hosted git repository.

showuon pushed a commit to branch trunk
in repository https://gitbox.apache.org/repos/asf/kafka.git


The following commit(s) were added to refs/heads/trunk by this push:
     new ac9d11b426f KAFKA-14997: Fix JmxToolTest failing on CI server (#13720)
ac9d11b426f is described below

commit ac9d11b426f755a97acbda259928131cde8c1fec
Author: Federico Valeri <fe...@gmail.com>
AuthorDate: Tue May 16 04:31:32 2023 +0200

    KAFKA-14997: Fix JmxToolTest failing on CI server (#13720)
    
    This test was reported as flaky on CI server.
    
    When connecting to a multi-homed machine using RMI, the wrong address may be returned by the RMI registry to the client, causing the connection to the RMI server to timeout.
    
    This change explicitly set the hostname returned to the the clients in the remote stub object.
    
    Reviewers: Luke Chen <sh...@gmail.com>, vamossagar12 <sa...@gmail.com>, Kamal Chandraprakash <ka...@gmail.com>, hudeqi <16...@bjtu.edu.cn>, Christo Lolov <ch...@gmail.com>
---
 .../java/org/apache/kafka/tools/JmxToolTest.java   | 33 +++++++++++-----------
 1 file changed, 16 insertions(+), 17 deletions(-)

diff --git a/tools/src/test/java/org/apache/kafka/tools/JmxToolTest.java b/tools/src/test/java/org/apache/kafka/tools/JmxToolTest.java
index 674599edccb..f936d3d094e 100644
--- a/tools/src/test/java/org/apache/kafka/tools/JmxToolTest.java
+++ b/tools/src/test/java/org/apache/kafka/tools/JmxToolTest.java
@@ -40,20 +40,33 @@ import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
 
+import static java.lang.String.format;
 import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
 public class JmxToolTest {
     private final ToolsTestUtils.MockExitProcedure exitProcedure = new ToolsTestUtils.MockExitProcedure();
-
     private static JMXConnectorServer jmxAgent;
     private static String jmxUrl;
 
     @BeforeAll
     public static void beforeAll() throws Exception {
         int port = findRandomOpenPortOnAllLocalInterfaces();
-        jmxAgent = startJmxAgent(port);
-        jmxUrl = String.format("service:jmx:rmi:///jndi/rmi://:%d/jmxrmi", port);
+        jmxUrl = format("service:jmx:rmi:///jndi/rmi://:%d/jmxrmi", port);
+        // explicitly set the hostname returned to the the clients in the remote stub object
+        // when connecting to a multi-homed machine using RMI, the wrong address may be returned
+        // by the RMI registry to the client, causing the connection to the RMI server to timeout
+        System.setProperty("java.rmi.server.hostname", "localhost");
+        LocateRegistry.createRegistry(port);
+        Map<String, Object> env = new HashMap<>();
+        env.put("com.sun.management.jmxremote.authenticate", "false");
+        env.put("com.sun.management.jmxremote.ssl", "false");
+        JMXServiceURL url = new JMXServiceURL(jmxUrl);
+        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
+        server.registerMBean(new Metrics(),
+            new ObjectName("kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSec"));
+        jmxAgent = JMXConnectorServerFactory.newJMXConnectorServer(url, env, server);
+        jmxAgent.start();
     }
 
     @AfterAll
@@ -324,20 +337,6 @@ public class JmxToolTest {
         assertTrue(validDateFormat(dateFormat, csv.get("time")));
     }
 
-    private static JMXConnectorServer startJmxAgent(int port) throws Exception {
-        LocateRegistry.createRegistry(port);
-        Map<String, Object> env = new HashMap<>();
-        env.put("com.sun.management.jmxremote.authenticate", "false");
-        env.put("com.sun.management.jmxremote.ssl", "false");
-        JMXServiceURL url = new JMXServiceURL(String.format("service:jmx:rmi:///jndi/rmi://:%d/jmxrmi", port));
-        MBeanServer server = ManagementFactory.getPlatformMBeanServer();
-        server.registerMBean(new Metrics(),
-            new ObjectName("kafka.server:type=BrokerTopicMetrics,name=MessagesInPerSec"));
-        JMXConnectorServer agent = JMXConnectorServerFactory.newJMXConnectorServer(url, env, server);
-        agent.start();
-        return agent;
-    }
-
     private static int findRandomOpenPortOnAllLocalInterfaces() throws Exception {
         try (ServerSocket socket = new ServerSocket(0)) {
             return socket.getLocalPort();