diff --git a/agents/drivers/rocketmq/src/main/java/com/dbx/agent/rocketmq/RocketMqAgent.java b/agents/drivers/rocketmq/src/main/java/com/dbx/agent/rocketmq/RocketMqAgent.java index 06c445712f..35e9e5a0ef 100644 --- a/agents/drivers/rocketmq/src/main/java/com/dbx/agent/rocketmq/RocketMqAgent.java +++ b/agents/drivers/rocketmq/src/main/java/com/dbx/agent/rocketmq/RocketMqAgent.java @@ -62,6 +62,9 @@ public final class RocketMqAgent { private static final Gson GSON = new GsonBuilder().serializeNulls().create(); private static final int DEFAULT_REQUEST_TIMEOUT_MS = 30_000; + private static final int DEFAULT_CONNECT_TIMEOUT_MS = 10_000; + private static final long MIN_ACL_PROBE_TIMEOUT_MS = 500; + private static final long MAX_ACL_PROBE_TIMEOUT_MS = 5_000; private static final int DEFAULT_LIST_LIMIT = 200; private static final int CONSUMER_GROUP_ENRICH_CONCURRENCY = 8; private static final int CONSUMER_GROUP_COLLECT_CONCURRENCY = 8; @@ -435,7 +438,7 @@ public final class RocketMqAgent { ) throws Exception { String clusterName = resolveClusterName(clusterInfo, conn); List> brokers = brokerNodes(clusterInfo); - boolean aclEnabled = probeAclSupport(admin, clusterInfo); + boolean aclEnabled = probeAclSupport(admin, clusterInfo, conn); Map result = new LinkedHashMap<>(); result.put("ok", true); @@ -3056,17 +3059,43 @@ public final class RocketMqAgent { }; } - private static boolean probeAclSupport(DefaultMQAdminExt admin, ClusterInfo clusterInfo) { + private static boolean probeAclSupport(DefaultMQAdminExt admin, ClusterInfo clusterInfo, JsonObject conn) { + return probeAclSupport(clusterInfo, conn, (brokerAddr, timeoutMs) -> { + admin.getDefaultMQAdminExtImpl() + .getMqClientInstance() + .getMQClientAPIImpl() + .getBrokerClusterAclInfo(brokerAddr, timeoutMs); + }); + } + + @FunctionalInterface + interface BrokerAclProbe { + void probe(String brokerAddr, long timeoutMs) throws Exception; + } + + static long aclProbeTimeoutMs(JsonObject conn) { + long connectTimeoutMs = Math.max( + 1_000L, + (long) intOrDefault(conn, "connect_timeout_ms", DEFAULT_CONNECT_TIMEOUT_MS) + ); + return Math.max( + MIN_ACL_PROBE_TIMEOUT_MS, + Math.min(MAX_ACL_PROBE_TIMEOUT_MS, connectTimeoutMs / 2) + ); + } + + static boolean probeAclSupport(ClusterInfo clusterInfo, JsonObject conn, BrokerAclProbe probe) { try { if (clusterInfo == null || clusterInfo.getBrokerAddrTable() == null) { return false; } + long timeoutMs = aclProbeTimeoutMs(conn); for (BrokerData broker : clusterInfo.getBrokerAddrTable().values()) { String brokerAddr = broker.selectBrokerAddr(); if (brokerAddr == null || brokerAddr.isBlank()) { continue; } - admin.examineBrokerClusterAclVersionInfo(brokerAddr); + probe.probe(remapBrokerAddrForClient(brokerAddr, conn), timeoutMs); return true; } } catch (Exception ignored) { diff --git a/agents/drivers/rocketmq/src/test/java/com/dbx/agent/rocketmq/RocketMqAgentTest.java b/agents/drivers/rocketmq/src/test/java/com/dbx/agent/rocketmq/RocketMqAgentTest.java index 5f55b8ea40..3ff6b81c24 100644 --- a/agents/drivers/rocketmq/src/test/java/com/dbx/agent/rocketmq/RocketMqAgentTest.java +++ b/agents/drivers/rocketmq/src/test/java/com/dbx/agent/rocketmq/RocketMqAgentTest.java @@ -213,6 +213,121 @@ class RocketMqAgentTest { assertEquals("published.example.com:10911", RocketMqAgent.remapBrokerAddrForClient("172.18.0.3:10911", conn)); } + @Test + void aclProbeTimeoutUsesConnectBudgetWithoutReducingOperationBudget() { + JsonObject conn = JsonParser.parseString(""" + {"connect_timeout_ms":10000,"request_timeout_ms":120000} + """).getAsJsonObject(); + assertEquals(5_000L, RocketMqAgent.aclProbeTimeoutMs(conn)); + assertEquals(120_000L, RocketMqAgent.operationBudgetMs(conn)); + + assertEquals(500L, RocketMqAgent.aclProbeTimeoutMs(JsonParser.parseString(""" + {"connect_timeout_ms":1000} + """).getAsJsonObject())); + assertEquals(5_000L, RocketMqAgent.aclProbeTimeoutMs(JsonParser.parseString(""" + {"connect_timeout_ms":120000} + """).getAsJsonObject())); + assertEquals(500L, RocketMqAgent.aclProbeTimeoutMs(JsonParser.parseString(""" + {"connect_timeout_ms":-2147483648} + """).getAsJsonObject())); + assertEquals(5_000L, RocketMqAgent.aclProbeTimeoutMs(JsonParser.parseString(""" + {"connect_timeout_ms":2147483647} + """).getAsJsonObject())); + } + + @Test + void probeAclSupportRemapsBrokerAndPassesIndependentTimeout() { + var clusterInfo = clusterInfoWithBrokers("172.18.0.3:10911"); + JsonObject conn = JsonParser.parseString(""" + { + "namesrv_addr":"127.0.0.1:9876", + "connect_timeout_ms":10000, + "request_timeout_ms":120000 + } + """).getAsJsonObject(); + List addresses = new ArrayList<>(); + List timeouts = new ArrayList<>(); + + assertTrue(RocketMqAgent.probeAclSupport(clusterInfo, conn, (address, timeoutMs) -> { + addresses.add(address); + timeouts.add(timeoutMs); + })); + assertEquals(List.of("127.0.0.1:10911"), addresses); + assertEquals(List.of(5_000L), timeouts); + } + + @Test + void probeAclSupportPreservesPublicExplicitAndSocksAddresses() { + List addresses = new ArrayList<>(); + RocketMqAgent.BrokerAclProbe capture = (address, timeoutMs) -> addresses.add(address); + + assertTrue(RocketMqAgent.probeAclSupport( + clusterInfoWithBrokers("broker.example.com:10911"), + JsonParser.parseString(""" + {"namesrv_addr":"ns.example.com:9876","connect_timeout_ms":10000} + """).getAsJsonObject(), + capture)); + assertTrue(RocketMqAgent.probeAclSupport( + clusterInfoWithBrokers("172.18.0.3:10911"), + JsonParser.parseString(""" + { + "namesrv_addr":"127.0.0.1:9876", + "broker_addr":"published.example.com:10911", + "connect_timeout_ms":10000 + } + """).getAsJsonObject(), + capture)); + assertTrue(RocketMqAgent.probeAclSupport( + clusterInfoWithBrokers("172.18.0.3:10911"), + JsonParser.parseString(""" + { + "namesrv_addr":"172.19.191.166:9876", + "socks_proxy":{"host":"127.0.0.1","port":41080}, + "connect_timeout_ms":10000 + } + """).getAsJsonObject(), + capture)); + + assertEquals(List.of( + "broker.example.com:10911", + "published.example.com:10911", + "172.18.0.3:10911" + ), addresses); + } + + @Test + void probeAclSupportReturnsFalseWithoutRetryOnTimeoutOrError() { + var clusterInfo = clusterInfoWithBrokers("broker-a:10911", "broker-b:10911"); + JsonObject conn = JsonParser.parseString(""" + {"namesrv_addr":"ns.example.com:9876","connect_timeout_ms":10000} + """).getAsJsonObject(); + AtomicInteger calls = new AtomicInteger(); + + assertFalse(RocketMqAgent.probeAclSupport(clusterInfo, conn, (address, timeoutMs) -> { + calls.incrementAndGet(); + throw new java.util.concurrent.TimeoutException("blackholed broker"); + })); + assertEquals(1, calls.get()); + + assertFalse(RocketMqAgent.probeAclSupport(clusterInfo, conn, (address, timeoutMs) -> { + throw new IllegalStateException("ACL unavailable"); + })); + } + + @Test + void probeAclSupportReturnsFalseForMissingBrokersWithoutNetworkCall() { + AtomicInteger calls = new AtomicInteger(); + RocketMqAgent.BrokerAclProbe probe = (address, timeoutMs) -> calls.incrementAndGet(); + JsonObject conn = JsonParser.parseString(""" + {"namesrv_addr":"ns.example.com:9876","connect_timeout_ms":10000} + """).getAsJsonObject(); + + assertFalse(RocketMqAgent.probeAclSupport(null, conn, probe)); + assertFalse(RocketMqAgent.probeAclSupport( + new org.apache.rocketmq.remoting.protocol.body.ClusterInfo(), conn, probe)); + assertEquals(0, calls.get()); + } + @Test void parseHostFromSocketAddressHandlesIpv6AndPorts() { assertEquals("127.0.0.1", RocketMqAgent.parseHostFromSocketAddress("127.0.0.1:9876")); @@ -1141,4 +1256,17 @@ class RocketMqAgentTest { data.setBrokerAddrs(addrs); return data; } + + private static org.apache.rocketmq.remoting.protocol.body.ClusterInfo clusterInfoWithBrokers( + String... masterAddrs) { + org.apache.rocketmq.remoting.protocol.body.ClusterInfo clusterInfo = + new org.apache.rocketmq.remoting.protocol.body.ClusterInfo(); + HashMap table = new HashMap<>(); + for (int index = 0; index < masterAddrs.length; index++) { + String name = "broker-" + index; + table.put(name, brokerData(name, masterAddrs[index])); + } + clusterInfo.setBrokerAddrTable(table); + return clusterInfo; + } }