Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
c67a14ad
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看板
提交
c67a14ad
编写于
2月 24, 2022
作者:
M
Minghao Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add raft store
上级
8b71c4f5
变更
6
隐藏空白更改
内联
并排
Showing
6 changed file
with
138 addition
and
42 deletion
+138
-42
source/libs/sync/inc/syncIO.h
source/libs/sync/inc/syncIO.h
+2
-2
source/libs/sync/inc/syncRaft.h
source/libs/sync/inc/syncRaft.h
+1
-1
source/libs/sync/inc/syncRaftStore.h
source/libs/sync/inc/syncRaftStore.h
+23
-7
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
+95
-29
source/libs/sync/test/syncTest.cpp
source/libs/sync/test/syncTest.cpp
+16
-2
未找到文件。
source/libs/sync/inc/syncIO.h
浏览文件 @
c67a14ad
...
...
@@ -37,10 +37,10 @@ typedef struct SSyncIO {
pthread_t
tid
;
int8_t
isStart
;
SEpSet
epSet
;
SEpSet
epSet
;
void
*
syncTimer
;
void
*
syncTimerManager
;
void
*
syncTimerManager
;
int32_t
(
*
start
)(
struct
SSyncIO
*
ths
);
int32_t
(
*
stop
)(
struct
SSyncIO
*
ths
);
...
...
source/libs/sync/inc/syncRaft.h
浏览文件 @
c67a14ad
...
...
@@ -28,7 +28,7 @@ extern "C" {
#include "taosdef.h"
typedef
struct
SRaftId
{
SyncNodeId
nodeId
;
SyncNodeId
addr
;
SyncGroupId
vgId
;
}
SRaftId
;
...
...
source/libs/sync/inc/syncRaftStore.h
浏览文件 @
c67a14ad
...
...
@@ -23,20 +23,36 @@ extern "C" {
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "sync.h"
#include "cJSON.h"
#include "syncInt.h"
#include "syncRaft.h"
#include "taosdef.h"
void
testJson
();
void
testJson2
();
#define RAFT_STORE_BLOCK_SIZE 512
#define RAFT_STORE_PATH_LEN 128
int32_t
currentTerm
(
SyncTerm
*
pCurrentTerm
);
typedef
struct
SRaftStore
{
SyncTerm
currentTerm
;
SRaftId
voteFor
;
FileFd
fd
;
char
path
[
RAFT_STORE_PATH_LEN
];
}
SRaftStore
;
int32_t
persistCurrentTerm
(
SyncTerm
currentTerm
);
SRaftStore
*
raftStoreOpen
(
const
char
*
path
);
int32_t
voteFor
(
SRaftId
*
pRaftId
);
static
int32_t
raftStoreInit
(
SRaftStore
*
pRaftStore
);
int32_t
persistVoteFor
(
SRaftId
*
pRaftId
);
int32_t
raftStoreClose
(
SRaftStore
*
pRaftStore
);
int32_t
raftStorePersist
(
SRaftStore
*
pRaftStore
);
static
bool
raftStoreFileExist
(
char
*
path
);
int32_t
raftStoreSerialize
(
SRaftStore
*
pRaftStore
,
char
*
buf
,
size_t
len
);
int32_t
raftStoreDeserialize
(
SRaftStore
*
pRaftStore
,
char
*
buf
,
size_t
len
);
void
raftStorePrint
(
SRaftStore
*
pRaftStore
);
#ifdef __cplusplus
}
...
...
source/libs/sync/src/syncIO.c
浏览文件 @
c67a14ad
...
...
@@ -45,7 +45,7 @@ void *syncConsumer(void *param) {
SSyncIO
*
io
=
param
;
STaosQall
*
qall
;
SRpcMsg
*
pRpcMsg
,
rpcMsg
;
SRpcMsg
*
pRpcMsg
,
rpcMsg
;
int
type
;
qall
=
taosAllocateQall
();
...
...
source/libs/sync/src/syncRaftStore.c
浏览文件 @
c67a14ad
...
...
@@ -17,50 +17,116 @@
#include "cJSON.h"
#include "sync.h"
FileFd
raftStoreFd
;
SRaftStore
*
raftStoreOpen
(
const
char
*
path
)
{
int32_t
ret
;
void
testJson
()
{
raftStoreFd
=
taosOpenFileReadWrite
(
"raft.store"
);
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
);
uint64_t
currentTerm
=
100
;
uint64_t
voteFor
=
200
;
char
storeBuf
[
RAFT_STORE_BLOCK_SIZE
]
;
memset
(
storeBuf
,
0
,
sizeof
(
storeBuf
))
;
cJSON
*
pRoot
=
cJSON_CreateObject
();
cJSON_AddNumberToObject
(
pRoot
,
"current_term"
,
currentTerm
);
cJSON_AddNumberToObject
(
pRoot
,
"vote_for"
,
voteFor
);
if
(
!
raftStoreFileExist
(
pRaftStore
->
path
))
{
ret
=
raftStoreInit
(
pRaftStore
);
assert
(
ret
==
0
);
}
char
*
serialized
=
cJSON_Print
(
pRoot
);
int
len
=
strlen
(
serialized
);
printf
(
"serialized: %s
\n
"
,
serialized
);
pRaftStore
->
fd
=
taosOpenFileReadWrite
(
pRaftStore
->
path
);
if
(
pRaftStore
->
fd
<
0
)
{
return
NULL
;
}
int
len
=
taosReadFile
(
pRaftStore
->
fd
,
storeBuf
,
sizeof
(
storeBuf
));
assert
(
len
==
RAFT_STORE_BLOCK_SIZE
);
taosWriteFile
(
raftStoreFd
,
serialized
,
len
);
ret
=
raftStoreDeserialize
(
pRaftStore
,
storeBuf
,
len
);
assert
(
ret
==
0
);
return
pRaftStore
;
}
void
testJson2
()
{
taosLSeekFile
(
raftStoreFd
,
0
,
SEEK_SET
);
static
int32_t
raftStoreInit
(
SRaftStore
*
pRaftStore
)
{
pRaftStore
->
fd
=
taosOpenFileCreateWrite
(
pRaftStore
->
path
);
if
(
pRaftStore
->
fd
<
0
)
{
return
-
1
;
}
char
buf
[
128
];
memset
(
buf
,
0
,
sizeof
(
buf
));
taosReadFile
(
raftStoreFd
,
buf
,
sizeof
(
buf
));
printf
(
"read file: %s
\n
"
,
buf
);
pRaftStore
->
currentTerm
=
0
;
pRaftStore
->
voteFor
.
addr
=
0
;
pRaftStore
->
voteFor
.
vgId
=
0
;
cJSON
*
pRoot
=
cJSON_Parse
(
buf
);
int32_t
ret
=
raftStorePersist
(
pRaftStore
);
assert
(
ret
==
0
);
cJSON
*
pCurrentTerm
=
cJSON_GetObjectItem
(
pRoot
,
"current_term"
);
uint64_t
currentTerm
=
pCurrentTerm
->
valueint
;
taosCloseFile
(
pRaftStore
->
fd
);
return
0
;
}
cJSON
*
pVoteFor
=
cJSON_GetObjectItem
(
pRoot
,
"vote_for"
);
uint64_t
voteFor
=
pVoteFor
->
valueint
;
int32_t
raftStoreClose
(
SRaftStore
*
pRaftStore
)
{
taosCloseFile
(
pRaftStore
->
fd
);
free
(
pRaftStore
);
return
0
;
}
int32_t
raftStorePersist
(
SRaftStore
*
pRaftStore
)
{
int32_t
ret
;
char
storeBuf
[
RAFT_STORE_BLOCK_SIZE
];
ret
=
raftStoreSerialize
(
pRaftStore
,
storeBuf
,
sizeof
(
storeBuf
));
assert
(
ret
==
0
);
printf
(
"read json: currentTerm:%lu, voteFor:%lu
\n
"
,
currentTerm
,
voteFor
);
taosLSeekFile
(
pRaftStore
->
fd
,
0
,
SEEK_SET
);
taosCloseFile
(
raftStoreFd
);
ret
=
taosWriteFile
(
pRaftStore
->
fd
,
storeBuf
,
sizeof
(
storeBuf
));
assert
(
ret
==
RAFT_STORE_BLOCK_SIZE
);
fsync
(
pRaftStore
->
fd
);
return
0
;
}
int32_t
currentTerm
(
SyncTerm
*
pCurrentTerm
)
{
return
0
;
}
static
bool
raftStoreFileExist
(
char
*
path
)
{
return
taosStatFile
(
path
,
NULL
,
NULL
)
>=
0
;
}
int32_t
persistCurrentTerm
(
SyncTerm
currentTerm
)
{
return
0
;
}
int32_t
raftStoreSerialize
(
SRaftStore
*
pRaftStore
,
char
*
buf
,
size_t
len
)
{
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
);
int32_t
voteFor
(
SRaftId
*
pRaftId
)
{
return
0
;
}
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
raftStoreDeserialize
(
SRaftStore
*
pRaftStore
,
char
*
buf
,
size_t
len
)
{
assert
(
len
>
0
&&
len
<=
RAFT_STORE_BLOCK_SIZE
);
cJSON
*
pRoot
=
cJSON_Parse
(
buf
);
cJSON
*
pCurrentTerm
=
cJSON_GetObjectItem
(
pRoot
,
"current_term"
);
pRaftStore
->
currentTerm
=
pCurrentTerm
->
valueint
;
int32_t
persistVoteFor
(
SRaftId
*
pRaftId
)
{
return
0
;
}
\ No newline at end of file
cJSON
*
pVoteForAddr
=
cJSON_GetObjectItem
(
pRoot
,
"vote_for_addr"
);
pRaftStore
->
voteFor
.
addr
=
pVoteForAddr
->
valueint
;
cJSON
*
pVoteForVgid
=
cJSON_GetObjectItem
(
pRoot
,
"vote_for_vgid"
);
pRaftStore
->
voteFor
.
vgId
=
pVoteForVgid
->
valueint
;
cJSON_Delete
(
pRoot
);
return
0
;
}
void
raftStorePrint
(
SRaftStore
*
pRaftStore
)
{
char
storeBuf
[
RAFT_STORE_BLOCK_SIZE
];
raftStoreSerialize
(
pRaftStore
,
storeBuf
,
sizeof
(
storeBuf
));
printf
(
"%s
\n
"
,
storeBuf
);
}
source/libs/sync/test/syncTest.cpp
浏览文件 @
c67a14ad
...
...
@@ -14,9 +14,23 @@ void *pingFunc(void *param) {
}
int
main
()
{
tsAsyncLog
=
0
;
taosInitLog
((
char
*
)
"syncTest.log"
,
100000
,
10
);
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
);
testJson
();
testJson2
();
tsAsyncLog
=
0
;
taosInitLog
((
char
*
)
"syncTest.log"
,
100000
,
10
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录