Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
7d897369
T
TDengine
项目概览
taosdata
/
TDengine
大约 2 年 前同步成功
通知
1192
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看板
提交
7d897369
编写于
3月 14, 2022
作者:
M
Minghao Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
sync index
上级
1a789177
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
220 addition
and
216 deletion
+220
-216
source/libs/sync/inc/syncInt.h
source/libs/sync/inc/syncInt.h
+16
-12
source/libs/sync/src/syncMain.c
source/libs/sync/src/syncMain.c
+204
-204
未找到文件。
source/libs/sync/inc/syncInt.h
浏览文件 @
7d897369
...
...
@@ -67,9 +67,6 @@ extern "C" {
} \
}
struct
SRaft
;
typedef
struct
SRaft
SRaft
;
struct
SyncTimeout
;
typedef
struct
SyncTimeout
SyncTimeout
;
...
...
@@ -117,6 +114,8 @@ typedef struct SSyncNode {
SSyncCfg
syncCfg
;
char
path
[
TSDB_FILENAME_LEN
];
char
raftStorePath
[
TSDB_FILENAME_LEN
*
2
];
// sync io
SWal
*
pWal
;
void
*
rpcClient
;
int32_t
(
*
FpSendMsg
)(
void
*
rpcClient
,
const
SEpSet
*
pEpSet
,
SRpcMsg
*
pMsg
);
...
...
@@ -164,7 +163,7 @@ typedef struct SSyncNode {
int32_t
pingTimerMS
;
uint64_t
pingTimerLogicClock
;
uint64_t
pingTimerLogicClockUser
;
TAOS_TMR_CALLBACK
FpPingTimer
;
// Timer Fp
TAOS_TMR_CALLBACK
FpPingTimer
CB
;
// Timer Fp
uint64_t
pingTimerCounter
;
// elect timer
...
...
@@ -172,7 +171,7 @@ typedef struct SSyncNode {
int32_t
electTimerMS
;
uint64_t
electTimerLogicClock
;
uint64_t
electTimerLogicClockUser
;
TAOS_TMR_CALLBACK
FpElectTimer
;
// Timer Fp
TAOS_TMR_CALLBACK
FpElectTimer
CB
;
// Timer Fp
uint64_t
electTimerCounter
;
// heartbeat timer
...
...
@@ -180,7 +179,7 @@ typedef struct SSyncNode {
int32_t
heartbeatTimerMS
;
uint64_t
heartbeatTimerLogicClock
;
uint64_t
heartbeatTimerLogicClockUser
;
TAOS_TMR_CALLBACK
FpHeartbeatTimer
;
// Timer Fp
TAOS_TMR_CALLBACK
FpHeartbeatTimer
CB
;
// Timer Fp
uint64_t
heartbeatTimerCounter
;
// callback
...
...
@@ -194,16 +193,17 @@ typedef struct SSyncNode {
}
SSyncNode
;
// open/close --------------
SSyncNode
*
syncNodeOpen
(
const
SSyncInfo
*
pSyncInfo
);
void
syncNodeClose
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodeSendMsgById
(
const
SRaftId
*
destRaftId
,
SSyncNode
*
pSyncNode
,
SRpcMsg
*
pMsg
);
int32_t
syncNodeSendMsgByInfo
(
const
SNodeInfo
*
nodeInfo
,
SSyncNode
*
pSyncNode
,
SRpcMsg
*
pMsg
);
// ping --------------
int32_t
syncNodePing
(
SSyncNode
*
pSyncNode
,
const
SRaftId
*
destRaftId
,
SyncPing
*
pMsg
);
int32_t
syncNodePingAll
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodePingPeers
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodePingSelf
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodePingPeers
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodePingAll
(
SSyncNode
*
pSyncNode
);
// timer control --------------
int32_t
syncNodeStartPingTimer
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodeStopPingTimer
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodeStartElectTimer
(
SSyncNode
*
pSyncNode
,
int32_t
ms
);
...
...
@@ -211,6 +211,10 @@ int32_t syncNodeStopElectTimer(SSyncNode* pSyncNode);
int32_t
syncNodeRestartElectTimer
(
SSyncNode
*
pSyncNode
,
int32_t
ms
);
int32_t
syncNodeStartHeartbeatTimer
(
SSyncNode
*
pSyncNode
);
int32_t
syncNodeStopHeartbeatTimer
(
SSyncNode
*
pSyncNode
);
// utils --------------
int32_t
syncNodeSendMsgById
(
const
SRaftId
*
destRaftId
,
SSyncNode
*
pSyncNode
,
SRpcMsg
*
pMsg
);
int32_t
syncNodeSendMsgByInfo
(
const
SNodeInfo
*
nodeInfo
,
SSyncNode
*
pSyncNode
,
SRpcMsg
*
pMsg
);
cJSON
*
syncNode2Json
(
const
SSyncNode
*
pSyncNode
);
char
*
syncNode2Str
(
const
SSyncNode
*
pSyncNode
);
...
...
source/libs/sync/src/syncMain.c
浏览文件 @
7d897369
...
...
@@ -31,13 +31,16 @@
static
int32_t
tsNodeRefId
=
-
1
;
// ------ local funciton ---------
// enqueue message ----
static
void
syncNodeEqPingTimer
(
void
*
param
,
void
*
tmrId
);
static
void
syncNodeEqElectTimer
(
void
*
param
,
void
*
tmrId
);
static
void
syncNodeEqHeartbeatTimer
(
void
*
param
,
void
*
tmrId
);
// on message ----
static
int32_t
syncNodeOnPingCb
(
SSyncNode
*
ths
,
SyncPing
*
pMsg
);
static
int32_t
syncNodeOnPingReplyCb
(
SSyncNode
*
ths
,
SyncPingReply
*
pMsg
);
// raft algorithm ----
static
void
UpdateTerm
(
SSyncNode
*
pSyncNode
,
SyncTerm
term
);
static
void
syncNodeBecomeFollower
(
SSyncNode
*
pSyncNode
);
static
void
syncNodeBecomeLeader
(
SSyncNode
*
pSyncNode
);
...
...
@@ -48,16 +51,20 @@ static void syncNodeCandidate2Follower(SSyncNode* pSyncNode);
// ---------------------------------
int32_t
syncInit
()
{
sTrace
(
"syncInit ok"
);
return
0
;
int32_t
ret
=
syncEnvStart
(
);
return
ret
;
}
void
syncCleanUp
()
{
sTrace
(
"syncCleanUp ok"
);
}
void
syncCleanUp
()
{
int32_t
ret
=
syncEnvStop
();
assert
(
ret
==
0
);
}
int64_t
syncStart
(
const
SSyncInfo
*
pSyncInfo
)
{
int32_t
ret
=
0
;
SSyncNode
*
pSyncNode
=
syncNodeOpen
(
pSyncInfo
);
assert
(
pSyncNode
!=
NULL
);
return
0
;
return
ret
;
}
void
syncStop
(
int64_t
rid
)
{
...
...
@@ -65,9 +72,13 @@ void syncStop(int64_t rid) {
syncNodeClose
(
pSyncNode
);
}
int32_t
syncReconfig
(
int64_t
rid
,
const
SSyncCfg
*
pSyncCfg
)
{
return
0
;
}
int32_t
syncReconfig
(
int64_t
rid
,
const
SSyncCfg
*
pSyncCfg
)
{
int32_t
ret
=
0
;
return
ret
;
}
int32_t
syncForwardToPeer
(
int64_t
rid
,
const
SRpcMsg
*
pMsg
,
bool
isWeak
)
{
int32_t
ret
=
0
;
SSyncNode
*
pSyncNode
=
NULL
;
// get pointer from rid
if
(
pSyncNode
->
state
==
TAOS_SYNC_STATE_LEADER
)
{
SyncClientRequest
*
pSyncMsg
=
syncClientRequestBuild2
(
pMsg
,
0
,
isWeak
);
...
...
@@ -75,11 +86,13 @@ int32_t syncForwardToPeer(int64_t rid, const SRpcMsg* pMsg, bool isWeak) {
syncClientRequest2RpcMsg
(
pSyncMsg
,
&
rpcMsg
);
pSyncNode
->
FpEqMsg
(
pSyncNode
->
queue
,
&
rpcMsg
);
syncClientRequestDestroy
(
pSyncMsg
);
ret
=
0
;
}
else
{
sTrace
(
"syncForwardToPeer not leader, %s"
,
syncUtilState2String
(
pSyncNode
->
state
));
ret
urn
-
1
;
// need define err code !!
ret
=
-
1
;
// need define err code !!
}
return
0
;
return
ret
;
}
ESyncState
syncGetMyRole
(
int64_t
rid
)
{
...
...
@@ -89,6 +102,7 @@ ESyncState syncGetMyRole(int64_t rid) {
void
syncGetNodesRole
(
int64_t
rid
,
SNodesRole
*
pNodeRole
)
{}
// open/close --------------
SSyncNode
*
syncNodeOpen
(
const
SSyncInfo
*
pSyncInfo
)
{
SSyncNode
*
pSyncNode
=
(
SSyncNode
*
)
malloc
(
sizeof
(
SSyncNode
));
assert
(
pSyncNode
!=
NULL
);
...
...
@@ -162,7 +176,7 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo) {
pSyncNode
->
pingTimerMS
=
PING_TIMER_MS
;
atomic_store_64
(
&
pSyncNode
->
pingTimerLogicClock
,
0
);
atomic_store_64
(
&
pSyncNode
->
pingTimerLogicClockUser
,
0
);
pSyncNode
->
FpPingTimer
=
syncNodeEqPingTimer
;
pSyncNode
->
FpPingTimer
CB
=
syncNodeEqPingTimer
;
pSyncNode
->
pingTimerCounter
=
0
;
// init elect timer
...
...
@@ -170,7 +184,7 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo) {
pSyncNode
->
electTimerMS
=
syncUtilElectRandomMS
();
atomic_store_64
(
&
pSyncNode
->
electTimerLogicClock
,
0
);
atomic_store_64
(
&
pSyncNode
->
electTimerLogicClockUser
,
0
);
pSyncNode
->
FpElectTimer
=
syncNodeEqElectTimer
;
pSyncNode
->
FpElectTimer
CB
=
syncNodeEqElectTimer
;
pSyncNode
->
electTimerCounter
=
0
;
// init heartbeat timer
...
...
@@ -178,7 +192,7 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo) {
pSyncNode
->
heartbeatTimerMS
=
HEARTBEAT_TIMER_MS
;
atomic_store_64
(
&
pSyncNode
->
heartbeatTimerLogicClock
,
0
);
atomic_store_64
(
&
pSyncNode
->
heartbeatTimerLogicClockUser
,
0
);
pSyncNode
->
FpHeartbeatTimer
=
syncNodeEqHeartbeatTimer
;
pSyncNode
->
FpHeartbeatTimer
CB
=
syncNodeEqHeartbeatTimer
;
pSyncNode
->
heartbeatTimerCounter
=
0
;
// init callback
...
...
@@ -194,10 +208,146 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo) {
}
void
syncNodeClose
(
SSyncNode
*
pSyncNode
)
{
int32_t
ret
;
assert
(
pSyncNode
!=
NULL
);
ret
=
raftStoreClose
(
pSyncNode
->
pRaftStore
);
assert
(
ret
==
0
);
voteGrantedDestroy
(
pSyncNode
->
pVotesGranted
);
votesRespondDestory
(
pSyncNode
->
pVotesRespond
);
syncIndexMgrDestroy
(
pSyncNode
->
pNextIndex
);
syncIndexMgrDestroy
(
pSyncNode
->
pMatchIndex
);
logStoreDestory
(
pSyncNode
->
pLogStore
);
syncNodeStopPingTimer
(
pSyncNode
);
syncNodeStopElectTimer
(
pSyncNode
);
syncNodeStopHeartbeatTimer
(
pSyncNode
);
free
(
pSyncNode
);
}
// ping --------------
int32_t
syncNodePing
(
SSyncNode
*
pSyncNode
,
const
SRaftId
*
destRaftId
,
SyncPing
*
pMsg
)
{
syncPingLog2
((
char
*
)
"==syncNodePing=="
,
pMsg
);
int32_t
ret
=
0
;
SRpcMsg
rpcMsg
;
syncPing2RpcMsg
(
pMsg
,
&
rpcMsg
);
syncRpcMsgLog2
((
char
*
)
"==syncNodePing=="
,
&
rpcMsg
);
ret
=
syncNodeSendMsgById
(
destRaftId
,
pSyncNode
,
&
rpcMsg
);
return
ret
;
}
int32_t
syncNodePingSelf
(
SSyncNode
*
pSyncNode
)
{
int32_t
ret
=
0
;
SyncPing
*
pMsg
=
syncPingBuild3
(
&
pSyncNode
->
myRaftId
,
&
pSyncNode
->
myRaftId
);
ret
=
syncNodePing
(
pSyncNode
,
&
pMsg
->
destId
,
pMsg
);
assert
(
ret
==
0
);
syncPingDestroy
(
pMsg
);
return
ret
;
}
int32_t
syncNodePingPeers
(
SSyncNode
*
pSyncNode
)
{
int32_t
ret
=
0
;
for
(
int
i
=
0
;
i
<
pSyncNode
->
peersNum
;
++
i
)
{
SRaftId
destId
;
syncUtilnodeInfo2raftId
(
&
pSyncNode
->
peersNodeInfo
[
i
],
pSyncNode
->
vgId
,
&
destId
);
SyncPing
*
pMsg
=
syncPingBuild3
(
&
pSyncNode
->
myRaftId
,
&
destId
);
ret
=
syncNodePing
(
pSyncNode
,
&
destId
,
pMsg
);
assert
(
ret
==
0
);
syncPingDestroy
(
pMsg
);
}
return
ret
;
}
int32_t
syncNodePingAll
(
SSyncNode
*
pSyncNode
)
{
int32_t
ret
=
0
;
for
(
int
i
=
0
;
i
<
pSyncNode
->
syncCfg
.
replicaNum
;
++
i
)
{
SRaftId
destId
;
syncUtilnodeInfo2raftId
(
&
pSyncNode
->
syncCfg
.
nodeInfo
[
i
],
pSyncNode
->
vgId
,
&
destId
);
SyncPing
*
pMsg
=
syncPingBuild3
(
&
pSyncNode
->
myRaftId
,
&
destId
);
ret
=
syncNodePing
(
pSyncNode
,
&
destId
,
pMsg
);
assert
(
ret
==
0
);
syncPingDestroy
(
pMsg
);
}
return
ret
;
}
// timer control --------------
int32_t
syncNodeStartPingTimer
(
SSyncNode
*
pSyncNode
)
{
int32_t
ret
=
0
;
taosTmrReset
(
pSyncNode
->
FpPingTimerCB
,
pSyncNode
->
pingTimerMS
,
pSyncNode
,
gSyncEnv
->
pTimerManager
,
&
pSyncNode
->
pPingTimer
);
atomic_store_64
(
&
pSyncNode
->
pingTimerLogicClock
,
pSyncNode
->
pingTimerLogicClockUser
);
return
ret
;
}
int32_t
syncNodeStopPingTimer
(
SSyncNode
*
pSyncNode
)
{
int32_t
ret
=
0
;
atomic_add_fetch_64
(
&
pSyncNode
->
pingTimerLogicClockUser
,
1
);
taosTmrStop
(
pSyncNode
->
pPingTimer
);
pSyncNode
->
pPingTimer
=
NULL
;
return
ret
;
}
int32_t
syncNodeStartElectTimer
(
SSyncNode
*
pSyncNode
,
int32_t
ms
)
{
int32_t
ret
=
0
;
pSyncNode
->
electTimerMS
=
ms
;
taosTmrReset
(
pSyncNode
->
FpElectTimerCB
,
pSyncNode
->
electTimerMS
,
pSyncNode
,
gSyncEnv
->
pTimerManager
,
&
pSyncNode
->
pElectTimer
);
atomic_store_64
(
&
pSyncNode
->
electTimerLogicClock
,
pSyncNode
->
electTimerLogicClockUser
);
return
ret
;
}
int32_t
syncNodeStopElectTimer
(
SSyncNode
*
pSyncNode
)
{
int32_t
ret
=
0
;
atomic_add_fetch_64
(
&
pSyncNode
->
electTimerLogicClockUser
,
1
);
taosTmrStop
(
pSyncNode
->
pElectTimer
);
pSyncNode
->
pElectTimer
=
NULL
;
return
ret
;
}
int32_t
syncNodeRestartElectTimer
(
SSyncNode
*
pSyncNode
,
int32_t
ms
)
{
int32_t
ret
=
0
;
syncNodeStopElectTimer
(
pSyncNode
);
syncNodeStartElectTimer
(
pSyncNode
,
ms
);
return
ret
;
}
int32_t
syncNodeStartHeartbeatTimer
(
SSyncNode
*
pSyncNode
)
{
int32_t
ret
=
0
;
taosTmrReset
(
pSyncNode
->
FpHeartbeatTimerCB
,
pSyncNode
->
heartbeatTimerMS
,
pSyncNode
,
gSyncEnv
->
pTimerManager
,
&
pSyncNode
->
pHeartbeatTimer
);
atomic_store_64
(
&
pSyncNode
->
heartbeatTimerLogicClock
,
pSyncNode
->
heartbeatTimerLogicClockUser
);
return
ret
;
}
int32_t
syncNodeStopHeartbeatTimer
(
SSyncNode
*
pSyncNode
)
{
int32_t
ret
=
0
;
atomic_add_fetch_64
(
&
pSyncNode
->
heartbeatTimerLogicClockUser
,
1
);
taosTmrStop
(
pSyncNode
->
pHeartbeatTimer
);
pSyncNode
->
pHeartbeatTimer
=
NULL
;
return
ret
;
}
// utils --------------
int32_t
syncNodeSendMsgById
(
const
SRaftId
*
destRaftId
,
SSyncNode
*
pSyncNode
,
SRpcMsg
*
pMsg
)
{
SEpSet
epSet
;
syncUtilraftId2EpSet
(
destRaftId
,
&
epSet
);
pSyncNode
->
FpSendMsg
(
pSyncNode
->
rpcClient
,
&
epSet
,
pMsg
);
return
0
;
}
int32_t
syncNodeSendMsgByInfo
(
const
SNodeInfo
*
nodeInfo
,
SSyncNode
*
pSyncNode
,
SRpcMsg
*
pMsg
)
{
SEpSet
epSet
;
syncUtilnodeInfo2EpSet
(
nodeInfo
,
&
epSet
);
pSyncNode
->
FpSendMsg
(
pSyncNode
->
rpcClient
,
&
epSet
,
pMsg
);
return
0
;
}
cJSON
*
syncNode2Json
(
const
SSyncNode
*
pSyncNode
)
{
char
u64buf
[
128
];
cJSON
*
pRoot
=
cJSON_CreateObject
();
...
...
@@ -253,12 +403,22 @@ cJSON* syncNode2Json(const SSyncNode* pSyncNode) {
// tla+ server vars
cJSON_AddNumberToObject
(
pRoot
,
"state"
,
pSyncNode
->
state
);
cJSON_AddStringToObject
(
pRoot
,
"state_str"
,
syncUtilState2String
(
pSyncNode
->
state
));
char
tmpBuf
[
RAFT_STORE_BLOCK_SIZE
];
raftStoreSerialize
(
pSyncNode
->
pRaftStore
,
tmpBuf
,
sizeof
(
tmpBuf
));
cJSON_AddStringToObject
(
pRoot
,
"pRaftStore"
,
tmpBuf
);
// tla+ candidate vars
cJSON_AddItemToObject
(
pRoot
,
"pVotesGranted"
,
voteGranted2Json
(
pSyncNode
->
pVotesGranted
));
cJSON_AddItemToObject
(
pRoot
,
"pVotesRespond"
,
votesRespond2Json
(
pSyncNode
->
pVotesRespond
));
// tla+ leader vars
cJSON_AddItemToObject
(
pRoot
,
"pNextIndex"
,
syncIndexMgr2Json
(
pSyncNode
->
pNextIndex
));
cJSON_AddItemToObject
(
pRoot
,
"pMatchIndex"
,
syncIndexMgr2Json
(
pSyncNode
->
pMatchIndex
));
// tla+ log vars
cJSON_AddItemToObject
(
pRoot
,
"pLogStore"
,
logStore2Json
(
pSyncNode
->
pLogStore
));
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%ld"
,
pSyncNode
->
commitIndex
);
cJSON_AddStringToObject
(
pRoot
,
"commitIndex"
,
u64buf
);
// ping timer
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pSyncNode
->
pPingTimer
);
...
...
@@ -268,8 +428,8 @@ cJSON* syncNode2Json(const SSyncNode* pSyncNode) {
cJSON_AddStringToObject
(
pRoot
,
"pingTimerLogicClock"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pSyncNode
->
pingTimerLogicClockUser
);
cJSON_AddStringToObject
(
pRoot
,
"pingTimerLogicClockUser"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pSyncNode
->
FpPingTimer
);
cJSON_AddStringToObject
(
pRoot
,
"FpPingTimer"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pSyncNode
->
FpPingTimer
CB
);
cJSON_AddStringToObject
(
pRoot
,
"FpPingTimer
CB
"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pSyncNode
->
pingTimerCounter
);
cJSON_AddStringToObject
(
pRoot
,
"pingTimerCounter"
,
u64buf
);
...
...
@@ -281,8 +441,8 @@ cJSON* syncNode2Json(const SSyncNode* pSyncNode) {
cJSON_AddStringToObject
(
pRoot
,
"electTimerLogicClock"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pSyncNode
->
electTimerLogicClockUser
);
cJSON_AddStringToObject
(
pRoot
,
"electTimerLogicClockUser"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pSyncNode
->
FpElectTimer
);
cJSON_AddStringToObject
(
pRoot
,
"FpElectTimer"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pSyncNode
->
FpElectTimer
CB
);
cJSON_AddStringToObject
(
pRoot
,
"FpElectTimer
CB
"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pSyncNode
->
electTimerCounter
);
cJSON_AddStringToObject
(
pRoot
,
"electTimerCounter"
,
u64buf
);
...
...
@@ -294,8 +454,8 @@ cJSON* syncNode2Json(const SSyncNode* pSyncNode) {
cJSON_AddStringToObject
(
pRoot
,
"heartbeatTimerLogicClock"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pSyncNode
->
heartbeatTimerLogicClockUser
);
cJSON_AddStringToObject
(
pRoot
,
"heartbeatTimerLogicClockUser"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pSyncNode
->
FpHeartbeatTimer
);
cJSON_AddStringToObject
(
pRoot
,
"FpHeartbeatTimer"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pSyncNode
->
FpHeartbeatTimer
CB
);
cJSON_AddStringToObject
(
pRoot
,
"FpHeartbeatTimer
CB
"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%lu"
,
pSyncNode
->
heartbeatTimerCounter
);
cJSON_AddStringToObject
(
pRoot
,
"heartbeatTimerCounter"
,
u64buf
);
...
...
@@ -327,143 +487,6 @@ char* syncNode2Str(const SSyncNode* pSyncNode) {
return
serialized
;
}
int32_t
syncNodeSendMsgById
(
const
SRaftId
*
destRaftId
,
SSyncNode
*
pSyncNode
,
SRpcMsg
*
pMsg
)
{
SEpSet
epSet
;
syncUtilraftId2EpSet
(
destRaftId
,
&
epSet
);
pSyncNode
->
FpSendMsg
(
pSyncNode
->
rpcClient
,
&
epSet
,
pMsg
);
return
0
;
}
int32_t
syncNodeSendMsgByInfo
(
const
SNodeInfo
*
nodeInfo
,
SSyncNode
*
pSyncNode
,
SRpcMsg
*
pMsg
)
{
SEpSet
epSet
;
syncUtilnodeInfo2EpSet
(
nodeInfo
,
&
epSet
);
pSyncNode
->
FpSendMsg
(
pSyncNode
->
rpcClient
,
&
epSet
,
pMsg
);
return
0
;
}
int32_t
syncNodePing
(
SSyncNode
*
pSyncNode
,
const
SRaftId
*
destRaftId
,
SyncPing
*
pMsg
)
{
sTrace
(
"syncNodePing pSyncNode:%p "
,
pSyncNode
);
int32_t
ret
=
0
;
SRpcMsg
rpcMsg
;
syncPing2RpcMsg
(
pMsg
,
&
rpcMsg
);
syncNodeSendMsgById
(
destRaftId
,
pSyncNode
,
&
rpcMsg
);
{
cJSON
*
pJson
=
syncPing2Json
(
pMsg
);
char
*
serialized
=
cJSON_Print
(
pJson
);
sTrace
(
"syncNodePing pMsg:%s "
,
serialized
);
free
(
serialized
);
cJSON_Delete
(
pJson
);
}
{
SyncPing
*
pMsg2
=
rpcMsg
.
pCont
;
cJSON
*
pJson
=
syncPing2Json
(
pMsg2
);
char
*
serialized
=
cJSON_Print
(
pJson
);
sTrace
(
"syncNodePing rpcMsg.pCont:%s "
,
serialized
);
free
(
serialized
);
cJSON_Delete
(
pJson
);
}
return
ret
;
}
int32_t
syncNodePingAll
(
SSyncNode
*
pSyncNode
)
{
sTrace
(
"syncNodePingAll pSyncNode:%p "
,
pSyncNode
);
int32_t
ret
=
0
;
for
(
int
i
=
0
;
i
<
pSyncNode
->
syncCfg
.
replicaNum
;
++
i
)
{
SRaftId
destId
;
syncUtilnodeInfo2raftId
(
&
pSyncNode
->
syncCfg
.
nodeInfo
[
i
],
pSyncNode
->
vgId
,
&
destId
);
SyncPing
*
pMsg
=
syncPingBuild3
(
&
pSyncNode
->
myRaftId
,
&
destId
);
ret
=
syncNodePing
(
pSyncNode
,
&
destId
,
pMsg
);
assert
(
ret
==
0
);
syncPingDestroy
(
pMsg
);
}
}
int32_t
syncNodePingPeers
(
SSyncNode
*
pSyncNode
)
{
int32_t
ret
=
0
;
for
(
int
i
=
0
;
i
<
pSyncNode
->
peersNum
;
++
i
)
{
SRaftId
destId
;
syncUtilnodeInfo2raftId
(
&
pSyncNode
->
peersNodeInfo
[
i
],
pSyncNode
->
vgId
,
&
destId
);
SyncPing
*
pMsg
=
syncPingBuild3
(
&
pSyncNode
->
myRaftId
,
&
destId
);
ret
=
syncNodePing
(
pSyncNode
,
&
destId
,
pMsg
);
assert
(
ret
==
0
);
syncPingDestroy
(
pMsg
);
}
}
int32_t
syncNodePingSelf
(
SSyncNode
*
pSyncNode
)
{
int32_t
ret
;
SyncPing
*
pMsg
=
syncPingBuild3
(
&
pSyncNode
->
myRaftId
,
&
pSyncNode
->
myRaftId
);
ret
=
syncNodePing
(
pSyncNode
,
&
pMsg
->
destId
,
pMsg
);
assert
(
ret
==
0
);
syncPingDestroy
(
pMsg
);
}
int32_t
syncNodeStartPingTimer
(
SSyncNode
*
pSyncNode
)
{
atomic_store_64
(
&
pSyncNode
->
pingTimerLogicClock
,
pSyncNode
->
pingTimerLogicClockUser
);
pSyncNode
->
pingTimerMS
=
PING_TIMER_MS
;
if
(
pSyncNode
->
pPingTimer
==
NULL
)
{
pSyncNode
->
pPingTimer
=
taosTmrStart
(
pSyncNode
->
FpPingTimer
,
pSyncNode
->
pingTimerMS
,
pSyncNode
,
gSyncEnv
->
pTimerManager
);
}
else
{
taosTmrReset
(
pSyncNode
->
FpPingTimer
,
pSyncNode
->
pingTimerMS
,
pSyncNode
,
gSyncEnv
->
pTimerManager
,
&
pSyncNode
->
pPingTimer
);
}
return
0
;
}
int32_t
syncNodeStopPingTimer
(
SSyncNode
*
pSyncNode
)
{
atomic_add_fetch_64
(
&
pSyncNode
->
pingTimerLogicClockUser
,
1
);
pSyncNode
->
pingTimerMS
=
TIMER_MAX_MS
;
return
0
;
}
int32_t
syncNodeStartElectTimer
(
SSyncNode
*
pSyncNode
,
int32_t
ms
)
{
pSyncNode
->
electTimerMS
=
ms
;
atomic_store_64
(
&
pSyncNode
->
electTimerLogicClock
,
pSyncNode
->
electTimerLogicClockUser
);
if
(
pSyncNode
->
pElectTimer
==
NULL
)
{
pSyncNode
->
pElectTimer
=
taosTmrStart
(
pSyncNode
->
FpElectTimer
,
pSyncNode
->
electTimerMS
,
pSyncNode
,
gSyncEnv
->
pTimerManager
);
}
else
{
taosTmrReset
(
pSyncNode
->
FpElectTimer
,
pSyncNode
->
electTimerMS
,
pSyncNode
,
gSyncEnv
->
pTimerManager
,
&
pSyncNode
->
pElectTimer
);
}
return
0
;
}
int32_t
syncNodeStopElectTimer
(
SSyncNode
*
pSyncNode
)
{
atomic_add_fetch_64
(
&
pSyncNode
->
electTimerLogicClockUser
,
1
);
pSyncNode
->
electTimerMS
=
TIMER_MAX_MS
;
return
0
;
}
int32_t
syncNodeRestartElectTimer
(
SSyncNode
*
pSyncNode
,
int32_t
ms
)
{
syncNodeStopElectTimer
(
pSyncNode
);
syncNodeStartElectTimer
(
pSyncNode
,
ms
);
return
0
;
}
int32_t
syncNodeStartHeartbeatTimer
(
SSyncNode
*
pSyncNode
)
{
atomic_store_64
(
&
pSyncNode
->
heartbeatTimerLogicClock
,
pSyncNode
->
heartbeatTimerLogicClockUser
);
if
(
pSyncNode
->
pHeartbeatTimer
==
NULL
)
{
pSyncNode
->
pHeartbeatTimer
=
taosTmrStart
(
pSyncNode
->
FpHeartbeatTimer
,
pSyncNode
->
heartbeatTimerMS
,
pSyncNode
,
gSyncEnv
->
pTimerManager
);
}
else
{
taosTmrReset
(
pSyncNode
->
FpHeartbeatTimer
,
pSyncNode
->
heartbeatTimerMS
,
pSyncNode
,
gSyncEnv
->
pTimerManager
,
&
pSyncNode
->
pHeartbeatTimer
);
}
return
0
;
}
int32_t
syncNodeStopHeartbeatTimer
(
SSyncNode
*
pSyncNode
)
{
atomic_add_fetch_64
(
&
pSyncNode
->
heartbeatTimerLogicClockUser
,
1
);
pSyncNode
->
heartbeatTimerMS
=
TIMER_MAX_MS
;
return
0
;
}
// for debug --------------
void
syncNodePrint
(
SSyncNode
*
pObj
)
{
char
*
serialized
=
syncNode2Str
(
pObj
);
...
...
@@ -492,61 +515,23 @@ void syncNodeLog2(char* s, SSyncNode* pObj) {
}
// ------ local funciton ---------
static
int32_t
syncNodeOnPingCb
(
SSyncNode
*
ths
,
SyncPing
*
pMsg
)
{
int32_t
ret
=
0
;
sTrace
(
"<-- syncNodeOnPingCb -->"
);
{
cJSON
*
pJson
=
syncPing2Json
(
pMsg
);
char
*
serialized
=
cJSON_Print
(
pJson
);
sTrace
(
"process syncMessage recv: syncNodeOnPingCb pMsg:%s "
,
serialized
);
free
(
serialized
);
cJSON_Delete
(
pJson
);
}
SyncPingReply
*
pMsgReply
=
syncPingReplyBuild3
(
&
ths
->
myRaftId
,
&
pMsg
->
srcId
);
SRpcMsg
rpcMsg
;
syncPingReply2RpcMsg
(
pMsgReply
,
&
rpcMsg
);
syncNodeSendMsgById
(
&
pMsgReply
->
destId
,
ths
,
&
rpcMsg
);
return
ret
;
}
static
int32_t
syncNodeOnPingReplyCb
(
SSyncNode
*
ths
,
SyncPingReply
*
pMsg
)
{
int32_t
ret
=
0
;
sTrace
(
"<-- syncNodeOnPingReplyCb -->"
);
{
cJSON
*
pJson
=
syncPingReply2Json
(
pMsg
);
char
*
serialized
=
cJSON_Print
(
pJson
);
sTrace
(
"process syncMessage recv: syncNodeOnPingReplyCb pMsg:%s "
,
serialized
);
free
(
serialized
);
cJSON_Delete
(
pJson
);
}
return
ret
;
}
// enqueue message ----
static
void
syncNodeEqPingTimer
(
void
*
param
,
void
*
tmrId
)
{
sTrace
(
"<-- syncNodeEqPingTimer -->"
);
SSyncNode
*
pSyncNode
=
(
SSyncNode
*
)
param
;
if
(
atomic_load_64
(
&
pSyncNode
->
pingTimerLogicClockUser
)
<=
atomic_load_64
(
&
pSyncNode
->
pingTimerLogicClock
))
{
SyncTimeout
*
pSyncMsg
=
syncTimeoutBuild2
(
SYNC_TIMEOUT_PING
,
atomic_load_64
(
&
pSyncNode
->
pingTimerLogicClock
),
pSyncNode
->
pingTimerMS
,
pSyncNode
);
SRpcMsg
rpcMsg
;
syncTimeout2RpcMsg
(
pSyncMsg
,
&
rpcMsg
);
syncRpcMsgLog2
((
char
*
)
"==syncNodeEqPingTimer=="
,
&
rpcMsg
);
pSyncNode
->
FpEqMsg
(
pSyncNode
->
queue
,
&
rpcMsg
);
syncTimeoutDestroy
(
pSyncMsg
);
// reset timer ms
// pSyncNode->pingTimerMS += 100;
taosTmrReset
(
syncNodeEqPingTimer
,
pSyncNode
->
pingTimerMS
,
pSyncNode
,
gSyncEnv
->
pTimerManager
,
&
pSyncNode
->
pPingTimer
);
}
else
{
sTrace
(
"
syncNodeEqPingTimer: pingTimerLogicClock:%lu, pingTimerLogicClockUser:%lu"
,
pSyncNode
->
pingTimerLogicClock
,
pSyncNode
->
pingTimerLogicClockUser
);
sTrace
(
"
==syncNodeEqPingTimer== pingTimerLogicClock:%lu, pingTimerLogicClockUser:%lu"
,
pSyncNode
->
pingTimerLogicClock
,
pSyncNode
->
pingTimerLogicClock
User
);
}
}
...
...
@@ -555,19 +540,18 @@ static void syncNodeEqElectTimer(void* param, void* tmrId) {
if
(
atomic_load_64
(
&
pSyncNode
->
electTimerLogicClockUser
)
<=
atomic_load_64
(
&
pSyncNode
->
electTimerLogicClock
))
{
SyncTimeout
*
pSyncMsg
=
syncTimeoutBuild2
(
SYNC_TIMEOUT_ELECTION
,
atomic_load_64
(
&
pSyncNode
->
electTimerLogicClock
),
pSyncNode
->
electTimerMS
,
pSyncNode
);
SRpcMsg
rpcMsg
;
syncTimeout2RpcMsg
(
pSyncMsg
,
&
rpcMsg
);
syncRpcMsgLog2
((
char
*
)
"==syncNodeEqElectTimer=="
,
&
rpcMsg
);
pSyncNode
->
FpEqMsg
(
pSyncNode
->
queue
,
&
rpcMsg
);
syncTimeoutDestroy
(
pSyncMsg
);
// reset timer ms
pSyncNode
->
electTimerMS
=
syncUtilElectRandomMS
();
taosTmrReset
(
syncNodeEqPingTimer
,
pSyncNode
->
pingTimerMS
,
pSyncNode
,
gSyncEnv
->
pTimerManager
,
&
pSyncNode
->
pPingTimer
);
}
else
{
sTrace
(
"
syncNodeEqElectTimer:
electTimerLogicClock:%lu, electTimerLogicClockUser:%lu"
,
sTrace
(
"
==syncNodeEqElectTimer==
electTimerLogicClock:%lu, electTimerLogicClockUser:%lu"
,
pSyncNode
->
electTimerLogicClock
,
pSyncNode
->
electTimerLogicClockUser
);
}
}
...
...
@@ -579,23 +563,39 @@ static void syncNodeEqHeartbeatTimer(void* param, void* tmrId) {
SyncTimeout
*
pSyncMsg
=
syncTimeoutBuild2
(
SYNC_TIMEOUT_HEARTBEAT
,
atomic_load_64
(
&
pSyncNode
->
heartbeatTimerLogicClock
),
pSyncNode
->
heartbeatTimerMS
,
pSyncNode
);
SRpcMsg
rpcMsg
;
syncTimeout2RpcMsg
(
pSyncMsg
,
&
rpcMsg
);
syncRpcMsgLog2
((
char
*
)
"==syncNodeEqHeartbeatTimer=="
,
&
rpcMsg
);
pSyncNode
->
FpEqMsg
(
pSyncNode
->
queue
,
&
rpcMsg
);
syncTimeoutDestroy
(
pSyncMsg
);
// reset timer ms
// pSyncNode->heartbeatTimerMS += 100;
taosTmrReset
(
syncNodeEqHeartbeatTimer
,
pSyncNode
->
heartbeatTimerMS
,
pSyncNode
,
gSyncEnv
->
pTimerManager
,
&
pSyncNode
->
pHeartbeatTimer
);
}
else
{
sTrace
(
"
syncNodeEqHeartbeatTimer:
heartbeatTimerLogicClock:%lu, heartbeatTimerLogicClockUser:%lu"
,
sTrace
(
"
==syncNodeEqHeartbeatTimer==
heartbeatTimerLogicClock:%lu, heartbeatTimerLogicClockUser:%lu"
,
pSyncNode
->
heartbeatTimerLogicClock
,
pSyncNode
->
heartbeatTimerLogicClockUser
);
}
}
// on message ----
static
int32_t
syncNodeOnPingCb
(
SSyncNode
*
ths
,
SyncPing
*
pMsg
)
{
int32_t
ret
=
0
;
syncPingLog2
(
"==syncNodeOnPingCb=="
,
pMsg
);
SyncPingReply
*
pMsgReply
=
syncPingReplyBuild3
(
&
ths
->
myRaftId
,
&
pMsg
->
srcId
);
SRpcMsg
rpcMsg
;
syncPingReply2RpcMsg
(
pMsgReply
,
&
rpcMsg
);
syncNodeSendMsgById
(
&
pMsgReply
->
destId
,
ths
,
&
rpcMsg
);
return
ret
;
}
static
int32_t
syncNodeOnPingReplyCb
(
SSyncNode
*
ths
,
SyncPingReply
*
pMsg
)
{
int32_t
ret
=
0
;
syncPingReplyLog2
(
"==syncNodeOnPingReplyCb=="
,
pMsg
);
return
ret
;
}
// raft algorithm ----
static
void
UpdateTerm
(
SSyncNode
*
pSyncNode
,
SyncTerm
term
)
{
if
(
term
>
pSyncNode
->
pRaftStore
->
currentTerm
)
{
pSyncNode
->
pRaftStore
->
currentTerm
=
term
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录