From 72a72072831adc3305a4e98853ed967fb57807a0 Mon Sep 17 00:00:00 2001 From: miracle Date: Thu, 2 Jul 2026 12:58:33 +0800 Subject: [PATCH] fix(kafka): improve mq admin compatibility --- .../java/com/dbx/agent/kafka/KafkaAgent.java | 170 +++++++++---- .../connection/ConnectionDialog.vue | 222 +++++++++++++++- .../src/components/mq/MqAdminConsole.vue | 11 +- .../src/components/mq/PoliciesPanel.vue | 8 +- .../components/mq/ProducerConsumerPanel.vue | 98 ++++++-- .../src/components/mq/SendMessagePanel.vue | 237 +++++++++++++++++- .../desktop/src/components/mq/TopicsPanel.vue | 24 +- .../desktop/src/lib/agentDriverInstallHint.ts | 2 +- apps/desktop/src/lib/mq-http.ts | 5 +- apps/desktop/src/lib/mq-tauri.ts | 5 +- apps/desktop/src/types/mq.ts | 5 + crates/dbx-core/src/agent_manager.rs | 32 ++- crates/dbx-core/src/agent_service.rs | 154 +++++++++--- crates/dbx-core/src/connection.rs | 13 +- crates/dbx-core/src/mq/adapters/kafka.rs | 36 ++- crates/dbx-core/src/mq/adapters/pulsar.rs | 8 +- crates/dbx-core/src/mq/mod.rs | 34 ++- crates/dbx-core/src/mq/port.rs | 8 +- crates/dbx-core/src/mq/service.rs | 25 +- crates/dbx-core/src/mq/types.rs | 11 + crates/dbx-core/tests/agent_service.rs | 67 ++++- crates/dbx-web/src/routes/mq.rs | 16 +- src-tauri/src/commands/connection.rs | 26 +- src-tauri/src/commands/mq_cmd.rs | 3 +- 24 files changed, 1050 insertions(+), 170 deletions(-) diff --git a/agents/drivers/kafka/src/main/java/com/dbx/agent/kafka/KafkaAgent.java b/agents/drivers/kafka/src/main/java/com/dbx/agent/kafka/KafkaAgent.java index cd81b902df..bcb13bdd79 100644 --- a/agents/drivers/kafka/src/main/java/com/dbx/agent/kafka/KafkaAgent.java +++ b/agents/drivers/kafka/src/main/java/com/dbx/agent/kafka/KafkaAgent.java @@ -87,7 +87,7 @@ public final class KafkaAgent { } catch (Exception e) { JsonObject error = new JsonObject(); error.addProperty("code", -1); - error.addProperty("message", e.getMessage() == null ? "Unknown error" : e.getMessage()); + error.addProperty("message", normalizeErrorMessage(e)); response.add("error", error); } return GSON.toJson(response); @@ -255,9 +255,10 @@ public final class KafkaAgent { if (securityProtocol.isBlank()) { securityProtocol = stringOrEmpty(conn, "securityProtocol"); } - if (!securityProtocol.isBlank()) { - props.put("security.protocol", securityProtocol); + if (securityProtocol.isBlank()) { + securityProtocol = "PLAINTEXT"; } + props.put("security.protocol", securityProtocol); String saslMechanism = stringOrEmpty(conn, "sasl_mechanism"); if (saslMechanism.isBlank()) { @@ -576,56 +577,63 @@ public final class KafkaAgent { int timeout = requestTimeout(params); String topic = stringOrEmpty(params, "topic"); - TopicDescription desc = admin.describeTopics(Collections.singletonList(topic)) - .allTopicNames().get(timeout, TimeUnit.MILLISECONDS).get(topic); - List partitions = desc.partitions().stream() - .map(pi -> new TopicPartition(topic, pi.partition())) - .collect(Collectors.toList()); + try { + TopicDescription desc = admin.describeTopics(Collections.singletonList(topic)) + .allTopicNames().get(timeout, TimeUnit.MILLISECONDS).get(topic); + List partitions = desc.partitions().stream() + .map(pi -> new TopicPartition(topic, pi.partition())) + .collect(Collectors.toList()); - DescribeProducersResult described = admin.describeProducers( - partitions, - new DescribeProducersOptions().timeoutMs(timeout)); + DescribeProducersResult described = admin.describeProducers( + partitions, + new DescribeProducersOptions().timeoutMs(timeout)); - Map> byProducer = new LinkedHashMap<>(); - for (TopicPartition tp : partitions) { - DescribeProducersResult.PartitionProducerState state = - described.partitionResult(tp).get(timeout, TimeUnit.MILLISECONDS); - for (ProducerState producerState : state.activeProducers()) { - long producerId = producerState.producerId(); - Map producer = byProducer.computeIfAbsent(producerId, id -> { - Map p = new LinkedHashMap<>(); - p.put("producerId", id); - p.put("producerName", "producer-" + id); - p.put("msgRateIn", 0.0); - p.put("msgThroughputIn", 0.0); - p.put("clientVersion", "Kafka producer"); - p.put("partitions", new ArrayList()); - p.put("lastTimestamp", producerState.lastTimestamp()); - return p; - }); - @SuppressWarnings("unchecked") - List producerPartitions = (List) producer.get("partitions"); - producerPartitions.add(tp.partition()); - long currentLastTimestamp = (long) producer.get("lastTimestamp"); - if (producerState.lastTimestamp() > currentLastTimestamp) { - producer.put("lastTimestamp", producerState.lastTimestamp()); + Map> byProducer = new LinkedHashMap<>(); + for (TopicPartition tp : partitions) { + DescribeProducersResult.PartitionProducerState state = + described.partitionResult(tp).get(timeout, TimeUnit.MILLISECONDS); + for (ProducerState producerState : state.activeProducers()) { + long producerId = producerState.producerId(); + Map producer = byProducer.computeIfAbsent(producerId, id -> { + Map p = new LinkedHashMap<>(); + p.put("producerId", id); + p.put("producerName", "producer-" + id); + p.put("msgRateIn", 0.0); + p.put("msgThroughputIn", 0.0); + p.put("clientVersion", "Kafka producer"); + p.put("partitions", new ArrayList()); + p.put("lastTimestamp", producerState.lastTimestamp()); + return p; + }); + @SuppressWarnings("unchecked") + List producerPartitions = (List) producer.get("partitions"); + producerPartitions.add(tp.partition()); + long currentLastTimestamp = (long) producer.get("lastTimestamp"); + if (producerState.lastTimestamp() > currentLastTimestamp) { + producer.put("lastTimestamp", producerState.lastTimestamp()); + } } } - } - for (Map producer : byProducer.values()) { - @SuppressWarnings("unchecked") - List producerPartitions = (List) producer.get("partitions"); - producerPartitions.sort(Integer::compareTo); - producer.put( - "address", - producerPartitions.size() == 1 - ? "partition " + producerPartitions.get(0) - : "partitions " + producerPartitions.stream().map(String::valueOf).collect(Collectors.joining(", ")) - ); - } + for (Map producer : byProducer.values()) { + @SuppressWarnings("unchecked") + List producerPartitions = (List) producer.get("partitions"); + producerPartitions.sort(Integer::compareTo); + producer.put( + "address", + producerPartitions.size() == 1 + ? "partition " + producerPartitions.get(0) + : "partitions " + producerPartitions.stream().map(String::valueOf).collect(Collectors.joining(", ")) + ); + } - return Collections.singletonMap("producers", new ArrayList<>(byProducer.values())); + return Collections.singletonMap("producers", new ArrayList<>(byProducer.values())); + } catch (Exception e) { + if (isUnsupportedVersionError(e)) { + return Collections.singletonMap("producers", Collections.emptyList()); + } + throw e; + } } private static Object deleteConsumerGroup(JsonObject params) throws Exception { @@ -1010,6 +1018,74 @@ public final class KafkaAgent { // Helpers // ----------------------------------------------------------------------- + private static String normalizeErrorMessage(Exception e) { + String message = e.getMessage() == null || e.getMessage().isBlank() + ? e.getClass().getName() + : e.getMessage(); + Throwable root = rootCause(e); + if (root != e && root.getMessage() != null && !root.getMessage().isBlank() + && !message.contains(root.getMessage())) { + message = message + ": " + root.getMessage(); + } + if (isSslHandshakeError(e)) { + message = message + + ". Hint: SSL handshake failed. Check the Kafka security protocol. " + + "Use PLAINTEXT for a PLAINTEXT broker listener, SSL for SSL, " + + "SASL_PLAINTEXT for SASL without TLS, or SASL_SSL for SASL with TLS. " + + "For older Kafka/JDK TLS setups, also check truststore settings, certificates, " + + "hostname verification, and enabled TLS protocol versions."; + } + return message; + } + + private static boolean isSslHandshakeError(Throwable error) { + for (Throwable current : causeChain(error)) { + String className = current.getClass().getName(); + String message = current.getMessage() == null ? "" : current.getMessage().toLowerCase(Locale.ROOT); + if (className.contains("SslAuthenticationException") + || className.contains("SSLHandshakeException") + || message.contains("ssl handshake failed")) { + return true; + } + } + return false; + } + + private static boolean isUnsupportedVersionError(Throwable error) { + for (Throwable current : causeChain(error)) { + String className = current.getClass().getName(); + String message = current.getMessage() == null ? "" : current.getMessage().toLowerCase(Locale.ROOT); + if (className.contains("UnsupportedVersionException") + || message.contains("unsupported version")) { + return true; + } + } + return false; + } + + private static Throwable rootCause(Throwable error) { + Throwable current = null; + for (Throwable cause : causeChain(error)) { + current = cause; + } + return current == null ? error : current; + } + + private static List causeChain(Throwable error) { + List chain = new ArrayList<>(); + Set seen = Collections.newSetFromMap(new IdentityHashMap<>()); + Throwable current = error; + for (int depth = 0; current != null && depth < 32 && seen.add(current); depth++) { + chain.add(current); + Throwable next = current.getCause(); + if (next == current) { + break; + } + current = next; + } + return chain; + } + private static AdminClient requireAdmin() { if (adminClient == null) { throw new IllegalStateException("Not connected. Call connect first."); diff --git a/apps/desktop/src/components/connection/ConnectionDialog.vue b/apps/desktop/src/components/connection/ConnectionDialog.vue index 8d4c8fcf18..1eac27c58e 100644 --- a/apps/desktop/src/components/connection/ConnectionDialog.vue +++ b/apps/desktop/src/components/connection/ConnectionDialog.vue @@ -1,5 +1,5 @@