开篇
- 这个系列主要用以分析mqadmin常见的比较核心的几个命令,主要包括订阅分组和topic的创建和删除、Topic的权限变更。
- 这篇文章主要是用来分析订阅分组创建和删除。
订阅分组的作用
public class PullMessageProcessor extends AsyncNettyRequestProcessor implements NettyRequestProcessor {
private RemotingCommand processRequest(final Channel channel, RemotingCommand request, boolean brokerAllowSuspend)
throws RemotingCommandException {
RemotingCommand response = RemotingCommand.createResponseCommand(PullMessageResponseHeader.class);
final PullMessageResponseHeader responseHeader = (PullMessageResponseHeader) response.readCustomHeader();
final PullMessageRequestHeader requestHeader =
(PullMessageRequestHeader) request.decodeCommandCustomHeader(PullMessageRequestHeader.class);
response.setOpaque(request.getOpaque());
// 查找SubscriptionGroupConfig,如果不存在就会直接返回错误
SubscriptionGroupConfig subscriptionGroupConfig =
this.brokerController.getSubscriptionGroupManager().findSubscriptionGroupConfig(requestHeader.getConsumerGroup());
if (null == subscriptionGroupConfig) {
response.setCode(ResponseCode.SUBSCRIPTION_GROUP_NOT_EXIST);
response.setRemark(String.format("subscription group [%s] does not exist, %s", requestHeader.getConsumerGroup(), FAQUrl.suggestTodo(FAQUrl.SUBSCRIPTION_GROUP_NOT_EXIST)));
return response;
}
}
- SubscriptionGroupConfig可以理解为消费分组和broker之间的契约,如果broker不存在消费分组的订阅关系,那么该消费分组就无法从该broker消费任何消息。
创建订阅分组
updateSubGroup
usage: mqadmin updateSubGroup [-a <arg>] [-b <arg>] [-c <arg>] [-d <arg>] -g <arg> [-h] [-i <arg>] [-m <arg>]
[-n <arg>] [-q <arg>] [-r <arg>] [-s <arg>] [-w <arg>]
-a,--notifyConsumerIdsChanged <arg> notify consumerId changed
-b,--brokerAddr <arg> create subscription group to which broker
-c,--clusterName <arg> create subscription group to which cluster
-d,--consumeBroadcastEnable <arg> broadcast
-g,--groupName <arg> consumer group name
-h,--help Print help
-i,--brokerId <arg> consumer from which broker id
-m,--consumeFromMinEnable <arg> from min offset
-n,--namesrvAddr <arg> Name server address list, eg: 192.168.0.1:9876;192.168.0.2:9876
-q,--retryQueueNums <arg> retry queue nums
-r,--retryMaxTimes <arg> retry max times
-s,--consumeEnable <arg> consume enable
-w,--whichBrokerWhenConsumeSlowly <arg> which broker id when consume slowly
- updateSubGroup负责创建订阅分组。
- updateSubGroup的命令参数中通过--groupName来指定consumer group name。
UpdateSubGroupSubCommand
public class UpdateSubGroupSubCommand implements SubCommand {
@Override
public String commandName() {
return "updateSubGroup";
}
@Override
public String commandDesc() {
return "Update or create subscription group";
}
@Override
public Options buildCommandlineOptions(Options options) {
Option opt = new Option("b", "brokerAddr", true, "create subscription group to which broker");
opt.setRequired(false);
options.addOption(opt);
opt = new Option("c", "clusterName", true, "create subscription group to which cluster");
opt.setRequired(false);
options.addOption(opt);
opt = new Option("g", "groupName", true, "consumer group name");
opt.setRequired(true);
options.addOption(opt);
opt = new Option("s", "consumeEnable", true, "consume enable");
opt.setRequired(false);
options.addOption(opt);
opt = new Option("m", "consumeFromMinEnable", true, "from min offset");
opt.setRequired(false);
options.addOption(opt);
opt = new Option("d", "consumeBroadcastEnable", true, "broadcast");
opt.setRequired(false);
options.addOption(opt);
opt = new Option("q", "retryQueueNums", true, "retry queue nums");
opt.setRequired(false);
options.addOption(opt);
opt = new Option("r", "retryMaxTimes", true, "retry max times");
opt.setRequired(false);
options.addOption(opt);
opt = new Option("i", "brokerId", true, "consumer from which broker id");
opt.setRequired(false);
options.addOption(opt);
opt = new Option("w", "whichBrokerWhenConsumeSlowly", true, "which broker id when consume slowly");
opt.setRequired(false);
options.addOption(opt);
opt = new Option("a", "notifyConsumerIdsChanged", true, "notify consumerId changed");
opt.setRequired(false);
options.addOption(opt);
return options;
}
@Override
public void execute(final CommandLine commandLine, final Options options,
RPCHook rpcHook) throws SubCommandException {
DefaultMQAdminExt defaultMQAdminExt = new DefaultMQAdminExt(rpcHook);
defaultMQAdminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
try {
SubscriptionGroupConfig subscriptionGroupConfig = new SubscriptionGroupConfig();
subscriptionGroupConfig.setConsumeBroadcastEnable(false);
subscriptionGroupConfig.setConsumeFromMinEnable(false);
// groupName
subscriptionGroupConfig.setGroupName(commandLine.getOptionValue('g').trim());
// consumeEnable
if (commandLine.hasOption('s')) {
subscriptionGroupConfig.setConsumeEnable(Boolean.parseBoolean(commandLine.getOptionValue('s')
.trim()));
}
// consumeFromMinEnable
if (commandLine.hasOption('m')) {
subscriptionGroupConfig.setConsumeFromMinEnable(Boolean.parseBoolean(commandLine
.getOptionValue('m').trim()));
}
// consumeBroadcastEnable
if (commandLine.hasOption('d')) {
subscriptionGroupConfig.setConsumeBroadcastEnable(Boolean.parseBoolean(commandLine
.getOptionValue('d').trim()));
}
// retryQueueNums
if (commandLine.hasOption('q')) {
subscriptionGroupConfig.setRetryQueueNums(Integer.parseInt(commandLine.getOptionValue('q')
.trim()));
}
// retryMaxTimes
if (commandLine.hasOption('r')) {
subscriptionGroupConfig.setRetryMaxTimes(Integer.parseInt(commandLine.getOptionValue('r')
.trim()));
}
// brokerId
if (commandLine.hasOption('i')) {
subscriptionGroupConfig.setBrokerId(Long.parseLong(commandLine.getOptionValue('i').trim()));
}
// whichBrokerWhenConsumeSlowly
if (commandLine.hasOption('w')) {
subscriptionGroupConfig.setWhichBrokerWhenConsumeSlowly(Long.parseLong(commandLine
.getOptionValue('w').trim()));
}
// notifyConsumerIdsChanged
if (commandLine.hasOption('a')) {
subscriptionGroupConfig.setNotifyConsumerIdsChangedEnable(Boolean.parseBoolean(commandLine
.getOptionValue('a').trim()));
}
if (commandLine.hasOption('b')) {
String addr = commandLine.getOptionValue('b').trim();
defaultMQAdminExt.start();
defaultMQAdminExt.createAndUpdateSubscriptionGroupConfig(addr, subscriptionGroupConfig);
System.out.printf("create subscription group to %s success.%n", addr);
System.out.printf("%s", subscriptionGroupConfig);
return;
} else if (commandLine.hasOption('c')) {
String clusterName = commandLine.getOptionValue('c').trim();
defaultMQAdminExt.start();
Set<String> masterSet =
CommandUtil.fetchMasterAddrByClusterName(defaultMQAdminExt, clusterName);
for (String addr : masterSet) {
try {
defaultMQAdminExt.createAndUpdateSubscriptionGroupConfig(addr, subscriptionGroupConfig);
System.out.printf("create subscription group to %s success.%n", addr);
} catch (Exception e) {
e.printStackTrace();
Thread.sleep(1000 * 1);
}
}
System.out.printf("%s", subscriptionGroupConfig);
return;
}
ServerUtil.printCommandLineHelp("mqadmin " + this.commandName(), options);
} catch (Exception e) {
throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
} finally {
defaultMQAdminExt.shutdown();
}
}
}
- UpdateSubGroupSubCommand是处理updateSubGroup命令的函数。
- 核心逻辑在于组装subscriptionGroupConfig对象,subscriptionGroupConfig就是订阅分组的变量。
- UpdateSubGroupSubCommand可以指定broker或者指定cluster,指定broker就在指定的broker上创建订阅分组,指定cluster就在cluster下的所有broker上创建订阅分组。
MQClientAPIImpl
public class MQClientAPIImpl {
public void createSubscriptionGroup(final String addr, final SubscriptionGroupConfig config,
final long timeoutMillis)
throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.UPDATE_AND_CREATE_SUBSCRIPTIONGROUP, null);
byte[] body = RemotingSerializable.encode(config);
request.setBody(body);
RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr),
request, timeoutMillis);
assert response != null;
switch (response.getCode()) {
case ResponseCode.SUCCESS: {
return;
}
default:
break;
}
throw new MQClientException(response.getCode(), response.getRemark());
}
}
- 创建订阅分组的RequestCode为UPDATE_AND_CREATE_SUBSCRIPTIONGROUP。
AdminBrokerProcessor
public class AdminBrokerProcessor extends AsyncNettyRequestProcessor implements NettyRequestProcessor {
private RemotingCommand updateAndCreateSubscriptionGroup(ChannelHandlerContext ctx, RemotingCommand request)
throws RemotingCommandException {
final RemotingCommand response = RemotingCommand.createResponseCommand(null);
log.info("updateAndCreateSubscriptionGroup called by {}", RemotingHelper.parseChannelRemoteAddr(ctx.channel()));
SubscriptionGroupConfig config = RemotingSerializable.decode(request.getBody(), SubscriptionGroupConfig.class);
if (config != null) {
this.brokerController.getSubscriptionGroupManager().updateSubscriptionGroupConfig(config);
}
response.setCode(ResponseCode.SUCCESS);
response.setRemark(null);
return response;
}
- updateAndCreateSubscriptionGroup在broker负责处理消费分组的创建逻辑。
- SubscriptionGroupManager负责保存订阅分组。
SubscriptionGroupManager
public class SubscriptionGroupManager extends ConfigManager {
private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.BROKER_LOGGER_NAME);
private final ConcurrentMap<String, SubscriptionGroupConfig> subscriptionGroupTable =
new ConcurrentHashMap<String, SubscriptionGroupConfig>(1024);
private final DataVersion dataVersion = new DataVersion();
public void updateSubscriptionGroupConfig(final SubscriptionGroupConfig config) {
SubscriptionGroupConfig old = this.subscriptionGroupTable.put(config.getGroupName(), config);
if (old != null) {
log.info("update subscription group config, old: {} new: {}", old, config);
} else {
log.info("create new subscription group, {}", config);
}
this.dataVersion.nextVersion();
this.persist();
}
}
- SubscriptionGroupManager的subscriptionGroupTable负责保存订阅分组。
删除订阅分组
deleteSubGroup
usage: mqadmin deleteSubGroup [-b <arg>] [-c <arg>] -g <arg> [-h] [-n <arg>]
-b,--brokerAddr <arg> delete subscription group from which broker
-c,--clusterName <arg> delete subscription group from which cluster
-g,--groupName <arg> subscription group name
-h,--help Print help
-n,--namesrvAddr <arg> Name server address list, eg: 192.168.0.1:9876;192.168.0.2:9876
- deleteSubGroup负责删除订阅分组。
- deleteSubGroup命令参数中通过--groupName来指定consumer group name。
DeleteSubscriptionGroupCommand
public class DeleteSubscriptionGroupCommand implements SubCommand {
@Override
public String commandName() {
return "deleteSubGroup";
}
@Override
public String commandDesc() {
return "Delete subscription group from broker.";
}
@Override
public Options buildCommandlineOptions(Options options) {
Option opt = new Option("b", "brokerAddr", true, "delete subscription group from which broker");
opt.setRequired(false);
options.addOption(opt);
opt = new Option("c", "clusterName", true, "delete subscription group from which cluster");
opt.setRequired(false);
options.addOption(opt);
opt = new Option("g", "groupName", true, "subscription group name");
opt.setRequired(true);
options.addOption(opt);
return options;
}
@Override
public void execute(CommandLine commandLine, Options options, RPCHook rpcHook) throws SubCommandException {
DefaultMQAdminExt adminExt = new DefaultMQAdminExt(rpcHook);
adminExt.setInstanceName(Long.toString(System.currentTimeMillis()));
try {
// groupName
String groupName = commandLine.getOptionValue('g').trim();
if (commandLine.hasOption('b')) {
String addr = commandLine.getOptionValue('b').trim();
adminExt.start();
adminExt.deleteSubscriptionGroup(addr, groupName);
System.out.printf("delete subscription group [%s] from broker [%s] success.%n", groupName,
addr);
return;
} else if (commandLine.hasOption('c')) {
String clusterName = commandLine.getOptionValue('c').trim();
adminExt.start();
Set<String> masterSet = CommandUtil.fetchMasterAddrByClusterName(adminExt, clusterName);
for (String master : masterSet) {
adminExt.deleteSubscriptionGroup(master, groupName);
System.out.printf(
"delete subscription group [%s] from broker [%s] in cluster [%s] success.%n",
groupName, master, clusterName);
}
try {
DeleteTopicSubCommand.deleteTopic(adminExt, clusterName, MixAll.RETRY_GROUP_TOPIC_PREFIX
+ groupName);
DeleteTopicSubCommand.deleteTopic(adminExt, clusterName, MixAll.DLQ_GROUP_TOPIC_PREFIX
+ groupName);
} catch (Exception e) {
e.printStackTrace();
}
return;
}
ServerUtil.printCommandLineHelp("mqadmin " + this.commandName(), options);
} catch (Exception e) {
throw new SubCommandException(this.getClass().getSimpleName() + " command failed", e);
} finally {
adminExt.shutdown();
}
}
}
- DeleteSubscriptionGroupCommand是处理deleteSubGroup命令的函数。
- 核心逻辑在于往指定的broker删除指定的consumer groupName。
- DeleteSubscriptionGroupCommand可以指定broker或者指定cluster,指定broker就在指定的broker上删除订阅分组,指定cluster就在cluster下的所有broker上删除订阅分组。
- 删除集群下订阅分组即消费分组,同时需要删除消费分组的重试Topic和死信Topic。
deleteSubscriptionGroup
public class MQClientAPIImpl {
public void deleteSubscriptionGroup(final String addr, final String groupName, final long timeoutMillis)
throws RemotingException, MQBrokerException, InterruptedException, MQClientException {
DeleteSubscriptionGroupRequestHeader requestHeader = new DeleteSubscriptionGroupRequestHeader();
requestHeader.setGroupName(groupName);
RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.DELETE_SUBSCRIPTIONGROUP, requestHeader);
RemotingCommand response = this.remotingClient.invokeSync(MixAll.brokerVIPChannel(this.clientConfig.isVipChannelEnabled(), addr),
request, timeoutMillis);
assert response != null;
switch (response.getCode()) {
case ResponseCode.SUCCESS: {
return;
}
default:
break;
}
throw new MQClientException(response.getCode(), response.getRemark());
}
}
- 删除订阅分组的RequestCode为DELETE_SUBSCRIPTIONGROUP。
AdminBrokerProcessor
public class AdminBrokerProcessor extends AsyncNettyRequestProcessor implements NettyRequestProcessor {
private RemotingCommand deleteSubscriptionGroup(ChannelHandlerContext ctx,
RemotingCommand request) throws RemotingCommandException {
final RemotingCommand response = RemotingCommand.createResponseCommand(null);
DeleteSubscriptionGroupRequestHeader requestHeader =
(DeleteSubscriptionGroupRequestHeader) request.decodeCommandCustomHeader(DeleteSubscriptionGroupRequestHeader.class);
log.info("deleteSubscriptionGroup called by {}", RemotingHelper.parseChannelRemoteAddr(ctx.channel()));
this.brokerController.getSubscriptionGroupManager().deleteSubscriptionGroupConfig(requestHeader.getGroupName());
if (this.brokerController.getBrokerConfig().isAutoDeleteUnusedStats()) {
this.brokerController.getBrokerStatsManager().onGroupDeleted(requestHeader.getGroupName());
}
response.setCode(ResponseCode.SUCCESS);
response.setRemark(null);
return response;
}
}
deleteSubscriptionGroup在broker负责处理订阅分组即消费分组的删除逻辑。
SubscriptionGroupManager负责删除订阅分组。
SubscriptionGroupManager
public class SubscriptionGroupManager extends ConfigManager {
private static final InternalLogger log = InternalLoggerFactory.getLogger(LoggerName.BROKER_LOGGER_NAME);
private final ConcurrentMap<String, SubscriptionGroupConfig> subscriptionGroupTable =
new ConcurrentHashMap<String, SubscriptionGroupConfig>(1024);
private final DataVersion dataVersion = new DataVersion();
private transient BrokerController brokerController;
public void deleteSubscriptionGroupConfig(final String groupName) {
SubscriptionGroupConfig old = this.subscriptionGroupTable.remove(groupName);
if (old != null) {
log.info("delete subscription group OK, subscription group:{}", old);
this.dataVersion.nextVersion();
this.persist();
} else {
log.warn("delete subscription group failed, subscription groupName: {} not exist", groupName);
}
}
}
- 从SubscriptionGroupManager的subscriptionGroupTable删除订阅分组。