提交 92f0e1f5 编写于 作者: 傅冲 提交者: Li Zhanhui

Optimize broker topic route registration to relieve stress on Java GC

上级 9932819e
...@@ -24,6 +24,7 @@ import java.util.List; ...@@ -24,6 +24,7 @@ import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
import java.util.concurrent.ConcurrentHashMap; import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.ExecutorService; import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue; import java.util.concurrent.LinkedBlockingQueue;
...@@ -62,6 +63,7 @@ import org.apache.rocketmq.broker.subscription.SubscriptionGroupManager; ...@@ -62,6 +63,7 @@ import org.apache.rocketmq.broker.subscription.SubscriptionGroupManager;
import org.apache.rocketmq.broker.topic.TopicConfigManager; import org.apache.rocketmq.broker.topic.TopicConfigManager;
import org.apache.rocketmq.common.BrokerConfig; import org.apache.rocketmq.common.BrokerConfig;
import org.apache.rocketmq.common.Configuration; import org.apache.rocketmq.common.Configuration;
import org.apache.rocketmq.common.DataVersion;
import org.apache.rocketmq.common.ThreadFactoryImpl; import org.apache.rocketmq.common.ThreadFactoryImpl;
import org.apache.rocketmq.common.TopicConfig; import org.apache.rocketmq.common.TopicConfig;
import org.apache.rocketmq.common.UtilAll; import org.apache.rocketmq.common.UtilAll;
...@@ -768,6 +770,24 @@ public class BrokerController { ...@@ -768,6 +770,24 @@ public class BrokerController {
} }
} }
public synchronized void registerIncrementBrokerData(TopicConfig topicConfig, DataVersion dataVersion) {
TopicConfig registerTopicConfig = topicConfig;
if (!PermName.isWriteable(this.getBrokerConfig().getBrokerPermission())
|| !PermName.isReadable(this.getBrokerConfig().getBrokerPermission())) {
registerTopicConfig =
new TopicConfig(topicConfig.getTopicName(), topicConfig.getReadQueueNums(), topicConfig.getWriteQueueNums(),
this.brokerConfig.getBrokerPermission());
}
ConcurrentMap<String, TopicConfig> topicConfigTable = new ConcurrentHashMap<String, TopicConfig>();
topicConfigTable.put(topicConfig.getTopicName(), registerTopicConfig);
TopicConfigSerializeWrapper topicConfigSerializeWrapper = new TopicConfigSerializeWrapper();
topicConfigSerializeWrapper.setDataVersion(dataVersion);
topicConfigSerializeWrapper.setTopicConfigTable(topicConfigTable);
doRegisterBrokerAll(true, false, topicConfigSerializeWrapper);
}
public synchronized void registerBrokerAll(final boolean checkOrderConfig, boolean oneway, boolean forceRegister) { public synchronized void registerBrokerAll(final boolean checkOrderConfig, boolean oneway, boolean forceRegister) {
TopicConfigSerializeWrapper topicConfigWrapper = this.getTopicConfigManager().buildTopicConfigSerializeWrapper(); TopicConfigSerializeWrapper topicConfigWrapper = this.getTopicConfigManager().buildTopicConfigSerializeWrapper();
...@@ -788,30 +808,35 @@ public class BrokerController { ...@@ -788,30 +808,35 @@ public class BrokerController {
this.brokerConfig.getBrokerName(), this.brokerConfig.getBrokerName(),
this.brokerConfig.getBrokerId(), this.brokerConfig.getBrokerId(),
this.brokerConfig.getRegisterBrokerTimeoutMills())) { this.brokerConfig.getRegisterBrokerTimeoutMills())) {
List<RegisterBrokerResult> registerBrokerResultList = this.brokerOuterAPI.registerBrokerAll( doRegisterBrokerAll(checkOrderConfig, oneway, topicConfigWrapper);
this.brokerConfig.getBrokerClusterName(), }
this.getBrokerAddr(), }
this.brokerConfig.getBrokerName(),
this.brokerConfig.getBrokerId(), private void doRegisterBrokerAll(boolean checkOrderConfig, boolean oneway,
this.getHAServerAddr(), TopicConfigSerializeWrapper topicConfigWrapper) {
topicConfigWrapper, List<RegisterBrokerResult> registerBrokerResultList = this.brokerOuterAPI.registerBrokerAll(
this.filterServerManager.buildNewFilterServerList(), this.brokerConfig.getBrokerClusterName(),
oneway, this.getBrokerAddr(),
this.brokerConfig.getRegisterBrokerTimeoutMills(), this.brokerConfig.getBrokerName(),
this.brokerConfig.isCompressedRegister()); this.brokerConfig.getBrokerId(),
this.getHAServerAddr(),
if (registerBrokerResultList.size() > 0) { topicConfigWrapper,
RegisterBrokerResult registerBrokerResult = registerBrokerResultList.get(0); this.filterServerManager.buildNewFilterServerList(),
if (registerBrokerResult != null) { oneway,
if (this.updateMasterHAServerAddrPeriodically && registerBrokerResult.getHaServerAddr() != null) { this.brokerConfig.getRegisterBrokerTimeoutMills(),
this.messageStore.updateHaMasterAddress(registerBrokerResult.getHaServerAddr()); this.brokerConfig.isCompressedRegister());
}
if (registerBrokerResultList.size() > 0) {
RegisterBrokerResult registerBrokerResult = registerBrokerResultList.get(0);
if (registerBrokerResult != null) {
if (this.updateMasterHAServerAddrPeriodically && registerBrokerResult.getHaServerAddr() != null) {
this.messageStore.updateHaMasterAddress(registerBrokerResult.getHaServerAddr());
}
this.slaveSynchronize.setMasterAddr(registerBrokerResult.getMasterAddr()); this.slaveSynchronize.setMasterAddr(registerBrokerResult.getMasterAddr());
if (checkOrderConfig) { if (checkOrderConfig) {
this.getTopicConfigManager().updateOrderTopicConfig(registerBrokerResult.getKvTable()); this.getTopicConfigManager().updateOrderTopicConfig(registerBrokerResult.getKvTable());
}
} }
} }
} }
......
...@@ -29,6 +29,7 @@ import org.apache.rocketmq.client.exception.MQBrokerException; ...@@ -29,6 +29,7 @@ import org.apache.rocketmq.client.exception.MQBrokerException;
import org.apache.rocketmq.common.DataVersion; import org.apache.rocketmq.common.DataVersion;
import org.apache.rocketmq.common.MixAll; import org.apache.rocketmq.common.MixAll;
import org.apache.rocketmq.common.ThreadFactoryImpl; import org.apache.rocketmq.common.ThreadFactoryImpl;
import org.apache.rocketmq.common.UtilAll;
import org.apache.rocketmq.common.constant.LoggerName; import org.apache.rocketmq.common.constant.LoggerName;
import org.apache.rocketmq.logging.InternalLogger; import org.apache.rocketmq.logging.InternalLogger;
import org.apache.rocketmq.logging.InternalLoggerFactory; import org.apache.rocketmq.logging.InternalLoggerFactory;
...@@ -125,14 +126,28 @@ public class BrokerOuterAPI { ...@@ -125,14 +126,28 @@ public class BrokerOuterAPI {
final List<RegisterBrokerResult> registerBrokerResultList = Lists.newArrayList(); final List<RegisterBrokerResult> registerBrokerResultList = Lists.newArrayList();
List<String> nameServerAddressList = this.remotingClient.getNameServerAddressList(); List<String> nameServerAddressList = this.remotingClient.getNameServerAddressList();
if (nameServerAddressList != null && nameServerAddressList.size() > 0) { if (nameServerAddressList != null && nameServerAddressList.size() > 0) {
final RegisterBrokerRequestHeader requestHeader = new RegisterBrokerRequestHeader();
requestHeader.setBrokerAddr(brokerAddr);
requestHeader.setBrokerId(brokerId);
requestHeader.setBrokerName(brokerName);
requestHeader.setClusterName(clusterName);
requestHeader.setHaServerAddr(haServerAddr);
requestHeader.setCompressed(compressed);
RegisterBrokerBody requestBody = new RegisterBrokerBody();
requestBody.setTopicConfigSerializeWrapper(topicConfigWrapper);
requestBody.setFilterServerList(filterServerList);
final byte[] body = requestBody.encode(compressed);
final int bodyCrc32 = UtilAll.crc32(body);
requestHeader.setBodyCrc32(bodyCrc32);
final CountDownLatch countDownLatch = new CountDownLatch(nameServerAddressList.size()); final CountDownLatch countDownLatch = new CountDownLatch(nameServerAddressList.size());
for (final String namesrvAddr : nameServerAddressList) { for (final String namesrvAddr : nameServerAddressList) {
brokerOuterExecutor.execute(new Runnable() { brokerOuterExecutor.execute(new Runnable() {
@Override @Override
public void run() { public void run() {
try { try {
RegisterBrokerResult result = registerBroker(namesrvAddr, clusterName, brokerAddr, brokerName, brokerId, RegisterBrokerResult result = registerBroker(namesrvAddr,oneway, timeoutMills,requestHeader,body);
haServerAddr, topicConfigWrapper, filterServerList, oneway, timeoutMills, compressed);
if (result != null) { if (result != null) {
registerBrokerResultList.add(result); registerBrokerResultList.add(result);
} }
...@@ -158,31 +173,14 @@ public class BrokerOuterAPI { ...@@ -158,31 +173,14 @@ public class BrokerOuterAPI {
private RegisterBrokerResult registerBroker( private RegisterBrokerResult registerBroker(
final String namesrvAddr, final String namesrvAddr,
final String clusterName,
final String brokerAddr,
final String brokerName,
final long brokerId,
final String haServerAddr,
final TopicConfigSerializeWrapper topicConfigWrapper,
final List<String> filterServerList,
final boolean oneway, final boolean oneway,
final int timeoutMills, final int timeoutMills,
final boolean compressed final RegisterBrokerRequestHeader requestHeader,
final byte[] body
) throws RemotingCommandException, MQBrokerException, RemotingConnectException, RemotingSendRequestException, RemotingTimeoutException, ) throws RemotingCommandException, MQBrokerException, RemotingConnectException, RemotingSendRequestException, RemotingTimeoutException,
InterruptedException { InterruptedException {
RegisterBrokerRequestHeader requestHeader = new RegisterBrokerRequestHeader();
requestHeader.setBrokerAddr(brokerAddr);
requestHeader.setBrokerId(brokerId);
requestHeader.setBrokerName(brokerName);
requestHeader.setClusterName(clusterName);
requestHeader.setHaServerAddr(haServerAddr);
requestHeader.setCompressed(compressed);
RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.REGISTER_BROKER, requestHeader); RemotingCommand request = RemotingCommand.createRequestCommand(RequestCode.REGISTER_BROKER, requestHeader);
request.setBody(body);
RegisterBrokerBody requestBody = new RegisterBrokerBody();
requestBody.setTopicConfigSerializeWrapper(topicConfigWrapper);
requestBody.setFilterServerList(filterServerList);
request.setBody(requestBody.encode(requestHeader.isCompressed()));
if (oneway) { if (oneway) {
try { try {
......
...@@ -212,7 +212,7 @@ public class AdminBrokerProcessor implements NettyRequestProcessor { ...@@ -212,7 +212,7 @@ public class AdminBrokerProcessor implements NettyRequestProcessor {
return false; return false;
} }
private RemotingCommand updateAndCreateTopic(ChannelHandlerContext ctx, private synchronized RemotingCommand updateAndCreateTopic(ChannelHandlerContext ctx,
RemotingCommand request) throws RemotingCommandException { RemotingCommand request) throws RemotingCommandException {
final RemotingCommand response = RemotingCommand.createResponseCommand(null); final RemotingCommand response = RemotingCommand.createResponseCommand(null);
final CreateTopicRequestHeader requestHeader = final CreateTopicRequestHeader requestHeader =
...@@ -246,14 +246,12 @@ public class AdminBrokerProcessor implements NettyRequestProcessor { ...@@ -246,14 +246,12 @@ public class AdminBrokerProcessor implements NettyRequestProcessor {
this.brokerController.getTopicConfigManager().updateTopicConfig(topicConfig); this.brokerController.getTopicConfigManager().updateTopicConfig(topicConfig);
if (brokerController.getBrokerConfig().getRegisterNameServerPeriod() == 0) { this.brokerController.registerIncrementBrokerData(topicConfig,this.brokerController.getTopicConfigManager().getDataVersion());
this.brokerController.registerBrokerAll(false, true, true);
}
return null; return null;
} }
private RemotingCommand deleteTopic(ChannelHandlerContext ctx, private synchronized RemotingCommand deleteTopic(ChannelHandlerContext ctx,
RemotingCommand request) throws RemotingCommandException { RemotingCommand request) throws RemotingCommandException {
final RemotingCommand response = RemotingCommand.createResponseCommand(null); final RemotingCommand response = RemotingCommand.createResponseCommand(null);
DeleteTopicRequestHeader requestHeader = DeleteTopicRequestHeader requestHeader =
...@@ -299,7 +297,7 @@ public class AdminBrokerProcessor implements NettyRequestProcessor { ...@@ -299,7 +297,7 @@ public class AdminBrokerProcessor implements NettyRequestProcessor {
return response; return response;
} }
private RemotingCommand updateBrokerConfig(ChannelHandlerContext ctx, RemotingCommand request) { private synchronized RemotingCommand updateBrokerConfig(ChannelHandlerContext ctx, RemotingCommand request) {
final RemotingCommand response = RemotingCommand.createResponseCommand(null); final RemotingCommand response = RemotingCommand.createResponseCommand(null);
log.info("updateBrokerConfig called by {}", RemotingHelper.parseChannelRemoteAddr(ctx.channel())); log.info("updateBrokerConfig called by {}", RemotingHelper.parseChannelRemoteAddr(ctx.channel()));
......
...@@ -141,8 +141,6 @@ public class BrokerConfig { ...@@ -141,8 +141,6 @@ public class BrokerConfig {
* This configurable item defines interval of topics registration of broker to name server. Allowing values are * This configurable item defines interval of topics registration of broker to name server. Allowing values are
* between 10, 000 and 60, 000 milliseconds. * between 10, 000 and 60, 000 milliseconds.
* *
* If set to 0, newly created topics will be immediately reported to name servers and interval of periodical
* registration defaults to 10, 000 in milliseconds.
*/ */
private int registerNameServerPeriod = 1000 * 30; private int registerNameServerPeriod = 1000 * 30;
......
...@@ -38,6 +38,8 @@ public class RegisterBrokerRequestHeader implements CommandCustomHeader { ...@@ -38,6 +38,8 @@ public class RegisterBrokerRequestHeader implements CommandCustomHeader {
private boolean compressed; private boolean compressed;
private Integer bodyCrc32 = 0;
public void checkFields() throws RemotingCommandException { public void checkFields() throws RemotingCommandException {
} }
...@@ -88,4 +90,12 @@ public class RegisterBrokerRequestHeader implements CommandCustomHeader { ...@@ -88,4 +90,12 @@ public class RegisterBrokerRequestHeader implements CommandCustomHeader {
public void setCompressed(boolean compressed) { public void setCompressed(boolean compressed) {
this.compressed = compressed; this.compressed = compressed;
} }
public Integer getBodyCrc32() {
return bodyCrc32;
}
public void setBodyCrc32(Integer bodyCrc32) {
this.bodyCrc32 = bodyCrc32;
}
} }
...@@ -24,6 +24,7 @@ import org.apache.rocketmq.common.DataVersion; ...@@ -24,6 +24,7 @@ import org.apache.rocketmq.common.DataVersion;
import org.apache.rocketmq.common.MQVersion; import org.apache.rocketmq.common.MQVersion;
import org.apache.rocketmq.common.MQVersion.Version; import org.apache.rocketmq.common.MQVersion.Version;
import org.apache.rocketmq.common.MixAll; import org.apache.rocketmq.common.MixAll;
import org.apache.rocketmq.common.UtilAll;
import org.apache.rocketmq.common.constant.LoggerName; import org.apache.rocketmq.common.constant.LoggerName;
import org.apache.rocketmq.common.help.FAQUrl; import org.apache.rocketmq.common.help.FAQUrl;
import org.apache.rocketmq.logging.InternalLogger; import org.apache.rocketmq.logging.InternalLogger;
...@@ -196,6 +197,12 @@ public class DefaultRequestProcessor implements NettyRequestProcessor { ...@@ -196,6 +197,12 @@ public class DefaultRequestProcessor implements NettyRequestProcessor {
final RegisterBrokerRequestHeader requestHeader = final RegisterBrokerRequestHeader requestHeader =
(RegisterBrokerRequestHeader) request.decodeCommandCustomHeader(RegisterBrokerRequestHeader.class); (RegisterBrokerRequestHeader) request.decodeCommandCustomHeader(RegisterBrokerRequestHeader.class);
if (!checksum(ctx, request, requestHeader)) {
response.setCode(ResponseCode.SYSTEM_ERROR);
response.setRemark("crc32 not match");
return response;
}
RegisterBrokerBody registerBrokerBody = new RegisterBrokerBody(); RegisterBrokerBody registerBrokerBody = new RegisterBrokerBody();
if (request.getBody() != null) { if (request.getBody() != null) {
...@@ -230,6 +237,19 @@ public class DefaultRequestProcessor implements NettyRequestProcessor { ...@@ -230,6 +237,19 @@ public class DefaultRequestProcessor implements NettyRequestProcessor {
return response; return response;
} }
private boolean checksum(ChannelHandlerContext ctx, RemotingCommand request,
RegisterBrokerRequestHeader requestHeader) {
if (requestHeader.getBodyCrc32() != 0) {
final int crc32 = UtilAll.crc32(request.getBody());
if (crc32 != requestHeader.getBodyCrc32()) {
log.warn(String.format("receive registerBroker request,crc32 not match,from %s",
RemotingHelper.parseChannelRemoteAddr(ctx.channel())));
return false;
}
}
return true;
}
public RemotingCommand queryBrokerTopicConfig(ChannelHandlerContext ctx, public RemotingCommand queryBrokerTopicConfig(ChannelHandlerContext ctx,
RemotingCommand request) throws RemotingCommandException { RemotingCommand request) throws RemotingCommandException {
final RemotingCommand response = RemotingCommand.createResponseCommand(QueryDataVersionResponseHeader.class); final RemotingCommand response = RemotingCommand.createResponseCommand(QueryDataVersionResponseHeader.class);
...@@ -261,6 +281,12 @@ public class DefaultRequestProcessor implements NettyRequestProcessor { ...@@ -261,6 +281,12 @@ public class DefaultRequestProcessor implements NettyRequestProcessor {
final RegisterBrokerRequestHeader requestHeader = final RegisterBrokerRequestHeader requestHeader =
(RegisterBrokerRequestHeader) request.decodeCommandCustomHeader(RegisterBrokerRequestHeader.class); (RegisterBrokerRequestHeader) request.decodeCommandCustomHeader(RegisterBrokerRequestHeader.class);
if (!checksum(ctx, request, requestHeader)) {
response.setCode(ResponseCode.SYSTEM_ERROR);
response.setRemark("crc32 not match");
return response;
}
TopicConfigSerializeWrapper topicConfigWrapper; TopicConfigSerializeWrapper topicConfigWrapper;
if (request.getBody() != null) { if (request.getBody() != null) {
topicConfigWrapper = TopicConfigSerializeWrapper.decode(request.getBody(), TopicConfigSerializeWrapper.class); topicConfigWrapper = TopicConfigSerializeWrapper.decode(request.getBody(), TopicConfigSerializeWrapper.class);
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册