Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
别团等shy哥发育
redis
提交
a445aa30
R
redis
项目概览
别团等shy哥发育
/
redis
与 Fork 源项目一致
从无法访问的项目Fork
通知
2
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
R
redis
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
a445aa30
编写于
9月 26, 2013
作者:
A
antirez
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Cluster: master node now uses new protocol to vote.
上级
fb9b76fe
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
37 addition
and
35 deletion
+37
-35
src/cluster.c
src/cluster.c
+34
-35
src/redis.h
src/redis.h
+3
-0
未找到文件。
src/cluster.c
浏览文件 @
a445aa30
...
...
@@ -258,6 +258,7 @@ void clusterInit(void) {
server
.
cluster
->
failover_auth_time
=
0
;
server
.
cluster
->
failover_auth_count
=
0
;
server
.
cluster
->
failover_auth_epoch
=
0
;
server
.
cluster
->
last_vote_epoch
=
0
;
memset
(
server
.
cluster
->
migrating_slots_to
,
0
,
sizeof
(
server
.
cluster
->
migrating_slots_to
));
memset
(
server
.
cluster
->
importing_slots_from
,
0
,
...
...
@@ -396,6 +397,7 @@ clusterNode *createClusterNode(char *nodename, int flags) {
memset
(
node
->
ip
,
0
,
sizeof
(
node
->
ip
));
node
->
port
=
0
;
node
->
fail_reports
=
listCreate
();
node
->
voted_time
=
0
;
listSetFreeMethod
(
node
->
fail_reports
,
zfree
);
return
node
;
}
...
...
@@ -1178,15 +1180,18 @@ int clusterProcessPacket(clusterLink *link) {
}
}
else
if
(
type
==
CLUSTERMSG_TYPE_FAILOVER_AUTH_REQUEST
)
{
if
(
!
sender
)
return
1
;
/* We don't know that node. */
/* If we are not a master, ignore that message at all. */
if
(
!
(
server
.
cluster
->
myself
->
flags
&
REDIS_NODE_MASTER
))
return
0
;
clusterSendFailoverAuthIfNeeded
(
sender
,
hdr
);
}
else
if
(
type
==
CLUSTERMSG_TYPE_FAILOVER_AUTH_ACK
)
{
if
(
!
sender
)
return
1
;
/* We don't know that node. */
/* If this is a master, increment the number of acknowledges
* we received so far. */
if
(
sender
->
flags
&
REDIS_NODE_MASTER
)
/* We consider this vote only if the sender if a master serving
* a non zero number of slots, with the currentEpoch that is equal
* to our currentEpoch. */
if
(
sender
->
flags
&
REDIS_NODE_MASTER
&&
sender
->
numslots
>
0
&&
senderCurrentEpoch
==
server
.
cluster
->
currentEpoch
)
{
server
.
cluster
->
failover_auth_count
++
;
}
}
else
{
redisLog
(
REDIS_WARNING
,
"Received unknown packet type: %d"
,
type
);
}
...
...
@@ -1538,43 +1543,38 @@ void clusterSendFailoverAuth(clusterNode *node, uint64_t reqtime) {
clusterSendMessage
(
node
->
link
,
buf
,
totlen
);
}
/* If we believe 'node' is the "first slave" of it's master, reply with
* a FAILOVER_AUTH_GRANTED packet.
* The 'request' field points to the authorization request packet header, we
* need it in order to copy back the 'time' field in our reply.
*
* To be a first slave the sender must:
* 1) Be a slave.
* 2) Its master should be in FAIL state.
* 3) Ordering all the slaves IDs for its master by run-id, it should be the
* first (the smallest) among the ones not in FAIL / PFAIL state.
*/
/* Vote for the node asking for our vote if there are the conditions. */
void
clusterSendFailoverAuthIfNeeded
(
clusterNode
*
node
,
clusterMsg
*
request
)
{
char
first
[
REDIS_CLUSTER_NAMELEN
];
clusterNode
*
master
=
node
->
slaveof
;
int
j
;
uint64_t
requestEpoch
=
ntohu64
(
request
->
currentEpoch
);
/* IF we are not a master serving at least 1 slot, we don't have the
* right to vote, as the cluster size in Redis Cluster is the number
* of masters serving at least one slot, and quorum is the cluster size + 1 */
if
(
!
(
server
.
cluster
->
myself
->
flags
&
REDIS_NODE_MASTER
))
return
;
if
(
server
.
cluster
->
myself
->
numslots
==
0
)
return
;
/* Request epoch must be >= our currentEpoch. */
if
(
requestEpoch
<
server
.
cluster
->
currentEpoch
)
return
;
/* Node is a slave? Its master is down? */
/* I already voted for this epoch? Return ASAP. */
if
(
server
.
cluster
->
last_vote_epoch
==
server
.
cluster
->
currentEpoch
)
return
;
/* Node must be a slave and its master down. */
if
(
!
(
node
->
flags
&
REDIS_NODE_SLAVE
)
||
master
==
NULL
||
!
(
master
->
flags
&
REDIS_NODE_FAIL
))
return
;
/* Iterate all the master slaves to check what's the first one. */
memset
(
first
,
0xff
,
sizeof
(
first
));
for
(
j
=
0
;
j
<
master
->
numslaves
;
j
++
)
{
clusterNode
*
slave
=
master
->
slaves
[
j
];
if
(
slave
->
flags
&
(
REDIS_NODE_FAIL
|
REDIS_NODE_PFAIL
))
continue
;
if
(
memcmp
(
slave
->
name
,
first
,
sizeof
(
first
))
<
0
)
{
memcpy
(
first
,
slave
->
name
,
sizeof
(
first
));
}
}
/* Is 'node' the first slave? */
if
(
memcmp
(
node
->
name
,
first
,
sizeof
(
first
))
!=
0
)
return
;
/* We did not voted for a slave about this master for two
* times the node timeout. This is not strictly needed for correctness
* of the algorithm but makes the base case more linear. */
if
(
server
.
unixtime
-
node
->
slaveof
->
voted_time
<
server
.
cluster_node_timeout
*
2
)
return
;
/* We can
send the packet
. */
/* We can
vote for this slave
. */
clusterSendFailoverAuth
(
node
,
request
->
time
);
server
.
cluster
->
last_vote_epoch
=
server
.
cluster
->
currentEpoch
;
node
->
slaveof
->
voted_time
=
server
.
unixtime
;
}
/* This function is called if we are a slave node and our master serving
...
...
@@ -1583,8 +1583,7 @@ void clusterSendFailoverAuthIfNeeded(clusterNode *node, clusterMsg *request) {
* The gaol of this function is:
* 1) To check if we are able to perform a failover, is our data updated?
* 2) Try to get elected by masters.
* 3) Check if there is the majority of masters agreeing we should failover.
* 4) Perform the failover informing all the other nodes.
* 3) Perform the failover informing all the other nodes.
*/
void
clusterHandleSlaveFailover
(
void
)
{
time_t
data_age
=
server
.
unixtime
-
server
.
repl_down_since
;
...
...
src/redis.h
浏览文件 @
a445aa30
...
...
@@ -628,6 +628,7 @@ struct clusterNode {
time_t
ping_sent
;
/* Unix time we sent latest ping */
time_t
pong_received
;
/* Unix time we received the pong */
time_t
fail_time
;
/* Unix time when FAIL flag was set */
time_t
voted_time
;
/* Last time we voted for a slave of this master */
char
ip
[
REDIS_IP_STR_LEN
];
/* Latest known IP address of this node */
int
port
;
/* Latest known port of this node */
clusterLink
*
link
;
/* TCP/IP link with this node */
...
...
@@ -650,6 +651,8 @@ typedef struct {
int
failover_auth_count
;
/* Number of votes received so far. */
int
failover_auth_sent
;
/* True if we already asked for votes. */
uint64_t
failover_auth_epoch
;
/* Epoch of the current election. */
/* The followign fields are uesd by masters to take state on elections. */
uint64_t
last_vote_epoch
;
/* Epoch of the last vote granted. */
}
clusterState
;
/* Redis cluster messages header */
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录