Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
17e60710
T
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1187
Star
22018
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看板
提交
17e60710
编写于
3月 15, 2022
作者:
M
Minghao Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
sync index
上级
e3ef9d49
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
121 addition
and
0 deletion
+121
-0
source/libs/sync/inc/syncMessage.h
source/libs/sync/inc/syncMessage.h
+1
-0
source/libs/sync/src/syncAppendEntries.c
source/libs/sync/src/syncAppendEntries.c
+117
-0
source/libs/sync/src/syncMessage.c
source/libs/sync/src/syncMessage.c
+3
-0
未找到文件。
source/libs/sync/inc/syncMessage.h
浏览文件 @
17e60710
...
...
@@ -255,6 +255,7 @@ typedef struct SyncAppendEntries {
SRaftId
srcId
;
SRaftId
destId
;
// private data
SyncTerm
term
;
SyncIndex
prevLogIndex
;
SyncTerm
prevLogTerm
;
SyncIndex
commitIndex
;
...
...
source/libs/sync/src/syncAppendEntries.c
浏览文件 @
17e60710
...
...
@@ -14,6 +14,11 @@
*/
#include "syncAppendEntries.h"
#include "syncInt.h"
#include "syncRaftLog.h"
#include "syncRaftStore.h"
#include "syncUtil.h"
#include "syncVoteMgr.h"
// TLA+ Spec
// HandleAppendEntriesRequest(i, j, m) ==
...
...
@@ -84,5 +89,117 @@ int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg) {
int32_t
ret
=
0
;
syncAppendEntriesLog2
(
"==syncNodeOnAppendEntriesCb=="
,
pMsg
);
if
(
pMsg
->
term
>
ths
->
pRaftStore
->
currentTerm
)
{
syncNodeUpdateTerm
(
ths
,
pMsg
->
term
);
}
assert
(
pMsg
->
term
<=
ths
->
pRaftStore
->
currentTerm
);
if
(
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
)
{
ths
->
leaderCache
=
pMsg
->
srcId
;
syncNodeResetElectTimer
(
ths
);
}
assert
(
pMsg
->
dataLen
>=
0
);
SyncTerm
localPreLogTerm
=
0
;
if
(
pMsg
->
prevLogTerm
>=
SYNC_INDEX_BEGIN
&&
pMsg
->
prevLogTerm
<=
ths
->
pLogStore
->
getLastIndex
(
ths
->
pLogStore
))
{
SSyncRaftEntry
*
pEntry
=
logStoreGetEntry
(
ths
->
pLogStore
,
pMsg
->
prevLogTerm
);
assert
(
pEntry
!=
NULL
);
localPreLogTerm
=
pEntry
->
term
;
syncEntryDestory
(
pEntry
);
}
bool
logOK
=
(
pMsg
->
prevLogIndex
==
SYNC_INDEX_INVALID
)
||
((
pMsg
->
prevLogIndex
>=
SYNC_INDEX_BEGIN
)
&&
(
pMsg
->
prevLogIndex
<=
ths
->
pLogStore
->
getLastIndex
(
ths
->
pLogStore
))
&&
(
pMsg
->
prevLogIndex
==
localPreLogTerm
));
// reject
if
((
pMsg
->
term
<
ths
->
pRaftStore
->
currentTerm
)
||
((
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
)
&&
(
ths
->
state
==
TAOS_SYNC_STATE_FOLLOWER
)
&&
!
logOK
))
{
SyncAppendEntriesReply
*
pReply
=
syncAppendEntriesReplyBuild
();
pReply
->
srcId
=
ths
->
myRaftId
;
pReply
->
destId
=
pMsg
->
srcId
;
pReply
->
term
=
ths
->
pRaftStore
->
currentTerm
;
pReply
->
success
=
false
;
pReply
->
matchIndex
=
SYNC_INDEX_INVALID
;
SRpcMsg
rpcMsg
;
syncAppendEntriesReply2RpcMsg
(
pReply
,
&
rpcMsg
);
syncNodeSendMsgById
(
&
pReply
->
destId
,
ths
,
&
rpcMsg
);
syncAppendEntriesReplyDestroy
(
pReply
);
return
ret
;
}
// return to follower state
if
(
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
&&
ths
->
state
==
TAOS_SYNC_STATE_CANDIDATE
)
{
syncNodeBecomeFollower
(
ths
);
}
// accept request
if
(
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
&&
ths
->
state
==
TAOS_SYNC_STATE_FOLLOWER
&&
logOK
)
{
bool
matchSuccess
=
false
;
if
(
pMsg
->
prevLogIndex
==
SYNC_INDEX_INVALID
&&
ths
->
pLogStore
->
getLastIndex
(
ths
->
pLogStore
)
==
SYNC_INDEX_INVALID
)
{
matchSuccess
=
true
;
}
if
(
pMsg
->
prevLogIndex
>=
SYNC_INDEX_BEGIN
&&
pMsg
->
prevLogIndex
<=
ths
->
pLogStore
->
getLastIndex
(
ths
->
pLogStore
))
{
SSyncRaftEntry
*
pEntry
=
logStoreGetEntry
(
ths
->
pLogStore
,
pMsg
->
prevLogTerm
);
assert
(
pEntry
!=
NULL
);
if
(
pMsg
->
prevLogTerm
==
pEntry
->
term
)
{
matchSuccess
=
true
;
}
syncEntryDestory
(
pEntry
);
}
if
(
matchSuccess
)
{
// delete conflict entries
if
(
ths
->
pLogStore
->
getLastIndex
(
ths
->
pLogStore
)
>
pMsg
->
prevLogIndex
)
{
SyncIndex
fromIndex
=
pMsg
->
prevLogIndex
+
1
;
ths
->
pLogStore
->
truncate
(
ths
->
pLogStore
,
fromIndex
);
}
// append one entry
if
(
pMsg
->
dataLen
>
0
)
{
SSyncRaftEntry
*
pEntry
=
syncEntryDeserialize
(
pMsg
->
data
,
pMsg
->
dataLen
);
ths
->
pLogStore
->
appendEntry
(
ths
->
pLogStore
,
pEntry
);
syncEntryDestory
(
pEntry
);
}
SyncAppendEntriesReply
*
pReply
=
syncAppendEntriesReplyBuild
();
pReply
->
srcId
=
ths
->
myRaftId
;
pReply
->
destId
=
pMsg
->
srcId
;
pReply
->
term
=
ths
->
pRaftStore
->
currentTerm
;
pReply
->
success
=
true
;
pReply
->
matchIndex
=
pMsg
->
prevLogIndex
+
1
;
SRpcMsg
rpcMsg
;
syncAppendEntriesReply2RpcMsg
(
pReply
,
&
rpcMsg
);
syncNodeSendMsgById
(
&
pReply
->
destId
,
ths
,
&
rpcMsg
);
syncAppendEntriesReplyDestroy
(
pReply
);
}
else
{
SyncAppendEntriesReply
*
pReply
=
syncAppendEntriesReplyBuild
();
pReply
->
srcId
=
ths
->
myRaftId
;
pReply
->
destId
=
pMsg
->
srcId
;
pReply
->
term
=
ths
->
pRaftStore
->
currentTerm
;
pReply
->
success
=
false
;
pReply
->
matchIndex
=
SYNC_INDEX_INVALID
;
SRpcMsg
rpcMsg
;
syncAppendEntriesReply2RpcMsg
(
pReply
,
&
rpcMsg
);
syncNodeSendMsgById
(
&
pReply
->
destId
,
ths
,
&
rpcMsg
);
syncAppendEntriesReplyDestroy
(
pReply
);
}
if
(
pMsg
->
commitIndex
>
ths
->
commitIndex
)
{
if
(
pMsg
->
commitIndex
<=
ths
->
pLogStore
->
getLastIndex
(
ths
->
pLogStore
))
{
// commit
ths
->
commitIndex
=
pMsg
->
commitIndex
;
ths
->
pLogStore
->
updateCommitIndex
(
ths
->
pLogStore
,
ths
->
commitIndex
);
}
}
}
return
ret
;
}
source/libs/sync/src/syncMessage.c
浏览文件 @
17e60710
...
...
@@ -1108,6 +1108,9 @@ cJSON* syncAppendEntries2Json(const SyncAppendEntries* pMsg) {
cJSON_AddNumberToObject
(
pDestId
,
"vgId"
,
pMsg
->
destId
.
vgId
);
cJSON_AddItemToObject
(
pRoot
,
"destId"
,
pDestId
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pMsg
->
term
);
cJSON_AddStringToObject
(
pRoot
,
"term"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pMsg
->
prevLogIndex
);
cJSON_AddStringToObject
(
pRoot
,
"pre_log_index"
,
u64buf
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录