From 731290e2b0015566891ae5562f2d8489e3c788c3 Mon Sep 17 00:00:00 2001 From: BurningCN <1015773611@qq.com> Date: Sun, 30 May 2021 15:55:50 +0800 Subject: [PATCH 1/2] Cache the parsed result of the HelpCommand and HelpTelnetHandler --- .../apache/dubbo/qos/command/impl/Help.java | 11 +++- .../support/command/HelpTelnetHandler.java | 66 +++++++++++-------- 2 files changed, 49 insertions(+), 28 deletions(-) diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Help.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Help.java index ba370ccd85f..e0ad2c1c26f 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Help.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Help.java @@ -25,18 +25,25 @@ import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; @Cmd(name = "help", summary = "help command", example = { "help", "help online" }) public class Help implements BaseCommand { + + private static final String MAIN_HELP = "mainHelp"; + + private static Map processedTable = new ConcurrentHashMap<>(); + @Override public String execute(CommandContext commandContext, String[] args) { if (args != null && args.length > 0) { - return commandHelp(args[0]); + return processedTable.computeIfAbsent(args[0], commandName -> commandHelp(commandName)); } else { - return mainHelp(); + return processedTable.computeIfAbsent(MAIN_HELP, commandName -> mainHelp()); } } diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/HelpTelnetHandler.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/HelpTelnetHandler.java index b22cd1ac667..ebdc248a423 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/HelpTelnetHandler.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/HelpTelnetHandler.java @@ -26,6 +26,8 @@ import java.util.ArrayList; import java.util.List; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; /** * HelpTelnetHandler @@ -36,38 +38,50 @@ public class HelpTelnetHandler implements TelnetHandler { private final ExtensionLoader extensionLoader = ExtensionLoader.getExtensionLoader(TelnetHandler.class); + private static final String MAIN_HELP = "mainHelp"; + + private static Map processedTable = new ConcurrentHashMap<>(); + @Override public String telnet(Channel channel, String message) { if (message.length() > 0) { - if (!extensionLoader.hasExtension(message)) { - return "No such command " + message; - } - TelnetHandler handler = extensionLoader.getExtension(message); - Help help = handler.getClass().getAnnotation(Help.class); - StringBuilder buf = new StringBuilder(); - buf.append("Command:\r\n "); - buf.append(message + " " + help.parameter().replace("\r\n", " ").replace("\n", " ")); - buf.append("\r\nSummary:\r\n "); - buf.append(help.summary().replace("\r\n", " ").replace("\n", " ")); - buf.append("\r\nDetail:\r\n "); - buf.append(help.detail().replace("\r\n", " \r\n").replace("\n", " \n")); - return buf.toString(); + return processedTable.computeIfAbsent(message, commandName -> generateForOneCommand(commandName)); } else { - List> table = new ArrayList>(); - List handlers = extensionLoader.getActivateExtension(channel.getUrl(), "telnet"); - if (CollectionUtils.isNotEmpty(handlers)) { - for (TelnetHandler handler : handlers) { - Help help = handler.getClass().getAnnotation(Help.class); - List row = new ArrayList(); - String parameter = " " + extensionLoader.getExtensionName(handler) + " " + (help != null ? help.parameter().replace("\r\n", " ").replace("\n", " ") : ""); - row.add(parameter.length() > 55 ? parameter.substring(0, 55) + "..." : parameter); - String summary = help != null ? help.summary().replace("\r\n", " ").replace("\n", " ") : ""; - row.add(summary.length() > 55 ? summary.substring(0, 55) + "..." : summary); - table.add(row); - } + return processedTable.computeIfAbsent(MAIN_HELP, commandName -> generateForAllCommand(channel)); + } + } + + private String generateForOneCommand(String message) { + if (!extensionLoader.hasExtension(message)) { + return "No such command " + message; + } + TelnetHandler handler = extensionLoader.getExtension(message); + Help help = handler.getClass().getAnnotation(Help.class); + StringBuilder buf = new StringBuilder(); + buf.append("Command:\r\n "); + buf.append(message + " " + help.parameter().replace("\r\n", " ").replace("\n", " ")); + buf.append("\r\nSummary:\r\n "); + buf.append(help.summary().replace("\r\n", " ").replace("\n", " ")); + buf.append("\r\nDetail:\r\n "); + buf.append(help.detail().replace("\r\n", " \r\n").replace("\n", " \n")); + return buf.toString(); + } + + private String generateForAllCommand(Channel channel) { + List> table = new ArrayList>(); + List handlers = extensionLoader.getActivateExtension(channel.getUrl(), "telnet"); + if (CollectionUtils.isNotEmpty(handlers)) { + for (TelnetHandler handler : handlers) { + Help help = handler.getClass().getAnnotation(Help.class); + List row = new ArrayList(); + String parameter = " " + extensionLoader.getExtensionName(handler) + " " + (help != null ? help.parameter().replace("\r\n", " ").replace("\n", " ") : ""); + row.add(parameter.length() > 55 ? parameter.substring(0, 55) + "..." : parameter); + String summary = help != null ? help.summary().replace("\r\n", " ").replace("\n", " ") : ""; + row.add(summary.length() > 55 ? summary.substring(0, 55) + "..." : summary); + table.add(row); } - return "Please input \"help [command]\" show detail.\r\n" + TelnetUtils.toList(table); } + return "Please input \"help [command]\" show detail.\r\n" + TelnetUtils.toList(table); } } From e3a2d0424ea78e4d712d12664b73c9b8c5ab218f Mon Sep 17 00:00:00 2001 From: BurningCN <1015773611@qq.com> Date: Mon, 31 May 2021 10:54:18 +0800 Subject: [PATCH 2/2] Modify the map type to WeakHashMap --- .../src/main/java/org/apache/dubbo/qos/command/impl/Help.java | 4 ++-- .../remoting/telnet/support/command/HelpTelnetHandler.java | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Help.java b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Help.java index e0ad2c1c26f..696d58f102d 100644 --- a/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Help.java +++ b/dubbo-plugin/dubbo-qos/src/main/java/org/apache/dubbo/qos/command/impl/Help.java @@ -26,7 +26,7 @@ import java.util.Comparator; import java.util.List; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; +import java.util.WeakHashMap; @Cmd(name = "help", summary = "help command", example = { "help", @@ -36,7 +36,7 @@ public class Help implements BaseCommand { private static final String MAIN_HELP = "mainHelp"; - private static Map processedTable = new ConcurrentHashMap<>(); + private static Map processedTable = new WeakHashMap<>(); @Override public String execute(CommandContext commandContext, String[] args) { diff --git a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/HelpTelnetHandler.java b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/HelpTelnetHandler.java index ebdc248a423..23390eb7328 100644 --- a/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/HelpTelnetHandler.java +++ b/dubbo-remoting/dubbo-remoting-api/src/main/java/org/apache/dubbo/remoting/telnet/support/command/HelpTelnetHandler.java @@ -27,7 +27,7 @@ import java.util.ArrayList; import java.util.List; import java.util.Map; -import java.util.concurrent.ConcurrentHashMap; +import java.util.WeakHashMap; /** * HelpTelnetHandler @@ -40,7 +40,7 @@ public class HelpTelnetHandler implements TelnetHandler { private static final String MAIN_HELP = "mainHelp"; - private static Map processedTable = new ConcurrentHashMap<>(); + private static Map processedTable = new WeakHashMap<>(); @Override public String telnet(Channel channel, String message) {