From 7e6bd2df3c277c8da8c6acb91bc33541dbe73614 Mon Sep 17 00:00:00 2001 From: RongtongJin Date: Sat, 17 Dec 2022 12:31:44 +0800 Subject: [PATCH 01/15] Fix the issue that the slave role does not initialize the transientPool in controller mode --- .../processor/AdminBrokerProcessor.java | 411 ++++++------------ .../store/AllocateMappedFileService.java | 4 +- .../org/apache/rocketmq/store/CommitLog.java | 8 +- .../rocketmq/store/DefaultMessageStore.java | 15 +- .../rocketmq/store/TransientStorePool.java | 12 +- .../store/config/MessageStoreConfig.java | 6 +- 6 files changed, 169 insertions(+), 287 deletions(-) diff --git a/broker/src/main/java/org/apache/rocketmq/broker/processor/AdminBrokerProcessor.java b/broker/src/main/java/org/apache/rocketmq/broker/processor/AdminBrokerProcessor.java index 12eab475b8e..f46f688d293 100644 --- a/broker/src/main/java/org/apache/rocketmq/broker/processor/AdminBrokerProcessor.java +++ b/broker/src/main/java/org/apache/rocketmq/broker/processor/AdminBrokerProcessor.java @@ -174,6 +174,7 @@ import org.apache.rocketmq.remoting.rpc.RpcRequest; import org.apache.rocketmq.remoting.rpc.RpcResponse; import org.apache.rocketmq.store.ConsumeQueueExt; +import org.apache.rocketmq.store.DefaultMessageStore; import org.apache.rocketmq.store.MessageFilter; import org.apache.rocketmq.store.MessageStore; import org.apache.rocketmq.store.PutMessageResult; @@ -196,8 +197,7 @@ public AdminBrokerProcessor(final BrokerController brokerController) { this.brokerController = brokerController; } - @Override - public RemotingCommand processRequest(ChannelHandlerContext ctx, + @Override public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { switch (request.getCode()) { case RequestCode.UPDATE_AND_CREATE_TOPIC: @@ -362,8 +362,8 @@ private RemotingCommand getSubscriptionGroup(ChannelHandlerContext ctx, * @param request * @return */ - private RemotingCommand updateAndGetGroupForbidden(ChannelHandlerContext ctx, RemotingCommand request) - throws RemotingCommandException { + private RemotingCommand updateAndGetGroupForbidden(ChannelHandlerContext ctx, + RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); UpdateGroupForbiddenRequestHeader requestHeader = (UpdateGroupForbiddenRequestHeader) // request.decodeCommandCustomHeader(UpdateGroupForbiddenRequestHeader.class); @@ -387,8 +387,7 @@ private RemotingCommand updateAndGetGroupForbidden(ChannelHandlerContext ctx, Re return response; } - @Override - public boolean rejectRequest() { + @Override public boolean rejectRequest() { return false; } @@ -398,11 +397,9 @@ private synchronized RemotingCommand updateAndCreateTopic(ChannelHandlerContext if (validateSlave(response)) { return response; } - final CreateTopicRequestHeader requestHeader = - (CreateTopicRequestHeader) request.decodeCommandCustomHeader(CreateTopicRequestHeader.class); + final CreateTopicRequestHeader requestHeader = (CreateTopicRequestHeader) request.decodeCommandCustomHeader(CreateTopicRequestHeader.class); - LOGGER.info("Broker receive request to update or create topic={}, caller address={}", - requestHeader.getTopic(), RemotingHelper.parseChannelRemoteAddr(ctx.channel())); + LOGGER.info("Broker receive request to update or create topic={}, caller address={}", requestHeader.getTopic(), RemotingHelper.parseChannelRemoteAddr(ctx.channel())); String topic = requestHeader.getTopic(); @@ -444,8 +441,7 @@ private synchronized RemotingCommand updateAndCreateTopic(ChannelHandlerContext private synchronized RemotingCommand updateAndCreateStaticTopic(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - final CreateTopicRequestHeader requestHeader = - (CreateTopicRequestHeader) request.decodeCommandCustomHeader(CreateTopicRequestHeader.class); + final CreateTopicRequestHeader requestHeader = (CreateTopicRequestHeader) request.decodeCommandCustomHeader(CreateTopicRequestHeader.class); LOGGER.info("Broker receive request to update or create static topic={}, caller address={}", requestHeader.getTopic(), RemotingHelper.parseChannelRemoteAddr(ctx.channel())); final TopicQueueMappingDetail topicQueueMappingDetail = RemotingSerializable.decode(request.getBody(), TopicQueueMappingDetail.class); @@ -498,11 +494,9 @@ private synchronized RemotingCommand deleteTopic(ChannelHandlerContext ctx, if (validateSlave(response)) { return response; } - DeleteTopicRequestHeader requestHeader = - (DeleteTopicRequestHeader) request.decodeCommandCustomHeader(DeleteTopicRequestHeader.class); + DeleteTopicRequestHeader requestHeader = (DeleteTopicRequestHeader) request.decodeCommandCustomHeader(DeleteTopicRequestHeader.class); - LOGGER.info("AdminBrokerProcessor#deleteTopic: broker receive request to delete topic={}, caller={}", - requestHeader.getTopic(), RemotingHelper.parseChannelRemoteAddr(ctx.channel())); + LOGGER.info("AdminBrokerProcessor#deleteTopic: broker receive request to delete topic={}, caller={}", requestHeader.getTopic(), RemotingHelper.parseChannelRemoteAddr(ctx.channel())); String topic = requestHeader.getTopic(); TopicValidator.ValidateTopicResult result = TopicValidator.validateTopic(topic); @@ -523,8 +517,7 @@ private synchronized RemotingCommand deleteTopic(ChannelHandlerContext ctx, this.brokerController.getTopicQueueMappingManager().delete(requestHeader.getTopic()); this.brokerController.getConsumerOffsetManager().cleanOffsetByTopic(requestHeader.getTopic()); this.brokerController.getPopInflightMessageCounter().clearInFlightMessageNumByTopicName(requestHeader.getTopic()); - this.brokerController.getMessageStore() - .cleanUnusedTopic(this.brokerController.getTopicConfigManager().getTopicConfigTable().keySet()); + this.brokerController.getMessageStore().cleanUnusedTopic(this.brokerController.getTopicConfigManager().getTopicConfigTable().keySet()); if (this.brokerController.getBrokerConfig().isAutoDeleteUnusedStats()) { this.brokerController.getBrokerStatsManager().onTopicDeleted(requestHeader.getTopic()); } @@ -538,8 +531,7 @@ private synchronized RemotingCommand updateAndCreateAccessConfig(ChannelHandlerC RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - final CreateAccessConfigRequestHeader requestHeader = - (CreateAccessConfigRequestHeader) request.decodeCommandCustomHeader(CreateAccessConfigRequestHeader.class); + final CreateAccessConfigRequestHeader requestHeader = (CreateAccessConfigRequestHeader) request.decodeCommandCustomHeader(CreateAccessConfigRequestHeader.class); PlainAccessConfig accessConfig = new PlainAccessConfig(); accessConfig.setAccessKey(requestHeader.getAccessKey()); @@ -580,8 +572,7 @@ private synchronized RemotingCommand deleteAccessConfig(ChannelHandlerContext ct RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - final DeleteAccessConfigRequestHeader requestHeader = - (DeleteAccessConfigRequestHeader) request.decodeCommandCustomHeader(DeleteAccessConfigRequestHeader.class); + final DeleteAccessConfigRequestHeader requestHeader = (DeleteAccessConfigRequestHeader) request.decodeCommandCustomHeader(DeleteAccessConfigRequestHeader.class); LOGGER.info("DeleteAccessConfig called by {}", RemotingHelper.parseChannelRemoteAddr(ctx.channel())); try { @@ -616,13 +607,11 @@ private synchronized RemotingCommand updateGlobalWhiteAddrsConfig(ChannelHandler final RemotingCommand response = RemotingCommand.createResponseCommand(null); - final UpdateGlobalWhiteAddrsConfigRequestHeader requestHeader = - (UpdateGlobalWhiteAddrsConfigRequestHeader) request.decodeCommandCustomHeader(UpdateGlobalWhiteAddrsConfigRequestHeader.class); + final UpdateGlobalWhiteAddrsConfigRequestHeader requestHeader = (UpdateGlobalWhiteAddrsConfigRequestHeader) request.decodeCommandCustomHeader(UpdateGlobalWhiteAddrsConfigRequestHeader.class); try { AccessValidator accessValidator = this.brokerController.getAccessValidatorMap().get(PlainAccessValidator.class); - if (accessValidator.updateGlobalWhiteAddrsConfig(UtilAll.split(requestHeader.getGlobalWhiteAddrs(), ","), - requestHeader.getAclFileFullPath())) { + if (accessValidator.updateGlobalWhiteAddrsConfig(UtilAll.split(requestHeader.getGlobalWhiteAddrs(), ","), requestHeader.getAclFileFullPath())) { response.setCode(ResponseCode.SUCCESS); response.setOpaque(request.getOpaque()); response.markResponseType(); @@ -692,8 +681,7 @@ private RemotingCommand getBrokerClusterAclConfig(ChannelHandlerContext ctx, Rem private RemotingCommand getUnknownCmdResponse(ChannelHandlerContext ctx, RemotingCommand request) { String error = " request type " + request.getCode() + " not supported"; - final RemotingCommand response = - RemotingCommand.createResponseCommand(RemotingSysResponseCode.REQUEST_CODE_NOT_SUPPORTED, error); + final RemotingCommand response = RemotingCommand.createResponseCommand(RemotingSysResponseCode.REQUEST_CODE_NOT_SUPPORTED, error); return response; } @@ -789,8 +777,7 @@ private synchronized RemotingCommand updateBrokerConfig(ChannelHandlerContext ct return response; } } catch (UnsupportedEncodingException e) { - LOGGER.error("AdminBrokerProcessor#updateBrokerConfig: unexpected error, caller={}", - callerAddress, e); + LOGGER.error("AdminBrokerProcessor#updateBrokerConfig: unexpected error, caller={}", callerAddress, e); response.setCode(ResponseCode.SYSTEM_ERROR); response.setRemark("UnsupportedEncodingException " + e); return response; @@ -812,8 +799,7 @@ private RemotingCommand getBrokerConfig(ChannelHandlerContext ctx, RemotingComma try { response.setBody(content.getBytes(MixAll.DEFAULT_CHARSET)); } catch (UnsupportedEncodingException e) { - LOGGER.error("AdminBrokerProcessor#getBrokerConfig: unexpected error, caller={}", - RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e); + LOGGER.error("AdminBrokerProcessor#getBrokerConfig: unexpected error, caller={}", RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e); response.setCode(ResponseCode.SYSTEM_ERROR); response.setRemark("UnsupportedEncodingException " + e); @@ -865,8 +851,7 @@ private RemotingCommand rewriteRequestForStaticTopic(SearchOffsetRequestHeader r throw rpcResponse.getException(); } SearchOffsetResponseHeader offsetResponseHeader = (SearchOffsetResponseHeader) rpcResponse.getHeader(); - if (offsetResponseHeader.getOffset() < 0 - || item.checkIfEndOffsetDecided() && offsetResponseHeader.getOffset() >= item.getEndOffset()) { + if (offsetResponseHeader.getOffset() < 0 || item.checkIfEndOffsetDecided() && offsetResponseHeader.getOffset() >= item.getEndOffset()) { continue; } else { offset = item.computeStaticQueueOffsetStrictly(offsetResponseHeader.getOffset()); @@ -889,8 +874,7 @@ private RemotingCommand searchOffsetByTimestamp(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(SearchOffsetResponseHeader.class); final SearchOffsetResponseHeader responseHeader = (SearchOffsetResponseHeader) response.readCustomHeader(); - final SearchOffsetRequestHeader requestHeader = - (SearchOffsetRequestHeader) request.decodeCommandCustomHeader(SearchOffsetRequestHeader.class); + final SearchOffsetRequestHeader requestHeader = (SearchOffsetRequestHeader) request.decodeCommandCustomHeader(SearchOffsetRequestHeader.class); TopicQueueMappingContext mappingContext = this.brokerController.getTopicQueueMappingManager().buildTopicQueueMappingContext(requestHeader); @@ -899,8 +883,7 @@ private RemotingCommand searchOffsetByTimestamp(ChannelHandlerContext ctx, return rewriteResult; } - long offset = this.brokerController.getMessageStore().getOffsetInQueueByTime(requestHeader.getTopic(), requestHeader.getQueueId(), - requestHeader.getTimestamp()); + long offset = this.brokerController.getMessageStore().getOffsetInQueueByTime(requestHeader.getTopic(), requestHeader.getQueueId(), requestHeader.getTimestamp()); responseHeader.setOffset(offset); @@ -958,8 +941,7 @@ private RemotingCommand getMaxOffset(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(GetMaxOffsetResponseHeader.class); final GetMaxOffsetResponseHeader responseHeader = (GetMaxOffsetResponseHeader) response.readCustomHeader(); - final GetMaxOffsetRequestHeader requestHeader = - (GetMaxOffsetRequestHeader) request.decodeCommandCustomHeader(GetMaxOffsetRequestHeader.class); + final GetMaxOffsetRequestHeader requestHeader = (GetMaxOffsetRequestHeader) request.decodeCommandCustomHeader(GetMaxOffsetRequestHeader.class); TopicQueueMappingContext mappingContext = this.brokerController.getTopicQueueMappingManager().buildTopicQueueMappingContext(requestHeader); RemotingCommand rewriteResult = rewriteRequestForStaticTopic(requestHeader, mappingContext); @@ -984,8 +966,7 @@ private CompletableFuture handleGetMinOffsetForStaticTopic(RpcReque TopicQueueMappingDetail mappingDetail = mappingContext.getMappingDetail(); if (!mappingContext.isLeader()) { //this may not - return CompletableFuture.completedFuture(new RpcResponse(new RpcException(ResponseCode.NOT_LEADER_FOR_QUEUE, - String.format("%s-%d is not leader in broker %s, request code %d", mappingContext.getTopic(), mappingContext.getGlobalId(), mappingDetail.getBname(), request.getCode())))); + return CompletableFuture.completedFuture(new RpcResponse(new RpcException(ResponseCode.NOT_LEADER_FOR_QUEUE, String.format("%s-%d is not leader in broker %s, request code %d", mappingContext.getTopic(), mappingContext.getGlobalId(), mappingDetail.getBname(), request.getCode())))); } GetMinOffsetRequestHeader requestHeader = (GetMinOffsetRequestHeader) request.getHeader(); LogicQueueMappingItem mappingItem = TopicQueueMappingUtils.findLogicQueueMappingItem(mappingContext.getMappingItemList(), 0L, true); @@ -1034,8 +1015,7 @@ private CompletableFuture handleGetMinOffset(RpcRequest request) { private RemotingCommand getMinOffset(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { - final GetMinOffsetRequestHeader requestHeader = - (GetMinOffsetRequestHeader) request.decodeCommandCustomHeader(GetMinOffsetRequestHeader.class); + final GetMinOffsetRequestHeader requestHeader = (GetMinOffsetRequestHeader) request.decodeCommandCustomHeader(GetMinOffsetRequestHeader.class); try { CompletableFuture responseFuture = handleGetMinOffset(new RpcRequest(RequestCode.GET_MIN_OFFSET, requestHeader, null)); RpcResponse rpcResponse = responseFuture.get(); @@ -1083,8 +1063,7 @@ private RemotingCommand getEarliestMsgStoretime(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(GetEarliestMsgStoretimeResponseHeader.class); final GetEarliestMsgStoretimeResponseHeader responseHeader = (GetEarliestMsgStoretimeResponseHeader) response.readCustomHeader(); - final GetEarliestMsgStoretimeRequestHeader requestHeader = - (GetEarliestMsgStoretimeRequestHeader) request.decodeCommandCustomHeader(GetEarliestMsgStoretimeRequestHeader.class); + final GetEarliestMsgStoretimeRequestHeader requestHeader = (GetEarliestMsgStoretimeRequestHeader) request.decodeCommandCustomHeader(GetEarliestMsgStoretimeRequestHeader.class); TopicQueueMappingContext mappingContext = this.brokerController.getTopicQueueMappingManager().buildTopicQueueMappingContext(requestHeader, false); RemotingCommand rewriteResult = rewriteRequestForStaticTopic(requestHeader, mappingContext); @@ -1092,8 +1071,7 @@ private RemotingCommand getEarliestMsgStoretime(ChannelHandlerContext ctx, return rewriteResult; } - long timestamp = - this.brokerController.getMessageStore().getEarliestMessageTime(requestHeader.getTopic(), requestHeader.getQueueId()); + long timestamp = this.brokerController.getMessageStore().getEarliestMessageTime(requestHeader.getTopic(), requestHeader.getQueueId()); responseHeader.setTimestamp(timestamp); response.setCode(ResponseCode.SUCCESS); @@ -1121,10 +1099,7 @@ private RemotingCommand lockBatchMQ(ChannelHandlerContext ctx, LockBatchRequestBody requestBody = LockBatchRequestBody.decode(request.getBody(), LockBatchRequestBody.class); Set lockOKMQSet = new HashSet<>(); - Set selfLockOKMQSet = this.brokerController.getRebalanceLockManager().tryLockBatch( - requestBody.getConsumerGroup(), - requestBody.getMqSet(), - requestBody.getClientId()); + Set selfLockOKMQSet = this.brokerController.getRebalanceLockManager().tryLockBatch(requestBody.getConsumerGroup(), requestBody.getMqSet(), requestBody.getClientId()); if (requestBody.isOnlyThisBroker() || !brokerController.getBrokerConfig().isLockInStrictMode()) { lockOKMQSet = selfLockOKMQSet; } else { @@ -1154,25 +1129,22 @@ private RemotingCommand lockBatchMQ(ChannelHandlerContext ctx, requestBody.setOnlyThisBroker(true); for (Long brokerId : addrMap.keySet()) { try { - this.brokerController.getBrokerOuterAPI().lockBatchMQAsync(addrMap.get(brokerId), - requestBody, 1000, new LockCallback() { - @Override - public void onSuccess(Set lockOKMQSet) { - for (MessageQueue mq : lockOKMQSet) { - if (!mqLockMap.containsKey(mq)) { - mqLockMap.put(mq, 0); - } - mqLockMap.put(mq, mqLockMap.get(mq) + 1); + this.brokerController.getBrokerOuterAPI().lockBatchMQAsync(addrMap.get(brokerId), requestBody, 1000, new LockCallback() { + @Override public void onSuccess(Set lockOKMQSet) { + for (MessageQueue mq : lockOKMQSet) { + if (!mqLockMap.containsKey(mq)) { + mqLockMap.put(mq, 0); } - countDownLatch.countDown(); - } - - @Override - public void onException(Throwable e) { - LOGGER.warn("lockBatchMQAsync on {} failed, {}", addrMap.get(brokerId), e); - countDownLatch.countDown(); + mqLockMap.put(mq, mqLockMap.get(mq) + 1); } - }); + countDownLatch.countDown(); + } + + @Override public void onException(Throwable e) { + LOGGER.warn("lockBatchMQAsync on {} failed, {}", addrMap.get(brokerId), e); + countDownLatch.countDown(); + } + }); } catch (Exception e) { LOGGER.warn("lockBatchMQAsync on {} failed, {}", addrMap.get(brokerId), e); countDownLatch.countDown(); @@ -1208,10 +1180,7 @@ private RemotingCommand unlockBatchMQ(ChannelHandlerContext ctx, UnlockBatchRequestBody requestBody = UnlockBatchRequestBody.decode(request.getBody(), UnlockBatchRequestBody.class); if (requestBody.isOnlyThisBroker() || !this.brokerController.getBrokerConfig().isLockInStrictMode()) { - this.brokerController.getRebalanceLockManager().unlockBatch( - requestBody.getConsumerGroup(), - requestBody.getMqSet(), - requestBody.getClientId()); + this.brokerController.getRebalanceLockManager().unlockBatch(requestBody.getConsumerGroup(), requestBody.getMqSet(), requestBody.getClientId()); } else { requestBody.setOnlyThisBroker(true); BrokerMemberGroup memberGroup = this.brokerController.getBrokerMemberGroup(); @@ -1221,13 +1190,11 @@ private RemotingCommand unlockBatchMQ(ChannelHandlerContext ctx, for (Long brokerId : addrMap.keySet()) { try { this.brokerController.getBrokerOuterAPI().unlockBatchMQAsync(addrMap.get(brokerId), requestBody, 1000, new UnlockCallback() { - @Override - public void onSuccess() { + @Override public void onSuccess() { } - @Override - public void onException(Throwable e) { + @Override public void onException(Throwable e) { LOGGER.warn("unlockBatchMQ exception on {}, {}", addrMap.get(brokerId), e); } }); @@ -1243,15 +1210,14 @@ public void onException(Throwable e) { return response; } - private RemotingCommand updateAndCreateSubscriptionGroup(ChannelHandlerContext ctx, RemotingCommand request) - throws RemotingCommandException { + private RemotingCommand updateAndCreateSubscriptionGroup(ChannelHandlerContext ctx, + RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); if (validateSlave(response)) { return response; } - LOGGER.info("AdminBrokerProcessor#updateAndCreateSubscriptionGroup called by {}", - RemotingHelper.parseChannelRemoteAddr(ctx.channel())); + LOGGER.info("AdminBrokerProcessor#updateAndCreateSubscriptionGroup called by {}", RemotingHelper.parseChannelRemoteAddr(ctx.channel())); SubscriptionGroupConfig config = RemotingSerializable.decode(request.getBody(), SubscriptionGroupConfig.class); if (config != null) { @@ -1278,8 +1244,7 @@ private void initConsumerOffset(String clientHost, String groupName, int mode, T } } this.brokerController.getConsumerOffsetManager().commitOffset(clientHost, groupName, topic, queueId, offset); - LOGGER.info("AdminBrokerProcessor#initConsumerOffset: consumerGroup={}, topic={}, queueId={}, offset={}", - groupName, topic, queueId, offset); + LOGGER.info("AdminBrokerProcessor#initConsumerOffset: consumerGroup={}, topic={}, queueId={}, offset={}", groupName, topic, queueId, offset); } } @@ -1316,11 +1281,9 @@ private RemotingCommand deleteSubscriptionGroup(ChannelHandlerContext ctx, if (validateSlave(response)) { return response; } - DeleteSubscriptionGroupRequestHeader requestHeader = - (DeleteSubscriptionGroupRequestHeader) request.decodeCommandCustomHeader(DeleteSubscriptionGroupRequestHeader.class); + DeleteSubscriptionGroupRequestHeader requestHeader = (DeleteSubscriptionGroupRequestHeader) request.decodeCommandCustomHeader(DeleteSubscriptionGroupRequestHeader.class); - LOGGER.info("AdminBrokerProcessor#deleteSubscriptionGroup, caller={}", - RemotingHelper.parseChannelRemoteAddr(ctx.channel())); + LOGGER.info("AdminBrokerProcessor#deleteSubscriptionGroup, caller={}", RemotingHelper.parseChannelRemoteAddr(ctx.channel())); this.brokerController.getSubscriptionGroupManager().deleteSubscriptionGroupConfig(requestHeader.getGroupName()); @@ -1340,8 +1303,7 @@ private RemotingCommand deleteSubscriptionGroup(ChannelHandlerContext ctx, private RemotingCommand getTopicStatsInfo(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - final GetTopicStatsInfoRequestHeader requestHeader = - (GetTopicStatsInfoRequestHeader) request.decodeCommandCustomHeader(GetTopicStatsInfoRequestHeader.class); + final GetTopicStatsInfoRequestHeader requestHeader = (GetTopicStatsInfoRequestHeader) request.decodeCommandCustomHeader(GetTopicStatsInfoRequestHeader.class); final String topic = requestHeader.getTopic(); TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(topic); @@ -1391,11 +1353,9 @@ private RemotingCommand getTopicStatsInfo(ChannelHandlerContext ctx, private RemotingCommand getConsumerConnectionList(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - final GetConsumerConnectionListRequestHeader requestHeader = - (GetConsumerConnectionListRequestHeader) request.decodeCommandCustomHeader(GetConsumerConnectionListRequestHeader.class); + final GetConsumerConnectionListRequestHeader requestHeader = (GetConsumerConnectionListRequestHeader) request.decodeCommandCustomHeader(GetConsumerConnectionListRequestHeader.class); - ConsumerGroupInfo consumerGroupInfo = - this.brokerController.getConsumerManager().getConsumerGroupInfo(requestHeader.getConsumerGroup()); + ConsumerGroupInfo consumerGroupInfo = this.brokerController.getConsumerManager().getConsumerGroupInfo(requestHeader.getConsumerGroup()); if (consumerGroupInfo != null) { ConsumerConnection bodydata = new ConsumerConnection(); bodydata.setConsumeFromWhere(consumerGroupInfo.getConsumeFromWhere()); @@ -1428,10 +1388,10 @@ private RemotingCommand getConsumerConnectionList(ChannelHandlerContext ctx, return response; } - private RemotingCommand getAllProducerInfo(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { + private RemotingCommand getAllProducerInfo(ChannelHandlerContext ctx, + RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - final GetAllProducerInfoRequestHeader requestHeader = - (GetAllProducerInfoRequestHeader) request.decodeCommandCustomHeader(GetAllProducerInfoRequestHeader.class); + final GetAllProducerInfoRequestHeader requestHeader = (GetAllProducerInfoRequestHeader) request.decodeCommandCustomHeader(GetAllProducerInfoRequestHeader.class); ProducerTableInfo producerTable = this.brokerController.getProducerManager().getProducerTable(); if (producerTable != null) { @@ -1445,15 +1405,14 @@ private RemotingCommand getAllProducerInfo(ChannelHandlerContext ctx, RemotingCo response.setCode(ResponseCode.SYSTEM_ERROR); return response; } + private RemotingCommand getProducerConnectionList(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - final GetProducerConnectionListRequestHeader requestHeader = - (GetProducerConnectionListRequestHeader) request.decodeCommandCustomHeader(GetProducerConnectionListRequestHeader.class); + final GetProducerConnectionListRequestHeader requestHeader = (GetProducerConnectionListRequestHeader) request.decodeCommandCustomHeader(GetProducerConnectionListRequestHeader.class); ProducerConnection bodydata = new ProducerConnection(); - Map channelInfoHashMap = - this.brokerController.getProducerManager().getGroupChannelTable().get(requestHeader.getProducerGroup()); + Map channelInfoHashMap = this.brokerController.getProducerManager().getGroupChannelTable().get(requestHeader.getProducerGroup()); if (channelInfoHashMap != null) { Iterator> it = channelInfoHashMap.entrySet().iterator(); while (it.hasNext()) { @@ -1482,8 +1441,7 @@ private RemotingCommand getProducerConnectionList(ChannelHandlerContext ctx, private RemotingCommand getConsumeStats(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - final GetConsumeStatsRequestHeader requestHeader = - (GetConsumeStatsRequestHeader) request.decodeCommandCustomHeader(GetConsumeStatsRequestHeader.class); + final GetConsumeStatsRequestHeader requestHeader = (GetConsumeStatsRequestHeader) request.decodeCommandCustomHeader(GetConsumeStatsRequestHeader.class); ConsumeStats consumeStats = new ConsumeStats(); @@ -1504,14 +1462,10 @@ private RemotingCommand getConsumeStats(ChannelHandlerContext ctx, TopicQueueMappingDetail mappingDetail = this.brokerController.getTopicQueueMappingManager().getTopicQueueMapping(topic); { - SubscriptionData findSubscriptionData = - this.brokerController.getConsumerManager().findSubscriptionData(requestHeader.getConsumerGroup(), topic); - - if (null == findSubscriptionData - && this.brokerController.getConsumerManager().findSubscriptionDataCount(requestHeader.getConsumerGroup()) > 0) { - LOGGER.warn( - "AdminBrokerProcessor#getConsumeStats: topic does not exist in consumer group's subscription, " - + "topic={}, consumer group={}", topic, requestHeader.getConsumerGroup()); + SubscriptionData findSubscriptionData = this.brokerController.getConsumerManager().findSubscriptionData(requestHeader.getConsumerGroup(), topic); + + if (null == findSubscriptionData && this.brokerController.getConsumerManager().findSubscriptionDataCount(requestHeader.getConsumerGroup()) > 0) { + LOGGER.warn("AdminBrokerProcessor#getConsumeStats: topic does not exist in consumer group's subscription, " + "topic={}, consumer group={}", topic, requestHeader.getConsumerGroup()); continue; } } @@ -1529,8 +1483,7 @@ private RemotingCommand getConsumeStats(ChannelHandlerContext ctx, brokerOffset = 0; } - long consumerOffset = this.brokerController.getConsumerOffsetManager().queryOffset( - requestHeader.getConsumerGroup(), topic, i); + long consumerOffset = this.brokerController.getConsumerOffsetManager().queryOffset(requestHeader.getConsumerGroup(), topic, i); // the consumerOffset cannot be zero for static topic because of the "double read check" strategy // just remain the logic for dynamic topic @@ -1541,8 +1494,7 @@ private RemotingCommand getConsumeStats(ChannelHandlerContext ctx, } } - long pullOffset = this.brokerController.getConsumerOffsetManager().queryPullOffset( - requestHeader.getConsumerGroup(), topic, i); + long pullOffset = this.brokerController.getConsumerOffsetManager().queryPullOffset(requestHeader.getConsumerGroup(), topic, i); offsetWrapper.setBrokerOffset(brokerOffset); offsetWrapper.setConsumerOffset(consumerOffset); @@ -1607,16 +1559,14 @@ private RemotingCommand getAllDelayOffset(ChannelHandlerContext ctx, RemotingCom try { response.setBody(content.getBytes(MixAll.DEFAULT_CHARSET)); } catch (UnsupportedEncodingException e) { - LOGGER.error("AdminBrokerProcessor#getAllDelayOffset: unexpected error, caller={}.", - RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e); + LOGGER.error("AdminBrokerProcessor#getAllDelayOffset: unexpected error, caller={}.", RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e); response.setCode(ResponseCode.SYSTEM_ERROR); response.setRemark("UnsupportedEncodingException " + e); return response; } } else { - LOGGER.error("AdminBrokerProcessor#getAllDelayOffset: no delay offset in this broker, caller={}", - RemotingHelper.parseChannelRemoteAddr(ctx.channel())); + LOGGER.error("AdminBrokerProcessor#getAllDelayOffset: no delay offset in this broker, caller={}", RemotingHelper.parseChannelRemoteAddr(ctx.channel())); response.setCode(ResponseCode.SYSTEM_ERROR); response.setRemark("No delay offset in this broker"); return response; @@ -1657,11 +1607,8 @@ private RemotingCommand getAllMessageRequestMode(ChannelHandlerContext ctx, Remo public RemotingCommand resetOffset(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { - final ResetOffsetRequestHeader requestHeader = - (ResetOffsetRequestHeader) request.decodeCommandCustomHeader(ResetOffsetRequestHeader.class); - LOGGER.info("[reset-offset] reset offset started by {}. topic={}, group={}, timestamp={}, isForce={}", - RemotingHelper.parseChannelRemoteAddr(ctx.channel()), requestHeader.getTopic(), requestHeader.getGroup(), - requestHeader.getTimestamp(), requestHeader.isForce()); + final ResetOffsetRequestHeader requestHeader = (ResetOffsetRequestHeader) request.decodeCommandCustomHeader(ResetOffsetRequestHeader.class); + LOGGER.info("[reset-offset] reset offset started by {}. topic={}, group={}, timestamp={}, isForce={}", RemotingHelper.parseChannelRemoteAddr(ctx.channel()), requestHeader.getTopic(), requestHeader.getGroup(), requestHeader.getTimestamp(), requestHeader.isForce()); if (this.brokerController.getBrokerConfig().isUseServerSideResetOffset()) { String topic = requestHeader.getTopic(); @@ -1679,8 +1626,7 @@ public RemotingCommand resetOffset(ChannelHandlerContext ctx, isC = true; break; } - return this.brokerController.getBroker2Client().resetOffset(requestHeader.getTopic(), requestHeader.getGroup(), - requestHeader.getTimestamp(), requestHeader.isForce(), isC); + return this.brokerController.getBroker2Client().resetOffset(requestHeader.getTopic(), requestHeader.getGroup(), requestHeader.getTimestamp(), requestHeader.isForce(), isC); } private Long searchOffsetByTimestamp(String topic, int queueId, long timestamp) { @@ -1694,13 +1640,13 @@ private Long searchOffsetByTimestamp(String topic, int queueId, long timestamp) /** * Reset consumer offset. * - * @param topic Required, not null. - * @param group Required, not null. - * @param queueId if target queue ID is negative, all message queues will be reset; - * otherwise, only the target queue would get reset. - * @param timestamp if timestamp is negative, offset would be reset to broker offset at the time being; - * otherwise, binary search is performed to locate target offset. - * @param offset Target offset to reset to if target queue ID is properly provided. + * @param topic Required, not null. + * @param group Required, not null. + * @param queueId if target queue ID is negative, all message queues will be reset; otherwise, only the target queue + * would get reset. + * @param timestamp if timestamp is negative, offset would be reset to broker offset at the time being; otherwise, + * binary search is performed to locate target offset. + * @param offset Target offset to reset to if target queue ID is properly provided. * @return Affected queues and their new offset */ private RemotingCommand resetOffsetInner(String topic, String group, int queueId, long timestamp, Long offset) { @@ -1736,8 +1682,7 @@ private RemotingCommand resetOffsetInner(String topic, String group, int queueId long max = brokerController.getMessageStore().getMaxOffsetInQueue(topic, queueId); if (min >= 0 && offset < min || offset > max + 1) { response.setCode(ResponseCode.SYSTEM_ERROR); - response.setRemark( - String.format("Target offset %d not in consume queue range [%d-%d]", offset, min, max)); + response.setRemark(String.format("Target offset %d not in consume queue range [%d-%d]", offset, min, max)); return response; } } else { @@ -1759,8 +1704,7 @@ private RemotingCommand resetOffsetInner(String topic, String group, int queueId } for (Map.Entry entry : queueOffsetMap.entrySet()) { - brokerController.getConsumerOffsetManager() - .assignResetOffset(topic, group, entry.getKey(), entry.getValue()); + brokerController.getConsumerOffsetManager().assignResetOffset(topic, group, entry.getKey(), entry.getValue()); } // Prepare reset result. @@ -1778,21 +1722,17 @@ private RemotingCommand resetOffsetInner(String topic, String group, int queueId public RemotingCommand getConsumerStatus(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { - final GetConsumerStatusRequestHeader requestHeader = - (GetConsumerStatusRequestHeader) request.decodeCommandCustomHeader(GetConsumerStatusRequestHeader.class); + final GetConsumerStatusRequestHeader requestHeader = (GetConsumerStatusRequestHeader) request.decodeCommandCustomHeader(GetConsumerStatusRequestHeader.class); - LOGGER.info("[get-consumer-status] get consumer status by {}. topic={}, group={}", - RemotingHelper.parseChannelRemoteAddr(ctx.channel()), requestHeader.getTopic(), requestHeader.getGroup()); + LOGGER.info("[get-consumer-status] get consumer status by {}. topic={}, group={}", RemotingHelper.parseChannelRemoteAddr(ctx.channel()), requestHeader.getTopic(), requestHeader.getGroup()); - return this.brokerController.getBroker2Client().getConsumeStatus(requestHeader.getTopic(), requestHeader.getGroup(), - requestHeader.getClientAddr()); + return this.brokerController.getBroker2Client().getConsumeStatus(requestHeader.getTopic(), requestHeader.getGroup(), requestHeader.getClientAddr()); } private RemotingCommand queryTopicConsumeByWho(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - QueryTopicConsumeByWhoRequestHeader requestHeader = - (QueryTopicConsumeByWhoRequestHeader) request.decodeCommandCustomHeader(QueryTopicConsumeByWhoRequestHeader.class); + QueryTopicConsumeByWhoRequestHeader requestHeader = (QueryTopicConsumeByWhoRequestHeader) request.decodeCommandCustomHeader(QueryTopicConsumeByWhoRequestHeader.class); HashSet groups = this.brokerController.getConsumerManager().queryTopicConsumeByWho(requestHeader.getTopic()); @@ -1814,8 +1754,7 @@ private RemotingCommand queryTopicConsumeByWho(ChannelHandlerContext ctx, private RemotingCommand queryTopicsByConsumer(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - QueryTopicsByConsumerRequestHeader requestHeader = - (QueryTopicsByConsumerRequestHeader) request.decodeCommandCustomHeader(QueryTopicsByConsumerRequestHeader.class); + QueryTopicsByConsumerRequestHeader requestHeader = (QueryTopicsByConsumerRequestHeader) request.decodeCommandCustomHeader(QueryTopicsByConsumerRequestHeader.class); Set topics = this.brokerController.getConsumerOffsetManager().whichTopicByConsumer(requestHeader.getGroup()); @@ -1833,11 +1772,9 @@ private RemotingCommand queryTopicsByConsumer(ChannelHandlerContext ctx, private RemotingCommand querySubscriptionByConsumer(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - QuerySubscriptionByConsumerRequestHeader requestHeader = - (QuerySubscriptionByConsumerRequestHeader) request.decodeCommandCustomHeader(QuerySubscriptionByConsumerRequestHeader.class); + QuerySubscriptionByConsumerRequestHeader requestHeader = (QuerySubscriptionByConsumerRequestHeader) request.decodeCommandCustomHeader(QuerySubscriptionByConsumerRequestHeader.class); - SubscriptionData subscriptionData = this.brokerController.getConsumerManager() - .findSubscriptionData(requestHeader.getGroup(), requestHeader.getTopic()); + SubscriptionData subscriptionData = this.brokerController.getConsumerManager().findSubscriptionData(requestHeader.getGroup(), requestHeader.getTopic()); QuerySubscriptionResponseBody responseBody = new QuerySubscriptionResponseBody(); responseBody.setGroup(requestHeader.getGroup()); @@ -1856,8 +1793,7 @@ private RemotingCommand registerFilterServer(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(RegisterFilterServerResponseHeader.class); final RegisterFilterServerResponseHeader responseHeader = (RegisterFilterServerResponseHeader) response.readCustomHeader(); - final RegisterFilterServerRequestHeader requestHeader = - (RegisterFilterServerRequestHeader) request.decodeCommandCustomHeader(RegisterFilterServerRequestHeader.class); + final RegisterFilterServerRequestHeader requestHeader = (RegisterFilterServerRequestHeader) request.decodeCommandCustomHeader(RegisterFilterServerRequestHeader.class); this.brokerController.getFilterServerManager().registerFilterServer(ctx.channel(), requestHeader.getFilterServerAddr()); @@ -1872,8 +1808,7 @@ private RemotingCommand registerFilterServer(ChannelHandlerContext ctx, private RemotingCommand queryConsumeTimeSpan(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - QueryConsumeTimeSpanRequestHeader requestHeader = - (QueryConsumeTimeSpanRequestHeader) request.decodeCommandCustomHeader(QueryConsumeTimeSpanRequestHeader.class); + QueryConsumeTimeSpanRequestHeader requestHeader = (QueryConsumeTimeSpanRequestHeader) request.decodeCommandCustomHeader(QueryConsumeTimeSpanRequestHeader.class); final String topic = requestHeader.getTopic(); TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(topic); @@ -1900,8 +1835,7 @@ private RemotingCommand queryConsumeTimeSpan(ChannelHandlerContext ctx, timeSpan.setMaxTimeStamp(maxTime); long consumeTime; - long consumerOffset = this.brokerController.getConsumerOffsetManager().queryOffset( - requestHeader.getGroup(), topic, i); + long consumerOffset = this.brokerController.getConsumerOffsetManager().queryOffset(requestHeader.getGroup(), topic, i); if (consumerOffset > 0) { consumeTime = this.brokerController.getMessageStore().getMessageStoreTimeStamp(topic, i, consumerOffset - 1); } else { @@ -1925,8 +1859,8 @@ private RemotingCommand queryConsumeTimeSpan(ChannelHandlerContext ctx, return response; } - private RemotingCommand getSystemTopicListFromBroker(ChannelHandlerContext ctx, RemotingCommand request) - throws RemotingCommandException { + private RemotingCommand getSystemTopicListFromBroker(ChannelHandlerContext ctx, + RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); Set topics = TopicValidator.getSystemTopicSet(); @@ -1970,30 +1904,24 @@ public RemotingCommand cleanUnusedTopic() { private RemotingCommand getConsumerRunningInfo(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { - final GetConsumerRunningInfoRequestHeader requestHeader = - (GetConsumerRunningInfoRequestHeader) request.decodeCommandCustomHeader(GetConsumerRunningInfoRequestHeader.class); + final GetConsumerRunningInfoRequestHeader requestHeader = (GetConsumerRunningInfoRequestHeader) request.decodeCommandCustomHeader(GetConsumerRunningInfoRequestHeader.class); - return this.callConsumer(RequestCode.GET_CONSUMER_RUNNING_INFO, request, requestHeader.getConsumerGroup(), - requestHeader.getClientId()); + return this.callConsumer(RequestCode.GET_CONSUMER_RUNNING_INFO, request, requestHeader.getConsumerGroup(), requestHeader.getClientId()); } private RemotingCommand queryCorrectionOffset(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - QueryCorrectionOffsetHeader requestHeader = - (QueryCorrectionOffsetHeader) request.decodeCommandCustomHeader(QueryCorrectionOffsetHeader.class); + QueryCorrectionOffsetHeader requestHeader = (QueryCorrectionOffsetHeader) request.decodeCommandCustomHeader(QueryCorrectionOffsetHeader.class); - Map correctionOffset = this.brokerController.getConsumerOffsetManager() - .queryMinOffsetInAllGroup(requestHeader.getTopic(), requestHeader.getFilterGroups()); + Map correctionOffset = this.brokerController.getConsumerOffsetManager().queryMinOffsetInAllGroup(requestHeader.getTopic(), requestHeader.getFilterGroups()); - Map compareOffset = - this.brokerController.getConsumerOffsetManager().queryOffset(requestHeader.getCompareGroup(), requestHeader.getTopic()); + Map compareOffset = this.brokerController.getConsumerOffsetManager().queryOffset(requestHeader.getCompareGroup(), requestHeader.getTopic()); if (compareOffset != null && !compareOffset.isEmpty()) { for (Map.Entry entry : compareOffset.entrySet()) { Integer queueId = entry.getKey(); - correctionOffset.put(queueId, - correctionOffset.get(queueId) > entry.getValue() ? Long.MAX_VALUE : correctionOffset.get(queueId)); + correctionOffset.put(queueId, correctionOffset.get(queueId) > entry.getValue() ? Long.MAX_VALUE : correctionOffset.get(queueId)); } } @@ -2007,8 +1935,7 @@ private RemotingCommand queryCorrectionOffset(ChannelHandlerContext ctx, private RemotingCommand consumeMessageDirectly(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { - final ConsumeMessageDirectlyResultRequestHeader requestHeader = (ConsumeMessageDirectlyResultRequestHeader) request - .decodeCommandCustomHeader(ConsumeMessageDirectlyResultRequestHeader.class); + final ConsumeMessageDirectlyResultRequestHeader requestHeader = (ConsumeMessageDirectlyResultRequestHeader) request.decodeCommandCustomHeader(ConsumeMessageDirectlyResultRequestHeader.class); // brokerName request.getExtFields().put("brokerName", this.brokerController.getBrokerConfig().getBrokerName()); @@ -2041,15 +1968,13 @@ private RemotingCommand consumeMessageDirectly(ChannelHandlerContext ctx, } } - return this.callConsumer(RequestCode.CONSUME_MESSAGE_DIRECTLY, request, requestHeader.getConsumerGroup(), - requestHeader.getClientId()); + return this.callConsumer(RequestCode.CONSUME_MESSAGE_DIRECTLY, request, requestHeader.getConsumerGroup(), requestHeader.getClientId()); } private RemotingCommand cloneGroupOffset(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - CloneGroupOffsetRequestHeader requestHeader = - (CloneGroupOffsetRequestHeader) request.decodeCommandCustomHeader(CloneGroupOffsetRequestHeader.class); + CloneGroupOffsetRequestHeader requestHeader = (CloneGroupOffsetRequestHeader) request.decodeCommandCustomHeader(CloneGroupOffsetRequestHeader.class); Set topics; if (UtilAll.isBlank(requestHeader.getTopic())) { @@ -2068,19 +1993,14 @@ private RemotingCommand cloneGroupOffset(ChannelHandlerContext ctx, if (!requestHeader.isOffline()) { - SubscriptionData findSubscriptionData = - this.brokerController.getConsumerManager().findSubscriptionData(requestHeader.getSrcGroup(), topic); - if (this.brokerController.getConsumerManager().findSubscriptionDataCount(requestHeader.getSrcGroup()) > 0 - && findSubscriptionData == null) { - LOGGER.warn( - "AdminBrokerProcessor#cloneGroupOffset: topic does not exist in consumer group's " - + "subscription, topic={}, consumer group={}", topic, requestHeader.getSrcGroup()); + SubscriptionData findSubscriptionData = this.brokerController.getConsumerManager().findSubscriptionData(requestHeader.getSrcGroup(), topic); + if (this.brokerController.getConsumerManager().findSubscriptionDataCount(requestHeader.getSrcGroup()) > 0 && findSubscriptionData == null) { + LOGGER.warn("AdminBrokerProcessor#cloneGroupOffset: topic does not exist in consumer group's " + "subscription, topic={}, consumer group={}", topic, requestHeader.getSrcGroup()); continue; } } - this.brokerController.getConsumerOffsetManager().cloneOffset(requestHeader.getSrcGroup(), requestHeader.getDestGroup(), - requestHeader.getTopic()); + this.brokerController.getConsumerOffsetManager().cloneOffset(requestHeader.getSrcGroup(), requestHeader.getDestGroup(), requestHeader.getTopic()); } response.setCode(ResponseCode.SUCCESS); @@ -2090,8 +2010,7 @@ private RemotingCommand cloneGroupOffset(ChannelHandlerContext ctx, private RemotingCommand ViewBrokerStatsData(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { - final ViewBrokerStatsDataRequestHeader requestHeader = - (ViewBrokerStatsDataRequestHeader) request.decodeCommandCustomHeader(ViewBrokerStatsDataRequestHeader.class); + final ViewBrokerStatsDataRequestHeader requestHeader = (ViewBrokerStatsDataRequestHeader) request.decodeCommandCustomHeader(ViewBrokerStatsDataRequestHeader.class); final RemotingCommand response = RemotingCommand.createResponseCommand(null); MessageStore messageStore = this.brokerController.getMessageStore(); @@ -2137,17 +2056,14 @@ private RemotingCommand ViewBrokerStatsData(ChannelHandlerContext ctx, return response; } - private RemotingCommand fetchAllConsumeStatsInBroker(ChannelHandlerContext ctx, RemotingCommand request) - throws RemotingCommandException { + private RemotingCommand fetchAllConsumeStatsInBroker(ChannelHandlerContext ctx, + RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - GetConsumeStatsInBrokerHeader requestHeader = - (GetConsumeStatsInBrokerHeader) request.decodeCommandCustomHeader(GetConsumeStatsInBrokerHeader.class); + GetConsumeStatsInBrokerHeader requestHeader = (GetConsumeStatsInBrokerHeader) request.decodeCommandCustomHeader(GetConsumeStatsInBrokerHeader.class); boolean isOrder = requestHeader.isOrder(); - ConcurrentMap subscriptionGroups = - brokerController.getSubscriptionGroupManager().getSubscriptionGroupTable(); + ConcurrentMap subscriptionGroups = brokerController.getSubscriptionGroupManager().getSubscriptionGroupTable(); - List>> brokerConsumeStatsList = - new ArrayList<>(); + List>> brokerConsumeStatsList = new ArrayList<>(); long totalDiff = 0L; long totalInflightDiff = 0L; @@ -2159,9 +2075,7 @@ private RemotingCommand fetchAllConsumeStatsInBroker(ChannelHandlerContext ctx, ConsumeStats consumeStats = new ConsumeStats(); TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(topic); if (null == topicConfig) { - LOGGER.warn( - "AdminBrokerProcessor#fetchAllConsumeStatsInBroker: topic config does not exist, topic={}", - topic); + LOGGER.warn("AdminBrokerProcessor#fetchAllConsumeStatsInBroker: topic config does not exist, topic={}", topic); continue; } @@ -2172,11 +2086,8 @@ private RemotingCommand fetchAllConsumeStatsInBroker(ChannelHandlerContext ctx, { SubscriptionData findSubscriptionData = this.brokerController.getConsumerManager().findSubscriptionData(group, topic); - if (null == findSubscriptionData - && this.brokerController.getConsumerManager().findSubscriptionDataCount(group) > 0) { - LOGGER.warn( - "AdminBrokerProcessor#fetchAllConsumeStatsInBroker: topic does not exist in consumer " - + "group's subscription, topic={}, consumer group={}", topic, group); + if (null == findSubscriptionData && this.brokerController.getConsumerManager().findSubscriptionDataCount(group) > 0) { + LOGGER.warn("AdminBrokerProcessor#fetchAllConsumeStatsInBroker: topic does not exist in consumer " + "group's subscription, topic={}, consumer group={}", topic, group); continue; } } @@ -2191,10 +2102,7 @@ private RemotingCommand fetchAllConsumeStatsInBroker(ChannelHandlerContext ctx, if (brokerOffset < 0) { brokerOffset = 0; } - long consumerOffset = this.brokerController.getConsumerOffsetManager().queryOffset( - group, - topic, - i); + long consumerOffset = this.brokerController.getConsumerOffsetManager().queryOffset(group, topic, i); if (consumerOffset < 0) consumerOffset = 0; @@ -2245,13 +2153,11 @@ private HashMap prepareRuntimeInfo() { runtimeInfo.put("brokerVersionDesc", MQVersion.getVersionDesc(MQVersion.CURRENT_VERSION)); runtimeInfo.put("brokerVersion", String.valueOf(MQVersion.CURRENT_VERSION)); - runtimeInfo.put("msgPutTotalYesterdayMorning", - String.valueOf(this.brokerController.getBrokerStats().getMsgPutTotalYesterdayMorning())); + runtimeInfo.put("msgPutTotalYesterdayMorning", String.valueOf(this.brokerController.getBrokerStats().getMsgPutTotalYesterdayMorning())); runtimeInfo.put("msgPutTotalTodayMorning", String.valueOf(this.brokerController.getBrokerStats().getMsgPutTotalTodayMorning())); runtimeInfo.put("msgPutTotalTodayNow", String.valueOf(this.brokerController.getBrokerStats().getMsgPutTotalTodayNow())); - runtimeInfo.put("msgGetTotalYesterdayMorning", - String.valueOf(this.brokerController.getBrokerStats().getMsgGetTotalYesterdayMorning())); + runtimeInfo.put("msgGetTotalYesterdayMorning", String.valueOf(this.brokerController.getBrokerStats().getMsgGetTotalYesterdayMorning())); runtimeInfo.put("msgGetTotalTodayMorning", String.valueOf(this.brokerController.getBrokerStats().getMsgGetTotalTodayMorning())); runtimeInfo.put("msgGetTotalTodayNow", String.valueOf(this.brokerController.getBrokerStats().getMsgGetTotalTodayNow())); @@ -2276,7 +2182,7 @@ private HashMap prepareRuntimeInfo() { } MessageStore messageStore = this.brokerController.getMessageStore(); runtimeInfo.put("remainTransientStoreBufferNumbs", String.valueOf(messageStore.remainTransientStoreBufferNumbs())); - if (this.brokerController.getMessageStoreConfig().isTransientStorePoolEnable()) { + if (this.brokerController.getMessageStore() instanceof DefaultMessageStore && ((DefaultMessageStore) this.brokerController.getMessageStore()).isTransientStorePoolEnable()) { runtimeInfo.put("remainHowManyDataToCommit", MixAll.humanReadableByteCount(messageStore.remainHowManyDataToCommit(), false)); } runtimeInfo.put("remainHowManyDataToFlush", MixAll.humanReadableByteCount(messageStore.remainHowManyDataToFlush(), false)); @@ -2287,20 +2193,16 @@ private HashMap prepareRuntimeInfo() { } runtimeInfo.put("sendThreadPoolQueueSize", String.valueOf(this.brokerController.getSendThreadPoolQueue().size())); - runtimeInfo.put("sendThreadPoolQueueCapacity", - String.valueOf(this.brokerController.getBrokerConfig().getSendThreadPoolQueueCapacity())); + runtimeInfo.put("sendThreadPoolQueueCapacity", String.valueOf(this.brokerController.getBrokerConfig().getSendThreadPoolQueueCapacity())); runtimeInfo.put("pullThreadPoolQueueSize", String.valueOf(this.brokerController.getPullThreadPoolQueue().size())); - runtimeInfo.put("pullThreadPoolQueueCapacity", - String.valueOf(this.brokerController.getBrokerConfig().getPullThreadPoolQueueCapacity())); + runtimeInfo.put("pullThreadPoolQueueCapacity", String.valueOf(this.brokerController.getBrokerConfig().getPullThreadPoolQueueCapacity())); runtimeInfo.put("litePullThreadPoolQueueSize", String.valueOf(brokerController.getLitePullThreadPoolQueue().size())); - runtimeInfo.put("litePullThreadPoolQueueCapacity", - String.valueOf(this.brokerController.getBrokerConfig().getLitePullThreadPoolQueueCapacity())); + runtimeInfo.put("litePullThreadPoolQueueCapacity", String.valueOf(this.brokerController.getBrokerConfig().getLitePullThreadPoolQueueCapacity())); runtimeInfo.put("queryThreadPoolQueueSize", String.valueOf(this.brokerController.getQueryThreadPoolQueue().size())); - runtimeInfo.put("queryThreadPoolQueueCapacity", - String.valueOf(this.brokerController.getBrokerConfig().getQueryThreadPoolQueueCapacity())); + runtimeInfo.put("queryThreadPoolQueueCapacity", String.valueOf(this.brokerController.getBrokerConfig().getQueryThreadPoolQueueCapacity())); runtimeInfo.put("sendThreadPoolQueueHeadWaitTimeMills", String.valueOf(this.brokerController.headSlowTimeMills4SendThreadPoolQueue())); runtimeInfo.put("pullThreadPoolQueueHeadWaitTimeMills", String.valueOf(brokerController.headSlowTimeMills4PullThreadPoolQueue())); @@ -2308,17 +2210,13 @@ private HashMap prepareRuntimeInfo() { runtimeInfo.put("litePullThreadPoolQueueHeadWaitTimeMills", String.valueOf(brokerController.headSlowTimeMills4LitePullThreadPoolQueue())); runtimeInfo.put("EndTransactionQueueSize", String.valueOf(this.brokerController.getEndTransactionThreadPoolQueue().size())); - runtimeInfo.put("EndTransactionThreadPoolQueueCapacity", - String.valueOf(this.brokerController.getBrokerConfig().getEndTransactionPoolQueueCapacity())); + runtimeInfo.put("EndTransactionThreadPoolQueueCapacity", String.valueOf(this.brokerController.getBrokerConfig().getEndTransactionPoolQueueCapacity())); return runtimeInfo; } - private RemotingCommand callConsumer( - final int requestCode, - final RemotingCommand request, - final String consumerGroup, - final String clientId) throws RemotingCommandException { + private RemotingCommand callConsumer(final int requestCode, final RemotingCommand request, + final String consumerGroup, final String clientId) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); ClientChannelInfo clientChannelInfo = this.brokerController.getConsumerManager().findChannel(consumerGroup, clientId); @@ -2330,9 +2228,7 @@ private RemotingCommand callConsumer( if (clientChannelInfo.getVersion() < MQVersion.Version.V3_1_8_SNAPSHOT.ordinal()) { response.setCode(ResponseCode.SYSTEM_ERROR); - response.setRemark(String.format("The Consumer <%s> Version <%s> too low to finish, please upgrade it to V3_1_8_SNAPSHOT", - clientId, - MQVersion.getVersionDesc(clientChannelInfo.getVersion()))); + response.setRemark(String.format("The Consumer <%s> Version <%s> too low to finish, please upgrade it to V3_1_8_SNAPSHOT", clientId, MQVersion.getVersionDesc(clientChannelInfo.getVersion()))); return response; } @@ -2344,26 +2240,22 @@ private RemotingCommand callConsumer( return this.brokerController.getBroker2Client().callClient(clientChannelInfo.getChannel(), newRequest); } catch (RemotingTimeoutException e) { response.setCode(ResponseCode.CONSUME_MSG_TIMEOUT); - response - .setRemark(String.format("consumer <%s> <%s> Timeout: %s", consumerGroup, clientId, UtilAll.exceptionSimpleDesc(e))); + response.setRemark(String.format("consumer <%s> <%s> Timeout: %s", consumerGroup, clientId, UtilAll.exceptionSimpleDesc(e))); return response; } catch (Exception e) { response.setCode(ResponseCode.SYSTEM_ERROR); - response.setRemark( - String.format("invoke consumer <%s> <%s> Exception: %s", consumerGroup, clientId, UtilAll.exceptionSimpleDesc(e))); + response.setRemark(String.format("invoke consumer <%s> <%s> Exception: %s", consumerGroup, clientId, UtilAll.exceptionSimpleDesc(e))); return response; } } private RemotingCommand queryConsumeQueue(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { - QueryConsumeQueueRequestHeader requestHeader = - (QueryConsumeQueueRequestHeader) request.decodeCommandCustomHeader(QueryConsumeQueueRequestHeader.class); + QueryConsumeQueueRequestHeader requestHeader = (QueryConsumeQueueRequestHeader) request.decodeCommandCustomHeader(QueryConsumeQueueRequestHeader.class); RemotingCommand response = RemotingCommand.createResponseCommand(null); - ConsumeQueueInterface consumeQueue = this.brokerController.getMessageStore().getConsumeQueue(requestHeader.getTopic(), - requestHeader.getQueueId()); + ConsumeQueueInterface consumeQueue = this.brokerController.getMessageStore().getConsumeQueue(requestHeader.getTopic(), requestHeader.getQueueId()); if (consumeQueue == null) { response.setCode(ResponseCode.SYSTEM_ERROR); response.setRemark(String.format("%d@%s is not exist!", requestHeader.getQueueId(), requestHeader.getTopic())); @@ -2377,19 +2269,15 @@ private RemotingCommand queryConsumeQueue(ChannelHandlerContext ctx, MessageFilter messageFilter = null; if (requestHeader.getConsumerGroup() != null) { - SubscriptionData subscriptionData = this.brokerController.getConsumerManager().findSubscriptionData( - requestHeader.getConsumerGroup(), requestHeader.getTopic() - ); + SubscriptionData subscriptionData = this.brokerController.getConsumerManager().findSubscriptionData(requestHeader.getConsumerGroup(), requestHeader.getTopic()); body.setSubscriptionData(subscriptionData); if (subscriptionData == null) { body.setFilterData(String.format("%s@%s is not online!", requestHeader.getConsumerGroup(), requestHeader.getTopic())); } else { - ConsumerFilterData filterData = this.brokerController.getConsumerFilterManager() - .get(requestHeader.getTopic(), requestHeader.getConsumerGroup()); + ConsumerFilterData filterData = this.brokerController.getConsumerFilterManager().get(requestHeader.getTopic(), requestHeader.getConsumerGroup()); body.setFilterData(JSON.toJSONString(filterData, true)); - messageFilter = new ExpressionMessageFilter(subscriptionData, filterData, - this.brokerController.getConsumerFilterManager()); + messageFilter = new ExpressionMessageFilter(subscriptionData, filterData, this.brokerController.getConsumerFilterManager()); } } @@ -2440,25 +2328,18 @@ private RemotingCommand queryConsumeQueue(ChannelHandlerContext ctx, } private RemotingCommand resumeCheckHalfMessage(ChannelHandlerContext ctx, - RemotingCommand request) - throws RemotingCommandException { - final ResumeCheckHalfMessageRequestHeader requestHeader = (ResumeCheckHalfMessageRequestHeader) request - .decodeCommandCustomHeader(ResumeCheckHalfMessageRequestHeader.class); + RemotingCommand request) throws RemotingCommandException { + final ResumeCheckHalfMessageRequestHeader requestHeader = (ResumeCheckHalfMessageRequestHeader) request.decodeCommandCustomHeader(ResumeCheckHalfMessageRequestHeader.class); final RemotingCommand response = RemotingCommand.createResponseCommand(null); SelectMappedBufferResult selectMappedBufferResult = null; try { MessageId messageId = MessageDecoder.decodeMessageId(requestHeader.getMsgId()); - selectMappedBufferResult = this.brokerController.getMessageStore() - .selectOneMessageByOffset(messageId.getOffset()); + selectMappedBufferResult = this.brokerController.getMessageStore().selectOneMessageByOffset(messageId.getOffset()); MessageExt msg = MessageDecoder.decode(selectMappedBufferResult.getByteBuffer()); msg.putUserProperty(MessageConst.PROPERTY_TRANSACTION_CHECK_TIMES, String.valueOf(0)); - PutMessageResult putMessageResult = this.brokerController.getMessageStore() - .putMessage(toMessageExtBrokerInner(msg)); - if (putMessageResult != null - && putMessageResult.getPutMessageStatus() == PutMessageStatus.PUT_OK) { - LOGGER.info( - "Put message back to RMQ_SYS_TRANS_HALF_TOPIC. real topic={}", - msg.getUserProperty(MessageConst.PROPERTY_REAL_TOPIC)); + PutMessageResult putMessageResult = this.brokerController.getMessageStore().putMessage(toMessageExtBrokerInner(msg)); + if (putMessageResult != null && putMessageResult.getPutMessageStatus() == PutMessageStatus.PUT_OK) { + LOGGER.info("Put message back to RMQ_SYS_TRANS_HALF_TOPIC. real topic={}", msg.getUserProperty(MessageConst.PROPERTY_REAL_TOPIC)); response.setCode(ResponseCode.SUCCESS); response.setRemark(null); } else { @@ -2538,9 +2419,7 @@ private RemotingCommand notifyMinBrokerIdChange(ChannelHandlerContext ctx, LOGGER.warn("min broker id changed, prev {}, new {}", this.brokerController.getMinBrokerIdInGroup(), requestHeader.getMinBrokerId()); - this.brokerController.updateMinBroker(requestHeader.getMinBrokerId(), requestHeader.getMinBrokerAddr(), - requestHeader.getOfflineBrokerAddr(), - requestHeader.getHaBrokerAddr()); + this.brokerController.updateMinBroker(requestHeader.getMinBrokerId(), requestHeader.getMinBrokerAddr(), requestHeader.getOfflineBrokerAddr(), requestHeader.getHaBrokerAddr()); response.setCode(ResponseCode.SUCCESS); response.setRemark(null); @@ -2556,8 +2435,7 @@ private RemotingCommand updateBrokerHaInfo(ChannelHandlerContext ctx, if (requestHeader.getMasterHaAddress() != null) { this.brokerController.getMessageStore().updateHaMasterAddress(requestHeader.getMasterHaAddress()); this.brokerController.getMessageStore().updateMasterAddress(requestHeader.getMasterAddress()); - if (this.brokerController.getMessageStore().getMasterFlushedOffset() == 0 - && this.brokerController.getMessageStoreConfig().isSyncMasterFlushOffsetWhenStartup()) { + if (this.brokerController.getMessageStore().getMasterFlushedOffset() == 0 && this.brokerController.getMessageStoreConfig().isSyncMasterFlushOffsetWhenStartup()) { LOGGER.info("Set master flush offset in slave to {}", requestHeader.getMasterFlushOffset()); this.brokerController.getMessageStore().setMasterFlushedOffset(requestHeader.getMasterFlushOffset()); } @@ -2596,8 +2474,7 @@ private RemotingCommand getBrokerEpochCache(ChannelHandlerContext ctx, RemotingC final ReplicasManager replicasManager = this.brokerController.getReplicasManager(); assert replicasManager != null; final BrokerConfig brokerConfig = this.brokerController.getBrokerConfig(); - final EpochEntryCache entryCache = new EpochEntryCache(brokerConfig.getBrokerClusterName(), - brokerConfig.getBrokerName(), brokerConfig.getBrokerId(), replicasManager.getEpochEntries(), this.brokerController.getMessageStore().getMaxPhyOffset()); + final EpochEntryCache entryCache = new EpochEntryCache(brokerConfig.getBrokerClusterName(), brokerConfig.getBrokerName(), brokerConfig.getBrokerId(), replicasManager.getEpochEntries(), this.brokerController.getMessageStore().getMaxPhyOffset()); final RemotingCommand response = RemotingCommand.createResponseCommand(null); response.setBody(entryCache.encode()); @@ -2606,7 +2483,6 @@ private RemotingCommand getBrokerEpochCache(ChannelHandlerContext ctx, RemotingC return response; } - private RemotingCommand resetMasterFlushOffset(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); @@ -2646,8 +2522,7 @@ private RemotingCommand notifyBrokerRoleChanged(ChannelHandlerContext ctx, private boolean validateSlave(RemotingCommand response) { if (this.brokerController.getMessageStoreConfig().getBrokerRole().equals(BrokerRole.SLAVE)) { response.setCode(ResponseCode.SYSTEM_ERROR); - response.setRemark("Can't modify topic or subscription group from slave broker, " + - "please execute it from master broker."); + response.setRemark("Can't modify topic or subscription group from slave broker, " + "please execute it from master broker."); return true; } return false; diff --git a/store/src/main/java/org/apache/rocketmq/store/AllocateMappedFileService.java b/store/src/main/java/org/apache/rocketmq/store/AllocateMappedFileService.java index 4d2fc51683b..dca7d53258d 100644 --- a/store/src/main/java/org/apache/rocketmq/store/AllocateMappedFileService.java +++ b/store/src/main/java/org/apache/rocketmq/store/AllocateMappedFileService.java @@ -52,7 +52,7 @@ public AllocateMappedFileService(DefaultMessageStore messageStore) { public MappedFile putRequestAndReturnMappedFile(String nextFilePath, String nextNextFilePath, int fileSize) { int canSubmitRequests = 2; - if (this.messageStore.getMessageStoreConfig().isTransientStorePoolEnable()) { + if (this.messageStore.isTransientStorePoolEnable()) { if (this.messageStore.getMessageStoreConfig().isFastFailIfNoBufferInStorePool() && BrokerRole.SLAVE != this.messageStore.getMessageStoreConfig().getBrokerRole()) { //if broker is slave, don't fast fail even no buffer in pool canSubmitRequests = this.messageStore.getTransientStorePool().availableBufferNums() - this.requestQueue.size(); @@ -171,7 +171,7 @@ private boolean mmapOperation() { long beginTime = System.currentTimeMillis(); MappedFile mappedFile; - if (messageStore.getMessageStoreConfig().isTransientStorePoolEnable()) { + if (messageStore.isTransientStorePoolEnable()) { try { mappedFile = ServiceLoader.load(MappedFile.class).iterator().next(); mappedFile.init(req.getFilePath(), req.getFileSize(), messageStore.getTransientStorePool()); diff --git a/store/src/main/java/org/apache/rocketmq/store/CommitLog.java b/store/src/main/java/org/apache/rocketmq/store/CommitLog.java index c38c2168e63..d7c40cc5057 100644 --- a/store/src/main/java/org/apache/rocketmq/store/CommitLog.java +++ b/store/src/main/java/org/apache/rocketmq/store/CommitLog.java @@ -1854,7 +1854,7 @@ public DefaultFlushManager() { public void start() { this.flushCommitLogService.start(); - if (defaultMessageStore.getMessageStoreConfig().isTransientStorePoolEnable()) { + if (defaultMessageStore.isTransientStorePoolEnable()) { this.commitLogService.start(); } } @@ -1886,7 +1886,7 @@ public void handleDiskFlush(AppendMessageResult result, PutMessageResult putMess } // Asynchronous flush else { - if (!CommitLog.this.defaultMessageStore.getMessageStoreConfig().isTransientStorePoolEnable()) { + if (!CommitLog.this.defaultMessageStore.isTransientStorePoolEnable()) { flushCommitLogService.wakeup(); } else { commitLogService.wakeup(); @@ -1911,7 +1911,7 @@ public CompletableFuture handleDiskFlush(AppendMessageResult r } // Asynchronous flush else { - if (!CommitLog.this.defaultMessageStore.getMessageStoreConfig().isTransientStorePoolEnable()) { + if (!CommitLog.this.defaultMessageStore.isTransientStorePoolEnable()) { flushCommitLogService.wakeup(); } else { commitLogService.wakeup(); @@ -1928,7 +1928,7 @@ public void wakeUpFlush() { @Override public void shutdown() { - if (defaultMessageStore.getMessageStoreConfig().isTransientStorePoolEnable()) { + if (defaultMessageStore.isTransientStorePoolEnable()) { this.commitLogService.shutdown(); } diff --git a/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java b/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java index f42960c425e..4a4c96ddcfc 100644 --- a/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java +++ b/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java @@ -223,7 +223,7 @@ public DefaultMessageStore(final MessageStoreConfig messageStoreConfig, final Br this.reputMessageService = new ReputMessageService(); - this.transientStorePool = new TransientStorePool(messageStoreConfig); + this.transientStorePool = new TransientStorePool(this); this.scheduledExecutorService = Executors.newSingleThreadScheduledExecutor(new ThreadFactoryImpl("StoreScheduledThread", getBrokerIdentity())); @@ -338,7 +338,7 @@ public void start() throws Exception { this.haService.init(this); } - if (messageStoreConfig.isTransientStorePoolEnable()) { + if (this.isTransientStorePoolEnable()) { this.transientStorePool.init(); } @@ -2745,4 +2745,15 @@ public List> getMetricsView() { public void initMetrics(Meter meter, Supplier attributesBuilderSupplier) { DefaultStoreMetricsManager.init(meter, attributesBuilderSupplier, this); } + + /** + * Enable transient commitLog store pool only if transientStorePoolEnable is true and broker role is not SLAVE or + * enableControllerMode is true + * + * @return true or false + */ + public boolean isTransientStorePoolEnable() { + return this.messageStoreConfig.isTransientStorePoolEnable() && + (this.brokerConfig.isEnableControllerMode() || this.messageStoreConfig.getBrokerRole() != BrokerRole.SLAVE); + } } diff --git a/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java b/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java index a873fe05b76..c70a7bac74b 100644 --- a/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java +++ b/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java @@ -34,12 +34,12 @@ public class TransientStorePool { private final int poolSize; private final int fileSize; private final Deque availableBuffers; - private final MessageStoreConfig storeConfig; + private final DefaultMessageStore messageStore; - public TransientStorePool(final MessageStoreConfig storeConfig) { - this.storeConfig = storeConfig; - this.poolSize = storeConfig.getTransientStorePoolSize(); - this.fileSize = storeConfig.getMappedFileSizeCommitLog(); + public TransientStorePool(final DefaultMessageStore messageStore) { + this.messageStore = messageStore; + this.poolSize = messageStore.getMessageStoreConfig().getTransientStorePoolSize(); + this.fileSize = messageStore.getMessageStoreConfig().getMappedFileSizeCommitLog(); this.availableBuffers = new ConcurrentLinkedDeque<>(); } @@ -81,7 +81,7 @@ public ByteBuffer borrowBuffer() { } public int availableBufferNums() { - if (storeConfig.isTransientStorePoolEnable()) { + if (messageStore.isTransientStorePoolEnable()) { return availableBuffers.size(); } return Integer.MAX_VALUE; diff --git a/store/src/main/java/org/apache/rocketmq/store/config/MessageStoreConfig.java b/store/src/main/java/org/apache/rocketmq/store/config/MessageStoreConfig.java index 91663558ebb..e29fdc2b06a 100644 --- a/store/src/main/java/org/apache/rocketmq/store/config/MessageStoreConfig.java +++ b/store/src/main/java/org/apache/rocketmq/store/config/MessageStoreConfig.java @@ -965,12 +965,8 @@ public void setDefaultQueryMaxNum(int defaultQueryMaxNum) { this.defaultQueryMaxNum = defaultQueryMaxNum; } - /** - * Enable transient commitLog store pool only if transientStorePoolEnable is true and broker role is not SLAVE - * @return true or false - */ public boolean isTransientStorePoolEnable() { - return transientStorePoolEnable && BrokerRole.SLAVE != getBrokerRole(); + return transientStorePoolEnable; } public void setTransientStorePoolEnable(final boolean transientStorePoolEnable) { From 985161d845b3490d689c070823ce1d3d72786caa Mon Sep 17 00:00:00 2001 From: RongtongJin Date: Sat, 17 Dec 2022 12:41:28 +0800 Subject: [PATCH 02/15] Format the checkstyle --- .../processor/AdminBrokerProcessor.java | 407 ++++++++++++------ 1 file changed, 266 insertions(+), 141 deletions(-) diff --git a/broker/src/main/java/org/apache/rocketmq/broker/processor/AdminBrokerProcessor.java b/broker/src/main/java/org/apache/rocketmq/broker/processor/AdminBrokerProcessor.java index f46f688d293..24162022c90 100644 --- a/broker/src/main/java/org/apache/rocketmq/broker/processor/AdminBrokerProcessor.java +++ b/broker/src/main/java/org/apache/rocketmq/broker/processor/AdminBrokerProcessor.java @@ -197,7 +197,8 @@ public AdminBrokerProcessor(final BrokerController brokerController) { this.brokerController = brokerController; } - @Override public RemotingCommand processRequest(ChannelHandlerContext ctx, + @Override + public RemotingCommand processRequest(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { switch (request.getCode()) { case RequestCode.UPDATE_AND_CREATE_TOPIC: @@ -362,8 +363,8 @@ private RemotingCommand getSubscriptionGroup(ChannelHandlerContext ctx, * @param request * @return */ - private RemotingCommand updateAndGetGroupForbidden(ChannelHandlerContext ctx, - RemotingCommand request) throws RemotingCommandException { + private RemotingCommand updateAndGetGroupForbidden(ChannelHandlerContext ctx, RemotingCommand request) + throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); UpdateGroupForbiddenRequestHeader requestHeader = (UpdateGroupForbiddenRequestHeader) // request.decodeCommandCustomHeader(UpdateGroupForbiddenRequestHeader.class); @@ -387,7 +388,8 @@ private RemotingCommand updateAndGetGroupForbidden(ChannelHandlerContext ctx, return response; } - @Override public boolean rejectRequest() { + @Override + public boolean rejectRequest() { return false; } @@ -397,9 +399,11 @@ private synchronized RemotingCommand updateAndCreateTopic(ChannelHandlerContext if (validateSlave(response)) { return response; } - final CreateTopicRequestHeader requestHeader = (CreateTopicRequestHeader) request.decodeCommandCustomHeader(CreateTopicRequestHeader.class); + final CreateTopicRequestHeader requestHeader = + (CreateTopicRequestHeader) request.decodeCommandCustomHeader(CreateTopicRequestHeader.class); - LOGGER.info("Broker receive request to update or create topic={}, caller address={}", requestHeader.getTopic(), RemotingHelper.parseChannelRemoteAddr(ctx.channel())); + LOGGER.info("Broker receive request to update or create topic={}, caller address={}", + requestHeader.getTopic(), RemotingHelper.parseChannelRemoteAddr(ctx.channel())); String topic = requestHeader.getTopic(); @@ -441,7 +445,8 @@ private synchronized RemotingCommand updateAndCreateTopic(ChannelHandlerContext private synchronized RemotingCommand updateAndCreateStaticTopic(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - final CreateTopicRequestHeader requestHeader = (CreateTopicRequestHeader) request.decodeCommandCustomHeader(CreateTopicRequestHeader.class); + final CreateTopicRequestHeader requestHeader = + (CreateTopicRequestHeader) request.decodeCommandCustomHeader(CreateTopicRequestHeader.class); LOGGER.info("Broker receive request to update or create static topic={}, caller address={}", requestHeader.getTopic(), RemotingHelper.parseChannelRemoteAddr(ctx.channel())); final TopicQueueMappingDetail topicQueueMappingDetail = RemotingSerializable.decode(request.getBody(), TopicQueueMappingDetail.class); @@ -494,9 +499,11 @@ private synchronized RemotingCommand deleteTopic(ChannelHandlerContext ctx, if (validateSlave(response)) { return response; } - DeleteTopicRequestHeader requestHeader = (DeleteTopicRequestHeader) request.decodeCommandCustomHeader(DeleteTopicRequestHeader.class); + DeleteTopicRequestHeader requestHeader = + (DeleteTopicRequestHeader) request.decodeCommandCustomHeader(DeleteTopicRequestHeader.class); - LOGGER.info("AdminBrokerProcessor#deleteTopic: broker receive request to delete topic={}, caller={}", requestHeader.getTopic(), RemotingHelper.parseChannelRemoteAddr(ctx.channel())); + LOGGER.info("AdminBrokerProcessor#deleteTopic: broker receive request to delete topic={}, caller={}", + requestHeader.getTopic(), RemotingHelper.parseChannelRemoteAddr(ctx.channel())); String topic = requestHeader.getTopic(); TopicValidator.ValidateTopicResult result = TopicValidator.validateTopic(topic); @@ -517,7 +524,8 @@ private synchronized RemotingCommand deleteTopic(ChannelHandlerContext ctx, this.brokerController.getTopicQueueMappingManager().delete(requestHeader.getTopic()); this.brokerController.getConsumerOffsetManager().cleanOffsetByTopic(requestHeader.getTopic()); this.brokerController.getPopInflightMessageCounter().clearInFlightMessageNumByTopicName(requestHeader.getTopic()); - this.brokerController.getMessageStore().cleanUnusedTopic(this.brokerController.getTopicConfigManager().getTopicConfigTable().keySet()); + this.brokerController.getMessageStore() + .cleanUnusedTopic(this.brokerController.getTopicConfigManager().getTopicConfigTable().keySet()); if (this.brokerController.getBrokerConfig().isAutoDeleteUnusedStats()) { this.brokerController.getBrokerStatsManager().onTopicDeleted(requestHeader.getTopic()); } @@ -531,7 +539,8 @@ private synchronized RemotingCommand updateAndCreateAccessConfig(ChannelHandlerC RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - final CreateAccessConfigRequestHeader requestHeader = (CreateAccessConfigRequestHeader) request.decodeCommandCustomHeader(CreateAccessConfigRequestHeader.class); + final CreateAccessConfigRequestHeader requestHeader = + (CreateAccessConfigRequestHeader) request.decodeCommandCustomHeader(CreateAccessConfigRequestHeader.class); PlainAccessConfig accessConfig = new PlainAccessConfig(); accessConfig.setAccessKey(requestHeader.getAccessKey()); @@ -572,7 +581,8 @@ private synchronized RemotingCommand deleteAccessConfig(ChannelHandlerContext ct RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - final DeleteAccessConfigRequestHeader requestHeader = (DeleteAccessConfigRequestHeader) request.decodeCommandCustomHeader(DeleteAccessConfigRequestHeader.class); + final DeleteAccessConfigRequestHeader requestHeader = + (DeleteAccessConfigRequestHeader) request.decodeCommandCustomHeader(DeleteAccessConfigRequestHeader.class); LOGGER.info("DeleteAccessConfig called by {}", RemotingHelper.parseChannelRemoteAddr(ctx.channel())); try { @@ -607,11 +617,13 @@ private synchronized RemotingCommand updateGlobalWhiteAddrsConfig(ChannelHandler final RemotingCommand response = RemotingCommand.createResponseCommand(null); - final UpdateGlobalWhiteAddrsConfigRequestHeader requestHeader = (UpdateGlobalWhiteAddrsConfigRequestHeader) request.decodeCommandCustomHeader(UpdateGlobalWhiteAddrsConfigRequestHeader.class); + final UpdateGlobalWhiteAddrsConfigRequestHeader requestHeader = + (UpdateGlobalWhiteAddrsConfigRequestHeader) request.decodeCommandCustomHeader(UpdateGlobalWhiteAddrsConfigRequestHeader.class); try { AccessValidator accessValidator = this.brokerController.getAccessValidatorMap().get(PlainAccessValidator.class); - if (accessValidator.updateGlobalWhiteAddrsConfig(UtilAll.split(requestHeader.getGlobalWhiteAddrs(), ","), requestHeader.getAclFileFullPath())) { + if (accessValidator.updateGlobalWhiteAddrsConfig(UtilAll.split(requestHeader.getGlobalWhiteAddrs(), ","), + requestHeader.getAclFileFullPath())) { response.setCode(ResponseCode.SUCCESS); response.setOpaque(request.getOpaque()); response.markResponseType(); @@ -681,7 +693,8 @@ private RemotingCommand getBrokerClusterAclConfig(ChannelHandlerContext ctx, Rem private RemotingCommand getUnknownCmdResponse(ChannelHandlerContext ctx, RemotingCommand request) { String error = " request type " + request.getCode() + " not supported"; - final RemotingCommand response = RemotingCommand.createResponseCommand(RemotingSysResponseCode.REQUEST_CODE_NOT_SUPPORTED, error); + final RemotingCommand response = + RemotingCommand.createResponseCommand(RemotingSysResponseCode.REQUEST_CODE_NOT_SUPPORTED, error); return response; } @@ -777,7 +790,8 @@ private synchronized RemotingCommand updateBrokerConfig(ChannelHandlerContext ct return response; } } catch (UnsupportedEncodingException e) { - LOGGER.error("AdminBrokerProcessor#updateBrokerConfig: unexpected error, caller={}", callerAddress, e); + LOGGER.error("AdminBrokerProcessor#updateBrokerConfig: unexpected error, caller={}", + callerAddress, e); response.setCode(ResponseCode.SYSTEM_ERROR); response.setRemark("UnsupportedEncodingException " + e); return response; @@ -799,7 +813,8 @@ private RemotingCommand getBrokerConfig(ChannelHandlerContext ctx, RemotingComma try { response.setBody(content.getBytes(MixAll.DEFAULT_CHARSET)); } catch (UnsupportedEncodingException e) { - LOGGER.error("AdminBrokerProcessor#getBrokerConfig: unexpected error, caller={}", RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e); + LOGGER.error("AdminBrokerProcessor#getBrokerConfig: unexpected error, caller={}", + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e); response.setCode(ResponseCode.SYSTEM_ERROR); response.setRemark("UnsupportedEncodingException " + e); @@ -851,7 +866,8 @@ private RemotingCommand rewriteRequestForStaticTopic(SearchOffsetRequestHeader r throw rpcResponse.getException(); } SearchOffsetResponseHeader offsetResponseHeader = (SearchOffsetResponseHeader) rpcResponse.getHeader(); - if (offsetResponseHeader.getOffset() < 0 || item.checkIfEndOffsetDecided() && offsetResponseHeader.getOffset() >= item.getEndOffset()) { + if (offsetResponseHeader.getOffset() < 0 + || item.checkIfEndOffsetDecided() && offsetResponseHeader.getOffset() >= item.getEndOffset()) { continue; } else { offset = item.computeStaticQueueOffsetStrictly(offsetResponseHeader.getOffset()); @@ -874,7 +890,8 @@ private RemotingCommand searchOffsetByTimestamp(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(SearchOffsetResponseHeader.class); final SearchOffsetResponseHeader responseHeader = (SearchOffsetResponseHeader) response.readCustomHeader(); - final SearchOffsetRequestHeader requestHeader = (SearchOffsetRequestHeader) request.decodeCommandCustomHeader(SearchOffsetRequestHeader.class); + final SearchOffsetRequestHeader requestHeader = + (SearchOffsetRequestHeader) request.decodeCommandCustomHeader(SearchOffsetRequestHeader.class); TopicQueueMappingContext mappingContext = this.brokerController.getTopicQueueMappingManager().buildTopicQueueMappingContext(requestHeader); @@ -883,7 +900,8 @@ private RemotingCommand searchOffsetByTimestamp(ChannelHandlerContext ctx, return rewriteResult; } - long offset = this.brokerController.getMessageStore().getOffsetInQueueByTime(requestHeader.getTopic(), requestHeader.getQueueId(), requestHeader.getTimestamp()); + long offset = this.brokerController.getMessageStore().getOffsetInQueueByTime(requestHeader.getTopic(), requestHeader.getQueueId(), + requestHeader.getTimestamp()); responseHeader.setOffset(offset); @@ -941,7 +959,8 @@ private RemotingCommand getMaxOffset(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(GetMaxOffsetResponseHeader.class); final GetMaxOffsetResponseHeader responseHeader = (GetMaxOffsetResponseHeader) response.readCustomHeader(); - final GetMaxOffsetRequestHeader requestHeader = (GetMaxOffsetRequestHeader) request.decodeCommandCustomHeader(GetMaxOffsetRequestHeader.class); + final GetMaxOffsetRequestHeader requestHeader = + (GetMaxOffsetRequestHeader) request.decodeCommandCustomHeader(GetMaxOffsetRequestHeader.class); TopicQueueMappingContext mappingContext = this.brokerController.getTopicQueueMappingManager().buildTopicQueueMappingContext(requestHeader); RemotingCommand rewriteResult = rewriteRequestForStaticTopic(requestHeader, mappingContext); @@ -966,7 +985,8 @@ private CompletableFuture handleGetMinOffsetForStaticTopic(RpcReque TopicQueueMappingDetail mappingDetail = mappingContext.getMappingDetail(); if (!mappingContext.isLeader()) { //this may not - return CompletableFuture.completedFuture(new RpcResponse(new RpcException(ResponseCode.NOT_LEADER_FOR_QUEUE, String.format("%s-%d is not leader in broker %s, request code %d", mappingContext.getTopic(), mappingContext.getGlobalId(), mappingDetail.getBname(), request.getCode())))); + return CompletableFuture.completedFuture(new RpcResponse(new RpcException(ResponseCode.NOT_LEADER_FOR_QUEUE, + String.format("%s-%d is not leader in broker %s, request code %d", mappingContext.getTopic(), mappingContext.getGlobalId(), mappingDetail.getBname(), request.getCode())))); } GetMinOffsetRequestHeader requestHeader = (GetMinOffsetRequestHeader) request.getHeader(); LogicQueueMappingItem mappingItem = TopicQueueMappingUtils.findLogicQueueMappingItem(mappingContext.getMappingItemList(), 0L, true); @@ -1015,7 +1035,8 @@ private CompletableFuture handleGetMinOffset(RpcRequest request) { private RemotingCommand getMinOffset(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { - final GetMinOffsetRequestHeader requestHeader = (GetMinOffsetRequestHeader) request.decodeCommandCustomHeader(GetMinOffsetRequestHeader.class); + final GetMinOffsetRequestHeader requestHeader = + (GetMinOffsetRequestHeader) request.decodeCommandCustomHeader(GetMinOffsetRequestHeader.class); try { CompletableFuture responseFuture = handleGetMinOffset(new RpcRequest(RequestCode.GET_MIN_OFFSET, requestHeader, null)); RpcResponse rpcResponse = responseFuture.get(); @@ -1063,7 +1084,8 @@ private RemotingCommand getEarliestMsgStoretime(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(GetEarliestMsgStoretimeResponseHeader.class); final GetEarliestMsgStoretimeResponseHeader responseHeader = (GetEarliestMsgStoretimeResponseHeader) response.readCustomHeader(); - final GetEarliestMsgStoretimeRequestHeader requestHeader = (GetEarliestMsgStoretimeRequestHeader) request.decodeCommandCustomHeader(GetEarliestMsgStoretimeRequestHeader.class); + final GetEarliestMsgStoretimeRequestHeader requestHeader = + (GetEarliestMsgStoretimeRequestHeader) request.decodeCommandCustomHeader(GetEarliestMsgStoretimeRequestHeader.class); TopicQueueMappingContext mappingContext = this.brokerController.getTopicQueueMappingManager().buildTopicQueueMappingContext(requestHeader, false); RemotingCommand rewriteResult = rewriteRequestForStaticTopic(requestHeader, mappingContext); @@ -1071,7 +1093,8 @@ private RemotingCommand getEarliestMsgStoretime(ChannelHandlerContext ctx, return rewriteResult; } - long timestamp = this.brokerController.getMessageStore().getEarliestMessageTime(requestHeader.getTopic(), requestHeader.getQueueId()); + long timestamp = + this.brokerController.getMessageStore().getEarliestMessageTime(requestHeader.getTopic(), requestHeader.getQueueId()); responseHeader.setTimestamp(timestamp); response.setCode(ResponseCode.SUCCESS); @@ -1099,7 +1122,10 @@ private RemotingCommand lockBatchMQ(ChannelHandlerContext ctx, LockBatchRequestBody requestBody = LockBatchRequestBody.decode(request.getBody(), LockBatchRequestBody.class); Set lockOKMQSet = new HashSet<>(); - Set selfLockOKMQSet = this.brokerController.getRebalanceLockManager().tryLockBatch(requestBody.getConsumerGroup(), requestBody.getMqSet(), requestBody.getClientId()); + Set selfLockOKMQSet = this.brokerController.getRebalanceLockManager().tryLockBatch( + requestBody.getConsumerGroup(), + requestBody.getMqSet(), + requestBody.getClientId()); if (requestBody.isOnlyThisBroker() || !brokerController.getBrokerConfig().isLockInStrictMode()) { lockOKMQSet = selfLockOKMQSet; } else { @@ -1129,22 +1155,25 @@ private RemotingCommand lockBatchMQ(ChannelHandlerContext ctx, requestBody.setOnlyThisBroker(true); for (Long brokerId : addrMap.keySet()) { try { - this.brokerController.getBrokerOuterAPI().lockBatchMQAsync(addrMap.get(brokerId), requestBody, 1000, new LockCallback() { - @Override public void onSuccess(Set lockOKMQSet) { - for (MessageQueue mq : lockOKMQSet) { - if (!mqLockMap.containsKey(mq)) { - mqLockMap.put(mq, 0); + this.brokerController.getBrokerOuterAPI().lockBatchMQAsync(addrMap.get(brokerId), + requestBody, 1000, new LockCallback() { + @Override + public void onSuccess(Set lockOKMQSet) { + for (MessageQueue mq : lockOKMQSet) { + if (!mqLockMap.containsKey(mq)) { + mqLockMap.put(mq, 0); + } + mqLockMap.put(mq, mqLockMap.get(mq) + 1); } - mqLockMap.put(mq, mqLockMap.get(mq) + 1); + countDownLatch.countDown(); + } + + @Override + public void onException(Throwable e) { + LOGGER.warn("lockBatchMQAsync on {} failed, {}", addrMap.get(brokerId), e); + countDownLatch.countDown(); } - countDownLatch.countDown(); - } - - @Override public void onException(Throwable e) { - LOGGER.warn("lockBatchMQAsync on {} failed, {}", addrMap.get(brokerId), e); - countDownLatch.countDown(); - } - }); + }); } catch (Exception e) { LOGGER.warn("lockBatchMQAsync on {} failed, {}", addrMap.get(brokerId), e); countDownLatch.countDown(); @@ -1180,7 +1209,10 @@ private RemotingCommand unlockBatchMQ(ChannelHandlerContext ctx, UnlockBatchRequestBody requestBody = UnlockBatchRequestBody.decode(request.getBody(), UnlockBatchRequestBody.class); if (requestBody.isOnlyThisBroker() || !this.brokerController.getBrokerConfig().isLockInStrictMode()) { - this.brokerController.getRebalanceLockManager().unlockBatch(requestBody.getConsumerGroup(), requestBody.getMqSet(), requestBody.getClientId()); + this.brokerController.getRebalanceLockManager().unlockBatch( + requestBody.getConsumerGroup(), + requestBody.getMqSet(), + requestBody.getClientId()); } else { requestBody.setOnlyThisBroker(true); BrokerMemberGroup memberGroup = this.brokerController.getBrokerMemberGroup(); @@ -1190,11 +1222,13 @@ private RemotingCommand unlockBatchMQ(ChannelHandlerContext ctx, for (Long brokerId : addrMap.keySet()) { try { this.brokerController.getBrokerOuterAPI().unlockBatchMQAsync(addrMap.get(brokerId), requestBody, 1000, new UnlockCallback() { - @Override public void onSuccess() { + @Override + public void onSuccess() { } - @Override public void onException(Throwable e) { + @Override + public void onException(Throwable e) { LOGGER.warn("unlockBatchMQ exception on {}, {}", addrMap.get(brokerId), e); } }); @@ -1210,14 +1244,15 @@ private RemotingCommand unlockBatchMQ(ChannelHandlerContext ctx, return response; } - private RemotingCommand updateAndCreateSubscriptionGroup(ChannelHandlerContext ctx, - RemotingCommand request) throws RemotingCommandException { + private RemotingCommand updateAndCreateSubscriptionGroup(ChannelHandlerContext ctx, RemotingCommand request) + throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); if (validateSlave(response)) { return response; } - LOGGER.info("AdminBrokerProcessor#updateAndCreateSubscriptionGroup called by {}", RemotingHelper.parseChannelRemoteAddr(ctx.channel())); + LOGGER.info("AdminBrokerProcessor#updateAndCreateSubscriptionGroup called by {}", + RemotingHelper.parseChannelRemoteAddr(ctx.channel())); SubscriptionGroupConfig config = RemotingSerializable.decode(request.getBody(), SubscriptionGroupConfig.class); if (config != null) { @@ -1244,7 +1279,8 @@ private void initConsumerOffset(String clientHost, String groupName, int mode, T } } this.brokerController.getConsumerOffsetManager().commitOffset(clientHost, groupName, topic, queueId, offset); - LOGGER.info("AdminBrokerProcessor#initConsumerOffset: consumerGroup={}, topic={}, queueId={}, offset={}", groupName, topic, queueId, offset); + LOGGER.info("AdminBrokerProcessor#initConsumerOffset: consumerGroup={}, topic={}, queueId={}, offset={}", + groupName, topic, queueId, offset); } } @@ -1281,9 +1317,11 @@ private RemotingCommand deleteSubscriptionGroup(ChannelHandlerContext ctx, if (validateSlave(response)) { return response; } - DeleteSubscriptionGroupRequestHeader requestHeader = (DeleteSubscriptionGroupRequestHeader) request.decodeCommandCustomHeader(DeleteSubscriptionGroupRequestHeader.class); + DeleteSubscriptionGroupRequestHeader requestHeader = + (DeleteSubscriptionGroupRequestHeader) request.decodeCommandCustomHeader(DeleteSubscriptionGroupRequestHeader.class); - LOGGER.info("AdminBrokerProcessor#deleteSubscriptionGroup, caller={}", RemotingHelper.parseChannelRemoteAddr(ctx.channel())); + LOGGER.info("AdminBrokerProcessor#deleteSubscriptionGroup, caller={}", + RemotingHelper.parseChannelRemoteAddr(ctx.channel())); this.brokerController.getSubscriptionGroupManager().deleteSubscriptionGroupConfig(requestHeader.getGroupName()); @@ -1303,7 +1341,8 @@ private RemotingCommand deleteSubscriptionGroup(ChannelHandlerContext ctx, private RemotingCommand getTopicStatsInfo(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - final GetTopicStatsInfoRequestHeader requestHeader = (GetTopicStatsInfoRequestHeader) request.decodeCommandCustomHeader(GetTopicStatsInfoRequestHeader.class); + final GetTopicStatsInfoRequestHeader requestHeader = + (GetTopicStatsInfoRequestHeader) request.decodeCommandCustomHeader(GetTopicStatsInfoRequestHeader.class); final String topic = requestHeader.getTopic(); TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(topic); @@ -1353,9 +1392,11 @@ private RemotingCommand getTopicStatsInfo(ChannelHandlerContext ctx, private RemotingCommand getConsumerConnectionList(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - final GetConsumerConnectionListRequestHeader requestHeader = (GetConsumerConnectionListRequestHeader) request.decodeCommandCustomHeader(GetConsumerConnectionListRequestHeader.class); + final GetConsumerConnectionListRequestHeader requestHeader = + (GetConsumerConnectionListRequestHeader) request.decodeCommandCustomHeader(GetConsumerConnectionListRequestHeader.class); - ConsumerGroupInfo consumerGroupInfo = this.brokerController.getConsumerManager().getConsumerGroupInfo(requestHeader.getConsumerGroup()); + ConsumerGroupInfo consumerGroupInfo = + this.brokerController.getConsumerManager().getConsumerGroupInfo(requestHeader.getConsumerGroup()); if (consumerGroupInfo != null) { ConsumerConnection bodydata = new ConsumerConnection(); bodydata.setConsumeFromWhere(consumerGroupInfo.getConsumeFromWhere()); @@ -1388,10 +1429,10 @@ private RemotingCommand getConsumerConnectionList(ChannelHandlerContext ctx, return response; } - private RemotingCommand getAllProducerInfo(ChannelHandlerContext ctx, - RemotingCommand request) throws RemotingCommandException { + private RemotingCommand getAllProducerInfo(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - final GetAllProducerInfoRequestHeader requestHeader = (GetAllProducerInfoRequestHeader) request.decodeCommandCustomHeader(GetAllProducerInfoRequestHeader.class); + final GetAllProducerInfoRequestHeader requestHeader = + (GetAllProducerInfoRequestHeader) request.decodeCommandCustomHeader(GetAllProducerInfoRequestHeader.class); ProducerTableInfo producerTable = this.brokerController.getProducerManager().getProducerTable(); if (producerTable != null) { @@ -1405,14 +1446,15 @@ private RemotingCommand getAllProducerInfo(ChannelHandlerContext ctx, response.setCode(ResponseCode.SYSTEM_ERROR); return response; } - private RemotingCommand getProducerConnectionList(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - final GetProducerConnectionListRequestHeader requestHeader = (GetProducerConnectionListRequestHeader) request.decodeCommandCustomHeader(GetProducerConnectionListRequestHeader.class); + final GetProducerConnectionListRequestHeader requestHeader = + (GetProducerConnectionListRequestHeader) request.decodeCommandCustomHeader(GetProducerConnectionListRequestHeader.class); ProducerConnection bodydata = new ProducerConnection(); - Map channelInfoHashMap = this.brokerController.getProducerManager().getGroupChannelTable().get(requestHeader.getProducerGroup()); + Map channelInfoHashMap = + this.brokerController.getProducerManager().getGroupChannelTable().get(requestHeader.getProducerGroup()); if (channelInfoHashMap != null) { Iterator> it = channelInfoHashMap.entrySet().iterator(); while (it.hasNext()) { @@ -1441,7 +1483,8 @@ private RemotingCommand getProducerConnectionList(ChannelHandlerContext ctx, private RemotingCommand getConsumeStats(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - final GetConsumeStatsRequestHeader requestHeader = (GetConsumeStatsRequestHeader) request.decodeCommandCustomHeader(GetConsumeStatsRequestHeader.class); + final GetConsumeStatsRequestHeader requestHeader = + (GetConsumeStatsRequestHeader) request.decodeCommandCustomHeader(GetConsumeStatsRequestHeader.class); ConsumeStats consumeStats = new ConsumeStats(); @@ -1462,10 +1505,14 @@ private RemotingCommand getConsumeStats(ChannelHandlerContext ctx, TopicQueueMappingDetail mappingDetail = this.brokerController.getTopicQueueMappingManager().getTopicQueueMapping(topic); { - SubscriptionData findSubscriptionData = this.brokerController.getConsumerManager().findSubscriptionData(requestHeader.getConsumerGroup(), topic); - - if (null == findSubscriptionData && this.brokerController.getConsumerManager().findSubscriptionDataCount(requestHeader.getConsumerGroup()) > 0) { - LOGGER.warn("AdminBrokerProcessor#getConsumeStats: topic does not exist in consumer group's subscription, " + "topic={}, consumer group={}", topic, requestHeader.getConsumerGroup()); + SubscriptionData findSubscriptionData = + this.brokerController.getConsumerManager().findSubscriptionData(requestHeader.getConsumerGroup(), topic); + + if (null == findSubscriptionData + && this.brokerController.getConsumerManager().findSubscriptionDataCount(requestHeader.getConsumerGroup()) > 0) { + LOGGER.warn( + "AdminBrokerProcessor#getConsumeStats: topic does not exist in consumer group's subscription, " + + "topic={}, consumer group={}", topic, requestHeader.getConsumerGroup()); continue; } } @@ -1483,7 +1530,8 @@ private RemotingCommand getConsumeStats(ChannelHandlerContext ctx, brokerOffset = 0; } - long consumerOffset = this.brokerController.getConsumerOffsetManager().queryOffset(requestHeader.getConsumerGroup(), topic, i); + long consumerOffset = this.brokerController.getConsumerOffsetManager().queryOffset( + requestHeader.getConsumerGroup(), topic, i); // the consumerOffset cannot be zero for static topic because of the "double read check" strategy // just remain the logic for dynamic topic @@ -1494,7 +1542,8 @@ private RemotingCommand getConsumeStats(ChannelHandlerContext ctx, } } - long pullOffset = this.brokerController.getConsumerOffsetManager().queryPullOffset(requestHeader.getConsumerGroup(), topic, i); + long pullOffset = this.brokerController.getConsumerOffsetManager().queryPullOffset( + requestHeader.getConsumerGroup(), topic, i); offsetWrapper.setBrokerOffset(brokerOffset); offsetWrapper.setConsumerOffset(consumerOffset); @@ -1559,14 +1608,16 @@ private RemotingCommand getAllDelayOffset(ChannelHandlerContext ctx, RemotingCom try { response.setBody(content.getBytes(MixAll.DEFAULT_CHARSET)); } catch (UnsupportedEncodingException e) { - LOGGER.error("AdminBrokerProcessor#getAllDelayOffset: unexpected error, caller={}.", RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e); + LOGGER.error("AdminBrokerProcessor#getAllDelayOffset: unexpected error, caller={}.", + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), e); response.setCode(ResponseCode.SYSTEM_ERROR); response.setRemark("UnsupportedEncodingException " + e); return response; } } else { - LOGGER.error("AdminBrokerProcessor#getAllDelayOffset: no delay offset in this broker, caller={}", RemotingHelper.parseChannelRemoteAddr(ctx.channel())); + LOGGER.error("AdminBrokerProcessor#getAllDelayOffset: no delay offset in this broker, caller={}", + RemotingHelper.parseChannelRemoteAddr(ctx.channel())); response.setCode(ResponseCode.SYSTEM_ERROR); response.setRemark("No delay offset in this broker"); return response; @@ -1607,8 +1658,11 @@ private RemotingCommand getAllMessageRequestMode(ChannelHandlerContext ctx, Remo public RemotingCommand resetOffset(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { - final ResetOffsetRequestHeader requestHeader = (ResetOffsetRequestHeader) request.decodeCommandCustomHeader(ResetOffsetRequestHeader.class); - LOGGER.info("[reset-offset] reset offset started by {}. topic={}, group={}, timestamp={}, isForce={}", RemotingHelper.parseChannelRemoteAddr(ctx.channel()), requestHeader.getTopic(), requestHeader.getGroup(), requestHeader.getTimestamp(), requestHeader.isForce()); + final ResetOffsetRequestHeader requestHeader = + (ResetOffsetRequestHeader) request.decodeCommandCustomHeader(ResetOffsetRequestHeader.class); + LOGGER.info("[reset-offset] reset offset started by {}. topic={}, group={}, timestamp={}, isForce={}", + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), requestHeader.getTopic(), requestHeader.getGroup(), + requestHeader.getTimestamp(), requestHeader.isForce()); if (this.brokerController.getBrokerConfig().isUseServerSideResetOffset()) { String topic = requestHeader.getTopic(); @@ -1626,7 +1680,8 @@ public RemotingCommand resetOffset(ChannelHandlerContext ctx, isC = true; break; } - return this.brokerController.getBroker2Client().resetOffset(requestHeader.getTopic(), requestHeader.getGroup(), requestHeader.getTimestamp(), requestHeader.isForce(), isC); + return this.brokerController.getBroker2Client().resetOffset(requestHeader.getTopic(), requestHeader.getGroup(), + requestHeader.getTimestamp(), requestHeader.isForce(), isC); } private Long searchOffsetByTimestamp(String topic, int queueId, long timestamp) { @@ -1640,13 +1695,13 @@ private Long searchOffsetByTimestamp(String topic, int queueId, long timestamp) /** * Reset consumer offset. * - * @param topic Required, not null. - * @param group Required, not null. - * @param queueId if target queue ID is negative, all message queues will be reset; otherwise, only the target queue - * would get reset. - * @param timestamp if timestamp is negative, offset would be reset to broker offset at the time being; otherwise, - * binary search is performed to locate target offset. - * @param offset Target offset to reset to if target queue ID is properly provided. + * @param topic Required, not null. + * @param group Required, not null. + * @param queueId if target queue ID is negative, all message queues will be reset; + * otherwise, only the target queue would get reset. + * @param timestamp if timestamp is negative, offset would be reset to broker offset at the time being; + * otherwise, binary search is performed to locate target offset. + * @param offset Target offset to reset to if target queue ID is properly provided. * @return Affected queues and their new offset */ private RemotingCommand resetOffsetInner(String topic, String group, int queueId, long timestamp, Long offset) { @@ -1682,7 +1737,8 @@ private RemotingCommand resetOffsetInner(String topic, String group, int queueId long max = brokerController.getMessageStore().getMaxOffsetInQueue(topic, queueId); if (min >= 0 && offset < min || offset > max + 1) { response.setCode(ResponseCode.SYSTEM_ERROR); - response.setRemark(String.format("Target offset %d not in consume queue range [%d-%d]", offset, min, max)); + response.setRemark( + String.format("Target offset %d not in consume queue range [%d-%d]", offset, min, max)); return response; } } else { @@ -1704,7 +1760,8 @@ private RemotingCommand resetOffsetInner(String topic, String group, int queueId } for (Map.Entry entry : queueOffsetMap.entrySet()) { - brokerController.getConsumerOffsetManager().assignResetOffset(topic, group, entry.getKey(), entry.getValue()); + brokerController.getConsumerOffsetManager() + .assignResetOffset(topic, group, entry.getKey(), entry.getValue()); } // Prepare reset result. @@ -1722,17 +1779,21 @@ private RemotingCommand resetOffsetInner(String topic, String group, int queueId public RemotingCommand getConsumerStatus(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { - final GetConsumerStatusRequestHeader requestHeader = (GetConsumerStatusRequestHeader) request.decodeCommandCustomHeader(GetConsumerStatusRequestHeader.class); + final GetConsumerStatusRequestHeader requestHeader = + (GetConsumerStatusRequestHeader) request.decodeCommandCustomHeader(GetConsumerStatusRequestHeader.class); - LOGGER.info("[get-consumer-status] get consumer status by {}. topic={}, group={}", RemotingHelper.parseChannelRemoteAddr(ctx.channel()), requestHeader.getTopic(), requestHeader.getGroup()); + LOGGER.info("[get-consumer-status] get consumer status by {}. topic={}, group={}", + RemotingHelper.parseChannelRemoteAddr(ctx.channel()), requestHeader.getTopic(), requestHeader.getGroup()); - return this.brokerController.getBroker2Client().getConsumeStatus(requestHeader.getTopic(), requestHeader.getGroup(), requestHeader.getClientAddr()); + return this.brokerController.getBroker2Client().getConsumeStatus(requestHeader.getTopic(), requestHeader.getGroup(), + requestHeader.getClientAddr()); } private RemotingCommand queryTopicConsumeByWho(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - QueryTopicConsumeByWhoRequestHeader requestHeader = (QueryTopicConsumeByWhoRequestHeader) request.decodeCommandCustomHeader(QueryTopicConsumeByWhoRequestHeader.class); + QueryTopicConsumeByWhoRequestHeader requestHeader = + (QueryTopicConsumeByWhoRequestHeader) request.decodeCommandCustomHeader(QueryTopicConsumeByWhoRequestHeader.class); HashSet groups = this.brokerController.getConsumerManager().queryTopicConsumeByWho(requestHeader.getTopic()); @@ -1754,7 +1815,8 @@ private RemotingCommand queryTopicConsumeByWho(ChannelHandlerContext ctx, private RemotingCommand queryTopicsByConsumer(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - QueryTopicsByConsumerRequestHeader requestHeader = (QueryTopicsByConsumerRequestHeader) request.decodeCommandCustomHeader(QueryTopicsByConsumerRequestHeader.class); + QueryTopicsByConsumerRequestHeader requestHeader = + (QueryTopicsByConsumerRequestHeader) request.decodeCommandCustomHeader(QueryTopicsByConsumerRequestHeader.class); Set topics = this.brokerController.getConsumerOffsetManager().whichTopicByConsumer(requestHeader.getGroup()); @@ -1772,9 +1834,11 @@ private RemotingCommand queryTopicsByConsumer(ChannelHandlerContext ctx, private RemotingCommand querySubscriptionByConsumer(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - QuerySubscriptionByConsumerRequestHeader requestHeader = (QuerySubscriptionByConsumerRequestHeader) request.decodeCommandCustomHeader(QuerySubscriptionByConsumerRequestHeader.class); + QuerySubscriptionByConsumerRequestHeader requestHeader = + (QuerySubscriptionByConsumerRequestHeader) request.decodeCommandCustomHeader(QuerySubscriptionByConsumerRequestHeader.class); - SubscriptionData subscriptionData = this.brokerController.getConsumerManager().findSubscriptionData(requestHeader.getGroup(), requestHeader.getTopic()); + SubscriptionData subscriptionData = this.brokerController.getConsumerManager() + .findSubscriptionData(requestHeader.getGroup(), requestHeader.getTopic()); QuerySubscriptionResponseBody responseBody = new QuerySubscriptionResponseBody(); responseBody.setGroup(requestHeader.getGroup()); @@ -1793,7 +1857,8 @@ private RemotingCommand registerFilterServer(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(RegisterFilterServerResponseHeader.class); final RegisterFilterServerResponseHeader responseHeader = (RegisterFilterServerResponseHeader) response.readCustomHeader(); - final RegisterFilterServerRequestHeader requestHeader = (RegisterFilterServerRequestHeader) request.decodeCommandCustomHeader(RegisterFilterServerRequestHeader.class); + final RegisterFilterServerRequestHeader requestHeader = + (RegisterFilterServerRequestHeader) request.decodeCommandCustomHeader(RegisterFilterServerRequestHeader.class); this.brokerController.getFilterServerManager().registerFilterServer(ctx.channel(), requestHeader.getFilterServerAddr()); @@ -1808,7 +1873,8 @@ private RemotingCommand registerFilterServer(ChannelHandlerContext ctx, private RemotingCommand queryConsumeTimeSpan(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - QueryConsumeTimeSpanRequestHeader requestHeader = (QueryConsumeTimeSpanRequestHeader) request.decodeCommandCustomHeader(QueryConsumeTimeSpanRequestHeader.class); + QueryConsumeTimeSpanRequestHeader requestHeader = + (QueryConsumeTimeSpanRequestHeader) request.decodeCommandCustomHeader(QueryConsumeTimeSpanRequestHeader.class); final String topic = requestHeader.getTopic(); TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(topic); @@ -1835,7 +1901,8 @@ private RemotingCommand queryConsumeTimeSpan(ChannelHandlerContext ctx, timeSpan.setMaxTimeStamp(maxTime); long consumeTime; - long consumerOffset = this.brokerController.getConsumerOffsetManager().queryOffset(requestHeader.getGroup(), topic, i); + long consumerOffset = this.brokerController.getConsumerOffsetManager().queryOffset( + requestHeader.getGroup(), topic, i); if (consumerOffset > 0) { consumeTime = this.brokerController.getMessageStore().getMessageStoreTimeStamp(topic, i, consumerOffset - 1); } else { @@ -1859,8 +1926,8 @@ private RemotingCommand queryConsumeTimeSpan(ChannelHandlerContext ctx, return response; } - private RemotingCommand getSystemTopicListFromBroker(ChannelHandlerContext ctx, - RemotingCommand request) throws RemotingCommandException { + private RemotingCommand getSystemTopicListFromBroker(ChannelHandlerContext ctx, RemotingCommand request) + throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); Set topics = TopicValidator.getSystemTopicSet(); @@ -1904,24 +1971,30 @@ public RemotingCommand cleanUnusedTopic() { private RemotingCommand getConsumerRunningInfo(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { - final GetConsumerRunningInfoRequestHeader requestHeader = (GetConsumerRunningInfoRequestHeader) request.decodeCommandCustomHeader(GetConsumerRunningInfoRequestHeader.class); + final GetConsumerRunningInfoRequestHeader requestHeader = + (GetConsumerRunningInfoRequestHeader) request.decodeCommandCustomHeader(GetConsumerRunningInfoRequestHeader.class); - return this.callConsumer(RequestCode.GET_CONSUMER_RUNNING_INFO, request, requestHeader.getConsumerGroup(), requestHeader.getClientId()); + return this.callConsumer(RequestCode.GET_CONSUMER_RUNNING_INFO, request, requestHeader.getConsumerGroup(), + requestHeader.getClientId()); } private RemotingCommand queryCorrectionOffset(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - QueryCorrectionOffsetHeader requestHeader = (QueryCorrectionOffsetHeader) request.decodeCommandCustomHeader(QueryCorrectionOffsetHeader.class); + QueryCorrectionOffsetHeader requestHeader = + (QueryCorrectionOffsetHeader) request.decodeCommandCustomHeader(QueryCorrectionOffsetHeader.class); - Map correctionOffset = this.brokerController.getConsumerOffsetManager().queryMinOffsetInAllGroup(requestHeader.getTopic(), requestHeader.getFilterGroups()); + Map correctionOffset = this.brokerController.getConsumerOffsetManager() + .queryMinOffsetInAllGroup(requestHeader.getTopic(), requestHeader.getFilterGroups()); - Map compareOffset = this.brokerController.getConsumerOffsetManager().queryOffset(requestHeader.getCompareGroup(), requestHeader.getTopic()); + Map compareOffset = + this.brokerController.getConsumerOffsetManager().queryOffset(requestHeader.getCompareGroup(), requestHeader.getTopic()); if (compareOffset != null && !compareOffset.isEmpty()) { for (Map.Entry entry : compareOffset.entrySet()) { Integer queueId = entry.getKey(); - correctionOffset.put(queueId, correctionOffset.get(queueId) > entry.getValue() ? Long.MAX_VALUE : correctionOffset.get(queueId)); + correctionOffset.put(queueId, + correctionOffset.get(queueId) > entry.getValue() ? Long.MAX_VALUE : correctionOffset.get(queueId)); } } @@ -1935,7 +2008,8 @@ private RemotingCommand queryCorrectionOffset(ChannelHandlerContext ctx, private RemotingCommand consumeMessageDirectly(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { - final ConsumeMessageDirectlyResultRequestHeader requestHeader = (ConsumeMessageDirectlyResultRequestHeader) request.decodeCommandCustomHeader(ConsumeMessageDirectlyResultRequestHeader.class); + final ConsumeMessageDirectlyResultRequestHeader requestHeader = (ConsumeMessageDirectlyResultRequestHeader) request + .decodeCommandCustomHeader(ConsumeMessageDirectlyResultRequestHeader.class); // brokerName request.getExtFields().put("brokerName", this.brokerController.getBrokerConfig().getBrokerName()); @@ -1968,13 +2042,15 @@ private RemotingCommand consumeMessageDirectly(ChannelHandlerContext ctx, } } - return this.callConsumer(RequestCode.CONSUME_MESSAGE_DIRECTLY, request, requestHeader.getConsumerGroup(), requestHeader.getClientId()); + return this.callConsumer(RequestCode.CONSUME_MESSAGE_DIRECTLY, request, requestHeader.getConsumerGroup(), + requestHeader.getClientId()); } private RemotingCommand cloneGroupOffset(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - CloneGroupOffsetRequestHeader requestHeader = (CloneGroupOffsetRequestHeader) request.decodeCommandCustomHeader(CloneGroupOffsetRequestHeader.class); + CloneGroupOffsetRequestHeader requestHeader = + (CloneGroupOffsetRequestHeader) request.decodeCommandCustomHeader(CloneGroupOffsetRequestHeader.class); Set topics; if (UtilAll.isBlank(requestHeader.getTopic())) { @@ -1993,14 +2069,19 @@ private RemotingCommand cloneGroupOffset(ChannelHandlerContext ctx, if (!requestHeader.isOffline()) { - SubscriptionData findSubscriptionData = this.brokerController.getConsumerManager().findSubscriptionData(requestHeader.getSrcGroup(), topic); - if (this.brokerController.getConsumerManager().findSubscriptionDataCount(requestHeader.getSrcGroup()) > 0 && findSubscriptionData == null) { - LOGGER.warn("AdminBrokerProcessor#cloneGroupOffset: topic does not exist in consumer group's " + "subscription, topic={}, consumer group={}", topic, requestHeader.getSrcGroup()); + SubscriptionData findSubscriptionData = + this.brokerController.getConsumerManager().findSubscriptionData(requestHeader.getSrcGroup(), topic); + if (this.brokerController.getConsumerManager().findSubscriptionDataCount(requestHeader.getSrcGroup()) > 0 + && findSubscriptionData == null) { + LOGGER.warn( + "AdminBrokerProcessor#cloneGroupOffset: topic does not exist in consumer group's " + + "subscription, topic={}, consumer group={}", topic, requestHeader.getSrcGroup()); continue; } } - this.brokerController.getConsumerOffsetManager().cloneOffset(requestHeader.getSrcGroup(), requestHeader.getDestGroup(), requestHeader.getTopic()); + this.brokerController.getConsumerOffsetManager().cloneOffset(requestHeader.getSrcGroup(), requestHeader.getDestGroup(), + requestHeader.getTopic()); } response.setCode(ResponseCode.SUCCESS); @@ -2010,7 +2091,8 @@ private RemotingCommand cloneGroupOffset(ChannelHandlerContext ctx, private RemotingCommand ViewBrokerStatsData(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { - final ViewBrokerStatsDataRequestHeader requestHeader = (ViewBrokerStatsDataRequestHeader) request.decodeCommandCustomHeader(ViewBrokerStatsDataRequestHeader.class); + final ViewBrokerStatsDataRequestHeader requestHeader = + (ViewBrokerStatsDataRequestHeader) request.decodeCommandCustomHeader(ViewBrokerStatsDataRequestHeader.class); final RemotingCommand response = RemotingCommand.createResponseCommand(null); MessageStore messageStore = this.brokerController.getMessageStore(); @@ -2056,14 +2138,17 @@ private RemotingCommand ViewBrokerStatsData(ChannelHandlerContext ctx, return response; } - private RemotingCommand fetchAllConsumeStatsInBroker(ChannelHandlerContext ctx, - RemotingCommand request) throws RemotingCommandException { + private RemotingCommand fetchAllConsumeStatsInBroker(ChannelHandlerContext ctx, RemotingCommand request) + throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); - GetConsumeStatsInBrokerHeader requestHeader = (GetConsumeStatsInBrokerHeader) request.decodeCommandCustomHeader(GetConsumeStatsInBrokerHeader.class); + GetConsumeStatsInBrokerHeader requestHeader = + (GetConsumeStatsInBrokerHeader) request.decodeCommandCustomHeader(GetConsumeStatsInBrokerHeader.class); boolean isOrder = requestHeader.isOrder(); - ConcurrentMap subscriptionGroups = brokerController.getSubscriptionGroupManager().getSubscriptionGroupTable(); + ConcurrentMap subscriptionGroups = + brokerController.getSubscriptionGroupManager().getSubscriptionGroupTable(); - List>> brokerConsumeStatsList = new ArrayList<>(); + List>> brokerConsumeStatsList = + new ArrayList<>(); long totalDiff = 0L; long totalInflightDiff = 0L; @@ -2075,7 +2160,9 @@ private RemotingCommand fetchAllConsumeStatsInBroker(ChannelHandlerContext ctx, ConsumeStats consumeStats = new ConsumeStats(); TopicConfig topicConfig = this.brokerController.getTopicConfigManager().selectTopicConfig(topic); if (null == topicConfig) { - LOGGER.warn("AdminBrokerProcessor#fetchAllConsumeStatsInBroker: topic config does not exist, topic={}", topic); + LOGGER.warn( + "AdminBrokerProcessor#fetchAllConsumeStatsInBroker: topic config does not exist, topic={}", + topic); continue; } @@ -2086,8 +2173,11 @@ private RemotingCommand fetchAllConsumeStatsInBroker(ChannelHandlerContext ctx, { SubscriptionData findSubscriptionData = this.brokerController.getConsumerManager().findSubscriptionData(group, topic); - if (null == findSubscriptionData && this.brokerController.getConsumerManager().findSubscriptionDataCount(group) > 0) { - LOGGER.warn("AdminBrokerProcessor#fetchAllConsumeStatsInBroker: topic does not exist in consumer " + "group's subscription, topic={}, consumer group={}", topic, group); + if (null == findSubscriptionData + && this.brokerController.getConsumerManager().findSubscriptionDataCount(group) > 0) { + LOGGER.warn( + "AdminBrokerProcessor#fetchAllConsumeStatsInBroker: topic does not exist in consumer " + + "group's subscription, topic={}, consumer group={}", topic, group); continue; } } @@ -2102,7 +2192,10 @@ private RemotingCommand fetchAllConsumeStatsInBroker(ChannelHandlerContext ctx, if (brokerOffset < 0) { brokerOffset = 0; } - long consumerOffset = this.brokerController.getConsumerOffsetManager().queryOffset(group, topic, i); + long consumerOffset = this.brokerController.getConsumerOffsetManager().queryOffset( + group, + topic, + i); if (consumerOffset < 0) consumerOffset = 0; @@ -2153,11 +2246,13 @@ private HashMap prepareRuntimeInfo() { runtimeInfo.put("brokerVersionDesc", MQVersion.getVersionDesc(MQVersion.CURRENT_VERSION)); runtimeInfo.put("brokerVersion", String.valueOf(MQVersion.CURRENT_VERSION)); - runtimeInfo.put("msgPutTotalYesterdayMorning", String.valueOf(this.brokerController.getBrokerStats().getMsgPutTotalYesterdayMorning())); + runtimeInfo.put("msgPutTotalYesterdayMorning", + String.valueOf(this.brokerController.getBrokerStats().getMsgPutTotalYesterdayMorning())); runtimeInfo.put("msgPutTotalTodayMorning", String.valueOf(this.brokerController.getBrokerStats().getMsgPutTotalTodayMorning())); runtimeInfo.put("msgPutTotalTodayNow", String.valueOf(this.brokerController.getBrokerStats().getMsgPutTotalTodayNow())); - runtimeInfo.put("msgGetTotalYesterdayMorning", String.valueOf(this.brokerController.getBrokerStats().getMsgGetTotalYesterdayMorning())); + runtimeInfo.put("msgGetTotalYesterdayMorning", + String.valueOf(this.brokerController.getBrokerStats().getMsgGetTotalYesterdayMorning())); runtimeInfo.put("msgGetTotalTodayMorning", String.valueOf(this.brokerController.getBrokerStats().getMsgGetTotalTodayMorning())); runtimeInfo.put("msgGetTotalTodayNow", String.valueOf(this.brokerController.getBrokerStats().getMsgGetTotalTodayNow())); @@ -2193,16 +2288,20 @@ private HashMap prepareRuntimeInfo() { } runtimeInfo.put("sendThreadPoolQueueSize", String.valueOf(this.brokerController.getSendThreadPoolQueue().size())); - runtimeInfo.put("sendThreadPoolQueueCapacity", String.valueOf(this.brokerController.getBrokerConfig().getSendThreadPoolQueueCapacity())); + runtimeInfo.put("sendThreadPoolQueueCapacity", + String.valueOf(this.brokerController.getBrokerConfig().getSendThreadPoolQueueCapacity())); runtimeInfo.put("pullThreadPoolQueueSize", String.valueOf(this.brokerController.getPullThreadPoolQueue().size())); - runtimeInfo.put("pullThreadPoolQueueCapacity", String.valueOf(this.brokerController.getBrokerConfig().getPullThreadPoolQueueCapacity())); + runtimeInfo.put("pullThreadPoolQueueCapacity", + String.valueOf(this.brokerController.getBrokerConfig().getPullThreadPoolQueueCapacity())); runtimeInfo.put("litePullThreadPoolQueueSize", String.valueOf(brokerController.getLitePullThreadPoolQueue().size())); - runtimeInfo.put("litePullThreadPoolQueueCapacity", String.valueOf(this.brokerController.getBrokerConfig().getLitePullThreadPoolQueueCapacity())); + runtimeInfo.put("litePullThreadPoolQueueCapacity", + String.valueOf(this.brokerController.getBrokerConfig().getLitePullThreadPoolQueueCapacity())); runtimeInfo.put("queryThreadPoolQueueSize", String.valueOf(this.brokerController.getQueryThreadPoolQueue().size())); - runtimeInfo.put("queryThreadPoolQueueCapacity", String.valueOf(this.brokerController.getBrokerConfig().getQueryThreadPoolQueueCapacity())); + runtimeInfo.put("queryThreadPoolQueueCapacity", + String.valueOf(this.brokerController.getBrokerConfig().getQueryThreadPoolQueueCapacity())); runtimeInfo.put("sendThreadPoolQueueHeadWaitTimeMills", String.valueOf(this.brokerController.headSlowTimeMills4SendThreadPoolQueue())); runtimeInfo.put("pullThreadPoolQueueHeadWaitTimeMills", String.valueOf(brokerController.headSlowTimeMills4PullThreadPoolQueue())); @@ -2210,13 +2309,17 @@ private HashMap prepareRuntimeInfo() { runtimeInfo.put("litePullThreadPoolQueueHeadWaitTimeMills", String.valueOf(brokerController.headSlowTimeMills4LitePullThreadPoolQueue())); runtimeInfo.put("EndTransactionQueueSize", String.valueOf(this.brokerController.getEndTransactionThreadPoolQueue().size())); - runtimeInfo.put("EndTransactionThreadPoolQueueCapacity", String.valueOf(this.brokerController.getBrokerConfig().getEndTransactionPoolQueueCapacity())); + runtimeInfo.put("EndTransactionThreadPoolQueueCapacity", + String.valueOf(this.brokerController.getBrokerConfig().getEndTransactionPoolQueueCapacity())); return runtimeInfo; } - private RemotingCommand callConsumer(final int requestCode, final RemotingCommand request, - final String consumerGroup, final String clientId) throws RemotingCommandException { + private RemotingCommand callConsumer( + final int requestCode, + final RemotingCommand request, + final String consumerGroup, + final String clientId) throws RemotingCommandException { final RemotingCommand response = RemotingCommand.createResponseCommand(null); ClientChannelInfo clientChannelInfo = this.brokerController.getConsumerManager().findChannel(consumerGroup, clientId); @@ -2228,7 +2331,9 @@ private RemotingCommand callConsumer(final int requestCode, final RemotingComman if (clientChannelInfo.getVersion() < MQVersion.Version.V3_1_8_SNAPSHOT.ordinal()) { response.setCode(ResponseCode.SYSTEM_ERROR); - response.setRemark(String.format("The Consumer <%s> Version <%s> too low to finish, please upgrade it to V3_1_8_SNAPSHOT", clientId, MQVersion.getVersionDesc(clientChannelInfo.getVersion()))); + response.setRemark(String.format("The Consumer <%s> Version <%s> too low to finish, please upgrade it to V3_1_8_SNAPSHOT", + clientId, + MQVersion.getVersionDesc(clientChannelInfo.getVersion()))); return response; } @@ -2240,22 +2345,26 @@ private RemotingCommand callConsumer(final int requestCode, final RemotingComman return this.brokerController.getBroker2Client().callClient(clientChannelInfo.getChannel(), newRequest); } catch (RemotingTimeoutException e) { response.setCode(ResponseCode.CONSUME_MSG_TIMEOUT); - response.setRemark(String.format("consumer <%s> <%s> Timeout: %s", consumerGroup, clientId, UtilAll.exceptionSimpleDesc(e))); + response + .setRemark(String.format("consumer <%s> <%s> Timeout: %s", consumerGroup, clientId, UtilAll.exceptionSimpleDesc(e))); return response; } catch (Exception e) { response.setCode(ResponseCode.SYSTEM_ERROR); - response.setRemark(String.format("invoke consumer <%s> <%s> Exception: %s", consumerGroup, clientId, UtilAll.exceptionSimpleDesc(e))); + response.setRemark( + String.format("invoke consumer <%s> <%s> Exception: %s", consumerGroup, clientId, UtilAll.exceptionSimpleDesc(e))); return response; } } private RemotingCommand queryConsumeQueue(ChannelHandlerContext ctx, RemotingCommand request) throws RemotingCommandException { - QueryConsumeQueueRequestHeader requestHeader = (QueryConsumeQueueRequestHeader) request.decodeCommandCustomHeader(QueryConsumeQueueRequestHeader.class); + QueryConsumeQueueRequestHeader requestHeader = + (QueryConsumeQueueRequestHeader) request.decodeCommandCustomHeader(QueryConsumeQueueRequestHeader.class); RemotingCommand response = RemotingCommand.createResponseCommand(null); - ConsumeQueueInterface consumeQueue = this.brokerController.getMessageStore().getConsumeQueue(requestHeader.getTopic(), requestHeader.getQueueId()); + ConsumeQueueInterface consumeQueue = this.brokerController.getMessageStore().getConsumeQueue(requestHeader.getTopic(), + requestHeader.getQueueId()); if (consumeQueue == null) { response.setCode(ResponseCode.SYSTEM_ERROR); response.setRemark(String.format("%d@%s is not exist!", requestHeader.getQueueId(), requestHeader.getTopic())); @@ -2269,15 +2378,19 @@ private RemotingCommand queryConsumeQueue(ChannelHandlerContext ctx, MessageFilter messageFilter = null; if (requestHeader.getConsumerGroup() != null) { - SubscriptionData subscriptionData = this.brokerController.getConsumerManager().findSubscriptionData(requestHeader.getConsumerGroup(), requestHeader.getTopic()); + SubscriptionData subscriptionData = this.brokerController.getConsumerManager().findSubscriptionData( + requestHeader.getConsumerGroup(), requestHeader.getTopic() + ); body.setSubscriptionData(subscriptionData); if (subscriptionData == null) { body.setFilterData(String.format("%s@%s is not online!", requestHeader.getConsumerGroup(), requestHeader.getTopic())); } else { - ConsumerFilterData filterData = this.brokerController.getConsumerFilterManager().get(requestHeader.getTopic(), requestHeader.getConsumerGroup()); + ConsumerFilterData filterData = this.brokerController.getConsumerFilterManager() + .get(requestHeader.getTopic(), requestHeader.getConsumerGroup()); body.setFilterData(JSON.toJSONString(filterData, true)); - messageFilter = new ExpressionMessageFilter(subscriptionData, filterData, this.brokerController.getConsumerFilterManager()); + messageFilter = new ExpressionMessageFilter(subscriptionData, filterData, + this.brokerController.getConsumerFilterManager()); } } @@ -2328,18 +2441,25 @@ private RemotingCommand queryConsumeQueue(ChannelHandlerContext ctx, } private RemotingCommand resumeCheckHalfMessage(ChannelHandlerContext ctx, - RemotingCommand request) throws RemotingCommandException { - final ResumeCheckHalfMessageRequestHeader requestHeader = (ResumeCheckHalfMessageRequestHeader) request.decodeCommandCustomHeader(ResumeCheckHalfMessageRequestHeader.class); + RemotingCommand request) + throws RemotingCommandException { + final ResumeCheckHalfMessageRequestHeader requestHeader = (ResumeCheckHalfMessageRequestHeader) request + .decodeCommandCustomHeader(ResumeCheckHalfMessageRequestHeader.class); final RemotingCommand response = RemotingCommand.createResponseCommand(null); SelectMappedBufferResult selectMappedBufferResult = null; try { MessageId messageId = MessageDecoder.decodeMessageId(requestHeader.getMsgId()); - selectMappedBufferResult = this.brokerController.getMessageStore().selectOneMessageByOffset(messageId.getOffset()); + selectMappedBufferResult = this.brokerController.getMessageStore() + .selectOneMessageByOffset(messageId.getOffset()); MessageExt msg = MessageDecoder.decode(selectMappedBufferResult.getByteBuffer()); msg.putUserProperty(MessageConst.PROPERTY_TRANSACTION_CHECK_TIMES, String.valueOf(0)); - PutMessageResult putMessageResult = this.brokerController.getMessageStore().putMessage(toMessageExtBrokerInner(msg)); - if (putMessageResult != null && putMessageResult.getPutMessageStatus() == PutMessageStatus.PUT_OK) { - LOGGER.info("Put message back to RMQ_SYS_TRANS_HALF_TOPIC. real topic={}", msg.getUserProperty(MessageConst.PROPERTY_REAL_TOPIC)); + PutMessageResult putMessageResult = this.brokerController.getMessageStore() + .putMessage(toMessageExtBrokerInner(msg)); + if (putMessageResult != null + && putMessageResult.getPutMessageStatus() == PutMessageStatus.PUT_OK) { + LOGGER.info( + "Put message back to RMQ_SYS_TRANS_HALF_TOPIC. real topic={}", + msg.getUserProperty(MessageConst.PROPERTY_REAL_TOPIC)); response.setCode(ResponseCode.SUCCESS); response.setRemark(null); } else { @@ -2419,7 +2539,9 @@ private RemotingCommand notifyMinBrokerIdChange(ChannelHandlerContext ctx, LOGGER.warn("min broker id changed, prev {}, new {}", this.brokerController.getMinBrokerIdInGroup(), requestHeader.getMinBrokerId()); - this.brokerController.updateMinBroker(requestHeader.getMinBrokerId(), requestHeader.getMinBrokerAddr(), requestHeader.getOfflineBrokerAddr(), requestHeader.getHaBrokerAddr()); + this.brokerController.updateMinBroker(requestHeader.getMinBrokerId(), requestHeader.getMinBrokerAddr(), + requestHeader.getOfflineBrokerAddr(), + requestHeader.getHaBrokerAddr()); response.setCode(ResponseCode.SUCCESS); response.setRemark(null); @@ -2435,7 +2557,8 @@ private RemotingCommand updateBrokerHaInfo(ChannelHandlerContext ctx, if (requestHeader.getMasterHaAddress() != null) { this.brokerController.getMessageStore().updateHaMasterAddress(requestHeader.getMasterHaAddress()); this.brokerController.getMessageStore().updateMasterAddress(requestHeader.getMasterAddress()); - if (this.brokerController.getMessageStore().getMasterFlushedOffset() == 0 && this.brokerController.getMessageStoreConfig().isSyncMasterFlushOffsetWhenStartup()) { + if (this.brokerController.getMessageStore().getMasterFlushedOffset() == 0 + && this.brokerController.getMessageStoreConfig().isSyncMasterFlushOffsetWhenStartup()) { LOGGER.info("Set master flush offset in slave to {}", requestHeader.getMasterFlushOffset()); this.brokerController.getMessageStore().setMasterFlushedOffset(requestHeader.getMasterFlushOffset()); } @@ -2474,7 +2597,8 @@ private RemotingCommand getBrokerEpochCache(ChannelHandlerContext ctx, RemotingC final ReplicasManager replicasManager = this.brokerController.getReplicasManager(); assert replicasManager != null; final BrokerConfig brokerConfig = this.brokerController.getBrokerConfig(); - final EpochEntryCache entryCache = new EpochEntryCache(brokerConfig.getBrokerClusterName(), brokerConfig.getBrokerName(), brokerConfig.getBrokerId(), replicasManager.getEpochEntries(), this.brokerController.getMessageStore().getMaxPhyOffset()); + final EpochEntryCache entryCache = new EpochEntryCache(brokerConfig.getBrokerClusterName(), + brokerConfig.getBrokerName(), brokerConfig.getBrokerId(), replicasManager.getEpochEntries(), this.brokerController.getMessageStore().getMaxPhyOffset()); final RemotingCommand response = RemotingCommand.createResponseCommand(null); response.setBody(entryCache.encode()); @@ -2522,7 +2646,8 @@ private RemotingCommand notifyBrokerRoleChanged(ChannelHandlerContext ctx, private boolean validateSlave(RemotingCommand response) { if (this.brokerController.getMessageStoreConfig().getBrokerRole().equals(BrokerRole.SLAVE)) { response.setCode(ResponseCode.SYSTEM_ERROR); - response.setRemark("Can't modify topic or subscription group from slave broker, " + "please execute it from master broker."); + response.setRemark("Can't modify topic or subscription group from slave broker, " + + "please execute it from master broker."); return true; } return false; From 5e8ccd1ec07670558d52ae5541f3de5d476705bf Mon Sep 17 00:00:00 2001 From: RongtongJin Date: Sat, 17 Dec 2022 17:23:01 +0800 Subject: [PATCH 03/15] Remove the useless import --- .../main/java/org/apache/rocketmq/store/TransientStorePool.java | 1 - 1 file changed, 1 deletion(-) diff --git a/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java b/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java index c70a7bac74b..b8baee2a2f3 100644 --- a/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java +++ b/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java @@ -24,7 +24,6 @@ import org.apache.rocketmq.common.constant.LoggerName; import org.apache.rocketmq.logging.org.slf4j.Logger; import org.apache.rocketmq.logging.org.slf4j.LoggerFactory; -import org.apache.rocketmq.store.config.MessageStoreConfig; import org.apache.rocketmq.store.util.LibC; import sun.nio.ch.DirectBuffer; From 7bb19db71d7c56d5bc0d18b7513406d03b9714bc Mon Sep 17 00:00:00 2001 From: RongtongJin Date: Mon, 19 Dec 2022 15:30:06 +0800 Subject: [PATCH 04/15] Fix the HA transmission disconnection issue when transientStorePoolEnable is true --- .../java/org/apache/rocketmq/store/TransientStorePool.java | 4 ++++ .../apache/rocketmq/store/logfile/DefaultMappedFile.java | 6 +++++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java b/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java index b8baee2a2f3..1a49b689edb 100644 --- a/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java +++ b/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java @@ -85,4 +85,8 @@ public int availableBufferNums() { } return Integer.MAX_VALUE; } + + public boolean enableControllerMode() { + return messageStore.getBrokerConfig().isEnableControllerMode(); + } } diff --git a/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java b/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java index 76ba89eba57..3bedf263d0b 100644 --- a/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java +++ b/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java @@ -555,7 +555,11 @@ public void setWrotePosition(int pos) { */ @Override public int getReadPosition() { - return this.writeBuffer == null ? WROTE_POSITION_UPDATER.get(this) : COMMITTED_POSITION_UPDATER.get(this); + if (this.transientStorePool == null || transientStorePool.enableControllerMode()) { + return WROTE_POSITION_UPDATER.get(this); + } else { + return COMMITTED_POSITION_UPDATER.get(this); + } } @Override From df745403c9b933531fa428951381bdd066f8a27a Mon Sep 17 00:00:00 2001 From: RongtongJin Date: Tue, 20 Dec 2022 16:48:01 +0800 Subject: [PATCH 05/15] just test --- .../org/apache/rocketmq/store/DefaultMessageStore.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java b/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java index 4a4c96ddcfc..8284bf60faf 100644 --- a/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java +++ b/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java @@ -2555,6 +2555,13 @@ private void doReput() { DefaultMessageStore.this.commitLog.checkMessageAndReturnSize(result.getByteBuffer(), false, false, false); int size = dispatchRequest.getBufferSize() == -1 ? dispatchRequest.getMsgSize() : dispatchRequest.getBufferSize(); + if (!dispatchRequest.isSuccess()) { + LOGGER.warn("reputFromOffset = " + reputFromOffset); + LOGGER.warn("confirmOffset = " + DefaultMessageStore.this.getConfirmOffset()); + LOGGER.warn("maxOffset = " + DefaultMessageStore.this.getMaxPhyOffset()); + LOGGER.warn("" + dispatchRequest.toString()); + } + if (reputFromOffset + size > DefaultMessageStore.this.getConfirmOffset()) { doNext = false; break; From 8f1b067cfb066794755a6624c72578058d52c7ea Mon Sep 17 00:00:00 2001 From: RongtongJin Date: Wed, 21 Dec 2022 10:27:15 +0800 Subject: [PATCH 06/15] just test --- .../org/apache/rocketmq/store/CommitLog.java | 10 ++++ .../rocketmq/store/DefaultMessageStore.java | 4 ++ .../ha/autoswitch/AutoSwitchHAClient.java | 2 +- .../store/logfile/DefaultMappedFile.java | 51 ++++++++++--------- 4 files changed, 41 insertions(+), 26 deletions(-) diff --git a/store/src/main/java/org/apache/rocketmq/store/CommitLog.java b/store/src/main/java/org/apache/rocketmq/store/CommitLog.java index d7c40cc5057..36de60926ce 100644 --- a/store/src/main/java/org/apache/rocketmq/store/CommitLog.java +++ b/store/src/main/java/org/apache/rocketmq/store/CommitLog.java @@ -167,6 +167,10 @@ public long getMaxOffset() { return this.mappedFileQueue.getMaxOffset(); } + public long getMaxWroteOffset() { + return this.mappedFileQueue.getMaxWrotePosition(); + } + public long remainHowManyDataToCommit() { return this.mappedFileQueue.remainHowManyDataToCommit(); } @@ -1266,6 +1270,12 @@ public String getServiceName() { public void run() { CommitLog.log.info(this.getServiceName() + " service started"); while (!this.isStopped()) { + + if (defaultMessageStore.getMessageStoreConfig().getBrokerRole() == BrokerRole.SLAVE) { + waitForRunning(5 * 1000); + continue; + } + int interval = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getCommitIntervalCommitLog(); int commitDataLeastPages = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getCommitCommitLogLeastPages(); diff --git a/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java b/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java index 8284bf60faf..e28a8dd94d6 100644 --- a/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java +++ b/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java @@ -1067,6 +1067,10 @@ public long getMaxPhyOffset() { return this.commitLog.getMaxOffset(); } + public long getMaxWrotePhyOffset() { + return this.commitLog.getMaxWroteOffset(); + } + @Override public long getMinPhyOffset() { return this.commitLog.getMinOffset(); diff --git a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClient.java b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClient.java index 2c3ab85f7af..24aaff872eb 100644 --- a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClient.java +++ b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClient.java @@ -549,7 +549,7 @@ protected boolean processReadResult(ByteBuffer byteBufferRead) { byteBufferRead.get(bodyData); byteBufferRead.position(readSocketPos); AutoSwitchHAClient.this.processPosition += AutoSwitchHAConnection.TRANSFER_HEADER_SIZE + bodySize; - long slavePhyOffset = AutoSwitchHAClient.this.messageStore.getMaxPhyOffset(); + long slavePhyOffset = AutoSwitchHAClient.this.messageStore.getMaxWrotePhyOffset(); if (slavePhyOffset != 0) { if (slavePhyOffset != masterOffset) { LOGGER.error("master pushed offset not equal the max phy offset in slave, SLAVE: " diff --git a/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java b/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java index 3bedf263d0b..a582a7d795d 100644 --- a/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java +++ b/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java @@ -102,7 +102,7 @@ public DefaultMappedFile(final String fileName, final int fileSize) throws IOExc } public DefaultMappedFile(final String fileName, final int fileSize, - final TransientStorePool transientStorePool) throws IOException { + final TransientStorePool transientStorePool) throws IOException { init(fileName, fileSize, transientStorePool); } @@ -116,7 +116,7 @@ public static long getTotalMappedVirtualMemory() { @Override public void init(final String fileName, final int fileSize, - final TransientStorePool transientStorePool) throws IOException { + final TransientStorePool transientStorePool) throws IOException { init(fileName, fileSize); this.writeBuffer = transientStorePool.borrowBuffer(); this.transientStorePool = transientStorePool; @@ -186,11 +186,11 @@ public boolean getData(int pos, int size, ByteBuffer byteBuffer) { } } else { log.debug("matched, but hold failed, request pos: " + pos + ", fileFromOffset: " - + this.fileFromOffset); + + this.fileFromOffset); } } else { log.warn("selectMappedBuffer request pos invalid, request pos: " + pos + ", size: " + size - + ", fileFromOffset: " + this.fileFromOffset); + + ", fileFromOffset: " + this.fileFromOffset); } return false; @@ -225,18 +225,18 @@ public AppendMessageResult appendMessage(final ByteBuffer byteBufferMsg, final C @Override public AppendMessageResult appendMessage(final MessageExtBrokerInner msg, final AppendMessageCallback cb, - PutMessageContext putMessageContext) { + PutMessageContext putMessageContext) { return appendMessagesInner(msg, cb, putMessageContext); } @Override public AppendMessageResult appendMessages(final MessageExtBatch messageExtBatch, final AppendMessageCallback cb, - PutMessageContext putMessageContext) { + PutMessageContext putMessageContext) { return appendMessagesInner(messageExtBatch, cb, putMessageContext); } public AppendMessageResult appendMessagesInner(final MessageExt messageExt, final AppendMessageCallback cb, - PutMessageContext putMessageContext) { + PutMessageContext putMessageContext) { assert messageExt != null; assert cb != null; @@ -249,11 +249,11 @@ public AppendMessageResult appendMessagesInner(final MessageExt messageExt, fina if (messageExt instanceof MessageExtBatch && !((MessageExtBatch) messageExt).isInnerBatch()) { // traditional batch message result = cb.doAppend(this.getFileFromOffset(), byteBuffer, this.fileSize - currentPos, - (MessageExtBatch) messageExt, putMessageContext); + (MessageExtBatch) messageExt, putMessageContext); } else if (messageExt instanceof MessageExtBrokerInner) { // traditional single message or newly introduced inner-batch message result = cb.doAppend(this.getFileFromOffset(), byteBuffer, this.fileSize - currentPos, - (MessageExtBrokerInner) messageExt, putMessageContext); + (MessageExtBrokerInner) messageExt, putMessageContext); } else { return new AppendMessageResult(AppendMessageStatus.UNKNOWN_ERROR); } @@ -459,11 +459,11 @@ public SelectMappedBufferResult selectMappedBuffer(int pos, int size) { return new SelectMappedBufferResult(this.fileFromOffset + pos, byteBufferNew, size, this); } else { log.warn("matched, but hold failed, request pos: " + pos + ", fileFromOffset: " - + this.fileFromOffset); + + this.fileFromOffset); } } else { log.warn("selectMappedBuffer request pos invalid, request pos: " + pos + ", size: " + size - + ", fileFromOffset: " + this.fileFromOffset); + + ", fileFromOffset: " + this.fileFromOffset); } return null; @@ -491,13 +491,13 @@ public SelectMappedBufferResult selectMappedBuffer(int pos) { public boolean cleanup(final long currentRef) { if (this.isAvailable()) { log.error("this file[REF:" + currentRef + "] " + this.fileName - + " have not shutdown, stop unmapping."); + + " have not shutdown, stop unmapping."); return false; } if (this.isCleanupOver()) { log.error("this file[REF:" + currentRef + "] " + this.fileName - + " have cleanup, do not do it again."); + + " have cleanup, do not do it again."); return true; } @@ -523,10 +523,10 @@ public boolean destroy(final long intervalForcibly) { long beginTime = System.currentTimeMillis(); boolean result = this.file.delete(); log.info("delete file[REF:" + this.getRefCount() + "] " + this.fileName - + (result ? " OK, " : " Failed, ") + "W:" + this.getWrotePosition() + " M:" - + this.getFlushedPosition() + ", " - + UtilAll.computeElapsedTimeMilliseconds(beginTime) - + "," + (System.currentTimeMillis() - lastModified)); + + (result ? " OK, " : " Failed, ") + "W:" + this.getWrotePosition() + " M:" + + this.getFlushedPosition() + ", " + + UtilAll.computeElapsedTimeMilliseconds(beginTime) + + "," + (System.currentTimeMillis() - lastModified)); } catch (Exception e) { log.warn("close file channel " + this.fileName + " Failed. ", e); } @@ -534,7 +534,7 @@ public boolean destroy(final long intervalForcibly) { return true; } else { log.warn("destroy mapped file[REF:" + this.getRefCount() + "] " + this.fileName - + " Failed. cleanupOver: " + this.cleanupOver); + + " Failed. cleanupOver: " + this.cleanupOver); } return false; @@ -555,11 +555,12 @@ public void setWrotePosition(int pos) { */ @Override public int getReadPosition() { - if (this.transientStorePool == null || transientStorePool.enableControllerMode()) { - return WROTE_POSITION_UPDATER.get(this); - } else { - return COMMITTED_POSITION_UPDATER.get(this); - } +// if (this.transientStorePool == null || transientStorePool.enableControllerMode()) { +// return WROTE_POSITION_UPDATER.get(this); +// } else { +// return COMMITTED_POSITION_UPDATER.get(this); +// } + return writeBuffer == null ? WROTE_POSITION_UPDATER.get(this) : COMMITTED_POSITION_UPDATER.get(this); } @Override @@ -600,11 +601,11 @@ public void warmMappedFile(FlushDiskType type, int pages) { // force flush when prepare load finished if (type == FlushDiskType.SYNC_FLUSH) { log.info("mapped file warm-up done, force to disk, mappedFile={}, costTime={}", - this.getFileName(), System.currentTimeMillis() - beginTime); + this.getFileName(), System.currentTimeMillis() - beginTime); mappedByteBuffer.force(); } log.info("mapped file warm-up done. mappedFile={}, costTime={}", this.getFileName(), - System.currentTimeMillis() - beginTime); + System.currentTimeMillis() - beginTime); this.mlock(); } From eab40266737d156dbdc6d502b5c6db7e82dbe8ad Mon Sep 17 00:00:00 2001 From: RongtongJin Date: Wed, 21 Dec 2022 11:00:51 +0800 Subject: [PATCH 07/15] just test --- .../apache/rocketmq/store/DefaultMessageStore.java | 4 ++-- .../store/ha/autoswitch/AutoSwitchHAClient.java | 8 ++++---- .../ha/autoswitch/AutoSwitchHAConnection.java | 6 +++--- .../store/ha/autoswitch/AutoSwitchHAService.java | 14 +++++++------- 4 files changed, 16 insertions(+), 16 deletions(-) diff --git a/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java b/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java index e28a8dd94d6..6684261a0a1 100644 --- a/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java +++ b/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java @@ -655,7 +655,7 @@ public CommitLog getCommitLog() { } public void truncateDirtyFiles(long offsetToTruncate) { - if (offsetToTruncate >= this.getMaxPhyOffset()) { + if (offsetToTruncate >= this.getMaxWrotePhyOffset()) { return; } @@ -678,7 +678,7 @@ public void truncateDirtyFiles(long offsetToTruncate) { @Override public boolean truncateFiles(long offsetToTruncate) { - if (offsetToTruncate >= this.getMaxPhyOffset()) { + if (offsetToTruncate >= this.getMaxWrotePhyOffset()) { return true; } diff --git a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClient.java b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClient.java index 24aaff872eb..75cc4aeb3c4 100644 --- a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClient.java +++ b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClient.java @@ -343,7 +343,7 @@ private boolean reportSlaveOffset(final long offsetToReport) throws IOException private boolean reportSlaveMaxOffset() throws IOException { boolean result = true; - final long maxPhyOffset = this.messageStore.getMaxPhyOffset(); + final long maxPhyOffset = this.messageStore.getMaxWrotePhyOffset(); if (maxPhyOffset > this.currentReportedOffset) { this.currentReportedOffset = maxPhyOffset; result = reportSlaveOffset(this.currentReportedOffset); @@ -363,7 +363,7 @@ public boolean connectMaster() throws IOException { changeCurrentState(HAConnectionState.HANDSHAKE); } } - this.currentReportedOffset = this.messageStore.getMaxPhyOffset(); + this.currentReportedOffset = this.messageStore.getMaxWrotePhyOffset(); this.lastReadTimestamp = System.currentTimeMillis(); } return this.socketChannel != null; @@ -455,7 +455,7 @@ private boolean doTruncate(List masterEpochEntries, long masterEndOf final List localEpochEntries = this.epochCache.getAllEntries(); final EpochFileCache localEpochCache = new EpochFileCache(); localEpochCache.initCacheFromEntries(localEpochEntries); - localEpochCache.setLastEpochEntryEndOffset(this.messageStore.getMaxPhyOffset()); + localEpochCache.setLastEpochEntryEndOffset(this.messageStore.getMaxWrotePhyOffset()); final long truncateOffset = localEpochCache.findConsistentPoint(masterEpochCache); if (truncateOffset < 0) { @@ -568,7 +568,7 @@ protected boolean processReadResult(ByteBuffer byteBufferRead) { AutoSwitchHAClient.this.messageStore.appendToCommitLog(masterOffset, bodyData, 0, bodyData.length); } - haService.updateConfirmOffset(Math.min(confirmOffset, messageStore.getMaxPhyOffset())); + haService.updateConfirmOffset(Math.min(confirmOffset, messageStore.getMaxWrotePhyOffset())); if (!reportSlaveMaxOffset()) { LOGGER.error("AutoSwitchHAClient report max offset to master failed"); diff --git a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAConnection.java b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAConnection.java index 7401574e5e0..309a6e5f39f 100644 --- a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAConnection.java +++ b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAConnection.java @@ -213,13 +213,13 @@ public boolean isSyncFromLastFile() { } private synchronized void updateLastTransferInfo() { - this.lastMasterMaxOffset = this.haService.getDefaultMessageStore().getMaxPhyOffset(); + this.lastMasterMaxOffset = this.haService.getDefaultMessageStore().getMaxWrotePhyOffset(); this.lastTransferTimeMs = System.currentTimeMillis(); } private synchronized void maybeExpandInSyncStateSet(long slaveMaxOffset) { if (!this.isAsyncLearner && slaveMaxOffset >= this.lastMasterMaxOffset) { - long caughtUpTimeMs = this.haService.getDefaultMessageStore().getMaxPhyOffset() == slaveMaxOffset ? System.currentTimeMillis() : this.lastTransferTimeMs; + long caughtUpTimeMs = this.haService.getDefaultMessageStore().getMaxWrotePhyOffset() == slaveMaxOffset ? System.currentTimeMillis() : this.lastTransferTimeMs; this.haService.updateConnectionLastCaughtUpTime(this.slaveAddress, caughtUpTimeMs); this.haService.maybeExpandInSyncStateSet(this.slaveAddress, slaveMaxOffset); } @@ -488,7 +488,7 @@ public long getNextTransferFromWhere() { private boolean buildHandshakeBuffer() { final List epochEntries = AutoSwitchHAConnection.this.epochCache.getAllEntries(); final int lastEpoch = AutoSwitchHAConnection.this.epochCache.lastEpoch(); - final long maxPhyOffset = AutoSwitchHAConnection.this.haService.getDefaultMessageStore().getMaxPhyOffset(); + final long maxPhyOffset = AutoSwitchHAConnection.this.haService.getDefaultMessageStore().getMaxWrotePhyOffset(); this.byteBufferHeader.position(0); this.byteBufferHeader.limit(HANDSHAKE_HEADER_SIZE); // State diff --git a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java index c4a9aeb812a..a092a6094b5 100644 --- a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java +++ b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java @@ -121,7 +121,7 @@ public boolean changeToMaster(int masterEpoch) { } // Append new epoch to epochFile - final EpochEntry newEpochEntry = new EpochEntry(masterEpoch, this.defaultMessageStore.getMaxPhyOffset()); + final EpochEntry newEpochEntry = new EpochEntry(masterEpoch, this.defaultMessageStore.getMaxWrotePhyOffset()); if (this.epochCache.lastEpoch() >= masterEpoch) { this.epochCache.truncateSuffixByEpoch(masterEpoch); } @@ -136,7 +136,7 @@ public boolean changeToMaster(int masterEpoch) { } } - LOGGER.info("TruncateOffset is {}, confirmOffset is {}, maxPhyOffset is {}", truncateOffset, getConfirmOffset(), this.defaultMessageStore.getMaxPhyOffset()); + LOGGER.info("TruncateOffset is {}, confirmOffset is {}, maxPhyOffset is {}", truncateOffset, getConfirmOffset(), this.defaultMessageStore.getMaxWrotePhyOffset()); this.defaultMessageStore.recoverTopicQueueTable(); LOGGER.info("Change ha to master success, newMasterEpoch:{}, startOffset:{}", masterEpoch, newEpochEntry.getStartOffset()); @@ -248,7 +248,7 @@ public void updateConnectionLastCaughtUpTime(final String slaveAddress, final lo public long getConfirmOffset() { if (this.defaultMessageStore.getMessageStoreConfig().getBrokerRole() != BrokerRole.SLAVE) { if (this.syncStateSet.size() == 1) { - return this.defaultMessageStore.getMaxPhyOffset(); + return this.defaultMessageStore.getMaxWrotePhyOffset(); } // First time compute confirmOffset. if (this.confirmOffset <= 0) { @@ -277,7 +277,7 @@ public HARuntimeInfo getRuntimeInfo(long masterPutWhere) { info.setMaster(false); info.getHaClientRuntimeInfo().setMasterAddr(this.haClient.getHaMasterAddress()); - info.getHaClientRuntimeInfo().setMaxOffset(this.getDefaultMessageStore().getMaxPhyOffset()); + info.getHaClientRuntimeInfo().setMaxOffset(this.getDefaultMessageStore().getMaxWrotePhyOffset()); info.getHaClientRuntimeInfo().setLastReadTimestamp(this.haClient.getLastReadTimestamp()); info.getHaClientRuntimeInfo().setLastWriteTimestamp(this.haClient.getLastWriteTimestamp()); info.getHaClientRuntimeInfo().setTransferredByteInSecond(this.haClient.getTransferredByteInSecond()); @@ -312,7 +312,7 @@ public void updateConfirmOffset(long confirmOffset) { private long computeConfirmOffset() { final Set currentSyncStateSet = getSyncStateSet(); - long confirmOffset = this.defaultMessageStore.getMaxPhyOffset(); + long confirmOffset = this.defaultMessageStore.getMaxWrotePhyOffset(); for (HAConnection connection : this.connectionList) { final String slaveAddress = ((AutoSwitchHAConnection) connection).getSlaveAddress(); if (currentSyncStateSet.contains(slaveAddress)) { @@ -355,7 +355,7 @@ public long truncateInvalidMsg() { } boolean doNext = true; - long reputFromOffset = this.defaultMessageStore.getMaxPhyOffset() - dispatchBehind; + long reputFromOffset = this.defaultMessageStore.getMaxWrotePhyOffset() - dispatchBehind; do { SelectMappedBufferResult result = this.defaultMessageStore.getCommitLog().getData(reputFromOffset); if (result == null) { @@ -385,7 +385,7 @@ public long truncateInvalidMsg() { } finally { result.release(); } - } while (reputFromOffset < this.defaultMessageStore.getMaxPhyOffset() && doNext); + } while (reputFromOffset < this.defaultMessageStore.getMaxWrotePhyOffset() && doNext); LOGGER.info("Truncate commitLog to {}", reputFromOffset); this.defaultMessageStore.truncateDirtyFiles(reputFromOffset); From 06e0562de47fc68ddbc8c47367ed15db47ab8fb3 Mon Sep 17 00:00:00 2001 From: RongtongJin Date: Wed, 21 Dec 2022 11:41:49 +0800 Subject: [PATCH 08/15] just test --- .../apache/rocketmq/store/DefaultMessageStore.java | 13 +++++++------ .../store/ha/autoswitch/AutoSwitchHAClient.java | 10 +++++----- .../ha/autoswitch/AutoSwitchHAConnection.java | 6 +++--- .../store/ha/autoswitch/AutoSwitchHAService.java | 14 +++++++------- 4 files changed, 22 insertions(+), 21 deletions(-) diff --git a/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java b/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java index 6684261a0a1..40ae89dfe26 100644 --- a/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java +++ b/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java @@ -655,7 +655,7 @@ public CommitLog getCommitLog() { } public void truncateDirtyFiles(long offsetToTruncate) { - if (offsetToTruncate >= this.getMaxWrotePhyOffset()) { + if (offsetToTruncate >= this.getMaxPhyOffset()) { return; } @@ -678,7 +678,7 @@ public void truncateDirtyFiles(long offsetToTruncate) { @Override public boolean truncateFiles(long offsetToTruncate) { - if (offsetToTruncate >= this.getMaxWrotePhyOffset()) { + if (offsetToTruncate >= this.getMaxPhyOffset()) { return true; } @@ -1064,12 +1064,13 @@ public HashMap getRuntimeInfo() { @Override public long getMaxPhyOffset() { - return this.commitLog.getMaxOffset(); + if (messageStoreConfig.isTransientStorePoolEnable() && messageStoreConfig.getBrokerRole() == BrokerRole.SLAVE) { + return this.commitLog.getMaxWroteOffset(); + } else { + return this.commitLog.getMaxOffset(); + } } - public long getMaxWrotePhyOffset() { - return this.commitLog.getMaxWroteOffset(); - } @Override public long getMinPhyOffset() { diff --git a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClient.java b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClient.java index 75cc4aeb3c4..2c3ab85f7af 100644 --- a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClient.java +++ b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAClient.java @@ -343,7 +343,7 @@ private boolean reportSlaveOffset(final long offsetToReport) throws IOException private boolean reportSlaveMaxOffset() throws IOException { boolean result = true; - final long maxPhyOffset = this.messageStore.getMaxWrotePhyOffset(); + final long maxPhyOffset = this.messageStore.getMaxPhyOffset(); if (maxPhyOffset > this.currentReportedOffset) { this.currentReportedOffset = maxPhyOffset; result = reportSlaveOffset(this.currentReportedOffset); @@ -363,7 +363,7 @@ public boolean connectMaster() throws IOException { changeCurrentState(HAConnectionState.HANDSHAKE); } } - this.currentReportedOffset = this.messageStore.getMaxWrotePhyOffset(); + this.currentReportedOffset = this.messageStore.getMaxPhyOffset(); this.lastReadTimestamp = System.currentTimeMillis(); } return this.socketChannel != null; @@ -455,7 +455,7 @@ private boolean doTruncate(List masterEpochEntries, long masterEndOf final List localEpochEntries = this.epochCache.getAllEntries(); final EpochFileCache localEpochCache = new EpochFileCache(); localEpochCache.initCacheFromEntries(localEpochEntries); - localEpochCache.setLastEpochEntryEndOffset(this.messageStore.getMaxWrotePhyOffset()); + localEpochCache.setLastEpochEntryEndOffset(this.messageStore.getMaxPhyOffset()); final long truncateOffset = localEpochCache.findConsistentPoint(masterEpochCache); if (truncateOffset < 0) { @@ -549,7 +549,7 @@ protected boolean processReadResult(ByteBuffer byteBufferRead) { byteBufferRead.get(bodyData); byteBufferRead.position(readSocketPos); AutoSwitchHAClient.this.processPosition += AutoSwitchHAConnection.TRANSFER_HEADER_SIZE + bodySize; - long slavePhyOffset = AutoSwitchHAClient.this.messageStore.getMaxWrotePhyOffset(); + long slavePhyOffset = AutoSwitchHAClient.this.messageStore.getMaxPhyOffset(); if (slavePhyOffset != 0) { if (slavePhyOffset != masterOffset) { LOGGER.error("master pushed offset not equal the max phy offset in slave, SLAVE: " @@ -568,7 +568,7 @@ protected boolean processReadResult(ByteBuffer byteBufferRead) { AutoSwitchHAClient.this.messageStore.appendToCommitLog(masterOffset, bodyData, 0, bodyData.length); } - haService.updateConfirmOffset(Math.min(confirmOffset, messageStore.getMaxWrotePhyOffset())); + haService.updateConfirmOffset(Math.min(confirmOffset, messageStore.getMaxPhyOffset())); if (!reportSlaveMaxOffset()) { LOGGER.error("AutoSwitchHAClient report max offset to master failed"); diff --git a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAConnection.java b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAConnection.java index 309a6e5f39f..7401574e5e0 100644 --- a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAConnection.java +++ b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAConnection.java @@ -213,13 +213,13 @@ public boolean isSyncFromLastFile() { } private synchronized void updateLastTransferInfo() { - this.lastMasterMaxOffset = this.haService.getDefaultMessageStore().getMaxWrotePhyOffset(); + this.lastMasterMaxOffset = this.haService.getDefaultMessageStore().getMaxPhyOffset(); this.lastTransferTimeMs = System.currentTimeMillis(); } private synchronized void maybeExpandInSyncStateSet(long slaveMaxOffset) { if (!this.isAsyncLearner && slaveMaxOffset >= this.lastMasterMaxOffset) { - long caughtUpTimeMs = this.haService.getDefaultMessageStore().getMaxWrotePhyOffset() == slaveMaxOffset ? System.currentTimeMillis() : this.lastTransferTimeMs; + long caughtUpTimeMs = this.haService.getDefaultMessageStore().getMaxPhyOffset() == slaveMaxOffset ? System.currentTimeMillis() : this.lastTransferTimeMs; this.haService.updateConnectionLastCaughtUpTime(this.slaveAddress, caughtUpTimeMs); this.haService.maybeExpandInSyncStateSet(this.slaveAddress, slaveMaxOffset); } @@ -488,7 +488,7 @@ public long getNextTransferFromWhere() { private boolean buildHandshakeBuffer() { final List epochEntries = AutoSwitchHAConnection.this.epochCache.getAllEntries(); final int lastEpoch = AutoSwitchHAConnection.this.epochCache.lastEpoch(); - final long maxPhyOffset = AutoSwitchHAConnection.this.haService.getDefaultMessageStore().getMaxWrotePhyOffset(); + final long maxPhyOffset = AutoSwitchHAConnection.this.haService.getDefaultMessageStore().getMaxPhyOffset(); this.byteBufferHeader.position(0); this.byteBufferHeader.limit(HANDSHAKE_HEADER_SIZE); // State diff --git a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java index a092a6094b5..c4a9aeb812a 100644 --- a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java +++ b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java @@ -121,7 +121,7 @@ public boolean changeToMaster(int masterEpoch) { } // Append new epoch to epochFile - final EpochEntry newEpochEntry = new EpochEntry(masterEpoch, this.defaultMessageStore.getMaxWrotePhyOffset()); + final EpochEntry newEpochEntry = new EpochEntry(masterEpoch, this.defaultMessageStore.getMaxPhyOffset()); if (this.epochCache.lastEpoch() >= masterEpoch) { this.epochCache.truncateSuffixByEpoch(masterEpoch); } @@ -136,7 +136,7 @@ public boolean changeToMaster(int masterEpoch) { } } - LOGGER.info("TruncateOffset is {}, confirmOffset is {}, maxPhyOffset is {}", truncateOffset, getConfirmOffset(), this.defaultMessageStore.getMaxWrotePhyOffset()); + LOGGER.info("TruncateOffset is {}, confirmOffset is {}, maxPhyOffset is {}", truncateOffset, getConfirmOffset(), this.defaultMessageStore.getMaxPhyOffset()); this.defaultMessageStore.recoverTopicQueueTable(); LOGGER.info("Change ha to master success, newMasterEpoch:{}, startOffset:{}", masterEpoch, newEpochEntry.getStartOffset()); @@ -248,7 +248,7 @@ public void updateConnectionLastCaughtUpTime(final String slaveAddress, final lo public long getConfirmOffset() { if (this.defaultMessageStore.getMessageStoreConfig().getBrokerRole() != BrokerRole.SLAVE) { if (this.syncStateSet.size() == 1) { - return this.defaultMessageStore.getMaxWrotePhyOffset(); + return this.defaultMessageStore.getMaxPhyOffset(); } // First time compute confirmOffset. if (this.confirmOffset <= 0) { @@ -277,7 +277,7 @@ public HARuntimeInfo getRuntimeInfo(long masterPutWhere) { info.setMaster(false); info.getHaClientRuntimeInfo().setMasterAddr(this.haClient.getHaMasterAddress()); - info.getHaClientRuntimeInfo().setMaxOffset(this.getDefaultMessageStore().getMaxWrotePhyOffset()); + info.getHaClientRuntimeInfo().setMaxOffset(this.getDefaultMessageStore().getMaxPhyOffset()); info.getHaClientRuntimeInfo().setLastReadTimestamp(this.haClient.getLastReadTimestamp()); info.getHaClientRuntimeInfo().setLastWriteTimestamp(this.haClient.getLastWriteTimestamp()); info.getHaClientRuntimeInfo().setTransferredByteInSecond(this.haClient.getTransferredByteInSecond()); @@ -312,7 +312,7 @@ public void updateConfirmOffset(long confirmOffset) { private long computeConfirmOffset() { final Set currentSyncStateSet = getSyncStateSet(); - long confirmOffset = this.defaultMessageStore.getMaxWrotePhyOffset(); + long confirmOffset = this.defaultMessageStore.getMaxPhyOffset(); for (HAConnection connection : this.connectionList) { final String slaveAddress = ((AutoSwitchHAConnection) connection).getSlaveAddress(); if (currentSyncStateSet.contains(slaveAddress)) { @@ -355,7 +355,7 @@ public long truncateInvalidMsg() { } boolean doNext = true; - long reputFromOffset = this.defaultMessageStore.getMaxWrotePhyOffset() - dispatchBehind; + long reputFromOffset = this.defaultMessageStore.getMaxPhyOffset() - dispatchBehind; do { SelectMappedBufferResult result = this.defaultMessageStore.getCommitLog().getData(reputFromOffset); if (result == null) { @@ -385,7 +385,7 @@ public long truncateInvalidMsg() { } finally { result.release(); } - } while (reputFromOffset < this.defaultMessageStore.getMaxWrotePhyOffset() && doNext); + } while (reputFromOffset < this.defaultMessageStore.getMaxPhyOffset() && doNext); LOGGER.info("Truncate commitLog to {}", reputFromOffset); this.defaultMessageStore.truncateDirtyFiles(reputFromOffset); From e833c255ad36409fbbc4182e9eaaf359850134d8 Mon Sep 17 00:00:00 2001 From: RongtongJin Date: Wed, 21 Dec 2022 14:54:49 +0800 Subject: [PATCH 09/15] just test --- .../org/apache/rocketmq/store/CommitLog.java | 174 +++++------------- .../rocketmq/store/TransientStorePool.java | 4 +- .../store/logfile/DefaultMappedFile.java | 3 +- 3 files changed, 53 insertions(+), 128 deletions(-) diff --git a/store/src/main/java/org/apache/rocketmq/store/CommitLog.java b/store/src/main/java/org/apache/rocketmq/store/CommitLog.java index 36de60926ce..77780580800 100644 --- a/store/src/main/java/org/apache/rocketmq/store/CommitLog.java +++ b/store/src/main/java/org/apache/rocketmq/store/CommitLog.java @@ -88,13 +88,9 @@ public class CommitLog implements Swappable { public CommitLog(final DefaultMessageStore messageStore) { String storePath = messageStore.getMessageStoreConfig().getStorePathCommitLog(); if (storePath.contains(MixAll.MULTI_PATH_SPLITTER)) { - this.mappedFileQueue = new MultiPathMappedFileQueue(messageStore.getMessageStoreConfig(), - messageStore.getMessageStoreConfig().getMappedFileSizeCommitLog(), - messageStore.getAllocateMappedFileService(), this::getFullStorePaths); + this.mappedFileQueue = new MultiPathMappedFileQueue(messageStore.getMessageStoreConfig(), messageStore.getMessageStoreConfig().getMappedFileSizeCommitLog(), messageStore.getAllocateMappedFileService(), this::getFullStorePaths); } else { - this.mappedFileQueue = new MappedFileQueue(storePath, - messageStore.getMessageStoreConfig().getMappedFileSizeCommitLog(), - messageStore.getAllocateMappedFileService()); + this.mappedFileQueue = new MappedFileQueue(storePath, messageStore.getMessageStoreConfig().getMappedFileSizeCommitLog(), messageStore.getAllocateMappedFileService()); } this.defaultMessageStore = messageStore; @@ -103,8 +99,7 @@ public CommitLog(final DefaultMessageStore messageStore) { this.appendMessageCallback = new DefaultAppendMessageCallback(); putMessageThreadLocal = new ThreadLocal() { - @Override - protected PutMessageThreadLocal initialValue() { + @Override protected PutMessageThreadLocal initialValue() { return new PutMessageThreadLocal(defaultMessageStore.getMessageStoreConfig().getMaxMessageSize()); } }; @@ -179,22 +174,13 @@ public long remainHowManyDataToFlush() { return this.mappedFileQueue.remainHowManyDataToFlush(); } - public int deleteExpiredFile( - final long expiredTime, - final int deleteFilesInterval, - final long intervalForcibly, - final boolean cleanImmediately - ) { + public int deleteExpiredFile(final long expiredTime, final int deleteFilesInterval, final long intervalForcibly, + final boolean cleanImmediately) { return deleteExpiredFile(expiredTime, deleteFilesInterval, intervalForcibly, cleanImmediately, 0); } - public int deleteExpiredFile( - final long expiredTime, - final int deleteFilesInterval, - final long intervalForcibly, - final boolean cleanImmediately, - final int deleteFileBatchMax - ) { + public int deleteExpiredFile(final long expiredTime, final int deleteFilesInterval, final long intervalForcibly, + final boolean cleanImmediately, final int deleteFileBatchMax) { return this.mappedFileQueue.deleteExpiredFileByTime(expiredTime, deleteFilesInterval, intervalForcibly, cleanImmediately, deleteFileBatchMax); } @@ -496,8 +482,7 @@ public DispatchRequest checkMessageAndReturnSize(java.nio.ByteBuffer byteBuffer, } if (delayLevel > 0) { - tagsCode = this.defaultMessageStore.computeDeliverTimestamp(delayLevel, - storeTimestamp); + tagsCode = this.defaultMessageStore.computeDeliverTimestamp(delayLevel, storeTimestamp); } } } @@ -510,26 +495,11 @@ public DispatchRequest checkMessageAndReturnSize(java.nio.ByteBuffer byteBuffer, doNothingForDeadCode(bornTimeStamp); doNothingForDeadCode(byteBuffer1); doNothingForDeadCode(byteBuffer2); - log.error( - "[BUG]read total count not equals msg total size. totalSize={}, readTotalCount={}, bodyLen={}, topicLen={}, propertiesLength={}", - totalSize, readLength, bodyLen, topicLen, propertiesLength); + log.error("[BUG]read total count not equals msg total size. totalSize={}, readTotalCount={}, bodyLen={}, topicLen={}, propertiesLength={}", totalSize, readLength, bodyLen, topicLen, propertiesLength); return new DispatchRequest(totalSize, false/* success */); } - DispatchRequest dispatchRequest = new DispatchRequest( - topic, - queueId, - physicOffset, - totalSize, - tagsCode, - storeTimestamp, - queueOffset, - keys, - uniqKey, - sysFlag, - preparedTransactionOffset, - propertiesMap - ); + DispatchRequest dispatchRequest = new DispatchRequest(topic, queueId, physicOffset, totalSize, tagsCode, storeTimestamp, queueOffset, keys, uniqKey, sysFlag, preparedTransactionOffset, propertiesMap); setBatchSizeIfNeeded(propertiesMap, dispatchRequest); @@ -571,8 +541,7 @@ public long getLastFileFromOffset() { return -1; } - @Deprecated - public void recoverAbnormally(long maxPhyOffsetOfConsumeQueue) { + @Deprecated public void recoverAbnormally(long maxPhyOffsetOfConsumeQueue) { // recover by the minimum time stamp boolean checkCRCOnRecover = this.defaultMessageStore.getMessageStoreConfig().isCheckCRCOnRecover(); boolean checkDupInfo = this.defaultMessageStore.getMessageStoreConfig().isDuplicationEnable(); @@ -704,19 +673,14 @@ private boolean isMappedFileMatchedRecover(final MappedFile mappedFile) { return false; } - if (this.defaultMessageStore.getMessageStoreConfig().isMessageIndexEnable() - && this.defaultMessageStore.getMessageStoreConfig().isMessageIndexSafe()) { + if (this.defaultMessageStore.getMessageStoreConfig().isMessageIndexEnable() && this.defaultMessageStore.getMessageStoreConfig().isMessageIndexSafe()) { if (storeTimestamp <= this.defaultMessageStore.getStoreCheckpoint().getMinTimestampIndex()) { - log.info("find check timestamp, {} {}", - storeTimestamp, - UtilAll.timeMillisToHumanString(storeTimestamp)); + log.info("find check timestamp, {} {}", storeTimestamp, UtilAll.timeMillisToHumanString(storeTimestamp)); return true; } } else { if (storeTimestamp <= this.defaultMessageStore.getStoreCheckpoint().getMinTimestamp()) { - log.info("find check timestamp, {} {}", - storeTimestamp, - UtilAll.timeMillisToHumanString(storeTimestamp)); + log.info("find check timestamp, {} {}", storeTimestamp, UtilAll.timeMillisToHumanString(storeTimestamp)); return true; } } @@ -748,8 +712,7 @@ public void setMappedFileQueueOffset(final long phyOffset) { public void updateMaxMessageSize(PutMessageThreadLocal putMessageThreadLocal) { // dynamically adjust maxMessageSize, but not support DLedger mode temporarily int newMaxMessageSize = this.defaultMessageStore.getMessageStoreConfig().getMaxMessageSize(); - if (newMaxMessageSize >= 10 && - putMessageThreadLocal.getEncoder().getMaxMessageBodySize() != newMaxMessageSize) { + if (newMaxMessageSize >= 10 && putMessageThreadLocal.getEncoder().getMaxMessageBodySize() != newMaxMessageSize) { putMessageThreadLocal.getEncoder().updateEncoderBufferCapacity(newMaxMessageSize); } } @@ -769,8 +732,7 @@ public CompletableFuture asyncPutMessage(final MessageExtBroke String topic = msg.getTopic(); msg.setVersion(MessageVersion.MESSAGE_VERSION_V1); - boolean autoMessageVersionOnTopicLen = - this.defaultMessageStore.getMessageStoreConfig().isAutoMessageVersionOnTopicLen(); + boolean autoMessageVersionOnTopicLen = this.defaultMessageStore.getMessageStoreConfig().isAutoMessageVersionOnTopicLen(); if (autoMessageVersionOnTopicLen && topic.length() > Byte.MAX_VALUE) { msg.setVersion(MessageVersion.MESSAGE_VERSION_V2); } @@ -811,8 +773,7 @@ public CompletableFuture asyncPutMessage(final MessageExtBroke needAckNums = MixAll.ALL_ACK_IN_SYNC_STATE_SET; } } else if (needHandleHA && this.defaultMessageStore.getBrokerConfig().isEnableSlaveActingMaster()) { - int inSyncReplicas = Math.min(this.defaultMessageStore.getAliveReplicaNumInGroup(), - this.defaultMessageStore.getHaService().inSyncReplicasNums(currOffset)); + int inSyncReplicas = Math.min(this.defaultMessageStore.getAliveReplicaNumInGroup(), this.defaultMessageStore.getHaService().inSyncReplicasNums(currOffset)); needAckNums = calcNeedAckNums(inSyncReplicas); if (needAckNums > inSyncReplicas) { // Tell the producer, don't have enough slaves to handle the send request @@ -824,8 +785,7 @@ public CompletableFuture asyncPutMessage(final MessageExtBroke try { boolean needAssignOffset = true; - if (defaultMessageStore.getMessageStoreConfig().isDuplicationEnable() - && defaultMessageStore.getMessageStoreConfig().getBrokerRole() != BrokerRole.SLAVE) { + if (defaultMessageStore.getMessageStoreConfig().isDuplicationEnable() && defaultMessageStore.getMessageStoreConfig().getBrokerRole() != BrokerRole.SLAVE) { needAssignOffset = false; } if (needAssignOffset) { @@ -957,7 +917,6 @@ public CompletableFuture asyncPutMessages(final MessageExtBatc int needAckNums = this.defaultMessageStore.getMessageStoreConfig().getInSyncReplicas(); boolean needHandleHA = needHandleHA(messageExtBatch); - if (needHandleHA && this.defaultMessageStore.getBrokerConfig().isEnableControllerMode()) { if (this.defaultMessageStore.getHaService().inSyncReplicasNums(currOffset) < this.defaultMessageStore.getMessageStoreConfig().getMinInSyncReplicas()) { return CompletableFuture.completedFuture(new PutMessageResult(PutMessageStatus.IN_SYNC_REPLICAS_NOT_ENOUGH, null)); @@ -967,8 +926,7 @@ public CompletableFuture asyncPutMessages(final MessageExtBatc needAckNums = MixAll.ALL_ACK_IN_SYNC_STATE_SET; } } else if (needHandleHA && this.defaultMessageStore.getBrokerConfig().isEnableSlaveActingMaster()) { - int inSyncReplicas = Math.min(this.defaultMessageStore.getAliveReplicaNumInGroup(), - this.defaultMessageStore.getHaService().inSyncReplicasNums(currOffset)); + int inSyncReplicas = Math.min(this.defaultMessageStore.getAliveReplicaNumInGroup(), this.defaultMessageStore.getHaService().inSyncReplicasNums(currOffset)); needAckNums = calcNeedAckNums(inSyncReplicas); if (needAckNums > inSyncReplicas) { // Tell the producer, don't have enough slaves to handle the send request @@ -977,8 +935,7 @@ public CompletableFuture asyncPutMessages(final MessageExtBatc } messageExtBatch.setVersion(MessageVersion.MESSAGE_VERSION_V1); - boolean autoMessageVersionOnTopicLen = - this.defaultMessageStore.getMessageStoreConfig().isAutoMessageVersionOnTopicLen(); + boolean autoMessageVersionOnTopicLen = this.defaultMessageStore.getMessageStoreConfig().isAutoMessageVersionOnTopicLen(); if (autoMessageVersionOnTopicLen && messageExtBatch.getTopic().length() > Byte.MAX_VALUE) { messageExtBatch.setVersion(MessageVersion.MESSAGE_VERSION_V2); } @@ -1258,30 +1215,22 @@ class CommitRealTimeService extends FlushCommitLogService { private long lastCommitTimestamp = 0; - @Override - public String getServiceName() { + @Override public String getServiceName() { if (CommitLog.this.defaultMessageStore.getBrokerConfig().isInBrokerContainer()) { return CommitLog.this.defaultMessageStore.getBrokerIdentity().getIdentifier() + CommitRealTimeService.class.getSimpleName(); } return CommitRealTimeService.class.getSimpleName(); } - @Override - public void run() { + @Override public void run() { CommitLog.log.info(this.getServiceName() + " service started"); while (!this.isStopped()) { - if (defaultMessageStore.getMessageStoreConfig().getBrokerRole() == BrokerRole.SLAVE) { - waitForRunning(5 * 1000); - continue; - } - int interval = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getCommitIntervalCommitLog(); int commitDataLeastPages = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getCommitCommitLogLeastPages(); - int commitDataThoroughInterval = - CommitLog.this.defaultMessageStore.getMessageStoreConfig().getCommitCommitLogThoroughInterval(); + int commitDataThoroughInterval = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getCommitCommitLogThoroughInterval(); long begin = System.currentTimeMillis(); if (begin >= (this.lastCommitTimestamp + commitDataThoroughInterval)) { @@ -1290,7 +1239,7 @@ public void run() { } try { - boolean result = CommitLog.this.mappedFileQueue.commit(commitDataLeastPages); + boolean result = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getBrokerRole() == BrokerRole.SLAVE || CommitLog.this.mappedFileQueue.commit(commitDataLeastPages); long end = System.currentTimeMillis(); if (!result) { this.lastCommitTimestamp = end; // result = false means some data committed. @@ -1319,8 +1268,7 @@ class FlushRealTimeService extends FlushCommitLogService { private long lastFlushTimestamp = 0; private long printTimes = 0; - @Override - public void run() { + @Override public void run() { CommitLog.log.info(this.getServiceName() + " service started"); while (!this.isStopped()) { @@ -1329,8 +1277,7 @@ public void run() { int interval = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getFlushIntervalCommitLog(); int flushPhysicQueueLeastPages = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getFlushCommitLogLeastPages(); - int flushPhysicQueueThoroughInterval = - CommitLog.this.defaultMessageStore.getMessageStoreConfig().getFlushCommitLogThoroughInterval(); + int flushPhysicQueueThoroughInterval = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getFlushCommitLogThoroughInterval(); boolean printFlushProgress = false; @@ -1382,8 +1329,7 @@ public void run() { CommitLog.log.info(this.getServiceName() + " service end"); } - @Override - public String getServiceName() { + @Override public String getServiceName() { if (CommitLog.this.defaultMessageStore.getBrokerConfig().isInBrokerContainer()) { return CommitLog.this.defaultMessageStore.getBrokerConfig().getIdentifier() + FlushRealTimeService.class.getSimpleName(); } @@ -1395,8 +1341,7 @@ private void printFlushProgress() { // + CommitLog.this.mappedFileQueue.howMuchFallBehind()); } - @Override - public long getJoinTime() { + @Override public long getJoinTime() { return 1000 * 60 * 5; } } @@ -1495,8 +1440,7 @@ private void doCommit() { } } - @Override - public void run() { + @Override public void run() { CommitLog.log.info(this.getServiceName() + " service started"); while (!this.isStopped()) { @@ -1522,21 +1466,18 @@ public void run() { CommitLog.log.info(this.getServiceName() + " service end"); } - @Override - protected void onWaitEnd() { + @Override protected void onWaitEnd() { this.swapRequests(); } - @Override - public String getServiceName() { + @Override public String getServiceName() { if (CommitLog.this.defaultMessageStore.getBrokerConfig().isInBrokerContainer()) { return CommitLog.this.defaultMessageStore.getBrokerConfig().getIdentifier() + GroupCommitService.class.getSimpleName(); } return GroupCommitService.class.getSimpleName(); } - @Override - public long getJoinTime() { + @Override public long getJoinTime() { return 1000 * 60 * 5; } } @@ -1556,11 +1497,9 @@ public synchronized boolean putRequest(final GroupCommitRequest request) { if (hasNotified.compareAndSet(false, true)) { waitPoint.countDown(); // notify } - boolean flag = this.requestsWrite.size() > - CommitLog.this.defaultMessageStore.getMessageStoreConfig().getMaxAsyncPutMessageRequests(); + boolean flag = this.requestsWrite.size() > CommitLog.this.defaultMessageStore.getMessageStoreConfig().getMaxAsyncPutMessageRequests(); if (flag) { - log.info("Async requests {} exceeded the threshold {}", requestsWrite.size(), - CommitLog.this.defaultMessageStore.getMessageStoreConfig().getMaxAsyncPutMessageRequests()); + log.info("Async requests {} exceeded the threshold {}", requestsWrite.size(), CommitLog.this.defaultMessageStore.getMessageStoreConfig().getMaxAsyncPutMessageRequests()); } return flag; @@ -1633,21 +1572,18 @@ public void run() { CommitLog.log.info(this.getServiceName() + " service end"); } - @Override - protected void onWaitEnd() { + @Override protected void onWaitEnd() { this.swapRequests(); } - @Override - public String getServiceName() { + @Override public String getServiceName() { if (CommitLog.this.defaultMessageStore.getBrokerConfig().isInBrokerContainer()) { return CommitLog.this.defaultMessageStore.getBrokerConfig().getIdentifier() + GroupCheckService.class.getSimpleName(); } return GroupCheckService.class.getSimpleName(); } - @Override - public long getJoinTime() { + @Override public long getJoinTime() { return 1000 * 60 * 5; } } @@ -1713,10 +1649,8 @@ public AppendMessageResult doAppend(final long fileFromOffset, final ByteBuffer // Here the length of the specially set maxBlank final long beginTimeMills = CommitLog.this.defaultMessageStore.now(); byteBuffer.put(this.msgStoreItemMemory.array(), 0, 8); - return new AppendMessageResult(AppendMessageStatus.END_OF_FILE, wroteOffset, - maxBlank, /* only wrote 8 bytes, but declare wrote maxBlank for compute write position */ - msgIdSupplier, msgInner.getStoreTimestamp(), - queueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills); + return new AppendMessageResult(AppendMessageStatus.END_OF_FILE, wroteOffset, maxBlank, /* only wrote 8 bytes, but declare wrote maxBlank for compute write position */ + msgIdSupplier, msgInner.getStoreTimestamp(), queueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills); } int pos = 4 + 4 + 4 + 4 + 4; @@ -1737,8 +1671,7 @@ public AppendMessageResult doAppend(final long fileFromOffset, final ByteBuffer byteBuffer.put(preEncodeBuffer); CommitLog.this.getMessageStore().getPerfCounter().endTick("WRITE_MEMORY_TIME_MS"); msgInner.setEncodedBuff(null); - return new AppendMessageResult(AppendMessageStatus.PUT_OK, wroteOffset, msgLen, msgIdSupplier, - msgInner.getStoreTimestamp(), queueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills, messageNum); + return new AppendMessageResult(AppendMessageStatus.PUT_OK, wroteOffset, msgLen, msgIdSupplier, msgInner.getStoreTimestamp(), queueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills, messageNum); } public AppendMessageResult doAppend(final long fileFromOffset, final ByteBuffer byteBuffer, final int maxBlank, @@ -1799,8 +1732,7 @@ public AppendMessageResult doAppend(final long fileFromOffset, final ByteBuffer // Here the length of the specially set maxBlank byteBuffer.reset(); //ignore the previous appended messages byteBuffer.put(this.msgStoreItemMemory.array(), 0, 8); - return new AppendMessageResult(AppendMessageStatus.END_OF_FILE, wroteOffset, maxBlank, msgIdSupplier, messageExtBatch.getStoreTimestamp(), - beginQueueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills); + return new AppendMessageResult(AppendMessageStatus.END_OF_FILE, wroteOffset, maxBlank, msgIdSupplier, messageExtBatch.getStoreTimestamp(), beginQueueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills); } //move to add queue offset and commitlog offset int pos = msgPos + 20; @@ -1822,8 +1754,7 @@ public AppendMessageResult doAppend(final long fileFromOffset, final ByteBuffer messagesByteBuff.limit(totalMsgLen); byteBuffer.put(messagesByteBuff); messageExtBatch.setEncodedBuff(null); - AppendMessageResult result = new AppendMessageResult(AppendMessageStatus.PUT_OK, wroteOffset, totalMsgLen, msgIdSupplier, - messageExtBatch.getStoreTimestamp(), beginQueueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills); + AppendMessageResult result = new AppendMessageResult(AppendMessageStatus.PUT_OK, wroteOffset, totalMsgLen, msgIdSupplier, messageExtBatch.getStoreTimestamp(), beginQueueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills); result.setMsgNum(msgNum); return result; @@ -1860,8 +1791,7 @@ public DefaultFlushManager() { this.commitLogService = new CommitLog.CommitRealTimeService(); } - @Override - public void start() { + @Override public void start() { this.flushCommitLogService.start(); if (defaultMessageStore.isTransientStorePoolEnable()) { @@ -1880,14 +1810,12 @@ public void handleDiskFlush(AppendMessageResult result, PutMessageResult putMess CompletableFuture flushOkFuture = request.future(); PutMessageStatus flushStatus = null; try { - flushStatus = flushOkFuture.get(CommitLog.this.defaultMessageStore.getMessageStoreConfig().getSyncFlushTimeout(), - TimeUnit.MILLISECONDS); + flushStatus = flushOkFuture.get(CommitLog.this.defaultMessageStore.getMessageStoreConfig().getSyncFlushTimeout(), TimeUnit.MILLISECONDS); } catch (InterruptedException | ExecutionException | TimeoutException e) { //flushOK=false; } if (flushStatus != PutMessageStatus.PUT_OK) { - log.error("do groupcommit, wait for flush failed, topic: " + messageExt.getTopic() + " tags: " + messageExt.getTags() - + " client address: " + messageExt.getBornHostString()); + log.error("do groupcommit, wait for flush failed, topic: " + messageExt.getTopic() + " tags: " + messageExt.getTags() + " client address: " + messageExt.getBornHostString()); putMessageResult.setPutMessageStatus(PutMessageStatus.FLUSH_DISK_TIMEOUT); } } else { @@ -1930,14 +1858,12 @@ public CompletableFuture handleDiskFlush(AppendMessageResult r } } - @Override - public void wakeUpFlush() { + @Override public void wakeUpFlush() { // now wake up flush thread. flushCommitLogService.wakeup(); } - @Override - public void shutdown() { + @Override public void shutdown() { if (defaultMessageStore.isTransientStorePoolEnable()) { this.commitLogService.shutdown(); } @@ -1959,8 +1885,7 @@ public MessageStore getMessageStore() { return defaultMessageStore; } - @Override - public void swapMap(int reserveNum, long forceSwapIntervalMs, long normalSwapIntervalMs) { + @Override public void swapMap(int reserveNum, long forceSwapIntervalMs, long normalSwapIntervalMs) { this.getMappedFileQueue().swapMap(reserveNum, forceSwapIntervalMs, normalSwapIntervalMs); } @@ -1968,8 +1893,7 @@ public boolean isMappedFilesEmpty() { return this.mappedFileQueue.isMappedFilesEmpty(); } - @Override - public void cleanSwappedMap(long forceCleanSwapIntervalMs) { + @Override public void cleanSwappedMap(long forceCleanSwapIntervalMs) { this.getMappedFileQueue().cleanSwappedMap(forceCleanSwapIntervalMs); } diff --git a/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java b/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java index 1a49b689edb..dcb21aa3091 100644 --- a/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java +++ b/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java @@ -86,7 +86,7 @@ public int availableBufferNums() { return Integer.MAX_VALUE; } - public boolean enableControllerMode() { - return messageStore.getBrokerConfig().isEnableControllerMode(); + public DefaultMessageStore getMessageStore() { + return messageStore; } } diff --git a/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java b/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java index a582a7d795d..9e7a1a8a96f 100644 --- a/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java +++ b/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java @@ -50,6 +50,7 @@ import org.apache.rocketmq.store.PutMessageContext; import org.apache.rocketmq.store.SelectMappedBufferResult; import org.apache.rocketmq.store.TransientStorePool; +import org.apache.rocketmq.store.config.BrokerRole; import org.apache.rocketmq.store.config.FlushDiskType; import org.apache.rocketmq.store.util.LibC; import sun.nio.ch.DirectBuffer; @@ -560,7 +561,7 @@ public int getReadPosition() { // } else { // return COMMITTED_POSITION_UPDATER.get(this); // } - return writeBuffer == null ? WROTE_POSITION_UPDATER.get(this) : COMMITTED_POSITION_UPDATER.get(this); + return transientStorePool == null || transientStorePool.getMessageStore().getMessageStoreConfig().getBrokerRole() == BrokerRole.SLAVE ? WROTE_POSITION_UPDATER.get(this) : COMMITTED_POSITION_UPDATER.get(this); } @Override From a6b43fbedb13d9ba4093691a50938d0dc1136de8 Mon Sep 17 00:00:00 2001 From: RongtongJin Date: Wed, 21 Dec 2022 15:23:52 +0800 Subject: [PATCH 10/15] just test --- .../org/apache/rocketmq/store/DefaultMessageStore.java | 7 ------- 1 file changed, 7 deletions(-) diff --git a/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java b/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java index 40ae89dfe26..fd90aca91aa 100644 --- a/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java +++ b/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java @@ -2560,13 +2560,6 @@ private void doReput() { DefaultMessageStore.this.commitLog.checkMessageAndReturnSize(result.getByteBuffer(), false, false, false); int size = dispatchRequest.getBufferSize() == -1 ? dispatchRequest.getMsgSize() : dispatchRequest.getBufferSize(); - if (!dispatchRequest.isSuccess()) { - LOGGER.warn("reputFromOffset = " + reputFromOffset); - LOGGER.warn("confirmOffset = " + DefaultMessageStore.this.getConfirmOffset()); - LOGGER.warn("maxOffset = " + DefaultMessageStore.this.getMaxPhyOffset()); - LOGGER.warn("" + dispatchRequest.toString()); - } - if (reputFromOffset + size > DefaultMessageStore.this.getConfirmOffset()) { doNext = false; break; From ff2069466f3c8768a19ffd2d0b351ad7e9c4b7ed Mon Sep 17 00:00:00 2001 From: RongtongJin Date: Thu, 22 Dec 2022 10:13:52 +0800 Subject: [PATCH 11/15] just test --- .../org/apache/rocketmq/store/CommitLog.java | 60 ++++++++++++------- .../apache/rocketmq/store/FlushManager.java | 40 +++++++++++++ .../rocketmq/store/TransientStorePool.java | 4 ++ .../ha/autoswitch/AutoSwitchHAService.java | 25 +++++++- .../store/logfile/DefaultMappedFile.java | 3 +- 5 files changed, 107 insertions(+), 25 deletions(-) create mode 100644 store/src/main/java/org/apache/rocketmq/store/FlushManager.java diff --git a/store/src/main/java/org/apache/rocketmq/store/CommitLog.java b/store/src/main/java/org/apache/rocketmq/store/CommitLog.java index 77780580800..25da533005a 100644 --- a/store/src/main/java/org/apache/rocketmq/store/CommitLog.java +++ b/store/src/main/java/org/apache/rocketmq/store/CommitLog.java @@ -505,7 +505,6 @@ public DispatchRequest checkMessageAndReturnSize(java.nio.ByteBuffer byteBuffer, return dispatchRequest; } catch (Exception e) { - log.error("Check message and return size error", e); } return new DispatchRequest(-1, false /* success */); @@ -1215,6 +1214,8 @@ class CommitRealTimeService extends FlushCommitLogService { private long lastCommitTimestamp = 0; + private volatile boolean shouldRunningCommit = false; + @Override public String getServiceName() { if (CommitLog.this.defaultMessageStore.getBrokerConfig().isInBrokerContainer()) { return CommitLog.this.defaultMessageStore.getBrokerIdentity().getIdentifier() + CommitRealTimeService.class.getSimpleName(); @@ -1226,6 +1227,11 @@ class CommitRealTimeService extends FlushCommitLogService { CommitLog.log.info(this.getServiceName() + " service started"); while (!this.isStopped()) { + if (!shouldRunningCommit) { + waitForRunning(5 * 1000); + continue; + } + int interval = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getCommitIntervalCommitLog(); int commitDataLeastPages = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getCommitCommitLogLeastPages(); @@ -1239,7 +1245,7 @@ class CommitRealTimeService extends FlushCommitLogService { } try { - boolean result = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getBrokerRole() == BrokerRole.SLAVE || CommitLog.this.mappedFileQueue.commit(commitDataLeastPages); + boolean result = CommitLog.this.mappedFileQueue.commit(commitDataLeastPages); long end = System.currentTimeMillis(); if (!result) { this.lastCommitTimestamp = end; // result = false means some data committed. @@ -1262,6 +1268,14 @@ class CommitRealTimeService extends FlushCommitLogService { } CommitLog.log.info(this.getServiceName() + " service end"); } + + public void setShouldRunningCommit(boolean shouldRunningCommit) { + this.shouldRunningCommit = shouldRunningCommit; + } + + public boolean isShouldRunningCommit() { + return shouldRunningCommit; + } } class FlushRealTimeService extends FlushCommitLogService { @@ -1759,27 +1773,15 @@ public AppendMessageResult doAppend(final long fileFromOffset, final ByteBuffer return result; } - } - interface FlushManager { - void start(); - - void shutdown(); - - void wakeUpFlush(); - - void handleDiskFlush(AppendMessageResult result, PutMessageResult putMessageResult, MessageExt messageExt); - - CompletableFuture handleDiskFlush(AppendMessageResult result, MessageExt messageExt); - } class DefaultFlushManager implements FlushManager { private final FlushCommitLogService flushCommitLogService; //If TransientStorePool enabled, we must flush message to FileChannel at fixed periods - private final FlushCommitLogService commitLogService; + private final CommitRealTimeService commitRealTimeService; public DefaultFlushManager() { if (FlushDiskType.SYNC_FLUSH == CommitLog.this.defaultMessageStore.getMessageStoreConfig().getFlushDiskType()) { @@ -1788,14 +1790,14 @@ public DefaultFlushManager() { this.flushCommitLogService = new CommitLog.FlushRealTimeService(); } - this.commitLogService = new CommitLog.CommitRealTimeService(); + this.commitRealTimeService = new CommitLog.CommitRealTimeService(); } @Override public void start() { this.flushCommitLogService.start(); if (defaultMessageStore.isTransientStorePoolEnable()) { - this.commitLogService.start(); + this.commitRealTimeService.start(); } } @@ -1827,7 +1829,7 @@ public void handleDiskFlush(AppendMessageResult result, PutMessageResult putMess if (!CommitLog.this.defaultMessageStore.isTransientStorePoolEnable()) { flushCommitLogService.wakeup(); } else { - commitLogService.wakeup(); + commitRealTimeService.wakeup(); } } } @@ -1852,7 +1854,7 @@ public CompletableFuture handleDiskFlush(AppendMessageResult r if (!CommitLog.this.defaultMessageStore.isTransientStorePoolEnable()) { flushCommitLogService.wakeup(); } else { - commitLogService.wakeup(); + commitRealTimeService.wakeup(); } return CompletableFuture.completedFuture(PutMessageStatus.PUT_OK); } @@ -1863,9 +1865,24 @@ public CompletableFuture handleDiskFlush(AppendMessageResult r flushCommitLogService.wakeup(); } + @Override + public void wakeUpCommit() { + // now wake up commit log thread. + commitRealTimeService.wakeup(); + } + + @Override + public void setShouldRunningCommit(boolean shouldRunningCommit) { + commitRealTimeService.setShouldRunningCommit(shouldRunningCommit); + } + + @Override public boolean isRunningCommit() { + return commitRealTimeService.isShouldRunningCommit(); + } + @Override public void shutdown() { if (defaultMessageStore.isTransientStorePoolEnable()) { - this.commitLogService.shutdown(); + this.commitRealTimeService.shutdown(); } this.flushCommitLogService.shutdown(); @@ -1897,4 +1914,7 @@ public boolean isMappedFilesEmpty() { this.getMappedFileQueue().cleanSwappedMap(forceCleanSwapIntervalMs); } + public FlushManager getFlushManager() { + return flushManager; + } } diff --git a/store/src/main/java/org/apache/rocketmq/store/FlushManager.java b/store/src/main/java/org/apache/rocketmq/store/FlushManager.java new file mode 100644 index 00000000000..fdd000884f1 --- /dev/null +++ b/store/src/main/java/org/apache/rocketmq/store/FlushManager.java @@ -0,0 +1,40 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.rocketmq.store; + +import java.util.concurrent.CompletableFuture; +import org.apache.rocketmq.common.message.MessageExt; + +public interface FlushManager { + + void start(); + + void shutdown(); + + void wakeUpFlush(); + + void wakeUpCommit(); + + void setShouldRunningCommit(boolean shouldRunningCommit); + + boolean isRunningCommit(); + + void handleDiskFlush(AppendMessageResult result, PutMessageResult putMessageResult, MessageExt messageExt); + + CompletableFuture handleDiskFlush(AppendMessageResult result, MessageExt messageExt); +} diff --git a/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java b/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java index dcb21aa3091..c3270e047c8 100644 --- a/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java +++ b/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java @@ -89,4 +89,8 @@ public int availableBufferNums() { public DefaultMessageStore getMessageStore() { return messageStore; } + + public boolean isRunningCommit() { + return messageStore.getCommitLog().getFlushManager().isRunningCommit(); + } } diff --git a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java index c4a9aeb812a..42c0f0368cf 100644 --- a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java +++ b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java @@ -136,6 +136,11 @@ public boolean changeToMaster(int masterEpoch) { } } + if (defaultMessageStore.isTransientStorePoolEnable()) { + defaultMessageStore.getCommitLog().resetOffset(defaultMessageStore.getMaxPhyOffset()); + defaultMessageStore.getCommitLog().getFlushManager().setShouldRunningCommit(true); + } + LOGGER.info("TruncateOffset is {}, confirmOffset is {}, maxPhyOffset is {}", truncateOffset, getConfirmOffset(), this.defaultMessageStore.getMaxPhyOffset()); this.defaultMessageStore.recoverTopicQueueTable(); @@ -162,6 +167,19 @@ public boolean changeToSlave(String newMasterAddr, int newMasterEpoch, Long slav this.haClient.updateMasterAddress(newMasterAddr); this.haClient.updateHaMasterAddress(null); this.haClient.start(); + + if (defaultMessageStore.isTransientStorePoolEnable()) { + while (getDefaultMessageStore().remainHowManyDataToCommit() > 0) { + getDefaultMessageStore().getCommitLog().getFlushManager().wakeUpCommit(); + try { + Thread.sleep(100); + } catch (Exception e) { + + } + } + defaultMessageStore.getCommitLog().getFlushManager().setShouldRunningCommit(false); + } + LOGGER.info("Change ha to slave success, newMasterAddress:{}, newMasterEpoch:{}", newMasterAddr, newMasterEpoch); return true; } catch (final Exception e) { @@ -199,8 +217,8 @@ public void notifySyncStateSetChanged(final Set newSyncStateSet) { } /** - * Check and maybe shrink the inSyncStateSet. - * A slave will be removed from inSyncStateSet if (curTime - HaConnection.lastCaughtUpTime) > option(haMaxTimeSlaveNotCatchup) + * Check and maybe shrink the inSyncStateSet. A slave will be removed from inSyncStateSet if (curTime - + * HaConnection.lastCaughtUpTime) > option(haMaxTimeSlaveNotCatchup) */ public Set maybeShrinkInSyncStateSet() { final Set newSyncStateSet = getSyncStateSet(); @@ -385,7 +403,8 @@ public long truncateInvalidMsg() { } finally { result.release(); } - } while (reputFromOffset < this.defaultMessageStore.getMaxPhyOffset() && doNext); + } + while (reputFromOffset < this.defaultMessageStore.getMaxPhyOffset() && doNext); LOGGER.info("Truncate commitLog to {}", reputFromOffset); this.defaultMessageStore.truncateDirtyFiles(reputFromOffset); diff --git a/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java b/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java index 9e7a1a8a96f..4ee2a4f9885 100644 --- a/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java +++ b/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java @@ -50,7 +50,6 @@ import org.apache.rocketmq.store.PutMessageContext; import org.apache.rocketmq.store.SelectMappedBufferResult; import org.apache.rocketmq.store.TransientStorePool; -import org.apache.rocketmq.store.config.BrokerRole; import org.apache.rocketmq.store.config.FlushDiskType; import org.apache.rocketmq.store.util.LibC; import sun.nio.ch.DirectBuffer; @@ -561,7 +560,7 @@ public int getReadPosition() { // } else { // return COMMITTED_POSITION_UPDATER.get(this); // } - return transientStorePool == null || transientStorePool.getMessageStore().getMessageStoreConfig().getBrokerRole() == BrokerRole.SLAVE ? WROTE_POSITION_UPDATER.get(this) : COMMITTED_POSITION_UPDATER.get(this); + return transientStorePool == null || !transientStorePool.isRunningCommit() ? WROTE_POSITION_UPDATER.get(this) : COMMITTED_POSITION_UPDATER.get(this); } @Override From eefd71c94f6a832bc6e757e288396ffa28c917b4 Mon Sep 17 00:00:00 2001 From: RongtongJin Date: Thu, 22 Dec 2022 11:12:37 +0800 Subject: [PATCH 12/15] just test --- .../apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java | 1 + 1 file changed, 1 insertion(+) diff --git a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java index 42c0f0368cf..cb6e80934ff 100644 --- a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java +++ b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java @@ -137,6 +137,7 @@ public boolean changeToMaster(int masterEpoch) { } if (defaultMessageStore.isTransientStorePoolEnable()) { + defaultMessageStore.setPhysicalOffset(defaultMessageStore.getMaxPhyOffset()); defaultMessageStore.getCommitLog().resetOffset(defaultMessageStore.getMaxPhyOffset()); defaultMessageStore.getCommitLog().getFlushManager().setShouldRunningCommit(true); } From 0f4fde27cd4d0d4ab94cfc7c108e392ec744a55b Mon Sep 17 00:00:00 2001 From: RongtongJin Date: Thu, 22 Dec 2022 14:46:42 +0800 Subject: [PATCH 13/15] just test --- .../org/apache/rocketmq/store/CommitLog.java | 24 ----------------- .../apache/rocketmq/store/FlushManager.java | 4 --- .../rocketmq/store/TransientStorePool.java | 9 +++++-- .../ha/autoswitch/AutoSwitchHAService.java | 27 ++++++++++--------- .../store/logfile/DefaultMappedFile.java | 8 ++++-- 5 files changed, 28 insertions(+), 44 deletions(-) diff --git a/store/src/main/java/org/apache/rocketmq/store/CommitLog.java b/store/src/main/java/org/apache/rocketmq/store/CommitLog.java index 25da533005a..09cead24a85 100644 --- a/store/src/main/java/org/apache/rocketmq/store/CommitLog.java +++ b/store/src/main/java/org/apache/rocketmq/store/CommitLog.java @@ -1214,8 +1214,6 @@ class CommitRealTimeService extends FlushCommitLogService { private long lastCommitTimestamp = 0; - private volatile boolean shouldRunningCommit = false; - @Override public String getServiceName() { if (CommitLog.this.defaultMessageStore.getBrokerConfig().isInBrokerContainer()) { return CommitLog.this.defaultMessageStore.getBrokerIdentity().getIdentifier() + CommitRealTimeService.class.getSimpleName(); @@ -1227,11 +1225,6 @@ class CommitRealTimeService extends FlushCommitLogService { CommitLog.log.info(this.getServiceName() + " service started"); while (!this.isStopped()) { - if (!shouldRunningCommit) { - waitForRunning(5 * 1000); - continue; - } - int interval = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getCommitIntervalCommitLog(); int commitDataLeastPages = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getCommitCommitLogLeastPages(); @@ -1268,14 +1261,6 @@ class CommitRealTimeService extends FlushCommitLogService { } CommitLog.log.info(this.getServiceName() + " service end"); } - - public void setShouldRunningCommit(boolean shouldRunningCommit) { - this.shouldRunningCommit = shouldRunningCommit; - } - - public boolean isShouldRunningCommit() { - return shouldRunningCommit; - } } class FlushRealTimeService extends FlushCommitLogService { @@ -1871,15 +1856,6 @@ public void wakeUpCommit() { commitRealTimeService.wakeup(); } - @Override - public void setShouldRunningCommit(boolean shouldRunningCommit) { - commitRealTimeService.setShouldRunningCommit(shouldRunningCommit); - } - - @Override public boolean isRunningCommit() { - return commitRealTimeService.isShouldRunningCommit(); - } - @Override public void shutdown() { if (defaultMessageStore.isTransientStorePoolEnable()) { this.commitRealTimeService.shutdown(); diff --git a/store/src/main/java/org/apache/rocketmq/store/FlushManager.java b/store/src/main/java/org/apache/rocketmq/store/FlushManager.java index fdd000884f1..fe3951ae7f7 100644 --- a/store/src/main/java/org/apache/rocketmq/store/FlushManager.java +++ b/store/src/main/java/org/apache/rocketmq/store/FlushManager.java @@ -30,10 +30,6 @@ public interface FlushManager { void wakeUpCommit(); - void setShouldRunningCommit(boolean shouldRunningCommit); - - boolean isRunningCommit(); - void handleDiskFlush(AppendMessageResult result, PutMessageResult putMessageResult, MessageExt messageExt); CompletableFuture handleDiskFlush(AppendMessageResult result, MessageExt messageExt); diff --git a/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java b/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java index c3270e047c8..72edcbe9f68 100644 --- a/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java +++ b/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java @@ -34,6 +34,7 @@ public class TransientStorePool { private final int fileSize; private final Deque availableBuffers; private final DefaultMessageStore messageStore; + private volatile boolean isRealCommit; public TransientStorePool(final DefaultMessageStore messageStore) { this.messageStore = messageStore; @@ -90,7 +91,11 @@ public DefaultMessageStore getMessageStore() { return messageStore; } - public boolean isRunningCommit() { - return messageStore.getCommitLog().getFlushManager().isRunningCommit(); + public boolean isRealCommit() { + return isRealCommit; + } + + public void setRealCommit(boolean realCommit) { + isRealCommit = realCommit; } } diff --git a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java index cb6e80934ff..628425753ce 100644 --- a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java +++ b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java @@ -137,9 +137,8 @@ public boolean changeToMaster(int masterEpoch) { } if (defaultMessageStore.isTransientStorePoolEnable()) { - defaultMessageStore.setPhysicalOffset(defaultMessageStore.getMaxPhyOffset()); - defaultMessageStore.getCommitLog().resetOffset(defaultMessageStore.getMaxPhyOffset()); - defaultMessageStore.getCommitLog().getFlushManager().setShouldRunningCommit(true); + waitingForAllCommit(); + defaultMessageStore.getTransientStorePool().setRealCommit(true); } LOGGER.info("TruncateOffset is {}, confirmOffset is {}, maxPhyOffset is {}", truncateOffset, getConfirmOffset(), this.defaultMessageStore.getMaxPhyOffset()); @@ -170,15 +169,8 @@ public boolean changeToSlave(String newMasterAddr, int newMasterEpoch, Long slav this.haClient.start(); if (defaultMessageStore.isTransientStorePoolEnable()) { - while (getDefaultMessageStore().remainHowManyDataToCommit() > 0) { - getDefaultMessageStore().getCommitLog().getFlushManager().wakeUpCommit(); - try { - Thread.sleep(100); - } catch (Exception e) { - - } - } - defaultMessageStore.getCommitLog().getFlushManager().setShouldRunningCommit(false); + waitingForAllCommit(); + defaultMessageStore.getTransientStorePool().setRealCommit(false); } LOGGER.info("Change ha to slave success, newMasterAddress:{}, newMasterEpoch:{}", newMasterAddr, newMasterEpoch); @@ -189,6 +181,17 @@ public boolean changeToSlave(String newMasterAddr, int newMasterEpoch, Long slav } } + public void waitingForAllCommit() { + while (getDefaultMessageStore().remainHowManyDataToCommit() > 0) { + getDefaultMessageStore().getCommitLog().getFlushManager().wakeUpCommit(); + try { + Thread.sleep(100); + } catch (Exception e) { + + } + } + } + @Override public HAClient getHAClient() { return this.haClient; diff --git a/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java b/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java index 4ee2a4f9885..6f5225b0246 100644 --- a/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java +++ b/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java @@ -364,7 +364,11 @@ public int commit(final int commitLeastPages) { //no need to commit data to file channel, so just regard wrotePosition as committedPosition. return WROTE_POSITION_UPDATER.get(this); } - if (this.isAbleToCommit(commitLeastPages)) { + + //no need to commit data to file channel, so just set committedPosition to wrotePosition. + if (transientStorePool != null && !transientStorePool.isRealCommit()) { + COMMITTED_POSITION_UPDATER.set(this, WROTE_POSITION_UPDATER.get(this)); + } else if (this.isAbleToCommit(commitLeastPages)) { if (this.hold()) { commit0(); this.release(); @@ -560,7 +564,7 @@ public int getReadPosition() { // } else { // return COMMITTED_POSITION_UPDATER.get(this); // } - return transientStorePool == null || !transientStorePool.isRunningCommit() ? WROTE_POSITION_UPDATER.get(this) : COMMITTED_POSITION_UPDATER.get(this); + return transientStorePool == null || !transientStorePool.isRealCommit() ? WROTE_POSITION_UPDATER.get(this) : COMMITTED_POSITION_UPDATER.get(this); } @Override From adf5361135a51bf892364e6a9cd0685260190f17 Mon Sep 17 00:00:00 2001 From: RongtongJin Date: Thu, 22 Dec 2022 15:20:59 +0800 Subject: [PATCH 14/15] Format the check style --- .../org/apache/rocketmq/store/CommitLog.java | 163 ++++++++++++------ .../rocketmq/store/DefaultMessageStore.java | 6 +- .../ha/autoswitch/AutoSwitchHAService.java | 7 +- .../store/logfile/DefaultMappedFile.java | 5 - 4 files changed, 117 insertions(+), 64 deletions(-) diff --git a/store/src/main/java/org/apache/rocketmq/store/CommitLog.java b/store/src/main/java/org/apache/rocketmq/store/CommitLog.java index 09cead24a85..9421a70f1f1 100644 --- a/store/src/main/java/org/apache/rocketmq/store/CommitLog.java +++ b/store/src/main/java/org/apache/rocketmq/store/CommitLog.java @@ -88,9 +88,13 @@ public class CommitLog implements Swappable { public CommitLog(final DefaultMessageStore messageStore) { String storePath = messageStore.getMessageStoreConfig().getStorePathCommitLog(); if (storePath.contains(MixAll.MULTI_PATH_SPLITTER)) { - this.mappedFileQueue = new MultiPathMappedFileQueue(messageStore.getMessageStoreConfig(), messageStore.getMessageStoreConfig().getMappedFileSizeCommitLog(), messageStore.getAllocateMappedFileService(), this::getFullStorePaths); + this.mappedFileQueue = new MultiPathMappedFileQueue(messageStore.getMessageStoreConfig(), + messageStore.getMessageStoreConfig().getMappedFileSizeCommitLog(), + messageStore.getAllocateMappedFileService(), this::getFullStorePaths); } else { - this.mappedFileQueue = new MappedFileQueue(storePath, messageStore.getMessageStoreConfig().getMappedFileSizeCommitLog(), messageStore.getAllocateMappedFileService()); + this.mappedFileQueue = new MappedFileQueue(storePath, + messageStore.getMessageStoreConfig().getMappedFileSizeCommitLog(), + messageStore.getAllocateMappedFileService()); } this.defaultMessageStore = messageStore; @@ -99,7 +103,8 @@ public CommitLog(final DefaultMessageStore messageStore) { this.appendMessageCallback = new DefaultAppendMessageCallback(); putMessageThreadLocal = new ThreadLocal() { - @Override protected PutMessageThreadLocal initialValue() { + @Override + protected PutMessageThreadLocal initialValue() { return new PutMessageThreadLocal(defaultMessageStore.getMessageStoreConfig().getMaxMessageSize()); } }; @@ -162,10 +167,6 @@ public long getMaxOffset() { return this.mappedFileQueue.getMaxOffset(); } - public long getMaxWroteOffset() { - return this.mappedFileQueue.getMaxWrotePosition(); - } - public long remainHowManyDataToCommit() { return this.mappedFileQueue.remainHowManyDataToCommit(); } @@ -174,13 +175,22 @@ public long remainHowManyDataToFlush() { return this.mappedFileQueue.remainHowManyDataToFlush(); } - public int deleteExpiredFile(final long expiredTime, final int deleteFilesInterval, final long intervalForcibly, - final boolean cleanImmediately) { + public int deleteExpiredFile( + final long expiredTime, + final int deleteFilesInterval, + final long intervalForcibly, + final boolean cleanImmediately + ) { return deleteExpiredFile(expiredTime, deleteFilesInterval, intervalForcibly, cleanImmediately, 0); } - public int deleteExpiredFile(final long expiredTime, final int deleteFilesInterval, final long intervalForcibly, - final boolean cleanImmediately, final int deleteFileBatchMax) { + public int deleteExpiredFile( + final long expiredTime, + final int deleteFilesInterval, + final long intervalForcibly, + final boolean cleanImmediately, + final int deleteFileBatchMax + ) { return this.mappedFileQueue.deleteExpiredFileByTime(expiredTime, deleteFilesInterval, intervalForcibly, cleanImmediately, deleteFileBatchMax); } @@ -482,7 +492,8 @@ public DispatchRequest checkMessageAndReturnSize(java.nio.ByteBuffer byteBuffer, } if (delayLevel > 0) { - tagsCode = this.defaultMessageStore.computeDeliverTimestamp(delayLevel, storeTimestamp); + tagsCode = this.defaultMessageStore.computeDeliverTimestamp(delayLevel, + storeTimestamp); } } } @@ -495,11 +506,26 @@ public DispatchRequest checkMessageAndReturnSize(java.nio.ByteBuffer byteBuffer, doNothingForDeadCode(bornTimeStamp); doNothingForDeadCode(byteBuffer1); doNothingForDeadCode(byteBuffer2); - log.error("[BUG]read total count not equals msg total size. totalSize={}, readTotalCount={}, bodyLen={}, topicLen={}, propertiesLength={}", totalSize, readLength, bodyLen, topicLen, propertiesLength); + log.error( + "[BUG]read total count not equals msg total size. totalSize={}, readTotalCount={}, bodyLen={}, topicLen={}, propertiesLength={}", + totalSize, readLength, bodyLen, topicLen, propertiesLength); return new DispatchRequest(totalSize, false/* success */); } - DispatchRequest dispatchRequest = new DispatchRequest(topic, queueId, physicOffset, totalSize, tagsCode, storeTimestamp, queueOffset, keys, uniqKey, sysFlag, preparedTransactionOffset, propertiesMap); + DispatchRequest dispatchRequest = new DispatchRequest( + topic, + queueId, + physicOffset, + totalSize, + tagsCode, + storeTimestamp, + queueOffset, + keys, + uniqKey, + sysFlag, + preparedTransactionOffset, + propertiesMap + ); setBatchSizeIfNeeded(propertiesMap, dispatchRequest); @@ -540,7 +566,8 @@ public long getLastFileFromOffset() { return -1; } - @Deprecated public void recoverAbnormally(long maxPhyOffsetOfConsumeQueue) { + @Deprecated + public void recoverAbnormally(long maxPhyOffsetOfConsumeQueue) { // recover by the minimum time stamp boolean checkCRCOnRecover = this.defaultMessageStore.getMessageStoreConfig().isCheckCRCOnRecover(); boolean checkDupInfo = this.defaultMessageStore.getMessageStoreConfig().isDuplicationEnable(); @@ -672,14 +699,19 @@ private boolean isMappedFileMatchedRecover(final MappedFile mappedFile) { return false; } - if (this.defaultMessageStore.getMessageStoreConfig().isMessageIndexEnable() && this.defaultMessageStore.getMessageStoreConfig().isMessageIndexSafe()) { + if (this.defaultMessageStore.getMessageStoreConfig().isMessageIndexEnable() + && this.defaultMessageStore.getMessageStoreConfig().isMessageIndexSafe()) { if (storeTimestamp <= this.defaultMessageStore.getStoreCheckpoint().getMinTimestampIndex()) { - log.info("find check timestamp, {} {}", storeTimestamp, UtilAll.timeMillisToHumanString(storeTimestamp)); + log.info("find check timestamp, {} {}", + storeTimestamp, + UtilAll.timeMillisToHumanString(storeTimestamp)); return true; } } else { if (storeTimestamp <= this.defaultMessageStore.getStoreCheckpoint().getMinTimestamp()) { - log.info("find check timestamp, {} {}", storeTimestamp, UtilAll.timeMillisToHumanString(storeTimestamp)); + log.info("find check timestamp, {} {}", + storeTimestamp, + UtilAll.timeMillisToHumanString(storeTimestamp)); return true; } } @@ -711,7 +743,8 @@ public void setMappedFileQueueOffset(final long phyOffset) { public void updateMaxMessageSize(PutMessageThreadLocal putMessageThreadLocal) { // dynamically adjust maxMessageSize, but not support DLedger mode temporarily int newMaxMessageSize = this.defaultMessageStore.getMessageStoreConfig().getMaxMessageSize(); - if (newMaxMessageSize >= 10 && putMessageThreadLocal.getEncoder().getMaxMessageBodySize() != newMaxMessageSize) { + if (newMaxMessageSize >= 10 && + putMessageThreadLocal.getEncoder().getMaxMessageBodySize() != newMaxMessageSize) { putMessageThreadLocal.getEncoder().updateEncoderBufferCapacity(newMaxMessageSize); } } @@ -731,7 +764,8 @@ public CompletableFuture asyncPutMessage(final MessageExtBroke String topic = msg.getTopic(); msg.setVersion(MessageVersion.MESSAGE_VERSION_V1); - boolean autoMessageVersionOnTopicLen = this.defaultMessageStore.getMessageStoreConfig().isAutoMessageVersionOnTopicLen(); + boolean autoMessageVersionOnTopicLen = + this.defaultMessageStore.getMessageStoreConfig().isAutoMessageVersionOnTopicLen(); if (autoMessageVersionOnTopicLen && topic.length() > Byte.MAX_VALUE) { msg.setVersion(MessageVersion.MESSAGE_VERSION_V2); } @@ -772,7 +806,8 @@ public CompletableFuture asyncPutMessage(final MessageExtBroke needAckNums = MixAll.ALL_ACK_IN_SYNC_STATE_SET; } } else if (needHandleHA && this.defaultMessageStore.getBrokerConfig().isEnableSlaveActingMaster()) { - int inSyncReplicas = Math.min(this.defaultMessageStore.getAliveReplicaNumInGroup(), this.defaultMessageStore.getHaService().inSyncReplicasNums(currOffset)); + int inSyncReplicas = Math.min(this.defaultMessageStore.getAliveReplicaNumInGroup(), + this.defaultMessageStore.getHaService().inSyncReplicasNums(currOffset)); needAckNums = calcNeedAckNums(inSyncReplicas); if (needAckNums > inSyncReplicas) { // Tell the producer, don't have enough slaves to handle the send request @@ -784,7 +819,8 @@ public CompletableFuture asyncPutMessage(final MessageExtBroke try { boolean needAssignOffset = true; - if (defaultMessageStore.getMessageStoreConfig().isDuplicationEnable() && defaultMessageStore.getMessageStoreConfig().getBrokerRole() != BrokerRole.SLAVE) { + if (defaultMessageStore.getMessageStoreConfig().isDuplicationEnable() + && defaultMessageStore.getMessageStoreConfig().getBrokerRole() != BrokerRole.SLAVE) { needAssignOffset = false; } if (needAssignOffset) { @@ -916,6 +952,7 @@ public CompletableFuture asyncPutMessages(final MessageExtBatc int needAckNums = this.defaultMessageStore.getMessageStoreConfig().getInSyncReplicas(); boolean needHandleHA = needHandleHA(messageExtBatch); + if (needHandleHA && this.defaultMessageStore.getBrokerConfig().isEnableControllerMode()) { if (this.defaultMessageStore.getHaService().inSyncReplicasNums(currOffset) < this.defaultMessageStore.getMessageStoreConfig().getMinInSyncReplicas()) { return CompletableFuture.completedFuture(new PutMessageResult(PutMessageStatus.IN_SYNC_REPLICAS_NOT_ENOUGH, null)); @@ -925,7 +962,8 @@ public CompletableFuture asyncPutMessages(final MessageExtBatc needAckNums = MixAll.ALL_ACK_IN_SYNC_STATE_SET; } } else if (needHandleHA && this.defaultMessageStore.getBrokerConfig().isEnableSlaveActingMaster()) { - int inSyncReplicas = Math.min(this.defaultMessageStore.getAliveReplicaNumInGroup(), this.defaultMessageStore.getHaService().inSyncReplicasNums(currOffset)); + int inSyncReplicas = Math.min(this.defaultMessageStore.getAliveReplicaNumInGroup(), + this.defaultMessageStore.getHaService().inSyncReplicasNums(currOffset)); needAckNums = calcNeedAckNums(inSyncReplicas); if (needAckNums > inSyncReplicas) { // Tell the producer, don't have enough slaves to handle the send request @@ -934,7 +972,8 @@ public CompletableFuture asyncPutMessages(final MessageExtBatc } messageExtBatch.setVersion(MessageVersion.MESSAGE_VERSION_V1); - boolean autoMessageVersionOnTopicLen = this.defaultMessageStore.getMessageStoreConfig().isAutoMessageVersionOnTopicLen(); + boolean autoMessageVersionOnTopicLen = + this.defaultMessageStore.getMessageStoreConfig().isAutoMessageVersionOnTopicLen(); if (autoMessageVersionOnTopicLen && messageExtBatch.getTopic().length() > Byte.MAX_VALUE) { messageExtBatch.setVersion(MessageVersion.MESSAGE_VERSION_V2); } @@ -1214,22 +1253,24 @@ class CommitRealTimeService extends FlushCommitLogService { private long lastCommitTimestamp = 0; - @Override public String getServiceName() { + @Override + public String getServiceName() { if (CommitLog.this.defaultMessageStore.getBrokerConfig().isInBrokerContainer()) { return CommitLog.this.defaultMessageStore.getBrokerIdentity().getIdentifier() + CommitRealTimeService.class.getSimpleName(); } return CommitRealTimeService.class.getSimpleName(); } - @Override public void run() { + @Override + public void run() { CommitLog.log.info(this.getServiceName() + " service started"); while (!this.isStopped()) { - int interval = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getCommitIntervalCommitLog(); int commitDataLeastPages = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getCommitCommitLogLeastPages(); - int commitDataThoroughInterval = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getCommitCommitLogThoroughInterval(); + int commitDataThoroughInterval = + CommitLog.this.defaultMessageStore.getMessageStoreConfig().getCommitCommitLogThoroughInterval(); long begin = System.currentTimeMillis(); if (begin >= (this.lastCommitTimestamp + commitDataThoroughInterval)) { @@ -1267,7 +1308,8 @@ class FlushRealTimeService extends FlushCommitLogService { private long lastFlushTimestamp = 0; private long printTimes = 0; - @Override public void run() { + @Override + public void run() { CommitLog.log.info(this.getServiceName() + " service started"); while (!this.isStopped()) { @@ -1276,7 +1318,8 @@ class FlushRealTimeService extends FlushCommitLogService { int interval = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getFlushIntervalCommitLog(); int flushPhysicQueueLeastPages = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getFlushCommitLogLeastPages(); - int flushPhysicQueueThoroughInterval = CommitLog.this.defaultMessageStore.getMessageStoreConfig().getFlushCommitLogThoroughInterval(); + int flushPhysicQueueThoroughInterval = + CommitLog.this.defaultMessageStore.getMessageStoreConfig().getFlushCommitLogThoroughInterval(); boolean printFlushProgress = false; @@ -1328,7 +1371,8 @@ class FlushRealTimeService extends FlushCommitLogService { CommitLog.log.info(this.getServiceName() + " service end"); } - @Override public String getServiceName() { + @Override + public String getServiceName() { if (CommitLog.this.defaultMessageStore.getBrokerConfig().isInBrokerContainer()) { return CommitLog.this.defaultMessageStore.getBrokerConfig().getIdentifier() + FlushRealTimeService.class.getSimpleName(); } @@ -1340,7 +1384,8 @@ private void printFlushProgress() { // + CommitLog.this.mappedFileQueue.howMuchFallBehind()); } - @Override public long getJoinTime() { + @Override + public long getJoinTime() { return 1000 * 60 * 5; } } @@ -1439,7 +1484,8 @@ private void doCommit() { } } - @Override public void run() { + @Override + public void run() { CommitLog.log.info(this.getServiceName() + " service started"); while (!this.isStopped()) { @@ -1465,18 +1511,21 @@ private void doCommit() { CommitLog.log.info(this.getServiceName() + " service end"); } - @Override protected void onWaitEnd() { + @Override + protected void onWaitEnd() { this.swapRequests(); } - @Override public String getServiceName() { + @Override + public String getServiceName() { if (CommitLog.this.defaultMessageStore.getBrokerConfig().isInBrokerContainer()) { return CommitLog.this.defaultMessageStore.getBrokerConfig().getIdentifier() + GroupCommitService.class.getSimpleName(); } return GroupCommitService.class.getSimpleName(); } - @Override public long getJoinTime() { + @Override + public long getJoinTime() { return 1000 * 60 * 5; } } @@ -1496,9 +1545,11 @@ public synchronized boolean putRequest(final GroupCommitRequest request) { if (hasNotified.compareAndSet(false, true)) { waitPoint.countDown(); // notify } - boolean flag = this.requestsWrite.size() > CommitLog.this.defaultMessageStore.getMessageStoreConfig().getMaxAsyncPutMessageRequests(); + boolean flag = this.requestsWrite.size() > + CommitLog.this.defaultMessageStore.getMessageStoreConfig().getMaxAsyncPutMessageRequests(); if (flag) { - log.info("Async requests {} exceeded the threshold {}", requestsWrite.size(), CommitLog.this.defaultMessageStore.getMessageStoreConfig().getMaxAsyncPutMessageRequests()); + log.info("Async requests {} exceeded the threshold {}", requestsWrite.size(), + CommitLog.this.defaultMessageStore.getMessageStoreConfig().getMaxAsyncPutMessageRequests()); } return flag; @@ -1571,18 +1622,21 @@ public void run() { CommitLog.log.info(this.getServiceName() + " service end"); } - @Override protected void onWaitEnd() { + @Override + protected void onWaitEnd() { this.swapRequests(); } - @Override public String getServiceName() { + @Override + public String getServiceName() { if (CommitLog.this.defaultMessageStore.getBrokerConfig().isInBrokerContainer()) { return CommitLog.this.defaultMessageStore.getBrokerConfig().getIdentifier() + GroupCheckService.class.getSimpleName(); } return GroupCheckService.class.getSimpleName(); } - @Override public long getJoinTime() { + @Override + public long getJoinTime() { return 1000 * 60 * 5; } } @@ -1648,8 +1702,10 @@ public AppendMessageResult doAppend(final long fileFromOffset, final ByteBuffer // Here the length of the specially set maxBlank final long beginTimeMills = CommitLog.this.defaultMessageStore.now(); byteBuffer.put(this.msgStoreItemMemory.array(), 0, 8); - return new AppendMessageResult(AppendMessageStatus.END_OF_FILE, wroteOffset, maxBlank, /* only wrote 8 bytes, but declare wrote maxBlank for compute write position */ - msgIdSupplier, msgInner.getStoreTimestamp(), queueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills); + return new AppendMessageResult(AppendMessageStatus.END_OF_FILE, wroteOffset, + maxBlank, /* only wrote 8 bytes, but declare wrote maxBlank for compute write position */ + msgIdSupplier, msgInner.getStoreTimestamp(), + queueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills); } int pos = 4 + 4 + 4 + 4 + 4; @@ -1670,7 +1726,8 @@ public AppendMessageResult doAppend(final long fileFromOffset, final ByteBuffer byteBuffer.put(preEncodeBuffer); CommitLog.this.getMessageStore().getPerfCounter().endTick("WRITE_MEMORY_TIME_MS"); msgInner.setEncodedBuff(null); - return new AppendMessageResult(AppendMessageStatus.PUT_OK, wroteOffset, msgLen, msgIdSupplier, msgInner.getStoreTimestamp(), queueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills, messageNum); + return new AppendMessageResult(AppendMessageStatus.PUT_OK, wroteOffset, msgLen, msgIdSupplier, + msgInner.getStoreTimestamp(), queueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills, messageNum); } public AppendMessageResult doAppend(final long fileFromOffset, final ByteBuffer byteBuffer, final int maxBlank, @@ -1731,7 +1788,8 @@ public AppendMessageResult doAppend(final long fileFromOffset, final ByteBuffer // Here the length of the specially set maxBlank byteBuffer.reset(); //ignore the previous appended messages byteBuffer.put(this.msgStoreItemMemory.array(), 0, 8); - return new AppendMessageResult(AppendMessageStatus.END_OF_FILE, wroteOffset, maxBlank, msgIdSupplier, messageExtBatch.getStoreTimestamp(), beginQueueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills); + return new AppendMessageResult(AppendMessageStatus.END_OF_FILE, wroteOffset, maxBlank, msgIdSupplier, messageExtBatch.getStoreTimestamp(), + beginQueueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills); } //move to add queue offset and commitlog offset int pos = msgPos + 20; @@ -1753,11 +1811,13 @@ public AppendMessageResult doAppend(final long fileFromOffset, final ByteBuffer messagesByteBuff.limit(totalMsgLen); byteBuffer.put(messagesByteBuff); messageExtBatch.setEncodedBuff(null); - AppendMessageResult result = new AppendMessageResult(AppendMessageStatus.PUT_OK, wroteOffset, totalMsgLen, msgIdSupplier, messageExtBatch.getStoreTimestamp(), beginQueueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills); + AppendMessageResult result = new AppendMessageResult(AppendMessageStatus.PUT_OK, wroteOffset, totalMsgLen, msgIdSupplier, + messageExtBatch.getStoreTimestamp(), beginQueueOffset, CommitLog.this.defaultMessageStore.now() - beginTimeMills); result.setMsgNum(msgNum); return result; } + } @@ -1766,7 +1826,7 @@ class DefaultFlushManager implements FlushManager { private final FlushCommitLogService flushCommitLogService; //If TransientStorePool enabled, we must flush message to FileChannel at fixed periods - private final CommitRealTimeService commitRealTimeService; + private final FlushCommitLogService commitRealTimeService; public DefaultFlushManager() { if (FlushDiskType.SYNC_FLUSH == CommitLog.this.defaultMessageStore.getMessageStoreConfig().getFlushDiskType()) { @@ -1845,7 +1905,8 @@ public CompletableFuture handleDiskFlush(AppendMessageResult r } } - @Override public void wakeUpFlush() { + @Override + public void wakeUpFlush() { // now wake up flush thread. flushCommitLogService.wakeup(); } @@ -1878,7 +1939,8 @@ public MessageStore getMessageStore() { return defaultMessageStore; } - @Override public void swapMap(int reserveNum, long forceSwapIntervalMs, long normalSwapIntervalMs) { + @Override + public void swapMap(int reserveNum, long forceSwapIntervalMs, long normalSwapIntervalMs) { this.getMappedFileQueue().swapMap(reserveNum, forceSwapIntervalMs, normalSwapIntervalMs); } @@ -1886,7 +1948,8 @@ public boolean isMappedFilesEmpty() { return this.mappedFileQueue.isMappedFilesEmpty(); } - @Override public void cleanSwappedMap(long forceCleanSwapIntervalMs) { + @Override + public void cleanSwappedMap(long forceCleanSwapIntervalMs) { this.getMappedFileQueue().cleanSwappedMap(forceCleanSwapIntervalMs); } diff --git a/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java b/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java index fd90aca91aa..3cf8efdfa44 100644 --- a/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java +++ b/store/src/main/java/org/apache/rocketmq/store/DefaultMessageStore.java @@ -1064,11 +1064,7 @@ public HashMap getRuntimeInfo() { @Override public long getMaxPhyOffset() { - if (messageStoreConfig.isTransientStorePoolEnable() && messageStoreConfig.getBrokerRole() == BrokerRole.SLAVE) { - return this.commitLog.getMaxWroteOffset(); - } else { - return this.commitLog.getMaxOffset(); - } + return this.commitLog.getMaxOffset(); } diff --git a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java index 628425753ce..f2b421ecd20 100644 --- a/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java +++ b/store/src/main/java/org/apache/rocketmq/store/ha/autoswitch/AutoSwitchHAService.java @@ -221,8 +221,8 @@ public void notifySyncStateSetChanged(final Set newSyncStateSet) { } /** - * Check and maybe shrink the inSyncStateSet. A slave will be removed from inSyncStateSet if (curTime - - * HaConnection.lastCaughtUpTime) > option(haMaxTimeSlaveNotCatchup) + * Check and maybe shrink the inSyncStateSet. + * A slave will be removed from inSyncStateSet if (curTime - HaConnection.lastCaughtUpTime) > option(haMaxTimeSlaveNotCatchup) */ public Set maybeShrinkInSyncStateSet() { final Set newSyncStateSet = getSyncStateSet(); @@ -407,8 +407,7 @@ public long truncateInvalidMsg() { } finally { result.release(); } - } - while (reputFromOffset < this.defaultMessageStore.getMaxPhyOffset() && doNext); + } while (reputFromOffset < this.defaultMessageStore.getMaxPhyOffset() && doNext); LOGGER.info("Truncate commitLog to {}", reputFromOffset); this.defaultMessageStore.truncateDirtyFiles(reputFromOffset); diff --git a/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java b/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java index 6f5225b0246..7b56150f64a 100644 --- a/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java +++ b/store/src/main/java/org/apache/rocketmq/store/logfile/DefaultMappedFile.java @@ -559,11 +559,6 @@ public void setWrotePosition(int pos) { */ @Override public int getReadPosition() { -// if (this.transientStorePool == null || transientStorePool.enableControllerMode()) { -// return WROTE_POSITION_UPDATER.get(this); -// } else { -// return COMMITTED_POSITION_UPDATER.get(this); -// } return transientStorePool == null || !transientStorePool.isRealCommit() ? WROTE_POSITION_UPDATER.get(this) : COMMITTED_POSITION_UPDATER.get(this); } From e3693a57a5a643321d53af7311fedc8dab60c351 Mon Sep 17 00:00:00 2001 From: RongtongJin Date: Thu, 22 Dec 2022 15:25:45 +0800 Subject: [PATCH 15/15] Format the check style --- store/src/main/java/org/apache/rocketmq/store/CommitLog.java | 3 ++- .../java/org/apache/rocketmq/store/TransientStorePool.java | 4 ---- 2 files changed, 2 insertions(+), 5 deletions(-) diff --git a/store/src/main/java/org/apache/rocketmq/store/CommitLog.java b/store/src/main/java/org/apache/rocketmq/store/CommitLog.java index 9421a70f1f1..d7e141d31c5 100644 --- a/store/src/main/java/org/apache/rocketmq/store/CommitLog.java +++ b/store/src/main/java/org/apache/rocketmq/store/CommitLog.java @@ -1917,7 +1917,8 @@ public void wakeUpCommit() { commitRealTimeService.wakeup(); } - @Override public void shutdown() { + @Override + public void shutdown() { if (defaultMessageStore.isTransientStorePoolEnable()) { this.commitRealTimeService.shutdown(); } diff --git a/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java b/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java index 72edcbe9f68..cab7f931faf 100644 --- a/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java +++ b/store/src/main/java/org/apache/rocketmq/store/TransientStorePool.java @@ -87,10 +87,6 @@ public int availableBufferNums() { return Integer.MAX_VALUE; } - public DefaultMessageStore getMessageStore() { - return messageStore; - } - public boolean isRealCommit() { return isRealCommit; }