Title: Refactor CLI Argument Parsing Logic for Measurement Units using JCommander's custom converter

Motivation

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.

Goals

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.

In Scope

  • 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

Out of Scope

  • Creation of a “util” module is out of the scope of this PIP.

Design & Implementation Details

  • 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.

Note

  • 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)

Concrete Example

Consider the code snippet from CmdNamespaces.java 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.

BEFORE

  @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 ....

    // parsing like....
    int maxBlockSizeInBytes=OffloadPoliciesImpl.DEFAULT_MAX_BLOCK_SIZE_IN_BYTES;
    if(StringUtils.isNotEmpty(maxBlockSizeStr)){
        long maxBlockSize=validateSizeString(maxBlockSizeStr);
        if(positiveCheck("MaxBlockSize",maxBlockSize)
            &&maxValueCheck("MaxBlockSize",maxBlockSize,Integer.MAX_VALUE)) {
            maxBlockSizeInBytes=Long.valueOf(maxBlockSize).intValue();
        }
    }

AFTER

    @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=DEFAULT_MAX_BLOCK_SIZE_IN_BYTES; // <---- default value in line

... and actual parsing in isolation, ready for reuse like...

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
}

Links