You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by te...@apache.org on 2022/05/26 07:08:20 UTC
[pulsar] branch master updated: [improve][admin] Enable users to specify TTL options in units other than seconds (#15657)
This is an automated email from the ASF dual-hosted git repository.
technoboy pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/pulsar.git
The following commit(s) were added to refs/heads/master by this push:
new 9b16619d45c [improve][admin] Enable users to specify TTL options in units other than seconds (#15657)
9b16619d45c is described below
commit 9b16619d45cea0508760c911497edaaec7028021
Author: Masahiro Sakamoto <ma...@yahoo-corp.jp>
AuthorDate: Thu May 26 16:08:13 2022 +0900
[improve][admin] Enable users to specify TTL options in units other than seconds (#15657)
---
.../pulsar/admin/cli/PulsarAdminToolTest.java | 5 +++++
.../org/apache/pulsar/admin/cli/CmdNamespaces.java | 21 +++++++++++++++++----
.../apache/pulsar/admin/cli/CmdTopicPolicies.java | 21 +++++++++++++++------
.../java/org/apache/pulsar/admin/cli/CmdTopics.java | 3 ++-
site2/docs/reference-pulsar-admin.md | 2 +-
5 files changed, 40 insertions(+), 12 deletions(-)
diff --git a/pulsar-client-tools-test/src/test/java/org/apache/pulsar/admin/cli/PulsarAdminToolTest.java b/pulsar-client-tools-test/src/test/java/org/apache/pulsar/admin/cli/PulsarAdminToolTest.java
index 1db5a1c5e80..dd48ff0f317 100644
--- a/pulsar-client-tools-test/src/test/java/org/apache/pulsar/admin/cli/PulsarAdminToolTest.java
+++ b/pulsar-client-tools-test/src/test/java/org/apache/pulsar/admin/cli/PulsarAdminToolTest.java
@@ -611,6 +611,9 @@ public class PulsarAdminToolTest {
when(admin.namespaces()).thenReturn(mockNamespaces);
namespaces = new CmdNamespaces(() -> admin);
+ namespaces.run(split("set-message-ttl myprop/clust/ns1 -ttl 6m"));
+ verify(mockNamespaces).setNamespaceMessageTTL("myprop/clust/ns1", 6 * 60);
+
namespaces.run(split("clear-backlog -b 0x80000000_0xffffffff myprop/clust/ns1 -force"));
verify(mockNamespaces).clearNamespaceBundleBacklog("myprop/clust/ns1", "0x80000000_0xffffffff");
@@ -1107,6 +1110,8 @@ public class PulsarAdminToolTest {
BacklogQuota.BacklogQuotaType.destination_storage);
//cmd with option cannot be executed repeatedly.
cmdTopics = new CmdTopicPolicies(() -> admin);
+ cmdTopics.run(split("set-message-ttl persistent://myprop/clust/ns1/ds1 -t 10h"));
+ verify(mockTopicsPolicies).setMessageTTL("persistent://myprop/clust/ns1/ds1", 10 * 60 * 60);
cmdTopics.run(split("set-backlog-quota persistent://myprop/clust/ns1/ds1 -lt 1w -p consumer_backlog_eviction"));
verify(mockTopicsPolicies).setBacklogQuota("persistent://myprop/clust/ns1/ds1",
BacklogQuota.builder()
diff --git a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdNamespaces.java b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdNamespaces.java
index 75a20c97374..ed0980b3ed6 100644
--- a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdNamespaces.java
+++ b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdNamespaces.java
@@ -386,14 +386,27 @@ public class CmdNamespaces extends CmdBase {
@Parameter(description = "tenant/namespace", required = true)
private java.util.List<String> params;
- @Parameter(names = { "--messageTTL", "-ttl" }, description = "Message TTL in seconds. "
- + "When the value is set to `0`, TTL is disabled.", required = true)
- private int messageTTL;
+ @Parameter(names = { "--messageTTL", "-ttl" },
+ description = "Message TTL in seconds (or minutes, hours, days, weeks eg: 100m, 3h, 2d, 5w). "
+ + "When the value is set to `0`, TTL is disabled.", required = true)
+ private String messageTTLStr;
@Override
void run() throws PulsarAdminException {
+ long messageTTLInSecond;
+ try {
+ messageTTLInSecond = RelativeTimeUtil.parseRelativeTimeInSeconds(messageTTLStr);
+ } catch (IllegalArgumentException e) {
+ throw new ParameterException(e.getMessage());
+ }
+
+ if (messageTTLInSecond < 0 || messageTTLInSecond > Integer.MAX_VALUE) {
+ throw new ParameterException(
+ String.format("Message TTL cannot be negative or greater than %d seconds", Integer.MAX_VALUE));
+ }
+
String namespace = validateNamespace(params);
- getAdmin().namespaces().setNamespaceMessageTTL(namespace, messageTTL);
+ getAdmin().namespaces().setNamespaceMessageTTL(namespace, (int) messageTTLInSecond);
}
}
diff --git a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTopicPolicies.java b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTopicPolicies.java
index 0bdd9c51582..1314f649c86 100644
--- a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTopicPolicies.java
+++ b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTopicPolicies.java
@@ -277,9 +277,10 @@ public class CmdTopicPolicies extends CmdBase {
@Parameter(description = "persistent://tenant/namespace/topic", required = true)
private java.util.List<String> params;
- @Parameter(names = { "-t", "--ttl" }, description = "Message TTL for topic in second, "
- + "allowed range from 1 to Integer.MAX_VALUE", required = true)
- private int messageTTLInSecond;
+ @Parameter(names = { "-t", "--ttl" },
+ description = "Message TTL for topic in seconds (or minutes, hours, days, weeks eg: 100m, 3h, 2d, 5w), "
+ + "allowed range from 1 to Integer.MAX_VALUE", required = true)
+ private String messageTTLStr;
@Parameter(names = { "--global", "-g" }, description = "Whether to set this policy globally. "
+ "If set to true, broker returned global topic policies")
@@ -287,12 +288,20 @@ public class CmdTopicPolicies extends CmdBase {
@Override
void run() throws PulsarAdminException {
- if (messageTTLInSecond < 0) {
- throw new ParameterException(String.format("Invalid retention policy type '%d'. ", messageTTLInSecond));
+ long messageTTLInSecond;
+ try {
+ messageTTLInSecond = RelativeTimeUtil.parseRelativeTimeInSeconds(messageTTLStr);
+ } catch (IllegalArgumentException e) {
+ throw new ParameterException(e.getMessage());
+ }
+
+ if (messageTTLInSecond < 0 || messageTTLInSecond > Integer.MAX_VALUE) {
+ throw new ParameterException(
+ String.format("Message TTL cannot be negative or greater than %d seconds", Integer.MAX_VALUE));
}
String persistentTopic = validatePersistentTopic(params);
- getTopicPolicies(isGlobal).setMessageTTL(persistentTopic, messageTTLInSecond);
+ getTopicPolicies(isGlobal).setMessageTTL(persistentTopic, (int) messageTTLInSecond);
}
}
diff --git a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTopics.java b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTopics.java
index 3c2d40c43cd..0d27d74b86a 100644
--- a/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTopics.java
+++ b/pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTopics.java
@@ -1653,7 +1653,8 @@ public class CmdTopics extends CmdBase {
}
if (messageTTLInSecond < 0 || messageTTLInSecond > Integer.MAX_VALUE) {
- throw new ParameterException(String.format("Invalid retention policy type '%d'. ", messageTTLInSecond));
+ throw new ParameterException(
+ String.format("Message TTL cannot be negative or greater than %d seconds", Integer.MAX_VALUE));
}
String persistentTopic = validatePersistentTopic(params);
diff --git a/site2/docs/reference-pulsar-admin.md b/site2/docs/reference-pulsar-admin.md
index d0ab78d1018..af179707872 100644
--- a/site2/docs/reference-pulsar-admin.md
+++ b/site2/docs/reference-pulsar-admin.md
@@ -1465,7 +1465,7 @@ Options
|Flag|Description|Default|
|----|---|---|
-|`-ttl`, `--messageTTL`|Message TTL in seconds. When the value is set to `0`, TTL is disabled. TTL is disabled by default. |0|
+|`-ttl`, `--messageTTL`|Message TTL in seconds (or minutes, hours, days, weeks eg: 100m, 3h, 2d, 5w). When the value is set to `0`, TTL is disabled. TTL is disabled by default. |0|
### `remove-message-ttl`
Remove the message TTL for a namespace.