Skip to content

Commit 9fc7851

Browse files
[ISSUE #9479] Relax topic length constraint when creating retry and dlq topics (#9480)
1 parent 8956768 commit 9fc7851

2 files changed

Lines changed: 60 additions & 2 deletions

File tree

common/src/main/java/org/apache/rocketmq/common/topic/TopicValidator.java

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.HashSet;
2020
import java.util.Set;
21+
import org.apache.rocketmq.common.MixAll;
2122
import org.apache.rocketmq.common.UtilAll;
2223

2324
public class TopicValidator {
@@ -38,6 +39,7 @@ public class TopicValidator {
3839

3940
public static final boolean[] VALID_CHAR_BIT_MAP = new boolean[128];
4041
private static final int TOPIC_MAX_LENGTH = 127;
42+
private static final int RETRY_OR_DLQ_TOPIC_MAX_LENGTH = 255;
4143

4244
private static final Set<String> SYSTEM_TOPIC_SET = new HashSet<>();
4345

@@ -111,8 +113,14 @@ public static ValidateTopicResult validateTopic(String topic) {
111113
return new ValidateTopicResult(false, "The specified topic contains illegal characters, allowing only ^[%|a-zA-Z0-9_-]+$");
112114
}
113115

114-
if (topic.length() > TOPIC_MAX_LENGTH) {
115-
return new ValidateTopicResult(false, "The specified topic is longer than topic max length.");
116+
if (topic.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX) || topic.startsWith(MixAll.DLQ_GROUP_TOPIC_PREFIX)) {
117+
if (topic.length() > RETRY_OR_DLQ_TOPIC_MAX_LENGTH) {
118+
return new ValidateTopicResult(false, "The specified topic is longer than topic max length.");
119+
}
120+
} else {
121+
if (topic.length() > TOPIC_MAX_LENGTH) {
122+
return new ValidateTopicResult(false, "The specified topic is longer than topic max length.");
123+
}
116124
}
117125

118126
return new ValidateTopicResult(true, "");

common/src/test/java/org/apache/rocketmq/common/topic/TopicValidatorTest.java

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,13 +35,37 @@ public void testTopicValidator_NotPass() {
3535
res = TopicValidator.validateTopic(generateString(128));
3636
assertThat(res.isValid()).isFalse();
3737
assertThat(res.getRemark()).contains("The specified topic is longer than topic max length.");
38+
39+
res = TopicValidator.validateTopic(generateString2(128));
40+
assertThat(res.isValid()).isFalse();
41+
assertThat(res.getRemark()).contains("The specified topic is longer than topic max length.");
42+
43+
res = TopicValidator.validateTopic(generateRetryTopic(256));
44+
assertThat(res.isValid()).isFalse();
45+
assertThat(res.getRemark()).contains("The specified topic is longer than topic max length.");
46+
47+
res = TopicValidator.validateTopic(generateDlqTopic(256));
48+
assertThat(res.isValid()).isFalse();
49+
assertThat(res.getRemark()).contains("The specified topic is longer than topic max length.");
3850
}
3951

4052
@Test
4153
public void testTopicValidator_Pass() {
4254
TopicValidator.ValidateTopicResult res = TopicValidator.validateTopic("TestTopic");
4355
assertThat(res.isValid()).isTrue();
4456
assertThat(res.getRemark()).isEmpty();
57+
58+
res = TopicValidator.validateTopic(generateString2(127));
59+
assertThat(res.isValid()).isTrue();
60+
assertThat(res.getRemark()).isEmpty();
61+
62+
res = TopicValidator.validateTopic(generateRetryTopic(255));
63+
assertThat(res.isValid()).isTrue();
64+
assertThat(res.getRemark()).isEmpty();
65+
66+
res = TopicValidator.validateTopic(generateDlqTopic(255));
67+
assertThat(res.isValid()).isTrue();
68+
assertThat(res.getRemark()).isEmpty();
4569
}
4670

4771
@Test
@@ -115,4 +139,30 @@ private static String generateString(int length) {
115139
}
116140
return stringBuffer.toString();
117141
}
142+
143+
private static String generateString2(int length) {
144+
StringBuilder stringBuilder = new StringBuilder();
145+
for (int i = 0; i < length; i++) {
146+
stringBuilder.append("a");
147+
}
148+
return stringBuilder.toString();
149+
}
150+
151+
private static String generateRetryTopic(int length) {
152+
StringBuilder stringBuilder = new StringBuilder();
153+
stringBuilder.append("%RETRY%");
154+
for (int i = 0; i < length - 7; i++) {
155+
stringBuilder.append("a");
156+
}
157+
return stringBuilder.toString();
158+
}
159+
160+
private static String generateDlqTopic(int length) {
161+
StringBuilder stringBuilder = new StringBuilder();
162+
stringBuilder.append("%DLQ%");
163+
for (int i = 0; i < length - 5; i++) {
164+
stringBuilder.append("a");
165+
}
166+
return stringBuilder.toString();
167+
}
118168
}

0 commit comments

Comments
 (0)