Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
264c30e5
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看板
提交
264c30e5
编写于
3月 08, 2022
作者:
M
Minghao Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
sync refactor
上级
8f108bfb
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
50 addition
and
9 deletion
+50
-9
source/libs/sync/inc/syncInt.h
source/libs/sync/inc/syncInt.h
+5
-4
source/libs/sync/inc/syncUtil.h
source/libs/sync/inc/syncUtil.h
+1
-0
source/libs/sync/src/syncMain.c
source/libs/sync/src/syncMain.c
+41
-4
source/libs/sync/src/syncUtil.c
source/libs/sync/src/syncUtil.c
+3
-1
未找到文件。
source/libs/sync/inc/syncInt.h
浏览文件 @
264c30e5
...
...
@@ -99,8 +99,8 @@ typedef struct SRaftStore SRaftStore;
struct
SVotesGranted
;
typedef
struct
SVotesGranted
SVotesGranted
;
struct
SVotesRespond
ed
;
typedef
struct
SVotesRespond
ed
SVotesResponde
d
;
struct
SVotesRespond
;
typedef
struct
SVotesRespond
SVotesRespon
d
;
typedef
struct
SRaftId
{
SyncNodeId
addr
;
// typedef uint64_t SyncNodeId;
...
...
@@ -112,6 +112,7 @@ typedef struct SSyncNode {
SyncGroupId
vgId
;
SSyncCfg
syncCfg
;
char
path
[
TSDB_FILENAME_LEN
];
char
walPath
[
TSDB_FILENAME_LEN
];
void
*
rpcClient
;
int32_t
(
*
FpSendMsg
)(
void
*
rpcClient
,
const
SEpSet
*
pEpSet
,
SRpcMsg
*
pMsg
);
void
*
queue
;
...
...
@@ -142,8 +143,8 @@ typedef struct SSyncNode {
SRaftStore
*
pRaftStore
;
// tla+ candidate vars
SVotesGranted
*
pVotesGranted
;
SVotesRespond
ed
*
pVotesResponde
d
;
SVotesGranted
*
pVotesGranted
;
SVotesRespond
*
pVotesRespon
d
;
// tla+ leader vars
SHashObj
*
pNextIndex
;
...
...
source/libs/sync/inc/syncUtil.h
浏览文件 @
264c30e5
...
...
@@ -44,6 +44,7 @@ void syncUtilbufCopyDeep(const SSyncBuffer* src, SSyncBuffer* dest);
// ---- misc ----
int32_t
syncUtilRand
(
int32_t
max
);
int32_t
syncUtilElectRandomMS
();
int32_t
syncUtilQuorum
(
int32_t
replicaNum
);
#ifdef __cplusplus
}
...
...
source/libs/sync/src/syncMain.c
浏览文件 @
264c30e5
...
...
@@ -20,10 +20,12 @@
#include "syncEnv.h"
#include "syncInt.h"
#include "syncRaft.h"
#include "syncRaftStore.h"
#include "syncRequestVote.h"
#include "syncRequestVoteReply.h"
#include "syncTimeout.h"
#include "syncUtil.h"
#include "syncVoteMgr.h"
static
int32_t
tsNodeRefId
=
-
1
;
...
...
@@ -35,6 +37,7 @@ static void syncNodeEqHeartbeatTimer(void* param, void* tmrId);
static
int32_t
syncNodeOnPingCb
(
SSyncNode
*
ths
,
SyncPing
*
pMsg
);
static
int32_t
syncNodeOnPingReplyCb
(
SSyncNode
*
ths
,
SyncPingReply
*
pMsg
);
static
void
UpdateTerm
(
SyncTerm
term
);
static
void
syncNodeBecomeFollower
(
SSyncNode
*
pSyncNode
);
static
void
syncNodeBecomeLeader
(
SSyncNode
*
pSyncNode
);
static
void
syncNodeFollower2Candidate
(
SSyncNode
*
pSyncNode
);
...
...
@@ -71,19 +74,22 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo) {
assert
(
pSyncNode
!=
NULL
);
memset
(
pSyncNode
,
0
,
sizeof
(
SSyncNode
));
// init by SSyncInfo
pSyncNode
->
vgId
=
pSyncInfo
->
vgId
;
pSyncNode
->
syncCfg
=
pSyncInfo
->
syncCfg
;
memcpy
(
pSyncNode
->
path
,
pSyncInfo
->
path
,
sizeof
(
pSyncNode
->
path
));
pSyncNode
->
pFsm
=
pSyncInfo
->
pFsm
;
memcpy
(
pSyncNode
->
walPath
,
pSyncInfo
->
walPath
,
sizeof
(
pSyncNode
->
walPath
));
pSyncNode
->
rpcClient
=
pSyncInfo
->
rpcClient
;
pSyncNode
->
FpSendMsg
=
pSyncInfo
->
FpSendMsg
;
pSyncNode
->
queue
=
pSyncInfo
->
queue
;
pSyncNode
->
FpEqMsg
=
pSyncInfo
->
FpEqMsg
;
// init internal
pSyncNode
->
me
=
pSyncInfo
->
syncCfg
.
nodeInfo
[
pSyncInfo
->
syncCfg
.
myIndex
];
pSyncNode
->
peersNum
=
pSyncInfo
->
syncCfg
.
replicaNum
-
1
;
syncUtilnodeInfo2raftId
(
&
pSyncNode
->
me
,
pSyncInfo
->
vgId
,
&
pSyncNode
->
raftId
)
;
// init peersNum, peers, peersId
pSyncNode
->
peersNum
=
pSyncInfo
->
syncCfg
.
replicaNum
-
1
;
int
j
=
0
;
for
(
int
i
=
0
;
i
<
pSyncInfo
->
syncCfg
.
replicaNum
;
++
i
)
{
if
(
i
!=
pSyncInfo
->
syncCfg
.
myIndex
)
{
...
...
@@ -91,9 +97,37 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo) {
j
++
;
}
}
for
(
int
i
=
0
;
i
<
pSyncNode
->
peersNum
;
++
i
)
{
syncUtilnodeInfo2raftId
(
&
pSyncNode
->
peers
[
i
],
pSyncInfo
->
vgId
,
&
pSyncNode
->
peersId
[
i
]);
}
// init replicaNum, replicasId
pSyncNode
->
replicaNum
=
pSyncInfo
->
syncCfg
.
replicaNum
;
for
(
int
i
=
0
;
i
<
pSyncInfo
->
syncCfg
.
replicaNum
;
++
i
)
{
syncUtilnodeInfo2raftId
(
&
pSyncInfo
->
syncCfg
.
nodeInfo
[
i
],
pSyncInfo
->
vgId
,
&
pSyncNode
->
replicasId
[
i
]);
}
// raft algorithm
pSyncNode
->
pFsm
=
pSyncInfo
->
pFsm
;
pSyncNode
->
quorum
=
syncUtilQuorum
(
pSyncInfo
->
syncCfg
.
replicaNum
);
pSyncNode
->
leaderCache
=
EMPTY_RAFT_ID
;
// life cycle
// init server vars
pSyncNode
->
state
=
TAOS_SYNC_STATE_FOLLOWER
;
syncUtilnodeInfo2raftId
(
&
pSyncNode
->
me
,
pSyncNode
->
vgId
,
&
pSyncNode
->
raftId
);
pSyncNode
->
pRaftStore
=
raftStoreOpen
(
pSyncInfo
->
walPath
);
assert
(
pSyncNode
->
pRaftStore
!=
NULL
);
// init candidate vars
pSyncNode
->
pVotesGranted
=
voteGrantedCreate
(
pSyncNode
);
assert
(
pSyncNode
->
pVotesGranted
!=
NULL
);
pSyncNode
->
pVotesRespond
=
votesRespondCreate
(
pSyncNode
);
assert
(
pSyncNode
->
pVotesRespond
!=
NULL
);
// init leader vars
pSyncNode
->
pNextIndex
=
NULL
;
pSyncNode
->
pMatchIndex
=
NULL
;
// init ping timer
pSyncNode
->
pPingTimer
=
NULL
;
...
...
@@ -119,6 +153,7 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo) {
pSyncNode
->
FpHeartbeatTimer
=
syncNodeEqHeartbeatTimer
;
pSyncNode
->
heartbeatTimerCounter
=
0
;
// init callback
pSyncNode
->
FpOnPing
=
syncNodeOnPingCb
;
pSyncNode
->
FpOnPingReply
=
syncNodeOnPingReplyCb
;
pSyncNode
->
FpOnRequestVote
=
syncNodeOnRequestVoteCb
;
...
...
@@ -353,6 +388,8 @@ static void syncNodeEqElectTimer(void* param, void* tmrId) {
static
void
syncNodeEqHeartbeatTimer
(
void
*
param
,
void
*
tmrId
)
{}
static
void
UpdateTerm
(
SyncTerm
term
)
{}
static
void
syncNodeBecomeFollower
(
SSyncNode
*
pSyncNode
)
{
if
(
pSyncNode
->
state
==
TAOS_SYNC_STATE_LEADER
)
{
pSyncNode
->
leaderCache
=
EMPTY_RAFT_ID
;
...
...
source/libs/sync/src/syncUtil.c
浏览文件 @
264c30e5
...
...
@@ -97,4 +97,6 @@ void syncUtilbufCopyDeep(const SSyncBuffer* src, SSyncBuffer* dest) {
int32_t
syncUtilRand
(
int32_t
max
)
{
return
rand
()
%
max
;
}
int32_t
syncUtilElectRandomMS
()
{
ELECT_TIMER_MS_MIN
+
syncUtilRand
(
ELECT_TIMER_MS_RANGE
);
}
\ No newline at end of file
int32_t
syncUtilElectRandomMS
()
{
ELECT_TIMER_MS_MIN
+
syncUtilRand
(
ELECT_TIMER_MS_RANGE
);
}
int32_t
syncUtilQuorum
(
int32_t
replicaNum
)
{
return
replicaNum
/
2
+
1
;
}
\ No newline at end of file
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录