Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
Apache RocketMQ
Rocketmq
提交
cabc3838
R
Rocketmq
项目概览
Apache RocketMQ
/
Rocketmq
上一次同步 大约 3 年
通知
270
Star
16139
Fork
68
代码
文件
提交
分支
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看板
提交
cabc3838
编写于
11月 18, 2021
作者:
D
dongeforever
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Finish the remapping command
上级
e9cafe9f
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
88 addition
and
9 deletion
+88
-9
common/src/main/java/org/apache/rocketmq/common/LogicQueueMappingItem.java
...ava/org/apache/rocketmq/common/LogicQueueMappingItem.java
+18
-0
tools/src/main/java/org/apache/rocketmq/tools/admin/DefaultMQAdminExt.java
...va/org/apache/rocketmq/tools/admin/DefaultMQAdminExt.java
+6
-0
tools/src/main/java/org/apache/rocketmq/tools/admin/DefaultMQAdminExtImpl.java
...rg/apache/rocketmq/tools/admin/DefaultMQAdminExtImpl.java
+8
-1
tools/src/main/java/org/apache/rocketmq/tools/admin/MQAdminExt.java
...main/java/org/apache/rocketmq/tools/admin/MQAdminExt.java
+3
-0
tools/src/main/java/org/apache/rocketmq/tools/command/topic/RemappingStaticTopicSubCommand.java
...q/tools/command/topic/RemappingStaticTopicSubCommand.java
+53
-8
未找到文件。
common/src/main/java/org/apache/rocketmq/common/LogicQueueMappingItem.java
浏览文件 @
cabc3838
...
...
@@ -88,4 +88,22 @@ public class LogicQueueMappingItem {
public
long
getTimeOfEnd
()
{
return
timeOfEnd
;
}
public
void
setLogicOffset
(
long
logicOffset
)
{
this
.
logicOffset
=
logicOffset
;
}
@Override
public
String
toString
()
{
return
"LogicQueueMappingItem{"
+
"gen="
+
gen
+
", queueId="
+
queueId
+
", bname='"
+
bname
+
'\''
+
", logicOffset="
+
logicOffset
+
", startOffset="
+
startOffset
+
", endOffset="
+
endOffset
+
", timeOfStart="
+
timeOfStart
+
", timeOfEnd="
+
timeOfEnd
+
'}'
;
}
}
tools/src/main/java/org/apache/rocketmq/tools/admin/DefaultMQAdminExt.java
浏览文件 @
cabc3838
...
...
@@ -227,6 +227,12 @@ public class DefaultMQAdminExt extends ClientConfig implements MQAdminExt {
return
defaultMQAdminExtImpl
.
examineTopicStats
(
topic
);
}
@Override
public
TopicStatsTable
examineTopicStats
(
String
brokerAddr
,
String
topic
)
throws
RemotingException
,
MQClientException
,
InterruptedException
,
MQBrokerException
{
return
defaultMQAdminExtImpl
.
examineTopicStats
(
brokerAddr
,
topic
);
}
@Override
public
TopicList
fetchAllTopicList
()
throws
RemotingException
,
MQClientException
,
InterruptedException
{
return
this
.
defaultMQAdminExtImpl
.
fetchAllTopicList
();
...
...
tools/src/main/java/org/apache/rocketmq/tools/admin/DefaultMQAdminExtImpl.java
浏览文件 @
cabc3838
...
...
@@ -274,6 +274,13 @@ public class DefaultMQAdminExtImpl implements MQAdminExt, MQAdminExtInner {
return
topicStatsTable
;
}
@Override
public
TopicStatsTable
examineTopicStats
(
String
brokerAddr
,
String
topic
)
throws
RemotingException
,
MQClientException
,
InterruptedException
,
MQBrokerException
{
return
this
.
mqClientInstance
.
getMQClientAPIImpl
().
getTopicStatsInfo
(
brokerAddr
,
topic
,
timeoutMillis
);
}
@Override
public
TopicList
fetchAllTopicList
()
throws
RemotingException
,
MQClientException
,
InterruptedException
{
return
this
.
mqClientInstance
.
getMQClientAPIImpl
().
getTopicListFromNameServer
(
timeoutMillis
);
...
...
@@ -1092,7 +1099,7 @@ public class DefaultMQAdminExtImpl implements MQAdminExt, MQAdminExtInner {
public
void
createStaticTopic
(
final
String
addr
,
final
String
defaultTopic
,
final
TopicConfig
topicConfig
,
final
TopicQueueMappingDetail
mappingDetail
)
throws
MQClientException
{
this
.
mqClientInstance
.
getMQAdminImpl
().
createStaticTopic
(
addr
,
defaultTopic
,
topicConfig
,
mappingDetail
);
}
@Override
public
long
searchOffset
(
MessageQueue
mq
,
long
timestamp
)
throws
MQClientException
{
return
this
.
mqClientInstance
.
getMQAdminImpl
().
searchOffset
(
mq
,
timestamp
);
...
...
tools/src/main/java/org/apache/rocketmq/tools/admin/MQAdminExt.java
浏览文件 @
cabc3838
...
...
@@ -108,6 +108,9 @@ public interface MQAdminExt extends MQAdmin {
final
String
topic
)
throws
RemotingException
,
MQClientException
,
InterruptedException
,
MQBrokerException
;
TopicStatsTable
examineTopicStats
(
String
brokerAddr
,
final
String
topic
)
throws
RemotingException
,
MQClientException
,
InterruptedException
,
MQBrokerException
;
TopicList
fetchAllTopicList
()
throws
RemotingException
,
MQClientException
,
InterruptedException
;
TopicList
fetchTopicsByCLuster
(
...
...
tools/src/main/java/org/apache/rocketmq/tools/command/topic/RemappingStaticTopicSubCommand.java
浏览文件 @
cabc3838
...
...
@@ -16,6 +16,7 @@
*/
package
org.apache.rocketmq.tools.command.topic
;
import
com.alibaba.fastjson.JSON
;
import
com.google.common.collect.ImmutableList
;
import
org.apache.commons.cli.CommandLine
;
import
org.apache.commons.cli.Option
;
...
...
@@ -27,6 +28,9 @@ import org.apache.rocketmq.common.TopicConfigAndQueueMapping;
import
org.apache.rocketmq.common.TopicQueueMappingDetail
;
import
org.apache.rocketmq.common.TopicQueueMappingOne
;
import
org.apache.rocketmq.common.TopicQueueMappingUtils
;
import
org.apache.rocketmq.common.admin.TopicOffset
;
import
org.apache.rocketmq.common.admin.TopicStatsTable
;
import
org.apache.rocketmq.common.message.MessageQueue
;
import
org.apache.rocketmq.common.protocol.body.ClusterInfo
;
import
org.apache.rocketmq.common.protocol.route.QueueData
;
import
org.apache.rocketmq.common.protocol.route.TopicRouteData
;
...
...
@@ -200,8 +204,8 @@ public class RemappingStaticTopicSubCommand implements SubCommand {
Map
<
String
,
Integer
>
expectedBrokerNumMap
=
allocator
.
getBrokerNumMap
();
Queue
<
Integer
>
waitAssignQueues
=
new
ArrayDeque
<
Integer
>();
Map
<
Integer
,
String
>
expectedIdToBroker
=
new
HashMap
<>();
//the following logic will make sure that, for one broker,
only "take in" or "take out" queues
//It can't
, take in some queues but alse take
out some queues.
//the following logic will make sure that, for one broker,
either "map in" or "map out"
//It can't
both, map in some queues but also map
out some queues.
globalIdMap
.
forEach
((
queueId
,
mappingOne
)
->
{
String
leaderBroker
=
mappingOne
.
getBname
();
if
(
expectedBrokerNumMap
.
containsKey
(
leaderBroker
))
{
...
...
@@ -251,6 +255,7 @@ public class RemappingStaticTopicSubCommand implements SubCommand {
items
.
add
(
new
LogicQueueMappingItem
(
last
.
getGen
()
+
1
,
mapInConfig
.
getWriteQueueNums
()
-
1
,
mapInBroker
,
-
1
,
0
,
-
1
,
-
1
,
-
1
));
ImmutableList
<
LogicQueueMappingItem
>
resultItems
=
ImmutableList
.
copyOf
(
items
);
//Use the same object
mapInConfig
.
getMappingDetail
().
putMappingInfo
(
queueId
,
resultItems
);
mapOutConfig
.
getMappingDetail
().
putMappingInfo
(
queueId
,
resultItems
);
}
...
...
@@ -260,13 +265,53 @@ public class RemappingStaticTopicSubCommand implements SubCommand {
configMapping
.
getMappingDetail
().
setEpoch
(
epoch
);
configMapping
.
getMappingDetail
().
setTotalQueues
(
maxNum
);
});
//decide the new offset
//If some succeed, and others fail, it will cause inconsistent data
for
(
Map
.
Entry
<
String
,
TopicConfigAndQueueMapping
>
entry
:
existedTopicConfigMap
.
entrySet
())
{
String
broker
=
entry
.
getKey
();
// now do the remapping
//Step1: let the new leader can be write without the logicOffset
for
(
String
broker:
brokersToMapIn
)
{
String
addr
=
clientMetadata
.
findMasterBrokerAddr
(
broker
);
TopicConfigAndQueueMapping
configMapping
=
existedTopicConfigMap
.
get
(
broker
);
defaultMQAdminExt
.
createStaticTopic
(
addr
,
defaultMQAdminExt
.
getCreateTopicKey
(),
configMapping
,
configMapping
.
getMappingDetail
());
}
//Step2: forbid the write of old leader
for
(
String
broker:
brokersToMapOut
)
{
String
addr
=
clientMetadata
.
findMasterBrokerAddr
(
broker
);
TopicConfigAndQueueMapping
configMapping
=
existedTopicConfigMap
.
get
(
broker
);
defaultMQAdminExt
.
createStaticTopic
(
addr
,
defaultMQAdminExt
.
getCreateTopicKey
(),
configMapping
,
configMapping
.
getMappingDetail
());
}
//Step3: decide the logic offset
for
(
String
broker:
brokersToMapOut
)
{
String
addr
=
clientMetadata
.
findMasterBrokerAddr
(
broker
);
TopicStatsTable
statsTable
=
defaultMQAdminExt
.
examineTopicStats
(
addr
,
topic
);
TopicConfigAndQueueMapping
mapOutConfig
=
existedTopicConfigMap
.
get
(
broker
);
for
(
Map
.
Entry
<
Integer
,
ImmutableList
<
LogicQueueMappingItem
>>
entry
:
mapOutConfig
.
getMappingDetail
().
getHostedQueues
().
entrySet
())
{
ImmutableList
<
LogicQueueMappingItem
>
items
=
entry
.
getValue
();
Integer
globalId
=
entry
.
getKey
();
if
(
items
.
size
()
<
2
)
{
continue
;
}
LogicQueueMappingItem
newLeader
=
items
.
get
(
items
.
size
()
-
1
);
LogicQueueMappingItem
oldLeader
=
items
.
get
(
items
.
size
()
-
2
);
if
(
newLeader
.
getLogicOffset
()
>
0
)
{
continue
;
}
TopicOffset
topicOffset
=
statsTable
.
getOffsetTable
().
get
(
new
MessageQueue
(
topic
,
oldLeader
.
getBname
(),
oldLeader
.
getQueueId
()));
if
(
topicOffset
==
null
)
{
throw
new
RuntimeException
(
"Cannot get the max offset for old leader "
+
oldLeader
);
}
//TODO check the max offset, will it return -1?
if
(
topicOffset
.
getMaxOffset
()
<
oldLeader
.
getStartOffset
())
{
throw
new
RuntimeException
(
"The max offset is smaller then the start offset "
+
oldLeader
+
" "
+
topicOffset
.
getMaxOffset
());
}
newLeader
.
setLogicOffset
(
oldLeader
.
computeStaticQueueOffset
(
topicOffset
.
getMaxOffset
()
+
10000
));
TopicConfigAndQueueMapping
mapInConfig
=
existedTopicConfigMap
.
get
(
newLeader
.
getBname
());
//fresh the new leader
mapInConfig
.
getMappingDetail
().
putMappingInfo
(
globalId
,
items
);
}
}
//Step4: write to the new leader with logic offset
for
(
String
broker:
brokersToMapIn
)
{
String
addr
=
clientMetadata
.
findMasterBrokerAddr
(
broker
);
TopicConfigAndQueueMapping
configMapping
=
e
ntry
.
getValue
(
);
TopicConfigAndQueueMapping
configMapping
=
e
xistedTopicConfigMap
.
get
(
broker
);
defaultMQAdminExt
.
createStaticTopic
(
addr
,
defaultMQAdminExt
.
getCreateTopicKey
(),
configMapping
,
configMapping
.
getMappingDetail
());
}
}
catch
(
Exception
e
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录