Skip to content

Commit f7f5c1b

Browse files
committed
[fix][cli] Fix set-retention with >2GB size value for topic policy
1 parent 85b3d54 commit f7f5c1b

File tree

4 files changed

+63
-8
lines changed

4 files changed

+63
-8
lines changed

pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTopicPolicies.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -546,8 +546,8 @@ private class SetRetention extends CliCommand {
546546
+ "For example, 4096, 10M, 16G, 3T. The size unit suffix character can be k/K, m/M, g/G, or t/T. "
547547
+ "If the size unit suffix is not specified, the default unit is bytes. "
548548
+ "0 or less than 1MB means no retention and -1 means infinite size retention", required = true,
549-
converter = ByteUnitToIntegerConverter.class)
550-
private Integer sizeLimit;
549+
converter = ByteUnitToLongConverter.class)
550+
private Long sizeLimit;
551551

552552
@Option(names = { "--global", "-g" }, description = "Whether to set this policy globally. "
553553
+ "If set to true, the policy is replicated to other clusters asynchronously, "
@@ -560,8 +560,8 @@ void run() throws PulsarAdminException {
560560
final int retentionTimeInMin = retentionTimeInSec != -1
561561
? (int) TimeUnit.SECONDS.toMinutes(retentionTimeInSec)
562562
: retentionTimeInSec.intValue();
563-
final int retentionSizeInMB = sizeLimit != -1
564-
? (int) (sizeLimit / (1024 * 1024))
563+
final long retentionSizeInMB = sizeLimit != -1
564+
? (sizeLimit / (1024 * 1024))
565565
: sizeLimit;
566566
getTopicPolicies(isGlobal).setRetention(persistentTopic,
567567
new RetentionPolicies(retentionTimeInMin, retentionSizeInMB));

pulsar-client-tools/src/main/java/org/apache/pulsar/admin/cli/CmdTopics.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1858,17 +1858,17 @@ private class SetRetention extends CliCommand {
18581858
+ "For example, 4096, 10M, 16G, 3T. The size unit suffix character can be k/K, m/M, g/G, or t/T. "
18591859
+ "If the size unit suffix is not specified, the default unit is bytes. "
18601860
+ "0 or less than 1MB means no retention and -1 means infinite size retention", required = true,
1861-
converter = ByteUnitToIntegerConverter.class)
1862-
private Integer sizeLimit;
1861+
converter = ByteUnitToLongConverter.class)
1862+
private Long sizeLimit;
18631863

18641864
@Override
18651865
void run() throws PulsarAdminException {
18661866
String persistentTopic = validatePersistentTopic(topicName);
18671867
final int retentionTimeInMin = retentionTimeInSec != -1
18681868
? (int) TimeUnit.SECONDS.toMinutes(retentionTimeInSec)
18691869
: retentionTimeInSec.intValue();
1870-
final int retentionSizeInMB = sizeLimit != -1
1871-
? (int) (sizeLimit / (1024 * 1024))
1870+
final long retentionSizeInMB = sizeLimit != -1
1871+
? (sizeLimit / (1024 * 1024))
18721872
: sizeLimit;
18731873
getTopics().setRetention(persistentTopic, new RetentionPolicies(retentionTimeInMin, retentionSizeInMB));
18741874
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
package org.apache.pulsar.admin.cli;
20+
21+
import static org.mockito.ArgumentMatchers.anyBoolean;
22+
import static org.mockito.Mockito.mock;
23+
import static org.mockito.Mockito.times;
24+
import static org.mockito.Mockito.verify;
25+
import static org.mockito.Mockito.when;
26+
import org.apache.pulsar.client.admin.PulsarAdmin;
27+
import org.apache.pulsar.client.admin.TopicPolicies;
28+
import org.apache.pulsar.common.policies.data.RetentionPolicies;
29+
import org.testng.annotations.Test;
30+
31+
public class CmdTopicPoliciesTest {
32+
33+
@Test
34+
public void testSetRetentionCmd() throws Exception {
35+
TopicPolicies topicPolicies = mock(TopicPolicies.class);
36+
37+
PulsarAdmin admin = mock(PulsarAdmin.class);
38+
when(admin.topicPolicies(anyBoolean())).thenReturn(topicPolicies);
39+
40+
CmdTopicPolicies cmd = new CmdTopicPolicies(() -> admin);
41+
42+
cmd.run("set-retention public/default/topic -s 2T -t 2h".split("\\s+"));
43+
44+
verify(topicPolicies, times(1)).setRetention("persistent://public/default/topic",
45+
new RetentionPolicies(120, 2 * 1024 * 1024));
46+
}
47+
}

pulsar-client-tools/src/test/java/org/apache/pulsar/admin/cli/TestCmdTopics.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,12 @@
4646
import org.apache.pulsar.client.admin.PulsarAdmin;
4747
import org.apache.pulsar.client.admin.PulsarAdminException;
4848
import org.apache.pulsar.client.admin.Schemas;
49+
import org.apache.pulsar.client.admin.TopicPolicies;
4950
import org.apache.pulsar.client.admin.Topics;
5051
import org.apache.pulsar.client.impl.MessageIdImpl;
5152
import org.apache.pulsar.common.naming.TopicDomain;
5253
import org.apache.pulsar.common.policies.data.ManagedLedgerInternalStats.LedgerInfo;
54+
import org.apache.pulsar.common.policies.data.RetentionPolicies;
5355
import org.mockito.Mockito;
5456
import org.testng.Assert;
5557
import org.testng.annotations.AfterMethod;
@@ -260,4 +262,10 @@ public void testRunDeleteTopicsFromFileWithException() throws PulsarAdminExcepti
260262
mockTopics = mock(Topics.class);
261263
}
262264

265+
@Test
266+
public void testSetRetentionCmd() throws Exception {
267+
cmdTopics.run("set-retention public/default/topic -s 2T -t 2h".split("\\s+"));
268+
verify(mockTopics, times(1)).setRetention("persistent://public/default/topic",
269+
new RetentionPolicies(120, 2 * 1024 * 1024));
270+
}
263271
}

0 commit comments

Comments
 (0)