Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
305f4a85
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看板
提交
305f4a85
编写于
6月 07, 2022
作者:
M
Minghao Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
enh(sync): add term into index mgr
上级
8680da28
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
72 addition
and
69 deletion
+72
-69
source/libs/sync/inc/syncIndexMgr.h
source/libs/sync/inc/syncIndexMgr.h
+6
-2
source/libs/sync/src/syncIndexMgr.c
source/libs/sync/src/syncIndexMgr.c
+45
-9
source/libs/sync/test/syncIndexMgrTest.cpp
source/libs/sync/test/syncIndexMgrTest.cpp
+21
-58
未找到文件。
source/libs/sync/inc/syncIndexMgr.h
浏览文件 @
305f4a85
...
...
@@ -30,6 +30,7 @@ extern "C" {
typedef
struct
SSyncIndexMgr
{
SRaftId
(
*
replicas
)[
TSDB_MAX_REPLICA
];
SyncIndex
index
[
TSDB_MAX_REPLICA
];
SyncTerm
privateTerm
[
TSDB_MAX_REPLICA
];
// for advanced function
int32_t
replicaNum
;
SSyncNode
*
pSyncNode
;
}
SSyncIndexMgr
;
...
...
@@ -40,8 +41,11 @@ void syncIndexMgrDestroy(SSyncIndexMgr *pSyncIndexMgr);
void
syncIndexMgrClear
(
SSyncIndexMgr
*
pSyncIndexMgr
);
void
syncIndexMgrSetIndex
(
SSyncIndexMgr
*
pSyncIndexMgr
,
const
SRaftId
*
pRaftId
,
SyncIndex
index
);
SyncIndex
syncIndexMgrGetIndex
(
SSyncIndexMgr
*
pSyncIndexMgr
,
const
SRaftId
*
pRaftId
);
cJSON
*
syncIndexMgr2Json
(
SSyncIndexMgr
*
pSyncIndexMgr
);
char
*
syncIndexMgr2Str
(
SSyncIndexMgr
*
pSyncIndexMgr
);
cJSON
*
syncIndexMgr2Json
(
SSyncIndexMgr
*
pSyncIndexMgr
);
char
*
syncIndexMgr2Str
(
SSyncIndexMgr
*
pSyncIndexMgr
);
void
syncIndexMgrSetTerm
(
SSyncIndexMgr
*
pSyncIndexMgr
,
const
SRaftId
*
pRaftId
,
SyncTerm
term
);
SyncTerm
syncIndexMgrGetTerm
(
SSyncIndexMgr
*
pSyncIndexMgr
,
const
SRaftId
*
pRaftId
);
// for debug -------------------
void
syncIndexMgrPrint
(
SSyncIndexMgr
*
pObj
);
...
...
source/libs/sync/src/syncIndexMgr.c
浏览文件 @
305f4a85
...
...
@@ -46,6 +46,7 @@ void syncIndexMgrDestroy(SSyncIndexMgr *pSyncIndexMgr) {
void
syncIndexMgrClear
(
SSyncIndexMgr
*
pSyncIndexMgr
)
{
memset
(
pSyncIndexMgr
->
index
,
0
,
sizeof
(
pSyncIndexMgr
->
index
));
memset
(
pSyncIndexMgr
->
privateTerm
,
0
,
sizeof
(
pSyncIndexMgr
->
privateTerm
));
/*
for (int i = 0; i < pSyncIndexMgr->replicaNum; ++i) {
pSyncIndexMgr->index[i] = 0;
...
...
@@ -62,7 +63,7 @@ void syncIndexMgrSetIndex(SSyncIndexMgr *pSyncIndexMgr, const SRaftId *pRaftId,
}
// maybe config change
//
assert(0);
assert
(
0
);
}
SyncIndex
syncIndexMgrGetIndex
(
SSyncIndexMgr
*
pSyncIndexMgr
,
const
SRaftId
*
pRaftId
)
{
...
...
@@ -86,14 +87,27 @@ cJSON *syncIndexMgr2Json(SSyncIndexMgr *pSyncIndexMgr) {
for
(
int
i
=
0
;
i
<
pSyncIndexMgr
->
replicaNum
;
++
i
)
{
cJSON_AddItemToArray
(
pReplicas
,
syncUtilRaftId2Json
(
&
(
*
(
pSyncIndexMgr
->
replicas
))[
i
]));
}
int
respondNum
=
0
;
int
*
arr
=
(
int
*
)
taosMemoryMalloc
(
sizeof
(
int
)
*
pSyncIndexMgr
->
replicaNum
);
for
(
int
i
=
0
;
i
<
pSyncIndexMgr
->
replicaNum
;
++
i
)
{
arr
[
i
]
=
pSyncIndexMgr
->
index
[
i
];
{
int
*
arr
=
(
int
*
)
taosMemoryMalloc
(
sizeof
(
int
)
*
pSyncIndexMgr
->
replicaNum
);
for
(
int
i
=
0
;
i
<
pSyncIndexMgr
->
replicaNum
;
++
i
)
{
arr
[
i
]
=
pSyncIndexMgr
->
index
[
i
];
}
cJSON
*
pIndex
=
cJSON_CreateIntArray
(
arr
,
pSyncIndexMgr
->
replicaNum
);
taosMemoryFree
(
arr
);
cJSON_AddItemToObject
(
pRoot
,
"index"
,
pIndex
);
}
cJSON
*
pIndex
=
cJSON_CreateIntArray
(
arr
,
pSyncIndexMgr
->
replicaNum
);
taosMemoryFree
(
arr
);
cJSON_AddItemToObject
(
pRoot
,
"index"
,
pIndex
);
{
int
*
arr
=
(
int
*
)
taosMemoryMalloc
(
sizeof
(
int
)
*
pSyncIndexMgr
->
replicaNum
);
for
(
int
i
=
0
;
i
<
pSyncIndexMgr
->
replicaNum
;
++
i
)
{
arr
[
i
]
=
pSyncIndexMgr
->
privateTerm
[
i
];
}
cJSON
*
pIndex
=
cJSON_CreateIntArray
(
arr
,
pSyncIndexMgr
->
replicaNum
);
taosMemoryFree
(
arr
);
cJSON_AddItemToObject
(
pRoot
,
"privateTerm"
,
pIndex
);
}
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%p"
,
pSyncIndexMgr
->
pSyncNode
);
cJSON_AddStringToObject
(
pRoot
,
"pSyncNode"
,
u64buf
);
}
...
...
@@ -105,7 +119,7 @@ cJSON *syncIndexMgr2Json(SSyncIndexMgr *pSyncIndexMgr) {
char
*
syncIndexMgr2Str
(
SSyncIndexMgr
*
pSyncIndexMgr
)
{
cJSON
*
pJson
=
syncIndexMgr2Json
(
pSyncIndexMgr
);
char
*
serialized
=
cJSON_Print
(
pJson
);
char
*
serialized
=
cJSON_Print
(
pJson
);
cJSON_Delete
(
pJson
);
return
serialized
;
}
...
...
@@ -135,4 +149,26 @@ void syncIndexMgrLog2(char *s, SSyncIndexMgr *pObj) {
char
*
serialized
=
syncIndexMgr2Str
(
pObj
);
sTrace
(
"syncIndexMgrLog2 | len:%lu | %s | %s"
,
strlen
(
serialized
),
s
,
serialized
);
taosMemoryFree
(
serialized
);
}
void
syncIndexMgrSetTerm
(
SSyncIndexMgr
*
pSyncIndexMgr
,
const
SRaftId
*
pRaftId
,
SyncTerm
term
)
{
for
(
int
i
=
0
;
i
<
pSyncIndexMgr
->
replicaNum
;
++
i
)
{
if
(
syncUtilSameId
(
&
((
*
(
pSyncIndexMgr
->
replicas
))[
i
]),
pRaftId
))
{
(
pSyncIndexMgr
->
privateTerm
)[
i
]
=
term
;
return
;
}
}
// maybe config change
assert
(
0
);
}
SyncTerm
syncIndexMgrGetTerm
(
SSyncIndexMgr
*
pSyncIndexMgr
,
const
SRaftId
*
pRaftId
)
{
for
(
int
i
=
0
;
i
<
pSyncIndexMgr
->
replicaNum
;
++
i
)
{
if
(
syncUtilSameId
(
&
((
*
(
pSyncIndexMgr
->
replicas
))[
i
]),
pRaftId
))
{
SyncTerm
term
=
(
pSyncIndexMgr
->
privateTerm
)[
i
];
return
term
;
}
}
assert
(
0
);
}
\ No newline at end of file
source/libs/sync/test/syncIndexMgrTest.cpp
浏览文件 @
305f4a85
...
...
@@ -22,55 +22,24 @@ int32_t replicaNum = 3;
int32_t
myIndex
=
0
;
SRaftId
ids
[
TSDB_MAX_REPLICA
];
SSyncInfo
syncInfo
;
SSyncFSM
*
pFsm
;
SSyncNode
*
pSyncNode
;
SSyncNode
*
syncNodeInit
()
{
syncInfo
.
vgId
=
1234
;
syncInfo
.
msgcb
=
&
gSyncIO
->
msgcb
;
syncInfo
.
FpSendMsg
=
syncIOSendMsg
;
syncInfo
.
FpEqMsg
=
syncIOEqMsg
;
syncInfo
.
pFsm
=
pFsm
;
snprintf
(
syncInfo
.
path
,
sizeof
(
syncInfo
.
path
),
"%s"
,
"./"
);
SSyncCfg
*
pCfg
=
&
syncInfo
.
syncCfg
;
pCfg
->
myIndex
=
myIndex
;
pCfg
->
replicaNum
=
replicaNum
;
pSyncNode
=
(
SSyncNode
*
)
taosMemoryMalloc
(
sizeof
(
SSyncNode
));
memset
(
pSyncNode
,
0
,
sizeof
(
SSyncNode
));
pSyncNode
->
replicaNum
=
replicaNum
;
for
(
int
i
=
0
;
i
<
replicaNum
;
++
i
)
{
pCfg
->
nodeInfo
[
i
].
nodePort
=
ports
[
i
];
snprintf
(
pCfg
->
nodeInfo
[
i
].
nodeFqdn
,
sizeof
(
pCfg
->
nodeInfo
[
i
].
nodeFqdn
),
"%s"
,
"127.0.0.1"
);
// taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn);
}
pSyncNode
=
syncNodeOpen
(
&
syncInfo
);
assert
(
pSyncNode
!=
NULL
);
pSyncNode
->
replicasId
[
i
].
addr
=
syncUtilAddr2U64
(
"127.0.0.1"
,
ports
[
i
]);
pSyncNode
->
replicasId
[
i
].
vgId
=
1234
;
gSyncIO
->
FpOnSyncPing
=
pSyncNode
->
FpOnPing
;
gSyncIO
->
FpOnSyncPingReply
=
pSyncNode
->
FpOnPingReply
;
gSyncIO
->
FpOnSyncRequestVote
=
pSyncNode
->
FpOnRequestVote
;
gSyncIO
->
FpOnSyncRequestVoteReply
=
pSyncNode
->
FpOnRequestVoteReply
;
gSyncIO
->
FpOnSyncAppendEntries
=
pSyncNode
->
FpOnAppendEntries
;
gSyncIO
->
FpOnSyncAppendEntriesReply
=
pSyncNode
->
FpOnAppendEntriesReply
;
gSyncIO
->
FpOnSyncPing
=
pSyncNode
->
FpOnPing
;
gSyncIO
->
FpOnSyncPingReply
=
pSyncNode
->
FpOnPingReply
;
gSyncIO
->
pSyncNode
=
pSyncNode
;
ids
[
i
].
addr
=
pSyncNode
->
replicasId
[
i
].
addr
;
ids
[
i
].
vgId
=
pSyncNode
->
replicasId
[
i
].
vgId
;
}
return
pSyncNode
;
}
SSyncNode
*
syncInitTest
()
{
return
syncNodeInit
();
}
void
initRaftId
(
SSyncNode
*
pSyncNode
)
{
for
(
int
i
=
0
;
i
<
replicaNum
;
++
i
)
{
ids
[
i
]
=
pSyncNode
->
replicasId
[
i
];
char
*
s
=
syncUtilRaftId2Str
(
&
ids
[
i
]);
printf
(
"raftId[%d] : %s
\n
"
,
i
,
s
);
taosMemoryFree
(
s
);
}
}
int
main
(
int
argc
,
char
**
argv
)
{
tsAsyncLog
=
0
;
sDebugFlag
=
DEBUG_TRACE
+
DEBUG_SCREEN
+
DEBUG_FILE
;
...
...
@@ -80,58 +49,52 @@ int main(int argc, char** argv) {
myIndex
=
atoi
(
argv
[
1
]);
}
int32_t
ret
=
syncIOStart
((
char
*
)
"127.0.0.1"
,
ports
[
myIndex
]);
assert
(
ret
==
0
);
ret
=
syncEnvStart
();
assert
(
ret
==
0
);
SSyncNode
*
pSyncNode
=
syncInitTest
();
SSyncNode
*
pSyncNode
=
syncNodeInit
();
assert
(
pSyncNode
!=
NULL
);
char
*
serialized
=
syncNode2Str
(
pSyncNode
);
printf
(
"%s
\n
"
,
serialized
);
taosMemoryFree
(
serialized
);
initRaftId
(
pSyncNode
);
printf
(
"---------------------------------------
\n
"
);
SSyncIndexMgr
*
pSyncIndexMgr
=
syncIndexMgrCreate
(
pSyncNode
);
assert
(
pSyncIndexMgr
!=
NULL
);
printf
(
"---------------------------------------
\n
"
);
{
char
*
serialized
=
syncIndexMgr2Str
(
pSyncIndexMgr
);
assert
(
serialized
!=
NULL
);
printf
(
"%s
\n
"
,
serialized
);
taosMemoryFree
(
serialized
);
}
printf
(
"---------------------------------------
\n
"
);
printf
(
"---------------------------------------
\n
"
);
syncIndexMgrSetIndex
(
pSyncIndexMgr
,
&
ids
[
0
],
100
);
syncIndexMgrSetIndex
(
pSyncIndexMgr
,
&
ids
[
1
],
200
);
syncIndexMgrSetIndex
(
pSyncIndexMgr
,
&
ids
[
2
],
300
);
printf
(
"---------------------------------------
\n
"
);
syncIndexMgrSetTerm
(
pSyncIndexMgr
,
&
ids
[
0
],
700
);
syncIndexMgrSetTerm
(
pSyncIndexMgr
,
&
ids
[
1
],
800
);
syncIndexMgrSetTerm
(
pSyncIndexMgr
,
&
ids
[
2
],
900
);
{
char
*
serialized
=
syncIndexMgr2Str
(
pSyncIndexMgr
);
assert
(
serialized
!=
NULL
);
printf
(
"%s
\n
"
,
serialized
);
taosMemoryFree
(
serialized
);
}
printf
(
"---------------------------------------
\n
"
);
printf
(
"---------------------------------------
\n
"
);
for
(
int
i
=
0
;
i
<
pSyncIndexMgr
->
replicaNum
;
++
i
)
{
SyncIndex
idx
=
syncIndexMgrGetIndex
(
pSyncIndexMgr
,
&
ids
[
i
]);
printf
(
"index %d : %lu
\n
"
,
i
,
idx
);
SyncTerm
term
=
syncIndexMgrGetTerm
(
pSyncIndexMgr
,
&
ids
[
i
]);
printf
(
"%d: index:%ld term:%lu
\n
"
,
i
,
idx
,
term
);
}
printf
(
"---------------------------------------
\n
"
);
syncIndexMgrClear
(
pSyncIndexMgr
);
printf
(
"---------------------------------------
\n
"
);
syncIndexMgrClear
(
pSyncIndexMgr
);
{
char
*
serialized
=
syncIndexMgr2Str
(
pSyncIndexMgr
);
assert
(
serialized
!=
NULL
);
printf
(
"%s
\n
"
,
serialized
);
taosMemoryFree
(
serialized
);
}
printf
(
"---------------------------------------
\n
"
);
syncIndexMgrDestroy
(
pSyncIndexMgr
);
return
0
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录