Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ public void execute(CommandLine commandLine, Options options, RPCHook rpcHook)
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();
Expand Down
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
Loading