Skip to content

Commit 84497c7

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

File tree

4 files changed

+62
-9
lines changed

4 files changed

+62
-9
lines changed

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

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.util.function.Supplier;
3030
import java.util.stream.Collectors;
3131
import org.apache.commons.lang3.StringUtils;
32-
import org.apache.pulsar.cli.converters.picocli.ByteUnitToIntegerConverter;
3332
import org.apache.pulsar.cli.converters.picocli.ByteUnitToLongConverter;
3433
import org.apache.pulsar.cli.converters.picocli.TimeUnitToMillisConverter;
3534
import org.apache.pulsar.cli.converters.picocli.TimeUnitToSecondsConverter;
@@ -546,8 +545,8 @@ private class SetRetention extends CliCommand {
546545
+ "For example, 4096, 10M, 16G, 3T. The size unit suffix character can be k/K, m/M, g/G, or t/T. "
547546
+ "If the size unit suffix is not specified, the default unit is bytes. "
548547
+ "0 or less than 1MB means no retention and -1 means infinite size retention", required = true,
549-
converter = ByteUnitToIntegerConverter.class)
550-
private Integer sizeLimit;
548+
converter = ByteUnitToLongConverter.class)
549+
private Long sizeLimit;
551550

552551
@Option(names = { "--global", "-g" }, description = "Whether to set this policy globally. "
553552
+ "If set to true, the policy is replicated to other clusters asynchronously, "
@@ -560,8 +559,8 @@ void run() throws PulsarAdminException {
560559
final int retentionTimeInMin = retentionTimeInSec != -1
561560
? (int) TimeUnit.SECONDS.toMinutes(retentionTimeInSec)
562561
: retentionTimeInSec.intValue();
563-
final int retentionSizeInMB = sizeLimit != -1
564-
? (int) (sizeLimit / (1024 * 1024))
562+
final long retentionSizeInMB = sizeLimit != -1
563+
? (sizeLimit / (1024 * 1024))
565564
: sizeLimit;
566565
getTopicPolicies(isGlobal).setRetention(persistentTopic,
567566
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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@
5050
import org.apache.pulsar.client.impl.MessageIdImpl;
5151
import org.apache.pulsar.common.naming.TopicDomain;
5252
import org.apache.pulsar.common.policies.data.ManagedLedgerInternalStats.LedgerInfo;
53+
import org.apache.pulsar.common.policies.data.RetentionPolicies;
5354
import org.mockito.Mockito;
5455
import org.testng.Assert;
5556
import org.testng.annotations.AfterMethod;
@@ -260,4 +261,10 @@ public void testRunDeleteTopicsFromFileWithException() throws PulsarAdminExcepti
260261
mockTopics = mock(Topics.class);
261262
}
262263

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

0 commit comments

Comments
 (0)