|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | +package org.apache.rocketmq.tools.command.export; |
| 18 | + |
| 19 | +import com.alibaba.fastjson.JSONObject; |
| 20 | +import org.apache.commons.cli.CommandLine; |
| 21 | +import org.apache.commons.cli.Option; |
| 22 | +import org.apache.commons.cli.Options; |
| 23 | +import org.apache.commons.lang3.StringUtils; |
| 24 | +import org.apache.rocketmq.common.UtilAll; |
| 25 | +import org.apache.rocketmq.common.config.ConfigRocksDBStorage; |
| 26 | +import org.apache.rocketmq.common.utils.DataConverter; |
| 27 | +import org.apache.rocketmq.remoting.RPCHook; |
| 28 | +import org.apache.rocketmq.tools.command.SubCommand; |
| 29 | +import org.apache.rocketmq.tools.command.SubCommandException; |
| 30 | +import org.rocksdb.RocksIterator; |
| 31 | + |
| 32 | +import java.util.HashMap; |
| 33 | +import java.util.Map; |
| 34 | +import java.util.concurrent.atomic.AtomicLong; |
| 35 | +import java.util.function.BiConsumer; |
| 36 | + |
| 37 | +public class ExportMetadataInRocksDBCommand implements SubCommand { |
| 38 | + private static final String TOPICS_JSON_CONFIG = "topics"; |
| 39 | + private static final String SUBSCRIPTION_GROUP_JSON_CONFIG = "subscriptionGroups"; |
| 40 | + |
| 41 | + @Override |
| 42 | + public String commandName() { |
| 43 | + return "exportMetadataInRocksDB"; |
| 44 | + } |
| 45 | + |
| 46 | + @Override |
| 47 | + public String commandDesc() { |
| 48 | + return "export RocksDB kv config (topics/subscriptionGroups)"; |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public Options buildCommandlineOptions(Options options) { |
| 53 | + Option pathOption = new Option("p", "path", true, |
| 54 | + "Absolute path for the metadata directory"); |
| 55 | + pathOption.setRequired(true); |
| 56 | + options.addOption(pathOption); |
| 57 | + |
| 58 | + Option configTypeOption = new Option("t", "configType", true, "Name of kv config, e.g. " + |
| 59 | + "topics/subscriptionGroups"); |
| 60 | + configTypeOption.setRequired(true); |
| 61 | + options.addOption(configTypeOption); |
| 62 | + |
| 63 | + Option jsonEnableOption = new Option("j", "jsonEnable", true, |
| 64 | + "Json format enable, Default: false"); |
| 65 | + jsonEnableOption.setRequired(false); |
| 66 | + options.addOption(jsonEnableOption); |
| 67 | + |
| 68 | + return options; |
| 69 | + } |
| 70 | + |
| 71 | + @Override |
| 72 | + public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException { |
| 73 | + String path = commandLine.getOptionValue("path").trim(); |
| 74 | + if (StringUtils.isEmpty(path) || !UtilAll.isPathExists(path)) { |
| 75 | + System.out.print("RocksDB path is invalid.\n"); |
| 76 | + return; |
| 77 | + } |
| 78 | + |
| 79 | + String configType = commandLine.getOptionValue("configType").trim().toLowerCase(); |
| 80 | + |
| 81 | + boolean jsonEnable = false; |
| 82 | + if (commandLine.hasOption("jsonEnable")) { |
| 83 | + jsonEnable = Boolean.parseBoolean(commandLine.getOptionValue("jsonEnable").trim()); |
| 84 | + } |
| 85 | + |
| 86 | + |
| 87 | + ConfigRocksDBStorage kvStore = new ConfigRocksDBStorage(path, true /* readOnly */); |
| 88 | + if (!kvStore.start()) { |
| 89 | + System.out.print("RocksDB load error, path=" + path + "\n"); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + try { |
| 94 | + if (TOPICS_JSON_CONFIG.equalsIgnoreCase(configType) || SUBSCRIPTION_GROUP_JSON_CONFIG.equalsIgnoreCase(configType)) { |
| 95 | + handleExportMetadata(kvStore, configType, jsonEnable); |
| 96 | + } else { |
| 97 | + System.out.printf("Invalid config type=%s, Options: topics,subscriptionGroups\n", configType); |
| 98 | + } |
| 99 | + } finally { |
| 100 | + kvStore.shutdown(); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + private static void handleExportMetadata(ConfigRocksDBStorage kvStore, String configType, boolean jsonEnable) { |
| 105 | + if (jsonEnable) { |
| 106 | + final Map<String, JSONObject> jsonConfig = new HashMap<>(); |
| 107 | + final Map<String, JSONObject> configTable = new HashMap<>(); |
| 108 | + iterateKvStore(kvStore, (key, value) -> { |
| 109 | + final String configKey = new String(key, DataConverter.charset); |
| 110 | + final String configValue = new String(value, DataConverter.charset); |
| 111 | + final JSONObject jsonObject = JSONObject.parseObject(configValue); |
| 112 | + configTable.put(configKey, jsonObject); |
| 113 | + } |
| 114 | + ); |
| 115 | + |
| 116 | + jsonConfig.put(configType.equalsIgnoreCase(TOPICS_JSON_CONFIG) ? "topicConfigTable" : "subscriptionGroupTable", |
| 117 | + (JSONObject) JSONObject.toJSON(configTable)); |
| 118 | + final String jsonConfigStr = JSONObject.toJSONString(jsonConfig, true); |
| 119 | + System.out.print(jsonConfigStr + "\n"); |
| 120 | + } else { |
| 121 | + AtomicLong count = new AtomicLong(0); |
| 122 | + iterateKvStore(kvStore, (key, value) -> { |
| 123 | + final String configKey = new String(key, DataConverter.charset); |
| 124 | + final String configValue = new String(value, DataConverter.charset); |
| 125 | + System.out.printf("%d, Key: %s, Value: %s%n", count.incrementAndGet(), configKey, configValue); |
| 126 | + }); |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + private static void iterateKvStore(ConfigRocksDBStorage kvStore, BiConsumer<byte[], byte[]> biConsumer) { |
| 131 | + try (RocksIterator iterator = kvStore.iterator()) { |
| 132 | + iterator.seekToFirst(); |
| 133 | + for (iterator.seekToFirst(); iterator.isValid(); iterator.next()) { |
| 134 | + biConsumer.accept(iterator.key(), iterator.value()); |
| 135 | + } |
| 136 | + } |
| 137 | + } |
| 138 | +} |
0 commit comments