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

ShannonDing pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git


The following commit(s) were added to refs/heads/develop by this push:
     new 94356527d2 fix(tools): export metadata when only a broker is specified 
(#11614)
94356527d2 is described below

commit 94356527d260d538396a37aa0f363636f82fb552
Author: Haoyu Wu <[email protected]>
AuthorDate: Wed Nov 25 22:46:38 2026 +0800

    fix(tools): export metadata when only a broker is specified (#11614)
---
 .../command/export/ExportMetadataCommand.java      | 10 +++
 .../command/export/ExportMetadataCommandTest.java  | 82 ++++++++++++++++++++++
 2 files changed, 92 insertions(+)

diff --git 
a/tools/src/main/java/org/apache/rocketmq/tools/command/export/ExportMetadataCommand.java
 
b/tools/src/main/java/org/apache/rocketmq/tools/command/export/ExportMetadataCommand.java
index 559936bcb8..ee34700f82 100644
--- 
a/tools/src/main/java/org/apache/rocketmq/tools/command/export/ExportMetadataCommand.java
+++ 
b/tools/src/main/java/org/apache/rocketmq/tools/command/export/ExportMetadataCommand.java
@@ -109,6 +109,16 @@ public class ExportMetadataCommand implements SubCommand {
                         brokerAddr, 10000L);
                     
MixAll.string2FileNotSafe(JSON.toJSONString(subscriptionGroupWrapper, 
JSONWriter.Feature.PrettyFormat), filePath);
                     System.out.printf("export %s success", filePath);
+                } else {
+                    Map<String, Object> result = new HashMap<>();
+                    result.put("topicConfigTable", 
defaultMQAdminExt.getUserTopicConfig(brokerAddr, specialTopic, 10000L)
+                        .getTopicConfigTable());
+                    result.put("subscriptionGroupTable", 
defaultMQAdminExt.getUserSubscriptionGroup(brokerAddr, 10000L)
+                        .getSubscriptionGroupTable());
+                    result.put("exportTime", System.currentTimeMillis());
+                    filePath = filePath + "/metadata.json";
+                    MixAll.string2FileNotSafe(JSON.toJSONString(result, 
JSONWriter.Feature.PrettyFormat), filePath);
+                    System.out.printf("export %s success%n", filePath);
                 }
             } else if (commandLine.hasOption('c')) {
                 String clusterName = commandLine.getOptionValue('c').trim();
diff --git 
a/tools/src/test/java/org/apache/rocketmq/tools/command/export/ExportMetadataCommandTest.java
 
b/tools/src/test/java/org/apache/rocketmq/tools/command/export/ExportMetadataCommandTest.java
new file mode 100644
index 0000000000..0e53211dd7
--- /dev/null
+++ 
b/tools/src/test/java/org/apache/rocketmq/tools/command/export/ExportMetadataCommandTest.java
@@ -0,0 +1,82 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.rocketmq.tools.command.export;
+
+import com.alibaba.fastjson2.JSON;
+import com.alibaba.fastjson2.JSONObject;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.util.Collections;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.DefaultParser;
+import org.apache.commons.cli.Options;
+import org.apache.rocketmq.common.MixAll;
+import org.apache.rocketmq.common.TopicConfig;
+import 
org.apache.rocketmq.remoting.protocol.subscription.SubscriptionGroupConfig;
+import org.apache.rocketmq.tools.command.server.ServerResponseMocker;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+public class ExportMetadataCommandTest {
+    @Rule
+    public TemporaryFolder tempFolder = new TemporaryFolder();
+
+    @Test
+    public void testBrokerExportIncludesTopicsAndSubscriptionGroupsByDefault() 
throws Exception {
+        TopicConfig topic = new TopicConfig("example-topic");
+        topic.setWriteQueueNums(4);
+        SubscriptionGroupConfig group = new SubscriptionGroupConfig();
+        group.setGroupName("example-group");
+
+        // The mock can return a single payload to each RPC because the 
response types read distinct fields.
+        JSONObject response = new JSONObject();
+        response.put("topicConfigTable", 
Collections.singletonMap(topic.getTopicName(), topic));
+        response.put("subscriptionGroupTable", 
Collections.singletonMap(group.getGroupName(), group));
+        response.put("topicList", Collections.emptyList());
+        ServerResponseMocker broker = 
ServerResponseMocker.startServer(JSON.toJSONBytes(response));
+        String address = "127.0.0.1:" + broker.listenPort();
+        String previousNamesrv = 
System.setProperty(MixAll.NAMESRV_ADDR_PROPERTY, address);
+
+        try {
+            Path output = tempFolder.newFolder().toPath();
+            ExportMetadataCommand command = new ExportMetadataCommand();
+            Options options = command.buildCommandlineOptions(new Options());
+            CommandLine args = new DefaultParser().parse(options, new String[] 
{"-b", address, "-f", output.toString()});
+            command.execute(args, options, null);
+
+            Path file = output.resolve("metadata.json");
+            assertTrue("A broker-only export should write metadata.json", 
Files.isRegularFile(file));
+            JSONObject metadata = JSON.parseObject(Files.readAllBytes(file));
+            JSONObject exportedTopic = 
metadata.getJSONObject("topicConfigTable").getJSONObject(topic.getTopicName());
+            assertEquals(4, exportedTopic.getIntValue("writeQueueNums"));
+            JSONObject exportedGroup = 
metadata.getJSONObject("subscriptionGroupTable").getJSONObject(group.getGroupName());
+            assertEquals(group.getGroupName(), 
exportedGroup.getString("groupName"));
+            assertTrue(metadata.getLongValue("exportTime") > 0);
+        } finally {
+            if (previousNamesrv == null) {
+                System.clearProperty(MixAll.NAMESRV_ADDR_PROPERTY);
+            } else {
+                System.setProperty(MixAll.NAMESRV_ADDR_PROPERTY, 
previousNamesrv);
+            }
+            broker.shutdown();
+        }
+    }
+}

Reply via email to