Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
4fc500c9
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看板
提交
4fc500c9
编写于
7月 02, 2022
作者:
M
Minghao Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(sync): add SyncAppendEntriesBatch
上级
086ec29c
变更
3
隐藏空白更改
内联
并排
Showing
3 changed file
with
53 addition
and
69 deletion
+53
-69
include/libs/sync/syncTools.h
include/libs/sync/syncTools.h
+1
-2
source/libs/sync/src/syncMessage.c
source/libs/sync/src/syncMessage.c
+24
-40
source/libs/sync/test/syncAppendEntriesBatchTest.cpp
source/libs/sync/test/syncAppendEntriesBatchTest.cpp
+28
-27
未找到文件。
include/libs/sync/syncTools.h
浏览文件 @
4fc500c9
...
...
@@ -392,9 +392,8 @@ typedef struct SyncAppendEntriesBatch {
char
data
[];
// block1, block2
}
SyncAppendEntriesBatch
;
// SyncAppendEntriesBatch* syncAppendEntriesBatchBuild(SRpcMsg* rpcMsgArr, int32_t arrSize, int32_t vgId);
SyncAppendEntriesBatch
*
syncAppendEntriesBatchBuild
(
SSyncRaftEntry
**
entryPArr
,
int32_t
arrSize
,
int32_t
vgId
);
SOffsetAndContLen
*
syncAppendEntriesBatchMetaTableArray
(
SyncAppendEntriesBatch
*
pMsg
);
void
syncAppendEntriesBatchDestroy
(
SyncAppendEntriesBatch
*
pMsg
);
void
syncAppendEntriesBatchSerialize
(
const
SyncAppendEntriesBatch
*
pMsg
,
char
*
buf
,
uint32_t
bufLen
);
void
syncAppendEntriesBatchDeserialize
(
const
char
*
buf
,
uint32_t
len
,
SyncAppendEntriesBatch
*
pMsg
);
...
...
source/libs/sync/src/syncMessage.c
浏览文件 @
4fc500c9
...
...
@@ -15,6 +15,7 @@
#include "syncMessage.h"
#include "syncRaftCfg.h"
#include "syncRaftEntry.h"
#include "syncUtil.h"
#include "tcoding.h"
...
...
@@ -1600,22 +1601,20 @@ void syncAppendEntriesLog2(char* s, const SyncAppendEntries* pMsg) {
// block1: SOffsetAndContLen
// block2: SOffsetAndContLen Array
// block3: SRpcMsg Array
// block4: SRpcMsg pCont Array
// block3: entry Array
/*
SyncAppendEntriesBatch* syncAppendEntriesBatchBuild(SRpcMsg* rpcMsgArr, int32_t arrSize, int32_t vgId) {
ASSERT(rpcMsgArr != NULL);
SyncAppendEntriesBatch
*
syncAppendEntriesBatchBuild
(
SSyncRaftEntry
**
entryPArr
,
int32_t
arrSize
,
int32_t
vgId
)
{
ASSERT
(
entryPArr
!=
NULL
);
ASSERT
(
arrSize
>
0
);
int32_t
dataLen
=
0
;
int32_t
metaArrayLen
=
sizeof
(
SOffsetAndContLen
)
*
arrSize
;
// <offset, contLen>
int32_t rpcArrayLen = sizeof(SRpcMsg) * arrSize; // SRpcMsg
int32_t contArrayLen = 0;
int32_t
entryArrayLen
=
0
;
for
(
int
i
=
0
;
i
<
arrSize
;
++
i
)
{
// SRpcMsg pCont
contArrayLen += rpcMsgArr[i].contLen;
SSyncRaftEntry
*
pEntry
=
entryPArr
[
i
];
entryArrayLen
+=
pEntry
->
bytes
;
}
dataLen += (metaArrayLen +
rpcArrayLen + cont
ArrayLen);
dataLen
+=
(
metaArrayLen
+
entry
ArrayLen
);
uint32_t
bytes
=
sizeof
(
SyncAppendEntriesBatch
)
+
dataLen
;
SyncAppendEntriesBatch
*
pMsg
=
taosMemoryMalloc
(
bytes
);
...
...
@@ -1627,37 +1626,28 @@ SyncAppendEntriesBatch* syncAppendEntriesBatchBuild(SRpcMsg* rpcMsgArr, int32_t
pMsg
->
dataLen
=
dataLen
;
SOffsetAndContLen
*
metaArr
=
(
SOffsetAndContLen
*
)(
pMsg
->
data
);
SRpcMsg* msgArr = (SRpcMsg*)((char*)(pMsg->data) + metaArrayLen);
char
*
pData
=
pMsg
->
data
;
for
(
int
i
=
0
;
i
<
arrSize
;
++
i
)
{
// init <offset, contLen>
// init
meta
<offset, contLen>
if
(
i
==
0
)
{
metaArr[i].offset = metaArrayLen
+ rpcArrayLen
;
metaArr[i].contLen =
rpcMsgArr[i].contLen
;
metaArr
[
i
].
offset
=
metaArrayLen
;
metaArr
[
i
].
contLen
=
entryPArr
[
i
]
->
bytes
;
}
else
{
metaArr
[
i
].
offset
=
metaArr
[
i
-
1
].
offset
+
metaArr
[
i
-
1
].
contLen
;
metaArr[i].contLen =
rpcMsgArr[i].contLen
;
metaArr
[
i
].
contLen
=
entryPArr
[
i
]
->
bytes
;
}
// init msgArr
msgArr[i] = rpcMsgArr[i];
// init data
ASSERT(rpcMsgArr[i].contLen == metaArr[i].contLen);
memcpy(pData + metaArr[i].offset, rpcMsgArr[i].pCont, rpcMsgArr[i].contLen);
// init entry array
ASSERT
(
metaArr
[
i
].
contLen
==
entryPArr
[
i
]
->
bytes
);
memcpy
(
pData
+
metaArr
[
i
].
offset
,
entryPArr
[
i
],
metaArr
[
i
].
contLen
);
}
return
pMsg
;
}
*/
// block1: SOffsetAndContLen
// block2: SOffsetAndContLen Array
// block3: entry Array
SyncAppendEntriesBatch
*
syncAppendEntriesBatchBuild
(
SSyncRaftEntry
**
entryPArr
,
int32_t
arrSize
,
int32_t
vgId
)
{
return
NULL
;
SOffsetAndContLen
*
syncAppendEntriesBatchMetaTableArray
(
SyncAppendEntriesBatch
*
pMsg
)
{
return
(
SOffsetAndContLen
*
)(
pMsg
->
data
);
}
void
syncAppendEntriesBatchDestroy
(
SyncAppendEntriesBatch
*
pMsg
)
{
...
...
@@ -1772,16 +1762,12 @@ cJSON* syncAppendEntriesBatch2Json(const SyncAppendEntriesBatch* pMsg) {
cJSON_AddNumberToObject
(
pRoot
,
"dataLen"
,
pMsg
->
dataLen
);
int32_t
metaArrayLen
=
sizeof
(
SOffsetAndContLen
)
*
pMsg
->
dataCount
;
// <offset, contLen>
int32_t
rpcArrayLen
=
sizeof
(
SRpcMsg
)
*
pMsg
->
dataCount
;
// SRpcMsg
int32_t
contArrayLen
=
pMsg
->
dataLen
-
metaArrayLen
-
rpcArrayLen
;
int32_t
entryArrayLen
=
pMsg
->
dataLen
-
metaArrayLen
;
cJSON_AddNumberToObject
(
pRoot
,
"metaArrayLen"
,
metaArrayLen
);
cJSON_AddNumberToObject
(
pRoot
,
"rpcArrayLen"
,
rpcArrayLen
);
cJSON_AddNumberToObject
(
pRoot
,
"contArrayLen"
,
contArrayLen
);
cJSON_AddNumberToObject
(
pRoot
,
"entryArrayLen"
,
entryArrayLen
);
SOffsetAndContLen
*
metaArr
=
(
SOffsetAndContLen
*
)(
pMsg
->
data
);
SRpcMsg
*
msgArr
=
(
SRpcMsg
*
)(
pMsg
->
data
+
metaArrayLen
);
void
*
pData
=
(
void
*
)(
pMsg
->
data
+
metaArrayLen
+
rpcArrayLen
);
cJSON
*
pMetaArr
=
cJSON_CreateArray
();
cJSON_AddItemToObject
(
pRoot
,
"metaArr"
,
pMetaArr
);
...
...
@@ -1792,14 +1778,12 @@ cJSON* syncAppendEntriesBatch2Json(const SyncAppendEntriesBatch* pMsg) {
cJSON_AddItemToArray
(
pMetaArr
,
pMeta
);
}
cJSON
*
p
Msg
Arr
=
cJSON_CreateArray
();
cJSON_AddItemToObject
(
pRoot
,
"
msgArr"
,
pMsg
Arr
);
cJSON
*
p
Entry
Arr
=
cJSON_CreateArray
();
cJSON_AddItemToObject
(
pRoot
,
"
entryArr"
,
pEntry
Arr
);
for
(
int
i
=
0
;
i
<
pMsg
->
dataCount
;
++
i
)
{
cJSON
*
pRpcMsgJson
=
cJSON_CreateObject
();
cJSON_AddNumberToObject
(
pRpcMsgJson
,
"code"
,
msgArr
[
i
].
code
);
cJSON_AddNumberToObject
(
pRpcMsgJson
,
"contLen"
,
msgArr
[
i
].
contLen
);
cJSON_AddNumberToObject
(
pRpcMsgJson
,
"msgType"
,
msgArr
[
i
].
msgType
);
cJSON_AddItemToArray
(
pMsgArr
,
pRpcMsgJson
);
SSyncRaftEntry
*
pEntry
=
(
SSyncRaftEntry
*
)(
pMsg
->
data
+
metaArr
[
i
].
offset
);
cJSON
*
pEntryJson
=
syncEntry2Json
(
pEntry
);
cJSON_AddItemToArray
(
pEntryArr
,
pEntryJson
);
}
char
*
s
;
...
...
source/libs/sync/test/syncAppendEntriesBatchTest.cpp
浏览文件 @
4fc500c9
...
...
@@ -3,6 +3,7 @@
#include "syncIO.h"
#include "syncInt.h"
#include "syncMessage.h"
#include "syncRaftEntry.h"
#include "syncUtil.h"
#include "trpc.h"
...
...
@@ -15,31 +16,29 @@ void logTest() {
sFatal
(
"--- sync log test: fatal"
);
}
/*
SRpcMsg *createRpcMsg(int32_t i, int32_t dataLen) {
SRpcMsg *pRpcMsg = (SRpcMsg *)taosMemoryMalloc(sizeof(SRpcMsg));
memset(pRpcMsg, 0, sizeof(SRpcMsg));
pRpcMsg->msgType = TDMT_SYNC_PING;
pRpcMsg->contLen = dataLen;
pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen);
pRpcMsg->code = 10 * i;
snprintf((char *)pRpcMsg->pCont, pRpcMsg->contLen, "value_%d", i);
return pRpcMsg;
SSyncRaftEntry
*
createEntry
(
int
i
)
{
SSyncRaftEntry
*
pEntry
=
syncEntryBuild
(
20
);
assert
(
pEntry
!=
NULL
);
pEntry
->
msgType
=
1
;
pEntry
->
originalRpcType
=
2
;
pEntry
->
seqNum
=
3
;
pEntry
->
isWeak
=
true
;
pEntry
->
term
=
100
;
pEntry
->
index
=
200
;
snprintf
(
pEntry
->
data
,
pEntry
->
dataLen
,
"value_%d"
,
i
);
return
pEntry
;
}
SyncAppendEntriesBatch
*
createMsg
()
{
S
RpcMsg rpcMsg
Arr[5];
memset(
rpcMsgArr, 0, sizeof(rpcMsg
Arr));
S
SyncRaftEntry
*
entryP
Arr
[
5
];
memset
(
entryPArr
,
0
,
sizeof
(
entryP
Arr
));
for
(
int32_t
i
=
0
;
i
<
5
;
++
i
)
{
SRpcMsg *pRpcMsg = createRpcMsg(i, 20);
rpcMsgArr[i] = *pRpcMsg;
taosMemoryFree(pRpcMsg);
SSyncRaftEntry
*
pEntry
=
createEntry
(
i
);
entryPArr
[
i
]
=
pEntry
;
}
SyncAppendEntriesBatch *pMsg = syncAppendEntriesBatchBuild(
rpcMsg
Arr, 5, 1234);
SyncAppendEntriesBatch
*
pMsg
=
syncAppendEntriesBatchBuild
(
entryP
Arr
,
5
,
1234
);
pMsg
->
srcId
.
addr
=
syncUtilAddr2U64
(
"127.0.0.1"
,
1234
);
pMsg
->
srcId
.
vgId
=
100
;
pMsg
->
destId
.
addr
=
syncUtilAddr2U64
(
"127.0.0.1"
,
5678
);
...
...
@@ -53,21 +52,22 @@ SyncAppendEntriesBatch *createMsg() {
void
test1
()
{
SyncAppendEntriesBatch
*
pMsg
=
createMsg
();
syncAppendEntriesBatchLog2((char *)"
test1:
", pMsg);
syncAppendEntriesBatchLog2
((
char
*
)
"
==test1==
"
,
pMsg
);
SRpcMsg rpcMsgArr[5];
int32_t retArrSize
;
syncAppendEntriesBatch2RpcMsgArray(pMsg, rpcMsgArr, 5, &retArrSize)
;
/*
SOffsetAndContLen *metaArr = syncAppendEntriesBatchMetaTableArray(pMsg)
;
int32_t retArrSize = pMsg->dataCount
;
for (int i = 0; i < retArrSize; ++i) {
char logBuf[128];
snprintf(logBuf, sizeof(logBuf), "==test1 decode rpc msg %d: msgType:%d, code:%d, contLen:%d, pCont:%s \n", i,
rpcMsgArr[i].msgType, rpcMsgArr[i].code, rpcMsgArr[i].contLen, (char *)rpcMsgArr[i].pCont);
sTrace("%s", logBuf);
SSyncRaftEntry *pEntry = (SSyncRaftEntry*)(pMsg->data + metaArr[i].offset);
ASSERT(pEntry->bytes == metaArr[i].contLen);
syncEntryPrint(pEntry);
}
*/
syncAppendEntriesBatchDestroy
(
pMsg
);
}
/*
void test2() {
SyncAppendEntries *pMsg = createMsg();
uint32_t len = pMsg->bytes;
...
...
@@ -126,8 +126,9 @@ int main() {
sDebugFlag
=
DEBUG_DEBUG
+
DEBUG_TRACE
+
DEBUG_SCREEN
+
DEBUG_FILE
;
logTest
();
test1
();
/*
test1();
test2();
test3();
test4();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录