Browse Source

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 <showuon@gmail.com>, vamossagar12 <sagarmeansocean@gmail.com>, Kamal Chandraprakash <kamal.chandraprakash@gmail.com>, hudeqi <16120374@bjtu.edu.cn>, Christo Lolov <christololov@gmail.com>
pull/13703/head
Federico Valeri 2 years ago committed by GitHub
parent
commit
ac9d11b426
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 33
      tools/src/test/java/org/apache/kafka/tools/JmxToolTest.java

33
tools/src/test/java/org/apache/kafka/tools/JmxToolTest.java

@ -40,20 +40,33 @@ import java.util.Arrays; @@ -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 { @@ -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();

Loading…
Cancel
Save