From e2abbc31d9feb59d04879071f3e123564374d9c4 Mon Sep 17 00:00:00 2001 From: yuz10 <845238369@qq.com> Date: Wed, 25 Sep 2024 10:14:38 +0800 Subject: [PATCH] [ISSUE #8740] fix rocksDBConfigToJson command (#8738) * fix rocksDBConfigToJson command * fix --- .../metadata/RocksDBConfigToJsonCommand.java | 72 +++++++++++++++---- 1 file changed, 58 insertions(+), 14 deletions(-) diff --git a/tools/src/main/java/org/apache/rocketmq/tools/command/metadata/RocksDBConfigToJsonCommand.java b/tools/src/main/java/org/apache/rocketmq/tools/command/metadata/RocksDBConfigToJsonCommand.java index 1d81287ac7..f2803b0cbb 100644 --- a/tools/src/main/java/org/apache/rocketmq/tools/command/metadata/RocksDBConfigToJsonCommand.java +++ b/tools/src/main/java/org/apache/rocketmq/tools/command/metadata/RocksDBConfigToJsonCommand.java @@ -17,7 +17,6 @@ package org.apache.rocketmq.tools.command.metadata; -import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONObject; import org.apache.commons.cli.CommandLine; import org.apache.commons.cli.Option; @@ -33,10 +32,13 @@ import org.rocksdb.RocksIterator; import java.io.File; import java.util.HashMap; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ConcurrentMap; public class RocksDBConfigToJsonCommand implements SubCommand { private static final String TOPICS_JSON_CONFIG = "topics"; private static final String SUBSCRIPTION_GROUP_JSON_CONFIG = "subscriptionGroups"; + private static final String CONSUMER_OFFSETS_JSON_CONFIG = "consumerOffsets"; @Override public String commandName() { @@ -45,7 +47,7 @@ public class RocksDBConfigToJsonCommand implements SubCommand { @Override public String commandDesc() { - return "Convert RocksDB kv config (topics/subscriptionGroups) to json"; + return "Convert RocksDB kv config (topics/subscriptionGroups/consumerOffsets) to json"; } @Override @@ -56,7 +58,7 @@ public class RocksDBConfigToJsonCommand implements SubCommand { options.addOption(pathOption); Option configTypeOption = new Option("t", "configType", true, "Name of kv config, e.g. " + - "topics/subscriptionGroups"); + "topics/subscriptionGroups/consumerOffsets"); configTypeOption.setRequired(true); options.addOption(configTypeOption); @@ -71,19 +73,21 @@ public class RocksDBConfigToJsonCommand implements SubCommand { return; } - String configType = commandLine.getOptionValue("configType").trim().toLowerCase(); + String configType = commandLine.getOptionValue("configType").trim(); if (!path.endsWith("/")) { path += "/"; } path += configType; - + if (CONSUMER_OFFSETS_JSON_CONFIG.equalsIgnoreCase(configType)) { + printConsumerOffsets(path); + return; + } ConfigRocksDBStorage configRocksDBStorage = new ConfigRocksDBStorage(path, true); configRocksDBStorage.start(); RocksIterator iterator = configRocksDBStorage.iterator(); - try { final Map configMap = new HashMap<>(); - final Map configTable = new HashMap<>(); + final JSONObject configTable = new JSONObject(); iterator.seekToFirst(); while (iterator.isValid()) { final byte[] key = iterator.key(); @@ -95,14 +99,16 @@ public class RocksDBConfigToJsonCommand implements SubCommand { iterator.next(); } byte[] kvDataVersion = configRocksDBStorage.getKvDataVersion(); - configMap.put("dataVersion", - JSONObject.parseObject(new String(kvDataVersion, DataConverter.CHARSET_UTF8))); - - if (TOPICS_JSON_CONFIG.toLowerCase().equals(configType)) { - configMap.put("topicConfigTable", JSON.parseObject(JSONObject.toJSONString(configTable))); + if (kvDataVersion != null) { + configMap.put("dataVersion", + JSONObject.parseObject(new String(kvDataVersion, DataConverter.CHARSET_UTF8))); } - if (SUBSCRIPTION_GROUP_JSON_CONFIG.toLowerCase().equals(configType)) { - configMap.put("subscriptionGroupTable", JSON.parseObject(JSONObject.toJSONString(configTable))); + + if (TOPICS_JSON_CONFIG.equalsIgnoreCase(configType)) { + configMap.put("topicConfigTable", configTable); + } + if (SUBSCRIPTION_GROUP_JSON_CONFIG.equalsIgnoreCase(configType)) { + configMap.put("subscriptionGroupTable", configTable); } System.out.print(JSONObject.toJSONString(configMap, true) + "\n"); } catch (Exception e) { @@ -111,4 +117,42 @@ public class RocksDBConfigToJsonCommand implements SubCommand { configRocksDBStorage.shutdown(); } } + + private void printConsumerOffsets(String path) { + ConfigRocksDBStorage configRocksDBStorage = new ConfigRocksDBStorage(path, true); + configRocksDBStorage.start(); + RocksIterator iterator = configRocksDBStorage.iterator(); + try { + final Map configMap = new HashMap<>(); + final JSONObject configTable = new JSONObject(); + iterator.seekToFirst(); + while (iterator.isValid()) { + final byte[] key = iterator.key(); + final byte[] value = iterator.value(); + final String name = new String(key, DataConverter.CHARSET_UTF8); + final String config = new String(value, DataConverter.CHARSET_UTF8); + final RocksDBOffsetSerializeWrapper jsonObject = JSONObject.parseObject(config, RocksDBOffsetSerializeWrapper.class); + configTable.put(name, jsonObject.getOffsetTable()); + iterator.next(); + } + configMap.put("offsetTable", configTable); + System.out.print(JSONObject.toJSONString(configMap, true) + "\n"); + } catch (Exception e) { + System.out.print("Error occurred while converting RocksDB kv config to json, " + "configType=consumerOffsets, " + e.getMessage() + "\n"); + } finally { + configRocksDBStorage.shutdown(); + } + } + + static class RocksDBOffsetSerializeWrapper { + private ConcurrentMap offsetTable = new ConcurrentHashMap<>(16); + + public ConcurrentMap getOffsetTable() { + return offsetTable; + } + + public void setOffsetTable(ConcurrentMap offsetTable) { + this.offsetTable = offsetTable; + } + } } \ No newline at end of file