Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
s920243400
Rocketmq
提交
2e222b6d
R
Rocketmq
项目概览
s920243400
/
Rocketmq
与 Fork 源项目一致
Fork自
Apache RocketMQ / Rocketmq
通知
1
Star
1
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
DevOps
流水线
流水线任务
计划
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
Rocketmq
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
DevOps
DevOps
流水线
流水线任务
计划
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
流水线任务
提交
Issue看板
提交
2e222b6d
编写于
4月 04, 2019
作者:
L
lollipop
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[ISSUE #1120] Add some test cases and remove redundant code.
上级
3884536c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
95 addition
and
132 deletion
+95
-132
common/src/main/java/org/apache/rocketmq/common/protocol/NamespaceUtil.java
...va/org/apache/rocketmq/common/protocol/NamespaceUtil.java
+58
-131
common/src/test/java/org/apache/rocketmq/common/protocol/NamespaceUtilTest.java
...rg/apache/rocketmq/common/protocol/NamespaceUtilTest.java
+37
-1
未找到文件。
common/src/main/java/org/apache/rocketmq/common/protocol/NamespaceUtil.java
浏览文件 @
2e222b6d
...
...
@@ -18,59 +18,42 @@ package org.apache.rocketmq.common.protocol;
import
org.apache.commons.lang3.StringUtils
;
import
org.apache.rocketmq.common.MixAll
;
import
org.apache.rocketmq.remoting.protocol.RemotingCommand
;
public
class
NamespaceUtil
{
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
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:
* (1) MQ_INST_XX%Topic_XXX --> Topic_XXX
* (2) %RETRY%MQ_INST_XX%GID_XXX --> %RETRY%GID_XXX
*
* @param resource
* @return
* @param resource
WithNamespace, topic/groupId with namespace.
* @return
topic/groupId without namespace.
*/
public
static
String
withoutNamespace
(
String
resource
)
{
if
(
StringUtils
.
isEmpty
(
resource
)
||
isSystemResource
(
resour
ce
))
{
return
resource
;
public
static
String
withoutNamespace
(
String
resource
WithNamespace
)
{
if
(
StringUtils
.
isEmpty
(
resource
WithNamespace
)
||
isSystemResource
(
resourceWithNamespa
ce
))
{
return
resource
WithNamespace
;
}
if
(
isRetryTopic
(
resource
))
{
int
index
=
resource
.
indexOf
(
NAMESPACE_SEPARATOR
,
RETRY_PREFIX_LENGTH
);
if
(
index
>
0
)
{
return
MixAll
.
getRetryTopic
(
resource
.
substring
(
index
+
1
));
}
return
resource
;
StringBuffer
strBuffer
=
new
StringBuffer
();
if
(
isRetryTopic
(
resourceWithNamespace
))
{
strBuffer
.
append
(
MixAll
.
RETRY_GROUP_TOPIC_PREFIX
);
}
if
(
isDLQTopic
(
resource
))
{
int
index
=
resource
.
indexOf
(
NAMESPACE_SEPARATOR
,
DLQ_PREFIX_LENGTH
);
if
(
index
>
0
)
{
return
MixAll
.
getDLQTopic
(
resource
.
substring
(
index
+
1
));
}
return
resource
;
if
(
isDLQTopic
(
resourceWithNamespace
))
{
strBuffer
.
append
(
MixAll
.
DLQ_GROUP_TOPIC_PREFIX
);
}
int
index
=
resource
.
indexOf
(
NAMESPACE_SEPARATOR
);
String
resourceWithoutRetryAndDLQ
=
withOutRetryAndDLQ
(
resourceWithNamespace
);
int
index
=
resourceWithoutRetryAndDLQ
.
indexOf
(
NAMESPACE_SEPARATOR
);
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 {
* (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
*
* @param resource
* @param namespace
* @return
* @param resource
WithNamespace, topic/groupId with namespace.
* @param namespace
, namespace to be unpacked.
* @return
topic/groupId without namespace.
*/
public
static
String
withoutNamespace
(
String
resource
,
String
namespace
)
{
if
(
StringUtils
.
isEmpty
(
resource
)
||
StringUtils
.
isEmpty
(
namespace
))
{
return
resource
;
}
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
);
public
static
String
withoutNamespace
(
String
resourceWithNamespace
,
String
namespace
)
{
if
(
StringUtils
.
isEmpty
(
resourceWithNamespace
)
||
StringUtils
.
isEmpty
(
namespace
))
{
return
resourceWithNamespace
;
}
prefixBuffer
.
append
(
namespace
).
append
(
NAMESPACE_SEPARATOR
);
if
(
resource
.
startsWith
(
prefixBuffer
.
toString
()))
{
return
withoutNamespace
(
resource
);
String
resourceWithoutRetryAndDLQ
=
withOutRetryAndDLQ
(
resourceWithNamespace
);
if
(
resourceWithoutRetryAndDLQ
.
startsWith
(
namespace
+
NAMESPACE_SEPARATOR
))
{
return
withoutNamespace
(
resourceWithNamespace
);
}
return
resource
;
return
resource
WithNamespace
;
}
public
static
String
wrapNamespace
(
String
namespace
,
String
resource
)
{
if
(
StringUtils
.
isEmpty
(
namespace
)
||
StringUtils
.
isEmpty
(
resource
))
{
return
resource
;
public
static
String
wrapNamespace
(
String
namespace
,
String
resource
WithOutNamespace
)
{
if
(
StringUtils
.
isEmpty
(
namespace
)
||
StringUtils
.
isEmpty
(
resource
WithOutNamespace
))
{
return
resource
WithOutNamespace
;
}
if
(
isSystemResource
(
resource
))
{
return
resource
;
if
(
isSystemResource
(
resource
WithOutNamespace
)
||
isAlreadyWithNamespace
(
resourceWithOutNamespace
,
namespace
))
{
return
resource
WithOutNamespace
;
}
if
(
isAlreadyWithNamespace
(
resource
,
namespace
))
{
return
resource
;
}
StringBuffer
strBuffer
=
new
StringBuffer
().
append
(
namespace
).
append
(
NAMESPACE_SEPARATOR
);
String
resourceWithoutRetryAndDLQ
=
withOutRetryAndDLQ
(
resourceWithOutNamespace
);
StringBuffer
strBuffer
=
new
StringBuffer
();
if
(
isRetryTopic
(
resource
))
{
strBuffer
.
append
(
resource
.
substring
(
RETRY_PREFIX_LENGTH
));
return
strBuffer
.
insert
(
0
,
MixAll
.
RETRY_GROUP_TOPIC_PREFIX
).
toString
();
if
(
isRetryTopic
(
resourceWithOutNamespace
))
{
strBuffer
.
append
(
MixAll
.
RETRY_GROUP_TOPIC_PREFIX
);
}
if
(
isDLQTopic
(
resource
))
{
strBuffer
.
append
(
resource
.
substring
(
DLQ_PREFIX_LENGTH
));
return
strBuffer
.
insert
(
0
,
MixAll
.
DLQ_GROUP_TOPIC_PREFIX
).
toString
();
if
(
isDLQTopic
(
resourceWithOutNamespace
))
{
strBuffer
.
append
(
MixAll
.
DLQ_GROUP_TOPIC_PREFIX
);
}
return
strBuffer
.
append
(
resource
).
toString
();
return
strBuffer
.
append
(
namespace
).
append
(
NAMESPACE_SEPARATOR
).
append
(
resourceWithoutRetryAndDLQ
).
toString
();
}
...
...
@@ -138,19 +109,9 @@ public class NamespaceUtil {
return
false
;
}
if
(
isRetryTopic
(
resource
))
{
resource
=
resource
.
substring
(
RETRY_PREFIX_LENGTH
);
}
if
(
isDLQTopic
(
resource
))
{
resource
=
resource
.
substring
(
DLQ_PREFIX_LENGTH
);
}
return
resource
.
startsWith
(
namespace
+
NAMESPACE_SEPARATOR
);
}
String
resourceWithoutRetryAndDLQ
=
withOutRetryAndDLQ
(
resource
);
public
static
String
withNamespaceAndRetry
(
RemotingCommand
request
,
String
consumerGroup
)
{
return
wrapNamespaceAndRetry
(
getNamespaceFromRequest
(
request
),
consumerGroup
);
return
resourceWithoutRetryAndDLQ
.
startsWith
(
namespace
+
NAMESPACE_SEPARATOR
);
}
public
static
String
wrapNamespaceAndRetry
(
String
namespace
,
String
consumerGroup
)
{
...
...
@@ -164,51 +125,29 @@ public class NamespaceUtil {
.
toString
();
}
public
static
String
getNamespaceFromRequest
(
RemotingCommand
request
)
{
if
(
null
==
request
||
null
==
request
.
getExtFields
())
{
return
null
;
}
String
namespace
;
switch
(
request
.
getCode
())
{
case
RequestCode
.
SEND_MESSAGE_V2
:
namespace
=
request
.
getExtFields
().
get
(
"n"
);
break
;
default
:
namespace
=
request
.
getExtFields
().
get
(
"namespace"
);
break
;
public
static
String
getNamespaceFromResource
(
String
resource
)
{
if
(
StringUtils
.
isEmpty
(
resource
)
||
isSystemResource
(
resource
))
{
return
STRING_BLANK
;
}
String
resourceWithoutRetryAndDLQ
=
withOutRetryAndDLQ
(
resource
);
int
index
=
resourceWithoutRetryAndDLQ
.
indexOf
(
NAMESPACE_SEPARATOR
);
return
namespace
;
return
index
>
0
?
resourceWithoutRetryAndDLQ
.
substring
(
0
,
index
)
:
STRING_BLANK
;
}
p
ublic
static
String
getNamespaceFromResource
(
String
r
esource
)
{
if
(
StringUtils
.
isEmpty
(
resource
)
||
isSystemResource
(
r
esource
))
{
return
""
;
p
rivate
static
String
withOutRetryAndDLQ
(
String
originalR
esource
)
{
if
(
StringUtils
.
isEmpty
(
originalR
esource
))
{
return
STRING_BLANK
;
}
if
(
isRetryTopic
(
resource
))
{
int
index
=
resource
.
indexOf
(
NAMESPACE_SEPARATOR
,
RETRY_PREFIX_LENGTH
);
if
(
index
>
0
)
{
return
resource
.
substring
(
RETRY_PREFIX_LENGTH
,
index
);
}
return
""
;
if
(
isRetryTopic
(
originalResource
))
{
return
originalResource
.
substring
(
RETRY_PREFIX_LENGTH
);
}
if
(
isDLQTopic
(
resource
))
{
int
index
=
resource
.
indexOf
(
NAMESPACE_SEPARATOR
,
DLQ_PREFIX_LENGTH
);
if
(
index
>
0
)
{
return
resource
.
substring
(
DLQ_PREFIX_LENGTH
,
index
);
}
return
""
;
if
(
isDLQTopic
(
originalResource
))
{
return
originalResource
.
substring
(
DLQ_PREFIX_LENGTH
);
}
int
index
=
resource
.
indexOf
(
NAMESPACE_SEPARATOR
);
if
(
index
>
0
)
{
return
resource
.
substring
(
0
,
index
);
}
return
""
;
return
originalResource
;
}
private
static
boolean
isSystemResource
(
String
resource
)
{
...
...
@@ -216,11 +155,7 @@ public class NamespaceUtil {
return
false
;
}
if
(
MixAll
.
isSystemTopic
(
resource
))
{
return
true
;
}
if
(
MixAll
.
isSysConsumerGroup
(
resource
))
{
if
(
MixAll
.
isSystemTopic
(
resource
)
||
MixAll
.
isSysConsumerGroup
(
resource
))
{
return
true
;
}
...
...
@@ -228,18 +163,10 @@ public class NamespaceUtil {
}
public
static
boolean
isRetryTopic
(
String
resource
)
{
if
(
StringUtils
.
isEmpty
(
resource
))
{
return
false
;
}
return
resource
.
startsWith
(
MixAll
.
RETRY_GROUP_TOPIC_PREFIX
);
return
StringUtils
.
isNotBlank
(
resource
)
&&
resource
.
startsWith
(
MixAll
.
RETRY_GROUP_TOPIC_PREFIX
);
}
public
static
boolean
isDLQTopic
(
String
resource
)
{
if
(
StringUtils
.
isEmpty
(
resource
))
{
return
false
;
}
return
resource
.
startsWith
(
MixAll
.
DLQ_GROUP_TOPIC_PREFIX
);
return
StringUtils
.
isNotBlank
(
resource
)
&&
resource
.
startsWith
(
MixAll
.
DLQ_GROUP_TOPIC_PREFIX
);
}
}
\ No newline at end of file
common/src/test/java/org/apache/rocketmq/common/protocol/NamespaceUtilTest.java
浏览文件 @
2e222b6d
...
...
@@ -28,14 +28,18 @@ public class NamespaceUtilTest {
private
static
final
String
INSTANCE_ID_WRONG
=
"MQ_INST_XXX1"
;
private
static
final
String
TOPIC
=
"TOPIC_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
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_WITH_NAMESPACE
=
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
public
void
w
ithoutNamespace
()
{
public
void
testW
ithoutNamespace
()
{
String
topic
=
NamespaceUtil
.
withoutNamespace
(
TOPIC_WITH_NAMESPACE
,
INSTANCE_ID
);
Assert
.
assertEquals
(
topic
,
TOPIC
);
String
topic1
=
NamespaceUtil
.
withoutNamespace
(
TOPIC_WITH_NAMESPACE
);
...
...
@@ -52,4 +56,36 @@ public class NamespaceUtilTest {
Assert
.
assertEquals
(
consumerId2
,
RETRY_TOPIC_WITH_NAMESPACE
);
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.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录