Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
da106e29
T
TDengine
项目概览
taosdata
/
TDengine
大约 1 年 前同步成功
通知
1184
Star
22015
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
da106e29
编写于
11月 04, 2021
作者:
L
lichuang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[TD-10645][raft]<feature>add raft vote message handle
上级
c25d174f
变更
8
隐藏空白更改
内联
并排
Showing
8 changed file
with
101 addition
and
9 deletion
+101
-9
source/libs/sync/inc/raft.h
source/libs/sync/inc/raft.h
+2
-0
source/libs/sync/inc/raft_log.h
source/libs/sync/inc/raft_log.h
+2
-0
source/libs/sync/inc/raft_message.h
source/libs/sync/inc/raft_message.h
+31
-5
source/libs/sync/src/raft.c
source/libs/sync/src/raft.c
+2
-2
source/libs/sync/src/raft_election.c
source/libs/sync/src/raft_election.c
+2
-1
source/libs/sync/src/raft_handle_vote_message.c
source/libs/sync/src/raft_handle_vote_message.c
+57
-0
source/libs/sync/src/raft_handle_vote_resp_message.c
source/libs/sync/src/raft_handle_vote_resp_message.c
+1
-1
source/libs/sync/src/raft_log.c
source/libs/sync/src/raft_log.c
+4
-0
未找到文件。
source/libs/sync/inc/raft.h
浏览文件 @
da106e29
...
...
@@ -38,6 +38,7 @@ typedef struct RaftCandidateState {
}
RaftCandidateState
;
typedef
struct
SSyncRaftIOMethods
{
// send SSyncMessage to node
int
(
*
send
)(
const
SSyncMessage
*
pMsg
,
const
SNodeInfo
*
pNode
);
}
SSyncRaftIOMethods
;
...
...
@@ -104,6 +105,7 @@ struct SSyncRaft {
SSyncRaftIOMethods
io
;
// union different state data
union
{
RaftLeaderState
leaderState
;
RaftCandidateState
candidateState
;
...
...
source/libs/sync/inc/raft_log.h
浏览文件 @
da106e29
...
...
@@ -35,6 +35,8 @@ SyncIndex syncRaftLogLastIndex(SSyncRaftLog* pLog);
SyncTerm
syncRaftLogLastTerm
(
SSyncRaftLog
*
pLog
);
bool
syncRaftLogIsUptodate
(
SSyncRaftLog
*
pLog
,
SyncIndex
index
,
SyncTerm
term
);
int
syncRaftLogNumOfPendingConf
(
SSyncRaftLog
*
pLog
);
bool
syncRaftHasUnappliedLog
(
SSyncRaftLog
*
pLog
);
...
...
source/libs/sync/inc/raft_message.h
浏览文件 @
da106e29
...
...
@@ -20,10 +20,13 @@
#include "sync_type.h"
/**
* below define message type which handled by Raft node thread
* internal message, which communicate in threads, start with RAFT_MSG_INTERNAL_*,
* internal message use pointer only, need not to be decode/encode
* outter message start with RAFT_MSG_*, need to implement its decode/encode functions
* below define message type which handled by Raft.
*
* internal message, which communicate between threads, start with RAFT_MSG_INTERNAL_*.
* internal message use pointer only and stack memory, need not to be decode/encode and free.
*
* outter message start with RAFT_MSG_*, which communicate between cluster peers,
* need to implement its decode/encode functions.
**/
typedef
enum
RaftMessageType
{
// client propose a cmd
...
...
@@ -36,6 +39,7 @@ typedef enum RaftMessageType {
RAFT_MSG_VOTE_RESP
=
4
,
RAFT_MSG_APPEND
=
5
,
RAFT_MSG_APPEND_RESP
=
6
,
}
RaftMessageType
;
typedef
struct
RaftMsgInternal_Prop
{
...
...
@@ -55,7 +59,7 @@ typedef struct RaftMsg_Vote {
}
RaftMsg_Vote
;
typedef
struct
RaftMsg_VoteResp
{
bool
reject
;
bool
reject
ed
;
SyncRaftElectionType
cType
;
}
RaftMsg_VoteResp
;
...
...
@@ -115,6 +119,7 @@ static FORCE_INLINE SSyncMessage* syncNewVoteMsg(SyncGroupId groupId, SyncNodeId
.
from
=
from
,
.
to
=
to
,
.
term
=
term
,
.
msgType
=
RAFT_MSG_VOTE
,
.
vote
=
(
RaftMsg_Vote
)
{
.
cType
=
cType
,
.
lastIndex
=
lastIndex
,
...
...
@@ -125,6 +130,26 @@ static FORCE_INLINE SSyncMessage* syncNewVoteMsg(SyncGroupId groupId, SyncNodeId
return
pMsg
;
}
static
FORCE_INLINE
SSyncMessage
*
syncNewVoteRespMsg
(
SyncGroupId
groupId
,
SyncNodeId
from
,
SyncNodeId
to
,
SyncRaftElectionType
cType
,
bool
rejected
)
{
SSyncMessage
*
pMsg
=
(
SSyncMessage
*
)
malloc
(
sizeof
(
SSyncMessage
));
if
(
pMsg
==
NULL
)
{
return
NULL
;
}
*
pMsg
=
(
SSyncMessage
)
{
.
groupId
=
groupId
,
.
from
=
from
,
.
to
=
to
,
.
msgType
=
RAFT_MSG_VOTE_RESP
,
.
voteResp
=
(
RaftMsg_VoteResp
)
{
.
cType
=
cType
,
.
rejected
=
rejected
,
},
};
return
pMsg
;
}
static
FORCE_INLINE
bool
syncIsInternalMsg
(
RaftMessageType
msgType
)
{
return
msgType
==
RAFT_MSG_INTERNAL_PROP
||
msgType
==
RAFT_MSG_INTERNAL_ELECTION
;
...
...
@@ -142,6 +167,7 @@ void syncFreeMessage(const SSyncMessage* pMsg);
// message handlers
int
syncRaftHandleElectionMessage
(
SSyncRaft
*
pRaft
,
const
SSyncMessage
*
pMsg
);
int
syncRaftHandleVoteMessage
(
SSyncRaft
*
pRaft
,
const
SSyncMessage
*
pMsg
);
int
syncRaftHandleVoteRespMessage
(
SSyncRaft
*
pRaft
,
const
SSyncMessage
*
pMsg
);
#endif
/* _TD_LIBS_SYNC_RAFT_MESSAGE_H */
\ No newline at end of file
source/libs/sync/src/raft.c
浏览文件 @
da106e29
...
...
@@ -107,7 +107,7 @@ int32_t syncRaftStep(SSyncRaft* pRaft, const SSyncMessage* pMsg) {
if
(
msgType
==
RAFT_MSG_INTERNAL_ELECTION
)
{
syncRaftHandleElectionMessage
(
pRaft
,
pMsg
);
}
else
if
(
msgType
==
RAFT_MSG_VOTE
)
{
syncRaftHandleVoteMessage
(
pRaft
,
pMsg
);
}
else
{
pRaft
->
stepFp
(
pRaft
,
pMsg
);
}
...
...
@@ -245,7 +245,7 @@ static bool preHandleNewTermMessage(SSyncRaft* pRaft, const SSyncMessage* pMsg)
if
(
syncIsPreVoteMsg
(
pMsg
))
{
// Never change our term in response to a PreVote
}
else
if
(
syncIsPreVoteRespMsg
(
pMsg
)
&&
!
pMsg
->
voteResp
.
reject
)
{
}
else
if
(
syncIsPreVoteRespMsg
(
pMsg
)
&&
!
pMsg
->
voteResp
.
reject
ed
)
{
/**
* We send pre-vote requests with a term in our future. If the
* pre-vote is granted, we will increment our term when we get a
...
...
source/libs/sync/src/raft_election.c
浏览文件 @
da106e29
...
...
@@ -15,6 +15,7 @@
#include "syncInt.h"
#include "raft.h"
#include "raft_log.h"
#include "raft_message.h"
void
syncRaftStartElection
(
SSyncRaft
*
pRaft
,
SyncRaftElectionType
cType
)
{
...
...
@@ -66,7 +67,7 @@ void syncRaftStartElection(SSyncRaft* pRaft, SyncRaftElectionType cType) {
continue
;
}
syncInfo
(
"[%d:%d] [logterm: %"
PRId64
", index: %
d
] sent %d request to %d at term %"
PRId64
""
,
syncInfo
(
"[%d:%d] [logterm: %"
PRId64
", index: %
"
PRId64
"
] sent %d request to %d at term %"
PRId64
""
,
pRaft
->
selfGroupId
,
pRaft
->
selfId
,
lastTerm
,
lastIndex
,
voteMsgType
,
nodeId
,
pRaft
->
term
);
...
...
source/libs/sync/src/raft_handle_vote_message.c
0 → 100644
浏览文件 @
da106e29
/*
* Copyright (c) 2019 TAOS Data, Inc. <cli@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "syncInt.h"
#include "raft.h"
#include "raft_log.h"
#include "raft_message.h"
static
bool
canGrantVoteMessage
(
SSyncRaft
*
pRaft
,
const
SSyncMessage
*
pMsg
);
int
syncRaftHandleVoteMessage
(
SSyncRaft
*
pRaft
,
const
SSyncMessage
*
pMsg
)
{
SSyncMessage
*
pRespMsg
;
int
voteIndex
=
syncRaftConfigurationIndexOfVoter
(
pRaft
,
pMsg
->
from
);
if
(
voteIndex
==
-
1
)
{
return
0
;
}
bool
grant
;
SyncIndex
lastIndex
=
syncRaftLogLastIndex
(
pRaft
->
log
);
SyncTerm
lastTerm
=
syncRaftLogLastTerm
(
pRaft
->
log
);
grant
=
canGrantVoteMessage
(
pRaft
,
pMsg
);
pRespMsg
=
syncNewVoteRespMsg
(
pRaft
->
selfGroupId
,
pRaft
->
selfId
,
pMsg
->
to
,
pMsg
->
vote
.
cType
,
!
grant
);
if
(
pRespMsg
==
NULL
)
{
return
0
;
}
syncInfo
(
"[%d:%d] [logterm: %"
PRId64
", index: %"
PRId64
", vote: %d] %s for %d"
\
"[logterm: %"
PRId64
", index: %"
PRId64
", vote: %d] at term %"
PRId64
""
,
pRaft
->
selfGroupId
,
pRaft
->
selfId
,
lastTerm
,
lastIndex
,
pRaft
->
voteFor
,
grant
?
"grant"
:
"reject"
,
pMsg
->
from
,
pMsg
->
vote
.
lastTerm
,
pMsg
->
vote
.
lastIndex
,
pRaft
->
term
);
pRaft
->
io
.
send
(
pRespMsg
,
&
(
pRaft
->
cluster
.
nodeInfo
[
voteIndex
]));
return
0
;
}
static
bool
canGrantVoteMessage
(
SSyncRaft
*
pRaft
,
const
SSyncMessage
*
pMsg
)
{
if
(
!
(
pRaft
->
voteFor
==
SYNC_NON_NODE_ID
||
pMsg
->
term
>
pRaft
->
term
||
pRaft
->
voteFor
==
pMsg
->
from
))
{
return
false
;
}
if
(
!
syncRaftLogIsUptodate
(
pRaft
,
pMsg
->
vote
.
lastIndex
,
pMsg
->
vote
.
lastTerm
))
{
return
false
;
}
return
true
;
}
\ No newline at end of file
source/libs/sync/src/raft_handle_vote_resp_message.c
浏览文件 @
da106e29
...
...
@@ -35,7 +35,7 @@ int syncRaftHandleVoteRespMessage(SSyncRaft* pRaft, const SSyncMessage* pMsg) {
granted
=
syncRaftNumOfGranted
(
pRaft
,
pMsg
->
from
,
pMsg
->
voteResp
.
cType
==
SYNC_RAFT_CAMPAIGN_PRE_ELECTION
,
!
pMsg
->
voteResp
.
reject
,
&
rejected
);
!
pMsg
->
voteResp
.
reject
ed
,
&
rejected
);
quorum
=
syncRaftQuorum
(
pRaft
);
syncInfo
(
"[%d:%d] [quorum:%d] has received %d votes and %d vote rejections"
,
...
...
source/libs/sync/src/raft_log.c
浏览文件 @
da106e29
...
...
@@ -27,6 +27,10 @@ SyncTerm syncRaftLogLastTerm(SSyncRaftLog* pLog) {
return
0
;
}
bool
syncRaftLogIsUptodate
(
SSyncRaftLog
*
pLog
,
SyncIndex
index
,
SyncTerm
term
)
{
return
true
;
}
int
syncRaftLogNumOfPendingConf
(
SSyncRaftLog
*
pLog
)
{
return
0
;
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录