入口
从cluster_announce_port入手;发现了cluster.c:clusterInit中使用了它;cluster.c:clusterInit在server.c:initServer中被调用。
如何开启redis cluster?
在Redis cluster tutorial中有介绍:
Redis Cluster configuration parameters
We are about to create an example cluster deployment. Before we continue, let's introduce the configuration parameters that Redis Cluster introduces in the
redis.conffile. Some will be obvious, others will be more clear as you continue reading.
- cluster-enabled
: If yes enables Redis Cluster support in a specific Redis instance. Otherwise the instance starts as a stand alone instance as usual.
在config.c中也是根据此配置项来初始化struct redisServer.c:cluster_enabled成员变量的,该成员变量表示是否启动redis cluster;
在server.c:initServer中有如下code:
if (server.cluster_enabled) clusterInit();
显然,只有在配置文件中开启了cluster后,redis server才会在启动的时候执行clusterInit()。
关于如何构建redis cluster,参见Redis系列九:redis集群高可用。
数据结构
server.c:struct clusterState 集群的状态
server.c:struct clusterState就是replicated state machines。
成员变量currentEpoch
server.c:struct clusterNode 集群节点
成员变量configEpoch
对于每个集群中的node而言,其配置是cluster最最关心的,所以给他取名中带有config。
成员变量currentEpoch VS 成员变量configEpoch
在 Redis Cluster Specification中有如下介绍:
- The
currentEpochandconfigEpochfields of the sending node that are used to mount the distributed algorithms used by Redis Cluster (this is explained in detail in the next sections). If the node is a slave theconfigEpochis the last knownconfigEpochof its master.
显然两者的相同点就是:mount the distributed algorithms used by Redis Cluster
在Redis源码解析:27集群(三)主从复制、故障转移中有对currentEpoch和configEpoch有着非常详细的介绍,其实从它们的用途来看,就可以明白为什么currentEpoch置于server.c:struct clusterNode而configEpoch置于server.c:struct clusterState中。