You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by lh...@apache.org on 2026/09/09 16:32:16 UTC

(pulsar) 03/11: [fix][cli] pulsar-perf: register subcommand classes with picocli instead of instances (#26467)

This is an automated email from the ASF dual-hosted git repository.

lhotari pushed a commit to branch branch-4.0
in repository https://gitbox.apache.org/repos/asf/pulsar.git

commit ea35cfe1037b5114a5126c13f84e7d18d3c01893
Author: Lari Hotari <lh...@users.noreply.github.com>
AuthorDate: Sat Sep 5 00:57:42 2026 +0300

    [fix][cli] pulsar-perf: register subcommand classes with picocli instead of instances (#26467)
    
    (cherry picked from commit f41e620a0035e8c18a38de880c6ed993838980a6)
---
 .../proxy/socket/client/PerformanceClient.java     |  8 +------
 .../testclient/CmdGenerateDocumentation.java       |  7 +-----
 .../pulsar/testclient/ManagedLedgerWriter.java     |  8 +------
 .../pulsar/testclient/PulsarPerfTestTool.java      | 27 +++++++++++-----------
 4 files changed, 16 insertions(+), 34 deletions(-)

diff --git a/pulsar-testclient/src/main/java/org/apache/pulsar/proxy/socket/client/PerformanceClient.java b/pulsar-testclient/src/main/java/org/apache/pulsar/proxy/socket/client/PerformanceClient.java
index 19b7792f8226..1b2a56933276 100644
--- a/pulsar-testclient/src/main/java/org/apache/pulsar/proxy/socket/client/PerformanceClient.java
+++ b/pulsar-testclient/src/main/java/org/apache/pulsar/proxy/socket/client/PerformanceClient.java
@@ -62,10 +62,8 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import picocli.CommandLine;
 import picocli.CommandLine.Command;
-import picocli.CommandLine.Model.CommandSpec;
 import picocli.CommandLine.Option;
 import picocli.CommandLine.Parameters;
-import picocli.CommandLine.Spec;
 
 @Command(name = "websocket-producer", description = "Test pulsar websocket producer performance.")
 public class PerformanceClient extends CmdBase {
@@ -138,12 +136,8 @@ public class PerformanceClient extends CmdBase {
         super("websocket-producer");
     }
 
-
-    @Spec
-    CommandSpec spec;
-
     public void loadArguments() {
-        CommandLine commander = spec.commandLine();
+        CommandLine commander = getCommander();
 
         if (isBlank(this.authPluginClassName) && !isBlank(this.deprecatedAuthPluginClassName)) {
             this.authPluginClassName = this.deprecatedAuthPluginClassName;
diff --git a/pulsar-testclient/src/main/java/org/apache/pulsar/testclient/CmdGenerateDocumentation.java b/pulsar-testclient/src/main/java/org/apache/pulsar/testclient/CmdGenerateDocumentation.java
index d2e08e2cc866..d827dce76ba8 100644
--- a/pulsar-testclient/src/main/java/org/apache/pulsar/testclient/CmdGenerateDocumentation.java
+++ b/pulsar-testclient/src/main/java/org/apache/pulsar/testclient/CmdGenerateDocumentation.java
@@ -27,9 +27,7 @@ import lombok.extern.slf4j.Slf4j;
 import org.apache.pulsar.proxy.socket.client.PerformanceClient;
 import picocli.CommandLine;
 import picocli.CommandLine.Command;
-import picocli.CommandLine.Model.CommandSpec;
 import picocli.CommandLine.Option;
-import picocli.CommandLine.Spec;
 
 @Slf4j
 @Command(name = "gen-doc", description = "Generate documentation automatically.")
@@ -42,12 +40,9 @@ public class CmdGenerateDocumentation extends CmdBase{
         super("gen-doc");
     }
 
-    @Spec
-    CommandSpec spec;
-
     @Override
     public void run() throws Exception {
-        CommandLine commander = spec.commandLine();
+        CommandLine commander = getCommander();
 
         Map<String, Class<?>> cmdClassMap = new LinkedHashMap<>();
         cmdClassMap.put("produce", PerformanceProducer.class);
diff --git a/pulsar-testclient/src/main/java/org/apache/pulsar/testclient/ManagedLedgerWriter.java b/pulsar-testclient/src/main/java/org/apache/pulsar/testclient/ManagedLedgerWriter.java
index 3d73abd803f5..a37e254a0ec6 100644
--- a/pulsar-testclient/src/main/java/org/apache/pulsar/testclient/ManagedLedgerWriter.java
+++ b/pulsar-testclient/src/main/java/org/apache/pulsar/testclient/ManagedLedgerWriter.java
@@ -62,9 +62,7 @@ import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 import picocli.CommandLine;
 import picocli.CommandLine.Command;
-import picocli.CommandLine.Model.CommandSpec;
 import picocli.CommandLine.Option;
-import picocli.CommandLine.Spec;
 
 @Command(name = "managed-ledger", description = "Write directly on managed-ledgers")
 public class ManagedLedgerWriter extends CmdBase{
@@ -134,13 +132,9 @@ public class ManagedLedgerWriter extends CmdBase{
         super("managed-ledger");
     }
 
-
-    @Spec
-    CommandSpec spec;
-
     @Override
     public void run() throws Exception {
-        CommandLine commander = spec.commandLine();
+        CommandLine commander = getCommander();
 
         if (this.metadataStoreUrl == null && this.zookeeperServers == null) {
             System.err.println("Metadata store address argument is required (--metadata-store)");
diff --git a/pulsar-testclient/src/main/java/org/apache/pulsar/testclient/PulsarPerfTestTool.java b/pulsar-testclient/src/main/java/org/apache/pulsar/testclient/PulsarPerfTestTool.java
index 826060dc6b79..3c471e1b1851 100644
--- a/pulsar-testclient/src/main/java/org/apache/pulsar/testclient/PulsarPerfTestTool.java
+++ b/pulsar-testclient/src/main/java/org/apache/pulsar/testclient/PulsarPerfTestTool.java
@@ -21,7 +21,6 @@ package org.apache.pulsar.testclient;
 import static org.apache.commons.lang3.StringUtils.isBlank;
 import static org.apache.commons.lang3.StringUtils.isNotBlank;
 import java.io.FileInputStream;
-import java.lang.reflect.Constructor;
 import java.util.Arrays;
 import java.util.HashMap;
 import java.util.Map;
@@ -66,26 +65,26 @@ public class PulsarPerfTestTool {
                 prop.load(fis);
             }
         }
-        commander.setDefaultValueProvider(PulsarPerfTestPropertiesProvider.create(prop));
-
+        // Register the command classes rather than instances: picocli builds each subcommand's spec
+        // from annotations and only instantiates the user object once that subcommand is selected.
+        // Instantiating them all here ran every command's static initializers on every invocation,
+        // which is how `pulsar-perf produce` ended up allocating the latency histograms of `consume`,
+        // `read`, `transaction` and `managed-ledger` as well.
         for (Map.Entry<String, Class<?>> c : commandMap.entrySet()) {
-            Constructor<?> constructor = c.getValue().getDeclaredConstructor();
-            constructor.setAccessible(true);
-            addCommand(c.getKey(), constructor.newInstance());
+            commander.addSubcommand(c.getKey(), c.getValue());
         }
 
+        // Both settings propagate to the subcommand hierarchy as it exists when they are called, so
+        // they must be applied after the subcommands are registered. CmdBase used to set the enum
+        // behaviour on the per-command CommandLine it built in its own constructor; that instance is
+        // no longer the one picocli parses with.
+        commander.setDefaultValueProvider(PulsarPerfTestPropertiesProvider.create(prop));
+        commander.setCaseInsensitiveEnumValuesAllowed(true);
+
         // Remove the first argument, it's the config file path
         return Arrays.copyOfRange(args, 1, args.length);
     }
 
-    private void addCommand(String name, Object o) {
-        if (o instanceof CmdBase) {
-            commander.addSubcommand(name, ((CmdBase) o).getCommander());
-        } else {
-            commander.addSubcommand(o);
-        }
-    }
-
     public static void main(String[] args) throws Exception {
         PulsarPerfTestTool tool = new PulsarPerfTestTool();
         args = tool.initCommander(args);