Skip to content

Commit 08a23a9

Browse files
authored
HDDS-15596. Hide deprecated CLI options (#10542)
1 parent 45b2051 commit 08a23a9

16 files changed

Lines changed: 157 additions & 360 deletions

File tree

hadoop-hdds/cli-common/src/main/java/org/apache/hadoop/hdds/cli/DeprecatedCliOption.java

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
import java.io.PrintWriter;
2121
import java.util.LinkedHashMap;
2222
import java.util.Map;
23-
import picocli.CommandLine;
23+
import java.util.Objects;
2424

2525
/**
2626
* Emits warnings when deprecated multi-character short CLI options are used.
@@ -51,24 +51,27 @@ private static Map<String, String> buildDeprecatedOptions() {
5151
}
5252

5353
/**
54-
* Print a warning to stderr for each deprecated option present on the command line.
54+
* If {@code arg} is a deprecated option (with or without {@code =value} part),
55+
* print a warning to stderr and return with the recommended replacement option.
5556
*/
56-
public static void warnIfMatched(CommandLine.ParseResult parseResult) {
57-
if (parseResult == null) {
58-
return;
57+
public static String toNonDeprecated(String arg, PrintWriter err) {
58+
if (arg == null || arg.isEmpty()) {
59+
return arg;
5960
}
6061

61-
for (CommandLine cli : parseResult.asCommandLineList()) {
62-
CommandLine.ParseResult subcommandResult = cli.getParseResult();
63-
if (subcommandResult.matchedOptions().isEmpty()) {
64-
continue;
65-
}
66-
for (Map.Entry<String, String> entry : DEPRECATED_OPTIONS.entrySet()) {
67-
if (subcommandResult.hasMatchedOption(entry.getKey())) {
68-
warn(cli.getErr(), entry.getKey(), entry.getValue());
69-
}
70-
}
62+
String result = arg;
63+
String[] parts = arg.split("=", 2);
64+
String opt = parts[0];
65+
String optToUse = DEPRECATED_OPTIONS.getOrDefault(opt, opt);
66+
67+
if (!Objects.equals(opt, optToUse)) {
68+
warn(err, opt, optToUse);
69+
result = parts.length == 2
70+
? optToUse + '=' + parts[1]
71+
: optToUse;
7172
}
73+
74+
return result;
7275
}
7376

7477
private static void warn(PrintWriter err, String deprecated, String replacement) {

hadoop-hdds/cli-common/src/main/java/org/apache/hadoop/hdds/cli/GenericCli.java

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,6 @@ public abstract class GenericCli implements GenericParentCommand {
4646

4747
private UserGroupInformation user;
4848

49-
private String configurationPath;
50-
private String deprecatedConfigurationPath;
51-
private boolean isConfigurationPathAdded = false;
52-
5349
@Option(names = {"--verbose"},
5450
scope = CommandLine.ScopeType.INHERIT,
5551
description = "More verbose output. Show the stack trace of the errors.")
@@ -63,22 +59,7 @@ public void setConfigurationOverrides(Map<String, String> configOverrides) {
6359
@Option(names = {"--conf"},
6460
description = "Path to custom configuration file.")
6561
public void setConfigurationPath(String configPath) {
66-
configurationPath = configPath;
67-
}
68-
69-
/** For backward compatibility. */
70-
@Option(names = {"-conf"}, hidden = true)
71-
@Deprecated
72-
@SuppressWarnings("DeprecatedIsStillUsed")
73-
public void setDeprecatedConfigurationPath(String configPath) {
74-
deprecatedConfigurationPath = configPath;
75-
}
76-
77-
private String getConfigurationPath() {
78-
if (configurationPath != null) {
79-
return configurationPath;
80-
}
81-
return deprecatedConfigurationPath;
62+
config.addResource(new Path(configPath));
8263
}
8364

8465
public GenericCli() {
@@ -87,16 +68,16 @@ public GenericCli() {
8768

8869
public GenericCli(CommandLine.IFactory factory) {
8970
cmd = new CommandLine(this, factory);
71+
ExtensibleParentCommand.addSubcommands(cmd);
72+
cmd.getCommandSpec().preprocessor((args, commandSpec, argSpec, info) -> {
73+
args.replaceAll(arg -> DeprecatedCliOption.toNonDeprecated(arg, cmd.getErr()));
74+
return false;
75+
});
76+
9077
cmd.setExecutionExceptionHandler((ex, commandLine, parseResult) -> {
9178
printError(ex);
9279
return EXECUTION_ERROR_EXIT_CODE;
9380
});
94-
cmd.setExecutionStrategy(parseResult -> {
95-
DeprecatedCliOption.warnIfMatched(parseResult);
96-
return new CommandLine.RunLast().execute(parseResult);
97-
});
98-
99-
ExtensibleParentCommand.addSubcommands(cmd);
10081
}
10182

10283
public void run(String[] argv) {
@@ -135,11 +116,6 @@ public void printError(Throwable error) {
135116

136117
@Override
137118
public OzoneConfiguration getOzoneConf() {
138-
String path = getConfigurationPath();
139-
if (path != null && !isConfigurationPathAdded) {
140-
config.addResource(new Path(path));
141-
isConfigurationPathAdded = true;
142-
}
143119
return config;
144120
}
145121

hadoop-hdds/cli-common/src/test/java/org/apache/hadoop/hdds/cli/TestGenericCliConfiguration.java

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -18,49 +18,44 @@
1818
package org.apache.hadoop.hdds.cli;
1919

2020
import static org.assertj.core.api.Assertions.assertThat;
21+
import static org.junit.jupiter.api.Assertions.assertThrows;
2122

2223
import java.io.IOException;
2324
import java.nio.charset.StandardCharsets;
2425
import java.nio.file.Files;
2526
import java.nio.file.Path;
27+
import org.junit.jupiter.api.BeforeAll;
2628
import org.junit.jupiter.api.Test;
29+
import picocli.CommandLine;
2730

2831
/**
2932
* Tests for {@link GenericCli} configuration option handling.
3033
*/
3134
public class TestGenericCliConfiguration {
3235

36+
private static Path deprecatedConf;
37+
private static Path preferredConf;
38+
3339
private static final class TestGenericCli extends GenericCli {
3440
}
3541

36-
@Test
37-
public void nonDeprecatedConfWinsWhenBothAreProvided() throws IOException {
38-
Path deprecatedConf = writeConf("deprecated");
39-
Path preferredConf = writeConf("preferred");
40-
41-
TestGenericCli cli = new TestGenericCli();
42-
cli.getCmd().parseArgs("-conf", deprecatedConf.toString(), "--conf",
43-
preferredConf.toString());
44-
45-
assertThat(cli.getOzoneConf().get("test.key")).isEqualTo("preferred");
42+
@BeforeAll
43+
static void setup() throws IOException {
44+
deprecatedConf = writeConf("deprecated");
45+
preferredConf = writeConf("preferred");
4646
}
4747

4848
@Test
49-
public void nonDeprecatedConfWinsRegardlessOfOrder() throws IOException {
50-
Path deprecatedConf = writeConf("deprecated");
51-
Path preferredConf = writeConf("preferred");
52-
53-
TestGenericCli cli = new TestGenericCli();
54-
cli.getCmd().parseArgs("--conf", preferredConf.toString(), "-conf",
55-
deprecatedConf.toString());
56-
57-
assertThat(cli.getOzoneConf().get("test.key")).isEqualTo("preferred");
49+
void confOptionsAreExclusive() {
50+
CommandLine cmd = new TestGenericCli().getCmd();
51+
assertThrows(CommandLine.OverwrittenOptionException.class,
52+
() -> cmd.parseArgs("-conf", deprecatedConf.toString(), "--conf", preferredConf.toString()));
53+
assertThrows(CommandLine.OverwrittenOptionException.class,
54+
() -> cmd.parseArgs("--conf", deprecatedConf.toString(), "-conf", preferredConf.toString()));
5855
}
5956

6057
@Test
61-
public void deprecatedConfIsUsedWhenNonDeprecatedIsAbsent() throws IOException {
62-
Path deprecatedConf = writeConf("deprecated");
63-
58+
void deprecatedConfIsUsedWhenNonDeprecatedIsAbsent() {
6459
TestGenericCli cli = new TestGenericCli();
6560
cli.getCmd().parseArgs("-conf", deprecatedConf.toString());
6661

hadoop-ozone/cli-admin/src/main/java/org/apache/hadoop/hdds/scm/cli/ScmOption.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,6 @@ public class ScmOption extends AbstractMixin {
4646
"ServiceId of SCM HA Cluster")
4747
private String scmServiceId;
4848

49-
/** For backward compatibility. */
50-
@CommandLine.Option(names = {"-id"}, hidden = true)
51-
@Deprecated
52-
@SuppressWarnings("DeprecatedIsStillUsed")
53-
private String deprecatedScmServiceId;
54-
5549
public ScmClient createScmClient() throws IOException {
5650
OzoneConfiguration conf = getOzoneConf();
5751
checkAndSetSCMAddressArg(conf);
@@ -76,9 +70,8 @@ private void checkAndSetSCMAddressArg(MutableConfigurationSource conf) {
7670

7771
// Use the scm service Id passed from the client.
7872

79-
String serviceId = getScmServiceId();
80-
if (StringUtils.isNotEmpty(serviceId)) {
81-
conf.set(ScmConfigKeys.OZONE_SCM_DEFAULT_SERVICE_ID, serviceId);
73+
if (StringUtils.isNotEmpty(scmServiceId)) {
74+
conf.set(ScmConfigKeys.OZONE_SCM_DEFAULT_SERVICE_ID, scmServiceId);
8275
} else if (StringUtils.isBlank(HddsUtils.getScmServiceId(conf))) {
8376
// Scm service id is not passed, and scm service id is not defined in
8477
// the config, assuming it should be non-HA cluster.
@@ -105,11 +98,4 @@ public SCMSecurityProtocol createScmSecurityClient() {
10598
public String getScm() {
10699
return scm;
107100
}
108-
109-
public String getScmServiceId() {
110-
if (StringUtils.isNotEmpty(scmServiceId)) {
111-
return scmServiceId;
112-
}
113-
return deprecatedScmServiceId;
114-
}
115101
}

hadoop-ozone/cli-admin/src/main/java/org/apache/hadoop/hdds/scm/cli/pipeline/FilterPipelineOptions.java

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -49,26 +49,16 @@ public class FilterPipelineOptions {
4949
description = "[deprecated] Filter pipelines by factor (e.g. ONE, THREE) (implies RATIS replication type)")
5050
private ReplicationFactor factor;
5151

52-
/** For backward compatibility. */
53-
@CommandLine.Option(
54-
names = {"-ffc"},
55-
hidden = true
56-
)
57-
@Deprecated
58-
@SuppressWarnings("DeprecatedIsStillUsed")
59-
private ReplicationFactor deprecatedFactor;
60-
6152
Optional<Predicate<? super Pipeline>> getReplicationFilter() {
62-
ReplicationFactor effectiveFactor = getFactor();
6353
boolean hasReplication = !Strings.isNullOrEmpty(replication);
64-
boolean hasFactor = effectiveFactor != null;
54+
boolean hasFactor = factor != null;
6555
boolean hasReplicationType = !Strings.isNullOrEmpty(replicationType);
6656

6757
if (hasFactor) {
6858
if (hasReplication) {
6959
throw new IllegalArgumentException("Factor and replication are mutually exclusive");
7060
}
71-
ReplicationConfig replicationConfig = RatisReplicationConfig.getInstance(effectiveFactor.toProto());
61+
ReplicationConfig replicationConfig = RatisReplicationConfig.getInstance(factor.toProto());
7262
return Optional.of(p -> replicationConfig.equals(p.getReplicationConfig()));
7363
}
7464

@@ -91,8 +81,4 @@ Optional<Predicate<? super Pipeline>> getReplicationFilter() {
9181

9282
return Optional.empty();
9383
}
94-
95-
private ReplicationFactor getFactor() {
96-
return factor != null ? factor : deprecatedFactor;
97-
}
9884
}

hadoop-ozone/cli-admin/src/main/java/org/apache/hadoop/hdds/scm/cli/pipeline/ListPipelinesSubcommand.java

Lines changed: 2 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -49,16 +49,6 @@ public class ListPipelinesSubcommand extends ScmSubcommand {
4949
defaultValue = "")
5050
private String state;
5151

52-
/** For backward compatibility. */
53-
@CommandLine.Option(
54-
names = {"-fst"},
55-
hidden = true,
56-
defaultValue = ""
57-
)
58-
@Deprecated
59-
@SuppressWarnings("DeprecatedIsStillUsed")
60-
private String deprecatedState;
61-
6252
@CommandLine.Option(
6353
names = {"--json"},
6454
defaultValue = "false",
@@ -73,10 +63,9 @@ public void execute(ScmClient scmClient) throws IOException {
7363
if (replicationFilter.isPresent()) {
7464
stream = stream.filter(replicationFilter.get());
7565
}
76-
String effectiveState = getState();
77-
if (!Strings.isNullOrEmpty(effectiveState)) {
66+
if (!Strings.isNullOrEmpty(state)) {
7867
stream = stream.filter(p -> p.getPipelineState().toString()
79-
.compareToIgnoreCase(effectiveState) == 0);
68+
.compareToIgnoreCase(state) == 0);
8069
}
8170

8271
if (json) {
@@ -87,11 +76,4 @@ public void execute(ScmClient scmClient) throws IOException {
8776
stream.forEach(System.out::println);
8877
}
8978
}
90-
91-
private String getState() {
92-
if (!Strings.isNullOrEmpty(state)) {
93-
return state;
94-
}
95-
return deprecatedState;
96-
}
9779
}

0 commit comments

Comments
 (0)