Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
a78d7028
T
TDengine
项目概览
taosdata
/
TDengine
1 年多 前同步成功
通知
1185
Star
22016
Fork
4786
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
1
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
T
TDengine
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
1
Issue
1
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
a78d7028
编写于
3月 03, 2022
作者:
M
Minghao Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
new fileIO
上级
8c289ed3
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
171 addition
and
10 deletion
+171
-10
source/libs/sync/inc/syncRaftStore.h
source/libs/sync/inc/syncRaftStore.h
+2
-1
source/libs/sync/src/syncIO.c
source/libs/sync/src/syncIO.c
+1
-1
source/libs/sync/src/syncRaftStore.c
source/libs/sync/src/syncRaftStore.c
+112
-8
source/libs/sync/test/CMakeLists.txt
source/libs/sync/test/CMakeLists.txt
+14
-0
source/libs/sync/test/syncRaftStoreTest.cpp
source/libs/sync/test/syncRaftStoreTest.cpp
+42
-0
未找到文件。
source/libs/sync/inc/syncRaftStore.h
浏览文件 @
a78d7028
...
...
@@ -35,7 +35,8 @@ typedef struct SRaftStore {
SyncTerm
currentTerm
;
SRaftId
voteFor
;
// FileFd fd;
char
path
[
RAFT_STORE_PATH_LEN
];
TdFilePtr
pFile
;
char
path
[
RAFT_STORE_PATH_LEN
];
}
SRaftStore
;
SRaftStore
*
raftStoreOpen
(
const
char
*
path
);
...
...
source/libs/sync/src/syncIO.c
浏览文件 @
a78d7028
...
...
@@ -193,7 +193,7 @@ static void *syncIOConsumerFunc(void *param) {
SSyncIO
*
io
=
param
;
STaosQall
*
qall
;
SRpcMsg
*
pRpcMsg
,
rpcMsg
;
SRpcMsg
*
pRpcMsg
,
rpcMsg
;
int
type
;
qall
=
taosAllocateQall
();
...
...
source/libs/sync/src/syncRaftStore.c
浏览文件 @
a78d7028
...
...
@@ -18,21 +18,125 @@
// to complie success: FileIO interface is modified
SRaftStore
*
raftStoreOpen
(
const
char
*
path
)
{
return
NULL
;
}
SRaftStore
*
raftStoreOpen
(
const
char
*
path
)
{
int32_t
ret
;
SRaftStore
*
pRaftStore
=
malloc
(
sizeof
(
SRaftStore
));
if
(
pRaftStore
==
NULL
)
{
sError
(
"raftStoreOpen malloc error"
);
return
NULL
;
}
memset
(
pRaftStore
,
0
,
sizeof
(
*
pRaftStore
));
snprintf
(
pRaftStore
->
path
,
sizeof
(
pRaftStore
->
path
),
"%s"
,
path
);
char
storeBuf
[
RAFT_STORE_BLOCK_SIZE
];
memset
(
storeBuf
,
0
,
sizeof
(
storeBuf
));
if
(
!
raftStoreFileExist
(
pRaftStore
->
path
))
{
ret
=
raftStoreInit
(
pRaftStore
);
assert
(
ret
==
0
);
}
pRaftStore
->
pFile
=
taosOpenFile
(
path
,
TD_FILE_READ
|
TD_FILE_WRITE
);
assert
(
pRaftStore
->
pFile
!=
NULL
);
int
len
=
taosReadFile
(
pRaftStore
->
pFile
,
storeBuf
,
RAFT_STORE_BLOCK_SIZE
);
assert
(
len
==
RAFT_STORE_BLOCK_SIZE
);
ret
=
raftStoreDeserialize
(
pRaftStore
,
storeBuf
,
len
);
assert
(
ret
==
0
);
return
pRaftStore
;
}
static
int32_t
raftStoreInit
(
SRaftStore
*
pRaftStore
)
{
assert
(
pRaftStore
!=
NULL
);
pRaftStore
->
pFile
=
taosOpenFile
(
pRaftStore
->
path
,
TD_FILE_CTEATE
|
TD_FILE_WRITE
);
assert
(
pRaftStore
->
pFile
!=
NULL
);
pRaftStore
->
currentTerm
=
0
;
pRaftStore
->
voteFor
.
addr
=
0
;
pRaftStore
->
voteFor
.
vgId
=
0
;
int32_t
ret
=
raftStorePersist
(
pRaftStore
);
assert
(
ret
==
0
);
taosCloseFile
(
&
pRaftStore
->
pFile
);
return
0
;
}
int32_t
raftStoreClose
(
SRaftStore
*
pRaftStore
)
{
assert
(
pRaftStore
!=
NULL
);
taosCloseFile
(
&
pRaftStore
->
pFile
);
free
(
pRaftStore
);
pRaftStore
=
NULL
;
return
0
;
}
int32_t
raftStorePersist
(
SRaftStore
*
pRaftStore
)
{
assert
(
pRaftStore
!=
NULL
);
static
int32_t
raftStoreInit
(
SRaftStore
*
pRaftStore
)
{
return
0
;
}
int32_t
ret
;
char
storeBuf
[
RAFT_STORE_BLOCK_SIZE
];
ret
=
raftStoreSerialize
(
pRaftStore
,
storeBuf
,
sizeof
(
storeBuf
));
assert
(
ret
==
0
);
taosLSeekFile
(
pRaftStore
->
pFile
,
0
,
SEEK_SET
);
ret
=
taosWriteFile
(
pRaftStore
->
pFile
,
storeBuf
,
sizeof
(
storeBuf
));
assert
(
ret
==
RAFT_STORE_BLOCK_SIZE
);
taosFsyncFile
(
pRaftStore
->
pFile
);
return
0
;
}
static
bool
raftStoreFileExist
(
char
*
path
)
{
return
taosStatFile
(
path
,
NULL
,
NULL
)
>=
0
;
}
int32_t
raftStoreSerialize
(
SRaftStore
*
pRaftStore
,
char
*
buf
,
size_t
len
)
{
assert
(
pRaftStore
!=
NULL
);
cJSON
*
pRoot
=
cJSON_CreateObject
();
cJSON_AddNumberToObject
(
pRoot
,
"current_term"
,
pRaftStore
->
currentTerm
);
cJSON_AddNumberToObject
(
pRoot
,
"vote_for_addr"
,
pRaftStore
->
voteFor
.
addr
);
cJSON_AddNumberToObject
(
pRoot
,
"vote_for_vgid"
,
pRaftStore
->
voteFor
.
vgId
);
char
*
serialized
=
cJSON_Print
(
pRoot
);
int
len2
=
strlen
(
serialized
);
assert
(
len2
<
len
);
memset
(
buf
,
0
,
len
);
snprintf
(
buf
,
len
,
"%s"
,
serialized
);
free
(
serialized
);
cJSON_Delete
(
pRoot
);
return
0
;
}
int32_t
raftStoreClose
(
SRaftStore
*
pRaftStore
)
{
return
0
;
}
int32_t
raftStoreDeserialize
(
SRaftStore
*
pRaftStore
,
char
*
buf
,
size_t
len
)
{
assert
(
pRaftStore
!=
NULL
);
assert
(
len
>
0
&&
len
<=
RAFT_STORE_BLOCK_SIZE
);
cJSON
*
pRoot
=
cJSON_Parse
(
buf
);
int32_t
raftStorePersist
(
SRaftStore
*
pRaftStore
)
{
return
0
;
}
cJSON
*
pCurrentTerm
=
cJSON_GetObjectItem
(
pRoot
,
"current_term"
);
pRaftStore
->
currentTerm
=
pCurrentTerm
->
valueint
;
static
bool
raftStoreFileExist
(
char
*
path
)
{
return
0
;
}
cJSON
*
pVoteForAddr
=
cJSON_GetObjectItem
(
pRoot
,
"vote_for_addr"
);
pRaftStore
->
voteFor
.
addr
=
pVoteForAddr
->
valueint
;
int32_t
raftStoreSerialize
(
SRaftStore
*
pRaftStore
,
char
*
buf
,
size_t
len
)
{
return
0
;
}
cJSON
*
pVoteForVgid
=
cJSON_GetObjectItem
(
pRoot
,
"vote_for_vgid"
);
pRaftStore
->
voteFor
.
vgId
=
pVoteForVgid
->
valueint
;
int32_t
raftStoreDeserialize
(
SRaftStore
*
pRaftStore
,
char
*
buf
,
size_t
len
)
{
return
0
;
}
cJSON_Delete
(
pRoot
);
return
0
;
}
void
raftStorePrint
(
SRaftStore
*
pRaftStore
)
{}
void
raftStorePrint
(
SRaftStore
*
pRaftStore
)
{
char
storeBuf
[
RAFT_STORE_BLOCK_SIZE
];
raftStoreSerialize
(
pRaftStore
,
storeBuf
,
sizeof
(
storeBuf
));
printf
(
"%s
\n
"
,
storeBuf
);
}
#if 0
...
...
source/libs/sync/test/CMakeLists.txt
浏览文件 @
a78d7028
...
...
@@ -7,6 +7,7 @@ add_executable(syncIOTickPingTest "")
add_executable
(
syncIOSendMsgTest
""
)
add_executable
(
syncIOSendMsgClientTest
""
)
add_executable
(
syncIOSendMsgServerTest
""
)
add_executable
(
syncRaftStoreTest
""
)
target_sources
(
syncTest
...
...
@@ -45,6 +46,10 @@ target_sources(syncIOSendMsgServerTest
PRIVATE
"syncIOSendMsgServerTest.cpp"
)
target_sources
(
syncRaftStoreTest
PRIVATE
"syncRaftStoreTest.cpp"
)
target_include_directories
(
syncTest
...
...
@@ -92,6 +97,11 @@ target_include_directories(syncIOSendMsgServerTest
"
${
CMAKE_SOURCE_DIR
}
/include/libs/sync"
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../inc"
)
target_include_directories
(
syncRaftStoreTest
PUBLIC
"
${
CMAKE_SOURCE_DIR
}
/include/libs/sync"
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../inc"
)
target_link_libraries
(
syncTest
...
...
@@ -130,6 +140,10 @@ target_link_libraries(syncIOSendMsgServerTest
sync
gtest_main
)
target_link_libraries
(
syncRaftStoreTest
sync
gtest_main
)
enable_testing
()
...
...
source/libs/sync/test/syncRaftStoreTest.cpp
0 → 100644
浏览文件 @
a78d7028
#include "syncRaftStore.h"
#include <stdio.h>
#include "gtest/gtest.h"
#include "syncIO.h"
#include "syncInt.h"
void
*
pingFunc
(
void
*
param
)
{
SSyncIO
*
io
=
(
SSyncIO
*
)
param
;
while
(
1
)
{
sDebug
(
"io->ping"
);
// io->ping(io);
sleep
(
1
);
}
return
NULL
;
}
int
main
()
{
// taosInitLog((char *)"syncTest.log", 100000, 10);
tsAsyncLog
=
0
;
sDebugFlag
=
143
+
64
;
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"
);
SRaftStore
*
pRaftStore
=
raftStoreOpen
(
"./raft_store.json"
);
assert
(
pRaftStore
!=
NULL
);
raftStorePrint
(
pRaftStore
);
pRaftStore
->
currentTerm
=
100
;
pRaftStore
->
voteFor
.
addr
=
200
;
pRaftStore
->
voteFor
.
vgId
=
300
;
raftStorePrint
(
pRaftStore
);
raftStorePersist
(
pRaftStore
);
return
0
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录