Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
c319d1cb
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看板
提交
c319d1cb
编写于
10月 29, 2021
作者:
L
lichuang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
[TD-10645][raft]<feature>add raft module
上级
2dc480ad
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
206 addition
and
4 deletion
+206
-4
include/libs/sync/sync.h
include/libs/sync/sync.h
+6
-1
source/libs/sync/inc/raft.h
source/libs/sync/inc/raft.h
+11
-1
source/libs/sync/inc/raft_message.h
source/libs/sync/inc/raft_message.h
+76
-0
source/libs/sync/inc/syncInt.h
source/libs/sync/inc/syncInt.h
+3
-0
source/libs/sync/src/raft.c
source/libs/sync/src/raft.c
+74
-0
source/libs/sync/src/raft_message.c
source/libs/sync/src/raft_message.c
+17
-0
source/libs/sync/src/sync.c
source/libs/sync/src/sync.c
+19
-2
未找到文件。
include/libs/sync/sync.h
浏览文件 @
c319d1cb
...
...
@@ -89,6 +89,10 @@ typedef struct SSyncLogStore {
// write log with given index
int32_t
(
*
logWrite
)(
struct
SSyncLogStore
*
logStore
,
SyncIndex
index
,
SSyncBuffer
*
pBuf
);
// read log from given index with limit, return the actual num in nBuf
int32_t
(
*
logRead
)(
struct
SSyncLogStore
*
logStore
,
SyncIndex
index
,
int
limit
,
SSyncBuffer
*
pBuf
,
int
*
nBuf
);
// mark log with given index has been commtted
int32_t
(
*
logCommit
)(
struct
SSyncLogStore
*
logStore
,
SyncIndex
index
);
...
...
@@ -102,6 +106,7 @@ typedef struct SSyncLogStore {
typedef
struct
SSyncServerState
{
SyncNodeId
voteFor
;
SSyncTerm
term
;
SyncIndex
commitIndex
;
}
SSyncServerState
;
typedef
struct
SSyncClusterConfig
{
...
...
@@ -146,7 +151,7 @@ SSyncNode* syncStart(const SSyncInfo*);
void
syncReconfig
(
const
SSyncNode
*
,
const
SSyncCluster
*
);
void
syncStop
(
const
SSyncNode
*
);
int32_t
syncPropose
(
SSyncNode
*
syncNode
,
SSyncBuffer
buffer
,
void
*
pData
,
bool
isWeak
);
int32_t
syncPropose
(
SSyncNode
*
syncNode
,
const
SSyncBuffer
*
pBuf
,
void
*
pData
,
bool
isWeak
);
// int32_t syncAddNode(SSyncNode syncNode, const SNodeInfo *pNode);
...
...
source/libs/sync/inc/raft.h
浏览文件 @
c319d1cb
...
...
@@ -16,8 +16,18 @@
#ifndef _TD_LIBS_SYNC_RAFT_H
#define _TD_LIBS_SYNC_RAFT_H
#include "sync.h"
#include "raft_message.h"
typedef
struct
SSyncRaft
{
// owner sync node
SSyncNode
*
pNode
;
SSyncInfo
info
;
}
SSyncRaft
;
int32_t
syncRaftStart
(
SSyncRaft
*
pRaft
,
const
SSyncInfo
*
pInfo
);
int32_t
syncRaftStep
(
SSyncRaft
*
pRaft
,
const
RaftMessage
*
pMsg
);
#endif
/* _TD_LIBS_SYNC_RAFT_H */
\ No newline at end of file
source/libs/sync/inc/raft_message.h
0 → 100644
浏览文件 @
c319d1cb
/*
* Copyright (c) 2019 TAOS Data, Inc. <cli@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _TD_LIBS_SYNC_RAFT_MESSAGE_H
#define _TD_LIBS_SYNC_RAFT_MESSAGE_H
#include "sync.h"
/**
* below define message type which handled by Raft node thread
* internal message, which communicate in threads, start with RAFT_MSG_INTERNAL_*,
* internal message use pointer only, need not to be decode/encode
* outter message start with RAFT_MSG_*, need to implement its decode/encode functions
**/
typedef
enum
RaftMessageType
{
// client propose a cmd
RAFT_MSG_INTERNAL_PROP
=
1
,
RAFT_MSG_APPEND
,
RAFT_MSG_APPEND_RESP
,
RAFT_MSG_VOTE
,
RAFT_MSG_VOTE_RESP
,
RAFT_MSG_PRE_VOTE
,
RAFT_MSG_PRE_VOTE_RESP
,
}
RaftMessageType
;
typedef
struct
RaftMsgInternal_Prop
{
const
SSyncBuffer
*
pBuf
;
bool
isWeak
;
void
*
pData
;
}
RaftMsgInternal_Prop
;
typedef
struct
RaftMessage
{
RaftMessageType
msgType
;
SSyncTerm
term
;
SyncNodeId
from
;
SyncNodeId
to
;
union
{
RaftMsgInternal_Prop
propose
;
};
}
RaftMessage
;
static
FORCE_INLINE
RaftMessage
*
syncInitPropMsg
(
RaftMessage
*
pMsg
,
const
SSyncBuffer
*
pBuf
,
void
*
pData
,
bool
isWeak
)
{
*
pMsg
=
(
RaftMessage
)
{
.
msgType
=
RAFT_MSG_INTERNAL_PROP
,
.
propose
=
(
RaftMsgInternal_Prop
)
{
.
isWeak
=
isWeak
,
.
pBuf
=
pBuf
,
.
pData
=
pData
,
},
};
return
pMsg
;
}
static
FORCE_INLINE
bool
syncIsInternalMsg
(
const
RaftMessage
*
pMsg
)
{
return
pMsg
->
msgType
==
RAFT_MSG_INTERNAL_PROP
;
}
#endif
/* _TD_LIBS_SYNC_RAFT_MESSAGE_H */
\ No newline at end of file
source/libs/sync/inc/syncInt.h
浏览文件 @
c319d1cb
...
...
@@ -40,6 +40,9 @@ typedef struct SSyncManager {
// worker threads
SSyncWorker
worker
[
TAOS_SYNC_MAX_WORKER
];
// sync net worker
SSyncWorker
netWorker
;
// vgroup hash table
SHashObj
*
vgroupTable
;
...
...
source/libs/sync/src/raft.c
0 → 100644
浏览文件 @
c319d1cb
/*
* Copyright (c) 2019 TAOS Data, Inc. <cli@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "raft.h"
#include "syncInt.h"
#ifndef MIN
#define MIN(x, y) (((x) < (y)) ? (x) : (y))
#endif
#define RAFT_READ_LOG_MAX_NUM 100
int32_t
syncRaftStart
(
SSyncRaft
*
pRaft
,
const
SSyncInfo
*
pInfo
)
{
SSyncNode
*
pNode
=
pRaft
->
pNode
;
SSyncServerState
serverState
;
SStateManager
*
stateManager
;
SSyncLogStore
*
logStore
;
SSyncFSM
*
fsm
;
SyncIndex
initIndex
=
pInfo
->
snapshotIndex
;
SSyncBuffer
buffer
[
RAFT_READ_LOG_MAX_NUM
];
int
nBuf
,
limit
,
i
;
memcpy
(
&
pRaft
->
info
,
pInfo
,
sizeof
(
SSyncInfo
));
stateManager
=
&
(
pRaft
->
info
.
stateManager
);
logStore
=
&
(
pRaft
->
info
.
logStore
);
fsm
=
&
(
pRaft
->
info
.
fsm
);
// read server state
if
(
stateManager
->
readServerState
(
stateManager
,
&
serverState
)
!=
0
)
{
syncError
(
"readServerState for vgid %d fail"
,
pInfo
->
vgId
);
return
-
1
;
}
assert
(
initIndex
<=
serverState
.
commitIndex
);
// restore fsm state from snapshot index + 1, until commitIndex
++
initIndex
;
while
(
initIndex
<
serverState
.
commitIndex
)
{
limit
=
MIN
(
RAFT_READ_LOG_MAX_NUM
,
serverState
.
commitIndex
-
initIndex
);
if
(
logStore
->
logRead
(
logStore
,
initIndex
,
limit
,
buffer
,
&
nBuf
)
!=
0
)
{
return
-
1
;
}
assert
(
limit
==
nBuf
);
for
(
i
=
0
;
i
<
limit
;
++
i
)
{
fsm
->
applyLog
(
fsm
,
initIndex
+
i
,
&
(
buffer
[
i
]),
NULL
);
free
(
buffer
[
i
].
data
);
}
initIndex
+=
nBuf
;
}
assert
(
initIndex
==
serverState
.
commitIndex
);
syncInfo
(
"restore vgid %d state: snapshot index:"
,
pInfo
->
vgId
);
return
0
;
}
int32_t
syncRaftStep
(
SSyncRaft
*
pRaft
,
const
RaftMessage
*
pMsg
)
{
if
(
!
syncIsInternalMsg
(
pMsg
))
{
free
(
pMsg
);
}
return
0
;
}
\ No newline at end of file
source/libs/sync/src/raft_message.c
0 → 100644
浏览文件 @
c319d1cb
/*
* Copyright (c) 2019 TAOS Data, Inc. <cli@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "raft_message.h"
source/libs/sync/src/sync.c
浏览文件 @
c319d1cb
...
...
@@ -75,7 +75,15 @@ SSyncNode* syncStart(const SSyncInfo* pInfo) {
SSyncNode
*
pNode
=
(
SSyncNode
*
)
malloc
(
sizeof
(
SSyncNode
));
if
(
pNode
==
NULL
)
{
syncInfo
(
"malloc vgroup %d node fail"
,
pInfo
->
vgId
);
syncError
(
"malloc vgroup %d node fail"
,
pInfo
->
vgId
);
pthread_mutex_unlock
(
&
gSyncManager
->
mutex
);
return
NULL
;
}
// start raft
pNode
->
raft
.
pNode
=
pNode
;
if
(
syncRaftStart
(
&
pNode
->
raft
,
pInfo
)
!=
0
)
{
syncError
(
"raft start at %d node fail"
,
pInfo
->
vgId
);
pthread_mutex_unlock
(
&
gSyncManager
->
mutex
);
return
NULL
;
}
...
...
@@ -102,10 +110,19 @@ void syncStop(const SSyncNode* pNode) {
taosHashRemove
(
gSyncManager
->
vgroupTable
,
&
pNode
->
vgId
,
sizeof
(
SyncGroupId
));
pthread_mutex_unlock
(
&
gSyncManager
->
mutex
);
pthread_mutex_destroy
(
&
pNode
->
mutex
);
pthread_mutex_destroy
(
&
((
*
ppNode
)
->
mutex
)
);
free
(
*
ppNode
);
}
int32_t
syncPropose
(
SSyncNode
*
syncNode
,
const
SSyncBuffer
*
pBuf
,
void
*
pData
,
bool
isWeak
)
{
RaftMessage
msg
;
pthread_mutex_lock
(
&
syncNode
->
mutex
);
int32_t
ret
=
syncRaftStep
(
&
syncNode
->
raft
,
syncInitPropMsg
(
&
msg
,
pBuf
,
pData
,
isWeak
));
pthread_mutex_unlock
(
&
syncNode
->
mutex
);
return
ret
;
}
void
syncReconfig
(
const
SSyncNode
*
pNode
,
const
SSyncCluster
*
pCfg
)
{}
static
int
syncOpenWorkerPool
(
SSyncManager
*
syncManager
)
{
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录