You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@rocketmq.apache.org by GitBox <gi...@apache.org> on 2021/07/14 12:25:47 UTC

[GitHub] [rocketmq] panzhi33 opened a new pull request #3147: [ISSUE #3146]Support metadata export and import

panzhi33 opened a new pull request #3147:
URL: https://github.com/apache/rocketmq/pull/3147


   **Make sure set the target branch to `develop`**
   
   ## What is the purpose of the change
   
   XXXXX
   
   ## Brief changelog
   
   XX
   
   ## Verifying this change
   
   XXXX
   
   Follow this checklist to help us incorporate your contribution quickly and easily. Notice, `it would be helpful if you could finish the following 5 checklist(the last one is not necessary)before request the community to review your PR`.
   
   - [x] Make sure there is a [Github issue](https://github.com/apache/rocketmq/issues) filed for the change (usually before you start working on it). Trivial changes like typos do not require a Github issue. Your pull request should address just this issue, without pulling in other changes - one PR resolves one issue. 
   - [x] Format the pull request title like `[ISSUE #123] Fix UnknownException when host config not exist`. Each commit in the pull request should have a meaningful subject line and body.
   - [x] Write a pull request description that is detailed enough to understand what the pull request does, how, and why.
   - [x] Write necessary unit-test(over 80% coverage) to verify your logic correction, more mock a little better when cross module dependency exist. If the new feature or significant change is committed, please remember to add integration-test in [test module](https://github.com/apache/rocketmq/tree/master/test).
   - [x] Run `mvn -B clean apache-rat:check findbugs:findbugs checkstyle:checkstyle` to make sure basic checks pass. Run `mvn clean install -DskipITs` to make sure unit-test pass. Run `mvn clean test-compile failsafe:integration-test`  to make sure integration-test pass.
   - [ ] If this contribution is large, please file an [Apache Individual Contributor License Agreement](http://www.apache.org/licenses/#clas).
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [rocketmq] coveralls commented on pull request #3147: [ISSUE #3146]Support metadata export and import

Posted by GitBox <gi...@apache.org>.
coveralls commented on pull request #3147:
URL: https://github.com/apache/rocketmq/pull/3147#issuecomment-880123616


   
   [![Coverage Status](https://coveralls.io/builds/41362626/badge)](https://coveralls.io/builds/41362626)
   
   Coverage decreased (-0.1%) to 53.943% when pulling **6d472e77be5f29dbff035f81b3c56f751700017e on panzhi33:develop** into **30058cffa5a2512894a61b5c5eb97a58ce6681ec on apache:develop**.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [rocketmq] RongtongJin commented on a change in pull request #3147: [ISSUE #3146]Support metadata export and import

Posted by GitBox <gi...@apache.org>.
RongtongJin commented on a change in pull request #3147:
URL: https://github.com/apache/rocketmq/pull/3147#discussion_r669589137



##########
File path: tools/src/main/java/org/apache/rocketmq/tools/command/consumer/ImportSubscriptionJsonCommand.java
##########
@@ -0,0 +1,135 @@
+/*
+ * 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.consumer;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.rocketmq.common.MixAll;
+import org.apache.rocketmq.common.protocol.body.SubscriptionGroupWrapper;
+import org.apache.rocketmq.common.subscription.SubscriptionGroupConfig;
+import org.apache.rocketmq.remoting.RPCHook;
+import org.apache.rocketmq.tools.admin.DefaultMQAdminExt;
+import org.apache.rocketmq.tools.command.CommandUtil;
+import org.apache.rocketmq.tools.command.MQAdminStartup;
+import org.apache.rocketmq.tools.command.SubCommand;
+import org.apache.rocketmq.tools.command.SubCommandException;
+
+public class ImportSubscriptionJsonCommand implements SubCommand {
+    @Override
+    public String commandName() {
+        return "importSubscriptionJson";
+    }
+
+    @Override
+    public String commandDesc() {
+        return "import subscription.json to broker or cluster";
+    }
+
+    @Override
+    public Options buildCommandlineOptions(Options options) {
+        Option opt = new Option("b", "brokerAddr", true, "import topic.json or subscription.json to which broker");
+        opt.setRequired(false);
+        options.addOption(opt);
+
+        opt = new Option("c", "clusterName", true, "import topic.json or subscription.json to which cluster");
+        opt.setRequired(false);
+        options.addOption(opt);
+
+        opt = new Option("f", "filePath", true, "import topic.json or subscription.json path");
+        opt.setRequired(true);
+        options.addOption(opt);
+        return options;
+    }
+
+    @Override
+    public void execute(CommandLine commandLine, Options options, RPCHook rpcHook)
+        throws SubCommandException {
+
+        DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
+
+        defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
+
+        try {
+            defaultMQAdminExt.start();
+            final String filePath = commandLine.getOptionValue('f').trim();
+
+            String jsonString = MixAll.file2String(filePath);
+            if (commandLine.hasOption('b')) {
+                final String brokerAddr = commandLine.getOptionValue('b').trim();
+                importTobroker(defaultMQAdminExt, brokerAddr, jsonString);
+                System.out.printf("import %s to broker[%s] success", filePath, brokerAddr);
+            } else if (commandLine.hasOption('c')) {
+                String clusterName = commandLine.getOptionValue('c').trim();
+                Set<String> masterSet =
+                    CommandUtil.fetchMasterAddrByClusterName(defaultMQAdminExt, clusterName);
+                for (String addr : masterSet) {
+                    importTobroker(defaultMQAdminExt, addr, jsonString);
+                }
+                System.out.printf("import %s to cluster[%s] success", filePath, clusterName);
+            }
+        } catch (Exception e) {
+            throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
+        } finally {
+            defaultMQAdminExt.shutdown();
+        }
+    }
+
+    private void importTobroker(DefaultMQAdminExt defaultMQAdminExt, String brokerAddr, String jsonString)

Review comment:
       Hump-style naming

##########
File path: tools/src/main/java/org/apache/rocketmq/tools/command/topic/ExportTopicJsonCommand.java
##########
@@ -0,0 +1,73 @@
+/*
+ * 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.topic;
+
+import com.alibaba.fastjson.JSON;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.rocketmq.common.MixAll;
+import org.apache.rocketmq.common.protocol.body.TopicConfigSerializeWrapper;
+import org.apache.rocketmq.remoting.RPCHook;
+import org.apache.rocketmq.tools.admin.DefaultMQAdminExt;
+import org.apache.rocketmq.tools.command.SubCommand;
+import org.apache.rocketmq.tools.command.SubCommandException;
+
+public class ExportTopicJsonCommand implements SubCommand {
+    @Override
+    public String commandName() {
+        return "exportTopicJson";
+    }
+
+    @Override
+    public String commandDesc() {
+        return "export topic.json";
+    }
+
+    @Override
+    public Options buildCommandlineOptions(Options options) {
+        Option opt = new Option("b", "brokerAddr", true, "export topic.json or subscription.json to which broker");
+        opt.setRequired(true);
+        options.addOption(opt);
+        return options;
+    }
+
+    @Override
+    public void execute(CommandLine commandLine, Options options, RPCHook rpcHook)
+        throws SubCommandException {
+        DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
+
+        defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
+
+        try {
+            defaultMQAdminExt.start();
+
+            final String brokerAddr = commandLine.getOptionValue('b').trim();
+
+            TopicConfigSerializeWrapper topicConfigSerializeWrapper = defaultMQAdminExt.getAllTopicConfig(
+                brokerAddr, 5000);
+            String topicJsonString = JSON.toJSONString(topicConfigSerializeWrapper, true);
+            String fileName = "D://topic.json";

Review comment:
       Do not write to fixed file path

##########
File path: tools/src/main/java/org/apache/rocketmq/tools/command/topic/ImportTopicJsonCommand.java
##########
@@ -0,0 +1,135 @@
+/*
+ * 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.topic;
+
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.Options;
+import org.apache.rocketmq.common.MixAll;
+import org.apache.rocketmq.common.TopicConfig;
+import org.apache.rocketmq.common.protocol.body.TopicConfigSerializeWrapper;
+import org.apache.rocketmq.common.topic.TopicValidator;
+import org.apache.rocketmq.remoting.RPCHook;
+import org.apache.rocketmq.tools.admin.DefaultMQAdminExt;
+import org.apache.rocketmq.tools.command.CommandUtil;
+import org.apache.rocketmq.tools.command.MQAdminStartup;
+import org.apache.rocketmq.tools.command.SubCommand;
+import org.apache.rocketmq.tools.command.SubCommandException;
+
+public class ImportTopicJsonCommand implements SubCommand {
+    @Override
+    public String commandName() {
+        return "importTopicJson";
+    }
+
+    @Override
+    public String commandDesc() {
+        return "import topic.json to broker or cluster";
+    }
+
+    @Override
+    public Options buildCommandlineOptions(Options options) {
+        Option opt = new Option("b", "brokerAddr", true, "import topic.json or subscription.json to which broker");
+        opt.setRequired(false);
+        options.addOption(opt);
+
+        opt = new Option("c", "clusterName", true, "import topic.json or subscription.json to which cluster");
+        opt.setRequired(false);
+        options.addOption(opt);
+
+        opt = new Option("f", "filePath", true, "import topic.json or subscription.json path");
+        opt.setRequired(true);
+        options.addOption(opt);
+        return options;
+    }
+
+    @Override
+    public void execute(CommandLine commandLine, Options options, RPCHook rpcHook)
+        throws SubCommandException {
+        DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
+
+        defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
+
+        try {
+            defaultMQAdminExt.start();
+            final String filePath = commandLine.getOptionValue('f').trim();
+
+            String jsonString = MixAll.file2String(filePath);
+            if (commandLine.hasOption('b')) {
+                final String brokerAddr = commandLine.getOptionValue('b').trim();
+                importTobroker(defaultMQAdminExt, brokerAddr, jsonString);
+                System.out.printf("import %s to broker[%s] success", filePath, brokerAddr);
+            } else if (commandLine.hasOption('c')) {
+                String clusterName = commandLine.getOptionValue('c').trim();
+                Set<String> masterSet =
+                    CommandUtil.fetchMasterAddrByClusterName(defaultMQAdminExt, clusterName);
+                for (String addr : masterSet) {
+                    importTobroker(defaultMQAdminExt, addr, jsonString);
+                }
+                System.out.printf("import %s to cluster[%s] success", filePath, clusterName);
+            }
+        } catch (Exception e) {
+            throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
+        } finally {
+            defaultMQAdminExt.shutdown();
+        }
+    }
+
+    private void importTobroker(DefaultMQAdminExt defaultMQAdminExt, String brokerAddr, String jsonString)
+        throws Exception {
+        StringBuilder unImportTopic = new StringBuilder("");
+        TopicConfigSerializeWrapper topicConfigSerializeWrapper = defaultMQAdminExt.getAllTopicConfig(
+            brokerAddr, 5000);
+        Map<String, TopicConfig> topicConfigMap = topicConfigSerializeWrapper.getTopicConfigTable();
+        Set<String> topicSet = topicConfigMap.keySet();
+
+        TopicConfigSerializeWrapper importTopicConfigSerializeWrapper =
+            TopicConfigSerializeWrapper.fromJson(jsonString, TopicConfigSerializeWrapper.class);
+
+        Map<String, TopicConfig> importTopicConfigMap = importTopicConfigSerializeWrapper
+            .getTopicConfigTable();
+        String splitor = "";
+        List<TopicConfig> topicConfigList = new LinkedList<>();
+        for (Map.Entry<String, TopicConfig> entry : importTopicConfigMap.entrySet()) {
+            if (TopicValidator.validateTopic(entry.getKey()) && !TopicValidator.isSystemTopic(entry.getKey())
+                && !topicSet.contains(entry.getKey())) {
+                topicConfigList.add(entry.getValue());
+            } else {
+                unImportTopic.append(splitor).append(entry.getKey());
+                splitor = ";";
+            }
+        }
+        defaultMQAdminExt.batchCreateAndUpdateTopicConfig(brokerAddr, topicConfigList);
+
+        System.out.printf(
+            "%s import failed,maybe these topic are not compliant or are system topics or already exist%n",
+            unImportTopic);
+    }
+
+    public static void main(String[] args) {
+        System.setProperty(MixAll.NAMESRV_ADDR_PROPERTY, "127.0.0.1:9876");
+        MQAdminStartup.main(
+            new String[] {new ImportTopicJsonCommand().commandName(), "-c", "DefaultCluster", "-f",
+                "D:\\topic.json"});
+
+    }

Review comment:
       Delete main method




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org



[GitHub] [rocketmq] panzhi33 closed pull request #3147: [ISSUE #3146]Support metadata export and import

Posted by GitBox <gi...@apache.org>.
panzhi33 closed pull request #3147:
URL: https://github.com/apache/rocketmq/pull/3147


   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: dev-unsubscribe@rocketmq.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org