Skip to content

Commit

Permalink
Keep topic-level policies commands consistent with that for namespace… (
Browse files Browse the repository at this point in the history
apache#9215)


Fixes apache#9205
### Motivation
In apache#9108, we add some topic-level policies commands, and found some commands are not consistent with that for namespace-level.
For example,
On namespace-level, the policies commands are:
```
get-max-producers
set-max-producers
remove-max-producers
get-max-unacked-messages-per-subscription
set-max-unacked-messages-per-subscription
remove-max-unacked-messages-per-subscription
```
On topic-level, the polices commands are:
```
get-maxProducers
set-maxProducers
remove-maxProducers
get-max-unacked-messages-on-subscription
set-max-unacked-messages-on-subscription
remove-max-unacked-messages-on-subscription
```

### Modifications
Keep topic-level policies commands consistent with that for namespace
  • Loading branch information
315157973 authored Jan 26, 2021
1 parent 0223bb1 commit d557e0a
Show file tree
Hide file tree
Showing 5 changed files with 275 additions and 2 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package org.apache.pulsar.admin.cli;


import com.beust.jcommander.DefaultUsageFormatter;
import org.apache.pulsar.client.admin.PulsarAdmin;
import org.apache.pulsar.client.admin.Schemas;
import org.apache.pulsar.client.admin.Topics;
import org.mockito.Mockito;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertNotEquals;
import static org.testng.Assert.assertTrue;

public class DeprecatedCommanderTest {
PulsarAdmin admin;
Topics mockTopics;
Schemas mockSchemas;
CmdTopics cmdTopics;

@BeforeMethod
public void setup() {
admin = Mockito.mock(PulsarAdmin.class);
mockTopics = mock(Topics.class);
when(admin.topics()).thenReturn(mockTopics);
mockSchemas = mock(Schemas.class);
when(admin.schemas()).thenReturn(mockSchemas);
cmdTopics = new CmdTopics(admin);
}

@Test
public void testDeprecatedCommanderWorks() throws Exception {

DefaultUsageFormatter defaultUsageFormatter = new DefaultUsageFormatter(cmdTopics.jcommander);
StringBuilder builder = new StringBuilder();
defaultUsageFormatter.usage(builder);
String defaultOutput = builder.toString();

StringBuilder builder2 = new StringBuilder();
cmdTopics.jcommander.getUsageFormatter().usage(builder2);
String outputWithFiltered = builder2.toString();

assertNotEquals(outputWithFiltered, defaultOutput);
assertFalse(outputWithFiltered.contains("enable-deduplication"));
assertTrue(defaultOutput.contains("enable-deduplication"));
assertFalse(outputWithFiltered.contains("get-max-unacked-messages-on-consumer"));
assertTrue(defaultOutput.contains("get-max-unacked-messages-on-consumer"));
assertTrue(outputWithFiltered.contains("get-deduplication"));
assertTrue(defaultOutput.contains("get-deduplication"));

// annotation was changed to hidden, reset it.
cmdTopics = new CmdTopics(admin);
CmdUsageFormatter formatter = (CmdUsageFormatter)cmdTopics.jcommander.getUsageFormatter();
formatter.clearDeprecatedCommand();
StringBuilder builder3 = new StringBuilder();
cmdTopics.jcommander.getUsageFormatter().usage(builder3);
String outputAfterClean = builder3.toString();

assertEquals(outputAfterClean, defaultOutput);

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -776,6 +776,9 @@ public void topics() throws Exception {
cmdTopics.run(split("disable-deduplication persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).enableDeduplication("persistent://myprop/clust/ns1/ds1", false);

cmdTopics.run(split("set-deduplication persistent://myprop/clust/ns1/ds1 --disable"));
verify(mockTopics, times(2)).enableDeduplication("persistent://myprop/clust/ns1/ds1", false);

cmdTopics.run(split("get-replicator-dispatch-rate persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).getReplicatorDispatchRate("persistent://myprop/clust/ns1/ds1");

Expand All @@ -788,6 +791,8 @@ public void topics() throws Exception {

cmdTopics.run(split("get-deduplication-enabled persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).getDeduplicationEnabled("persistent://myprop/clust/ns1/ds1");
cmdTopics.run(split("get-deduplication persistent://myprop/clust/ns1/ds1"));
verify(mockTopics, times(2)).getDeduplicationEnabled("persistent://myprop/clust/ns1/ds1");

cmdTopics.run(split("get-offload-policies persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).getOffloadPolicies("persistent://myprop/clust/ns1/ds1");
Expand All @@ -811,17 +816,29 @@ public void topics() throws Exception {

cmdTopics.run(split("get-max-unacked-messages-on-consumer persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).getMaxUnackedMessagesOnConsumer("persistent://myprop/clust/ns1/ds1");
cmdTopics.run(split("get-max-unacked-messages-per-consumer persistent://myprop/clust/ns1/ds1"));
verify(mockTopics, times(2)).getMaxUnackedMessagesOnConsumer("persistent://myprop/clust/ns1/ds1");
cmdTopics.run(split("remove-max-unacked-messages-on-consumer persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).removeMaxUnackedMessagesOnConsumer("persistent://myprop/clust/ns1/ds1");
cmdTopics.run(split("remove-max-unacked-messages-per-consumer persistent://myprop/clust/ns1/ds1"));
verify(mockTopics, times(2)).removeMaxUnackedMessagesOnConsumer("persistent://myprop/clust/ns1/ds1");
cmdTopics.run(split("set-max-unacked-messages-on-consumer persistent://myprop/clust/ns1/ds1 -m 999"));
verify(mockTopics).setMaxUnackedMessagesOnConsumer("persistent://myprop/clust/ns1/ds1", 999);
cmdTopics.run(split("set-max-unacked-messages-per-consumer persistent://myprop/clust/ns1/ds1 -m 999"));
verify(mockTopics, times(2)).setMaxUnackedMessagesOnConsumer("persistent://myprop/clust/ns1/ds1", 999);

cmdTopics.run(split("get-max-unacked-messages-on-subscription persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).getMaxUnackedMessagesOnSubscription("persistent://myprop/clust/ns1/ds1");
cmdTopics.run(split("get-max-unacked-messages-per-subscription persistent://myprop/clust/ns1/ds1"));
verify(mockTopics, times(2)).getMaxUnackedMessagesOnSubscription("persistent://myprop/clust/ns1/ds1");
cmdTopics.run(split("remove-max-unacked-messages-on-subscription persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).removeMaxUnackedMessagesOnSubscription("persistent://myprop/clust/ns1/ds1");
cmdTopics.run(split("remove-max-unacked-messages-per-subscription persistent://myprop/clust/ns1/ds1"));
verify(mockTopics, times(2)).removeMaxUnackedMessagesOnSubscription("persistent://myprop/clust/ns1/ds1");
cmdTopics.run(split("set-max-unacked-messages-on-subscription persistent://myprop/clust/ns1/ds1 -m 99"));
verify(mockTopics).setMaxUnackedMessagesOnSubscription("persistent://myprop/clust/ns1/ds1", 99);
cmdTopics.run(split("set-max-unacked-messages-per-subscription persistent://myprop/clust/ns1/ds1 -m 99"));
verify(mockTopics, times(2)).setMaxUnackedMessagesOnSubscription("persistent://myprop/clust/ns1/ds1", 99);

cmdTopics.run(split("get-max-message-size persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).getMaxMessageSize("persistent://myprop/clust/ns1/ds1");
Expand All @@ -830,6 +847,20 @@ public void topics() throws Exception {
cmdTopics.run(split("set-max-message-size persistent://myprop/clust/ns1/ds1 -m 99"));
verify(mockTopics).setMaxMessageSize("persistent://myprop/clust/ns1/ds1", 99);

cmdTopics.run(split("get-max-producers persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).getMaxProducers("persistent://myprop/clust/ns1/ds1");
cmdTopics.run(split("remove-max-producers persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).removeMaxProducers("persistent://myprop/clust/ns1/ds1");
cmdTopics.run(split("set-max-producers persistent://myprop/clust/ns1/ds1 -p 99"));
verify(mockTopics).setMaxProducers("persistent://myprop/clust/ns1/ds1", 99);

cmdTopics.run(split("get-max-consumers persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).getMaxConsumers("persistent://myprop/clust/ns1/ds1");
cmdTopics.run(split("remove-max-consumers persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).removeMaxConsumers("persistent://myprop/clust/ns1/ds1");
cmdTopics.run(split("set-max-consumers persistent://myprop/clust/ns1/ds1 -c 99"));
verify(mockTopics).setMaxConsumers("persistent://myprop/clust/ns1/ds1", 99);

cmdTopics.run(split("get-deduplication-snapshot-interval persistent://myprop/clust/ns1/ds1"));
verify(mockTopics).getDeduplicationSnapshotInterval("persistent://myprop/clust/ns1/ds1");
cmdTopics.run(split("remove-deduplication-snapshot-interval persistent://myprop/clust/ns1/ds1"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@ public abstract class CmdBase {
public CmdBase(String cmdName, PulsarAdmin admin) {
this.admin = admin;
jcommander = new JCommander();
usageFormatter = new DefaultUsageFormatter(jcommander);
usageFormatter = new CmdUsageFormatter(jcommander);
jcommander.setProgramName("pulsar-admin " + cmdName);
jcommander.setUsageFormatter(usageFormatter);
}

private void tryShowCommandUsage() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
package org.apache.pulsar.admin.cli;

import static org.apache.commons.lang3.StringUtils.isNotBlank;

import com.beust.jcommander.IUsageFormatter;
import com.beust.jcommander.Parameter;
import com.beust.jcommander.ParameterException;
import com.beust.jcommander.Parameters;
Expand Down Expand Up @@ -119,11 +121,14 @@ public CmdTopics(PulsarAdmin admin) {
jcommander.addCommand("get-retention", new GetRetention());
jcommander.addCommand("set-retention", new SetRetention());
jcommander.addCommand("remove-retention", new RemoveRetention());

//deprecated commands
jcommander.addCommand("enable-deduplication", new EnableDeduplication());
jcommander.addCommand("disable-deduplication", new DisableDeduplication());
jcommander.addCommand("get-deduplication-enabled", new GetDeduplicationEnabled());

jcommander.addCommand("set-deduplication", new SetDeduplication());
jcommander.addCommand("get-deduplication", new GetDeduplicationEnabled());

jcommander.addCommand("get-deduplication-snapshot-interval", new GetDeduplicationSnapshotInterval());
jcommander.addCommand("set-deduplication-snapshot-interval", new SetDeduplicationSnapshotInterval());
jcommander.addCommand("remove-deduplication-snapshot-interval", new RemoveDeduplicationSnapshotInterval());
Expand Down Expand Up @@ -153,20 +158,34 @@ public CmdTopics(PulsarAdmin admin) {
jcommander.addCommand("get-compaction-threshold", new GetCompactionThreshold());
jcommander.addCommand("set-compaction-threshold", new SetCompactionThreshold());
jcommander.addCommand("remove-compaction-threshold", new RemoveCompactionThreshold());

//deprecated commands
jcommander.addCommand("get-max-unacked-messages-on-consumer", new GetMaxUnackedMessagesOnConsumer());
jcommander.addCommand("set-max-unacked-messages-on-consumer", new SetMaxUnackedMessagesOnConsumer());
jcommander.addCommand("remove-max-unacked-messages-on-consumer", new RemoveMaxUnackedMessagesOnConsumer());
jcommander.addCommand("get-max-unacked-messages-on-subscription", new GetMaxUnackedMessagesOnSubscription());
jcommander.addCommand("set-max-unacked-messages-on-subscription", new SetMaxUnackedMessagesOnSubscription());
jcommander.addCommand("remove-max-unacked-messages-on-subscription", new RemoveMaxUnackedMessagesOnSubscription());

jcommander.addCommand("get-max-unacked-messages-per-consumer", new GetMaxUnackedMessagesOnConsumer());
jcommander.addCommand("set-max-unacked-messages-per-consumer", new SetMaxUnackedMessagesOnConsumer());
jcommander.addCommand("remove-max-unacked-messages-per-consumer", new RemoveMaxUnackedMessagesOnConsumer());
jcommander.addCommand("get-max-unacked-messages-per-subscription", new GetMaxUnackedMessagesOnSubscription());
jcommander.addCommand("set-max-unacked-messages-per-subscription", new SetMaxUnackedMessagesOnSubscription());
jcommander.addCommand("remove-max-unacked-messages-per-subscription", new RemoveMaxUnackedMessagesOnSubscription());
jcommander.addCommand("get-publish-rate", new GetPublishRate());
jcommander.addCommand("set-publish-rate", new SetPublishRate());
jcommander.addCommand("remove-publish-rate", new RemovePublishRate());

//deprecated commands
jcommander.addCommand("get-maxProducers", new GetMaxProducers());
jcommander.addCommand("set-maxProducers", new SetMaxProducers());
jcommander.addCommand("remove-maxProducers", new RemoveMaxProducers());

jcommander.addCommand("get-max-producers", new GetMaxProducers());
jcommander.addCommand("set-max-producers", new SetMaxProducers());
jcommander.addCommand("remove-max-producers", new RemoveMaxProducers());

jcommander.addCommand("get-max-subscriptions-per-topic", new GetMaxSubscriptionsPerTopic());
jcommander.addCommand("set-max-subscriptions-per-topic", new SetMaxSubscriptionsPerTopic());
jcommander.addCommand("remove-max-subscriptions-per-topic", new RemoveMaxSubscriptionsPerTopic());
Expand All @@ -190,6 +209,30 @@ public CmdTopics(PulsarAdmin admin) {
jcommander.addCommand("get-subscribe-rate", new GetSubscribeRate());
jcommander.addCommand("set-subscribe-rate", new SetSubscribeRate());
jcommander.addCommand("remove-subscribe-rate", new RemoveSubscribeRate());

initDeprecatedCommands();
}

private void initDeprecatedCommands() {
IUsageFormatter usageFormatter = jcommander.getUsageFormatter();
if (usageFormatter instanceof CmdUsageFormatter) {
CmdUsageFormatter cmdUsageFormatter = (CmdUsageFormatter) usageFormatter;
cmdUsageFormatter.addDeprecatedCommand("enable-deduplication");
cmdUsageFormatter.addDeprecatedCommand("disable-deduplication");
cmdUsageFormatter.addDeprecatedCommand("get-deduplication-enabled");

cmdUsageFormatter.addDeprecatedCommand("get-max-unacked-messages-on-consumer");
cmdUsageFormatter.addDeprecatedCommand("remove-max-unacked-messages-on-consumer");
cmdUsageFormatter.addDeprecatedCommand("set-max-unacked-messages-on-consumer");

cmdUsageFormatter.addDeprecatedCommand("get-max-unacked-messages-on-subscription");
cmdUsageFormatter.addDeprecatedCommand("remove-max-unacked-messages-on-subscription");
cmdUsageFormatter.addDeprecatedCommand("set-max-unacked-messages-on-subscription");

cmdUsageFormatter.addDeprecatedCommand("get-maxProducers");
cmdUsageFormatter.addDeprecatedCommand("set-maxProducers");
cmdUsageFormatter.addDeprecatedCommand("remove-maxProducers");
}
}

@Parameters(commandDescription = "Get the list of topics under a namespace.")
Expand Down Expand Up @@ -1220,6 +1263,7 @@ void run() throws PulsarAdminException {
}
}

@Deprecated
@Parameters(commandDescription = "Enable the deduplication policy for a topic")
private class EnableDeduplication extends CliCommand {
@Parameter(description = "persistent://tenant/namespace/topic", required = true)
Expand All @@ -1232,6 +1276,7 @@ void run() throws PulsarAdminException {
}
}

@Deprecated
@Parameters(commandDescription = "Disable the deduplication policy for a topic")
private class DisableDeduplication extends CliCommand {
@Parameter(description = "persistent://tenant/namespace/topic", required = true)
Expand All @@ -1244,6 +1289,28 @@ void run() throws PulsarAdminException {
}
}

@Parameters(commandDescription = "Enable or disable deduplication for a topic")
private class SetDeduplication extends CliCommand {
@Parameter(description = "persistent://tenant/namespace/topic", required = true)
private java.util.List<String> params;

@Parameter(names = { "--enable", "-e" }, description = "Enable deduplication")
private boolean enable = false;

@Parameter(names = { "--disable", "-d" }, description = "Disable deduplication")
private boolean disable = false;

@Override
void run() throws PulsarAdminException {
String persistentTopic = validatePersistentTopic(params);

if (enable == disable) {
throw new ParameterException("Need to specify either --enable or --disable");
}
admin.topics().enableDeduplication(persistentTopic, enable);
}
}

@Parameters(commandDescription = "Get the deduplication policy for a topic")
private class GetDeduplicationEnabled extends CliCommand {
@Parameter(description = "persistent://tenant/namespace/topic", required = true)
Expand Down
Loading

0 comments on commit d557e0a

Please sign in to comment.