You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@rocketmq.apache.org by di...@apache.org on 2019/02/28 06:57:19 UTC

[rocketmq] branch develop updated: [RIP-9] Add English document for Design_LoadBlancing.md and Design_Remoting.md (#810)

This is an automated email from the ASF dual-hosted git repository.

dinglei pushed a commit to branch develop
in repository https://gitbox.apache.org/repos/asf/rocketmq.git


The following commit(s) were added to refs/heads/develop by this push:
     new 059c777  [RIP-9] Add English document for Design_LoadBlancing.md and Design_Remoting.md (#810)
059c777 is described below

commit 059c77737b1b4427c7302389ac62622a40b55a1f
Author: UniverseFeeler <un...@users.noreply.github.com>
AuthorDate: Thu Feb 28 14:57:13 2019 +0800

    [RIP-9] Add English document for Design_LoadBlancing.md and Design_Remoting.md (#810)
    
    [RIP-9] Add English document for Design_LoadBlancing.md and Design_Remoting.md
---
 docs/en/Design_LoadBlancing.md | 42 +++++++++++++++++++++++++++++++++++
 docs/en/Design_Remoting.md     | 50 ++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 92 insertions(+)

diff --git a/docs/en/Design_LoadBlancing.md b/docs/en/Design_LoadBlancing.md
new file mode 100644
index 0000000..b93b6ea
--- /dev/null
+++ b/docs/en/Design_LoadBlancing.md
@@ -0,0 +1,42 @@
+## 4 Load Balancing
+Load balancing in RocketMQ is accomplished on Client side. Specifically, it can be divided into load balancing at Producer side when sending messages and load balancing at Constumer side when subscribing messages.
+
+### 4.1 Producer Load Balancing
+When the Producer sends a message, it will first find the specified TopicPublishInfo according to Topic. After getting the routing information of TopicPublishInfo, the RocketMQ client will select a queue (MessageQueue) from the messageQueue List in TopicPublishInfo  to send the message by default.Specific fault-tolerant strategies are defined in the MQFaultStrategy class.
+Here is a sendLatencyFaultEnable switch variable, which, if turned on, filters out the Broker agent of not available on the basis of randomly gradually increasing modular arithmetic selection. The so-called "latencyFault Tolerance" refers to a certain period of time to avoid previous failures. For example, if the latency of the last request exceeds 550 Lms, it will evade 3000 Lms; if it exceeds 1000L, it will evade 60000 L; if it is closed, it will choose a queue (MessageQueue) to send m [...]
+
+### 4.2 Consumer Load Balancing
+In RocketMQ, the two consumption modes (Push/Pull) on the Consumer side are both based on the pull mode to get the message, while in the Push mode it is only a kind of encapsulation of the pull mode, which is essentially implemented as the message pulling thread after pulling a batch of messages from the server. After submitting to the message consuming thread pool, it continues to try again to pull the message to the server. If the message is not pulled, the pull is delayed and continue [...]
+
+ 1, Heartbeat Packet Sending on Consumer side
+After Consumer is started, it continuously sends heartbeat packets to all Broker instances in the RocketMQ cluster via timing task (which contains the message consumption group name, subscription relationship collection,Message communication mode and the value of the client id,etc). After receiving the heartbeat message from Consumer, Broker side maintains it in Consumer Manager's local caching variable—consumerTable, At the same time, the encapsulated client network channel information  [...]
+2,Core Class for Load Balancing on Consumer side—RebalanceImpl
+Starting the MQClientInstance instance in the startup process of the Consumer instance will complete the start of the load balancing service thread-RebalanceService (executed every 20 s). By looking at the source code, we can find that the run () method of the RebalanceService thread calls the rebalanceByTopic () method of the RebalanceImpl class, which is the core of the Consumer end load balancing. Here, rebalanceByTopic () method will do different logical processing depending on wheth [...]
+(1) Get the message consumption queue set (mqSet) under the Topic from the local cache variable—topicSubscribeInfoTable of the rebalanceImpl instance.
+(2) Call mQClientFactory. findConsumerIdList () method to send a RPC communication request to Broker side to obtain the consumer Id list under the consumer group based on the parameters of topic and consumer group (consumer table constructed by Broker side based on the heartbeat data reported by the front consumer side responds and returns, business request code: GET_CONSUMER_LIST_BY_GROUP);
+(3) First, the message consumption queue and the consumer Id under Topic are sorted, then the message queue to be pulled is calculated by using the message queue allocation strategy algorithm (default: the average allocation algorithm of the message queue). The average allocation algorithm here is similar to the paging algorithm. It ranks all MessageQueues like records. It ranks all consumers like pages. It calculates the average size of each page and the range of each page record. Final [...]
+![Image text](https://github.com/apache/rocketmq/raw/develop/docs/cn/image/rocketmq_design_8.png)
+(4) Then, the updateProcessQueueTableInRebalance () method is invoked, which first compares the allocated message queue set (mqSet) with processQueueTable for filtering.
+![Image text](https://github.com/apache/rocketmq/raw/develop/docs/cn/image/rocketmq_design_9.png)
+
+ - The red part of the processQueueTable annotation in the figure above
+   indicates that it is not included with the assigned message queue set
+   mqSet. Set the Dropped attribute to true for these queues, and then
+   check whether these queues can remove the processQueueTable cache
+   variable or not. The removeUnnecessaryMessageQueue () method is
+   executed here, that is, check every 1s to see if the locks of the
+   current consumption processing queue can be retrieved and return true
+   if they are retrieved. If the lock of the current consumer processing
+   queue is still not available after waiting for 1s, it returns false.
+   If true is returned, the corresponding Entry is removed from the
+   processQueueTable cache variable.
+ - The green section in processQueueTable above represents the
+   intersection with the assigned message queue set mqSet. Determine
+   whether the ProcessQueue has expired, regardless of Pull mode, if it
+   is Push mode, set the Dropped attribute to true, and call the
+   removeUnnecessaryMessageQueue () method to try to remove Entry as
+   above;
+   
+Finally, a ProcessQueue object is created for each MessageQueue in the filtered message queue set (mqSet) and stored in the processQueueTable queue of RebalanceImpl (where the computePullFromWhere (MessageQueue mq) method of the RebalanceImpl instance is invoked to obtain the next progress consumption value offset of the MessageQueue object, which is then populated into the attribute of pullRequest object to be created next time.), and create pull request object—pullRequest to add to pul [...]
+
+The core design idea of message consumption queue is that a message consumption queue can only be consumed by one consumer in the same consumer group at the same time, and a message consumer can consume multiple message queues at the same time.
diff --git a/docs/en/Design_Remoting.md b/docs/en/Design_Remoting.md
new file mode 100644
index 0000000..ff2c99d
--- /dev/null
+++ b/docs/en/Design_Remoting.md
@@ -0,0 +1,50 @@
+## 2 Communication Mechanism
+RocketMQ message queue cluster mainly includes four roles: NameServer, Broker (Master/Slave), Producer and Consumer. The basic communication process is as follows:
+(1) After Broker start-up, it needs to complete one operation: register itself to NameServer, and then report Topic routing information to NameServer at regular intervals of 30 seconds.
+(2) When message producer Producer sends a message as a client, it needs to obtain routing information from the local cache TopicPublishInfoTable according to the Topic of the message. If not, it will be retrieved from NameServer and update to local cache, at the same time, Producer will retrieve routing information from NameServer every 30 seconds by default.
+(3) Message producer Producer chooses a queue to send the message according to the routing information obtained in 2); Broker receives the message and records it in disk as the receiver of the message.
+(4) After message consumer Consumer get the routing information according to 2) and complete the load balancing of the client, then select one or several message queues to pull messages and consume them.
+
+From 1) ~ 3 above, we can see that both Producer, Broker and NameServer communicate with each other(only part of MQ communication is mentioned here), so how to design a good network communication module is very important in MQ. It will determine the overall messaging capability and final performance of the RocketMQ cluster.
+
+rocketmq-remoting module is the module responsible for network communication in RocketMQ message queue. It is relied on and referenced by almost all other modules (such as rocketmq-client,rocketmq-broker,rocketmq-namesrv) that need network communication. In order to realize the efficient data request and reception between the client and the server, the RocketMQ message queue defines the communication protocol and extends the communication module on the basis of Netty.
+
+### 2.1 Remoting Communication Class Structure
+![](https://github.com/apache/rocketmq/raw/develop/docs/cn/image/rocketmq_design_3.png)
+### 2.2 Protocol Design and Codec
+When a message is sent between Client and Server, a protocol convention is needed for the message sent, so it is necessary to customize the message protocol of RocketMQ. At the same time, in order to efficiently transmit messages and read the received messages, it is necessary to encode and decode the messages. In RocketMQ, the RemotingCommand class encapsulates all data content in the process of message transmission, which includes not only all data structures, but also encoding and dec [...]
+
+Header field | Type | Request desc | Response desc
+--- | --- | --- | --- |
+code |int | Request  code. answering business processing is different according to different requests code | Response code. 0 means success, and non-zero means errors.
+language | LanguageCode | Language implemented by the requester | Language implemented by the responder
+version | int | Version of Request Equation | Version of Response Equation
+opaque | int |Equivalent to reqeustId, the different request identification codes on the same connection correspond to those in the response message| The response returns directly without modification
+flag | int | Sign, used to distinguish between ordinary RPC or oneway RPC | Sign, used to distinguish between ordinary RPC or oneway RPC
+remark | String | Transfer custom text information | Transfer custom text information 
+extFields | HashMap<String, String> | Request custom extension information| Response custom extension information
+![](https://github.com/apache/rocketmq/raw/develop/docs/cn/image/rocketmq_design_4.png)
+From the above figure, the transport content can be divided into four parts: 
+
+ (1) Message length: total length, four bytes of storage, occupying an int type; 
+ 
+(2) Serialization type header length: occupying an int type. The first byte represents the serialization type, and the last three bytes represent the header length;
+
+(3) Header data: serialized header data;
+
+(4) Message body data: binary byte data content of message body;
+#### 2.3 Message Communication Mode and Procedure
+There are three main ways to support communication in RocketMQ message queue: synchronous (sync), asynchronous (async), one-way (oneway). The "one-way" communication mode is relatively simple and is generally used in sending heartbeat packets without paying attention to its Response. Here, mainly introduce the asynchronous communication flow of RocketMQ.
+![](https://github.com/apache/rocketmq/raw/develop/docs/cn/image/rocketmq_design_5.png)
+#### 2.4 Reactor Multithread Design
+The RPC communication of RocketMQ uses Netty component as the underlying communication library, and also follows the Reactor multithread model. At the same time, some extensions and optimizations are made on it.
+![](https://github.com/apache/rocketmq/raw/develop/docs/cn/image/rocketmq_design_6.png)
+Above block diagram can roughly understand the Reactor multi-thread model of NettyRemotingServer in RocketMQ. A Reactor main thread (eventLoopGroupBoss, is 1 above) is responsible for listening to TCP network connection requests, establishing connections, creating SocketChannel, and registering on selector. The source code of RocketMQ automatically selects NIO and Epoll according to the type of OS. Then listen to real network data. After you get the network data, you throw it to the Work [...]
+Number of thread | Name of thread | Desc of thread
+ --- | --- | --- 
+1 | NettyBoss_%d | Reactor Main thread
+N | NettyServerEPOLLSelector_%d_%d | Reactor thread pool
+M1 | NettyServerCodecThread_%d | Worker thread pool
+M2 | RemotingExecutorThread_%d | bussiness processor thread pool
+
+