Project: Pulsar (App Definition and Development / Streaming & Messaging)
File: pip-280.md

In the current Pulsar codebase, the logic to parse CLI arguments for measurement units like time and bytes is scattered across various CLI classes. Each value read has its distinct parsing implementation, leading to a lack of code reuse. This PIP is to refactor the argument parsing logic to leverage the `@Parameter.converter` functionality provided by JCommander [link 3]. This will isolate the measurement-specific parsing logic and increase code reusability. Refactor all `Cmd` classes to utilize the converter functionality of JCommander. This will streamline the parsing logic and simplify the codebase. Refer to bottom section "Concrete Example", before "Links" Or on-going PR with small use case in https://github.com/apache/pulsar/pull/20663 Creation of a "util" module is out of the scope of this PIP. The refactoring will be carried out on a class-by-class basis or per inner-class basis. Target command classes for this refactoring include `CmdNamespaces.java` `CmdTopics.java`, `CmdTopicPolicies.java`. Additional classes may be included as the refactoring progresses. Respective PRs will be added here also. The refactoring should not introduce any breaking change New parameters should be covered by unit test (at least by existing and preferably new) Consider the code snippet from for example. The existing code uses a local variable `maxBlockSizeStr` to temporarily store the value of `--maxBlockSize` or `-mbs`. This is then parsed and validated in a separate section of the code. ```java @Parameter( names = {"--maxBlockSize", "-mbs"}, description = "Max block size (eg: 32M, 64M), default is 64MB s3 and google-cloud-storage requires this parameter", required = false) private String maxBlockSizeStr; ``` parsing like below .... ```java // parsing like.... int maxBlockSizeInBytes=OffloadPoliciesImpl.DEFAULTMAXBLOCKSIZEIN_BYTES; if(StringUtils.isNotEmpty(maxBlockSizeStr)){ long maxBlockSize=validateSizeString(maxBlockSizeStr); if(positiveCheck("MaxBlockSize",maxBlockSize) &&maxValueCheck("MaxBlockSize",maxBlockSize,Integer.MAX_VALUE)) { maxBlockSizeInBytes=Long.valueOf(maxBlockSize).intValue(); } } ``` ```java @Parameter( names = {"--maxBlockSize", "-mbs"}, description = "Max block size (eg: 32M, 64M), default is 64MB s3 and google-cloud-storage requires this parameter", required = false, converter = MemoryUnitToByteConverter.class) // < parsing logic "inline" easy to follow private long maxBlockSizeStr=DEFAULTMAXBLOCKSIZEIN_BYTES; // <- default value in line ``` ... and actual parsing in isolation, ready for reuse like... ```java class MemoryUnitToByteConverter implements IStringConverter<Long> { private static Set<Character> sizeUnit = Sets.newHashSet('k', 'K', 'm', 'M', 'g', 'G', 't', 'T'); private final long defaultValue; public MemoryUnitToByteConverter(long defaultValue) { this.defaultValue = defaultValue; } @Override public Long convert(String memoryLimitArgument) { parseBytes(memoryLimitArgument); } long parseBytes(String memoryLimitArgument) { if (StringUtils.isNotEmpty(memoryLimitArgument)) { long memoryLimitArg = validateSizeString(memoryLimitArgument); if (positiveCheckStatic("memory-limit", memoryLimitArg)) { return memoryLimitArg; } } return defaultValue; } ... more internal helper methods } ``` Mailing List discussion thread: https://lists.apache.org/thread/b77bfnjlt62w7zywcs8tqklvyokpykok Mailing List voting thread: https://lists.apache.org/thread/0r3bh0h7f86g2x9odvrd1fp2gwddq904 [3] https://jcommander.org/#customtypesconvertersand_splitters