Redis source code reading – link establishment

In production environments where concurrent requests are high, a single Redis cannot meet performance requirements, and usually a Redis cluster is configured to improve service performance. Redis supported cluster mode after 3.

  RedisThe officially provided cluster functionality is centralized, and command requests can be sent to any Redis node. If the key of the request is not handled by that node, it is returned to the client MOVED error, prompting the client to switch to the corresponding processing node for that key. Support cluster modeThe redis client will turn automatically, while the normal mode client will only return the MOVED error.

  Let’s take a look at the next common Redis cluster structure:

  There are connections between nodes, only the master node can handle the client’s command requests; the slave node replicates the master node data, and upgrades the master node after the master node is offline. Each master node can hang more than one slave node. After the master node is offline, the slave node needs to compete. Only one slave node is elected as the master node..

Consider the following key points:

  1. How are nodes found each other, and how are requests allocated to each node?
  2. Some nodes fail, how do other nodes discover and recover?
  3. How does the main node get out of the line and compete with the nodes?
  4. Is it possible to make dynamic expansion without interrupting Redis services?

  The next few articles will examine the Redis cluster source from these key issues; first, look at the basic data structure of the cluster and how nodes connect to each other

1. data structure

   RedisClusters are non central, and each node stores information of all nodes in the cluster. Let’s look at the data structure for storing cluster node information in the Redis source code:

Copy code ></p>
<pre>struct clusterNode { //clusterState->nodesStructure cluster data interaction reception place in clusterProcessPacketMstime_t CTime; / * Node object creation time. * /CharName[REDIS_CLUSTER_NAMELEN]; / * Node name, hex string, sha1-size * /Int flags; / * REDIS_NODE_... * / / / value can refer to clusterGenNodeDescriptionUint64_t configEpoch; / * Last configEpoch observedFor this node * /Unsigned char slots[REDIS_CLUSTER_SLOTS/8]; / * slots handled by this node * /Int numslots; / * Number of slots handled by this node * /Int numslaves; / * Number of SLAve nodes, if this is a master * /Struct clusterNode **slaves; / * pointers to slave nodes * /Struct cluster Node * slaveof; /* pointer to the master node */ / / Note ClusterNode. slaveof and cluster Msg.Correlation of slaveofMstime_t ping_sent; / * Unix time we sent latest Ping * /Mstime_t pong_receIved; / * Unix time we received the pong * /Mstime_t fail_time; / * Unix time when FAIL flaG was set * /Mstime_t voted_time; / * Last time we voted for a slave of this master * /Mstime_t repl_offset_time; / * Unix time we received offset for this node * /Long long repl_offSet; / * Last known repl offset for this node. * /Char ip[REDIS_IP_STR_LEN]; / * Latest knoWn IP address of this node * /Int port; / * Latest known port of this node * /A node B nodeClusterNode-B (link1) ---> link2 (the link does not belong to any clusterNode).(A initiates meet to B) step 1Link4 < - clusteRNode-A (link 3) (which does not belong to any cluster Node) Step 2* * ///clusterCRon If the link of the node is NULL, it needs to be reconnected. If it hangs abnormally with a node in the cluster in freeClusterLink, the node is aware by reading and writing events./ / then set it to NULL at freeClusterLink.ClusteLink * link; /* TCP / IP link with this node */ / There is also an assignment in ClusteCron when active and end-to-end connections are establishedTime assignmentList * fail_reports; /* List of nodes signaling this as failing */RNodeFailReport};Typedef struct clusterNode clusterNode;</pre>
<p><img class=

Copy code ></p>
<p>  It can be seen that the function of Redis service processing cluster meet instruction is clusterCommand.</p>
<p><img class=

Copy code ></p>
<p>  AWhen the node receives the cluster meet B instruction, A enters the processing function clusterCommand, where it calls the cluster StartHandshake to connect to the B server. This function essentially creates a record of B nodes.The clusterNode (B) of the information and the link of clusterNode (B) are empty. The real initiating connection is the cluster time event handling function clusterCron. ClusterCron traverses all the nodes on the A node.And initiate a connection to the link node that is empty. The connection here also uses the file event mechanism introduced earlier.</p>
<p><strong>GossipMessage diffusion</strong></p>
<p>  GossipMessage diffusion is to use Ping messages between nodes. In order to detect the online status of nodes after establishing a connection through meet, each node must send Ping messages to its known cluster nodes. If it returns Pong within a time-out period, the node is considered to be online normally.</p>
<p>  Assuming that for three nodes of A, B and C, the following two meet instructions are sent to A nodes only:</p>
<p>    Cluster meet B</p>
<p>    Cluster meet C</p>
<p>  For A, both B and C are known node information; A sends Ping messages to B and C, respectively; when A sends Ping messages to B, sender A randomly takes known node information (assuming it contains C nodes) in the gossip message body; and node B receives Ping messages.The node information in the gossip message body is parsed and it is found that the C node is an unknown node. Then the C node is handshaken and the connection is established. For B, C is also known as a node.</p>
<p>  Look at the function of receiving gossip messages and processing unknown nodes:</p>
<p><img class=

Copy code ></p>
<p>  GossipGenerally speaking, the principle of the protocol is to transmit ten messages to each other, and eventually all nodes in the system can get the complete cluster nodes. Adding cluster node information to a ping message imposes an additional burden of being preempted each time a ping message is receivedGo through all the node information in gossip messages, and determine whether there are any nodes containing their own unknown, but also establish a connection. To alleviate the burden on the receiver, gossip messages can be delivered without all node information, and random nodes can eventually reach the goal that all nodes go to the full cluster information.Yes.</p>
<p>http://www.cgpwyj.cn/<br />http://news.cgpwyj.cn/<br />http://item.cgpwyj.cn/<br />http://www.peacemind.com.cn/<br />http://news.peacemind.com.cn/<br />http://item.peacemind.com.cn/<br />http://www.tasknet.com.cn/<br />http://news.tasknet.com.cn/<br />http://item.tasknet.com.cn/<br />http://www.ownbar.cn/<br />http://news.ownbar.cn/<br />http://item.ownbar.cn<br />http://www.shtarchao.net.cn/<br />http://news.shtarchao.net.cn/<br />http://item.shtarchao.net.cn/<br />http://www.metroworld.com.cn/<br />http://news.metroworld.com.cn/<br />http://www.cngodo.cn/<br />http://news.cngodo.cn/<br />http://item.cngodo.cn/<br />http://www.gzrdbp.cn/<br />http://news.gzrdbp.cn/<br />http://item.gzrdbp.cn/<br />http://www.dnapt.cn/<br />http://news.dnapt.cn/<br />http://item.dnapt.cn/<br />http://www.ncxlk.cn/<br />http://news.ncxlk.cn/<br />http://item.ncxlk.cn/<br />http://www.zgxxyp.cn/<br />http://news.zgxxyp.cn/<br />http://item.zgxxyp.cn/<br />http://www.sjjdvr.cn/<br />http://news.sjjdvr.cn/<br />http://item.sjjdvr.cn/<br />http://www.sujinkeji.cn/<br />http://news.sujinkeji.cn/<br />http://item.sujinkeji.cn/<br />http://www.zsjxbd.cn/<br />http://news.zsjxbd.cn/<br />http://item.zsjxbd.cn/<br />http://www.yesgas.cn/<br />http://news.yesgas.cn/<br />http://item.yesgas.cn/<br />http://www.quickpass.sh.cn/<br />http://news.quickpass.sh.cn/<br />http://item.quickpass.sh.cn/<br />http://www.jspcrm.cn/<br />http://news.jspcrm.cn/<br />http://item.jspcrm.cn/<br />http://www.yjdwpt.cn/<br />http://news.yjdwpt.cn/<br />http://item.yjdwpt.cn/<br />http://www.henanwulian.cn/<br />http://news.henanwulian.cn/<br />http://item.henanwulian.cn/<br />http://www.hhrshh.cn/<br />http://news.hhrshh.cn/<br />http://item.hhrshh.cn/<br />http://www.gpgold.cn/<br />http://news.gpgold.cn/<br />http://item.gpgold.cn/<br />http://www.jingzhuiyou.cn/<br />http://news.jingzhuiyou.cn/<br />http://item.jingzhuiyou.cn/</p>
</div>
	</div><!-- .entry-content -->

	<footer class= Posted on Categories default

Leave a Reply

Your email address will not be published. Required fields are marked *