目录
https://github.com/redis-io/redis/pull/7398
一、修改源码
通过前面分析,在网络正常的情况下,从库自动failover需要两轮投票的根本原因是,从库认为主库客观下线时,并不广播,无法通知负责投票的存活主库也修改挂掉主库的flgas。各个节点为一个主库所维护的clusterNode->flags是相互独立的。见前面博文:
Redis cluster 从库自动failover需要两轮投票的分析
本文测试目的,是通过修改源码,在从库认为主库客观下线时,也将fail信息广播给其他节点,正常网络情况下可以认为其他节点可以同时收到广播信息,再加上从库延迟投票的机制,就不会再有从库自动failover第一轮投票失败的情况。
删除源码markNodeAsFailingIfNeeded函数的if (nodeIsMaster(myself)) clusterSendFail(node->name)的if判断
void markNodeAsFailingIfNeeded(clusterNode *node) {
int failures;
int needed_quorum = (server.cluster->size / 2) + 1;
if (!nodeTimedOut(node)) return; /* We can reach it. */
if (nodeFailed(node)) return; /* Already FAILing. */
failures = clusterNodeFailureReportsCount(node);
/* Also count myself as a voter if I'm a master. */
if (nodeIsMaster(myself)) failures++;
if (failures < needed_quorum) return; /* No weak agreement from masters. */
serverLog(LL_NOTICE,
"Marking node %.40s as failing (quorum reached).", node->name);
/* Mark the node as failing. */
node->flags &= ~CLUSTER_NODE_PFAIL;
node->flags |= CLUSTER_NODE_FAIL;
node->fail_time = mstime();
/* Broadcast the failing node name to everybody, forcing all the other
* reachable nodes to flag the node as FAIL. */
if (nodeIsMaster(myself)) clusterSendFail(node->name); /*这里导致从库检测到客观下线时不广播*/
clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE|CLUSTER_TODO_SAVE_CONFIG);
}
修改为:
void markNodeAsFailingIfNeeded(clusterNode *node) {
int failures;
int needed_quorum = (server.cluster->size / 2) + 1;
if (!nodeTimedOut(node)) return; /* We can reach it. */
if (nodeFailed(node)) return; /* Already FAILing. */
failures = clusterNodeFailureReportsCount(node);
/* Also count myself as a voter if I'm a master. */
if (nodeIsMaster(myself)) failures++;
if (failures < needed_quorum) return; /* No weak agreement from masters. */
serverLog(LL_NOTICE,
"Marking node %.40s as failing (quorum reached).", node->name);
/* Mark the node as failing. */
node->flags &= ~CLUSTER_NODE_PFAIL;
node->flags |= CLUSTER_NODE_FAIL;
node->fail_time = mstime();
/* Broadcast the failing node name to everybody, forcing all the other
* reachable nodes to flag the node as FAIL. */
clusterSendFail(node->name); /*删掉if判断,让所有节点检测到客观下线时,都进行广播*/
clusterDoBeforeSleep(CLUSTER_TODO_UPDATE_STATE|CLUSTER_TODO_SAVE_CONFIG);
}