Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
a6b94983
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
a6b94983
编写于
4月 21, 2022
作者:
M
Minghao Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
delete syncElectTest 1-3, add syncReplicateTest.cpp
上级
52b6a9c0
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
127 addition
and
562 deletion
+127
-562
source/libs/sync/test/CMakeLists.txt
source/libs/sync/test/CMakeLists.txt
+0
-42
source/libs/sync/test/syncElectTest1.cpp
source/libs/sync/test/syncElectTest1.cpp
+0
-129
source/libs/sync/test/syncElectTest2.cpp
source/libs/sync/test/syncElectTest2.cpp
+0
-139
source/libs/sync/test/syncElectTest3.cpp
source/libs/sync/test/syncElectTest3.cpp
+0
-142
source/libs/sync/test/syncReplicateTest.cpp
source/libs/sync/test/syncReplicateTest.cpp
+127
-110
未找到文件。
source/libs/sync/test/CMakeLists.txt
浏览文件 @
a6b94983
...
...
@@ -28,9 +28,6 @@ add_executable(syncRpcMsgTest "")
add_executable
(
syncPingTimerTest2
""
)
add_executable
(
syncPingSelfTest
""
)
add_executable
(
syncElectTest
""
)
add_executable
(
syncElectTest1
""
)
add_executable
(
syncElectTest2
""
)
add_executable
(
syncElectTest3
""
)
add_executable
(
syncEncodeTest
""
)
add_executable
(
syncWriteTest
""
)
add_executable
(
syncReplicateTest
""
)
...
...
@@ -165,18 +162,6 @@ target_sources(syncElectTest
PRIVATE
"syncElectTest.cpp"
)
target_sources
(
syncElectTest1
PRIVATE
"syncElectTest1.cpp"
)
target_sources
(
syncElectTest2
PRIVATE
"syncElectTest2.cpp"
)
target_sources
(
syncElectTest3
PRIVATE
"syncElectTest3.cpp"
)
target_sources
(
syncEncodeTest
PRIVATE
"syncEncodeTest.cpp"
...
...
@@ -377,21 +362,6 @@ target_include_directories(syncElectTest
"
${
CMAKE_SOURCE_DIR
}
/include/libs/sync"
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../inc"
)
target_include_directories
(
syncElectTest1
PUBLIC
"
${
CMAKE_SOURCE_DIR
}
/include/libs/sync"
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../inc"
)
target_include_directories
(
syncElectTest2
PUBLIC
"
${
CMAKE_SOURCE_DIR
}
/include/libs/sync"
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../inc"
)
target_include_directories
(
syncElectTest3
PUBLIC
"
${
CMAKE_SOURCE_DIR
}
/include/libs/sync"
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../inc"
)
target_include_directories
(
syncEncodeTest
PUBLIC
"
${
CMAKE_SOURCE_DIR
}
/include/libs/sync"
...
...
@@ -574,18 +544,6 @@ target_link_libraries(syncElectTest
sync
gtest_main
)
target_link_libraries
(
syncElectTest1
sync
gtest_main
)
target_link_libraries
(
syncElectTest2
sync
gtest_main
)
target_link_libraries
(
syncElectTest3
sync
gtest_main
)
target_link_libraries
(
syncEncodeTest
sync
gtest_main
...
...
source/libs/sync/test/syncElectTest1.cpp
已删除
100644 → 0
浏览文件 @
52b6a9c0
#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"
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
ports
[]
=
{
7010
,
7110
,
7210
,
7310
,
7410
};
int32_t
replicaNum
=
1
;
int32_t
myIndex
=
0
;
SRaftId
ids
[
TSDB_MAX_REPLICA
];
SSyncInfo
syncInfo
;
SSyncFSM
*
pFsm
;
SWal
*
pWal
;
SSyncNode
*
gSyncNode
;
const
char
*
pDir
=
"./syncElectTest1"
;
SSyncNode
*
syncNodeInit
()
{
syncInfo
.
vgId
=
1234
;
syncInfo
.
rpcClient
=
gSyncIO
->
clientRpc
;
syncInfo
.
FpSendMsg
=
syncIOSendMsg
;
syncInfo
.
queue
=
gSyncIO
->
pMsgQ
;
syncInfo
.
FpEqMsg
=
syncIOEqMsg
;
syncInfo
.
pFsm
=
pFsm
;
snprintf
(
syncInfo
.
path
,
sizeof
(
syncInfo
.
path
),
"%s_sync_%d"
,
pDir
,
myIndex
);
int
code
=
walInit
();
assert
(
code
==
0
);
SWalCfg
walCfg
;
memset
(
&
walCfg
,
0
,
sizeof
(
SWalCfg
));
walCfg
.
vgId
=
syncInfo
.
vgId
;
walCfg
.
fsyncPeriod
=
1000
;
walCfg
.
retentionPeriod
=
1000
;
walCfg
.
rollPeriod
=
1000
;
walCfg
.
retentionSize
=
1000
;
walCfg
.
segSize
=
1000
;
walCfg
.
level
=
TAOS_WAL_FSYNC
;
char
tmpdir
[
128
];
snprintf
(
tmpdir
,
sizeof
(
tmpdir
),
"%s_wal_%d"
,
pDir
,
myIndex
);
pWal
=
walOpen
(
tmpdir
,
&
walCfg
);
assert
(
pWal
!=
NULL
);
syncInfo
.
pWal
=
pWal
;
SSyncCfg
*
pCfg
=
&
syncInfo
.
syncCfg
;
pCfg
->
myIndex
=
myIndex
;
pCfg
->
replicaNum
=
replicaNum
;
for
(
int
i
=
0
;
i
<
replicaNum
;
++
i
)
{
pCfg
->
nodeInfo
[
i
].
nodePort
=
ports
[
i
];
snprintf
(
pCfg
->
nodeInfo
[
i
].
nodeFqdn
,
sizeof
(
pCfg
->
nodeInfo
[
i
].
nodeFqdn
),
"%s"
,
"127.0.0.1"
);
// taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn);
}
SSyncNode
*
pSyncNode
=
syncNodeOpen
(
&
syncInfo
);
assert
(
pSyncNode
!=
NULL
);
gSyncIO
->
FpOnSyncPing
=
pSyncNode
->
FpOnPing
;
gSyncIO
->
FpOnSyncPingReply
=
pSyncNode
->
FpOnPingReply
;
gSyncIO
->
FpOnSyncRequestVote
=
pSyncNode
->
FpOnRequestVote
;
gSyncIO
->
FpOnSyncRequestVoteReply
=
pSyncNode
->
FpOnRequestVoteReply
;
gSyncIO
->
FpOnSyncAppendEntries
=
pSyncNode
->
FpOnAppendEntries
;
gSyncIO
->
FpOnSyncAppendEntriesReply
=
pSyncNode
->
FpOnAppendEntriesReply
;
gSyncIO
->
FpOnSyncPing
=
pSyncNode
->
FpOnPing
;
gSyncIO
->
FpOnSyncPingReply
=
pSyncNode
->
FpOnPingReply
;
gSyncIO
->
FpOnSyncTimeout
=
pSyncNode
->
FpOnTimeout
;
gSyncIO
->
pSyncNode
=
pSyncNode
;
syncNodeStart
(
pSyncNode
);
return
pSyncNode
;
}
SSyncNode
*
syncInitTest
()
{
return
syncNodeInit
();
}
void
initRaftId
(
SSyncNode
*
pSyncNode
)
{
for
(
int
i
=
0
;
i
<
replicaNum
;
++
i
)
{
ids
[
i
]
=
pSyncNode
->
replicasId
[
i
];
char
*
s
=
syncUtilRaftId2Str
(
&
ids
[
i
]);
printf
(
"raftId[%d] : %s
\n
"
,
i
,
s
);
taosMemoryFree
(
s
);
}
}
int
main
(
int
argc
,
char
**
argv
)
{
// taosInitLog((char *)"syncTest.log", 100000, 10);
tsAsyncLog
=
0
;
sDebugFlag
=
143
+
64
;
myIndex
=
0
;
if
(
argc
>=
2
)
{
myIndex
=
atoi
(
argv
[
1
]);
}
int32_t
ret
=
syncIOStart
((
char
*
)
"127.0.0.1"
,
ports
[
myIndex
]);
assert
(
ret
==
0
);
ret
=
syncEnvStart
();
assert
(
ret
==
0
);
gSyncNode
=
syncInitTest
();
assert
(
gSyncNode
!=
NULL
);
syncNodeLog2
((
char
*
)
""
,
gSyncNode
);
initRaftId
(
gSyncNode
);
//---------------------------
while
(
1
)
{
char
*
s
=
syncNode2SimpleStr
(
gSyncNode
);
sTrace
(
"%s"
,
s
);
taosMemoryFree
(
s
);
taosMsleep
(
1000
);
}
return
0
;
}
source/libs/sync/test/syncElectTest2.cpp
已删除
100644 → 0
浏览文件 @
52b6a9c0
#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 "tref.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
ports
[]
=
{
7010
,
7110
,
7210
,
7310
,
7410
};
int32_t
replicaNum
=
3
;
int32_t
myIndex
=
0
;
SRaftId
ids
[
TSDB_MAX_REPLICA
];
SSyncInfo
syncInfo
;
SSyncFSM
*
pFsm
;
SWal
*
pWal
;
const
char
*
pDir
=
"./syncElectTest2"
;
int64_t
syncNodeInit
()
{
syncInfo
.
vgId
=
1234
;
syncInfo
.
rpcClient
=
gSyncIO
->
clientRpc
;
syncInfo
.
FpSendMsg
=
syncIOSendMsg
;
syncInfo
.
queue
=
gSyncIO
->
pMsgQ
;
syncInfo
.
FpEqMsg
=
syncIOEqMsg
;
syncInfo
.
pFsm
=
pFsm
;
snprintf
(
syncInfo
.
path
,
sizeof
(
syncInfo
.
path
),
"%s_sync_%d"
,
pDir
,
myIndex
);
int
code
=
walInit
();
assert
(
code
==
0
);
SWalCfg
walCfg
;
memset
(
&
walCfg
,
0
,
sizeof
(
SWalCfg
));
walCfg
.
vgId
=
syncInfo
.
vgId
;
walCfg
.
fsyncPeriod
=
1000
;
walCfg
.
retentionPeriod
=
1000
;
walCfg
.
rollPeriod
=
1000
;
walCfg
.
retentionSize
=
1000
;
walCfg
.
segSize
=
1000
;
walCfg
.
level
=
TAOS_WAL_FSYNC
;
char
tmpdir
[
128
];
snprintf
(
tmpdir
,
sizeof
(
tmpdir
),
"%s_wal_%d"
,
pDir
,
myIndex
);
pWal
=
walOpen
(
tmpdir
,
&
walCfg
);
assert
(
pWal
!=
NULL
);
syncInfo
.
pWal
=
pWal
;
SSyncCfg
*
pCfg
=
&
syncInfo
.
syncCfg
;
pCfg
->
myIndex
=
myIndex
;
pCfg
->
replicaNum
=
replicaNum
;
for
(
int
i
=
0
;
i
<
replicaNum
;
++
i
)
{
pCfg
->
nodeInfo
[
i
].
nodePort
=
ports
[
i
];
snprintf
(
pCfg
->
nodeInfo
[
i
].
nodeFqdn
,
sizeof
(
pCfg
->
nodeInfo
[
i
].
nodeFqdn
),
"%s"
,
"127.0.0.1"
);
// taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn);
}
int64_t
rid
=
syncOpen
(
&
syncInfo
);
assert
(
rid
>
0
);
syncStart
(
rid
);
SSyncNode
*
pSyncNode
=
(
SSyncNode
*
)
syncNodeAcquire
(
rid
);
assert
(
pSyncNode
!=
NULL
);
gSyncIO
->
FpOnSyncPing
=
pSyncNode
->
FpOnPing
;
gSyncIO
->
FpOnSyncPingReply
=
pSyncNode
->
FpOnPingReply
;
gSyncIO
->
FpOnSyncRequestVote
=
pSyncNode
->
FpOnRequestVote
;
gSyncIO
->
FpOnSyncRequestVoteReply
=
pSyncNode
->
FpOnRequestVoteReply
;
gSyncIO
->
FpOnSyncAppendEntries
=
pSyncNode
->
FpOnAppendEntries
;
gSyncIO
->
FpOnSyncAppendEntriesReply
=
pSyncNode
->
FpOnAppendEntriesReply
;
gSyncIO
->
FpOnSyncPing
=
pSyncNode
->
FpOnPing
;
gSyncIO
->
FpOnSyncPingReply
=
pSyncNode
->
FpOnPingReply
;
gSyncIO
->
FpOnSyncTimeout
=
pSyncNode
->
FpOnTimeout
;
gSyncIO
->
pSyncNode
=
pSyncNode
;
syncNodeStart
(
pSyncNode
);
syncNodeRelease
(
pSyncNode
);
return
rid
;
}
void
initRaftId
(
SSyncNode
*
pSyncNode
)
{
for
(
int
i
=
0
;
i
<
replicaNum
;
++
i
)
{
ids
[
i
]
=
pSyncNode
->
replicasId
[
i
];
char
*
s
=
syncUtilRaftId2Str
(
&
ids
[
i
]);
printf
(
"raftId[%d] : %s
\n
"
,
i
,
s
);
taosMemoryFree
(
s
);
}
}
int
main
(
int
argc
,
char
**
argv
)
{
// taosInitLog((char *)"syncTest.log", 100000, 10);
tsAsyncLog
=
0
;
sDebugFlag
=
143
+
64
;
myIndex
=
0
;
if
(
argc
>=
2
)
{
myIndex
=
atoi
(
argv
[
1
]);
}
int32_t
ret
=
syncIOStart
((
char
*
)
"127.0.0.1"
,
ports
[
myIndex
]);
assert
(
ret
==
0
);
ret
=
syncInit
();
assert
(
ret
==
0
);
int64_t
rid
=
syncNodeInit
();
assert
(
rid
>
0
);
SSyncNode
*
pSyncNode
=
(
SSyncNode
*
)
syncNodeAcquire
(
rid
);
assert
(
pSyncNode
!=
NULL
);
syncNodeLog2
((
char
*
)
""
,
pSyncNode
);
initRaftId
(
pSyncNode
);
//---------------------------
while
(
1
)
{
sTrace
(
"elect sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, electTimerMS:%d"
,
pSyncNode
->
state
,
syncUtilState2String
(
pSyncNode
->
state
),
pSyncNode
->
pRaftStore
->
currentTerm
,
pSyncNode
->
electTimerLogicClock
,
pSyncNode
->
electTimerLogicClockUser
,
pSyncNode
->
electTimerMS
);
taosMsleep
(
1000
);
}
syncNodeRelease
(
pSyncNode
);
return
0
;
}
source/libs/sync/test/syncElectTest3.cpp
已删除
100644 → 0
浏览文件 @
52b6a9c0
#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 "tref.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
ports
[]
=
{
7010
,
7110
,
7210
,
7310
,
7410
};
int32_t
replicaNum
=
3
;
int32_t
myIndex
=
0
;
SRaftId
ids
[
TSDB_MAX_REPLICA
];
SSyncInfo
syncInfo
;
SSyncFSM
*
pFsm
;
SWal
*
pWal
;
const
char
*
pDir
=
"./syncElectTest3"
;
int64_t
syncNodeInit
()
{
syncInfo
.
vgId
=
1234
;
syncInfo
.
rpcClient
=
gSyncIO
->
clientRpc
;
syncInfo
.
FpSendMsg
=
syncIOSendMsg
;
syncInfo
.
queue
=
gSyncIO
->
pMsgQ
;
syncInfo
.
FpEqMsg
=
syncIOEqMsg
;
syncInfo
.
pFsm
=
pFsm
;
snprintf
(
syncInfo
.
path
,
sizeof
(
syncInfo
.
path
),
"%s_sync_%d"
,
pDir
,
myIndex
);
int
code
=
walInit
();
assert
(
code
==
0
);
SWalCfg
walCfg
;
memset
(
&
walCfg
,
0
,
sizeof
(
SWalCfg
));
walCfg
.
vgId
=
syncInfo
.
vgId
;
walCfg
.
fsyncPeriod
=
1000
;
walCfg
.
retentionPeriod
=
1000
;
walCfg
.
rollPeriod
=
1000
;
walCfg
.
retentionSize
=
1000
;
walCfg
.
segSize
=
1000
;
walCfg
.
level
=
TAOS_WAL_FSYNC
;
char
tmpdir
[
128
];
snprintf
(
tmpdir
,
sizeof
(
tmpdir
),
"%s_wal_%d"
,
pDir
,
myIndex
);
pWal
=
walOpen
(
tmpdir
,
&
walCfg
);
assert
(
pWal
!=
NULL
);
syncInfo
.
pWal
=
pWal
;
SSyncCfg
*
pCfg
=
&
syncInfo
.
syncCfg
;
pCfg
->
myIndex
=
myIndex
;
pCfg
->
replicaNum
=
replicaNum
;
for
(
int
i
=
0
;
i
<
replicaNum
;
++
i
)
{
pCfg
->
nodeInfo
[
i
].
nodePort
=
ports
[
i
];
snprintf
(
pCfg
->
nodeInfo
[
i
].
nodeFqdn
,
sizeof
(
pCfg
->
nodeInfo
[
i
].
nodeFqdn
),
"%s"
,
"127.0.0.1"
);
// taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn);
}
int64_t
rid
=
syncOpen
(
&
syncInfo
);
assert
(
rid
>
0
);
syncStart
(
rid
);
SSyncNode
*
pSyncNode
=
(
SSyncNode
*
)
syncNodeAcquire
(
rid
);
assert
(
pSyncNode
!=
NULL
);
pSyncNode
->
hbBaseLine
=
500
;
pSyncNode
->
electBaseLine
=
1500
;
gSyncIO
->
FpOnSyncPing
=
pSyncNode
->
FpOnPing
;
gSyncIO
->
FpOnSyncPingReply
=
pSyncNode
->
FpOnPingReply
;
gSyncIO
->
FpOnSyncRequestVote
=
pSyncNode
->
FpOnRequestVote
;
gSyncIO
->
FpOnSyncRequestVoteReply
=
pSyncNode
->
FpOnRequestVoteReply
;
gSyncIO
->
FpOnSyncAppendEntries
=
pSyncNode
->
FpOnAppendEntries
;
gSyncIO
->
FpOnSyncAppendEntriesReply
=
pSyncNode
->
FpOnAppendEntriesReply
;
gSyncIO
->
FpOnSyncPing
=
pSyncNode
->
FpOnPing
;
gSyncIO
->
FpOnSyncPingReply
=
pSyncNode
->
FpOnPingReply
;
gSyncIO
->
FpOnSyncTimeout
=
pSyncNode
->
FpOnTimeout
;
gSyncIO
->
pSyncNode
=
pSyncNode
;
syncNodeStart
(
pSyncNode
);
syncNodeRelease
(
pSyncNode
);
return
rid
;
}
void
initRaftId
(
SSyncNode
*
pSyncNode
)
{
for
(
int
i
=
0
;
i
<
replicaNum
;
++
i
)
{
ids
[
i
]
=
pSyncNode
->
replicasId
[
i
];
char
*
s
=
syncUtilRaftId2Str
(
&
ids
[
i
]);
printf
(
"raftId[%d] : %s
\n
"
,
i
,
s
);
taosMemoryFree
(
s
);
}
}
int
main
(
int
argc
,
char
**
argv
)
{
// taosInitLog((char *)"syncTest.log", 100000, 10);
tsAsyncLog
=
0
;
sDebugFlag
=
143
+
64
;
myIndex
=
0
;
if
(
argc
>=
2
)
{
myIndex
=
atoi
(
argv
[
1
]);
}
int32_t
ret
=
syncIOStart
((
char
*
)
"127.0.0.1"
,
ports
[
myIndex
]);
assert
(
ret
==
0
);
ret
=
syncInit
();
assert
(
ret
==
0
);
int64_t
rid
=
syncNodeInit
();
assert
(
rid
>
0
);
SSyncNode
*
pSyncNode
=
(
SSyncNode
*
)
syncNodeAcquire
(
rid
);
assert
(
pSyncNode
!=
NULL
);
syncNodeLog2
((
char
*
)
""
,
pSyncNode
);
initRaftId
(
pSyncNode
);
//---------------------------
while
(
1
)
{
sTrace
(
"elect sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, electTimerMS:%d"
,
pSyncNode
->
state
,
syncUtilState2String
(
pSyncNode
->
state
),
pSyncNode
->
pRaftStore
->
currentTerm
,
pSyncNode
->
electTimerLogicClock
,
pSyncNode
->
electTimerLogicClockUser
,
pSyncNode
->
electTimerMS
);
taosMsleep
(
1000
);
}
syncNodeRelease
(
pSyncNode
);
return
0
;
}
source/libs/sync/test/syncReplicateTest.cpp
浏览文件 @
a6b94983
...
...
@@ -3,11 +3,8 @@
#include "syncEnv.h"
#include "syncIO.h"
#include "syncInt.h"
#include "syncMessage.h"
#include "syncRaftEntry.h"
#include "syncRaftLog.h"
#include "syncRaftStore.h"
#include "syncUtil.h"
#include "wal.h"
void
logTest
()
{
sTrace
(
"--- sync log test: trace"
);
...
...
@@ -18,184 +15,204 @@ void logTest() {
sFatal
(
"--- sync log test: fatal"
);
}
uint16_t
ports
[]
=
{
7010
,
7110
,
7210
,
7310
,
7410
};
int32_t
replicaNum
=
3
;
int32_t
myIndex
=
0
;
uint16_t
gPorts
[]
=
{
7010
,
7110
,
7210
,
7310
,
7410
};
const
char
*
gDir
=
"./syncReplicateTest"
;
int32_t
gVgId
=
1234
;
SyncIndex
gSnapshotLastApplyIndex
;
SRaftId
ids
[
TSDB_MAX_REPLICA
];
SSyncInfo
syncInfo
;
SSyncFSM
*
pFsm
;
SWal
*
pWal
;
SSyncNode
*
gSyncNode
;
void
init
()
{
int
code
=
walInit
();
assert
(
code
==
0
);
code
=
syncInit
();
assert
(
code
==
0
);
}
void
cleanup
()
{
walCleanUp
();
}
void
CommitCb
(
struct
SSyncFSM
*
pFsm
,
const
SRpcMsg
*
pMsg
,
SFsmCbMeta
cbMeta
)
{
char
logBuf
[
256
];
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"==callback== ==CommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s
\n
"
,
pFsm
,
cbMeta
.
index
,
cbMeta
.
isWeak
,
cbMeta
.
code
,
cbMeta
.
state
,
syncUtilState2String
(
cbMeta
.
state
));
syncRpcMsgLog2
(
logBuf
,
(
SRpcMsg
*
)
pMsg
);
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
];
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"==callback== ==CommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s
\n
"
,
pFsm
,
cbMeta
.
index
,
cbMeta
.
isWeak
,
cbMeta
.
code
,
cbMeta
.
state
,
syncUtilState2String
(
cbMeta
.
state
));
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
)
{
void
PreCommitCb
(
struct
SSyncFSM
*
pFsm
,
const
SRpcMsg
*
pMsg
,
SFsmCbMeta
cbMeta
)
{
char
logBuf
[
256
];
snprintf
(
logBuf
,
sizeof
(
logBuf
),
"==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s
\n
"
,
pFsm
,
cbMeta
.
index
,
cbMeta
.
isWeak
,
cbMeta
.
code
,
cbMeta
.
state
,
syncUtilState2String
(
cbMeta
.
state
));
syncRpcMsgLog2
(
logBuf
,
(
SRpcMsg
*
)
pMsg
);
syncRpcMsgLog2
(
logBuf
,
(
SRpcMsg
*
)
pMsg
);
}
void
RollBackCb
(
struct
SSyncFSM
*
pFsm
,
const
SRpcMsg
*
pMsg
,
SFsmCbMeta
cbMeta
)
{
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
\n
"
,
pFsm
,
cbMeta
.
index
,
cbMeta
.
isWeak
,
cbMeta
.
code
,
cbMeta
.
state
,
syncUtilState2String
(
cbMeta
.
state
));
syncRpcMsgLog2
(
logBuf
,
(
SRpcMsg
*
)
pMsg
);
syncRpcMsgLog2
(
logBuf
,
(
SRpcMsg
*
)
pMsg
);
}
int32_t
GetSnapshotCb
(
struct
SSyncFSM
*
pFsm
,
SSnapshot
*
pSnapshot
)
{
pSnapshot
->
data
=
NULL
;
pSnapshot
->
lastApplyIndex
=
gSnapshotLastApplyIndex
;
pSnapshot
->
lastApplyTerm
=
100
;
return
0
;
}
void
init
Fsm
()
{
pFsm
=
(
SSyncFSM
*
)
taosMemoryMalloc
(
sizeof
(
SSyncFSM
));
SSyncFSM
*
create
Fsm
()
{
SSyncFSM
*
pFsm
=
(
SSyncFSM
*
)
taosMemoryMalloc
(
sizeof
(
SSyncFSM
));
pFsm
->
FpCommitCb
=
CommitCb
;
pFsm
->
FpPreCommitCb
=
PreCommitCb
;
pFsm
->
FpRollBackCb
=
RollBackCb
;
pFsm
->
FpGetSnapshot
=
GetSnapshotCb
;
return
pFsm
;
}
SSyncNode
*
syncNodeInit
()
{
syncInfo
.
vgId
=
1234
;
syncInfo
.
rpcClient
=
gSyncIO
->
clientRpc
;
syncInfo
.
FpSendMsg
=
syncIOSendMsg
;
syncInfo
.
queue
=
gSyncIO
->
pMsgQ
;
syncInfo
.
FpEqMsg
=
syncIOEqMsg
;
syncInfo
.
pFsm
=
pFsm
;
snprintf
(
syncInfo
.
path
,
sizeof
(
syncInfo
.
path
),
"./replicate_test_%d"
,
myIndex
);
int
code
=
walInit
();
assert
(
code
==
0
);
SWal
*
createWal
(
char
*
path
,
int32_t
vgId
)
{
SWalCfg
walCfg
;
memset
(
&
walCfg
,
0
,
sizeof
(
SWalCfg
));
walCfg
.
vgId
=
syncInfo
.
vgId
;
walCfg
.
vgId
=
vgId
;
walCfg
.
fsyncPeriod
=
1000
;
walCfg
.
retentionPeriod
=
1000
;
walCfg
.
rollPeriod
=
1000
;
walCfg
.
retentionSize
=
1000
;
walCfg
.
segSize
=
1000
;
walCfg
.
level
=
TAOS_WAL_FSYNC
;
char
tmpdir
[
128
];
snprintf
(
tmpdir
,
sizeof
(
tmpdir
),
"./replicate_test_wal_%d"
,
myIndex
);
pWal
=
walOpen
(
tmpdir
,
&
walCfg
);
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
)
{
SSyncInfo
syncInfo
;
syncInfo
.
vgId
=
vgId
;
syncInfo
.
rpcClient
=
gSyncIO
->
clientRpc
;
syncInfo
.
FpSendMsg
=
syncIOSendMsg
;
syncInfo
.
queue
=
gSyncIO
->
pMsgQ
;
syncInfo
.
FpEqMsg
=
syncIOEqMsg
;
syncInfo
.
pFsm
=
createFsm
();
snprintf
(
syncInfo
.
path
,
sizeof
(
syncInfo
.
path
),
"%s_sync_replica%d_index%d"
,
path
,
replicaNum
,
myIndex
);
syncInfo
.
pWal
=
pWal
;
SSyncCfg
*
pCfg
=
&
syncInfo
.
syncCfg
;
SSyncCfg
*
pCfg
=
&
syncInfo
.
syncCfg
;
pCfg
->
myIndex
=
myIndex
;
pCfg
->
replicaNum
=
replicaNum
;
for
(
int
i
=
0
;
i
<
replicaNum
;
++
i
)
{
pCfg
->
nodeInfo
[
i
].
nodePort
=
p
orts
[
i
];
snprintf
(
pCfg
->
nodeInfo
[
i
].
nodeFqdn
,
sizeof
(
pCfg
->
nodeInfo
[
i
].
nodeFqdn
),
"%s"
,
"127.0.0.1"
);
//
taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn
);
pCfg
->
nodeInfo
[
i
].
nodePort
=
gP
orts
[
i
];
taosGetFqdn
(
pCfg
->
nodeInfo
[
i
].
nodeFqdn
);
//
snprintf(pCfg->nodeInfo[i].nodeFqdn, sizeof(pCfg->nodeInfo[i].nodeFqdn), "%s", "127.0.0.1"
);
}
SSyncNode
*
pSyncNode
=
syncNodeOpen
(
&
syncInfo
);
int64_t
rid
=
syncOpen
(
&
syncInfo
);
assert
(
rid
>
0
);
syncStart
(
rid
);
SSyncNode
*
pSyncNode
=
(
SSyncNode
*
)
syncNodeAcquire
(
rid
);
assert
(
pSyncNode
!=
NULL
);
gSyncIO
->
FpOnSyncPing
=
pSyncNode
->
FpOnPing
;
gSyncIO
->
FpOnSyncClientRequest
=
pSyncNode
->
FpOnClientRequest
;
gSyncIO
->
FpOnSyncPingReply
=
pSyncNode
->
FpOnPingReply
;
gSyncIO
->
FpOnSyncRequestVote
=
pSyncNode
->
FpOnRequestVote
;
gSyncIO
->
FpOnSyncRequestVoteReply
=
pSyncNode
->
FpOnRequestVoteReply
;
gSyncIO
->
FpOnSyncAppendEntries
=
pSyncNode
->
FpOnAppendEntries
;
gSyncIO
->
FpOnSyncAppendEntriesReply
=
pSyncNode
->
FpOnAppendEntriesReply
;
gSyncIO
->
FpOnSyncPing
=
pSyncNode
->
FpOnPing
;
gSyncIO
->
FpOnSyncPingReply
=
pSyncNode
->
FpOnPingReply
;
gSyncIO
->
FpOnSyncTimeout
=
pSyncNode
->
FpOnTimeout
;
gSyncIO
->
pSyncNode
=
pSyncNode
;
return
pSyncNode
;
}
SSyncNode
*
syncInitTest
()
{
return
syncNodeInit
();
}
syncNodeStart
(
pSyncNode
);
void
initRaftId
(
SSyncNode
*
pSyncNode
)
{
for
(
int
i
=
0
;
i
<
replicaNum
;
++
i
)
{
ids
[
i
]
=
pSyncNode
->
replicasId
[
i
];
char
*
s
=
syncUtilRaftId2Str
(
&
ids
[
i
]);
printf
(
"raftId[%d] : %s
\n
"
,
i
,
s
);
taosMemoryFree
(
s
);
}
return
rid
;
}
SRpcMsg
*
step0
(
int
i
)
{
void
usage
(
char
*
exe
)
{
printf
(
"usage: %s replicaNum myIndex lastApplyIndex writeRecordNum
\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
=
128
;
pMsg
->
pCont
=
taosMemoryMalloc
(
pMsg
->
contLen
);
snprintf
((
char
*
)(
pMsg
->
pCont
),
pMsg
->
contLen
,
"value-
%u-%d"
,
ports
[
myIndex
],
i
);
snprintf
((
char
*
)(
pMsg
->
pCont
),
pMsg
->
contLen
,
"value-
myIndex:%u-%d-%d-%ld"
,
myIndex
,
i
,
count
,
taosGetTimestampMs
()
);
return
pMsg
;
}
SyncClientRequest
*
step1
(
const
SRpcMsg
*
pMsg
)
{
SyncClientRequest
*
pRetMsg
=
syncClientRequestBuild2
(
pMsg
,
123
,
true
,
1000
);
return
pRetMsg
;
}
int
main
(
int
argc
,
char
**
argv
)
{
// taosInitLog((char *)"syncTest.log", 100000, 10);
int
main
(
int
argc
,
char
**
argv
)
{
tsAsyncLog
=
0
;
sDebugFlag
=
143
+
64
;
void
logTest
();
myIndex
=
0
;
if
(
argc
>=
2
)
{
myIndex
=
atoi
(
argv
[
1
]);
sDebugFlag
=
DEBUG_TRACE
+
DEBUG_SCREEN
+
DEBUG_FILE
;
if
(
argc
!=
5
)
{
usage
(
argv
[
0
]);
exit
(
-
1
);
}
int32_t
ret
=
syncIOStart
((
char
*
)
"127.0.0.1"
,
ports
[
myIndex
]);
assert
(
ret
==
0
);
ret
=
syncEnvStart
();
int32_t
replicaNum
=
atoi
(
argv
[
1
]);
int32_t
myIndex
=
atoi
(
argv
[
2
]);
int32_t
lastApplyIndex
=
atoi
(
argv
[
3
]);
int32_t
writeRecordNum
=
atoi
(
argv
[
4
]);
gSnapshotLastApplyIndex
=
lastApplyIndex
;
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
);
taosRemoveDir
(
"./wal_test"
);
initFsm
();
gSyncNode
=
syncInitTest
();
assert
(
gSyncNode
!=
NULL
);
syncNodeLog2
((
char
*
)
""
,
gSyncNode
);
initRaftId
(
gSyncNode
);
char
walPath
[
128
];
snprintf
(
walPath
,
sizeof
(
walPath
),
"%s_wal_replica%d_index%d"
,
gDir
,
replicaNum
,
myIndex
);
SWal
*
pWal
=
createWal
(
walPath
,
gVgId
);
for
(
int
i
=
0
;
i
<
30
;
++
i
)
{
// step0
SRpcMsg
*
pMsg0
=
step0
(
i
);
syncRpcMsgLog2
((
char
*
)
"==step0=="
,
pMsg0
);
int64_t
rid
=
createSyncNode
(
replicaNum
,
myIndex
,
gVgId
,
pWal
,
(
char
*
)
gDir
);
assert
(
rid
>
0
);
syncStart
(
rid
);
// step1
SyncClientRequest
*
pMsg1
=
step1
(
pMsg0
);
syncClientRequestLog2
((
char
*
)
"==step1=="
,
pMsg1
);
SSyncNode
*
pSyncNode
=
(
SSyncNode
*
)
syncNodeAcquire
(
rid
);
assert
(
pSyncNode
!=
NULL
);
SyncClientRequest
*
pSyncClientRequest
=
pMsg1
;
SRpcMsg
rpcMsg
;
syncClientRequest2RpcMsg
(
pSyncClientRequest
,
&
rpcMsg
);
gSyncNode
->
FpEqMsg
(
gSyncNode
->
queue
,
&
rpcMsg
);
//---------------------------
int32_t
alreadySend
=
0
;
while
(
1
)
{
char
*
s
=
syncNode2SimpleStr
(
pSyncNode
);
sTrace
(
"%s"
,
s
);
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
);
}
taosMsleep
(
1000
);
sTrace
(
"syncPropose sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, "
"electTimerMS:%d, commitIndex:%ld"
,
gSyncNode
->
state
,
syncUtilState2String
(
gSyncNode
->
state
),
gSyncNode
->
pRaftStore
->
currentTerm
,
gSyncNode
->
electTimerLogicClock
,
gSyncNode
->
electTimerLogicClockUser
,
gSyncNode
->
electTimerMS
,
gSyncNode
->
commitIndex
);
}
while
(
1
)
{
sTrace
(
"replicate sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, "
"electTimerMS:%d, commitIndex:%ld"
,
gSyncNode
->
state
,
syncUtilState2String
(
gSyncNode
->
state
),
gSyncNode
->
pRaftStore
->
currentTerm
,
gSyncNode
->
electTimerLogicClock
,
gSyncNode
->
electTimerLogicClockUser
,
gSyncNode
->
electTimerMS
,
gSyncNode
->
commitIndex
);
taosMemoryFree
(
s
);
taosMsleep
(
1000
);
}
syncNodeRelease
(
pSyncNode
);
syncStop
(
rid
);
walClose
(
pWal
);
syncIOStop
();
cleanup
();
return
0
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录