First from redis. the main () function of C source code starts to initialize the cluster according to whether to set the cluster_enabled parameter in addition to initializing some configurations and environments of the redis Node itself in the initserver function called, as follows:
Initserver // This is the cluster-enabled parameter in redis. conf configuration. If it is set to yes, the cluster mode is displayed. If (server. cluster_enabled) clusterinit ();
Next, let's take a look at how the clusterinit function brings redis into the cluster mode:
Void clusterinit (void) {int saveconf = 0;/* initialize clusterstate in the redisserver structure */server. cluster = zmalloc (sizeof (clusterstate); server. cluster-> myself = NULL; server. cluster-> currentepoch = 0; // the server is in the Fail status. cluster-> state = redis_cluster_fail; // Number of Master nodes server. cluster-> size = 1; server. cluster-> todo_before_sleep = 0; server. cluster-> nodes = dictcreate (& clusternodesdicttype, null); server. cluster-> nodes_black_list = dictcreate (& clusternodesblacklistdicttype, null); server. cluster-> failover_auth_time = 0; server. cluster-> failover_auth_count = 0; server. cluster-> failover_auth_rank = 0; server. cluster-> failover_auth_epoch = 0; server. cluster-> lastvoteepoch = 0; server. cluster-> stats_bus_messages_sent = 0; server. cluster-> stats_bus_messages_received = 0; memset (server. cluster-> slots, 0, sizeof (server. cluster-> slots); clustercloseallslots ();/* lock the cluster config file to make sure every node uses * its own nodes. conf. */If (clusterlockconfig (server. cluster_configfile) = redis_err) Exit (1);/* load or create a new nodes configuration. * // load or create a new node configuration file // if the load fails, create a flags = myself | new master node if (clusterloadconfig (server. cluster_configfile) = redis_err) {/* No configuration found. we will just use the random name provided * by the createclusternode () function. */myself = server. cluster-> myself = createclusternode (null, redis_node_myself | redis_node_master); redislog (redis_notice, "No cluster configuration found, I'm %. 40 s ", myself-> name ); // Add this node to the nodes hash table of the cluster // This nodes maintains a nodename-> node hash table clusteraddnode (myself); saveconf = 1 ;} // The specific configuration file name is specified by the cluster-config-file parameter if (saveconf) clustersaveconfigordie (1);/* we need a listening TCP port for our cluster messaging needs. */server. cfd_count = 0;/* port sanity check II * the other handshake Port Check is triggered too late to stop * us from trying to use a too-high cluster port number. * /// port verification is performed here. The redis client-oriented listening port must be smaller than 55535, so as to avoid the case where the listening port of the // cluster channel is greater than 65535 if (server. port> (65535-redis_cluster_port_incr) {redislog (redis_warning, "redis port number too high. "" cluster communication port is 10,000 port "" numbers higher than your redis port. "" Your redis port number must be "" lower than 55535. "); exit (1) ;}// open the non-blocking listening port of the cluster channel if (listentoport (server. port + redis_cluster_port_incr, server. CFD, & server. cfd_count) = redis_err) {exit (1) ;}else {Int J; // create an accept event processor on one or more non-blocking listening sockets // You can select an appropriate event model based on the system platform (for example, epoll on Linux. For details, see the aeapiaddevent source code) // here the clusteraccepthandler function is specified as the accept event processor for (j = 0; j <server. cfd_count; j ++) {If (aecreatefileevent (server. el, server. CFD [J], AE _readable, clusteraccepthandler, null) = AE _err) redispanic ("unrecoverable error creating redis cluster" "file event. ") ;}}/* The slots-> keys map is a sorted set. init it. */server. cluster-> slots_to_keys = zslcreate ();/* set myself-> port to my listening port, we'll just need to discover * the IP address via meet messages. */myself-> Port = server. port; resetmanualfailover ();}
From the source code, we can see that the core of redis cluster Initialization is the loading of nodes. conf and the starting of the cluster bus channel listening service:
1, nodes. conf loading or creation: from the source code, we can see that the node configuration is first loaded (view the clusterloadconfig () function implementation, and do not expand it here). If it does not exist or the file content length is 0, the node configuration is newly created, and the initial creation only contains the configuration information of the node. The nodes. conf configuration example is as follows:
$ Cat nodes. conf
8868592d98d84b7cf5752cc0b97af4ac807d1a12 127.0.0.1: 7007 slave bfc910f924d772fe03d9fe6a19aabd73d5730d26 0 1410882108055 8 connected
F5bdda1518cd3826100a30f5953ed82a5861ed48 127.0.0.1: 7002 slave bfc910f924d772fe03d9fe6a19aabd73d5730d26 0 1410882107151 8 connected
82578e8ec9747e46cbb4b8cc2484c71b9b2c91f4 127.0.0.1: 7001 master-0 1410882106146 2 connected 6461-10922
61dfb1055760d5dcf6519e35435d60dc5b207940 127.0.0.1: 7004 slave 82578e8ec9747e46cbb4b8cc2484c71b9b2c91f4 0 1410882107651 5 connected
6d1ebedad33bb31ffbaa99bad095eef4a5920857 127.0.0.1: 7006 master-0 1410882106648 0 connected
Bfc910f924d772fe03d9fe6a19aabd73d5730d26 127.0.0.1: 7005 master-0 1410882106648 8 connected11923-16383
35e0f6fdadbf81a00a1d6d1841098613e653867b 127.0.0.1: 7003 slave 123ed65d59ff22370f2f09546f109d31207789f6 0 1410882106146 7 connected
123ed65d59ff22370f2f09546f450d31207789f6 127.0.0.1: 7000 myself, master-0 0 7 connected 0-6460 10923-11922
Vars currentepoch 8 lastvoteepoch 8
Two types of information are listed here:
1) configuration information of all nodes in the cluster. The fields are:
0 nodeid: 6d1ebedad33bb31ffbaa99bad095eef4a5920857
IP address of 1 node: Port: 127.0.0.1: 7006
2 flag: some options for identifying a node. The optional value is master | slave | myself | fail? | Handshake | noaddr | noflags, corresponding to clusternode. flags
3. master node ID: if it is a server Load balancer node, the ID of the master node is displayed. Otherwise, the ID-indicates that the node is a master node.
4. Last ping time
5. Last time Pong was received
6. configepoch
7 connection status
8. For the master node, the last field records the slots range to be processed. for migration, the format is [slots-> nodeid, which indicates migrating the slots specified by slots to nodeid;
2) rows starting with vars. Currently, the values of currentepoch and lastvoteepoch are mainly recorded (epoch is analyzed separately below)
This article is from the "quiet Madman" blog, please be sure to keep this source http://quietmadman.blog.51cto.com/3269500/1558289
Implementation of redis cluster-initialization (1)