Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
6dc92d70
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看板
提交
6dc92d70
编写于
12月 22, 2021
作者:
M
Minghao Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add raft code
上级
82f89875
变更
5
隐藏空白更改
内联
并排
Showing
5 changed file
with
105 addition
and
17 deletion
+105
-17
contrib/test/craft/clear.sh
contrib/test/craft/clear.sh
+1
-0
contrib/test/craft/common.h
contrib/test/craft/common.h
+17
-0
contrib/test/craft/raftMain.c
contrib/test/craft/raftMain.c
+15
-10
contrib/test/craft/raftServer.c
contrib/test/craft/raftServer.c
+63
-2
contrib/test/craft/raftServer.h
contrib/test/craft/raftServer.h
+9
-5
未找到文件。
contrib/test/craft/clear.sh
浏览文件 @
6dc92d70
#!/bin/bash
rm
-rf
127.0.0.1
*
rm
-rf
./data
contrib/test/craft/common.h
0 → 100644
浏览文件 @
6dc92d70
#ifndef TDENGINE_COMMON_H
#define TDENGINE_COMMON_H
#ifdef __cplusplus
extern
"C"
{
#endif
#define COMMAND_LEN 256
#define DIR_LEN 128
#define HOST_LEN 128
#define ADDRESS_LEN (HOST_LEN + 16)
#ifdef __cplusplus
}
#endif
#endif // TDENGINE_COMMON_H
contrib/test/craft/raftMain.c
浏览文件 @
6dc92d70
#include <stdio.h>
#include <raft.h>
#include <raft/uv.h>
#include <unistd.h>
#include <pthread.h>
#include <string.h>
...
...
@@ -8,9 +6,10 @@
#include <time.h>
#include <stdlib.h>
#include <getopt.h>
#include <raft.h>
#include <raft/uv.h>
#include "raftServer.h"
#define COMMAND_LEN 128
#include "common.h"
const
char
*
exe_name
;
...
...
@@ -52,8 +51,6 @@ void *startConsoleFunc(void *param) {
}
// Config ---------------------------------
#define DIR_LEN 128
#define HOST_LEN 128
typedef
struct
SRaftServerConfig
{
char
host
[
HOST_LEN
];
uint32_t
port
;
...
...
@@ -61,16 +58,22 @@ typedef struct SRaftServerConfig {
}
SRaftServerConfig
;
void
parseConf
(
int
argc
,
char
**
argv
,
SRaftServerConfig
*
pConf
)
{
snprintf
(
pConf
->
dir
,
sizeof
(
pConf
->
dir
),
"%s"
,
argv
[
1
]);
snprintf
(
pConf
->
host
,
sizeof
(
pConf
->
host
),
"%s"
,
argv
[
1
]);
sscanf
(
argv
[
2
],
"%u"
,
&
pConf
->
port
);
snprintf
(
pConf
->
dir
,
sizeof
(
pConf
->
dir
),
"%s"
,
argv
[
3
]);
}
void
printConf
(
SRaftServerConfig
*
pConf
)
{
printf
(
"conf: %s:%u %s
\n
"
,
pConf
->
host
,
pConf
->
port
,
pConf
->
dir
);
}
// -----------------------------------------
void
usage
()
{
printf
(
"
\n
"
);
printf
(
"usage: %s host port dir
\n
"
,
exe_name
);
printf
(
"eg : %s 127.0.0.1 10000 ./data
\n
"
,
exe_name
);
printf
(
"
\n
"
);
printf
(
"eg:
\n
"
);
printf
(
"%s 127.0.0.1 10000 ./data
\n
"
,
exe_name
);
printf
(
"
\n
"
);
}
...
...
@@ -86,12 +89,14 @@ int main(int argc, char **argv) {
SRaftServerConfig
conf
;
parseConf
(
argc
,
argv
,
&
conf
);
printConf
(
&
conf
);
struct
raft_fsm
fsm
;
initFsm
(
&
fsm
);
SRaftServer
raftServer
;
ret
=
raftServerInit
(
&
raftServer
,
&
conf
,
&
fsm
);
ret
=
raftServerInit
(
&
raftServer
,
conf
.
host
,
conf
.
port
,
conf
.
dir
,
&
fsm
);
assert
(
ret
==
0
);
pthread_t
tidRaftServer
;
pthread_create
(
&
tidRaftServer
,
NULL
,
startServerFunc
,
&
raftServer
);
...
...
contrib/test/craft/raftServer.c
浏览文件 @
6dc92d70
#include <stdlib.h>
#include "common.h"
#include "raftServer.h"
int32_t
raftServerInit
(
SRaftServer
*
pRaftServer
,
struct
SRaftServerConfig
*
pConf
,
struct
raft_fsm
*
pFsm
)
{
uint64_t
raftId
(
const
char
*
host
,
uint32_t
port
)
{
uint32_t
host_uint32
=
(
uint32_t
)
inet_addr
(
host
);
assert
(
host_uint32
!=
(
uint32_t
)
-
1
);
uint64_t
code
=
((
uint64_t
)
host_uint32
)
<<
32
|
port
;
return
code
;
}
int32_t
raftServerInit
(
SRaftServer
*
pRaftServer
,
const
char
*
host
,
uint32_t
port
,
const
char
*
dir
,
struct
raft_fsm
*
pFsm
)
{
int
ret
;
char
cmd_buf
[
COMMAND_LEN
];
snprintf
(
cmd_buf
,
sizeof
(
cmd_buf
),
"mkdir -p %s"
,
dir
);
system
(
cmd_buf
);
snprintf
(
pRaftServer
->
host
,
sizeof
(
pRaftServer
->
host
),
"%s"
,
host
);
pRaftServer
->
port
=
port
;
snprintf
(
pRaftServer
->
address
,
sizeof
(
pRaftServer
->
address
),
"%s:%u"
,
host
,
port
);
strncpy
(
pRaftServer
->
dir
,
dir
,
sizeof
(
pRaftServer
->
dir
));
pRaftServer
->
raftId
=
raftId
(
pRaftServer
->
host
,
pRaftServer
->
port
);
pRaftServer
->
fsm
=
pFsm
;
ret
=
uv_loop_init
(
&
pRaftServer
->
loop
);
if
(
!
ret
)
{
fprintf
(
stderr
,
"%s
\n
"
,
raft_errmsg
(
&
pRaftServer
->
raft
));
}
ret
=
raft_uv_tcp_init
(
&
pRaftServer
->
transport
,
&
pRaftServer
->
loop
);
if
(
!
ret
)
{
fprintf
(
stderr
,
"%s
\n
"
,
raft_errmsg
(
&
pRaftServer
->
raft
));
}
ret
=
raft_uv_init
(
&
pRaftServer
->
io
,
&
pRaftServer
->
loop
,
dir
,
&
pRaftServer
->
transport
);
if
(
!
ret
)
{
fprintf
(
stderr
,
"%s
\n
"
,
raft_errmsg
(
&
pRaftServer
->
raft
));
}
ret
=
raft_init
(
&
pRaftServer
->
raft
,
&
pRaftServer
->
io
,
pRaftServer
->
fsm
,
pRaftServer
->
raftId
,
pRaftServer
->
address
);
if
(
!
ret
)
{
fprintf
(
stderr
,
"%s
\n
"
,
raft_errmsg
(
&
pRaftServer
->
raft
));
}
struct
raft_configuration
conf
;
raft_configuration_init
(
&
conf
);
raft_configuration_add
(
&
conf
,
pRaftServer
->
raftId
,
pRaftServer
->
address
,
RAFT_VOTER
);
raft_bootstrap
(
&
pRaftServer
->
raft
,
&
conf
);
return
0
;
}
int32_t
raftServerStart
(
SRaftServer
*
pRaftServer
)
{
int
ret
;
ret
=
raft_start
(
&
pRaftServer
->
raft
);
if
(
!
ret
)
{
fprintf
(
stderr
,
"%s
\n
"
,
raft_errmsg
(
&
pRaftServer
->
raft
));
}
uv_run
(
&
pRaftServer
->
loop
,
UV_RUN_DEFAULT
);
}
...
...
@@ -14,7 +67,15 @@ void raftServerClose(SRaftServer *pRaftServer) {
}
int32_t
initFsm
(
struct
raft_fsm
*
fsm
)
{
int
fsmApplyCb
(
struct
raft_fsm
*
pFsm
,
const
struct
raft_buffer
*
buf
,
void
**
result
)
{
char
*
msg
=
(
char
*
)
buf
->
base
;
printf
(
"%s
\n
"
,
msg
);
return
0
;
}
int32_t
initFsm
(
struct
raft_fsm
*
fsm
)
{
fsm
->
apply
=
fsmApplyCb
;
return
0
;
}
contrib/test/craft/raftServer.h
浏览文件 @
6dc92d70
...
...
@@ -5,15 +5,20 @@
extern
"C"
{
#endif
#include <netinet/in.h>
#include <arpa/inet.h>
#include <assert.h>
#include <string.h>
#include "raft.h"
#include "raft/uv.h"
#define DIR_LEN 128
#define ADDRESS_LEN 128
#define HOST_LEN 128
#define ADDRESS_LEN (HOST_LEN + 16)
typedef
struct
{
char
dir
[
DIR_LEN
];
/* Data dir of UV I/O backend */
char
host
[
HOST_LEN
];
uint32_t
port
;
char
address
[
ADDRESS_LEN
];
/* Raft instance address */
raft_id
raftId
;
/* For vote */
struct
raft_fsm
*
fsm
;
/* Sample application FSM */
...
...
@@ -24,8 +29,7 @@ typedef struct {
struct
raft_uv_transport
transport
;
/* UV I/O backend transport */
}
SRaftServer
;
struct
SRaftServerConfig
;
int32_t
raftServerInit
(
SRaftServer
*
pRaftServer
,
struct
SRaftServerConfig
*
pConf
,
struct
raft_fsm
*
pFsm
);
int32_t
raftServerInit
(
SRaftServer
*
pRaftServer
,
const
char
*
host
,
uint32_t
port
,
const
char
*
dir
,
struct
raft_fsm
*
pFsm
);
int32_t
raftServerStart
(
SRaftServer
*
pRaftServer
);
void
raftServerClose
(
SRaftServer
*
pRaftServer
);
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录