Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
687caac2
TDengine
项目概览
taosdata
/
TDengine
12 个月 前同步成功
通知
1181
Star
22014
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
687caac2
编写于
6月 08, 2022
作者:
M
Minghao Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(sync): add raft log test
上级
f6aed440
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
391 addition
and
241 deletion
+391
-241
source/libs/sync/src/syncAppendEntries.c
source/libs/sync/src/syncAppendEntries.c
+186
-202
source/libs/sync/src/syncMain.c
source/libs/sync/src/syncMain.c
+15
-30
source/libs/sync/src/syncRaftLog.c
source/libs/sync/src/syncRaftLog.c
+22
-8
source/libs/sync/test/syncRaftLogTest2.cpp
source/libs/sync/test/syncRaftLogTest2.cpp
+167
-0
source/libs/sync/test/syncTimeoutTest.cpp
source/libs/sync/test/syncTimeoutTest.cpp
+1
-1
未找到文件。
source/libs/sync/src/syncAppendEntries.c
浏览文件 @
687caac2
...
@@ -434,92 +434,43 @@ int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg) {
...
@@ -434,92 +434,43 @@ int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg) {
return
ret
;
return
ret
;
}
}
static
bool
syncNodeOnAppendEntriesLogOK
(
SSyncNode
*
pSyncNode
,
SyncAppendEntries
*
pMsg
)
{
static
int32_t
syncNodeMakeLogSame
(
SSyncNode
*
ths
,
SyncAppendEntries
*
pMsg
)
{
if
(
pMsg
->
prevLogIndex
==
SYNC_INDEX_INVALID
)
{
return
true
;
}
SyncIndex
myLastIndex
=
syncNodeGetLastIndex
(
pSyncNode
);
if
(
pMsg
->
prevLogIndex
>
myLastIndex
)
{
return
false
;
}
SyncTerm
myPreLogTerm
=
syncNodeGetPreTerm
(
pSyncNode
,
pMsg
->
prevLogIndex
+
1
);
if
(
pMsg
->
prevLogIndex
<=
myLastIndex
&&
pMsg
->
prevLogTerm
==
myPreLogTerm
)
{
return
true
;
}
return
false
;
}
static
int32_t
syncNodeMakeLogSame
(
SSyncNode
*
ths
,
SyncAppendEntries
*
pMsg
,
SSyncRaftEntry
**
ppAppendEntry
,
bool
*
pEntryAlreadyWritten
)
{
int32_t
code
;
int32_t
code
;
*
ppAppendEntry
=
NULL
;
*
pEntryAlreadyWritten
=
false
;
// not conflict by default
bool
conflict
=
false
;
SyncIndex
extraIndex
=
pMsg
->
prevLogIndex
+
1
;
SSyncRaftEntry
*
pExtraEntry
;
code
=
ths
->
pLogStore
->
syncLogGetEntry
(
ths
->
pLogStore
,
extraIndex
,
&
pExtraEntry
);
ASSERT
(
pExtraEntry
!=
NULL
);
*
ppAppendEntry
=
syncEntryDeserialize
(
pMsg
->
data
,
pMsg
->
dataLen
);
ASSERT
(
*
ppAppendEntry
!=
NULL
);
ASSERT
(
extraIndex
==
(
*
ppAppendEntry
)
->
index
);
if
(
pExtraEntry
->
term
!=
(
*
ppAppendEntry
)
->
term
)
{
// log not match, conflict, need delete
conflict
=
true
;
}
else
{
// log match, already written
ASSERT
(
extraIndex
==
(
*
ppAppendEntry
)
->
index
&&
pExtraEntry
->
term
==
(
*
ppAppendEntry
)
->
term
);
*
pEntryAlreadyWritten
=
true
;
sInfo
(
"entry already written, term:%lu, index:%ld"
,
pExtraEntry
->
term
,
pExtraEntry
->
index
);
}
syncEntryDestory
(
pExtraEntry
);
if
(
conflict
)
{
// roll back
SyncIndex
delBegin
=
ths
->
pLogStore
->
syncLogLastIndex
(
ths
->
pLogStore
);
SyncIndex
delEnd
=
extraIndex
;
sTrace
(
"entry conflict:%d, delBegin:%ld, delEnd:%ld"
,
conflict
,
delBegin
,
delEnd
);
// notice! reverse roll back!
for
(
SyncIndex
index
=
delEnd
;
index
>=
delBegin
;
--
index
)
{
if
(
ths
->
pFsm
->
FpRollBackCb
!=
NULL
)
{
SSyncRaftEntry
*
pRollBackEntry
;
code
=
ths
->
pLogStore
->
syncLogGetEntry
(
ths
->
pLogStore
,
index
,
&
pRollBackEntry
);
ASSERT
(
code
==
0
);
ASSERT
(
pRollBackEntry
!=
NULL
);
if
(
syncUtilUserRollback
(
pRollBackEntry
->
msgType
))
{
SyncIndex
delBegin
=
pMsg
->
prevLogIndex
+
1
;
SRpcMsg
rpcMsg
;
SyncIndex
delEnd
=
ths
->
pLogStore
->
syncLogLastIndex
(
ths
->
pLogStore
);
syncEntry2OriginalRpc
(
pRollBackEntry
,
&
rpcMsg
);
SFsmCbMeta
cbMeta
;
// invert roll back!
cbMeta
.
index
=
pRollBackEntry
->
index
;
for
(
SyncIndex
index
=
delEnd
;
index
>=
delBegin
;
--
index
)
{
cbMeta
.
isWeak
=
pRollBackEntry
->
isWeak
;
if
(
ths
->
pFsm
->
FpRollBackCb
!=
NULL
)
{
cbMeta
.
code
=
0
;
SSyncRaftEntry
*
pRollBackEntry
;
cbMeta
.
state
=
ths
->
state
;
code
=
ths
->
pLogStore
->
syncLogGetEntry
(
ths
->
pLogStore
,
index
,
&
pRollBackEntry
);
cbMeta
.
seqNum
=
pRollBackEntry
->
seqNum
;
ASSERT
(
code
==
0
);
ths
->
pFsm
->
FpRollBackCb
(
ths
->
pFsm
,
&
rpcMsg
,
cbMeta
);
ASSERT
(
pRollBackEntry
!=
NULL
);
rpcFreeCont
(
rpcMsg
.
pCont
);
}
syncEntryDestory
(
pRollBackEntry
);
if
(
syncUtilUserRollback
(
pRollBackEntry
->
msgType
))
{
SRpcMsg
rpcMsg
;
syncEntry2OriginalRpc
(
pRollBackEntry
,
&
rpcMsg
);
SFsmCbMeta
cbMeta
;
cbMeta
.
index
=
pRollBackEntry
->
index
;
cbMeta
.
isWeak
=
pRollBackEntry
->
isWeak
;
cbMeta
.
code
=
0
;
cbMeta
.
state
=
ths
->
state
;
cbMeta
.
seqNum
=
pRollBackEntry
->
seqNum
;
ths
->
pFsm
->
FpRollBackCb
(
ths
->
pFsm
,
&
rpcMsg
,
cbMeta
);
rpcFreeCont
(
rpcMsg
.
pCont
);
}
}
}
// delete confict entries
syncEntryDestory
(
pRollBackEntry
);
code
=
ths
->
pLogStore
->
syncLogTruncate
(
ths
->
pLogStore
,
extraIndex
);
}
ASSERT
(
code
==
0
);
}
}
return
0
;
// delete confict entries
code
=
ths
->
pLogStore
->
syncLogTruncate
(
ths
->
pLogStore
,
delBegin
);
ASSERT
(
code
==
0
);
return
code
;
}
}
static
int32_t
syncNodePreCommit
(
SSyncNode
*
ths
,
SSyncRaftEntry
*
pEntry
)
{
static
int32_t
syncNodePreCommit
(
SSyncNode
*
ths
,
SSyncRaftEntry
*
pEntry
)
{
...
@@ -540,6 +491,26 @@ static int32_t syncNodePreCommit(SSyncNode* ths, SSyncRaftEntry* pEntry) {
...
@@ -540,6 +491,26 @@ static int32_t syncNodePreCommit(SSyncNode* ths, SSyncRaftEntry* pEntry) {
return
0
;
return
0
;
}
}
// really pre log match
// prevLogIndex == -1
static
bool
syncNodeOnAppendEntriesLogOK
(
SSyncNode
*
pSyncNode
,
SyncAppendEntries
*
pMsg
)
{
if
(
pMsg
->
prevLogIndex
==
SYNC_INDEX_INVALID
)
{
return
true
;
}
SyncIndex
myLastIndex
=
syncNodeGetLastIndex
(
pSyncNode
);
if
(
pMsg
->
prevLogIndex
>
myLastIndex
)
{
return
false
;
}
SyncTerm
myPreLogTerm
=
syncNodeGetPreTerm
(
pSyncNode
,
pMsg
->
prevLogIndex
+
1
);
if
(
pMsg
->
prevLogIndex
<=
myLastIndex
&&
pMsg
->
prevLogTerm
==
myPreLogTerm
)
{
return
true
;
}
return
false
;
}
int32_t
syncNodeOnAppendEntriesSnapshotCb
(
SSyncNode
*
ths
,
SyncAppendEntries
*
pMsg
)
{
int32_t
syncNodeOnAppendEntriesSnapshotCb
(
SSyncNode
*
ths
,
SyncAppendEntries
*
pMsg
)
{
int32_t
ret
=
0
;
int32_t
ret
=
0
;
int32_t
code
=
0
;
int32_t
code
=
0
;
...
@@ -549,7 +520,7 @@ int32_t syncNodeOnAppendEntriesSnapshotCb(SSyncNode* ths, SyncAppendEntries* pMs
...
@@ -549,7 +520,7 @@ int32_t syncNodeOnAppendEntriesSnapshotCb(SSyncNode* ths, SyncAppendEntries* pMs
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"recv SyncAppendEntries, term:%lu"
,
ths
->
pRaftStore
->
currentTerm
);
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"recv SyncAppendEntries, term:%lu"
,
ths
->
pRaftStore
->
currentTerm
);
syncAppendEntriesLog2
(
logBuf
,
pMsg
);
syncAppendEntriesLog2
(
logBuf
,
pMsg
);
// if I am standby, be added into a raft group, I should process SyncAppendEntries msg
// if I am standby,
to
be added into a raft group, I should process SyncAppendEntries msg
/*
/*
// if already drop replica, do not process
// if already drop replica, do not process
if (!syncNodeInRaftGroup(ths, &(pMsg->srcId))) {
if (!syncNodeInRaftGroup(ths, &(pMsg->srcId))) {
...
@@ -573,57 +544,55 @@ int32_t syncNodeOnAppendEntriesSnapshotCb(SSyncNode* ths, SyncAppendEntries* pMs
...
@@ -573,57 +544,55 @@ int32_t syncNodeOnAppendEntriesSnapshotCb(SSyncNode* ths, SyncAppendEntries* pMs
bool
logOK
=
syncNodeOnAppendEntriesLogOK
(
ths
,
pMsg
);
bool
logOK
=
syncNodeOnAppendEntriesLogOK
(
ths
,
pMsg
);
// case1, reject request
// candidate to follower
if
((
pMsg
->
term
<
ths
->
pRaftStore
->
currentTerm
)
||
//
((
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
)
&&
(
ths
->
state
==
TAOS_SYNC_STATE_FOLLOWER
)
&&
!
logOK
))
{
// operation:
sTrace
(
"recv SyncAppendEntries, reject, receive_term:%lu, current_term:%lu, ths->state:%d, logOK:%d"
,
pMsg
->
term
,
// to follower
ths
->
pRaftStore
->
currentTerm
,
ths
->
state
,
logOK
);
do
{
bool
condition
=
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
&&
ths
->
state
==
TAOS_SYNC_STATE_CANDIDATE
;
// send response
if
(
condition
)
{
SyncAppendEntriesReply
*
pReply
=
syncAppendEntriesReplyBuild
(
ths
->
vgId
);
syncNodeBecomeFollower
(
ths
);
pReply
->
srcId
=
ths
->
myRaftId
;
// do not reply?
pReply
->
destId
=
pMsg
->
srcId
;
return
ret
;
pReply
->
term
=
ths
->
pRaftStore
->
currentTerm
;
}
pReply
->
success
=
false
;
}
while
(
0
);
pReply
->
matchIndex
=
SYNC_INDEX_INVALID
;
pReply
->
privateTerm
=
ths
->
pNewNodeReceiver
->
privateTerm
;
// fake match
//
SRpcMsg
rpcMsg
;
// condition1:
syncAppendEntriesReply2RpcMsg
(
pReply
,
&
rpcMsg
);
// I have snapshot, no log, preIndex > myLastIndex
syncNodeSendMsgById
(
&
pReply
->
destId
,
ths
,
&
rpcMsg
);
//
syncAppendEntriesReplyDestroy
(
pReply
);
// condition2:
// I have snapshot, have log, log <= snapshot, preIndex > myLastIndex
return
ret
;
//
}
// condition3:
// I have snapshot, preIndex <= snapshot.lastApplyIndex
// case 2, return to follower state
//
if
(
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
&&
ths
->
state
==
TAOS_SYNC_STATE_CANDIDATE
)
{
// operation:
sTrace
(
"recv SyncAppendEntries, return to follower, receive_term:%lu, current_term:%lu, ths->state:%d, logOK:%d"
,
// match snapshot.lastApplyIndex - 1;
pMsg
->
term
,
ths
->
pRaftStore
->
currentTerm
,
ths
->
state
,
logOK
);
// no operation on log
do
{
syncNodeBecomeFollower
(
ths
);
SyncIndex
myLastIndex
=
syncNodeGetLastIndex
(
ths
);
// ret or reply?
return
ret
;
}
// case 3, index in my snapshot
if
(
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
&&
syncNodeHasSnapshot
(
ths
))
{
SSnapshot
snapshot
;
SSnapshot
snapshot
;
ths
->
pFsm
->
FpGetSnapshot
(
ths
->
pFsm
,
&
snapshot
);
ths
->
pFsm
->
FpGetSnapshot
(
ths
->
pFsm
,
&
snapshot
);
if
(
pMsg
->
prevLogIndex
<
snapshot
.
lastApplyIndex
)
{
sTrace
(
"recv SyncAppendEntries, accept, in snapshot, receive_term:%lu, current_term:%lu, ths->state:%d, logOK:%d, "
"snapshot.lastApplyIndex:%ld, snapshot.lastApplyTerm:%lu"
,
pMsg
->
term
,
ths
->
pRaftStore
->
currentTerm
,
ths
->
state
,
logOK
,
snapshot
.
lastApplyIndex
,
snapshot
.
lastApplyTerm
);
bool
condition0
=
(
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
)
&&
(
ths
->
state
==
TAOS_SYNC_STATE_FOLLOWER
)
&&
syncNodeHasSnapshot
(
ths
);
bool
condition1
=
condition0
&&
(
ths
->
pLogStore
->
syncLogEntryCount
(
ths
->
pLogStore
)
==
0
)
&&
(
pMsg
->
prevLogIndex
>
myLastIndex
);
bool
condition2
=
condition0
&&
(
ths
->
pLogStore
->
syncLogLastIndex
(
ths
->
pLogStore
)
<=
snapshot
.
lastApplyIndex
)
&&
(
pMsg
->
prevLogIndex
>
myLastIndex
);
bool
condition3
=
condition0
&&
(
pMsg
->
prevLogIndex
<=
snapshot
.
lastApplyIndex
);
bool
condition
=
condition1
||
condition2
||
condition3
;
if
(
condition
)
{
// prepare response msg
// prepare response msg
SyncAppendEntriesReply
*
pReply
=
syncAppendEntriesReplyBuild
(
ths
->
vgId
);
SyncAppendEntriesReply
*
pReply
=
syncAppendEntriesReplyBuild
(
ths
->
vgId
);
pReply
->
srcId
=
ths
->
myRaftId
;
pReply
->
srcId
=
ths
->
myRaftId
;
pReply
->
destId
=
pMsg
->
srcId
;
pReply
->
destId
=
pMsg
->
srcId
;
pReply
->
term
=
ths
->
pRaftStore
->
currentTerm
;
pReply
->
term
=
ths
->
pRaftStore
->
currentTerm
;
pReply
->
success
=
true
;
pReply
->
privateTerm
=
ths
->
pNewNodeReceiver
->
privateTerm
;
pReply
->
privateTerm
=
ths
->
pNewNodeReceiver
->
privateTerm
;
pReply
->
success
=
true
;
pReply
->
matchIndex
=
snapshot
.
lastApplyIndex
-
1
;
pReply
->
matchIndex
=
snapshot
.
lastApplyIndex
-
1
;
// send response
// send response
...
@@ -634,105 +603,120 @@ int32_t syncNodeOnAppendEntriesSnapshotCb(SSyncNode* ths, SyncAppendEntries* pMs
...
@@ -634,105 +603,120 @@ int32_t syncNodeOnAppendEntriesSnapshotCb(SSyncNode* ths, SyncAppendEntries* pMs
return
ret
;
return
ret
;
}
}
}
}
while
(
0
);
// case 4, accept request
// not match
if
(
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
&&
ths
->
state
==
TAOS_SYNC_STATE_FOLLOWER
&&
logOK
)
{
//
// has extra entries (> preIndex) in local log
// condition1:
SyncIndex
myLastIndex
=
syncNodeGetLastIndex
(
ths
);
// term < myTerm
bool
hasExtraEntries
=
myLastIndex
>
pMsg
->
prevLogIndex
;
//
// condition2:
// !logOK
//
// operation:
// not match
// no operation on log
do
{
bool
condition1
=
pMsg
->
term
<
ths
->
pRaftStore
->
currentTerm
;
bool
condition2
=
(
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
)
&&
(
ths
->
state
==
TAOS_SYNC_STATE_FOLLOWER
)
&&
!
logOK
;
bool
condition
=
condition1
||
condition2
;
if
(
condition
)
{
// prepare response msg
SyncAppendEntriesReply
*
pReply
=
syncAppendEntriesReplyBuild
(
ths
->
vgId
);
pReply
->
srcId
=
ths
->
myRaftId
;
pReply
->
destId
=
pMsg
->
srcId
;
pReply
->
term
=
ths
->
pRaftStore
->
currentTerm
;
pReply
->
privateTerm
=
ths
->
pNewNodeReceiver
->
privateTerm
;
pReply
->
success
=
false
;
pReply
->
matchIndex
=
SYNC_INDEX_INVALID
;
// has entries in SyncAppendEntries msg
// send response
bool
hasAppendEntries
=
pMsg
->
dataLen
>
0
;
SRpcMsg
rpcMsg
;
syncAppendEntriesReply2RpcMsg
(
pReply
,
&
rpcMsg
);
syncNodeSendMsgById
(
&
pReply
->
destId
,
ths
,
&
rpcMsg
);
syncAppendEntriesReplyDestroy
(
pReply
);
sTrace
(
return
ret
;
"recv SyncAppendEntries, accept, receive_term:%lu, current_term:%lu, ths->state:%d, logOK:%d, "
}
"hasExtraEntries:%d, hasAppendEntries:%d"
,
}
while
(
0
);
pMsg
->
term
,
ths
->
pRaftStore
->
currentTerm
,
ths
->
state
,
logOK
,
hasExtraEntries
,
hasAppendEntries
);
// really match
//
// condition:
// logOK
//
// operation:
// match
// make log same
do
{
bool
condition
=
(
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
)
&&
(
ths
->
state
==
TAOS_SYNC_STATE_FOLLOWER
)
&&
logOK
;
if
(
condition
)
{
// has extra entries (> preIndex) in local log
SyncIndex
myLastIndex
=
syncNodeGetLastIndex
(
ths
);
bool
hasExtraEntries
=
myLastIndex
>
pMsg
->
prevLogIndex
;
// has entries in SyncAppendEntries msg
bool
hasAppendEntries
=
pMsg
->
dataLen
>
0
;
if
(
hasExtraEntries
)
{
// make log same, rollback deleted entries
code
=
syncNodeMakeLogSame
(
ths
,
pMsg
);
ASSERT
(
code
==
0
);
}
if
(
hasExtraEntries
&&
hasAppendEntries
)
{
if
(
hasAppendEntries
)
{
// make log same
// append entry
SSyncRaftEntry
*
pAppendEntry
;
SSyncRaftEntry
*
pAppendEntry
=
syncEntryDeserialize
(
pMsg
->
data
,
pMsg
->
dataLen
);
bool
entryAlreadyWritten
;
ASSERT
(
pAppendEntry
!=
NULL
);
code
=
syncNodeMakeLogSame
(
ths
,
pMsg
,
&
pAppendEntry
,
&
entryAlreadyWritten
);
ASSERT
(
code
==
0
);
ASSERT
(
pAppendEntry
!=
NULL
);
if
(
!
entryAlreadyWritten
)
{
// append new entries
code
=
ths
->
pLogStore
->
syncLogAppendEntry
(
ths
->
pLogStore
,
pAppendEntry
);
code
=
ths
->
pLogStore
->
syncLogAppendEntry
(
ths
->
pLogStore
,
pAppendEntry
);
ASSERT
(
code
==
0
);
ASSERT
(
code
==
0
);
// pre commit
// pre commit
code
=
syncNodePreCommit
(
ths
,
pAppendEntry
);
code
=
syncNodePreCommit
(
ths
,
pAppendEntry
);
ASSERT
(
code
==
0
);
ASSERT
(
code
==
0
);
}
syncEntryDestory
(
pAppendEntry
);
}
else
if
(
hasExtraEntries
&&
!
hasAppendEntries
)
{
// do nothing
}
else
if
(
!
hasExtraEntries
&&
hasAppendEntries
)
{
SSyncRaftEntry
*
pAppendEntry
=
syncEntryDeserialize
(
pMsg
->
data
,
pMsg
->
dataLen
);
ASSERT
(
pAppendEntry
!=
NULL
);
// append new entries
code
=
ths
->
pLogStore
->
syncLogAppendEntry
(
ths
->
pLogStore
,
pAppendEntry
);
ASSERT
(
code
==
0
);
// pre commit
code
=
syncNodePreCommit
(
ths
,
pAppendEntry
);
ASSERT
(
code
==
0
);
syncEntryDestory
(
pAppendEntry
);
syncEntryDestory
(
pAppendEntry
);
}
}
else
if
(
!
hasExtraEntries
&&
!
hasAppendEntries
)
{
// do nothing
}
else
{
ASSERT
(
0
);
}
// prepare response msg
SyncAppendEntriesReply
*
pReply
=
syncAppendEntriesReplyBuild
(
ths
->
vgId
);
pReply
->
srcId
=
ths
->
myRaftId
;
pReply
->
destId
=
pMsg
->
srcId
;
pReply
->
term
=
ths
->
pRaftStore
->
currentTerm
;
pReply
->
success
=
true
;
pReply
->
privateTerm
=
ths
->
pNewNodeReceiver
->
privateTerm
;
if
(
hasAppendEntries
)
{
// prepare response msg
pReply
->
matchIndex
=
pMsg
->
prevLogIndex
+
1
;
SyncAppendEntriesReply
*
pReply
=
syncAppendEntriesReplyBuild
(
ths
->
vgId
);
}
else
{
pReply
->
srcId
=
ths
->
myRaftId
;
pReply
->
matchIndex
=
pMsg
->
prevLogIndex
;
pReply
->
destId
=
pMsg
->
srcId
;
}
pReply
->
term
=
ths
->
pRaftStore
->
currentTerm
;
pReply
->
privateTerm
=
ths
->
pNewNodeReceiver
->
privateTerm
;
pReply
->
success
=
true
;
pReply
->
matchIndex
=
hasAppendEntries
?
pMsg
->
prevLogIndex
+
1
:
pMsg
->
prevLogIndex
;
// send response
// send response
SRpcMsg
rpcMsg
;
SRpcMsg
rpcMsg
;
syncAppendEntriesReply2RpcMsg
(
pReply
,
&
rpcMsg
);
syncAppendEntriesReply2RpcMsg
(
pReply
,
&
rpcMsg
);
syncNodeSendMsgById
(
&
pReply
->
destId
,
ths
,
&
rpcMsg
);
syncNodeSendMsgById
(
&
pReply
->
destId
,
ths
,
&
rpcMsg
);
syncAppendEntriesReplyDestroy
(
pReply
);
syncAppendEntriesReplyDestroy
(
pReply
);
// maybe update commit index from leader
// maybe update commit index, leader notice me
if
(
pMsg
->
commitIndex
>
ths
->
commitIndex
)
{
if
(
pMsg
->
commitIndex
>
ths
->
commitIndex
)
{
// has commit entry in local
// has commit entry in local
if
(
pMsg
->
commitIndex
<=
ths
->
pLogStore
->
syncLogLastIndex
(
ths
->
pLogStore
))
{
if
(
pMsg
->
commitIndex
<=
ths
->
pLogStore
->
syncLogLastIndex
(
ths
->
pLogStore
))
{
SyncIndex
beginIndex
=
ths
->
commitIndex
+
1
;
SyncIndex
beginIndex
=
ths
->
commitIndex
+
1
;
SyncIndex
endIndex
=
pMsg
->
commitIndex
;
SyncIndex
endIndex
=
pMsg
->
commitIndex
;
// update commit index
// update commit index
ths
->
commitIndex
=
pMsg
->
commitIndex
;
ths
->
commitIndex
=
pMsg
->
commitIndex
;
// call back Wal
// call back Wal
code
=
ths
->
pLogStore
->
updateCommitIndex
(
ths
->
pLogStore
,
ths
->
commitIndex
);
code
=
ths
->
pLogStore
->
updateCommitIndex
(
ths
->
pLogStore
,
ths
->
commitIndex
);
ASSERT
(
code
==
0
);
ASSERT
(
code
==
0
);
code
=
syncNodeCommit
(
ths
,
beginIndex
,
endIndex
,
ths
->
state
);
code
=
syncNodeCommit
(
ths
,
beginIndex
,
endIndex
,
ths
->
state
);
ASSERT
(
code
==
0
);
ASSERT
(
code
==
0
);
}
}
}
return
ret
;
}
}
}
}
while
(
0
);
return
ret
;
return
ret
;
}
}
\ No newline at end of file
source/libs/sync/src/syncMain.c
浏览文件 @
687caac2
...
@@ -1283,7 +1283,6 @@ bool syncNodeIsIndexInSnapshot(SSyncNode* pSyncNode, SyncIndex index) {
...
@@ -1283,7 +1283,6 @@ bool syncNodeIsIndexInSnapshot(SSyncNode* pSyncNode, SyncIndex index) {
SSnapshot
snapshot
;
SSnapshot
snapshot
;
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSyncNode
->
pFsm
,
&
snapshot
);
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSyncNode
->
pFsm
,
&
snapshot
);
bool
b
=
(
index
<=
snapshot
.
lastApplyIndex
);
bool
b
=
(
index
<=
snapshot
.
lastApplyIndex
);
return
b
;
return
b
;
}
}
...
@@ -1307,17 +1306,10 @@ SyncTerm syncNodeGetLastTerm(SSyncNode* pSyncNode) {
...
@@ -1307,17 +1306,10 @@ SyncTerm syncNodeGetLastTerm(SSyncNode* pSyncNode) {
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSyncNode
->
pFsm
,
&
snapshot
);
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSyncNode
->
pFsm
,
&
snapshot
);
}
}
if
(
pSyncNode
->
pLogStore
->
syncLogEntryCount
(
pSyncNode
->
pLogStore
)
>
0
)
{
SyncIndex
logLastIndex
=
pSyncNode
->
pLogStore
->
syncLogLastIndex
(
pSyncNode
->
pLogStore
);
// has log
if
(
logLastIndex
>
snapshot
.
lastApplyIndex
)
{
SyncIndex
logLastIndex
=
pSyncNode
->
pLogStore
->
syncLogLastIndex
(
pSyncNode
->
pLogStore
);
lastTerm
=
pSyncNode
->
pLogStore
->
syncLogLastTerm
(
pSyncNode
->
pLogStore
);
if
(
logLastIndex
>
snapshot
.
lastApplyIndex
)
{
lastTerm
=
pSyncNode
->
pLogStore
->
syncLogLastTerm
(
pSyncNode
->
pLogStore
);
}
else
{
lastTerm
=
snapshot
.
lastApplyTerm
;
}
}
else
{
}
else
{
// no log
lastTerm
=
snapshot
.
lastApplyTerm
;
lastTerm
=
snapshot
.
lastApplyTerm
;
}
}
...
@@ -1346,22 +1338,7 @@ SyncIndex syncNodeGetPreIndex(SSyncNode* pSyncNode, SyncIndex index) {
...
@@ -1346,22 +1338,7 @@ SyncIndex syncNodeGetPreIndex(SSyncNode* pSyncNode, SyncIndex index) {
SyncIndex
syncStartIndex
=
syncNodeSyncStartIndex
(
pSyncNode
);
SyncIndex
syncStartIndex
=
syncNodeSyncStartIndex
(
pSyncNode
);
ASSERT
(
index
<=
syncStartIndex
);
ASSERT
(
index
<=
syncStartIndex
);
SyncIndex
preIndex
;
SyncIndex
preIndex
=
index
-
1
;
if
(
syncNodeHasSnapshot
(
pSyncNode
))
{
// has snapshot
SSnapshot
snapshot
=
{.
data
=
NULL
,
.
lastApplyIndex
=
-
1
,
.
lastApplyTerm
=
0
};
if
(
pSyncNode
->
pFsm
->
FpGetSnapshot
!=
NULL
)
{
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSyncNode
->
pFsm
,
&
snapshot
);
}
// ASSERT(index > snapshot.lastApplyIndex);
preIndex
=
index
-
1
;
}
else
{
// no snapshot
preIndex
=
index
-
1
;
}
return
preIndex
;
return
preIndex
;
}
}
...
@@ -1382,7 +1359,6 @@ SyncTerm syncNodeGetPreTerm(SSyncNode* pSyncNode, SyncIndex index) {
...
@@ -1382,7 +1359,6 @@ SyncTerm syncNodeGetPreTerm(SSyncNode* pSyncNode, SyncIndex index) {
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSyncNode
->
pFsm
,
&
snapshot
);
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSyncNode
->
pFsm
,
&
snapshot
);
}
}
// ASSERT(index > snapshot.lastApplyIndex);
if
(
index
>
snapshot
.
lastApplyIndex
+
1
)
{
if
(
index
>
snapshot
.
lastApplyIndex
+
1
)
{
// should be log preTerm
// should be log preTerm
SSyncRaftEntry
*
pPreEntry
=
NULL
;
SSyncRaftEntry
*
pPreEntry
=
NULL
;
...
@@ -1395,10 +1371,19 @@ SyncTerm syncNodeGetPreTerm(SSyncNode* pSyncNode, SyncIndex index) {
...
@@ -1395,10 +1371,19 @@ SyncTerm syncNodeGetPreTerm(SSyncNode* pSyncNode, SyncIndex index) {
}
else
if
(
index
==
snapshot
.
lastApplyIndex
+
1
)
{
}
else
if
(
index
==
snapshot
.
lastApplyIndex
+
1
)
{
preTerm
=
snapshot
.
lastApplyTerm
;
preTerm
=
snapshot
.
lastApplyTerm
;
}
else
{
}
else
{
// ASSERT(0);
// maybe snapshot change
// maybe snapshot change
preTerm
=
snapshot
.
lastApplyTerm
;
sError
(
"sync get pre term, bad scene. index:%ld"
,
index
);
logStoreLog2
(
"sync get pre term, bad scene"
,
pSyncNode
->
pLogStore
);
SSyncRaftEntry
*
pPreEntry
=
NULL
;
int32_t
code
=
pSyncNode
->
pLogStore
->
syncLogGetEntry
(
pSyncNode
->
pLogStore
,
index
-
1
,
&
pPreEntry
);
ASSERT
(
code
==
0
);
ASSERT
(
pPreEntry
!=
NULL
);
preTerm
=
pPreEntry
->
term
;
taosMemoryFree
(
pPreEntry
);
}
}
}
else
{
}
else
{
...
...
source/libs/sync/src/syncRaftLog.c
浏览文件 @
687caac2
...
@@ -437,10 +437,6 @@ cJSON* logStore2Json(SSyncLogStore* pLogStore) {
...
@@ -437,10 +437,6 @@ cJSON* logStore2Json(SSyncLogStore* pLogStore) {
cJSON_AddStringToObject
(
pRoot
,
"pSyncNode"
,
u64buf
);
cJSON_AddStringToObject
(
pRoot
,
"pSyncNode"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pData
->
pWal
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pData
->
pWal
);
cJSON_AddStringToObject
(
pRoot
,
"pWal"
,
u64buf
);
cJSON_AddStringToObject
(
pRoot
,
"pWal"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
raftLogLastIndex
(
pLogStore
));
cJSON_AddStringToObject
(
pRoot
,
"LastIndex"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
raftLogLastTerm
(
pLogStore
));
cJSON_AddStringToObject
(
pRoot
,
"LastTerm"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
pData
->
beginIndex
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
pData
->
beginIndex
);
cJSON_AddStringToObject
(
pRoot
,
"beginIndex"
,
u64buf
);
cJSON_AddStringToObject
(
pRoot
,
"beginIndex"
,
u64buf
);
...
@@ -452,6 +448,17 @@ cJSON* logStore2Json(SSyncLogStore* pLogStore) {
...
@@ -452,6 +448,17 @@ cJSON* logStore2Json(SSyncLogStore* pLogStore) {
int32_t
count
=
raftLogEntryCount
(
pLogStore
);
int32_t
count
=
raftLogEntryCount
(
pLogStore
);
cJSON_AddNumberToObject
(
pRoot
,
"entryCount"
,
count
);
cJSON_AddNumberToObject
(
pRoot
,
"entryCount"
,
count
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
raftLogWriteIndex
(
pLogStore
));
cJSON_AddStringToObject
(
pRoot
,
"WriteIndex"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%d"
,
raftLogIsEmpty
(
pLogStore
));
cJSON_AddStringToObject
(
pRoot
,
"IsEmpty"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
raftLogLastIndex
(
pLogStore
));
cJSON_AddStringToObject
(
pRoot
,
"LastIndex"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
raftLogLastTerm
(
pLogStore
));
cJSON_AddStringToObject
(
pRoot
,
"LastTerm"
,
u64buf
);
cJSON
*
pEntries
=
cJSON_CreateArray
();
cJSON
*
pEntries
=
cJSON_CreateArray
();
cJSON_AddItemToObject
(
pRoot
,
"pEntries"
,
pEntries
);
cJSON_AddItemToObject
(
pRoot
,
"pEntries"
,
pEntries
);
...
@@ -484,10 +491,6 @@ cJSON* logStoreSimple2Json(SSyncLogStore* pLogStore) {
...
@@ -484,10 +491,6 @@ cJSON* logStoreSimple2Json(SSyncLogStore* pLogStore) {
cJSON_AddStringToObject
(
pRoot
,
"pSyncNode"
,
u64buf
);
cJSON_AddStringToObject
(
pRoot
,
"pSyncNode"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pData
->
pWal
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pData
->
pWal
);
cJSON_AddStringToObject
(
pRoot
,
"pWal"
,
u64buf
);
cJSON_AddStringToObject
(
pRoot
,
"pWal"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
raftLogLastIndex
(
pLogStore
));
cJSON_AddStringToObject
(
pRoot
,
"LastIndex"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
raftLogLastTerm
(
pLogStore
));
cJSON_AddStringToObject
(
pRoot
,
"LastTerm"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
pData
->
beginIndex
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
pData
->
beginIndex
);
cJSON_AddStringToObject
(
pRoot
,
"beginIndex"
,
u64buf
);
cJSON_AddStringToObject
(
pRoot
,
"beginIndex"
,
u64buf
);
...
@@ -498,6 +501,17 @@ cJSON* logStoreSimple2Json(SSyncLogStore* pLogStore) {
...
@@ -498,6 +501,17 @@ cJSON* logStoreSimple2Json(SSyncLogStore* pLogStore) {
int32_t
count
=
raftLogEntryCount
(
pLogStore
);
int32_t
count
=
raftLogEntryCount
(
pLogStore
);
cJSON_AddNumberToObject
(
pRoot
,
"entryCount"
,
count
);
cJSON_AddNumberToObject
(
pRoot
,
"entryCount"
,
count
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
raftLogWriteIndex
(
pLogStore
));
cJSON_AddStringToObject
(
pRoot
,
"WriteIndex"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%d"
,
raftLogIsEmpty
(
pLogStore
));
cJSON_AddStringToObject
(
pRoot
,
"IsEmpty"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
raftLogLastIndex
(
pLogStore
));
cJSON_AddStringToObject
(
pRoot
,
"LastIndex"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
raftLogLastTerm
(
pLogStore
));
cJSON_AddStringToObject
(
pRoot
,
"LastTerm"
,
u64buf
);
}
}
cJSON
*
pJson
=
cJSON_CreateObject
();
cJSON
*
pJson
=
cJSON_CreateObject
();
...
...
source/libs/sync/test/syncRaftLogTest2.cpp
浏览文件 @
687caac2
...
@@ -32,6 +32,8 @@ int32_t GetSnapshotCb(struct SSyncFSM* pFsm, SSnapshot* pSnapshot) {
...
@@ -32,6 +32,8 @@ int32_t GetSnapshotCb(struct SSyncFSM* pFsm, SSnapshot* pSnapshot) {
return
0
;
return
0
;
}
}
bool
gAssert
=
true
;
void
init
()
{
void
init
()
{
walInit
();
walInit
();
...
@@ -68,6 +70,17 @@ void test1() {
...
@@ -68,6 +70,17 @@ void test1() {
pLogStore
=
logStoreCreate
(
pSyncNode
);
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test1 ----- "
,
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test1 ----- "
,
pLogStore
);
if
(
gAssert
)
{
assert
(
pLogStore
->
syncLogBeginIndex
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogEndIndex
(
pLogStore
)
==
-
1
);
assert
(
pLogStore
->
syncLogEntryCount
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogWriteIndex
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogIsEmpty
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogLastIndex
(
pLogStore
)
==
-
1
);
assert
(
pLogStore
->
syncLogLastTerm
(
pLogStore
)
==
0
);
}
logStoreDestory
(
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
cleanup
();
...
@@ -76,6 +89,17 @@ void test1() {
...
@@ -76,6 +89,17 @@ void test1() {
pLogStore
=
logStoreCreate
(
pSyncNode
);
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test1 restart ----- "
,
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test1 restart ----- "
,
pLogStore
);
if
(
gAssert
)
{
assert
(
pLogStore
->
syncLogBeginIndex
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogEndIndex
(
pLogStore
)
==
-
1
);
assert
(
pLogStore
->
syncLogEntryCount
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogWriteIndex
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogIsEmpty
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogLastIndex
(
pLogStore
)
==
-
1
);
assert
(
pLogStore
->
syncLogLastTerm
(
pLogStore
)
==
0
);
}
logStoreDestory
(
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
cleanup
();
}
}
...
@@ -88,6 +112,17 @@ void test2() {
...
@@ -88,6 +112,17 @@ void test2() {
assert
(
pLogStore
);
assert
(
pLogStore
);
pLogStore
->
syncLogSetBeginIndex
(
pLogStore
,
5
);
pLogStore
->
syncLogSetBeginIndex
(
pLogStore
,
5
);
logStoreLog2
((
char
*
)
"
\n\n\n
test2 ----- "
,
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test2 ----- "
,
pLogStore
);
if
(
gAssert
)
{
assert
(
pLogStore
->
syncLogBeginIndex
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogEndIndex
(
pLogStore
)
==
-
1
);
assert
(
pLogStore
->
syncLogEntryCount
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogWriteIndex
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogIsEmpty
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogLastIndex
(
pLogStore
)
==
-
1
);
assert
(
pLogStore
->
syncLogLastTerm
(
pLogStore
)
==
0
);
}
logStoreDestory
(
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
cleanup
();
...
@@ -96,6 +131,17 @@ void test2() {
...
@@ -96,6 +131,17 @@ void test2() {
pLogStore
=
logStoreCreate
(
pSyncNode
);
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test2 restart ----- "
,
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test2 restart ----- "
,
pLogStore
);
if
(
gAssert
)
{
assert
(
pLogStore
->
syncLogBeginIndex
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogEndIndex
(
pLogStore
)
==
-
1
);
assert
(
pLogStore
->
syncLogEntryCount
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogWriteIndex
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogIsEmpty
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogLastIndex
(
pLogStore
)
==
-
1
);
assert
(
pLogStore
->
syncLogLastTerm
(
pLogStore
)
==
0
);
}
logStoreDestory
(
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
cleanup
();
}
}
...
@@ -108,6 +154,16 @@ void test3() {
...
@@ -108,6 +154,16 @@ void test3() {
assert
(
pLogStore
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test3 ----- "
,
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test3 ----- "
,
pLogStore
);
if
(
gAssert
)
{
assert
(
pLogStore
->
syncLogBeginIndex
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogEndIndex
(
pLogStore
)
==
-
1
);
assert
(
pLogStore
->
syncLogEntryCount
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogWriteIndex
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogIsEmpty
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogLastIndex
(
pLogStore
)
==
-
1
);
assert
(
pLogStore
->
syncLogLastTerm
(
pLogStore
)
==
0
);
}
for
(
int
i
=
0
;
i
<=
4
;
++
i
)
{
for
(
int
i
=
0
;
i
<=
4
;
++
i
)
{
int32_t
dataLen
=
10
;
int32_t
dataLen
=
10
;
SSyncRaftEntry
*
pEntry
=
syncEntryBuild
(
dataLen
);
SSyncRaftEntry
*
pEntry
=
syncEntryBuild
(
dataLen
);
...
@@ -124,6 +180,17 @@ void test3() {
...
@@ -124,6 +180,17 @@ void test3() {
syncEntryDestory
(
pEntry
);
syncEntryDestory
(
pEntry
);
}
}
logStoreLog2
((
char
*
)
"test3 after appendEntry"
,
pLogStore
);
logStoreLog2
((
char
*
)
"test3 after appendEntry"
,
pLogStore
);
if
(
gAssert
)
{
assert
(
pLogStore
->
syncLogBeginIndex
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogEndIndex
(
pLogStore
)
==
4
);
assert
(
pLogStore
->
syncLogEntryCount
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogWriteIndex
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogIsEmpty
(
pLogStore
)
==
1
);
assert
(
pLogStore
->
syncLogLastIndex
(
pLogStore
)
==
4
);
assert
(
pLogStore
->
syncLogLastTerm
(
pLogStore
)
==
104
);
}
logStoreDestory
(
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
cleanup
();
...
@@ -132,6 +199,17 @@ void test3() {
...
@@ -132,6 +199,17 @@ void test3() {
pLogStore
=
logStoreCreate
(
pSyncNode
);
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test3 restart ----- "
,
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test3 restart ----- "
,
pLogStore
);
if
(
gAssert
)
{
assert
(
pLogStore
->
syncLogBeginIndex
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogEndIndex
(
pLogStore
)
==
4
);
assert
(
pLogStore
->
syncLogEntryCount
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogWriteIndex
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogIsEmpty
(
pLogStore
)
==
1
);
assert
(
pLogStore
->
syncLogLastIndex
(
pLogStore
)
==
4
);
assert
(
pLogStore
->
syncLogLastTerm
(
pLogStore
)
==
104
);
}
logStoreDestory
(
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
cleanup
();
}
}
...
@@ -161,6 +239,17 @@ void test4() {
...
@@ -161,6 +239,17 @@ void test4() {
syncEntryDestory
(
pEntry
);
syncEntryDestory
(
pEntry
);
}
}
logStoreLog2
((
char
*
)
"test4 after appendEntry"
,
pLogStore
);
logStoreLog2
((
char
*
)
"test4 after appendEntry"
,
pLogStore
);
if
(
gAssert
)
{
assert
(
pLogStore
->
syncLogBeginIndex
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogEndIndex
(
pLogStore
)
==
9
);
assert
(
pLogStore
->
syncLogEntryCount
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogWriteIndex
(
pLogStore
)
==
10
);
assert
(
pLogStore
->
syncLogIsEmpty
(
pLogStore
)
==
1
);
assert
(
pLogStore
->
syncLogLastIndex
(
pLogStore
)
==
9
);
assert
(
pLogStore
->
syncLogLastTerm
(
pLogStore
)
==
109
);
}
logStoreDestory
(
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
cleanup
();
...
@@ -169,6 +258,17 @@ void test4() {
...
@@ -169,6 +258,17 @@ void test4() {
pLogStore
=
logStoreCreate
(
pSyncNode
);
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test4 restart ----- "
,
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test4 restart ----- "
,
pLogStore
);
if
(
gAssert
)
{
assert
(
pLogStore
->
syncLogBeginIndex
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogEndIndex
(
pLogStore
)
==
9
);
assert
(
pLogStore
->
syncLogEntryCount
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogWriteIndex
(
pLogStore
)
==
10
);
assert
(
pLogStore
->
syncLogIsEmpty
(
pLogStore
)
==
1
);
assert
(
pLogStore
->
syncLogLastIndex
(
pLogStore
)
==
9
);
assert
(
pLogStore
->
syncLogLastTerm
(
pLogStore
)
==
109
);
}
logStoreDestory
(
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
cleanup
();
}
}
...
@@ -199,9 +299,29 @@ void test5() {
...
@@ -199,9 +299,29 @@ void test5() {
}
}
logStoreLog2
((
char
*
)
"test5 after appendEntry"
,
pLogStore
);
logStoreLog2
((
char
*
)
"test5 after appendEntry"
,
pLogStore
);
if
(
gAssert
)
{
assert
(
pLogStore
->
syncLogBeginIndex
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogEndIndex
(
pLogStore
)
==
9
);
assert
(
pLogStore
->
syncLogEntryCount
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogWriteIndex
(
pLogStore
)
==
10
);
assert
(
pLogStore
->
syncLogIsEmpty
(
pLogStore
)
==
1
);
assert
(
pLogStore
->
syncLogLastIndex
(
pLogStore
)
==
9
);
assert
(
pLogStore
->
syncLogLastTerm
(
pLogStore
)
==
109
);
}
pLogStore
->
syncLogTruncate
(
pLogStore
,
7
);
pLogStore
->
syncLogTruncate
(
pLogStore
,
7
);
logStoreLog2
((
char
*
)
"after truncate 7"
,
pLogStore
);
logStoreLog2
((
char
*
)
"after truncate 7"
,
pLogStore
);
if
(
gAssert
)
{
assert
(
pLogStore
->
syncLogBeginIndex
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogEndIndex
(
pLogStore
)
==
6
);
assert
(
pLogStore
->
syncLogEntryCount
(
pLogStore
)
==
2
);
assert
(
pLogStore
->
syncLogWriteIndex
(
pLogStore
)
==
7
);
assert
(
pLogStore
->
syncLogIsEmpty
(
pLogStore
)
==
1
);
assert
(
pLogStore
->
syncLogLastIndex
(
pLogStore
)
==
6
);
assert
(
pLogStore
->
syncLogLastTerm
(
pLogStore
)
==
106
);
}
logStoreDestory
(
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
cleanup
();
...
@@ -210,6 +330,17 @@ void test5() {
...
@@ -210,6 +330,17 @@ void test5() {
pLogStore
=
logStoreCreate
(
pSyncNode
);
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test5 restart ----- "
,
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test5 restart ----- "
,
pLogStore
);
if
(
gAssert
)
{
assert
(
pLogStore
->
syncLogBeginIndex
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogEndIndex
(
pLogStore
)
==
6
);
assert
(
pLogStore
->
syncLogEntryCount
(
pLogStore
)
==
2
);
assert
(
pLogStore
->
syncLogWriteIndex
(
pLogStore
)
==
7
);
assert
(
pLogStore
->
syncLogIsEmpty
(
pLogStore
)
==
1
);
assert
(
pLogStore
->
syncLogLastIndex
(
pLogStore
)
==
6
);
assert
(
pLogStore
->
syncLogLastTerm
(
pLogStore
)
==
106
);
}
logStoreDestory
(
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
cleanup
();
}
}
...
@@ -240,9 +371,29 @@ void test6() {
...
@@ -240,9 +371,29 @@ void test6() {
}
}
logStoreLog2
((
char
*
)
"test6 after appendEntry"
,
pLogStore
);
logStoreLog2
((
char
*
)
"test6 after appendEntry"
,
pLogStore
);
if
(
gAssert
)
{
assert
(
pLogStore
->
syncLogBeginIndex
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogEndIndex
(
pLogStore
)
==
9
);
assert
(
pLogStore
->
syncLogEntryCount
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogWriteIndex
(
pLogStore
)
==
10
);
assert
(
pLogStore
->
syncLogIsEmpty
(
pLogStore
)
==
1
);
assert
(
pLogStore
->
syncLogLastIndex
(
pLogStore
)
==
9
);
assert
(
pLogStore
->
syncLogLastTerm
(
pLogStore
)
==
109
);
}
pLogStore
->
syncLogTruncate
(
pLogStore
,
5
);
pLogStore
->
syncLogTruncate
(
pLogStore
,
5
);
logStoreLog2
((
char
*
)
"after truncate 5"
,
pLogStore
);
logStoreLog2
((
char
*
)
"after truncate 5"
,
pLogStore
);
if
(
gAssert
)
{
assert
(
pLogStore
->
syncLogBeginIndex
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogEndIndex
(
pLogStore
)
==
-
1
);
assert
(
pLogStore
->
syncLogEntryCount
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogWriteIndex
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogIsEmpty
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogLastIndex
(
pLogStore
)
==
-
1
);
assert
(
pLogStore
->
syncLogLastTerm
(
pLogStore
)
==
0
);
}
logStoreDestory
(
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
cleanup
();
...
@@ -251,6 +402,17 @@ void test6() {
...
@@ -251,6 +402,17 @@ void test6() {
pLogStore
=
logStoreCreate
(
pSyncNode
);
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test6 restart ----- "
,
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test6 restart ----- "
,
pLogStore
);
if
(
gAssert
)
{
assert
(
pLogStore
->
syncLogBeginIndex
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogEndIndex
(
pLogStore
)
==
-
1
);
assert
(
pLogStore
->
syncLogEntryCount
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogWriteIndex
(
pLogStore
)
==
5
);
assert
(
pLogStore
->
syncLogIsEmpty
(
pLogStore
)
==
0
);
assert
(
pLogStore
->
syncLogLastIndex
(
pLogStore
)
==
-
1
);
assert
(
pLogStore
->
syncLogLastTerm
(
pLogStore
)
==
0
);
}
logStoreDestory
(
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
cleanup
();
}
}
...
@@ -259,6 +421,11 @@ int main(int argc, char** argv) {
...
@@ -259,6 +421,11 @@ int main(int argc, char** argv) {
tsAsyncLog
=
0
;
tsAsyncLog
=
0
;
sDebugFlag
=
DEBUG_TRACE
+
DEBUG_INFO
+
DEBUG_SCREEN
+
DEBUG_FILE
;
sDebugFlag
=
DEBUG_TRACE
+
DEBUG_INFO
+
DEBUG_SCREEN
+
DEBUG_FILE
;
if
(
argc
==
2
)
{
gAssert
=
atoi
(
argv
[
1
]);
}
sTrace
(
"gAssert : %d"
,
gAssert
);
test1
();
test1
();
test2
();
test2
();
test3
();
test3
();
...
...
source/libs/sync/test/syncTimeoutTest.cpp
浏览文件 @
687caac2
...
@@ -80,7 +80,7 @@ void test5() {
...
@@ -80,7 +80,7 @@ void test5() {
void
test6
()
{
void
test6
()
{
SyncTimeout
*
pMsg
=
createMsg
();
SyncTimeout
*
pMsg
=
createMsg
();
char
*
jsonStr
=
syncTimeout2Str
(
pMsg
);
char
*
jsonStr
=
syncTimeout2Str
(
pMsg
);
sTrace
(
"jsonStr: %s"
,
jsonStr
);
sTrace
(
"jsonStr: %s"
,
jsonStr
);
syncUtilJson2Line
(
jsonStr
);
syncUtilJson2Line
(
jsonStr
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录