Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
a047d4a5
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看板
提交
a047d4a5
编写于
6月 07, 2022
作者:
S
Shengliang Guan
浏览文件
操作
浏览文件
下载
差异文件
Merge remote-tracking branch 'origin/feature/sync-refactor' into fix/mnode
上级
48cf2591
8680da28
变更
48
展开全部
隐藏空白更改
内联
并排
Showing
48 changed file
with
4450 addition
and
133 deletion
+4450
-133
include/common/tmsgdef.h
include/common/tmsgdef.h
+2
-0
include/libs/sync/sync.h
include/libs/sync/sync.h
+19
-1
include/libs/sync/syncTools.h
include/libs/sync/syncTools.h
+89
-0
source/dnode/mgmt/mgmt_mnode/src/mmHandle.c
source/dnode/mgmt/mgmt_mnode/src/mmHandle.c
+5
-3
source/dnode/mnode/impl/inc/mndInt.h
source/dnode/mnode/impl/inc/mndInt.h
+1
-0
source/dnode/mnode/impl/src/mndMain.c
source/dnode/mnode/impl/src/mndMain.c
+86
-34
source/dnode/mnode/impl/src/mndSync.c
source/dnode/mnode/impl/src/mndSync.c
+5
-4
source/dnode/mnode/sdb/inc/sdb.h
source/dnode/mnode/sdb/inc/sdb.h
+3
-1
source/dnode/mnode/sdb/src/sdb.c
source/dnode/mnode/sdb/src/sdb.c
+6
-2
source/dnode/mnode/sdb/src/sdbFile.c
source/dnode/mnode/sdb/src/sdbFile.c
+4
-1
source/dnode/vnode/src/vnd/vnodeSync.c
source/dnode/vnode/src/vnd/vnodeSync.c
+2
-0
source/libs/sync/inc/syncAppendEntries.h
source/libs/sync/inc/syncAppendEntries.h
+1
-0
source/libs/sync/inc/syncAppendEntriesReply.h
source/libs/sync/inc/syncAppendEntriesReply.h
+1
-0
source/libs/sync/inc/syncElection.h
source/libs/sync/inc/syncElection.h
+2
-0
source/libs/sync/inc/syncIO.h
source/libs/sync/inc/syncIO.h
+3
-0
source/libs/sync/inc/syncInt.h
source/libs/sync/inc/syncInt.h
+35
-13
source/libs/sync/inc/syncRaftCfg.h
source/libs/sync/inc/syncRaftCfg.h
+7
-1
source/libs/sync/inc/syncRaftLog.h
source/libs/sync/inc/syncRaftLog.h
+2
-8
source/libs/sync/inc/syncReplication.h
source/libs/sync/inc/syncReplication.h
+1
-0
source/libs/sync/inc/syncRequestVote.h
source/libs/sync/inc/syncRequestVote.h
+1
-0
source/libs/sync/inc/syncRequestVoteReply.h
source/libs/sync/inc/syncRequestVoteReply.h
+1
-0
source/libs/sync/inc/syncSnapshot.h
source/libs/sync/inc/syncSnapshot.h
+40
-18
source/libs/sync/src/syncAppendEntries.c
source/libs/sync/src/syncAppendEntries.c
+269
-1
source/libs/sync/src/syncAppendEntriesReply.c
source/libs/sync/src/syncAppendEntriesReply.c
+122
-0
source/libs/sync/src/syncCommit.c
source/libs/sync/src/syncCommit.c
+10
-1
source/libs/sync/src/syncElection.c
source/libs/sync/src/syncElection.c
+27
-1
source/libs/sync/src/syncIO.c
source/libs/sync/src/syncIO.c
+26
-4
source/libs/sync/src/syncMain.c
source/libs/sync/src/syncMain.c
+371
-15
source/libs/sync/src/syncMessage.c
source/libs/sync/src/syncMessage.c
+329
-0
source/libs/sync/src/syncRaftCfg.c
source/libs/sync/src/syncRaftCfg.c
+7
-2
source/libs/sync/src/syncRaftLog.c
source/libs/sync/src/syncRaftLog.c
+270
-8
source/libs/sync/src/syncReplication.c
source/libs/sync/src/syncReplication.c
+80
-1
source/libs/sync/src/syncRequestVote.c
source/libs/sync/src/syncRequestVote.c
+58
-0
source/libs/sync/src/syncRequestVoteReply.c
source/libs/sync/src/syncRequestVoteReply.c
+59
-0
source/libs/sync/src/syncSnapshot.c
source/libs/sync/src/syncSnapshot.c
+509
-10
source/libs/sync/test/CMakeLists.txt
source/libs/sync/test/CMakeLists.txt
+126
-0
source/libs/sync/test/syncConfigChangeSnapshotTest.cpp
source/libs/sync/test/syncConfigChangeSnapshotTest.cpp
+366
-0
source/libs/sync/test/syncConfigChangeTest.cpp
source/libs/sync/test/syncConfigChangeTest.cpp
+0
-1
source/libs/sync/test/syncRaftCfgTest.cpp
source/libs/sync/test/syncRaftCfgTest.cpp
+5
-1
source/libs/sync/test/syncRaftLogTest.cpp
source/libs/sync/test/syncRaftLogTest.cpp
+172
-0
source/libs/sync/test/syncRaftLogTest2.cpp
source/libs/sync/test/syncRaftLogTest2.cpp
+270
-0
source/libs/sync/test/syncRaftLogTest3.cpp
source/libs/sync/test/syncRaftLogTest3.cpp
+337
-0
source/libs/sync/test/syncSnapshotReceiverTest.cpp
source/libs/sync/test/syncSnapshotReceiverTest.cpp
+62
-0
source/libs/sync/test/syncSnapshotRspTest.cpp
source/libs/sync/test/syncSnapshotRspTest.cpp
+99
-0
source/libs/sync/test/syncSnapshotSendTest.cpp
source/libs/sync/test/syncSnapshotSendTest.cpp
+100
-0
source/libs/sync/test/syncSnapshotSenderTest.cpp
source/libs/sync/test/syncSnapshotSenderTest.cpp
+70
-0
source/libs/sync/test/syncTest.cpp
source/libs/sync/test/syncTest.cpp
+5
-2
source/libs/sync/test/syncTestTool.cpp
source/libs/sync/test/syncTestTool.cpp
+385
-0
未找到文件。
include/common/tmsgdef.h
浏览文件 @
a047d4a5
...
...
@@ -236,6 +236,8 @@ enum {
TD_DEF_MSG_TYPE
(
TDMT_SYNC_COMMON_RESPONSE
,
"sync-common-response"
,
NULL
,
NULL
)
TD_DEF_MSG_TYPE
(
TDMT_SYNC_APPLY_MSG
,
"sync-apply-msg"
,
NULL
,
NULL
)
TD_DEF_MSG_TYPE
(
TDMT_SYNC_CONFIG_CHANGE
,
"sync-config-change"
,
NULL
,
NULL
)
TD_DEF_MSG_TYPE
(
TDMT_SYNC_SNAPSHOT_SEND
,
"sync-snapshot-send"
,
NULL
,
NULL
)
TD_DEF_MSG_TYPE
(
TDMT_SYNC_SNAPSHOT_RSP
,
"sync-snapshot-rsp"
,
NULL
,
NULL
)
#if defined(TD_MSG_NUMBER_)
TDMT_MAX
...
...
include/libs/sync/sync.h
浏览文件 @
a047d4a5
...
...
@@ -88,9 +88,10 @@ typedef struct SReConfigCbMeta {
}
SReConfigCbMeta
;
typedef
struct
SSnapshot
{
void
*
data
;
void
*
data
;
SyncIndex
lastApplyIndex
;
SyncTerm
lastApplyTerm
;
SyncIndex
lastConfigIndex
;
}
SSnapshot
;
typedef
struct
SSyncFSM
{
...
...
@@ -141,10 +142,27 @@ typedef struct SSyncLogStore {
// return commit index of log
SyncIndex
(
*
getCommitIndex
)(
struct
SSyncLogStore
*
pLogStore
);
// refactor, log[0 .. n] ==> log[m .. n]
int32_t
(
*
syncLogSetBeginIndex
)(
struct
SSyncLogStore
*
pLogStore
,
SyncIndex
beginIndex
);
SyncIndex
(
*
syncLogBeginIndex
)(
struct
SSyncLogStore
*
pLogStore
);
SyncIndex
(
*
syncLogEndIndex
)(
struct
SSyncLogStore
*
pLogStore
);
bool
(
*
syncLogIsEmpty
)(
struct
SSyncLogStore
*
pLogStore
);
int32_t
(
*
syncLogEntryCount
)(
struct
SSyncLogStore
*
pLogStore
);
bool
(
*
syncLogInRange
)(
struct
SSyncLogStore
*
pLogStore
,
SyncIndex
index
);
SyncIndex
(
*
syncLogWriteIndex
)(
struct
SSyncLogStore
*
pLogStore
);
SyncIndex
(
*
syncLogLastIndex
)(
struct
SSyncLogStore
*
pLogStore
);
SyncTerm
(
*
syncLogLastTerm
)(
struct
SSyncLogStore
*
pLogStore
);
int32_t
(
*
syncLogAppendEntry
)(
struct
SSyncLogStore
*
pLogStore
,
SSyncRaftEntry
*
pEntry
);
int32_t
(
*
syncLogGetEntry
)(
struct
SSyncLogStore
*
pLogStore
,
SyncIndex
index
,
SSyncRaftEntry
**
ppEntry
);
int32_t
(
*
syncLogTruncate
)(
struct
SSyncLogStore
*
pLogStore
,
SyncIndex
fromIndex
);
}
SSyncLogStore
;
typedef
struct
SSyncInfo
{
bool
isStandBy
;
bool
snapshotEnable
;
SyncGroupId
vgId
;
SSyncCfg
syncCfg
;
char
path
[
TSDB_FILENAME_LEN
];
...
...
include/libs/sync/syncTools.h
浏览文件 @
a047d4a5
...
...
@@ -385,6 +385,72 @@ void syncApplyMsgPrint2(char* s, const SyncApplyMsg* pMsg);
void
syncApplyMsgLog
(
const
SyncApplyMsg
*
pMsg
);
void
syncApplyMsgLog2
(
char
*
s
,
const
SyncApplyMsg
*
pMsg
);
// ---------------------------------------------
typedef
struct
SyncSnapshotSend
{
uint32_t
bytes
;
int32_t
vgId
;
uint32_t
msgType
;
SRaftId
srcId
;
SRaftId
destId
;
SyncTerm
term
;
SyncIndex
lastIndex
;
// lastIndex of snapshot
SyncTerm
lastTerm
;
// lastTerm of snapshot
int32_t
seq
;
uint32_t
dataLen
;
char
data
[];
}
SyncSnapshotSend
;
SyncSnapshotSend
*
syncSnapshotSendBuild
(
uint32_t
dataLen
,
int32_t
vgId
);
void
syncSnapshotSendDestroy
(
SyncSnapshotSend
*
pMsg
);
void
syncSnapshotSendSerialize
(
const
SyncSnapshotSend
*
pMsg
,
char
*
buf
,
uint32_t
bufLen
);
void
syncSnapshotSendDeserialize
(
const
char
*
buf
,
uint32_t
len
,
SyncSnapshotSend
*
pMsg
);
char
*
syncSnapshotSendSerialize2
(
const
SyncSnapshotSend
*
pMsg
,
uint32_t
*
len
);
SyncSnapshotSend
*
syncSnapshotSendDeserialize2
(
const
char
*
buf
,
uint32_t
len
);
void
syncSnapshotSend2RpcMsg
(
const
SyncSnapshotSend
*
pMsg
,
SRpcMsg
*
pRpcMsg
);
void
syncSnapshotSendFromRpcMsg
(
const
SRpcMsg
*
pRpcMsg
,
SyncSnapshotSend
*
pMsg
);
SyncSnapshotSend
*
syncSnapshotSendFromRpcMsg2
(
const
SRpcMsg
*
pRpcMsg
);
cJSON
*
syncSnapshotSend2Json
(
const
SyncSnapshotSend
*
pMsg
);
char
*
syncSnapshotSend2Str
(
const
SyncSnapshotSend
*
pMsg
);
// for debug ----------------------
void
syncSnapshotSendPrint
(
const
SyncSnapshotSend
*
pMsg
);
void
syncSnapshotSendPrint2
(
char
*
s
,
const
SyncSnapshotSend
*
pMsg
);
void
syncSnapshotSendLog
(
const
SyncSnapshotSend
*
pMsg
);
void
syncSnapshotSendLog2
(
char
*
s
,
const
SyncSnapshotSend
*
pMsg
);
// ---------------------------------------------
typedef
struct
SyncSnapshotRsp
{
uint32_t
bytes
;
int32_t
vgId
;
uint32_t
msgType
;
SRaftId
srcId
;
SRaftId
destId
;
SyncTerm
term
;
SyncIndex
lastIndex
;
SyncTerm
lastTerm
;
int32_t
ack
;
}
SyncSnapshotRsp
;
SyncSnapshotRsp
*
syncSnapshotRspBuild
(
int32_t
vgId
);
void
syncSnapshotRspDestroy
(
SyncSnapshotRsp
*
pMsg
);
void
syncSnapshotRspSerialize
(
const
SyncSnapshotRsp
*
pMsg
,
char
*
buf
,
uint32_t
bufLen
);
void
syncSnapshotRspDeserialize
(
const
char
*
buf
,
uint32_t
len
,
SyncSnapshotRsp
*
pMsg
);
char
*
syncSnapshotRspSerialize2
(
const
SyncSnapshotRsp
*
pMsg
,
uint32_t
*
len
);
SyncSnapshotRsp
*
syncSnapshotRspDeserialize2
(
const
char
*
buf
,
uint32_t
len
);
void
syncSnapshotRsp2RpcMsg
(
const
SyncSnapshotRsp
*
pMsg
,
SRpcMsg
*
pRpcMsg
);
void
syncSnapshotRspFromRpcMsg
(
const
SRpcMsg
*
pRpcMsg
,
SyncSnapshotRsp
*
pMsg
);
SyncSnapshotRsp
*
syncSnapshotRspFromRpcMsg2
(
const
SRpcMsg
*
pRpcMsg
);
cJSON
*
syncSnapshotRsp2Json
(
const
SyncSnapshotRsp
*
pMsg
);
char
*
syncSnapshotRsp2Str
(
const
SyncSnapshotRsp
*
pMsg
);
// for debug ----------------------
void
syncSnapshotRspPrint
(
const
SyncSnapshotRsp
*
pMsg
);
void
syncSnapshotRspPrint2
(
char
*
s
,
const
SyncSnapshotRsp
*
pMsg
);
void
syncSnapshotRspLog
(
const
SyncSnapshotRsp
*
pMsg
);
void
syncSnapshotRspLog2
(
char
*
s
,
const
SyncSnapshotRsp
*
pMsg
);
// on message ----------------------
int32_t
syncNodeOnPingCb
(
SSyncNode
*
ths
,
SyncPing
*
pMsg
);
int32_t
syncNodeOnPingReplyCb
(
SSyncNode
*
ths
,
SyncPingReply
*
pMsg
);
...
...
@@ -395,6 +461,29 @@ int32_t syncNodeOnRequestVoteReplyCb(SSyncNode* ths, SyncRequestVoteReply* pMsg)
int32_t
syncNodeOnAppendEntriesCb
(
SSyncNode
*
ths
,
SyncAppendEntries
*
pMsg
);
int32_t
syncNodeOnAppendEntriesReplyCb
(
SSyncNode
*
ths
,
SyncAppendEntriesReply
*
pMsg
);
int32_t
syncNodeOnRequestVoteSnapshotCb
(
SSyncNode
*
ths
,
SyncRequestVote
*
pMsg
);
int32_t
syncNodeOnRequestVoteReplySnapshotCb
(
SSyncNode
*
ths
,
SyncRequestVoteReply
*
pMsg
);
int32_t
syncNodeOnAppendEntriesSnapshotCb
(
SSyncNode
*
ths
,
SyncAppendEntries
*
pMsg
);
int32_t
syncNodeOnAppendEntriesReplySnapshotCb
(
SSyncNode
*
ths
,
SyncAppendEntriesReply
*
pMsg
);
int32_t
syncNodeOnSnapshotSendCb
(
SSyncNode
*
ths
,
SyncSnapshotSend
*
pMsg
);
int32_t
syncNodeOnSnapshotRspCb
(
SSyncNode
*
ths
,
SyncSnapshotRsp
*
pMsg
);
// -----------------------------------------
typedef
int32_t
(
*
FpOnPingCb
)(
SSyncNode
*
ths
,
SyncPing
*
pMsg
);
typedef
int32_t
(
*
FpOnPingReplyCb
)(
SSyncNode
*
ths
,
SyncPingReply
*
pMsg
);
typedef
int32_t
(
*
FpOnClientRequestCb
)(
SSyncNode
*
ths
,
SyncClientRequest
*
pMsg
);
typedef
int32_t
(
*
FpOnRequestVoteCb
)(
SSyncNode
*
ths
,
SyncRequestVote
*
pMsg
);
typedef
int32_t
(
*
FpOnRequestVoteReplyCb
)(
SSyncNode
*
ths
,
SyncRequestVoteReply
*
pMsg
);
typedef
int32_t
(
*
FpOnAppendEntriesCb
)(
SSyncNode
*
ths
,
SyncAppendEntries
*
pMsg
);
typedef
int32_t
(
*
FpOnAppendEntriesReplyCb
)(
SSyncNode
*
ths
,
SyncAppendEntriesReply
*
pMsg
);
typedef
int32_t
(
*
FpOnTimeoutCb
)(
SSyncNode
*
pSyncNode
,
SyncTimeout
*
pMsg
);
typedef
int32_t
(
*
FpOnSnapshotSendCb
)(
SSyncNode
*
ths
,
SyncSnapshotSend
*
pMsg
);
typedef
int32_t
(
*
FpOnSnapshotRspCb
)(
SSyncNode
*
ths
,
SyncSnapshotRsp
*
pMsg
);
// option ----------------------------------
bool
syncNodeSnapshotEnable
(
SSyncNode
*
pSyncNode
);
// ---------------------------------------------
#ifdef __cplusplus
...
...
source/dnode/mgmt/mgmt_mnode/src/mmHandle.c
浏览文件 @
a047d4a5
...
...
@@ -216,6 +216,9 @@ SArray *mmGetMsgHandles() {
if
(
dmSetMgmtHandle
(
pArray
,
TDMT_VND_ALTER_CONFIRM_RSP
,
mmPutNodeMsgToWriteQueue
,
0
)
==
NULL
)
goto
_OVER
;
if
(
dmSetMgmtHandle
(
pArray
,
TDMT_VND_COMPACT_RSP
,
mmPutNodeMsgToWriteQueue
,
0
)
==
NULL
)
goto
_OVER
;
if
(
dmSetMgmtHandle
(
pArray
,
TDMT_MON_MM_INFO
,
mmPutNodeMsgToMonitorQueue
,
0
)
==
NULL
)
goto
_OVER
;
if
(
dmSetMgmtHandle
(
pArray
,
TDMT_MON_MM_LOAD
,
mmPutNodeMsgToMonitorQueue
,
0
)
==
NULL
)
goto
_OVER
;
if
(
dmSetMgmtHandle
(
pArray
,
TDMT_SYNC_TIMEOUT
,
mmPutNodeMsgToSyncQueue
,
1
)
==
NULL
)
goto
_OVER
;
if
(
dmSetMgmtHandle
(
pArray
,
TDMT_SYNC_PING
,
mmPutNodeMsgToSyncQueue
,
1
)
==
NULL
)
goto
_OVER
;
if
(
dmSetMgmtHandle
(
pArray
,
TDMT_SYNC_PING_REPLY
,
mmPutNodeMsgToSyncQueue
,
1
)
==
NULL
)
goto
_OVER
;
...
...
@@ -225,9 +228,8 @@ SArray *mmGetMsgHandles() {
if
(
dmSetMgmtHandle
(
pArray
,
TDMT_SYNC_REQUEST_VOTE_REPLY
,
mmPutNodeMsgToSyncQueue
,
1
)
==
NULL
)
goto
_OVER
;
if
(
dmSetMgmtHandle
(
pArray
,
TDMT_SYNC_APPEND_ENTRIES
,
mmPutNodeMsgToSyncQueue
,
1
)
==
NULL
)
goto
_OVER
;
if
(
dmSetMgmtHandle
(
pArray
,
TDMT_SYNC_APPEND_ENTRIES_REPLY
,
mmPutNodeMsgToSyncQueue
,
1
)
==
NULL
)
goto
_OVER
;
if
(
dmSetMgmtHandle
(
pArray
,
TDMT_MON_MM_INFO
,
mmPutNodeMsgToMonitorQueue
,
0
)
==
NULL
)
goto
_OVER
;
if
(
dmSetMgmtHandle
(
pArray
,
TDMT_MON_MM_LOAD
,
mmPutNodeMsgToMonitorQueue
,
0
)
==
NULL
)
goto
_OVER
;
if
(
dmSetMgmtHandle
(
pArray
,
TDMT_SYNC_SNAPSHOT_SEND
,
mmPutNodeMsgToSyncQueue
,
1
)
==
NULL
)
goto
_OVER
;
if
(
dmSetMgmtHandle
(
pArray
,
TDMT_SYNC_SNAPSHOT_RSP
,
mmPutNodeMsgToSyncQueue
,
1
)
==
NULL
)
goto
_OVER
;
code
=
0
;
...
...
source/dnode/mnode/impl/inc/mndInt.h
浏览文件 @
a047d4a5
...
...
@@ -19,6 +19,7 @@
#include "mndDef.h"
#include "sdb.h"
#include "sync.h"
#include "syncTools.h"
#include "tcache.h"
#include "tdatablock.h"
...
...
source/dnode/mnode/impl/src/mndMain.c
浏览文件 @
a047d4a5
...
...
@@ -376,41 +376,93 @@ int32_t mndProcessSyncMsg(SRpcMsg *pMsg) {
syncRpcMsgLog2
(
logBuf
,
pMsg
);
taosMemoryFree
(
syncNodeStr
);
if
(
pMsg
->
msgType
==
TDMT_SYNC_TIMEOUT
)
{
SyncTimeout
*
pSyncMsg
=
syncTimeoutFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnTimeoutCb
(
pSyncNode
,
pSyncMsg
);
syncTimeoutDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_PING
)
{
SyncPing
*
pSyncMsg
=
syncPingFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnPingCb
(
pSyncNode
,
pSyncMsg
);
syncPingDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_PING_REPLY
)
{
SyncPingReply
*
pSyncMsg
=
syncPingReplyFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnPingReplyCb
(
pSyncNode
,
pSyncMsg
);
syncPingReplyDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_CLIENT_REQUEST
)
{
SyncClientRequest
*
pSyncMsg
=
syncClientRequestFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnClientRequestCb
(
pSyncNode
,
pSyncMsg
);
syncClientRequestDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_REQUEST_VOTE
)
{
SyncRequestVote
*
pSyncMsg
=
syncRequestVoteFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnRequestVoteCb
(
pSyncNode
,
pSyncMsg
);
syncRequestVoteDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_REQUEST_VOTE_REPLY
)
{
SyncRequestVoteReply
*
pSyncMsg
=
syncRequestVoteReplyFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnRequestVoteReplyCb
(
pSyncNode
,
pSyncMsg
);
syncRequestVoteReplyDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_APPEND_ENTRIES
)
{
SyncAppendEntries
*
pSyncMsg
=
syncAppendEntriesFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnAppendEntriesCb
(
pSyncNode
,
pSyncMsg
);
syncAppendEntriesDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_APPEND_ENTRIES_REPLY
)
{
SyncAppendEntriesReply
*
pSyncMsg
=
syncAppendEntriesReplyFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnAppendEntriesReplyCb
(
pSyncNode
,
pSyncMsg
);
syncAppendEntriesReplyDestroy
(
pSyncMsg
);
// ToDo: ugly! use function pointer
if
(
syncNodeSnapshotEnable
(
pSyncNode
))
{
if
(
pMsg
->
msgType
==
TDMT_SYNC_TIMEOUT
)
{
SyncTimeout
*
pSyncMsg
=
syncTimeoutFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnTimeoutCb
(
pSyncNode
,
pSyncMsg
);
syncTimeoutDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_PING
)
{
SyncPing
*
pSyncMsg
=
syncPingFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnPingCb
(
pSyncNode
,
pSyncMsg
);
syncPingDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_PING_REPLY
)
{
SyncPingReply
*
pSyncMsg
=
syncPingReplyFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnPingReplyCb
(
pSyncNode
,
pSyncMsg
);
syncPingReplyDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_CLIENT_REQUEST
)
{
SyncClientRequest
*
pSyncMsg
=
syncClientRequestFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnClientRequestCb
(
pSyncNode
,
pSyncMsg
);
syncClientRequestDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_REQUEST_VOTE
)
{
SyncRequestVote
*
pSyncMsg
=
syncRequestVoteFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnRequestVoteSnapshotCb
(
pSyncNode
,
pSyncMsg
);
syncRequestVoteDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_REQUEST_VOTE_REPLY
)
{
SyncRequestVoteReply
*
pSyncMsg
=
syncRequestVoteReplyFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnRequestVoteReplySnapshotCb
(
pSyncNode
,
pSyncMsg
);
syncRequestVoteReplyDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_APPEND_ENTRIES
)
{
SyncAppendEntries
*
pSyncMsg
=
syncAppendEntriesFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnAppendEntriesSnapshotCb
(
pSyncNode
,
pSyncMsg
);
syncAppendEntriesDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_APPEND_ENTRIES_REPLY
)
{
SyncAppendEntriesReply
*
pSyncMsg
=
syncAppendEntriesReplyFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnAppendEntriesReplySnapshotCb
(
pSyncNode
,
pSyncMsg
);
syncAppendEntriesReplyDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_SNAPSHOT_SEND
)
{
SyncSnapshotSend
*
pSyncMsg
=
syncSnapshotSendFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnSnapshotSendCb
(
pSyncNode
,
pSyncMsg
);
syncSnapshotSendDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_SNAPSHOT_RSP
)
{
SyncSnapshotRsp
*
pSyncMsg
=
syncSnapshotRspFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnSnapshotRspCb
(
pSyncNode
,
pSyncMsg
);
syncSnapshotRspDestroy
(
pSyncMsg
);
}
else
{
mError
(
"failed to process msg:%p since invalid type:%s"
,
pMsg
,
TMSG_INFO
(
pMsg
->
msgType
));
code
=
TAOS_SYNC_PROPOSE_OTHER_ERROR
;
}
}
else
{
mError
(
"failed to process msg:%p since invalid type:%s"
,
pMsg
,
TMSG_INFO
(
pMsg
->
msgType
));
code
=
TAOS_SYNC_PROPOSE_OTHER_ERROR
;
if
(
pMsg
->
msgType
==
TDMT_SYNC_TIMEOUT
)
{
SyncTimeout
*
pSyncMsg
=
syncTimeoutFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnTimeoutCb
(
pSyncNode
,
pSyncMsg
);
syncTimeoutDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_PING
)
{
SyncPing
*
pSyncMsg
=
syncPingFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnPingCb
(
pSyncNode
,
pSyncMsg
);
syncPingDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_PING_REPLY
)
{
SyncPingReply
*
pSyncMsg
=
syncPingReplyFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnPingReplyCb
(
pSyncNode
,
pSyncMsg
);
syncPingReplyDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_CLIENT_REQUEST
)
{
SyncClientRequest
*
pSyncMsg
=
syncClientRequestFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnClientRequestCb
(
pSyncNode
,
pSyncMsg
);
syncClientRequestDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_REQUEST_VOTE
)
{
SyncRequestVote
*
pSyncMsg
=
syncRequestVoteFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnRequestVoteCb
(
pSyncNode
,
pSyncMsg
);
syncRequestVoteDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_REQUEST_VOTE_REPLY
)
{
SyncRequestVoteReply
*
pSyncMsg
=
syncRequestVoteReplyFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnRequestVoteReplyCb
(
pSyncNode
,
pSyncMsg
);
syncRequestVoteReplyDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_APPEND_ENTRIES
)
{
SyncAppendEntries
*
pSyncMsg
=
syncAppendEntriesFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnAppendEntriesCb
(
pSyncNode
,
pSyncMsg
);
syncAppendEntriesDestroy
(
pSyncMsg
);
}
else
if
(
pMsg
->
msgType
==
TDMT_SYNC_APPEND_ENTRIES_REPLY
)
{
SyncAppendEntriesReply
*
pSyncMsg
=
syncAppendEntriesReplyFromRpcMsg2
(
pMsg
);
code
=
syncNodeOnAppendEntriesReplyCb
(
pSyncNode
,
pSyncMsg
);
syncAppendEntriesReplyDestroy
(
pSyncMsg
);
}
else
{
mError
(
"failed to process msg:%p since invalid type:%s"
,
pMsg
,
TMSG_INFO
(
pMsg
->
msgType
));
code
=
TAOS_SYNC_PROPOSE_OTHER_ERROR
;
}
}
mndReleaseSyncRef
(
pMnode
);
...
...
source/dnode/mnode/impl/src/mndSync.c
浏览文件 @
a047d4a5
...
...
@@ -34,7 +34,7 @@ void mndSyncCommitMsg(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbM
int32_t
transId
=
sdbGetIdFromRaw
(
pMnode
->
pSdb
,
pRaw
);
pMgmt
->
errCode
=
cbMeta
.
code
;
m
Trace
(
"trans:%d, is proposed, savedTransId:%d code:0x%x, ver
:%"
PRId64
" term:%"
PRId64
" role:%s raw:%p"
,
transId
,
m
Debug
(
"trans:%d, is proposed, saved:%d code:0x%x, index
:%"
PRId64
" term:%"
PRId64
" role:%s raw:%p"
,
transId
,
pMgmt
->
transId
,
cbMeta
.
code
,
cbMeta
.
index
,
cbMeta
.
term
,
syncStr
(
cbMeta
.
state
),
pRaw
);
if
(
pMgmt
->
errCode
==
0
)
{
...
...
@@ -57,8 +57,8 @@ void mndSyncCommitMsg(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbM
int32_t
mndSyncGetSnapshot
(
struct
SSyncFSM
*
pFsm
,
SSnapshot
*
pSnapshot
)
{
SMnode
*
pMnode
=
pFsm
->
data
;
pSnapshot
->
lastApplyIndex
=
sdbGet
Apply
Index
(
pMnode
->
pSdb
);
pSnapshot
->
lastApplyTerm
=
sdbGet
Apply
Term
(
pMnode
->
pSdb
);
pSnapshot
->
lastApplyIndex
=
sdbGet
Commit
Index
(
pMnode
->
pSdb
);
pSnapshot
->
lastApplyTerm
=
sdbGet
Commit
Term
(
pMnode
->
pSdb
);
return
0
;
}
...
...
@@ -78,7 +78,7 @@ void mndReConfig(struct SSyncFSM *pFsm, SSyncCfg newCfg, SReConfigCbMeta cbMeta)
SSyncMgmt
*
pMgmt
=
&
pMnode
->
syncMgmt
;
pMgmt
->
errCode
=
cbMeta
.
code
;
mInfo
(
"trans:-1, sync reconfig is proposed, saved
TransId:%d code:0x%x, curTerm
:%"
PRId64
" term:%"
PRId64
,
mInfo
(
"trans:-1, sync reconfig is proposed, saved
:%d code:0x%x, index
:%"
PRId64
" term:%"
PRId64
,
pMgmt
->
transId
,
cbMeta
.
code
,
cbMeta
.
index
,
cbMeta
.
term
);
if
(
pMgmt
->
transId
==
-
1
)
{
...
...
@@ -167,6 +167,7 @@ int32_t mndInitSync(SMnode *pMnode) {
syncInfo
.
pWal
=
pMgmt
->
pWal
;
syncInfo
.
pFsm
=
mndSyncMakeFsm
(
pMnode
);
syncInfo
.
isStandBy
=
pMgmt
->
standby
;
syncInfo
.
snapshotEnable
=
true
;
SSyncCfg
*
pCfg
=
&
syncInfo
.
syncCfg
;
pCfg
->
replicaNum
=
pMnode
->
replica
;
...
...
source/dnode/mnode/sdb/inc/sdb.h
浏览文件 @
a047d4a5
...
...
@@ -358,9 +358,11 @@ int64_t sdbGetTableVer(SSdb *pSdb, ESdbType type);
* @return int32_t The current index of sdb
*/
void
sdbSetApplyIndex
(
SSdb
*
pSdb
,
int64_t
index
);
int64_t
sdbGetApplyIndex
(
SSdb
*
pSdb
);
void
sdbSetApplyTerm
(
SSdb
*
pSdb
,
int64_t
term
);
int64_t
sdbGetApplyIndex
(
SSdb
*
pSdb
);
int64_t
sdbGetApplyTerm
(
SSdb
*
pSdb
);
int64_t
sdbGetCommitIndex
(
SSdb
*
pSdb
);
int64_t
sdbGetCommitTerm
(
SSdb
*
pSdb
);
SSdbRaw
*
sdbAllocRaw
(
ESdbType
type
,
int8_t
sver
,
int32_t
dataLen
);
void
sdbFreeRaw
(
SSdbRaw
*
pRaw
);
...
...
source/dnode/mnode/sdb/src/sdb.c
浏览文件 @
a047d4a5
...
...
@@ -159,8 +159,12 @@ static int32_t sdbCreateDir(SSdb *pSdb) {
void
sdbSetApplyIndex
(
SSdb
*
pSdb
,
int64_t
index
)
{
pSdb
->
curVer
=
index
;
}
int64_t
sdbGetApplyIndex
(
SSdb
*
pSdb
)
{
return
pSdb
->
curVer
;
}
void
sdbSetApplyTerm
(
SSdb
*
pSdb
,
int64_t
term
)
{
pSdb
->
curTerm
=
term
;
}
int64_t
sdbGetApplyIndex
(
SSdb
*
pSdb
)
{
return
pSdb
->
curVer
;
}
int64_t
sdbGetApplyTerm
(
SSdb
*
pSdb
)
{
return
pSdb
->
curTerm
;
}
int64_t
sdbGetCommitIndex
(
SSdb
*
pSdb
)
{
return
pSdb
->
lastCommitVer
;
}
int64_t
sdbGetCommitTerm
(
SSdb
*
pSdb
)
{
return
pSdb
->
lastCommitTerm
;
}
\ No newline at end of file
source/dnode/mnode/sdb/src/sdbFile.c
浏览文件 @
a047d4a5
...
...
@@ -496,6 +496,8 @@ int32_t sdbStartRead(SSdb *pSdb, SSdbIter **ppIter) {
snprintf
(
datafile
,
sizeof
(
datafile
),
"%s%ssdb.data"
,
pSdb
->
currDir
,
TD_DIRSEP
);
taosThreadMutexLock
(
&
pSdb
->
filelock
);
int64_t
commitIndex
=
pSdb
->
lastCommitVer
;
int64_t
commitTerm
=
pSdb
->
lastCommitTerm
;
if
(
taosCopyFile
(
datafile
,
pIter
->
name
)
<
0
)
{
taosThreadMutexUnlock
(
&
pSdb
->
filelock
);
terrno
=
TAOS_SYSTEM_ERROR
(
errno
);
...
...
@@ -514,7 +516,8 @@ int32_t sdbStartRead(SSdb *pSdb, SSdbIter **ppIter) {
}
*
ppIter
=
pIter
;
mInfo
(
"sdbiter:%p, is created to read snapshot, file:%s"
,
pIter
,
pIter
->
name
);
mInfo
(
"sdbiter:%p, is created to read snapshot, index:%"
PRId64
" term:%"
PRId64
" file:%s"
,
pIter
,
commitIndex
,
commitTerm
,
pIter
->
name
);
return
0
;
}
...
...
source/dnode/vnode/src/vnd/vnodeSync.c
浏览文件 @
a047d4a5
...
...
@@ -26,6 +26,8 @@ static int32_t vnodeSyncGetSnapshot(SSyncFSM *pFsm, SSnapshot *pSnapshot);
int32_t
vnodeSyncOpen
(
SVnode
*
pVnode
,
char
*
path
)
{
SSyncInfo
syncInfo
=
{
.
isStandBy
=
false
,
.
snapshotEnable
=
false
,
.
vgId
=
pVnode
->
config
.
vgId
,
.
isStandBy
=
pVnode
->
config
.
standby
,
.
syncCfg
=
pVnode
->
config
.
syncCfg
,
...
...
source/libs/sync/inc/syncAppendEntries.h
浏览文件 @
a047d4a5
...
...
@@ -93,6 +93,7 @@ extern "C" {
// /\ UNCHANGED <<candidateVars, leaderVars>>
//
int32_t
syncNodeOnAppendEntriesCb
(
SSyncNode
*
ths
,
SyncAppendEntries
*
pMsg
);
int32_t
syncNodeOnAppendEntriesSnapshotCb
(
SSyncNode
*
ths
,
SyncAppendEntries
*
pMsg
);
#ifdef __cplusplus
}
...
...
source/libs/sync/inc/syncAppendEntriesReply.h
浏览文件 @
a047d4a5
...
...
@@ -41,6 +41,7 @@ extern "C" {
// /\ UNCHANGED <<serverVars, candidateVars, logVars, elections>>
//
int32_t
syncNodeOnAppendEntriesReplyCb
(
SSyncNode
*
ths
,
SyncAppendEntriesReply
*
pMsg
);
int32_t
syncNodeOnAppendEntriesReplySnapshotCb
(
SSyncNode
*
ths
,
SyncAppendEntriesReply
*
pMsg
);
#ifdef __cplusplus
}
...
...
source/libs/sync/inc/syncElection.h
浏览文件 @
a047d4a5
...
...
@@ -39,6 +39,8 @@ extern "C" {
// /\ UNCHANGED <<serverVars, candidateVars, leaderVars, logVars>>
//
int32_t
syncNodeRequestVotePeers
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodeRequestVotePeersSnapshot
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodeElect
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodeRequestVote
(
SSyncNode
*
pSyncNode
,
const
SRaftId
*
destRaftId
,
const
SyncRequestVote
*
pMsg
);
...
...
source/libs/sync/inc/syncIO.h
浏览文件 @
a047d4a5
...
...
@@ -57,6 +57,9 @@ typedef struct SSyncIO {
int32_t
(
*
FpOnSyncAppendEntriesReply
)(
SSyncNode
*
pSyncNode
,
SyncAppendEntriesReply
*
pMsg
);
int32_t
(
*
FpOnSyncTimeout
)(
SSyncNode
*
pSyncNode
,
SyncTimeout
*
pMsg
);
int32_t
(
*
FpOnSyncSnapshotSend
)(
SSyncNode
*
pSyncNode
,
SyncSnapshotSend
*
pMsg
);
int32_t
(
*
FpOnSyncSnapshotRsp
)(
SSyncNode
*
pSyncNode
,
SyncSnapshotRsp
*
pMsg
);
int8_t
isStart
;
}
SSyncIO
;
...
...
source/libs/sync/inc/syncInt.h
浏览文件 @
a047d4a5
...
...
@@ -137,24 +137,25 @@ typedef struct SSyncNode {
uint64_t
heartbeatTimerCounter
;
// callback
int32_t
(
*
FpOnPing
)(
SSyncNode
*
ths
,
SyncPing
*
pMsg
);
int32_t
(
*
FpOnPingReply
)(
SSyncNode
*
ths
,
SyncPingReply
*
pMsg
);
int32_t
(
*
FpOnClientRequest
)(
SSyncNode
*
ths
,
SyncClientRequest
*
pMsg
);
int32_t
(
*
FpOnRequestVote
)(
SSyncNode
*
ths
,
SyncRequestVote
*
pMsg
);
int32_t
(
*
FpOnRequestVoteReply
)(
SSyncNode
*
ths
,
SyncRequestVoteReply
*
pMsg
);
int32_t
(
*
FpOnAppendEntries
)(
SSyncNode
*
ths
,
SyncAppendEntries
*
pMsg
);
int32_t
(
*
FpOnAppendEntriesReply
)(
SSyncNode
*
ths
,
SyncAppendEntriesReply
*
pMsg
);
int32_t
(
*
FpOnTimeout
)(
SSyncNode
*
pSyncNode
,
SyncTimeout
*
pMsg
);
FpOnPingCb
FpOnPing
;
FpOnPingReplyCb
FpOnPingReply
;
FpOnClientRequestCb
FpOnClientRequest
;
FpOnTimeoutCb
FpOnTimeout
;
FpOnRequestVoteCb
FpOnRequestVote
;
FpOnRequestVoteReplyCb
FpOnRequestVoteReply
;
FpOnAppendEntriesCb
FpOnAppendEntries
;
FpOnAppendEntriesReplyCb
FpOnAppendEntriesReply
;
FpOnSnapshotSendCb
FpOnSnapshotSend
;
FpOnSnapshotRspCb
FpOnSnapshotRsp
;
// tools
SSyncRespMgr
*
pSyncRespMgr
;
// restore state
// sem_t restoreSem;
bool
restoreFinish
;
SSnapshot
*
pSnapshot
;
SSyncSnapshotSender
*
pSender
;
SSyncSnapshotReceiver
*
pReceiver
;
bool
restoreFinish
;
// SSnapshot* pSnapshot;
SSyncSnapshotSender
*
senders
[
TSDB_MAX_REPLICA
];
SSyncSnapshotReceiver
*
pNewNodeReceiver
;
}
SSyncNode
;
...
...
@@ -164,6 +165,9 @@ void syncNodeStart(SSyncNode* pSyncNode);
void
syncNodeStartStandBy
(
SSyncNode
*
pSyncNode
);
void
syncNodeClose
(
SSyncNode
*
pSyncNode
);
// option
bool
syncNodeSnapshotEnable
(
SSyncNode
*
pSyncNode
);
// ping --------------
int32_t
syncNodePing
(
SSyncNode
*
pSyncNode
,
const
SRaftId
*
destRaftId
,
SyncPing
*
pMsg
);
int32_t
syncNodePingSelf
(
SSyncNode
*
pSyncNode
);
...
...
@@ -205,6 +209,24 @@ void syncNodeCandidate2Follower(SSyncNode* pSyncNode);
void
syncNodeVoteForTerm
(
SSyncNode
*
pSyncNode
,
SyncTerm
term
,
SRaftId
*
pRaftId
);
void
syncNodeVoteForSelf
(
SSyncNode
*
pSyncNode
);
// snapshot --------------
bool
syncNodeHasSnapshot
(
SSyncNode
*
pSyncNode
);
bool
syncNodeIsIndexInSnapshot
(
SSyncNode
*
pSyncNode
,
SyncIndex
index
);
SyncIndex
syncNodeGetLastIndex
(
SSyncNode
*
pSyncNode
);
SyncTerm
syncNodeGetLastTerm
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodeGetLastIndexTerm
(
SSyncNode
*
pSyncNode
,
SyncIndex
*
pLastIndex
,
SyncTerm
*
pLastTerm
);
SyncIndex
syncNodeSyncStartIndex
(
SSyncNode
*
pSyncNode
);
SyncIndex
syncNodeGetPreIndex
(
SSyncNode
*
pSyncNode
,
SyncIndex
index
);
SyncTerm
syncNodeGetPreTerm
(
SSyncNode
*
pSyncNode
,
SyncIndex
index
);
int32_t
syncNodeGetPreIndexTerm
(
SSyncNode
*
pSyncNode
,
SyncIndex
index
,
SyncIndex
*
pPreIndex
,
SyncTerm
*
pPreTerm
);
int32_t
syncNodeCommit
(
SSyncNode
*
ths
,
SyncIndex
beginIndex
,
SyncIndex
endIndex
,
uint64_t
flag
);
bool
syncNodeInRaftGroup
(
SSyncNode
*
ths
,
SRaftId
*
pRaftId
);
// for debug --------------
void
syncNodePrint
(
SSyncNode
*
pObj
);
void
syncNodePrint2
(
char
*
s
,
SSyncNode
*
pObj
);
...
...
source/libs/sync/inc/syncRaftCfg.h
浏览文件 @
a047d4a5
...
...
@@ -34,6 +34,7 @@ typedef struct SRaftCfg {
TdFilePtr
pFile
;
char
path
[
TSDB_FILENAME_LEN
*
2
];
int8_t
isStandBy
;
int8_t
snapshotEnable
;
}
SRaftCfg
;
SRaftCfg
*
raftCfgOpen
(
const
char
*
path
);
...
...
@@ -50,7 +51,12 @@ char * raftCfg2Str(SRaftCfg *pRaftCfg);
int32_t
raftCfgFromJson
(
const
cJSON
*
pRoot
,
SRaftCfg
*
pRaftCfg
);
int32_t
raftCfgFromStr
(
const
char
*
s
,
SRaftCfg
*
pRaftCfg
);
int32_t
raftCfgCreateFile
(
SSyncCfg
*
pCfg
,
int8_t
isStandBy
,
const
char
*
path
);
typedef
struct
SRaftCfgMeta
{
int8_t
isStandBy
;
int8_t
snapshotEnable
;
}
SRaftCfgMeta
;
int32_t
raftCfgCreateFile
(
SSyncCfg
*
pCfg
,
SRaftCfgMeta
meta
,
const
char
*
path
);
// for debug ----------------------
void
syncCfgPrint
(
SSyncCfg
*
pCfg
);
...
...
source/libs/sync/inc/syncRaftLog.h
浏览文件 @
a047d4a5
...
...
@@ -30,6 +30,7 @@ extern "C" {
typedef
struct
SSyncLogStoreData
{
SSyncNode
*
pSyncNode
;
SWal
*
pWal
;
SyncIndex
beginIndex
;
// valid begin index, default 0, may be set beginIndex > 0
}
SSyncLogStoreData
;
SSyncLogStore
*
logStoreCreate
(
SSyncNode
*
pSyncNode
);
...
...
@@ -39,14 +40,7 @@ char* logStore2Str(SSyncLogStore* pLogStore);
cJSON
*
logStoreSimple2Json
(
SSyncLogStore
*
pLogStore
);
char
*
logStoreSimple2Str
(
SSyncLogStore
*
pLogStore
);
// SSyncRaftEntry* logStoreGetLastEntry(SSyncLogStore* pLogStore);
// SyncIndex logStoreLastIndex(SSyncLogStore* pLogStore);
// SyncTerm logStoreLastTerm(SSyncLogStore* pLogStore);
// SSyncRaftEntry* logStoreGetEntry(SSyncLogStore* pLogStore, SyncIndex index);
// int32_t logStoreAppendEntry(SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry);
// int32_t logStoreTruncate(SSyncLogStore* pLogStore, SyncIndex fromIndex);
// int32_t logStoreUpdateCommitIndex(SSyncLogStore* pLogStore, SyncIndex index);
// SyncIndex logStoreGetCommitIndex(SSyncLogStore* pLogStore);
SyncIndex
logStoreFirstIndex
(
SSyncLogStore
*
pLogStore
);
// for debug
void
logStorePrint
(
SSyncLogStore
*
pLogStore
);
...
...
source/libs/sync/inc/syncReplication.h
浏览文件 @
a047d4a5
...
...
@@ -52,6 +52,7 @@ extern "C" {
// /\ UNCHANGED <<serverVars, candidateVars, leaderVars, logVars>>
//
int32_t
syncNodeAppendEntriesPeers
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodeAppendEntriesPeersSnapshot
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodeReplicate
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodeAppendEntries
(
SSyncNode
*
pSyncNode
,
const
SRaftId
*
destRaftId
,
const
SyncAppendEntries
*
pMsg
);
...
...
source/libs/sync/inc/syncRequestVote.h
浏览文件 @
a047d4a5
...
...
@@ -50,6 +50,7 @@ extern "C" {
// /\ UNCHANGED <<state, currentTerm, candidateVars, leaderVars, logVars>>
//
int32_t
syncNodeOnRequestVoteCb
(
SSyncNode
*
ths
,
SyncRequestVote
*
pMsg
);
int32_t
syncNodeOnRequestVoteSnapshotCb
(
SSyncNode
*
ths
,
SyncRequestVote
*
pMsg
);
#ifdef __cplusplus
}
...
...
source/libs/sync/inc/syncRequestVoteReply.h
浏览文件 @
a047d4a5
...
...
@@ -45,6 +45,7 @@ extern "C" {
// /\ UNCHANGED <<serverVars, votedFor, leaderVars, logVars>>
//
int32_t
syncNodeOnRequestVoteReplyCb
(
SSyncNode
*
ths
,
SyncRequestVoteReply
*
pMsg
);
int32_t
syncNodeOnRequestVoteReplySnapshotCb
(
SSyncNode
*
ths
,
SyncRequestVoteReply
*
pMsg
);
#ifdef __cplusplus
}
...
...
source/libs/sync/inc/syncSnapshot.h
浏览文件 @
a047d4a5
...
...
@@ -25,40 +25,62 @@ extern "C" {
#include <stdlib.h>
#include "cJSON.h"
#include "syncInt.h"
#include "syncMessage.h"
#include "taosdef.h"
#define SYNC_SNAPSHOT_SEQ_INVALID -1
#define SYNC_SNAPSHOT_SEQ_FORCE_CLOSE -2
#define SYNC_SNAPSHOT_SEQ_BEGIN 0
#define SYNC_SNAPSHOT_SEQ_END 0x7FFFFFFF
#define SYNC_SNAPSHOT_RETRY_MS 5000
typedef
struct
SSyncSnapshotSender
{
int32_t
sending
;
int32_t
received
;
bool
finish
;
void
*
pCurrentBlock
;
bool
start
;
int32_t
seq
;
int32_t
ack
;
void
*
pReader
;
void
*
pCurrentBlock
;
int32_t
blockLen
;
SSnapshot
snapshot
;
int64_t
sendingMS
;
SSnapshot
*
pSnapshot
;
SSyncNode
*
pSyncNode
;
int32_t
replicaIndex
;
SyncTerm
term
;
bool
finish
;
}
SSyncSnapshotSender
;
SSyncSnapshotSender
*
snapshotSenderCreate
(
SSyncNode
*
pSyncNode
);
void
snapshotSenderDoStart
(
SSyncSnapshotSender
*
pSender
);
SSyncSnapshotSender
*
snapshotSenderCreate
(
SSyncNode
*
pSyncNode
,
int32_t
replicaIndex
);
void
snapshotSenderDestroy
(
SSyncSnapshotSender
*
pSender
);
void
snapshotSenderStart
(
SSyncSnapshotSender
*
pSender
);
void
snapshotSenderStop
(
SSyncSnapshotSender
*
pSender
);
int32_t
snapshotSend
(
SSyncSnapshotSender
*
pSender
);
cJSON
*
snapshotSender2Json
(
SSyncSnapshotSender
*
pSender
);
char
*
snapshotSender2Str
(
SSyncSnapshotSender
*
pSender
);
int32_t
snapshotReSend
(
SSyncSnapshotSender
*
pSender
);
cJSON
*
snapshotSender2Json
(
SSyncSnapshotSender
*
pSender
);
char
*
snapshotSender2Str
(
SSyncSnapshotSender
*
pSender
);
typedef
struct
SSyncSnapshotReceiver
{
bool
start
;
int32_t
received
;
int32_t
progressIndex
;
void
*
pCurrentBlock
;
int32_t
len
;
SSnapshot
*
pSnapshot
;
bool
start
;
int32_t
ack
;
void
*
pWriter
;
SyncTerm
term
;
SSyncNode
*
pSyncNode
;
int32_t
replicaIndex
;
}
SSyncSnapshotReceiver
;
SSyncSnapshotReceiver
*
snapshotReceiverCreate
(
SSyncNode
*
pSyncNode
);
SSyncSnapshotReceiver
*
snapshotReceiverCreate
(
SSyncNode
*
pSyncNode
,
int32_t
replicaIndex
);
void
snapshotReceiverDestroy
(
SSyncSnapshotReceiver
*
pReceiver
);
int32_t
snapshotReceive
(
SSyncSnapshotReceiver
*
pReceiver
);
cJSON
*
snapshotReceiver2Json
(
SSyncSnapshotReceiver
*
pReceiver
);
char
*
snapshotReceiver2Str
(
SSyncSnapshotReceiver
*
pReceiver
);
void
snapshotReceiverStart
(
SSyncSnapshotReceiver
*
pReceiver
);
void
snapshotReceiverStop
(
SSyncSnapshotReceiver
*
pReceiver
);
cJSON
*
snapshotReceiver2Json
(
SSyncSnapshotReceiver
*
pReceiver
);
char
*
snapshotReceiver2Str
(
SSyncSnapshotReceiver
*
pReceiver
);
int32_t
syncNodeOnSnapshotSendCb
(
SSyncNode
*
ths
,
SyncSnapshotSend
*
pMsg
);
int32_t
syncNodeOnSnapshotRspCb
(
SSyncNode
*
ths
,
SyncSnapshotRsp
*
pMsg
);
#ifdef __cplusplus
}
...
...
source/libs/sync/src/syncAppendEntries.c
浏览文件 @
a047d4a5
...
...
@@ -20,6 +20,7 @@
#include "syncRaftStore.h"
#include "syncUtil.h"
#include "syncVoteMgr.h"
#include "wal.h"
// TLA+ Spec
// HandleAppendEntriesRequest(i, j, m) ==
...
...
@@ -335,8 +336,12 @@ int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg) {
cbMeta
.
currentTerm
=
ths
->
pRaftStore
->
currentTerm
;
cbMeta
.
flag
=
0x11
;
SSnapshot
snapshot
;
ASSERT
(
ths
->
pFsm
->
FpGetSnapshot
!=
NULL
);
ths
->
pFsm
->
FpGetSnapshot
(
ths
->
pFsm
,
&
snapshot
);
bool
needExecute
=
true
;
if
(
ths
->
pSnapshot
!=
NULL
&&
cbMeta
.
index
<=
ths
->
pSnapshot
->
lastApplyIndex
)
{
if
(
cbMeta
.
index
<=
snapshot
.
lastApplyIndex
)
{
needExecute
=
false
;
}
...
...
@@ -427,3 +432,266 @@ int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg) {
return
ret
;
}
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
;
}
static
int32_t
syncNodeMakeLogSame
(
SSyncNode
*
ths
,
SyncAppendEntries
*
pMsg
,
SSyncRaftEntry
**
ppAppendEntry
,
bool
*
pEntryAlreadyWritten
)
{
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
))
{
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
);
}
syncEntryDestory
(
pRollBackEntry
);
}
}
// delete confict entries
code
=
ths
->
pLogStore
->
syncLogTruncate
(
ths
->
pLogStore
,
extraIndex
);
ASSERT
(
code
==
0
);
}
return
0
;
}
static
int32_t
syncNodePreCommit
(
SSyncNode
*
ths
,
SSyncRaftEntry
*
pEntry
)
{
SRpcMsg
rpcMsg
;
syncEntry2OriginalRpc
(
pEntry
,
&
rpcMsg
);
if
(
ths
->
pFsm
!=
NULL
)
{
if
(
ths
->
pFsm
->
FpPreCommitCb
!=
NULL
&&
syncUtilUserPreCommit
(
pEntry
->
originalRpcType
))
{
SFsmCbMeta
cbMeta
;
cbMeta
.
index
=
pEntry
->
index
;
cbMeta
.
isWeak
=
pEntry
->
isWeak
;
cbMeta
.
code
=
2
;
cbMeta
.
state
=
ths
->
state
;
cbMeta
.
seqNum
=
pEntry
->
seqNum
;
ths
->
pFsm
->
FpPreCommitCb
(
ths
->
pFsm
,
&
rpcMsg
,
cbMeta
);
}
}
rpcFreeCont
(
rpcMsg
.
pCont
);
return
0
;
}
int32_t
syncNodeOnAppendEntriesSnapshotCb
(
SSyncNode
*
ths
,
SyncAppendEntries
*
pMsg
)
{
int32_t
ret
=
0
;
int32_t
code
=
0
;
// print log
char
logBuf
[
128
]
=
{
0
};
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"recv SyncAppendEntries, term:%lu"
,
ths
->
pRaftStore
->
currentTerm
);
syncAppendEntriesLog2
(
logBuf
,
pMsg
);
// maybe update term
if
(
pMsg
->
term
>
ths
->
pRaftStore
->
currentTerm
)
{
syncNodeUpdateTerm
(
ths
,
pMsg
->
term
);
}
ASSERT
(
pMsg
->
term
<=
ths
->
pRaftStore
->
currentTerm
);
// reset elect timer
if
(
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
)
{
ths
->
leaderCache
=
pMsg
->
srcId
;
syncNodeResetElectTimer
(
ths
);
}
ASSERT
(
pMsg
->
dataLen
>=
0
);
bool
logOK
=
syncNodeOnAppendEntriesLogOK
(
ths
,
pMsg
);
// case1, reject request
if
((
pMsg
->
term
<
ths
->
pRaftStore
->
currentTerm
)
||
((
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
)
&&
(
ths
->
state
==
TAOS_SYNC_STATE_FOLLOWER
)
&&
!
logOK
))
{
sTrace
(
"recv SyncAppendEntries, reject, receive_term:%lu, current_term:%lu, ths->state:%d, logOK:%d"
,
pMsg
->
term
,
ths
->
pRaftStore
->
currentTerm
,
ths
->
state
,
logOK
);
// send response
SyncAppendEntriesReply
*
pReply
=
syncAppendEntriesReplyBuild
(
ths
->
vgId
);
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
;
}
// case 2, return to follower state
if
(
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
&&
ths
->
state
==
TAOS_SYNC_STATE_CANDIDATE
)
{
sTrace
(
"recv SyncAppendEntries, return to follower, receive_term:%lu, current_term:%lu, ths->state:%d, logOK:%d"
,
pMsg
->
term
,
ths
->
pRaftStore
->
currentTerm
,
ths
->
state
,
logOK
);
syncNodeBecomeFollower
(
ths
);
// ret or reply?
return
ret
;
}
// case 3, accept request
if
(
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
&&
ths
->
state
==
TAOS_SYNC_STATE_FOLLOWER
&&
logOK
)
{
// 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
;
sTrace
(
"recv SyncAppendEntries, accept, receive_term:%lu, current_term:%lu, ths->state:%d, logOK:%d, "
"hasExtraEntries:%d, hasAppendEntries:%d"
,
pMsg
->
term
,
ths
->
pRaftStore
->
currentTerm
,
ths
->
state
,
logOK
,
hasExtraEntries
,
hasAppendEntries
);
if
(
hasExtraEntries
&&
hasAppendEntries
)
{
// make log same
SSyncRaftEntry
*
pAppendEntry
;
bool
entryAlreadyWritten
;
code
=
syncNodeMakeLogSame
(
ths
,
pMsg
,
&
pAppendEntry
,
&
entryAlreadyWritten
);
ASSERT
(
code
==
0
);
ASSERT
(
pAppendEntry
!=
NULL
);
if
(
!
entryAlreadyWritten
)
{
// append new entries
code
=
ths
->
pLogStore
->
syncLogAppendEntry
(
ths
->
pLogStore
,
pAppendEntry
);
ASSERT
(
code
==
0
);
// pre commit
code
=
syncNodePreCommit
(
ths
,
pAppendEntry
);
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
);
}
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
;
if
(
hasAppendEntries
)
{
pReply
->
matchIndex
=
pMsg
->
prevLogIndex
+
1
;
}
else
{
pReply
->
matchIndex
=
pMsg
->
prevLogIndex
;
}
// send response
SRpcMsg
rpcMsg
;
syncAppendEntriesReply2RpcMsg
(
pReply
,
&
rpcMsg
);
syncNodeSendMsgById
(
&
pReply
->
destId
,
ths
,
&
rpcMsg
);
syncAppendEntriesReplyDestroy
(
pReply
);
// maybe update commit index from leader
if
(
pMsg
->
commitIndex
>
ths
->
commitIndex
)
{
// has commit entry in local
if
(
pMsg
->
commitIndex
<=
ths
->
pLogStore
->
syncLogLastIndex
(
ths
->
pLogStore
))
{
SyncIndex
beginIndex
=
ths
->
commitIndex
+
1
;
SyncIndex
endIndex
=
pMsg
->
commitIndex
;
// update commit index
ths
->
commitIndex
=
pMsg
->
commitIndex
;
// call back Wal
code
=
ths
->
pLogStore
->
updateCommitIndex
(
ths
->
pLogStore
,
ths
->
commitIndex
);
ASSERT
(
code
==
0
);
code
=
syncNodeCommit
(
ths
,
beginIndex
,
endIndex
,
ths
->
state
);
ASSERT
(
code
==
0
);
}
}
}
return
ret
;
}
\ No newline at end of file
source/libs/sync/src/syncAppendEntriesReply.c
浏览文件 @
a047d4a5
...
...
@@ -19,6 +19,7 @@
#include "syncInt.h"
#include "syncRaftLog.h"
#include "syncRaftStore.h"
#include "syncSnapshot.h"
#include "syncUtil.h"
#include "syncVoteMgr.h"
...
...
@@ -94,3 +95,124 @@ int32_t syncNodeOnAppendEntriesReplyCb(SSyncNode* ths, SyncAppendEntriesReply* p
return
ret
;
}
int32_t
syncNodeOnAppendEntriesReplySnapshotCb
(
SSyncNode
*
ths
,
SyncAppendEntriesReply
*
pMsg
)
{
int32_t
ret
=
0
;
// print log
char
logBuf
[
128
]
=
{
0
};
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"recv SyncAppendEntriesReply, term:%lu"
,
ths
->
pRaftStore
->
currentTerm
);
syncAppendEntriesReplyLog2
(
logBuf
,
pMsg
);
// if already drop replica, do not process
if
(
!
syncNodeInRaftGroup
(
ths
,
&
(
pMsg
->
srcId
)))
{
sInfo
(
"maybe already dropped"
);
return
ret
;
}
// drop stale response
if
(
pMsg
->
term
<
ths
->
pRaftStore
->
currentTerm
)
{
sTrace
(
"recv SyncAppendEntriesReply, drop stale response, receive_term:%lu current_term:%lu"
,
pMsg
->
term
,
ths
->
pRaftStore
->
currentTerm
);
return
ret
;
}
syncIndexMgrLog2
(
"recv SyncAppendEntriesReply, before pNextIndex:"
,
ths
->
pNextIndex
);
syncIndexMgrLog2
(
"recv SyncAppendEntriesReply, before pMatchIndex:"
,
ths
->
pMatchIndex
);
{
SSnapshot
snapshot
;
ths
->
pFsm
->
FpGetSnapshot
(
ths
->
pFsm
,
&
snapshot
);
sTrace
(
"recv SyncAppendEntriesReply, before snapshot.lastApplyIndex:%ld, snapshot.lastApplyTerm:%lu"
,
snapshot
.
lastApplyIndex
,
snapshot
.
lastApplyTerm
);
}
// 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
)
{
char
logBuf
[
128
]
=
{
0
};
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"recv SyncAppendEntriesReply, error term, receive_term:%lu current_term:%lu"
,
pMsg
->
term
,
ths
->
pRaftStore
->
currentTerm
);
syncNodeLog2
(
logBuf
,
ths
);
sError
(
"%s"
,
logBuf
);
return
ret
;
}
ASSERT
(
pMsg
->
term
==
ths
->
pRaftStore
->
currentTerm
);
if
(
pMsg
->
success
)
{
// nextIndex' = [nextIndex EXCEPT ![i][j] = m.mmatchIndex + 1]
syncIndexMgrSetIndex
(
ths
->
pNextIndex
,
&
(
pMsg
->
srcId
),
pMsg
->
matchIndex
+
1
);
sTrace
(
"update next index:%ld, success:%d"
,
pMsg
->
matchIndex
+
1
,
pMsg
->
success
);
// matchIndex' = [matchIndex EXCEPT ![i][j] = m.mmatchIndex]
syncIndexMgrSetIndex
(
ths
->
pMatchIndex
,
&
(
pMsg
->
srcId
),
pMsg
->
matchIndex
);
// maybe commit
if
(
ths
->
state
==
TAOS_SYNC_STATE_LEADER
)
{
syncMaybeAdvanceCommitIndex
(
ths
);
}
}
else
{
SyncIndex
nextIndex
=
syncIndexMgrGetIndex
(
ths
->
pNextIndex
,
&
(
pMsg
->
srcId
));
sTrace
(
"begin to update next index:%ld, success:%d"
,
nextIndex
,
pMsg
->
success
);
// notice! int64, uint64
if
(
nextIndex
>
SYNC_INDEX_BEGIN
)
{
--
nextIndex
;
// has snapshot
if
(
syncNodeHasSnapshot
(
ths
))
{
// get sender
SSyncSnapshotSender
*
pSender
=
NULL
;
for
(
int
i
=
0
;
i
<
ths
->
replicaNum
;
++
i
)
{
if
(
syncUtilSameId
(
&
(
pMsg
->
srcId
),
&
((
ths
->
replicasId
)[
i
])))
{
pSender
=
(
ths
->
senders
)[
i
];
}
}
ASSERT
(
pSender
!=
NULL
);
SyncIndex
sentryIndex
;
if
(
pSender
->
start
&&
pSender
->
term
==
ths
->
pRaftStore
->
currentTerm
)
{
// already start
sentryIndex
=
pSender
->
snapshot
.
lastApplyIndex
;
sTrace
(
"sending snapshot already start: pSender->term:%lu, ths->pRaftStore->currentTerm:%lu"
,
pSender
->
term
,
ths
->
pRaftStore
->
currentTerm
);
}
else
{
// start send snapshot, first time
sTrace
(
"sending snapshot start first: pSender->term:%lu, ths->pRaftStore->currentTerm:%lu"
,
pSender
->
term
,
ths
->
pRaftStore
->
currentTerm
);
snapshotSenderDoStart
(
pSender
);
pSender
->
start
=
true
;
sentryIndex
=
pSender
->
snapshot
.
lastApplyIndex
;
}
// update nextIndex to sentryIndex + 1
if
(
nextIndex
<=
sentryIndex
)
{
nextIndex
=
sentryIndex
+
1
;
}
}
}
else
{
nextIndex
=
SYNC_INDEX_BEGIN
;
}
syncIndexMgrSetIndex
(
ths
->
pNextIndex
,
&
(
pMsg
->
srcId
),
nextIndex
);
sTrace
(
"update next index:%ld, success:%d"
,
nextIndex
,
pMsg
->
success
);
}
syncIndexMgrLog2
(
"recv SyncAppendEntriesReply, after pNextIndex:"
,
ths
->
pNextIndex
);
syncIndexMgrLog2
(
"recv SyncAppendEntriesReply, after pMatchIndex:"
,
ths
->
pMatchIndex
);
{
SSnapshot
snapshot
;
ths
->
pFsm
->
FpGetSnapshot
(
ths
->
pFsm
,
&
snapshot
);
sTrace
(
"recv SyncAppendEntriesReply, after snapshot.lastApplyIndex:%ld, snapshot.lastApplyTerm:%lu"
,
snapshot
.
lastApplyIndex
,
snapshot
.
lastApplyTerm
);
}
return
ret
;
}
\ No newline at end of file
source/libs/sync/src/syncCommit.c
浏览文件 @
a047d4a5
...
...
@@ -94,6 +94,10 @@ void syncMaybeAdvanceCommitIndex(SSyncNode* pSyncNode) {
// execute fsm
if
(
pSyncNode
->
pFsm
!=
NULL
)
{
int32_t
code
=
syncNodeCommit
(
pSyncNode
,
beginIndex
,
endIndex
,
pSyncNode
->
state
);
ASSERT
(
code
==
0
);
#if 0
for (SyncIndex i = beginIndex; i <= endIndex; ++i) {
if (i != SYNC_INDEX_INVALID) {
SSyncRaftEntry* pEntry = pSyncNode->pLogStore->getEntry(pSyncNode->pLogStore, i);
...
...
@@ -113,8 +117,12 @@ void syncMaybeAdvanceCommitIndex(SSyncNode* pSyncNode) {
cbMeta.currentTerm = pSyncNode->pRaftStore->currentTerm;
cbMeta.flag = 0x1;
SSnapshot snapshot;
ASSERT(pSyncNode->pFsm->FpGetSnapshot != NULL);
pSyncNode->pFsm->FpGetSnapshot(pSyncNode->pFsm, &snapshot);
bool needExecute = true;
if
(
pSyncNode
->
pSnapshot
!=
NULL
&&
cbMeta
.
index
<=
pSyncNode
->
pSnapshot
->
lastApplyIndex
)
{
if (
cbMeta.index <= snapshot.
lastApplyIndex) {
needExecute = false;
}
...
...
@@ -197,6 +205,7 @@ void syncMaybeAdvanceCommitIndex(SSyncNode* pSyncNode) {
syncEntryDestory(pEntry);
}
}
#endif
}
}
}
...
...
source/libs/sync/src/syncElection.c
浏览文件 @
a047d4a5
...
...
@@ -15,6 +15,7 @@
#include "syncElection.h"
#include "syncMessage.h"
#include "syncRaftCfg.h"
#include "syncRaftStore.h"
#include "syncVoteMgr.h"
...
...
@@ -49,6 +50,26 @@ int32_t syncNodeRequestVotePeers(SSyncNode* pSyncNode) {
return
ret
;
}
int32_t
syncNodeRequestVotePeersSnapshot
(
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
)
{
int32_t
ret
=
0
;
if
(
pSyncNode
->
state
==
TAOS_SYNC_STATE_FOLLOWER
)
{
...
...
@@ -71,7 +92,12 @@ int32_t syncNodeElect(SSyncNode* pSyncNode) {
return
ret
;
}
ret
=
syncNodeRequestVotePeers
(
pSyncNode
);
if
(
pSyncNode
->
pRaftCfg
->
snapshotEnable
)
{
ret
=
syncNodeRequestVotePeersSnapshot
(
pSyncNode
);
}
else
{
ret
=
syncNodeRequestVotePeers
(
pSyncNode
);
}
assert
(
ret
==
0
);
syncNodeResetElectTimer
(
pSyncNode
);
...
...
source/libs/sync/src/syncIO.c
浏览文件 @
a047d4a5
...
...
@@ -75,7 +75,8 @@ int32_t syncIOSendMsg(const SEpSet *pEpSet, SRpcMsg *pMsg) {
syncUtilMsgNtoH
(
pMsg
->
pCont
);
char
logBuf
[
256
]
=
{
0
};
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"==syncIOSendMsg== %s:%d"
,
pEpSet
->
eps
[
0
].
fqdn
,
pEpSet
->
eps
[
0
].
port
);
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"==syncIOSendMsg== %s:%d msgType:%d"
,
pEpSet
->
eps
[
0
].
fqdn
,
pEpSet
->
eps
[
0
].
port
,
pMsg
->
msgType
);
syncRpcMsgLog2
(
logBuf
,
pMsg
);
syncUtilMsgHtoN
(
pMsg
->
pCont
);
...
...
@@ -89,8 +90,10 @@ int32_t syncIOSendMsg(const SEpSet *pEpSet, SRpcMsg *pMsg) {
int32_t
syncIOEqMsg
(
const
SMsgCb
*
msgcb
,
SRpcMsg
*
pMsg
)
{
int32_t
ret
=
0
;
char
logBuf
[
128
]
=
{
0
};
syncRpcMsgLog2
((
char
*
)
"==syncIOEqMsg=="
,
pMsg
);
char
logBuf
[
256
]
=
{
0
};
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"==syncIOEqMsg== msgType:%d"
,
pMsg
->
msgType
);
syncRpcMsgLog2
(
logBuf
,
pMsg
);
SRpcMsg
*
pTemp
;
pTemp
=
taosAllocateQitem
(
sizeof
(
SRpcMsg
),
DEF_QITEM
);
...
...
@@ -253,7 +256,9 @@ static void *syncIOConsumerFunc(void *param) {
for
(
int
i
=
0
;
i
<
numOfMsgs
;
++
i
)
{
taosGetQitem
(
qall
,
(
void
**
)
&
pRpcMsg
);
syncRpcMsgLog2
((
char
*
)
"==syncIOConsumerFunc=="
,
pRpcMsg
);
char
logBuf
[
128
];
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"==syncIOConsumMsg== msgType:%d"
,
pRpcMsg
->
msgType
);
syncRpcMsgLog2
(
logBuf
,
pRpcMsg
);
// use switch case instead of if else
if
(
pRpcMsg
->
msgType
==
TDMT_SYNC_PING
)
{
...
...
@@ -319,6 +324,23 @@ static void *syncIOConsumerFunc(void *param) {
io
->
FpOnSyncTimeout
(
io
->
pSyncNode
,
pSyncMsg
);
syncTimeoutDestroy
(
pSyncMsg
);
}
}
else
if
(
pRpcMsg
->
msgType
==
TDMT_SYNC_SNAPSHOT_SEND
)
{
if
(
io
->
FpOnSyncSnapshotSend
!=
NULL
)
{
SyncSnapshotSend
*
pSyncMsg
=
syncSnapshotSendFromRpcMsg2
(
pRpcMsg
);
assert
(
pSyncMsg
!=
NULL
);
io
->
FpOnSyncSnapshotSend
(
io
->
pSyncNode
,
pSyncMsg
);
syncSnapshotSendDestroy
(
pSyncMsg
);
}
}
else
if
(
pRpcMsg
->
msgType
==
TDMT_SYNC_SNAPSHOT_RSP
)
{
if
(
io
->
FpOnSyncSnapshotRsp
!=
NULL
)
{
SyncSnapshotRsp
*
pSyncMsg
=
syncSnapshotRspFromRpcMsg2
(
pRpcMsg
);
assert
(
pSyncMsg
!=
NULL
);
io
->
FpOnSyncSnapshotRsp
(
io
->
pSyncNode
,
pSyncMsg
);
syncSnapshotRspDestroy
(
pSyncMsg
);
}
}
else
{
sTrace
(
"unknown msgType:%d, no operator"
,
pRpcMsg
->
msgType
);
}
...
...
source/libs/sync/src/syncMain.c
浏览文件 @
a047d4a5
...
...
@@ -29,6 +29,7 @@
#include "syncRequestVote.h"
#include "syncRequestVoteReply.h"
#include "syncRespMgr.h"
#include "syncSnapshot.h"
#include "syncTimeout.h"
#include "syncUtil.h"
#include "syncVoteMgr.h"
...
...
@@ -411,8 +412,11 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pOldSyncInfo) {
snprintf
(
pSyncNode
->
configPath
,
sizeof
(
pSyncNode
->
configPath
),
"%s/raft_config.json"
,
pSyncInfo
->
path
);
if
(
!
taosCheckExistFile
(
pSyncNode
->
configPath
))
{
// create raft config file
ret
=
raftCfgCreateFile
((
SSyncCfg
*
)
&
(
pSyncInfo
->
syncCfg
),
pSyncInfo
->
isStandBy
,
pSyncNode
->
configPath
);
// create a new raft config file
SRaftCfgMeta
meta
;
meta
.
isStandBy
=
pSyncInfo
->
isStandBy
;
meta
.
snapshotEnable
=
pSyncInfo
->
snapshotEnable
;
ret
=
raftCfgCreateFile
((
SSyncCfg
*
)
&
(
pSyncInfo
->
syncCfg
),
meta
,
pSyncNode
->
configPath
);
assert
(
ret
==
0
);
}
else
{
...
...
@@ -552,25 +556,50 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pOldSyncInfo) {
pSyncNode
->
FpOnPing
=
syncNodeOnPingCb
;
pSyncNode
->
FpOnPingReply
=
syncNodeOnPingReplyCb
;
pSyncNode
->
FpOnClientRequest
=
syncNodeOnClientRequestCb
;
pSyncNode
->
FpOnRequestVote
=
syncNodeOnRequestVoteCb
;
pSyncNode
->
FpOnRequestVoteReply
=
syncNodeOnRequestVoteReplyCb
;
pSyncNode
->
FpOnAppendEntries
=
syncNodeOnAppendEntriesCb
;
pSyncNode
->
FpOnAppendEntriesReply
=
syncNodeOnAppendEntriesReplyCb
;
pSyncNode
->
FpOnTimeout
=
syncNodeOnTimeoutCb
;
pSyncNode
->
FpOnSnapshotSend
=
syncNodeOnSnapshotSendCb
;
pSyncNode
->
FpOnSnapshotRsp
=
syncNodeOnSnapshotRspCb
;
if
(
pSyncNode
->
pRaftCfg
->
snapshotEnable
)
{
sInfo
(
"sync node use snapshot"
);
pSyncNode
->
FpOnRequestVote
=
syncNodeOnRequestVoteSnapshotCb
;
pSyncNode
->
FpOnRequestVoteReply
=
syncNodeOnRequestVoteReplySnapshotCb
;
pSyncNode
->
FpOnAppendEntries
=
syncNodeOnAppendEntriesSnapshotCb
;
pSyncNode
->
FpOnAppendEntriesReply
=
syncNodeOnAppendEntriesReplySnapshotCb
;
}
else
{
sInfo
(
"sync node do not use snapshot"
);
pSyncNode
->
FpOnRequestVote
=
syncNodeOnRequestVoteCb
;
pSyncNode
->
FpOnRequestVoteReply
=
syncNodeOnRequestVoteReplyCb
;
pSyncNode
->
FpOnAppendEntries
=
syncNodeOnAppendEntriesCb
;
pSyncNode
->
FpOnAppendEntriesReply
=
syncNodeOnAppendEntriesReplyCb
;
}
// tools
pSyncNode
->
pSyncRespMgr
=
syncRespMgrCreate
(
NULL
,
0
);
assert
(
pSyncNode
->
pSyncRespMgr
!=
NULL
);
// restore state
pSyncNode
->
restoreFinish
=
false
;
pSyncNode
->
pSnapshot
=
NULL
;
if
(
pSyncNode
->
pFsm
->
FpGetSnapshot
!=
NULL
)
{
pSyncNode
->
pSnapshot
=
taosMemoryMalloc
(
sizeof
(
SSnapshot
));
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSyncNode
->
pFsm
,
pSyncNode
->
pSnapshot
);
}
// pSyncNode->pSnapshot = NULL;
// if (pSyncNode->pFsm->FpGetSnapshot != NULL) {
// pSyncNode->pSnapshot = taosMemoryMalloc(sizeof(SSnapshot));
// pSyncNode->pFsm->FpGetSnapshot(pSyncNode->pFsm, pSyncNode->pSnapshot);
// }
// tsem_init(&(pSyncNode->restoreSem), 0, 0);
// snapshot senders
for
(
int
i
=
0
;
i
<
TSDB_MAX_REPLICA
;
++
i
)
{
SSyncSnapshotSender
*
pSender
=
snapshotSenderCreate
(
pSyncNode
,
i
);
// ASSERT(pSender != NULL);
(
pSyncNode
->
senders
)[
i
]
=
pSender
;
}
// snapshot receivers
pSyncNode
->
pNewNodeReceiver
=
snapshotReceiverCreate
(
pSyncNode
,
100
);
// start in syncNodeStart
// start raft
// syncNodeBecomeFollower(pSyncNode);
...
...
@@ -581,6 +610,7 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pOldSyncInfo) {
void
syncNodeStart
(
SSyncNode
*
pSyncNode
)
{
// start raft
if
(
pSyncNode
->
replicaNum
==
1
)
{
raftStoreNextTerm
(
pSyncNode
->
pRaftStore
);
syncNodeBecomeLeader
(
pSyncNode
);
syncNodeLog2
(
"==state change become leader immediately=="
,
pSyncNode
);
...
...
@@ -662,9 +692,23 @@ void syncNodeClose(SSyncNode* pSyncNode) {
taosMemoryFree
(
pSyncNode
->
pFsm
);
}
for
(
int
i
=
0
;
i
<
TSDB_MAX_REPLICA
;
++
i
)
{
if
((
pSyncNode
->
senders
)[
i
]
!=
NULL
)
{
snapshotSenderDestroy
((
pSyncNode
->
senders
)[
i
]);
(
pSyncNode
->
senders
)[
i
]
=
NULL
;
}
}
if
(
pSyncNode
->
pNewNodeReceiver
!=
NULL
)
{
snapshotReceiverDestroy
(
pSyncNode
->
pNewNodeReceiver
);
pSyncNode
->
pNewNodeReceiver
=
NULL
;
}
/*
if (pSyncNode->pSnapshot != NULL) {
taosMemoryFree(pSyncNode->pSnapshot);
}
*/
// tsem_destroy(&pSyncNode->restoreSem);
...
...
@@ -672,6 +716,9 @@ void syncNodeClose(SSyncNode* pSyncNode) {
// taosMemoryFree(pSyncNode);
}
// option
bool
syncNodeSnapshotEnable
(
SSyncNode
*
pSyncNode
)
{
return
pSyncNode
->
pRaftCfg
->
snapshotEnable
;
}
// ping --------------
int32_t
syncNodePing
(
SSyncNode
*
pSyncNode
,
const
SRaftId
*
destRaftId
,
SyncPing
*
pMsg
)
{
syncPingLog2
((
char
*
)
"==syncNodePing=="
,
pMsg
);
...
...
@@ -952,6 +999,20 @@ cJSON* syncNode2Json(const SSyncNode* pSyncNode) {
cJSON_AddStringToObject
(
pRoot
,
"FpOnAppendEntriesReply"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pSyncNode
->
FpOnTimeout
);
cJSON_AddStringToObject
(
pRoot
,
"FpOnTimeout"
,
u64buf
);
// restoreFinish
cJSON_AddNumberToObject
(
pRoot
,
"restoreFinish"
,
pSyncNode
->
restoreFinish
);
// snapshot senders
cJSON
*
pSenders
=
cJSON_CreateArray
();
cJSON_AddItemToObject
(
pRoot
,
"senders"
,
pSenders
);
for
(
int
i
=
0
;
i
<
TSDB_MAX_REPLICA
;
++
i
)
{
cJSON_AddItemToArray
(
pSenders
,
snapshotSender2Json
((
pSyncNode
->
senders
)[
i
]));
}
// snapshot receivers
cJSON
*
pReceivers
=
cJSON_CreateArray
();
cJSON_AddItemToObject
(
pRoot
,
"receiver"
,
snapshotReceiver2Json
(
pSyncNode
->
pNewNodeReceiver
));
}
cJSON
*
pJson
=
cJSON_CreateObject
();
...
...
@@ -1103,7 +1164,15 @@ void syncNodeBecomeLeader(SSyncNode* pSyncNode) {
for
(
int
i
=
0
;
i
<
pSyncNode
->
pNextIndex
->
replicaNum
;
++
i
)
{
// maybe overwrite myself, no harm
// just do it!
pSyncNode
->
pNextIndex
->
index
[
i
]
=
pSyncNode
->
pLogStore
->
getLastIndex
(
pSyncNode
->
pLogStore
)
+
1
;
// pSyncNode->pNextIndex->index[i] = pSyncNode->pLogStore->getLastIndex(pSyncNode->pLogStore) + 1;
// maybe wal is deleted
SyncIndex
lastIndex
;
SyncTerm
lastTerm
;
int32_t
code
=
syncNodeGetLastIndexTerm
(
pSyncNode
,
&
lastIndex
,
&
lastTerm
);
ASSERT
(
code
==
0
);
pSyncNode
->
pNextIndex
->
index
[
i
]
=
lastIndex
+
1
;
}
for
(
int
i
=
0
;
i
<
pSyncNode
->
pMatchIndex
->
replicaNum
;
++
i
)
{
...
...
@@ -1186,6 +1255,168 @@ void syncNodeVoteForSelf(SSyncNode* pSyncNode) {
syncRequestVoteReplyDestroy
(
pMsg
);
}
// snapshot --------------
bool
syncNodeHasSnapshot
(
SSyncNode
*
pSyncNode
)
{
bool
ret
=
false
;
SSnapshot
snapshot
=
{.
data
=
NULL
,
.
lastApplyIndex
=
-
1
,
.
lastApplyTerm
=
0
};
if
(
pSyncNode
->
pFsm
->
FpGetSnapshot
!=
NULL
)
{
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSyncNode
->
pFsm
,
&
snapshot
);
if
(
snapshot
.
lastApplyIndex
>=
SYNC_INDEX_BEGIN
)
{
ret
=
true
;
}
}
return
ret
;
}
bool
syncNodeIsIndexInSnapshot
(
SSyncNode
*
pSyncNode
,
SyncIndex
index
)
{
ASSERT
(
syncNodeHasSnapshot
(
pSyncNode
));
ASSERT
(
pSyncNode
->
pFsm
->
FpGetSnapshot
!=
NULL
);
ASSERT
(
index
>=
SYNC_INDEX_BEGIN
);
SSnapshot
snapshot
;
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSyncNode
->
pFsm
,
&
snapshot
);
bool
b
=
(
index
<=
snapshot
.
lastApplyIndex
);
return
b
;
}
SyncIndex
syncNodeGetLastIndex
(
SSyncNode
*
pSyncNode
)
{
SSnapshot
snapshot
=
{.
data
=
NULL
,
.
lastApplyIndex
=
-
1
,
.
lastApplyTerm
=
0
};
if
(
pSyncNode
->
pFsm
->
FpGetSnapshot
!=
NULL
)
{
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSyncNode
->
pFsm
,
&
snapshot
);
}
SyncIndex
logLastIndex
=
pSyncNode
->
pLogStore
->
syncLogLastIndex
(
pSyncNode
->
pLogStore
);
SyncIndex
lastIndex
=
logLastIndex
>
snapshot
.
lastApplyIndex
?
logLastIndex
:
snapshot
.
lastApplyIndex
;
return
lastIndex
;
}
SyncTerm
syncNodeGetLastTerm
(
SSyncNode
*
pSyncNode
)
{
SyncTerm
lastTerm
=
0
;
if
(
syncNodeHasSnapshot
(
pSyncNode
))
{
// has snapshot
SSnapshot
snapshot
=
{.
data
=
NULL
,
.
lastApplyIndex
=
-
1
,
.
lastApplyTerm
=
0
};
if
(
pSyncNode
->
pFsm
->
FpGetSnapshot
!=
NULL
)
{
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSyncNode
->
pFsm
,
&
snapshot
);
}
if
(
pSyncNode
->
pLogStore
->
syncLogEntryCount
(
pSyncNode
->
pLogStore
)
>
0
)
{
// has log
SyncIndex
logLastIndex
=
pSyncNode
->
pLogStore
->
syncLogLastIndex
(
pSyncNode
->
pLogStore
);
if
(
logLastIndex
>
snapshot
.
lastApplyIndex
)
{
lastTerm
=
pSyncNode
->
pLogStore
->
syncLogLastTerm
(
pSyncNode
->
pLogStore
);
}
else
{
lastTerm
=
snapshot
.
lastApplyTerm
;
}
}
else
{
// no log
lastTerm
=
snapshot
.
lastApplyTerm
;
}
}
else
{
// no snapshot
lastTerm
=
pSyncNode
->
pLogStore
->
syncLogLastTerm
(
pSyncNode
->
pLogStore
);
}
return
lastTerm
;
}
// get last index and term along with snapshot
int32_t
syncNodeGetLastIndexTerm
(
SSyncNode
*
pSyncNode
,
SyncIndex
*
pLastIndex
,
SyncTerm
*
pLastTerm
)
{
*
pLastIndex
=
syncNodeGetLastIndex
(
pSyncNode
);
*
pLastTerm
=
syncNodeGetLastTerm
(
pSyncNode
);
return
0
;
}
SyncIndex
syncNodeSyncStartIndex
(
SSyncNode
*
pSyncNode
)
{
SyncIndex
syncStartIndex
=
syncNodeGetLastIndex
(
pSyncNode
)
+
1
;
return
syncStartIndex
;
}
SyncIndex
syncNodeGetPreIndex
(
SSyncNode
*
pSyncNode
,
SyncIndex
index
)
{
ASSERT
(
index
>=
SYNC_INDEX_BEGIN
);
SyncIndex
syncStartIndex
=
syncNodeSyncStartIndex
(
pSyncNode
);
ASSERT
(
index
<=
syncStartIndex
);
SyncIndex
preIndex
;
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
;
}
SyncTerm
syncNodeGetPreTerm
(
SSyncNode
*
pSyncNode
,
SyncIndex
index
)
{
ASSERT
(
index
>=
SYNC_INDEX_BEGIN
);
SyncIndex
syncStartIndex
=
syncNodeSyncStartIndex
(
pSyncNode
);
ASSERT
(
index
<=
syncStartIndex
);
if
(
index
==
SYNC_INDEX_BEGIN
)
{
return
0
;
}
SyncTerm
preTerm
=
0
;
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);
if
(
index
>
snapshot
.
lastApplyIndex
+
1
)
{
// should be log preTerm
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
if
(
index
==
snapshot
.
lastApplyIndex
+
1
)
{
preTerm
=
snapshot
.
lastApplyTerm
;
}
else
{
// ASSERT(0);
// maybe snapshot change
preTerm
=
snapshot
.
lastApplyTerm
;
}
}
else
{
// no snapshot
ASSERT
(
index
>
SYNC_INDEX_BEGIN
);
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
);
}
return
preTerm
;
}
// get pre index and term of "index"
int32_t
syncNodeGetPreIndexTerm
(
SSyncNode
*
pSyncNode
,
SyncIndex
index
,
SyncIndex
*
pPreIndex
,
SyncTerm
*
pPreTerm
)
{
*
pPreIndex
=
syncNodeGetPreIndex
(
pSyncNode
,
index
);
*
pPreTerm
=
syncNodeGetPreTerm
(
pSyncNode
,
index
);
return
0
;
}
// for debug --------------
void
syncNodePrint
(
SSyncNode
*
pObj
)
{
char
*
serialized
=
syncNode2Str
(
pObj
);
...
...
@@ -1327,7 +1558,8 @@ static int32_t syncNodeAppendNoop(SSyncNode* ths) {
assert
(
pEntry
!=
NULL
);
if
(
ths
->
state
==
TAOS_SYNC_STATE_LEADER
)
{
ths
->
pLogStore
->
appendEntry
(
ths
->
pLogStore
,
pEntry
);
// ths->pLogStore->appendEntry(ths->pLogStore, pEntry);
ths
->
pLogStore
->
syncLogAppendEntry
(
ths
->
pLogStore
,
pEntry
);
syncNodeReplicate
(
ths
);
}
...
...
@@ -1383,13 +1615,14 @@ int32_t syncNodeOnClientRequestCb(SSyncNode* ths, SyncClientRequest* pMsg) {
int32_t
ret
=
0
;
syncClientRequestLog2
(
"==syncNodeOnClientRequestCb=="
,
pMsg
);
SyncIndex
index
=
ths
->
pLogStore
->
getLastIndex
(
ths
->
pLogStore
)
+
1
;
SyncIndex
index
=
ths
->
pLogStore
->
syncLogWriteIndex
(
ths
->
pLogStore
)
;
SyncTerm
term
=
ths
->
pRaftStore
->
currentTerm
;
SSyncRaftEntry
*
pEntry
=
syncEntryBuild2
((
SyncClientRequest
*
)
pMsg
,
term
,
index
);
assert
(
pEntry
!=
NULL
);
if
(
ths
->
state
==
TAOS_SYNC_STATE_LEADER
)
{
ths
->
pLogStore
->
appendEntry
(
ths
->
pLogStore
,
pEntry
);
// ths->pLogStore->appendEntry(ths->pLogStore, pEntry);
ths
->
pLogStore
->
syncLogAppendEntry
(
ths
->
pLogStore
,
pEntry
);
// start replicate right now!
syncNodeReplicate
(
ths
);
...
...
@@ -1459,3 +1692,126 @@ const char* syncStr(ESyncState state) {
return
"error"
;
}
}
int32_t
syncNodeCommit
(
SSyncNode
*
ths
,
SyncIndex
beginIndex
,
SyncIndex
endIndex
,
uint64_t
flag
)
{
int32_t
code
=
0
;
ESyncState
state
=
flag
;
sInfo
(
"sync event commit from %ld to %ld, %s"
,
beginIndex
,
endIndex
,
syncUtilState2String
(
state
));
// maybe execute by leader, skip snapshot
SSnapshot
snapshot
=
{.
data
=
NULL
,
.
lastApplyIndex
=
-
1
,
.
lastApplyTerm
=
0
};
if
(
ths
->
pFsm
->
FpGetSnapshot
!=
NULL
)
{
ths
->
pFsm
->
FpGetSnapshot
(
ths
->
pFsm
,
&
snapshot
);
}
if
(
beginIndex
<=
snapshot
.
lastApplyIndex
)
{
beginIndex
=
snapshot
.
lastApplyIndex
+
1
;
}
// execute fsm
if
(
ths
->
pFsm
!=
NULL
)
{
for
(
SyncIndex
i
=
beginIndex
;
i
<=
endIndex
;
++
i
)
{
if
(
i
!=
SYNC_INDEX_INVALID
)
{
SSyncRaftEntry
*
pEntry
;
code
=
ths
->
pLogStore
->
syncLogGetEntry
(
ths
->
pLogStore
,
i
,
&
pEntry
);
ASSERT
(
code
==
0
);
ASSERT
(
pEntry
!=
NULL
);
SRpcMsg
rpcMsg
;
syncEntry2OriginalRpc
(
pEntry
,
&
rpcMsg
);
if
(
ths
->
pFsm
->
FpCommitCb
!=
NULL
&&
syncUtilUserCommit
(
pEntry
->
originalRpcType
))
{
SFsmCbMeta
cbMeta
;
cbMeta
.
index
=
pEntry
->
index
;
cbMeta
.
isWeak
=
pEntry
->
isWeak
;
cbMeta
.
code
=
0
;
cbMeta
.
state
=
ths
->
state
;
cbMeta
.
seqNum
=
pEntry
->
seqNum
;
cbMeta
.
term
=
pEntry
->
term
;
cbMeta
.
currentTerm
=
ths
->
pRaftStore
->
currentTerm
;
cbMeta
.
flag
=
flag
;
ths
->
pFsm
->
FpCommitCb
(
ths
->
pFsm
,
&
rpcMsg
,
cbMeta
);
}
// config change
if
(
pEntry
->
originalRpcType
==
TDMT_SYNC_CONFIG_CHANGE
)
{
SSyncCfg
oldSyncCfg
=
ths
->
pRaftCfg
->
cfg
;
SSyncCfg
newSyncCfg
;
int32_t
ret
=
syncCfgFromStr
(
rpcMsg
.
pCont
,
&
newSyncCfg
);
ASSERT
(
ret
==
0
);
// update new config myIndex
bool
hit
=
false
;
for
(
int
i
=
0
;
i
<
newSyncCfg
.
replicaNum
;
++
i
)
{
if
(
strcmp
(
ths
->
myNodeInfo
.
nodeFqdn
,
(
newSyncCfg
.
nodeInfo
)[
i
].
nodeFqdn
)
==
0
&&
ths
->
myNodeInfo
.
nodePort
==
(
newSyncCfg
.
nodeInfo
)[
i
].
nodePort
)
{
newSyncCfg
.
myIndex
=
i
;
hit
=
true
;
break
;
}
}
SReConfigCbMeta
cbMeta
=
{
0
};
bool
isDrop
;
// I am in newConfig
if
(
hit
)
{
syncNodeUpdateConfig
(
ths
,
&
newSyncCfg
,
&
isDrop
);
// change isStandBy to normal
if
(
!
isDrop
)
{
if
(
ths
->
state
==
TAOS_SYNC_STATE_LEADER
)
{
syncNodeBecomeLeader
(
ths
);
}
else
{
syncNodeBecomeFollower
(
ths
);
}
}
char
*
sOld
=
syncCfg2Str
(
&
oldSyncCfg
);
char
*
sNew
=
syncCfg2Str
(
&
newSyncCfg
);
sInfo
(
"==config change== 0x11 old:%s new:%s isDrop:%d
\n
"
,
sOld
,
sNew
,
isDrop
);
taosMemoryFree
(
sOld
);
taosMemoryFree
(
sNew
);
}
// always call FpReConfigCb
if
(
ths
->
pFsm
->
FpReConfigCb
!=
NULL
)
{
cbMeta
.
code
=
0
;
cbMeta
.
currentTerm
=
ths
->
pRaftStore
->
currentTerm
;
cbMeta
.
index
=
pEntry
->
index
;
cbMeta
.
term
=
pEntry
->
term
;
cbMeta
.
oldCfg
=
oldSyncCfg
;
cbMeta
.
flag
=
0x11
;
cbMeta
.
isDrop
=
isDrop
;
ths
->
pFsm
->
FpReConfigCb
(
ths
->
pFsm
,
newSyncCfg
,
cbMeta
);
}
}
// restore finish
if
(
pEntry
->
index
==
ths
->
pLogStore
->
syncLogLastIndex
(
ths
->
pLogStore
))
{
if
(
ths
->
restoreFinish
==
false
)
{
if
(
ths
->
pFsm
->
FpRestoreFinishCb
!=
NULL
)
{
ths
->
pFsm
->
FpRestoreFinishCb
(
ths
->
pFsm
);
}
ths
->
restoreFinish
=
true
;
sInfo
(
"restore finish %p vgId:%d"
,
ths
,
ths
->
vgId
);
}
}
rpcFreeCont
(
rpcMsg
.
pCont
);
syncEntryDestory
(
pEntry
);
}
}
}
return
0
;
}
bool
syncNodeInRaftGroup
(
SSyncNode
*
ths
,
SRaftId
*
pRaftId
)
{
for
(
int
i
=
0
;
i
<
ths
->
replicaNum
;
++
i
)
{
if
(
syncUtilSameId
(
&
((
ths
->
replicasId
)[
i
]),
pRaftId
))
{
return
true
;
}
}
return
false
;
}
\ No newline at end of file
source/libs/sync/src/syncMessage.c
浏览文件 @
a047d4a5
...
...
@@ -65,6 +65,16 @@ cJSON* syncRpcMsg2Json(SRpcMsg* pRpcMsg) {
pRoot
=
syncAppendEntriesReply2Json
(
pSyncMsg
);
syncAppendEntriesReplyDestroy
(
pSyncMsg
);
}
else
if
(
pRpcMsg
->
msgType
==
TDMT_SYNC_SNAPSHOT_SEND
)
{
SyncSnapshotSend
*
pSyncMsg
=
syncSnapshotSendDeserialize2
(
pRpcMsg
->
pCont
,
pRpcMsg
->
contLen
);
pRoot
=
syncSnapshotSend2Json
(
pSyncMsg
);
syncSnapshotSendDestroy
(
pSyncMsg
);
}
else
if
(
pRpcMsg
->
msgType
==
TDMT_SYNC_SNAPSHOT_RSP
)
{
SyncSnapshotRsp
*
pSyncMsg
=
syncSnapshotRspDeserialize2
(
pRpcMsg
->
pCont
,
pRpcMsg
->
contLen
);
pRoot
=
syncSnapshotRsp2Json
(
pSyncMsg
);
syncSnapshotRspDestroy
(
pSyncMsg
);
}
else
if
(
pRpcMsg
->
msgType
==
TDMT_SYNC_COMMON_RESPONSE
)
{
pRoot
=
cJSON_CreateObject
();
char
*
s
;
...
...
@@ -1690,3 +1700,322 @@ void syncApplyMsgLog2(char* s, const SyncApplyMsg* pMsg) {
sTrace
(
"syncApplyMsgLog2 | len:%lu | %s | %s"
,
strlen
(
serialized
),
s
,
serialized
);
taosMemoryFree
(
serialized
);
}
// ---------------------------------------------
SyncSnapshotSend
*
syncSnapshotSendBuild
(
uint32_t
dataLen
,
int32_t
vgId
)
{
uint32_t
bytes
=
sizeof
(
SyncSnapshotSend
)
+
dataLen
;
SyncSnapshotSend
*
pMsg
=
taosMemoryMalloc
(
bytes
);
memset
(
pMsg
,
0
,
bytes
);
pMsg
->
bytes
=
bytes
;
pMsg
->
vgId
=
vgId
;
pMsg
->
msgType
=
TDMT_SYNC_SNAPSHOT_SEND
;
pMsg
->
dataLen
=
dataLen
;
return
pMsg
;
}
void
syncSnapshotSendDestroy
(
SyncSnapshotSend
*
pMsg
)
{
if
(
pMsg
!=
NULL
)
{
taosMemoryFree
(
pMsg
);
}
}
void
syncSnapshotSendSerialize
(
const
SyncSnapshotSend
*
pMsg
,
char
*
buf
,
uint32_t
bufLen
)
{
assert
(
pMsg
->
bytes
<=
bufLen
);
memcpy
(
buf
,
pMsg
,
pMsg
->
bytes
);
}
void
syncSnapshotSendDeserialize
(
const
char
*
buf
,
uint32_t
len
,
SyncSnapshotSend
*
pMsg
)
{
memcpy
(
pMsg
,
buf
,
len
);
assert
(
len
==
pMsg
->
bytes
);
assert
(
pMsg
->
bytes
==
sizeof
(
SyncSnapshotSend
)
+
pMsg
->
dataLen
);
}
char
*
syncSnapshotSendSerialize2
(
const
SyncSnapshotSend
*
pMsg
,
uint32_t
*
len
)
{
char
*
buf
=
taosMemoryMalloc
(
pMsg
->
bytes
);
assert
(
buf
!=
NULL
);
syncSnapshotSendSerialize
(
pMsg
,
buf
,
pMsg
->
bytes
);
if
(
len
!=
NULL
)
{
*
len
=
pMsg
->
bytes
;
}
return
buf
;
}
SyncSnapshotSend
*
syncSnapshotSendDeserialize2
(
const
char
*
buf
,
uint32_t
len
)
{
uint32_t
bytes
=
*
((
uint32_t
*
)
buf
);
SyncSnapshotSend
*
pMsg
=
taosMemoryMalloc
(
bytes
);
assert
(
pMsg
!=
NULL
);
syncSnapshotSendDeserialize
(
buf
,
len
,
pMsg
);
assert
(
len
==
pMsg
->
bytes
);
return
pMsg
;
}
void
syncSnapshotSend2RpcMsg
(
const
SyncSnapshotSend
*
pMsg
,
SRpcMsg
*
pRpcMsg
)
{
memset
(
pRpcMsg
,
0
,
sizeof
(
*
pRpcMsg
));
pRpcMsg
->
msgType
=
pMsg
->
msgType
;
pRpcMsg
->
contLen
=
pMsg
->
bytes
;
pRpcMsg
->
pCont
=
rpcMallocCont
(
pRpcMsg
->
contLen
);
syncSnapshotSendSerialize
(
pMsg
,
pRpcMsg
->
pCont
,
pRpcMsg
->
contLen
);
}
void
syncSnapshotSendFromRpcMsg
(
const
SRpcMsg
*
pRpcMsg
,
SyncSnapshotSend
*
pMsg
)
{
syncSnapshotSendDeserialize
(
pRpcMsg
->
pCont
,
pRpcMsg
->
contLen
,
pMsg
);
}
SyncSnapshotSend
*
syncSnapshotSendFromRpcMsg2
(
const
SRpcMsg
*
pRpcMsg
)
{
SyncSnapshotSend
*
pMsg
=
syncSnapshotSendDeserialize2
(
pRpcMsg
->
pCont
,
pRpcMsg
->
contLen
);
assert
(
pMsg
!=
NULL
);
return
pMsg
;
}
cJSON
*
syncSnapshotSend2Json
(
const
SyncSnapshotSend
*
pMsg
)
{
char
u64buf
[
128
];
cJSON
*
pRoot
=
cJSON_CreateObject
();
if
(
pMsg
!=
NULL
)
{
cJSON_AddNumberToObject
(
pRoot
,
"bytes"
,
pMsg
->
bytes
);
cJSON_AddNumberToObject
(
pRoot
,
"vgId"
,
pMsg
->
vgId
);
cJSON_AddNumberToObject
(
pRoot
,
"msgType"
,
pMsg
->
msgType
);
cJSON
*
pSrcId
=
cJSON_CreateObject
();
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pMsg
->
srcId
.
addr
);
cJSON_AddStringToObject
(
pSrcId
,
"addr"
,
u64buf
);
{
uint64_t
u64
=
pMsg
->
srcId
.
addr
;
cJSON
*
pTmp
=
pSrcId
;
char
host
[
128
];
uint16_t
port
;
syncUtilU642Addr
(
u64
,
host
,
sizeof
(
host
),
&
port
);
cJSON_AddStringToObject
(
pTmp
,
"addr_host"
,
host
);
cJSON_AddNumberToObject
(
pTmp
,
"addr_port"
,
port
);
}
cJSON_AddNumberToObject
(
pSrcId
,
"vgId"
,
pMsg
->
srcId
.
vgId
);
cJSON_AddItemToObject
(
pRoot
,
"srcId"
,
pSrcId
);
cJSON
*
pDestId
=
cJSON_CreateObject
();
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pMsg
->
destId
.
addr
);
cJSON_AddStringToObject
(
pDestId
,
"addr"
,
u64buf
);
{
uint64_t
u64
=
pMsg
->
destId
.
addr
;
cJSON
*
pTmp
=
pDestId
;
char
host
[
128
];
uint16_t
port
;
syncUtilU642Addr
(
u64
,
host
,
sizeof
(
host
),
&
port
);
cJSON_AddStringToObject
(
pTmp
,
"addr_host"
,
host
);
cJSON_AddNumberToObject
(
pTmp
,
"addr_port"
,
port
);
}
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
),
"%ld"
,
pMsg
->
lastIndex
);
cJSON_AddStringToObject
(
pRoot
,
"lastIndex"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pMsg
->
lastTerm
);
cJSON_AddStringToObject
(
pRoot
,
"lastTerm"
,
u64buf
);
cJSON_AddNumberToObject
(
pRoot
,
"seq"
,
pMsg
->
seq
);
cJSON_AddNumberToObject
(
pRoot
,
"dataLen"
,
pMsg
->
dataLen
);
char
*
s
;
s
=
syncUtilprintBin
((
char
*
)(
pMsg
->
data
),
pMsg
->
dataLen
);
cJSON_AddStringToObject
(
pRoot
,
"data"
,
s
);
taosMemoryFree
(
s
);
s
=
syncUtilprintBin2
((
char
*
)(
pMsg
->
data
),
pMsg
->
dataLen
);
cJSON_AddStringToObject
(
pRoot
,
"data2"
,
s
);
taosMemoryFree
(
s
);
}
cJSON
*
pJson
=
cJSON_CreateObject
();
cJSON_AddItemToObject
(
pJson
,
"SyncSnapshotSend"
,
pRoot
);
return
pJson
;
}
char
*
syncSnapshotSend2Str
(
const
SyncSnapshotSend
*
pMsg
)
{
cJSON
*
pJson
=
syncSnapshotSend2Json
(
pMsg
);
char
*
serialized
=
cJSON_Print
(
pJson
);
cJSON_Delete
(
pJson
);
return
serialized
;
}
// for debug ----------------------
void
syncSnapshotSendPrint
(
const
SyncSnapshotSend
*
pMsg
)
{
char
*
serialized
=
syncSnapshotSend2Str
(
pMsg
);
printf
(
"syncSnapshotSendPrint | len:%lu | %s
\n
"
,
strlen
(
serialized
),
serialized
);
fflush
(
NULL
);
taosMemoryFree
(
serialized
);
}
void
syncSnapshotSendPrint2
(
char
*
s
,
const
SyncSnapshotSend
*
pMsg
)
{
char
*
serialized
=
syncSnapshotSend2Str
(
pMsg
);
printf
(
"syncSnapshotSendPrint2 | len:%lu | %s | %s
\n
"
,
strlen
(
serialized
),
s
,
serialized
);
fflush
(
NULL
);
taosMemoryFree
(
serialized
);
}
void
syncSnapshotSendLog
(
const
SyncSnapshotSend
*
pMsg
)
{
char
*
serialized
=
syncSnapshotSend2Str
(
pMsg
);
sTrace
(
"syncSnapshotSendLog | len:%lu | %s"
,
strlen
(
serialized
),
serialized
);
taosMemoryFree
(
serialized
);
}
void
syncSnapshotSendLog2
(
char
*
s
,
const
SyncSnapshotSend
*
pMsg
)
{
char
*
serialized
=
syncSnapshotSend2Str
(
pMsg
);
sTrace
(
"syncSnapshotSendLog2 | len:%lu | %s | %s"
,
strlen
(
serialized
),
s
,
serialized
);
taosMemoryFree
(
serialized
);
}
// ---------------------------------------------
SyncSnapshotRsp
*
syncSnapshotRspBuild
(
int32_t
vgId
)
{
uint32_t
bytes
=
sizeof
(
SyncSnapshotRsp
);
SyncSnapshotRsp
*
pMsg
=
taosMemoryMalloc
(
bytes
);
memset
(
pMsg
,
0
,
bytes
);
pMsg
->
bytes
=
bytes
;
pMsg
->
vgId
=
vgId
;
pMsg
->
msgType
=
TDMT_SYNC_SNAPSHOT_RSP
;
return
pMsg
;
}
void
syncSnapshotRspDestroy
(
SyncSnapshotRsp
*
pMsg
)
{
if
(
pMsg
!=
NULL
)
{
taosMemoryFree
(
pMsg
);
}
}
void
syncSnapshotRspSerialize
(
const
SyncSnapshotRsp
*
pMsg
,
char
*
buf
,
uint32_t
bufLen
)
{
assert
(
pMsg
->
bytes
<=
bufLen
);
memcpy
(
buf
,
pMsg
,
pMsg
->
bytes
);
}
void
syncSnapshotRspDeserialize
(
const
char
*
buf
,
uint32_t
len
,
SyncSnapshotRsp
*
pMsg
)
{
memcpy
(
pMsg
,
buf
,
len
);
assert
(
len
==
pMsg
->
bytes
);
}
char
*
syncSnapshotRspSerialize2
(
const
SyncSnapshotRsp
*
pMsg
,
uint32_t
*
len
)
{
char
*
buf
=
taosMemoryMalloc
(
pMsg
->
bytes
);
assert
(
buf
!=
NULL
);
syncSnapshotRspSerialize
(
pMsg
,
buf
,
pMsg
->
bytes
);
if
(
len
!=
NULL
)
{
*
len
=
pMsg
->
bytes
;
}
return
buf
;
}
SyncSnapshotRsp
*
syncSnapshotRspDeserialize2
(
const
char
*
buf
,
uint32_t
len
)
{
uint32_t
bytes
=
*
((
uint32_t
*
)
buf
);
SyncSnapshotRsp
*
pMsg
=
taosMemoryMalloc
(
bytes
);
assert
(
pMsg
!=
NULL
);
syncSnapshotRspDeserialize
(
buf
,
len
,
pMsg
);
assert
(
len
==
pMsg
->
bytes
);
return
pMsg
;
}
void
syncSnapshotRsp2RpcMsg
(
const
SyncSnapshotRsp
*
pMsg
,
SRpcMsg
*
pRpcMsg
)
{
memset
(
pRpcMsg
,
0
,
sizeof
(
*
pRpcMsg
));
pRpcMsg
->
msgType
=
pMsg
->
msgType
;
pRpcMsg
->
contLen
=
pMsg
->
bytes
;
pRpcMsg
->
pCont
=
rpcMallocCont
(
pRpcMsg
->
contLen
);
syncSnapshotRspSerialize
(
pMsg
,
pRpcMsg
->
pCont
,
pRpcMsg
->
contLen
);
}
void
syncSnapshotRspFromRpcMsg
(
const
SRpcMsg
*
pRpcMsg
,
SyncSnapshotRsp
*
pMsg
)
{
syncSnapshotRspDeserialize
(
pRpcMsg
->
pCont
,
pRpcMsg
->
contLen
,
pMsg
);
}
SyncSnapshotRsp
*
syncSnapshotRspFromRpcMsg2
(
const
SRpcMsg
*
pRpcMsg
)
{
SyncSnapshotRsp
*
pMsg
=
syncSnapshotRspDeserialize2
(
pRpcMsg
->
pCont
,
pRpcMsg
->
contLen
);
assert
(
pMsg
!=
NULL
);
return
pMsg
;
}
cJSON
*
syncSnapshotRsp2Json
(
const
SyncSnapshotRsp
*
pMsg
)
{
char
u64buf
[
128
];
cJSON
*
pRoot
=
cJSON_CreateObject
();
if
(
pMsg
!=
NULL
)
{
cJSON_AddNumberToObject
(
pRoot
,
"bytes"
,
pMsg
->
bytes
);
cJSON_AddNumberToObject
(
pRoot
,
"vgId"
,
pMsg
->
vgId
);
cJSON_AddNumberToObject
(
pRoot
,
"msgType"
,
pMsg
->
msgType
);
cJSON
*
pSrcId
=
cJSON_CreateObject
();
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pMsg
->
srcId
.
addr
);
cJSON_AddStringToObject
(
pSrcId
,
"addr"
,
u64buf
);
{
uint64_t
u64
=
pMsg
->
srcId
.
addr
;
cJSON
*
pTmp
=
pSrcId
;
char
host
[
128
];
uint16_t
port
;
syncUtilU642Addr
(
u64
,
host
,
sizeof
(
host
),
&
port
);
cJSON_AddStringToObject
(
pTmp
,
"addr_host"
,
host
);
cJSON_AddNumberToObject
(
pTmp
,
"addr_port"
,
port
);
}
cJSON_AddNumberToObject
(
pSrcId
,
"vgId"
,
pMsg
->
srcId
.
vgId
);
cJSON_AddItemToObject
(
pRoot
,
"srcId"
,
pSrcId
);
cJSON
*
pDestId
=
cJSON_CreateObject
();
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pMsg
->
destId
.
addr
);
cJSON_AddStringToObject
(
pDestId
,
"addr"
,
u64buf
);
{
uint64_t
u64
=
pMsg
->
destId
.
addr
;
cJSON
*
pTmp
=
pDestId
;
char
host
[
128
];
uint16_t
port
;
syncUtilU642Addr
(
u64
,
host
,
sizeof
(
host
),
&
port
);
cJSON_AddStringToObject
(
pTmp
,
"addr_host"
,
host
);
cJSON_AddNumberToObject
(
pTmp
,
"addr_port"
,
port
);
}
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
),
"%ld"
,
pMsg
->
lastIndex
);
cJSON_AddStringToObject
(
pRoot
,
"lastIndex"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pMsg
->
lastTerm
);
cJSON_AddStringToObject
(
pRoot
,
"lastTerm"
,
u64buf
);
cJSON_AddNumberToObject
(
pRoot
,
"ack"
,
pMsg
->
ack
);
}
cJSON
*
pJson
=
cJSON_CreateObject
();
cJSON_AddItemToObject
(
pJson
,
"SyncSnapshotRsp"
,
pRoot
);
return
pJson
;
}
char
*
syncSnapshotRsp2Str
(
const
SyncSnapshotRsp
*
pMsg
)
{
cJSON
*
pJson
=
syncSnapshotRsp2Json
(
pMsg
);
char
*
serialized
=
cJSON_Print
(
pJson
);
cJSON_Delete
(
pJson
);
return
serialized
;
}
// for debug ----------------------
void
syncSnapshotRspPrint
(
const
SyncSnapshotRsp
*
pMsg
)
{
char
*
serialized
=
syncSnapshotRsp2Str
(
pMsg
);
printf
(
"syncSnapshotRspPrint | len:%lu | %s
\n
"
,
strlen
(
serialized
),
serialized
);
fflush
(
NULL
);
taosMemoryFree
(
serialized
);
}
void
syncSnapshotRspPrint2
(
char
*
s
,
const
SyncSnapshotRsp
*
pMsg
)
{
char
*
serialized
=
syncSnapshotRsp2Str
(
pMsg
);
printf
(
"syncSnapshotRspPrint2 | len:%lu | %s | %s
\n
"
,
strlen
(
serialized
),
s
,
serialized
);
fflush
(
NULL
);
taosMemoryFree
(
serialized
);
}
void
syncSnapshotRspLog
(
const
SyncSnapshotRsp
*
pMsg
)
{
char
*
serialized
=
syncSnapshotRsp2Str
(
pMsg
);
sTrace
(
"syncSnapshotRspLog | len:%lu | %s"
,
strlen
(
serialized
),
serialized
);
taosMemoryFree
(
serialized
);
}
void
syncSnapshotRspLog2
(
char
*
s
,
const
SyncSnapshotRsp
*
pMsg
)
{
char
*
serialized
=
syncSnapshotRsp2Str
(
pMsg
);
sTrace
(
"syncSnapshotRspLog2 | len:%lu | %s | %s"
,
strlen
(
serialized
),
s
,
serialized
);
taosMemoryFree
(
serialized
);
}
\ No newline at end of file
source/libs/sync/src/syncRaftCfg.c
浏览文件 @
a047d4a5
...
...
@@ -148,6 +148,7 @@ cJSON *raftCfg2Json(SRaftCfg *pRaftCfg) {
cJSON
*
pRoot
=
cJSON_CreateObject
();
cJSON_AddItemToObject
(
pRoot
,
"SSyncCfg"
,
syncCfg2Json
(
&
(
pRaftCfg
->
cfg
)));
cJSON_AddNumberToObject
(
pRoot
,
"isStandBy"
,
pRaftCfg
->
isStandBy
);
cJSON_AddNumberToObject
(
pRoot
,
"snapshotEnable"
,
pRaftCfg
->
snapshotEnable
);
cJSON
*
pJson
=
cJSON_CreateObject
();
cJSON_AddItemToObject
(
pJson
,
"RaftCfg"
,
pRoot
);
...
...
@@ -161,7 +162,7 @@ char *raftCfg2Str(SRaftCfg *pRaftCfg) {
return
serialized
;
}
int32_t
raftCfgCreateFile
(
SSyncCfg
*
pCfg
,
int8_t
isStandBy
,
const
char
*
path
)
{
int32_t
raftCfgCreateFile
(
SSyncCfg
*
pCfg
,
SRaftCfgMeta
meta
,
const
char
*
path
)
{
assert
(
pCfg
!=
NULL
);
TdFilePtr
pFile
=
taosOpenFile
(
path
,
TD_FILE_CREATE
|
TD_FILE_WRITE
);
...
...
@@ -169,7 +170,8 @@ int32_t raftCfgCreateFile(SSyncCfg *pCfg, int8_t isStandBy, const char *path) {
SRaftCfg
raftCfg
;
raftCfg
.
cfg
=
*
pCfg
;
raftCfg
.
isStandBy
=
isStandBy
;
raftCfg
.
isStandBy
=
meta
.
isStandBy
;
raftCfg
.
snapshotEnable
=
meta
.
snapshotEnable
;
char
*
s
=
raftCfg2Str
(
&
raftCfg
);
char
buf
[
CONFIG_FILE_LEN
]
=
{
0
};
...
...
@@ -194,6 +196,9 @@ int32_t raftCfgFromJson(const cJSON *pRoot, SRaftCfg *pRaftCfg) {
cJSON
*
pJsonIsStandBy
=
cJSON_GetObjectItem
(
pJson
,
"isStandBy"
);
pRaftCfg
->
isStandBy
=
cJSON_GetNumberValue
(
pJsonIsStandBy
);
cJSON
*
pJsonSnapshotEnable
=
cJSON_GetObjectItem
(
pJson
,
"snapshotEnable"
);
pRaftCfg
->
snapshotEnable
=
cJSON_GetNumberValue
(
pJsonSnapshotEnable
);
cJSON
*
pJsonSyncCfg
=
cJSON_GetObjectItem
(
pJson
,
"SSyncCfg"
);
int32_t
code
=
syncCfgFromJson
(
pJsonSyncCfg
,
&
(
pRaftCfg
->
cfg
));
ASSERT
(
code
==
0
);
...
...
source/libs/sync/src/syncRaftLog.c
浏览文件 @
a047d4a5
...
...
@@ -16,6 +16,23 @@
#include "syncRaftLog.h"
#include "wal.h"
// refactor, log[0 .. n] ==> log[m .. n]
static
int32_t
raftLogSetBeginIndex
(
struct
SSyncLogStore
*
pLogStore
,
SyncIndex
beginIndex
);
static
SyncIndex
raftLogBeginIndex
(
struct
SSyncLogStore
*
pLogStore
);
static
SyncIndex
raftLogEndIndex
(
struct
SSyncLogStore
*
pLogStore
);
static
SyncIndex
raftLogWriteIndex
(
struct
SSyncLogStore
*
pLogStore
);
static
bool
raftLogIsEmpty
(
struct
SSyncLogStore
*
pLogStore
);
static
int32_t
raftLogEntryCount
(
struct
SSyncLogStore
*
pLogStore
);
static
bool
raftLogInRange
(
struct
SSyncLogStore
*
pLogStore
,
SyncIndex
index
);
static
SyncIndex
raftLogLastIndex
(
struct
SSyncLogStore
*
pLogStore
);
static
SyncTerm
raftLogLastTerm
(
struct
SSyncLogStore
*
pLogStore
);
static
int32_t
raftLogAppendEntry
(
struct
SSyncLogStore
*
pLogStore
,
SSyncRaftEntry
*
pEntry
);
static
int32_t
raftLogGetEntry
(
struct
SSyncLogStore
*
pLogStore
,
SyncIndex
index
,
SSyncRaftEntry
**
ppEntry
);
static
int32_t
raftLogTruncate
(
struct
SSyncLogStore
*
pLogStore
,
SyncIndex
fromIndex
);
static
int32_t
raftLogGetLastEntry
(
SSyncLogStore
*
pLogStore
,
SSyncRaftEntry
**
ppLastEntry
);
//-------------------------------
static
SSyncRaftEntry
*
logStoreGetLastEntry
(
SSyncLogStore
*
pLogStore
);
static
SyncIndex
logStoreLastIndex
(
SSyncLogStore
*
pLogStore
);
static
SyncTerm
logStoreLastTerm
(
SSyncLogStore
*
pLogStore
);
...
...
@@ -25,6 +42,198 @@ static int32_t logStoreTruncate(SSyncLogStore* pLogStore, SyncIndex from
static
int32_t
logStoreUpdateCommitIndex
(
SSyncLogStore
*
pLogStore
,
SyncIndex
index
);
static
SyncIndex
logStoreGetCommitIndex
(
SSyncLogStore
*
pLogStore
);
// refactor, log[0 .. n] ==> log[m .. n]
static
int32_t
raftLogSetBeginIndex
(
struct
SSyncLogStore
*
pLogStore
,
SyncIndex
beginIndex
)
{
// if beginIndex == 0, donot need call this funciton
ASSERT
(
beginIndex
>
0
);
SSyncLogStoreData
*
pData
=
pLogStore
->
data
;
SWal
*
pWal
=
pData
->
pWal
;
pData
->
beginIndex
=
beginIndex
;
walRestoreFromSnapshot
(
pWal
,
beginIndex
-
1
);
return
0
;
}
static
SyncIndex
raftLogBeginIndex
(
struct
SSyncLogStore
*
pLogStore
)
{
SSyncLogStoreData
*
pData
=
pLogStore
->
data
;
SWal
*
pWal
=
pData
->
pWal
;
return
pData
->
beginIndex
;
}
static
SyncIndex
raftLogEndIndex
(
struct
SSyncLogStore
*
pLogStore
)
{
return
raftLogLastIndex
(
pLogStore
);
}
static
bool
raftLogIsEmpty
(
struct
SSyncLogStore
*
pLogStore
)
{
SyncIndex
beginIndex
=
raftLogBeginIndex
(
pLogStore
);
SyncIndex
endIndex
=
raftLogEndIndex
(
pLogStore
);
return
(
endIndex
>=
beginIndex
);
}
static
int32_t
raftLogEntryCount
(
struct
SSyncLogStore
*
pLogStore
)
{
SyncIndex
beginIndex
=
raftLogBeginIndex
(
pLogStore
);
SyncIndex
endIndex
=
raftLogEndIndex
(
pLogStore
);
int32_t
count
=
endIndex
-
beginIndex
+
1
;
return
count
>
0
?
count
:
0
;
}
static
bool
raftLogInRange
(
struct
SSyncLogStore
*
pLogStore
,
SyncIndex
index
)
{
SyncIndex
beginIndex
=
raftLogBeginIndex
(
pLogStore
);
SyncIndex
endIndex
=
raftLogEndIndex
(
pLogStore
);
if
(
index
>=
beginIndex
&&
index
<=
endIndex
)
{
return
true
;
}
else
{
return
false
;
}
}
static
SyncIndex
raftLogLastIndex
(
struct
SSyncLogStore
*
pLogStore
)
{
SyncIndex
lastIndex
;
SSyncLogStoreData
*
pData
=
pLogStore
->
data
;
SWal
*
pWal
=
pData
->
pWal
;
SyncIndex
lastVer
=
walGetLastVer
(
pWal
);
SyncIndex
firstVer
=
walGetFirstVer
(
pWal
);
if
(
lastVer
<
firstVer
)
{
// no record
lastIndex
=
-
1
;
}
else
{
if
(
firstVer
>=
0
)
{
lastIndex
=
lastVer
;
}
else
if
(
firstVer
==
-
1
)
{
lastIndex
=
-
1
;
}
else
{
ASSERT
(
0
);
}
}
return
lastIndex
;
}
static
SyncIndex
raftLogWriteIndex
(
struct
SSyncLogStore
*
pLogStore
)
{
SSyncLogStoreData
*
pData
=
pLogStore
->
data
;
SWal
*
pWal
=
pData
->
pWal
;
SyncIndex
lastVer
=
walGetLastVer
(
pWal
);
return
lastVer
+
1
;
}
static
SyncTerm
raftLogLastTerm
(
struct
SSyncLogStore
*
pLogStore
)
{
SyncTerm
lastTerm
=
0
;
if
(
raftLogEntryCount
(
pLogStore
)
==
0
)
{
lastTerm
=
0
;
}
else
{
SSyncRaftEntry
*
pLastEntry
;
int32_t
code
=
raftLogGetLastEntry
(
pLogStore
,
&
pLastEntry
);
ASSERT
(
code
==
0
);
if
(
pLastEntry
!=
NULL
)
{
lastTerm
=
pLastEntry
->
term
;
taosMemoryFree
(
pLastEntry
);
}
}
return
lastTerm
;
}
static
int32_t
raftLogAppendEntry
(
struct
SSyncLogStore
*
pLogStore
,
SSyncRaftEntry
*
pEntry
)
{
SSyncLogStoreData
*
pData
=
pLogStore
->
data
;
SWal
*
pWal
=
pData
->
pWal
;
SyncIndex
writeIndex
=
raftLogWriteIndex
(
pLogStore
);
ASSERT
(
pEntry
->
index
==
writeIndex
);
int
code
=
0
;
SSyncLogMeta
syncMeta
;
syncMeta
.
isWeek
=
pEntry
->
isWeak
;
syncMeta
.
seqNum
=
pEntry
->
seqNum
;
syncMeta
.
term
=
pEntry
->
term
;
code
=
walWriteWithSyncInfo
(
pWal
,
pEntry
->
index
,
pEntry
->
originalRpcType
,
syncMeta
,
pEntry
->
data
,
pEntry
->
dataLen
);
if
(
code
!=
0
)
{
int32_t
err
=
terrno
;
const
char
*
errStr
=
tstrerror
(
err
);
int32_t
linuxErr
=
errno
;
const
char
*
linuxErrMsg
=
strerror
(
errno
);
sError
(
"raftLogAppendEntry error, err:%d %X, msg:%s, linuxErr:%d, linuxErrMsg:%s"
,
err
,
err
,
errStr
,
linuxErr
,
linuxErrMsg
);
ASSERT
(
0
);
}
walFsync
(
pWal
,
true
);
sTrace
(
"sync event write wal: %ld"
,
pEntry
->
index
);
return
code
;
}
static
int32_t
raftLogGetEntry
(
struct
SSyncLogStore
*
pLogStore
,
SyncIndex
index
,
SSyncRaftEntry
**
ppEntry
)
{
SSyncLogStoreData
*
pData
=
pLogStore
->
data
;
SWal
*
pWal
=
pData
->
pWal
;
int32_t
code
;
*
ppEntry
=
NULL
;
if
(
raftLogInRange
(
pLogStore
,
index
))
{
SWalReadHandle
*
pWalHandle
=
walOpenReadHandle
(
pWal
);
ASSERT
(
pWalHandle
!=
NULL
);
code
=
walReadWithHandle
(
pWalHandle
,
index
);
if
(
code
!=
0
)
{
int32_t
err
=
terrno
;
const
char
*
errStr
=
tstrerror
(
err
);
int32_t
linuxErr
=
errno
;
const
char
*
linuxErrMsg
=
strerror
(
errno
);
sError
(
"raftLogGetEntry error, err:%d %X, msg:%s, linuxErr:%d, linuxErrMsg:%s"
,
err
,
err
,
errStr
,
linuxErr
,
linuxErrMsg
);
walCloseReadHandle
(
pWalHandle
);
ASSERT
(
0
);
return
code
;
}
*
ppEntry
=
syncEntryBuild
(
pWalHandle
->
pHead
->
head
.
bodyLen
);
ASSERT
(
*
ppEntry
!=
NULL
);
(
*
ppEntry
)
->
msgType
=
TDMT_SYNC_CLIENT_REQUEST
;
(
*
ppEntry
)
->
originalRpcType
=
pWalHandle
->
pHead
->
head
.
msgType
;
(
*
ppEntry
)
->
seqNum
=
pWalHandle
->
pHead
->
head
.
syncMeta
.
seqNum
;
(
*
ppEntry
)
->
isWeak
=
pWalHandle
->
pHead
->
head
.
syncMeta
.
isWeek
;
(
*
ppEntry
)
->
term
=
pWalHandle
->
pHead
->
head
.
syncMeta
.
term
;
(
*
ppEntry
)
->
index
=
index
;
ASSERT
((
*
ppEntry
)
->
dataLen
==
pWalHandle
->
pHead
->
head
.
bodyLen
);
memcpy
((
*
ppEntry
)
->
data
,
pWalHandle
->
pHead
->
head
.
body
,
pWalHandle
->
pHead
->
head
.
bodyLen
);
// need to hold, do not new every time!!
walCloseReadHandle
(
pWalHandle
);
}
else
{
// index not in range
code
=
0
;
}
return
code
;
}
static
int32_t
raftLogTruncate
(
struct
SSyncLogStore
*
pLogStore
,
SyncIndex
fromIndex
)
{
SSyncLogStoreData
*
pData
=
pLogStore
->
data
;
SWal
*
pWal
=
pData
->
pWal
;
int32_t
code
=
walRollback
(
pWal
,
fromIndex
);
if
(
code
!=
0
)
{
int32_t
err
=
terrno
;
const
char
*
errStr
=
tstrerror
(
err
);
int32_t
linuxErr
=
errno
;
const
char
*
linuxErrMsg
=
strerror
(
errno
);
sError
(
"raftLogTruncate error, err:%d %X, msg:%s, linuxErr:%d, linuxErrMsg:%s"
,
err
,
err
,
errStr
,
linuxErr
,
linuxErrMsg
);
ASSERT
(
0
);
}
return
code
;
}
static
int32_t
raftLogGetLastEntry
(
SSyncLogStore
*
pLogStore
,
SSyncRaftEntry
**
ppLastEntry
)
{
*
ppLastEntry
=
NULL
;
if
(
raftLogEntryCount
(
pLogStore
)
==
0
)
{
return
0
;
}
SyncIndex
lastIndex
=
raftLogLastIndex
(
pLogStore
);
int32_t
code
=
raftLogGetEntry
(
pLogStore
,
lastIndex
,
ppLastEntry
);
return
code
;
}
//-------------------------------
SSyncLogStore
*
logStoreCreate
(
SSyncNode
*
pSyncNode
)
{
SSyncLogStore
*
pLogStore
=
taosMemoryMalloc
(
sizeof
(
SSyncLogStore
));
assert
(
pLogStore
!=
NULL
);
...
...
@@ -36,6 +245,16 @@ SSyncLogStore* logStoreCreate(SSyncNode* pSyncNode) {
pData
->
pSyncNode
=
pSyncNode
;
pData
->
pWal
=
pSyncNode
->
pWal
;
SyncIndex
firstVer
=
walGetFirstVer
(
pData
->
pWal
);
SyncIndex
lastVer
=
walGetLastVer
(
pData
->
pWal
);
if
(
firstVer
>=
0
)
{
pData
->
beginIndex
=
firstVer
;
}
else
if
(
firstVer
==
-
1
)
{
pData
->
beginIndex
=
lastVer
+
1
;
}
else
{
ASSERT
(
0
);
}
pLogStore
->
appendEntry
=
logStoreAppendEntry
;
pLogStore
->
getEntry
=
logStoreGetEntry
;
pLogStore
->
truncate
=
logStoreTruncate
;
...
...
@@ -43,6 +262,20 @@ SSyncLogStore* logStoreCreate(SSyncNode* pSyncNode) {
pLogStore
->
getLastTerm
=
logStoreLastTerm
;
pLogStore
->
updateCommitIndex
=
logStoreUpdateCommitIndex
;
pLogStore
->
getCommitIndex
=
logStoreGetCommitIndex
;
pLogStore
->
syncLogSetBeginIndex
=
raftLogSetBeginIndex
;
pLogStore
->
syncLogBeginIndex
=
raftLogBeginIndex
;
pLogStore
->
syncLogEndIndex
=
raftLogEndIndex
;
pLogStore
->
syncLogIsEmpty
=
raftLogIsEmpty
;
pLogStore
->
syncLogEntryCount
=
raftLogEntryCount
;
pLogStore
->
syncLogInRange
=
raftLogInRange
;
pLogStore
->
syncLogLastIndex
=
raftLogLastIndex
;
pLogStore
->
syncLogLastTerm
=
raftLogLastTerm
;
pLogStore
->
syncLogAppendEntry
=
raftLogAppendEntry
;
pLogStore
->
syncLogGetEntry
=
raftLogGetEntry
;
pLogStore
->
syncLogTruncate
=
raftLogTruncate
;
pLogStore
->
syncLogWriteIndex
=
raftLogWriteIndex
;
return
pLogStore
;
}
...
...
@@ -53,6 +286,7 @@ void logStoreDestory(SSyncLogStore* pLogStore) {
}
}
//-------------------------------
int32_t
logStoreAppendEntry
(
SSyncLogStore
*
pLogStore
,
SSyncRaftEntry
*
pEntry
)
{
SSyncLogStoreData
*
pData
=
pLogStore
->
data
;
SWal
*
pWal
=
pData
->
pWal
;
...
...
@@ -78,6 +312,8 @@ int32_t logStoreAppendEntry(SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry) {
// assert(code == 0);
walFsync
(
pWal
,
true
);
sTrace
(
"sync event old write wal: %ld"
,
pEntry
->
index
);
return
code
;
}
...
...
@@ -136,7 +372,7 @@ int32_t logStoreTruncate(SSyncLogStore* pLogStore, SyncIndex fromIndex) {
linuxErrMsg
);
ASSERT
(
0
);
}
return
0
;
// to avoid compiler error
return
0
;
}
SyncIndex
logStoreLastIndex
(
SSyncLogStore
*
pLogStore
)
{
...
...
@@ -169,7 +405,7 @@ int32_t logStoreUpdateCommitIndex(SSyncLogStore* pLogStore, SyncIndex index) {
sError
(
"walCommit error, err:%d %X, msg:%s, linuxErr:%d, linuxErrMsg:%s"
,
err
,
err
,
errStr
,
linuxErr
,
linuxErrMsg
);
ASSERT
(
0
);
}
return
0
;
// to avoid compiler error
return
0
;
}
SyncIndex
logStoreGetCommitIndex
(
SSyncLogStore
*
pLogStore
)
{
...
...
@@ -199,15 +435,25 @@ cJSON* logStore2Json(SSyncLogStore* pLogStore) {
cJSON_AddStringToObject
(
pRoot
,
"pSyncNode"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pData
->
pWal
);
cJSON_AddStringToObject
(
pRoot
,
"pWal"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
logStore
LastIndex
(
pLogStore
));
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
raftLog
LastIndex
(
pLogStore
));
cJSON_AddStringToObject
(
pRoot
,
"LastIndex"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
logStore
LastTerm
(
pLogStore
));
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
raftLog
LastTerm
(
pLogStore
));
cJSON_AddStringToObject
(
pRoot
,
"LastTerm"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
pData
->
beginIndex
);
cJSON_AddStringToObject
(
pRoot
,
"beginIndex"
,
u64buf
);
SyncIndex
endIndex
=
raftLogEndIndex
(
pLogStore
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
endIndex
);
cJSON_AddStringToObject
(
pRoot
,
"endIndex"
,
u64buf
);
int32_t
count
=
raftLogEntryCount
(
pLogStore
);
cJSON_AddNumberToObject
(
pRoot
,
"entryCount"
,
count
);
cJSON
*
pEntries
=
cJSON_CreateArray
();
cJSON_AddItemToObject
(
pRoot
,
"pEntries"
,
pEntries
);
SyncIndex
lastIndex
=
logStoreLastIndex
(
pLogStore
);
for
(
SyncIndex
i
=
0
;
i
<=
last
Index
;
++
i
)
{
for
(
SyncIndex
i
=
pData
->
beginIndex
;
i
<=
end
Index
;
++
i
)
{
SSyncRaftEntry
*
pEntry
=
logStoreGetEntry
(
pLogStore
,
i
);
cJSON_AddItemToArray
(
pEntries
,
syncEntry2Json
(
pEntry
));
syncEntryDestory
(
pEntry
);
...
...
@@ -236,10 +482,20 @@ cJSON* logStoreSimple2Json(SSyncLogStore* pLogStore) {
cJSON_AddStringToObject
(
pRoot
,
"pSyncNode"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pData
->
pWal
);
cJSON_AddStringToObject
(
pRoot
,
"pWal"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
logStore
LastIndex
(
pLogStore
));
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
raftLog
LastIndex
(
pLogStore
));
cJSON_AddStringToObject
(
pRoot
,
"LastIndex"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
logStore
LastTerm
(
pLogStore
));
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
raftLog
LastTerm
(
pLogStore
));
cJSON_AddStringToObject
(
pRoot
,
"LastTerm"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
pData
->
beginIndex
);
cJSON_AddStringToObject
(
pRoot
,
"beginIndex"
,
u64buf
);
SyncIndex
endIndex
=
raftLogEndIndex
(
pLogStore
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
endIndex
);
cJSON_AddStringToObject
(
pRoot
,
"endIndex"
,
u64buf
);
int32_t
count
=
raftLogEntryCount
(
pLogStore
);
cJSON_AddNumberToObject
(
pRoot
,
"entryCount"
,
count
);
}
cJSON
*
pJson
=
cJSON_CreateObject
();
...
...
@@ -254,6 +510,12 @@ char* logStoreSimple2Str(SSyncLogStore* pLogStore) {
return
serialized
;
}
SyncIndex
logStoreFirstIndex
(
SSyncLogStore
*
pLogStore
)
{
SSyncLogStoreData
*
pData
=
pLogStore
->
data
;
SWal
*
pWal
=
pData
->
pWal
;
return
walGetFirstVer
(
pWal
);
}
// for debug -----------------
void
logStorePrint
(
SSyncLogStore
*
pLogStore
)
{
char
*
serialized
=
logStore2Str
(
pLogStore
);
...
...
source/libs/sync/src/syncReplication.c
浏览文件 @
a047d4a5
...
...
@@ -16,9 +16,11 @@
#include "syncReplication.h"
#include "syncIndexMgr.h"
#include "syncMessage.h"
#include "syncRaftCfg.h"
#include "syncRaftEntry.h"
#include "syncRaftLog.h"
#include "syncRaftStore.h"
#include "syncSnapshot.h"
#include "syncUtil.h"
// TLA+ Spec
...
...
@@ -59,6 +61,7 @@ int32_t syncNodeAppendEntriesPeers(SSyncNode* pSyncNode) {
// set prevLogIndex
SyncIndex
nextIndex
=
syncIndexMgrGetIndex
(
pSyncNode
->
pNextIndex
,
pDestId
);
SyncIndex
preLogIndex
=
nextIndex
-
1
;
// set preLogTerm
...
...
@@ -113,9 +116,85 @@ int32_t syncNodeAppendEntriesPeers(SSyncNode* pSyncNode) {
return
ret
;
}
int32_t
syncNodeAppendEntriesPeersSnapshot
(
SSyncNode
*
pSyncNode
)
{
ASSERT
(
pSyncNode
->
state
==
TAOS_SYNC_STATE_LEADER
);
syncIndexMgrLog2
(
"begin append entries peers pNextIndex:"
,
pSyncNode
->
pNextIndex
);
syncIndexMgrLog2
(
"begin append entries peers pMatchIndex:"
,
pSyncNode
->
pMatchIndex
);
logStoreSimpleLog2
(
"begin append entries peers LogStore:"
,
pSyncNode
->
pLogStore
);
{
SSnapshot
snapshot
;
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSyncNode
->
pFsm
,
&
snapshot
);
sTrace
(
"begin append entries peers, snapshot.lastApplyIndex:%ld, snapshot.lastApplyTerm:%lu"
,
snapshot
.
lastApplyIndex
,
snapshot
.
lastApplyTerm
);
}
int32_t
ret
=
0
;
for
(
int
i
=
0
;
i
<
pSyncNode
->
peersNum
;
++
i
)
{
SRaftId
*
pDestId
=
&
(
pSyncNode
->
peersId
[
i
]);
// next index
SyncIndex
nextIndex
=
syncIndexMgrGetIndex
(
pSyncNode
->
pNextIndex
,
pDestId
);
// pre index, pre term
SyncIndex
preLogIndex
=
syncNodeGetPreIndex
(
pSyncNode
,
nextIndex
);
SyncTerm
preLogTerm
=
syncNodeGetPreTerm
(
pSyncNode
,
nextIndex
);
// batch optimized
// SyncIndex lastIndex = syncUtilMinIndex(pSyncNode->pLogStore->getLastIndex(pSyncNode->pLogStore), nextIndex);
// prepare entry
SyncAppendEntries
*
pMsg
=
NULL
;
SSyncRaftEntry
*
pEntry
;
int32_t
code
=
pSyncNode
->
pLogStore
->
syncLogGetEntry
(
pSyncNode
->
pLogStore
,
nextIndex
,
&
pEntry
);
ASSERT
(
code
==
0
);
if
(
pEntry
!=
NULL
)
{
pMsg
=
syncAppendEntriesBuild
(
pEntry
->
bytes
,
pSyncNode
->
vgId
);
ASSERT
(
pMsg
!=
NULL
);
// add pEntry into msg
uint32_t
len
;
char
*
serialized
=
syncEntrySerialize
(
pEntry
,
&
len
);
assert
(
len
==
pEntry
->
bytes
);
memcpy
(
pMsg
->
data
,
serialized
,
len
);
taosMemoryFree
(
serialized
);
syncEntryDestory
(
pEntry
);
}
else
{
// no entry in log
pMsg
=
syncAppendEntriesBuild
(
0
,
pSyncNode
->
vgId
);
ASSERT
(
pMsg
!=
NULL
);
}
// prepare msg
ASSERT
(
pMsg
!=
NULL
);
pMsg
->
srcId
=
pSyncNode
->
myRaftId
;
pMsg
->
destId
=
*
pDestId
;
pMsg
->
term
=
pSyncNode
->
pRaftStore
->
currentTerm
;
pMsg
->
prevLogIndex
=
preLogIndex
;
pMsg
->
prevLogTerm
=
preLogTerm
;
pMsg
->
commitIndex
=
pSyncNode
->
commitIndex
;
// send msg
syncNodeAppendEntries
(
pSyncNode
,
pDestId
,
pMsg
);
syncAppendEntriesDestroy
(
pMsg
);
}
return
ret
;
}
int32_t
syncNodeReplicate
(
SSyncNode
*
pSyncNode
)
{
// start replicate
int32_t
ret
=
syncNodeAppendEntriesPeers
(
pSyncNode
);
int32_t
ret
=
0
;
if
(
pSyncNode
->
pRaftCfg
->
snapshotEnable
)
{
ret
=
syncNodeAppendEntriesPeersSnapshot
(
pSyncNode
);
}
else
{
ret
=
syncNodeAppendEntriesPeers
(
pSyncNode
);
}
return
ret
;
}
...
...
source/libs/sync/src/syncRequestVote.c
浏览文件 @
a047d4a5
...
...
@@ -62,6 +62,9 @@ int32_t syncNodeOnRequestVoteCb(SSyncNode* ths, SyncRequestVote* pMsg) {
// maybe has already voted for pMsg->srcId
// vote again, no harm
raftStoreVote
(
ths
->
pRaftStore
,
&
(
pMsg
->
srcId
));
// forbid elect for this round
syncNodeResetElectTimer
(
ths
);
}
SyncRequestVoteReply
*
pReply
=
syncRequestVoteReplyBuild
(
ths
->
vgId
);
...
...
@@ -77,3 +80,58 @@ int32_t syncNodeOnRequestVoteCb(SSyncNode* ths, SyncRequestVote* pMsg) {
return
ret
;
}
static
bool
syncNodeOnRequestVoteLogOK
(
SSyncNode
*
pSyncNode
,
SyncRequestVote
*
pMsg
)
{
SyncTerm
myLastTerm
=
syncNodeGetLastTerm
(
pSyncNode
);
SyncIndex
myLastIndex
=
syncNodeGetLastIndex
(
pSyncNode
);
if
(
pMsg
->
lastLogTerm
>
myLastTerm
)
{
return
true
;
}
if
(
pMsg
->
lastLogTerm
==
myLastTerm
&&
pMsg
->
lastLogIndex
>=
myLastIndex
)
{
return
true
;
}
return
false
;
}
int32_t
syncNodeOnRequestVoteSnapshotCb
(
SSyncNode
*
ths
,
SyncRequestVote
*
pMsg
)
{
int32_t
ret
=
0
;
// print log
char
logBuf
[
128
]
=
{
0
};
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"recv SyncRequestVote, currentTerm:%lu"
,
ths
->
pRaftStore
->
currentTerm
);
syncRequestVoteLog2
(
logBuf
,
pMsg
);
// maybe update term
if
(
pMsg
->
term
>
ths
->
pRaftStore
->
currentTerm
)
{
syncNodeUpdateTerm
(
ths
,
pMsg
->
term
);
}
ASSERT
(
pMsg
->
term
<=
ths
->
pRaftStore
->
currentTerm
);
bool
logOK
=
syncNodeOnRequestVoteLogOK
(
ths
,
pMsg
);
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
;
SRpcMsg
rpcMsg
;
syncRequestVoteReply2RpcMsg
(
pReply
,
&
rpcMsg
);
syncNodeSendMsgById
(
&
pReply
->
destId
,
ths
,
&
rpcMsg
);
syncRequestVoteReplyDestroy
(
pReply
);
return
ret
;
}
\ No newline at end of file
source/libs/sync/src/syncRequestVoteReply.c
浏览文件 @
a047d4a5
...
...
@@ -92,3 +92,62 @@ int32_t syncNodeOnRequestVoteReplyCb(SSyncNode* ths, SyncRequestVoteReply* pMsg)
return
ret
;
}
int32_t
syncNodeOnRequestVoteReplySnapshotCb
(
SSyncNode
*
ths
,
SyncRequestVoteReply
*
pMsg
)
{
int32_t
ret
=
0
;
// print log
char
logBuf
[
128
]
=
{
0
};
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"recv SyncRequestVoteReply, term:%lu"
,
ths
->
pRaftStore
->
currentTerm
);
syncRequestVoteReplyLog2
(
logBuf
,
pMsg
);
// drop stale response
if
(
pMsg
->
term
<
ths
->
pRaftStore
->
currentTerm
)
{
sTrace
(
"recv SyncRequestVoteReply, drop stale response, receive_term:%lu current_term:%lu"
,
pMsg
->
term
,
ths
->
pRaftStore
->
currentTerm
);
return
ret
;
}
// 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
)
{
char
logBuf
[
128
]
=
{
0
};
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"recv SyncRequestVoteReply, error term, receive_term:%lu current_term:%lu"
,
pMsg
->
term
,
ths
->
pRaftStore
->
currentTerm
);
syncNodePrint2
(
logBuf
,
ths
);
sError
(
"%s"
,
logBuf
);
return
ret
;
}
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
)
{
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
ret
;
}
\ No newline at end of file
source/libs/sync/src/syncSnapshot.c
浏览文件 @
a047d4a5
...
...
@@ -14,23 +14,522 @@
*/
#include "syncSnapshot.h"
#include "syncRaftLog.h"
#include "syncRaftStore.h"
#include "syncUtil.h"
#include "wal.h"
SSyncSnapshotSender
*
snapshotSenderCreate
(
SSyncNode
*
pSyncNode
)
{
return
NULL
;
}
// static void snapshotSenderDoStart(SSyncSnapshotSender *pSender);
static
void
snapshotReceiverDoStart
(
SSyncSnapshotReceiver
*
pReceiver
);
void
snapshotSenderDestroy
(
SSyncSnapshotSender
*
pSender
)
{}
SSyncSnapshotSender
*
snapshotSenderCreate
(
SSyncNode
*
pSyncNode
,
int32_t
replicaIndex
)
{
bool
condition
=
(
pSyncNode
->
pFsm
->
FpSnapshotStartRead
!=
NULL
)
&&
(
pSyncNode
->
pFsm
->
FpSnapshotStopRead
!=
NULL
)
&&
(
pSyncNode
->
pFsm
->
FpSnapshotDoRead
!=
NULL
);
int32_t
snapshotSend
(
SSyncSnapshotSender
*
pSender
)
{
return
0
;
}
SSyncSnapshotSender
*
pSender
=
NULL
;
if
(
condition
)
{
pSender
=
taosMemoryMalloc
(
sizeof
(
SSyncSnapshotSender
));
ASSERT
(
pSender
!=
NULL
);
memset
(
pSender
,
0
,
sizeof
(
*
pSender
));
cJSON
*
snapshotSender2Json
(
SSyncSnapshotSender
*
pSender
)
{
return
NULL
;
}
pSender
->
start
=
false
;
pSender
->
seq
=
SYNC_SNAPSHOT_SEQ_INVALID
;
pSender
->
ack
=
SYNC_SNAPSHOT_SEQ_INVALID
;
pSender
->
pReader
=
NULL
;
pSender
->
pCurrentBlock
=
NULL
;
pSender
->
blockLen
=
0
;
pSender
->
sendingMS
=
SYNC_SNAPSHOT_RETRY_MS
;
pSender
->
pSyncNode
=
pSyncNode
;
pSender
->
replicaIndex
=
replicaIndex
;
pSender
->
term
=
pSyncNode
->
pRaftStore
->
currentTerm
;
pSender
->
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSender
->
pSyncNode
->
pFsm
,
&
(
pSender
->
snapshot
));
char
*
snapshotSender2Str
(
SSyncSnapshotSender
*
pSender
)
{
return
NULL
;
}
pSender
->
finish
=
false
;
}
else
{
sInfo
(
"snapshotSenderCreate cannot create sender"
);
}
SSyncSnapshotReceiver
*
snapshotReceiverCreate
(
SSyncNode
*
pSyncNode
)
{
return
NULL
;
}
return
pSender
;
}
void
snapshotReceiverDestroy
(
SSyncSnapshotReceiver
*
pReceiver
)
{}
void
snapshotSenderDestroy
(
SSyncSnapshotSender
*
pSender
)
{
if
(
pSender
!=
NULL
)
{
taosMemoryFree
(
pSender
);
}
}
int32_t
snapshotReceive
(
SSyncSnapshotReceiver
*
pReceiver
)
{
return
0
;
}
// begin send snapshot (current term, seq begin)
void
snapshotSenderDoStart
(
SSyncSnapshotSender
*
pSender
)
{
pSender
->
term
=
pSender
->
pSyncNode
->
pRaftStore
->
currentTerm
;
pSender
->
seq
=
SYNC_SNAPSHOT_SEQ_BEGIN
;
pSender
->
ack
=
SYNC_SNAPSHOT_SEQ_INVALID
;
cJSON
*
snapshotReceiver2Json
(
SSyncSnapshotReceiver
*
pReceiver
)
{
return
NULL
;
}
// open snapshot reader
ASSERT
(
pSender
->
pReader
==
NULL
);
int32_t
ret
=
pSender
->
pSyncNode
->
pFsm
->
FpSnapshotStartRead
(
pSender
->
pSyncNode
->
pFsm
,
&
(
pSender
->
pReader
));
ASSERT
(
ret
==
0
);
char
*
snapshotReceiver2Str
(
SSyncSnapshotReceiver
*
pReceiver
)
{
return
NULL
;
}
// get current snapshot info
pSender
->
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSender
->
pSyncNode
->
pFsm
,
&
(
pSender
->
snapshot
));
// build begin msg
SyncSnapshotSend
*
pMsg
=
syncSnapshotSendBuild
(
0
,
pSender
->
pSyncNode
->
vgId
);
pMsg
->
srcId
=
pSender
->
pSyncNode
->
myRaftId
;
pMsg
->
destId
=
(
pSender
->
pSyncNode
->
replicasId
)[
pSender
->
replicaIndex
];
pMsg
->
term
=
pSender
->
pSyncNode
->
pRaftStore
->
currentTerm
;
pMsg
->
lastIndex
=
pSender
->
snapshot
.
lastApplyIndex
;
pMsg
->
lastTerm
=
pSender
->
snapshot
.
lastApplyTerm
;
pMsg
->
seq
=
pSender
->
seq
;
// SYNC_SNAPSHOT_SEQ_BEGIN
// send
SRpcMsg
rpcMsg
;
syncSnapshotSend2RpcMsg
(
pMsg
,
&
rpcMsg
);
syncNodeSendMsgById
(
&
(
pMsg
->
destId
),
pSender
->
pSyncNode
,
&
rpcMsg
);
char
*
msgStr
=
syncSnapshotSend2Str
(
pMsg
);
sTrace
(
"snapshot send begin seq:%d ack:%d send msg:%s"
,
pSender
->
seq
,
pSender
->
ack
,
msgStr
);
taosMemoryFree
(
msgStr
);
syncSnapshotSendDestroy
(
pMsg
);
}
// when entry in snapshot, start sender
void
snapshotSenderStart
(
SSyncSnapshotSender
*
pSender
)
{
if
(
!
(
pSender
->
start
))
{
// start
snapshotSenderDoStart
(
pSender
);
pSender
->
start
=
true
;
}
else
{
// already start
ASSERT
(
pSender
->
pSyncNode
->
pRaftStore
->
currentTerm
>=
pSender
->
term
);
// if current term is higher, need start again
if
(
pSender
->
pSyncNode
->
pRaftStore
->
currentTerm
>
pSender
->
term
)
{
// force peer rollback
SyncSnapshotSend
*
pMsg
=
syncSnapshotSendBuild
(
0
,
pSender
->
pSyncNode
->
vgId
);
pMsg
->
srcId
=
pSender
->
pSyncNode
->
myRaftId
;
pMsg
->
destId
=
(
pSender
->
pSyncNode
->
replicasId
)[
pSender
->
replicaIndex
];
pMsg
->
term
=
pSender
->
pSyncNode
->
pRaftStore
->
currentTerm
;
pMsg
->
lastIndex
=
pSender
->
snapshot
.
lastApplyIndex
;
pMsg
->
lastTerm
=
pSender
->
snapshot
.
lastApplyTerm
;
pMsg
->
seq
=
SYNC_SNAPSHOT_SEQ_FORCE_CLOSE
;
SRpcMsg
rpcMsg
;
syncSnapshotSend2RpcMsg
(
pMsg
,
&
rpcMsg
);
syncNodeSendMsgById
(
&
(
pMsg
->
destId
),
pSender
->
pSyncNode
,
&
rpcMsg
);
char
*
msgStr
=
syncSnapshotSend2Str
(
pMsg
);
sTrace
(
"snapshot send force close seq:%d ack:%d send msg:%s"
,
pSender
->
seq
,
pSender
->
ack
,
msgStr
);
taosMemoryFree
(
msgStr
);
syncSnapshotSendDestroy
(
pMsg
);
// close reader
int32_t
ret
=
pSender
->
pSyncNode
->
pFsm
->
FpSnapshotStopRead
(
pSender
->
pSyncNode
->
pFsm
,
pSender
->
pReader
);
ASSERT
(
ret
==
0
);
pSender
->
pReader
=
NULL
;
// start again
snapshotSenderDoStart
(
pSender
);
pSender
->
start
=
true
;
}
else
{
// current term, do nothing
ASSERT
(
pSender
->
pSyncNode
->
pRaftStore
->
currentTerm
==
pSender
->
term
);
}
}
char
*
s
=
snapshotSender2Str
(
pSender
);
sInfo
(
"snapshotSenderStart %s"
,
s
);
taosMemoryFree
(
s
);
}
void
snapshotSenderStop
(
SSyncSnapshotSender
*
pSender
)
{
if
(
pSender
->
pReader
!=
NULL
)
{
int32_t
ret
=
pSender
->
pSyncNode
->
pFsm
->
FpSnapshotStopRead
(
pSender
->
pSyncNode
->
pFsm
,
pSender
->
pReader
);
ASSERT
(
ret
==
0
);
pSender
->
pReader
=
NULL
;
}
if
(
pSender
->
pCurrentBlock
!=
NULL
)
{
taosMemoryFree
(
pSender
->
pCurrentBlock
);
pSender
->
pCurrentBlock
=
NULL
;
pSender
->
blockLen
=
0
;
}
pSender
->
start
=
false
;
char
*
s
=
snapshotSender2Str
(
pSender
);
sInfo
(
"snapshotSenderStop %s"
,
s
);
taosMemoryFree
(
s
);
}
// when sender receiver ack, call this function to send msg from seq
// seq = ack + 1, already updated
int32_t
snapshotSend
(
SSyncSnapshotSender
*
pSender
)
{
// free memory last time (seq - 1)
if
(
pSender
->
pCurrentBlock
!=
NULL
)
{
taosMemoryFree
(
pSender
->
pCurrentBlock
);
pSender
->
pCurrentBlock
=
NULL
;
pSender
->
blockLen
=
0
;
}
// read data
int32_t
ret
=
pSender
->
pSyncNode
->
pFsm
->
FpSnapshotDoRead
(
pSender
->
pSyncNode
->
pFsm
,
pSender
->
pReader
,
&
(
pSender
->
pCurrentBlock
),
&
(
pSender
->
blockLen
));
ASSERT
(
ret
==
0
);
if
(
pSender
->
blockLen
>
0
)
{
// has read data
}
else
{
// read finish
pSender
->
seq
=
SYNC_SNAPSHOT_SEQ_END
;
}
SyncSnapshotSend
*
pMsg
=
syncSnapshotSendBuild
(
pSender
->
blockLen
,
pSender
->
pSyncNode
->
vgId
);
pMsg
->
srcId
=
pSender
->
pSyncNode
->
myRaftId
;
pMsg
->
destId
=
(
pSender
->
pSyncNode
->
replicasId
)[
pSender
->
replicaIndex
];
pMsg
->
term
=
pSender
->
pSyncNode
->
pRaftStore
->
currentTerm
;
pMsg
->
lastIndex
=
pSender
->
snapshot
.
lastApplyIndex
;
pMsg
->
lastTerm
=
pSender
->
snapshot
.
lastApplyTerm
;
pMsg
->
seq
=
pSender
->
seq
;
memcpy
(
pMsg
->
data
,
pSender
->
pCurrentBlock
,
pSender
->
blockLen
);
SRpcMsg
rpcMsg
;
syncSnapshotSend2RpcMsg
(
pMsg
,
&
rpcMsg
);
syncNodeSendMsgById
(
&
(
pMsg
->
destId
),
pSender
->
pSyncNode
,
&
rpcMsg
);
char
*
msgStr
=
syncSnapshotSend2Str
(
pMsg
);
if
(
pSender
->
seq
==
SYNC_SNAPSHOT_SEQ_END
)
{
sTrace
(
"snapshot send finish seq:%d ack:%d send msg:%s"
,
pSender
->
seq
,
pSender
->
ack
,
msgStr
);
}
else
{
sTrace
(
"snapshot send sending seq:%d ack:%d send msg:%s"
,
pSender
->
seq
,
pSender
->
ack
,
msgStr
);
}
taosMemoryFree
(
msgStr
);
syncSnapshotSendDestroy
(
pMsg
);
return
0
;
}
// send snapshot data from cache
int32_t
snapshotReSend
(
SSyncSnapshotSender
*
pSender
)
{
if
(
pSender
->
pCurrentBlock
!=
NULL
)
{
SyncSnapshotSend
*
pMsg
=
syncSnapshotSendBuild
(
pSender
->
blockLen
,
pSender
->
pSyncNode
->
vgId
);
pMsg
->
srcId
=
pSender
->
pSyncNode
->
myRaftId
;
pMsg
->
destId
=
(
pSender
->
pSyncNode
->
replicasId
)[
pSender
->
replicaIndex
];
pMsg
->
term
=
pSender
->
pSyncNode
->
pRaftStore
->
currentTerm
;
pMsg
->
lastIndex
=
pSender
->
snapshot
.
lastApplyIndex
;
pMsg
->
lastTerm
=
pSender
->
snapshot
.
lastApplyTerm
;
pMsg
->
seq
=
pSender
->
seq
;
memcpy
(
pMsg
->
data
,
pSender
->
pCurrentBlock
,
pSender
->
blockLen
);
SRpcMsg
rpcMsg
;
syncSnapshotSend2RpcMsg
(
pMsg
,
&
rpcMsg
);
syncNodeSendMsgById
(
&
(
pMsg
->
destId
),
pSender
->
pSyncNode
,
&
rpcMsg
);
char
*
msgStr
=
syncSnapshotSend2Str
(
pMsg
);
sTrace
(
"snapshot send resend seq:%d ack:%d send msg:%s"
,
pSender
->
seq
,
pSender
->
ack
,
msgStr
);
taosMemoryFree
(
msgStr
);
syncSnapshotSendDestroy
(
pMsg
);
}
return
0
;
}
cJSON
*
snapshotSender2Json
(
SSyncSnapshotSender
*
pSender
)
{
char
u64buf
[
128
];
cJSON
*
pRoot
=
cJSON_CreateObject
();
if
(
pSender
!=
NULL
)
{
cJSON_AddNumberToObject
(
pRoot
,
"start"
,
pSender
->
start
);
cJSON_AddNumberToObject
(
pRoot
,
"seq"
,
pSender
->
seq
);
cJSON_AddNumberToObject
(
pRoot
,
"ack"
,
pSender
->
ack
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pSender
->
pReader
);
cJSON_AddStringToObject
(
pRoot
,
"pReader"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pSender
->
pCurrentBlock
);
cJSON_AddStringToObject
(
pRoot
,
"pCurrentBlock"
,
u64buf
);
cJSON_AddNumberToObject
(
pRoot
,
"blockLen"
,
pSender
->
blockLen
);
if
(
pSender
->
pCurrentBlock
!=
NULL
)
{
char
*
s
;
s
=
syncUtilprintBin
((
char
*
)(
pSender
->
pCurrentBlock
),
pSender
->
blockLen
);
cJSON_AddStringToObject
(
pRoot
,
"pCurrentBlock"
,
s
);
taosMemoryFree
(
s
);
s
=
syncUtilprintBin2
((
char
*
)(
pSender
->
pCurrentBlock
),
pSender
->
blockLen
);
cJSON_AddStringToObject
(
pRoot
,
"pCurrentBlock2"
,
s
);
taosMemoryFree
(
s
);
}
cJSON
*
pSnapshot
=
cJSON_CreateObject
();
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pSender
->
snapshot
.
lastApplyIndex
);
cJSON_AddStringToObject
(
pSnapshot
,
"lastApplyIndex"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pSender
->
snapshot
.
lastApplyTerm
);
cJSON_AddStringToObject
(
pSnapshot
,
"lastApplyTerm"
,
u64buf
);
cJSON_AddItemToObject
(
pRoot
,
"snapshot"
,
pSnapshot
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pSender
->
sendingMS
);
cJSON_AddStringToObject
(
pRoot
,
"sendingMS"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pSender
->
pSyncNode
);
cJSON_AddStringToObject
(
pRoot
,
"pSyncNode"
,
u64buf
);
cJSON_AddNumberToObject
(
pRoot
,
"replicaIndex"
,
pSender
->
replicaIndex
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pSender
->
term
);
cJSON_AddStringToObject
(
pRoot
,
"term"
,
u64buf
);
cJSON_AddNumberToObject
(
pRoot
,
"finish"
,
pSender
->
finish
);
}
cJSON
*
pJson
=
cJSON_CreateObject
();
cJSON_AddItemToObject
(
pJson
,
"SSyncSnapshotSender"
,
pRoot
);
return
pJson
;
}
char
*
snapshotSender2Str
(
SSyncSnapshotSender
*
pSender
)
{
cJSON
*
pJson
=
snapshotSender2Json
(
pSender
);
char
*
serialized
=
cJSON_Print
(
pJson
);
cJSON_Delete
(
pJson
);
return
serialized
;
}
// -------------------------------------
SSyncSnapshotReceiver
*
snapshotReceiverCreate
(
SSyncNode
*
pSyncNode
,
int32_t
replicaIndex
)
{
bool
condition
=
(
pSyncNode
->
pFsm
->
FpSnapshotStartWrite
!=
NULL
)
&&
(
pSyncNode
->
pFsm
->
FpSnapshotStopWrite
!=
NULL
)
&&
(
pSyncNode
->
pFsm
->
FpSnapshotDoWrite
!=
NULL
);
SSyncSnapshotReceiver
*
pReceiver
;
if
(
condition
)
{
pReceiver
=
taosMemoryMalloc
(
sizeof
(
SSyncSnapshotReceiver
));
ASSERT
(
pReceiver
!=
NULL
);
memset
(
pReceiver
,
0
,
sizeof
(
*
pReceiver
));
pReceiver
->
start
=
false
;
pReceiver
->
ack
=
SYNC_SNAPSHOT_SEQ_BEGIN
;
pReceiver
->
pWriter
=
NULL
;
pReceiver
->
pSyncNode
=
pSyncNode
;
pReceiver
->
replicaIndex
=
replicaIndex
;
pReceiver
->
term
=
pSyncNode
->
pRaftStore
->
currentTerm
;
}
else
{
sInfo
(
"snapshotReceiverCreate cannot create receiver"
);
}
return
pReceiver
;
}
void
snapshotReceiverDestroy
(
SSyncSnapshotReceiver
*
pReceiver
)
{
if
(
pReceiver
!=
NULL
)
{
taosMemoryFree
(
pReceiver
);
}
}
// begin receive snapshot msg (current term, seq begin)
static
void
snapshotReceiverDoStart
(
SSyncSnapshotReceiver
*
pReceiver
)
{
pReceiver
->
term
=
pReceiver
->
pSyncNode
->
pRaftStore
->
currentTerm
;
pReceiver
->
ack
=
SYNC_SNAPSHOT_SEQ_BEGIN
;
ASSERT
(
pReceiver
->
pWriter
==
NULL
);
int32_t
ret
=
pReceiver
->
pSyncNode
->
pFsm
->
FpSnapshotStartWrite
(
pReceiver
->
pSyncNode
->
pFsm
,
&
(
pReceiver
->
pWriter
));
ASSERT
(
ret
==
0
);
}
// if receiver receive msg from seq = SYNC_SNAPSHOT_SEQ_BEGIN, start receiver
// if already start, force close, start again
void
snapshotReceiverStart
(
SSyncSnapshotReceiver
*
pReceiver
)
{
if
(
!
(
pReceiver
->
start
))
{
// start
snapshotReceiverDoStart
(
pReceiver
);
pReceiver
->
start
=
true
;
}
else
{
// already start
// force close, abandon incomplete data
int32_t
ret
=
pReceiver
->
pSyncNode
->
pFsm
->
FpSnapshotStopWrite
(
pReceiver
->
pSyncNode
->
pFsm
,
pReceiver
->
pWriter
,
false
);
ASSERT
(
ret
==
0
);
pReceiver
->
pWriter
=
NULL
;
// start again
snapshotReceiverDoStart
(
pReceiver
);
pReceiver
->
start
=
true
;
ASSERT
(
0
);
}
char
*
s
=
snapshotReceiver2Str
(
pReceiver
);
sInfo
(
"snapshotReceiverStart %s"
,
s
);
taosMemoryFree
(
s
);
}
void
snapshotReceiverStop
(
SSyncSnapshotReceiver
*
pReceiver
)
{
if
(
pReceiver
->
pWriter
!=
NULL
)
{
int32_t
ret
=
pReceiver
->
pSyncNode
->
pFsm
->
FpSnapshotStopWrite
(
pReceiver
->
pSyncNode
->
pFsm
,
pReceiver
->
pWriter
,
false
);
ASSERT
(
ret
==
0
);
pReceiver
->
pWriter
=
NULL
;
}
pReceiver
->
start
=
false
;
char
*
s
=
snapshotReceiver2Str
(
pReceiver
);
sInfo
(
"snapshotReceiverStop %s"
,
s
);
taosMemoryFree
(
s
);
}
cJSON
*
snapshotReceiver2Json
(
SSyncSnapshotReceiver
*
pReceiver
)
{
char
u64buf
[
128
];
cJSON
*
pRoot
=
cJSON_CreateObject
();
if
(
pReceiver
!=
NULL
)
{
cJSON_AddNumberToObject
(
pRoot
,
"start"
,
pReceiver
->
start
);
cJSON_AddNumberToObject
(
pRoot
,
"ack"
,
pReceiver
->
ack
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pReceiver
->
pWriter
);
cJSON_AddStringToObject
(
pRoot
,
"pWriter"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pReceiver
->
pSyncNode
);
cJSON_AddStringToObject
(
pRoot
,
"pSyncNode"
,
u64buf
);
cJSON_AddNumberToObject
(
pRoot
,
"replicaIndex"
,
pReceiver
->
replicaIndex
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pReceiver
->
term
);
cJSON_AddStringToObject
(
pRoot
,
"term"
,
u64buf
);
}
cJSON
*
pJson
=
cJSON_CreateObject
();
cJSON_AddItemToObject
(
pJson
,
"SSyncSnapshotReceiver"
,
pRoot
);
return
pJson
;
}
char
*
snapshotReceiver2Str
(
SSyncSnapshotReceiver
*
pReceiver
)
{
cJSON
*
pJson
=
snapshotReceiver2Json
(
pReceiver
);
char
*
serialized
=
cJSON_Print
(
pJson
);
cJSON_Delete
(
pJson
);
return
serialized
;
}
// receiver do something
int32_t
syncNodeOnSnapshotSendCb
(
SSyncNode
*
pSyncNode
,
SyncSnapshotSend
*
pMsg
)
{
// get receiver
SSyncSnapshotReceiver
*
pReceiver
=
pSyncNode
->
pNewNodeReceiver
;
bool
needRsp
=
false
;
// state, term, seq/ack
if
(
pSyncNode
->
state
==
TAOS_SYNC_STATE_FOLLOWER
)
{
if
(
pMsg
->
term
==
pSyncNode
->
pRaftStore
->
currentTerm
)
{
if
(
pMsg
->
seq
==
SYNC_SNAPSHOT_SEQ_BEGIN
)
{
// begin
snapshotReceiverStart
(
pReceiver
);
pReceiver
->
ack
=
pMsg
->
seq
;
needRsp
=
true
;
char
*
msgStr
=
syncSnapshotSend2Str
(
pMsg
);
sTrace
(
"snapshot recv begin ack:%d recv msg:%s"
,
pReceiver
->
ack
,
msgStr
);
taosMemoryFree
(
msgStr
);
}
else
if
(
pMsg
->
seq
==
SYNC_SNAPSHOT_SEQ_END
)
{
// end, finish FSM
pSyncNode
->
pFsm
->
FpSnapshotDoWrite
(
pSyncNode
->
pFsm
,
pReceiver
->
pWriter
,
pMsg
->
data
,
pMsg
->
dataLen
);
pSyncNode
->
pFsm
->
FpSnapshotStopWrite
(
pSyncNode
->
pFsm
,
pReceiver
->
pWriter
,
true
);
pSyncNode
->
pLogStore
->
syncLogSetBeginIndex
(
pSyncNode
->
pLogStore
,
pMsg
->
lastIndex
+
1
);
char
*
logSimpleStr
=
logStoreSimple2Str
(
pSyncNode
->
pLogStore
);
sInfo
(
"snapshot receive finish, update log begin index:%ld, raft log:%s"
,
pMsg
->
lastIndex
+
1
,
logSimpleStr
);
taosMemoryFree
(
logSimpleStr
);
// walRestoreFromSnapshot(pSyncNode->pWal, pMsg->lastIndex);
// sInfo("walRestoreFromSnapshot lastIndex:%ld", pMsg->lastIndex);
pReceiver
->
pWriter
=
NULL
;
snapshotReceiverStop
(
pReceiver
);
pReceiver
->
ack
=
pMsg
->
seq
;
needRsp
=
true
;
char
*
msgStr
=
syncSnapshotSend2Str
(
pMsg
);
sTrace
(
"snapshot recv end ack:%d recv msg:%s"
,
pReceiver
->
ack
,
msgStr
);
taosMemoryFree
(
msgStr
);
}
else
if
(
pMsg
->
seq
==
SYNC_SNAPSHOT_SEQ_FORCE_CLOSE
)
{
pSyncNode
->
pFsm
->
FpSnapshotStopWrite
(
pSyncNode
->
pFsm
,
pReceiver
->
pWriter
,
false
);
snapshotReceiverStop
(
pReceiver
);
needRsp
=
false
;
char
*
msgStr
=
syncSnapshotSend2Str
(
pMsg
);
sTrace
(
"snapshot recv force close ack:%d recv msg:%s"
,
pReceiver
->
ack
,
msgStr
);
taosMemoryFree
(
msgStr
);
}
else
if
(
pMsg
->
seq
>
SYNC_SNAPSHOT_SEQ_BEGIN
&&
pMsg
->
seq
<
SYNC_SNAPSHOT_SEQ_END
)
{
// transfering
if
(
pMsg
->
seq
==
pReceiver
->
ack
+
1
)
{
pSyncNode
->
pFsm
->
FpSnapshotDoWrite
(
pSyncNode
->
pFsm
,
pReceiver
->
pWriter
,
pMsg
->
data
,
pMsg
->
dataLen
);
pReceiver
->
ack
=
pMsg
->
seq
;
}
needRsp
=
true
;
char
*
msgStr
=
syncSnapshotSend2Str
(
pMsg
);
sTrace
(
"snapshot recv receiving ack:%d recv msg:%s"
,
pReceiver
->
ack
,
msgStr
);
taosMemoryFree
(
msgStr
);
}
else
{
ASSERT
(
0
);
}
if
(
needRsp
)
{
SyncSnapshotRsp
*
pRspMsg
=
syncSnapshotRspBuild
(
pSyncNode
->
vgId
);
pRspMsg
->
srcId
=
pSyncNode
->
myRaftId
;
pRspMsg
->
destId
=
pMsg
->
srcId
;
pRspMsg
->
term
=
pSyncNode
->
pRaftStore
->
currentTerm
;
pRspMsg
->
lastIndex
=
pMsg
->
lastIndex
;
pRspMsg
->
lastTerm
=
pMsg
->
lastTerm
;
pRspMsg
->
ack
=
pReceiver
->
ack
;
SRpcMsg
rpcMsg
;
syncSnapshotRsp2RpcMsg
(
pRspMsg
,
&
rpcMsg
);
syncNodeSendMsgById
(
&
(
pRspMsg
->
destId
),
pSyncNode
,
&
rpcMsg
);
syncSnapshotRspDestroy
(
pRspMsg
);
}
}
}
else
{
syncNodeLog2
(
"syncNodeOnSnapshotSendCb not follower"
,
pSyncNode
);
}
return
0
;
}
// sender receives ack, set seq = ack + 1, send msg from seq
// if ack == SYNC_SNAPSHOT_SEQ_END, stop sender
int32_t
syncNodeOnSnapshotRspCb
(
SSyncNode
*
pSyncNode
,
SyncSnapshotRsp
*
pMsg
)
{
// get sender
SSyncSnapshotSender
*
pSender
=
NULL
;
for
(
int
i
=
0
;
i
<
pSyncNode
->
replicaNum
;
++
i
)
{
if
(
syncUtilSameId
(
&
(
pMsg
->
srcId
),
&
((
pSyncNode
->
replicasId
)[
i
])))
{
pSender
=
(
pSyncNode
->
senders
)[
i
];
}
}
ASSERT
(
pSender
!=
NULL
);
// state, term, seq/ack
if
(
pSyncNode
->
state
==
TAOS_SYNC_STATE_LEADER
)
{
if
(
pMsg
->
term
==
pSyncNode
->
pRaftStore
->
currentTerm
)
{
// receiver ack is finish, close sender
if
(
pMsg
->
ack
==
SYNC_SNAPSHOT_SEQ_END
)
{
pSender
->
finish
=
true
;
snapshotSenderStop
(
pSender
);
return
0
;
}
// send next msg
if
(
pMsg
->
ack
==
pSender
->
seq
)
{
// update sender ack
pSender
->
ack
=
pMsg
->
ack
;
(
pSender
->
seq
)
++
;
snapshotSend
(
pSender
);
}
else
if
(
pMsg
->
ack
==
pSender
->
seq
-
1
)
{
snapshotReSend
(
pSender
);
}
else
{
ASSERT
(
0
);
}
}
}
else
{
syncNodeLog2
(
"syncNodeOnSnapshotRspCb not leader"
,
pSyncNode
);
}
return
0
;
}
\ No newline at end of file
source/libs/sync/test/CMakeLists.txt
浏览文件 @
a047d4a5
...
...
@@ -38,6 +38,15 @@ add_executable(syncRespMgrTest "")
add_executable
(
syncSnapshotTest
""
)
add_executable
(
syncApplyMsgTest
""
)
add_executable
(
syncConfigChangeTest
""
)
add_executable
(
syncConfigChangeSnapshotTest
""
)
add_executable
(
syncSnapshotSendTest
""
)
add_executable
(
syncSnapshotRspTest
""
)
add_executable
(
syncSnapshotSenderTest
""
)
add_executable
(
syncSnapshotReceiverTest
""
)
add_executable
(
syncTestTool
""
)
add_executable
(
syncRaftLogTest
""
)
add_executable
(
syncRaftLogTest2
""
)
add_executable
(
syncRaftLogTest3
""
)
target_sources
(
syncTest
...
...
@@ -200,6 +209,42 @@ target_sources(syncConfigChangeTest
PRIVATE
"syncConfigChangeTest.cpp"
)
target_sources
(
syncConfigChangeSnapshotTest
PRIVATE
"syncConfigChangeSnapshotTest.cpp"
)
target_sources
(
syncSnapshotSendTest
PRIVATE
"syncSnapshotSendTest.cpp"
)
target_sources
(
syncSnapshotRspTest
PRIVATE
"syncSnapshotRspTest.cpp"
)
target_sources
(
syncSnapshotSenderTest
PRIVATE
"syncSnapshotSenderTest.cpp"
)
target_sources
(
syncSnapshotReceiverTest
PRIVATE
"syncSnapshotReceiverTest.cpp"
)
target_sources
(
syncTestTool
PRIVATE
"syncTestTool.cpp"
)
target_sources
(
syncRaftLogTest
PRIVATE
"syncRaftLogTest.cpp"
)
target_sources
(
syncRaftLogTest2
PRIVATE
"syncRaftLogTest2.cpp"
)
target_sources
(
syncRaftLogTest3
PRIVATE
"syncRaftLogTest3.cpp"
)
target_include_directories
(
syncTest
...
...
@@ -402,6 +447,51 @@ target_include_directories(syncConfigChangeTest
"
${
TD_SOURCE_DIR
}
/include/libs/sync"
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../inc"
)
target_include_directories
(
syncConfigChangeSnapshotTest
PUBLIC
"
${
TD_SOURCE_DIR
}
/include/libs/sync"
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../inc"
)
target_include_directories
(
syncSnapshotSendTest
PUBLIC
"
${
TD_SOURCE_DIR
}
/include/libs/sync"
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../inc"
)
target_include_directories
(
syncSnapshotRspTest
PUBLIC
"
${
TD_SOURCE_DIR
}
/include/libs/sync"
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../inc"
)
target_include_directories
(
syncSnapshotSenderTest
PUBLIC
"
${
TD_SOURCE_DIR
}
/include/libs/sync"
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../inc"
)
target_include_directories
(
syncSnapshotReceiverTest
PUBLIC
"
${
TD_SOURCE_DIR
}
/include/libs/sync"
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../inc"
)
target_include_directories
(
syncTestTool
PUBLIC
"
${
TD_SOURCE_DIR
}
/include/libs/sync"
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../inc"
)
target_include_directories
(
syncRaftLogTest
PUBLIC
"
${
TD_SOURCE_DIR
}
/include/libs/sync"
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../inc"
)
target_include_directories
(
syncRaftLogTest2
PUBLIC
"
${
TD_SOURCE_DIR
}
/include/libs/sync"
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../inc"
)
target_include_directories
(
syncRaftLogTest3
PUBLIC
"
${
TD_SOURCE_DIR
}
/include/libs/sync"
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../inc"
)
target_link_libraries
(
syncTest
...
...
@@ -564,6 +654,42 @@ target_link_libraries(syncConfigChangeTest
sync
gtest_main
)
target_link_libraries
(
syncConfigChangeSnapshotTest
sync
gtest_main
)
target_link_libraries
(
syncSnapshotSendTest
sync
gtest_main
)
target_link_libraries
(
syncSnapshotRspTest
sync
gtest_main
)
target_link_libraries
(
syncSnapshotSenderTest
sync
gtest_main
)
target_link_libraries
(
syncSnapshotReceiverTest
sync
gtest_main
)
target_link_libraries
(
syncTestTool
sync
gtest_main
)
target_link_libraries
(
syncRaftLogTest
sync
gtest_main
)
target_link_libraries
(
syncRaftLogTest2
sync
gtest_main
)
target_link_libraries
(
syncRaftLogTest3
sync
gtest_main
)
enable_testing
()
...
...
source/libs/sync/test/syncConfigChangeSnapshotTest.cpp
0 → 100644
浏览文件 @
a047d4a5
#include <gtest/gtest.h>
#include <stdio.h>
#include "os.h"
#include "syncEnv.h"
#include "syncIO.h"
#include "syncInt.h"
#include "syncUtil.h"
#include "wal.h"
void
logTest
()
{
sTrace
(
"--- sync log test: trace"
);
sDebug
(
"--- sync log test: debug"
);
sInfo
(
"--- sync log test: info"
);
sWarn
(
"--- sync log test: warn"
);
sError
(
"--- sync log test: error"
);
sFatal
(
"--- sync log test: fatal"
);
}
uint16_t
gPorts
[]
=
{
7010
,
7110
,
7210
,
7310
,
7410
};
const
char
*
gDir
=
"./syncReplicateTest"
;
int32_t
gVgId
=
1234
;
SyncIndex
gSnapshotLastApplyIndex
;
SyncIndex
gSnapshotLastApplyTerm
;
void
init
()
{
int
code
=
walInit
();
assert
(
code
==
0
);
code
=
syncInit
();
assert
(
code
==
0
);
sprintf
(
tsTempDir
,
"%s"
,
"."
);
}
void
cleanup
()
{
walCleanUp
();
}
void
CommitCb
(
struct
SSyncFSM
*
pFsm
,
const
SRpcMsg
*
pMsg
,
SFsmCbMeta
cbMeta
)
{
SyncIndex
beginIndex
=
SYNC_INDEX_INVALID
;
if
(
pFsm
->
FpGetSnapshot
!=
NULL
)
{
SSnapshot
snapshot
;
pFsm
->
FpGetSnapshot
(
pFsm
,
&
snapshot
);
beginIndex
=
snapshot
.
lastApplyIndex
;
}
if
(
cbMeta
.
index
>
beginIndex
)
{
char
logBuf
[
256
]
=
{
0
};
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"==callback== ==CommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s, flag:%lu, term:%lu
\n
"
,
pFsm
,
cbMeta
.
index
,
cbMeta
.
isWeak
,
cbMeta
.
code
,
cbMeta
.
state
,
syncUtilState2String
(
cbMeta
.
state
),
cbMeta
.
flag
,
cbMeta
.
term
);
syncRpcMsgLog2
(
logBuf
,
(
SRpcMsg
*
)
pMsg
);
}
else
{
sTrace
(
"==callback== ==CommitCb== do not apply again %ld"
,
cbMeta
.
index
);
}
}
void
PreCommitCb
(
struct
SSyncFSM
*
pFsm
,
const
SRpcMsg
*
pMsg
,
SFsmCbMeta
cbMeta
)
{
char
logBuf
[
256
]
=
{
0
};
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s flag:%lu
\n
"
,
pFsm
,
cbMeta
.
index
,
cbMeta
.
isWeak
,
cbMeta
.
code
,
cbMeta
.
state
,
syncUtilState2String
(
cbMeta
.
state
),
cbMeta
.
flag
);
syncRpcMsgLog2
(
logBuf
,
(
SRpcMsg
*
)
pMsg
);
}
void
RollBackCb
(
struct
SSyncFSM
*
pFsm
,
const
SRpcMsg
*
pMsg
,
SFsmCbMeta
cbMeta
)
{
char
logBuf
[
256
];
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"==callback== ==RollBackCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s flag:%lu
\n
"
,
pFsm
,
cbMeta
.
index
,
cbMeta
.
isWeak
,
cbMeta
.
code
,
cbMeta
.
state
,
syncUtilState2String
(
cbMeta
.
state
),
cbMeta
.
flag
);
syncRpcMsgLog2
(
logBuf
,
(
SRpcMsg
*
)
pMsg
);
}
int32_t
GetSnapshotCb
(
struct
SSyncFSM
*
pFsm
,
SSnapshot
*
pSnapshot
)
{
pSnapshot
->
data
=
NULL
;
pSnapshot
->
lastApplyIndex
=
gSnapshotLastApplyIndex
;
pSnapshot
->
lastApplyTerm
=
gSnapshotLastApplyTerm
;
return
0
;
}
int32_t
SnapshotStartRead
(
struct
SSyncFSM
*
pFsm
,
void
**
ppReader
)
{
*
ppReader
=
(
void
*
)
0xABCD
;
char
logBuf
[
256
]
=
{
0
};
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"==callback== ==SnapshotStartRead== pFsm:%p, *ppReader:%p"
,
pFsm
,
*
ppReader
);
sTrace
(
"%s"
,
logBuf
);
return
0
;
}
int32_t
SnapshotStopRead
(
struct
SSyncFSM
*
pFsm
,
void
*
pReader
)
{
char
logBuf
[
256
]
=
{
0
};
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"==callback== ==SnapshotStopRead== pFsm:%p, pReader:%p"
,
pFsm
,
pReader
);
sTrace
(
"%s"
,
logBuf
);
return
0
;
}
int32_t
SnapshotDoRead
(
struct
SSyncFSM
*
pFsm
,
void
*
pReader
,
void
**
ppBuf
,
int32_t
*
len
)
{
static
int
readIter
=
0
;
if
(
readIter
==
5
)
{
*
len
=
0
;
*
ppBuf
=
NULL
;
}
else
if
(
readIter
<
5
)
{
*
len
=
20
;
*
ppBuf
=
taosMemoryMalloc
(
*
len
);
snprintf
((
char
*
)
*
ppBuf
,
*
len
,
"data iter:%d"
,
readIter
);
}
char
logBuf
[
256
]
=
{
0
};
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"==callback== ==SnapshotDoRead== pFsm:%p, pReader:%p, *len:%d, *ppBuf:%s, readIter:%d"
,
pFsm
,
pReader
,
*
len
,
(
char
*
)(
*
ppBuf
),
readIter
);
sTrace
(
"%s"
,
logBuf
);
readIter
++
;
return
0
;
}
int32_t
SnapshotStartWrite
(
struct
SSyncFSM
*
pFsm
,
void
**
ppWriter
)
{
*
ppWriter
=
(
void
*
)
0xCDEF
;
char
logBuf
[
256
]
=
{
0
};
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"==callback== ==SnapshotStartWrite== pFsm:%p, *ppWriter:%p"
,
pFsm
,
*
ppWriter
);
sTrace
(
"%s"
,
logBuf
);
return
0
;
}
int32_t
SnapshotStopWrite
(
struct
SSyncFSM
*
pFsm
,
void
*
pWriter
,
bool
isApply
)
{
char
logBuf
[
256
]
=
{
0
};
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"==callback== ==SnapshotStopWrite== pFsm:%p, pWriter:%p, isApply:%d"
,
pFsm
,
pWriter
,
isApply
);
sTrace
(
"%s"
,
logBuf
);
if
(
isApply
)
{
gSnapshotLastApplyIndex
=
10
;
gSnapshotLastApplyTerm
=
1
;
}
return
0
;
}
int32_t
SnapshotDoWrite
(
struct
SSyncFSM
*
pFsm
,
void
*
pWriter
,
void
*
pBuf
,
int32_t
len
)
{
char
logBuf
[
256
]
=
{
0
};
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"==callback== ==SnapshotDoWrite== pFsm:%p, pWriter:%p, len:%d pBuf:%s"
,
pFsm
,
pWriter
,
len
,
(
char
*
)
pBuf
);
sTrace
(
"%s"
,
logBuf
);
return
0
;
}
void
RestoreFinishCb
(
struct
SSyncFSM
*
pFsm
)
{
sTrace
(
"==callback== ==RestoreFinishCb=="
);
}
void
ReConfigCb
(
struct
SSyncFSM
*
pFsm
,
SSyncCfg
newCfg
,
SReConfigCbMeta
cbMeta
)
{
sTrace
(
"==callback== ==ReConfigCb== flag:0x%lX, isDrop:%d, index:%ld, code:%d, currentTerm:%lu, term:%lu"
,
cbMeta
.
flag
,
cbMeta
.
isDrop
,
cbMeta
.
index
,
cbMeta
.
code
,
cbMeta
.
currentTerm
,
cbMeta
.
term
);
}
SSyncFSM
*
createFsm
()
{
SSyncFSM
*
pFsm
=
(
SSyncFSM
*
)
taosMemoryMalloc
(
sizeof
(
SSyncFSM
));
memset
(
pFsm
,
0
,
sizeof
(
*
pFsm
));
pFsm
->
FpCommitCb
=
CommitCb
;
pFsm
->
FpPreCommitCb
=
PreCommitCb
;
pFsm
->
FpRollBackCb
=
RollBackCb
;
pFsm
->
FpGetSnapshot
=
GetSnapshotCb
;
pFsm
->
FpRestoreFinishCb
=
RestoreFinishCb
;
pFsm
->
FpSnapshotStartRead
=
SnapshotStartRead
;
pFsm
->
FpSnapshotStopRead
=
SnapshotStopRead
;
pFsm
->
FpSnapshotDoRead
=
SnapshotDoRead
;
pFsm
->
FpSnapshotStartWrite
=
SnapshotStartWrite
;
pFsm
->
FpSnapshotStopWrite
=
SnapshotStopWrite
;
pFsm
->
FpSnapshotDoWrite
=
SnapshotDoWrite
;
pFsm
->
FpReConfigCb
=
ReConfigCb
;
return
pFsm
;
}
SWal
*
createWal
(
char
*
path
,
int32_t
vgId
)
{
SWalCfg
walCfg
;
memset
(
&
walCfg
,
0
,
sizeof
(
SWalCfg
));
walCfg
.
vgId
=
vgId
;
walCfg
.
fsyncPeriod
=
1000
;
walCfg
.
retentionPeriod
=
1000
;
walCfg
.
rollPeriod
=
1000
;
walCfg
.
retentionSize
=
1000
;
walCfg
.
segSize
=
1000
;
walCfg
.
level
=
TAOS_WAL_FSYNC
;
SWal
*
pWal
=
walOpen
(
path
,
&
walCfg
);
assert
(
pWal
!=
NULL
);
return
pWal
;
}
int64_t
createSyncNode
(
int32_t
replicaNum
,
int32_t
myIndex
,
int32_t
vgId
,
SWal
*
pWal
,
char
*
path
,
bool
isStandBy
)
{
SSyncInfo
syncInfo
;
syncInfo
.
vgId
=
vgId
;
syncInfo
.
msgcb
=
&
gSyncIO
->
msgcb
;
syncInfo
.
FpSendMsg
=
syncIOSendMsg
;
syncInfo
.
FpEqMsg
=
syncIOEqMsg
;
syncInfo
.
pFsm
=
createFsm
();
snprintf
(
syncInfo
.
path
,
sizeof
(
syncInfo
.
path
),
"%s_sync_replica%d_index%d"
,
path
,
replicaNum
,
myIndex
);
syncInfo
.
pWal
=
pWal
;
syncInfo
.
isStandBy
=
isStandBy
;
syncInfo
.
snapshotEnable
=
true
;
SSyncCfg
*
pCfg
=
&
syncInfo
.
syncCfg
;
if
(
isStandBy
)
{
pCfg
->
myIndex
=
0
;
pCfg
->
replicaNum
=
1
;
pCfg
->
nodeInfo
[
0
].
nodePort
=
gPorts
[
myIndex
];
taosGetFqdn
(
pCfg
->
nodeInfo
[
0
].
nodeFqdn
);
}
else
{
pCfg
->
myIndex
=
myIndex
;
pCfg
->
replicaNum
=
replicaNum
;
for
(
int
i
=
0
;
i
<
replicaNum
;
++
i
)
{
pCfg
->
nodeInfo
[
i
].
nodePort
=
gPorts
[
i
];
taosGetFqdn
(
pCfg
->
nodeInfo
[
i
].
nodeFqdn
);
// snprintf(pCfg->nodeInfo[i].nodeFqdn, sizeof(pCfg->nodeInfo[i].nodeFqdn), "%s", "127.0.0.1");
}
}
int64_t
rid
=
syncOpen
(
&
syncInfo
);
assert
(
rid
>
0
);
SSyncNode
*
pSyncNode
=
(
SSyncNode
*
)
syncNodeAcquire
(
rid
);
assert
(
pSyncNode
!=
NULL
);
gSyncIO
->
FpOnSyncPing
=
pSyncNode
->
FpOnPing
;
gSyncIO
->
FpOnSyncPingReply
=
pSyncNode
->
FpOnPingReply
;
gSyncIO
->
FpOnSyncTimeout
=
pSyncNode
->
FpOnTimeout
;
gSyncIO
->
FpOnSyncClientRequest
=
pSyncNode
->
FpOnClientRequest
;
gSyncIO
->
FpOnSyncRequestVote
=
pSyncNode
->
FpOnRequestVote
;
gSyncIO
->
FpOnSyncRequestVoteReply
=
pSyncNode
->
FpOnRequestVoteReply
;
gSyncIO
->
FpOnSyncAppendEntries
=
pSyncNode
->
FpOnAppendEntries
;
gSyncIO
->
FpOnSyncAppendEntriesReply
=
pSyncNode
->
FpOnAppendEntriesReply
;
gSyncIO
->
FpOnSyncSnapshotSend
=
pSyncNode
->
FpOnSnapshotSend
;
gSyncIO
->
FpOnSyncSnapshotRsp
=
pSyncNode
->
FpOnSnapshotRsp
;
gSyncIO
->
pSyncNode
=
pSyncNode
;
syncNodeRelease
(
pSyncNode
);
return
rid
;
}
void
configChange
(
int64_t
rid
,
int32_t
replicaNum
,
int32_t
myIndex
)
{
SSyncCfg
syncCfg
;
syncCfg
.
myIndex
=
myIndex
;
syncCfg
.
replicaNum
=
replicaNum
;
for
(
int
i
=
0
;
i
<
replicaNum
;
++
i
)
{
syncCfg
.
nodeInfo
[
i
].
nodePort
=
gPorts
[
i
];
taosGetFqdn
(
syncCfg
.
nodeInfo
[
i
].
nodeFqdn
);
}
syncReconfig
(
rid
,
&
syncCfg
);
}
void
usage
(
char
*
exe
)
{
printf
(
"usage: %s replicaNum myIndex lastApplyIndex writeRecordNum isStandBy isConfigChange lastApplyTerm
\n
"
,
exe
);
}
SRpcMsg
*
createRpcMsg
(
int
i
,
int
count
,
int
myIndex
)
{
SRpcMsg
*
pMsg
=
(
SRpcMsg
*
)
taosMemoryMalloc
(
sizeof
(
SRpcMsg
));
memset
(
pMsg
,
0
,
sizeof
(
SRpcMsg
));
pMsg
->
msgType
=
9999
;
pMsg
->
contLen
=
256
;
pMsg
->
pCont
=
rpcMallocCont
(
pMsg
->
contLen
);
snprintf
((
char
*
)(
pMsg
->
pCont
),
pMsg
->
contLen
,
"value-myIndex:%u-%d-%d-%ld"
,
myIndex
,
i
,
count
,
taosGetTimestampMs
());
return
pMsg
;
}
int
main
(
int
argc
,
char
**
argv
)
{
tsAsyncLog
=
0
;
sDebugFlag
=
DEBUG_TRACE
+
DEBUG_SCREEN
+
DEBUG_FILE
+
DEBUG_INFO
;
if
(
argc
!=
8
)
{
usage
(
argv
[
0
]);
exit
(
-
1
);
}
int32_t
replicaNum
=
atoi
(
argv
[
1
]);
int32_t
myIndex
=
atoi
(
argv
[
2
]);
int32_t
lastApplyIndex
=
atoi
(
argv
[
3
]);
int32_t
writeRecordNum
=
atoi
(
argv
[
4
]);
bool
isStandBy
=
atoi
(
argv
[
5
]);
bool
isConfigChange
=
atoi
(
argv
[
6
]);
int32_t
lastApplyTerm
=
atoi
(
argv
[
7
]);
sTrace
(
"args: replicaNum:%d, myIndex:%d, lastApplyIndex:%d, writeRecordNum:%d, isStandBy:%d, isConfigChange:%d, "
"lastApplyTerm:%d"
,
replicaNum
,
myIndex
,
lastApplyIndex
,
writeRecordNum
,
isStandBy
,
isConfigChange
,
lastApplyTerm
);
gSnapshotLastApplyIndex
=
lastApplyIndex
;
gSnapshotLastApplyTerm
=
lastApplyTerm
;
if
(
!
isStandBy
)
{
assert
(
replicaNum
>=
1
&&
replicaNum
<=
5
);
assert
(
myIndex
>=
0
&&
myIndex
<
replicaNum
);
assert
(
lastApplyIndex
>=
-
1
);
assert
(
writeRecordNum
>=
0
);
}
init
();
int32_t
ret
=
syncIOStart
((
char
*
)
"127.0.0.1"
,
gPorts
[
myIndex
]);
assert
(
ret
==
0
);
char
walPath
[
128
];
snprintf
(
walPath
,
sizeof
(
walPath
),
"%s_wal_replica%d_index%d"
,
gDir
,
replicaNum
,
myIndex
);
SWal
*
pWal
=
createWal
(
walPath
,
gVgId
);
int64_t
rid
=
createSyncNode
(
replicaNum
,
myIndex
,
gVgId
,
pWal
,
(
char
*
)
gDir
,
isStandBy
);
assert
(
rid
>
0
);
syncStart
(
rid
);
/*
if (isStandBy) {
syncStartStandBy(rid);
} else {
syncStart(rid);
}
*/
SSyncNode
*
pSyncNode
=
(
SSyncNode
*
)
syncNodeAcquire
(
rid
);
assert
(
pSyncNode
!=
NULL
);
if
(
isConfigChange
)
{
configChange
(
rid
,
2
,
myIndex
);
}
//---------------------------
int32_t
alreadySend
=
0
;
while
(
1
)
{
char
*
s
=
syncNode2SimpleStr
(
pSyncNode
);
if
(
alreadySend
<
writeRecordNum
)
{
SRpcMsg
*
pRpcMsg
=
createRpcMsg
(
alreadySend
,
writeRecordNum
,
myIndex
);
int32_t
ret
=
syncPropose
(
rid
,
pRpcMsg
,
false
);
if
(
ret
==
TAOS_SYNC_PROPOSE_NOT_LEADER
)
{
sTrace
(
"%s value%d write not leader"
,
s
,
alreadySend
);
}
else
{
assert
(
ret
==
0
);
sTrace
(
"%s value%d write ok"
,
s
,
alreadySend
);
}
alreadySend
++
;
rpcFreeCont
(
pRpcMsg
->
pCont
);
taosMemoryFree
(
pRpcMsg
);
}
else
{
sTrace
(
"%s"
,
s
);
}
taosMsleep
(
1000
);
taosMemoryFree
(
s
);
taosMsleep
(
1000
);
}
syncNodeRelease
(
pSyncNode
);
syncStop
(
rid
);
walClose
(
pWal
);
syncIOStop
();
cleanup
();
return
0
;
}
source/libs/sync/test/syncConfigChangeTest.cpp
浏览文件 @
a047d4a5
...
...
@@ -93,7 +93,6 @@ SSyncFSM* createFsm() {
pFsm
->
FpGetSnapshot
=
GetSnapshotCb
;
pFsm
->
FpRestoreFinishCb
=
RestoreFinishCb
;
pFsm
->
FpReConfigCb
=
ReConfigCb
;
return
pFsm
;
...
...
source/libs/sync/test/syncRaftCfgTest.cpp
浏览文件 @
a047d4a5
...
...
@@ -71,7 +71,10 @@ void test3() {
if
(
taosCheckExistFile
(
s
))
{
printf
(
"%s file: %s already exist!
\n
"
,
(
char
*
)
__FUNCTION__
,
s
);
}
else
{
raftCfgCreateFile
(
pCfg
,
7
,
s
);
SRaftCfgMeta
meta
;
meta
.
isStandBy
=
7
;
meta
.
snapshotEnable
=
9
;
raftCfgCreateFile
(
pCfg
,
meta
,
s
);
printf
(
"%s create json file: %s
\n
"
,
(
char
*
)
__FUNCTION__
,
s
);
}
...
...
@@ -94,6 +97,7 @@ void test5() {
pCfg
->
cfg
.
myIndex
=
taosGetTimestampSec
();
pCfg
->
isStandBy
+=
2
;
pCfg
->
snapshotEnable
+=
3
;
raftCfgPersist
(
pCfg
);
printf
(
"%s update json file: %s myIndex->%d
\n
"
,
(
char
*
)
__FUNCTION__
,
"./test3_raft_cfg.json"
,
pCfg
->
cfg
.
myIndex
);
...
...
source/libs/sync/test/syncRaftLogTest.cpp
0 → 100644
浏览文件 @
a047d4a5
#include "syncRaftLog.h"
//#include <gtest/gtest.h>
#include <stdio.h>
#include "syncEnv.h"
#include "syncIO.h"
#include "syncInt.h"
#include "syncRaftStore.h"
#include "syncUtil.h"
#include "wal.h"
void
logTest
()
{
sTrace
(
"--- sync log test: trace"
);
sDebug
(
"--- sync log test: debug"
);
sInfo
(
"--- sync log test: info"
);
sWarn
(
"--- sync log test: warn"
);
sError
(
"--- sync log test: error"
);
sFatal
(
"--- sync log test: fatal"
);
}
const
char
*
gWalPath
=
"./syncLogStoreTest_wal"
;
void
init
()
{
walInit
();
}
void
test1
()
{
taosRemoveDir
(
gWalPath
);
SWalCfg
walCfg
;
memset
(
&
walCfg
,
0
,
sizeof
(
SWalCfg
));
walCfg
.
vgId
=
1000
;
walCfg
.
fsyncPeriod
=
1000
;
walCfg
.
retentionPeriod
=
1000
;
walCfg
.
rollPeriod
=
1000
;
walCfg
.
retentionSize
=
1000
;
walCfg
.
segSize
=
1000
;
walCfg
.
level
=
TAOS_WAL_FSYNC
;
SWal
*
pWal
=
walOpen
(
gWalPath
,
&
walCfg
);
assert
(
pWal
!=
NULL
);
int64_t
firstVer
=
walGetFirstVer
(
pWal
);
int64_t
lastVer
=
walGetLastVer
(
pWal
);
printf
(
"firstVer:%ld lastVer:%ld
\n
"
,
firstVer
,
lastVer
);
walClose
(
pWal
);
}
void
test2
()
{
taosRemoveDir
(
gWalPath
);
SWalCfg
walCfg
;
memset
(
&
walCfg
,
0
,
sizeof
(
SWalCfg
));
walCfg
.
vgId
=
1000
;
walCfg
.
fsyncPeriod
=
1000
;
walCfg
.
retentionPeriod
=
1000
;
walCfg
.
rollPeriod
=
1000
;
walCfg
.
retentionSize
=
1000
;
walCfg
.
segSize
=
1000
;
walCfg
.
level
=
TAOS_WAL_FSYNC
;
SWal
*
pWal
=
walOpen
(
gWalPath
,
&
walCfg
);
assert
(
pWal
!=
NULL
);
for
(
int
i
=
0
;
i
<
5
;
++
i
)
{
int
code
=
walWrite
(
pWal
,
i
,
100
,
"aa"
,
3
);
if
(
code
!=
0
)
{
printf
(
"code:%d terror:%d msg:%s i:%d
\n
"
,
code
,
terrno
,
tstrerror
(
terrno
),
i
);
assert
(
0
);
}
}
int64_t
firstVer
=
walGetFirstVer
(
pWal
);
int64_t
lastVer
=
walGetLastVer
(
pWal
);
printf
(
"firstVer:%ld lastVer:%ld
\n
"
,
firstVer
,
lastVer
);
walClose
(
pWal
);
}
void
test3
()
{
taosRemoveDir
(
gWalPath
);
SWalCfg
walCfg
;
memset
(
&
walCfg
,
0
,
sizeof
(
SWalCfg
));
walCfg
.
vgId
=
1000
;
walCfg
.
fsyncPeriod
=
1000
;
walCfg
.
retentionPeriod
=
1000
;
walCfg
.
rollPeriod
=
1000
;
walCfg
.
retentionSize
=
1000
;
walCfg
.
segSize
=
1000
;
walCfg
.
level
=
TAOS_WAL_FSYNC
;
SWal
*
pWal
=
walOpen
(
gWalPath
,
&
walCfg
);
assert
(
pWal
!=
NULL
);
walRestoreFromSnapshot
(
pWal
,
5
);
int64_t
firstVer
=
walGetFirstVer
(
pWal
);
int64_t
lastVer
=
walGetLastVer
(
pWal
);
printf
(
"firstVer:%ld lastVer:%ld
\n
"
,
firstVer
,
lastVer
);
walClose
(
pWal
);
}
void
test4
()
{
taosRemoveDir
(
gWalPath
);
SWalCfg
walCfg
;
memset
(
&
walCfg
,
0
,
sizeof
(
SWalCfg
));
walCfg
.
vgId
=
1000
;
walCfg
.
fsyncPeriod
=
1000
;
walCfg
.
retentionPeriod
=
1000
;
walCfg
.
rollPeriod
=
1000
;
walCfg
.
retentionSize
=
1000
;
walCfg
.
segSize
=
1000
;
walCfg
.
level
=
TAOS_WAL_FSYNC
;
SWal
*
pWal
=
walOpen
(
gWalPath
,
&
walCfg
);
assert
(
pWal
!=
NULL
);
walRestoreFromSnapshot
(
pWal
,
5
);
for
(
int
i
=
6
;
i
<
10
;
++
i
)
{
int
code
=
walWrite
(
pWal
,
i
,
100
,
"aa"
,
3
);
if
(
code
!=
0
)
{
printf
(
"code:%d terror:%d msg:%s i:%d
\n
"
,
code
,
terrno
,
tstrerror
(
terrno
),
i
);
assert
(
0
);
}
}
int64_t
firstVer
=
walGetFirstVer
(
pWal
);
int64_t
lastVer
=
walGetLastVer
(
pWal
);
printf
(
"firstVer:%ld lastVer:%ld
\n
"
,
firstVer
,
lastVer
);
walClose
(
pWal
);
}
void
test5
()
{
taosRemoveDir
(
gWalPath
);
SWalCfg
walCfg
;
memset
(
&
walCfg
,
0
,
sizeof
(
SWalCfg
));
walCfg
.
vgId
=
1000
;
walCfg
.
fsyncPeriod
=
1000
;
walCfg
.
retentionPeriod
=
1000
;
walCfg
.
rollPeriod
=
1000
;
walCfg
.
retentionSize
=
1000
;
walCfg
.
segSize
=
1000
;
walCfg
.
level
=
TAOS_WAL_FSYNC
;
SWal
*
pWal
=
walOpen
(
gWalPath
,
&
walCfg
);
assert
(
pWal
!=
NULL
);
walRestoreFromSnapshot
(
pWal
,
5
);
walRestoreFromSnapshot
(
pWal
,
7
);
int64_t
firstVer
=
walGetFirstVer
(
pWal
);
int64_t
lastVer
=
walGetLastVer
(
pWal
);
printf
(
"firstVer:%ld lastVer:%ld
\n
"
,
firstVer
,
lastVer
);
walClose
(
pWal
);
}
void
cleanup
()
{
walCleanUp
();
}
int
main
(
int
argc
,
char
**
argv
)
{
tsAsyncLog
=
0
;
sDebugFlag
=
DEBUG_TRACE
+
DEBUG_SCREEN
+
DEBUG_FILE
;
init
();
test1
();
test2
();
test3
();
test4
();
test5
();
cleanup
();
return
0
;
}
source/libs/sync/test/syncRaftLogTest2.cpp
0 → 100644
浏览文件 @
a047d4a5
#include <gtest/gtest.h>
#include <stdio.h>
#include "syncEnv.h"
#include "syncIO.h"
#include "syncInt.h"
#include "syncRaftLog.h"
#include "syncRaftStore.h"
#include "syncUtil.h"
#include "wal.h"
void
logTest
()
{
sTrace
(
"--- sync log test: trace"
);
sDebug
(
"--- sync log test: debug"
);
sInfo
(
"--- sync log test: info"
);
sWarn
(
"--- sync log test: warn"
);
sError
(
"--- sync log test: error"
);
sFatal
(
"--- sync log test: fatal"
);
}
SSyncNode
*
pSyncNode
;
SWal
*
pWal
;
SSyncLogStore
*
pLogStore
;
const
char
*
pWalPath
=
"./syncLogStoreTest_wal"
;
SyncIndex
gSnapshotLastApplyIndex
;
SyncIndex
gSnapshotLastApplyTerm
;
int32_t
GetSnapshotCb
(
struct
SSyncFSM
*
pFsm
,
SSnapshot
*
pSnapshot
)
{
pSnapshot
->
data
=
NULL
;
pSnapshot
->
lastApplyIndex
=
gSnapshotLastApplyIndex
;
pSnapshot
->
lastApplyTerm
=
gSnapshotLastApplyTerm
;
return
0
;
}
void
init
()
{
walInit
();
SWalCfg
walCfg
;
memset
(
&
walCfg
,
0
,
sizeof
(
SWalCfg
));
walCfg
.
vgId
=
1000
;
walCfg
.
fsyncPeriod
=
1000
;
walCfg
.
retentionPeriod
=
1000
;
walCfg
.
rollPeriod
=
1000
;
walCfg
.
retentionSize
=
1000
;
walCfg
.
segSize
=
1000
;
walCfg
.
level
=
TAOS_WAL_FSYNC
;
pWal
=
walOpen
(
pWalPath
,
&
walCfg
);
assert
(
pWal
!=
NULL
);
pSyncNode
=
(
SSyncNode
*
)
taosMemoryMalloc
(
sizeof
(
SSyncNode
));
memset
(
pSyncNode
,
0
,
sizeof
(
SSyncNode
));
pSyncNode
->
pWal
=
pWal
;
pSyncNode
->
pFsm
=
(
SSyncFSM
*
)
taosMemoryMalloc
(
sizeof
(
SSyncFSM
));
pSyncNode
->
pFsm
->
FpGetSnapshot
=
GetSnapshotCb
;
}
void
cleanup
()
{
walClose
(
pWal
);
walCleanUp
();
taosMemoryFree
(
pSyncNode
);
}
void
test1
()
{
taosRemoveDir
(
pWalPath
);
init
();
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test1 ----- "
,
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
// restart
init
();
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test1 restart ----- "
,
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
}
void
test2
()
{
taosRemoveDir
(
pWalPath
);
init
();
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
pLogStore
->
syncLogSetBeginIndex
(
pLogStore
,
5
);
logStoreLog2
((
char
*
)
"
\n\n\n
test2 ----- "
,
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
// restart
init
();
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test2 restart ----- "
,
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
}
void
test3
()
{
taosRemoveDir
(
pWalPath
);
init
();
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test3 ----- "
,
pLogStore
);
for
(
int
i
=
0
;
i
<=
4
;
++
i
)
{
int32_t
dataLen
=
10
;
SSyncRaftEntry
*
pEntry
=
syncEntryBuild
(
dataLen
);
assert
(
pEntry
!=
NULL
);
pEntry
->
msgType
=
1
;
pEntry
->
originalRpcType
=
2
;
pEntry
->
seqNum
=
3
;
pEntry
->
isWeak
=
true
;
pEntry
->
term
=
100
+
i
;
pEntry
->
index
=
pLogStore
->
syncLogWriteIndex
(
pLogStore
);
snprintf
(
pEntry
->
data
,
dataLen
,
"value%d"
,
i
);
pLogStore
->
syncLogAppendEntry
(
pLogStore
,
pEntry
);
syncEntryDestory
(
pEntry
);
}
logStoreLog2
((
char
*
)
"test3 after appendEntry"
,
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
// restart
init
();
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test3 restart ----- "
,
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
}
void
test4
()
{
taosRemoveDir
(
pWalPath
);
init
();
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test4 ----- "
,
pLogStore
);
pLogStore
->
syncLogSetBeginIndex
(
pLogStore
,
5
);
for
(
int
i
=
5
;
i
<=
9
;
++
i
)
{
int32_t
dataLen
=
10
;
SSyncRaftEntry
*
pEntry
=
syncEntryBuild
(
dataLen
);
assert
(
pEntry
!=
NULL
);
pEntry
->
msgType
=
1
;
pEntry
->
originalRpcType
=
2
;
pEntry
->
seqNum
=
3
;
pEntry
->
isWeak
=
true
;
pEntry
->
term
=
100
+
i
;
pEntry
->
index
=
pLogStore
->
syncLogWriteIndex
(
pLogStore
);
snprintf
(
pEntry
->
data
,
dataLen
,
"value%d"
,
i
);
pLogStore
->
syncLogAppendEntry
(
pLogStore
,
pEntry
);
syncEntryDestory
(
pEntry
);
}
logStoreLog2
((
char
*
)
"test4 after appendEntry"
,
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
// restart
init
();
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test4 restart ----- "
,
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
}
void
test5
()
{
taosRemoveDir
(
pWalPath
);
init
();
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test5 ----- "
,
pLogStore
);
pLogStore
->
syncLogSetBeginIndex
(
pLogStore
,
5
);
for
(
int
i
=
5
;
i
<=
9
;
++
i
)
{
int32_t
dataLen
=
10
;
SSyncRaftEntry
*
pEntry
=
syncEntryBuild
(
dataLen
);
assert
(
pEntry
!=
NULL
);
pEntry
->
msgType
=
1
;
pEntry
->
originalRpcType
=
2
;
pEntry
->
seqNum
=
3
;
pEntry
->
isWeak
=
true
;
pEntry
->
term
=
100
+
i
;
pEntry
->
index
=
pLogStore
->
syncLogWriteIndex
(
pLogStore
);
snprintf
(
pEntry
->
data
,
dataLen
,
"value%d"
,
i
);
pLogStore
->
syncLogAppendEntry
(
pLogStore
,
pEntry
);
syncEntryDestory
(
pEntry
);
}
logStoreLog2
((
char
*
)
"test5 after appendEntry"
,
pLogStore
);
pLogStore
->
syncLogTruncate
(
pLogStore
,
7
);
logStoreLog2
((
char
*
)
"after truncate 7"
,
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
// restart
init
();
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test5 restart ----- "
,
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
}
void
test6
()
{
taosRemoveDir
(
pWalPath
);
init
();
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test6 ----- "
,
pLogStore
);
pLogStore
->
syncLogSetBeginIndex
(
pLogStore
,
5
);
for
(
int
i
=
5
;
i
<=
9
;
++
i
)
{
int32_t
dataLen
=
10
;
SSyncRaftEntry
*
pEntry
=
syncEntryBuild
(
dataLen
);
assert
(
pEntry
!=
NULL
);
pEntry
->
msgType
=
1
;
pEntry
->
originalRpcType
=
2
;
pEntry
->
seqNum
=
3
;
pEntry
->
isWeak
=
true
;
pEntry
->
term
=
100
+
i
;
pEntry
->
index
=
pLogStore
->
syncLogWriteIndex
(
pLogStore
);
snprintf
(
pEntry
->
data
,
dataLen
,
"value%d"
,
i
);
pLogStore
->
syncLogAppendEntry
(
pLogStore
,
pEntry
);
syncEntryDestory
(
pEntry
);
}
logStoreLog2
((
char
*
)
"test6 after appendEntry"
,
pLogStore
);
pLogStore
->
syncLogTruncate
(
pLogStore
,
5
);
logStoreLog2
((
char
*
)
"after truncate 5"
,
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
// restart
init
();
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
logStoreLog2
((
char
*
)
"
\n\n\n
test6 restart ----- "
,
pLogStore
);
logStoreDestory
(
pLogStore
);
cleanup
();
}
int
main
(
int
argc
,
char
**
argv
)
{
tsAsyncLog
=
0
;
sDebugFlag
=
DEBUG_TRACE
+
DEBUG_INFO
+
DEBUG_SCREEN
+
DEBUG_FILE
;
test1
();
test2
();
test3
();
test4
();
test5
();
test6
();
return
0
;
}
source/libs/sync/test/syncRaftLogTest3.cpp
0 → 100644
浏览文件 @
a047d4a5
#include <gtest/gtest.h>
#include <stdio.h>
#include "syncEnv.h"
#include "syncIO.h"
#include "syncInt.h"
#include "syncRaftLog.h"
#include "syncRaftStore.h"
#include "syncUtil.h"
#include "wal.h"
void
logTest
()
{
sTrace
(
"--- sync log test: trace"
);
sDebug
(
"--- sync log test: debug"
);
sInfo
(
"--- sync log test: info"
);
sWarn
(
"--- sync log test: warn"
);
sError
(
"--- sync log test: error"
);
sFatal
(
"--- sync log test: fatal"
);
}
SSyncNode
*
pSyncNode
;
SWal
*
pWal
;
SSyncLogStore
*
pLogStore
;
const
char
*
pWalPath
=
"./syncLogStoreTest_wal"
;
SyncIndex
gSnapshotLastApplyIndex
;
SyncIndex
gSnapshotLastApplyTerm
;
int32_t
GetSnapshotCb
(
struct
SSyncFSM
*
pFsm
,
SSnapshot
*
pSnapshot
)
{
pSnapshot
->
data
=
NULL
;
pSnapshot
->
lastApplyIndex
=
gSnapshotLastApplyIndex
;
pSnapshot
->
lastApplyTerm
=
gSnapshotLastApplyTerm
;
return
0
;
}
void
init
()
{
walInit
();
SWalCfg
walCfg
;
memset
(
&
walCfg
,
0
,
sizeof
(
SWalCfg
));
walCfg
.
vgId
=
1000
;
walCfg
.
fsyncPeriod
=
1000
;
walCfg
.
retentionPeriod
=
1000
;
walCfg
.
rollPeriod
=
1000
;
walCfg
.
retentionSize
=
1000
;
walCfg
.
segSize
=
1000
;
walCfg
.
level
=
TAOS_WAL_FSYNC
;
pWal
=
walOpen
(
pWalPath
,
&
walCfg
);
assert
(
pWal
!=
NULL
);
pSyncNode
=
(
SSyncNode
*
)
taosMemoryMalloc
(
sizeof
(
SSyncNode
));
memset
(
pSyncNode
,
0
,
sizeof
(
SSyncNode
));
pSyncNode
->
pWal
=
pWal
;
pSyncNode
->
pFsm
=
(
SSyncFSM
*
)
taosMemoryMalloc
(
sizeof
(
SSyncFSM
));
pSyncNode
->
pFsm
->
FpGetSnapshot
=
GetSnapshotCb
;
}
void
cleanup
()
{
walClose
(
pWal
);
walCleanUp
();
taosMemoryFree
(
pSyncNode
);
}
void
test1
()
{
// no snapshot
// no log
taosRemoveDir
(
pWalPath
);
init
();
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
pSyncNode
->
pLogStore
=
pLogStore
;
logStoreLog2
((
char
*
)
"
\n\n\n
test1 ----- "
,
pLogStore
);
gSnapshotLastApplyIndex
=
-
1
;
gSnapshotLastApplyTerm
=
0
;
bool
hasSnapshot
=
syncNodeHasSnapshot
(
pSyncNode
);
SSnapshot
snapshot
;
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSyncNode
->
pFsm
,
&
snapshot
);
SyncIndex
lastIndex
=
syncNodeGetLastIndex
(
pSyncNode
);
SyncTerm
lastTerm
=
syncNodeGetLastTerm
(
pSyncNode
);
SyncIndex
testIndex
=
0
;
SyncIndex
preIndex
=
syncNodeGetPreIndex
(
pSyncNode
,
testIndex
);
SyncTerm
preTerm
=
syncNodeGetPreTerm
(
pSyncNode
,
testIndex
);
SyncIndex
syncStartIndex
=
syncNodeSyncStartIndex
(
pSyncNode
);
sTrace
(
"test1"
);
sTrace
(
"hasSnapshot:%d, lastApplyIndex:%ld, lastApplyTerm:%lu"
,
hasSnapshot
,
snapshot
.
lastApplyIndex
,
snapshot
.
lastApplyTerm
);
sTrace
(
"lastIndex: %ld"
,
lastIndex
);
sTrace
(
"lastTerm: %lu"
,
lastTerm
);
sTrace
(
"syncStartIndex: %ld"
,
syncStartIndex
);
sTrace
(
"%ld's preIndex: %ld"
,
testIndex
,
preIndex
);
sTrace
(
"%ld's preTerm: %lu"
,
testIndex
,
preTerm
);
logStoreDestory
(
pLogStore
);
cleanup
();
}
void
test2
()
{
// no snapshot
// whole log
taosRemoveDir
(
pWalPath
);
init
();
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
pSyncNode
->
pLogStore
=
pLogStore
;
logStoreLog2
((
char
*
)
"
\n\n\n
test2 ----- "
,
pLogStore
);
for
(
int
i
=
0
;
i
<=
10
;
++
i
)
{
int32_t
dataLen
=
10
;
SSyncRaftEntry
*
pEntry
=
syncEntryBuild
(
dataLen
);
assert
(
pEntry
!=
NULL
);
pEntry
->
msgType
=
1
;
pEntry
->
originalRpcType
=
2
;
pEntry
->
seqNum
=
3
;
pEntry
->
isWeak
=
true
;
pEntry
->
term
=
100
+
i
;
pEntry
->
index
=
pLogStore
->
syncLogWriteIndex
(
pLogStore
);
snprintf
(
pEntry
->
data
,
dataLen
,
"value%d"
,
i
);
pLogStore
->
syncLogAppendEntry
(
pLogStore
,
pEntry
);
syncEntryDestory
(
pEntry
);
}
logStoreLog2
((
char
*
)
"test2 after appendEntry"
,
pLogStore
);
gSnapshotLastApplyIndex
=
-
1
;
gSnapshotLastApplyTerm
=
0
;
bool
hasSnapshot
=
syncNodeHasSnapshot
(
pSyncNode
);
SSnapshot
snapshot
;
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSyncNode
->
pFsm
,
&
snapshot
);
SyncIndex
lastIndex
=
syncNodeGetLastIndex
(
pSyncNode
);
SyncTerm
lastTerm
=
syncNodeGetLastTerm
(
pSyncNode
);
SyncIndex
syncStartIndex
=
syncNodeSyncStartIndex
(
pSyncNode
);
sTrace
(
"test2"
);
sTrace
(
"hasSnapshot:%d, lastApplyIndex:%ld, lastApplyTerm:%lu"
,
hasSnapshot
,
snapshot
.
lastApplyIndex
,
snapshot
.
lastApplyTerm
);
sTrace
(
"lastIndex: %ld"
,
lastIndex
);
sTrace
(
"lastTerm: %lu"
,
lastTerm
);
sTrace
(
"syncStartIndex: %ld"
,
syncStartIndex
);
for
(
SyncIndex
i
=
11
;
i
>=
0
;
--
i
)
{
SyncIndex
preIndex
=
syncNodeGetPreIndex
(
pSyncNode
,
i
);
SyncTerm
preTerm
=
syncNodeGetPreTerm
(
pSyncNode
,
i
);
sTrace
(
"%ld's preIndex: %ld"
,
i
,
preIndex
);
sTrace
(
"%ld's preTerm: %lu"
,
i
,
preTerm
);
}
logStoreDestory
(
pLogStore
);
cleanup
();
}
void
test3
()
{
// has snapshot
// no log
taosRemoveDir
(
pWalPath
);
init
();
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
pSyncNode
->
pLogStore
=
pLogStore
;
logStoreLog2
((
char
*
)
"
\n\n\n
test3 ----- "
,
pLogStore
);
gSnapshotLastApplyIndex
=
5
;
gSnapshotLastApplyTerm
=
100
;
bool
hasSnapshot
=
syncNodeHasSnapshot
(
pSyncNode
);
SSnapshot
snapshot
;
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSyncNode
->
pFsm
,
&
snapshot
);
SyncIndex
lastIndex
=
syncNodeGetLastIndex
(
pSyncNode
);
SyncTerm
lastTerm
=
syncNodeGetLastTerm
(
pSyncNode
);
SyncIndex
preIndex
=
syncNodeGetPreIndex
(
pSyncNode
,
6
);
SyncTerm
preTerm
=
syncNodeGetPreTerm
(
pSyncNode
,
6
);
SyncIndex
syncStartIndex
=
syncNodeSyncStartIndex
(
pSyncNode
);
sTrace
(
"test3"
);
sTrace
(
"hasSnapshot:%d, lastApplyIndex:%ld, lastApplyTerm:%lu"
,
hasSnapshot
,
snapshot
.
lastApplyIndex
,
snapshot
.
lastApplyTerm
);
sTrace
(
"lastIndex: %ld"
,
lastIndex
);
sTrace
(
"lastTerm: %lu"
,
lastTerm
);
sTrace
(
"syncStartIndex: %ld"
,
syncStartIndex
);
sTrace
(
"%d's preIndex: %ld"
,
6
,
preIndex
);
sTrace
(
"%d's preTerm: %lu"
,
6
,
preTerm
);
logStoreDestory
(
pLogStore
);
cleanup
();
}
void
test4
()
{
// has snapshot
// whole log
taosRemoveDir
(
pWalPath
);
init
();
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
pSyncNode
->
pLogStore
=
pLogStore
;
logStoreLog2
((
char
*
)
"
\n\n\n
test4 ----- "
,
pLogStore
);
for
(
int
i
=
0
;
i
<=
10
;
++
i
)
{
int32_t
dataLen
=
10
;
SSyncRaftEntry
*
pEntry
=
syncEntryBuild
(
dataLen
);
assert
(
pEntry
!=
NULL
);
pEntry
->
msgType
=
1
;
pEntry
->
originalRpcType
=
2
;
pEntry
->
seqNum
=
3
;
pEntry
->
isWeak
=
true
;
pEntry
->
term
=
100
+
i
;
pEntry
->
index
=
pLogStore
->
syncLogWriteIndex
(
pLogStore
);
snprintf
(
pEntry
->
data
,
dataLen
,
"value%d"
,
i
);
pLogStore
->
syncLogAppendEntry
(
pLogStore
,
pEntry
);
syncEntryDestory
(
pEntry
);
}
logStoreLog2
((
char
*
)
"test4 after appendEntry"
,
pLogStore
);
gSnapshotLastApplyIndex
=
5
;
gSnapshotLastApplyTerm
=
100
;
bool
hasSnapshot
=
syncNodeHasSnapshot
(
pSyncNode
);
SSnapshot
snapshot
;
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSyncNode
->
pFsm
,
&
snapshot
);
SyncIndex
lastIndex
=
syncNodeGetLastIndex
(
pSyncNode
);
SyncTerm
lastTerm
=
syncNodeGetLastTerm
(
pSyncNode
);
SyncIndex
syncStartIndex
=
syncNodeSyncStartIndex
(
pSyncNode
);
sTrace
(
"test4"
);
sTrace
(
"hasSnapshot:%d, lastApplyIndex:%ld, lastApplyTerm:%lu"
,
hasSnapshot
,
snapshot
.
lastApplyIndex
,
snapshot
.
lastApplyTerm
);
sTrace
(
"lastIndex: %ld"
,
lastIndex
);
sTrace
(
"lastTerm: %lu"
,
lastTerm
);
sTrace
(
"syncStartIndex: %ld"
,
syncStartIndex
);
for
(
SyncIndex
i
=
11
;
i
>=
6
;
--
i
)
{
SyncIndex
preIndex
=
syncNodeGetPreIndex
(
pSyncNode
,
i
);
SyncTerm
preTerm
=
syncNodeGetPreTerm
(
pSyncNode
,
i
);
sTrace
(
"%ld's preIndex: %ld"
,
i
,
preIndex
);
sTrace
(
"%ld's preTerm: %lu"
,
i
,
preTerm
);
}
logStoreDestory
(
pLogStore
);
cleanup
();
}
void
test5
()
{
// has snapshot
// partial log
taosRemoveDir
(
pWalPath
);
init
();
pLogStore
=
logStoreCreate
(
pSyncNode
);
assert
(
pLogStore
);
pSyncNode
->
pLogStore
=
pLogStore
;
logStoreLog2
((
char
*
)
"
\n\n\n
test5 ----- "
,
pLogStore
);
pSyncNode
->
pLogStore
->
syncLogSetBeginIndex
(
pSyncNode
->
pLogStore
,
6
);
for
(
int
i
=
6
;
i
<=
10
;
++
i
)
{
int32_t
dataLen
=
10
;
SSyncRaftEntry
*
pEntry
=
syncEntryBuild
(
dataLen
);
assert
(
pEntry
!=
NULL
);
pEntry
->
msgType
=
1
;
pEntry
->
originalRpcType
=
2
;
pEntry
->
seqNum
=
3
;
pEntry
->
isWeak
=
true
;
pEntry
->
term
=
100
+
i
;
pEntry
->
index
=
pLogStore
->
syncLogWriteIndex
(
pLogStore
);
snprintf
(
pEntry
->
data
,
dataLen
,
"value%d"
,
i
);
pLogStore
->
syncLogAppendEntry
(
pLogStore
,
pEntry
);
syncEntryDestory
(
pEntry
);
}
logStoreLog2
((
char
*
)
"test5 after appendEntry"
,
pLogStore
);
gSnapshotLastApplyIndex
=
5
;
gSnapshotLastApplyTerm
=
100
;
bool
hasSnapshot
=
syncNodeHasSnapshot
(
pSyncNode
);
SSnapshot
snapshot
;
pSyncNode
->
pFsm
->
FpGetSnapshot
(
pSyncNode
->
pFsm
,
&
snapshot
);
SyncIndex
lastIndex
=
syncNodeGetLastIndex
(
pSyncNode
);
SyncTerm
lastTerm
=
syncNodeGetLastTerm
(
pSyncNode
);
SyncIndex
syncStartIndex
=
syncNodeSyncStartIndex
(
pSyncNode
);
sTrace
(
"test5"
);
sTrace
(
"hasSnapshot:%d, lastApplyIndex:%ld, lastApplyTerm:%lu"
,
hasSnapshot
,
snapshot
.
lastApplyIndex
,
snapshot
.
lastApplyTerm
);
sTrace
(
"lastIndex: %ld"
,
lastIndex
);
sTrace
(
"lastTerm: %lu"
,
lastTerm
);
sTrace
(
"syncStartIndex: %ld"
,
syncStartIndex
);
for
(
SyncIndex
i
=
11
;
i
>=
6
;
--
i
)
{
SyncIndex
preIndex
=
syncNodeGetPreIndex
(
pSyncNode
,
i
);
SyncTerm
preTerm
=
syncNodeGetPreTerm
(
pSyncNode
,
i
);
sTrace
(
"%ld's preIndex: %ld"
,
i
,
preIndex
);
sTrace
(
"%ld's preTerm: %lu"
,
i
,
preTerm
);
}
logStoreDestory
(
pLogStore
);
cleanup
();
}
int
main
(
int
argc
,
char
**
argv
)
{
tsAsyncLog
=
0
;
sDebugFlag
=
DEBUG_TRACE
+
DEBUG_INFO
+
DEBUG_SCREEN
+
DEBUG_FILE
;
test1
();
test2
();
test3
();
test4
();
test5
();
return
0
;
}
source/libs/sync/test/syncSnapshotReceiverTest.cpp
0 → 100644
浏览文件 @
a047d4a5
#include <gtest/gtest.h>
#include <stdio.h>
#include "syncIO.h"
#include "syncInt.h"
#include "syncMessage.h"
#include "syncRaftStore.h"
#include "syncSnapshot.h"
#include "syncUtil.h"
void
logTest
()
{
sTrace
(
"--- sync log test: trace"
);
sDebug
(
"--- sync log test: debug"
);
sInfo
(
"--- sync log test: info"
);
sWarn
(
"--- sync log test: warn"
);
sError
(
"--- sync log test: error"
);
sFatal
(
"--- sync log test: fatal"
);
}
void
CommitCb
(
struct
SSyncFSM
*
pFsm
,
const
SRpcMsg
*
pMsg
,
SFsmCbMeta
cbMeta
)
{}
void
PreCommitCb
(
struct
SSyncFSM
*
pFsm
,
const
SRpcMsg
*
pMsg
,
SFsmCbMeta
cbMeta
)
{}
void
RollBackCb
(
struct
SSyncFSM
*
pFsm
,
const
SRpcMsg
*
pMsg
,
SFsmCbMeta
cbMeta
)
{}
void
RestoreFinishCb
(
struct
SSyncFSM
*
pFsm
)
{}
void
ReConfigCb
(
struct
SSyncFSM
*
pFsm
,
SSyncCfg
newCfg
,
SReConfigCbMeta
cbMeta
)
{}
int32_t
GetSnapshot
(
struct
SSyncFSM
*
pFsm
,
SSnapshot
*
pSnapshot
)
{
return
0
;
}
int32_t
SnapshotStartRead
(
struct
SSyncFSM
*
pFsm
,
void
**
ppReader
)
{
return
0
;
}
int32_t
SnapshotStopRead
(
struct
SSyncFSM
*
pFsm
,
void
*
pReader
)
{
return
0
;
}
int32_t
SnapshotDoRead
(
struct
SSyncFSM
*
pFsm
,
void
*
pReader
,
void
**
ppBuf
,
int32_t
*
len
)
{
return
0
;
}
int32_t
SnapshotStartWrite
(
struct
SSyncFSM
*
pFsm
,
void
**
ppWriter
)
{
return
0
;
}
int32_t
SnapshotStopWrite
(
struct
SSyncFSM
*
pFsm
,
void
*
pWriter
,
bool
isApply
)
{
return
0
;
}
int32_t
SnapshotDoWrite
(
struct
SSyncFSM
*
pFsm
,
void
*
pWriter
,
void
*
pBuf
,
int32_t
len
)
{
return
0
;
}
SSyncSnapshotReceiver
*
createReceiver
()
{
SSyncNode
*
pSyncNode
=
(
SSyncNode
*
)
taosMemoryMalloc
(
sizeof
(
*
pSyncNode
));
pSyncNode
->
pRaftStore
=
(
SRaftStore
*
)
taosMemoryMalloc
(
sizeof
(
*
(
pSyncNode
->
pRaftStore
)));
pSyncNode
->
pFsm
=
(
SSyncFSM
*
)
taosMemoryMalloc
(
sizeof
(
*
(
pSyncNode
->
pFsm
)));
pSyncNode
->
pFsm
->
FpSnapshotStartWrite
=
SnapshotStartWrite
;
pSyncNode
->
pFsm
->
FpSnapshotStopWrite
=
SnapshotStopWrite
;
pSyncNode
->
pFsm
->
FpSnapshotDoWrite
=
SnapshotDoWrite
;
SSyncSnapshotReceiver
*
pReceiver
=
snapshotReceiverCreate
(
pSyncNode
,
2
);
pReceiver
->
start
=
true
;
pReceiver
->
ack
=
20
;
pReceiver
->
pWriter
=
(
void
*
)
0x11
;
pReceiver
->
term
=
66
;
return
pReceiver
;
}
int
main
()
{
tsAsyncLog
=
0
;
sDebugFlag
=
DEBUG_TRACE
+
DEBUG_SCREEN
+
DEBUG_FILE
;
logTest
();
SSyncSnapshotReceiver
*
pReceiver
=
createReceiver
();
sTrace
(
"%s"
,
snapshotReceiver2Str
(
pReceiver
));
return
0
;
}
source/libs/sync/test/syncSnapshotRspTest.cpp
0 → 100644
浏览文件 @
a047d4a5
#include <gtest/gtest.h>
#include <stdio.h>
#include "syncIO.h"
#include "syncInt.h"
#include "syncMessage.h"
#include "syncUtil.h"
void
logTest
()
{
sTrace
(
"--- sync log test: trace"
);
sDebug
(
"--- sync log test: debug"
);
sInfo
(
"--- sync log test: info"
);
sWarn
(
"--- sync log test: warn"
);
sError
(
"--- sync log test: error"
);
sFatal
(
"--- sync log test: fatal"
);
}
SyncSnapshotRsp
*
createMsg
()
{
SyncSnapshotRsp
*
pMsg
=
syncSnapshotRspBuild
(
1000
);
pMsg
->
srcId
.
addr
=
syncUtilAddr2U64
(
"127.0.0.1"
,
1234
);
pMsg
->
srcId
.
vgId
=
100
;
pMsg
->
destId
.
addr
=
syncUtilAddr2U64
(
"127.0.0.1"
,
5678
);
pMsg
->
destId
.
vgId
=
100
;
pMsg
->
term
=
11
;
pMsg
->
lastIndex
=
22
;
pMsg
->
lastTerm
=
33
;
pMsg
->
ack
=
44
;
return
pMsg
;
}
void
test1
()
{
SyncSnapshotRsp
*
pMsg
=
createMsg
();
syncSnapshotRspLog2
((
char
*
)
"test1:"
,
pMsg
);
syncSnapshotRspDestroy
(
pMsg
);
}
void
test2
()
{
SyncSnapshotRsp
*
pMsg
=
createMsg
();
uint32_t
len
=
pMsg
->
bytes
;
char
*
serialized
=
(
char
*
)
taosMemoryMalloc
(
len
);
syncSnapshotRspSerialize
(
pMsg
,
serialized
,
len
);
SyncSnapshotRsp
*
pMsg2
=
syncSnapshotRspBuild
(
1000
);
syncSnapshotRspDeserialize
(
serialized
,
len
,
pMsg2
);
syncSnapshotRspLog2
((
char
*
)
"test2: syncSnapshotRspSerialize -> syncSnapshotRspDeserialize "
,
pMsg2
);
taosMemoryFree
(
serialized
);
syncSnapshotRspDestroy
(
pMsg
);
syncSnapshotRspDestroy
(
pMsg2
);
}
void
test3
()
{
SyncSnapshotRsp
*
pMsg
=
createMsg
();
uint32_t
len
;
char
*
serialized
=
syncSnapshotRspSerialize2
(
pMsg
,
&
len
);
SyncSnapshotRsp
*
pMsg2
=
syncSnapshotRspDeserialize2
(
serialized
,
len
);
syncSnapshotRspLog2
((
char
*
)
"test3: syncSnapshotRspSerialize2 -> syncSnapshotRspDeserialize2 "
,
pMsg2
);
taosMemoryFree
(
serialized
);
syncSnapshotRspDestroy
(
pMsg
);
syncSnapshotRspDestroy
(
pMsg2
);
}
void
test4
()
{
SyncSnapshotRsp
*
pMsg
=
createMsg
();
SRpcMsg
rpcMsg
;
syncSnapshotRsp2RpcMsg
(
pMsg
,
&
rpcMsg
);
SyncSnapshotRsp
*
pMsg2
=
(
SyncSnapshotRsp
*
)
taosMemoryMalloc
(
rpcMsg
.
contLen
);
syncSnapshotRspFromRpcMsg
(
&
rpcMsg
,
pMsg2
);
syncSnapshotRspLog2
((
char
*
)
"test4: syncSnapshotRsp2RpcMsg -> syncSnapshotRspFromRpcMsg "
,
pMsg2
);
rpcFreeCont
(
rpcMsg
.
pCont
);
syncSnapshotRspDestroy
(
pMsg
);
syncSnapshotRspDestroy
(
pMsg2
);
}
void
test5
()
{
SyncSnapshotRsp
*
pMsg
=
createMsg
();
SRpcMsg
rpcMsg
;
syncSnapshotRsp2RpcMsg
(
pMsg
,
&
rpcMsg
);
SyncSnapshotRsp
*
pMsg2
=
syncSnapshotRspFromRpcMsg2
(
&
rpcMsg
);
syncSnapshotRspLog2
((
char
*
)
"test5: syncSnapshotRsp2RpcMsg -> syncSnapshotRspFromRpcMsg2 "
,
pMsg2
);
rpcFreeCont
(
rpcMsg
.
pCont
);
syncSnapshotRspDestroy
(
pMsg
);
syncSnapshotRspDestroy
(
pMsg2
);
}
int
main
()
{
tsAsyncLog
=
0
;
sDebugFlag
=
DEBUG_TRACE
+
DEBUG_SCREEN
+
DEBUG_FILE
;
logTest
();
test1
();
test2
();
test3
();
test4
();
test5
();
return
0
;
}
source/libs/sync/test/syncSnapshotSendTest.cpp
0 → 100644
浏览文件 @
a047d4a5
#include <gtest/gtest.h>
#include <stdio.h>
#include "syncIO.h"
#include "syncInt.h"
#include "syncMessage.h"
#include "syncUtil.h"
void
logTest
()
{
sTrace
(
"--- sync log test: trace"
);
sDebug
(
"--- sync log test: debug"
);
sInfo
(
"--- sync log test: info"
);
sWarn
(
"--- sync log test: warn"
);
sError
(
"--- sync log test: error"
);
sFatal
(
"--- sync log test: fatal"
);
}
SyncSnapshotSend
*
createMsg
()
{
SyncSnapshotSend
*
pMsg
=
syncSnapshotSendBuild
(
20
,
1000
);
pMsg
->
srcId
.
addr
=
syncUtilAddr2U64
(
"127.0.0.1"
,
1234
);
pMsg
->
srcId
.
vgId
=
100
;
pMsg
->
destId
.
addr
=
syncUtilAddr2U64
(
"127.0.0.1"
,
5678
);
pMsg
->
destId
.
vgId
=
100
;
pMsg
->
term
=
11
;
pMsg
->
lastIndex
=
22
;
pMsg
->
lastTerm
=
33
;
pMsg
->
seq
=
44
;
strcpy
(
pMsg
->
data
,
"hello world"
);
return
pMsg
;
}
void
test1
()
{
SyncSnapshotSend
*
pMsg
=
createMsg
();
syncSnapshotSendLog2
((
char
*
)
"test1:"
,
pMsg
);
syncSnapshotSendDestroy
(
pMsg
);
}
void
test2
()
{
SyncSnapshotSend
*
pMsg
=
createMsg
();
uint32_t
len
=
pMsg
->
bytes
;
char
*
serialized
=
(
char
*
)
taosMemoryMalloc
(
len
);
syncSnapshotSendSerialize
(
pMsg
,
serialized
,
len
);
SyncSnapshotSend
*
pMsg2
=
syncSnapshotSendBuild
(
pMsg
->
dataLen
,
1000
);
syncSnapshotSendDeserialize
(
serialized
,
len
,
pMsg2
);
syncSnapshotSendLog2
((
char
*
)
"test2: syncSnapshotSendSerialize -> syncSnapshotSendDeserialize "
,
pMsg2
);
taosMemoryFree
(
serialized
);
syncSnapshotSendDestroy
(
pMsg
);
syncSnapshotSendDestroy
(
pMsg2
);
}
void
test3
()
{
SyncSnapshotSend
*
pMsg
=
createMsg
();
uint32_t
len
;
char
*
serialized
=
syncSnapshotSendSerialize2
(
pMsg
,
&
len
);
SyncSnapshotSend
*
pMsg2
=
syncSnapshotSendDeserialize2
(
serialized
,
len
);
syncSnapshotSendLog2
((
char
*
)
"test3: syncSnapshotSendSerialize2 -> syncSnapshotSendDeserialize2 "
,
pMsg2
);
taosMemoryFree
(
serialized
);
syncSnapshotSendDestroy
(
pMsg
);
syncSnapshotSendDestroy
(
pMsg2
);
}
void
test4
()
{
SyncSnapshotSend
*
pMsg
=
createMsg
();
SRpcMsg
rpcMsg
;
syncSnapshotSend2RpcMsg
(
pMsg
,
&
rpcMsg
);
SyncSnapshotSend
*
pMsg2
=
(
SyncSnapshotSend
*
)
taosMemoryMalloc
(
rpcMsg
.
contLen
);
syncSnapshotSendFromRpcMsg
(
&
rpcMsg
,
pMsg2
);
syncSnapshotSendLog2
((
char
*
)
"test4: syncSnapshotSend2RpcMsg -> syncSnapshotSendFromRpcMsg "
,
pMsg2
);
rpcFreeCont
(
rpcMsg
.
pCont
);
syncSnapshotSendDestroy
(
pMsg
);
syncSnapshotSendDestroy
(
pMsg2
);
}
void
test5
()
{
SyncSnapshotSend
*
pMsg
=
createMsg
();
SRpcMsg
rpcMsg
;
syncSnapshotSend2RpcMsg
(
pMsg
,
&
rpcMsg
);
SyncSnapshotSend
*
pMsg2
=
syncSnapshotSendFromRpcMsg2
(
&
rpcMsg
);
syncSnapshotSendLog2
((
char
*
)
"test5: syncSnapshotSend2RpcMsg -> syncSnapshotSendFromRpcMsg2 "
,
pMsg2
);
rpcFreeCont
(
rpcMsg
.
pCont
);
syncSnapshotSendDestroy
(
pMsg
);
syncSnapshotSendDestroy
(
pMsg2
);
}
int
main
()
{
tsAsyncLog
=
0
;
sDebugFlag
=
DEBUG_TRACE
+
DEBUG_SCREEN
+
DEBUG_FILE
;
logTest
();
test1
();
test2
();
test3
();
test4
();
test5
();
return
0
;
}
source/libs/sync/test/syncSnapshotSenderTest.cpp
0 → 100644
浏览文件 @
a047d4a5
#include <gtest/gtest.h>
#include <stdio.h>
#include "syncIO.h"
#include "syncInt.h"
#include "syncMessage.h"
#include "syncRaftStore.h"
#include "syncSnapshot.h"
#include "syncUtil.h"
void
logTest
()
{
sTrace
(
"--- sync log test: trace"
);
sDebug
(
"--- sync log test: debug"
);
sInfo
(
"--- sync log test: info"
);
sWarn
(
"--- sync log test: warn"
);
sError
(
"--- sync log test: error"
);
sFatal
(
"--- sync log test: fatal"
);
}
void
CommitCb
(
struct
SSyncFSM
*
pFsm
,
const
SRpcMsg
*
pMsg
,
SFsmCbMeta
cbMeta
)
{}
void
PreCommitCb
(
struct
SSyncFSM
*
pFsm
,
const
SRpcMsg
*
pMsg
,
SFsmCbMeta
cbMeta
)
{}
void
RollBackCb
(
struct
SSyncFSM
*
pFsm
,
const
SRpcMsg
*
pMsg
,
SFsmCbMeta
cbMeta
)
{}
void
RestoreFinishCb
(
struct
SSyncFSM
*
pFsm
)
{}
void
ReConfigCb
(
struct
SSyncFSM
*
pFsm
,
SSyncCfg
newCfg
,
SReConfigCbMeta
cbMeta
)
{}
int32_t
GetSnapshot
(
struct
SSyncFSM
*
pFsm
,
SSnapshot
*
pSnapshot
)
{
return
0
;
}
int32_t
SnapshotStartRead
(
struct
SSyncFSM
*
pFsm
,
void
**
ppReader
)
{
return
0
;
}
int32_t
SnapshotStopRead
(
struct
SSyncFSM
*
pFsm
,
void
*
pReader
)
{
return
0
;
}
int32_t
SnapshotDoRead
(
struct
SSyncFSM
*
pFsm
,
void
*
pReader
,
void
**
ppBuf
,
int32_t
*
len
)
{
return
0
;
}
int32_t
SnapshotStartWrite
(
struct
SSyncFSM
*
pFsm
,
void
**
ppWriter
)
{
return
0
;
}
int32_t
SnapshotStopWrite
(
struct
SSyncFSM
*
pFsm
,
void
*
pWriter
,
bool
isApply
)
{
return
0
;
}
int32_t
SnapshotDoWrite
(
struct
SSyncFSM
*
pFsm
,
void
*
pWriter
,
void
*
pBuf
,
int32_t
len
)
{
return
0
;
}
SSyncSnapshotSender
*
createSender
()
{
SSyncNode
*
pSyncNode
=
(
SSyncNode
*
)
taosMemoryMalloc
(
sizeof
(
*
pSyncNode
));
pSyncNode
->
pRaftStore
=
(
SRaftStore
*
)
taosMemoryMalloc
(
sizeof
(
*
(
pSyncNode
->
pRaftStore
)));
pSyncNode
->
pFsm
=
(
SSyncFSM
*
)
taosMemoryMalloc
(
sizeof
(
*
(
pSyncNode
->
pFsm
)));
pSyncNode
->
pFsm
->
FpSnapshotStartRead
=
SnapshotStartRead
;
pSyncNode
->
pFsm
->
FpSnapshotStopRead
=
SnapshotStopRead
;
pSyncNode
->
pFsm
->
FpSnapshotDoRead
=
SnapshotDoRead
;
SSyncSnapshotSender
*
pSender
=
snapshotSenderCreate
(
pSyncNode
,
2
);
pSender
->
start
=
true
;
pSender
->
seq
=
10
;
pSender
->
ack
=
20
;
pSender
->
pReader
=
(
void
*
)
0x11
;
pSender
->
blockLen
=
20
;
pSender
->
pCurrentBlock
=
taosMemoryMalloc
(
pSender
->
blockLen
);
snprintf
((
char
*
)(
pSender
->
pCurrentBlock
),
pSender
->
blockLen
,
"%s"
,
"hello"
);
pSender
->
snapshot
.
lastApplyIndex
=
99
;
pSender
->
snapshot
.
lastApplyTerm
=
88
;
pSender
->
sendingMS
=
77
;
pSender
->
term
=
66
;
return
pSender
;
}
int
main
()
{
tsAsyncLog
=
0
;
sDebugFlag
=
DEBUG_TRACE
+
DEBUG_SCREEN
+
DEBUG_FILE
;
logTest
();
SSyncSnapshotSender
*
pSender
=
createSender
();
sTrace
(
"%s"
,
snapshotSender2Str
(
pSender
));
return
0
;
}
source/libs/sync/test/syncTest.cpp
浏览文件 @
a047d4a5
...
...
@@ -50,14 +50,16 @@ void test4() {
}
int
main
(
int
argc
,
char
**
argv
)
{
// taosInitLog("
tmp/syncTest.log", 100);
taosInitLog
(
"/
tmp/syncTest.log"
,
100
);
tsAsyncLog
=
0
;
sDebugFlag
=
DEBUG_SCREEN
+
DEBUG_FILE
+
DEBUG_TRACE
+
DEBUG_INFO
+
DEBUG_ERROR
;
test1
();
test2
();
test3
();
test4
();
/*
if (argc == 2) {
bool bTaosDirExist = taosDirExist(argv[1]);
printf("%s bTaosDirExist:%d \n", argv[1], bTaosDirExist);
...
...
@@ -65,7 +67,8 @@ int main(int argc, char** argv) {
bool bTaosCheckExistFile = taosCheckExistFile(argv[1]);
printf("%s bTaosCheckExistFile:%d \n", argv[1], bTaosCheckExistFile);
}
*/
//
taosCloseLog();
taosCloseLog
();
return
0
;
}
source/libs/sync/test/syncTestTool.cpp
0 → 100644
浏览文件 @
a047d4a5
此差异已折叠。
点击以展开。
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录