提交 2e222b6d 编写于 作者: L lollipop

[ISSUE #1120] Add some test cases and remove redundant code.

上级 3884536c
...@@ -18,59 +18,42 @@ package org.apache.rocketmq.common.protocol; ...@@ -18,59 +18,42 @@ package org.apache.rocketmq.common.protocol;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.apache.rocketmq.common.MixAll; import org.apache.rocketmq.common.MixAll;
import org.apache.rocketmq.remoting.protocol.RemotingCommand;
public class NamespaceUtil { public class NamespaceUtil {
public static final char NAMESPACE_SEPARATOR = '%'; public static final char NAMESPACE_SEPARATOR = '%';
public static final String STRING_BLANK = "";
public static final int RETRY_PREFIX_LENGTH = MixAll.RETRY_GROUP_TOPIC_PREFIX.length(); public static final int RETRY_PREFIX_LENGTH = MixAll.RETRY_GROUP_TOPIC_PREFIX.length();
public static final int DLQ_PREFIX_LENGTH = MixAll.DLQ_GROUP_TOPIC_PREFIX.length(); public static final int DLQ_PREFIX_LENGTH = MixAll.DLQ_GROUP_TOPIC_PREFIX.length();
/**
* Parse namespace from request @see {@link RemotingCommand}, just like:
* (Topic_XXX, MQ_INST_XX) --> MQ_INST_XX%Topic_XXX
*
* @param request
* @param resource resource without namespace.
* @return resource with namespace.
*/
public static String withNamespace(RemotingCommand request, String resource) {
return wrapNamespace(getNamespaceFromRequest(request), resource);
}
/** /**
* Unpack namespace from resource, just like: * Unpack namespace from resource, just like:
* (1) MQ_INST_XX%Topic_XXX --> Topic_XXX * (1) MQ_INST_XX%Topic_XXX --> Topic_XXX
* (2) %RETRY%MQ_INST_XX%GID_XXX --> %RETRY%GID_XXX * (2) %RETRY%MQ_INST_XX%GID_XXX --> %RETRY%GID_XXX
* *
* @param resource * @param resourceWithNamespace, topic/groupId with namespace.
* @return * @return topic/groupId without namespace.
*/ */
public static String withoutNamespace(String resource) { public static String withoutNamespace(String resourceWithNamespace) {
if (StringUtils.isEmpty(resource) || isSystemResource(resource)) { if (StringUtils.isEmpty(resourceWithNamespace) || isSystemResource(resourceWithNamespace)) {
return resource; return resourceWithNamespace;
} }
if (isRetryTopic(resource)) { StringBuffer strBuffer = new StringBuffer();
int index = resource.indexOf(NAMESPACE_SEPARATOR, RETRY_PREFIX_LENGTH); if (isRetryTopic(resourceWithNamespace)) {
if (index > 0) { strBuffer.append(MixAll.RETRY_GROUP_TOPIC_PREFIX);
return MixAll.getRetryTopic(resource.substring(index + 1));
}
return resource;
} }
if (isDLQTopic(resourceWithNamespace)) {
if (isDLQTopic(resource)) { strBuffer.append(MixAll.DLQ_GROUP_TOPIC_PREFIX);
int index = resource.indexOf(NAMESPACE_SEPARATOR, DLQ_PREFIX_LENGTH);
if (index > 0) {
return MixAll.getDLQTopic(resource.substring(index + 1));
}
return resource;
} }
int index = resource.indexOf(NAMESPACE_SEPARATOR); String resourceWithoutRetryAndDLQ = withOutRetryAndDLQ(resourceWithNamespace);
int index = resourceWithoutRetryAndDLQ.indexOf(NAMESPACE_SEPARATOR);
if (index > 0) { if (index > 0) {
return resource.substring(index + 1); String resourceWithoutNamespace = resourceWithoutRetryAndDLQ.substring(index + 1);
return strBuffer.append(resourceWithoutNamespace).toString();
} }
return resource;
return resourceWithNamespace;
} }
/** /**
...@@ -80,56 +63,44 @@ public class NamespaceUtil { ...@@ -80,56 +63,44 @@ public class NamespaceUtil {
* (3) (%RETRY%MQ_INST_XX1%GID_XXX1, MQ_INST_XX1) --> %RETRY%GID_XXX1 * (3) (%RETRY%MQ_INST_XX1%GID_XXX1, MQ_INST_XX1) --> %RETRY%GID_XXX1
* (4) (%RETRY%MQ_INST_XX2%GID_XXX2, MQ_INST_XX3) --> %RETRY%MQ_INST_XX2%GID_XXX2 * (4) (%RETRY%MQ_INST_XX2%GID_XXX2, MQ_INST_XX3) --> %RETRY%MQ_INST_XX2%GID_XXX2
* *
* @param resource * @param resourceWithNamespace, topic/groupId with namespace.
* @param namespace * @param namespace, namespace to be unpacked.
* @return * @return topic/groupId without namespace.
*/ */
public static String withoutNamespace(String resource, String namespace) { public static String withoutNamespace(String resourceWithNamespace, String namespace) {
if (StringUtils.isEmpty(resource) || StringUtils.isEmpty(namespace)) { if (StringUtils.isEmpty(resourceWithNamespace) || StringUtils.isEmpty(namespace)) {
return resource; return resourceWithNamespace;
}
StringBuffer prefixBuffer = new StringBuffer();
if (isRetryTopic(resource)) {
prefixBuffer.append(MixAll.RETRY_GROUP_TOPIC_PREFIX);
} else if (isDLQTopic(resource)) {
prefixBuffer.append(MixAll.DLQ_GROUP_TOPIC_PREFIX);
} }
prefixBuffer.append(namespace).append(NAMESPACE_SEPARATOR);
if (resource.startsWith(prefixBuffer.toString())) { String resourceWithoutRetryAndDLQ = withOutRetryAndDLQ(resourceWithNamespace);
return withoutNamespace(resource); if (resourceWithoutRetryAndDLQ.startsWith(namespace + NAMESPACE_SEPARATOR)) {
return withoutNamespace(resourceWithNamespace);
} }
return resource; return resourceWithNamespace;
} }
public static String wrapNamespace(String namespace, String resource) { public static String wrapNamespace(String namespace, String resourceWithOutNamespace) {
if (StringUtils.isEmpty(namespace) || StringUtils.isEmpty(resource)) { if (StringUtils.isEmpty(namespace) || StringUtils.isEmpty(resourceWithOutNamespace)) {
return resource; return resourceWithOutNamespace;
} }
if (isSystemResource(resource)) { if (isSystemResource(resourceWithOutNamespace) || isAlreadyWithNamespace(resourceWithOutNamespace, namespace)) {
return resource; return resourceWithOutNamespace;
} }
if (isAlreadyWithNamespace(resource, namespace)) { String resourceWithoutRetryAndDLQ = withOutRetryAndDLQ(resourceWithOutNamespace);
return resource; StringBuffer strBuffer = new StringBuffer();
}
StringBuffer strBuffer = new StringBuffer().append(namespace).append(NAMESPACE_SEPARATOR);
if (isRetryTopic(resource)) { if (isRetryTopic(resourceWithOutNamespace)) {
strBuffer.append(resource.substring(RETRY_PREFIX_LENGTH)); strBuffer.append(MixAll.RETRY_GROUP_TOPIC_PREFIX);
return strBuffer.insert(0, MixAll.RETRY_GROUP_TOPIC_PREFIX).toString();
} }
if (isDLQTopic(resource)) { if (isDLQTopic(resourceWithOutNamespace)) {
strBuffer.append(resource.substring(DLQ_PREFIX_LENGTH)); strBuffer.append(MixAll.DLQ_GROUP_TOPIC_PREFIX);
return strBuffer.insert(0, MixAll.DLQ_GROUP_TOPIC_PREFIX).toString();
} }
return strBuffer.append(resource).toString(); return strBuffer.append(namespace).append(NAMESPACE_SEPARATOR).append(resourceWithoutRetryAndDLQ).toString();
} }
...@@ -138,19 +109,9 @@ public class NamespaceUtil { ...@@ -138,19 +109,9 @@ public class NamespaceUtil {
return false; return false;
} }
if (isRetryTopic(resource)) { String resourceWithoutRetryAndDLQ = withOutRetryAndDLQ(resource);
resource = resource.substring(RETRY_PREFIX_LENGTH);
}
if (isDLQTopic(resource)) {
resource = resource.substring(DLQ_PREFIX_LENGTH);
}
return resource.startsWith(namespace + NAMESPACE_SEPARATOR);
}
public static String withNamespaceAndRetry(RemotingCommand request, String consumerGroup) { return resourceWithoutRetryAndDLQ.startsWith(namespace + NAMESPACE_SEPARATOR);
return wrapNamespaceAndRetry(getNamespaceFromRequest(request), consumerGroup);
} }
public static String wrapNamespaceAndRetry(String namespace, String consumerGroup) { public static String wrapNamespaceAndRetry(String namespace, String consumerGroup) {
...@@ -164,51 +125,29 @@ public class NamespaceUtil { ...@@ -164,51 +125,29 @@ public class NamespaceUtil {
.toString(); .toString();
} }
public static String getNamespaceFromRequest(RemotingCommand request) { public static String getNamespaceFromResource(String resource) {
if (null == request || null == request.getExtFields()) { if (StringUtils.isEmpty(resource) || isSystemResource(resource)) {
return null; return STRING_BLANK;
}
String namespace;
switch (request.getCode()) {
case RequestCode.SEND_MESSAGE_V2:
namespace = request.getExtFields().get("n");
break;
default:
namespace = request.getExtFields().get("namespace");
break;
} }
String resourceWithoutRetryAndDLQ = withOutRetryAndDLQ(resource);
int index = resourceWithoutRetryAndDLQ.indexOf(NAMESPACE_SEPARATOR);
return namespace; return index > 0 ? resourceWithoutRetryAndDLQ.substring(0, index) : STRING_BLANK;
} }
public static String getNamespaceFromResource(String resource) { private static String withOutRetryAndDLQ(String originalResource) {
if (StringUtils.isEmpty(resource) || isSystemResource(resource)) { if (StringUtils.isEmpty(originalResource)) {
return ""; return STRING_BLANK;
} }
if (isRetryTopic(originalResource)) {
if (isRetryTopic(resource)) { return originalResource.substring(RETRY_PREFIX_LENGTH);
int index = resource.indexOf(NAMESPACE_SEPARATOR, RETRY_PREFIX_LENGTH);
if (index > 0) {
return resource.substring(RETRY_PREFIX_LENGTH, index);
}
return "";
} }
if (isDLQTopic(resource)) { if (isDLQTopic(originalResource)) {
int index = resource.indexOf(NAMESPACE_SEPARATOR, DLQ_PREFIX_LENGTH); return originalResource.substring(DLQ_PREFIX_LENGTH);
if (index > 0) {
return resource.substring(DLQ_PREFIX_LENGTH, index);
}
return "";
} }
int index = resource.indexOf(NAMESPACE_SEPARATOR); return originalResource;
if (index > 0) {
return resource.substring(0, index);
}
return "";
} }
private static boolean isSystemResource(String resource) { private static boolean isSystemResource(String resource) {
...@@ -216,11 +155,7 @@ public class NamespaceUtil { ...@@ -216,11 +155,7 @@ public class NamespaceUtil {
return false; return false;
} }
if (MixAll.isSystemTopic(resource)) { if (MixAll.isSystemTopic(resource) || MixAll.isSysConsumerGroup(resource)) {
return true;
}
if (MixAll.isSysConsumerGroup(resource)) {
return true; return true;
} }
...@@ -228,18 +163,10 @@ public class NamespaceUtil { ...@@ -228,18 +163,10 @@ public class NamespaceUtil {
} }
public static boolean isRetryTopic(String resource) { public static boolean isRetryTopic(String resource) {
if (StringUtils.isEmpty(resource)) { return StringUtils.isNotBlank(resource) && resource.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX);
return false;
}
return resource.startsWith(MixAll.RETRY_GROUP_TOPIC_PREFIX);
} }
public static boolean isDLQTopic(String resource) { public static boolean isDLQTopic(String resource) {
if (StringUtils.isEmpty(resource)) { return StringUtils.isNotBlank(resource) && resource.startsWith(MixAll.DLQ_GROUP_TOPIC_PREFIX);
return false;
}
return resource.startsWith(MixAll.DLQ_GROUP_TOPIC_PREFIX);
} }
} }
\ No newline at end of file
...@@ -28,14 +28,18 @@ public class NamespaceUtilTest { ...@@ -28,14 +28,18 @@ public class NamespaceUtilTest {
private static final String INSTANCE_ID_WRONG = "MQ_INST_XXX1"; private static final String INSTANCE_ID_WRONG = "MQ_INST_XXX1";
private static final String TOPIC = "TOPIC_XXX"; private static final String TOPIC = "TOPIC_XXX";
private static final String GROUP_ID = "GID_XXX"; private static final String GROUP_ID = "GID_XXX";
private static final String SYSTEM_TOPIC = "rmq_sys_topic";
private static final String GROUP_ID_WITH_NAMESPACE = INSTANCE_ID + NamespaceUtil.NAMESPACE_SEPARATOR + GROUP_ID; private static final String GROUP_ID_WITH_NAMESPACE = INSTANCE_ID + NamespaceUtil.NAMESPACE_SEPARATOR + GROUP_ID;
private static final String TOPIC_WITH_NAMESPACE = INSTANCE_ID + NamespaceUtil.NAMESPACE_SEPARATOR + TOPIC; private static final String TOPIC_WITH_NAMESPACE = INSTANCE_ID + NamespaceUtil.NAMESPACE_SEPARATOR + TOPIC;
private static final String RETRY_TOPIC = MixAll.RETRY_GROUP_TOPIC_PREFIX + GROUP_ID; private static final String RETRY_TOPIC = MixAll.RETRY_GROUP_TOPIC_PREFIX + GROUP_ID;
private static final String RETRY_TOPIC_WITH_NAMESPACE = private static final String RETRY_TOPIC_WITH_NAMESPACE =
MixAll.RETRY_GROUP_TOPIC_PREFIX + INSTANCE_ID + NamespaceUtil.NAMESPACE_SEPARATOR + GROUP_ID; MixAll.RETRY_GROUP_TOPIC_PREFIX + INSTANCE_ID + NamespaceUtil.NAMESPACE_SEPARATOR + GROUP_ID;
private static final String DLQ_TOPIC = MixAll.DLQ_GROUP_TOPIC_PREFIX + GROUP_ID;
private static final String DLQ_TOPIC_WITH_NAMESPACE =
MixAll.DLQ_GROUP_TOPIC_PREFIX + INSTANCE_ID + NamespaceUtil.NAMESPACE_SEPARATOR + GROUP_ID;
@Test @Test
public void withoutNamespace() { public void testWithoutNamespace() {
String topic = NamespaceUtil.withoutNamespace(TOPIC_WITH_NAMESPACE, INSTANCE_ID); String topic = NamespaceUtil.withoutNamespace(TOPIC_WITH_NAMESPACE, INSTANCE_ID);
Assert.assertEquals(topic, TOPIC); Assert.assertEquals(topic, TOPIC);
String topic1 = NamespaceUtil.withoutNamespace(TOPIC_WITH_NAMESPACE); String topic1 = NamespaceUtil.withoutNamespace(TOPIC_WITH_NAMESPACE);
...@@ -52,4 +56,36 @@ public class NamespaceUtilTest { ...@@ -52,4 +56,36 @@ public class NamespaceUtilTest {
Assert.assertEquals(consumerId2, RETRY_TOPIC_WITH_NAMESPACE); Assert.assertEquals(consumerId2, RETRY_TOPIC_WITH_NAMESPACE);
Assert.assertNotEquals(consumerId2, RETRY_TOPIC); Assert.assertNotEquals(consumerId2, RETRY_TOPIC);
} }
@Test
public void testWrapNamespace() {
String topic1 = NamespaceUtil.wrapNamespace(INSTANCE_ID, TOPIC);
Assert.assertEquals(topic1, TOPIC_WITH_NAMESPACE);
String topicWithNamespaceAgain = NamespaceUtil.wrapNamespace(INSTANCE_ID, topic1);
Assert.assertEquals(topicWithNamespaceAgain, TOPIC_WITH_NAMESPACE);
//Wrap retry topic
String retryTopicWithNamespace = NamespaceUtil.wrapNamespace(INSTANCE_ID, RETRY_TOPIC);
Assert.assertEquals(retryTopicWithNamespace, RETRY_TOPIC_WITH_NAMESPACE);
String retryTopicWithNamespaceAgain = NamespaceUtil.wrapNamespace(INSTANCE_ID, retryTopicWithNamespace);
Assert.assertEquals(retryTopicWithNamespaceAgain, retryTopicWithNamespace);
//Wrap DLQ topic
String dlqTopicWithNamespace = NamespaceUtil.wrapNamespace(INSTANCE_ID, DLQ_TOPIC);
Assert.assertEquals(dlqTopicWithNamespace, DLQ_TOPIC_WITH_NAMESPACE);
String dlqTopicWithNamespaceAgain = NamespaceUtil.wrapNamespace(INSTANCE_ID, dlqTopicWithNamespace);
Assert.assertEquals(dlqTopicWithNamespaceAgain, dlqTopicWithNamespace);
Assert.assertEquals(dlqTopicWithNamespaceAgain, DLQ_TOPIC_WITH_NAMESPACE );
//test system topic
String systemTopic = NamespaceUtil.wrapNamespace(INSTANCE_ID, SYSTEM_TOPIC);
Assert.assertEquals(systemTopic, SYSTEM_TOPIC);
}
@Test
public void testGetNamespaceFromResource(){
String namespaceExpectBlank = NamespaceUtil.getNamespaceFromResource(TOPIC);
Assert.assertEquals(namespaceExpectBlank, NamespaceUtil.STRING_BLANK);
String namespace = NamespaceUtil.getNamespaceFromResource(TOPIC_WITH_NAMESPACE);
Assert.assertEquals(namespace, INSTANCE_ID);
String namespaceFromRetryTopic = NamespaceUtil.getNamespaceFromResource(RETRY_TOPIC_WITH_NAMESPACE);
Assert.assertEquals(namespaceFromRetryTopic, INSTANCE_ID);
}
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册