Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
taosdata
TDengine
提交
69af9556
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看板
提交
69af9556
编写于
11月 01, 2022
作者:
M
Minghao Li
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
refactor(sync): add PreSnapshotReply
上级
981461ea
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
295 addition
and
0 deletion
+295
-0
include/libs/sync/syncTools.h
include/libs/sync/syncTools.h
+32
-0
source/libs/sync/src/syncMessage.c
source/libs/sync/src/syncMessage.c
+150
-0
source/libs/sync/test/CMakeLists.txt
source/libs/sync/test/CMakeLists.txt
+14
-0
source/libs/sync/test/syncPreSnapshotReplyTest.cpp
source/libs/sync/test/syncPreSnapshotReplyTest.cpp
+99
-0
未找到文件。
include/libs/sync/syncTools.h
浏览文件 @
69af9556
...
...
@@ -544,6 +544,38 @@ void syncPreSnapshotPrint2(char* s, const SyncPreSnapshot* pMsg);
void
syncPreSnapshotLog
(
const
SyncPreSnapshot
*
pMsg
);
void
syncPreSnapshotLog2
(
char
*
s
,
const
SyncPreSnapshot
*
pMsg
);
// ---------------------------------------------
typedef
struct
SyncPreSnapshotReply
{
uint32_t
bytes
;
int32_t
vgId
;
uint32_t
msgType
;
SRaftId
srcId
;
SRaftId
destId
;
// private data
SyncTerm
term
;
SyncIndex
matchIndex
;
}
SyncPreSnapshotReply
;
SyncPreSnapshotReply
*
syncPreSnapshotReplyBuild
(
int32_t
vgId
);
void
syncPreSnapshotReplyDestroy
(
SyncPreSnapshotReply
*
pMsg
);
void
syncPreSnapshotReplySerialize
(
const
SyncPreSnapshotReply
*
pMsg
,
char
*
buf
,
uint32_t
bufLen
);
void
syncPreSnapshotReplyDeserialize
(
const
char
*
buf
,
uint32_t
len
,
SyncPreSnapshotReply
*
pMsg
);
char
*
syncPreSnapshotReplySerialize2
(
const
SyncPreSnapshotReply
*
pMsg
,
uint32_t
*
len
);
SyncPreSnapshotReply
*
syncPreSnapshotReplyDeserialize2
(
const
char
*
buf
,
uint32_t
len
);
void
syncPreSnapshotReply2RpcMsg
(
const
SyncPreSnapshotReply
*
pMsg
,
SRpcMsg
*
pRpcMsg
);
void
syncPreSnapshotReplyFromRpcMsg
(
const
SRpcMsg
*
pRpcMsg
,
SyncPreSnapshotReply
*
pMsg
);
SyncPreSnapshotReply
*
syncPreSnapshotReplyFromRpcMsg2
(
const
SRpcMsg
*
pRpcMsg
);
cJSON
*
syncPreSnapshotReply2Json
(
const
SyncPreSnapshotReply
*
pMsg
);
char
*
syncPreSnapshotReply2Str
(
const
SyncPreSnapshotReply
*
pMsg
);
// for debug ----------------------
void
syncPreSnapshotReplyPrint
(
const
SyncPreSnapshotReply
*
pMsg
);
void
syncPreSnapshotReplyPrint2
(
char
*
s
,
const
SyncPreSnapshotReply
*
pMsg
);
void
syncPreSnapshotReplyLog
(
const
SyncPreSnapshotReply
*
pMsg
);
void
syncPreSnapshotReplyLog2
(
char
*
s
,
const
SyncPreSnapshotReply
*
pMsg
);
// ---------------------------------------------
typedef
struct
SyncApplyMsg
{
uint32_t
bytes
;
...
...
source/libs/sync/src/syncMessage.c
浏览文件 @
69af9556
...
...
@@ -2462,6 +2462,156 @@ void syncPreSnapshotLog2(char* s, const SyncPreSnapshot* pMsg) {
}
}
// ---- message process SyncPreSnapshotReply----
SyncPreSnapshotReply
*
syncPreSnapshotReplyBuild
(
int32_t
vgId
)
{
uint32_t
bytes
=
sizeof
(
SyncPreSnapshotReply
);
SyncPreSnapshotReply
*
pMsg
=
taosMemoryMalloc
(
bytes
);
memset
(
pMsg
,
0
,
bytes
);
pMsg
->
bytes
=
bytes
;
pMsg
->
vgId
=
vgId
;
pMsg
->
msgType
=
TDMT_SYNC_PRE_SNAPSHOT_REPLY
;
return
pMsg
;
}
void
syncPreSnapshotReplyDestroy
(
SyncPreSnapshotReply
*
pMsg
)
{
if
(
pMsg
!=
NULL
)
{
taosMemoryFree
(
pMsg
);
}
}
void
syncPreSnapshotReplySerialize
(
const
SyncPreSnapshotReply
*
pMsg
,
char
*
buf
,
uint32_t
bufLen
)
{
ASSERT
(
pMsg
->
bytes
<=
bufLen
);
memcpy
(
buf
,
pMsg
,
pMsg
->
bytes
);
}
void
syncPreSnapshotReplyDeserialize
(
const
char
*
buf
,
uint32_t
len
,
SyncPreSnapshotReply
*
pMsg
)
{
memcpy
(
pMsg
,
buf
,
len
);
ASSERT
(
len
==
pMsg
->
bytes
);
}
char
*
syncPreSnapshotReplySerialize2
(
const
SyncPreSnapshotReply
*
pMsg
,
uint32_t
*
len
)
{
char
*
buf
=
taosMemoryMalloc
(
pMsg
->
bytes
);
ASSERT
(
buf
!=
NULL
);
syncPreSnapshotReplySerialize
(
pMsg
,
buf
,
pMsg
->
bytes
);
if
(
len
!=
NULL
)
{
*
len
=
pMsg
->
bytes
;
}
return
buf
;
}
SyncPreSnapshotReply
*
syncPreSnapshotReplyDeserialize2
(
const
char
*
buf
,
uint32_t
len
)
{
uint32_t
bytes
=
*
((
uint32_t
*
)
buf
);
SyncPreSnapshotReply
*
pMsg
=
taosMemoryMalloc
(
bytes
);
ASSERT
(
pMsg
!=
NULL
);
syncPreSnapshotReplyDeserialize
(
buf
,
len
,
pMsg
);
ASSERT
(
len
==
pMsg
->
bytes
);
return
pMsg
;
}
void
syncPreSnapshotReply2RpcMsg
(
const
SyncPreSnapshotReply
*
pMsg
,
SRpcMsg
*
pRpcMsg
)
{
memset
(
pRpcMsg
,
0
,
sizeof
(
*
pRpcMsg
));
pRpcMsg
->
msgType
=
pMsg
->
msgType
;
pRpcMsg
->
contLen
=
pMsg
->
bytes
;
pRpcMsg
->
pCont
=
rpcMallocCont
(
pRpcMsg
->
contLen
);
syncPreSnapshotReplySerialize
(
pMsg
,
pRpcMsg
->
pCont
,
pRpcMsg
->
contLen
);
}
void
syncPreSnapshotReplyFromRpcMsg
(
const
SRpcMsg
*
pRpcMsg
,
SyncPreSnapshotReply
*
pMsg
)
{
syncPreSnapshotReplyDeserialize
(
pRpcMsg
->
pCont
,
pRpcMsg
->
contLen
,
pMsg
);
}
SyncPreSnapshotReply
*
syncPreSnapshotReplyFromRpcMsg2
(
const
SRpcMsg
*
pRpcMsg
)
{
SyncPreSnapshotReply
*
pMsg
=
syncPreSnapshotReplyDeserialize2
(
pRpcMsg
->
pCont
,
pRpcMsg
->
contLen
);
ASSERT
(
pMsg
!=
NULL
);
return
pMsg
;
}
cJSON
*
syncPreSnapshotReply2Json
(
const
SyncPreSnapshotReply
*
pMsg
)
{
char
u64buf
[
128
]
=
{
0
};
cJSON
*
pRoot
=
cJSON_CreateObject
();
if
(
pMsg
!=
NULL
)
{
cJSON_AddNumberToObject
(
pRoot
,
"bytes"
,
pMsg
->
bytes
);
cJSON_AddNumberToObject
(
pRoot
,
"vgId"
,
pMsg
->
vgId
);
cJSON_AddNumberToObject
(
pRoot
,
"msgType"
,
pMsg
->
msgType
);
cJSON
*
pSrcId
=
cJSON_CreateObject
();
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%"
PRIu64
,
pMsg
->
srcId
.
addr
);
cJSON_AddStringToObject
(
pSrcId
,
"addr"
,
u64buf
);
{
uint64_t
u64
=
pMsg
->
srcId
.
addr
;
cJSON
*
pTmp
=
pSrcId
;
char
host
[
128
]
=
{
0
};
uint16_t
port
;
syncUtilU642Addr
(
u64
,
host
,
sizeof
(
host
),
&
port
);
cJSON_AddStringToObject
(
pTmp
,
"addr_host"
,
host
);
cJSON_AddNumberToObject
(
pTmp
,
"addr_port"
,
port
);
}
cJSON_AddNumberToObject
(
pSrcId
,
"vgId"
,
pMsg
->
srcId
.
vgId
);
cJSON_AddItemToObject
(
pRoot
,
"srcId"
,
pSrcId
);
cJSON
*
pDestId
=
cJSON_CreateObject
();
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%"
PRIu64
,
pMsg
->
destId
.
addr
);
cJSON_AddStringToObject
(
pDestId
,
"addr"
,
u64buf
);
{
uint64_t
u64
=
pMsg
->
destId
.
addr
;
cJSON
*
pTmp
=
pDestId
;
char
host
[
128
]
=
{
0
};
uint16_t
port
;
syncUtilU642Addr
(
u64
,
host
,
sizeof
(
host
),
&
port
);
cJSON_AddStringToObject
(
pTmp
,
"addr_host"
,
host
);
cJSON_AddNumberToObject
(
pTmp
,
"addr_port"
,
port
);
}
cJSON_AddNumberToObject
(
pDestId
,
"vgId"
,
pMsg
->
destId
.
vgId
);
cJSON_AddItemToObject
(
pRoot
,
"destId"
,
pDestId
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%"
PRIu64
,
pMsg
->
term
);
cJSON_AddStringToObject
(
pRoot
,
"term"
,
u64buf
);
snprintf
(
u64buf
,
sizeof
(
u64buf
),
"%"
PRId64
,
pMsg
->
matchIndex
);
cJSON_AddStringToObject
(
pRoot
,
"match-index"
,
u64buf
);
}
cJSON
*
pJson
=
cJSON_CreateObject
();
cJSON_AddItemToObject
(
pJson
,
"SyncPreSnapshotReply"
,
pRoot
);
return
pJson
;
}
char
*
syncPreSnapshotReply2Str
(
const
SyncPreSnapshotReply
*
pMsg
)
{
cJSON
*
pJson
=
syncPreSnapshotReply2Json
(
pMsg
);
char
*
serialized
=
cJSON_Print
(
pJson
);
cJSON_Delete
(
pJson
);
return
serialized
;
}
void
syncPreSnapshotReplyPrint
(
const
SyncPreSnapshotReply
*
pMsg
)
{
char
*
serialized
=
syncPreSnapshotReply2Str
(
pMsg
);
printf
(
"syncPreSnapshotReplyPrint | len:%d | %s
\n
"
,
(
int32_t
)
strlen
(
serialized
),
serialized
);
fflush
(
NULL
);
taosMemoryFree
(
serialized
);
}
void
syncPreSnapshotReplyPrint2
(
char
*
s
,
const
SyncPreSnapshotReply
*
pMsg
)
{
char
*
serialized
=
syncPreSnapshotReply2Str
(
pMsg
);
printf
(
"syncPreSnapshotReplyPrint2 | len:%d | %s | %s
\n
"
,
(
int32_t
)
strlen
(
serialized
),
s
,
serialized
);
fflush
(
NULL
);
taosMemoryFree
(
serialized
);
}
void
syncPreSnapshotReplyLog
(
const
SyncPreSnapshotReply
*
pMsg
)
{
char
*
serialized
=
syncPreSnapshotReply2Str
(
pMsg
);
sTrace
(
"syncPreSnapshotReplyLog | len:%d | %s"
,
(
int32_t
)
strlen
(
serialized
),
serialized
);
taosMemoryFree
(
serialized
);
}
void
syncPreSnapshotReplyLog2
(
char
*
s
,
const
SyncPreSnapshotReply
*
pMsg
)
{
if
(
gRaftDetailLog
)
{
char
*
serialized
=
syncPreSnapshotReply2Str
(
pMsg
);
sTrace
(
"syncPreSnapshotReplyLog2 | len:%d | %s | %s"
,
(
int32_t
)
strlen
(
serialized
),
s
,
serialized
);
taosMemoryFree
(
serialized
);
}
}
// ---- message process SyncApplyMsg----
SyncApplyMsg
*
syncApplyMsgBuild
(
uint32_t
dataLen
)
{
uint32_t
bytes
=
sizeof
(
SyncApplyMsg
)
+
dataLen
;
...
...
source/libs/sync/test/CMakeLists.txt
浏览文件 @
69af9556
...
...
@@ -61,6 +61,7 @@ add_executable(syncHeartbeatTest "")
add_executable
(
syncHeartbeatReplyTest
""
)
add_executable
(
syncLocalCmdTest
""
)
add_executable
(
syncPreSnapshotTest
""
)
add_executable
(
syncPreSnapshotReplyTest
""
)
target_sources
(
syncTest
...
...
@@ -315,6 +316,10 @@ target_sources(syncPreSnapshotTest
PRIVATE
"syncPreSnapshotTest.cpp"
)
target_sources
(
syncPreSnapshotReplyTest
PRIVATE
"syncPreSnapshotReplyTest.cpp"
)
target_include_directories
(
syncTest
...
...
@@ -632,6 +637,11 @@ target_include_directories(syncPreSnapshotTest
"
${
TD_SOURCE_DIR
}
/include/libs/sync"
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../inc"
)
target_include_directories
(
syncPreSnapshotReplyTest
PUBLIC
"
${
TD_SOURCE_DIR
}
/include/libs/sync"
"
${
CMAKE_CURRENT_SOURCE_DIR
}
/../inc"
)
target_link_libraries
(
syncTest
...
...
@@ -886,6 +896,10 @@ target_link_libraries(syncPreSnapshotTest
sync
gtest_main
)
target_link_libraries
(
syncPreSnapshotReplyTest
sync
gtest_main
)
enable_testing
()
...
...
source/libs/sync/test/syncPreSnapshotReplyTest.cpp
0 → 100644
浏览文件 @
69af9556
#include <gtest/gtest.h>
#include <stdio.h>
#include "syncIO.h"
#include "syncInt.h"
#include "syncMessage.h"
#include "syncUtil.h"
void
logTest
()
{
sTrace
(
"--- sync log test: trace"
);
sDebug
(
"--- sync log test: debug"
);
sInfo
(
"--- sync log test: info"
);
sWarn
(
"--- sync log test: warn"
);
sError
(
"--- sync log test: error"
);
sFatal
(
"--- sync log test: fatal"
);
}
SyncPreSnapshotReply
*
createMsg
()
{
SyncPreSnapshotReply
*
pMsg
=
syncPreSnapshotReplyBuild
(
789
);
pMsg
->
srcId
.
addr
=
syncUtilAddr2U64
(
"127.0.0.1"
,
1234
);
pMsg
->
srcId
.
vgId
=
100
;
pMsg
->
destId
.
addr
=
syncUtilAddr2U64
(
"127.0.0.1"
,
5678
);
pMsg
->
destId
.
vgId
=
100
;
pMsg
->
term
=
9527
;
pMsg
->
matchIndex
=
12306
;
return
pMsg
;
}
void
test1
()
{
SyncPreSnapshotReply
*
pMsg
=
createMsg
();
syncPreSnapshotReplyLog2
((
char
*
)
"test1:"
,
pMsg
);
syncPreSnapshotReplyDestroy
(
pMsg
);
}
void
test2
()
{
SyncPreSnapshotReply
*
pMsg
=
createMsg
();
uint32_t
len
=
pMsg
->
bytes
;
char
*
serialized
=
(
char
*
)
taosMemoryMalloc
(
len
);
syncPreSnapshotReplySerialize
(
pMsg
,
serialized
,
len
);
SyncPreSnapshotReply
*
pMsg2
=
syncPreSnapshotReplyBuild
(
789
);
syncPreSnapshotReplyDeserialize
(
serialized
,
len
,
pMsg2
);
syncPreSnapshotReplyLog2
((
char
*
)
"test2: syncPreSnapshotReplySerialize -> syncPreSnapshotReplyDeserialize "
,
pMsg2
);
taosMemoryFree
(
serialized
);
syncPreSnapshotReplyDestroy
(
pMsg
);
syncPreSnapshotReplyDestroy
(
pMsg2
);
}
void
test3
()
{
SyncPreSnapshotReply
*
pMsg
=
createMsg
();
uint32_t
len
;
char
*
serialized
=
syncPreSnapshotReplySerialize2
(
pMsg
,
&
len
);
SyncPreSnapshotReply
*
pMsg2
=
syncPreSnapshotReplyDeserialize2
(
serialized
,
len
);
syncPreSnapshotReplyLog2
((
char
*
)
"test3: syncPreSnapshotReplySerialize2 -> syncPreSnapshotReplyDeserialize2 "
,
pMsg2
);
taosMemoryFree
(
serialized
);
syncPreSnapshotReplyDestroy
(
pMsg
);
syncPreSnapshotReplyDestroy
(
pMsg2
);
}
void
test4
()
{
SyncPreSnapshotReply
*
pMsg
=
createMsg
();
SRpcMsg
rpcMsg
;
syncPreSnapshotReply2RpcMsg
(
pMsg
,
&
rpcMsg
);
SyncPreSnapshotReply
*
pMsg2
=
(
SyncPreSnapshotReply
*
)
taosMemoryMalloc
(
rpcMsg
.
contLen
);
syncPreSnapshotReplyFromRpcMsg
(
&
rpcMsg
,
pMsg2
);
syncPreSnapshotReplyLog2
((
char
*
)
"test4: syncPreSnapshotReply2RpcMsg -> syncPreSnapshotReplyFromRpcMsg "
,
pMsg2
);
rpcFreeCont
(
rpcMsg
.
pCont
);
syncPreSnapshotReplyDestroy
(
pMsg
);
syncPreSnapshotReplyDestroy
(
pMsg2
);
}
void
test5
()
{
SyncPreSnapshotReply
*
pMsg
=
createMsg
();
SRpcMsg
rpcMsg
;
syncPreSnapshotReply2RpcMsg
(
pMsg
,
&
rpcMsg
);
SyncPreSnapshotReply
*
pMsg2
=
syncPreSnapshotReplyFromRpcMsg2
(
&
rpcMsg
);
syncPreSnapshotReplyLog2
((
char
*
)
"test5: syncPreSnapshotReply2RpcMsg -> syncPreSnapshotReplyFromRpcMsg2 "
,
pMsg2
);
rpcFreeCont
(
rpcMsg
.
pCont
);
syncPreSnapshotReplyDestroy
(
pMsg
);
syncPreSnapshotReplyDestroy
(
pMsg2
);
}
int
main
()
{
tsAsyncLog
=
0
;
sDebugFlag
=
DEBUG_TRACE
+
DEBUG_SCREEN
+
DEBUG_FILE
;
gRaftDetailLog
=
true
;
logTest
();
test1
();
test2
();
test3
();
test4
();
test5
();
return
0
;
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录