当前位置:网站首页>Redis Cluster - the bottom principle of building clusters

Redis Cluster - the bottom principle of building clusters

2022-06-13 07:33:00 A hard-working dog

Redis colony

Redis Clusters are Redis The distributed database solution provided , Clustering is done by fragmentation (sharding) To share data , It also provides replication and failover capabilities .

node

One Redis A cluster usually consists of multiple nodes ( Running in cluster mode Reids The server )

// The work of connecting various nodes can use 
CLUSTER MEET <IP><port>

Send... To a node CLUSTER MEET command , It can make node Node and ip and port Handshake with the specified node (handshake), If the handshake is successful , that node The node will ip and port The specified node is added to node In the cluster .

Data structure of cluster

clusterNode The structure holds the current state of a node : Such as the creation time of the node 、 Node name 、 The current configuration level element of the node 、 Node IP And port numbers, etc .

One for each node clusterNode Structure to keep track of your state , And for all other nodes in the cluster ( Including master node and slave node ) Create a corresponding clusterNode structure , To record the status of other nodes .

struct clusterNode{
	// Time of node creation 
	mstime_t ctime;
  // Node name , from 40 It's made up of 16 hexadecimal characters 
  char name[REDIS_CLUSTER_NAMELEN];
  // Node identification , Use different identity values to record the role of the node , And the current state of the node 
  int flags;
  // The current configuration era for the node , For failover 
  uint64_t configEpoch;
  // Node ip Address 
  char Ip[Redis_ip_str_len];
  // The port number of the node 
  int port;
  // Save the information needed to connect nodes 
  clusterLink *link;
}
struct clusterLink{
 // Time when the connection was created 
 mstime_t ctime;
 //TCP  socket descriptor 
 int fd;
 // Output buffer , Save messages waiting to be sent to other nodes 
 sds sndbuf;
 // Input buffer , Save messages received from other nodes 
 sds rcvbuf;
 // The node associated with this connection , If not, for null
 struct clusternode * node;
}

Each node holds a clusterState structure , This structure records the current status of the current node cluster

typedf struct clusterState{
	// Pointer to the current node 
    clusterNode *myself;
    // The current configuration era for the node , For failover 
  	uint64_t configEpoch;
    // The current state of the cluster : Online or offline 
    int state;
    // The number of nodes in the cluster that handle at least one slot 
    int size;
    // Cluster node list ( Include myself)
    // The key of the dictionary is the name of the node , The value of the dictionary is... Corresponding to the node clusterNode The pointer 
    dict *nodes;
}clusterState

CLUSTER MEET Implementation of commands

Client to node A send out CLUSTER MEET command , Let node A The nodes B Add to cluster , Node receiving command A Will be on node B A handshake , To confirm each other's existence .

CLUSTER MEET <ip><port>

1. node A Will be for the node B Create a clusterNode structure ,, And add the structure to your own clusterState.Nodes In the dictionary 
2. after , node A Based on the CLUSTER MEET  The command is given IP And port number , To the node B Send a MEET news 
3. If all goes well , node B Will receive node A Sent MEET news , node B For the node A Create a ClusterNode structure , And add the structure to your own clusterState.Nodes In the dictionary 
4. after , node B To the node A Return a PONG news 
5. If all goes well , Then the node A At the receiving node B Back to PONG news , Through this article PONG Message node A You can know the node B I have successfully received the one I sent MEET news 
6. after , node A To the node B Return a PING news 
7. If all goes well , node B Will receive node A Back to PING news , Through this article PING Message node B You can know the node A You have successfully received your returned PONG news , Shake hands with success .
8. After the node A The node will be B Information through Gossip The protocol is propagated to other nodes in the cluster , Let other nodes also be associated with nodes B A handshake , Last node B Will be recognized by all nodes in the cluster .

原网站

版权声明
本文为[A hard-working dog]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202270548049479.html