Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
797d1324
T
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
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看板
提交
797d1324
编写于
10月 15, 2022
作者:
M
Minghao Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(sync): request vote
上级
4d123c4d
变更
9
隐藏空白更改
内联
并排
Showing
9 changed file
with
210 addition
and
24 deletion
+210
-24
source/libs/sync/inc/syncElection.h
source/libs/sync/inc/syncElection.h
+1
-0
source/libs/sync/inc/syncInt.h
source/libs/sync/inc/syncInt.h
+2
-0
source/libs/sync/inc/syncReplication.h
source/libs/sync/inc/syncReplication.h
+1
-0
source/libs/sync/src/syncElection.c
source/libs/sync/src/syncElection.c
+38
-13
source/libs/sync/src/syncMain.c
source/libs/sync/src/syncMain.c
+25
-8
source/libs/sync/src/syncReplication.c
source/libs/sync/src/syncReplication.c
+21
-0
source/libs/sync/src/syncRequestVote.c
source/libs/sync/src/syncRequestVote.c
+57
-1
source/libs/sync/src/syncRequestVoteReply.c
source/libs/sync/src/syncRequestVoteReply.c
+63
-1
source/libs/sync/src/syncTimeout.c
source/libs/sync/src/syncTimeout.c
+2
-1
未找到文件。
source/libs/sync/inc/syncElection.h
浏览文件 @
797d1324
...
...
@@ -40,6 +40,7 @@ extern "C" {
//
int32_t
syncNodeRequestVotePeers
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodeRequestVotePeersSnapshot
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodeDoRequestVote
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodeElect
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodeRequestVote
(
SSyncNode
*
pSyncNode
,
const
SRaftId
*
destRaftId
,
const
SyncRequestVote
*
pMsg
);
...
...
source/libs/sync/inc/syncInt.h
浏览文件 @
797d1324
...
...
@@ -299,6 +299,8 @@ int32_t syncDoLeaderTransfer(SSyncNode* ths, SRpcMsg* pRpcMsg, SSyncRaftEntry* p
int32_t
syncNodeDynamicQuorum
(
const
SSyncNode
*
pSyncNode
);
bool
syncNodeIsMnode
(
SSyncNode
*
pSyncNode
);
// trace log
void
syncLogSendRequestVote
(
SSyncNode
*
pSyncNode
,
const
SyncRequestVote
*
pMsg
,
const
char
*
s
);
void
syncLogRecvRequestVote
(
SSyncNode
*
pSyncNode
,
const
SyncRequestVote
*
pMsg
,
const
char
*
s
);
...
...
source/libs/sync/inc/syncReplication.h
浏览文件 @
797d1324
...
...
@@ -62,6 +62,7 @@ int32_t syncNodeAppendEntries(SSyncNode* pSyncNode, const SRaftId* destRaftId, c
int32_t
syncNodeAppendEntriesBatch
(
SSyncNode
*
pSyncNode
,
const
SRaftId
*
destRaftId
,
const
SyncAppendEntriesBatch
*
pMsg
);
int32_t
syncNodeHeartbeat
(
SSyncNode
*
pSyncNode
,
const
SRaftId
*
destRaftId
,
const
SyncHeartbeat
*
pMsg
);
int32_t
syncNodeHeartbeatPeers
(
SSyncNode
*
pSyncNode
);
#ifdef __cplusplus
}
...
...
source/libs/sync/src/syncElection.c
浏览文件 @
797d1324
...
...
@@ -70,6 +70,26 @@ int32_t syncNodeRequestVotePeersSnapshot(SSyncNode* pSyncNode) {
return
ret
;
}
int32_t
syncNodeDoRequestVote
(
SSyncNode
*
pSyncNode
)
{
ASSERT
(
pSyncNode
->
state
==
TAOS_SYNC_STATE_CANDIDATE
);
int32_t
ret
=
0
;
for
(
int
i
=
0
;
i
<
pSyncNode
->
peersNum
;
++
i
)
{
SyncRequestVote
*
pMsg
=
syncRequestVoteBuild
(
pSyncNode
->
vgId
);
pMsg
->
srcId
=
pSyncNode
->
myRaftId
;
pMsg
->
destId
=
pSyncNode
->
peersId
[
i
];
pMsg
->
term
=
pSyncNode
->
pRaftStore
->
currentTerm
;
ret
=
syncNodeGetLastIndexTerm
(
pSyncNode
,
&
(
pMsg
->
lastLogIndex
),
&
(
pMsg
->
lastLogTerm
));
ASSERT
(
ret
==
0
);
ret
=
syncNodeRequestVote
(
pSyncNode
,
&
pSyncNode
->
peersId
[
i
],
pMsg
);
ASSERT
(
ret
==
0
);
syncRequestVoteDestroy
(
pMsg
);
}
return
ret
;
}
int32_t
syncNodeElect
(
SSyncNode
*
pSyncNode
)
{
syncNodeEventLog
(
pSyncNode
,
"begin election"
);
...
...
@@ -98,20 +118,25 @@ int32_t syncNodeElect(SSyncNode* pSyncNode) {
return
ret
;
}
switch
(
pSyncNode
->
pRaftCfg
->
snapshotStrategy
)
{
case
SYNC_STRATEGY_NO_SNAPSHOT
:
ret
=
syncNodeRequestVotePeers
(
pSyncNode
);
break
;
case
SYNC_STRATEGY_STANDARD_SNAPSHOT
:
case
SYNC_STRATEGY_WAL_FIRST
:
ret
=
syncNodeRequestVotePeersSnapshot
(
pSyncNode
);
break
;
default:
ret
=
syncNodeRequestVotePeers
(
pSyncNode
);
break
;
if
(
syncNodeIsMnode
(
pSyncNode
))
{
switch
(
pSyncNode
->
pRaftCfg
->
snapshotStrategy
)
{
case
SYNC_STRATEGY_NO_SNAPSHOT
:
ret
=
syncNodeRequestVotePeers
(
pSyncNode
);
break
;
case
SYNC_STRATEGY_STANDARD_SNAPSHOT
:
case
SYNC_STRATEGY_WAL_FIRST
:
ret
=
syncNodeRequestVotePeersSnapshot
(
pSyncNode
);
break
;
default:
ret
=
syncNodeRequestVotePeers
(
pSyncNode
);
break
;
}
}
else
{
ret
=
syncNodeDoRequestVote
(
pSyncNode
);
}
ASSERT
(
ret
==
0
);
syncNodeResetElectTimer
(
pSyncNode
);
...
...
source/libs/sync/src/syncMain.c
浏览文件 @
797d1324
...
...
@@ -1262,6 +1262,7 @@ void syncNodeStart(SSyncNode* pSyncNode) {
// Raft 3.6.2 Committing entries from previous terms
syncNodeAppendNoop
(
pSyncNode
);
syncMaybeAdvanceCommitIndex
(
pSyncNode
);
}
else
{
syncNodeBecomeFollower
(
pSyncNode
,
"first start"
);
}
...
...
@@ -1491,6 +1492,14 @@ static int32_t syncNodeDoStartHeartbeatTimer(SSyncNode* pSyncNode) {
int32_t
syncNodeStartHeartbeatTimer
(
SSyncNode
*
pSyncNode
)
{
pSyncNode
->
heartbeatTimerMS
=
pSyncNode
->
hbBaseLine
;
int32_t
ret
=
syncNodeDoStartHeartbeatTimer
(
pSyncNode
);
do
{
for
(
int
i
=
0
;
i
<
pSyncNode
->
peersNum
;
++
i
)
{
SSyncTimer
*
pSyncTimer
=
syncNodeGetHbTimer
(
pSyncNode
,
&
(
pSyncNode
->
peersId
[
i
]));
syncHbTimerStart
(
pSyncNode
,
pSyncTimer
);
}
}
while
(
0
);
return
ret
;
}
...
...
@@ -1514,6 +1523,13 @@ int32_t syncNodeStopHeartbeatTimer(SSyncNode* pSyncNode) {
sTrace
(
"vgId:%d, sync %s stop heartbeat timer"
,
pSyncNode
->
vgId
,
syncUtilState2String
(
pSyncNode
->
state
));
do
{
for
(
int
i
=
0
;
i
<
pSyncNode
->
peersNum
;
++
i
)
{
SSyncTimer
*
pSyncTimer
=
syncNodeGetHbTimer
(
pSyncNode
,
&
(
pSyncNode
->
peersId
[
i
]));
syncHbTimerStop
(
pSyncNode
,
pSyncTimer
);
}
}
while
(
0
);
return
ret
;
}
...
...
@@ -2177,10 +2193,6 @@ void syncNodeBecomeFollower(SSyncNode* pSyncNode, const char* debugStr) {
// state change
pSyncNode
->
state
=
TAOS_SYNC_STATE_FOLLOWER
;
syncNodeStopHeartbeatTimer
(
pSyncNode
);
for
(
int
i
=
0
;
i
<
pSyncNode
->
peersNum
;
++
i
)
{
SSyncTimer
*
pSyncTimer
=
syncNodeGetHbTimer
(
pSyncNode
,
&
(
pSyncNode
->
peersId
[
i
]));
syncHbTimerStop
(
pSyncNode
,
pSyncTimer
);
}
// reset elect timer
syncNodeResetElectTimer
(
pSyncNode
);
...
...
@@ -2280,10 +2292,9 @@ void syncNodeBecomeLeader(SSyncNode* pSyncNode, const char* debugStr) {
// start heartbeat timer
syncNodeStartHeartbeatTimer
(
pSyncNode
);
for
(
int
i
=
0
;
i
<
pSyncNode
->
peersNum
;
++
i
)
{
SSyncTimer
*
pSyncTimer
=
syncNodeGetHbTimer
(
pSyncNode
,
&
(
pSyncNode
->
peersId
[
i
]));
syncHbTimerStart
(
pSyncNode
,
pSyncTimer
);
}
// send heartbeat right now
syncNodeHeartbeatPeers
(
pSyncNode
);
// call back
if
(
pSyncNode
->
pFsm
!=
NULL
&&
pSyncNode
->
pFsm
->
FpBecomeLeaderCb
!=
NULL
)
{
...
...
@@ -2318,6 +2329,8 @@ void syncNodeCandidate2Leader(SSyncNode* pSyncNode) {
syncMaybeAdvanceCommitIndex
(
pSyncNode
);
}
bool
syncNodeIsMnode
(
SSyncNode
*
pSyncNode
)
{
return
(
pSyncNode
->
vgId
==
1
);
}
void
syncNodeFollower2Candidate
(
SSyncNode
*
pSyncNode
)
{
ASSERT
(
pSyncNode
->
state
==
TAOS_SYNC_STATE_FOLLOWER
);
pSyncNode
->
state
=
TAOS_SYNC_STATE_CANDIDATE
;
...
...
@@ -2817,6 +2830,10 @@ int32_t syncNodeOnHeartbeat(SSyncNode* ths, SyncHeartbeat* pMsg) {
SRpcMsg
rpcMsg
;
syncHeartbeatReply2RpcMsg
(
pMsgReply
,
&
rpcMsg
);
if
(
pMsg
->
term
>=
ths
->
pRaftStore
->
currentTerm
&&
ths
->
state
!=
TAOS_SYNC_STATE_FOLLOWER
)
{
syncNodeBecomeFollower
(
ths
,
"become follower by hb"
);
}
if
(
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
)
{
sInfo
(
"vgId:%d, heartbeat reset timer"
,
1
);
syncNodeResetElectTimer
(
ths
);
...
...
source/libs/sync/src/syncReplication.c
浏览文件 @
797d1324
...
...
@@ -506,4 +506,25 @@ int32_t syncNodeHeartbeat(SSyncNode* pSyncNode, const SRaftId* destRaftId, const
syncHeartbeat2RpcMsg
(
pMsg
,
&
rpcMsg
);
syncNodeSendMsgById
(
&
(
pMsg
->
destId
),
pSyncNode
,
&
rpcMsg
);
return
ret
;
}
int32_t
syncNodeHeartbeatPeers
(
SSyncNode
*
pSyncNode
)
{
for
(
int32_t
i
=
0
;
i
<
pSyncNode
->
peersNum
;
++
i
)
{
SyncHeartbeat
*
pSyncMsg
=
syncHeartbeatBuild
(
pSyncNode
->
vgId
);
pSyncMsg
->
srcId
=
pSyncNode
->
myRaftId
;
pSyncMsg
->
destId
=
pSyncNode
->
peersId
[
i
];
pSyncMsg
->
term
=
pSyncNode
->
pRaftStore
->
currentTerm
;
pSyncMsg
->
commitIndex
=
pSyncNode
->
commitIndex
;
pSyncMsg
->
privateTerm
=
0
;
SRpcMsg
rpcMsg
;
syncHeartbeat2RpcMsg
(
pSyncMsg
,
&
rpcMsg
);
// send msg
syncNodeHeartbeat
(
pSyncNode
,
&
(
pSyncMsg
->
destId
),
pSyncMsg
);
syncHeartbeatDestroy
(
pSyncMsg
);
}
return
0
;
}
\ No newline at end of file
source/libs/sync/src/syncRequestVote.c
浏览文件 @
797d1324
...
...
@@ -215,4 +215,60 @@ int32_t syncNodeOnRequestVoteSnapshotCb(SSyncNode* ths, SyncRequestVote* pMsg) {
return
0
;
}
int32_t
syncNodeOnRequestVote
(
SSyncNode
*
ths
,
SyncRequestVote
*
pMsg
)
{
return
0
;
}
\ No newline at end of file
int32_t
syncNodeOnRequestVote
(
SSyncNode
*
ths
,
SyncRequestVote
*
pMsg
)
{
int32_t
ret
=
0
;
// if already drop replica, do not process
if
(
!
syncNodeInRaftGroup
(
ths
,
&
(
pMsg
->
srcId
))
&&
!
ths
->
pRaftCfg
->
isStandBy
)
{
syncLogRecvRequestVote
(
ths
,
pMsg
,
"maybe replica already dropped"
);
return
-
1
;
}
bool
logOK
=
syncNodeOnRequestVoteLogOK
(
ths
,
pMsg
);
// maybe update term
if
(
pMsg
->
term
>
ths
->
pRaftStore
->
currentTerm
)
{
syncNodeUpdateTerm
(
ths
,
pMsg
->
term
);
#if 0
if (logOK) {
syncNodeUpdateTerm(ths, pMsg->term);
} else {
syncNodeUpdateTermWithoutStepDown(ths, pMsg->term);
}
#endif
}
ASSERT
(
pMsg
->
term
<=
ths
->
pRaftStore
->
currentTerm
);
bool
grant
=
(
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
)
&&
logOK
&&
((
!
raftStoreHasVoted
(
ths
->
pRaftStore
))
||
(
syncUtilSameId
(
&
(
ths
->
pRaftStore
->
voteFor
),
&
(
pMsg
->
srcId
))));
if
(
grant
)
{
// maybe has already voted for pMsg->srcId
// vote again, no harm
raftStoreVote
(
ths
->
pRaftStore
,
&
(
pMsg
->
srcId
));
// forbid elect for this round
syncNodeResetElectTimer
(
ths
);
}
// send msg
SyncRequestVoteReply
*
pReply
=
syncRequestVoteReplyBuild
(
ths
->
vgId
);
pReply
->
srcId
=
ths
->
myRaftId
;
pReply
->
destId
=
pMsg
->
srcId
;
pReply
->
term
=
ths
->
pRaftStore
->
currentTerm
;
pReply
->
voteGranted
=
grant
;
// trace log
do
{
char
logBuf
[
32
];
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"grant:%d"
,
pReply
->
voteGranted
);
syncLogRecvRequestVote
(
ths
,
pMsg
,
logBuf
);
syncLogSendRequestVoteReply
(
ths
,
pReply
,
""
);
}
while
(
0
);
SRpcMsg
rpcMsg
;
syncRequestVoteReply2RpcMsg
(
pReply
,
&
rpcMsg
);
syncNodeSendMsgById
(
&
pReply
->
destId
,
ths
,
&
rpcMsg
);
syncRequestVoteReplyDestroy
(
pReply
);
return
0
;
}
\ No newline at end of file
source/libs/sync/src/syncRequestVoteReply.c
浏览文件 @
797d1324
...
...
@@ -157,4 +157,66 @@ int32_t syncNodeOnRequestVoteReplySnapshotCb(SSyncNode* ths, SyncRequestVoteRepl
return
0
;
}
int32_t
syncNodeOnRequestVoteReply
(
SSyncNode
*
ths
,
SyncRequestVoteReply
*
pMsg
)
{
return
0
;
}
\ No newline at end of file
int32_t
syncNodeOnRequestVoteReply
(
SSyncNode
*
ths
,
SyncRequestVoteReply
*
pMsg
)
{
int32_t
ret
=
0
;
// if already drop replica, do not process
if
(
!
syncNodeInRaftGroup
(
ths
,
&
(
pMsg
->
srcId
))
&&
!
ths
->
pRaftCfg
->
isStandBy
)
{
syncLogRecvRequestVoteReply
(
ths
,
pMsg
,
"maybe replica already dropped"
);
return
-
1
;
}
// drop stale response
if
(
pMsg
->
term
<
ths
->
pRaftStore
->
currentTerm
)
{
syncLogRecvRequestVoteReply
(
ths
,
pMsg
,
"drop stale response"
);
return
-
1
;
}
// ASSERT(!(pMsg->term > ths->pRaftStore->currentTerm));
// no need this code, because if I receive reply.term, then I must have sent for that term.
// if (pMsg->term > ths->pRaftStore->currentTerm) {
// syncNodeUpdateTerm(ths, pMsg->term);
// }
if
(
pMsg
->
term
>
ths
->
pRaftStore
->
currentTerm
)
{
syncLogRecvRequestVoteReply
(
ths
,
pMsg
,
"error term"
);
return
-
1
;
}
syncLogRecvRequestVoteReply
(
ths
,
pMsg
,
""
);
ASSERT
(
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
);
// This tallies votes even when the current state is not Candidate,
// but they won't be looked at, so it doesn't matter.
if
(
ths
->
state
==
TAOS_SYNC_STATE_CANDIDATE
)
{
if
(
ths
->
pVotesRespond
->
term
!=
pMsg
->
term
)
{
char
logBuf
[
128
];
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"vote respond error vote-respond-mgr term:%lu, msg term:lu"
,
ths
->
pVotesRespond
->
term
,
pMsg
->
term
);
syncNodeErrorLog
(
ths
,
logBuf
);
return
-
1
;
}
votesRespondAdd
(
ths
->
pVotesRespond
,
pMsg
);
if
(
pMsg
->
voteGranted
)
{
// add vote
voteGrantedVote
(
ths
->
pVotesGranted
,
pMsg
);
// maybe to leader
if
(
voteGrantedMajority
(
ths
->
pVotesGranted
))
{
if
(
!
ths
->
pVotesGranted
->
toLeader
)
{
syncNodeCandidate2Leader
(
ths
);
// prevent to leader again!
ths
->
pVotesGranted
->
toLeader
=
true
;
}
}
}
else
{
;
// do nothing
// UNCHANGED <<votesGranted, voterLog>>
}
}
return
0
;
}
\ No newline at end of file
source/libs/sync/src/syncTimeout.c
浏览文件 @
797d1324
...
...
@@ -101,8 +101,9 @@ int32_t syncNodeOnTimeoutCb(SSyncNode* ths, SyncTimeout* pMsg) {
++
(
ths
->
heartbeatTimerCounter
);
sTrace
(
"vgId:%d, sync timer, type:replicate count:%d, heartbeatTimerLogicClockUser:%ld"
,
ths
->
vgId
,
ths
->
heartbeatTimerCounter
,
ths
->
heartbeatTimerLogicClockUser
);
syncNodeReplicate
(
ths
,
true
);
//
syncNodeReplicate(ths, true);
}
}
else
{
sError
(
"vgId:%d, unknown timeout-type:%d"
,
ths
->
vgId
,
pMsg
->
timeoutType
);
}
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录