From 7066274cc741e7bf94f36d8d88bc0576d0c41e40 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Mon, 18 Apr 2022 21:50:56 +0800 Subject: [PATCH 01/51] add sync format code --- include/common/tmsgdef.h | 13 + include/libs/sync/sync.h | 108 ++--- include/libs/sync/syncTools.h | 376 ++++++++++++++++ source/libs/sync/CMakeLists.txt | 2 +- source/libs/sync/inc/syncEnv.h | 4 +- source/libs/sync/inc/syncInt.h | 64 ++- source/libs/sync/inc/syncMessage.h | 272 ------------ source/libs/sync/inc/syncRaftCfg.h | 65 +++ source/libs/sync/inc/syncRaftEntry.h | 19 +- source/libs/sync/inc/syncRaftStore.h | 2 +- source/libs/sync/inc/syncRespMgr.h | 55 +++ source/libs/sync/inc/syncSnapshot.h | 1 - source/libs/sync/inc/syncUtil.h | 2 + source/libs/sync/src/syncAppendEntries.c | 53 ++- source/libs/sync/src/syncAppendEntriesReply.c | 9 + source/libs/sync/src/syncCommit.c | 10 +- source/libs/sync/src/syncElection.c | 2 +- source/libs/sync/src/syncEnv.c | 3 + source/libs/sync/src/syncIO.c | 54 ++- source/libs/sync/src/syncMain.c | 419 ++++++++++++++---- source/libs/sync/src/syncMessage.c | 263 +++++++++-- source/libs/sync/src/syncRaftCfg.c | 211 +++++++++ source/libs/sync/src/syncRaftEntry.c | 31 +- source/libs/sync/src/syncRaftLog.c | 42 +- source/libs/sync/src/syncReplication.c | 4 +- source/libs/sync/src/syncRequestVote.c | 2 +- source/libs/sync/src/syncRequestVoteReply.c | 19 +- source/libs/sync/src/syncRespMgr.c | 93 ++++ source/libs/sync/src/syncUtil.c | 14 + source/libs/sync/test/CMakeLists.txt | 108 +++-- .../sync/test/syncAppendEntriesReplyTest.cpp | 29 +- .../libs/sync/test/syncAppendEntriesTest.cpp | 19 +- .../libs/sync/test/syncClientRequestTest.cpp | 18 +- source/libs/sync/test/syncElectTest.cpp | 26 +- source/libs/sync/test/syncElectTest2.cpp | 20 +- source/libs/sync/test/syncElectTest3.cpp | 20 +- source/libs/sync/test/syncEncodeTest.cpp | 18 +- source/libs/sync/test/syncEnqTest.cpp | 4 +- source/libs/sync/test/syncEntryTest.cpp | 6 +- source/libs/sync/test/syncIOClientTest.cpp | 2 +- source/libs/sync/test/syncIOSendMsgTest.cpp | 7 +- source/libs/sync/test/syncIndexMgrTest.cpp | 3 +- source/libs/sync/test/syncInitTest.cpp | 16 +- source/libs/sync/test/syncLogStoreCheck.cpp | 2 +- source/libs/sync/test/syncLogStoreTest.cpp | 123 ++--- source/libs/sync/test/syncPingReplyTest.cpp | 32 +- source/libs/sync/test/syncPingSelfTest.cpp | 2 +- source/libs/sync/test/syncPingTest.cpp | 32 +- source/libs/sync/test/syncPingTimerTest.cpp | 10 +- source/libs/sync/test/syncPingTimerTest2.cpp | 2 +- source/libs/sync/test/syncRaftCfgTest.cpp | 101 +++++ source/libs/sync/test/syncRaftStoreTest.cpp | 23 +- .../libs/sync/test/syncReplicateLoadTest.cpp | 30 +- source/libs/sync/test/syncReplicateTest.cpp | 31 +- source/libs/sync/test/syncReplicateTest2.cpp | 32 +- source/libs/sync/test/syncReplicateTest3.cpp | 32 +- .../sync/test/syncRequestVoteReplyTest.cpp | 22 +- source/libs/sync/test/syncRequestVoteTest.cpp | 21 +- source/libs/sync/test/syncRespMgrTest.cpp | 144 ++++++ source/libs/sync/test/syncRpcMsgTest.cpp | 35 +- source/libs/sync/test/syncTest.cpp | 62 ++- source/libs/sync/test/syncTimeoutTest.cpp | 17 +- source/libs/sync/test/syncUtilTest.cpp | 3 +- .../libs/sync/test/syncVotesGrantedTest.cpp | 5 +- .../libs/sync/test/syncVotesRespondTest.cpp | 5 +- source/libs/sync/test/syncWriteTest.cpp | 37 +- 66 files changed, 2372 insertions(+), 939 deletions(-) create mode 100644 include/libs/sync/syncTools.h create mode 100644 source/libs/sync/inc/syncRaftCfg.h create mode 100644 source/libs/sync/inc/syncRespMgr.h create mode 100644 source/libs/sync/src/syncRaftCfg.c create mode 100644 source/libs/sync/src/syncRespMgr.c create mode 100644 source/libs/sync/test/syncRaftCfgTest.cpp create mode 100644 source/libs/sync/test/syncRespMgrTest.cpp diff --git a/include/common/tmsgdef.h b/include/common/tmsgdef.h index e553dff270..24d259b0f2 100644 --- a/include/common/tmsgdef.h +++ b/include/common/tmsgdef.h @@ -206,6 +206,19 @@ enum { TD_DEF_MSG_TYPE(TDMT_VND_CANCEL_SMA, "vnode-cancel-sma", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_VND_DROP_SMA, "vnode-drop-sma", NULL, NULL) + TD_DEF_MSG_TYPE(TDMT_VND_SYNC_TIMEOUT, "vnode-sync-timeout", NULL, NULL) + TD_DEF_MSG_TYPE(TDMT_VND_SYNC_PING, "vnode-sync-ping", NULL, NULL) + TD_DEF_MSG_TYPE(TDMT_VND_SYNC_PING_REPLY, "vnode-sync-ping-reply", NULL, NULL) + TD_DEF_MSG_TYPE(TDMT_VND_SYNC_CLIENT_REQUEST, "vnode-sync-client-request", NULL, NULL) + TD_DEF_MSG_TYPE(TDMT_VND_SYNC_CLIENT_REQUEST_REPLY, "vnode-sync-client-request-reply", NULL, NULL) + TD_DEF_MSG_TYPE(TDMT_VND_SYNC_REQUEST_VOTE, "vnode-sync-request-vote", NULL, NULL) + TD_DEF_MSG_TYPE(TDMT_VND_SYNC_REQUEST_VOTE_REPLY, "vnode-sync-request-vote-reply", NULL, NULL) + TD_DEF_MSG_TYPE(TDMT_VND_SYNC_APPEND_ENTRIES, "vnode-sync-append-entries", NULL, NULL) + TD_DEF_MSG_TYPE(TDMT_VND_SYNC_APPEND_ENTRIES_REPLY, "vnode-sync-append-entries-reply", NULL, NULL) + TD_DEF_MSG_TYPE(TDMT_VND_SYNC_NOOP, "vnode-sync-noop", NULL, NULL) + TD_DEF_MSG_TYPE(TDMT_VND_SYNC_UNKNOWN, "vnode-sync-unknown", NULL, NULL) + TD_DEF_MSG_TYPE(TDMT_VND_SYNC_COMMON_RESPONSE, "vnode-sync-common-response", NULL, NULL) + // Requests handled by QNODE TD_NEW_MSG_SEG(TDMT_QND_MSG) diff --git a/include/libs/sync/sync.h b/include/libs/sync/sync.h index a2f88490f0..86666f4c68 100644 --- a/include/libs/sync/sync.h +++ b/include/libs/sync/sync.h @@ -22,6 +22,7 @@ extern "C" { #include #include +#include "cJSON.h" #include "taosdef.h" #include "trpc.h" #include "wal.h" @@ -38,14 +39,9 @@ typedef enum { TAOS_SYNC_STATE_ERROR = 103, } ESyncState; -typedef struct SSyncBuffer { - void* data; - size_t len; -} SSyncBuffer; - typedef struct SNodeInfo { - uint16_t nodePort; // node sync Port - char nodeFqdn[TSDB_FQDN_LEN]; // node FQDN + uint16_t nodePort; + char nodeFqdn[TSDB_FQDN_LEN]; } SNodeInfo; typedef struct SSyncCfg { @@ -54,40 +50,37 @@ typedef struct SSyncCfg { SNodeInfo nodeInfo[TSDB_MAX_REPLICA]; } SSyncCfg; -typedef struct SNodesRole { - int32_t replicaNum; - SNodeInfo nodeInfo[TSDB_MAX_REPLICA]; - ESyncState role[TSDB_MAX_REPLICA]; -} SNodesRole; +typedef struct SRaftId { + SyncNodeId addr; + SyncGroupId vgId; +} SRaftId; -// abstract definition of snapshot typedef struct SSnapshot { void* data; SyncIndex lastApplyIndex; } SSnapshot; +typedef enum { + TAOS_SYNC_FSM_CB_SUCCESS = 0, + TAOS_SYNC_FSM_CB_OTHER_ERROR, +} ESyncFsmCbCode; + +typedef struct SFsmCbMeta { + SyncIndex index; + bool isWeak; + int32_t code; + ESyncState state; + uint64_t seqNum; +} SFsmCbMeta; + typedef struct SSyncFSM { void* data; - // when value in pMsg finish a raft flow, FpCommitCb is called, code indicates the result - // user can do something according to the code and isWeak. for example, write data into tsdb - void (*FpCommitCb)(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SyncIndex index, bool isWeak, int32_t code, - ESyncState state); + void (*FpCommitCb)(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta); + void (*FpPreCommitCb)(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta); + void (*FpRollBackCb)(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta); - // when value in pMsg has been written into local log store, FpPreCommitCb is called, code indicates the result - // user can do something according to the code and isWeak. for example, write data into tsdb - void (*FpPreCommitCb)(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SyncIndex index, bool isWeak, int32_t code, - ESyncState state); - - // when log entry is updated by a new one, FpRollBackCb is called - // user can do something to roll back. for example, delete data from tsdb, or just ignore it - void (*FpRollBackCb)(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SyncIndex index, bool isWeak, int32_t code, - ESyncState state); - - // user should implement this function, use "data" to take snapshot into "snapshot" int32_t (*FpTakeSnapshot)(SSnapshot* snapshot); - - // user should implement this function, restore "data" from "snapshot" int32_t (*FpRestoreSnapshot)(const SSnapshot* snapshot); } SSyncFSM; @@ -123,21 +116,6 @@ typedef struct SSyncLogStore { } SSyncLogStore; -// raft need to persist two variables in storage: currentTerm, voteFor -typedef struct SStateMgr { - void* data; - - int32_t (*getCurrentTerm)(struct SStateMgr* pMgr, SyncTerm* pCurrentTerm); - int32_t (*persistCurrentTerm)(struct SStateMgr* pMgr, SyncTerm pCurrentTerm); - - int32_t (*getVoteFor)(struct SStateMgr* pMgr, SyncNodeId* pVoteFor); - int32_t (*persistVoteFor)(struct SStateMgr* pMgr, SyncNodeId voteFor); - - int32_t (*getSyncCfg)(struct SStateMgr* pMgr, SSyncCfg* pSyncCfg); - int32_t (*persistSyncCfg)(struct SStateMgr* pMgr, SSyncCfg* pSyncCfg); - -} SStateMgr; - typedef struct SSyncInfo { SyncGroupId vgId; SSyncCfg syncCfg; @@ -152,25 +130,33 @@ typedef struct SSyncInfo { } SSyncInfo; +int32_t syncInit(); +void syncCleanUp(); +int64_t syncOpen(const SSyncInfo* pSyncInfo); +void syncStart(int64_t rid); +void syncStop(int64_t rid); +int32_t syncReconfig(int64_t rid, const SSyncCfg* pSyncCfg); +ESyncState syncGetMyRole(int64_t rid); +const char* syncGetMyRoleStr(int64_t rid); +SyncTerm syncGetMyTerm(int64_t rid); + +typedef enum { + TAOS_SYNC_PROPOSE_SUCCESS = 0, + TAOS_SYNC_PROPOSE_NOT_LEADER, + TAOS_SYNC_PROPOSE_OTHER_ERROR, +} ESyncProposeCode; + +int32_t syncPropose(int64_t rid, const SRpcMsg* pMsg, bool isWeak); + +extern int32_t sDebugFlag; + +//----------------------------------------- struct SSyncNode; typedef struct SSyncNode SSyncNode; -int32_t syncInit(); -void syncCleanUp(); - -int64_t syncStart(const SSyncInfo* pSyncInfo); -void syncStop(int64_t rid); -int32_t syncReconfig(int64_t rid, const SSyncCfg* pSyncCfg); -int32_t syncPropose(int64_t rid, const SRpcMsg* pMsg, bool isWeak); -ESyncState syncGetMyRole(int64_t rid); - -// propose with sequence number, to implement linearizable semantics -int32_t syncPropose2(int64_t rid, const SRpcMsg* pMsg, bool isWeak, uint64_t seqNum); - -// for compatibility, the same as syncPropose -int32_t syncForwardToPeer(int64_t rid, const SRpcMsg* pMsg, bool isWeak); - -extern int32_t sDebugFlag; +struct SSyncBuffer; +typedef struct SSyncBuffer SSyncBuffer; +//----------------------------------------- #ifdef __cplusplus } diff --git a/include/libs/sync/syncTools.h b/include/libs/sync/syncTools.h new file mode 100644 index 0000000000..6d28f54e29 --- /dev/null +++ b/include/libs/sync/syncTools.h @@ -0,0 +1,376 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_LIBS_SYNC_TOOLS_H +#define _TD_LIBS_SYNC_TOOLS_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include "cJSON.h" +#include "taosdef.h" +#include "trpc.h" +#include "wal.h" + +// ------------------ control ------------------- +struct SSyncNode; +typedef struct SSyncNode SSyncNode; + +SSyncNode* syncNodeAcquire(int64_t rid); +void syncNodeRelease(SSyncNode* pNode); + +int32_t syncGetRespRpc(int64_t rid, uint64_t index, SRpcMsg* msg); +int32_t syncGetAndDelRespRpc(int64_t rid, uint64_t index, SRpcMsg* msg); +void syncSetQ(int64_t rid, void* queueHandle); +void syncSetRpc(int64_t rid, void* rpcHandle); +char* sync2SimpleStr(int64_t rid); + +// set timer ms +void setPingTimerMS(int64_t rid, int32_t pingTimerMS); +void setElectTimerMS(int64_t rid, int32_t electTimerMS); +void setHeartbeatTimerMS(int64_t rid, int32_t hbTimerMS); + +// for compatibility, the same as syncPropose +int32_t syncForwardToPeer(int64_t rid, const SRpcMsg* pMsg, bool isWeak); + +// utils +const char* syncUtilState2String(ESyncState state); + +// ------------------ for debug ------------------- +void syncRpcMsgPrint(SRpcMsg* pMsg); +void syncRpcMsgPrint2(char* s, SRpcMsg* pMsg); +void syncRpcMsgLog(SRpcMsg* pMsg); +void syncRpcMsgLog2(char* s, SRpcMsg* pMsg); + +// ------------------ for compile ------------------- +typedef struct SSyncBuffer { + void* data; + size_t len; +} SSyncBuffer; + +typedef struct SNodesRole { + int32_t replicaNum; + SNodeInfo nodeInfo[TSDB_MAX_REPLICA]; + ESyncState role[TSDB_MAX_REPLICA]; +} SNodesRole; + +typedef struct SStateMgr { + void* data; + + int32_t (*getCurrentTerm)(struct SStateMgr* pMgr, SyncTerm* pCurrentTerm); + int32_t (*persistCurrentTerm)(struct SStateMgr* pMgr, SyncTerm pCurrentTerm); + + int32_t (*getVoteFor)(struct SStateMgr* pMgr, SyncNodeId* pVoteFor); + int32_t (*persistVoteFor)(struct SStateMgr* pMgr, SyncNodeId voteFor); + + int32_t (*getSyncCfg)(struct SStateMgr* pMgr, SSyncCfg* pSyncCfg); + int32_t (*persistSyncCfg)(struct SStateMgr* pMgr, SSyncCfg* pSyncCfg); + +} SStateMgr; + +// ------------------ for message process ------------------- + +// --------------------------------------------- +typedef struct SyncPing { + uint32_t bytes; + int32_t vgId; + uint32_t msgType; + SRaftId srcId; + SRaftId destId; + // private data + uint32_t dataLen; + char data[]; +} SyncPing; + +SyncPing* syncPingBuild(uint32_t dataLen); +SyncPing* syncPingBuild2(const SRaftId* srcId, const SRaftId* destId, int32_t vgId, const char* str); +SyncPing* syncPingBuild3(const SRaftId* srcId, const SRaftId* destId, int32_t vgId); +void syncPingDestroy(SyncPing* pMsg); +void syncPingSerialize(const SyncPing* pMsg, char* buf, uint32_t bufLen); +void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pMsg); +char* syncPingSerialize2(const SyncPing* pMsg, uint32_t* len); +SyncPing* syncPingDeserialize2(const char* buf, uint32_t len); +int32_t syncPingSerialize3(const SyncPing* pMsg, char* buf, int32_t bufLen); +SyncPing* syncPingDeserialize3(void* buf, int32_t bufLen); +void syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg); +void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pMsg); +SyncPing* syncPingFromRpcMsg2(const SRpcMsg* pRpcMsg); +cJSON* syncPing2Json(const SyncPing* pMsg); +char* syncPing2Str(const SyncPing* pMsg); + +// for debug ---------------------- +void syncPingPrint(const SyncPing* pMsg); +void syncPingPrint2(char* s, const SyncPing* pMsg); +void syncPingLog(const SyncPing* pMsg); +void syncPingLog2(char* s, const SyncPing* pMsg); + +// --------------------------------------------- +typedef struct SyncPingReply { + uint32_t bytes; + int32_t vgId; + uint32_t msgType; + SRaftId srcId; + SRaftId destId; + // private data + uint32_t dataLen; + char data[]; +} SyncPingReply; + +SyncPingReply* syncPingReplyBuild(uint32_t dataLen); +SyncPingReply* syncPingReplyBuild2(const SRaftId* srcId, const SRaftId* destId, int32_t vgId, const char* str); +SyncPingReply* syncPingReplyBuild3(const SRaftId* srcId, const SRaftId* destId, int32_t vgId); +void syncPingReplyDestroy(SyncPingReply* pMsg); +void syncPingReplySerialize(const SyncPingReply* pMsg, char* buf, uint32_t bufLen); +void syncPingReplyDeserialize(const char* buf, uint32_t len, SyncPingReply* pMsg); +char* syncPingReplySerialize2(const SyncPingReply* pMsg, uint32_t* len); +SyncPingReply* syncPingReplyDeserialize2(const char* buf, uint32_t len); +int32_t syncPingReplySerialize3(const SyncPingReply* pMsg, char* buf, int32_t bufLen); +SyncPingReply* syncPingReplyDeserialize3(void* buf, int32_t bufLen); +void syncPingReply2RpcMsg(const SyncPingReply* pMsg, SRpcMsg* pRpcMsg); +void syncPingReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPingReply* pMsg); +SyncPingReply* syncPingReplyFromRpcMsg2(const SRpcMsg* pRpcMsg); +cJSON* syncPingReply2Json(const SyncPingReply* pMsg); +char* syncPingReply2Str(const SyncPingReply* pMsg); + +// for debug ---------------------- +void syncPingReplyPrint(const SyncPingReply* pMsg); +void syncPingReplyPrint2(char* s, const SyncPingReply* pMsg); +void syncPingReplyLog(const SyncPingReply* pMsg); +void syncPingReplyLog2(char* s, const SyncPingReply* pMsg); + +// --------------------------------------------- +typedef enum ESyncTimeoutType { + SYNC_TIMEOUT_PING = 100, + SYNC_TIMEOUT_ELECTION, + SYNC_TIMEOUT_HEARTBEAT, +} ESyncTimeoutType; + +typedef struct SyncTimeout { + uint32_t bytes; + int32_t vgId; + uint32_t msgType; + ESyncTimeoutType timeoutType; + uint64_t logicClock; + int32_t timerMS; + void* data; // need optimized +} SyncTimeout; + +SyncTimeout* syncTimeoutBuild(); +SyncTimeout* syncTimeoutBuild2(ESyncTimeoutType timeoutType, uint64_t logicClock, int32_t timerMS, int32_t vgId, + void* data); +void syncTimeoutDestroy(SyncTimeout* pMsg); +void syncTimeoutSerialize(const SyncTimeout* pMsg, char* buf, uint32_t bufLen); +void syncTimeoutDeserialize(const char* buf, uint32_t len, SyncTimeout* pMsg); +char* syncTimeoutSerialize2(const SyncTimeout* pMsg, uint32_t* len); +SyncTimeout* syncTimeoutDeserialize2(const char* buf, uint32_t len); +void syncTimeout2RpcMsg(const SyncTimeout* pMsg, SRpcMsg* pRpcMsg); +void syncTimeoutFromRpcMsg(const SRpcMsg* pRpcMsg, SyncTimeout* pMsg); +SyncTimeout* syncTimeoutFromRpcMsg2(const SRpcMsg* pRpcMsg); +cJSON* syncTimeout2Json(const SyncTimeout* pMsg); +char* syncTimeout2Str(const SyncTimeout* pMsg); + +// for debug ---------------------- +void syncTimeoutPrint(const SyncTimeout* pMsg); +void syncTimeoutPrint2(char* s, const SyncTimeout* pMsg); +void syncTimeoutLog(const SyncTimeout* pMsg); +void syncTimeoutLog2(char* s, const SyncTimeout* pMsg); + +// --------------------------------------------- +typedef struct SyncClientRequest { + uint32_t bytes; + int32_t vgId; + uint32_t msgType; // SyncClientRequest msgType + uint32_t originalRpcType; // user RpcMsg msgType + uint64_t seqNum; + bool isWeak; + uint32_t dataLen; // user RpcMsg.contLen + char data[]; // user RpcMsg.pCont +} SyncClientRequest; + +SyncClientRequest* syncClientRequestBuild(uint32_t dataLen); +SyncClientRequest* syncClientRequestBuild2(const SRpcMsg* pOriginalRpcMsg, uint64_t seqNum, bool isWeak, + int32_t vgId); // step 1 +void syncClientRequestDestroy(SyncClientRequest* pMsg); +void syncClientRequestSerialize(const SyncClientRequest* pMsg, char* buf, uint32_t bufLen); +void syncClientRequestDeserialize(const char* buf, uint32_t len, SyncClientRequest* pMsg); +char* syncClientRequestSerialize2(const SyncClientRequest* pMsg, uint32_t* len); +SyncClientRequest* syncClientRequestDeserialize2(const char* buf, uint32_t len); +void syncClientRequest2RpcMsg(const SyncClientRequest* pMsg, SRpcMsg* pRpcMsg); // step 2 +void syncClientRequestFromRpcMsg(const SRpcMsg* pRpcMsg, SyncClientRequest* pMsg); +SyncClientRequest* syncClientRequestFromRpcMsg2(const SRpcMsg* pRpcMsg); // step 3 +cJSON* syncClientRequest2Json(const SyncClientRequest* pMsg); +char* syncClientRequest2Str(const SyncClientRequest* pMsg); + +// for debug ---------------------- +void syncClientRequestPrint(const SyncClientRequest* pMsg); +void syncClientRequestPrint2(char* s, const SyncClientRequest* pMsg); +void syncClientRequestLog(const SyncClientRequest* pMsg); +void syncClientRequestLog2(char* s, const SyncClientRequest* pMsg); + +// --------------------------------------------- +typedef struct SyncClientRequestReply { + uint32_t bytes; + int32_t vgId; + uint32_t msgType; + int32_t errCode; + SRaftId leaderHint; +} SyncClientRequestReply; + +// --------------------------------------------- +typedef struct SyncRequestVote { + uint32_t bytes; + int32_t vgId; + uint32_t msgType; + SRaftId srcId; + SRaftId destId; + // private data + SyncTerm term; + SyncIndex lastLogIndex; + SyncTerm lastLogTerm; +} SyncRequestVote; + +SyncRequestVote* syncRequestVoteBuild(int32_t vgId); +void syncRequestVoteDestroy(SyncRequestVote* pMsg); +void syncRequestVoteSerialize(const SyncRequestVote* pMsg, char* buf, uint32_t bufLen); +void syncRequestVoteDeserialize(const char* buf, uint32_t len, SyncRequestVote* pMsg); +char* syncRequestVoteSerialize2(const SyncRequestVote* pMsg, uint32_t* len); +SyncRequestVote* syncRequestVoteDeserialize2(const char* buf, uint32_t len); +void syncRequestVote2RpcMsg(const SyncRequestVote* pMsg, SRpcMsg* pRpcMsg); +void syncRequestVoteFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVote* pMsg); +SyncRequestVote* syncRequestVoteFromRpcMsg2(const SRpcMsg* pRpcMsg); +cJSON* syncRequestVote2Json(const SyncRequestVote* pMsg); +char* syncRequestVote2Str(const SyncRequestVote* pMsg); + +// for debug ---------------------- +void syncRequestVotePrint(const SyncRequestVote* pMsg); +void syncRequestVotePrint2(char* s, const SyncRequestVote* pMsg); +void syncRequestVoteLog(const SyncRequestVote* pMsg); +void syncRequestVoteLog2(char* s, const SyncRequestVote* pMsg); + +// --------------------------------------------- +typedef struct SyncRequestVoteReply { + uint32_t bytes; + int32_t vgId; + uint32_t msgType; + SRaftId srcId; + SRaftId destId; + // private data + SyncTerm term; + bool voteGranted; +} SyncRequestVoteReply; + +SyncRequestVoteReply* syncRequestVoteReplyBuild(int32_t vgId); +void syncRequestVoteReplyDestroy(SyncRequestVoteReply* pMsg); +void syncRequestVoteReplySerialize(const SyncRequestVoteReply* pMsg, char* buf, uint32_t bufLen); +void syncRequestVoteReplyDeserialize(const char* buf, uint32_t len, SyncRequestVoteReply* pMsg); +char* syncRequestVoteReplySerialize2(const SyncRequestVoteReply* pMsg, uint32_t* len); +SyncRequestVoteReply* syncRequestVoteReplyDeserialize2(const char* buf, uint32_t len); +void syncRequestVoteReply2RpcMsg(const SyncRequestVoteReply* pMsg, SRpcMsg* pRpcMsg); +void syncRequestVoteReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVoteReply* pMsg); +SyncRequestVoteReply* syncRequestVoteReplyFromRpcMsg2(const SRpcMsg* pRpcMsg); +cJSON* syncRequestVoteReply2Json(const SyncRequestVoteReply* pMsg); +char* syncRequestVoteReply2Str(const SyncRequestVoteReply* pMsg); + +// for debug ---------------------- +void syncRequestVoteReplyPrint(const SyncRequestVoteReply* pMsg); +void syncRequestVoteReplyPrint2(char* s, const SyncRequestVoteReply* pMsg); +void syncRequestVoteReplyLog(const SyncRequestVoteReply* pMsg); +void syncRequestVoteReplyLog2(char* s, const SyncRequestVoteReply* pMsg); + +// --------------------------------------------- +typedef struct SyncAppendEntries { + uint32_t bytes; + int32_t vgId; + uint32_t msgType; + SRaftId srcId; + SRaftId destId; + // private data + SyncTerm term; + SyncIndex prevLogIndex; + SyncTerm prevLogTerm; + SyncIndex commitIndex; + uint32_t dataLen; + char data[]; +} SyncAppendEntries; + +SyncAppendEntries* syncAppendEntriesBuild(uint32_t dataLen, int32_t vgId); +void syncAppendEntriesDestroy(SyncAppendEntries* pMsg); +void syncAppendEntriesSerialize(const SyncAppendEntries* pMsg, char* buf, uint32_t bufLen); +void syncAppendEntriesDeserialize(const char* buf, uint32_t len, SyncAppendEntries* pMsg); +char* syncAppendEntriesSerialize2(const SyncAppendEntries* pMsg, uint32_t* len); +SyncAppendEntries* syncAppendEntriesDeserialize2(const char* buf, uint32_t len); +void syncAppendEntries2RpcMsg(const SyncAppendEntries* pMsg, SRpcMsg* pRpcMsg); +void syncAppendEntriesFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntries* pMsg); +SyncAppendEntries* syncAppendEntriesFromRpcMsg2(const SRpcMsg* pRpcMsg); +cJSON* syncAppendEntries2Json(const SyncAppendEntries* pMsg); +char* syncAppendEntries2Str(const SyncAppendEntries* pMsg); + +// for debug ---------------------- +void syncAppendEntriesPrint(const SyncAppendEntries* pMsg); +void syncAppendEntriesPrint2(char* s, const SyncAppendEntries* pMsg); +void syncAppendEntriesLog(const SyncAppendEntries* pMsg); +void syncAppendEntriesLog2(char* s, const SyncAppendEntries* pMsg); + +// --------------------------------------------- +typedef struct SyncAppendEntriesReply { + uint32_t bytes; + int32_t vgId; + uint32_t msgType; + SRaftId srcId; + SRaftId destId; + // private data + SyncTerm term; + bool success; + SyncIndex matchIndex; +} SyncAppendEntriesReply; + +SyncAppendEntriesReply* syncAppendEntriesReplyBuild(int32_t vgId); +void syncAppendEntriesReplyDestroy(SyncAppendEntriesReply* pMsg); +void syncAppendEntriesReplySerialize(const SyncAppendEntriesReply* pMsg, char* buf, uint32_t bufLen); +void syncAppendEntriesReplyDeserialize(const char* buf, uint32_t len, SyncAppendEntriesReply* pMsg); +char* syncAppendEntriesReplySerialize2(const SyncAppendEntriesReply* pMsg, uint32_t* len); +SyncAppendEntriesReply* syncAppendEntriesReplyDeserialize2(const char* buf, uint32_t len); +void syncAppendEntriesReply2RpcMsg(const SyncAppendEntriesReply* pMsg, SRpcMsg* pRpcMsg); +void syncAppendEntriesReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntriesReply* pMsg); +SyncAppendEntriesReply* syncAppendEntriesReplyFromRpcMsg2(const SRpcMsg* pRpcMsg); +cJSON* syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg); +char* syncAppendEntriesReply2Str(const SyncAppendEntriesReply* pMsg); + +// for debug ---------------------- +void syncAppendEntriesReplyPrint(const SyncAppendEntriesReply* pMsg); +void syncAppendEntriesReplyPrint2(char* s, const SyncAppendEntriesReply* pMsg); +void syncAppendEntriesReplyLog(const SyncAppendEntriesReply* pMsg); +void syncAppendEntriesReplyLog2(char* s, const SyncAppendEntriesReply* pMsg); + +// on message ---------------------- +int32_t syncNodeOnPingCb(SSyncNode* ths, SyncPing* pMsg); +int32_t syncNodeOnPingReplyCb(SSyncNode* ths, SyncPingReply* pMsg); +int32_t syncNodeOnTimeoutCb(SSyncNode* ths, SyncTimeout* pMsg); +int32_t syncNodeOnClientRequestCb(SSyncNode* ths, SyncClientRequest* pMsg); +int32_t syncNodeOnRequestVoteCb(SSyncNode* ths, SyncRequestVote* pMsg); +int32_t syncNodeOnRequestVoteReplyCb(SSyncNode* ths, SyncRequestVoteReply* pMsg); +int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg); +int32_t syncNodeOnAppendEntriesReplyCb(SSyncNode* ths, SyncAppendEntriesReply* pMsg); + +//--------------------- + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_LIBS_SYNC_TOOLS_H*/ diff --git a/source/libs/sync/CMakeLists.txt b/source/libs/sync/CMakeLists.txt index 551849c6f2..cb196acc02 100644 --- a/source/libs/sync/CMakeLists.txt +++ b/source/libs/sync/CMakeLists.txt @@ -11,7 +11,7 @@ target_link_libraries( target_include_directories( sync - PUBLIC "${TD_SOURCE_DIR}/include/libs/sync" + PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/sync" PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) diff --git a/source/libs/sync/inc/syncEnv.h b/source/libs/sync/inc/syncEnv.h index 7940d7f436..e9550ff989 100644 --- a/source/libs/sync/inc/syncEnv.h +++ b/source/libs/sync/inc/syncEnv.h @@ -31,10 +31,10 @@ extern "C" { #define TIMER_MAX_MS 0x7FFFFFFF #define ENV_TICK_TIMER_MS 1000 #define PING_TIMER_MS 1000 -#define ELECT_TIMER_MS_MIN 150 +#define ELECT_TIMER_MS_MIN 500 #define ELECT_TIMER_MS_MAX (ELECT_TIMER_MS_MIN * 2) #define ELECT_TIMER_MS_RANGE (ELECT_TIMER_MS_MAX - ELECT_TIMER_MS_MIN) -#define HEARTBEAT_TIMER_MS 30 +#define HEARTBEAT_TIMER_MS 100 #define EMPTY_RAFT_ID ((SRaftId){.addr = 0, .vgId = 0}) diff --git a/source/libs/sync/inc/syncInt.h b/source/libs/sync/inc/syncInt.h index cfa87048a7..cb539e8379 100644 --- a/source/libs/sync/inc/syncInt.h +++ b/source/libs/sync/inc/syncInt.h @@ -25,6 +25,7 @@ extern "C" { #include #include "cJSON.h" #include "sync.h" +#include "syncTools.h" #include "taosdef.h" #include "tglobal.h" #include "tlog.h" @@ -67,6 +68,43 @@ extern "C" { } \ } +#define sFatalLong(...) \ + { \ + if (sDebugFlag & DEBUG_FATAL) { \ + taosPrintLongString("SYN FATAL ", DEBUG_FATAL, 255, __VA_ARGS__); \ + } \ + } +#define sErrorLong(...) \ + { \ + if (sDebugFlag & DEBUG_ERROR) { \ + taosPrintLongString("SYN ERROR ", DEBUG_ERROR, 255, __VA_ARGS__); \ + } \ + } +#define sWarnLong(...) \ + { \ + if (sDebugFlag & DEBUG_WARN) { \ + taosPrintLongString("SYN WARN ", DEBUG_WARN, 255, __VA_ARGS__); \ + } \ + } +#define sInfoLong(...) \ + { \ + if (sDebugFlag & DEBUG_INFO) { \ + taosPrintLongString("SYN INFO ", DEBUG_INFO, 255, __VA_ARGS__); \ + } \ + } +#define sDebugLong(...) \ + { \ + if (sDebugFlag & DEBUG_DEBUG) { \ + taosPrintLongString("SYN DEBUG ", DEBUG_DEBUG, sDebugFlag, __VA_ARGS__); \ + } \ + } +#define sTraceLong(...) \ + { \ + if (sDebugFlag & DEBUG_TRACE) { \ + taosPrintLongString("SYN TRACE ", DEBUG_TRACE, sDebugFlag, __VA_ARGS__); \ + } \ + } + struct SyncTimeout; typedef struct SyncTimeout SyncTimeout; @@ -106,17 +144,19 @@ typedef struct SVotesRespond SVotesRespond; struct SSyncIndexMgr; typedef struct SSyncIndexMgr SSyncIndexMgr; -typedef struct SRaftId { - SyncNodeId addr; // typedef uint64_t SyncNodeId; - SyncGroupId vgId; // typedef int32_t SyncGroupId; -} SRaftId; +struct SRaftCfg; +typedef struct SRaftCfg SRaftCfg; + +struct SSyncRespMgr; +typedef struct SSyncRespMgr SSyncRespMgr; typedef struct SSyncNode { // init by SSyncInfo SyncGroupId vgId; - SSyncCfg syncCfg; + SRaftCfg* pRaftCfg; char path[TSDB_FILENAME_LEN]; char raftStorePath[TSDB_FILENAME_LEN * 2]; + char configPath[TSDB_FILENAME_LEN * 2]; // sync io SWal* pWal; @@ -199,10 +239,14 @@ typedef struct SSyncNode { int32_t (*FpOnAppendEntriesReply)(SSyncNode* ths, SyncAppendEntriesReply* pMsg); int32_t (*FpOnTimeout)(SSyncNode* pSyncNode, SyncTimeout* pMsg); + // tools + SSyncRespMgr* pSyncRespMgr; + } SSyncNode; // open/close -------------- SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo); +void syncNodeStart(SSyncNode* pSyncNode); void syncNodeClose(SSyncNode* pSyncNode); // ping -------------- @@ -222,10 +266,12 @@ int32_t syncNodeStartHeartbeatTimer(SSyncNode* pSyncNode); int32_t syncNodeStopHeartbeatTimer(SSyncNode* pSyncNode); // utils -------------- -int32_t syncNodeSendMsgById(const SRaftId* destRaftId, SSyncNode* pSyncNode, SRpcMsg* pMsg); -int32_t syncNodeSendMsgByInfo(const SNodeInfo* nodeInfo, SSyncNode* pSyncNode, SRpcMsg* pMsg); -cJSON* syncNode2Json(const SSyncNode* pSyncNode); -char* syncNode2Str(const SSyncNode* pSyncNode); +int32_t syncNodeSendMsgById(const SRaftId* destRaftId, SSyncNode* pSyncNode, SRpcMsg* pMsg); +int32_t syncNodeSendMsgByInfo(const SNodeInfo* nodeInfo, SSyncNode* pSyncNode, SRpcMsg* pMsg); +cJSON* syncNode2Json(const SSyncNode* pSyncNode); +char* syncNode2Str(const SSyncNode* pSyncNode); +char* syncNode2SimpleStr(const SSyncNode* pSyncNode); + SSyncNode* syncNodeAcquire(int64_t rid); void syncNodeRelease(SSyncNode* pNode); diff --git a/source/libs/sync/inc/syncMessage.h b/source/libs/sync/inc/syncMessage.h index ffb2ae3a26..19079d518d 100644 --- a/source/libs/sync/inc/syncMessage.h +++ b/source/libs/sync/inc/syncMessage.h @@ -27,22 +27,6 @@ extern "C" { #include "syncInt.h" #include "taosdef.h" -// encode as uint32 -typedef enum ESyncMessageType { - SYNC_UNKNOWN = 9999, - SYNC_TIMEOUT = 99, - SYNC_PING = 101, - SYNC_PING_REPLY = 103, - SYNC_CLIENT_REQUEST = 105, - SYNC_CLIENT_REQUEST_REPLY = 107, - SYNC_REQUEST_VOTE = 109, - SYNC_REQUEST_VOTE_REPLY = 111, - SYNC_APPEND_ENTRIES = 113, - SYNC_APPEND_ENTRIES_REPLY = 115, - SYNC_RESPONSE = 119, - -} ESyncMessageType; - // --------------------------------------------- cJSON* syncRpcMsg2Json(SRpcMsg* pRpcMsg); cJSON* syncRpcUnknownMsg2Json(); @@ -53,263 +37,7 @@ void syncRpcMsgPrint(SRpcMsg* pMsg); void syncRpcMsgPrint2(char* s, SRpcMsg* pMsg); void syncRpcMsgLog(SRpcMsg* pMsg); void syncRpcMsgLog2(char* s, SRpcMsg* pMsg); - // --------------------------------------------- -typedef enum ESyncTimeoutType { - SYNC_TIMEOUT_PING = 100, - SYNC_TIMEOUT_ELECTION, - SYNC_TIMEOUT_HEARTBEAT, -} ESyncTimeoutType; - -typedef struct SyncTimeout { - uint32_t bytes; - uint32_t msgType; - ESyncTimeoutType timeoutType; - uint64_t logicClock; - int32_t timerMS; - void* data; // need optimized -} SyncTimeout; - -SyncTimeout* syncTimeoutBuild(); -SyncTimeout* syncTimeoutBuild2(ESyncTimeoutType timeoutType, uint64_t logicClock, int32_t timerMS, void* data); -void syncTimeoutDestroy(SyncTimeout* pMsg); -void syncTimeoutSerialize(const SyncTimeout* pMsg, char* buf, uint32_t bufLen); -void syncTimeoutDeserialize(const char* buf, uint32_t len, SyncTimeout* pMsg); -char* syncTimeoutSerialize2(const SyncTimeout* pMsg, uint32_t* len); // -SyncTimeout* syncTimeoutDeserialize2(const char* buf, uint32_t len); // -void syncTimeout2RpcMsg(const SyncTimeout* pMsg, SRpcMsg* pRpcMsg); -void syncTimeoutFromRpcMsg(const SRpcMsg* pRpcMsg, SyncTimeout* pMsg); -SyncTimeout* syncTimeoutFromRpcMsg2(const SRpcMsg* pRpcMsg); // -cJSON* syncTimeout2Json(const SyncTimeout* pMsg); -char* syncTimeout2Str(const SyncTimeout* pMsg); // - -// for debug ---------------------- -void syncTimeoutPrint(const SyncTimeout* pMsg); -void syncTimeoutPrint2(char* s, const SyncTimeout* pMsg); -void syncTimeoutLog(const SyncTimeout* pMsg); -void syncTimeoutLog2(char* s, const SyncTimeout* pMsg); - -// --------------------------------------------- -typedef struct SyncPing { - uint32_t bytes; - uint32_t msgType; - SRaftId srcId; - SRaftId destId; - // private data - uint32_t dataLen; - char data[]; -} SyncPing; - -SyncPing* syncPingBuild(uint32_t dataLen); -SyncPing* syncPingBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str); -SyncPing* syncPingBuild3(const SRaftId* srcId, const SRaftId* destId); -void syncPingDestroy(SyncPing* pMsg); -void syncPingSerialize(const SyncPing* pMsg, char* buf, uint32_t bufLen); -void syncPingDeserialize(const char* buf, uint32_t len, SyncPing* pMsg); -char* syncPingSerialize2(const SyncPing* pMsg, uint32_t* len); -SyncPing* syncPingDeserialize2(const char* buf, uint32_t len); -void syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg); -void syncPingFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPing* pMsg); -SyncPing* syncPingFromRpcMsg2(const SRpcMsg* pRpcMsg); -cJSON* syncPing2Json(const SyncPing* pMsg); -char* syncPing2Str(const SyncPing* pMsg); - -// for debug ---------------------- -void syncPingPrint(const SyncPing* pMsg); -void syncPingPrint2(char* s, const SyncPing* pMsg); -void syncPingLog(const SyncPing* pMsg); -void syncPingLog2(char* s, const SyncPing* pMsg); - -// --------------------------------------------- -typedef struct SyncPingReply { - uint32_t bytes; - uint32_t msgType; - SRaftId srcId; - SRaftId destId; - // private data - uint32_t dataLen; - char data[]; -} SyncPingReply; - -SyncPingReply* syncPingReplyBuild(uint32_t dataLen); -SyncPingReply* syncPingReplyBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str); -SyncPingReply* syncPingReplyBuild3(const SRaftId* srcId, const SRaftId* destId); -void syncPingReplyDestroy(SyncPingReply* pMsg); -void syncPingReplySerialize(const SyncPingReply* pMsg, char* buf, uint32_t bufLen); -void syncPingReplyDeserialize(const char* buf, uint32_t len, SyncPingReply* pMsg); -char* syncPingReplySerialize2(const SyncPingReply* pMsg, uint32_t* len); // -SyncPingReply* syncPingReplyDeserialize2(const char* buf, uint32_t len); // -void syncPingReply2RpcMsg(const SyncPingReply* pMsg, SRpcMsg* pRpcMsg); -void syncPingReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncPingReply* pMsg); -SyncPingReply* syncPingReplyFromRpcMsg2(const SRpcMsg* pRpcMsg); // -cJSON* syncPingReply2Json(const SyncPingReply* pMsg); -char* syncPingReply2Str(const SyncPingReply* pMsg); // - -// for debug ---------------------- -void syncPingReplyPrint(const SyncPingReply* pMsg); -void syncPingReplyPrint2(char* s, const SyncPingReply* pMsg); -void syncPingReplyLog(const SyncPingReply* pMsg); -void syncPingReplyLog2(char* s, const SyncPingReply* pMsg); - -// --------------------------------------------- -typedef struct SyncClientRequest { - uint32_t bytes; - uint32_t msgType; - uint32_t originalRpcType; - uint64_t seqNum; - bool isWeak; - uint32_t dataLen; - char data[]; -} SyncClientRequest; - -SyncClientRequest* syncClientRequestBuild(uint32_t dataLen); -SyncClientRequest* syncClientRequestBuild2(const SRpcMsg* pOriginalRpcMsg, uint64_t seqNum, bool isWeak); // step 1 -void syncClientRequestDestroy(SyncClientRequest* pMsg); -void syncClientRequestSerialize(const SyncClientRequest* pMsg, char* buf, uint32_t bufLen); -void syncClientRequestDeserialize(const char* buf, uint32_t len, SyncClientRequest* pMsg); -char* syncClientRequestSerialize2(const SyncClientRequest* pMsg, uint32_t* len); -SyncClientRequest* syncClientRequestDeserialize2(const char* buf, uint32_t len); -void syncClientRequest2RpcMsg(const SyncClientRequest* pMsg, SRpcMsg* pRpcMsg); // step 2 -void syncClientRequestFromRpcMsg(const SRpcMsg* pRpcMsg, SyncClientRequest* pMsg); -SyncClientRequest* syncClientRequestFromRpcMsg2(const SRpcMsg* pRpcMsg); // step 3 -cJSON* syncClientRequest2Json(const SyncClientRequest* pMsg); -char* syncClientRequest2Str(const SyncClientRequest* pMsg); - -// for debug ---------------------- -void syncClientRequestPrint(const SyncClientRequest* pMsg); -void syncClientRequestPrint2(char* s, const SyncClientRequest* pMsg); -void syncClientRequestLog(const SyncClientRequest* pMsg); -void syncClientRequestLog2(char* s, const SyncClientRequest* pMsg); - -// --------------------------------------------- -typedef struct SyncClientRequestReply { - uint32_t bytes; - uint32_t msgType; - int32_t errCode; - SRaftId leaderHint; -} SyncClientRequestReply; - -// --------------------------------------------- -typedef struct SyncRequestVote { - uint32_t bytes; - uint32_t msgType; - SRaftId srcId; - SRaftId destId; - // private data - SyncTerm term; - SyncIndex lastLogIndex; - SyncTerm lastLogTerm; -} SyncRequestVote; - -SyncRequestVote* syncRequestVoteBuild(); -void syncRequestVoteDestroy(SyncRequestVote* pMsg); -void syncRequestVoteSerialize(const SyncRequestVote* pMsg, char* buf, uint32_t bufLen); -void syncRequestVoteDeserialize(const char* buf, uint32_t len, SyncRequestVote* pMsg); -char* syncRequestVoteSerialize2(const SyncRequestVote* pMsg, uint32_t* len); -SyncRequestVote* syncRequestVoteDeserialize2(const char* buf, uint32_t len); -void syncRequestVote2RpcMsg(const SyncRequestVote* pMsg, SRpcMsg* pRpcMsg); -void syncRequestVoteFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVote* pMsg); -SyncRequestVote* syncRequestVoteFromRpcMsg2(const SRpcMsg* pRpcMsg); -cJSON* syncRequestVote2Json(const SyncRequestVote* pMsg); -char* syncRequestVote2Str(const SyncRequestVote* pMsg); - -// for debug ---------------------- -void syncRequestVotePrint(const SyncRequestVote* pMsg); -void syncRequestVotePrint2(char* s, const SyncRequestVote* pMsg); -void syncRequestVoteLog(const SyncRequestVote* pMsg); -void syncRequestVoteLog2(char* s, const SyncRequestVote* pMsg); - -// --------------------------------------------- -typedef struct SyncRequestVoteReply { - uint32_t bytes; - uint32_t msgType; - SRaftId srcId; - SRaftId destId; - // private data - SyncTerm term; - bool voteGranted; -} SyncRequestVoteReply; - -SyncRequestVoteReply* syncRequestVoteReplyBuild(); -void syncRequestVoteReplyDestroy(SyncRequestVoteReply* pMsg); -void syncRequestVoteReplySerialize(const SyncRequestVoteReply* pMsg, char* buf, uint32_t bufLen); -void syncRequestVoteReplyDeserialize(const char* buf, uint32_t len, SyncRequestVoteReply* pMsg); -char* syncRequestVoteReplySerialize2(const SyncRequestVoteReply* pMsg, uint32_t* len); -SyncRequestVoteReply* syncRequestVoteReplyDeserialize2(const char* buf, uint32_t len); -void syncRequestVoteReply2RpcMsg(const SyncRequestVoteReply* pMsg, SRpcMsg* pRpcMsg); -void syncRequestVoteReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncRequestVoteReply* pMsg); -SyncRequestVoteReply* syncRequestVoteReplyFromRpcMsg2(const SRpcMsg* pRpcMsg); -cJSON* syncRequestVoteReply2Json(const SyncRequestVoteReply* pMsg); -char* syncRequestVoteReply2Str(const SyncRequestVoteReply* pMsg); - -// for debug ---------------------- -void syncRequestVoteReplyPrint(const SyncRequestVoteReply* pMsg); -void syncRequestVoteReplyPrint2(char* s, const SyncRequestVoteReply* pMsg); -void syncRequestVoteReplyLog(const SyncRequestVoteReply* pMsg); -void syncRequestVoteReplyLog2(char* s, const SyncRequestVoteReply* pMsg); - -// --------------------------------------------- -typedef struct SyncAppendEntries { - uint32_t bytes; - uint32_t msgType; - SRaftId srcId; - SRaftId destId; - // private data - SyncTerm term; - SyncIndex prevLogIndex; - SyncTerm prevLogTerm; - SyncIndex commitIndex; - uint32_t dataLen; - char data[]; -} SyncAppendEntries; - -SyncAppendEntries* syncAppendEntriesBuild(uint32_t dataLen); -void syncAppendEntriesDestroy(SyncAppendEntries* pMsg); -void syncAppendEntriesSerialize(const SyncAppendEntries* pMsg, char* buf, uint32_t bufLen); -void syncAppendEntriesDeserialize(const char* buf, uint32_t len, SyncAppendEntries* pMsg); -char* syncAppendEntriesSerialize2(const SyncAppendEntries* pMsg, uint32_t* len); -SyncAppendEntries* syncAppendEntriesDeserialize2(const char* buf, uint32_t len); -void syncAppendEntries2RpcMsg(const SyncAppendEntries* pMsg, SRpcMsg* pRpcMsg); -void syncAppendEntriesFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntries* pMsg); -SyncAppendEntries* syncAppendEntriesFromRpcMsg2(const SRpcMsg* pRpcMsg); -cJSON* syncAppendEntries2Json(const SyncAppendEntries* pMsg); -char* syncAppendEntries2Str(const SyncAppendEntries* pMsg); - -// for debug ---------------------- -void syncAppendEntriesPrint(const SyncAppendEntries* pMsg); -void syncAppendEntriesPrint2(char* s, const SyncAppendEntries* pMsg); -void syncAppendEntriesLog(const SyncAppendEntries* pMsg); -void syncAppendEntriesLog2(char* s, const SyncAppendEntries* pMsg); - -// --------------------------------------------- -typedef struct SyncAppendEntriesReply { - uint32_t bytes; - uint32_t msgType; - SRaftId srcId; - SRaftId destId; - // private data - SyncTerm term; - bool success; - SyncIndex matchIndex; -} SyncAppendEntriesReply; - -SyncAppendEntriesReply* syncAppendEntriesReplyBuild(); -void syncAppendEntriesReplyDestroy(SyncAppendEntriesReply* pMsg); -void syncAppendEntriesReplySerialize(const SyncAppendEntriesReply* pMsg, char* buf, uint32_t bufLen); -void syncAppendEntriesReplyDeserialize(const char* buf, uint32_t len, SyncAppendEntriesReply* pMsg); -char* syncAppendEntriesReplySerialize2(const SyncAppendEntriesReply* pMsg, uint32_t* len); -SyncAppendEntriesReply* syncAppendEntriesReplyDeserialize2(const char* buf, uint32_t len); -void syncAppendEntriesReply2RpcMsg(const SyncAppendEntriesReply* pMsg, SRpcMsg* pRpcMsg); -void syncAppendEntriesReplyFromRpcMsg(const SRpcMsg* pRpcMsg, SyncAppendEntriesReply* pMsg); -SyncAppendEntriesReply* syncAppendEntriesReplyFromRpcMsg2(const SRpcMsg* pRpcMsg); -cJSON* syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg); -char* syncAppendEntriesReply2Str(const SyncAppendEntriesReply* pMsg); - -// for debug ---------------------- -void syncAppendEntriesReplyPrint(const SyncAppendEntriesReply* pMsg); -void syncAppendEntriesReplyPrint2(char* s, const SyncAppendEntriesReply* pMsg); -void syncAppendEntriesReplyLog(const SyncAppendEntriesReply* pMsg); -void syncAppendEntriesReplyLog2(char* s, const SyncAppendEntriesReply* pMsg); #ifdef __cplusplus } diff --git a/source/libs/sync/inc/syncRaftCfg.h b/source/libs/sync/inc/syncRaftCfg.h new file mode 100644 index 0000000000..bfc64cb7b6 --- /dev/null +++ b/source/libs/sync/inc/syncRaftCfg.h @@ -0,0 +1,65 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_LIBS_SYNC_RAFT_CFG_H +#define _TD_LIBS_SYNC_RAFT_CFG_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include "cJSON.h" +#include "syncInt.h" +#include "taosdef.h" + +typedef struct SRaftCfg { + SSyncCfg cfg; + TdFilePtr pFile; + char path[TSDB_FILENAME_LEN * 2]; +} SRaftCfg; + +SRaftCfg *raftCfgOpen(const char *path); +int32_t raftCfgClose(SRaftCfg *pRaftCfg); +int32_t raftCfgPersist(SRaftCfg *pRaftCfg); + +cJSON * syncCfg2Json(SSyncCfg *pSyncCfg); +char * syncCfg2Str(SSyncCfg *pSyncCfg); +int32_t syncCfgFromJson(const cJSON *pRoot, SSyncCfg *pSyncCfg); +int32_t syncCfgFromStr(const char *s, SSyncCfg *pSyncCfg); + +cJSON *raftCfg2Json(SRaftCfg *pRaftCfg); +char * raftCfg2Str(SRaftCfg *pRaftCfg); + +int32_t syncCfgCreateFile(SSyncCfg *pCfg, const char *path); + +// for debug ---------------------- +void syncCfgPrint(SSyncCfg *pCfg); +void syncCfgPrint2(char *s, SSyncCfg *pCfg); +void syncCfgLog(SSyncCfg *pCfg); +void syncCfgLog2(char *s, SSyncCfg *pCfg); + +void raftCfgPrint(SRaftCfg *pCfg); +void raftCfgPrint2(char *s, SRaftCfg *pCfg); +void raftCfgLog(SRaftCfg *pCfg); +void raftCfgLog2(char *s, SRaftCfg *pCfg); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_LIBS_SYNC_RAFT_CFG_H*/ diff --git a/source/libs/sync/inc/syncRaftEntry.h b/source/libs/sync/inc/syncRaftEntry.h index d4e63e9676..b130186516 100644 --- a/source/libs/sync/inc/syncRaftEntry.h +++ b/source/libs/sync/inc/syncRaftEntry.h @@ -27,29 +27,22 @@ extern "C" { #include "syncMessage.h" #include "taosdef.h" -typedef enum EntryType { - SYNC_RAFT_ENTRY_NOOP = 0, - SYNC_RAFT_ENTRY_DATA = 1, - SYNC_RAFT_ENTRY_CONFIG = 2, -} EntryType; - typedef struct SSyncRaftEntry { uint32_t bytes; - uint32_t msgType; - uint32_t originalRpcType; + uint32_t msgType; // SyncClientRequest msgType + uint32_t originalRpcType; // user RpcMsg msgType uint64_t seqNum; bool isWeak; SyncTerm term; SyncIndex index; - EntryType entryType; - uint32_t dataLen; - char data[]; + uint32_t dataLen; // user RpcMsg.contLen + char data[]; // user RpcMsg.pCont } SSyncRaftEntry; SSyncRaftEntry* syncEntryBuild(uint32_t dataLen); SSyncRaftEntry* syncEntryBuild2(SyncClientRequest* pMsg, SyncTerm term, SyncIndex index); // step 4 -SSyncRaftEntry* syncEntryBuild3(SyncClientRequest* pMsg, SyncTerm term, SyncIndex index, EntryType entryType); -SSyncRaftEntry* syncEntryBuildNoop(SyncTerm term, SyncIndex index); +SSyncRaftEntry* syncEntryBuild3(SyncClientRequest* pMsg, SyncTerm term, SyncIndex index); +SSyncRaftEntry* syncEntryBuildNoop(SyncTerm term, SyncIndex index, int32_t vgId); void syncEntryDestory(SSyncRaftEntry* pEntry); char* syncEntrySerialize(const SSyncRaftEntry* pEntry, uint32_t* len); // step 5 SSyncRaftEntry* syncEntryDeserialize(const char* buf, uint32_t len); // step 6 diff --git a/source/libs/sync/inc/syncRaftStore.h b/source/libs/sync/inc/syncRaftStore.h index 355a08ac84..e0cbcf0744 100644 --- a/source/libs/sync/inc/syncRaftStore.h +++ b/source/libs/sync/inc/syncRaftStore.h @@ -28,7 +28,7 @@ extern "C" { #include "taosdef.h" #define RAFT_STORE_BLOCK_SIZE 512 -#define RAFT_STORE_PATH_LEN 128 +#define RAFT_STORE_PATH_LEN (TSDB_FILENAME_LEN * 2) typedef struct SRaftStore { SyncTerm currentTerm; diff --git a/source/libs/sync/inc/syncRespMgr.h b/source/libs/sync/inc/syncRespMgr.h new file mode 100644 index 0000000000..e37c0bb625 --- /dev/null +++ b/source/libs/sync/inc/syncRespMgr.h @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_LIBS_SYNC_RESP_MGR_H +#define _TD_LIBS_SYNC_RESP_MGR_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include +#include +#include "syncInt.h" +#include "taosdef.h" + +typedef struct SRespStub { + SRpcMsg rpcMsg; + int64_t createTime; +} SRespStub; + +typedef struct SSyncRespMgr { + SHashObj * pRespHash; + int64_t ttl; + void * data; + TdThreadMutex mutex; + uint64_t seqNum; +} SSyncRespMgr; + +SSyncRespMgr *syncRespMgrCreate(void *data, int64_t ttl); +void syncRespMgrDestroy(SSyncRespMgr *pObj); +int64_t syncRespMgrAdd(SSyncRespMgr *pObj, SRespStub *pStub); +int32_t syncRespMgrDel(SSyncRespMgr *pObj, uint64_t index); +int32_t syncRespMgrGet(SSyncRespMgr *pObj, uint64_t index, SRespStub *pStub); +int32_t syncRespMgrGetAndDel(SSyncRespMgr *pObj, uint64_t index, SRespStub *pStub); +void syncRespClean(SSyncRespMgr *pObj); +void syncRespCleanByTTL(SSyncRespMgr *pObj, int64_t ttl); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_LIBS_SYNC_RESP_MGR_H*/ diff --git a/source/libs/sync/inc/syncSnapshot.h b/source/libs/sync/inc/syncSnapshot.h index 611f33a0f2..fd2119ce65 100644 --- a/source/libs/sync/inc/syncSnapshot.h +++ b/source/libs/sync/inc/syncSnapshot.h @@ -27,7 +27,6 @@ extern "C" { #include "taosdef.h" int32_t takeSnapshot(SSyncFSM *pFsm, SSnapshot *pSnapshot); - int32_t restoreSnapshot(SSyncFSM *pFsm, SSnapshot *pSnapshot); #ifdef __cplusplus diff --git a/source/libs/sync/inc/syncUtil.h b/source/libs/sync/inc/syncUtil.h index a4bb11afc6..83f31c5dac 100644 --- a/source/libs/sync/inc/syncUtil.h +++ b/source/libs/sync/inc/syncUtil.h @@ -55,6 +55,8 @@ char* syncUtilprintBin(char* ptr, uint32_t len); char* syncUtilprintBin2(char* ptr, uint32_t len); SyncIndex syncUtilMinIndex(SyncIndex a, SyncIndex b); SyncIndex syncUtilMaxIndex(SyncIndex a, SyncIndex b); +void syncUtilMsgHtoN(void* msg); +void syncUtilMsgNtoH(void* msg); #ifdef __cplusplus } diff --git a/source/libs/sync/src/syncAppendEntries.c b/source/libs/sync/src/syncAppendEntries.c index 2e9cd63de6..6623ed1caa 100644 --- a/source/libs/sync/src/syncAppendEntries.c +++ b/source/libs/sync/src/syncAppendEntries.c @@ -125,7 +125,7 @@ int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg) { "logOK:%d", pMsg->term, ths->pRaftStore->currentTerm, ths->state, logOK); - SyncAppendEntriesReply* pReply = syncAppendEntriesReplyBuild(); + SyncAppendEntriesReply* pReply = syncAppendEntriesReplyBuild(ths->vgId); pReply->srcId = ths->myRaftId; pReply->destId = pMsg->srcId; pReply->term = ths->pRaftStore->currentTerm; @@ -199,13 +199,20 @@ int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg) { SSyncRaftEntry* pRollBackEntry = logStoreGetEntry(ths->pLogStore, index); assert(pRollBackEntry != NULL); - // maybe is a NOOP ENTRY - // assert(pRollBackEntry->entryType == SYNC_RAFT_ENTRY_DATA); + if (pRollBackEntry->msgType != TDMT_VND_SYNC_NOOP) { + SRpcMsg rpcMsg; + syncEntry2OriginalRpc(pRollBackEntry, &rpcMsg); + + SFsmCbMeta cbMeta; + cbMeta.index = pRollBackEntry->index; + cbMeta.isWeak = pRollBackEntry->isWeak; + cbMeta.code = 0; + cbMeta.state = ths->state; + cbMeta.seqNum = pRollBackEntry->seqNum; + ths->pFsm->FpRollBackCb(ths->pFsm, &rpcMsg, cbMeta); + rpcFreeCont(rpcMsg.pCont); + } - SRpcMsg rpcMsg; - syncEntry2OriginalRpc(pRollBackEntry, &rpcMsg); - ths->pFsm->FpRollBackCb(ths->pFsm, &rpcMsg, pRollBackEntry->index, pRollBackEntry->isWeak, 0, ths->state); - rpcFreeCont(rpcMsg.pCont); syncEntryDestory(pRollBackEntry); } } @@ -220,8 +227,14 @@ int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg) { SRpcMsg rpcMsg; syncEntry2OriginalRpc(pAppendEntry, &rpcMsg); if (ths->pFsm != NULL) { - if (ths->pFsm->FpPreCommitCb != NULL && pAppendEntry->entryType == SYNC_RAFT_ENTRY_DATA) { - ths->pFsm->FpPreCommitCb(ths->pFsm, &rpcMsg, pAppendEntry->index, pAppendEntry->isWeak, 2, ths->state); + if (ths->pFsm->FpPreCommitCb != NULL && pAppendEntry->originalRpcType != TDMT_VND_SYNC_NOOP) { + SFsmCbMeta cbMeta; + cbMeta.index = pAppendEntry->index; + cbMeta.isWeak = pAppendEntry->isWeak; + cbMeta.code = 2; + cbMeta.state = ths->state; + cbMeta.seqNum = pAppendEntry->seqNum; + ths->pFsm->FpPreCommitCb(ths->pFsm, &rpcMsg, cbMeta); } } rpcFreeCont(rpcMsg.pCont); @@ -245,8 +258,14 @@ int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg) { SRpcMsg rpcMsg; syncEntry2OriginalRpc(pAppendEntry, &rpcMsg); if (ths->pFsm != NULL) { - if (ths->pFsm->FpPreCommitCb != NULL && pAppendEntry->entryType == SYNC_RAFT_ENTRY_DATA) { - ths->pFsm->FpPreCommitCb(ths->pFsm, &rpcMsg, pAppendEntry->index, pAppendEntry->isWeak, 3, ths->state); + if (ths->pFsm->FpPreCommitCb != NULL && pAppendEntry->originalRpcType != TDMT_VND_SYNC_NOOP) { + SFsmCbMeta cbMeta; + cbMeta.index = pAppendEntry->index; + cbMeta.isWeak = pAppendEntry->isWeak; + cbMeta.code = 3; + cbMeta.state = ths->state; + cbMeta.seqNum = pAppendEntry->seqNum; + ths->pFsm->FpPreCommitCb(ths->pFsm, &rpcMsg, cbMeta); } } rpcFreeCont(rpcMsg.pCont); @@ -261,7 +280,7 @@ int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg) { assert(0); } - SyncAppendEntriesReply* pReply = syncAppendEntriesReplyBuild(); + SyncAppendEntriesReply* pReply = syncAppendEntriesReplyBuild(ths->vgId); pReply->srcId = ths->myRaftId; pReply->destId = pMsg->srcId; pReply->term = ths->pRaftStore->currentTerm; @@ -301,8 +320,14 @@ int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg) { SRpcMsg rpcMsg; syncEntry2OriginalRpc(pEntry, &rpcMsg); - if (ths->pFsm->FpCommitCb != NULL && pEntry->entryType == SYNC_RAFT_ENTRY_DATA) { - ths->pFsm->FpCommitCb(ths->pFsm, &rpcMsg, pEntry->index, pEntry->isWeak, 0, ths->state); + if (ths->pFsm->FpCommitCb != NULL && pEntry->originalRpcType != TDMT_VND_SYNC_NOOP) { + SFsmCbMeta cbMeta; + cbMeta.index = pEntry->index; + cbMeta.isWeak = pEntry->isWeak; + cbMeta.code = 0; + cbMeta.state = ths->state; + cbMeta.seqNum = pEntry->seqNum; + ths->pFsm->FpCommitCb(ths->pFsm, &rpcMsg, cbMeta); } rpcFreeCont(rpcMsg.pCont); diff --git a/source/libs/sync/src/syncAppendEntriesReply.c b/source/libs/sync/src/syncAppendEntriesReply.c index 790ac7f8e1..77d85e2915 100644 --- a/source/libs/sync/src/syncAppendEntriesReply.c +++ b/source/libs/sync/src/syncAppendEntriesReply.c @@ -56,6 +56,15 @@ int32_t syncNodeOnAppendEntriesReplyCb(SSyncNode* ths, SyncAppendEntriesReply* p // syncNodeUpdateTerm(ths, pMsg->term); // } + if (pMsg->term > ths->pRaftStore->currentTerm) { + char logBuf[128]; + snprintf(logBuf, sizeof(logBuf), "syncNodeOnAppendEntriesReplyCb error term, receive:%lu current:%lu", pMsg->term, + ths->pRaftStore->currentTerm); + syncNodeLog2(logBuf, ths); + sError("%s", logBuf); + return ret; + } + assert(pMsg->term == ths->pRaftStore->currentTerm); if (pMsg->success) { diff --git a/source/libs/sync/src/syncCommit.c b/source/libs/sync/src/syncCommit.c index 6023c00afa..8b54b0a090 100644 --- a/source/libs/sync/src/syncCommit.c +++ b/source/libs/sync/src/syncCommit.c @@ -97,8 +97,14 @@ void syncMaybeAdvanceCommitIndex(SSyncNode* pSyncNode) { SRpcMsg rpcMsg; syncEntry2OriginalRpc(pEntry, &rpcMsg); - if (pSyncNode->pFsm->FpCommitCb != NULL && pEntry->entryType == SYNC_RAFT_ENTRY_DATA) { - pSyncNode->pFsm->FpCommitCb(pSyncNode->pFsm, &rpcMsg, pEntry->index, pEntry->isWeak, 0, pSyncNode->state); + if (pSyncNode->pFsm->FpCommitCb != NULL && pEntry->originalRpcType != TDMT_VND_SYNC_NOOP) { + SFsmCbMeta cbMeta; + cbMeta.index = pEntry->index; + cbMeta.isWeak = pEntry->isWeak; + cbMeta.code = 0; + cbMeta.state = pSyncNode->state; + cbMeta.seqNum = pEntry->seqNum; + pSyncNode->pFsm->FpCommitCb(pSyncNode->pFsm, &rpcMsg, cbMeta); } rpcFreeCont(rpcMsg.pCont); diff --git a/source/libs/sync/src/syncElection.c b/source/libs/sync/src/syncElection.c index 6ae70689ef..5101344b84 100644 --- a/source/libs/sync/src/syncElection.c +++ b/source/libs/sync/src/syncElection.c @@ -35,7 +35,7 @@ int32_t syncNodeRequestVotePeers(SSyncNode* pSyncNode) { int32_t ret = 0; for (int i = 0; i < pSyncNode->peersNum; ++i) { - SyncRequestVote* pMsg = syncRequestVoteBuild(); + SyncRequestVote* pMsg = syncRequestVoteBuild(pSyncNode->vgId); pMsg->srcId = pSyncNode->myRaftId; pMsg->destId = pSyncNode->peersId[i]; pMsg->term = pSyncNode->pRaftStore->currentTerm; diff --git a/source/libs/sync/src/syncEnv.c b/source/libs/sync/src/syncEnv.c index 726ebdae07..4213d7b45d 100644 --- a/source/libs/sync/src/syncEnv.c +++ b/source/libs/sync/src/syncEnv.c @@ -27,10 +27,13 @@ static void syncEnvTick(void *param, void *tmrId); // -------------------------------- int32_t syncEnvStart() { + sDebugFlag = 207; + int32_t ret = 0; taosSeedRand(taosGetTimestampSec()); gSyncEnv = doSyncEnvStart(gSyncEnv); assert(gSyncEnv != NULL); + sTrace("syncEnvStart ok!"); return ret; } diff --git a/source/libs/sync/src/syncIO.c b/source/libs/sync/src/syncIO.c index 717b1cc756..b1d7af3254 100644 --- a/source/libs/sync/src/syncIO.c +++ b/source/libs/sync/src/syncIO.c @@ -29,7 +29,7 @@ static int32_t syncIODestroy(SSyncIO *io); static int32_t syncIOStartInternal(SSyncIO *io); static int32_t syncIOStopInternal(SSyncIO *io); -static void *syncIOConsumerFunc(void *param); +static void * syncIOConsumerFunc(void *param); static void syncIOProcessRequest(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); static void syncIOProcessReply(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet); static int32_t syncIOAuth(void *parent, char *meterId, char *spi, char *encrypt, char *secret, char *ckey); @@ -70,9 +70,15 @@ int32_t syncIOSendMsg(void *clientRpc, const SEpSet *pEpSet, SRpcMsg *pMsg) { assert(pEpSet->numOfEps == 1); int32_t ret = 0; - char logBuf[256]; - snprintf(logBuf, sizeof(logBuf), "==syncIOSendMsg== %s:%d", pEpSet->eps[0].fqdn, pEpSet->eps[0].port); - syncRpcMsgPrint2(logBuf, pMsg); + { + syncUtilMsgNtoH(pMsg->pCont); + + char logBuf[256]; + snprintf(logBuf, sizeof(logBuf), "==syncIOSendMsg== %s:%d", pEpSet->eps[0].fqdn, pEpSet->eps[0].port); + syncRpcMsgLog2(logBuf, pMsg); + + syncUtilMsgHtoN(pMsg->pCont); + } pMsg->handle = NULL; pMsg->noResp = 1; @@ -83,7 +89,7 @@ int32_t syncIOSendMsg(void *clientRpc, const SEpSet *pEpSet, SRpcMsg *pMsg) { int32_t syncIOEqMsg(void *queue, SRpcMsg *pMsg) { int32_t ret = 0; char logBuf[128]; - syncRpcMsgPrint2((char *)"==syncIOEqMsg==", pMsg); + syncRpcMsgLog2((char *)"==syncIOEqMsg==", pMsg); SRpcMsg *pTemp; pTemp = taosAllocateQitem(sizeof(SRpcMsg)); @@ -235,9 +241,9 @@ static int32_t syncIOStopInternal(SSyncIO *io) { } static void *syncIOConsumerFunc(void *param) { - SSyncIO *io = param; + SSyncIO * io = param; STaosQall *qall; - SRpcMsg *pRpcMsg, rpcMsg; + SRpcMsg * pRpcMsg, rpcMsg; qall = taosAllocateQall(); while (1) { @@ -252,7 +258,7 @@ static void *syncIOConsumerFunc(void *param) { syncRpcMsgLog2((char *)"==syncIOConsumerFunc==", pRpcMsg); // use switch case instead of if else - if (pRpcMsg->msgType == SYNC_PING) { + if (pRpcMsg->msgType == TDMT_VND_SYNC_PING) { if (io->FpOnSyncPing != NULL) { SyncPing *pSyncMsg = syncPingFromRpcMsg2(pRpcMsg); assert(pSyncMsg != NULL); @@ -260,7 +266,7 @@ static void *syncIOConsumerFunc(void *param) { syncPingDestroy(pSyncMsg); } - } else if (pRpcMsg->msgType == SYNC_PING_REPLY) { + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_PING_REPLY) { if (io->FpOnSyncPingReply != NULL) { SyncPingReply *pSyncMsg = syncPingReplyFromRpcMsg2(pRpcMsg); assert(pSyncMsg != NULL); @@ -268,7 +274,7 @@ static void *syncIOConsumerFunc(void *param) { syncPingReplyDestroy(pSyncMsg); } - } else if (pRpcMsg->msgType == SYNC_CLIENT_REQUEST) { + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_CLIENT_REQUEST) { if (io->FpOnSyncClientRequest != NULL) { SyncClientRequest *pSyncMsg = syncClientRequestFromRpcMsg2(pRpcMsg); assert(pSyncMsg != NULL); @@ -276,7 +282,7 @@ static void *syncIOConsumerFunc(void *param) { syncClientRequestDestroy(pSyncMsg); } - } else if (pRpcMsg->msgType == SYNC_REQUEST_VOTE) { + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_REQUEST_VOTE) { if (io->FpOnSyncRequestVote != NULL) { SyncRequestVote *pSyncMsg = syncRequestVoteFromRpcMsg2(pRpcMsg); assert(pSyncMsg != NULL); @@ -284,7 +290,7 @@ static void *syncIOConsumerFunc(void *param) { syncRequestVoteDestroy(pSyncMsg); } - } else if (pRpcMsg->msgType == SYNC_REQUEST_VOTE_REPLY) { + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_REQUEST_VOTE_REPLY) { if (io->FpOnSyncRequestVoteReply != NULL) { SyncRequestVoteReply *pSyncMsg = syncRequestVoteReplyFromRpcMsg2(pRpcMsg); assert(pSyncMsg != NULL); @@ -292,7 +298,7 @@ static void *syncIOConsumerFunc(void *param) { syncRequestVoteReplyDestroy(pSyncMsg); } - } else if (pRpcMsg->msgType == SYNC_APPEND_ENTRIES) { + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_APPEND_ENTRIES) { if (io->FpOnSyncAppendEntries != NULL) { SyncAppendEntries *pSyncMsg = syncAppendEntriesFromRpcMsg2(pRpcMsg); assert(pSyncMsg != NULL); @@ -300,7 +306,7 @@ static void *syncIOConsumerFunc(void *param) { syncAppendEntriesDestroy(pSyncMsg); } - } else if (pRpcMsg->msgType == SYNC_APPEND_ENTRIES_REPLY) { + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_APPEND_ENTRIES_REPLY) { if (io->FpOnSyncAppendEntriesReply != NULL) { SyncAppendEntriesReply *pSyncMsg = syncAppendEntriesReplyFromRpcMsg2(pRpcMsg); assert(pSyncMsg != NULL); @@ -308,7 +314,7 @@ static void *syncIOConsumerFunc(void *param) { syncAppendEntriesReplyDestroy(pSyncMsg); } - } else if (pRpcMsg->msgType == SYNC_TIMEOUT) { + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_TIMEOUT) { if (io->FpOnSyncTimeout != NULL) { SyncTimeout *pSyncMsg = syncTimeoutFromRpcMsg2(pRpcMsg); assert(pSyncMsg != NULL); @@ -336,7 +342,7 @@ static void *syncIOConsumerFunc(void *param) { rpcMsg.handle = pRpcMsg->handle; rpcMsg.code = 0; - syncRpcMsgPrint2((char *)"syncIOConsumerFunc rpcSendResponse --> ", &rpcMsg); + syncRpcMsgLog2((char *)"syncIOConsumerFunc rpcSendResponse --> ", &rpcMsg); rpcSendResponse(&rpcMsg); } */ @@ -350,7 +356,9 @@ static void *syncIOConsumerFunc(void *param) { } static void syncIOProcessRequest(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { - syncRpcMsgPrint2((char *)"==syncIOProcessRequest==", pMsg); + syncUtilMsgNtoH(pMsg->pCont); + + syncRpcMsgLog2((char *)"==syncIOProcessRequest==", pMsg); SSyncIO *io = pParent; SRpcMsg *pTemp; pTemp = taosAllocateQitem(sizeof(SRpcMsg)); @@ -359,10 +367,10 @@ static void syncIOProcessRequest(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { } static void syncIOProcessReply(void *pParent, SRpcMsg *pMsg, SEpSet *pEpSet) { - if (pMsg->msgType == SYNC_RESPONSE) { + if (pMsg->msgType == TDMT_VND_SYNC_COMMON_RESPONSE) { sTrace("==syncIOProcessReply=="); } else { - syncRpcMsgPrint2((char *)"==syncIOProcessReply==", pMsg); + syncRpcMsgLog2((char *)"==syncIOProcessReply==", pMsg); } rpcFreeCont(pMsg->pCont); } @@ -408,14 +416,14 @@ static void syncIOTickQ(void *param, void *tmrId) { srcId.vgId = -1; destId.addr = syncUtilAddr2U64(io->myAddr.eps[0].fqdn, io->myAddr.eps[0].port); destId.vgId = -1; - SyncPingReply *pMsg = syncPingReplyBuild2(&srcId, &destId, "syncIOTickQ"); + SyncPingReply *pMsg = syncPingReplyBuild2(&srcId, &destId, -1, "syncIOTickQ"); SRpcMsg rpcMsg; syncPingReply2RpcMsg(pMsg, &rpcMsg); SRpcMsg *pTemp; pTemp = taosAllocateQitem(sizeof(SRpcMsg)); memcpy(pTemp, &rpcMsg, sizeof(SRpcMsg)); - syncRpcMsgPrint2((char *)"==syncIOTickQ==", &rpcMsg); + syncRpcMsgLog2((char *)"==syncIOTickQ==", &rpcMsg); taosWriteQitem(io->pMsgQ, pTemp); syncPingReplyDestroy(pMsg); @@ -430,12 +438,12 @@ static void syncIOTickPing(void *param, void *tmrId) { srcId.vgId = -1; destId.addr = syncUtilAddr2U64(io->myAddr.eps[0].fqdn, io->myAddr.eps[0].port); destId.vgId = -1; - SyncPing *pMsg = syncPingBuild2(&srcId, &destId, "syncIOTickPing"); + SyncPing *pMsg = syncPingBuild2(&srcId, &destId, -1, "syncIOTickPing"); // SyncPing *pMsg = syncPingBuild3(&srcId, &destId); SRpcMsg rpcMsg; syncPing2RpcMsg(pMsg, &rpcMsg); - syncRpcMsgPrint2((char *)"==syncIOTickPing==", &rpcMsg); + syncRpcMsgLog2((char *)"==syncIOTickPing==", &rpcMsg); rpcSendRequest(io->clientRpc, &io->myAddr, &rpcMsg, NULL); syncPingDestroy(pMsg); diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index 78e454309a..6d51cee14e 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -23,11 +23,13 @@ #include "syncIndexMgr.h" #include "syncInt.h" #include "syncMessage.h" +#include "syncRaftCfg.h" #include "syncRaftLog.h" #include "syncRaftStore.h" #include "syncReplication.h" #include "syncRequestVote.h" #include "syncRequestVoteReply.h" +#include "syncRespMgr.h" #include "syncTimeout.h" #include "syncUtil.h" #include "syncVoteMgr.h" @@ -44,9 +46,9 @@ static int32_t syncNodeEqNoop(SSyncNode* ths); static int32_t syncNodeAppendNoop(SSyncNode* ths); // process message ---- -static int32_t syncNodeOnPingCb(SSyncNode* ths, SyncPing* pMsg); -static int32_t syncNodeOnPingReplyCb(SSyncNode* ths, SyncPingReply* pMsg); -static int32_t syncNodeOnClientRequestCb(SSyncNode* ths, SyncClientRequest* pMsg); +int32_t syncNodeOnPingCb(SSyncNode* ths, SyncPing* pMsg); +int32_t syncNodeOnPingReplyCb(SSyncNode* ths, SyncPingReply* pMsg); +int32_t syncNodeOnClientRequestCb(SSyncNode* ths, SyncClientRequest* pMsg); // life cycle static void syncFreeNode(void* param); @@ -76,10 +78,12 @@ void syncCleanUp() { } } -int64_t syncStart(const SSyncInfo* pSyncInfo) { +int64_t syncOpen(const SSyncInfo* pSyncInfo) { SSyncNode* pSyncNode = syncNodeOpen(pSyncInfo); assert(pSyncNode != NULL); + syncNodeLog2("syncNodeOpen open success", pSyncNode); + pSyncNode->rid = taosAddRef(tsNodeRefId, pSyncNode); if (pSyncNode->rid < 0) { syncFreeNode(pSyncNode); @@ -89,6 +93,16 @@ int64_t syncStart(const SSyncInfo* pSyncInfo) { return pSyncNode->rid; } +void syncStart(int64_t rid) { + SSyncNode* pSyncNode = (SSyncNode*)taosAcquireRef(tsNodeRefId, rid); + if (pSyncNode == NULL) { + return; + } + syncNodeStart(pSyncNode); + + taosReleaseRef(tsNodeRefId, pSyncNode->rid); +} + void syncStop(int64_t rid) { SSyncNode* pSyncNode = (SSyncNode*)taosAcquireRef(tsNodeRefId, rid); if (pSyncNode == NULL) { @@ -105,11 +119,6 @@ int32_t syncReconfig(int64_t rid, const SSyncCfg* pSyncCfg) { return ret; } -int32_t syncPropose(int64_t rid, const SRpcMsg* pMsg, bool isWeak) { - int32_t ret = syncPropose2(rid, pMsg, isWeak, 0); - return ret; -} - int32_t syncForwardToPeer(int64_t rid, const SRpcMsg* pMsg, bool isWeak) { int32_t ret = syncPropose(rid, pMsg, isWeak); return ret; @@ -121,28 +130,163 @@ ESyncState syncGetMyRole(int64_t rid) { return TAOS_SYNC_STATE_ERROR; } assert(rid == pSyncNode->rid); - return pSyncNode->state; + ESyncState state = pSyncNode->state; + + taosReleaseRef(tsNodeRefId, pSyncNode->rid); + return state; } -int32_t syncPropose2(int64_t rid, const SRpcMsg* pMsg, bool isWeak, uint64_t seqNum) { - int32_t ret = 0; +const char* syncGetMyRoleStr(int64_t rid) { + const char* s = syncUtilState2String(syncGetMyRole(rid)); + return s; +} + +SyncTerm syncGetMyTerm(int64_t rid) { SSyncNode* pSyncNode = (SSyncNode*)taosAcquireRef(tsNodeRefId, rid); if (pSyncNode == NULL) { - return -1; + return TAOS_SYNC_STATE_ERROR; + } + assert(rid == pSyncNode->rid); + SyncTerm term = pSyncNode->pRaftStore->currentTerm; + + taosReleaseRef(tsNodeRefId, pSyncNode->rid); + return term; +} + +int32_t syncGetRespRpc(int64_t rid, uint64_t index, SRpcMsg* msg) { + SSyncNode* pSyncNode = (SSyncNode*)taosAcquireRef(tsNodeRefId, rid); + if (pSyncNode == NULL) { + return TAOS_SYNC_STATE_ERROR; + } + assert(rid == pSyncNode->rid); + + SRespStub stub; + int32_t ret = syncRespMgrGet(pSyncNode->pSyncRespMgr, index, &stub); + if (ret == 1) { + memcpy(msg, &(stub.rpcMsg), sizeof(SRpcMsg)); + } + + taosReleaseRef(tsNodeRefId, pSyncNode->rid); + return ret; +} + +int32_t syncGetAndDelRespRpc(int64_t rid, uint64_t index, SRpcMsg* msg) { + SSyncNode* pSyncNode = (SSyncNode*)taosAcquireRef(tsNodeRefId, rid); + if (pSyncNode == NULL) { + return TAOS_SYNC_STATE_ERROR; + } + assert(rid == pSyncNode->rid); + + SRespStub stub; + int32_t ret = syncRespMgrGetAndDel(pSyncNode->pSyncRespMgr, index, &stub); + if (ret == 1) { + memcpy(msg, &(stub.rpcMsg), sizeof(SRpcMsg)); + } + + taosReleaseRef(tsNodeRefId, pSyncNode->rid); + return ret; +} + +void syncSetQ(int64_t rid, void* queue) { + SSyncNode* pSyncNode = (SSyncNode*)taosAcquireRef(tsNodeRefId, rid); + if (pSyncNode == NULL) { + sTrace("syncSetQ get pSyncNode is NULL, rid:%ld", rid); + return; + } + assert(rid == pSyncNode->rid); + pSyncNode->queue = queue; + + taosReleaseRef(tsNodeRefId, pSyncNode->rid); +} + +void syncSetRpc(int64_t rid, void* rpcHandle) { + SSyncNode* pSyncNode = (SSyncNode*)taosAcquireRef(tsNodeRefId, rid); + if (pSyncNode == NULL) { + sTrace("syncSetRpc get pSyncNode is NULL, rid:%ld", rid); + return; + } + assert(rid == pSyncNode->rid); + pSyncNode->rpcClient = rpcHandle; + + taosReleaseRef(tsNodeRefId, pSyncNode->rid); +} + +char* sync2SimpleStr(int64_t rid) { + SSyncNode* pSyncNode = (SSyncNode*)taosAcquireRef(tsNodeRefId, rid); + if (pSyncNode == NULL) { + sTrace("syncSetRpc get pSyncNode is NULL, rid:%ld", rid); + return NULL; + } + assert(rid == pSyncNode->rid); + char* s = syncNode2SimpleStr(pSyncNode); + taosReleaseRef(tsNodeRefId, pSyncNode->rid); + + return s; +} + +void setPingTimerMS(int64_t rid, int32_t pingTimerMS) { + SSyncNode* pSyncNode = (SSyncNode*)taosAcquireRef(tsNodeRefId, rid); + if (pSyncNode == NULL) { + return; + } + assert(rid == pSyncNode->rid); + pSyncNode->pingBaseLine = pingTimerMS; + pSyncNode->pingTimerMS = pingTimerMS; + + taosReleaseRef(tsNodeRefId, pSyncNode->rid); +} + +void setElectTimerMS(int64_t rid, int32_t electTimerMS) { + SSyncNode* pSyncNode = (SSyncNode*)taosAcquireRef(tsNodeRefId, rid); + if (pSyncNode == NULL) { + return; + } + assert(rid == pSyncNode->rid); + pSyncNode->electBaseLine = electTimerMS; + + taosReleaseRef(tsNodeRefId, pSyncNode->rid); +} + +void setHeartbeatTimerMS(int64_t rid, int32_t hbTimerMS) { + SSyncNode* pSyncNode = (SSyncNode*)taosAcquireRef(tsNodeRefId, rid); + if (pSyncNode == NULL) { + return; + } + assert(rid == pSyncNode->rid); + pSyncNode->hbBaseLine = hbTimerMS; + pSyncNode->heartbeatTimerMS = hbTimerMS; + + taosReleaseRef(tsNodeRefId, pSyncNode->rid); +} + +int32_t syncPropose(int64_t rid, const SRpcMsg* pMsg, bool isWeak) { + int32_t ret = TAOS_SYNC_PROPOSE_SUCCESS; + SSyncNode* pSyncNode = (SSyncNode*)taosAcquireRef(tsNodeRefId, rid); + if (pSyncNode == NULL) { + return TAOS_SYNC_PROPOSE_OTHER_ERROR; } assert(rid == pSyncNode->rid); if (pSyncNode->state == TAOS_SYNC_STATE_LEADER) { - SyncClientRequest* pSyncMsg = syncClientRequestBuild2(pMsg, seqNum, isWeak); + SRespStub stub; + stub.createTime = taosGetTimestampMs(); + stub.rpcMsg = *pMsg; + uint64_t seqNum = syncRespMgrAdd(pSyncNode->pSyncRespMgr, &stub); + + SyncClientRequest* pSyncMsg = syncClientRequestBuild2(pMsg, seqNum, isWeak, pSyncNode->vgId); SRpcMsg rpcMsg; syncClientRequest2RpcMsg(pSyncMsg, &rpcMsg); - pSyncNode->FpEqMsg(pSyncNode->queue, &rpcMsg); + if (pSyncNode->FpEqMsg != NULL) { + pSyncNode->FpEqMsg(pSyncNode->queue, &rpcMsg); + } else { + sTrace("syncPropose pSyncNode->FpEqMsg is NULL"); + } syncClientRequestDestroy(pSyncMsg); - ret = 0; + ret = TAOS_SYNC_PROPOSE_SUCCESS; } else { sTrace("syncPropose not leader, %s", syncUtilState2String(pSyncNode->state)); - ret = -1; // todo : need define err code !! + ret = TAOS_SYNC_PROPOSE_NOT_LEADER; } taosReleaseRef(tsNodeRefId, pSyncNode->rid); @@ -155,49 +299,62 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo) { assert(pSyncNode != NULL); memset(pSyncNode, 0, sizeof(SSyncNode)); - if (taosMkDir(pSyncInfo->path) != 0) { - terrno = TAOS_SYSTEM_ERROR(errno); - sError("failed to create dir:%s since %s", pSyncInfo->path, terrstr()); - return NULL; + int32_t ret = 0; + if (!taosDirExist((char*)(pSyncInfo->path))) { + if (taosMkDir(pSyncInfo->path) != 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + sError("failed to create dir:%s since %s", pSyncInfo->path, terrstr()); + return NULL; + } + + // create raft config file + snprintf(pSyncNode->configPath, sizeof(pSyncNode->configPath), "%s/raft_config.json", pSyncInfo->path); + ret = syncCfgCreateFile((SSyncCfg*)&(pSyncInfo->syncCfg), pSyncNode->configPath); + assert(ret == 0); } // init by SSyncInfo pSyncNode->vgId = pSyncInfo->vgId; - pSyncNode->syncCfg = pSyncInfo->syncCfg; memcpy(pSyncNode->path, pSyncInfo->path, sizeof(pSyncNode->path)); snprintf(pSyncNode->raftStorePath, sizeof(pSyncNode->raftStorePath), "%s/raft_store.json", pSyncInfo->path); + snprintf(pSyncNode->configPath, sizeof(pSyncNode->configPath), "%s/raft_config.json", pSyncInfo->path); + pSyncNode->pWal = pSyncInfo->pWal; pSyncNode->rpcClient = pSyncInfo->rpcClient; pSyncNode->FpSendMsg = pSyncInfo->FpSendMsg; pSyncNode->queue = pSyncInfo->queue; pSyncNode->FpEqMsg = pSyncInfo->FpEqMsg; + // init raft config + pSyncNode->pRaftCfg = raftCfgOpen(pSyncNode->configPath); + assert(pSyncNode->pRaftCfg != NULL); + // init internal - pSyncNode->myNodeInfo = pSyncInfo->syncCfg.nodeInfo[pSyncInfo->syncCfg.myIndex]; - syncUtilnodeInfo2raftId(&pSyncNode->myNodeInfo, pSyncInfo->vgId, &pSyncNode->myRaftId); + pSyncNode->myNodeInfo = pSyncNode->pRaftCfg->cfg.nodeInfo[pSyncNode->pRaftCfg->cfg.myIndex]; + syncUtilnodeInfo2raftId(&pSyncNode->myNodeInfo, pSyncNode->vgId, &pSyncNode->myRaftId); // init peersNum, peers, peersId - pSyncNode->peersNum = pSyncInfo->syncCfg.replicaNum - 1; + pSyncNode->peersNum = pSyncNode->pRaftCfg->cfg.replicaNum - 1; int j = 0; - for (int i = 0; i < pSyncInfo->syncCfg.replicaNum; ++i) { - if (i != pSyncInfo->syncCfg.myIndex) { - pSyncNode->peersNodeInfo[j] = pSyncInfo->syncCfg.nodeInfo[i]; + for (int i = 0; i < pSyncNode->pRaftCfg->cfg.replicaNum; ++i) { + if (i != pSyncNode->pRaftCfg->cfg.myIndex) { + pSyncNode->peersNodeInfo[j] = pSyncNode->pRaftCfg->cfg.nodeInfo[i]; j++; } } for (int i = 0; i < pSyncNode->peersNum; ++i) { - syncUtilnodeInfo2raftId(&pSyncNode->peersNodeInfo[i], pSyncInfo->vgId, &pSyncNode->peersId[i]); + syncUtilnodeInfo2raftId(&pSyncNode->peersNodeInfo[i], pSyncNode->vgId, &pSyncNode->peersId[i]); } // init replicaNum, replicasId - pSyncNode->replicaNum = pSyncInfo->syncCfg.replicaNum; - for (int i = 0; i < pSyncInfo->syncCfg.replicaNum; ++i) { - syncUtilnodeInfo2raftId(&pSyncInfo->syncCfg.nodeInfo[i], pSyncInfo->vgId, &pSyncNode->replicasId[i]); + pSyncNode->replicaNum = pSyncNode->pRaftCfg->cfg.replicaNum; + for (int i = 0; i < pSyncNode->pRaftCfg->cfg.replicaNum; ++i) { + syncUtilnodeInfo2raftId(&pSyncNode->pRaftCfg->cfg.nodeInfo[i], pSyncNode->vgId, &pSyncNode->replicasId[i]); } // init raft algorithm pSyncNode->pFsm = pSyncInfo->pFsm; - pSyncNode->quorum = syncUtilQuorum(pSyncInfo->syncCfg.replicaNum); + pSyncNode->quorum = syncUtilQuorum(pSyncNode->pRaftCfg->cfg.replicaNum); pSyncNode->leaderCache = EMPTY_RAFT_ID; // init life cycle outside @@ -287,10 +444,25 @@ SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo) { pSyncNode->FpOnAppendEntriesReply = syncNodeOnAppendEntriesReplyCb; pSyncNode->FpOnTimeout = syncNodeOnTimeoutCb; + // tools + pSyncNode->pSyncRespMgr = syncRespMgrCreate(NULL, 0); + assert(pSyncNode->pSyncRespMgr != NULL); + + // start in syncNodeStart + // start raft + // syncNodeBecomeFollower(pSyncNode); + + return pSyncNode; +} + +void syncNodeStart(SSyncNode* pSyncNode) { // start raft syncNodeBecomeFollower(pSyncNode); - return pSyncNode; + // for test + int32_t ret = 0; + // ret = syncNodeStartPingTimer(pSyncNode); + assert(ret == 0); } void syncNodeClose(SSyncNode* pSyncNode) { @@ -305,12 +477,18 @@ void syncNodeClose(SSyncNode* pSyncNode) { syncIndexMgrDestroy(pSyncNode->pNextIndex); syncIndexMgrDestroy(pSyncNode->pMatchIndex); logStoreDestory(pSyncNode->pLogStore); + raftCfgClose(pSyncNode->pRaftCfg); syncNodeStopPingTimer(pSyncNode); syncNodeStopElectTimer(pSyncNode); syncNodeStopHeartbeatTimer(pSyncNode); - taosMemoryFree(pSyncNode); + if (pSyncNode->pFsm != NULL) { + taosMemoryFree(pSyncNode->pFsm); + } + + // free memory in syncFreeNode + // taosMemoryFree(pSyncNode); } // ping -------------- @@ -328,7 +506,7 @@ int32_t syncNodePing(SSyncNode* pSyncNode, const SRaftId* destRaftId, SyncPing* int32_t syncNodePingSelf(SSyncNode* pSyncNode) { int32_t ret = 0; - SyncPing* pMsg = syncPingBuild3(&pSyncNode->myRaftId, &pSyncNode->myRaftId); + SyncPing* pMsg = syncPingBuild3(&pSyncNode->myRaftId, &pSyncNode->myRaftId, pSyncNode->vgId); ret = syncNodePing(pSyncNode, &pMsg->destId, pMsg); assert(ret == 0); @@ -339,10 +517,9 @@ int32_t syncNodePingSelf(SSyncNode* pSyncNode) { int32_t syncNodePingPeers(SSyncNode* pSyncNode) { int32_t ret = 0; for (int i = 0; i < pSyncNode->peersNum; ++i) { - SRaftId destId; - syncUtilnodeInfo2raftId(&pSyncNode->peersNodeInfo[i], pSyncNode->vgId, &destId); - SyncPing* pMsg = syncPingBuild3(&pSyncNode->myRaftId, &destId); - ret = syncNodePing(pSyncNode, &destId, pMsg); + SRaftId* destId = &(pSyncNode->peersId[i]); + SyncPing* pMsg = syncPingBuild3(&pSyncNode->myRaftId, destId, pSyncNode->vgId); + ret = syncNodePing(pSyncNode, destId, pMsg); assert(ret == 0); syncPingDestroy(pMsg); } @@ -351,11 +528,10 @@ int32_t syncNodePingPeers(SSyncNode* pSyncNode) { int32_t syncNodePingAll(SSyncNode* pSyncNode) { int32_t ret = 0; - for (int i = 0; i < pSyncNode->syncCfg.replicaNum; ++i) { - SRaftId destId; - syncUtilnodeInfo2raftId(&pSyncNode->syncCfg.nodeInfo[i], pSyncNode->vgId, &destId); - SyncPing* pMsg = syncPingBuild3(&pSyncNode->myRaftId, &destId); - ret = syncNodePing(pSyncNode, &destId, pMsg); + for (int i = 0; i < pSyncNode->pRaftCfg->cfg.replicaNum; ++i) { + SRaftId* destId = &(pSyncNode->replicasId[i]); + SyncPing* pMsg = syncPingBuild3(&pSyncNode->myRaftId, destId, pSyncNode->vgId); + ret = syncNodePing(pSyncNode, destId, pMsg); assert(ret == 0); syncPingDestroy(pMsg); } @@ -430,14 +606,30 @@ int32_t syncNodeStopHeartbeatTimer(SSyncNode* pSyncNode) { int32_t syncNodeSendMsgById(const SRaftId* destRaftId, SSyncNode* pSyncNode, SRpcMsg* pMsg) { SEpSet epSet; syncUtilraftId2EpSet(destRaftId, &epSet); - pSyncNode->FpSendMsg(pSyncNode->rpcClient, &epSet, pMsg); + if (pSyncNode->FpSendMsg != NULL) { + pMsg->noResp = 1; + // htonl + syncUtilMsgHtoN(pMsg->pCont); + + pSyncNode->FpSendMsg(pSyncNode->rpcClient, &epSet, pMsg); + } else { + sTrace("syncNodeSendMsgById pSyncNode->FpSendMsg is NULL"); + } return 0; } int32_t syncNodeSendMsgByInfo(const SNodeInfo* nodeInfo, SSyncNode* pSyncNode, SRpcMsg* pMsg) { SEpSet epSet; syncUtilnodeInfo2EpSet(nodeInfo, &epSet); - pSyncNode->FpSendMsg(pSyncNode->rpcClient, &epSet, pMsg); + if (pSyncNode->FpSendMsg != NULL) { + pMsg->noResp = 1; + // htonl + syncUtilMsgHtoN(pMsg->pCont); + + pSyncNode->FpSendMsg(pSyncNode->rpcClient, &epSet, pMsg); + } else { + sTrace("syncNodeSendMsgByInfo pSyncNode->FpSendMsg is NULL"); + } return 0; } @@ -448,7 +640,11 @@ cJSON* syncNode2Json(const SSyncNode* pSyncNode) { if (pSyncNode != NULL) { // init by SSyncInfo cJSON_AddNumberToObject(pRoot, "vgId", pSyncNode->vgId); + cJSON_AddItemToObject(pRoot, "SRaftCfg", raftCfg2Json(pSyncNode->pRaftCfg)); cJSON_AddStringToObject(pRoot, "path", pSyncNode->path); + cJSON_AddStringToObject(pRoot, "raftStorePath", pSyncNode->raftStorePath); + cJSON_AddStringToObject(pRoot, "configPath", pSyncNode->configPath); + snprintf(u64buf, sizeof(u64buf), "%p", pSyncNode->pWal); cJSON_AddStringToObject(pRoot, "pWal", u64buf); @@ -501,9 +697,7 @@ cJSON* syncNode2Json(const SSyncNode* pSyncNode) { // tla+ server vars cJSON_AddNumberToObject(pRoot, "state", pSyncNode->state); cJSON_AddStringToObject(pRoot, "state_str", syncUtilState2String(pSyncNode->state)); - char tmpBuf[RAFT_STORE_BLOCK_SIZE]; - raftStoreSerialize(pSyncNode->pRaftStore, tmpBuf, sizeof(tmpBuf)); - cJSON_AddStringToObject(pRoot, "pRaftStore", tmpBuf); + cJSON_AddItemToObject(pRoot, "pRaftStore", raftStore2Json(pSyncNode->pRaftStore)); // tla+ candidate vars cJSON_AddItemToObject(pRoot, "pVotesGranted", voteGranted2Json(pSyncNode->pVotesGranted)); @@ -518,6 +712,11 @@ cJSON* syncNode2Json(const SSyncNode* pSyncNode) { snprintf(u64buf, sizeof(u64buf), "%" PRId64 "", pSyncNode->commitIndex); cJSON_AddStringToObject(pRoot, "commitIndex", u64buf); + // timer ms init + cJSON_AddNumberToObject(pRoot, "pingBaseLine", pSyncNode->pingBaseLine); + cJSON_AddNumberToObject(pRoot, "electBaseLine", pSyncNode->electBaseLine); + cJSON_AddNumberToObject(pRoot, "hbBaseLine", pSyncNode->hbBaseLine); + // ping timer snprintf(u64buf, sizeof(u64buf), "%p", pSyncNode->pPingTimer); cJSON_AddStringToObject(pRoot, "pPingTimer", u64buf); @@ -586,6 +785,19 @@ char* syncNode2Str(const SSyncNode* pSyncNode) { return serialized; } +char* syncNode2SimpleStr(const SSyncNode* pSyncNode) { + int len = 256; + char* s = (char*)taosMemoryMalloc(len); + snprintf(s, len, + "syncNode2SimpleStr vgId:%d currentTerm:%lu, commitIndex:%ld, state:%d %s, electTimerLogicClock:%lu, " + "electTimerLogicClockUser:%lu, " + "electTimerMS:%d", + pSyncNode->vgId, pSyncNode->pRaftStore->currentTerm, pSyncNode->commitIndex, pSyncNode->state, + syncUtilState2String(pSyncNode->state), pSyncNode->electTimerLogicClock, pSyncNode->electTimerLogicClockUser, + pSyncNode->electTimerMS); + return s; +} + SSyncNode* syncNodeAcquire(int64_t rid) { SSyncNode* pNode = taosAcquireRef(tsNodeRefId, rid); if (pNode == NULL) { @@ -672,24 +884,36 @@ void syncNodeCandidate2Leader(SSyncNode* pSyncNode) { assert(voteGrantedMajority(pSyncNode->pVotesGranted)); syncNodeBecomeLeader(pSyncNode); + syncNodeLog2("==state change syncNodeCandidate2Leader==", pSyncNode); + // Raft 3.6.2 Committing entries from previous terms + + // use this now syncNodeAppendNoop(pSyncNode); + + // do not use this // syncNodeEqNoop(pSyncNode); } void syncNodeFollower2Candidate(SSyncNode* pSyncNode) { assert(pSyncNode->state == TAOS_SYNC_STATE_FOLLOWER); pSyncNode->state = TAOS_SYNC_STATE_CANDIDATE; + + syncNodeLog2("==state change syncNodeFollower2Candidate==", pSyncNode); } void syncNodeLeader2Follower(SSyncNode* pSyncNode) { assert(pSyncNode->state == TAOS_SYNC_STATE_LEADER); syncNodeBecomeFollower(pSyncNode); + + syncNodeLog2("==state change syncNodeLeader2Follower==", pSyncNode); } void syncNodeCandidate2Follower(SSyncNode* pSyncNode) { assert(pSyncNode->state == TAOS_SYNC_STATE_CANDIDATE); syncNodeBecomeFollower(pSyncNode); + + syncNodeLog2("==state change syncNodeCandidate2Follower==", pSyncNode); } // raft vote -------------- @@ -707,7 +931,7 @@ void syncNodeVoteForTerm(SSyncNode* pSyncNode, SyncTerm term, SRaftId* pRaftId) void syncNodeVoteForSelf(SSyncNode* pSyncNode) { syncNodeVoteForTerm(pSyncNode, pSyncNode->pRaftStore->currentTerm, &(pSyncNode->myRaftId)); - SyncRequestVoteReply* pMsg = syncRequestVoteReplyBuild(); + SyncRequestVoteReply* pMsg = syncRequestVoteReplyBuild(pSyncNode->vgId); pMsg->srcId = pSyncNode->myRaftId; pMsg->destId = pSyncNode->myRaftId; pMsg->term = pSyncNode->pRaftStore->currentTerm; @@ -735,13 +959,13 @@ void syncNodePrint2(char* s, SSyncNode* pObj) { void syncNodeLog(SSyncNode* pObj) { char* serialized = syncNode2Str(pObj); - sTrace("syncNodeLog | len:%lu | %s", strlen(serialized), serialized); + sTraceLong("syncNodeLog | len:%lu | %s", strlen(serialized), serialized); taosMemoryFree(serialized); } void syncNodeLog2(char* s, SSyncNode* pObj) { char* serialized = syncNode2Str(pObj); - sTrace("syncNodeLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); + sTraceLong("syncNodeLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); taosMemoryFree(serialized); } @@ -751,11 +975,15 @@ static void syncNodeEqPingTimer(void* param, void* tmrId) { SSyncNode* pSyncNode = (SSyncNode*)param; if (atomic_load_64(&pSyncNode->pingTimerLogicClockUser) <= atomic_load_64(&pSyncNode->pingTimerLogicClock)) { SyncTimeout* pSyncMsg = syncTimeoutBuild2(SYNC_TIMEOUT_PING, atomic_load_64(&pSyncNode->pingTimerLogicClock), - pSyncNode->pingTimerMS, pSyncNode); + pSyncNode->pingTimerMS, pSyncNode->vgId, pSyncNode); SRpcMsg rpcMsg; syncTimeout2RpcMsg(pSyncMsg, &rpcMsg); syncRpcMsgLog2((char*)"==syncNodeEqPingTimer==", &rpcMsg); - pSyncNode->FpEqMsg(pSyncNode->queue, &rpcMsg); + if (pSyncNode->FpEqMsg != NULL) { + pSyncNode->FpEqMsg(pSyncNode->queue, &rpcMsg); + } else { + sTrace("syncNodeEqPingTimer pSyncNode->FpEqMsg is NULL"); + } syncTimeoutDestroy(pSyncMsg); taosTmrReset(syncNodeEqPingTimer, pSyncNode->pingTimerMS, pSyncNode, gSyncEnv->pTimerManager, @@ -770,17 +998,21 @@ static void syncNodeEqElectTimer(void* param, void* tmrId) { SSyncNode* pSyncNode = (SSyncNode*)param; if (atomic_load_64(&pSyncNode->electTimerLogicClockUser) <= atomic_load_64(&pSyncNode->electTimerLogicClock)) { SyncTimeout* pSyncMsg = syncTimeoutBuild2(SYNC_TIMEOUT_ELECTION, atomic_load_64(&pSyncNode->electTimerLogicClock), - pSyncNode->electTimerMS, pSyncNode); + pSyncNode->electTimerMS, pSyncNode->vgId, pSyncNode); SRpcMsg rpcMsg; syncTimeout2RpcMsg(pSyncMsg, &rpcMsg); syncRpcMsgLog2((char*)"==syncNodeEqElectTimer==", &rpcMsg); - pSyncNode->FpEqMsg(pSyncNode->queue, &rpcMsg); + if (pSyncNode->FpEqMsg != NULL) { + pSyncNode->FpEqMsg(pSyncNode->queue, &rpcMsg); + } else { + sTrace("syncNodeEqElectTimer pSyncNode->FpEqMsg is NULL"); + } syncTimeoutDestroy(pSyncMsg); // reset timer ms pSyncNode->electTimerMS = syncUtilElectRandomMS(pSyncNode->electBaseLine, 2 * pSyncNode->electBaseLine); - taosTmrReset(syncNodeEqPingTimer, pSyncNode->pingTimerMS, pSyncNode, gSyncEnv->pTimerManager, - &pSyncNode->pPingTimer); + taosTmrReset(syncNodeEqElectTimer, pSyncNode->electTimerMS, pSyncNode, gSyncEnv->pTimerManager, + &pSyncNode->pElectTimer); } else { sTrace("==syncNodeEqElectTimer== electTimerLogicClock:%" PRIu64 ", electTimerLogicClockUser:%" PRIu64 "", pSyncNode->electTimerLogicClock, pSyncNode->electTimerLogicClockUser); @@ -793,11 +1025,15 @@ static void syncNodeEqHeartbeatTimer(void* param, void* tmrId) { atomic_load_64(&pSyncNode->heartbeatTimerLogicClock)) { SyncTimeout* pSyncMsg = syncTimeoutBuild2(SYNC_TIMEOUT_HEARTBEAT, atomic_load_64(&pSyncNode->heartbeatTimerLogicClock), - pSyncNode->heartbeatTimerMS, pSyncNode); + pSyncNode->heartbeatTimerMS, pSyncNode->vgId, pSyncNode); SRpcMsg rpcMsg; syncTimeout2RpcMsg(pSyncMsg, &rpcMsg); syncRpcMsgLog2((char*)"==syncNodeEqHeartbeatTimer==", &rpcMsg); - pSyncNode->FpEqMsg(pSyncNode->queue, &rpcMsg); + if (pSyncNode->FpEqMsg != NULL) { + pSyncNode->FpEqMsg(pSyncNode->queue, &rpcMsg); + } else { + sTrace("syncNodeEqHeartbeatTimer pSyncNode->FpEqMsg is NULL"); + } syncTimeoutDestroy(pSyncMsg); taosTmrReset(syncNodeEqHeartbeatTimer, pSyncNode->heartbeatTimerMS, pSyncNode, gSyncEnv->pTimerManager, @@ -815,7 +1051,7 @@ static int32_t syncNodeEqNoop(SSyncNode* ths) { SyncIndex index = ths->pLogStore->getLastIndex(ths->pLogStore) + 1; SyncTerm term = ths->pRaftStore->currentTerm; - SSyncRaftEntry* pEntry = syncEntryBuildNoop(term, index); + SSyncRaftEntry* pEntry = syncEntryBuildNoop(term, index, ths->vgId); assert(pEntry != NULL); uint32_t entryLen; @@ -826,7 +1062,11 @@ static int32_t syncNodeEqNoop(SSyncNode* ths) { SRpcMsg rpcMsg; syncClientRequest2RpcMsg(pSyncMsg, &rpcMsg); - ths->FpEqMsg(ths->queue, &rpcMsg); + if (ths->FpEqMsg != NULL) { + ths->FpEqMsg(ths->queue, &rpcMsg); + } else { + sTrace("syncNodeEqNoop pSyncNode->FpEqMsg is NULL"); + } taosMemoryFree(serialized); syncClientRequestDestroy(pSyncMsg); @@ -839,7 +1079,7 @@ static int32_t syncNodeAppendNoop(SSyncNode* ths) { SyncIndex index = ths->pLogStore->getLastIndex(ths->pLogStore) + 1; SyncTerm term = ths->pRaftStore->currentTerm; - SSyncRaftEntry* pEntry = syncEntryBuildNoop(term, index); + SSyncRaftEntry* pEntry = syncEntryBuildNoop(term, index, ths->vgId); assert(pEntry != NULL); if (ths->state == TAOS_SYNC_STATE_LEADER) { @@ -851,18 +1091,34 @@ static int32_t syncNodeAppendNoop(SSyncNode* ths) { } // on message ---- -static int32_t syncNodeOnPingCb(SSyncNode* ths, SyncPing* pMsg) { +int32_t syncNodeOnPingCb(SSyncNode* ths, SyncPing* pMsg) { + // log state + char logBuf[1024]; + snprintf(logBuf, sizeof(logBuf), + "==syncNodeOnPingCb== vgId:%d, state: %d, %s, term:%lu electTimerLogicClock:%lu, " + "electTimerLogicClockUser:%lu, electTimerMS:%d", + ths->vgId, ths->state, syncUtilState2String(ths->state), ths->pRaftStore->currentTerm, + ths->electTimerLogicClock, ths->electTimerLogicClockUser, ths->electTimerMS); + int32_t ret = 0; - syncPingLog2("==syncNodeOnPingCb==", pMsg); - SyncPingReply* pMsgReply = syncPingReplyBuild3(&ths->myRaftId, &pMsg->srcId); + syncPingLog2(logBuf, pMsg); + SyncPingReply* pMsgReply = syncPingReplyBuild3(&ths->myRaftId, &pMsg->srcId, ths->vgId); SRpcMsg rpcMsg; syncPingReply2RpcMsg(pMsgReply, &rpcMsg); + + /* + // htonl + SMsgHead* pHead = rpcMsg.pCont; + pHead->contLen = htonl(pHead->contLen); + pHead->vgId = htonl(pHead->vgId); + */ + syncNodeSendMsgById(&pMsgReply->destId, ths, &rpcMsg); return ret; } -static int32_t syncNodeOnPingReplyCb(SSyncNode* ths, SyncPingReply* pMsg) { +int32_t syncNodeOnPingReplyCb(SSyncNode* ths, SyncPingReply* pMsg) { int32_t ret = 0; syncPingReplyLog2("==syncNodeOnPingReplyCb==", pMsg); return ret; @@ -878,7 +1134,7 @@ static int32_t syncNodeOnPingReplyCb(SSyncNode* ths, SyncPingReply* pMsg) { // /\ UNCHANGED <> // -static int32_t syncNodeOnClientRequestCb(SSyncNode* ths, SyncClientRequest* pMsg) { +int32_t syncNodeOnClientRequestCb(SSyncNode* ths, SyncClientRequest* pMsg) { int32_t ret = 0; syncClientRequestLog2("==syncNodeOnClientRequestCb==", pMsg); @@ -898,8 +1154,14 @@ static int32_t syncNodeOnClientRequestCb(SSyncNode* ths, SyncClientRequest* pMsg syncEntry2OriginalRpc(pEntry, &rpcMsg); if (ths->pFsm != NULL) { - if (ths->pFsm->FpPreCommitCb != NULL && pEntry->entryType == SYNC_RAFT_ENTRY_DATA) { - ths->pFsm->FpPreCommitCb(ths->pFsm, &rpcMsg, pEntry->index, pEntry->isWeak, 0, ths->state); + if (ths->pFsm->FpPreCommitCb != NULL && pEntry->originalRpcType != TDMT_VND_SYNC_NOOP) { + SFsmCbMeta cbMeta; + cbMeta.index = pEntry->index; + cbMeta.isWeak = pEntry->isWeak; + cbMeta.code = 0; + cbMeta.state = ths->state; + cbMeta.seqNum = pEntry->seqNum; + ths->pFsm->FpPreCommitCb(ths->pFsm, &rpcMsg, cbMeta); } } rpcFreeCont(rpcMsg.pCont); @@ -913,8 +1175,14 @@ static int32_t syncNodeOnClientRequestCb(SSyncNode* ths, SyncClientRequest* pMsg syncEntry2OriginalRpc(pEntry, &rpcMsg); if (ths->pFsm != NULL) { - if (ths->pFsm->FpPreCommitCb != NULL && pEntry->entryType == SYNC_RAFT_ENTRY_DATA) { - ths->pFsm->FpPreCommitCb(ths->pFsm, &rpcMsg, pEntry->index, pEntry->isWeak, 1, ths->state); + if (ths->pFsm->FpPreCommitCb != NULL && pEntry->originalRpcType != TDMT_VND_SYNC_NOOP) { + SFsmCbMeta cbMeta; + cbMeta.index = pEntry->index; + cbMeta.isWeak = pEntry->isWeak; + cbMeta.code = 1; + cbMeta.state = ths->state; + cbMeta.seqNum = pEntry->seqNum; + ths->pFsm->FpPreCommitCb(ths->pFsm, &rpcMsg, cbMeta); } } rpcFreeCont(rpcMsg.pCont); @@ -926,7 +1194,8 @@ static int32_t syncNodeOnClientRequestCb(SSyncNode* ths, SyncClientRequest* pMsg static void syncFreeNode(void* param) { SSyncNode* pNode = param; - syncNodePrint2((char*)"==syncFreeNode==", pNode); + // inner object already free + // syncNodePrint2((char*)"==syncFreeNode==", pNode); taosMemoryFree(pNode); -} \ No newline at end of file +} diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index 5dca165455..76c68569c3 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -22,50 +22,50 @@ cJSON* syncRpcMsg2Json(SRpcMsg* pRpcMsg) { cJSON* pRoot; // in compiler optimization, switch case = if else constants - if (pRpcMsg->msgType == SYNC_TIMEOUT) { + if (pRpcMsg->msgType == TDMT_VND_SYNC_TIMEOUT) { SyncTimeout* pSyncMsg = syncTimeoutDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); pRoot = syncTimeout2Json(pSyncMsg); syncTimeoutDestroy(pSyncMsg); - } else if (pRpcMsg->msgType == SYNC_PING) { + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_PING) { SyncPing* pSyncMsg = syncPingDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); pRoot = syncPing2Json(pSyncMsg); syncPingDestroy(pSyncMsg); - } else if (pRpcMsg->msgType == SYNC_PING_REPLY) { + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_PING_REPLY) { SyncPingReply* pSyncMsg = syncPingReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); pRoot = syncPingReply2Json(pSyncMsg); syncPingReplyDestroy(pSyncMsg); - } else if (pRpcMsg->msgType == SYNC_CLIENT_REQUEST) { + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_CLIENT_REQUEST) { SyncClientRequest* pSyncMsg = syncClientRequestDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); pRoot = syncClientRequest2Json(pSyncMsg); syncClientRequestDestroy(pSyncMsg); - } else if (pRpcMsg->msgType == SYNC_CLIENT_REQUEST_REPLY) { + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_CLIENT_REQUEST_REPLY) { pRoot = syncRpcUnknownMsg2Json(); - } else if (pRpcMsg->msgType == SYNC_REQUEST_VOTE) { + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_REQUEST_VOTE) { SyncRequestVote* pSyncMsg = syncRequestVoteDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); pRoot = syncRequestVote2Json(pSyncMsg); syncRequestVoteDestroy(pSyncMsg); - } else if (pRpcMsg->msgType == SYNC_REQUEST_VOTE_REPLY) { + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_REQUEST_VOTE_REPLY) { SyncRequestVoteReply* pSyncMsg = syncRequestVoteReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); pRoot = syncRequestVoteReply2Json(pSyncMsg); syncRequestVoteReplyDestroy(pSyncMsg); - } else if (pRpcMsg->msgType == SYNC_APPEND_ENTRIES) { + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_APPEND_ENTRIES) { SyncAppendEntries* pSyncMsg = syncAppendEntriesDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); pRoot = syncAppendEntries2Json(pSyncMsg); syncAppendEntriesDestroy(pSyncMsg); - } else if (pRpcMsg->msgType == SYNC_APPEND_ENTRIES_REPLY) { + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_APPEND_ENTRIES_REPLY) { SyncAppendEntriesReply* pSyncMsg = syncAppendEntriesReplyDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); pRoot = syncAppendEntriesReply2Json(pSyncMsg); syncAppendEntriesReplyDestroy(pSyncMsg); - } else if (pRpcMsg->msgType == SYNC_RESPONSE) { + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_COMMON_RESPONSE) { pRoot = cJSON_CreateObject(); char* s; s = syncUtilprintBin((char*)(pRpcMsg->pCont), pRpcMsg->contLen); @@ -98,7 +98,7 @@ cJSON* syncRpcMsg2Json(SRpcMsg* pRpcMsg) { cJSON* syncRpcUnknownMsg2Json() { cJSON* pRoot = cJSON_CreateObject(); - cJSON_AddNumberToObject(pRoot, "msgType", SYNC_UNKNOWN); + cJSON_AddNumberToObject(pRoot, "msgType", TDMT_VND_SYNC_UNKNOWN); cJSON_AddStringToObject(pRoot, "data", "unknown message"); cJSON* pJson = cJSON_CreateObject(); @@ -146,12 +146,14 @@ SyncTimeout* syncTimeoutBuild() { SyncTimeout* pMsg = taosMemoryMalloc(bytes); memset(pMsg, 0, bytes); pMsg->bytes = bytes; - pMsg->msgType = SYNC_TIMEOUT; + pMsg->msgType = TDMT_VND_SYNC_TIMEOUT; return pMsg; } -SyncTimeout* syncTimeoutBuild2(ESyncTimeoutType timeoutType, uint64_t logicClock, int32_t timerMS, void* data) { +SyncTimeout* syncTimeoutBuild2(ESyncTimeoutType timeoutType, uint64_t logicClock, int32_t timerMS, int32_t vgId, + void* data) { SyncTimeout* pMsg = syncTimeoutBuild(); + pMsg->vgId = vgId; pMsg->timeoutType = timeoutType; pMsg->logicClock = logicClock; pMsg->timerMS = timerMS; @@ -217,6 +219,7 @@ cJSON* syncTimeout2Json(const SyncTimeout* pMsg) { if (pMsg != NULL) { cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes); + cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId); cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType); cJSON_AddNumberToObject(pRoot, "timeoutType", pMsg->timeoutType); snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->logicClock); @@ -271,22 +274,23 @@ SyncPing* syncPingBuild(uint32_t dataLen) { SyncPing* pMsg = taosMemoryMalloc(bytes); memset(pMsg, 0, bytes); pMsg->bytes = bytes; - pMsg->msgType = SYNC_PING; + pMsg->msgType = TDMT_VND_SYNC_PING; pMsg->dataLen = dataLen; return pMsg; } -SyncPing* syncPingBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str) { +SyncPing* syncPingBuild2(const SRaftId* srcId, const SRaftId* destId, int32_t vgId, const char* str) { uint32_t dataLen = strlen(str) + 1; SyncPing* pMsg = syncPingBuild(dataLen); + pMsg->vgId = vgId; pMsg->srcId = *srcId; pMsg->destId = *destId; snprintf(pMsg->data, pMsg->dataLen, "%s", str); return pMsg; } -SyncPing* syncPingBuild3(const SRaftId* srcId, const SRaftId* destId) { - SyncPing* pMsg = syncPingBuild2(srcId, destId, "ping"); +SyncPing* syncPingBuild3(const SRaftId* srcId, const SRaftId* destId, int32_t vgId) { + SyncPing* pMsg = syncPingBuild2(srcId, destId, vgId, "ping"); return pMsg; } @@ -326,6 +330,98 @@ SyncPing* syncPingDeserialize2(const char* buf, uint32_t len) { return pMsg; } +int32_t syncPingSerialize3(const SyncPing* pMsg, char* buf, int32_t bufLen) { + SCoder encoder = {0}; + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); + if (tStartEncode(&encoder) < 0) { + return -1; + } + + if (tEncodeU32(&encoder, pMsg->bytes) < 0) { + return -1; + } + if (tEncodeI32(&encoder, pMsg->vgId) < 0) { + return -1; + } + if (tEncodeU32(&encoder, pMsg->msgType) < 0) { + return -1; + } + if (tEncodeU64(&encoder, pMsg->srcId.addr) < 0) { + return -1; + } + if (tEncodeI32(&encoder, pMsg->srcId.vgId) < 0) { + return -1; + } + if (tEncodeU64(&encoder, pMsg->destId.addr) < 0) { + return -1; + } + if (tEncodeI32(&encoder, pMsg->destId.vgId) < 0) { + return -1; + } + if (tEncodeU32(&encoder, pMsg->dataLen) < 0) { + return -1; + } + if (tEncodeBinary(&encoder, pMsg->data, pMsg->dataLen)) { + return -1; + } + + tEndEncode(&encoder); + int32_t tlen = encoder.pos; + tCoderClear(&encoder); + return tlen; +} + +SyncPing* syncPingDeserialize3(void* buf, int32_t bufLen) { + SCoder decoder = {0}; + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER); + if (tStartDecode(&decoder) < 0) { + return NULL; + } + + SyncPing* pMsg = NULL; + uint32_t bytes; + if (tDecodeU32(&decoder, &bytes) < 0) { + return NULL; + } + + pMsg = taosMemoryMalloc(bytes); + assert(pMsg != NULL); + pMsg->bytes = bytes; + + if (tDecodeI32(&decoder, &pMsg->vgId) < 0) { + return NULL; + } + if (tDecodeU32(&decoder, &pMsg->msgType) < 0) { + return NULL; + } + if (tDecodeU64(&decoder, &pMsg->srcId.addr) < 0) { + return NULL; + } + if (tDecodeI32(&decoder, &pMsg->srcId.vgId) < 0) { + return NULL; + } + if (tDecodeU64(&decoder, &pMsg->destId.addr) < 0) { + return NULL; + } + if (tDecodeI32(&decoder, &pMsg->destId.vgId) < 0) { + return NULL; + } + if (tDecodeU32(&decoder, &pMsg->dataLen) < 0) { + return NULL; + } + uint64_t len; + char* data = NULL; + if (tDecodeBinary(&decoder, (const void**)(&data), &len) < 0) { + return NULL; + } + assert(len = pMsg->dataLen); + memcpy(pMsg->data, data, len); + + tEndDecode(&decoder); + tCoderClear(&decoder); + return pMsg; +} + void syncPing2RpcMsg(const SyncPing* pMsg, SRpcMsg* pRpcMsg) { memset(pRpcMsg, 0, sizeof(*pRpcMsg)); pRpcMsg->msgType = pMsg->msgType; @@ -349,6 +445,7 @@ cJSON* syncPing2Json(const SyncPing* pMsg) { 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(); @@ -436,22 +533,23 @@ SyncPingReply* syncPingReplyBuild(uint32_t dataLen) { SyncPingReply* pMsg = taosMemoryMalloc(bytes); memset(pMsg, 0, bytes); pMsg->bytes = bytes; - pMsg->msgType = SYNC_PING_REPLY; + pMsg->msgType = TDMT_VND_SYNC_PING_REPLY; pMsg->dataLen = dataLen; return pMsg; } -SyncPingReply* syncPingReplyBuild2(const SRaftId* srcId, const SRaftId* destId, const char* str) { +SyncPingReply* syncPingReplyBuild2(const SRaftId* srcId, const SRaftId* destId, int32_t vgId, const char* str) { uint32_t dataLen = strlen(str) + 1; SyncPingReply* pMsg = syncPingReplyBuild(dataLen); + pMsg->vgId = vgId; pMsg->srcId = *srcId; pMsg->destId = *destId; snprintf(pMsg->data, pMsg->dataLen, "%s", str); return pMsg; } -SyncPingReply* syncPingReplyBuild3(const SRaftId* srcId, const SRaftId* destId) { - SyncPingReply* pMsg = syncPingReplyBuild2(srcId, destId, "pang"); +SyncPingReply* syncPingReplyBuild3(const SRaftId* srcId, const SRaftId* destId, int32_t vgId) { + SyncPingReply* pMsg = syncPingReplyBuild2(srcId, destId, vgId, "pang"); return pMsg; } @@ -491,6 +589,98 @@ SyncPingReply* syncPingReplyDeserialize2(const char* buf, uint32_t len) { return pMsg; } +int32_t syncPingReplySerialize3(const SyncPingReply* pMsg, char* buf, int32_t bufLen) { + SCoder encoder = {0}; + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); + if (tStartEncode(&encoder) < 0) { + return -1; + } + + if (tEncodeU32(&encoder, pMsg->bytes) < 0) { + return -1; + } + if (tEncodeI32(&encoder, pMsg->vgId) < 0) { + return -1; + } + if (tEncodeU32(&encoder, pMsg->msgType) < 0) { + return -1; + } + if (tEncodeU64(&encoder, pMsg->srcId.addr) < 0) { + return -1; + } + if (tEncodeI32(&encoder, pMsg->srcId.vgId) < 0) { + return -1; + } + if (tEncodeU64(&encoder, pMsg->destId.addr) < 0) { + return -1; + } + if (tEncodeI32(&encoder, pMsg->destId.vgId) < 0) { + return -1; + } + if (tEncodeU32(&encoder, pMsg->dataLen) < 0) { + return -1; + } + if (tEncodeBinary(&encoder, pMsg->data, pMsg->dataLen)) { + return -1; + } + + tEndEncode(&encoder); + int32_t tlen = encoder.pos; + tCoderClear(&encoder); + return tlen; +} + +SyncPingReply* syncPingReplyDeserialize3(void* buf, int32_t bufLen) { + SCoder decoder = {0}; + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER); + if (tStartDecode(&decoder) < 0) { + return NULL; + } + + SyncPingReply* pMsg = NULL; + uint32_t bytes; + if (tDecodeU32(&decoder, &bytes) < 0) { + return NULL; + } + + pMsg = taosMemoryMalloc(bytes); + assert(pMsg != NULL); + pMsg->bytes = bytes; + + if (tDecodeI32(&decoder, &pMsg->vgId) < 0) { + return NULL; + } + if (tDecodeU32(&decoder, &pMsg->msgType) < 0) { + return NULL; + } + if (tDecodeU64(&decoder, &pMsg->srcId.addr) < 0) { + return NULL; + } + if (tDecodeI32(&decoder, &pMsg->srcId.vgId) < 0) { + return NULL; + } + if (tDecodeU64(&decoder, &pMsg->destId.addr) < 0) { + return NULL; + } + if (tDecodeI32(&decoder, &pMsg->destId.vgId) < 0) { + return NULL; + } + if (tDecodeU32(&decoder, &pMsg->dataLen) < 0) { + return NULL; + } + uint64_t len; + char* data = NULL; + if (tDecodeBinary(&decoder, (const void**)(&data), &len) < 0) { + return NULL; + } + assert(len = pMsg->dataLen); + memcpy(pMsg->data, data, len); + + tEndDecode(&decoder); + tCoderClear(&decoder); + return pMsg; +} + void syncPingReply2RpcMsg(const SyncPingReply* pMsg, SRpcMsg* pRpcMsg) { memset(pRpcMsg, 0, sizeof(*pRpcMsg)); pRpcMsg->msgType = pMsg->msgType; @@ -514,6 +704,7 @@ cJSON* syncPingReply2Json(const SyncPingReply* pMsg) { 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(); @@ -601,7 +792,7 @@ SyncClientRequest* syncClientRequestBuild(uint32_t dataLen) { SyncClientRequest* pMsg = taosMemoryMalloc(bytes); memset(pMsg, 0, bytes); pMsg->bytes = bytes; - pMsg->msgType = SYNC_CLIENT_REQUEST; + pMsg->msgType = TDMT_VND_SYNC_CLIENT_REQUEST; pMsg->seqNum = 0; pMsg->isWeak = false; pMsg->dataLen = dataLen; @@ -609,8 +800,9 @@ SyncClientRequest* syncClientRequestBuild(uint32_t dataLen) { } // step 1. original SRpcMsg => SyncClientRequest, add seqNum, isWeak -SyncClientRequest* syncClientRequestBuild2(const SRpcMsg* pOriginalRpcMsg, uint64_t seqNum, bool isWeak) { +SyncClientRequest* syncClientRequestBuild2(const SRpcMsg* pOriginalRpcMsg, uint64_t seqNum, bool isWeak, int32_t vgId) { SyncClientRequest* pMsg = syncClientRequestBuild(pOriginalRpcMsg->contLen); + pMsg->vgId = vgId; pMsg->originalRpcType = pOriginalRpcMsg->msgType; pMsg->seqNum = seqNum; pMsg->isWeak = isWeak; @@ -678,6 +870,7 @@ cJSON* syncClientRequest2Json(const SyncClientRequest* pMsg) { if (pMsg != NULL) { cJSON_AddNumberToObject(pRoot, "bytes", pMsg->bytes); + cJSON_AddNumberToObject(pRoot, "vgId", pMsg->vgId); cJSON_AddNumberToObject(pRoot, "msgType", pMsg->msgType); cJSON_AddNumberToObject(pRoot, "originalRpcType", pMsg->originalRpcType); snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->seqNum); @@ -734,12 +927,13 @@ void syncClientRequestLog2(char* s, const SyncClientRequest* pMsg) { } // ---- message process SyncRequestVote---- -SyncRequestVote* syncRequestVoteBuild() { +SyncRequestVote* syncRequestVoteBuild(int32_t vgId) { uint32_t bytes = sizeof(SyncRequestVote); SyncRequestVote* pMsg = taosMemoryMalloc(bytes); memset(pMsg, 0, bytes); pMsg->bytes = bytes; - pMsg->msgType = SYNC_REQUEST_VOTE; + pMsg->vgId = vgId; + pMsg->msgType = TDMT_VND_SYNC_REQUEST_VOTE; return pMsg; } @@ -801,6 +995,7 @@ cJSON* syncRequestVote2Json(const SyncRequestVote* pMsg) { 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(); @@ -880,12 +1075,13 @@ void syncRequestVoteLog2(char* s, const SyncRequestVote* pMsg) { } // ---- message process SyncRequestVoteReply---- -SyncRequestVoteReply* syncRequestVoteReplyBuild() { +SyncRequestVoteReply* syncRequestVoteReplyBuild(int32_t vgId) { uint32_t bytes = sizeof(SyncRequestVoteReply); SyncRequestVoteReply* pMsg = taosMemoryMalloc(bytes); memset(pMsg, 0, bytes); pMsg->bytes = bytes; - pMsg->msgType = SYNC_REQUEST_VOTE_REPLY; + pMsg->vgId = vgId; + pMsg->msgType = TDMT_VND_SYNC_REQUEST_VOTE_REPLY; return pMsg; } @@ -947,6 +1143,7 @@ cJSON* syncRequestVoteReply2Json(const SyncRequestVoteReply* pMsg) { 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(); @@ -1023,12 +1220,13 @@ void syncRequestVoteReplyLog2(char* s, const SyncRequestVoteReply* pMsg) { } // ---- message process SyncAppendEntries---- -SyncAppendEntries* syncAppendEntriesBuild(uint32_t dataLen) { +SyncAppendEntries* syncAppendEntriesBuild(uint32_t dataLen, int32_t vgId) { uint32_t bytes = sizeof(SyncAppendEntries) + dataLen; SyncAppendEntries* pMsg = taosMemoryMalloc(bytes); memset(pMsg, 0, bytes); pMsg->bytes = bytes; - pMsg->msgType = SYNC_APPEND_ENTRIES; + pMsg->vgId = vgId; + pMsg->msgType = TDMT_VND_SYNC_APPEND_ENTRIES; pMsg->dataLen = dataLen; return pMsg; } @@ -1092,6 +1290,7 @@ cJSON* syncAppendEntries2Json(const SyncAppendEntries* pMsg) { 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(); @@ -1186,12 +1385,13 @@ void syncAppendEntriesLog2(char* s, const SyncAppendEntries* pMsg) { } // ---- message process SyncAppendEntriesReply---- -SyncAppendEntriesReply* syncAppendEntriesReplyBuild() { +SyncAppendEntriesReply* syncAppendEntriesReplyBuild(int32_t vgId) { uint32_t bytes = sizeof(SyncAppendEntriesReply); SyncAppendEntriesReply* pMsg = taosMemoryMalloc(bytes); memset(pMsg, 0, bytes); pMsg->bytes = bytes; - pMsg->msgType = SYNC_APPEND_ENTRIES_REPLY; + pMsg->vgId = vgId; + pMsg->msgType = TDMT_VND_SYNC_APPEND_ENTRIES_REPLY; return pMsg; } @@ -1253,6 +1453,7 @@ cJSON* syncAppendEntriesReply2Json(const SyncAppendEntriesReply* pMsg) { 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(); diff --git a/source/libs/sync/src/syncRaftCfg.c b/source/libs/sync/src/syncRaftCfg.c new file mode 100644 index 0000000000..dc540424ec --- /dev/null +++ b/source/libs/sync/src/syncRaftCfg.c @@ -0,0 +1,211 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#include "syncRaftCfg.h" +#include "cJSON.h" +#include "syncEnv.h" +#include "syncUtil.h" + +// file must already exist! +SRaftCfg *raftCfgOpen(const char *path) { + SRaftCfg *pCfg = taosMemoryMalloc(sizeof(SRaftCfg)); + snprintf(pCfg->path, sizeof(pCfg->path), "%s", path); + + pCfg->pFile = taosOpenFile(pCfg->path, TD_FILE_READ | TD_FILE_WRITE); + assert(pCfg->pFile != NULL); + + taosLSeekFile(pCfg->pFile, 0, SEEK_SET); + + char buf[1024]; + int len = taosReadFile(pCfg->pFile, buf, sizeof(buf)); + assert(len > 0); + + int32_t ret = syncCfgFromStr(buf, &(pCfg->cfg)); + assert(ret == 0); + + return pCfg; +} + +int32_t raftCfgClose(SRaftCfg *pRaftCfg) { + int64_t ret = taosCloseFile(&(pRaftCfg->pFile)); + assert(ret == 0); + taosMemoryFree(pRaftCfg); + return 0; +} + +int32_t raftCfgPersist(SRaftCfg *pRaftCfg) { + assert(pRaftCfg != NULL); + + char *s = syncCfg2Str(&(pRaftCfg->cfg)); + taosLSeekFile(pRaftCfg->pFile, 0, SEEK_SET); + int64_t ret = taosWriteFile(pRaftCfg->pFile, s, strlen(s) + 1); + assert(ret == strlen(s) + 1); + taosMemoryFree(s); + + taosFsyncFile(pRaftCfg->pFile); + return 0; +} + +cJSON *syncCfg2Json(SSyncCfg *pSyncCfg) { + char u64buf[128]; + cJSON *pRoot = cJSON_CreateObject(); + + if (pSyncCfg != NULL) { + cJSON_AddNumberToObject(pRoot, "replicaNum", pSyncCfg->replicaNum); + cJSON_AddNumberToObject(pRoot, "myIndex", pSyncCfg->myIndex); + + cJSON *pNodeInfoArr = cJSON_CreateArray(); + cJSON_AddItemToObject(pRoot, "nodeInfo", pNodeInfoArr); + for (int i = 0; i < pSyncCfg->replicaNum; ++i) { + cJSON *pNodeInfo = cJSON_CreateObject(); + cJSON_AddNumberToObject(pNodeInfo, "nodePort", ((pSyncCfg->nodeInfo)[i]).nodePort); + cJSON_AddStringToObject(pNodeInfo, "nodeFqdn", ((pSyncCfg->nodeInfo)[i]).nodeFqdn); + cJSON_AddItemToArray(pNodeInfoArr, pNodeInfo); + } + } + + cJSON *pJson = cJSON_CreateObject(); + cJSON_AddItemToObject(pJson, "SSyncCfg", pRoot); + return pJson; +} + +char *syncCfg2Str(SSyncCfg *pSyncCfg) { + cJSON *pJson = syncCfg2Json(pSyncCfg); + char * serialized = cJSON_Print(pJson); + cJSON_Delete(pJson); + return serialized; +} + +int32_t syncCfgFromJson(const cJSON *pRoot, SSyncCfg *pSyncCfg) { + memset(pSyncCfg, 0, sizeof(SSyncCfg)); + cJSON *pJson = cJSON_GetObjectItem(pRoot, "SSyncCfg"); + + cJSON *pReplicaNum = cJSON_GetObjectItem(pJson, "replicaNum"); + assert(cJSON_IsNumber(pReplicaNum)); + pSyncCfg->replicaNum = cJSON_GetNumberValue(pReplicaNum); + + cJSON *pMyIndex = cJSON_GetObjectItem(pJson, "myIndex"); + assert(cJSON_IsNumber(pMyIndex)); + pSyncCfg->myIndex = cJSON_GetNumberValue(pMyIndex); + + cJSON *pNodeInfoArr = cJSON_GetObjectItem(pJson, "nodeInfo"); + int arraySize = cJSON_GetArraySize(pNodeInfoArr); + assert(arraySize == pSyncCfg->replicaNum); + + for (int i = 0; i < arraySize; ++i) { + cJSON *pNodeInfo = cJSON_GetArrayItem(pNodeInfoArr, i); + assert(pNodeInfo != NULL); + + cJSON *pNodePort = cJSON_GetObjectItem(pNodeInfo, "nodePort"); + assert(cJSON_IsNumber(pNodePort)); + ((pSyncCfg->nodeInfo)[i]).nodePort = cJSON_GetNumberValue(pNodePort); + + cJSON *pNodeFqdn = cJSON_GetObjectItem(pNodeInfo, "nodeFqdn"); + assert(cJSON_IsString(pNodeFqdn)); + snprintf(((pSyncCfg->nodeInfo)[i]).nodeFqdn, sizeof(((pSyncCfg->nodeInfo)[i]).nodeFqdn), "%s", + pNodeFqdn->valuestring); + } + + return 0; +} + +int32_t syncCfgFromStr(const char *s, SSyncCfg *pSyncCfg) { + cJSON *pRoot = cJSON_Parse(s); + assert(pRoot != NULL); + + int32_t ret = syncCfgFromJson(pRoot, pSyncCfg); + assert(ret == 0); + + cJSON_Delete(pRoot); + return 0; +} + +cJSON *raftCfg2Json(SRaftCfg *pRaftCfg) { + cJSON *pJson = syncCfg2Json(&(pRaftCfg->cfg)); + return pJson; +} + +char *raftCfg2Str(SRaftCfg *pRaftCfg) { + char *s = syncCfg2Str(&(pRaftCfg->cfg)); + return s; +} + +int32_t syncCfgCreateFile(SSyncCfg *pCfg, const char *path) { + assert(pCfg != NULL); + + TdFilePtr pFile = taosOpenFile(path, TD_FILE_CREATE | TD_FILE_WRITE); + assert(pFile != NULL); + + char * s = syncCfg2Str(pCfg); + int64_t ret = taosWriteFile(pFile, s, strlen(s) + 1); + assert(ret == strlen(s) + 1); + + taosMemoryFree(s); + taosCloseFile(&pFile); + return 0; +} + +// for debug ---------------------- +void syncCfgPrint(SSyncCfg *pCfg) { + char *serialized = syncCfg2Str(pCfg); + printf("syncCfgPrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); + taosMemoryFree(serialized); +} + +void syncCfgPrint2(char *s, SSyncCfg *pCfg) { + char *serialized = syncCfg2Str(pCfg); + printf("syncCfgPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); + taosMemoryFree(serialized); +} + +void syncCfgLog(SSyncCfg *pCfg) { + char *serialized = syncCfg2Str(pCfg); + sTrace("syncCfgLog | len:%lu | %s", strlen(serialized), serialized); + taosMemoryFree(serialized); +} + +void syncCfgLog2(char *s, SSyncCfg *pCfg) { + char *serialized = syncCfg2Str(pCfg); + sTrace("syncCfgLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); + taosMemoryFree(serialized); +} + +void raftCfgPrint(SRaftCfg *pCfg) { + char *serialized = raftCfg2Str(pCfg); + printf("raftCfgPrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); + taosMemoryFree(serialized); +} + +void raftCfgPrint2(char *s, SRaftCfg *pCfg) { + char *serialized = raftCfg2Str(pCfg); + printf("raftCfgPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); + taosMemoryFree(serialized); +} + +void raftCfgLog(SRaftCfg *pCfg) { + char *serialized = raftCfg2Str(pCfg); + sTrace("raftCfgLog | len:%lu | %s", strlen(serialized), serialized); + taosMemoryFree(serialized); +} + +void raftCfgLog2(char *s, SRaftCfg *pCfg) { + char *serialized = raftCfg2Str(pCfg); + sTrace("raftCfgLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); + taosMemoryFree(serialized); +} diff --git a/source/libs/sync/src/syncRaftEntry.c b/source/libs/sync/src/syncRaftEntry.c index 3de39a6aa8..21ee35eaf9 100644 --- a/source/libs/sync/src/syncRaftEntry.c +++ b/source/libs/sync/src/syncRaftEntry.c @@ -28,13 +28,13 @@ SSyncRaftEntry* syncEntryBuild(uint32_t dataLen) { // step 4. SyncClientRequest => SSyncRaftEntry, add term, index SSyncRaftEntry* syncEntryBuild2(SyncClientRequest* pMsg, SyncTerm term, SyncIndex index) { - SSyncRaftEntry* pEntry = syncEntryBuild3(pMsg, term, index, SYNC_RAFT_ENTRY_DATA); + SSyncRaftEntry* pEntry = syncEntryBuild3(pMsg, term, index); assert(pEntry != NULL); return pEntry; } -SSyncRaftEntry* syncEntryBuild3(SyncClientRequest* pMsg, SyncTerm term, SyncIndex index, EntryType entryType) { +SSyncRaftEntry* syncEntryBuild3(SyncClientRequest* pMsg, SyncTerm term, SyncIndex index) { SSyncRaftEntry* pEntry = syncEntryBuild(pMsg->dataLen); assert(pEntry != NULL); @@ -44,19 +44,37 @@ SSyncRaftEntry* syncEntryBuild3(SyncClientRequest* pMsg, SyncTerm term, SyncInde pEntry->isWeak = pMsg->isWeak; pEntry->term = term; pEntry->index = index; - pEntry->entryType = entryType; pEntry->dataLen = pMsg->dataLen; memcpy(pEntry->data, pMsg->data, pMsg->dataLen); return pEntry; } -SSyncRaftEntry* syncEntryBuildNoop(SyncTerm term, SyncIndex index) { - SSyncRaftEntry* pEntry = syncEntryBuild(0); +SSyncRaftEntry* syncEntryBuildNoop(SyncTerm term, SyncIndex index, int32_t vgId) { + // init rpcMsg + SMsgHead head; + head.vgId = vgId; + head.contLen = sizeof(SMsgHead); + SRpcMsg rpcMsg; + memset(&rpcMsg, 0, sizeof(SRpcMsg)); + rpcMsg.contLen = head.contLen; + rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen); + rpcMsg.msgType = TDMT_VND_SYNC_NOOP; + memcpy(rpcMsg.pCont, &head, sizeof(head)); + + SSyncRaftEntry* pEntry = syncEntryBuild(rpcMsg.contLen); assert(pEntry != NULL); + + pEntry->msgType = TDMT_VND_SYNC_CLIENT_REQUEST; + pEntry->originalRpcType = TDMT_VND_SYNC_NOOP; + pEntry->seqNum = 0; + pEntry->isWeak = 0; pEntry->term = term; pEntry->index = index; - pEntry->entryType = SYNC_RAFT_ENTRY_NOOP; + + assert(pEntry->dataLen == rpcMsg.contLen); + memcpy(pEntry->data, rpcMsg.pCont, rpcMsg.contLen); + rpcFreeCont(rpcMsg.pCont); return pEntry; } @@ -103,7 +121,6 @@ cJSON* syncEntry2Json(const SSyncRaftEntry* pEntry) { cJSON_AddStringToObject(pRoot, "term", u64buf); snprintf(u64buf, sizeof(u64buf), "%lu", pEntry->index); cJSON_AddStringToObject(pRoot, "index", u64buf); - cJSON_AddNumberToObject(pRoot, "entryType", pEntry->entryType); cJSON_AddNumberToObject(pRoot, "dataLen", pEntry->dataLen); char* s; diff --git a/source/libs/sync/src/syncRaftLog.c b/source/libs/sync/src/syncRaftLog.c index 63c6265d10..dc903ad7b2 100644 --- a/source/libs/sync/src/syncRaftLog.c +++ b/source/libs/sync/src/syncRaftLog.c @@ -50,38 +50,46 @@ int32_t logStoreAppendEntry(SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry) { SyncIndex lastIndex = logStoreLastIndex(pLogStore); assert(pEntry->index == lastIndex + 1); - uint32_t len; - char* serialized = syncEntrySerialize(pEntry, &len); - assert(serialized != NULL); - int code = 0; - /* - code = walWrite(pWal, pEntry->index, pEntry->entryType, serialized, len); - assert(code == 0); - */ - assert(walWrite(pWal, pEntry->index, pEntry->entryType, serialized, len) == 0); + int code = 0; + SSyncLogMeta syncMeta; + syncMeta.isWeek = pEntry->isWeak; + syncMeta.seqNum = pEntry->seqNum; + syncMeta.term = pEntry->term; + code = walWriteWithSyncInfo(pWal, pEntry->index, pEntry->originalRpcType, syncMeta, pEntry->data, pEntry->dataLen); + assert(code == 0); walFsync(pWal, true); - taosMemoryFree(serialized); return code; } SSyncRaftEntry* logStoreGetEntry(SSyncLogStore* pLogStore, SyncIndex index) { SSyncLogStoreData* pData = pLogStore->data; SWal* pWal = pData->pWal; - SSyncRaftEntry* pEntry = NULL; if (index >= SYNC_INDEX_BEGIN && index <= logStoreLastIndex(pLogStore)) { SWalReadHandle* pWalHandle = walOpenReadHandle(pWal); assert(walReadWithHandle(pWalHandle, index) == 0); - pEntry = syncEntryDeserialize(pWalHandle->pHead->head.body, pWalHandle->pHead->head.len); + + SSyncRaftEntry* pEntry = syncEntryBuild(pWalHandle->pHead->head.len); assert(pEntry != NULL); + pEntry->msgType = TDMT_VND_SYNC_CLIENT_REQUEST; + pEntry->originalRpcType = pWalHandle->pHead->head.msgType; + pEntry->seqNum = pWalHandle->pHead->head.syncMeta.seqNum; + pEntry->isWeak = pWalHandle->pHead->head.syncMeta.isWeek; + pEntry->term = pWalHandle->pHead->head.syncMeta.term; + pEntry->index = index; + assert(pEntry->dataLen == pWalHandle->pHead->head.len); + memcpy(pEntry->data, pWalHandle->pHead->head.body, pWalHandle->pHead->head.len); + // need to hold, do not new every time!! walCloseReadHandle(pWalHandle); - } + return pEntry; - return pEntry; + } else { + return NULL; + } } int32_t logStoreTruncate(SSyncLogStore* pLogStore, SyncIndex fromIndex) { @@ -207,20 +215,20 @@ void logStorePrint(SSyncLogStore* pLogStore) { void logStorePrint2(char* s, SSyncLogStore* pLogStore) { char* serialized = logStore2Str(pLogStore); - printf("logStorePrint | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + printf("logStorePrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); fflush(NULL); taosMemoryFree(serialized); } void logStoreLog(SSyncLogStore* pLogStore) { char* serialized = logStore2Str(pLogStore); - sTrace("logStorePrint | len:%lu | %s", strlen(serialized), serialized); + sTraceLong("logStoreLog | len:%lu | %s", strlen(serialized), serialized); taosMemoryFree(serialized); } void logStoreLog2(char* s, SSyncLogStore* pLogStore) { char* serialized = logStore2Str(pLogStore); - sTrace("logStorePrint | len:%lu | %s | %s", strlen(serialized), s, serialized); + sTraceLong("logStoreLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); taosMemoryFree(serialized); } diff --git a/source/libs/sync/src/syncReplication.c b/source/libs/sync/src/syncReplication.c index 3ced7a18bc..2fdb8a0e17 100644 --- a/source/libs/sync/src/syncReplication.c +++ b/source/libs/sync/src/syncReplication.c @@ -77,7 +77,7 @@ int32_t syncNodeAppendEntriesPeers(SSyncNode* pSyncNode) { SyncAppendEntries* pMsg = NULL; SSyncRaftEntry* pEntry = logStoreGetEntry(pSyncNode->pLogStore, nextIndex); if (pEntry != NULL) { - pMsg = syncAppendEntriesBuild(pEntry->bytes); + pMsg = syncAppendEntriesBuild(pEntry->bytes, pSyncNode->vgId); assert(pMsg != NULL); // add pEntry into msg @@ -91,7 +91,7 @@ int32_t syncNodeAppendEntriesPeers(SSyncNode* pSyncNode) { } else { // maybe overflow, send empty record - pMsg = syncAppendEntriesBuild(0); + pMsg = syncAppendEntriesBuild(0, pSyncNode->vgId); assert(pMsg != NULL); } diff --git a/source/libs/sync/src/syncRequestVote.c b/source/libs/sync/src/syncRequestVote.c index e23748a81e..619a1546a9 100644 --- a/source/libs/sync/src/syncRequestVote.c +++ b/source/libs/sync/src/syncRequestVote.c @@ -64,7 +64,7 @@ int32_t syncNodeOnRequestVoteCb(SSyncNode* ths, SyncRequestVote* pMsg) { raftStoreVote(ths->pRaftStore, &(pMsg->srcId)); } - SyncRequestVoteReply* pReply = syncRequestVoteReplyBuild(); + SyncRequestVoteReply* pReply = syncRequestVoteReplyBuild(ths->vgId); pReply->srcId = ths->myRaftId; pReply->destId = pMsg->srcId; pReply->term = ths->pRaftStore->currentTerm; diff --git a/source/libs/sync/src/syncRequestVoteReply.c b/source/libs/sync/src/syncRequestVoteReply.c index 1531f701ff..a6348dff50 100644 --- a/source/libs/sync/src/syncRequestVoteReply.c +++ b/source/libs/sync/src/syncRequestVoteReply.c @@ -49,11 +49,20 @@ int32_t syncNodeOnRequestVoteReplyCb(SSyncNode* ths, SyncRequestVoteReply* pMsg) return ret; } - assert(!(pMsg->term > ths->pRaftStore->currentTerm)); - // no need this code, because if I receive reply.term, then I must have sent for that term. - // if (pMsg->term > ths->pRaftStore->currentTerm) { - // syncNodeUpdateTerm(ths, pMsg->term); - // } + // assert(!(pMsg->term > ths->pRaftStore->currentTerm)); + // no need this code, because if I receive reply.term, then I must have sent for that term. + // if (pMsg->term > ths->pRaftStore->currentTerm) { + // syncNodeUpdateTerm(ths, pMsg->term); + // } + + if (pMsg->term > ths->pRaftStore->currentTerm) { + char logBuf[128]; + snprintf(logBuf, sizeof(logBuf), "syncNodeOnRequestVoteReplyCb error term, receive:%lu current:%lu", pMsg->term, + ths->pRaftStore->currentTerm); + syncNodePrint2(logBuf, ths); + sError("%s", logBuf); + return ret; + } assert(pMsg->term == ths->pRaftStore->currentTerm); diff --git a/source/libs/sync/src/syncRespMgr.c b/source/libs/sync/src/syncRespMgr.c new file mode 100644 index 0000000000..47c73745fd --- /dev/null +++ b/source/libs/sync/src/syncRespMgr.c @@ -0,0 +1,93 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#include "syncRespMgr.h" + +SSyncRespMgr *syncRespMgrCreate(void *data, int64_t ttl) { + SSyncRespMgr *pObj = (SSyncRespMgr *)taosMemoryMalloc(sizeof(SSyncRespMgr)); + memset(pObj, 0, sizeof(SSyncRespMgr)); + + pObj->pRespHash = + taosHashInit(sizeof(uint64_t), taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); + assert(pObj->pRespHash != NULL); + pObj->ttl = ttl; + pObj->data = data; + pObj->seqNum = 0; + taosThreadMutexInit(&(pObj->mutex), NULL); + + return pObj; +} + +void syncRespMgrDestroy(SSyncRespMgr *pObj) { + taosThreadMutexLock(&(pObj->mutex)); + taosHashCleanup(pObj->pRespHash); + taosThreadMutexUnlock(&(pObj->mutex)); + taosThreadMutexDestroy(&(pObj->mutex)); + taosMemoryFree(pObj); +} + +int64_t syncRespMgrAdd(SSyncRespMgr *pObj, SRespStub *pStub) { + taosThreadMutexLock(&(pObj->mutex)); + + uint64_t keyCode = ++(pObj->seqNum); + taosHashPut(pObj->pRespHash, &keyCode, sizeof(keyCode), pStub, sizeof(SRespStub)); + + taosThreadMutexUnlock(&(pObj->mutex)); + return keyCode; +} + +int32_t syncRespMgrDel(SSyncRespMgr *pObj, uint64_t index) { + taosThreadMutexLock(&(pObj->mutex)); + + taosHashRemove(pObj->pRespHash, &index, sizeof(index)); + + taosThreadMutexUnlock(&(pObj->mutex)); + return 0; +} + +int32_t syncRespMgrGet(SSyncRespMgr *pObj, uint64_t index, SRespStub *pStub) { + taosThreadMutexLock(&(pObj->mutex)); + + void *pTmp = taosHashGet(pObj->pRespHash, &index, sizeof(index)); + if (pTmp != NULL) { + memcpy(pStub, pTmp, sizeof(SRespStub)); + taosThreadMutexUnlock(&(pObj->mutex)); + return 1; // get one object + } + taosThreadMutexUnlock(&(pObj->mutex)); + return 0; // get none object +} + +int32_t syncRespMgrGetAndDel(SSyncRespMgr *pObj, uint64_t index, SRespStub *pStub) { + taosThreadMutexLock(&(pObj->mutex)); + + void *pTmp = taosHashGet(pObj->pRespHash, &index, sizeof(index)); + if (pTmp != NULL) { + memcpy(pStub, pTmp, sizeof(SRespStub)); + taosThreadMutexUnlock(&(pObj->mutex)); + taosHashRemove(pObj->pRespHash, &index, sizeof(index)); + return 1; // get one object + } + taosThreadMutexUnlock(&(pObj->mutex)); + return 0; // get none object +} + +void syncRespClean(SSyncRespMgr *pObj) { + taosThreadMutexLock(&(pObj->mutex)); + syncRespCleanByTTL(pObj, pObj->ttl); + taosThreadMutexUnlock(&(pObj->mutex)); +} + +void syncRespCleanByTTL(SSyncRespMgr *pObj, int64_t ttl) {} \ No newline at end of file diff --git a/source/libs/sync/src/syncUtil.c b/source/libs/sync/src/syncUtil.c index 287500a587..3b20dab0db 100644 --- a/source/libs/sync/src/syncUtil.c +++ b/source/libs/sync/src/syncUtil.c @@ -197,3 +197,17 @@ SyncIndex syncUtilMaxIndex(SyncIndex a, SyncIndex b) { SyncIndex r = a > b ? a : b; return r; } + +void syncUtilMsgHtoN(void* msg) { + // htonl + SMsgHead* pHead = msg; + pHead->contLen = htonl(pHead->contLen); + pHead->vgId = htonl(pHead->vgId); +} + +void syncUtilMsgNtoH(void* msg) { + // ntohl + SMsgHead* pHead = msg; + pHead->contLen = ntohl(pHead->contLen); + pHead->vgId = ntohl(pHead->vgId); +} \ No newline at end of file diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index 58b01e4ca2..85fe285661 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -38,6 +38,8 @@ add_executable(syncReplicateTest3 "") add_executable(syncReplicateLoadTest "") add_executable(syncRefTest "") add_executable(syncLogStoreCheck "") +add_executable(syncRaftCfgTest "") +add_executable(syncRespMgrTest "") target_sources(syncTest @@ -200,206 +202,224 @@ target_sources(syncLogStoreCheck PRIVATE "syncLogStoreCheck.cpp" ) +target_sources(syncRaftCfgTest + PRIVATE + "syncRaftCfgTest.cpp" +) +target_sources(syncRespMgrTest + PRIVATE + "syncRespMgrTest.cpp" +) target_include_directories(syncTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncEnvTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncPingTimerTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncIOTickQTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncIOTickPingTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncIOSendMsgTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncIOClientTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncIOServerTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncRaftStoreTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncEnqTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncIndexTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncInitTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncUtilTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncVotesGrantedTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncVotesRespondTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncIndexMgrTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncLogStoreTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncEntryTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncRequestVoteTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncRequestVoteReplyTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncAppendEntriesTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncAppendEntriesReplyTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncClientRequestTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncTimeoutTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncPingTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncPingReplyTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncRpcMsgTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncPingTimerTest2 PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncPingSelfTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncElectTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncElectTest2 PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncElectTest3 PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncEncodeTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncWriteTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncReplicateTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncReplicateTest2 PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncReplicateTest3 PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncReplicateLoadTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncRefTest PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) target_include_directories(syncLogStoreCheck PUBLIC - "${TD_SOURCE_DIR}/include/libs/sync" + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) +target_include_directories(syncRaftCfgTest + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) +target_include_directories(syncRespMgrTest + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) @@ -564,6 +584,14 @@ target_link_libraries(syncLogStoreCheck sync gtest_main ) +target_link_libraries(syncRaftCfgTest + sync + gtest_main +) +target_link_libraries(syncRespMgrTest + sync + gtest_main +) enable_testing() diff --git a/source/libs/sync/test/syncAppendEntriesReplyTest.cpp b/source/libs/sync/test/syncAppendEntriesReplyTest.cpp index 72aeb155e7..a90259bc3a 100644 --- a/source/libs/sync/test/syncAppendEntriesReplyTest.cpp +++ b/source/libs/sync/test/syncAppendEntriesReplyTest.cpp @@ -15,7 +15,7 @@ void logTest() { } SyncAppendEntriesReply *createMsg() { - SyncAppendEntriesReply *pMsg = syncAppendEntriesReplyBuild(); + SyncAppendEntriesReply *pMsg = syncAppendEntriesReplyBuild(1000); pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); pMsg->srcId.vgId = 100; pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); @@ -27,7 +27,7 @@ SyncAppendEntriesReply *createMsg() { void test1() { SyncAppendEntriesReply *pMsg = createMsg(); - syncAppendEntriesReplyPrint2((char *)"test1:", pMsg); + syncAppendEntriesReplyLog2((char *)"test1:", pMsg); syncAppendEntriesReplyDestroy(pMsg); } @@ -36,10 +36,10 @@ void test2() { uint32_t len = pMsg->bytes; char * serialized = (char *)taosMemoryMalloc(len); syncAppendEntriesReplySerialize(pMsg, serialized, len); - SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyBuild(); + SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyBuild(1000); syncAppendEntriesReplyDeserialize(serialized, len, pMsg2); - syncAppendEntriesReplyPrint2((char *)"test2: syncAppendEntriesReplySerialize -> syncAppendEntriesReplyDeserialize ", - pMsg2); + syncAppendEntriesReplyLog2((char *)"test2: syncAppendEntriesReplySerialize -> syncAppendEntriesReplyDeserialize ", + pMsg2); taosMemoryFree(serialized); syncAppendEntriesReplyDestroy(pMsg); @@ -51,8 +51,8 @@ void test3() { uint32_t len; char * serialized = syncAppendEntriesReplySerialize2(pMsg, &len); SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyDeserialize2(serialized, len); - syncAppendEntriesReplyPrint2((char *)"test3: syncAppendEntriesReplySerialize3 -> syncAppendEntriesReplyDeserialize2 ", - pMsg2); + syncAppendEntriesReplyLog2((char *)"test3: syncAppendEntriesReplySerialize3 -> syncAppendEntriesReplyDeserialize2 ", + pMsg2); taosMemoryFree(serialized); syncAppendEntriesReplyDestroy(pMsg); @@ -63,11 +63,12 @@ void test4() { SyncAppendEntriesReply *pMsg = createMsg(); SRpcMsg rpcMsg; syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg); - SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyBuild(); + SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyBuild(1000); syncAppendEntriesReplyFromRpcMsg(&rpcMsg, pMsg2); - syncAppendEntriesReplyPrint2((char *)"test4: syncAppendEntriesReply2RpcMsg -> syncAppendEntriesReplyFromRpcMsg ", - pMsg2); + syncAppendEntriesReplyLog2((char *)"test4: syncAppendEntriesReply2RpcMsg -> syncAppendEntriesReplyFromRpcMsg ", + pMsg2); + rpcFreeCont(rpcMsg.pCont); syncAppendEntriesReplyDestroy(pMsg); syncAppendEntriesReplyDestroy(pMsg2); } @@ -77,17 +78,17 @@ void test5() { SRpcMsg rpcMsg; syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg); SyncAppendEntriesReply *pMsg2 = syncAppendEntriesReplyFromRpcMsg2(&rpcMsg); - syncAppendEntriesReplyPrint2((char *)"test5: syncAppendEntriesReply2RpcMsg -> syncAppendEntriesReplyFromRpcMsg2 ", - pMsg2); + syncAppendEntriesReplyLog2((char *)"test5: syncAppendEntriesReply2RpcMsg -> syncAppendEntriesReplyFromRpcMsg2 ", + pMsg2); + rpcFreeCont(rpcMsg.pCont); syncAppendEntriesReplyDestroy(pMsg); syncAppendEntriesReplyDestroy(pMsg2); } int main() { - // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; - sDebugFlag = 143 + 64; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; logTest(); test1(); diff --git a/source/libs/sync/test/syncAppendEntriesTest.cpp b/source/libs/sync/test/syncAppendEntriesTest.cpp index 69a0aee9a8..bb9f306a1c 100644 --- a/source/libs/sync/test/syncAppendEntriesTest.cpp +++ b/source/libs/sync/test/syncAppendEntriesTest.cpp @@ -15,7 +15,7 @@ void logTest() { } SyncAppendEntries *createMsg() { - SyncAppendEntries *pMsg = syncAppendEntriesBuild(20); + SyncAppendEntries *pMsg = syncAppendEntriesBuild(20, 1000); pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); pMsg->srcId.vgId = 100; pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); @@ -29,7 +29,7 @@ SyncAppendEntries *createMsg() { void test1() { SyncAppendEntries *pMsg = createMsg(); - syncAppendEntriesPrint2((char *)"test1:", pMsg); + syncAppendEntriesLog2((char *)"test1:", pMsg); syncAppendEntriesDestroy(pMsg); } @@ -38,9 +38,9 @@ void test2() { uint32_t len = pMsg->bytes; char * serialized = (char *)taosMemoryMalloc(len); syncAppendEntriesSerialize(pMsg, serialized, len); - SyncAppendEntries *pMsg2 = syncAppendEntriesBuild(pMsg->dataLen); + SyncAppendEntries *pMsg2 = syncAppendEntriesBuild(pMsg->dataLen, 1000); syncAppendEntriesDeserialize(serialized, len, pMsg2); - syncAppendEntriesPrint2((char *)"test2: syncAppendEntriesSerialize -> syncAppendEntriesDeserialize ", pMsg2); + syncAppendEntriesLog2((char *)"test2: syncAppendEntriesSerialize -> syncAppendEntriesDeserialize ", pMsg2); taosMemoryFree(serialized); syncAppendEntriesDestroy(pMsg); @@ -52,7 +52,7 @@ void test3() { uint32_t len; char * serialized = syncAppendEntriesSerialize2(pMsg, &len); SyncAppendEntries *pMsg2 = syncAppendEntriesDeserialize2(serialized, len); - syncAppendEntriesPrint2((char *)"test3: syncAppendEntriesSerialize3 -> syncAppendEntriesDeserialize2 ", pMsg2); + syncAppendEntriesLog2((char *)"test3: syncAppendEntriesSerialize3 -> syncAppendEntriesDeserialize2 ", pMsg2); taosMemoryFree(serialized); syncAppendEntriesDestroy(pMsg); @@ -65,8 +65,9 @@ void test4() { syncAppendEntries2RpcMsg(pMsg, &rpcMsg); SyncAppendEntries *pMsg2 = (SyncAppendEntries *)taosMemoryMalloc(rpcMsg.contLen); syncAppendEntriesFromRpcMsg(&rpcMsg, pMsg2); - syncAppendEntriesPrint2((char *)"test4: syncAppendEntries2RpcMsg -> syncAppendEntriesFromRpcMsg ", pMsg2); + syncAppendEntriesLog2((char *)"test4: syncAppendEntries2RpcMsg -> syncAppendEntriesFromRpcMsg ", pMsg2); + rpcFreeCont(rpcMsg.pCont); syncAppendEntriesDestroy(pMsg); syncAppendEntriesDestroy(pMsg2); } @@ -76,16 +77,16 @@ void test5() { SRpcMsg rpcMsg; syncAppendEntries2RpcMsg(pMsg, &rpcMsg); SyncAppendEntries *pMsg2 = syncAppendEntriesFromRpcMsg2(&rpcMsg); - syncAppendEntriesPrint2((char *)"test5: syncAppendEntries2RpcMsg -> syncAppendEntriesFromRpcMsg2 ", pMsg2); + syncAppendEntriesLog2((char *)"test5: syncAppendEntries2RpcMsg -> syncAppendEntriesFromRpcMsg2 ", pMsg2); + rpcFreeCont(rpcMsg.pCont); syncAppendEntriesDestroy(pMsg); syncAppendEntriesDestroy(pMsg2); } int main() { - // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; - sDebugFlag = 143 + 64; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; logTest(); test1(); diff --git a/source/libs/sync/test/syncClientRequestTest.cpp b/source/libs/sync/test/syncClientRequestTest.cpp index f22478d538..9376aca91f 100644 --- a/source/libs/sync/test/syncClientRequestTest.cpp +++ b/source/libs/sync/test/syncClientRequestTest.cpp @@ -21,13 +21,14 @@ SyncClientRequest *createMsg() { rpcMsg.contLen = 20; rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen); strcpy((char *)rpcMsg.pCont, "hello rpc"); - SyncClientRequest *pMsg = syncClientRequestBuild2(&rpcMsg, 123, true); + SyncClientRequest *pMsg = syncClientRequestBuild2(&rpcMsg, 123, true, 1000); + rpcFreeCont(rpcMsg.pCont); return pMsg; } void test1() { SyncClientRequest *pMsg = createMsg(); - syncClientRequestPrint2((char *)"test1:", pMsg); + syncClientRequestLog2((char *)"test1:", pMsg); syncClientRequestDestroy(pMsg); } @@ -38,7 +39,7 @@ void test2() { syncClientRequestSerialize(pMsg, serialized, len); SyncClientRequest *pMsg2 = syncClientRequestBuild(pMsg->dataLen); syncClientRequestDeserialize(serialized, len, pMsg2); - syncClientRequestPrint2((char *)"test2: syncClientRequestSerialize -> syncClientRequestDeserialize ", pMsg2); + syncClientRequestLog2((char *)"test2: syncClientRequestSerialize -> syncClientRequestDeserialize ", pMsg2); taosMemoryFree(serialized); syncClientRequestDestroy(pMsg); @@ -50,7 +51,7 @@ void test3() { uint32_t len; char * serialized = syncClientRequestSerialize2(pMsg, &len); SyncClientRequest *pMsg2 = syncClientRequestDeserialize2(serialized, len); - syncClientRequestPrint2((char *)"test3: syncClientRequestSerialize3 -> syncClientRequestDeserialize2 ", pMsg2); + syncClientRequestLog2((char *)"test3: syncClientRequestSerialize3 -> syncClientRequestDeserialize2 ", pMsg2); taosMemoryFree(serialized); syncClientRequestDestroy(pMsg); @@ -63,8 +64,9 @@ void test4() { syncClientRequest2RpcMsg(pMsg, &rpcMsg); SyncClientRequest *pMsg2 = (SyncClientRequest *)taosMemoryMalloc(rpcMsg.contLen); syncClientRequestFromRpcMsg(&rpcMsg, pMsg2); - syncClientRequestPrint2((char *)"test4: syncClientRequest2RpcMsg -> syncClientRequestFromRpcMsg ", pMsg2); + syncClientRequestLog2((char *)"test4: syncClientRequest2RpcMsg -> syncClientRequestFromRpcMsg ", pMsg2); + rpcFreeCont(rpcMsg.pCont); syncClientRequestDestroy(pMsg); syncClientRequestDestroy(pMsg2); } @@ -74,16 +76,16 @@ void test5() { SRpcMsg rpcMsg; syncClientRequest2RpcMsg(pMsg, &rpcMsg); SyncClientRequest *pMsg2 = syncClientRequestFromRpcMsg2(&rpcMsg); - syncClientRequestPrint2((char *)"test5: syncClientRequest2RpcMsg -> syncClientRequestFromRpcMsg2 ", pMsg2); + syncClientRequestLog2((char *)"test5: syncClientRequest2RpcMsg -> syncClientRequestFromRpcMsg2 ", pMsg2); + rpcFreeCont(rpcMsg.pCont); syncClientRequestDestroy(pMsg); syncClientRequestDestroy(pMsg2); } int main() { - // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; - sDebugFlag = 143 + 64; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; logTest(); test1(); diff --git a/source/libs/sync/test/syncElectTest.cpp b/source/libs/sync/test/syncElectTest.cpp index 251a4b538f..39b5eba1aa 100644 --- a/source/libs/sync/test/syncElectTest.cpp +++ b/source/libs/sync/test/syncElectTest.cpp @@ -20,11 +20,12 @@ uint16_t ports[] = {7010, 7110, 7210, 7310, 7410}; int32_t replicaNum = 3; int32_t myIndex = 0; -SRaftId ids[TSDB_MAX_REPLICA]; -SSyncInfo syncInfo; -SSyncFSM* pFsm; -SWal* pWal; -SSyncNode* gSyncNode; +SRaftId ids[TSDB_MAX_REPLICA]; +SSyncInfo syncInfo; +SSyncFSM* pFsm; +SWal* pWal; +SSyncNode* gSyncNode; +const char* pDir = "./syncElectTest"; SSyncNode* syncNodeInit() { syncInfo.vgId = 1234; @@ -33,7 +34,7 @@ SSyncNode* syncNodeInit() { syncInfo.queue = gSyncIO->pMsgQ; syncInfo.FpEqMsg = syncIOEqMsg; syncInfo.pFsm = pFsm; - snprintf(syncInfo.path, sizeof(syncInfo.path), "./elect_test_%d", myIndex); + snprintf(syncInfo.path, sizeof(syncInfo.path), "%s_sync_%d", pDir, myIndex); int code = walInit(); assert(code == 0); @@ -48,7 +49,7 @@ SSyncNode* syncNodeInit() { walCfg.level = TAOS_WAL_FSYNC; char tmpdir[128]; - snprintf(tmpdir, sizeof(tmpdir), "./elect_test_wal_%d", myIndex); + snprintf(tmpdir, sizeof(tmpdir), "%s_wal_%d", pDir, myIndex); pWal = walOpen(tmpdir, &walCfg); assert(pWal != NULL); @@ -78,6 +79,8 @@ SSyncNode* syncNodeInit() { gSyncIO->FpOnSyncTimeout = pSyncNode->FpOnTimeout; gSyncIO->pSyncNode = pSyncNode; + syncNodeStart(pSyncNode); + return pSyncNode; } @@ -110,16 +113,15 @@ int main(int argc, char** argv) { gSyncNode = syncInitTest(); assert(gSyncNode != NULL); - syncNodePrint2((char*)"", gSyncNode); + syncNodeLog2((char*)"", gSyncNode); initRaftId(gSyncNode); //--------------------------- while (1) { - sTrace( - "elect sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, electTimerMS:%d", - gSyncNode->state, syncUtilState2String(gSyncNode->state), gSyncNode->pRaftStore->currentTerm, - gSyncNode->electTimerLogicClock, gSyncNode->electTimerLogicClockUser, gSyncNode->electTimerMS); + char* s = syncNode2SimpleStr(gSyncNode); + sTrace("%s", s); + taosMemoryFree(s); taosMsleep(1000); } diff --git a/source/libs/sync/test/syncElectTest2.cpp b/source/libs/sync/test/syncElectTest2.cpp index 709d9d8580..d569b47fb1 100644 --- a/source/libs/sync/test/syncElectTest2.cpp +++ b/source/libs/sync/test/syncElectTest2.cpp @@ -21,10 +21,11 @@ uint16_t ports[] = {7010, 7110, 7210, 7310, 7410}; int32_t replicaNum = 3; int32_t myIndex = 0; -SRaftId ids[TSDB_MAX_REPLICA]; -SSyncInfo syncInfo; -SSyncFSM* pFsm; -SWal* pWal; +SRaftId ids[TSDB_MAX_REPLICA]; +SSyncInfo syncInfo; +SSyncFSM* pFsm; +SWal* pWal; +const char* pDir = "./syncElectTest2"; int64_t syncNodeInit() { syncInfo.vgId = 1234; @@ -33,7 +34,7 @@ int64_t syncNodeInit() { syncInfo.queue = gSyncIO->pMsgQ; syncInfo.FpEqMsg = syncIOEqMsg; syncInfo.pFsm = pFsm; - snprintf(syncInfo.path, sizeof(syncInfo.path), "./elect2_test_%d", myIndex); + snprintf(syncInfo.path, sizeof(syncInfo.path), "%s_sync_%d", pDir, myIndex); int code = walInit(); assert(code == 0); @@ -48,7 +49,7 @@ int64_t syncNodeInit() { walCfg.level = TAOS_WAL_FSYNC; char tmpdir[128]; - snprintf(tmpdir, sizeof(tmpdir), "./elect2_test_wal_%d", myIndex); + snprintf(tmpdir, sizeof(tmpdir), "%s_wal_%d", pDir, myIndex); pWal = walOpen(tmpdir, &walCfg); assert(pWal != NULL); @@ -64,8 +65,9 @@ int64_t syncNodeInit() { // taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn); } - int64_t rid = syncStart(&syncInfo); + int64_t rid = syncOpen(&syncInfo); assert(rid > 0); + syncStart(rid); SSyncNode* pSyncNode = (SSyncNode*)syncNodeAcquire(rid); assert(pSyncNode != NULL); @@ -81,6 +83,8 @@ int64_t syncNodeInit() { gSyncIO->FpOnSyncTimeout = pSyncNode->FpOnTimeout; gSyncIO->pSyncNode = pSyncNode; + syncNodeStart(pSyncNode); + syncNodeRelease(pSyncNode); return rid; @@ -117,7 +121,7 @@ int main(int argc, char** argv) { SSyncNode* pSyncNode = (SSyncNode*)syncNodeAcquire(rid); assert(pSyncNode != NULL); - syncNodePrint2((char*)"", pSyncNode); + syncNodeLog2((char*)"", pSyncNode); initRaftId(pSyncNode); //--------------------------- diff --git a/source/libs/sync/test/syncElectTest3.cpp b/source/libs/sync/test/syncElectTest3.cpp index e36a2d0fb1..59e1b993d8 100644 --- a/source/libs/sync/test/syncElectTest3.cpp +++ b/source/libs/sync/test/syncElectTest3.cpp @@ -21,10 +21,11 @@ uint16_t ports[] = {7010, 7110, 7210, 7310, 7410}; int32_t replicaNum = 3; int32_t myIndex = 0; -SRaftId ids[TSDB_MAX_REPLICA]; -SSyncInfo syncInfo; -SSyncFSM* pFsm; -SWal* pWal; +SRaftId ids[TSDB_MAX_REPLICA]; +SSyncInfo syncInfo; +SSyncFSM* pFsm; +SWal* pWal; +const char* pDir = "./syncElectTest3"; int64_t syncNodeInit() { syncInfo.vgId = 1234; @@ -33,7 +34,7 @@ int64_t syncNodeInit() { syncInfo.queue = gSyncIO->pMsgQ; syncInfo.FpEqMsg = syncIOEqMsg; syncInfo.pFsm = pFsm; - snprintf(syncInfo.path, sizeof(syncInfo.path), "./elect3_test_%d", myIndex); + snprintf(syncInfo.path, sizeof(syncInfo.path), "%s_sync_%d", pDir, myIndex); int code = walInit(); assert(code == 0); @@ -48,7 +49,7 @@ int64_t syncNodeInit() { walCfg.level = TAOS_WAL_FSYNC; char tmpdir[128]; - snprintf(tmpdir, sizeof(tmpdir), "./elect3_test_wal_%d", myIndex); + snprintf(tmpdir, sizeof(tmpdir), "%s_wal_%d", pDir, myIndex); pWal = walOpen(tmpdir, &walCfg); assert(pWal != NULL); @@ -64,8 +65,9 @@ int64_t syncNodeInit() { // taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn); } - int64_t rid = syncStart(&syncInfo); + int64_t rid = syncOpen(&syncInfo); assert(rid > 0); + syncStart(rid); SSyncNode* pSyncNode = (SSyncNode*)syncNodeAcquire(rid); assert(pSyncNode != NULL); @@ -84,6 +86,8 @@ int64_t syncNodeInit() { gSyncIO->FpOnSyncTimeout = pSyncNode->FpOnTimeout; gSyncIO->pSyncNode = pSyncNode; + syncNodeStart(pSyncNode); + syncNodeRelease(pSyncNode); return rid; @@ -120,7 +124,7 @@ int main(int argc, char** argv) { SSyncNode* pSyncNode = (SSyncNode*)syncNodeAcquire(rid); assert(pSyncNode != NULL); - syncNodePrint2((char*)"", pSyncNode); + syncNodeLog2((char*)"", pSyncNode); initRaftId(pSyncNode); //--------------------------- diff --git a/source/libs/sync/test/syncEncodeTest.cpp b/source/libs/sync/test/syncEncodeTest.cpp index b1ba95dc3e..f052d08a1e 100644 --- a/source/libs/sync/test/syncEncodeTest.cpp +++ b/source/libs/sync/test/syncEncodeTest.cpp @@ -102,7 +102,7 @@ SRpcMsg *step0() { } SyncClientRequest *step1(const SRpcMsg *pMsg) { - SyncClientRequest *pRetMsg = syncClientRequestBuild2(pMsg, 123, true); + SyncClientRequest *pRetMsg = syncClientRequestBuild2(pMsg, 123, true, 1000); return pRetMsg; } @@ -159,23 +159,23 @@ int main(int argc, char **argv) { // step0 SRpcMsg *pMsg0 = step0(); - syncRpcMsgPrint2((char *)"==step0==", pMsg0); + syncRpcMsgLog2((char *)"==step0==", pMsg0); // step1 SyncClientRequest *pMsg1 = step1(pMsg0); - syncClientRequestPrint2((char *)"==step1==", pMsg1); + syncClientRequestLog2((char *)"==step1==", pMsg1); // step2 SRpcMsg *pMsg2 = step2(pMsg1); - syncRpcMsgPrint2((char *)"==step2==", pMsg2); + syncRpcMsgLog2((char *)"==step2==", pMsg2); // step3 SyncClientRequest *pMsg3 = step3(pMsg2); - syncClientRequestPrint2((char *)"==step3==", pMsg3); + syncClientRequestLog2((char *)"==step3==", pMsg3); // step4 SSyncRaftEntry *pMsg4 = step4(pMsg3); - syncEntryPrint2((char *)"==step4==", pMsg4); + syncEntryLog2((char *)"==step4==", pMsg4); // log, relog SSyncNode *pSyncNode = syncNodeInit(); @@ -183,7 +183,7 @@ int main(int argc, char **argv) { SSyncRaftEntry *pEntry = pMsg4; pSyncNode->pLogStore->appendEntry(pSyncNode->pLogStore, pEntry); SSyncRaftEntry *pEntry2 = pSyncNode->pLogStore->getEntry(pSyncNode->pLogStore, pEntry->index); - syncEntryPrint2((char *)"==pEntry2==", pEntry2); + syncEntryLog2((char *)"==pEntry2==", pEntry2); // step5 uint32_t len; @@ -194,11 +194,11 @@ int main(int argc, char **argv) { // step6 SSyncRaftEntry *pMsg6 = step6(pMsg5, len); - syncEntryPrint2((char *)"==step6==", pMsg6); + syncEntryLog2((char *)"==step6==", pMsg6); // step7 SRpcMsg *pMsg7 = step7(pMsg6); - syncRpcMsgPrint2((char *)"==step7==", pMsg7); + syncRpcMsgLog2((char *)"==step7==", pMsg7); return 0; } diff --git a/source/libs/sync/test/syncEnqTest.cpp b/source/libs/sync/test/syncEnqTest.cpp index 48cc7caf2f..6f83ede5a0 100644 --- a/source/libs/sync/test/syncEnqTest.cpp +++ b/source/libs/sync/test/syncEnqTest.cpp @@ -89,14 +89,14 @@ int main(int argc, char** argv) { SSyncNode* pSyncNode = syncInitTest(); assert(pSyncNode != NULL); - syncNodePrint2((char*)"syncInitTest", pSyncNode); + syncNodeLog2((char*)"syncInitTest", pSyncNode); initRaftId(pSyncNode); //-------------------------------------------------------------- for (int i = 0; i < 10; ++i) { - SyncPingReply* pSyncMsg = syncPingReplyBuild2(&pSyncNode->myRaftId, &pSyncNode->myRaftId, "syncEnqTest"); + SyncPingReply* pSyncMsg = syncPingReplyBuild2(&pSyncNode->myRaftId, &pSyncNode->myRaftId, 1000, "syncEnqTest"); SRpcMsg rpcMsg; syncPingReply2RpcMsg(pSyncMsg, &rpcMsg); pSyncNode->FpEqMsg(pSyncNode->queue, &rpcMsg); diff --git a/source/libs/sync/test/syncEntryTest.cpp b/source/libs/sync/test/syncEntryTest.cpp index e8427e8168..53ae91fc60 100644 --- a/source/libs/sync/test/syncEntryTest.cpp +++ b/source/libs/sync/test/syncEntryTest.cpp @@ -52,7 +52,7 @@ void test3() { pSyncMsg->isWeak = 1; strcpy(pSyncMsg->data, "test3"); - SSyncRaftEntry* pEntry = syncEntryBuild3(pSyncMsg, 100, 200, SYNC_RAFT_ENTRY_NOOP); + SSyncRaftEntry* pEntry = syncEntryBuild3(pSyncMsg, 100, 200); syncEntryPrint(pEntry); syncClientRequestDestroy(pSyncMsg); @@ -68,7 +68,6 @@ void test4() { pEntry->isWeak = true; pEntry->term = 44; pEntry->index = 55; - pEntry->entryType = SYNC_RAFT_ENTRY_CONFIG; strcpy(pEntry->data, "test4"); syncEntryPrint(pEntry); @@ -84,9 +83,8 @@ void test4() { } int main(int argc, char** argv) { - // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; - sDebugFlag = 143 + 64; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; test1(); test2(); diff --git a/source/libs/sync/test/syncIOClientTest.cpp b/source/libs/sync/test/syncIOClientTest.cpp index dffa8b5cb9..c146ff4e92 100644 --- a/source/libs/sync/test/syncIOClientTest.cpp +++ b/source/libs/sync/test/syncIOClientTest.cpp @@ -38,7 +38,7 @@ int main() { destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); destId.vgId = 100; - SyncPingReply* pSyncMsg = syncPingReplyBuild2(&srcId, &destId, "syncIOClientTest"); + SyncPingReply* pSyncMsg = syncPingReplyBuild2(&srcId, &destId, 1000, "syncIOClientTest"); SRpcMsg rpcMsg; syncPingReply2RpcMsg(pSyncMsg, &rpcMsg); diff --git a/source/libs/sync/test/syncIOSendMsgTest.cpp b/source/libs/sync/test/syncIOSendMsgTest.cpp index 913e57e59a..03d308ea28 100644 --- a/source/libs/sync/test/syncIOSendMsgTest.cpp +++ b/source/libs/sync/test/syncIOSendMsgTest.cpp @@ -89,15 +89,16 @@ int main(int argc, char** argv) { SSyncNode* pSyncNode = syncInitTest(); assert(pSyncNode != NULL); - syncNodePrint2((char*)"syncInitTest", pSyncNode); + syncNodeLog2((char*)"syncInitTest", pSyncNode); initRaftId(pSyncNode); //-------------------------------------------------------------- for (int i = 0; i < 10; ++i) { - SyncPingReply* pSyncMsg = syncPingReplyBuild2(&pSyncNode->myRaftId, &pSyncNode->myRaftId, "syncIOSendMsgTest"); - SRpcMsg rpcMsg; + SyncPingReply* pSyncMsg = + syncPingReplyBuild2(&pSyncNode->myRaftId, &pSyncNode->myRaftId, 1000, "syncIOSendMsgTest"); + SRpcMsg rpcMsg; syncPingReply2RpcMsg(pSyncMsg, &rpcMsg); SEpSet epSet; diff --git a/source/libs/sync/test/syncIndexMgrTest.cpp b/source/libs/sync/test/syncIndexMgrTest.cpp index 319ea3e15a..ea5d5f6b6f 100644 --- a/source/libs/sync/test/syncIndexMgrTest.cpp +++ b/source/libs/sync/test/syncIndexMgrTest.cpp @@ -73,9 +73,8 @@ void initRaftId(SSyncNode* pSyncNode) { } int main(int argc, char** argv) { - // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; - sDebugFlag = 143 + 64; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; myIndex = 0; if (argc >= 2) { diff --git a/source/libs/sync/test/syncInitTest.cpp b/source/libs/sync/test/syncInitTest.cpp index 48b5488e41..ca0657c74d 100644 --- a/source/libs/sync/test/syncInitTest.cpp +++ b/source/libs/sync/test/syncInitTest.cpp @@ -30,7 +30,7 @@ SSyncNode* syncNodeInit() { syncInfo.queue = gSyncIO->pMsgQ; syncInfo.FpEqMsg = syncIOEqMsg; syncInfo.pFsm = pFsm; - snprintf(syncInfo.path, sizeof(syncInfo.path), "%s", "./"); + snprintf(syncInfo.path, sizeof(syncInfo.path), "%s", "./sync_init_test"); SSyncCfg* pCfg = &syncInfo.syncCfg; pCfg->myIndex = myIndex; @@ -64,15 +64,15 @@ 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); + sTrace("raftId[%d] : %s\n", i, s); taosMemoryFree(s); } } int main(int argc, char** argv) { - // taosInitLog((char *)"syncTest.log", 100000, 10); + // taosInitLog((char *)"tmp/syncInitTest.log", 100); tsAsyncLog = 0; - sDebugFlag = 143 + 64; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; myIndex = 0; if (argc >= 2) { @@ -88,11 +88,13 @@ int main(int argc, char** argv) { SSyncNode* pSyncNode = syncInitTest(); assert(pSyncNode != NULL); - syncNodePrint2((char*)"syncInitTest", pSyncNode); - + syncNodeLog2((char*)"syncInitTest", pSyncNode); initRaftId(pSyncNode); - //-------------------------------------------------------------- + syncNodeClose(pSyncNode); + syncEnvStop(); + // syncIOStop(); + // taosCloseLog(); return 0; } \ No newline at end of file diff --git a/source/libs/sync/test/syncLogStoreCheck.cpp b/source/libs/sync/test/syncLogStoreCheck.cpp index 85e39a61c5..b77a744c30 100644 --- a/source/libs/sync/test/syncLogStoreCheck.cpp +++ b/source/libs/sync/test/syncLogStoreCheck.cpp @@ -99,7 +99,7 @@ int main(int argc, char** argv) { pSyncNode = logStoreCheck(argv[1]); assert(pSyncNode != NULL); - logStorePrint2((char*)"logStoreCheck", pSyncNode->pLogStore); + logStoreLog2((char*)"logStoreCheck", pSyncNode->pLogStore); return 0; } diff --git a/source/libs/sync/test/syncLogStoreTest.cpp b/source/libs/sync/test/syncLogStoreTest.cpp index a5adba9a88..c4a5f6cae4 100644 --- a/source/libs/sync/test/syncLogStoreTest.cpp +++ b/source/libs/sync/test/syncLogStoreTest.cpp @@ -16,74 +16,44 @@ void logTest() { sFatal("--- sync log test: fatal"); } -uint16_t ports[] = {7010, 7110, 7210, 7310, 7410}; -int32_t replicaNum = 1; -int32_t myIndex = 0; +SSyncNode* pSyncNode; +SWal* pWal; +SSyncLogStore* pLogStore; +const char* pWalPath = "./syncLogStoreTest_wal"; -SRaftId ids[TSDB_MAX_REPLICA]; -SSyncInfo syncInfo; -SSyncFSM* pFsm; -SWal* pWal; -SSyncNode* pSyncNode; +void init() { + walInit(); + taosRemoveDir(pWalPath); -SSyncNode* syncNodeInit() { - syncInfo.vgId = 1234; - syncInfo.rpcClient = gSyncIO->clientRpc; - syncInfo.FpSendMsg = syncIOSendMsg; - syncInfo.queue = gSyncIO->pMsgQ; - syncInfo.FpEqMsg = syncIOEqMsg; - syncInfo.pFsm = pFsm; - snprintf(syncInfo.path, sizeof(syncInfo.path), "%s", "./"); - - int code = walInit(); - assert(code == 0); SWalCfg walCfg; memset(&walCfg, 0, sizeof(SWalCfg)); - walCfg.vgId = syncInfo.vgId; + walCfg.vgId = 1000; walCfg.fsyncPeriod = 1000; walCfg.retentionPeriod = 1000; walCfg.rollPeriod = 1000; walCfg.retentionSize = 1000; walCfg.segSize = 1000; walCfg.level = TAOS_WAL_FSYNC; - pWal = walOpen("./wal_test", &walCfg); + pWal = walOpen(pWalPath, &walCfg); assert(pWal != NULL); - syncInfo.pWal = pWal; - - SSyncCfg* pCfg = &syncInfo.syncCfg; - pCfg->myIndex = myIndex; - pCfg->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); - - 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->FpOnSyncTimeout = pSyncNode->FpOnTimeout; - gSyncIO->pSyncNode = pSyncNode; - - return pSyncNode; + pSyncNode = (SSyncNode*)taosMemoryMalloc(sizeof(SSyncNode)); + memset(pSyncNode, 0, sizeof(SSyncNode)); + pSyncNode->pWal = pWal; } -SSyncNode* syncInitTest() { return syncNodeInit(); } +void cleanup() { + walClose(pWal); + walCleanUp(); + taosMemoryFree(pSyncNode); +} void logStoreTest() { - logStorePrint2((char*)"logStoreTest", pSyncNode->pLogStore); + pLogStore = logStoreCreate(pSyncNode); + assert(pLogStore); + assert(pLogStore->getLastIndex(pLogStore) == SYNC_INDEX_INVALID); - assert(pSyncNode->pLogStore->getLastIndex(pSyncNode->pLogStore) == SYNC_INDEX_INVALID); + logStoreLog2((char*)"logStoreTest", pLogStore); for (int i = 0; i < 5; ++i) { int32_t dataLen = 10; @@ -93,58 +63,35 @@ void logStoreTest() { pEntry->originalRpcType = 2; pEntry->seqNum = 3; pEntry->isWeak = true; - pEntry->term = 100; - pEntry->index = pSyncNode->pLogStore->getLastIndex(pSyncNode->pLogStore) + 1; + pEntry->term = 100 + i; + pEntry->index = pLogStore->getLastIndex(pLogStore) + 1; snprintf(pEntry->data, dataLen, "value%d", i); - // syncEntryPrint2((char*)"write entry:", pEntry); - pSyncNode->pLogStore->appendEntry(pSyncNode->pLogStore, pEntry); + syncEntryLog2((char*)"==write entry== :", pEntry); + pLogStore->appendEntry(pLogStore, pEntry); syncEntryDestory(pEntry); if (i == 0) { - assert(pSyncNode->pLogStore->getLastIndex(pSyncNode->pLogStore) == SYNC_INDEX_BEGIN); + assert(pLogStore->getLastIndex(pLogStore) == SYNC_INDEX_BEGIN); } } - logStorePrint2((char*)"after appendEntry", pSyncNode->pLogStore); + logStoreLog2((char*)"after appendEntry", pLogStore); - pSyncNode->pLogStore->truncate(pSyncNode->pLogStore, 3); - logStorePrint2((char*)"after truncate 3", pSyncNode->pLogStore); -} + pLogStore->truncate(pLogStore, 3); + logStoreLog2((char*)"after truncate 3", pLogStore); -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); - } + logStoreDestory(pLogStore); } int main(int argc, char** argv) { - // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; - sDebugFlag = 143 + 64; - - myIndex = 0; - if (argc >= 2) { - myIndex = atoi(argv[1]); - } - - int32_t ret = syncIOStart((char*)"127.0.0.1", ports[myIndex]); - assert(ret == 0); - - ret = syncEnvStart(); - assert(ret == 0); - - taosRemoveDir("./wal_test"); - - pSyncNode = syncInitTest(); - assert(pSyncNode != NULL); - - // syncNodePrint((char*)"syncLogStoreTest", pSyncNode); - // initRaftId(pSyncNode); + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; + init(); logStoreTest(); + taosMsleep(2000); + cleanup(); + return 0; } diff --git a/source/libs/sync/test/syncPingReplyTest.cpp b/source/libs/sync/test/syncPingReplyTest.cpp index 40592eab6f..2bd8671e6c 100644 --- a/source/libs/sync/test/syncPingReplyTest.cpp +++ b/source/libs/sync/test/syncPingReplyTest.cpp @@ -20,13 +20,13 @@ SyncPingReply *createMsg() { srcId.vgId = 100; destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); destId.vgId = 100; - SyncPingReply *pMsg = syncPingReplyBuild3(&srcId, &destId); + SyncPingReply *pMsg = syncPingReplyBuild3(&srcId, &destId, 1000); return pMsg; } void test1() { SyncPingReply *pMsg = createMsg(); - syncPingReplyPrint2((char *)"test1:", pMsg); + syncPingReplyLog2((char *)"test1:", pMsg); syncPingReplyDestroy(pMsg); } @@ -37,7 +37,7 @@ void test2() { syncPingReplySerialize(pMsg, serialized, len); SyncPingReply *pMsg2 = syncPingReplyBuild(pMsg->dataLen); syncPingReplyDeserialize(serialized, len, pMsg2); - syncPingReplyPrint2((char *)"test2: syncPingReplySerialize -> syncPingReplyDeserialize ", pMsg2); + syncPingReplyLog2((char *)"test2: syncPingReplySerialize -> syncPingReplyDeserialize ", pMsg2); taosMemoryFree(serialized); syncPingReplyDestroy(pMsg); @@ -49,7 +49,7 @@ void test3() { uint32_t len; char * serialized = syncPingReplySerialize2(pMsg, &len); SyncPingReply *pMsg2 = syncPingReplyDeserialize2(serialized, len); - syncPingReplyPrint2((char *)"test3: syncPingReplySerialize3 -> syncPingReplyDeserialize2 ", pMsg2); + syncPingReplyLog2((char *)"test3: syncPingReplySerialize2 -> syncPingReplyDeserialize2 ", pMsg2); taosMemoryFree(serialized); syncPingReplyDestroy(pMsg); @@ -62,8 +62,9 @@ void test4() { syncPingReply2RpcMsg(pMsg, &rpcMsg); SyncPingReply *pMsg2 = (SyncPingReply *)taosMemoryMalloc(rpcMsg.contLen); syncPingReplyFromRpcMsg(&rpcMsg, pMsg2); - syncPingReplyPrint2((char *)"test4: syncPingReply2RpcMsg -> syncPingReplyFromRpcMsg ", pMsg2); + syncPingReplyLog2((char *)"test4: syncPingReply2RpcMsg -> syncPingReplyFromRpcMsg ", pMsg2); + rpcFreeCont(rpcMsg.pCont); syncPingReplyDestroy(pMsg); syncPingReplyDestroy(pMsg2); } @@ -73,16 +74,30 @@ void test5() { SRpcMsg rpcMsg; syncPingReply2RpcMsg(pMsg, &rpcMsg); SyncPingReply *pMsg2 = syncPingReplyFromRpcMsg2(&rpcMsg); - syncPingReplyPrint2((char *)"test5: syncPingReply2RpcMsg -> syncPingReplyFromRpcMsg2 ", pMsg2); + syncPingReplyLog2((char *)"test5: syncPingReply2RpcMsg -> syncPingReplyFromRpcMsg2 ", pMsg2); + rpcFreeCont(rpcMsg.pCont); + syncPingReplyDestroy(pMsg); + syncPingReplyDestroy(pMsg2); +} + +void test6() { + SyncPingReply *pMsg = createMsg(); + int32_t bufLen = syncPingReplySerialize3(pMsg, NULL, 0); + char * serialized = (char *)taosMemoryMalloc(bufLen); + syncPingReplySerialize3(pMsg, serialized, bufLen); + SyncPingReply *pMsg2 = syncPingReplyDeserialize3(serialized, bufLen); + assert(pMsg2 != NULL); + syncPingReplyLog2((char *)"test6: syncPingReplySerialize3 -> syncPingReplyDeserialize3 ", pMsg2); + + taosMemoryFree(serialized); syncPingReplyDestroy(pMsg); syncPingReplyDestroy(pMsg2); } int main() { - // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; - sDebugFlag = 143 + 64; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; logTest(); test1(); @@ -90,6 +105,7 @@ int main() { test3(); test4(); test5(); + test6(); return 0; } diff --git a/source/libs/sync/test/syncPingSelfTest.cpp b/source/libs/sync/test/syncPingSelfTest.cpp index 677518c6ac..641ff059be 100644 --- a/source/libs/sync/test/syncPingSelfTest.cpp +++ b/source/libs/sync/test/syncPingSelfTest.cpp @@ -87,7 +87,7 @@ int main(int argc, char** argv) { SSyncNode* pSyncNode = syncInitTest(); assert(pSyncNode != NULL); - syncNodePrint2((char*)"", pSyncNode); + syncNodeLog2((char*)"", pSyncNode); initRaftId(pSyncNode); diff --git a/source/libs/sync/test/syncPingTest.cpp b/source/libs/sync/test/syncPingTest.cpp index eb774d77c3..a02f5e4b43 100644 --- a/source/libs/sync/test/syncPingTest.cpp +++ b/source/libs/sync/test/syncPingTest.cpp @@ -20,13 +20,13 @@ SyncPing *createMsg() { srcId.vgId = 100; destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); destId.vgId = 100; - SyncPing *pMsg = syncPingBuild3(&srcId, &destId); + SyncPing *pMsg = syncPingBuild3(&srcId, &destId, 1000); return pMsg; } void test1() { SyncPing *pMsg = createMsg(); - syncPingPrint2((char *)"test1:", pMsg); + syncPingLog2((char *)"test1:", pMsg); syncPingDestroy(pMsg); } @@ -37,7 +37,7 @@ void test2() { syncPingSerialize(pMsg, serialized, len); SyncPing *pMsg2 = syncPingBuild(pMsg->dataLen); syncPingDeserialize(serialized, len, pMsg2); - syncPingPrint2((char *)"test2: syncPingSerialize -> syncPingDeserialize ", pMsg2); + syncPingLog2((char *)"test2: syncPingSerialize -> syncPingDeserialize ", pMsg2); taosMemoryFree(serialized); syncPingDestroy(pMsg); @@ -49,7 +49,7 @@ void test3() { uint32_t len; char * serialized = syncPingSerialize2(pMsg, &len); SyncPing *pMsg2 = syncPingDeserialize2(serialized, len); - syncPingPrint2((char *)"test3: syncPingSerialize3 -> syncPingDeserialize2 ", pMsg2); + syncPingLog2((char *)"test3: syncPingSerialize2 -> syncPingDeserialize2 ", pMsg2); taosMemoryFree(serialized); syncPingDestroy(pMsg); @@ -62,10 +62,11 @@ void test4() { syncPing2RpcMsg(pMsg, &rpcMsg); SyncPing *pMsg2 = (SyncPing *)taosMemoryMalloc(rpcMsg.contLen); syncPingFromRpcMsg(&rpcMsg, pMsg2); - syncPingPrint2((char *)"test4: syncPing2RpcMsg -> syncPingFromRpcMsg ", pMsg2); + syncPingLog2((char *)"test4: syncPing2RpcMsg -> syncPingFromRpcMsg ", pMsg2); syncPingDestroy(pMsg); syncPingDestroy(pMsg2); + rpcFreeCont(rpcMsg.pCont); } void test5() { @@ -73,16 +74,30 @@ void test5() { SRpcMsg rpcMsg; syncPing2RpcMsg(pMsg, &rpcMsg); SyncPing *pMsg2 = syncPingFromRpcMsg2(&rpcMsg); - syncPingPrint2((char *)"test5: syncPing2RpcMsg -> syncPingFromRpcMsg2 ", pMsg2); + syncPingLog2((char *)"test5: syncPing2RpcMsg -> syncPingFromRpcMsg2 ", pMsg2); syncPingDestroy(pMsg); syncPingDestroy(pMsg2); + rpcFreeCont(rpcMsg.pCont); +} + +void test6() { + SyncPing *pMsg = createMsg(); + int32_t bufLen = syncPingSerialize3(pMsg, NULL, 0); + char * serialized = (char *)taosMemoryMalloc(bufLen); + syncPingSerialize3(pMsg, serialized, bufLen); + SyncPing *pMsg2 = syncPingDeserialize3(serialized, bufLen); + assert(pMsg2 != NULL); + syncPingLog2((char *)"test6: syncPingSerialize3 -> syncPingDeserialize3 ", pMsg2); + + taosMemoryFree(serialized); + syncPingDestroy(pMsg); + syncPingDestroy(pMsg2); } int main() { - // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; - sDebugFlag = 143 + 64; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; logTest(); test1(); @@ -90,6 +105,7 @@ int main() { test3(); test4(); test5(); + test6(); return 0; } diff --git a/source/libs/sync/test/syncPingTimerTest.cpp b/source/libs/sync/test/syncPingTimerTest.cpp index 3c5e76ca14..29e99435be 100644 --- a/source/libs/sync/test/syncPingTimerTest.cpp +++ b/source/libs/sync/test/syncPingTimerTest.cpp @@ -87,7 +87,7 @@ int main(int argc, char** argv) { SSyncNode* pSyncNode = syncInitTest(); assert(pSyncNode != NULL); - syncNodePrint2((char*)"----1", pSyncNode); + syncNodeLog2((char*)"----1", pSyncNode); initRaftId(pSyncNode); @@ -96,7 +96,7 @@ int main(int argc, char** argv) { sTrace("syncNodeStartPingTimer ..."); ret = syncNodeStartPingTimer(pSyncNode); assert(ret == 0); - syncNodePrint2((char*)"----2", pSyncNode); + syncNodeLog2((char*)"----2", pSyncNode); sTrace("sleep ..."); taosMsleep(10000); @@ -104,7 +104,7 @@ int main(int argc, char** argv) { sTrace("syncNodeStopPingTimer ..."); ret = syncNodeStopPingTimer(pSyncNode); assert(ret == 0); - syncNodePrint2((char*)"----3", pSyncNode); + syncNodeLog2((char*)"----3", pSyncNode); sTrace("sleep ..."); taosMsleep(5000); @@ -112,7 +112,7 @@ int main(int argc, char** argv) { sTrace("syncNodeStartPingTimer ..."); ret = syncNodeStartPingTimer(pSyncNode); assert(ret == 0); - syncNodePrint2((char*)"----4", pSyncNode); + syncNodeLog2((char*)"----4", pSyncNode); sTrace("sleep ..."); taosMsleep(10000); @@ -120,7 +120,7 @@ int main(int argc, char** argv) { sTrace("syncNodeStopPingTimer ..."); ret = syncNodeStopPingTimer(pSyncNode); assert(ret == 0); - syncNodePrint2((char*)"----5", pSyncNode); + syncNodeLog2((char*)"----5", pSyncNode); while (1) { sTrace("while 1 sleep ..."); diff --git a/source/libs/sync/test/syncPingTimerTest2.cpp b/source/libs/sync/test/syncPingTimerTest2.cpp index 554f67d365..285828125d 100644 --- a/source/libs/sync/test/syncPingTimerTest2.cpp +++ b/source/libs/sync/test/syncPingTimerTest2.cpp @@ -87,7 +87,7 @@ int main(int argc, char** argv) { SSyncNode* pSyncNode = syncInitTest(); assert(pSyncNode != NULL); - syncNodePrint2((char*)"", pSyncNode); + syncNodeLog2((char*)"", pSyncNode); initRaftId(pSyncNode); diff --git a/source/libs/sync/test/syncRaftCfgTest.cpp b/source/libs/sync/test/syncRaftCfgTest.cpp new file mode 100644 index 0000000000..d3c06fa83e --- /dev/null +++ b/source/libs/sync/test/syncRaftCfgTest.cpp @@ -0,0 +1,101 @@ +#include "syncRaftStore.h" +//#include +#include +#include "syncIO.h" +#include "syncInt.h" +#include "syncRaftCfg.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"); +} + +SSyncCfg* createSyncCfg() { + SSyncCfg* pCfg = (SSyncCfg*)taosMemoryMalloc(sizeof(SSyncCfg)); + memset(pCfg, 0, sizeof(SSyncCfg)); + + pCfg->replicaNum = 3; + pCfg->myIndex = 1; + for (int i = 0; i < pCfg->replicaNum; ++i) { + ((pCfg->nodeInfo)[i]).nodePort = i * 100; + snprintf(((pCfg->nodeInfo)[i]).nodeFqdn, sizeof(((pCfg->nodeInfo)[i]).nodeFqdn), "100.200.300.%d", i); + } + + return pCfg; +} + +void test1() { + SSyncCfg* pCfg = createSyncCfg(); + syncCfgLog2((char*)__FUNCTION__, pCfg); + taosMemoryFree(pCfg); +} + +void test2() { + SSyncCfg* pCfg = createSyncCfg(); + char* s = syncCfg2Str(pCfg); + + SSyncCfg* pCfg2 = (SSyncCfg*)taosMemoryMalloc(sizeof(SSyncCfg)); + int32_t ret = syncCfgFromStr(s, pCfg2); + assert(ret == 0); + syncCfgLog2((char*)__FUNCTION__, pCfg2); + + taosMemoryFree(pCfg); + taosMemoryFree(s); + taosMemoryFree(pCfg2); +} + +void test3() { + SSyncCfg* pCfg = createSyncCfg(); + char* s = (char*)"./test3_raft_cfg.json"; + + if (taosCheckExistFile(s)) { + printf("%s file: %s already exist! \n", (char*)__FUNCTION__, s); + } else { + syncCfgCreateFile(pCfg, s); + printf("%s create json file: %s \n", (char*)__FUNCTION__, s); + } + + taosMemoryFree(pCfg); +} + +void test4() { + SRaftCfg* pCfg = raftCfgOpen("./test3_raft_cfg.json"); + assert(pCfg != NULL); + + raftCfgLog2((char*)__FUNCTION__, pCfg); + + int32_t ret = raftCfgClose(pCfg); + assert(ret == 0); +} + +void test5() { + SRaftCfg* pCfg = raftCfgOpen("./test3_raft_cfg.json"); + assert(pCfg != NULL); + + pCfg->cfg.myIndex = taosGetTimestampSec(); + raftCfgPersist(pCfg); + + printf("%s update json file: %s myIndex->%d \n", (char*)__FUNCTION__, "./test3_raft_cfg.json", pCfg->cfg.myIndex); + + int32_t ret = raftCfgClose(pCfg); + assert(ret == 0); +} + +int main() { + tsAsyncLog = 0; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; + + logTest(); + test1(); + test2(); + test3(); + test4(); + test5(); + + return 0; +} diff --git a/source/libs/sync/test/syncRaftStoreTest.cpp b/source/libs/sync/test/syncRaftStoreTest.cpp index 460ff90f4f..070f8c0f07 100644 --- a/source/libs/sync/test/syncRaftStoreTest.cpp +++ b/source/libs/sync/test/syncRaftStoreTest.cpp @@ -30,9 +30,8 @@ void initRaftId() { } int main() { - // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; - sDebugFlag = 143 + 64; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; logTest(); @@ -40,31 +39,33 @@ int main() { SRaftStore* pRaftStore = raftStoreOpen("./test_raft_store.json"); assert(pRaftStore != NULL); - raftStorePrint2((char*)"==raftStoreOpen==", pRaftStore); + raftStoreLog2((char*)"==raftStoreOpen==", pRaftStore); raftStoreSetTerm(pRaftStore, 100); - raftStorePrint2((char*)"==raftStoreSetTerm==", pRaftStore); + raftStoreLog2((char*)"==raftStoreSetTerm==", pRaftStore); raftStoreVote(pRaftStore, &ids[0]); - raftStorePrint2((char*)"==raftStoreVote==", pRaftStore); + raftStoreLog2((char*)"==raftStoreVote==", pRaftStore); raftStoreClearVote(pRaftStore); - raftStorePrint2((char*)"==raftStoreClearVote==", pRaftStore); + raftStoreLog2((char*)"==raftStoreClearVote==", pRaftStore); raftStoreVote(pRaftStore, &ids[1]); - raftStorePrint2((char*)"==raftStoreVote==", pRaftStore); + raftStoreLog2((char*)"==raftStoreVote==", pRaftStore); raftStoreNextTerm(pRaftStore); - raftStorePrint2((char*)"==raftStoreNextTerm==", pRaftStore); + raftStoreLog2((char*)"==raftStoreNextTerm==", pRaftStore); raftStoreNextTerm(pRaftStore); - raftStorePrint2((char*)"==raftStoreNextTerm==", pRaftStore); + raftStoreLog2((char*)"==raftStoreNextTerm==", pRaftStore); raftStoreNextTerm(pRaftStore); - raftStorePrint2((char*)"==raftStoreNextTerm==", pRaftStore); + raftStoreLog2((char*)"==raftStoreNextTerm==", pRaftStore); raftStoreNextTerm(pRaftStore); - raftStorePrint2((char*)"==raftStoreNextTerm==", pRaftStore); + raftStoreLog2((char*)"==raftStoreNextTerm==", pRaftStore); + + raftStoreClose(pRaftStore); return 0; } diff --git a/source/libs/sync/test/syncReplicateLoadTest.cpp b/source/libs/sync/test/syncReplicateLoadTest.cpp index 0841083b4a..f58c372445 100644 --- a/source/libs/sync/test/syncReplicateLoadTest.cpp +++ b/source/libs/sync/test/syncReplicateLoadTest.cpp @@ -27,29 +27,26 @@ SSyncInfo syncInfo; SSyncFSM *pFsm; SWal * pWal; -void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SyncIndex index, bool isWeak, int32_t code, - ESyncState state) { +void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { char logBuf[256]; snprintf(logBuf, sizeof(logBuf), "==callback== ==CommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", - pFsm, index, isWeak, code, state, syncUtilState2String(state)); - syncRpcMsgPrint2(logBuf, (SRpcMsg *)pMsg); + pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); } -void PreCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SyncIndex index, bool isWeak, int32_t code, - ESyncState state) { +void PreCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { char logBuf[256]; snprintf(logBuf, sizeof(logBuf), - "==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, index, isWeak, - code, state, syncUtilState2String(state)); - syncRpcMsgPrint2(logBuf, (SRpcMsg *)pMsg); + "==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, cbMeta.index, + cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); } -void RollBackCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SyncIndex index, bool isWeak, int32_t code, - ESyncState state) { +void RollBackCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { char logBuf[256]; snprintf(logBuf, sizeof(logBuf), "==callback== ==RollBackCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", - pFsm, index, isWeak, code, state, syncUtilState2String(state)); - syncRpcMsgPrint2(logBuf, (SRpcMsg *)pMsg); + pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); } void initFsm() { @@ -97,8 +94,9 @@ int64_t syncNodeInit() { // taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn); } - int64_t rid = syncStart(&syncInfo); + int64_t rid = syncOpen(&syncInfo); assert(rid > 0); + syncStart(rid); SSyncNode *pSyncNode = (SSyncNode *)syncNodeAcquire(rid); assert(pSyncNode != NULL); @@ -141,7 +139,7 @@ SRpcMsg *step0(int i) { } SyncClientRequest *step1(const SRpcMsg *pMsg) { - SyncClientRequest *pRetMsg = syncClientRequestBuild2(pMsg, 123, true); + SyncClientRequest *pRetMsg = syncClientRequestBuild2(pMsg, 123, true, 1000); return pRetMsg; } @@ -170,7 +168,7 @@ int main(int argc, char **argv) { SSyncNode *pSyncNode = (SSyncNode *)syncNodeAcquire(rid); assert(pSyncNode != NULL); - syncNodePrint2((char *)"", pSyncNode); + syncNodeLog2((char *)"", pSyncNode); initRaftId(pSyncNode); // only load ... diff --git a/source/libs/sync/test/syncReplicateTest.cpp b/source/libs/sync/test/syncReplicateTest.cpp index 9783bac7e5..9b5aaf5c18 100644 --- a/source/libs/sync/test/syncReplicateTest.cpp +++ b/source/libs/sync/test/syncReplicateTest.cpp @@ -28,29 +28,26 @@ SSyncFSM * pFsm; SWal * pWal; SSyncNode *gSyncNode; -void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SyncIndex index, bool isWeak, int32_t code, - ESyncState state) { +void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { char logBuf[256]; snprintf(logBuf, sizeof(logBuf), "==callback== ==CommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", - pFsm, index, isWeak, code, state, syncUtilState2String(state)); - syncRpcMsgPrint2(logBuf, (SRpcMsg *)pMsg); + pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); } -void PreCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SyncIndex index, bool isWeak, int32_t code, - ESyncState state) { +void PreCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { char logBuf[256]; snprintf(logBuf, sizeof(logBuf), - "==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, index, isWeak, - code, state, syncUtilState2String(state)); - syncRpcMsgPrint2(logBuf, (SRpcMsg *)pMsg); + "==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, cbMeta.index, + cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); } -void RollBackCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SyncIndex index, bool isWeak, int32_t code, - ESyncState state) { +void RollBackCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { char logBuf[256]; snprintf(logBuf, sizeof(logBuf), "==callback== ==RollBackCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", - pFsm, index, isWeak, code, state, syncUtilState2String(state)); - syncRpcMsgPrint2(logBuf, (SRpcMsg *)pMsg); + pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); } void initFsm() { @@ -136,7 +133,7 @@ SRpcMsg *step0(int i) { } SyncClientRequest *step1(const SRpcMsg *pMsg) { - SyncClientRequest *pRetMsg = syncClientRequestBuild2(pMsg, 123, true); + SyncClientRequest *pRetMsg = syncClientRequestBuild2(pMsg, 123, true, 1000); return pRetMsg; } @@ -163,18 +160,18 @@ int main(int argc, char **argv) { gSyncNode = syncInitTest(); assert(gSyncNode != NULL); - syncNodePrint2((char *)"", gSyncNode); + syncNodeLog2((char *)"", gSyncNode); initRaftId(gSyncNode); for (int i = 0; i < 30; ++i) { // step0 SRpcMsg *pMsg0 = step0(i); - syncRpcMsgPrint2((char *)"==step0==", pMsg0); + syncRpcMsgLog2((char *)"==step0==", pMsg0); // step1 SyncClientRequest *pMsg1 = step1(pMsg0); - syncClientRequestPrint2((char *)"==step1==", pMsg1); + syncClientRequestLog2((char *)"==step1==", pMsg1); SyncClientRequest *pSyncClientRequest = pMsg1; SRpcMsg rpcMsg; diff --git a/source/libs/sync/test/syncReplicateTest2.cpp b/source/libs/sync/test/syncReplicateTest2.cpp index 430df7eebd..1e2f823be5 100644 --- a/source/libs/sync/test/syncReplicateTest2.cpp +++ b/source/libs/sync/test/syncReplicateTest2.cpp @@ -27,29 +27,26 @@ SSyncInfo syncInfo; SSyncFSM *pFsm; SWal * pWal; -void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SyncIndex index, bool isWeak, int32_t code, - ESyncState state) { +void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { char logBuf[256]; snprintf(logBuf, sizeof(logBuf), "==callback== ==CommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", - pFsm, index, isWeak, code, state, syncUtilState2String(state)); - syncRpcMsgPrint2(logBuf, (SRpcMsg *)pMsg); + pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); } -void PreCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SyncIndex index, bool isWeak, int32_t code, - ESyncState state) { +void PreCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { char logBuf[256]; snprintf(logBuf, sizeof(logBuf), - "==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, index, isWeak, - code, state, syncUtilState2String(state)); - syncRpcMsgPrint2(logBuf, (SRpcMsg *)pMsg); + "==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, cbMeta.index, + cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); } -void RollBackCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SyncIndex index, bool isWeak, int32_t code, - ESyncState state) { +void RollBackCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { char logBuf[256]; snprintf(logBuf, sizeof(logBuf), "==callback== ==RollBackCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", - pFsm, index, isWeak, code, state, syncUtilState2String(state)); - syncRpcMsgPrint2(logBuf, (SRpcMsg *)pMsg); + pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); } void initFsm() { @@ -97,8 +94,9 @@ int64_t syncNodeInit() { // taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn); } - int64_t rid = syncStart(&syncInfo); + int64_t rid = syncOpen(&syncInfo); assert(rid > 0); + syncStart(rid); SSyncNode *pSyncNode = (SSyncNode *)syncNodeAcquire(rid); assert(pSyncNode != NULL); @@ -141,7 +139,7 @@ SRpcMsg *step0(int i) { } SyncClientRequest *step1(const SRpcMsg *pMsg) { - SyncClientRequest *pRetMsg = syncClientRequestBuild2(pMsg, 123, true); + SyncClientRequest *pRetMsg = syncClientRequestBuild2(pMsg, 123, true, 1000); return pRetMsg; } @@ -170,13 +168,13 @@ int main(int argc, char **argv) { SSyncNode *pSyncNode = (SSyncNode *)syncNodeAcquire(rid); assert(pSyncNode != NULL); - syncNodePrint2((char *)"", pSyncNode); + syncNodeLog2((char *)"", pSyncNode); initRaftId(pSyncNode); for (int i = 0; i < 30; ++i) { // step0 SRpcMsg *pMsg0 = step0(i); - syncRpcMsgPrint2((char *)"==step0==", pMsg0); + syncRpcMsgLog2((char *)"==step0==", pMsg0); syncPropose(rid, pMsg0, true); taosMsleep(1000); diff --git a/source/libs/sync/test/syncReplicateTest3.cpp b/source/libs/sync/test/syncReplicateTest3.cpp index 792b9c94cf..e8e24b507d 100644 --- a/source/libs/sync/test/syncReplicateTest3.cpp +++ b/source/libs/sync/test/syncReplicateTest3.cpp @@ -29,29 +29,26 @@ SSyncInfo syncInfo; SSyncFSM *pFsm; SWal * pWal; -void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SyncIndex index, bool isWeak, int32_t code, - ESyncState state) { +void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { char logBuf[256]; snprintf(logBuf, sizeof(logBuf), "==callback== ==CommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", - pFsm, index, isWeak, code, state, syncUtilState2String(state)); - syncRpcMsgPrint2(logBuf, (SRpcMsg *)pMsg); + pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); } -void PreCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SyncIndex index, bool isWeak, int32_t code, - ESyncState state) { +void PreCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { char logBuf[256]; snprintf(logBuf, sizeof(logBuf), - "==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, index, isWeak, - code, state, syncUtilState2String(state)); - syncRpcMsgPrint2(logBuf, (SRpcMsg *)pMsg); + "==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, cbMeta.index, + cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); } -void RollBackCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SyncIndex index, bool isWeak, int32_t code, - ESyncState state) { +void RollBackCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { char logBuf[256]; snprintf(logBuf, sizeof(logBuf), "==callback== ==RollBackCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", - pFsm, index, isWeak, code, state, syncUtilState2String(state)); - syncRpcMsgPrint2(logBuf, (SRpcMsg *)pMsg); + pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); } void initFsm() { @@ -99,8 +96,9 @@ int64_t syncNodeInit() { // taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn); } - int64_t rid = syncStart(&syncInfo); + int64_t rid = syncOpen(&syncInfo); assert(rid > 0); + syncStart(rid); SSyncNode *pSyncNode = (SSyncNode *)syncNodeAcquire(rid); assert(pSyncNode != NULL); @@ -143,7 +141,7 @@ SRpcMsg *step0(int i) { } SyncClientRequest *step1(const SRpcMsg *pMsg) { - SyncClientRequest *pRetMsg = syncClientRequestBuild2(pMsg, 123, true); + SyncClientRequest *pRetMsg = syncClientRequestBuild2(pMsg, 123, true, 1000); return pRetMsg; } @@ -182,13 +180,13 @@ int main(int argc, char **argv) { SSyncNode *pSyncNode = (SSyncNode *)syncNodeAcquire(rid); assert(pSyncNode != NULL); - syncNodePrint2((char *)"", pSyncNode); + syncNodeLog2((char *)"", pSyncNode); initRaftId(pSyncNode); for (int i = 0; i < recordCount; ++i) { // step0 SRpcMsg *pMsg0 = step0(i); - syncRpcMsgPrint2((char *)"==step0==", pMsg0); + syncRpcMsgLog2((char *)"==step0==", pMsg0); syncPropose(rid, pMsg0, true); taosMsleep(sleepMS); diff --git a/source/libs/sync/test/syncRequestVoteReplyTest.cpp b/source/libs/sync/test/syncRequestVoteReplyTest.cpp index 9e0ebee313..d9fe6fa011 100644 --- a/source/libs/sync/test/syncRequestVoteReplyTest.cpp +++ b/source/libs/sync/test/syncRequestVoteReplyTest.cpp @@ -15,7 +15,7 @@ void logTest() { } SyncRequestVoteReply *createMsg() { - SyncRequestVoteReply *pMsg = syncRequestVoteReplyBuild(); + SyncRequestVoteReply *pMsg = syncRequestVoteReplyBuild(1000); pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); pMsg->srcId.vgId = 100; pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); @@ -27,7 +27,7 @@ SyncRequestVoteReply *createMsg() { void test1() { SyncRequestVoteReply *pMsg = createMsg(); - syncRequestVoteReplyPrint2((char *)"test1:", pMsg); + syncRequestVoteReplyLog2((char *)"test1:", pMsg); syncRequestVoteReplyDestroy(pMsg); } @@ -36,9 +36,9 @@ void test2() { uint32_t len = pMsg->bytes; char * serialized = (char *)taosMemoryMalloc(len); syncRequestVoteReplySerialize(pMsg, serialized, len); - SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyBuild(); + SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyBuild(1000); syncRequestVoteReplyDeserialize(serialized, len, pMsg2); - syncRequestVoteReplyPrint2((char *)"test2: syncRequestVoteReplySerialize -> syncRequestVoteReplyDeserialize ", pMsg2); + syncRequestVoteReplyLog2((char *)"test2: syncRequestVoteReplySerialize -> syncRequestVoteReplyDeserialize ", pMsg2); taosMemoryFree(serialized); syncRequestVoteReplyDestroy(pMsg); @@ -50,8 +50,7 @@ void test3() { uint32_t len; char * serialized = syncRequestVoteReplySerialize2(pMsg, &len); SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyDeserialize2(serialized, len); - syncRequestVoteReplyPrint2((char *)"test3: syncRequestVoteReplySerialize3 -> syncRequestVoteReplyDeserialize2 ", - pMsg2); + syncRequestVoteReplyLog2((char *)"test3: syncRequestVoteReplySerialize3 -> syncRequestVoteReplyDeserialize2 ", pMsg2); taosMemoryFree(serialized); syncRequestVoteReplyDestroy(pMsg); @@ -62,10 +61,11 @@ void test4() { SyncRequestVoteReply *pMsg = createMsg(); SRpcMsg rpcMsg; syncRequestVoteReply2RpcMsg(pMsg, &rpcMsg); - SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyBuild(); + SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyBuild(1000); syncRequestVoteReplyFromRpcMsg(&rpcMsg, pMsg2); - syncRequestVoteReplyPrint2((char *)"test4: syncRequestVoteReply2RpcMsg -> syncRequestVoteReplyFromRpcMsg ", pMsg2); + syncRequestVoteReplyLog2((char *)"test4: syncRequestVoteReply2RpcMsg -> syncRequestVoteReplyFromRpcMsg ", pMsg2); + rpcFreeCont(rpcMsg.pCont); syncRequestVoteReplyDestroy(pMsg); syncRequestVoteReplyDestroy(pMsg2); } @@ -75,16 +75,16 @@ void test5() { SRpcMsg rpcMsg; syncRequestVoteReply2RpcMsg(pMsg, &rpcMsg); SyncRequestVoteReply *pMsg2 = syncRequestVoteReplyFromRpcMsg2(&rpcMsg); - syncRequestVoteReplyPrint2((char *)"test5: syncRequestVoteReply2RpcMsg -> syncRequestVoteReplyFromRpcMsg2 ", pMsg2); + syncRequestVoteReplyLog2((char *)"test5: syncRequestVoteReply2RpcMsg -> syncRequestVoteReplyFromRpcMsg2 ", pMsg2); + rpcFreeCont(rpcMsg.pCont); syncRequestVoteReplyDestroy(pMsg); syncRequestVoteReplyDestroy(pMsg2); } int main() { - // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; - sDebugFlag = 143 + 64; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; logTest(); test1(); diff --git a/source/libs/sync/test/syncRequestVoteTest.cpp b/source/libs/sync/test/syncRequestVoteTest.cpp index 6b2120e124..4c6d7f94ad 100644 --- a/source/libs/sync/test/syncRequestVoteTest.cpp +++ b/source/libs/sync/test/syncRequestVoteTest.cpp @@ -15,7 +15,7 @@ void logTest() { } SyncRequestVote *createMsg() { - SyncRequestVote *pMsg = syncRequestVoteBuild(); + SyncRequestVote *pMsg = syncRequestVoteBuild(1000); pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); pMsg->srcId.vgId = 100; pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); @@ -28,7 +28,7 @@ SyncRequestVote *createMsg() { void test1() { SyncRequestVote *pMsg = createMsg(); - syncRequestVotePrint2((char *)"test1:", pMsg); + syncRequestVoteLog2((char *)"test1:", pMsg); syncRequestVoteDestroy(pMsg); } @@ -37,9 +37,9 @@ void test2() { uint32_t len = pMsg->bytes; char * serialized = (char *)taosMemoryMalloc(len); syncRequestVoteSerialize(pMsg, serialized, len); - SyncRequestVote *pMsg2 = syncRequestVoteBuild(); + SyncRequestVote *pMsg2 = syncRequestVoteBuild(1000); syncRequestVoteDeserialize(serialized, len, pMsg2); - syncRequestVotePrint2((char *)"test2: syncRequestVoteSerialize -> syncRequestVoteDeserialize ", pMsg2); + syncRequestVoteLog2((char *)"test2: syncRequestVoteSerialize -> syncRequestVoteDeserialize ", pMsg2); taosMemoryFree(serialized); syncRequestVoteDestroy(pMsg); @@ -51,7 +51,7 @@ void test3() { uint32_t len; char * serialized = syncRequestVoteSerialize2(pMsg, &len); SyncRequestVote *pMsg2 = syncRequestVoteDeserialize2(serialized, len); - syncRequestVotePrint2((char *)"test3: syncRequestVoteSerialize3 -> syncRequestVoteDeserialize2 ", pMsg2); + syncRequestVoteLog2((char *)"test3: syncRequestVoteSerialize3 -> syncRequestVoteDeserialize2 ", pMsg2); taosMemoryFree(serialized); syncRequestVoteDestroy(pMsg); @@ -62,10 +62,11 @@ void test4() { SyncRequestVote *pMsg = createMsg(); SRpcMsg rpcMsg; syncRequestVote2RpcMsg(pMsg, &rpcMsg); - SyncRequestVote *pMsg2 = syncRequestVoteBuild(); + SyncRequestVote *pMsg2 = syncRequestVoteBuild(1000); syncRequestVoteFromRpcMsg(&rpcMsg, pMsg2); - syncRequestVotePrint2((char *)"test4: syncRequestVote2RpcMsg -> syncRequestVoteFromRpcMsg ", pMsg2); + syncRequestVoteLog2((char *)"test4: syncRequestVote2RpcMsg -> syncRequestVoteFromRpcMsg ", pMsg2); + rpcFreeCont(rpcMsg.pCont); syncRequestVoteDestroy(pMsg); syncRequestVoteDestroy(pMsg2); } @@ -75,16 +76,16 @@ void test5() { SRpcMsg rpcMsg; syncRequestVote2RpcMsg(pMsg, &rpcMsg); SyncRequestVote *pMsg2 = syncRequestVoteFromRpcMsg2(&rpcMsg); - syncRequestVotePrint2((char *)"test5: syncRequestVote2RpcMsg -> syncRequestVoteFromRpcMsg2 ", pMsg2); + syncRequestVoteLog2((char *)"test5: syncRequestVote2RpcMsg -> syncRequestVoteFromRpcMsg2 ", pMsg2); + rpcFreeCont(rpcMsg.pCont); syncRequestVoteDestroy(pMsg); syncRequestVoteDestroy(pMsg2); } int main() { - // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; - sDebugFlag = 143 + 64; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; logTest(); test1(); diff --git a/source/libs/sync/test/syncRespMgrTest.cpp b/source/libs/sync/test/syncRespMgrTest.cpp new file mode 100644 index 0000000000..d22c37d75d --- /dev/null +++ b/source/libs/sync/test/syncRespMgrTest.cpp @@ -0,0 +1,144 @@ +#include "syncRespMgr.h" +//#include +#include +#include "syncIO.h" +#include "syncInt.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"); +} +SSyncRespMgr *pMgr = NULL; + +void syncRespMgrInsert(uint64_t count) { + for (uint64_t i = 0; i < count; ++i) { + SRespStub stub; + memset(&stub, 0, sizeof(SRespStub)); + stub.createTime = taosGetTimestampMs(); + stub.rpcMsg.code = (pMgr->seqNum + 1); + stub.rpcMsg.ahandle = (void *)(200 + i); + stub.rpcMsg.handle = (void *)(300 + i); + uint64_t ret = syncRespMgrAdd(pMgr, &stub); + printf("insert %lu \n", ret); + } +} + +void syncRespMgrDelTest(uint64_t begin, uint64_t end) { + for (uint64_t i = begin; i <= end; ++i) { + int32_t ret = syncRespMgrDel(pMgr, i); + assert(ret == 0); + } +} + +void printStub(SRespStub *p) { + printf("createTime:%ld, rpcMsg.code:%d rpcMsg.ahandle:%ld rpcMsg.handle:%ld \n", p->createTime, p->rpcMsg.code, + (int64_t)(p->rpcMsg.ahandle), (int64_t)(p->rpcMsg.handle)); +} +void syncRespMgrPrint() { + printf("\n----------------syncRespMgrPrint--------------\n"); + taosThreadMutexLock(&(pMgr->mutex)); + + SRespStub *p = (SRespStub *)taosHashIterate(pMgr->pRespHash, NULL); + while (p) { + printStub(p); + p = (SRespStub *)taosHashIterate(pMgr->pRespHash, p); + } + + taosThreadMutexUnlock(&(pMgr->mutex)); +} + +void syncRespMgrGetTest(uint64_t i) { + printf("------syncRespMgrGetTest------- %lu -- \n", i); + SRespStub stub; + int32_t ret = syncRespMgrGet(pMgr, i, &stub); + if (ret == 1) { + printStub(&stub); + } else if (ret == 0) { + printf("%ld notFound \n", i); + } +} + +void syncRespMgrGetAndDelTest(uint64_t i) { + printf("------syncRespMgrGetAndDelTest-------%lu-- \n", i); + SRespStub stub; + int32_t ret = syncRespMgrGetAndDel(pMgr, i, &stub); + if (ret == 1) { + printStub(&stub); + } else if (ret == 0) { + printf("%ld notFound \n", i); + } +} + +void test1() { + printf("------- test1 ---------\n"); + pMgr = syncRespMgrCreate(NULL, 0); + assert(pMgr != NULL); + + syncRespMgrInsert(10); + syncRespMgrPrint(); + + printf("====== get print \n"); + for (uint64_t i = 1; i <= 10; ++i) { + syncRespMgrGetTest(i); + } + + printf("===== delete 5 - 7 \n"); + syncRespMgrDelTest(5, 7); + syncRespMgrPrint(); + + printf("====== get print \n"); + for (uint64_t i = 1; i <= 10; ++i) { + syncRespMgrGetTest(i); + } + + syncRespMgrDestroy(pMgr); +} + +void test2() { + printf("------- test2 ---------\n"); + pMgr = syncRespMgrCreate(NULL, 0); + assert(pMgr != NULL); + + syncRespMgrInsert(10); + syncRespMgrPrint(); + + printf("====== get and delete 3 - 7 \n"); + for (uint64_t i = 3; i <= 7; ++i) { + syncRespMgrGetAndDelTest(i); + } + + syncRespMgrPrint(); + syncRespMgrDestroy(pMgr); +} + +void test3() { + printf("------- test3 ---------\n"); + pMgr = syncRespMgrCreate(NULL, 0); + assert(pMgr != NULL); + + syncRespMgrInsert(10); + syncRespMgrPrint(); + + printf("====== get and delete 0 - 20 \n"); + for (uint64_t i = 0; i <= 20; ++i) { + syncRespMgrGetAndDelTest(i); + } + + syncRespMgrPrint(); + syncRespMgrDestroy(pMgr); +} + +int main() { + tsAsyncLog = 0; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; + logTest(); + test1(); + test2(); + test3(); + + return 0; +} diff --git a/source/libs/sync/test/syncRpcMsgTest.cpp b/source/libs/sync/test/syncRpcMsgTest.cpp index ee6f6c0800..0b21f41080 100644 --- a/source/libs/sync/test/syncRpcMsgTest.cpp +++ b/source/libs/sync/test/syncRpcMsgTest.cpp @@ -16,7 +16,7 @@ void logTest() { int gg = 0; SyncTimeout *createSyncTimeout() { - SyncTimeout *pMsg = syncTimeoutBuild2(SYNC_TIMEOUT_PING, 999, 333, &gg); + SyncTimeout *pMsg = syncTimeoutBuild2(SYNC_TIMEOUT_PING, 999, 333, 1000, &gg); return pMsg; } @@ -26,7 +26,7 @@ SyncPing *createSyncPing() { srcId.vgId = 100; destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); destId.vgId = 100; - SyncPing *pMsg = syncPingBuild3(&srcId, &destId); + SyncPing *pMsg = syncPingBuild3(&srcId, &destId, 1000); return pMsg; } @@ -36,7 +36,7 @@ SyncPingReply *createSyncPingReply() { srcId.vgId = 100; destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); destId.vgId = 100; - SyncPingReply *pMsg = syncPingReplyBuild3(&srcId, &destId); + SyncPingReply *pMsg = syncPingReplyBuild3(&srcId, &destId, 1000); return pMsg; } @@ -47,12 +47,12 @@ SyncClientRequest *createSyncClientRequest() { rpcMsg.contLen = 20; rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen); strcpy((char *)rpcMsg.pCont, "hello rpc"); - SyncClientRequest *pMsg = syncClientRequestBuild2(&rpcMsg, 123, true); + SyncClientRequest *pMsg = syncClientRequestBuild2(&rpcMsg, 123, true, 1000); return pMsg; } SyncRequestVote *createSyncRequestVote() { - SyncRequestVote *pMsg = syncRequestVoteBuild(); + SyncRequestVote *pMsg = syncRequestVoteBuild(1000); pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); pMsg->srcId.vgId = 100; pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); @@ -64,7 +64,7 @@ SyncRequestVote *createSyncRequestVote() { } SyncRequestVoteReply *createSyncRequestVoteReply() { - SyncRequestVoteReply *pMsg = syncRequestVoteReplyBuild(); + SyncRequestVoteReply *pMsg = syncRequestVoteReplyBuild(1000); pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); pMsg->srcId.vgId = 100; pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); @@ -75,7 +75,7 @@ SyncRequestVoteReply *createSyncRequestVoteReply() { } SyncAppendEntries *createSyncAppendEntries() { - SyncAppendEntries *pMsg = syncAppendEntriesBuild(20); + SyncAppendEntries *pMsg = syncAppendEntriesBuild(20, 1000); pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); pMsg->srcId.vgId = 100; pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); @@ -88,7 +88,7 @@ SyncAppendEntries *createSyncAppendEntries() { } SyncAppendEntriesReply *createSyncAppendEntriesReply() { - SyncAppendEntriesReply *pMsg = syncAppendEntriesReplyBuild(); + SyncAppendEntriesReply *pMsg = syncAppendEntriesReplyBuild(1000); pMsg->srcId.addr = syncUtilAddr2U64("127.0.0.1", 1234); pMsg->srcId.vgId = 100; pMsg->destId.addr = syncUtilAddr2U64("127.0.0.1", 5678); @@ -102,7 +102,7 @@ void test1() { SyncTimeout *pMsg = createSyncTimeout(); SRpcMsg rpcMsg; syncTimeout2RpcMsg(pMsg, &rpcMsg); - syncRpcMsgPrint2((char *)"test1", &rpcMsg); + syncRpcMsgLog2((char *)"test1", &rpcMsg); syncTimeoutDestroy(pMsg); } @@ -110,7 +110,7 @@ void test2() { SyncPing *pMsg = createSyncPing(); SRpcMsg rpcMsg; syncPing2RpcMsg(pMsg, &rpcMsg); - syncRpcMsgPrint2((char *)"test2", &rpcMsg); + syncRpcMsgLog2((char *)"test2", &rpcMsg); syncPingDestroy(pMsg); } @@ -118,7 +118,7 @@ void test3() { SyncPingReply *pMsg = createSyncPingReply(); SRpcMsg rpcMsg; syncPingReply2RpcMsg(pMsg, &rpcMsg); - syncRpcMsgPrint2((char *)"test3", &rpcMsg); + syncRpcMsgLog2((char *)"test3", &rpcMsg); syncPingReplyDestroy(pMsg); } @@ -126,7 +126,7 @@ void test4() { SyncRequestVote *pMsg = createSyncRequestVote(); SRpcMsg rpcMsg; syncRequestVote2RpcMsg(pMsg, &rpcMsg); - syncRpcMsgPrint2((char *)"test4", &rpcMsg); + syncRpcMsgLog2((char *)"test4", &rpcMsg); syncRequestVoteDestroy(pMsg); } @@ -134,7 +134,7 @@ void test5() { SyncRequestVoteReply *pMsg = createSyncRequestVoteReply(); SRpcMsg rpcMsg; syncRequestVoteReply2RpcMsg(pMsg, &rpcMsg); - syncRpcMsgPrint2((char *)"test5", &rpcMsg); + syncRpcMsgLog2((char *)"test5", &rpcMsg); syncRequestVoteReplyDestroy(pMsg); } @@ -142,7 +142,7 @@ void test6() { SyncAppendEntries *pMsg = createSyncAppendEntries(); SRpcMsg rpcMsg; syncAppendEntries2RpcMsg(pMsg, &rpcMsg); - syncRpcMsgPrint2((char *)"test6", &rpcMsg); + syncRpcMsgLog2((char *)"test6", &rpcMsg); syncAppendEntriesDestroy(pMsg); } @@ -150,7 +150,7 @@ void test7() { SyncAppendEntriesReply *pMsg = createSyncAppendEntriesReply(); SRpcMsg rpcMsg; syncAppendEntriesReply2RpcMsg(pMsg, &rpcMsg); - syncRpcMsgPrint2((char *)"test7", &rpcMsg); + syncRpcMsgLog2((char *)"test7", &rpcMsg); syncAppendEntriesReplyDestroy(pMsg); } @@ -158,14 +158,13 @@ void test8() { SyncClientRequest *pMsg = createSyncClientRequest(); SRpcMsg rpcMsg; syncClientRequest2RpcMsg(pMsg, &rpcMsg); - syncRpcMsgPrint2((char *)"test8", &rpcMsg); + syncRpcMsgLog2((char *)"test8", &rpcMsg); syncClientRequestDestroy(pMsg); } int main() { - // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; - sDebugFlag = 143 + 64; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; logTest(); test1(); diff --git a/source/libs/sync/test/syncTest.cpp b/source/libs/sync/test/syncTest.cpp index ec402ff57a..76024e061e 100644 --- a/source/libs/sync/test/syncTest.cpp +++ b/source/libs/sync/test/syncTest.cpp @@ -2,22 +2,62 @@ #include #include "syncIO.h" #include "syncInt.h" -#include "syncRaftStore.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"); +/* +typedef enum { + DEBUG_FATAL = 1, + DEBUG_ERROR = 1, + DEBUG_WARN = 2, + DEBUG_INFO = 2, + DEBUG_DEBUG = 4, + DEBUG_TRACE = 8, + DEBUG_DUMP = 16, + DEBUG_SCREEN = 64, + DEBUG_FILE = 128 +} ELogLevel; +*/ + +void logTest(char* s) { + sFatal("==%s== sync log test: fatal", s); + sError("==%s== sync log test: error", s); + + sWarn("==%s== sync log test: warn", s); + sInfo("==%s== sync log test: info", s); + + sDebug("==%s== sync log test: debug", s); + + sTrace("==%s== sync log test: trace", s); +} + +void test1() { + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; + logTest((char*)__FUNCTION__); +} + +void test2() { + sDebugFlag = DEBUG_DEBUG + DEBUG_SCREEN + DEBUG_FILE; + logTest((char*)__FUNCTION__); +} + +void test3() { + sDebugFlag = DEBUG_INFO + DEBUG_SCREEN + DEBUG_FILE; + logTest((char*)__FUNCTION__); +} + +void test4() { + sDebugFlag = DEBUG_ERROR + DEBUG_SCREEN + DEBUG_FILE; + logTest((char*)__FUNCTION__); } int main() { - // taosInitLog((char *)"syncTest.log", 100000, 10); + // taosInitLog("tmp/syncTest.log", 100); tsAsyncLog = 0; - sDebugFlag = 143 + 64; - logTest(); + test1(); + test2(); + test3(); + test4(); + + // taosCloseLog(); return 0; } diff --git a/source/libs/sync/test/syncTimeoutTest.cpp b/source/libs/sync/test/syncTimeoutTest.cpp index ab36f42d0d..30f25bd1d8 100644 --- a/source/libs/sync/test/syncTimeoutTest.cpp +++ b/source/libs/sync/test/syncTimeoutTest.cpp @@ -17,13 +17,13 @@ void logTest() { int gg = 0; SyncTimeout *createMsg() { - SyncTimeout *pMsg = syncTimeoutBuild2(SYNC_TIMEOUT_PING, 999, 333, &gg); + SyncTimeout *pMsg = syncTimeoutBuild2(SYNC_TIMEOUT_PING, 999, 333, 1000, &gg); return pMsg; } void test1() { SyncTimeout *pMsg = createMsg(); - syncTimeoutPrint2((char *)"test1:", pMsg); + syncTimeoutLog2((char *)"test1:", pMsg); syncTimeoutDestroy(pMsg); } @@ -34,7 +34,7 @@ void test2() { syncTimeoutSerialize(pMsg, serialized, len); SyncTimeout *pMsg2 = syncTimeoutBuild(); syncTimeoutDeserialize(serialized, len, pMsg2); - syncTimeoutPrint2((char *)"test2: syncTimeoutSerialize -> syncTimeoutDeserialize ", pMsg2); + syncTimeoutLog2((char *)"test2: syncTimeoutSerialize -> syncTimeoutDeserialize ", pMsg2); taosMemoryFree(serialized); syncTimeoutDestroy(pMsg); @@ -46,7 +46,7 @@ void test3() { uint32_t len; char * serialized = syncTimeoutSerialize2(pMsg, &len); SyncTimeout *pMsg2 = syncTimeoutDeserialize2(serialized, len); - syncTimeoutPrint2((char *)"test3: syncTimeoutSerialize3 -> syncTimeoutDeserialize2 ", pMsg2); + syncTimeoutLog2((char *)"test3: syncTimeoutSerialize3 -> syncTimeoutDeserialize2 ", pMsg2); taosMemoryFree(serialized); syncTimeoutDestroy(pMsg); @@ -59,8 +59,9 @@ void test4() { syncTimeout2RpcMsg(pMsg, &rpcMsg); SyncTimeout *pMsg2 = (SyncTimeout *)taosMemoryMalloc(rpcMsg.contLen); syncTimeoutFromRpcMsg(&rpcMsg, pMsg2); - syncTimeoutPrint2((char *)"test4: syncTimeout2RpcMsg -> syncTimeoutFromRpcMsg ", pMsg2); + syncTimeoutLog2((char *)"test4: syncTimeout2RpcMsg -> syncTimeoutFromRpcMsg ", pMsg2); + rpcFreeCont(rpcMsg.pCont); syncTimeoutDestroy(pMsg); syncTimeoutDestroy(pMsg2); } @@ -70,16 +71,16 @@ void test5() { SRpcMsg rpcMsg; syncTimeout2RpcMsg(pMsg, &rpcMsg); SyncTimeout *pMsg2 = syncTimeoutFromRpcMsg2(&rpcMsg); - syncTimeoutPrint2((char *)"test5: syncTimeout2RpcMsg -> syncTimeoutFromRpcMsg2 ", pMsg2); + syncTimeoutLog2((char *)"test5: syncTimeout2RpcMsg -> syncTimeoutFromRpcMsg2 ", pMsg2); + rpcFreeCont(rpcMsg.pCont); syncTimeoutDestroy(pMsg); syncTimeoutDestroy(pMsg2); } int main() { - // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; - sDebugFlag = 143 + 64; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; logTest(); test1(); diff --git a/source/libs/sync/test/syncUtilTest.cpp b/source/libs/sync/test/syncUtilTest.cpp index ae2afd2f53..411915fe22 100644 --- a/source/libs/sync/test/syncUtilTest.cpp +++ b/source/libs/sync/test/syncUtilTest.cpp @@ -22,9 +22,8 @@ void electRandomMSTest() { } int main() { - // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; - sDebugFlag = 143 + 64; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; logTest(); electRandomMSTest(); diff --git a/source/libs/sync/test/syncVotesGrantedTest.cpp b/source/libs/sync/test/syncVotesGrantedTest.cpp index 6164aed351..02a35b3d00 100644 --- a/source/libs/sync/test/syncVotesGrantedTest.cpp +++ b/source/libs/sync/test/syncVotesGrantedTest.cpp @@ -72,9 +72,8 @@ void initRaftId(SSyncNode* pSyncNode) { } int main(int argc, char** argv) { - // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; - sDebugFlag = 143 + 64; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; myIndex = 0; if (argc >= 2) { @@ -118,7 +117,7 @@ int main(int argc, char** argv) { } for (int i = 0; i < replicaNum; ++i) { - SyncRequestVoteReply* reply = syncRequestVoteReplyBuild(); + SyncRequestVoteReply* reply = syncRequestVoteReplyBuild(1000); reply->destId = pSyncNode->myRaftId; reply->srcId = ids[i]; reply->term = term; diff --git a/source/libs/sync/test/syncVotesRespondTest.cpp b/source/libs/sync/test/syncVotesRespondTest.cpp index 39ba6253a1..f276d34745 100644 --- a/source/libs/sync/test/syncVotesRespondTest.cpp +++ b/source/libs/sync/test/syncVotesRespondTest.cpp @@ -72,9 +72,8 @@ void initRaftId(SSyncNode* pSyncNode) { } int main(int argc, char** argv) { - // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; - sDebugFlag = 143 + 64; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; myIndex = 0; if (argc >= 2) { @@ -118,7 +117,7 @@ int main(int argc, char** argv) { } for (int i = 0; i < replicaNum; ++i) { - SyncRequestVoteReply* reply = syncRequestVoteReplyBuild(); + SyncRequestVoteReply* reply = syncRequestVoteReplyBuild(1000); reply->destId = pSyncNode->myRaftId; reply->srcId = ids[i]; reply->term = term; diff --git a/source/libs/sync/test/syncWriteTest.cpp b/source/libs/sync/test/syncWriteTest.cpp index a3c24f69ff..cff76a3ae7 100644 --- a/source/libs/sync/test/syncWriteTest.cpp +++ b/source/libs/sync/test/syncWriteTest.cpp @@ -28,29 +28,28 @@ SSyncFSM * pFsm; SWal * pWal; SSyncNode *gSyncNode; -void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SyncIndex index, bool isWeak, int32_t code, - ESyncState state) { +const char *pDir = "./syncWriteTest"; + +void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { char logBuf[256]; snprintf(logBuf, sizeof(logBuf), "==callback== ==CommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", - pFsm, index, isWeak, code, state, syncUtilState2String(state)); - syncRpcMsgPrint2(logBuf, (SRpcMsg *)pMsg); + pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); } -void PreCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SyncIndex index, bool isWeak, int32_t code, - ESyncState state) { +void PreCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { char logBuf[256]; snprintf(logBuf, sizeof(logBuf), - "==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, index, isWeak, - code, state, syncUtilState2String(state)); - syncRpcMsgPrint2(logBuf, (SRpcMsg *)pMsg); + "==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, cbMeta.index, + cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); } -void RollBackCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SyncIndex index, bool isWeak, int32_t code, - ESyncState state) { +void RollBackCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { char logBuf[256]; snprintf(logBuf, sizeof(logBuf), "==callback== ==RollBackCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", - pFsm, index, isWeak, code, state, syncUtilState2String(state)); - syncRpcMsgPrint2(logBuf, (SRpcMsg *)pMsg); + pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); } void initFsm() { @@ -67,7 +66,7 @@ SSyncNode *syncNodeInit() { syncInfo.queue = gSyncIO->pMsgQ; syncInfo.FpEqMsg = syncIOEqMsg; syncInfo.pFsm = pFsm; - snprintf(syncInfo.path, sizeof(syncInfo.path), "%s", "./write_test"); + snprintf(syncInfo.path, sizeof(syncInfo.path), "%s", pDir); int code = walInit(); assert(code == 0); @@ -108,6 +107,8 @@ SSyncNode *syncNodeInit() { gSyncIO->FpOnSyncTimeout = pSyncNode->FpOnTimeout; gSyncIO->pSyncNode = pSyncNode; + syncNodeStart(pSyncNode); + return pSyncNode; } @@ -133,7 +134,7 @@ SRpcMsg *step0() { } SyncClientRequest *step1(const SRpcMsg *pMsg) { - SyncClientRequest *pRetMsg = syncClientRequestBuild2(pMsg, 123, true); + SyncClientRequest *pRetMsg = syncClientRequestBuild2(pMsg, 123, true, 1000); return pRetMsg; } @@ -160,17 +161,17 @@ int main(int argc, char **argv) { gSyncNode = syncInitTest(); assert(gSyncNode != NULL); - syncNodePrint2((char *)"", gSyncNode); + syncNodeLog2((char *)"", gSyncNode); initRaftId(gSyncNode); // step0 SRpcMsg *pMsg0 = step0(); - syncRpcMsgPrint2((char *)"==step0==", pMsg0); + syncRpcMsgLog2((char *)"==step0==", pMsg0); // step1 SyncClientRequest *pMsg1 = step1(pMsg0); - syncClientRequestPrint2((char *)"==step1==", pMsg1); + syncClientRequestLog2((char *)"==step1==", pMsg1); for (int i = 0; i < 10; ++i) { SyncClientRequest *pSyncClientRequest = pMsg1; From 01008939f2c441c4b28f01c6eb1963720b4000b5 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Tue, 19 Apr 2022 11:51:51 +0800 Subject: [PATCH 02/51] sync snapshot --- include/libs/sync/sync.h | 10 +- include/libs/sync/syncTools.h | 6 + source/libs/sync/test/CMakeLists.txt | 14 ++ source/libs/sync/test/syncSnapshotTest.cpp | 213 +++++++++++++++++++++ 4 files changed, 236 insertions(+), 7 deletions(-) create mode 100644 source/libs/sync/test/syncSnapshotTest.cpp diff --git a/include/libs/sync/sync.h b/include/libs/sync/sync.h index 86666f4c68..6bafa29a50 100644 --- a/include/libs/sync/sync.h +++ b/include/libs/sync/sync.h @@ -50,14 +50,10 @@ typedef struct SSyncCfg { SNodeInfo nodeInfo[TSDB_MAX_REPLICA]; } SSyncCfg; -typedef struct SRaftId { - SyncNodeId addr; - SyncGroupId vgId; -} SRaftId; - typedef struct SSnapshot { void* data; SyncIndex lastApplyIndex; + SyncTerm lastApplyTerm; } SSnapshot; typedef enum { @@ -80,8 +76,8 @@ typedef struct SSyncFSM { void (*FpPreCommitCb)(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta); void (*FpRollBackCb)(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta); - int32_t (*FpTakeSnapshot)(SSnapshot* snapshot); - int32_t (*FpRestoreSnapshot)(const SSnapshot* snapshot); + int32_t (*FpGetSnapshot)(struct SSyncFSM* pFsm, SSnapshot* pSnapshot); + int32_t (*FpRestoreSnapshot)(struct SSyncFSM* pFsm, const SSnapshot* snapshot); } SSyncFSM; diff --git a/include/libs/sync/syncTools.h b/include/libs/sync/syncTools.h index 6d28f54e29..567889191a 100644 --- a/include/libs/sync/syncTools.h +++ b/include/libs/sync/syncTools.h @@ -27,6 +27,12 @@ extern "C" { #include "trpc.h" #include "wal.h" +// ------------------ ds ------------------- +typedef struct SRaftId { + SyncNodeId addr; + SyncGroupId vgId; +} SRaftId; + // ------------------ control ------------------- struct SSyncNode; typedef struct SSyncNode SSyncNode; diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index 85fe285661..e2ac38150f 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -40,6 +40,7 @@ add_executable(syncRefTest "") add_executable(syncLogStoreCheck "") add_executable(syncRaftCfgTest "") add_executable(syncRespMgrTest "") +add_executable(syncSnapshotTest "") target_sources(syncTest @@ -210,6 +211,10 @@ target_sources(syncRespMgrTest PRIVATE "syncRespMgrTest.cpp" ) +target_sources(syncSnapshotTest + PRIVATE + "syncSnapshotTest.cpp" +) target_include_directories(syncTest @@ -422,6 +427,11 @@ target_include_directories(syncRespMgrTest "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) +target_include_directories(syncSnapshotTest + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) target_link_libraries(syncTest @@ -592,6 +602,10 @@ target_link_libraries(syncRespMgrTest sync gtest_main ) +target_link_libraries(syncSnapshotTest + sync + gtest_main +) enable_testing() diff --git a/source/libs/sync/test/syncSnapshotTest.cpp b/source/libs/sync/test/syncSnapshotTest.cpp new file mode 100644 index 0000000000..e55f37d2d6 --- /dev/null +++ b/source/libs/sync/test/syncSnapshotTest.cpp @@ -0,0 +1,213 @@ +#include +#include +#include "syncEnv.h" +#include "syncIO.h" +#include "syncInt.h" +#include "syncMessage.h" +#include "syncRaftEntry.h" +#include "syncRaftLog.h" +#include "syncRaftStore.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"); +} + +uint16_t ports[] = {7010, 7110, 7210, 7310, 7410}; +int32_t replicaNum = 1; +int32_t myIndex = 0; + +SRaftId ids[TSDB_MAX_REPLICA]; +SSyncInfo syncInfo; +SSyncFSM * pFsm; +SWal * pWal; +SSyncNode *gSyncNode; +SyncIndex snapshotLastApplyIndex = SYNC_INDEX_INVALID; + +const char *pDir = "./syncSnapshotTest"; +const char *pWalDir = "./syncSnapshotTest_wal"; + +void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { + SyncIndex beginIndex = SYNC_INDEX_INVALID; + if (pFsm->FpGetSnapshot != NULL) { + SSnapshot snapshot; + pFsm->FpGetSnapshot(pFsm, &snapshot); + beginIndex = snapshot.lastApplyIndex; + } + + if (cbMeta.index > beginIndex) { + char logBuf[256]; + snprintf(logBuf, sizeof(logBuf), "==callback== ==CommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", + pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); + } else { + sTrace("==callback== ==CommitCb== do not apply again %ld", cbMeta.index); + } +} + +void PreCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { + char logBuf[256]; + snprintf(logBuf, sizeof(logBuf), + "==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, cbMeta.index, + cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); +} + +void RollBackCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { + char logBuf[256]; + snprintf(logBuf, sizeof(logBuf), "==callback== ==RollBackCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", + pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); +} + +int32_t GetSnapshotCb(struct SSyncFSM *pFsm, SSnapshot *pSnapshot) { + pSnapshot->data = NULL; + pSnapshot->lastApplyIndex = snapshotLastApplyIndex; + pSnapshot->lastApplyTerm = 100; + return 0; +} + +void initFsm() { + pFsm = (SSyncFSM *)taosMemoryMalloc(sizeof(SSyncFSM)); + pFsm->FpCommitCb = CommitCb; + pFsm->FpPreCommitCb = PreCommitCb; + pFsm->FpRollBackCb = RollBackCb; + pFsm->FpGetSnapshot = GetSnapshotCb; +} + +SSyncNode *syncNodeInit() { + syncInfo.vgId = 1234; + syncInfo.rpcClient = gSyncIO->clientRpc; + syncInfo.FpSendMsg = syncIOSendMsg; + syncInfo.queue = gSyncIO->pMsgQ; + syncInfo.FpEqMsg = syncIOEqMsg; + syncInfo.pFsm = pFsm; + snprintf(syncInfo.path, sizeof(syncInfo.path), "%s", pDir); + + int code = walInit(); + assert(code == 0); + SWalCfg walCfg; + memset(&walCfg, 0, sizeof(SWalCfg)); + walCfg.vgId = syncInfo.vgId; + walCfg.fsyncPeriod = 1000; + walCfg.retentionPeriod = 1000; + walCfg.rollPeriod = 1000; + walCfg.retentionSize = 1000; + walCfg.segSize = 1000; + walCfg.level = TAOS_WAL_FSYNC; + pWal = walOpen(pWalDir, &walCfg); + assert(pWal != NULL); + + syncInfo.pWal = pWal; + + SSyncCfg *pCfg = &syncInfo.syncCfg; + pCfg->myIndex = myIndex; + pCfg->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); + } + + SSyncNode *pSyncNode = syncNodeOpen(&syncInfo); + assert(pSyncNode != NULL); + + gSyncIO->FpOnSyncPing = pSyncNode->FpOnPing; + gSyncIO->FpOnSyncClientRequest = pSyncNode->FpOnClientRequest; + gSyncIO->FpOnSyncPingReply = pSyncNode->FpOnPingReply; + gSyncIO->FpOnSyncRequestVote = pSyncNode->FpOnRequestVote; + gSyncIO->FpOnSyncRequestVoteReply = pSyncNode->FpOnRequestVoteReply; + gSyncIO->FpOnSyncAppendEntries = pSyncNode->FpOnAppendEntries; + gSyncIO->FpOnSyncAppendEntriesReply = pSyncNode->FpOnAppendEntriesReply; + gSyncIO->FpOnSyncTimeout = pSyncNode->FpOnTimeout; + gSyncIO->pSyncNode = pSyncNode; + + syncNodeStart(pSyncNode); + + 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); + } +} + +SRpcMsg *step0() { + SRpcMsg *pMsg = (SRpcMsg *)taosMemoryMalloc(sizeof(SRpcMsg)); + memset(pMsg, 0, sizeof(SRpcMsg)); + pMsg->msgType = 9999; + pMsg->contLen = 32; + pMsg->pCont = taosMemoryMalloc(pMsg->contLen); + snprintf((char *)(pMsg->pCont), pMsg->contLen, "hello, world"); + return pMsg; +} + +SyncClientRequest *step1(const SRpcMsg *pMsg) { + SyncClientRequest *pRetMsg = syncClientRequestBuild2(pMsg, 123, true, 1000); + return pRetMsg; +} + +int main(int argc, char **argv) { + // taosInitLog((char *)"syncTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + void logTest(); + + myIndex = 0; + if (argc >= 2) { + snapshotLastApplyIndex = atoi(argv[1]); + } + sTrace("--snapshotLastApplyIndex : %ld \n", snapshotLastApplyIndex); + + int32_t ret = syncIOStart((char *)"127.0.0.1", ports[myIndex]); + assert(ret == 0); + + ret = syncEnvStart(); + assert(ret == 0); + + // taosRemoveDir(pWalDir); + + initFsm(); + + gSyncNode = syncInitTest(); + assert(gSyncNode != NULL); + syncNodeLog2((char *)"", gSyncNode); + + initRaftId(gSyncNode); + + // step0 + SRpcMsg *pMsg0 = step0(); + syncRpcMsgLog2((char *)"==step0==", pMsg0); + + // step1 + SyncClientRequest *pMsg1 = step1(pMsg0); + syncClientRequestLog2((char *)"==step1==", pMsg1); + + for (int i = 0; i < 10; ++i) { + SyncClientRequest *pSyncClientRequest = pMsg1; + SRpcMsg rpcMsg; + syncClientRequest2RpcMsg(pSyncClientRequest, &rpcMsg); + gSyncNode->FpEqMsg(gSyncNode->queue, &rpcMsg); + + taosMsleep(1000); + } + + while (1) { + sTrace("while 1 sleep"); + taosMsleep(1000); + } + + return 0; +} From 9a7924049f566f9c008f1c38cc7af8b75f7787e5 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Tue, 19 Apr 2022 15:43:24 +0800 Subject: [PATCH 03/51] sync integration --- include/libs/sync/sync.h | 3 + source/dnode/mgmt/mgmt_vnode/inc/vmInt.h | 1 + source/dnode/mgmt/mgmt_vnode/src/vmHandle.c | 22 +++ source/dnode/mgmt/mgmt_vnode/src/vmInt.c | 1 + source/dnode/mgmt/mgmt_vnode/src/vmWorker.c | 9 ++ source/dnode/vnode/CMakeLists.txt | 1 + source/dnode/vnode/inc/vnode.h | 2 + source/dnode/vnode/src/inc/vnodeInt.h | 31 ++-- source/dnode/vnode/src/inc/vnodeSync.h | 41 +++++ source/dnode/vnode/src/vnd/vnodeOpen.c | 6 + source/dnode/vnode/src/vnd/vnodeSvr.c | 82 +++++++++- source/dnode/vnode/src/vnd/vnodeSync.c | 162 ++++++++++++++++++++ source/libs/sync/inc/syncRaftLog.h | 3 - source/libs/sync/test/syncSnapshotTest.cpp | 2 +- 14 files changed, 347 insertions(+), 19 deletions(-) create mode 100644 source/dnode/vnode/src/inc/vnodeSync.h create mode 100644 source/dnode/vnode/src/vnd/vnodeSync.c diff --git a/include/libs/sync/sync.h b/include/libs/sync/sync.h index 6bafa29a50..6705846d05 100644 --- a/include/libs/sync/sync.h +++ b/include/libs/sync/sync.h @@ -84,6 +84,9 @@ typedef struct SSyncFSM { struct SSyncRaftEntry; typedef struct SSyncRaftEntry SSyncRaftEntry; +#define SYNC_INDEX_BEGIN 0 +#define SYNC_INDEX_INVALID -1 + // abstract definition of log store in raft // SWal implements it typedef struct SSyncLogStore { diff --git a/source/dnode/mgmt/mgmt_vnode/inc/vmInt.h b/source/dnode/mgmt/mgmt_vnode/inc/vmInt.h index ab8328d038..73cf07690d 100644 --- a/source/dnode/mgmt/mgmt_vnode/inc/vmInt.h +++ b/source/dnode/mgmt/mgmt_vnode/inc/vmInt.h @@ -109,6 +109,7 @@ void vmStopWorker(SVnodesMgmt *pMgmt); int32_t vmAllocQueue(SVnodesMgmt *pMgmt, SVnodeObj *pVnode); void vmFreeQueue(SVnodesMgmt *pMgmt, SVnodeObj *pVnode); +int32_t vmPutMsgToSyncQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc); // sync integration int32_t vmPutMsgToQueryQueue(SMgmtWrapper *pWrapper, SRpcMsg *pMsg); int32_t vmPutMsgToFetchQueue(SMgmtWrapper *pWrapper, SRpcMsg *pMsg); int32_t vmPutMsgToApplyQueue(SMgmtWrapper *pWrapper, SRpcMsg *pMsg); diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c b/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c index 7eb08a8f4f..95ef865312 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c @@ -106,6 +106,16 @@ static void vmGenerateVnodeCfg(SCreateVnodeReq *pCreate, SVnodeCfg *pCfg) { pCfg->hashBegin = pCreate->hashBegin; pCfg->hashEnd = pCreate->hashEnd; pCfg->hashMethod = pCreate->hashMethod; + + // sync integration + pCfg->syncCfg.myIndex = pCreate->selfIndex; + pCfg->syncCfg.replicaNum = pCreate->replica; + memset(&(pCfg->syncCfg.nodeInfo), 0, sizeof(pCfg->syncCfg.nodeInfo)); + for (int i = 0; i < pCreate->replica; ++i) { + (pCfg->syncCfg.nodeInfo)[i].nodePort = (pCreate->replicas)[i].port; + snprintf((pCfg->syncCfg.nodeInfo)[i].nodeFqdn, sizeof((pCfg->syncCfg.nodeInfo)[i].nodeFqdn), "%s", + (pCreate->replicas)[i].fqdn); + } } static void vmGenerateWrapperCfg(SVnodesMgmt *pMgmt, SCreateVnodeReq *pCreate, SWrapperCfg *pCfg) { @@ -157,6 +167,7 @@ int32_t vmProcessCreateVnodeReq(SVnodesMgmt *pMgmt, SNodeMsg *pMsg) { msgCb.queueFps[QUERY_QUEUE] = vmPutMsgToQueryQueue; msgCb.queueFps[FETCH_QUEUE] = vmPutMsgToFetchQueue; msgCb.queueFps[APPLY_QUEUE] = vmPutMsgToApplyQueue; + msgCb.queueFps[SYNC_QUEUE] = vmPutMsgToSyncQueue; // sync integration msgCb.qsizeFp = vmGetQueueSize; SVnode *pImpl = vnodeOpen(path, pMgmt->pTfs, msgCb); @@ -357,4 +368,15 @@ void vmInitMsgHandle(SMgmtWrapper *pWrapper) { dmSetMsgHandle(pWrapper, TDMT_DND_DROP_VNODE, vmProcessMgmtMsg, DEFAULT_HANDLE); dmSetMsgHandle(pWrapper, TDMT_DND_SYNC_VNODE, vmProcessMgmtMsg, DEFAULT_HANDLE); dmSetMsgHandle(pWrapper, TDMT_DND_COMPACT_VNODE, vmProcessMgmtMsg, DEFAULT_HANDLE); + + // sync integration + dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_TIMEOUT, (NodeMsgFp)vmProcessSyncMsg, DEFAULT_HANDLE); + dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_PING, (NodeMsgFp)vmProcessSyncMsg, DEFAULT_HANDLE); + dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_PING_REPLY, (NodeMsgFp)vmProcessSyncMsg, DEFAULT_HANDLE); + dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_CLIENT_REQUEST, (NodeMsgFp)vmProcessSyncMsg, DEFAULT_HANDLE); + dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_CLIENT_REQUEST_REPLY, (NodeMsgFp)vmProcessSyncMsg, DEFAULT_HANDLE); + dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_REQUEST_VOTE, (NodeMsgFp)vmProcessSyncMsg, DEFAULT_HANDLE); + dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_REQUEST_VOTE_REPLY, (NodeMsgFp)vmProcessSyncMsg, DEFAULT_HANDLE); + dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_APPEND_ENTRIES, (NodeMsgFp)vmProcessSyncMsg, DEFAULT_HANDLE); + dmSetMsgHandle(pWrapper, TDMT_VND_SYNC_APPEND_ENTRIES_REPLY, (NodeMsgFp)vmProcessSyncMsg, DEFAULT_HANDLE); } diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c index a212d7edf4..cde73e6fa0 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c @@ -137,6 +137,7 @@ static void *vmOpenVnodeFunc(void *param) { msgCb.queueFps[QUERY_QUEUE] = vmPutMsgToQueryQueue; msgCb.queueFps[FETCH_QUEUE] = vmPutMsgToFetchQueue; msgCb.queueFps[APPLY_QUEUE] = vmPutMsgToApplyQueue; + msgCb.queueFps[SYNC_QUEUE] = vmPutMsgToSyncQueue; // sync integration msgCb.qsizeFp = vmGetQueueSize; snprintf(path, TSDB_FILENAME_LEN, "vnode%svnode%d", TD_DIRSEP, pCfg->vgId); SVnode *pImpl = vnodeOpen(path, pMgmt->pTfs, msgCb); diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c index 2fb29ce944..f660990184 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c @@ -306,6 +306,10 @@ static int32_t vmPutRpcMsgToQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc, EQueueT dTrace("msg:%p, will be put into vnode-merge queue", pMsg); taosWriteQitem(pVnode->pMergeQ, pMsg); break; + case SYNC_QUEUE: // sync integration + dTrace("msg:%p, will be put into vnode-sync queue", pMsg); + taosWriteQitem(pVnode->pSyncQ, pMsg); + break; default: code = -1; terrno = TSDB_CODE_INVALID_PARA; @@ -332,6 +336,11 @@ int32_t vmPutMsgToMergeQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) { return vmPutRpcMsgToQueue(pWrapper, pRpc, MERGE_QUEUE); } +// sync integration +int32_t vmPutMsgToSyncQueue(SMgmtWrapper *pWrapper, SRpcMsg *pRpc) { + return vmPutRpcMsgToQueue(pWrapper, pRpc, SYNC_QUEUE); +} + int32_t vmGetQueueSize(SMgmtWrapper *pWrapper, int32_t vgId, EQueueType qtype) { int32_t size = -1; SVnodeObj *pVnode = vmAcquireVnode(pWrapper->pMgmt, vgId); diff --git a/source/dnode/vnode/CMakeLists.txt b/source/dnode/vnode/CMakeLists.txt index 5a6fdd1b40..727d6d72ae 100644 --- a/source/dnode/vnode/CMakeLists.txt +++ b/source/dnode/vnode/CMakeLists.txt @@ -16,6 +16,7 @@ target_sources( "src/vnd/vnodeWrite.c" "src/vnd/vnodeModule.c" "src/vnd/vnodeSvr.c" + "src/vnd/vnodeSync.c" # meta "src/meta/metaOpen.c" diff --git a/source/dnode/vnode/inc/vnode.h b/source/dnode/vnode/inc/vnode.h index 86e876ecba..a20b30a47b 100644 --- a/source/dnode/vnode/inc/vnode.h +++ b/source/dnode/vnode/inc/vnode.h @@ -24,6 +24,7 @@ #include "tarray.h" #include "tfs.h" #include "wal.h" +#include "sync.h" #include "tcommon.h" #include "tfs.h" @@ -144,6 +145,7 @@ struct SVnodeCfg { bool isWeak; STsdbCfg tsdbCfg; SWalCfg walCfg; + SSyncCfg syncCfg; // sync integration uint32_t hashBegin; uint32_t hashEnd; int8_t hashMethod; diff --git a/source/dnode/vnode/src/inc/vnodeInt.h b/source/dnode/vnode/src/inc/vnodeInt.h index 0ff1408c91..55b1473e4e 100644 --- a/source/dnode/vnode/src/inc/vnodeInt.h +++ b/source/dnode/vnode/src/inc/vnodeInt.h @@ -54,8 +54,8 @@ typedef struct SQWorkerMgmt SQHandle; #define VNODE_META_DIR "meta" #define VNODE_TSDB_DIR "tsdb" -#define VNODE_TQ_DIR "tq" -#define VNODE_WAL_DIR "wal" +#define VNODE_TQ_DIR "tq" +#define VNODE_WAL_DIR "wal" typedef struct { int8_t streamType; // sma or other @@ -68,8 +68,8 @@ typedef struct { } SStreamSinkInfo; typedef struct { - SVnode* pVnode; - SHashObj* pHash; // streamId -> SStreamSinkInfo + SVnode *pVnode; + SHashObj *pHash; // streamId -> SStreamSinkInfo } SSink; // SVState @@ -85,25 +85,26 @@ struct SVnodeInfo { }; struct SVnode { - char* path; + char *path; SVnodeCfg config; SVState state; - STfs* pTfs; + STfs *pTfs; SMsgCb msgCb; - SVBufPool* pBufPool; - SMeta* pMeta; - STsdb* pTsdb; - SWal* pWal; - STQ* pTq; - SSink* pSink; + SVBufPool *pBufPool; + SMeta *pMeta; + STsdb *pTsdb; + SWal *pWal; + STQ *pTq; + SSink *pSink; + int64_t sync; // sync integration tsem_t canCommit; - SQHandle* pQuery; + SQHandle *pQuery; }; #define TD_VID(PVNODE) (PVNODE)->config.vgId // sma -void smaHandleRes(void* pVnode, int64_t smaId, const SArray* data); +void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data); #include "vnd.h" @@ -113,6 +114,8 @@ void smaHandleRes(void* pVnode, int64_t smaId, const SArray* data); #include "tq.h" +#include "vnodeSync.h" + #ifdef __cplusplus } #endif diff --git a/source/dnode/vnode/src/inc/vnodeSync.h b/source/dnode/vnode/src/inc/vnodeSync.h new file mode 100644 index 0000000000..a3612bec92 --- /dev/null +++ b/source/dnode/vnode/src/inc/vnodeSync.h @@ -0,0 +1,41 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef _TD_VNODE_SYNC_H_ +#define _TD_VNODE_SYNC_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t vnodeSyncOpen(SVnode *pVnode); +int32_t vnodeSyncStart(SVnode *pVnode); +void vnodeSyncClose(SVnode *pVnode); + +int32_t vnodeSyncEqMsg(void *qHandle, SRpcMsg *pMsg); +int32_t vnodeSendMsg(void *rpcHandle, const SEpSet *pEpSet, SRpcMsg *pMsg); + +void vnodeSyncCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta); +void vnodeSyncPreCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta); +void vnodeSyncRollBackCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta); +int32_t vnodeSyncGetSnapshotCb(struct SSyncFSM *pFsm, SSnapshot *pSnapshot); + +SSyncFSM *syncVnodeMakeFsm(); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_VNODE_SYNC_H_*/ diff --git a/source/dnode/vnode/src/vnd/vnodeOpen.c b/source/dnode/vnode/src/vnd/vnodeOpen.c index 9e4aa714e2..b61bb48db3 100644 --- a/source/dnode/vnode/src/vnd/vnodeOpen.c +++ b/source/dnode/vnode/src/vnd/vnodeOpen.c @@ -124,6 +124,12 @@ SVnode *vnodeOpen(const char *path, STfs *pTfs, SMsgCb msgCb) { goto _err; } + // sync integration + // open sync + if (vnodeSyncOpen(pVnode)) { + goto _err; + } + #if 0 if (vnodeBegin() < 0) { goto _err; diff --git a/source/dnode/vnode/src/vnd/vnodeSvr.c b/source/dnode/vnode/src/vnd/vnodeSvr.c index 288241eb66..274ce5ec3e 100644 --- a/source/dnode/vnode/src/vnd/vnodeSvr.c +++ b/source/dnode/vnode/src/vnd/vnodeSvr.c @@ -13,6 +13,8 @@ * along with this program. If not, see . */ +#include "sync.h" +#include "syncTools.h" #include "vnodeInt.h" static int vnodeProcessCreateStbReq(SVnode *pVnode, void *pReq); @@ -198,8 +200,86 @@ void smaHandleRes(void *pVnode, int64_t smaId, const SArray *data) { tsdbInsertTSmaData(((SVnode *)pVnode)->pTsdb, smaId, (const char *)data); } +// sync integration int vnodeProcessSyncReq(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) { - /*vInfo("sync message is processed");*/ + SSyncNode *pSyncNode = syncNodeAcquire(pVnode->sync); + assert(pSyncNode != NULL); + + ESyncState state = syncGetMyRole(pVnode->sync); + SyncTerm currentTerm = syncGetMyTerm(pVnode->sync); + + SMsgHead *pHead = pMsg->pCont; + + char logBuf[512]; + char *syncNodeStr = sync2SimpleStr(pVnode->sync); + snprintf(logBuf, sizeof(logBuf), "==vnodeProcessSyncReq== msgType:%d, syncNode: %s", pMsg->msgType, syncNodeStr); + syncRpcMsgLog2(logBuf, pMsg); + taosMemoryFree(syncNodeStr); + + SRpcMsg *pRpcMsg = pMsg; + + if (pRpcMsg->msgType == TDMT_VND_SYNC_TIMEOUT) { + SyncTimeout *pSyncMsg = syncTimeoutFromRpcMsg2(pRpcMsg); + assert(pSyncMsg != NULL); + + syncNodeOnTimeoutCb(pSyncNode, pSyncMsg); + syncTimeoutDestroy(pSyncMsg); + + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_PING) { + SyncPing *pSyncMsg = syncPingFromRpcMsg2(pRpcMsg); + assert(pSyncMsg != NULL); + + syncNodeOnPingCb(pSyncNode, pSyncMsg); + syncPingDestroy(pSyncMsg); + + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_PING_REPLY) { + SyncPingReply *pSyncMsg = syncPingReplyFromRpcMsg2(pRpcMsg); + assert(pSyncMsg != NULL); + + syncNodeOnPingReplyCb(pSyncNode, pSyncMsg); + syncPingReplyDestroy(pSyncMsg); + + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_CLIENT_REQUEST) { + SyncClientRequest *pSyncMsg = syncClientRequestFromRpcMsg2(pRpcMsg); + assert(pSyncMsg != NULL); + + syncNodeOnClientRequestCb(pSyncNode, pSyncMsg); + syncClientRequestDestroy(pSyncMsg); + + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_REQUEST_VOTE) { + SyncRequestVote *pSyncMsg = syncRequestVoteFromRpcMsg2(pRpcMsg); + assert(pSyncMsg != NULL); + + syncNodeOnRequestVoteCb(pSyncNode, pSyncMsg); + syncRequestVoteDestroy(pSyncMsg); + + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_REQUEST_VOTE_REPLY) { + SyncRequestVoteReply *pSyncMsg = syncRequestVoteReplyFromRpcMsg2(pRpcMsg); + assert(pSyncMsg != NULL); + + syncNodeOnRequestVoteReplyCb(pSyncNode, pSyncMsg); + syncRequestVoteReplyDestroy(pSyncMsg); + + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_APPEND_ENTRIES) { + SyncAppendEntries *pSyncMsg = syncAppendEntriesFromRpcMsg2(pRpcMsg); + assert(pSyncMsg != NULL); + + syncNodeOnAppendEntriesCb(pSyncNode, pSyncMsg); + syncAppendEntriesDestroy(pSyncMsg); + + } else if (pRpcMsg->msgType == TDMT_VND_SYNC_APPEND_ENTRIES_REPLY) { + SyncAppendEntriesReply *pSyncMsg = syncAppendEntriesReplyFromRpcMsg2(pRpcMsg); + assert(pSyncMsg != NULL); + + syncNodeOnAppendEntriesReplyCb(pSyncNode, pSyncMsg); + syncAppendEntriesReplyDestroy(pSyncMsg); + + } else { + vError("==vnodeProcessSyncReq== error msg type:%d", pRpcMsg->msgType); + } + + syncNodeRelease(pSyncNode); + return 0; } diff --git a/source/dnode/vnode/src/vnd/vnodeSync.c b/source/dnode/vnode/src/vnd/vnodeSync.c new file mode 100644 index 0000000000..bf1ff736a3 --- /dev/null +++ b/source/dnode/vnode/src/vnd/vnodeSync.c @@ -0,0 +1,162 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#include "sync.h" +#include "syncTools.h" +#include "tmsgcb.h" +#include "vnodeInt.h" + +// sync integration + +int32_t vnodeSyncOpen(SVnode *pVnode) { + SSyncInfo syncInfo; + syncInfo.vgId = pVnode->config.vgId; + SSyncCfg *pCfg = &(syncInfo.syncCfg); + pCfg->replicaNum = pVnode->config.syncCfg.replicaNum; + pCfg->myIndex = pVnode->config.syncCfg.myIndex; + memcpy(pCfg->nodeInfo, pVnode->config.syncCfg.nodeInfo, sizeof(pCfg->nodeInfo)); + + snprintf(syncInfo.path, sizeof(syncInfo.path), "%s/sync", pVnode->path); + syncInfo.pWal = pVnode->pWal; + + syncInfo.pFsm = syncVnodeMakeFsm(pVnode); + syncInfo.rpcClient = NULL; + syncInfo.FpSendMsg = vnodeSendMsg; + syncInfo.queue = NULL; + syncInfo.FpEqMsg = vnodeSyncEqMsg; + + pVnode->sync = syncOpen(&syncInfo); + assert(pVnode->sync > 0); + + // for test + setPingTimerMS(pVnode->sync, 3000); + setElectTimerMS(pVnode->sync, 500); + setHeartbeatTimerMS(pVnode->sync, 100); + + return 0; +} + +int32_t vnodeSyncStart(SVnode *pVnode) { + syncStart(pVnode->sync); + return 0; +} + +void vnodeSyncClose(SVnode *pVnode) { + // stop by ref id + syncStop(pVnode->sync); +} + +void vnodeSyncSetQ(SVnode *pVnode, void *qHandle) { syncSetQ(pVnode->sync, (void *)(&(pVnode->msgCb))); } + +void vnodeSyncSetRpc(SVnode *pVnode, void *rpcHandle) { syncSetRpc(pVnode->sync, (void *)(&(pVnode->msgCb))); } + +int32_t vnodeSyncEqMsg(void *qHandle, SRpcMsg *pMsg) { + int32_t ret = 0; + SMsgCb *pMsgCb = qHandle; + if (pMsgCb->queueFps[SYNC_QUEUE] != NULL) { + tmsgPutToQueue(qHandle, SYNC_QUEUE, pMsg); + } else { + vError("vnodeSyncEqMsg queue is NULL, SYNC_QUEUE:%d", SYNC_QUEUE); + } + return ret; +} + +int32_t vnodeSendMsg(void *rpcHandle, const SEpSet *pEpSet, SRpcMsg *pMsg) { + int32_t ret = 0; + SMsgCb *pMsgCb = rpcHandle; + if (pMsgCb->queueFps[SYNC_QUEUE] != NULL) { + tmsgSendReq(rpcHandle, pEpSet, pMsg); + } else { + vError("vnodeSendMsg queue is NULL, SYNC_QUEUE:%d", SYNC_QUEUE); + } + return ret; +} + +int32_t vnodeSyncGetSnapshotCb(struct SSyncFSM *pFsm, SSnapshot *pSnapshot) { + pSnapshot->data = NULL; + pSnapshot->lastApplyIndex = 0; + pSnapshot->lastApplyTerm = 0; + return 0; +} + +void vnodeSyncCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { + SyncIndex beginIndex = SYNC_INDEX_INVALID; + if (pFsm->FpGetSnapshot != NULL) { + SSnapshot snapshot; + pFsm->FpGetSnapshot(pFsm, &snapshot); + beginIndex = snapshot.lastApplyIndex; + } + + if (cbMeta.index > beginIndex) { + char logBuf[256]; + snprintf( + logBuf, sizeof(logBuf), + "==callback== ==CommitCb== execute, pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s, beginIndex :%ld\n", + pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state), beginIndex); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); + + SRpcMsg applyMsg; + applyMsg = *pMsg; + applyMsg.pCont = rpcMallocCont(applyMsg.contLen); + assert(applyMsg.contLen == pMsg->contLen); + memcpy(applyMsg.pCont, pMsg->pCont, applyMsg.contLen); + + // recover handle for response + SVnode *pVnode = (SVnode *)(pFsm->data); + SRpcMsg saveRpcMsg; + int32_t ret = syncGetAndDelRespRpc(pVnode->sync, cbMeta.seqNum, &saveRpcMsg); + if (ret == 1) { + applyMsg.handle = saveRpcMsg.handle; + applyMsg.ahandle = saveRpcMsg.ahandle; + } + + // put to applyQ + tmsgPutToQueue(&(pVnode->msgCb), APPLY_QUEUE, &applyMsg); + + } else { + char logBuf[256]; + snprintf(logBuf, sizeof(logBuf), + "==callback== ==CommitCb== do not execute, pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s, " + "beginIndex :%ld\n", + pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state), + beginIndex); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); + } +} + +void vnodeSyncPreCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { + char logBuf[256]; + snprintf(logBuf, sizeof(logBuf), + "==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, cbMeta.index, + cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); +} + +void vnodeSyncRollBackCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { + char logBuf[256]; + snprintf(logBuf, sizeof(logBuf), "==callback== ==RollBackCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", + pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); +} + +SSyncFSM *syncVnodeMakeFsm(SVnode *pVnode) { + SSyncFSM *pFsm = (SSyncFSM *)taosMemoryMalloc(sizeof(SSyncFSM)); + pFsm->data = pVnode; + pFsm->FpCommitCb = vnodeSyncCommitCb; + pFsm->FpPreCommitCb = vnodeSyncPreCommitCb; + pFsm->FpRollBackCb = vnodeSyncRollBackCb; + pFsm->FpGetSnapshot = vnodeSyncGetSnapshotCb; + return pFsm; +} diff --git a/source/libs/sync/inc/syncRaftLog.h b/source/libs/sync/inc/syncRaftLog.h index 2196d6c207..7db62e14d5 100644 --- a/source/libs/sync/inc/syncRaftLog.h +++ b/source/libs/sync/inc/syncRaftLog.h @@ -27,9 +27,6 @@ extern "C" { #include "syncRaftEntry.h" #include "taosdef.h" -#define SYNC_INDEX_BEGIN 0 -#define SYNC_INDEX_INVALID -1 - typedef struct SSyncLogStoreData { SSyncNode* pSyncNode; SWal* pWal; diff --git a/source/libs/sync/test/syncSnapshotTest.cpp b/source/libs/sync/test/syncSnapshotTest.cpp index e55f37d2d6..7ad14e0f5e 100644 --- a/source/libs/sync/test/syncSnapshotTest.cpp +++ b/source/libs/sync/test/syncSnapshotTest.cpp @@ -112,7 +112,7 @@ SSyncNode *syncNodeInit() { 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); + taosGetFqdn(pCfg->nodeInfo[0].nodeFqdn); } SSyncNode *pSyncNode = syncNodeOpen(&syncInfo); From 785f25c49d96ad44611398de5d2a81b07f08bfd5 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Tue, 19 Apr 2022 16:16:02 +0800 Subject: [PATCH 04/51] sync integration --- source/dnode/mgmt/mgmt_vnode/src/vmWorker.c | 33 +++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c index f660990184..8bd334b018 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c @@ -117,6 +117,36 @@ static void vmProcessWriteQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO vnodePreprocessWriteReqs(pVnode->pImpl, pArray); + // sync integration response + /* + for (int i = 0; i < taosArrayGetSize(pArray); i++) { + SNodeMsg *pMsg; + SRpcMsg *pRpc; + + pMsg = *(SNodeMsg **)taosArrayGet(pArray, i); + pRpc = &pMsg->rpcMsg; + + // set request version + void *pBuf = POINTER_SHIFT(pRpc->pCont, sizeof(SMsgHead)); + // int64_t ver = pVnode->pImpl->state.processed++; // ??????? + int64_t ver; + taosEncodeFixedI64(&pBuf, ver); + + int32_t ret = syncPropose(pVnode->pImpl->sync, pRpc, false); + if (ret == TAOS_SYNC_PROPOSE_NOT_LEADER) { + // not leader + // send response + } else if (ret == TAOS_SYNC_PROPOSE_OTHER_ERROR) { + // send response + } else if (ret == TAOS_SYNC_PROPOSE_SUCCESS) { + // ok + // send response in applyQ + } else { + assert(0); + } + } + */ + numOfMsgs = taosArrayGetSize(pArray); for (int32_t i = 0; i < numOfMsgs; i++) { SNodeMsg *pMsg = *(SNodeMsg **)taosArrayGet(pArray, i); @@ -154,6 +184,9 @@ static void vmProcessApplyQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO // todo SRpcMsg *pRsp = NULL; (void)vnodeProcessWriteReq(pVnode->pImpl, &pMsg->rpcMsg, &pRsp); + + // sync integration response + // send response } } From 12294b0100d6c2afdbd094fcee021e840ab66b32 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Tue, 19 Apr 2022 16:16:59 +0800 Subject: [PATCH 05/51] sync integration --- source/dnode/mgmt/mgmt_vnode/src/vmWorker.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c index 8bd334b018..40a29b5f59 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c @@ -186,7 +186,7 @@ static void vmProcessApplyQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO (void)vnodeProcessWriteReq(pVnode->pImpl, &pMsg->rpcMsg, &pRsp); // sync integration response - // send response + // if leader send response } } From 4fbcb4929aae3203f36bf9369286007b076b3023 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Tue, 19 Apr 2022 16:24:05 +0800 Subject: [PATCH 06/51] sync integration --- source/dnode/mgmt/mgmt_vnode/src/vmWorker.c | 5 ++++- source/dnode/vnode/src/vnd/vnodeSync.c | 5 ++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c index 40a29b5f59..ef05278129 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c @@ -186,7 +186,10 @@ static void vmProcessApplyQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO (void)vnodeProcessWriteReq(pVnode->pImpl, &pMsg->rpcMsg, &pRsp); // sync integration response - // if leader send response + // leader + // if (pMsg->rpcMsg.handle != NULL && pMsg->rpcMsg.ahandle !-NULL) { + // send response; + //} } } diff --git a/source/dnode/vnode/src/vnd/vnodeSync.c b/source/dnode/vnode/src/vnd/vnodeSync.c index bf1ff736a3..e0e421b95c 100644 --- a/source/dnode/vnode/src/vnd/vnodeSync.c +++ b/source/dnode/vnode/src/vnd/vnodeSync.c @@ -117,9 +117,12 @@ void vnodeSyncCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cb SVnode *pVnode = (SVnode *)(pFsm->data); SRpcMsg saveRpcMsg; int32_t ret = syncGetAndDelRespRpc(pVnode->sync, cbMeta.seqNum, &saveRpcMsg); - if (ret == 1) { + if (ret == 1 && cbMeta.state == TAOS_SYNC_STATE_LEADER) { applyMsg.handle = saveRpcMsg.handle; applyMsg.ahandle = saveRpcMsg.ahandle; + } else { + applyMsg.handle = NULL; + applyMsg.ahandle = NULL; } // put to applyQ From 6f491b70b1e772c11af855c4077596cdf435a7ac Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Tue, 19 Apr 2022 16:31:18 +0800 Subject: [PATCH 07/51] sync integration --- source/dnode/mgmt/mgmt_vnode/src/vmWorker.c | 34 ++++++++++----------- 1 file changed, 17 insertions(+), 17 deletions(-) diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c index ef05278129..afc06bb1e7 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c @@ -117,6 +117,23 @@ static void vmProcessWriteQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO vnodePreprocessWriteReqs(pVnode->pImpl, pArray); + numOfMsgs = taosArrayGetSize(pArray); + for (int32_t i = 0; i < numOfMsgs; i++) { + SNodeMsg *pMsg = *(SNodeMsg **)taosArrayGet(pArray, i); + SRpcMsg *pRpc = &pMsg->rpcMsg; + SRpcMsg *pRsp = NULL; + + int32_t code = vnodeProcessWriteReq(pVnode->pImpl, pRpc, &pRsp); + if (pRsp != NULL) { + pRsp->ahandle = pRpc->ahandle; + tmsgSendRsp(pRsp); + taosMemoryFree(pRsp); + } else { + if (code != 0 && terrno != 0) code = terrno; + vmSendRsp(pVnode->pWrapper, pMsg, code); + } + } + // sync integration response /* for (int i = 0; i < taosArrayGetSize(pArray); i++) { @@ -147,23 +164,6 @@ static void vmProcessWriteQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO } */ - numOfMsgs = taosArrayGetSize(pArray); - for (int32_t i = 0; i < numOfMsgs; i++) { - SNodeMsg *pMsg = *(SNodeMsg **)taosArrayGet(pArray, i); - SRpcMsg *pRpc = &pMsg->rpcMsg; - SRpcMsg *pRsp = NULL; - - int32_t code = vnodeProcessWriteReq(pVnode->pImpl, pRpc, &pRsp); - if (pRsp != NULL) { - pRsp->ahandle = pRpc->ahandle; - tmsgSendRsp(pRsp); - taosMemoryFree(pRsp); - } else { - if (code != 0 && terrno != 0) code = terrno; - vmSendRsp(pVnode->pWrapper, pMsg, code); - } - } - for (int32_t i = 0; i < numOfMsgs; i++) { SNodeMsg *pMsg = *(SNodeMsg **)taosArrayGet(pArray, i); dTrace("msg:%p, is freed", pMsg); From 167657c1926a1959327887657c3d6d48be30029a Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 19 Apr 2022 09:07:42 +0000 Subject: [PATCH 08/51] refactor: sync --- source/dnode/mgmt/mgmt_vnode/src/vmWorker.c | 57 +++++++++++---------- source/dnode/vnode/inc/vnode.h | 6 ++- source/dnode/vnode/src/inc/vnodeInt.h | 4 +- source/dnode/vnode/src/vnd/vnodeOpen.c | 2 + 4 files changed, 38 insertions(+), 31 deletions(-) diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c index 754a174b6b..17347eb59a 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c @@ -97,7 +97,7 @@ static void vmProcessFetchQueue(SQueueInfo *pInfo, SNodeMsg *pMsg) { static void vmProcessWriteQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) { SVnodeObj *pVnode = pInfo->ahandle; - int64_t version; + SRpcMsg rsp; SArray *pArray = taosArrayInit(numOfMsgs, sizeof(SNodeMsg *)); if (pArray == NULL) { @@ -116,13 +116,15 @@ static void vmProcessWriteQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO } } +#if 1 + int64_t version; + vnodePreprocessWriteReqs(pVnode->pImpl, pArray, &version); numOfMsgs = taosArrayGetSize(pArray); for (int32_t i = 0; i < numOfMsgs; i++) { SNodeMsg *pMsg = *(SNodeMsg **)taosArrayGet(pArray, i); SRpcMsg *pRpc = &pMsg->rpcMsg; - SRpcMsg rsp; rsp.pCont = NULL; rsp.contLen = 0; @@ -132,20 +134,9 @@ static void vmProcessWriteQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO int32_t code = vnodeProcessWriteReq(pVnode->pImpl, pRpc, version++, &rsp); tmsgSendRsp(&rsp); - -#if 0 - if (pRsp != NULL) { - pRsp->ahandle = pRpc->ahandle; - taosMemoryFree(pRsp); - } else { - if (code != 0 && terrno != 0) code = terrno; - vmSendRsp(pVnode->pWrapper, pMsg, code); - } -#endif } - +#else // sync integration response - /* for (int i = 0; i < taosArrayGetSize(pArray); i++) { SNodeMsg *pMsg; SRpcMsg *pRpc; @@ -153,18 +144,18 @@ static void vmProcessWriteQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO pMsg = *(SNodeMsg **)taosArrayGet(pArray, i); pRpc = &pMsg->rpcMsg; - // set request version - void *pBuf = POINTER_SHIFT(pRpc->pCont, sizeof(SMsgHead)); - // int64_t ver = pVnode->pImpl->state.processed++; // ??????? - int64_t ver; - taosEncodeFixedI64(&pBuf, ver); + rsp.ahandle = pRpc->ahandle; + rsp.handle = pRpc->handle; + rsp.pCont = NULL; + rsp.contLen = 0; - int32_t ret = syncPropose(pVnode->pImpl->sync, pRpc, false); + int32_t ret = syncPropose(vnodeGetSyncHandle(pVnode->pImpl), pRpc, false); if (ret == TAOS_SYNC_PROPOSE_NOT_LEADER) { - // not leader - // send response + rsp.code = -1; + tmsgSendRsp(&rsp); } else if (ret == TAOS_SYNC_PROPOSE_OTHER_ERROR) { - // send response + rsp.code = -2; + tmsgSendRsp(&rsp); } else if (ret == TAOS_SYNC_PROPOSE_SUCCESS) { // ok // send response in applyQ @@ -172,7 +163,7 @@ static void vmProcessWriteQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO assert(0); } } - */ +#endif for (int32_t i = 0; i < numOfMsgs; i++) { SNodeMsg *pMsg = *(SNodeMsg **)taosArrayGet(pArray, i); @@ -187,13 +178,25 @@ static void vmProcessWriteQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO static void vmProcessApplyQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) { SVnodeObj *pVnode = pInfo->ahandle; SNodeMsg *pMsg = NULL; + SRpcMsg rsp; for (int32_t i = 0; i < numOfMsgs; ++i) { +#if 0 taosGetQitem(qall, (void **)&pMsg); - // todo - SRpcMsg *pRsp = NULL; - // (void)vnodeProcessWriteReq(pVnode->pImpl, &pMsg->rpcMsg, &pRsp); + if (pMsg->rpcMsg.handle != NULL && pMsg->rpcMsg.ahandle != NULL) { + rsp.ahandle = pMsg->rpcMsg.ahandle; + rsp.handle = pMsg->rpcMsg.handle; + rsp.code = 0; + rsp.pCont = NULL; + rsp.contLen = 0; + + if (vnodeProcessWriteReq(pVnode->pImpl, &pMsg->rpcMsg, &rsp) < 0) { + rsp.code = terrno; + tmsgSendRsp(&rsp); + } + } +#endif } } diff --git a/source/dnode/vnode/inc/vnode.h b/source/dnode/vnode/inc/vnode.h index 4e42927be0..ff9f87d65e 100644 --- a/source/dnode/vnode/inc/vnode.h +++ b/source/dnode/vnode/inc/vnode.h @@ -21,10 +21,10 @@ #include "tqueue.h" #include "trpc.h" +#include "sync.h" #include "tarray.h" #include "tfs.h" #include "wal.h" -#include "sync.h" #include "tcommon.h" #include "tfs.h" @@ -61,6 +61,8 @@ int32_t vnodeSync(SVnode *pVnode); int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad); int vnodeValidateTableHash(SVnodeCfg *pVnodeOptions, char *tableFName); +int64_t vnodeGetSyncHandle(SVnode *pVnode); + // meta typedef struct SMeta SMeta; // todo: remove typedef struct SMTbCursor SMTbCursor; @@ -148,7 +150,7 @@ struct SVnodeCfg { bool isWeak; STsdbCfg tsdbCfg; SWalCfg walCfg; - SSyncCfg syncCfg; // sync integration + SSyncCfg syncCfg; // sync integration uint32_t hashBegin; uint32_t hashEnd; int8_t hashMethod; diff --git a/source/dnode/vnode/src/inc/vnodeInt.h b/source/dnode/vnode/src/inc/vnodeInt.h index 55b1473e4e..06ec03741c 100644 --- a/source/dnode/vnode/src/inc/vnodeInt.h +++ b/source/dnode/vnode/src/inc/vnodeInt.h @@ -54,8 +54,8 @@ typedef struct SQWorkerMgmt SQHandle; #define VNODE_META_DIR "meta" #define VNODE_TSDB_DIR "tsdb" -#define VNODE_TQ_DIR "tq" -#define VNODE_WAL_DIR "wal" +#define VNODE_TQ_DIR "tq" +#define VNODE_WAL_DIR "wal" typedef struct { int8_t streamType; // sma or other diff --git a/source/dnode/vnode/src/vnd/vnodeOpen.c b/source/dnode/vnode/src/vnd/vnodeOpen.c index b61bb48db3..ae5c99ff03 100644 --- a/source/dnode/vnode/src/vnd/vnodeOpen.c +++ b/source/dnode/vnode/src/vnd/vnodeOpen.c @@ -165,3 +165,5 @@ void vnodeClose(SVnode *pVnode) { taosMemoryFree(pVnode); } } + +int64_t vnodeGetSyncHandle(SVnode *pVnode) { return pVnode->sync; } \ No newline at end of file From a63559cfef3d196fcd70885efe9517c12cb2a14b Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Tue, 19 Apr 2022 09:11:40 +0000 Subject: [PATCH 09/51] refactor: sync --- include/libs/transport/trpc.h | 29 +++++++++++---------- source/dnode/mgmt/mgmt_vnode/src/vmWorker.c | 8 +++--- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/include/libs/transport/trpc.h b/include/libs/transport/trpc.h index 3e2f596784..614f358ac2 100644 --- a/include/libs/transport/trpc.h +++ b/include/libs/transport/trpc.h @@ -38,11 +38,11 @@ typedef struct SRpcConnInfo { typedef struct SRpcMsg { tmsg_t msgType; - void * pCont; + void *pCont; int contLen; int32_t code; - void * handle; // rpc handle returned to app - void * ahandle; // app handle set by client + void *handle; // rpc handle returned to app + void *ahandle; // app handle set by client int noResp; // has response or not(default 0, 0: resp, 1: no resp); int persistHandle; // persist handle or not @@ -63,7 +63,7 @@ typedef int (*RpcAfp)(void *parent, char *tableId, char *spi, char *encrypt, cha typedef struct SRpcInit { uint16_t localPort; // local port - char * label; // for debug purpose + char *label; // for debug purpose int numOfThreads; // number of threads to handle connections int sessions; // number of sessions allowed int8_t connType; // TAOS_CONN_UDP, TAOS_CONN_TCPC, TAOS_CONN_TCPS @@ -80,36 +80,37 @@ typedef struct SRpcInit { RpcCfp cfp; // call back to retrieve the client auth info, for server app only - RpcAfp afp;; + RpcAfp afp; + ; void *parent; } SRpcInit; typedef struct { - void *val; + void *val; int32_t (*clone)(void *src, void **dst); - void (*freeFunc)(const void *arg); + void (*freeFunc)(const void *arg); } SRpcCtxVal; typedef struct { - int32_t msgType; - void *val; + int32_t msgType; + void *val; int32_t (*clone)(void *src, void **dst); - void (*freeFunc)(const void *arg); + void (*freeFunc)(const void *arg); } SRpcBrokenlinkVal; typedef struct { - SHashObj * args; + SHashObj *args; SRpcBrokenlinkVal brokenVal; } SRpcCtx; int32_t rpcInit(); void rpcCleanup(); -void * rpcOpen(const SRpcInit *pRpc); +void *rpcOpen(const SRpcInit *pRpc); void rpcClose(void *); -void * rpcMallocCont(int contLen); +void *rpcMallocCont(int contLen); void rpcFreeCont(void *pCont); -void * rpcReallocCont(void *ptr, int contLen); +void *rpcReallocCont(void *ptr, int contLen); // Because taosd supports multi-process mode // These functions should not be used on the server side diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c index 17347eb59a..d15f97b042 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c @@ -116,7 +116,7 @@ static void vmProcessWriteQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO } } -#if 1 +#if 0 int64_t version; vnodePreprocessWriteReqs(pVnode->pImpl, pArray, &version); @@ -180,8 +180,10 @@ static void vmProcessApplyQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO SNodeMsg *pMsg = NULL; SRpcMsg rsp; + static int64_t version = 0; + for (int32_t i = 0; i < numOfMsgs; ++i) { -#if 0 +#if 1 taosGetQitem(qall, (void **)&pMsg); if (pMsg->rpcMsg.handle != NULL && pMsg->rpcMsg.ahandle != NULL) { @@ -191,7 +193,7 @@ static void vmProcessApplyQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO rsp.pCont = NULL; rsp.contLen = 0; - if (vnodeProcessWriteReq(pVnode->pImpl, &pMsg->rpcMsg, &rsp) < 0) { + if (vnodeProcessWriteReq(pVnode->pImpl, &pMsg->rpcMsg, version++, &rsp) < 0) { rsp.code = terrno; tmsgSendRsp(&rsp); } From f7fb9b023f46a319323cb003d95006458994e989 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Tue, 19 Apr 2022 20:14:55 +0800 Subject: [PATCH 10/51] sync integration --- source/dnode/mgmt/mgmt_vnode/src/vmInt.c | 12 ++++++++++++ source/dnode/vnode/inc/vnode.h | 5 +++++ source/dnode/vnode/src/inc/vnodeSync.h | 3 +++ 3 files changed, 20 insertions(+) diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c index cde73e6fa0..48bae163d9 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c @@ -73,6 +73,12 @@ int32_t vmOpenVnode(SVnodesMgmt *pMgmt, SWrapperCfg *pCfg, SVnode *pImpl) { return -1; } + // sync integration + vnodeSyncSetQ(pImpl, NULL); + vnodeSyncSetRpc(pImpl, NULL); + int32_t ret = vnodeSyncStart(pImpl); + assert(ret == 0); + taosWLockLatch(&pMgmt->latch); int32_t code = taosHashPut(pMgmt->hash, &pVnode->vgId, sizeof(int32_t), &pVnode, sizeof(SVnodeObj *)); taosWUnLockLatch(&pMgmt->latch); @@ -305,6 +311,12 @@ static int32_t vmInit(SMgmtWrapper *pWrapper) { goto _OVER; } + // sync integration + if (syncInit() != 0) { + dError("failed to open sync since %s", terrstr()); + return -1; + } + if (vnodeInit(tsNumOfCommitThreads) != 0) { dError("failed to init vnode since %s", terrstr()); goto _OVER; diff --git a/source/dnode/vnode/inc/vnode.h b/source/dnode/vnode/inc/vnode.h index ff9f87d65e..00bcec7be9 100644 --- a/source/dnode/vnode/inc/vnode.h +++ b/source/dnode/vnode/inc/vnode.h @@ -170,6 +170,11 @@ typedef struct { uint64_t uid; } STableKeyInfo; +// sync integration +void vnodeSyncSetQ(SVnode *pVnode, void *qHandle); +void vnodeSyncSetRpc(SVnode *pVnode, void *rpcHandle); +int32_t vnodeSyncStart(SVnode *pVnode); + #ifdef __cplusplus } #endif diff --git a/source/dnode/vnode/src/inc/vnodeSync.h b/source/dnode/vnode/src/inc/vnodeSync.h index a3612bec92..6bbdc4ae72 100644 --- a/source/dnode/vnode/src/inc/vnodeSync.h +++ b/source/dnode/vnode/src/inc/vnodeSync.h @@ -24,6 +24,9 @@ int32_t vnodeSyncOpen(SVnode *pVnode); int32_t vnodeSyncStart(SVnode *pVnode); void vnodeSyncClose(SVnode *pVnode); +void vnodeSyncSetQ(SVnode *pVnode, void *qHandle); +void vnodeSyncSetRpc(SVnode *pVnode, void *rpcHandle); + int32_t vnodeSyncEqMsg(void *qHandle, SRpcMsg *pMsg); int32_t vnodeSendMsg(void *rpcHandle, const SEpSet *pEpSet, SRpcMsg *pMsg); From e0332e9c124282dd5f4f1836fe7af5c2a3523701 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Tue, 19 Apr 2022 22:04:02 +0800 Subject: [PATCH 11/51] sync integration --- source/dnode/vnode/src/inc/vnodeSync.h | 2 +- source/dnode/vnode/src/vnd/vnodeCfg.c | 27 ++++++++++++++++++++++++++ source/dnode/vnode/src/vnd/vnodeOpen.c | 2 +- source/dnode/vnode/src/vnd/vnodeSync.c | 4 ++-- 4 files changed, 31 insertions(+), 4 deletions(-) diff --git a/source/dnode/vnode/src/inc/vnodeSync.h b/source/dnode/vnode/src/inc/vnodeSync.h index 6bbdc4ae72..fea94c4607 100644 --- a/source/dnode/vnode/src/inc/vnodeSync.h +++ b/source/dnode/vnode/src/inc/vnodeSync.h @@ -20,7 +20,7 @@ extern "C" { #endif -int32_t vnodeSyncOpen(SVnode *pVnode); +int32_t vnodeSyncOpen(SVnode *pVnode, char *path); int32_t vnodeSyncStart(SVnode *pVnode); void vnodeSyncClose(SVnode *pVnode); diff --git a/source/dnode/vnode/src/vnd/vnodeCfg.c b/source/dnode/vnode/src/vnd/vnodeCfg.c index 625a2b3aed..0f8bab7856 100644 --- a/source/dnode/vnode/src/vnd/vnodeCfg.c +++ b/source/dnode/vnode/src/vnd/vnodeCfg.c @@ -82,6 +82,18 @@ int vnodeEncodeConfig(const void *pObj, SJson *pJson) { if (tjsonAddIntegerToObject(pJson, "hashEnd", pCfg->hashEnd) < 0) return -1; if (tjsonAddIntegerToObject(pJson, "hashMethod", pCfg->hashMethod) < 0) return -1; + // sync integration + if (tjsonAddIntegerToObject(pJson, "syncCfg.replicaNum", pCfg->syncCfg.replicaNum) < 0) return -1; + if (tjsonAddIntegerToObject(pJson, "syncCfg.myIndex", pCfg->syncCfg.myIndex) < 0) return -1; + SJson *pNodeInfoArr = tjsonCreateArray(); + tjsonAddItemToObject(pJson, "syncCfg.nodeInfo", pNodeInfoArr); + for (int i = 0; i < pCfg->syncCfg.replicaNum; ++i) { + SJson *pNodeInfo = tjsonCreateObject(); + tjsonAddIntegerToObject(pNodeInfo, "nodePort", (pCfg->syncCfg.nodeInfo)[i].nodePort); + tjsonAddStringToObject(pNodeInfo, "nodeFqdn", (pCfg->syncCfg.nodeInfo)[i].nodeFqdn); + tjsonAddItemToArray(pNodeInfoArr, pNodeInfo); + } + return 0; } @@ -120,6 +132,21 @@ int vnodeDecodeConfig(const SJson *pJson, void *pObj) { if (tjsonGetNumberValue(pJson, "hashEnd", pCfg->hashEnd) < 0) return -1; if (tjsonGetNumberValue(pJson, "hashMethod", pCfg->hashMethod) < 0) return -1; + // sync integration + if (tjsonGetNumberValue(pJson, "syncCfg.replicaNum", pCfg->syncCfg.replicaNum) < 0) return -1; + if (tjsonGetNumberValue(pJson, "syncCfg.myIndex", pCfg->syncCfg.myIndex) < 0) return -1; + + SJson *pNodeInfoArr = tjsonGetObjectItem(pJson, "syncCfg.nodeInfo"); + int arraySize = tjsonGetArraySize(pNodeInfoArr); + assert(arraySize == pCfg->syncCfg.replicaNum); + + for (int i = 0; i < arraySize; ++i) { + cJSON *pNodeInfo = tjsonGetArrayItem(pNodeInfoArr, i); + assert(pNodeInfo != NULL); + tjsonGetNumberValue(pNodeInfo, "nodePort", (pCfg->syncCfg.nodeInfo)[i].nodePort); + tjsonGetStringValue(pNodeInfo, "nodeFqdn", (pCfg->syncCfg.nodeInfo)[i].nodeFqdn); + } + return 0; } diff --git a/source/dnode/vnode/src/vnd/vnodeOpen.c b/source/dnode/vnode/src/vnd/vnodeOpen.c index ae5c99ff03..30eb9e2a42 100644 --- a/source/dnode/vnode/src/vnd/vnodeOpen.c +++ b/source/dnode/vnode/src/vnd/vnodeOpen.c @@ -126,7 +126,7 @@ SVnode *vnodeOpen(const char *path, STfs *pTfs, SMsgCb msgCb) { // sync integration // open sync - if (vnodeSyncOpen(pVnode)) { + if (vnodeSyncOpen(pVnode, dir)) { goto _err; } diff --git a/source/dnode/vnode/src/vnd/vnodeSync.c b/source/dnode/vnode/src/vnd/vnodeSync.c index e0e421b95c..4db483851e 100644 --- a/source/dnode/vnode/src/vnd/vnodeSync.c +++ b/source/dnode/vnode/src/vnd/vnodeSync.c @@ -20,7 +20,7 @@ // sync integration -int32_t vnodeSyncOpen(SVnode *pVnode) { +int32_t vnodeSyncOpen(SVnode *pVnode, char *path) { SSyncInfo syncInfo; syncInfo.vgId = pVnode->config.vgId; SSyncCfg *pCfg = &(syncInfo.syncCfg); @@ -28,7 +28,7 @@ int32_t vnodeSyncOpen(SVnode *pVnode) { pCfg->myIndex = pVnode->config.syncCfg.myIndex; memcpy(pCfg->nodeInfo, pVnode->config.syncCfg.nodeInfo, sizeof(pCfg->nodeInfo)); - snprintf(syncInfo.path, sizeof(syncInfo.path), "%s/sync", pVnode->path); + snprintf(syncInfo.path, sizeof(syncInfo.path), "%s/sync", path); syncInfo.pWal = pVnode->pWal; syncInfo.pFsm = syncVnodeMakeFsm(pVnode); From 75684fed32cadfa51e980542fe8a12a0dd5f3cc4 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 20 Apr 2022 10:24:37 +0800 Subject: [PATCH 12/51] report status by dnodeId equal --- source/dnode/mnode/impl/src/mndDnode.c | 33 ++++++++++++++------------ 1 file changed, 18 insertions(+), 15 deletions(-) diff --git a/source/dnode/mnode/impl/src/mndDnode.c b/source/dnode/mnode/impl/src/mndDnode.c index 5ce87a413c..f225f2fbe7 100644 --- a/source/dnode/mnode/impl/src/mndDnode.c +++ b/source/dnode/mnode/impl/src/mndDnode.c @@ -52,9 +52,9 @@ static int32_t mndProcessConfigDnodeReq(SNodeMsg *pReq); static int32_t mndProcessConfigDnodeRsp(SNodeMsg *pRsp); static int32_t mndProcessStatusReq(SNodeMsg *pReq); -static int32_t mndRetrieveConfigs(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock* pBlock, int32_t rows); +static int32_t mndRetrieveConfigs(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows); static void mndCancelGetNextConfig(SMnode *pMnode, void *pIter); -static int32_t mndRetrieveDnodes(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock* pBlock, int32_t rows); +static int32_t mndRetrieveDnodes(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows); static void mndCancelGetNextDnode(SMnode *pMnode, void *pIter); int32_t mndInitDnode(SMnode *pMnode) { @@ -335,10 +335,13 @@ static int32_t mndProcessStatusReq(SNodeMsg *pReq) { } bool roleChanged = false; for (int32_t vg = 0; vg < pVgroup->replica; ++vg) { - if (pVgroup->vnodeGid[vg].role != pVload->role) { - roleChanged = true; + // sync integration + if (pVgroup->vnodeGid[vg].dnodeId == statusReq.dnodeId) { + if (pVgroup->vnodeGid[vg].role != pVload->role) { + roleChanged = true; + } + pVgroup->vnodeGid[vg].role = pVload->role; } - pVgroup->vnodeGid[vg].role = pVload->role; } if (roleChanged) { // notify scheduler role has changed @@ -634,7 +637,7 @@ static int32_t mndProcessConfigDnodeRsp(SNodeMsg *pRsp) { return TSDB_CODE_SUCCESS; } -static int32_t mndRetrieveConfigs(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock* pBlock, int32_t rows) { +static int32_t mndRetrieveConfigs(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) { SMnode *pMnode = pReq->pNode; int32_t totalRows = 0; int32_t numOfRows = 0; @@ -667,12 +670,12 @@ static int32_t mndRetrieveConfigs(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock* STR_WITH_MAXSIZE_TO_VARSTR(buf, cfgOpts[i], TSDB_CONFIG_OPTION_LEN); - SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); - colDataAppend(pColInfo, numOfRows, (const char*) buf, false); + SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); + colDataAppend(pColInfo, numOfRows, (const char *)buf, false); STR_WITH_MAXSIZE_TO_VARSTR(bufVal, cfgVals[i], TSDB_CONIIG_VALUE_LEN); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); - colDataAppend(pColInfo, numOfRows, (const char*) bufVal, false); + colDataAppend(pColInfo, numOfRows, (const char *)bufVal, false); numOfRows++; } @@ -683,7 +686,7 @@ static int32_t mndRetrieveConfigs(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock* static void mndCancelGetNextConfig(SMnode *pMnode, void *pIter) {} -static int32_t mndRetrieveDnodes(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock* pBlock, int32_t rows) { +static int32_t mndRetrieveDnodes(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) { SMnode *pMnode = pReq->pNode; SSdb *pSdb = pMnode->pSdb; int32_t numOfRows = 0; @@ -698,8 +701,8 @@ static int32_t mndRetrieveDnodes(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock* p cols = 0; - SColumnInfoData* pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); - colDataAppend(pColInfo, numOfRows, (const char*) &pDnode->id, false); + SColumnInfoData *pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); + colDataAppend(pColInfo, numOfRows, (const char *)&pDnode->id, false); char buf[tListLen(pDnode->ep) + VARSTR_HEADER_SIZE] = {0}; STR_WITH_MAXSIZE_TO_VARSTR(buf, pDnode->ep, pShow->bytes[cols]); @@ -709,19 +712,19 @@ static int32_t mndRetrieveDnodes(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock* p pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); int16_t id = mndGetVnodesNum(pMnode, pDnode->id); - colDataAppend(pColInfo, numOfRows, (const char*) &id, false); + colDataAppend(pColInfo, numOfRows, (const char *)&id, false); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); colDataAppend(pColInfo, numOfRows, (const char *)&pDnode->numOfSupportVnodes, false); char b1[9] = {0}; - STR_TO_VARSTR(b1, online? "ready":"offline"); + STR_TO_VARSTR(b1, online ? "ready" : "offline"); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); colDataAppend(pColInfo, numOfRows, b1, false); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); - colDataAppend(pColInfo, numOfRows, (const char*) &pDnode->createdTime, false); + colDataAppend(pColInfo, numOfRows, (const char *)&pDnode->createdTime, false); char b[tListLen(offlineReason) + VARSTR_HEADER_SIZE] = {0}; STR_TO_VARSTR(b, online ? "" : offlineReason[pDnode->offlineReason]); From 58792537a4a0aceb24ffcef32db7d00010a24ec4 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 20 Apr 2022 10:40:51 +0800 Subject: [PATCH 13/51] =?UTF-8?q?sync=20integration=20send=20response?= =?UTF-8?q?=E2=80=9C?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- source/dnode/mgmt/mgmt_vnode/src/vmWorker.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c index d15f97b042..a53127afce 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c @@ -184,19 +184,22 @@ static void vmProcessApplyQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO for (int32_t i = 0; i < numOfMsgs; ++i) { #if 1 + // sync integration + taosGetQitem(qall, (void **)&pMsg); + rsp.code = 0; + rsp.pCont = NULL; + rsp.contLen = 0; + if (vnodeProcessWriteReq(pVnode->pImpl, &pMsg->rpcMsg, version++, &rsp) < 0) { + rsp.code = terrno; + dTrace("vnodeProcessWriteReq error, code:%d", terrno); + } + if (pMsg->rpcMsg.handle != NULL && pMsg->rpcMsg.ahandle != NULL) { rsp.ahandle = pMsg->rpcMsg.ahandle; rsp.handle = pMsg->rpcMsg.handle; - rsp.code = 0; - rsp.pCont = NULL; - rsp.contLen = 0; - - if (vnodeProcessWriteReq(pVnode->pImpl, &pMsg->rpcMsg, version++, &rsp) < 0) { - rsp.code = terrno; - tmsgSendRsp(&rsp); - } + tmsgSendRsp(&rsp); } #endif } From 704671d2fe035e37a7f9135ca8a418d2c176ac16 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 20 Apr 2022 10:54:27 +0800 Subject: [PATCH 14/51] sync integration add vnodeSyncClose --- source/dnode/vnode/src/vnd/vnodeOpen.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/source/dnode/vnode/src/vnd/vnodeOpen.c b/source/dnode/vnode/src/vnd/vnodeOpen.c index 30eb9e2a42..dee54be5ac 100644 --- a/source/dnode/vnode/src/vnd/vnodeOpen.c +++ b/source/dnode/vnode/src/vnd/vnodeOpen.c @@ -155,6 +155,10 @@ void vnodeClose(SVnode *pVnode) { vnodeSyncCommit(pVnode); // close vnode vnodeQueryClose(pVnode); + + // sync integration + vnodeSyncClose(pVnode); + walClose(pVnode->pWal); tqClose(pVnode->pTq); tsdbClose(pVnode->pTsdb); From 6035f0305d39911dbff10c35fd32a0aa57068d14 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 20 Apr 2022 11:51:00 +0800 Subject: [PATCH 15/51] sync integration add SyncApplyMsg --- include/libs/sync/syncTools.h | 33 +++++++++++++++++++++++++++++- source/libs/sync/src/syncMessage.c | 28 +++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 1 deletion(-) diff --git a/include/libs/sync/syncTools.h b/include/libs/sync/syncTools.h index 567889191a..327450c10a 100644 --- a/include/libs/sync/syncTools.h +++ b/include/libs/sync/syncTools.h @@ -363,6 +363,37 @@ void syncAppendEntriesReplyPrint2(char* s, const SyncAppendEntriesReply* pMsg); void syncAppendEntriesReplyLog(const SyncAppendEntriesReply* pMsg); void syncAppendEntriesReplyLog2(char* s, const SyncAppendEntriesReply* pMsg); +// --------------------------------------------- +typedef struct SyncApplyMsg { + uint32_t bytes; + int32_t vgId; + uint32_t msgType; // user SyncApplyMsg msgType + uint32_t originalRpcType; // user RpcMsg msgType + SFsmCbMeta fsmMeta; + uint32_t dataLen; // user RpcMsg.contLen + char data[]; // user RpcMsg.pCont +} SyncApplyMsg; + +SyncApplyMsg* syncApplyMsgBuild(uint32_t dataLen); +SyncApplyMsg* syncApplyMsgBuild2(const SRpcMsg* pOriginalRpcMsg, int32_t vgId, SFsmCbMeta* pMeta); +void syncApplyMsgDestroy(SyncApplyMsg* pMsg); +void syncApplyMsgSerialize(const SyncApplyMsg* pMsg, char* buf, uint32_t bufLen); +void syncApplyMsgDeserialize(const char* buf, uint32_t len, SyncApplyMsg* pMsg); +char* syncApplyMsgSerialize2(const SyncApplyMsg* pMsg, uint32_t* len); +SyncApplyMsg* syncApplyMsgDeserialize2(const char* buf, uint32_t len); +void syncApplyMsg2RpcMsg(const SyncApplyMsg* pMsg, SRpcMsg* pRpcMsg); // SyncApplyMsg to SRpcMsg, put it into ApplyQ +void syncApplyMsgFromRpcMsg(const SRpcMsg* pRpcMsg, SyncApplyMsg* pMsg); // get SRpcMsg from ApplyQ, to SyncApplyMsg +void syncApplyMsg2OriginalRpcMsg(const SyncApplyMsg* pMsg, SRpcMsg* pOriginalRpcMsg); // SyncApplyMsg to OriginalRpcMsg +SyncApplyMsg* syncApplyMsgFromRpcMsg2(const SRpcMsg* pRpcMsg); +cJSON* syncApplyMsg2Json(const SyncApplyMsg* pMsg); +char* syncApplyMsg2Str(const SyncApplyMsg* pMsg); + +// for debug ---------------------- +void syncApplyMsgPrint(const SyncApplyMsg* pMsg); +void syncApplyMsgPrint2(char* s, const SyncApplyMsg* pMsg); +void ssyncApplyMsgLog(const SyncApplyMsg* pMsg); +void syncApplyMsgLog2(char* s, const SyncApplyMsg* pMsg); + // on message ---------------------- int32_t syncNodeOnPingCb(SSyncNode* ths, SyncPing* pMsg); int32_t syncNodeOnPingReplyCb(SSyncNode* ths, SyncPingReply* pMsg); @@ -373,7 +404,7 @@ int32_t syncNodeOnRequestVoteReplyCb(SSyncNode* ths, SyncRequestVoteReply* pMsg) int32_t syncNodeOnAppendEntriesCb(SSyncNode* ths, SyncAppendEntries* pMsg); int32_t syncNodeOnAppendEntriesReplyCb(SSyncNode* ths, SyncAppendEntriesReply* pMsg); -//--------------------- +// --------------------------------------------- #ifdef __cplusplus } diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index 76c68569c3..1299b37769 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -1531,3 +1531,31 @@ void syncAppendEntriesReplyLog2(char* s, const SyncAppendEntriesReply* pMsg) { sTrace("syncAppendEntriesReplyLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); taosMemoryFree(serialized); } + +// ---- message process SyncApplyMsg---- +SyncApplyMsg* syncApplyMsgBuild(uint32_t dataLen) { return NULL; } +SyncApplyMsg* syncApplyMsgBuild2(const SRpcMsg* pOriginalRpcMsg, int32_t vgId, SFsmCbMeta* pMeta) { return NULL; } +void syncApplyMsgDestroy(SyncApplyMsg* pMsg) {} +void syncApplyMsgSerialize(const SyncApplyMsg* pMsg, char* buf, uint32_t bufLen) {} +void syncApplyMsgDeserialize(const char* buf, uint32_t len, SyncApplyMsg* pMsg) {} +char* syncApplyMsgSerialize2(const SyncApplyMsg* pMsg, uint32_t* len) { return NULL; } +SyncApplyMsg* syncApplyMsgDeserialize2(const char* buf, uint32_t len) { return NULL; } + +// SyncApplyMsg to SRpcMsg, put it into ApplyQ +void syncApplyMsg2RpcMsg(const SyncApplyMsg* pMsg, SRpcMsg* pRpcMsg) {} + +// get SRpcMsg from ApplyQ, to SyncApplyMsg +void syncApplyMsgFromRpcMsg(const SRpcMsg* pRpcMsg, SyncApplyMsg* pMsg) {} + +// SyncApplyMsg to OriginalRpcMsg +void syncApplyMsg2OriginalRpcMsg(const SyncApplyMsg* pMsg, SRpcMsg* pOriginalRpcMsg) {} + +SyncApplyMsg* syncApplyMsgFromRpcMsg2(const SRpcMsg* pRpcMsg) { return NULL; } +cJSON* syncApplyMsg2Json(const SyncApplyMsg* pMsg) { return NULL; } +char* syncApplyMsg2Str(const SyncApplyMsg* pMsg) { return NULL; } + +// for debug ---------------------- +void syncApplyMsgPrint(const SyncApplyMsg* pMsg) {} +void syncApplyMsgPrint2(char* s, const SyncApplyMsg* pMsg) {} +void ssyncApplyMsgLog(const SyncApplyMsg* pMsg) {} +void syncApplyMsgLog2(char* s, const SyncApplyMsg* pMsg) {} From 9cb2192a879ee85b30df4378f3d8d56e05a0050f Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 20 Apr 2022 14:30:34 +0800 Subject: [PATCH 16/51] add SyncApplyMsg and test --- include/common/tmsgdef.h | 2 + include/libs/sync/syncTools.h | 8 +- source/libs/sync/src/syncMessage.c | 157 ++++++++++++++++++--- source/libs/sync/test/CMakeLists.txt | 14 ++ source/libs/sync/test/syncApplyMsgTest.cpp | 123 ++++++++++++++++ 5 files changed, 283 insertions(+), 21 deletions(-) create mode 100644 source/libs/sync/test/syncApplyMsgTest.cpp diff --git a/include/common/tmsgdef.h b/include/common/tmsgdef.h index 24d259b0f2..b3c47353b8 100644 --- a/include/common/tmsgdef.h +++ b/include/common/tmsgdef.h @@ -206,6 +206,7 @@ enum { TD_DEF_MSG_TYPE(TDMT_VND_CANCEL_SMA, "vnode-cancel-sma", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_VND_DROP_SMA, "vnode-drop-sma", NULL, NULL) + // sync integration TD_DEF_MSG_TYPE(TDMT_VND_SYNC_TIMEOUT, "vnode-sync-timeout", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_VND_SYNC_PING, "vnode-sync-ping", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_VND_SYNC_PING_REPLY, "vnode-sync-ping-reply", NULL, NULL) @@ -218,6 +219,7 @@ enum { TD_DEF_MSG_TYPE(TDMT_VND_SYNC_NOOP, "vnode-sync-noop", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_VND_SYNC_UNKNOWN, "vnode-sync-unknown", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_VND_SYNC_COMMON_RESPONSE, "vnode-sync-common-response", NULL, NULL) + TD_DEF_MSG_TYPE(TDMT_VND_SYNC_APPLY_MSG, "vnode-sync-apply-msg", NULL, NULL) // Requests handled by QNODE TD_NEW_MSG_SEG(TDMT_QND_MSG) diff --git a/include/libs/sync/syncTools.h b/include/libs/sync/syncTools.h index 327450c10a..0438d6d436 100644 --- a/include/libs/sync/syncTools.h +++ b/include/libs/sync/syncTools.h @@ -383,15 +383,15 @@ char* syncApplyMsgSerialize2(const SyncApplyMsg* pMsg, uint32_t* len); SyncApplyMsg* syncApplyMsgDeserialize2(const char* buf, uint32_t len); void syncApplyMsg2RpcMsg(const SyncApplyMsg* pMsg, SRpcMsg* pRpcMsg); // SyncApplyMsg to SRpcMsg, put it into ApplyQ void syncApplyMsgFromRpcMsg(const SRpcMsg* pRpcMsg, SyncApplyMsg* pMsg); // get SRpcMsg from ApplyQ, to SyncApplyMsg -void syncApplyMsg2OriginalRpcMsg(const SyncApplyMsg* pMsg, SRpcMsg* pOriginalRpcMsg); // SyncApplyMsg to OriginalRpcMsg SyncApplyMsg* syncApplyMsgFromRpcMsg2(const SRpcMsg* pRpcMsg); -cJSON* syncApplyMsg2Json(const SyncApplyMsg* pMsg); -char* syncApplyMsg2Str(const SyncApplyMsg* pMsg); +void syncApplyMsg2OriginalRpcMsg(const SyncApplyMsg* pMsg, SRpcMsg* pOriginalRpcMsg); // SyncApplyMsg to OriginalRpcMsg +cJSON* syncApplyMsg2Json(const SyncApplyMsg* pMsg); +char* syncApplyMsg2Str(const SyncApplyMsg* pMsg); // for debug ---------------------- void syncApplyMsgPrint(const SyncApplyMsg* pMsg); void syncApplyMsgPrint2(char* s, const SyncApplyMsg* pMsg); -void ssyncApplyMsgLog(const SyncApplyMsg* pMsg); +void syncApplyMsgLog(const SyncApplyMsg* pMsg); void syncApplyMsgLog2(char* s, const SyncApplyMsg* pMsg); // on message ---------------------- diff --git a/source/libs/sync/src/syncMessage.c b/source/libs/sync/src/syncMessage.c index 1299b37769..2b1b40997e 100644 --- a/source/libs/sync/src/syncMessage.c +++ b/source/libs/sync/src/syncMessage.c @@ -1533,29 +1533,152 @@ void syncAppendEntriesReplyLog2(char* s, const SyncAppendEntriesReply* pMsg) { } // ---- message process SyncApplyMsg---- -SyncApplyMsg* syncApplyMsgBuild(uint32_t dataLen) { return NULL; } -SyncApplyMsg* syncApplyMsgBuild2(const SRpcMsg* pOriginalRpcMsg, int32_t vgId, SFsmCbMeta* pMeta) { return NULL; } -void syncApplyMsgDestroy(SyncApplyMsg* pMsg) {} -void syncApplyMsgSerialize(const SyncApplyMsg* pMsg, char* buf, uint32_t bufLen) {} -void syncApplyMsgDeserialize(const char* buf, uint32_t len, SyncApplyMsg* pMsg) {} -char* syncApplyMsgSerialize2(const SyncApplyMsg* pMsg, uint32_t* len) { return NULL; } -SyncApplyMsg* syncApplyMsgDeserialize2(const char* buf, uint32_t len) { return NULL; } +SyncApplyMsg* syncApplyMsgBuild(uint32_t dataLen) { + uint32_t bytes = sizeof(SyncApplyMsg) + dataLen; + SyncApplyMsg* pMsg = taosMemoryMalloc(bytes); + memset(pMsg, 0, bytes); + pMsg->bytes = bytes; + pMsg->msgType = TDMT_VND_SYNC_APPLY_MSG; + pMsg->dataLen = dataLen; + return pMsg; +} + +SyncApplyMsg* syncApplyMsgBuild2(const SRpcMsg* pOriginalRpcMsg, int32_t vgId, SFsmCbMeta* pMeta) { + SyncApplyMsg* pMsg = syncApplyMsgBuild(pOriginalRpcMsg->contLen); + pMsg->vgId = vgId; + pMsg->originalRpcType = pOriginalRpcMsg->msgType; + pMsg->fsmMeta = *pMeta; + memcpy(pMsg->data, pOriginalRpcMsg->pCont, pOriginalRpcMsg->contLen); + return pMsg; +} + +void syncApplyMsgDestroy(SyncApplyMsg* pMsg) { + if (pMsg != NULL) { + taosMemoryFree(pMsg); + } +} + +void syncApplyMsgSerialize(const SyncApplyMsg* pMsg, char* buf, uint32_t bufLen) { + assert(pMsg->bytes <= bufLen); + memcpy(buf, pMsg, pMsg->bytes); +} + +void syncApplyMsgDeserialize(const char* buf, uint32_t len, SyncApplyMsg* pMsg) { + memcpy(pMsg, buf, len); + assert(len == pMsg->bytes); +} + +char* syncApplyMsgSerialize2(const SyncApplyMsg* pMsg, uint32_t* len) { + char* buf = taosMemoryMalloc(pMsg->bytes); + assert(buf != NULL); + syncApplyMsgSerialize(pMsg, buf, pMsg->bytes); + if (len != NULL) { + *len = pMsg->bytes; + } + return buf; +} + +SyncApplyMsg* syncApplyMsgDeserialize2(const char* buf, uint32_t len) { + uint32_t bytes = *((uint32_t*)buf); + SyncApplyMsg* pMsg = taosMemoryMalloc(bytes); + assert(pMsg != NULL); + syncApplyMsgDeserialize(buf, len, pMsg); + assert(len == pMsg->bytes); + return pMsg; +} // SyncApplyMsg to SRpcMsg, put it into ApplyQ -void syncApplyMsg2RpcMsg(const SyncApplyMsg* pMsg, SRpcMsg* pRpcMsg) {} +void syncApplyMsg2RpcMsg(const SyncApplyMsg* pMsg, SRpcMsg* pRpcMsg) { + memset(pRpcMsg, 0, sizeof(*pRpcMsg)); + pRpcMsg->msgType = pMsg->msgType; + pRpcMsg->contLen = pMsg->bytes; + pRpcMsg->pCont = rpcMallocCont(pRpcMsg->contLen); + syncApplyMsgSerialize(pMsg, pRpcMsg->pCont, pRpcMsg->contLen); +} // get SRpcMsg from ApplyQ, to SyncApplyMsg -void syncApplyMsgFromRpcMsg(const SRpcMsg* pRpcMsg, SyncApplyMsg* pMsg) {} +void syncApplyMsgFromRpcMsg(const SRpcMsg* pRpcMsg, SyncApplyMsg* pMsg) { + syncApplyMsgDeserialize(pRpcMsg->pCont, pRpcMsg->contLen, pMsg); +} + +SyncApplyMsg* syncApplyMsgFromRpcMsg2(const SRpcMsg* pRpcMsg) { + SyncApplyMsg* pMsg = syncApplyMsgDeserialize2(pRpcMsg->pCont, pRpcMsg->contLen); + return pMsg; +} // SyncApplyMsg to OriginalRpcMsg -void syncApplyMsg2OriginalRpcMsg(const SyncApplyMsg* pMsg, SRpcMsg* pOriginalRpcMsg) {} +void syncApplyMsg2OriginalRpcMsg(const SyncApplyMsg* pMsg, SRpcMsg* pOriginalRpcMsg) { + memset(pOriginalRpcMsg, 0, sizeof(*pOriginalRpcMsg)); + pOriginalRpcMsg->msgType = pMsg->originalRpcType; + pOriginalRpcMsg->contLen = pMsg->dataLen; + pOriginalRpcMsg->pCont = rpcMallocCont(pOriginalRpcMsg->contLen); + memcpy(pOriginalRpcMsg->pCont, pMsg->data, pOriginalRpcMsg->contLen); +} -SyncApplyMsg* syncApplyMsgFromRpcMsg2(const SRpcMsg* pRpcMsg) { return NULL; } -cJSON* syncApplyMsg2Json(const SyncApplyMsg* pMsg) { return NULL; } -char* syncApplyMsg2Str(const SyncApplyMsg* pMsg) { return NULL; } +cJSON* syncApplyMsg2Json(const SyncApplyMsg* pMsg) { + char u64buf[128]; + 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_AddNumberToObject(pRoot, "originalRpcType", pMsg->originalRpcType); + + snprintf(u64buf, sizeof(u64buf), "%ld", pMsg->fsmMeta.index); + cJSON_AddStringToObject(pRoot, "fsmMeta.index", u64buf); + cJSON_AddNumberToObject(pRoot, "fsmMeta.isWeak", pMsg->fsmMeta.isWeak); + cJSON_AddNumberToObject(pRoot, "fsmMeta.code", pMsg->fsmMeta.code); + cJSON_AddNumberToObject(pRoot, "fsmMeta.state", pMsg->fsmMeta.state); + cJSON_AddStringToObject(pRoot, "fsmMeta.state.str", syncUtilState2String(pMsg->fsmMeta.state)); + snprintf(u64buf, sizeof(u64buf), "%lu", pMsg->fsmMeta.seqNum); + cJSON_AddStringToObject(pRoot, "fsmMeta.seqNum", u64buf); + + cJSON_AddNumberToObject(pRoot, "dataLen", pMsg->dataLen); + char* s; + s = syncUtilprintBin((char*)(pMsg->data), pMsg->dataLen); + cJSON_AddStringToObject(pRoot, "data", s); + taosMemoryFree(s); + s = syncUtilprintBin2((char*)(pMsg->data), pMsg->dataLen); + cJSON_AddStringToObject(pRoot, "data2", s); + taosMemoryFree(s); + } + + cJSON* pJson = cJSON_CreateObject(); + cJSON_AddItemToObject(pJson, "SyncApplyMsg", pRoot); + return pJson; +} + +char* syncApplyMsg2Str(const SyncApplyMsg* pMsg) { + cJSON* pJson = syncApplyMsg2Json(pMsg); + char* serialized = cJSON_Print(pJson); + cJSON_Delete(pJson); + return serialized; +} // for debug ---------------------- -void syncApplyMsgPrint(const SyncApplyMsg* pMsg) {} -void syncApplyMsgPrint2(char* s, const SyncApplyMsg* pMsg) {} -void ssyncApplyMsgLog(const SyncApplyMsg* pMsg) {} -void syncApplyMsgLog2(char* s, const SyncApplyMsg* pMsg) {} +void syncApplyMsgPrint(const SyncApplyMsg* pMsg) { + char* serialized = syncApplyMsg2Str(pMsg); + printf("syncApplyMsgPrint | len:%lu | %s \n", strlen(serialized), serialized); + fflush(NULL); + taosMemoryFree(serialized); +} + +void syncApplyMsgPrint2(char* s, const SyncApplyMsg* pMsg) { + char* serialized = syncApplyMsg2Str(pMsg); + printf("syncApplyMsgPrint2 | len:%lu | %s | %s \n", strlen(serialized), s, serialized); + fflush(NULL); + taosMemoryFree(serialized); +} + +void syncApplyMsgLog(const SyncApplyMsg* pMsg) { + char* serialized = syncApplyMsg2Str(pMsg); + sTrace("ssyncApplyMsgLog | len:%lu | %s", strlen(serialized), serialized); + taosMemoryFree(serialized); +} + +void syncApplyMsgLog2(char* s, const SyncApplyMsg* pMsg) { + char* serialized = syncApplyMsg2Str(pMsg); + sTrace("syncApplyMsgLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); + taosMemoryFree(serialized); +} diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index e2ac38150f..5d43d47358 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -41,6 +41,7 @@ add_executable(syncLogStoreCheck "") add_executable(syncRaftCfgTest "") add_executable(syncRespMgrTest "") add_executable(syncSnapshotTest "") +add_executable(syncApplyMsgTest "") target_sources(syncTest @@ -215,6 +216,10 @@ target_sources(syncSnapshotTest PRIVATE "syncSnapshotTest.cpp" ) +target_sources(syncApplyMsgTest + PRIVATE + "syncApplyMsgTest.cpp" +) target_include_directories(syncTest @@ -432,6 +437,11 @@ target_include_directories(syncSnapshotTest "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) +target_include_directories(syncApplyMsgTest + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) target_link_libraries(syncTest @@ -606,6 +616,10 @@ target_link_libraries(syncSnapshotTest sync gtest_main ) +target_link_libraries(syncApplyMsgTest + sync + gtest_main +) enable_testing() diff --git a/source/libs/sync/test/syncApplyMsgTest.cpp b/source/libs/sync/test/syncApplyMsgTest.cpp new file mode 100644 index 0000000000..7544613c5d --- /dev/null +++ b/source/libs/sync/test/syncApplyMsgTest.cpp @@ -0,0 +1,123 @@ +#include +#include +#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"); +} + +SyncApplyMsg *createMsg() { + SRpcMsg rpcMsg; + memset(&rpcMsg, 0, sizeof(rpcMsg)); + rpcMsg.msgType = 12345; + rpcMsg.contLen = 20; + rpcMsg.pCont = rpcMallocCont(rpcMsg.contLen); + strcpy((char *)rpcMsg.pCont, "hello rpc"); + + SFsmCbMeta meta; + meta.code = 11; + meta.index = 22; + meta.isWeak = 1; + meta.seqNum = 33; + meta.state = TAOS_SYNC_STATE_LEADER; + + SyncApplyMsg *pMsg = syncApplyMsgBuild2(&rpcMsg, 123, &meta); + rpcFreeCont(rpcMsg.pCont); + return pMsg; +} + +void test1() { + SyncApplyMsg *pMsg = createMsg(); + syncApplyMsgLog2((char *)"test1:", pMsg); + syncApplyMsgDestroy(pMsg); +} + +void test2() { + SyncApplyMsg *pMsg = createMsg(); + uint32_t len = pMsg->bytes; + char * serialized = (char *)taosMemoryMalloc(len); + syncApplyMsgSerialize(pMsg, serialized, len); + SyncApplyMsg *pMsg2 = syncApplyMsgBuild(pMsg->dataLen); + syncApplyMsgDeserialize(serialized, len, pMsg2); + syncApplyMsgLog2((char *)"test2: syncApplyMsgSerialize -> syncApplyMsgDeserialize ", pMsg2); + + taosMemoryFree(serialized); + syncApplyMsgDestroy(pMsg); + syncApplyMsgDestroy(pMsg2); +} + +void test3() { + SyncApplyMsg *pMsg = createMsg(); + uint32_t len; + char * serialized = syncApplyMsgSerialize2(pMsg, &len); + SyncApplyMsg *pMsg2 = syncApplyMsgDeserialize2(serialized, len); + syncApplyMsgLog2((char *)"test3: syncApplyMsgSerialize2 -> syncApplyMsgDeserialize2 ", pMsg2); + + taosMemoryFree(serialized); + syncApplyMsgDestroy(pMsg); + syncApplyMsgDestroy(pMsg2); +} + +void test4() { + SyncApplyMsg *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncApplyMsg2RpcMsg(pMsg, &rpcMsg); + SyncApplyMsg *pMsg2 = (SyncApplyMsg *)taosMemoryMalloc(rpcMsg.contLen); + syncApplyMsgFromRpcMsg(&rpcMsg, pMsg2); + syncApplyMsgLog2((char *)"test4: syncApplyMsg2RpcMsg -> syncApplyMsgFromRpcMsg ", pMsg2); + + rpcFreeCont(rpcMsg.pCont); + syncApplyMsgDestroy(pMsg); + syncApplyMsgDestroy(pMsg2); +} + +void test5() { + SyncApplyMsg *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncApplyMsg2RpcMsg(pMsg, &rpcMsg); + SyncApplyMsg *pMsg2 = syncApplyMsgFromRpcMsg2(&rpcMsg); + syncApplyMsgLog2((char *)"test5: syncClientRequest2RpcMsg -> syncApplyMsgFromRpcMsg2 ", pMsg2); + + rpcFreeCont(rpcMsg.pCont); + syncApplyMsgDestroy(pMsg); + syncApplyMsgDestroy(pMsg2); +} + +void test6() { + SyncApplyMsg *pMsg = createMsg(); + SRpcMsg rpcMsg; + syncApplyMsg2RpcMsg(pMsg, &rpcMsg); + SyncApplyMsg *pMsg2 = syncApplyMsgFromRpcMsg2(&rpcMsg); + + SRpcMsg originalRpcMsg; + syncApplyMsg2OriginalRpcMsg(pMsg2, &originalRpcMsg); + syncRpcMsgLog2((char*)"test6", &originalRpcMsg); + + rpcFreeCont(originalRpcMsg.pCont); + rpcFreeCont(rpcMsg.pCont); + syncApplyMsgDestroy(pMsg); + syncApplyMsgDestroy(pMsg2); +} + +int main() { + tsAsyncLog = 0; + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; + logTest(); + + test1(); + test2(); + test3(); + test4(); + test5(); + test6(); + + return 0; +} From ec73953a0f518e9259bcd55e539bbb03f96efdb9 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 20 Apr 2022 15:02:00 +0800 Subject: [PATCH 17/51] add SyncApplyMsg into vmProcessApplyQueue --- source/dnode/mgmt/mgmt_vnode/src/vmWorker.c | 17 +++++++++++++++-- source/dnode/vnode/src/vnd/vnodeSync.c | 19 +++++++++++++------ 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c index a53127afce..9e42f8dfb6 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c @@ -14,6 +14,8 @@ */ #define _DEFAULT_SOURCE +#include "sync.h" +#include "syncTools.h" #include "vmInt.h" static inline void vmSendRsp(SMgmtWrapper *pWrapper, SNodeMsg *pMsg, int32_t code) { @@ -180,7 +182,7 @@ static void vmProcessApplyQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO SNodeMsg *pMsg = NULL; SRpcMsg rsp; - static int64_t version = 0; + // static int64_t version = 0; for (int32_t i = 0; i < numOfMsgs; ++i) { #if 1 @@ -191,11 +193,22 @@ static void vmProcessApplyQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO rsp.code = 0; rsp.pCont = NULL; rsp.contLen = 0; - if (vnodeProcessWriteReq(pVnode->pImpl, &pMsg->rpcMsg, version++, &rsp) < 0) { + + // get original rpc msg + assert(pMsg->rpcMsg.msgType == TDMT_VND_SYNC_APPLY_MSG); + SyncApplyMsg *pSyncApplyMsg = syncApplyMsgFromRpcMsg2(&pMsg->rpcMsg); + syncApplyMsgLog2("==vmProcessApplyQueue==", pSyncApplyMsg); + SRpcMsg originalRpcMsg; + syncApplyMsg2OriginalRpcMsg(pSyncApplyMsg, &originalRpcMsg); + + if (vnodeProcessWriteReq(pVnode->pImpl, &originalRpcMsg, pSyncApplyMsg->fsmMeta.index, &rsp) < 0) { rsp.code = terrno; dTrace("vnodeProcessWriteReq error, code:%d", terrno); } + syncApplyMsgDestroy(pSyncApplyMsg); + rpcFreeCont(originalRpcMsg.pCont); + if (pMsg->rpcMsg.handle != NULL && pMsg->rpcMsg.ahandle != NULL) { rsp.ahandle = pMsg->rpcMsg.ahandle; rsp.handle = pMsg->rpcMsg.handle; diff --git a/source/dnode/vnode/src/vnd/vnodeSync.c b/source/dnode/vnode/src/vnd/vnodeSync.c index 4db483851e..93c40aa019 100644 --- a/source/dnode/vnode/src/vnd/vnodeSync.c +++ b/source/dnode/vnode/src/vnd/vnodeSync.c @@ -107,14 +107,21 @@ void vnodeSyncCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cb pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state), beginIndex); syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); - SRpcMsg applyMsg; - applyMsg = *pMsg; - applyMsg.pCont = rpcMallocCont(applyMsg.contLen); - assert(applyMsg.contLen == pMsg->contLen); - memcpy(applyMsg.pCont, pMsg->pCont, applyMsg.contLen); + SVnode *pVnode = (SVnode *)(pFsm->data); + SyncApplyMsg *pSyncApplyMsg = syncApplyMsgBuild2(pMsg, pVnode->config.vgId, &cbMeta); + SRpcMsg applyMsg; + syncApplyMsg2RpcMsg(pSyncApplyMsg, &applyMsg); + syncApplyMsgDestroy(pSyncApplyMsg); + + /* + SRpcMsg applyMsg; + applyMsg = *pMsg; + applyMsg.pCont = rpcMallocCont(applyMsg.contLen); + assert(applyMsg.contLen == pMsg->contLen); + memcpy(applyMsg.pCont, pMsg->pCont, applyMsg.contLen); + */ // recover handle for response - SVnode *pVnode = (SVnode *)(pFsm->data); SRpcMsg saveRpcMsg; int32_t ret = syncGetAndDelRespRpc(pVnode->sync, cbMeta.seqNum, &saveRpcMsg); if (ret == 1 && cbMeta.state == TAOS_SYNC_STATE_LEADER) { From d7edb105f263538b0dee13abbff3838845bccd75 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 20 Apr 2022 15:05:14 +0800 Subject: [PATCH 18/51] add SyncApplyMsg into vmProcessApplyQueue --- source/dnode/mgmt/mgmt_vnode/src/vmWorker.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c index 9e42f8dfb6..b3afba51c0 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c @@ -190,6 +190,7 @@ static void vmProcessApplyQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO taosGetQitem(qall, (void **)&pMsg); + // init response rpc msg rsp.code = 0; rsp.pCont = NULL; rsp.contLen = 0; @@ -201,6 +202,7 @@ static void vmProcessApplyQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO SRpcMsg originalRpcMsg; syncApplyMsg2OriginalRpcMsg(pSyncApplyMsg, &originalRpcMsg); + // apply data into tsdb if (vnodeProcessWriteReq(pVnode->pImpl, &originalRpcMsg, pSyncApplyMsg->fsmMeta.index, &rsp) < 0) { rsp.code = terrno; dTrace("vnodeProcessWriteReq error, code:%d", terrno); @@ -209,6 +211,7 @@ static void vmProcessApplyQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO syncApplyMsgDestroy(pSyncApplyMsg); rpcFreeCont(originalRpcMsg.pCont); + // if leader, send response if (pMsg->rpcMsg.handle != NULL && pMsg->rpcMsg.ahandle != NULL) { rsp.ahandle = pMsg->rpcMsg.ahandle; rsp.handle = pMsg->rpcMsg.handle; From eef910f7e2c820c81b68ed5d1308973e47d51ce0 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 20 Apr 2022 16:33:20 +0800 Subject: [PATCH 19/51] sync integration vnodeCfg cJson->SJson --- source/dnode/vnode/src/vnd/vnodeCfg.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/dnode/vnode/src/vnd/vnodeCfg.c b/source/dnode/vnode/src/vnd/vnodeCfg.c index 0f8bab7856..714497e786 100644 --- a/source/dnode/vnode/src/vnd/vnodeCfg.c +++ b/source/dnode/vnode/src/vnd/vnodeCfg.c @@ -141,7 +141,7 @@ int vnodeDecodeConfig(const SJson *pJson, void *pObj) { assert(arraySize == pCfg->syncCfg.replicaNum); for (int i = 0; i < arraySize; ++i) { - cJSON *pNodeInfo = tjsonGetArrayItem(pNodeInfoArr, i); + SJson *pNodeInfo = tjsonGetArrayItem(pNodeInfoArr, i); assert(pNodeInfo != NULL); tjsonGetNumberValue(pNodeInfo, "nodePort", (pCfg->syncCfg.nodeInfo)[i].nodePort); tjsonGetStringValue(pNodeInfo, "nodeFqdn", (pCfg->syncCfg.nodeInfo)[i].nodeFqdn); From bf3fd9b9c3c0eca158ad4a030dffd4a1a90c24b1 Mon Sep 17 00:00:00 2001 From: plum-lihui Date: Wed, 20 Apr 2022 16:35:51 +0800 Subject: [PATCH 20/51] [test: modify cases] --- tests/script/tsim/sync/oneReplica1VgElect.sim | 1 - tests/script/tsim/sync/oneReplica1VgElectWithInsert.sim | 1 - tests/script/tsim/sync/threeReplica1VgElect.sim | 1 - tests/script/tsim/sync/threeReplica1VgElectWihtInsert.sim | 1 - 4 files changed, 4 deletions(-) diff --git a/tests/script/tsim/sync/oneReplica1VgElect.sim b/tests/script/tsim/sync/oneReplica1VgElect.sim index 9726c0fdf5..8e99bc4b2e 100644 --- a/tests/script/tsim/sync/oneReplica1VgElect.sim +++ b/tests/script/tsim/sync/oneReplica1VgElect.sim @@ -203,7 +203,6 @@ if $data[0][4] == LEADER then $dnodeId = $data[0][3] elif $data[0][6] == LEADER then $dnodeId = $data[0][5] -endi elif $data[0][8] == LEADER then $dnodeId = $data[0][7] else diff --git a/tests/script/tsim/sync/oneReplica1VgElectWithInsert.sim b/tests/script/tsim/sync/oneReplica1VgElectWithInsert.sim index 972447ba9b..e85d5ea437 100644 --- a/tests/script/tsim/sync/oneReplica1VgElectWithInsert.sim +++ b/tests/script/tsim/sync/oneReplica1VgElectWithInsert.sim @@ -212,7 +212,6 @@ if $data[0][4] == LEADER then $dnodeId = $data[0][3] elif $data[0][6] == LEADER then $dnodeId = $data[0][5] -endi elif $data[0][8] == LEADER then $dnodeId = $data[0][7] else diff --git a/tests/script/tsim/sync/threeReplica1VgElect.sim b/tests/script/tsim/sync/threeReplica1VgElect.sim index f709c72dd7..e06bd86daa 100644 --- a/tests/script/tsim/sync/threeReplica1VgElect.sim +++ b/tests/script/tsim/sync/threeReplica1VgElect.sim @@ -203,7 +203,6 @@ if $data[0][4] == LEADER then $dnodeId = $data[0][3] elif $data[0][6] == LEADER then $dnodeId = $data[0][5] -endi elif $data[0][8] == LEADER then $dnodeId = $data[0][7] else diff --git a/tests/script/tsim/sync/threeReplica1VgElectWihtInsert.sim b/tests/script/tsim/sync/threeReplica1VgElectWihtInsert.sim index d934e82b98..797baea811 100644 --- a/tests/script/tsim/sync/threeReplica1VgElectWihtInsert.sim +++ b/tests/script/tsim/sync/threeReplica1VgElectWihtInsert.sim @@ -212,7 +212,6 @@ if $data[0][4] == LEADER then $dnodeId = $data[0][3] elif $data[0][6] == LEADER then $dnodeId = $data[0][5] -endi elif $data[0][8] == LEADER then $dnodeId = $data[0][7] else From a1a498a19adb8d8a1ff19224892f5900cec26cba Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 20 Apr 2022 16:56:00 +0800 Subject: [PATCH 21/51] sync integration add error code --- include/util/taoserror.h | 4 ++++ source/dnode/mgmt/mgmt_vnode/src/vmWorker.c | 4 ++-- source/libs/sync/test/syncApplyMsgTest.cpp | 16 ++++++++-------- 3 files changed, 14 insertions(+), 10 deletions(-) diff --git a/include/util/taoserror.h b/include/util/taoserror.h index c5b477343d..93ab133dc1 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -407,6 +407,10 @@ int32_t* taosGetErrno(); #define TSDB_CODE_SYN_INVALID_MSGLEN TAOS_DEF_ERROR_CODE(0, 0x0909) #define TSDB_CODE_SYN_INVALID_MSGTYPE TAOS_DEF_ERROR_CODE(0, 0x090A) +// sync integration +#define TSDB_CODE_SYN_NOT_LEADER TAOS_DEF_ERROR_CODE(0, 0x0910) +#define TSDB_CODE_SYN_INTERNAL_ERROR TAOS_DEF_ERROR_CODE(0, 0x09FF) + // tq #define TSDB_CODE_TQ_INVALID_CONFIG TAOS_DEF_ERROR_CODE(0, 0x0A00) #define TSDB_CODE_TQ_INIT_FAILED TAOS_DEF_ERROR_CODE(0, 0x0A01) diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c index b3afba51c0..206f53d36b 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c @@ -153,10 +153,10 @@ static void vmProcessWriteQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numO int32_t ret = syncPropose(vnodeGetSyncHandle(pVnode->pImpl), pRpc, false); if (ret == TAOS_SYNC_PROPOSE_NOT_LEADER) { - rsp.code = -1; + rsp.code = TSDB_CODE_SYN_NOT_LEADER; tmsgSendRsp(&rsp); } else if (ret == TAOS_SYNC_PROPOSE_OTHER_ERROR) { - rsp.code = -2; + rsp.code = TSDB_CODE_SYN_INTERNAL_ERROR; tmsgSendRsp(&rsp); } else if (ret == TAOS_SYNC_PROPOSE_SUCCESS) { // ok diff --git a/source/libs/sync/test/syncApplyMsgTest.cpp b/source/libs/sync/test/syncApplyMsgTest.cpp index 7544613c5d..30019f71e5 100644 --- a/source/libs/sync/test/syncApplyMsgTest.cpp +++ b/source/libs/sync/test/syncApplyMsgTest.cpp @@ -42,8 +42,8 @@ void test1() { void test2() { SyncApplyMsg *pMsg = createMsg(); - uint32_t len = pMsg->bytes; - char * serialized = (char *)taosMemoryMalloc(len); + uint32_t len = pMsg->bytes; + char * serialized = (char *)taosMemoryMalloc(len); syncApplyMsgSerialize(pMsg, serialized, len); SyncApplyMsg *pMsg2 = syncApplyMsgBuild(pMsg->dataLen); syncApplyMsgDeserialize(serialized, len, pMsg2); @@ -56,8 +56,8 @@ void test2() { void test3() { SyncApplyMsg *pMsg = createMsg(); - uint32_t len; - char * serialized = syncApplyMsgSerialize2(pMsg, &len); + uint32_t len; + char * serialized = syncApplyMsgSerialize2(pMsg, &len); SyncApplyMsg *pMsg2 = syncApplyMsgDeserialize2(serialized, len); syncApplyMsgLog2((char *)"test3: syncApplyMsgSerialize2 -> syncApplyMsgDeserialize2 ", pMsg2); @@ -68,7 +68,7 @@ void test3() { void test4() { SyncApplyMsg *pMsg = createMsg(); - SRpcMsg rpcMsg; + SRpcMsg rpcMsg; syncApplyMsg2RpcMsg(pMsg, &rpcMsg); SyncApplyMsg *pMsg2 = (SyncApplyMsg *)taosMemoryMalloc(rpcMsg.contLen); syncApplyMsgFromRpcMsg(&rpcMsg, pMsg2); @@ -81,7 +81,7 @@ void test4() { void test5() { SyncApplyMsg *pMsg = createMsg(); - SRpcMsg rpcMsg; + SRpcMsg rpcMsg; syncApplyMsg2RpcMsg(pMsg, &rpcMsg); SyncApplyMsg *pMsg2 = syncApplyMsgFromRpcMsg2(&rpcMsg); syncApplyMsgLog2((char *)"test5: syncClientRequest2RpcMsg -> syncApplyMsgFromRpcMsg2 ", pMsg2); @@ -93,13 +93,13 @@ void test5() { void test6() { SyncApplyMsg *pMsg = createMsg(); - SRpcMsg rpcMsg; + SRpcMsg rpcMsg; syncApplyMsg2RpcMsg(pMsg, &rpcMsg); SyncApplyMsg *pMsg2 = syncApplyMsgFromRpcMsg2(&rpcMsg); SRpcMsg originalRpcMsg; syncApplyMsg2OriginalRpcMsg(pMsg2, &originalRpcMsg); - syncRpcMsgLog2((char*)"test6", &originalRpcMsg); + syncRpcMsgLog2((char *)"test6", &originalRpcMsg); rpcFreeCont(originalRpcMsg.pCont); rpcFreeCont(rpcMsg.pCont); From 6953688761f0e4674cf63f0471482aaf2530b17c Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Wed, 20 Apr 2022 11:30:18 +0000 Subject: [PATCH 22/51] add commit snapshot --- source/dnode/vnode/inc/vnode.h | 1 + source/dnode/vnode/src/inc/vnodeInt.h | 2 +- source/dnode/vnode/src/vnd/vnodeCommit.c | 15 ++++++++++++++- source/dnode/vnode/src/vnd/vnodeOpen.c | 7 ++++--- source/dnode/vnode/src/vnd/vnodeSvr.c | 2 ++ 5 files changed, 22 insertions(+), 5 deletions(-) diff --git a/source/dnode/vnode/inc/vnode.h b/source/dnode/vnode/inc/vnode.h index 00bcec7be9..08b97ed6c4 100644 --- a/source/dnode/vnode/inc/vnode.h +++ b/source/dnode/vnode/inc/vnode.h @@ -62,6 +62,7 @@ int32_t vnodeGetLoad(SVnode *pVnode, SVnodeLoad *pLoad); int vnodeValidateTableHash(SVnodeCfg *pVnodeOptions, char *tableFName); int64_t vnodeGetSyncHandle(SVnode *pVnode); +void vnodeGetSnapshot(SVnode *pVnode, SSnapshot *pSnapshot); // meta typedef struct SMeta SMeta; // todo: remove diff --git a/source/dnode/vnode/src/inc/vnodeInt.h b/source/dnode/vnode/src/inc/vnodeInt.h index 06ec03741c..0027424829 100644 --- a/source/dnode/vnode/src/inc/vnodeInt.h +++ b/source/dnode/vnode/src/inc/vnodeInt.h @@ -74,7 +74,7 @@ typedef struct { // SVState struct SVState { - int64_t processed; + // int64_t processed; int64_t committed; int64_t applied; }; diff --git a/source/dnode/vnode/src/vnd/vnodeCommit.c b/source/dnode/vnode/src/vnd/vnodeCommit.c index 55e4c5110a..a034b13fbe 100644 --- a/source/dnode/vnode/src/vnd/vnodeCommit.c +++ b/source/dnode/vnode/src/vnd/vnodeCommit.c @@ -158,12 +158,23 @@ int vnodeSyncCommit(SVnode *pVnode) { } static int vnodeCommit(void *arg) { - SVnode *pVnode = (SVnode *)arg; + SVnode *pVnode = (SVnode *)arg; + char dir[TSDB_FILENAME_LEN]; + SVnodeInfo info = {0}; + + snprintf(dir, TSDB_FILENAME_LEN, "%s%s%s", tfsGetPrimaryPath(pVnode->pTfs), TD_DIRSEP, pVnode->path); + info.config = pVnode->config; + info.state.committed = pVnode->state.applied; + info.state.applied = pVnode->state.applied; + + vnodeSaveInfo(dir, &info); // metaCommit(pVnode->pMeta); tqCommit(pVnode->pTq); tsdbCommit(pVnode->pTsdb); + vnodeCommitInfo(dir, &info); + vnodeBufPoolRecycle(pVnode); tsem_post(&(pVnode->canCommit)); return 0; @@ -185,6 +196,7 @@ static int vnodeEncodeState(const void *pObj, SJson *pJson) { const SVState *pState = (SVState *)pObj; if (tjsonAddIntegerToObject(pJson, "commit version", pState->committed) < 0) return -1; + if (tjsonAddIntegerToObject(pJson, "applied version", pState->applied) < 0) return -1; return 0; } @@ -193,6 +205,7 @@ static int vnodeDecodeState(const SJson *pJson, void *pObj) { SVState *pState = (SVState *)pObj; if (tjsonGetNumberValue(pJson, "commit version", pState->committed) < 0) return -1; + if (tjsonGetNumberValue(pJson, "applied version", pState->applied) < 0) return -1; return 0; } diff --git a/source/dnode/vnode/src/vnd/vnodeOpen.c b/source/dnode/vnode/src/vnd/vnodeOpen.c index dee54be5ac..91a754815e 100644 --- a/source/dnode/vnode/src/vnd/vnodeOpen.c +++ b/source/dnode/vnode/src/vnd/vnodeOpen.c @@ -75,8 +75,7 @@ SVnode *vnodeOpen(const char *path, STfs *pTfs, SMsgCb msgCb) { pVnode->path = (char *)&pVnode[1]; strcpy(pVnode->path, path); pVnode->config = info.config; - pVnode->state.committed = info.state.committed; - pVnode->state.processed = pVnode->state.applied = pVnode->state.committed; + pVnode->state = info.state; pVnode->pTfs = pTfs; pVnode->msgCb = msgCb; @@ -170,4 +169,6 @@ void vnodeClose(SVnode *pVnode) { } } -int64_t vnodeGetSyncHandle(SVnode *pVnode) { return pVnode->sync; } \ No newline at end of file +int64_t vnodeGetSyncHandle(SVnode *pVnode) { return pVnode->sync; } + +void vnodeGetSnapshot(SVnode *pVnode, SSnapshot *pSnapshot) { pSnapshot->lastApplyIndex = pVnode->state.committed; } \ No newline at end of file diff --git a/source/dnode/vnode/src/vnd/vnodeSvr.c b/source/dnode/vnode/src/vnd/vnodeSvr.c index fbeb59fe23..56d4ba392c 100644 --- a/source/dnode/vnode/src/vnd/vnodeSvr.c +++ b/source/dnode/vnode/src/vnd/vnodeSvr.c @@ -23,6 +23,7 @@ static int vnodeProcessAlterStbReq(SVnode *pVnode, void *pReq); static int vnodeProcessSubmitReq(SVnode *pVnode, SSubmitReq *pSubmitReq, SRpcMsg *pRsp); int vnodePreprocessWriteReqs(SVnode *pVnode, SArray *pMsgs, int64_t *version) { +#if 0 SNodeMsg *pMsg; SRpcMsg *pRpc; @@ -40,6 +41,7 @@ int vnodePreprocessWriteReqs(SVnode *pVnode, SArray *pMsgs, int64_t *version) { walFsync(pVnode->pWal, false); +#endif return 0; } From 250622d1de6d675d0c3813019d595a224c0011e8 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 20 Apr 2022 19:58:36 +0800 Subject: [PATCH 23/51] snapshot from -1 --- source/dnode/vnode/src/vnd/vnodeOpen.c | 2 ++ source/dnode/vnode/src/vnd/vnodeSync.c | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/source/dnode/vnode/src/vnd/vnodeOpen.c b/source/dnode/vnode/src/vnd/vnodeOpen.c index 91a754815e..ecae050427 100644 --- a/source/dnode/vnode/src/vnd/vnodeOpen.c +++ b/source/dnode/vnode/src/vnd/vnodeOpen.c @@ -35,6 +35,8 @@ int vnodeCreate(const char *path, SVnodeCfg *pCfg, STfs *pTfs) { snprintf(dir, TSDB_FILENAME_LEN, "%s%s%s", tfsGetPrimaryPath(pTfs), TD_DIRSEP, path); info.config = *pCfg; + info.state.committed = -1; + info.state.applied = -1; if (vnodeSaveInfo(dir, &info) < 0 || vnodeCommitInfo(dir, &info) < 0) { vError("vgId: %d failed to save vnode config since %s", pCfg->vgId, tstrerror(terrno)); diff --git a/source/dnode/vnode/src/vnd/vnodeSync.c b/source/dnode/vnode/src/vnd/vnodeSync.c index 93c40aa019..546e925106 100644 --- a/source/dnode/vnode/src/vnd/vnodeSync.c +++ b/source/dnode/vnode/src/vnd/vnodeSync.c @@ -85,9 +85,15 @@ int32_t vnodeSendMsg(void *rpcHandle, const SEpSet *pEpSet, SRpcMsg *pMsg) { } int32_t vnodeSyncGetSnapshotCb(struct SSyncFSM *pFsm, SSnapshot *pSnapshot) { + SVnode *pVnode = (SVnode *)(pFsm->data); + vnodeGetSnapshot(pVnode, pSnapshot); + + /* pSnapshot->data = NULL; pSnapshot->lastApplyIndex = 0; pSnapshot->lastApplyTerm = 0; + */ + return 0; } From a6ee17fd43365cb5db1318c5397059653d071fa5 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 20 Apr 2022 20:26:10 +0800 Subject: [PATCH 24/51] add syncElectTest1.cpp --- source/libs/sync/test/CMakeLists.txt | 14 +++ source/libs/sync/test/syncElectTest1.cpp | 129 +++++++++++++++++++++++ 2 files changed, 143 insertions(+) create mode 100644 source/libs/sync/test/syncElectTest1.cpp diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index 5d43d47358..2500cd3c7b 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -28,6 +28,7 @@ add_executable(syncRpcMsgTest "") add_executable(syncPingTimerTest2 "") add_executable(syncPingSelfTest "") add_executable(syncElectTest "") +add_executable(syncElectTest1 "") add_executable(syncElectTest2 "") add_executable(syncElectTest3 "") add_executable(syncEncodeTest "") @@ -164,6 +165,10 @@ target_sources(syncElectTest PRIVATE "syncElectTest.cpp" ) +target_sources(syncElectTest1 + PRIVATE + "syncElectTest1.cpp" +) target_sources(syncElectTest2 PRIVATE "syncElectTest2.cpp" @@ -372,6 +377,11 @@ target_include_directories(syncElectTest "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) +target_include_directories(syncElectTest1 + PUBLIC + "${CMAKE_SOURCE_DIR}/include/libs/sync" + "${CMAKE_CURRENT_SOURCE_DIR}/../inc" +) target_include_directories(syncElectTest2 PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/sync" @@ -564,6 +574,10 @@ target_link_libraries(syncElectTest sync gtest_main ) +target_link_libraries(syncElectTest1 + sync + gtest_main +) target_link_libraries(syncElectTest2 sync gtest_main diff --git a/source/libs/sync/test/syncElectTest1.cpp b/source/libs/sync/test/syncElectTest1.cpp new file mode 100644 index 0000000000..feea95af82 --- /dev/null +++ b/source/libs/sync/test/syncElectTest1.cpp @@ -0,0 +1,129 @@ +#include +#include +#include "syncEnv.h" +#include "syncIO.h" +#include "syncInt.h" +#include "syncRaftLog.h" +#include "syncRaftStore.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"); +} + +uint16_t ports[] = {7010, 7110, 7210, 7310, 7410}; +int32_t replicaNum = 1; +int32_t myIndex = 0; + +SRaftId ids[TSDB_MAX_REPLICA]; +SSyncInfo syncInfo; +SSyncFSM* pFsm; +SWal* pWal; +SSyncNode* gSyncNode; +const char* pDir = "./syncElectTest1"; + +SSyncNode* syncNodeInit() { + syncInfo.vgId = 1234; + syncInfo.rpcClient = gSyncIO->clientRpc; + syncInfo.FpSendMsg = syncIOSendMsg; + syncInfo.queue = gSyncIO->pMsgQ; + syncInfo.FpEqMsg = syncIOEqMsg; + syncInfo.pFsm = pFsm; + snprintf(syncInfo.path, sizeof(syncInfo.path), "%s_sync_%d", pDir, myIndex); + + int code = walInit(); + assert(code == 0); + SWalCfg walCfg; + memset(&walCfg, 0, sizeof(SWalCfg)); + walCfg.vgId = syncInfo.vgId; + walCfg.fsyncPeriod = 1000; + walCfg.retentionPeriod = 1000; + walCfg.rollPeriod = 1000; + walCfg.retentionSize = 1000; + walCfg.segSize = 1000; + walCfg.level = TAOS_WAL_FSYNC; + + char tmpdir[128]; + snprintf(tmpdir, sizeof(tmpdir), "%s_wal_%d", pDir, myIndex); + pWal = walOpen(tmpdir, &walCfg); + assert(pWal != NULL); + + syncInfo.pWal = pWal; + + SSyncCfg* pCfg = &syncInfo.syncCfg; + pCfg->myIndex = myIndex; + pCfg->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); + } + + SSyncNode* pSyncNode = syncNodeOpen(&syncInfo); + assert(pSyncNode != NULL); + + 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->FpOnSyncTimeout = pSyncNode->FpOnTimeout; + gSyncIO->pSyncNode = pSyncNode; + + syncNodeStart(pSyncNode); + + 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) { + // taosInitLog((char *)"syncTest.log", 100000, 10); + tsAsyncLog = 0; + sDebugFlag = 143 + 64; + + myIndex = 0; + if (argc >= 2) { + myIndex = atoi(argv[1]); + } + + int32_t ret = syncIOStart((char*)"127.0.0.1", ports[myIndex]); + assert(ret == 0); + + ret = syncEnvStart(); + assert(ret == 0); + + gSyncNode = syncInitTest(); + assert(gSyncNode != NULL); + syncNodeLog2((char*)"", gSyncNode); + + initRaftId(gSyncNode); + + //--------------------------- + while (1) { + char* s = syncNode2SimpleStr(gSyncNode); + sTrace("%s", s); + taosMemoryFree(s); + taosMsleep(1000); + } + + return 0; +} From bbe85695cfd02d380761b4d2d1363e1e7b5b168c Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 20 Apr 2022 20:29:20 +0800 Subject: [PATCH 25/51] add syncMaybeAdvanceCommitIndex, maybe only one replica --- source/libs/sync/src/syncMain.c | 1 + 1 file changed, 1 insertion(+) diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index 6d51cee14e..e260ef4654 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -890,6 +890,7 @@ void syncNodeCandidate2Leader(SSyncNode* pSyncNode) { // use this now syncNodeAppendNoop(pSyncNode); + syncMaybeAdvanceCommitIndex(pSyncNode); // maybe only one replica // do not use this // syncNodeEqNoop(pSyncNode); From 0df5100836c0246cdd06defd99db581c1fbf00ac Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Thu, 21 Apr 2022 11:25:17 +0800 Subject: [PATCH 26/51] add syncLogStoreCheck.cpp --- source/libs/sync/test/syncLogStoreCheck.cpp | 96 +++++++-------------- 1 file changed, 33 insertions(+), 63 deletions(-) diff --git a/source/libs/sync/test/syncLogStoreCheck.cpp b/source/libs/sync/test/syncLogStoreCheck.cpp index b77a744c30..ae725ac5a2 100644 --- a/source/libs/sync/test/syncLogStoreCheck.cpp +++ b/source/libs/sync/test/syncLogStoreCheck.cpp @@ -16,90 +16,60 @@ void logTest() { sFatal("--- sync log test: fatal"); } -uint16_t ports[] = {7010, 7110, 7210, 7310, 7410}; -int32_t replicaNum = 1; -int32_t myIndex = 0; - -SRaftId ids[TSDB_MAX_REPLICA]; -SSyncInfo syncInfo; -SSyncFSM* pFsm; -SWal* pWal; -SSyncNode* pSyncNode; - -SSyncNode* syncNodeInit(const char* path) { - syncInfo.vgId = 1234; - syncInfo.rpcClient = gSyncIO->clientRpc; - syncInfo.FpSendMsg = syncIOSendMsg; - syncInfo.queue = gSyncIO->pMsgQ; - syncInfo.FpEqMsg = syncIOEqMsg; - syncInfo.pFsm = pFsm; - snprintf(syncInfo.path, sizeof(syncInfo.path), "%s", "./log_check"); - +void init() { int code = walInit(); assert(code == 0); +} + +void cleanup() { walCleanUp(); } + +SWal* createWal(char* path, int32_t vgId) { SWalCfg walCfg; memset(&walCfg, 0, sizeof(SWalCfg)); - walCfg.vgId = syncInfo.vgId; + walCfg.vgId = vgId; walCfg.fsyncPeriod = 1000; walCfg.retentionPeriod = 1000; walCfg.rollPeriod = 1000; walCfg.retentionSize = 1000; walCfg.segSize = 1000; walCfg.level = TAOS_WAL_FSYNC; - pWal = walOpen(path, &walCfg); + SWal* pWal = walOpen(path, &walCfg); assert(pWal != NULL); + return pWal; +} - syncInfo.pWal = pWal; - - SSyncCfg* pCfg = &syncInfo.syncCfg; - pCfg->myIndex = myIndex; - pCfg->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); - - 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->FpOnSyncTimeout = pSyncNode->FpOnTimeout; - gSyncIO->pSyncNode = pSyncNode; - +SSyncNode* createSyncNode(SWal* pWal) { + SSyncNode* pSyncNode = (SSyncNode*)taosMemoryMalloc(sizeof(SSyncNode)); + memset(pSyncNode, 0, sizeof(SSyncNode)); + pSyncNode->pWal = pWal; return pSyncNode; } -SSyncNode* logStoreCheck(const char* path) { return syncNodeInit(path); } +void usage(char* exe) { printf("usage: %s path vgId \n", exe); } int main(int argc, char** argv) { - // taosInitLog((char *)"syncTest.log", 100000, 10); - tsAsyncLog = 0; - sDebugFlag = 143 + 64; - - myIndex = 0; - if (argc >= 2) { - myIndex = atoi(argv[1]); + if (argc != 3) { + usage(argv[0]); + exit(-1); } + char* path = argv[1]; + int32_t vgId = atoi(argv[2]); - int32_t ret = syncIOStart((char*)"127.0.0.1", ports[myIndex]); - assert(ret == 0); - - ret = syncEnvStart(); - assert(ret == 0); - - pSyncNode = logStoreCheck(argv[1]); + init(); + SWal* pWal = createWal(path, vgId); + assert(pWal != NULL); + SSyncNode* pSyncNode = createSyncNode(pWal); assert(pSyncNode != NULL); - logStoreLog2((char*)"logStoreCheck", pSyncNode->pLogStore); + SSyncLogStore* pLog = logStoreCreate(pSyncNode); + assert(pLog != NULL); + logStorePrint2((char*)"==syncLogStoreCheck==", pLog); + + walClose(pWal); + logStoreDestory(pLog); + taosMemoryFree(pSyncNode); + + cleanup(); return 0; } From 672be6f4ff9125e32b0f051bef07699cbd59af9b Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Thu, 21 Apr 2022 11:35:22 +0800 Subject: [PATCH 27/51] del trace log flag --- source/libs/sync/src/syncEnv.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/source/libs/sync/src/syncEnv.c b/source/libs/sync/src/syncEnv.c index 4213d7b45d..941478c9d6 100644 --- a/source/libs/sync/src/syncEnv.c +++ b/source/libs/sync/src/syncEnv.c @@ -27,8 +27,6 @@ static void syncEnvTick(void *param, void *tmrId); // -------------------------------- int32_t syncEnvStart() { - sDebugFlag = 207; - int32_t ret = 0; taosSeedRand(taosGetTimestampSec()); gSyncEnv = doSyncEnvStart(gSyncEnv); From 52b6a9c0588f78f42ea2fac481c300856727da33 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Thu, 21 Apr 2022 13:59:42 +0800 Subject: [PATCH 28/51] add syncElectTest, config replicaNum myIndex --- source/libs/sync/test/syncElectTest.cpp | 101 ++++++++++++------------ 1 file changed, 49 insertions(+), 52 deletions(-) diff --git a/source/libs/sync/test/syncElectTest.cpp b/source/libs/sync/test/syncElectTest.cpp index 39b5eba1aa..f58b6b670b 100644 --- a/source/libs/sync/test/syncElectTest.cpp +++ b/source/libs/sync/test/syncElectTest.cpp @@ -3,9 +3,8 @@ #include "syncEnv.h" #include "syncIO.h" #include "syncInt.h" -#include "syncRaftLog.h" -#include "syncRaftStore.h" #include "syncUtil.h" +#include "wal.h" void logTest() { sTrace("--- sync log test: trace"); @@ -16,43 +15,41 @@ void logTest() { sFatal("--- sync log test: fatal"); } -uint16_t ports[] = {7010, 7110, 7210, 7310, 7410}; -int32_t replicaNum = 3; -int32_t myIndex = 0; - -SRaftId ids[TSDB_MAX_REPLICA]; -SSyncInfo syncInfo; -SSyncFSM* pFsm; -SWal* pWal; -SSyncNode* gSyncNode; -const char* pDir = "./syncElectTest"; - -SSyncNode* syncNodeInit() { - syncInfo.vgId = 1234; - syncInfo.rpcClient = gSyncIO->clientRpc; - syncInfo.FpSendMsg = syncIOSendMsg; - syncInfo.queue = gSyncIO->pMsgQ; - syncInfo.FpEqMsg = syncIOEqMsg; - syncInfo.pFsm = pFsm; - snprintf(syncInfo.path, sizeof(syncInfo.path), "%s_sync_%d", pDir, myIndex); +uint16_t gPorts[] = {7010, 7110, 7210, 7310, 7410}; +const char* gDir = "./syncElectTest"; +int32_t gVgId = 1234; +void init() { int code = walInit(); assert(code == 0); +} + +void cleanup() { walCleanUp(); } + +SWal* createWal(char* path, int32_t vgId) { SWalCfg walCfg; memset(&walCfg, 0, sizeof(SWalCfg)); - walCfg.vgId = syncInfo.vgId; + walCfg.vgId = vgId; walCfg.fsyncPeriod = 1000; walCfg.retentionPeriod = 1000; walCfg.rollPeriod = 1000; walCfg.retentionSize = 1000; walCfg.segSize = 1000; walCfg.level = TAOS_WAL_FSYNC; - - char tmpdir[128]; - snprintf(tmpdir, sizeof(tmpdir), "%s_wal_%d", pDir, myIndex); - pWal = walOpen(tmpdir, &walCfg); + SWal* pWal = walOpen(path, &walCfg); assert(pWal != NULL); + return pWal; +} +SSyncNode* createSyncNode(int32_t replicaNum, int32_t myIndex, int32_t vgId, SWal* pWal, char* path) { + SSyncInfo syncInfo; + syncInfo.vgId = vgId; + syncInfo.rpcClient = gSyncIO->clientRpc; + syncInfo.FpSendMsg = syncIOSendMsg; + syncInfo.queue = gSyncIO->pMsgQ; + syncInfo.FpEqMsg = syncIOEqMsg; + syncInfo.pFsm = NULL; + snprintf(syncInfo.path, sizeof(syncInfo.path), "%s_sync_replica%d_index%d", path, replicaNum, myIndex); syncInfo.pWal = pWal; SSyncCfg* pCfg = &syncInfo.syncCfg; @@ -60,9 +57,9 @@ SSyncNode* syncNodeInit() { pCfg->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); + pCfg->nodeInfo[i].nodePort = gPorts[i]; + taosGetFqdn(pCfg->nodeInfo[i].nodeFqdn); + // snprintf(pCfg->nodeInfo[i].nodeFqdn, sizeof(pCfg->nodeInfo[i].nodeFqdn), "%s", "127.0.0.1"); } SSyncNode* pSyncNode = syncNodeOpen(&syncInfo); @@ -84,46 +81,46 @@ SSyncNode* syncNodeInit() { 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); - } -} +void usage(char* exe) { printf("usage: %s replicaNum myIndex \n", exe); } int main(int argc, char** argv) { - // taosInitLog((char *)"syncTest.log", 100000, 10); tsAsyncLog = 0; - sDebugFlag = 143 + 64; - - myIndex = 0; - if (argc >= 2) { - myIndex = atoi(argv[1]); + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; + if (argc != 3) { + usage(argv[0]); + exit(-1); } + int32_t replicaNum = atoi(argv[1]); + int32_t myIndex = atoi(argv[2]); - int32_t ret = syncIOStart((char*)"127.0.0.1", ports[myIndex]); + assert(replicaNum >= 1 && replicaNum <= 5); + assert(myIndex >= 0 && myIndex < replicaNum); + + init(); + int32_t ret = syncIOStart((char*)"127.0.0.1", gPorts[myIndex]); assert(ret == 0); - ret = syncEnvStart(); assert(ret == 0); - gSyncNode = syncInitTest(); - assert(gSyncNode != NULL); - syncNodeLog2((char*)"", gSyncNode); + char walPath[128]; + snprintf(walPath, sizeof(walPath), "%s_wal_replica%d_index%d", gDir, replicaNum, myIndex); + SWal* pWal = createWal(walPath, gVgId); - initRaftId(gSyncNode); + SSyncNode* pSyncNode = createSyncNode(replicaNum, myIndex, gVgId, pWal, (char*)gDir); + assert(pSyncNode != NULL); + syncNodeLog2((char*)"==syncElectTest==", pSyncNode); //--------------------------- while (1) { - char* s = syncNode2SimpleStr(gSyncNode); + char* s = syncNode2SimpleStr(pSyncNode); sTrace("%s", s); taosMemoryFree(s); taosMsleep(1000); } + syncNodeClose(pSyncNode); + walClose(pWal); + syncIOStop(); + cleanup(); return 0; } From a6b949831aba690fe9322009b3f7723a1b8f8186 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Thu, 21 Apr 2022 14:56:41 +0800 Subject: [PATCH 29/51] delete syncElectTest 1-3, add syncReplicateTest.cpp --- source/libs/sync/test/CMakeLists.txt | 42 ---- source/libs/sync/test/syncElectTest1.cpp | 129 ----------- source/libs/sync/test/syncElectTest2.cpp | 139 ------------ source/libs/sync/test/syncElectTest3.cpp | 142 ------------ source/libs/sync/test/syncReplicateTest.cpp | 239 +++++++++++--------- 5 files changed, 128 insertions(+), 563 deletions(-) delete mode 100644 source/libs/sync/test/syncElectTest1.cpp delete mode 100644 source/libs/sync/test/syncElectTest2.cpp delete mode 100644 source/libs/sync/test/syncElectTest3.cpp diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index 2500cd3c7b..14d4401233 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -28,9 +28,6 @@ add_executable(syncRpcMsgTest "") add_executable(syncPingTimerTest2 "") add_executable(syncPingSelfTest "") add_executable(syncElectTest "") -add_executable(syncElectTest1 "") -add_executable(syncElectTest2 "") -add_executable(syncElectTest3 "") add_executable(syncEncodeTest "") add_executable(syncWriteTest "") add_executable(syncReplicateTest "") @@ -165,18 +162,6 @@ target_sources(syncElectTest PRIVATE "syncElectTest.cpp" ) -target_sources(syncElectTest1 - PRIVATE - "syncElectTest1.cpp" -) -target_sources(syncElectTest2 - PRIVATE - "syncElectTest2.cpp" -) -target_sources(syncElectTest3 - PRIVATE - "syncElectTest3.cpp" -) target_sources(syncEncodeTest PRIVATE "syncEncodeTest.cpp" @@ -377,21 +362,6 @@ target_include_directories(syncElectTest "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) -target_include_directories(syncElectTest1 - PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" - "${CMAKE_CURRENT_SOURCE_DIR}/../inc" -) -target_include_directories(syncElectTest2 - PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" - "${CMAKE_CURRENT_SOURCE_DIR}/../inc" -) -target_include_directories(syncElectTest3 - PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" - "${CMAKE_CURRENT_SOURCE_DIR}/../inc" -) target_include_directories(syncEncodeTest PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/sync" @@ -574,18 +544,6 @@ target_link_libraries(syncElectTest sync gtest_main ) -target_link_libraries(syncElectTest1 - sync - gtest_main -) -target_link_libraries(syncElectTest2 - sync - gtest_main -) -target_link_libraries(syncElectTest3 - sync - gtest_main -) target_link_libraries(syncEncodeTest sync gtest_main diff --git a/source/libs/sync/test/syncElectTest1.cpp b/source/libs/sync/test/syncElectTest1.cpp deleted file mode 100644 index feea95af82..0000000000 --- a/source/libs/sync/test/syncElectTest1.cpp +++ /dev/null @@ -1,129 +0,0 @@ -#include -#include -#include "syncEnv.h" -#include "syncIO.h" -#include "syncInt.h" -#include "syncRaftLog.h" -#include "syncRaftStore.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"); -} - -uint16_t ports[] = {7010, 7110, 7210, 7310, 7410}; -int32_t replicaNum = 1; -int32_t myIndex = 0; - -SRaftId ids[TSDB_MAX_REPLICA]; -SSyncInfo syncInfo; -SSyncFSM* pFsm; -SWal* pWal; -SSyncNode* gSyncNode; -const char* pDir = "./syncElectTest1"; - -SSyncNode* syncNodeInit() { - syncInfo.vgId = 1234; - syncInfo.rpcClient = gSyncIO->clientRpc; - syncInfo.FpSendMsg = syncIOSendMsg; - syncInfo.queue = gSyncIO->pMsgQ; - syncInfo.FpEqMsg = syncIOEqMsg; - syncInfo.pFsm = pFsm; - snprintf(syncInfo.path, sizeof(syncInfo.path), "%s_sync_%d", pDir, myIndex); - - int code = walInit(); - assert(code == 0); - SWalCfg walCfg; - memset(&walCfg, 0, sizeof(SWalCfg)); - walCfg.vgId = syncInfo.vgId; - walCfg.fsyncPeriod = 1000; - walCfg.retentionPeriod = 1000; - walCfg.rollPeriod = 1000; - walCfg.retentionSize = 1000; - walCfg.segSize = 1000; - walCfg.level = TAOS_WAL_FSYNC; - - char tmpdir[128]; - snprintf(tmpdir, sizeof(tmpdir), "%s_wal_%d", pDir, myIndex); - pWal = walOpen(tmpdir, &walCfg); - assert(pWal != NULL); - - syncInfo.pWal = pWal; - - SSyncCfg* pCfg = &syncInfo.syncCfg; - pCfg->myIndex = myIndex; - pCfg->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); - } - - SSyncNode* pSyncNode = syncNodeOpen(&syncInfo); - assert(pSyncNode != NULL); - - 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->FpOnSyncTimeout = pSyncNode->FpOnTimeout; - gSyncIO->pSyncNode = pSyncNode; - - syncNodeStart(pSyncNode); - - 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) { - // taosInitLog((char *)"syncTest.log", 100000, 10); - tsAsyncLog = 0; - sDebugFlag = 143 + 64; - - myIndex = 0; - if (argc >= 2) { - myIndex = atoi(argv[1]); - } - - int32_t ret = syncIOStart((char*)"127.0.0.1", ports[myIndex]); - assert(ret == 0); - - ret = syncEnvStart(); - assert(ret == 0); - - gSyncNode = syncInitTest(); - assert(gSyncNode != NULL); - syncNodeLog2((char*)"", gSyncNode); - - initRaftId(gSyncNode); - - //--------------------------- - while (1) { - char* s = syncNode2SimpleStr(gSyncNode); - sTrace("%s", s); - taosMemoryFree(s); - taosMsleep(1000); - } - - return 0; -} diff --git a/source/libs/sync/test/syncElectTest2.cpp b/source/libs/sync/test/syncElectTest2.cpp deleted file mode 100644 index d569b47fb1..0000000000 --- a/source/libs/sync/test/syncElectTest2.cpp +++ /dev/null @@ -1,139 +0,0 @@ -#include -#include -#include "syncEnv.h" -#include "syncIO.h" -#include "syncInt.h" -#include "syncRaftLog.h" -#include "syncRaftStore.h" -#include "syncUtil.h" -#include "tref.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"); -} - -uint16_t ports[] = {7010, 7110, 7210, 7310, 7410}; -int32_t replicaNum = 3; -int32_t myIndex = 0; - -SRaftId ids[TSDB_MAX_REPLICA]; -SSyncInfo syncInfo; -SSyncFSM* pFsm; -SWal* pWal; -const char* pDir = "./syncElectTest2"; - -int64_t syncNodeInit() { - syncInfo.vgId = 1234; - syncInfo.rpcClient = gSyncIO->clientRpc; - syncInfo.FpSendMsg = syncIOSendMsg; - syncInfo.queue = gSyncIO->pMsgQ; - syncInfo.FpEqMsg = syncIOEqMsg; - syncInfo.pFsm = pFsm; - snprintf(syncInfo.path, sizeof(syncInfo.path), "%s_sync_%d", pDir, myIndex); - - int code = walInit(); - assert(code == 0); - SWalCfg walCfg; - memset(&walCfg, 0, sizeof(SWalCfg)); - walCfg.vgId = syncInfo.vgId; - walCfg.fsyncPeriod = 1000; - walCfg.retentionPeriod = 1000; - walCfg.rollPeriod = 1000; - walCfg.retentionSize = 1000; - walCfg.segSize = 1000; - walCfg.level = TAOS_WAL_FSYNC; - - char tmpdir[128]; - snprintf(tmpdir, sizeof(tmpdir), "%s_wal_%d", pDir, myIndex); - pWal = walOpen(tmpdir, &walCfg); - assert(pWal != NULL); - - syncInfo.pWal = pWal; - - SSyncCfg* pCfg = &syncInfo.syncCfg; - pCfg->myIndex = myIndex; - pCfg->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); - } - - int64_t rid = syncOpen(&syncInfo); - assert(rid > 0); - syncStart(rid); - - SSyncNode* pSyncNode = (SSyncNode*)syncNodeAcquire(rid); - assert(pSyncNode != NULL); - - 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->FpOnSyncTimeout = pSyncNode->FpOnTimeout; - gSyncIO->pSyncNode = pSyncNode; - - syncNodeStart(pSyncNode); - - syncNodeRelease(pSyncNode); - - return rid; -} - -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) { - // taosInitLog((char *)"syncTest.log", 100000, 10); - tsAsyncLog = 0; - sDebugFlag = 143 + 64; - - myIndex = 0; - if (argc >= 2) { - myIndex = atoi(argv[1]); - } - - int32_t ret = syncIOStart((char*)"127.0.0.1", ports[myIndex]); - assert(ret == 0); - - ret = syncInit(); - assert(ret == 0); - - int64_t rid = syncNodeInit(); - assert(rid > 0); - - SSyncNode* pSyncNode = (SSyncNode*)syncNodeAcquire(rid); - assert(pSyncNode != NULL); - - syncNodeLog2((char*)"", pSyncNode); - initRaftId(pSyncNode); - - //--------------------------- - while (1) { - sTrace( - "elect sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, electTimerMS:%d", - pSyncNode->state, syncUtilState2String(pSyncNode->state), pSyncNode->pRaftStore->currentTerm, - pSyncNode->electTimerLogicClock, pSyncNode->electTimerLogicClockUser, pSyncNode->electTimerMS); - taosMsleep(1000); - } - - syncNodeRelease(pSyncNode); - - return 0; -} diff --git a/source/libs/sync/test/syncElectTest3.cpp b/source/libs/sync/test/syncElectTest3.cpp deleted file mode 100644 index 59e1b993d8..0000000000 --- a/source/libs/sync/test/syncElectTest3.cpp +++ /dev/null @@ -1,142 +0,0 @@ -#include -#include -#include "syncEnv.h" -#include "syncIO.h" -#include "syncInt.h" -#include "syncRaftLog.h" -#include "syncRaftStore.h" -#include "syncUtil.h" -#include "tref.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"); -} - -uint16_t ports[] = {7010, 7110, 7210, 7310, 7410}; -int32_t replicaNum = 3; -int32_t myIndex = 0; - -SRaftId ids[TSDB_MAX_REPLICA]; -SSyncInfo syncInfo; -SSyncFSM* pFsm; -SWal* pWal; -const char* pDir = "./syncElectTest3"; - -int64_t syncNodeInit() { - syncInfo.vgId = 1234; - syncInfo.rpcClient = gSyncIO->clientRpc; - syncInfo.FpSendMsg = syncIOSendMsg; - syncInfo.queue = gSyncIO->pMsgQ; - syncInfo.FpEqMsg = syncIOEqMsg; - syncInfo.pFsm = pFsm; - snprintf(syncInfo.path, sizeof(syncInfo.path), "%s_sync_%d", pDir, myIndex); - - int code = walInit(); - assert(code == 0); - SWalCfg walCfg; - memset(&walCfg, 0, sizeof(SWalCfg)); - walCfg.vgId = syncInfo.vgId; - walCfg.fsyncPeriod = 1000; - walCfg.retentionPeriod = 1000; - walCfg.rollPeriod = 1000; - walCfg.retentionSize = 1000; - walCfg.segSize = 1000; - walCfg.level = TAOS_WAL_FSYNC; - - char tmpdir[128]; - snprintf(tmpdir, sizeof(tmpdir), "%s_wal_%d", pDir, myIndex); - pWal = walOpen(tmpdir, &walCfg); - assert(pWal != NULL); - - syncInfo.pWal = pWal; - - SSyncCfg* pCfg = &syncInfo.syncCfg; - pCfg->myIndex = myIndex; - pCfg->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); - } - - int64_t rid = syncOpen(&syncInfo); - assert(rid > 0); - syncStart(rid); - - SSyncNode* pSyncNode = (SSyncNode*)syncNodeAcquire(rid); - assert(pSyncNode != NULL); - - pSyncNode->hbBaseLine = 500; - pSyncNode->electBaseLine = 1500; - - 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->FpOnSyncTimeout = pSyncNode->FpOnTimeout; - gSyncIO->pSyncNode = pSyncNode; - - syncNodeStart(pSyncNode); - - syncNodeRelease(pSyncNode); - - return rid; -} - -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) { - // taosInitLog((char *)"syncTest.log", 100000, 10); - tsAsyncLog = 0; - sDebugFlag = 143 + 64; - - myIndex = 0; - if (argc >= 2) { - myIndex = atoi(argv[1]); - } - - int32_t ret = syncIOStart((char*)"127.0.0.1", ports[myIndex]); - assert(ret == 0); - - ret = syncInit(); - assert(ret == 0); - - int64_t rid = syncNodeInit(); - assert(rid > 0); - - SSyncNode* pSyncNode = (SSyncNode*)syncNodeAcquire(rid); - assert(pSyncNode != NULL); - - syncNodeLog2((char*)"", pSyncNode); - initRaftId(pSyncNode); - - //--------------------------- - while (1) { - sTrace( - "elect sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, electTimerMS:%d", - pSyncNode->state, syncUtilState2String(pSyncNode->state), pSyncNode->pRaftStore->currentTerm, - pSyncNode->electTimerLogicClock, pSyncNode->electTimerLogicClockUser, pSyncNode->electTimerMS); - taosMsleep(1000); - } - - syncNodeRelease(pSyncNode); - - return 0; -} diff --git a/source/libs/sync/test/syncReplicateTest.cpp b/source/libs/sync/test/syncReplicateTest.cpp index 9b5aaf5c18..14132848b4 100644 --- a/source/libs/sync/test/syncReplicateTest.cpp +++ b/source/libs/sync/test/syncReplicateTest.cpp @@ -3,11 +3,8 @@ #include "syncEnv.h" #include "syncIO.h" #include "syncInt.h" -#include "syncMessage.h" -#include "syncRaftEntry.h" -#include "syncRaftLog.h" -#include "syncRaftStore.h" #include "syncUtil.h" +#include "wal.h" void logTest() { sTrace("--- sync log test: trace"); @@ -18,184 +15,204 @@ void logTest() { sFatal("--- sync log test: fatal"); } -uint16_t ports[] = {7010, 7110, 7210, 7310, 7410}; -int32_t replicaNum = 3; -int32_t myIndex = 0; +uint16_t gPorts[] = {7010, 7110, 7210, 7310, 7410}; +const char* gDir = "./syncReplicateTest"; +int32_t gVgId = 1234; +SyncIndex gSnapshotLastApplyIndex; -SRaftId ids[TSDB_MAX_REPLICA]; -SSyncInfo syncInfo; -SSyncFSM * pFsm; -SWal * pWal; -SSyncNode *gSyncNode; +void init() { + int code = walInit(); + assert(code == 0); -void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { - char logBuf[256]; - snprintf(logBuf, sizeof(logBuf), "==callback== ==CommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", - pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); - syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); + code = syncInit(); + assert(code == 0); } -void PreCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { +void cleanup() { walCleanUp(); } + +void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { + SyncIndex beginIndex = SYNC_INDEX_INVALID; + if (pFsm->FpGetSnapshot != NULL) { + SSnapshot snapshot; + pFsm->FpGetSnapshot(pFsm, &snapshot); + beginIndex = snapshot.lastApplyIndex; + } + + if (cbMeta.index > beginIndex) { + char logBuf[256]; + snprintf(logBuf, sizeof(logBuf), "==callback== ==CommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", + pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); + syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); + } else { + sTrace("==callback== ==CommitCb== do not apply again %ld", cbMeta.index); + } +} + +void PreCommitCb(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta) { char logBuf[256]; snprintf(logBuf, sizeof(logBuf), "==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); - syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); + syncRpcMsgLog2(logBuf, (SRpcMsg*)pMsg); } -void RollBackCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { +void RollBackCb(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta) { char logBuf[256]; snprintf(logBuf, sizeof(logBuf), "==callback== ==RollBackCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); - syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); + syncRpcMsgLog2(logBuf, (SRpcMsg*)pMsg); } -void initFsm() { - pFsm = (SSyncFSM *)taosMemoryMalloc(sizeof(SSyncFSM)); +int32_t GetSnapshotCb(struct SSyncFSM* pFsm, SSnapshot* pSnapshot) { + pSnapshot->data = NULL; + pSnapshot->lastApplyIndex = gSnapshotLastApplyIndex; + pSnapshot->lastApplyTerm = 100; + return 0; +} + +SSyncFSM* createFsm() { + SSyncFSM* pFsm = (SSyncFSM*)taosMemoryMalloc(sizeof(SSyncFSM)); pFsm->FpCommitCb = CommitCb; pFsm->FpPreCommitCb = PreCommitCb; pFsm->FpRollBackCb = RollBackCb; + pFsm->FpGetSnapshot = GetSnapshotCb; + return pFsm; } -SSyncNode *syncNodeInit() { - syncInfo.vgId = 1234; - syncInfo.rpcClient = gSyncIO->clientRpc; - syncInfo.FpSendMsg = syncIOSendMsg; - syncInfo.queue = gSyncIO->pMsgQ; - syncInfo.FpEqMsg = syncIOEqMsg; - syncInfo.pFsm = pFsm; - snprintf(syncInfo.path, sizeof(syncInfo.path), "./replicate_test_%d", myIndex); - - int code = walInit(); - assert(code == 0); +SWal* createWal(char* path, int32_t vgId) { SWalCfg walCfg; memset(&walCfg, 0, sizeof(SWalCfg)); - walCfg.vgId = syncInfo.vgId; + walCfg.vgId = vgId; walCfg.fsyncPeriod = 1000; walCfg.retentionPeriod = 1000; walCfg.rollPeriod = 1000; walCfg.retentionSize = 1000; walCfg.segSize = 1000; walCfg.level = TAOS_WAL_FSYNC; - - char tmpdir[128]; - snprintf(tmpdir, sizeof(tmpdir), "./replicate_test_wal_%d", myIndex); - pWal = walOpen(tmpdir, &walCfg); + SWal* pWal = walOpen(path, &walCfg); assert(pWal != NULL); + return pWal; +} +int64_t createSyncNode(int32_t replicaNum, int32_t myIndex, int32_t vgId, SWal* pWal, char* path) { + SSyncInfo syncInfo; + syncInfo.vgId = vgId; + syncInfo.rpcClient = gSyncIO->clientRpc; + syncInfo.FpSendMsg = syncIOSendMsg; + syncInfo.queue = gSyncIO->pMsgQ; + syncInfo.FpEqMsg = syncIOEqMsg; + syncInfo.pFsm = createFsm(); + snprintf(syncInfo.path, sizeof(syncInfo.path), "%s_sync_replica%d_index%d", path, replicaNum, myIndex); syncInfo.pWal = pWal; - SSyncCfg *pCfg = &syncInfo.syncCfg; + SSyncCfg* pCfg = &syncInfo.syncCfg; pCfg->myIndex = myIndex; pCfg->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); + pCfg->nodeInfo[i].nodePort = gPorts[i]; + taosGetFqdn(pCfg->nodeInfo[i].nodeFqdn); + // snprintf(pCfg->nodeInfo[i].nodeFqdn, sizeof(pCfg->nodeInfo[i].nodeFqdn), "%s", "127.0.0.1"); } - SSyncNode *pSyncNode = syncNodeOpen(&syncInfo); + int64_t rid = syncOpen(&syncInfo); + assert(rid > 0); + syncStart(rid); + + SSyncNode* pSyncNode = (SSyncNode*)syncNodeAcquire(rid); assert(pSyncNode != NULL); gSyncIO->FpOnSyncPing = pSyncNode->FpOnPing; - gSyncIO->FpOnSyncClientRequest = pSyncNode->FpOnClientRequest; 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->FpOnSyncTimeout = pSyncNode->FpOnTimeout; gSyncIO->pSyncNode = pSyncNode; - return pSyncNode; + syncNodeStart(pSyncNode); + + return rid; } -SSyncNode *syncInitTest() { return syncNodeInit(); } +void usage(char* exe) { printf("usage: %s replicaNum myIndex lastApplyIndex writeRecordNum \n", exe); } -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); - } -} - -SRpcMsg *step0(int i) { +SRpcMsg *createRpcMsg(int i, int count, int myIndex) { SRpcMsg *pMsg = (SRpcMsg *)taosMemoryMalloc(sizeof(SRpcMsg)); memset(pMsg, 0, sizeof(SRpcMsg)); pMsg->msgType = 9999; pMsg->contLen = 128; pMsg->pCont = taosMemoryMalloc(pMsg->contLen); - snprintf((char *)(pMsg->pCont), pMsg->contLen, "value-%u-%d", ports[myIndex], i); + snprintf((char *)(pMsg->pCont), pMsg->contLen, "value-myIndex:%u-%d-%d-%ld", myIndex, i, count, taosGetTimestampMs()); return pMsg; } -SyncClientRequest *step1(const SRpcMsg *pMsg) { - SyncClientRequest *pRetMsg = syncClientRequestBuild2(pMsg, 123, true, 1000); - return pRetMsg; -} - -int main(int argc, char **argv) { - // taosInitLog((char *)"syncTest.log", 100000, 10); +int main(int argc, char** argv) { tsAsyncLog = 0; - sDebugFlag = 143 + 64; - void logTest(); - - myIndex = 0; - if (argc >= 2) { - myIndex = atoi(argv[1]); + sDebugFlag = DEBUG_TRACE + DEBUG_SCREEN + DEBUG_FILE; + if (argc != 5) { + usage(argv[0]); + exit(-1); } + int32_t replicaNum = atoi(argv[1]); + int32_t myIndex = atoi(argv[2]); + int32_t lastApplyIndex = atoi(argv[3]); + int32_t writeRecordNum = atoi(argv[4]); + gSnapshotLastApplyIndex = lastApplyIndex; - int32_t ret = syncIOStart((char *)"127.0.0.1", ports[myIndex]); + assert(replicaNum >= 1 && replicaNum <= 5); + assert(myIndex >= 0 && myIndex < replicaNum); + assert(lastApplyIndex >= -1); + assert(writeRecordNum >= 0); + + init(); + int32_t ret = syncIOStart((char*)"127.0.0.1", gPorts[myIndex]); assert(ret == 0); - ret = syncEnvStart(); - assert(ret == 0); + char walPath[128]; + snprintf(walPath, sizeof(walPath), "%s_wal_replica%d_index%d", gDir, replicaNum, myIndex); + SWal* pWal = createWal(walPath, gVgId); - taosRemoveDir("./wal_test"); + int64_t rid = createSyncNode(replicaNum, myIndex, gVgId, pWal, (char*)gDir); + assert(rid > 0); + syncStart(rid); - initFsm(); - - gSyncNode = syncInitTest(); - assert(gSyncNode != NULL); - syncNodeLog2((char *)"", gSyncNode); - - initRaftId(gSyncNode); - - for (int i = 0; i < 30; ++i) { - // step0 - SRpcMsg *pMsg0 = step0(i); - syncRpcMsgLog2((char *)"==step0==", pMsg0); - - // step1 - SyncClientRequest *pMsg1 = step1(pMsg0); - syncClientRequestLog2((char *)"==step1==", pMsg1); - - SyncClientRequest *pSyncClientRequest = pMsg1; - SRpcMsg rpcMsg; - syncClientRequest2RpcMsg(pSyncClientRequest, &rpcMsg); - gSyncNode->FpEqMsg(gSyncNode->queue, &rpcMsg); - - taosMsleep(1000); - sTrace( - "syncPropose sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, " - "electTimerMS:%d, commitIndex:%ld", - gSyncNode->state, syncUtilState2String(gSyncNode->state), gSyncNode->pRaftStore->currentTerm, - gSyncNode->electTimerLogicClock, gSyncNode->electTimerLogicClockUser, gSyncNode->electTimerMS, - gSyncNode->commitIndex); - } + SSyncNode *pSyncNode = (SSyncNode *)syncNodeAcquire(rid); + assert(pSyncNode != NULL); + //--------------------------- + int32_t alreadySend = 0; while (1) { - sTrace( - "replicate sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, " - "electTimerMS:%d, commitIndex:%ld", - gSyncNode->state, syncUtilState2String(gSyncNode->state), gSyncNode->pRaftStore->currentTerm, - gSyncNode->electTimerLogicClock, gSyncNode->electTimerLogicClockUser, gSyncNode->electTimerMS, - gSyncNode->commitIndex); + char* s = syncNode2SimpleStr(pSyncNode); + sTrace("%s", s); + + if (alreadySend < writeRecordNum) { + SRpcMsg *pRpcMsg = createRpcMsg(alreadySend, writeRecordNum, myIndex); + int32_t ret = syncPropose(rid, pRpcMsg, false); + if (ret == TAOS_SYNC_PROPOSE_NOT_LEADER) { + sTrace("%s value%d write not leader", s, alreadySend); + } else { + assert(ret == 0); + sTrace("%s value%d write ok", s, alreadySend); + } + alreadySend++; + + //rpcFreeCont(pRpcMsg->pCont); + taosMemoryFree(pRpcMsg); + } + + taosMsleep(1000); + taosMemoryFree(s); taosMsleep(1000); } + syncNodeRelease(pSyncNode); + syncStop(rid); + walClose(pWal); + syncIOStop(); + cleanup(); return 0; } From 49cf252495b0f1b782ab73ba7b353fa34b0d689d Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Thu, 21 Apr 2022 15:24:50 +0800 Subject: [PATCH 30/51] add syncReplicateTest.cpp --- source/libs/sync/test/syncReplicateTest.cpp | 33 ++++++++++----------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/source/libs/sync/test/syncReplicateTest.cpp b/source/libs/sync/test/syncReplicateTest.cpp index 14132848b4..0e94498a38 100644 --- a/source/libs/sync/test/syncReplicateTest.cpp +++ b/source/libs/sync/test/syncReplicateTest.cpp @@ -30,7 +30,7 @@ void init() { void cleanup() { walCleanUp(); } -void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { +void CommitCb(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta) { SyncIndex beginIndex = SYNC_INDEX_INVALID; if (pFsm->FpGetSnapshot != NULL) { SSnapshot snapshot; @@ -42,7 +42,7 @@ void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { char logBuf[256]; snprintf(logBuf, sizeof(logBuf), "==callback== ==CommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); - syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); + syncRpcMsgLog2(logBuf, (SRpcMsg*)pMsg); } else { sTrace("==callback== ==CommitCb== do not apply again %ld", cbMeta.index); } @@ -117,11 +117,9 @@ int64_t createSyncNode(int32_t replicaNum, int32_t myIndex, int32_t vgId, SWal* int64_t rid = syncOpen(&syncInfo); assert(rid > 0); - syncStart(rid); SSyncNode* pSyncNode = (SSyncNode*)syncNodeAcquire(rid); assert(pSyncNode != NULL); - gSyncIO->FpOnSyncPing = pSyncNode->FpOnPing; gSyncIO->FpOnSyncPingReply = pSyncNode->FpOnPingReply; gSyncIO->FpOnSyncRequestVote = pSyncNode->FpOnRequestVote; @@ -131,22 +129,22 @@ int64_t createSyncNode(int32_t replicaNum, int32_t myIndex, int32_t vgId, SWal* gSyncIO->FpOnSyncPing = pSyncNode->FpOnPing; gSyncIO->FpOnSyncPingReply = pSyncNode->FpOnPingReply; gSyncIO->FpOnSyncTimeout = pSyncNode->FpOnTimeout; + gSyncIO->FpOnSyncClientRequest = pSyncNode->FpOnClientRequest; gSyncIO->pSyncNode = pSyncNode; - - syncNodeStart(pSyncNode); + syncNodeRelease(pSyncNode); return rid; } void usage(char* exe) { printf("usage: %s replicaNum myIndex lastApplyIndex writeRecordNum \n", exe); } -SRpcMsg *createRpcMsg(int i, int count, int myIndex) { - SRpcMsg *pMsg = (SRpcMsg *)taosMemoryMalloc(sizeof(SRpcMsg)); +SRpcMsg* createRpcMsg(int i, int count, int myIndex) { + SRpcMsg* pMsg = (SRpcMsg*)taosMemoryMalloc(sizeof(SRpcMsg)); memset(pMsg, 0, sizeof(SRpcMsg)); pMsg->msgType = 9999; - pMsg->contLen = 128; - pMsg->pCont = taosMemoryMalloc(pMsg->contLen); - snprintf((char *)(pMsg->pCont), pMsg->contLen, "value-myIndex:%u-%d-%d-%ld", myIndex, i, count, taosGetTimestampMs()); + pMsg->contLen = 256; + pMsg->pCont = rpcMallocCont(pMsg->contLen); + snprintf((char*)(pMsg->pCont), pMsg->contLen, "value-myIndex:%u-%d-%d-%ld", myIndex, i, count, taosGetTimestampMs()); return pMsg; } @@ -180,28 +178,29 @@ int main(int argc, char** argv) { assert(rid > 0); syncStart(rid); - SSyncNode *pSyncNode = (SSyncNode *)syncNodeAcquire(rid); + SSyncNode* pSyncNode = (SSyncNode*)syncNodeAcquire(rid); assert(pSyncNode != NULL); //--------------------------- int32_t alreadySend = 0; while (1) { char* s = syncNode2SimpleStr(pSyncNode); - sTrace("%s", s); if (alreadySend < writeRecordNum) { - SRpcMsg *pRpcMsg = createRpcMsg(alreadySend, writeRecordNum, myIndex); - int32_t ret = syncPropose(rid, pRpcMsg, false); + SRpcMsg* pRpcMsg = createRpcMsg(alreadySend, writeRecordNum, myIndex); + int32_t ret = syncPropose(rid, pRpcMsg, false); if (ret == TAOS_SYNC_PROPOSE_NOT_LEADER) { - sTrace("%s value%d write not leader", s, alreadySend); + sTrace("%s value%d write not leader", s, alreadySend); } else { assert(ret == 0); sTrace("%s value%d write ok", s, alreadySend); } alreadySend++; - //rpcFreeCont(pRpcMsg->pCont); + rpcFreeCont(pRpcMsg->pCont); taosMemoryFree(pRpcMsg); + } else { + sTrace("%s", s); } taosMsleep(1000); From 446a2ed1bfe19cab6570125b35298efa29bdc82a Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Thu, 21 Apr 2022 17:35:48 +0800 Subject: [PATCH 31/51] rm syncReplicateTest 2, 3, Load --- source/libs/sync/test/CMakeLists.txt | 42 ---- .../libs/sync/test/syncReplicateLoadTest.cpp | 187 --------------- source/libs/sync/test/syncReplicateTest2.cpp | 203 ----------------- source/libs/sync/test/syncReplicateTest3.cpp | 215 ------------------ 4 files changed, 647 deletions(-) delete mode 100644 source/libs/sync/test/syncReplicateLoadTest.cpp delete mode 100644 source/libs/sync/test/syncReplicateTest2.cpp delete mode 100644 source/libs/sync/test/syncReplicateTest3.cpp diff --git a/source/libs/sync/test/CMakeLists.txt b/source/libs/sync/test/CMakeLists.txt index 14d4401233..3319a73fcf 100644 --- a/source/libs/sync/test/CMakeLists.txt +++ b/source/libs/sync/test/CMakeLists.txt @@ -31,9 +31,6 @@ add_executable(syncElectTest "") add_executable(syncEncodeTest "") add_executable(syncWriteTest "") add_executable(syncReplicateTest "") -add_executable(syncReplicateTest2 "") -add_executable(syncReplicateTest3 "") -add_executable(syncReplicateLoadTest "") add_executable(syncRefTest "") add_executable(syncLogStoreCheck "") add_executable(syncRaftCfgTest "") @@ -174,18 +171,6 @@ target_sources(syncReplicateTest PRIVATE "syncReplicateTest.cpp" ) -target_sources(syncReplicateTest2 - PRIVATE - "syncReplicateTest2.cpp" -) -target_sources(syncReplicateTest3 - PRIVATE - "syncReplicateTest3.cpp" -) -target_sources(syncReplicateLoadTest - PRIVATE - "syncReplicateLoadTest.cpp" -) target_sources(syncRefTest PRIVATE "syncRefTest.cpp" @@ -377,21 +362,6 @@ target_include_directories(syncReplicateTest "${CMAKE_SOURCE_DIR}/include/libs/sync" "${CMAKE_CURRENT_SOURCE_DIR}/../inc" ) -target_include_directories(syncReplicateTest2 - PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" - "${CMAKE_CURRENT_SOURCE_DIR}/../inc" -) -target_include_directories(syncReplicateTest3 - PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" - "${CMAKE_CURRENT_SOURCE_DIR}/../inc" -) -target_include_directories(syncReplicateLoadTest - PUBLIC - "${CMAKE_SOURCE_DIR}/include/libs/sync" - "${CMAKE_CURRENT_SOURCE_DIR}/../inc" -) target_include_directories(syncRefTest PUBLIC "${CMAKE_SOURCE_DIR}/include/libs/sync" @@ -556,18 +526,6 @@ target_link_libraries(syncReplicateTest sync gtest_main ) -target_link_libraries(syncReplicateTest2 - sync - gtest_main -) -target_link_libraries(syncReplicateTest3 - sync - gtest_main -) -target_link_libraries(syncReplicateLoadTest - sync - gtest_main -) target_link_libraries(syncRefTest sync gtest_main diff --git a/source/libs/sync/test/syncReplicateLoadTest.cpp b/source/libs/sync/test/syncReplicateLoadTest.cpp deleted file mode 100644 index f58c372445..0000000000 --- a/source/libs/sync/test/syncReplicateLoadTest.cpp +++ /dev/null @@ -1,187 +0,0 @@ -#include -#include -#include "syncEnv.h" -#include "syncIO.h" -#include "syncInt.h" -#include "syncMessage.h" -#include "syncRaftEntry.h" -#include "syncRaftLog.h" -#include "syncRaftStore.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"); -} - -uint16_t ports[] = {7010, 7110, 7210, 7310, 7410}; -int32_t replicaNum = 3; -int32_t myIndex = 0; - -SRaftId ids[TSDB_MAX_REPLICA]; -SSyncInfo syncInfo; -SSyncFSM *pFsm; -SWal * pWal; - -void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { - char logBuf[256]; - snprintf(logBuf, sizeof(logBuf), "==callback== ==CommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", - pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); - syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); -} - -void PreCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { - char logBuf[256]; - snprintf(logBuf, sizeof(logBuf), - "==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, cbMeta.index, - cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); - syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); -} - -void RollBackCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { - char logBuf[256]; - snprintf(logBuf, sizeof(logBuf), "==callback== ==RollBackCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", - pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); - syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); -} - -void initFsm() { - pFsm = (SSyncFSM *)taosMemoryMalloc(sizeof(SSyncFSM)); - pFsm->FpCommitCb = CommitCb; - pFsm->FpPreCommitCb = PreCommitCb; - pFsm->FpRollBackCb = RollBackCb; -} - -int64_t syncNodeInit() { - syncInfo.vgId = 1234; - syncInfo.rpcClient = gSyncIO->clientRpc; - syncInfo.FpSendMsg = syncIOSendMsg; - syncInfo.queue = gSyncIO->pMsgQ; - syncInfo.FpEqMsg = syncIOEqMsg; - syncInfo.pFsm = pFsm; - snprintf(syncInfo.path, sizeof(syncInfo.path), "./replicate2_test_%d", myIndex); - - int code = walInit(); - assert(code == 0); - SWalCfg walCfg; - memset(&walCfg, 0, sizeof(SWalCfg)); - walCfg.vgId = syncInfo.vgId; - walCfg.fsyncPeriod = 1000; - walCfg.retentionPeriod = 1000; - walCfg.rollPeriod = 1000; - walCfg.retentionSize = 1000; - walCfg.segSize = 1000; - walCfg.level = TAOS_WAL_FSYNC; - - char tmpdir[128]; - snprintf(tmpdir, sizeof(tmpdir), "./replicate2_test_wal_%d", myIndex); - pWal = walOpen(tmpdir, &walCfg); - assert(pWal != NULL); - - syncInfo.pWal = pWal; - - SSyncCfg *pCfg = &syncInfo.syncCfg; - pCfg->myIndex = myIndex; - pCfg->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); - } - - int64_t rid = syncOpen(&syncInfo); - assert(rid > 0); - syncStart(rid); - - SSyncNode *pSyncNode = (SSyncNode *)syncNodeAcquire(rid); - assert(pSyncNode != NULL); - - // pSyncNode->hbBaseLine = 500; - // pSyncNode->electBaseLine = 1500; - - 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->FpOnSyncTimeout = pSyncNode->FpOnTimeout; - gSyncIO->FpOnSyncClientRequest = pSyncNode->FpOnClientRequest; - gSyncIO->pSyncNode = pSyncNode; - - syncNodeRelease(pSyncNode); - - return rid; -} - -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); - } -} - -SRpcMsg *step0(int i) { - SRpcMsg *pMsg = (SRpcMsg *)taosMemoryMalloc(sizeof(SRpcMsg)); - memset(pMsg, 0, sizeof(SRpcMsg)); - pMsg->msgType = 9999; - pMsg->contLen = 128; - pMsg->pCont = taosMemoryMalloc(pMsg->contLen); - snprintf((char *)(pMsg->pCont), pMsg->contLen, "value-%u-%d", ports[myIndex], i); - return pMsg; -} - -SyncClientRequest *step1(const SRpcMsg *pMsg) { - SyncClientRequest *pRetMsg = syncClientRequestBuild2(pMsg, 123, true, 1000); - return pRetMsg; -} - -int main(int argc, char **argv) { - // taosInitLog((char *)"syncTest.log", 100000, 10); - tsAsyncLog = 0; - sDebugFlag = 143 + 64; - void logTest(); - - myIndex = 0; - if (argc >= 2) { - myIndex = atoi(argv[1]); - } - - int32_t ret = syncIOStart((char *)"127.0.0.1", ports[myIndex]); - assert(ret == 0); - - initFsm(); - - ret = syncInit(); - assert(ret == 0); - - int64_t rid = syncNodeInit(); - assert(rid > 0); - - SSyncNode *pSyncNode = (SSyncNode *)syncNodeAcquire(rid); - assert(pSyncNode != NULL); - - syncNodeLog2((char *)"", pSyncNode); - initRaftId(pSyncNode); - - // only load ... - - while (1) { - sTrace( - "replicate sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, " - "electTimerMS:%d, commitIndex:%ld", - pSyncNode->state, syncUtilState2String(pSyncNode->state), pSyncNode->pRaftStore->currentTerm, - pSyncNode->electTimerLogicClock, pSyncNode->electTimerLogicClockUser, pSyncNode->electTimerMS, - pSyncNode->commitIndex); - taosMsleep(1000); - } - - return 0; -} diff --git a/source/libs/sync/test/syncReplicateTest2.cpp b/source/libs/sync/test/syncReplicateTest2.cpp deleted file mode 100644 index 1e2f823be5..0000000000 --- a/source/libs/sync/test/syncReplicateTest2.cpp +++ /dev/null @@ -1,203 +0,0 @@ -#include -#include -#include "syncEnv.h" -#include "syncIO.h" -#include "syncInt.h" -#include "syncMessage.h" -#include "syncRaftEntry.h" -#include "syncRaftLog.h" -#include "syncRaftStore.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"); -} - -uint16_t ports[] = {7010, 7110, 7210, 7310, 7410}; -int32_t replicaNum = 3; -int32_t myIndex = 0; - -SRaftId ids[TSDB_MAX_REPLICA]; -SSyncInfo syncInfo; -SSyncFSM *pFsm; -SWal * pWal; - -void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { - char logBuf[256]; - snprintf(logBuf, sizeof(logBuf), "==callback== ==CommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", - pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); - syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); -} - -void PreCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { - char logBuf[256]; - snprintf(logBuf, sizeof(logBuf), - "==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, cbMeta.index, - cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); - syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); -} - -void RollBackCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { - char logBuf[256]; - snprintf(logBuf, sizeof(logBuf), "==callback== ==RollBackCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", - pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); - syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); -} - -void initFsm() { - pFsm = (SSyncFSM *)taosMemoryMalloc(sizeof(SSyncFSM)); - pFsm->FpCommitCb = CommitCb; - pFsm->FpPreCommitCb = PreCommitCb; - pFsm->FpRollBackCb = RollBackCb; -} - -int64_t syncNodeInit() { - syncInfo.vgId = 1234; - syncInfo.rpcClient = gSyncIO->clientRpc; - syncInfo.FpSendMsg = syncIOSendMsg; - syncInfo.queue = gSyncIO->pMsgQ; - syncInfo.FpEqMsg = syncIOEqMsg; - syncInfo.pFsm = pFsm; - snprintf(syncInfo.path, sizeof(syncInfo.path), "./replicate2_test_%d", myIndex); - - int code = walInit(); - assert(code == 0); - SWalCfg walCfg; - memset(&walCfg, 0, sizeof(SWalCfg)); - walCfg.vgId = syncInfo.vgId; - walCfg.fsyncPeriod = 1000; - walCfg.retentionPeriod = 1000; - walCfg.rollPeriod = 1000; - walCfg.retentionSize = 1000; - walCfg.segSize = 1000; - walCfg.level = TAOS_WAL_FSYNC; - - char tmpdir[128]; - snprintf(tmpdir, sizeof(tmpdir), "./replicate2_test_wal_%d", myIndex); - pWal = walOpen(tmpdir, &walCfg); - assert(pWal != NULL); - - syncInfo.pWal = pWal; - - SSyncCfg *pCfg = &syncInfo.syncCfg; - pCfg->myIndex = myIndex; - pCfg->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); - } - - int64_t rid = syncOpen(&syncInfo); - assert(rid > 0); - syncStart(rid); - - SSyncNode *pSyncNode = (SSyncNode *)syncNodeAcquire(rid); - assert(pSyncNode != NULL); - - // pSyncNode->hbBaseLine = 500; - // pSyncNode->electBaseLine = 1500; - - 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->FpOnSyncTimeout = pSyncNode->FpOnTimeout; - gSyncIO->FpOnSyncClientRequest = pSyncNode->FpOnClientRequest; - gSyncIO->pSyncNode = pSyncNode; - - syncNodeRelease(pSyncNode); - - return rid; -} - -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); - } -} - -SRpcMsg *step0(int i) { - SRpcMsg *pMsg = (SRpcMsg *)taosMemoryMalloc(sizeof(SRpcMsg)); - memset(pMsg, 0, sizeof(SRpcMsg)); - pMsg->msgType = 9999; - pMsg->contLen = 128; - pMsg->pCont = taosMemoryMalloc(pMsg->contLen); - snprintf((char *)(pMsg->pCont), pMsg->contLen, "value-%u-%d", ports[myIndex], i); - return pMsg; -} - -SyncClientRequest *step1(const SRpcMsg *pMsg) { - SyncClientRequest *pRetMsg = syncClientRequestBuild2(pMsg, 123, true, 1000); - return pRetMsg; -} - -int main(int argc, char **argv) { - // taosInitLog((char *)"syncTest.log", 100000, 10); - tsAsyncLog = 0; - sDebugFlag = 143 + 64; - void logTest(); - - myIndex = 0; - if (argc >= 2) { - myIndex = atoi(argv[1]); - } - - int32_t ret = syncIOStart((char *)"127.0.0.1", ports[myIndex]); - assert(ret == 0); - - initFsm(); - - ret = syncInit(); - assert(ret == 0); - - int64_t rid = syncNodeInit(); - assert(rid > 0); - - SSyncNode *pSyncNode = (SSyncNode *)syncNodeAcquire(rid); - assert(pSyncNode != NULL); - - syncNodeLog2((char *)"", pSyncNode); - initRaftId(pSyncNode); - - for (int i = 0; i < 30; ++i) { - // step0 - SRpcMsg *pMsg0 = step0(i); - syncRpcMsgLog2((char *)"==step0==", pMsg0); - - syncPropose(rid, pMsg0, true); - taosMsleep(1000); - - taosMemoryFree(pMsg0); - - sTrace( - "syncPropose sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, " - "electTimerMS:%d, commitIndex:%ld", - pSyncNode->state, syncUtilState2String(pSyncNode->state), pSyncNode->pRaftStore->currentTerm, - pSyncNode->electTimerLogicClock, pSyncNode->electTimerLogicClockUser, pSyncNode->electTimerMS, - pSyncNode->commitIndex); - } - - while (1) { - sTrace( - "replicate sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, " - "electTimerMS:%d, commitIndex:%ld", - pSyncNode->state, syncUtilState2String(pSyncNode->state), pSyncNode->pRaftStore->currentTerm, - pSyncNode->electTimerLogicClock, pSyncNode->electTimerLogicClockUser, pSyncNode->electTimerMS, - pSyncNode->commitIndex); - taosMsleep(1000); - } - - return 0; -} diff --git a/source/libs/sync/test/syncReplicateTest3.cpp b/source/libs/sync/test/syncReplicateTest3.cpp deleted file mode 100644 index e8e24b507d..0000000000 --- a/source/libs/sync/test/syncReplicateTest3.cpp +++ /dev/null @@ -1,215 +0,0 @@ -#define ALLOW_FORBID_FUNC - -#include -#include -#include "syncEnv.h" -#include "syncIO.h" -#include "syncInt.h" -#include "syncMessage.h" -#include "syncRaftEntry.h" -#include "syncRaftLog.h" -#include "syncRaftStore.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"); -} - -uint16_t ports[] = {7010, 7110, 7210, 7310, 7410}; -int32_t replicaNum = 3; -int32_t myIndex = 0; - -SRaftId ids[TSDB_MAX_REPLICA]; -SSyncInfo syncInfo; -SSyncFSM *pFsm; -SWal * pWal; - -void CommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { - char logBuf[256]; - snprintf(logBuf, sizeof(logBuf), "==callback== ==CommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", - pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); - syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); -} - -void PreCommitCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { - char logBuf[256]; - snprintf(logBuf, sizeof(logBuf), - "==callback== ==PreCommitCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", pFsm, cbMeta.index, - cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); - syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); -} - -void RollBackCb(struct SSyncFSM *pFsm, const SRpcMsg *pMsg, SFsmCbMeta cbMeta) { - char logBuf[256]; - snprintf(logBuf, sizeof(logBuf), "==callback== ==RollBackCb== pFsm:%p, index:%ld, isWeak:%d, code:%d, state:%d %s \n", - pFsm, cbMeta.index, cbMeta.isWeak, cbMeta.code, cbMeta.state, syncUtilState2String(cbMeta.state)); - syncRpcMsgLog2(logBuf, (SRpcMsg *)pMsg); -} - -void initFsm() { - pFsm = (SSyncFSM *)taosMemoryMalloc(sizeof(SSyncFSM)); - pFsm->FpCommitCb = CommitCb; - pFsm->FpPreCommitCb = PreCommitCb; - pFsm->FpRollBackCb = RollBackCb; -} - -int64_t syncNodeInit() { - syncInfo.vgId = 1234; - syncInfo.rpcClient = gSyncIO->clientRpc; - syncInfo.FpSendMsg = syncIOSendMsg; - syncInfo.queue = gSyncIO->pMsgQ; - syncInfo.FpEqMsg = syncIOEqMsg; - syncInfo.pFsm = pFsm; - snprintf(syncInfo.path, sizeof(syncInfo.path), "./replicate2_test_%d", myIndex); - - int code = walInit(); - assert(code == 0); - SWalCfg walCfg; - memset(&walCfg, 0, sizeof(SWalCfg)); - walCfg.vgId = syncInfo.vgId; - walCfg.fsyncPeriod = 1000; - walCfg.retentionPeriod = 1000; - walCfg.rollPeriod = 1000; - walCfg.retentionSize = 1000; - walCfg.segSize = 1000; - walCfg.level = TAOS_WAL_FSYNC; - - char tmpdir[128]; - snprintf(tmpdir, sizeof(tmpdir), "./replicate2_test_wal_%d", myIndex); - pWal = walOpen(tmpdir, &walCfg); - assert(pWal != NULL); - - syncInfo.pWal = pWal; - - SSyncCfg *pCfg = &syncInfo.syncCfg; - pCfg->myIndex = myIndex; - pCfg->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); - } - - int64_t rid = syncOpen(&syncInfo); - assert(rid > 0); - syncStart(rid); - - SSyncNode *pSyncNode = (SSyncNode *)syncNodeAcquire(rid); - assert(pSyncNode != NULL); - - // pSyncNode->hbBaseLine = 500; - // pSyncNode->electBaseLine = 1500; - - 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->FpOnSyncTimeout = pSyncNode->FpOnTimeout; - gSyncIO->FpOnSyncClientRequest = pSyncNode->FpOnClientRequest; - gSyncIO->pSyncNode = pSyncNode; - - syncNodeRelease(pSyncNode); - - return rid; -} - -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); - } -} - -SRpcMsg *step0(int i) { - SRpcMsg *pMsg = (SRpcMsg *)taosMemoryMalloc(sizeof(SRpcMsg)); - memset(pMsg, 0, sizeof(SRpcMsg)); - pMsg->msgType = 9999; - pMsg->contLen = 128; - pMsg->pCont = taosMemoryMalloc(pMsg->contLen); - snprintf((char *)(pMsg->pCont), pMsg->contLen, "value-%u-%d", ports[myIndex], i); - return pMsg; -} - -SyncClientRequest *step1(const SRpcMsg *pMsg) { - SyncClientRequest *pRetMsg = syncClientRequestBuild2(pMsg, 123, true, 1000); - return pRetMsg; -} - -int main(int argc, char **argv) { - // taosInitLog((char *)"syncTest.log", 100000, 10); - tsAsyncLog = 0; - sDebugFlag = 143 + 64; - void logTest(); - - myIndex = 0; - if (argc >= 2) { - myIndex = atoi(argv[1]); - } - - int recordCount = 100; - if (argc >= 3) { - recordCount = atoi(argv[2]); - } - - int sleepMS = 10; - if (argc >= 4) { - sleepMS = atoi(argv[3]); - } - - int32_t ret = syncIOStart((char *)"127.0.0.1", ports[myIndex]); - assert(ret == 0); - - initFsm(); - - ret = syncInit(); - assert(ret == 0); - - int64_t rid = syncNodeInit(); - assert(rid > 0); - - SSyncNode *pSyncNode = (SSyncNode *)syncNodeAcquire(rid); - assert(pSyncNode != NULL); - - syncNodeLog2((char *)"", pSyncNode); - initRaftId(pSyncNode); - - for (int i = 0; i < recordCount; ++i) { - // step0 - SRpcMsg *pMsg0 = step0(i); - syncRpcMsgLog2((char *)"==step0==", pMsg0); - - syncPropose(rid, pMsg0, true); - taosMsleep(sleepMS); - - taosMemoryFree(pMsg0); - - sTrace( - "syncPropose sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, " - "electTimerMS:%d, commitIndex:%ld", - pSyncNode->state, syncUtilState2String(pSyncNode->state), pSyncNode->pRaftStore->currentTerm, - pSyncNode->electTimerLogicClock, pSyncNode->electTimerLogicClockUser, pSyncNode->electTimerMS, - pSyncNode->commitIndex); - } - - while (1) { - sTrace( - "replicate sleep, state: %d, %s, term:%lu electTimerLogicClock:%lu, electTimerLogicClockUser:%lu, " - "electTimerMS:%d, commitIndex:%ld", - pSyncNode->state, syncUtilState2String(pSyncNode->state), pSyncNode->pRaftStore->currentTerm, - pSyncNode->electTimerLogicClock, pSyncNode->electTimerLogicClockUser, pSyncNode->electTimerMS, - pSyncNode->commitIndex); - taosMsleep(1000); - } - - return 0; -} From e28b75b8d4754fb036c62bdf6b202f7b7d5aa315 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Fri, 22 Apr 2022 15:07:20 +0800 Subject: [PATCH 32/51] refactor(query): do some internal refactor. --- include/libs/function/function.h | 4 +- source/libs/executor/inc/executil.h | 2 +- source/libs/executor/inc/executorimpl.h | 4 +- source/libs/executor/src/executorimpl.c | 77 ++++++++--------- source/libs/executor/src/groupoperator.c | 4 +- source/libs/function/inc/builtinsimpl.h | 7 +- source/libs/function/src/builtins.c | 2 +- source/libs/function/src/builtinsimpl.c | 100 +++++++++++++++++------ 8 files changed, 118 insertions(+), 82 deletions(-) diff --git a/include/libs/function/function.h b/include/libs/function/function.h index 68fc3be617..004d834287 100644 --- a/include/libs/function/function.h +++ b/include/libs/function/function.h @@ -37,7 +37,7 @@ typedef struct SFuncExecEnv { typedef bool (*FExecGetEnv)(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); typedef bool (*FExecInit)(struct SqlFunctionCtx *pCtx, struct SResultRowEntryInfo* pResultCellInfo); typedef int32_t (*FExecProcess)(struct SqlFunctionCtx *pCtx); -typedef void (*FExecFinalize)(struct SqlFunctionCtx *pCtx); +typedef int32_t (*FExecFinalize)(struct SqlFunctionCtx *pCtx, SSDataBlock* pBlock, int32_t slotId); typedef int32_t (*FScalarExecProcess)(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput); typedef struct SScalarFuncExecFuncs { @@ -113,7 +113,7 @@ typedef struct SResultRowEntryInfo { bool initialized:1; // output buffer has been initialized bool complete:1; // query has completed uint8_t isNullRes:6; // the result is null - uint8_t numOfRes; // num of output result in current buffer + uint8_t numOfRes; // num of output result in current buffer } SResultRowEntryInfo; // determine the real data need to calculated the result diff --git a/source/libs/executor/inc/executil.h b/source/libs/executor/inc/executil.h index 7c931c860c..834d37927a 100644 --- a/source/libs/executor/inc/executil.h +++ b/source/libs/executor/inc/executil.h @@ -60,7 +60,7 @@ typedef struct SResultRow { uint32_t numOfRows; // number of rows of current time window struct SResultRowEntryInfo* pEntryInfo; // For each result column, there is a resultInfo STimeWindow win; - char *key; // start key of current result row + char *key; // start key of current result row } SResultRow; typedef struct SResultRowPosition { diff --git a/source/libs/executor/inc/executorimpl.h b/source/libs/executor/inc/executorimpl.h index 5adfb7caca..c6d209e706 100644 --- a/source/libs/executor/inc/executorimpl.h +++ b/source/libs/executor/inc/executorimpl.h @@ -608,8 +608,8 @@ int32_t appendDownstream(SOperatorInfo* p, SOperatorInfo** pDownstream, int32_t int32_t initAggInfo(SOptrBasicInfo* pBasicInfo, SAggSupporter* pAggSup, SExprInfo* pExprInfo, int32_t numOfCols, SSDataBlock* pResultBlock, size_t keyBufSize, const char* pkey); void initResultSizeInfo(SOperatorInfo* pOperator, int32_t numOfRows); -void doBuildResultDatablock(SSDataBlock* pBlock, int32_t rowCapacity, SGroupResInfo* pGroupResInfo, SExprInfo* pExprInfo, - SDiskbasedBuf* pBuf, int32_t* rowCellOffset); +void doBuildResultDatablock(SSDataBlock* pBlock, SGroupResInfo* pGroupResInfo, SExprInfo* pExprInfo, + SDiskbasedBuf* pBuf, int32_t* rowCellOffset, SqlFunctionCtx* pCtx); void finalizeMultiTupleQueryResult(SqlFunctionCtx* pCtx, int32_t numOfOutput, SDiskbasedBuf* pBuf, SResultRowInfo* pResultRowInfo, int32_t* rowCellInfoOffset); void doApplyFunctions(SqlFunctionCtx* pCtx, STimeWindow* pWin, SColumnInfoData* pTimeWindowData, int32_t offset, diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index 7a7cb68424..4a0206f55e 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -230,10 +230,10 @@ int32_t operatorDummyOpenFn(SOperatorInfo* pOperator) { void operatorDummyCloseFn(void* param, int32_t numOfCols) {} -static int32_t doCopyToSDataBlock(SSDataBlock* pBlock, int32_t rowCapacity, SExprInfo* pExprInfo, SDiskbasedBuf* pBuf, SGroupResInfo* pGroupResInfo, - int32_t orderType, int32_t* rowCellOffset); -static void initCtxOutputBuffer(SqlFunctionCtx* pCtx, int32_t size); +static int32_t doCopyToSDataBlock(SSDataBlock* pBlock, SExprInfo* pExprInfo, SDiskbasedBuf* pBuf, SGroupResInfo* pGroupResInfo, + int32_t orderType, int32_t* rowCellOffset, SqlFunctionCtx* pCtx); +static void initCtxOutputBuffer(SqlFunctionCtx* pCtx, int32_t size); static void setResultBufSize(STaskAttr* pQueryAttr, SResultInfo* pResultInfo); static void setCtxTagForJoin(STaskRuntimeEnv* pRuntimeEnv, SqlFunctionCtx* pCtx, SExprInfo* pExprInfo, void* pTable); static void doSetTableGroupOutputBuf(SAggOperatorInfo* pAggInfo, int32_t numOfOutput, uint64_t groupId, SExecTaskInfo* pTaskInfo); @@ -2809,16 +2809,6 @@ void setTaskStatus(SExecTaskInfo* pTaskInfo, int8_t status) { } } -void finalizeQueryResult(SqlFunctionCtx* pCtx, int32_t numOfOutput) { - for (int32_t j = 0; j < numOfOutput; ++j) { - if (pCtx[j].functionId == -1) { - continue; - } - - pCtx[j].fpSet.finalize(&pCtx[j]); - } -} - void finalizeMultiTupleQueryResult(SqlFunctionCtx* pCtx, int32_t numOfOutput, SDiskbasedBuf* pBuf, SResultRowInfo* pResultRowInfo, int32_t* rowCellInfoOffset) { for (int32_t i = 0; i < pResultRowInfo->size; ++i) { @@ -2841,7 +2831,7 @@ void finalizeMultiTupleQueryResult(SqlFunctionCtx* pCtx, int32_t numOfOutput, SD } if (pCtx[j].fpSet.process) { // TODO set the dummy function, to avoid the check for null ptr. - pCtx[j].fpSet.finalize(&pCtx[j]); +// pCtx[j].fpSet.finalize(&pCtx[j]); } if (pRow->numOfRows < pResInfo->numOfRes) { @@ -2872,7 +2862,7 @@ void finalizeUpdatedResult(SqlFunctionCtx* pCtx, int32_t numOfOutput, SDiskbased } if (pCtx[j].fpSet.process) { // TODO set the dummy function. - pCtx[j].fpSet.finalize(&pCtx[j]); +// pCtx[j].fpSet.finalize(&pCtx[j]); pResInfo->initialized = true; } @@ -3107,7 +3097,8 @@ void setIntervalQueryRange(STableQueryInfo* pTableQueryInfo, TSKEY key, STimeWin * @param pQInfo * @param result */ -int32_t doCopyToSDataBlock(SSDataBlock* pBlock, int32_t rowCapacity, SExprInfo* pExprInfo, SDiskbasedBuf* pBuf, SGroupResInfo* pGroupResInfo, int32_t orderType, int32_t* rowCellOffset) { +int32_t doCopyToSDataBlock(SSDataBlock* pBlock, SExprInfo* pExprInfo, SDiskbasedBuf* pBuf, SGroupResInfo* pGroupResInfo, + int32_t orderType, int32_t* rowCellOffset, SqlFunctionCtx* pCtx) { int32_t numOfRows = getNumOfTotalRes(pGroupResInfo); int32_t numOfResult = pBlock->info.rows; // there are already exists result rows @@ -3125,8 +3116,6 @@ int32_t doCopyToSDataBlock(SSDataBlock* pBlock, int32_t rowCapacity, SExprInfo* step = -1; } - int32_t nrows = pBlock->info.rows; - for (int32_t i = start; (i < numOfRows) && (i >= 0); i += step) { SResultRowPosition* pPos = taosArrayGet(pGroupResInfo->pRows, i); SFilePage* page = getBufPage(pBuf, pPos->pageId); @@ -3139,7 +3128,7 @@ int32_t doCopyToSDataBlock(SSDataBlock* pBlock, int32_t rowCapacity, SExprInfo* // TODO copy multiple rows? int32_t numOfRowsToCopy = pRow->numOfRows; - if (numOfResult + numOfRowsToCopy >= rowCapacity) { + if (numOfResult + numOfRowsToCopy >= pBlock->info.capacity) { break; } @@ -3148,31 +3137,32 @@ int32_t doCopyToSDataBlock(SSDataBlock* pBlock, int32_t rowCapacity, SExprInfo* for (int32_t j = 0; j < pBlock->info.numOfCols; ++j) { int32_t slotId = pExprInfo[j].base.resSchema.slotId; - SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, slotId); - SResultRowEntryInfo* pEntryInfo = getResultCell(pRow, j, rowCellOffset); + pCtx[j].resultInfo = getResultCell(pRow, j, rowCellOffset); + if (pCtx[j].fpSet.process) { + pCtx[j].fpSet.finalize(&pCtx[j], pBlock, slotId); + } else { + SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, slotId); - char* in = GET_ROWCELL_INTERBUF(pEntryInfo); - colDataAppend(pColInfoData, nrows, in, pEntryInfo->isNullRes); + char* in = GET_ROWCELL_INTERBUF(pCtx[j].resultInfo); + colDataAppend(pColInfoData, pBlock->info.rows, in, pCtx[j].resultInfo->isNullRes); + } } releaseBufPage(pBuf, page); - nrows += 1; - numOfResult += numOfRowsToCopy; - if (numOfResult == rowCapacity) { // output buffer is full + pBlock->info.rows += pRow->numOfRows; + if (pBlock->info.rows >= pBlock->info.capacity) { // output buffer is full break; } } // qDebug("QInfo:0x%"PRIx64" copy data to query buf completed", GET_TASKID(pRuntimeEnv)); - pBlock->info.rows = numOfResult; blockDataUpdateTsWindow(pBlock); - return 0; } -void doBuildResultDatablock(SSDataBlock* pBlock, int32_t rowCapacity, SGroupResInfo* pGroupResInfo, SExprInfo* pExprInfo, SDiskbasedBuf* pBuf, - int32_t* rowCellOffset) { +void doBuildResultDatablock(SSDataBlock* pBlock, SGroupResInfo* pGroupResInfo, SExprInfo* pExprInfo, SDiskbasedBuf* pBuf, + int32_t* rowCellOffset, SqlFunctionCtx* pCtx) { assert(pGroupResInfo->currentGroup <= pGroupResInfo->totalGroup); blockDataCleanup(pBlock); @@ -3181,7 +3171,7 @@ void doBuildResultDatablock(SSDataBlock* pBlock, int32_t rowCapacity, SGroupResI } int32_t orderType = TSDB_ORDER_ASC; - doCopyToSDataBlock(pBlock, rowCapacity, pExprInfo, pBuf, pGroupResInfo, orderType, rowCellOffset); + doCopyToSDataBlock(pBlock, pExprInfo, pBuf, pGroupResInfo, orderType, rowCellOffset, pCtx); // add condition (pBlock->info.rows >= 1) just to runtime happy blockDataUpdateTsWindow(pBlock); @@ -4405,7 +4395,7 @@ static void doFinalizeResultImpl(SqlFunctionCtx* pCtx, int32_t numOfExpr) { // SUdfInfo* pUdfInfo = taosArrayGet(pInfo->udfInfo, -1 * functionId - 1); // doInvokeUdf(pUdfInfo, &pCtx[j], 0, TSDB_UDF_FUNC_FINALIZE); // } else { - pCtx[j].fpSet.finalize(&pCtx[j]); +// pCtx[j].fpSet.finalize(&pCtx[j]); } } @@ -4805,7 +4795,7 @@ static SSDataBlock* getAggregateResult(SOperatorInfo* pOperator, bool* newgroup) } blockDataEnsureCapacity(pInfo->pRes, pOperator->resultInfo.capacity); - doBuildResultDatablock(pInfo->pRes, pOperator->resultInfo.capacity, &pAggInfo->groupResInfo, pOperator->pExpr, pAggInfo->aggSup.pResultBuf, pInfo->rowCellInfoOffset); + doBuildResultDatablock(pInfo->pRes, &pAggInfo->groupResInfo, pOperator->pExpr, pAggInfo->aggSup.pResultBuf, pInfo->rowCellInfoOffset, pInfo->pCtx); if (pInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pAggInfo->groupResInfo)) { doSetOperatorCompleted(pOperator); } @@ -5129,8 +5119,7 @@ static SSDataBlock* doBuildIntervalResult(SOperatorInfo* pOperator, bool* newgro } blockDataEnsureCapacity(pBlock, pOperator->resultInfo.capacity); - doBuildResultDatablock(pBlock, pOperator->resultInfo.capacity, &pInfo->groupResInfo, pOperator->pExpr, - pInfo->aggSup.pResultBuf, pInfo->binfo.rowCellInfoOffset); + doBuildResultDatablock(pBlock, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pInfo->binfo.rowCellInfoOffset, pInfo->binfo.pCtx); if (pBlock->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { doSetOperatorCompleted(pOperator); @@ -5149,7 +5138,7 @@ static SSDataBlock* doStreamIntervalAgg(SOperatorInfo *pOperator, bool* newgroup } if (pOperator->status == OP_RES_TO_RETURN) { - doBuildResultDatablock(pInfo->binfo.pRes, pOperator->resultInfo.capacity, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pInfo->binfo.rowCellInfoOffset); + doBuildResultDatablock(pInfo->binfo.pRes, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pInfo->binfo.rowCellInfoOffset, pInfo->binfo.pCtx); if (pInfo->binfo.pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { pOperator->status = OP_EXEC_DONE; } @@ -5184,7 +5173,7 @@ static SSDataBlock* doStreamIntervalAgg(SOperatorInfo *pOperator, bool* newgroup initMultiResInfoFromArrayList(&pInfo->groupResInfo, pUpdated); blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity); - doBuildResultDatablock(pInfo->binfo.pRes, pOperator->resultInfo.capacity, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pInfo->binfo.rowCellInfoOffset); + doBuildResultDatablock(pInfo->binfo.pRes, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pInfo->binfo.rowCellInfoOffset, pInfo->binfo.pCtx); ASSERT(pInfo->binfo.pRes->info.rows > 0); pOperator->status = OP_RES_TO_RETURN; @@ -5229,7 +5218,7 @@ static SSDataBlock* doAllIntervalAgg(SOperatorInfo *pOperator, bool* newgroup) { pOperator->status = OP_RES_TO_RETURN; closeAllResultRows(&pSliceInfo->binfo.resultRowInfo); setTaskStatus(pOperator->pTaskInfo, TASK_COMPLETED); - finalizeQueryResult(pSliceInfo->binfo.pCtx, pOperator->numOfOutput); +// finalizeQueryResult(pSliceInfo->binfo.pCtx, pOperator->numOfOutput); initGroupResInfo(&pSliceInfo->groupResInfo, &pSliceInfo->binfo.resultRowInfo); // doBuildResultDatablock(&pRuntimeEnv->groupResInfo, pRuntimeEnv, pSliceInfo->pRes); @@ -5286,8 +5275,8 @@ static SSDataBlock* doSTableIntervalAgg(SOperatorInfo* pOperator, bool* newgroup OPTR_SET_OPENED(pOperator); blockDataEnsureCapacity(pInfo->binfo.pRes, pOperator->resultInfo.capacity); - doBuildResultDatablock(pInfo->binfo.pRes, pOperator->resultInfo.capacity, &pInfo->groupResInfo, pOperator->pExpr, - pInfo->aggSup.pResultBuf, pInfo->binfo.rowCellInfoOffset); + doBuildResultDatablock(pInfo->binfo.pRes, &pInfo->groupResInfo, pOperator->pExpr, + pInfo->aggSup.pResultBuf, pInfo->binfo.rowCellInfoOffset, pInfo->binfo.pCtx); if (pInfo->binfo.pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { doSetOperatorCompleted(pOperator); @@ -5377,7 +5366,7 @@ static SSDataBlock* doStateWindowAgg(SOperatorInfo* pOperator, bool* newgroup) { SOptrBasicInfo* pBInfo = &pInfo->binfo; if (pOperator->status == OP_RES_TO_RETURN) { - doBuildResultDatablock(pBInfo->pRes, pOperator->resultInfo.capacity, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pBInfo->rowCellInfoOffset); + doBuildResultDatablock(pBInfo->pRes, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pBInfo->rowCellInfoOffset, pInfo->binfo.pCtx); if (pBInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { doSetOperatorCompleted(pOperator); return NULL; @@ -5409,7 +5398,7 @@ static SSDataBlock* doStateWindowAgg(SOperatorInfo* pOperator, bool* newgroup) { initGroupResInfo(&pInfo->groupResInfo, &pBInfo->resultRowInfo); blockDataEnsureCapacity(pBInfo->pRes, pOperator->resultInfo.capacity); - doBuildResultDatablock(pBInfo->pRes, pOperator->resultInfo.capacity, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pBInfo->rowCellInfoOffset); + doBuildResultDatablock(pBInfo->pRes, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pBInfo->rowCellInfoOffset, pInfo->binfo.pCtx); if (pBInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { doSetOperatorCompleted(pOperator); } @@ -5426,7 +5415,7 @@ static SSDataBlock* doSessionWindowAgg(SOperatorInfo* pOperator, bool* newgroup) SOptrBasicInfo* pBInfo = &pInfo->binfo; if (pOperator->status == OP_RES_TO_RETURN) { - doBuildResultDatablock(pBInfo->pRes, pOperator->resultInfo.capacity, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pBInfo->rowCellInfoOffset); + doBuildResultDatablock(pBInfo->pRes, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pBInfo->rowCellInfoOffset, pInfo->binfo.pCtx); if (pBInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { doSetOperatorCompleted(pOperator); return NULL; @@ -5458,7 +5447,7 @@ static SSDataBlock* doSessionWindowAgg(SOperatorInfo* pOperator, bool* newgroup) initGroupResInfo(&pInfo->groupResInfo, &pBInfo->resultRowInfo); blockDataEnsureCapacity(pBInfo->pRes, pOperator->resultInfo.capacity); - doBuildResultDatablock(pBInfo->pRes, pOperator->resultInfo.capacity, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pBInfo->rowCellInfoOffset); + doBuildResultDatablock(pBInfo->pRes, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pBInfo->rowCellInfoOffset, pInfo->binfo.pCtx); if (pBInfo->pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { doSetOperatorCompleted(pOperator); } diff --git a/source/libs/executor/src/groupoperator.c b/source/libs/executor/src/groupoperator.c index 9f75f97632..8a0da52a8e 100644 --- a/source/libs/executor/src/groupoperator.c +++ b/source/libs/executor/src/groupoperator.c @@ -265,7 +265,7 @@ static SSDataBlock* hashGroupbyAggregate(SOperatorInfo* pOperator, bool* newgrou SSDataBlock* pRes = pInfo->binfo.pRes; if (pOperator->status == OP_RES_TO_RETURN) { - doBuildResultDatablock(pRes, pOperator->resultInfo.capacity, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pInfo->binfo.rowCellInfoOffset); + doBuildResultDatablock(pRes, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pInfo->binfo.rowCellInfoOffset, pInfo->binfo.pCtx); if (pRes->info.rows == 0 || !hasRemainDataInCurrentGroup(&pInfo->groupResInfo)) { pOperator->status = OP_EXEC_DONE; } @@ -311,7 +311,7 @@ static SSDataBlock* hashGroupbyAggregate(SOperatorInfo* pOperator, bool* newgrou initGroupResInfo(&pInfo->groupResInfo, &pInfo->binfo.resultRowInfo); while(1) { - doBuildResultDatablock(pRes, pOperator->resultInfo.capacity, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pInfo->binfo.rowCellInfoOffset); + doBuildResultDatablock(pRes, &pInfo->groupResInfo, pOperator->pExpr, pInfo->aggSup.pResultBuf, pInfo->binfo.rowCellInfoOffset, pInfo->binfo.pCtx); doFilter(pInfo->pCondition, pRes); bool hasRemain = hasRemainDataInCurrentGroup(&pInfo->groupResInfo); diff --git a/source/libs/function/inc/builtinsimpl.h b/source/libs/function/inc/builtinsimpl.h index 8473138e4a..db92740fff 100644 --- a/source/libs/function/inc/builtinsimpl.h +++ b/source/libs/function/inc/builtinsimpl.h @@ -24,7 +24,7 @@ extern "C" { #include "functionMgt.h" bool functionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo* pResultInfo); -void functionFinalize(SqlFunctionCtx *pCtx); +int32_t functionFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId); EFuncDataRequired countDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow); bool getCountFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); @@ -43,12 +43,12 @@ int32_t maxFunction(SqlFunctionCtx *pCtx); bool getStddevFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); bool stddevFunctionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo* pResultInfo); int32_t stddevFunction(SqlFunctionCtx* pCtx); -void stddevFinalize(SqlFunctionCtx* pCtx); +int32_t stddevFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId); bool getPercentileFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); bool percentileFunctionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo* pResultInfo); int32_t percentileFunction(SqlFunctionCtx *pCtx); -void percentileFinalize(SqlFunctionCtx* pCtx); +int32_t percentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId); bool getDiffFuncEnv(struct SFunctionNode* pFunc, SFuncExecEnv* pEnv); bool diffFunctionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo* pResInfo); @@ -60,6 +60,7 @@ int32_t lastFunction(SqlFunctionCtx *pCtx); bool getTopBotFuncEnv(SFunctionNode* UNUSED_PARAM(pFunc), SFuncExecEnv* pEnv); int32_t topFunction(SqlFunctionCtx *pCtx); +int32_t topBotFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId); #ifdef __cplusplus } diff --git a/source/libs/function/src/builtins.c b/source/libs/function/src/builtins.c index efd8de84c4..4f5dc7d6a1 100644 --- a/source/libs/function/src/builtins.c +++ b/source/libs/function/src/builtins.c @@ -507,7 +507,7 @@ const SBuiltinFuncDefinition funcMgtBuiltins[] = { .getEnvFunc = getTopBotFuncEnv, .initFunc = functionSetup, .processFunc = topFunction, - .finalizeFunc = functionFinalize + .finalizeFunc = topBotFinalize, }, { .name = "bottom", diff --git a/source/libs/function/src/builtinsimpl.c b/source/libs/function/src/builtinsimpl.c index 0942726588..3dc2e62e92 100644 --- a/source/libs/function/src/builtinsimpl.c +++ b/source/libs/function/src/builtinsimpl.c @@ -49,11 +49,17 @@ bool functionSetup(SqlFunctionCtx *pCtx, SResultRowEntryInfo* pResultInfo) { return true; } -void functionFinalize(SqlFunctionCtx *pCtx) { - SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); +int32_t functionFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId) { + SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); - cleanupResultRowEntry(pResInfo); + SResultRowEntryInfo* pResInfo = GET_RES_INFO(pCtx); pResInfo->isNullRes = (pResInfo->numOfRes == 0)? 1:0; + cleanupResultRowEntry(pResInfo); + + char* in = GET_ROWCELL_INTERBUF(pResInfo); + colDataAppend(pCol, pBlock->info.rows, in, pResInfo->isNullRes); + + return pResInfo->numOfRes; } EFuncDataRequired countDataRequired(SFunctionNode* pFunc, STimeWindow* pTimeWindow) { @@ -612,12 +618,11 @@ int32_t stddevFunction(SqlFunctionCtx* pCtx) { return TSDB_CODE_SUCCESS; } -void stddevFinalize(SqlFunctionCtx* pCtx) { - functionFinalize(pCtx); - +int32_t stddevFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId) { SStddevRes* pStddevRes = GET_ROWCELL_INTERBUF(GET_RES_INFO(pCtx)); double avg = pStddevRes->isum / ((double) pStddevRes->count); pStddevRes->result = sqrt(pStddevRes->quadraticISum/((double)pStddevRes->count) - avg*avg); + return functionFinalize(pCtx, pBlock, slotId); } typedef struct SPercentileInfo { @@ -739,7 +744,7 @@ int32_t percentileFunction(SqlFunctionCtx *pCtx) { return TSDB_CODE_SUCCESS; } -void percentileFinalize(SqlFunctionCtx* pCtx) { +int32_t percentileFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId) { SVariant* pVal = &pCtx->param[1].param; double v = pVal->nType == TSDB_DATA_TYPE_INT ? pVal->i : pVal->d; @@ -752,7 +757,7 @@ void percentileFinalize(SqlFunctionCtx* pCtx) { } tMemBucketDestroy(pMemBucket); - functionFinalize(pCtx); + return functionFinalize(pCtx, pBlock, slotId); } bool getFirstLastFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) { @@ -1173,16 +1178,14 @@ typedef struct STopBotResItem { } STopBotResItem; typedef struct STopBotRes { - int32_t num; + int32_t pageId; +// int32_t num; STopBotResItem *pItems; } STopBotRes; bool getTopBotFuncEnv(SFunctionNode* pFunc, SFuncExecEnv* pEnv) { - SColumnNode* pColNode = (SColumnNode*) nodesListGetNode(pFunc->pParameterList, 0); - int32_t bytes = pColNode->node.resType.bytes; SValueNode* pkNode = (SValueNode*) nodesListGetNode(pFunc->pParameterList, 1); - - pEnv->calcMemSize = sizeof(STopBotRes) + pkNode->datum.i * bytes; + pEnv->calcMemSize = sizeof(STopBotRes) + pkNode->datum.i * sizeof(STopBotResItem); return true; } @@ -1194,13 +1197,14 @@ static STopBotRes *getTopBotOutputInfo(SqlFunctionCtx *pCtx) { return pRes; } -static void doAddIntoResult(STopBotRes *pRes, int32_t maxSize, void *pData, uint16_t type, uint64_t uid); +static void doAddIntoResult(STopBotRes* pRes, int32_t maxSize, void* pData, int32_t rowIndex, SSDataBlock* pSrcBlock, + uint16_t type, uint64_t uid, SResultRowEntryInfo* pEntryInfo); int32_t topFunction(SqlFunctionCtx *pCtx) { int32_t numOfElems = 0; + SResultRowEntryInfo *pResInfo = GET_RES_INFO(pCtx); STopBotRes *pRes = getTopBotOutputInfo(pCtx); - assert(pRes->num >= 0); // if ((void *)pRes->res[0] != (void *)((char *)pRes + sizeof(STopBotRes) + POINTER_BYTES * pCtx->param[0].i)) { // buildTopBotStruct(pRes, pCtx); @@ -1221,11 +1225,9 @@ int32_t topFunction(SqlFunctionCtx *pCtx) { numOfElems++; char* data = colDataGetData(pCol, i); - doAddIntoResult(pRes, pCtx->param[1].param.i, data, type, pInput->uid); + doAddIntoResult(pRes, pCtx->param[1].param.i, data, i, NULL, type, pInput->uid, pResInfo); } - // treat the result as only one result - SET_VAL(GET_RES_INFO(pCtx), numOfElems, 1); return TSDB_CODE_SUCCESS; } @@ -1256,7 +1258,9 @@ static int32_t topBotResComparFn(const void *p1, const void *p2, const void *par return (val1->v.d > val2->v.d) ? 1 : -1; } -void doAddIntoResult(STopBotRes *pRes, int32_t maxSize, void *pData, uint16_t type, uint64_t uid) { + +void doAddIntoResult(STopBotRes *pRes, int32_t maxSize, void *pData, int32_t rowIndex, SSDataBlock* pSrcBlock, uint16_t type, + uint64_t uid, SResultRowEntryInfo* pEntryInfo) { SVariant val = {0}; taosVariantCreateFromBinary(&val, pData, tDataTypes[type].bytes, type); @@ -1264,29 +1268,71 @@ void doAddIntoResult(STopBotRes *pRes, int32_t maxSize, void *pData, uint16_t ty assert(pItems != NULL); // not full yet - if (pRes->num < maxSize) { - STopBotResItem* pItem = &pItems[pRes->num]; + if (pEntryInfo->numOfRes < maxSize) { + STopBotResItem* pItem = &pItems[pEntryInfo->numOfRes]; pItem->v = val; pItem->uid = uid; pItem->tuplePos.pageId = -1; // todo set the corresponding tuple data in the disk-based buffer - pRes->num++; - taosheapsort((void *) pItem, sizeof(STopBotResItem), pRes->num, (const void *) &type, topBotResComparFn, false); + if (pRes->pageId == -1) { + SFilePage* pPage = getNewBufPage(NULL, 0, &pRes->pageId); + pPage->num = sizeof(SFilePage); + + // keep the current row data + for(int32_t i = 0; i < pSrcBlock->info.numOfCols; ++i) { + SColumnInfoData* pCol = taosArrayGet(pSrcBlock->pDataBlock, i); + bool isNull = colDataIsNull_s(pCol, rowIndex); + + + colDataGetData(pCol, rowIndex); + } + + } + + // allocate the buffer and keep the data of this row into the new allocated buffer + pEntryInfo->numOfRes++; + taosheapsort((void *) pItems, sizeof(STopBotResItem), pEntryInfo->numOfRes, (const void *) &type, topBotResComparFn, false); } else { // replace the minimum value in the result if ((IS_SIGNED_NUMERIC_TYPE(type) && val.i > pItems[0].v.i) || (IS_UNSIGNED_NUMERIC_TYPE(type) && val.u > pItems[0].v.u) || (IS_FLOAT_TYPE(type) && val.d > pItems[0].v.d)) { - STopBotResItem* pItem = &pItems[pRes->num]; + STopBotResItem* pItem = &pItems[0]; pItem->v = val; pItem->uid = uid; pItem->tuplePos.pageId = -1; // todo set the corresponding tuple data in the disk-based buffer - taosheapadjust((void *) pItem, sizeof(STopBotResItem), 0, pRes->num - 1, (const void *) &type, topBotResComparFn, NULL, false); + taosheapadjust((void *) pItems, sizeof(STopBotResItem), 0, pEntryInfo->numOfRes - 1, (const void *) &type, topBotResComparFn, NULL, false); } } } -void topBotFinalize(SqlFunctionCtx* pCtx) { - functionFinalize(pCtx); +int32_t topBotFinalize(SqlFunctionCtx* pCtx, SSDataBlock* pBlock, int32_t slotId) { + SResultRowEntryInfo *pEntryInfo = GET_RES_INFO(pCtx); + STopBotRes* pRes = GET_ROWCELL_INTERBUF(pEntryInfo); + pEntryInfo->complete = true; + int32_t type = pCtx->input.pData[0]->info.type; + SColumnInfoData* pCol = taosArrayGet(pBlock->pDataBlock, slotId); + + // todo assign the tag value and the corresponding row data + int32_t currentRow = pBlock->info.rows; + switch(type) { + case TSDB_DATA_TYPE_INT: { + for (int32_t i = 0; i < pEntryInfo->numOfRes; ++i) { + STopBotResItem* pItem = &pRes->pItems[i]; + colDataAppendInt32(pCol, currentRow++, (int32_t*)&pItem->v.i); + + int32_t pageId = pItem->tuplePos.pageId; + int32_t offset = pItem->tuplePos.offset; + if (pageId != -1) { + // todo + } + } + break; + } + } + + return pEntryInfo->numOfRes; + +// return functionFinalize(pCtx, pBlock, slotId); } \ No newline at end of file From d28afa2bd9e657ef66ed2e25c37feb873e493ab8 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Fri, 22 Apr 2022 15:26:02 +0800 Subject: [PATCH 33/51] refactor: do some internal refactor. --- source/client/src/clientImpl.c | 7 ++++++- source/common/src/tdatablock.c | 1 - 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index 10edb38bf1..890b7b87cb 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -436,7 +436,12 @@ int initEpSetFromCfg(const char* firstEp, const char* secondEp, SCorEpSet* pEpSe return -1; } - taosGetFqdnPortFromEp(firstEp, &mgmtEpSet->eps[0]); + int32_t code = taosGetFqdnPortFromEp(firstEp, &mgmtEpSet->eps[0]); + if (code != TSDB_CODE_SUCCESS) { + terrno = TSDB_CODE_TSC_INVALID_FQDN; + return terrno; + } + mgmtEpSet->numOfEps++; } diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index d796df16d3..800bcd70ce 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -31,7 +31,6 @@ int32_t taosGetFqdnPortFromEp(const char* ep, SEp* pEp) { if (pEp->port == 0) { pEp->port = tsServerPort; - return -1; } return 0; From 7cd259592f2752b336a667e881f48dc753216a15 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Fri, 22 Apr 2022 15:57:49 +0800 Subject: [PATCH 34/51] fix(driver): the user specified port that is in taos_connect API is active. --- source/client/src/clientImpl.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index 890b7b87cb..aae9303bdc 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -95,16 +95,16 @@ TAOS* taos_connect_internal(const char* ip, const char* user, const char* pass, if (initEpSetFromCfg(ip, NULL, &epSet) < 0) { return NULL; } - - if (port) { - epSet.epSet.eps[0].port = port; - } } else { if (initEpSetFromCfg(tsFirst, tsSecond, &epSet) < 0) { return NULL; } } + if (port) { + epSet.epSet.eps[0].port = port; + } + char* key = getClusterKey(user, secretEncrypt, ip, port); SAppInstInfo** pInst = NULL; From 8cfcd6d4f4c9a5493183aaed00b13ade10b94921 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Fri, 22 Apr 2022 17:48:12 +0800 Subject: [PATCH 35/51] feature/qnode --- source/client/src/clientMsgHandler.c | 3 +- source/dnode/mgmt/implement/src/dmHandle.c | 6 +- source/dnode/mnode/impl/inc/mndVgroup.h | 1 + source/dnode/mnode/impl/src/mndDb.c | 8 +-- source/libs/qworker/src/qworker.c | 8 +++ source/libs/scalar/inc/filterInt.h | 4 ++ source/libs/scalar/src/filter.c | 66 ++++++++++++++++++---- source/libs/scalar/src/sclvector.c | 18 +++++- 8 files changed, 89 insertions(+), 25 deletions(-) diff --git a/source/client/src/clientMsgHandler.c b/source/client/src/clientMsgHandler.c index 06b4c7b8e0..a33351373e 100644 --- a/source/client/src/clientMsgHandler.c +++ b/source/client/src/clientMsgHandler.c @@ -151,13 +151,14 @@ int32_t processUseDbRsp(void* param, const SDataBuf* pMsg, int32_t code) { taosMemoryFreeClear(output.dbVgroup); tscError("failed to build use db output since %s", terrstr()); - } else { + } else if (output.dbVgroup) { struct SCatalog* pCatalog = NULL; int32_t code1 = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog); if (code1 != TSDB_CODE_SUCCESS) { tscWarn("catalogGetHandle failed, clusterId:%" PRIx64 ", error:%s", pRequest->pTscObj->pAppInfo->clusterId, tstrerror(code1)); + taosMemoryFreeClear(output.dbVgroup); } else { catalogUpdateDBVgInfo(pCatalog, output.db, output.dbId, output.dbVgroup); } diff --git a/source/dnode/mgmt/implement/src/dmHandle.c b/source/dnode/mgmt/implement/src/dmHandle.c index 376f589acd..10cc1f4873 100644 --- a/source/dnode/mgmt/implement/src/dmHandle.c +++ b/source/dnode/mgmt/implement/src/dmHandle.c @@ -367,9 +367,9 @@ static int32_t dmInitMgmt(SMgmtWrapper *pWrapper) { } dmReportStartup(pDnode, "dnode-transport", "initialized"); - if (dmStartUdfd(pDnode) != 0) { - dError("failed to start udfd"); - } +// if (dmStartUdfd(pDnode) != 0) { +// dError("failed to start udfd"); +// } dInfo("dnode-mgmt is initialized"); return 0; diff --git a/source/dnode/mnode/impl/inc/mndVgroup.h b/source/dnode/mnode/impl/inc/mndVgroup.h index f42829eddf..fe8c35004d 100644 --- a/source/dnode/mnode/impl/inc/mndVgroup.h +++ b/source/dnode/mnode/impl/inc/mndVgroup.h @@ -30,6 +30,7 @@ SSdbRaw *mndVgroupActionEncode(SVgObj *pVgroup); int32_t mndAllocVgroup(SMnode *pMnode, SDbObj *pDb, SVgObj **ppVgroups); SEpSet mndGetVgroupEpset(SMnode *pMnode, const SVgObj *pVgroup); int32_t mndGetVnodesNum(SMnode *pMnode, int32_t dnodeId); +int32_t mndGetGlobalVgroupVersion(int32_t *vgId); void *mndBuildCreateVnodeReq(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup, int32_t *pContLen); void *mndBuildDropVnodeReq(SMnode *pMnode, SDnodeObj *pDnode, SDbObj *pDb, SVgObj *pVgroup, int32_t *pContLen); diff --git a/source/dnode/mnode/impl/src/mndDb.c b/source/dnode/mnode/impl/src/mndDb.c index dab587432e..9caadb7e03 100644 --- a/source/dnode/mnode/impl/src/mndDb.c +++ b/source/dnode/mnode/impl/src/mndDb.c @@ -1191,6 +1191,7 @@ static int32_t mndProcessUseDbReq(SNodeMsg *pReq) { char *p = strchr(usedbReq.db, '.'); if (p && 0 == strcmp(p + 1, TSDB_INFORMATION_SCHEMA_DB)) { memcpy(usedbRsp.db, usedbReq.db, TSDB_DB_FNAME_LEN); + //mndGetGlobalVgroupVersion(); TODO static int32_t vgVersion = 1; if (usedbReq.vgVersion < vgVersion) { usedbRsp.pVgroupInfos = taosArrayInit(10, sizeof(SVgroupInfo)); @@ -1202,16 +1203,11 @@ static int32_t mndProcessUseDbReq(SNodeMsg *pReq) { mndBuildDBVgroupInfo(NULL, pMnode, usedbRsp.pVgroupInfos); usedbRsp.vgVersion = vgVersion++; - if (taosArrayGetSize(usedbRsp.pVgroupInfos) <= 0) { - terrno = TSDB_CODE_MND_DB_NOT_EXIST; - } else { - code = 0; - } } else { usedbRsp.vgVersion = usedbReq.vgVersion; - code = 0; } usedbRsp.vgNum = taosArrayGetSize(usedbRsp.pVgroupInfos); + code = 0; // no jump, need to construct rsp } else { diff --git a/source/libs/qworker/src/qworker.c b/source/libs/qworker/src/qworker.c index a74c28cf53..7329dc76a3 100644 --- a/source/libs/qworker/src/qworker.c +++ b/source/libs/qworker/src/qworker.c @@ -1388,6 +1388,14 @@ void qwProcessHbTimerEvent(void *param, void *tmrId) { void *pIter = taosHashIterate(mgmt->schHash, NULL); while (pIter) { + SQWSchStatus *sch = (SQWSchStatus *)pIter; + if (NULL == sch->hbConnInfo.handle) { + uint64_t *sId = taosHashGetKey(pIter, NULL); + QW_DLOG("cancel send hb to sch %" PRIx64 " cause of no connection handle", *sId); + pIter = taosHashIterate(mgmt->schHash, pIter); + continue; + } + code = qwGenerateSchHbRsp(mgmt, (SQWSchStatus *)pIter, &rspList[i]); if (code) { taosHashCancelIterate(mgmt->schHash, pIter); diff --git a/source/libs/scalar/inc/filterInt.h b/source/libs/scalar/inc/filterInt.h index 99c33d3165..f4cd9dd1c1 100644 --- a/source/libs/scalar/inc/filterInt.h +++ b/source/libs/scalar/inc/filterInt.h @@ -186,6 +186,7 @@ typedef struct SFilterColCtx { typedef struct SFilterCompare { uint8_t type; + int8_t precision; uint8_t optr; uint8_t optr2; } SFilterCompare; @@ -218,6 +219,7 @@ typedef struct SFltTreeStat { int32_t code; int8_t precision; bool scalarMode; + SArray* nodeList; SFilterInfo* info; } SFltTreeStat; @@ -303,6 +305,7 @@ struct SFilterInfo { #define FILTER_GET_FIELD(i, id) (&((i)->fields[(id).type].fields[(id).idx])) #define FILTER_GET_COL_FIELD(i, idx) (&((i)->fields[FLD_TYPE_COLUMN].fields[idx])) #define FILTER_GET_COL_FIELD_TYPE(fi) (((SColumnNode *)((fi)->desc))->node.resType.type) +#define FILTER_GET_COL_FIELD_PRECISION(fi) (((SColumnNode *)((fi)->desc))->node.resType.precision) #define FILTER_GET_COL_FIELD_SIZE(fi) (((SColumnNode *)((fi)->desc))->node.resType.bytes) #define FILTER_GET_COL_FIELD_ID(fi) (((SColumnNode *)((fi)->desc))->colId) #define FILTER_GET_COL_FIELD_SLOT_ID(fi) (((SColumnNode *)((fi)->desc))->slotId) @@ -317,6 +320,7 @@ struct SFilterInfo { #define FILTER_UNIT_RIGHT_FIELD(i, u) FILTER_GET_FIELD(i, (u)->right) #define FILTER_UNIT_RIGHT2_FIELD(i, u) FILTER_GET_FIELD(i, (u)->right2) #define FILTER_UNIT_DATA_TYPE(u) ((u)->compare.type) +#define FILTER_UNIT_DATA_PRECISION(u) ((u)->compare.precision) #define FILTER_UNIT_COL_DESC(i, u) FILTER_GET_COL_FIELD_DESC(FILTER_UNIT_LEFT_FIELD(i, u)) #define FILTER_UNIT_COL_DATA(i, u, ri) FILTER_GET_COL_FIELD_DATA(FILTER_UNIT_LEFT_FIELD(i, u), ri) #define FILTER_UNIT_COL_SIZE(i, u) FILTER_GET_COL_FIELD_SIZE(FILTER_UNIT_LEFT_FIELD(i, u)) diff --git a/source/libs/scalar/src/filter.c b/source/libs/scalar/src/filter.c index e24162bccb..c17e854aa9 100644 --- a/source/libs/scalar/src/filter.c +++ b/source/libs/scalar/src/filter.c @@ -21,6 +21,7 @@ #include "sclInt.h" #include "tcompare.h" #include "tdatablock.h" +#include "ttime.h" OptrStr gOptrStr[] = { {0, "invalid"}, @@ -986,6 +987,7 @@ int32_t filterAddUnit(SFilterInfo *info, uint8_t optr, SFilterFieldId *left, SFi assert(FILTER_GET_FLAG(col->flag, FLD_TYPE_COLUMN)); info->units[info->unitNum].compare.type = FILTER_GET_COL_FIELD_TYPE(col); + info->units[info->unitNum].compare.precision = FILTER_GET_COL_FIELD_PRECISION(col); *uidx = info->unitNum; @@ -1748,6 +1750,7 @@ int32_t fltInitValFieldData(SFilterInfo *info) { assert(FILTER_GET_FLAG(right->flag, FLD_TYPE_VALUE)); uint32_t type = FILTER_UNIT_DATA_TYPE(unit); + int8_t precision = FILTER_UNIT_DATA_PRECISION(unit); SFilterField* fi = right; SValueNode* var = (SValueNode *)fi->desc; @@ -1801,6 +1804,7 @@ int32_t fltInitValFieldData(SFilterInfo *info) { } else { SScalarParam out = {.columnData = taosMemoryCalloc(1, sizeof(SColumnInfoData))}; out.columnData->info.type = type; + out.columnData->info.precision = precision; if (IS_VAR_DATA_TYPE(type)) { out.columnData->info.bytes = bytes; } else { @@ -3475,6 +3479,33 @@ int32_t filterFreeNcharColumns(SFilterInfo* info) { return TSDB_CODE_SUCCESS; } +int32_t fltAddValueNodeToConverList(SFltTreeStat *stat, SValueNode* pNode) { + if (NULL == stat->nodeList) { + stat->nodeList = taosArrayInit(10, POINTER_BYTES); + if (NULL == stat->nodeList) { + FLT_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); + } + } + + if (NULL == taosArrayPush(stat->nodeList, &pNode)) { + FLT_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); + } + + return TSDB_CODE_SUCCESS; +} + +void fltConvertToTsValueNode(SFltTreeStat *stat, SValueNode* valueNode) { + char *timeStr = valueNode->datum.p; + if (convertStringToTimestamp(valueNode->node.resType.type, valueNode->datum.p, stat->precision, &valueNode->datum.i) != + TSDB_CODE_SUCCESS) { + valueNode->datum.i = 0; + } + taosMemoryFree(timeStr); + + valueNode->node.resType.type = TSDB_DATA_TYPE_TIMESTAMP; + valueNode->node.resType.bytes = tDataTypes[TSDB_DATA_TYPE_TIMESTAMP].bytes; +} + EDealRes fltReviseRewriter(SNode** pNode, void* pContext) { SFltTreeStat *stat = (SFltTreeStat *)pContext; @@ -3504,25 +3535,23 @@ EDealRes fltReviseRewriter(SNode** pNode, void* pContext) { } SValueNode *valueNode = (SValueNode *)*pNode; - if (TSDB_DATA_TYPE_BINARY != valueNode->node.resType.type) { + if (TSDB_DATA_TYPE_BINARY != valueNode->node.resType.type && TSDB_DATA_TYPE_NCHAR != valueNode->node.resType.type) { return DEAL_RES_CONTINUE; } -#if 0 if (stat->precision < 0) { - //TODO + int32_t code = fltAddValueNodeToConverList(stat, valueNode); + if (code) { + stat->code = code; + return DEAL_RES_ERROR; + } + return DEAL_RES_CONTINUE; } - char *timeStr = valueNode->datum.p; - if (taosParseTime(valueNode->datum.p, &valueNode->datum.i, valueNode->node.resType.bytes, stat->precision, tsDaylight) != - TSDB_CODE_SUCCESS) { - return generateDealNodeErrMsg(pCxt, TSDB_CODE_PAR_WRONG_VALUE_TYPE, pVal->literal); - } - TODO -#else + fltConvertToTsValueNode(stat, valueNode); + return DEAL_RES_CONTINUE; -#endif } if (QUERY_NODE_COLUMN == nodeType(*pNode)) { @@ -3619,9 +3648,22 @@ EDealRes fltReviseRewriter(SNode** pNode, void* pContext) { } int32_t fltReviseNodes(SFilterInfo *pInfo, SNode** pNode, SFltTreeStat *pStat) { + int32_t code = 0; nodesRewriteExprPostOrder(pNode, fltReviseRewriter, (void *)pStat); - FLT_RET(pStat->code); + FLT_ERR_JRET(pStat->code); + + int32_t nodeNum = taosArrayGetSize(pStat->nodeList); + for (int32_t i = 0; i < nodeNum; ++i) { + SValueNode *valueNode = *(SValueNode **)taosArrayGet(pStat->nodeList, i); + + fltConvertToTsValueNode(pStat, valueNode); + } + +_return: + + taosArrayDestroy(pStat->nodeList); + FLT_RET(code); } int32_t fltOptimizeNodes(SFilterInfo *pInfo, SNode** pNode, SFltTreeStat *pStat) { diff --git a/source/libs/scalar/src/sclvector.c b/source/libs/scalar/src/sclvector.c index d484738328..f63e539a96 100644 --- a/source/libs/scalar/src/sclvector.c +++ b/source/libs/scalar/src/sclvector.c @@ -24,6 +24,7 @@ #include "tcompare.h" #include "tdatablock.h" #include "ttypes.h" +#include "ttime.h" #define LEFT_COL ((pLeftCol->info.type == TSDB_DATA_TYPE_JSON ? (void*)pLeftCol : pLeftCol->pData)) #define RIGHT_COL ((pRightCol->info.type == TSDB_DATA_TYPE_JSON ? (void*)pRightCol : pRightCol->pData)) @@ -252,6 +253,15 @@ _getValueAddr_fn_t getVectorValueAddrFn(int32_t srcType) { return p; } +static FORCE_INLINE void varToTimestamp(char *buf, SScalarParam* pOut, int32_t rowIndex) { + int64_t value = 0; + if (taosParseTime(buf, &value, strlen(buf), pOut->columnData->info.precision, tsDaylight) != TSDB_CODE_SUCCESS) { + value = 0; + } + + colDataAppendInt64(pOut->columnData, rowIndex, &value); +} + static FORCE_INLINE void varToSigned(char *buf, SScalarParam* pOut, int32_t rowIndex) { int64_t value = strtoll(buf, NULL, 10); colDataAppendInt64(pOut->columnData, rowIndex, &value); @@ -295,7 +305,7 @@ int32_t vectorConvertFromVarData(const SScalarParam* pIn, SScalarParam* pOut, in _bufConverteFunc func = NULL; if (TSDB_DATA_TYPE_BOOL == outType) { func = varToBool; - } else if (IS_SIGNED_NUMERIC_TYPE(outType) || TSDB_DATA_TYPE_TIMESTAMP == outType) { + } else if (IS_SIGNED_NUMERIC_TYPE(outType)) { func = varToSigned; } else if (IS_UNSIGNED_NUMERIC_TYPE(outType)) { func = varToUnsigned; @@ -305,6 +315,8 @@ int32_t vectorConvertFromVarData(const SScalarParam* pIn, SScalarParam* pOut, in ASSERT(inType == TSDB_DATA_TYPE_VARCHAR); func = varToNchar; vton = true; + } else if (TSDB_DATA_TYPE_TIMESTAMP == outType) { + func = varToTimestamp; } else { sclError("invalid convert outType:%d", outType); return TSDB_CODE_QRY_APP_ERROR; @@ -594,8 +606,8 @@ int8_t gConvertTypes[TSDB_DATA_TYPE_BLOB+1][TSDB_DATA_TYPE_BLOB+1] = { /*BIGI*/ 0, 0, 0, 0, 0, 0, 6, 7, 7, 0, 7, 5, 5, 5, 7, 0, 7, 0, 0, /*FLOA*/ 0, 0, 0, 0, 0, 0, 0, 7, 7, 6, 7, 6, 6, 6, 6, 0, 7, 0, 0, /*DOUB*/ 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 7, 7, 7, 0, 7, 0, 0, -/*VARC*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, 7, 7, 7, 7, 0, 0, 0, 0, -/*TIME*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 9, 9, 9, 7, 0, 7, 0, 0, +/*VARC*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 0, 7, 7, 7, 7, 0, 0, 0, 0, +/*TIME*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 9, 9, 9, 9, 7, 0, 7, 0, 0, /*NCHA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 7, 7, 7, 0, 0, 0, 0, /*UTIN*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 12, 13, 14, 0, 7, 0, 0, /*USMA*/ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 13, 14, 0, 7, 0, 0, From d53b3a4480cf18b322a6303c747768294a517c8a Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Fri, 22 Apr 2022 18:41:32 +0800 Subject: [PATCH 36/51] call syncCleanUp taosMemoryFree(pSyncEnv) --- source/dnode/mgmt/mgmt_vnode/src/vmInt.c | 3 +++ source/libs/sync/src/syncEnv.c | 1 + 2 files changed, 4 insertions(+) diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c index 77171e8b4b..ffbc6eb3b9 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c @@ -273,6 +273,9 @@ static void vmCleanup(SMgmtWrapper *pWrapper) { // walCleanUp(); taosMemoryFree(pMgmt); pWrapper->pMgmt = NULL; + + syncCleanUp(); + dInfo("vnode-mgmt is cleaned up"); } diff --git a/source/libs/sync/src/syncEnv.c b/source/libs/sync/src/syncEnv.c index b4cbe493f2..0e04793a83 100644 --- a/source/libs/sync/src/syncEnv.c +++ b/source/libs/sync/src/syncEnv.c @@ -92,6 +92,7 @@ static SSyncEnv *doSyncEnvStart() { static int32_t doSyncEnvStop(SSyncEnv *pSyncEnv) { taosTmrCleanUp(pSyncEnv->pTimerManager); + taosMemoryFree(pSyncEnv); return 0; } From 11cb0afa300693105d45ba9d9ea921c3b176b3cc Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Fri, 22 Apr 2022 19:28:27 +0800 Subject: [PATCH 37/51] feature/qnode --- include/common/tmsg.h | 8 +++++++- include/libs/qcom/query.h | 6 ------ source/common/src/tmsg.c | 27 +++++++++++++++++++------- source/dnode/mnode/impl/src/mndQnode.c | 14 +++++++------ source/libs/qcom/src/querymsg.c | 4 ++-- 5 files changed, 37 insertions(+), 22 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 5ea53b5510..d33244a3dd 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -639,13 +639,19 @@ int32_t tSerializeSQnodeListReq(void* buf, int32_t bufLen, SQnodeListReq* pReq); int32_t tDeserializeSQnodeListReq(void* buf, int32_t bufLen, SQnodeListReq* pReq); typedef struct { - SArray* epSetList; // SArray + SArray* addrsList; // SArray } SQnodeListRsp; int32_t tSerializeSQnodeListRsp(void* buf, int32_t bufLen, SQnodeListRsp* pRsp); int32_t tDeserializeSQnodeListRsp(void* buf, int32_t bufLen, SQnodeListRsp* pRsp); void tFreeSQnodeListRsp(SQnodeListRsp* pRsp); +typedef struct SQueryNodeAddr { + int32_t nodeId; // vgId or qnodeId + SEpSet epSet; +} SQueryNodeAddr; + + typedef struct { SArray* pArray; // Array of SUseDbRsp } SUseDbBatchRsp; diff --git a/include/libs/qcom/query.h b/include/libs/qcom/query.h index bb550e75e8..e50276cc69 100644 --- a/include/libs/qcom/query.h +++ b/include/libs/qcom/query.h @@ -129,12 +129,6 @@ typedef struct SMsgSendInfo { SDataBuf msgInfo; } SMsgSendInfo; -typedef struct SQueryNodeAddr { - int32_t nodeId; // vgId or qnodeId - SEpSet epSet; -} SQueryNodeAddr; - - typedef struct SQueryNodeStat { int32_t tableNum; // vg table number, unit is TSDB_TABLE_NUM_UNIT } SQueryNodeStat; diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index 0ea196f1a1..cfb82b9f41 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -114,6 +114,19 @@ int32_t tDecodeSEpSet(SCoder *pDecoder, SEpSet *pEp) { return 0; } +int32_t tEncodeSQueryNodeAddr(SCoder *pEncoder, SQueryNodeAddr *pAddr) { + if (tEncodeI32(pEncoder, pAddr->nodeId) < 0) return -1; + if (tEncodeSEpSet(pEncoder, &pAddr->epSet) < 0) return -1; + return 0; +} + +int32_t tDecodeSQueryNodeAddr(SCoder *pDecoder, SQueryNodeAddr *pAddr) { + if (tDecodeI32(pDecoder, &pAddr->nodeId) < 0) return -1; + if (tDecodeSEpSet(pDecoder, &pAddr->epSet) < 0) return -1; + return 0; +} + + int32_t taosEncodeSEpSet(void **buf, const SEpSet *pEp) { int32_t tlen = 0; tlen += taosEncodeFixedI8(buf, pEp->inUse); @@ -2058,11 +2071,11 @@ int32_t tSerializeSQnodeListRsp(void *buf, int32_t bufLen, SQnodeListRsp *pRsp) tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); if (tStartEncode(&encoder) < 0) return -1; - int32_t num = taosArrayGetSize(pRsp->epSetList); + int32_t num = taosArrayGetSize(pRsp->addrsList); if (tEncodeI32(&encoder, num) < 0) return -1; for (int32_t i = 0; i < num; ++i) { - SEpSet *epSet = taosArrayGet(pRsp->epSetList, i); - if (tEncodeSEpSet(&encoder, epSet) < 0) return -1; + SQueryNodeAddr *addr = taosArrayGet(pRsp->addrsList, i); + if (tEncodeSQueryNodeAddr(&encoder, addr) < 0) return -1; } tEndEncode(&encoder); @@ -2078,10 +2091,10 @@ int32_t tDeserializeSQnodeListRsp(void *buf, int32_t bufLen, SQnodeListRsp *pRsp if (tStartDecode(&decoder) < 0) return -1; int32_t num = 0; if (tDecodeI32(&decoder, &num) < 0) return -1; - pRsp->epSetList = taosArrayInit(num, sizeof(SEpSet)); - if (NULL == pRsp->epSetList) return -1; + pRsp->addrsList = taosArrayInit(num, sizeof(SQueryNodeAddr)); + if (NULL == pRsp->addrsList) return -1; for (int32_t i = 0; i < num; ++i) { - if (tDecodeSEpSet(&decoder, TARRAY_GET_ELEM(pRsp->epSetList, i)) < 0) return -1; + if (tDecodeSQueryNodeAddr(&decoder, TARRAY_GET_ELEM(pRsp->addrsList, i)) < 0) return -1; } tEndDecode(&decoder); @@ -2089,7 +2102,7 @@ int32_t tDeserializeSQnodeListRsp(void *buf, int32_t bufLen, SQnodeListRsp *pRsp return 0; } -void tFreeSQnodeListRsp(SQnodeListRsp *pRsp) { taosArrayDestroy(pRsp->epSetList); } +void tFreeSQnodeListRsp(SQnodeListRsp *pRsp) { taosArrayDestroy(pRsp->addrsList); } int32_t tSerializeSCompactDbReq(void *buf, int32_t bufLen, SCompactDbReq *pReq) { SCoder encoder = {0}; diff --git a/source/dnode/mnode/impl/src/mndQnode.c b/source/dnode/mnode/impl/src/mndQnode.c index 96dba9baf6..1c03ca30f4 100644 --- a/source/dnode/mnode/impl/src/mndQnode.c +++ b/source/dnode/mnode/impl/src/mndQnode.c @@ -444,8 +444,8 @@ static int32_t mndProcessQnodeListReq(SNodeMsg *pReq) { goto _OVER; } - qlistRsp.epSetList = taosArrayInit(5, sizeof(SEpSet)); - if (NULL == qlistRsp.epSetList) { + qlistRsp.addrsList = taosArrayInit(5, sizeof(SQueryNodeAddr)); + if (NULL == qlistRsp.addrsList) { mError("failed to alloc epSet while process qnode list req"); terrno = TSDB_CODE_OUT_OF_MEMORY; goto _OVER; @@ -455,11 +455,13 @@ static int32_t mndProcessQnodeListReq(SNodeMsg *pReq) { void *pIter = sdbFetch(pSdb, SDB_QNODE, NULL, (void **)&pObj); if (pIter == NULL) break; - SEpSet epSet = {.numOfEps = 1}; - tstrncpy(epSet.eps[0].fqdn, pObj->pDnode->fqdn, TSDB_FQDN_LEN); - epSet.eps[0].port = pObj->pDnode->port; + SQueryNodeAddr nodeAddr = {0}; + nodeAddr.nodeId = QNODE_HANDLE; + nodeAddr.epSet.numOfEps = 1; + tstrncpy(nodeAddr.epSet.eps[0].fqdn, pObj->pDnode->fqdn, TSDB_FQDN_LEN); + nodeAddr.epSet.eps[0].port = pObj->pDnode->port; - (void)taosArrayPush(qlistRsp.epSetList, &epSet); + (void)taosArrayPush(qlistRsp.addrsList, &nodeAddr); numOfRows++; sdbRelease(pSdb, pObj); diff --git a/source/libs/qcom/src/querymsg.c b/source/libs/qcom/src/querymsg.c index 9e37784cab..de805d2c77 100644 --- a/source/libs/qcom/src/querymsg.c +++ b/source/libs/qcom/src/querymsg.c @@ -359,10 +359,10 @@ PROCESS_QLIST_OVER: if (code != 0) { tFreeSQnodeListRsp(&out); - out.epSetList = NULL; + out.addrsList = NULL; } - *(SArray **)output = out.epSetList; + *(SArray **)output = out.addrsList; return code; } From 32e988766478b1caea3dab9883899860805a1c03 Mon Sep 17 00:00:00 2001 From: Liu Jicong Date: Fri, 22 Apr 2022 19:47:00 +0800 Subject: [PATCH 38/51] feat(tmq): add push mode --- example/src/tmq.c | 2 - include/client/taos.h | 2 + include/os/osSemaphore.h | 45 +++--- include/util/tcache.h | 16 +- source/client/src/tmq.c | 12 +- source/dnode/mnode/impl/src/mndPerfSchema.c | 2 + source/dnode/mnode/impl/src/mndProfile.c | 65 ++++---- source/dnode/mnode/impl/src/mndShow.c | 2 +- source/dnode/mnode/impl/src/mndTopic.c | 4 +- source/dnode/vnode/src/inc/tq.h | 39 +++-- source/dnode/vnode/src/tq/tq.c | 167 +++++++++++++++++++- source/os/src/osSemaphore.c | 78 ++++++--- source/util/src/tcache.c | 144 +++++++++-------- source/util/test/cacheTest.cpp | 86 +++++----- 14 files changed, 441 insertions(+), 223 deletions(-) diff --git a/example/src/tmq.c b/example/src/tmq.c index 46f57799e7..2abf915fd8 100644 --- a/example/src/tmq.c +++ b/example/src/tmq.c @@ -101,8 +101,6 @@ int32_t create_topic() { } taos_free_result(pRes); - /*const char* sql = "select * from tu1";*/ - /*pRes = tmq_create_topic(pConn, "test_stb_topic_1", sql, strlen(sql));*/ /*pRes = taos_query(pConn, "create topic topic_ctb_column as abc1");*/ pRes = taos_query(pConn, "create topic topic_ctb_column as select ts, c1, c2, c3 from ct1"); if (taos_errno(pRes) != 0) { diff --git a/include/client/taos.h b/include/client/taos.h index 3d139ce6d2..30e99e202c 100644 --- a/include/client/taos.h +++ b/include/client/taos.h @@ -245,6 +245,8 @@ typedef void(tmq_commit_cb(tmq_t *, tmq_resp_err_t, tmq_topic_vgroup_list_t *)); DLL_EXPORT tmq_list_t *tmq_list_new(); DLL_EXPORT int32_t tmq_list_append(tmq_list_t *, const char *); DLL_EXPORT void tmq_list_destroy(tmq_list_t *); +DLL_EXPORT int32_t tmq_list_get_size(const tmq_list_t *); +DLL_EXPORT char **tmq_list_to_c_array(const tmq_list_t *); #if 0 DLL_EXPORT tmq_t *tmq_consumer_new(void *conn, tmq_conf_t *conf, char *errstr, int32_t errstrLen); diff --git a/include/os/osSemaphore.h b/include/os/osSemaphore.h index 594daf1bf3..21c88c9976 100644 --- a/include/os/osSemaphore.h +++ b/include/os/osSemaphore.h @@ -22,21 +22,28 @@ extern "C" { #include -#if defined (_TD_DARWIN_64) - typedef struct tsem_s *tsem_t; - int tsem_init(tsem_t *sem, int pshared, unsigned int value); - int tsem_wait(tsem_t *sem); - int tsem_post(tsem_t *sem); - int tsem_destroy(tsem_t *sem); +#if defined(_TD_DARWIN_64) + +typedef struct tsem_s *tsem_t; + +int tsem_init(tsem_t *sem, int pshared, unsigned int value); +int tsem_wait(tsem_t *sem); +int tsem_timewait(tsem_t *sim, int64_t nanosecs); +int tsem_post(tsem_t *sem); +int tsem_destroy(tsem_t *sem); + #else - #define tsem_t sem_t - #define tsem_init sem_init - int tsem_wait(tsem_t* sem); - #define tsem_post sem_post - #define tsem_destroy sem_destroy + +#define tsem_t sem_t +#define tsem_init sem_init +int tsem_wait(tsem_t *sem); +int tsem_timewait(tsem_t *sim, int64_t nanosecs); +#define tsem_post sem_post +#define tsem_destroy sem_destroy + #endif -#if defined (_TD_DARWIN_64) +#if defined(_TD_DARWIN_64) // #define TdThreadRwlock TdThreadMutex // #define taosThreadRwlockInit(lock, NULL) taosThreadMutexInit(lock, NULL) // #define taosThreadRwlockDestroy(lock) taosThreadMutexDestroy(lock) @@ -44,20 +51,20 @@ extern "C" { // #define taosThreadRwlockRdlock(lock) taosThreadMutexLock(lock) // #define taosThreadRwlockUnlock(lock) taosThreadMutexUnlock(lock) - #define TdThreadSpinlock TdThreadMutex - #define taosThreadSpinInit(lock, NULL) taosThreadMutexInit(lock, NULL) - #define taosThreadSpinDestroy(lock) taosThreadMutexDestroy(lock) - #define taosThreadSpinLock(lock) taosThreadMutexLock(lock) - #define taosThreadSpinUnlock(lock) taosThreadMutexUnlock(lock) +#define TdThreadSpinlock TdThreadMutex +#define taosThreadSpinInit(lock, NULL) taosThreadMutexInit(lock, NULL) +#define taosThreadSpinDestroy(lock) taosThreadMutexDestroy(lock) +#define taosThreadSpinLock(lock) taosThreadMutexLock(lock) +#define taosThreadSpinUnlock(lock) taosThreadMutexUnlock(lock) #endif bool taosCheckPthreadValid(TdThread thread); int64_t taosGetSelfPthreadId(); int64_t taosGetPthreadId(TdThread thread); -void taosResetPthread(TdThread* thread); +void taosResetPthread(TdThread *thread); bool taosComparePthread(TdThread first, TdThread second); int32_t taosGetPId(); -int32_t taosGetAppName(char* name, int32_t* len); +int32_t taosGetAppName(char *name, int32_t *len); #ifdef __cplusplus } diff --git a/include/util/tcache.h b/include/util/tcache.h index b5c1578380..d8ab018570 100644 --- a/include/util/tcache.h +++ b/include/util/tcache.h @@ -47,13 +47,13 @@ typedef struct STrashElem STrashElem; /** * initialize the cache object * @param keyType key type - * @param refreshTimeInSeconds refresh operation interval time, the maximum survival time when one element is expired + * @param refreshTimeInMs refresh operation interval time, the maximum survival time when one element is expired * and not referenced by other objects * @param extendLifespan auto extend lifespan, if accessed * @param fn free resource callback function * @return */ -SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInSeconds, bool extendLifespan, __cache_free_fn_t fn, +SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInMs, bool extendLifespan, __cache_free_fn_t fn, const char *cacheName); /** @@ -111,7 +111,7 @@ void taosCacheRelease(SCacheObj *pCacheObj, void **data, bool _remove); * @param pCacheObj * @return */ -size_t taosCacheGetNumOfObj(const SCacheObj* pCacheObj); +size_t taosCacheGetNumOfObj(const SCacheObj *pCacheObj); /** * move all data node into trash, clear node in trash can if it is not referenced by any clients @@ -145,11 +145,11 @@ void taosCacheRefresh(SCacheObj *pCacheObj, __cache_trav_fn_t fp, void *param1); */ void taosStopCacheRefreshWorker(); -SCacheIter* taosCacheCreateIter(const SCacheObj* pCacheObj); -bool taosCacheIterNext(SCacheIter* pIter); -void* taosCacheIterGetData(const SCacheIter* pIter, size_t* dataLen); -void* taosCacheIterGetKey(const SCacheIter* pIter, size_t* keyLen); -void taosCacheDestroyIter(SCacheIter* pIter); +SCacheIter *taosCacheCreateIter(const SCacheObj *pCacheObj); +bool taosCacheIterNext(SCacheIter *pIter); +void *taosCacheIterGetData(const SCacheIter *pIter, size_t *dataLen); +void *taosCacheIterGetKey(const SCacheIter *pIter, size_t *keyLen); +void taosCacheDestroyIter(SCacheIter *pIter); #ifdef __cplusplus } diff --git a/source/client/src/tmq.c b/source/client/src/tmq.c index 51e422f5c2..4dc0e6e5cf 100644 --- a/source/client/src/tmq.c +++ b/source/client/src/tmq.c @@ -260,6 +260,16 @@ void tmq_list_destroy(tmq_list_t* list) { taosArrayDestroy(container); } +int32_t tmq_list_get_size(const tmq_list_t* list) { + const SArray* container = &list->container; + return taosArrayGetSize(container); +} + +char** tmq_list_to_c_array(const tmq_list_t* list) { + const SArray* container = &list->container; + return container->pData; +} + static int32_t tmqMakeTopicVgKey(char* dst, const char* topicName, int32_t vg) { return sprintf(dst, "%s:%d", topicName, vg); } @@ -387,7 +397,7 @@ tmq_t* tmq_consumer_new(tmq_conf_t* conf, char* errstr, int32_t errstrLen) { pTmq->commit_cb = conf->commit_cb; pTmq->resetOffsetCfg = conf->resetOffset; - pTmq->consumerId = generateRequestId() & (((uint64_t)-1) >> 1); + pTmq->consumerId = tGenIdPI64(); pTmq->clientTopics = taosArrayInit(0, sizeof(SMqClientTopic)); if (pTmq->clientTopics == NULL) { taosMemoryFree(pTmq); diff --git a/source/dnode/mnode/impl/src/mndPerfSchema.c b/source/dnode/mnode/impl/src/mndPerfSchema.c index fe96bcb709..737068a2dd 100644 --- a/source/dnode/mnode/impl/src/mndPerfSchema.c +++ b/source/dnode/mnode/impl/src/mndPerfSchema.c @@ -49,6 +49,7 @@ static const SPerfsTableSchema topicSchema[] = { static const SPerfsTableSchema consumerSchema[] = { {.name = "client_id", .bytes = SYSTABLE_SCH_TABLE_NAME_LEN, .type = TSDB_DATA_TYPE_BINARY}, + {.name = "app_id", .bytes = SYSTABLE_SCH_TABLE_NAME_LEN, .type = TSDB_DATA_TYPE_BINARY}, {.name = "group_id", .bytes = SYSTABLE_SCH_TABLE_NAME_LEN, .type = TSDB_DATA_TYPE_BINARY}, {.name = "pid", .bytes = 4, .type = TSDB_DATA_TYPE_INT}, {.name = "status", .bytes = 4, .type = TSDB_DATA_TYPE_INT}, @@ -61,6 +62,7 @@ static const SPerfsTableSchema subscribeSchema[] = { {.name = "topic_name", .bytes = SYSTABLE_SCH_TABLE_NAME_LEN, .type = TSDB_DATA_TYPE_BINARY}, {.name = "group_id", .bytes = SYSTABLE_SCH_TABLE_NAME_LEN, .type = TSDB_DATA_TYPE_BINARY}, {.name = "vgroup_id", .bytes = 4, .type = TSDB_DATA_TYPE_INT}, + {.name = "offset", .bytes = 8, .type = TSDB_DATA_TYPE_BIGINT}, {.name = "client_id", .bytes = SYSTABLE_SCH_TABLE_NAME_LEN, .type = TSDB_DATA_TYPE_BINARY}, }; diff --git a/source/dnode/mnode/impl/src/mndProfile.c b/source/dnode/mnode/impl/src/mndProfile.c index 826a73afc6..fef57196f0 100644 --- a/source/dnode/mnode/impl/src/mndProfile.c +++ b/source/dnode/mnode/impl/src/mndProfile.c @@ -24,20 +24,20 @@ #include "version.h" typedef struct { - uint32_t id; - int8_t connType; - char user[TSDB_USER_LEN]; - char app[TSDB_APP_NAME_LEN]; // app name that invokes taosc - int64_t appStartTimeMs; // app start time - int32_t pid; // pid of app that invokes taosc - uint32_t ip; - uint16_t port; - int8_t killed; - int64_t loginTimeMs; - int64_t lastAccessTimeMs; - uint64_t killId; - int32_t numOfQueries; - SArray *pQueries; //SArray + uint32_t id; + int8_t connType; + char user[TSDB_USER_LEN]; + char app[TSDB_APP_NAME_LEN]; // app name that invokes taosc + int64_t appStartTimeMs; // app start time + int32_t pid; // pid of app that invokes taosc + uint32_t ip; + uint16_t port; + int8_t killed; + int64_t loginTimeMs; + int64_t lastAccessTimeMs; + uint64_t killId; + int32_t numOfQueries; + SArray *pQueries; // SArray } SConnObj; static SConnObj *mndCreateConn(SMnode *pMnode, const char *user, int8_t connType, uint32_t ip, uint16_t port, @@ -58,7 +58,8 @@ static void mndCancelGetNextQuery(SMnode *pMnode, void *pIter); int32_t mndInitProfile(SMnode *pMnode) { SProfileMgmt *pMgmt = &pMnode->profileMgmt; - int32_t connCheckTime = tsShellActivityTimer * 2; + // in ms + int32_t connCheckTime = tsShellActivityTimer * 2 * 1000; pMgmt->cache = taosCacheInit(TSDB_DATA_TYPE_INT, connCheckTime, true, (__cache_free_fn_t)mndFreeConn, "conn"); if (pMgmt->cache == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; @@ -71,9 +72,9 @@ int32_t mndInitProfile(SMnode *pMnode) { mndSetMsgHandle(pMnode, TDMT_MND_KILL_QUERY, mndProcessKillQueryReq); mndSetMsgHandle(pMnode, TDMT_MND_KILL_CONN, mndProcessKillConnReq); -// mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONNS, mndRetrieveConns); + // mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_CONNS, mndRetrieveConns); mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_CONNS, mndCancelGetNextConn); -// mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_QUERIES, mndRetrieveQueries); + // mndAddShowRetrieveHandle(pMnode, TSDB_MGMT_TABLE_QUERIES, mndRetrieveQueries); mndAddShowFreeIterHandle(pMnode, TSDB_MGMT_TABLE_QUERIES, mndCancelGetNextQuery); return 0; @@ -91,7 +92,7 @@ static SConnObj *mndCreateConn(SMnode *pMnode, const char *user, int8_t connType int32_t pid, const char *app, int64_t startTime) { SProfileMgmt *pMgmt = &pMnode->profileMgmt; - char connStr[255] = {0}; + char connStr[255] = {0}; int32_t len = snprintf(connStr, sizeof(connStr), "%s%d%d%d%s", user, ip, port, pid, app); int32_t connId = mndGenerateUid(connStr, len); if (startTime == 0) startTime = taosGetTimestampMs(); @@ -253,8 +254,8 @@ static int32_t mndSaveQueryList(SConnObj *pConn, SQueryHbReqBasic *pBasic) { pConn->pQueries = pBasic->queryDesc; pBasic->queryDesc = NULL; - - pConn->numOfQueries = pBasic->queryDesc ? taosArrayGetSize(pBasic->queryDesc) : 0; + + pConn->numOfQueries = pBasic->queryDesc ? taosArrayGetSize(pBasic->queryDesc) : 0; return TSDB_CODE_SUCCESS; } @@ -324,9 +325,10 @@ static SClientHbRsp *mndMqHbBuildRsp(SMnode *pMnode, SClientHbReq *pReq) { return NULL; } -static int32_t mndProcessQueryHeartBeat(SMnode *pMnode, SRpcMsg *pMsg, SClientHbReq *pHbReq, SClientHbBatchRsp *pBatchRsp) { +static int32_t mndProcessQueryHeartBeat(SMnode *pMnode, SRpcMsg *pMsg, SClientHbReq *pHbReq, + SClientHbBatchRsp *pBatchRsp) { SProfileMgmt *pMgmt = &pMnode->profileMgmt; - SClientHbRsp hbRsp = {.connKey = pHbReq->connKey, .status = 0, .info = NULL, .query = NULL}; + SClientHbRsp hbRsp = {.connKey = pHbReq->connKey, .status = 0, .info = NULL, .query = NULL}; if (pHbReq->query) { SQueryHbReqBasic *pBasic = pHbReq->query; @@ -335,8 +337,9 @@ static int32_t mndProcessQueryHeartBeat(SMnode *pMnode, SRpcMsg *pMsg, SClientHb rpcGetConnInfo(pMsg->handle, &connInfo); SConnObj *pConn = mndAcquireConn(pMnode, pBasic->connId); - if (pConn == NULL) { - pConn = mndCreateConn(pMnode, connInfo.user, CONN_TYPE__QUERY, connInfo.clientIp, connInfo.clientPort, pBasic->pid, pBasic->app, 0); + if (pConn == NULL) { + pConn = mndCreateConn(pMnode, connInfo.user, CONN_TYPE__QUERY, connInfo.clientIp, connInfo.clientPort, + pBasic->pid, pBasic->app, 0); if (pConn == NULL) { mError("user:%s, conn:%u is freed and failed to create new since %s", connInfo.user, pBasic->connId, terrstr()); return -1; @@ -345,7 +348,7 @@ static int32_t mndProcessQueryHeartBeat(SMnode *pMnode, SRpcMsg *pMsg, SClientHb } } else if (pConn->killed) { mError("user:%s, conn:%u is already killed", connInfo.user, pConn->id); - mndReleaseConn(pMnode, pConn); + mndReleaseConn(pMnode, pConn); terrno = TSDB_CODE_MND_INVALID_CONNECTION; return -1; } @@ -369,8 +372,8 @@ static int32_t mndProcessQueryHeartBeat(SMnode *pMnode, SRpcMsg *pMsg, SClientHb } rspBasic->connId = pConn->id; - rspBasic->totalDnodes = 1; //TODO - rspBasic->onlineDnodes = 1; //TODO + rspBasic->totalDnodes = 1; // TODO + rspBasic->onlineDnodes = 1; // TODO mndGetMnodeEpSet(pMnode, &rspBasic->epSet); mndReleaseConn(pMnode, pConn); @@ -379,7 +382,7 @@ static int32_t mndProcessQueryHeartBeat(SMnode *pMnode, SRpcMsg *pMsg, SClientHb int32_t kvNum = taosHashGetSize(pHbReq->info); if (NULL == pHbReq->info || kvNum <= 0) { - taosArrayPush(pBatchRsp->rsps, &hbRsp); + taosArrayPush(pBatchRsp->rsps, &hbRsp); return TSDB_CODE_SUCCESS; } @@ -604,8 +607,8 @@ static int32_t mndRetrieveConns(SNodeMsg *pReq, SShowObj *pShow, char *data, int } static int32_t mndRetrieveQueries(SNodeMsg *pReq, SShowObj *pShow, char *data, int32_t rows) { - SMnode *pMnode = pReq->pNode; - int32_t numOfRows = 0; + SMnode *pMnode = pReq->pNode; + int32_t numOfRows = 0; #if 0 SConnObj *pConn = NULL; int32_t cols = 0; @@ -703,7 +706,7 @@ static int32_t mndRetrieveQueries(SNodeMsg *pReq, SShowObj *pShow, char *data, i } pShow->numOfRows += numOfRows; -#endif +#endif return numOfRows; } diff --git a/source/dnode/mnode/impl/src/mndShow.c b/source/dnode/mnode/impl/src/mndShow.c index 1a14c94640..804fa2e17f 100644 --- a/source/dnode/mnode/impl/src/mndShow.c +++ b/source/dnode/mnode/impl/src/mndShow.c @@ -28,7 +28,7 @@ static int32_t mndProcessRetrieveSysTableReq(SNodeMsg *pReq); int32_t mndInitShow(SMnode *pMnode) { SShowMgmt *pMgmt = &pMnode->showMgmt; - pMgmt->cache = taosCacheInit(TSDB_DATA_TYPE_INT, 5, true, (__cache_free_fn_t)mndFreeShowObj, "show"); + pMgmt->cache = taosCacheInit(TSDB_DATA_TYPE_INT, 5000, true, (__cache_free_fn_t)mndFreeShowObj, "show"); if (pMgmt->cache == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; mError("failed to alloc show cache since %s", terrstr()); diff --git a/source/dnode/mnode/impl/src/mndTopic.c b/source/dnode/mnode/impl/src/mndTopic.c index f17b8a4d88..a4b98ba01a 100644 --- a/source/dnode/mnode/impl/src/mndTopic.c +++ b/source/dnode/mnode/impl/src/mndTopic.c @@ -534,8 +534,8 @@ static int32_t mndRetrieveTopic(SNodeMsg *pReq, SShowObj *pShow, SSDataBlock *pB colDataAppend(pColInfo, numOfRows, (const char *)&pTopic->createTime, false); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); - char *sql = taosMemoryCalloc(1, strlen(pTopic->sql) + 1 + VARSTR_HEADER_SIZE); - strcpy(&sql[VARSTR_HEADER_SIZE], pTopic->sql); + char sql[TSDB_SHOW_SQL_LEN + VARSTR_HEADER_SIZE] = {0}; + tstrncpy(&sql[VARSTR_HEADER_SIZE], pTopic->sql, TSDB_SHOW_SQL_LEN); varDataSetLen(sql, strlen(&sql[VARSTR_HEADER_SIZE])); colDataAppend(pColInfo, numOfRows, (const char *)sql, false); diff --git a/source/dnode/vnode/src/inc/tq.h b/source/dnode/vnode/src/inc/tq.h index 6f929ce829..2250aab35c 100644 --- a/source/dnode/vnode/src/inc/tq.h +++ b/source/dnode/vnode/src/inc/tq.h @@ -18,8 +18,10 @@ #include "executor.h" #include "os.h" +#include "tcache.h" #include "thash.h" #include "tmsg.h" +#include "trpc.h" #include "ttimer.h" #include "wal.h" @@ -142,26 +144,37 @@ typedef struct { } STqMetaStore; typedef struct { - char subKey[TSDB_SUBSCRIBE_KEY_LEN]; - int64_t consumerId; - int32_t epoch; - int8_t subType; - int8_t withTbName; - int8_t withSchema; - int8_t withTag; - int8_t withTagSchema; - char* qmsg; + int64_t consumerId; + int32_t epoch; + int32_t skipLogNum; + int64_t reqOffset; + SRWLatch lock; + SRpcMsg* handle; +} STqPushHandle; + +typedef struct { + char subKey[TSDB_SUBSCRIBE_KEY_LEN]; + int64_t consumerId; + int32_t epoch; + int8_t subType; + int8_t withTbName; + int8_t withSchema; + int8_t withTag; + int8_t withTagSchema; + char* qmsg; + STqPushHandle pushHandle; // SRWLatch lock; SWalReadHandle* pWalReader; - // number should be identical to fetch thread num - STqReadHandle* pStreamReader[4]; - qTaskInfo_t task[4]; + // task number should be the same with fetch thread + STqReadHandle* pExecReader[5]; + qTaskInfo_t task[5]; } STqExec; struct STQ { char* path; // STqMetaStore* tqMeta; - SHashObj* execs; // subKey -> tqExec + SHashObj* pushMgr; // consumerId -> STqExec* + SHashObj* execs; // subKey -> STqExec SHashObj* pStreamTasks; SVnode* pVnode; SWal* pWal; diff --git a/source/dnode/vnode/src/tq/tq.c b/source/dnode/vnode/src/tq/tq.c index 30f75218b4..aa04ae3b46 100644 --- a/source/dnode/vnode/src/tq/tq.c +++ b/source/dnode/vnode/src/tq/tq.c @@ -41,6 +41,8 @@ STQ* tqOpen(const char* path, SVnode* pVnode, SWal* pWal) { pTq->pStreamTasks = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_NO_LOCK); + pTq->pushMgr = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK); + return pTq; } @@ -52,8 +54,139 @@ void tqClose(STQ* pTq) { // TODO } +int32_t tqPushMsgNew(STQ* pTq, void* msg, int32_t msgLen, tmsg_t msgType, int64_t ver) { + if (msgType != TDMT_VND_SUBMIT) return 0; + void* pIter = NULL; + STqExec* pExec = NULL; + SSubmitReq* pReq = (SSubmitReq*)msg; + int32_t workerId = 4; + int64_t fetchOffset = ver; + + while (1) { + pIter = taosHashIterate(pTq->pushMgr, pIter); + if (pIter == NULL) break; + pExec = (STqExec**)pIter; + + taosWLockLatch(&pExec->pushHandle.lock); + + SRpcMsg* pMsg = atomic_load_ptr(&pExec->pushHandle.handle); + ASSERT(pMsg); + + SMqDataBlkRsp rsp = {0}; + rsp.reqOffset = pExec->pushHandle.reqOffset; + rsp.blockData = taosArrayInit(0, sizeof(int32_t)); + rsp.blockDataLen = taosArrayInit(0, sizeof(int32_t)); + + if (pExec->subType == TOPIC_SUB_TYPE__TABLE) { + qTaskInfo_t task = pExec->task[workerId]; + ASSERT(task); + qSetStreamInput(task, pReq, STREAM_DATA_TYPE_SUBMIT_BLOCK); + while (1) { + SSDataBlock* pDataBlock = NULL; + uint64_t ts = 0; + if (qExecTask(task, &pDataBlock, &ts) < 0) { + ASSERT(0); + } + if (pDataBlock == NULL) break; + + ASSERT(pDataBlock->info.rows != 0); + ASSERT(pDataBlock->info.numOfCols != 0); + + int32_t dataStrLen = sizeof(SRetrieveTableRsp) + blockGetEncodeSize(pDataBlock); + void* buf = taosMemoryCalloc(1, dataStrLen); + SRetrieveTableRsp* pRetrieve = (SRetrieveTableRsp*)buf; + pRetrieve->useconds = ts; + pRetrieve->precision = TSDB_DEFAULT_PRECISION; + pRetrieve->compressed = 0; + pRetrieve->completed = 1; + pRetrieve->numOfRows = htonl(pDataBlock->info.rows); + + // TODO enable compress + int32_t actualLen = 0; + blockCompressEncode(pDataBlock, pRetrieve->data, &actualLen, pDataBlock->info.numOfCols, false); + actualLen += sizeof(SRetrieveTableRsp); + ASSERT(actualLen <= dataStrLen); + taosArrayPush(rsp.blockDataLen, &actualLen); + taosArrayPush(rsp.blockData, &buf); + rsp.blockNum++; + } + } else if (pExec->subType == TOPIC_SUB_TYPE__DB) { + STqReadHandle* pReader = pExec->pExecReader[workerId]; + tqReadHandleSetMsg(pReader, pReq, 0); + while (tqNextDataBlock(pReader)) { + SSDataBlock block = {0}; + if (tqRetrieveDataBlock(&block.pDataBlock, pReader, &block.info.groupId, &block.info.rows, + &block.info.numOfCols) < 0) { + ASSERT(0); + } + int32_t dataStrLen = sizeof(SRetrieveTableRsp) + blockGetEncodeSize(&block); + void* buf = taosMemoryCalloc(1, dataStrLen); + SRetrieveTableRsp* pRetrieve = (SRetrieveTableRsp*)buf; + /*pRetrieve->useconds = 0;*/ + pRetrieve->precision = TSDB_DEFAULT_PRECISION; + pRetrieve->compressed = 0; + pRetrieve->completed = 1; + pRetrieve->numOfRows = htonl(block.info.rows); + + // TODO enable compress + int32_t actualLen = 0; + blockCompressEncode(&block, pRetrieve->data, &actualLen, block.info.numOfCols, false); + actualLen += sizeof(SRetrieveTableRsp); + ASSERT(actualLen <= dataStrLen); + taosArrayPush(rsp.blockDataLen, &actualLen); + taosArrayPush(rsp.blockData, &buf); + rsp.blockNum++; + } + } else { + ASSERT(0); + } + + if (rsp.blockNum == 0) { + taosWUnLockLatch(&pExec->pushHandle.lock); + continue; + } + + ASSERT(taosArrayGetSize(rsp.blockData) == rsp.blockNum); + ASSERT(taosArrayGetSize(rsp.blockDataLen) == rsp.blockNum); + + rsp.rspOffset = fetchOffset; + + int32_t tlen = sizeof(SMqRspHead) + tEncodeSMqDataBlkRsp(NULL, &rsp); + void* buf = rpcMallocCont(tlen); + if (buf == NULL) { + pMsg->code = -1; + return -1; + } + + ((SMqRspHead*)buf)->mqMsgType = TMQ_MSG_TYPE__POLL_RSP; + ((SMqRspHead*)buf)->epoch = pExec->pushHandle.epoch; + ((SMqRspHead*)buf)->consumerId = pExec->pushHandle.consumerId; + + void* abuf = POINTER_SHIFT(buf, sizeof(SMqRspHead)); + tEncodeSMqDataBlkRsp(&abuf, &rsp); + pMsg->pCont = buf; + pMsg->contLen = tlen; + pMsg->code = 0; + tmsgSendRsp(pMsg); + + atomic_store_ptr(&pExec->pushHandle.handle, NULL); + taosWUnLockLatch(&pExec->pushHandle.lock); + + vDebug("vg %d offset %ld from consumer %ld (epoch %d) send rsp, block num: %d, reqOffset: %ld, rspOffset: %ld", + TD_VID(pTq->pVnode), fetchOffset, pExec->pushHandle.consumerId, pExec->pushHandle.epoch, rsp.blockNum, + rsp.reqOffset, rsp.rspOffset); + + // TODO destroy + taosArrayDestroy(rsp.blockData); + taosArrayDestroy(rsp.blockDataLen); + } + + return 0; +} + int tqPushMsg(STQ* pTq, void* msg, int32_t msgLen, tmsg_t msgType, int64_t version) { if (msgType != TDMT_VND_SUBMIT) return 0; + void* data = taosMemoryMalloc(msgLen); if (data == NULL) { return -1; @@ -71,6 +204,7 @@ int tqPushMsg(STQ* pTq, void* msg, int32_t msgLen, tmsg_t msgType, int64_t versi .pCont = data, .contLen = msgLen, }; + tmsgPutToQueue(&pTq->pVnode->msgCb, FETCH_QUEUE, &req); #if 0 @@ -240,6 +374,7 @@ int32_t tqDeserializeConsumer(STQ* pTq, const STqSerializedHead* pHead, STqConsu int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) { SMqPollReqV2* pReq = pMsg->pCont; int64_t consumerId = pReq->consumerId; + int64_t waitTime = pReq->blockingTime; int32_t reqEpoch = pReq->epoch; int64_t fetchOffset; @@ -265,8 +400,8 @@ int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) { SMqDataBlkRsp rsp = {0}; rsp.reqOffset = pReq->currentOffset; - rsp.blockDataLen = taosArrayInit(0, sizeof(int32_t)); rsp.blockData = taosArrayInit(0, sizeof(void*)); + rsp.blockDataLen = taosArrayInit(0, sizeof(int32_t)); while (1) { consumerEpoch = atomic_load_32(&pExec->epoch); @@ -283,6 +418,28 @@ int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) { // response to user vDebug("tmq poll: consumer %ld (epoch %d) vg %d offset %ld, no more log to return", consumerId, pReq->epoch, TD_VID(pTq->pVnode), fetchOffset); + +#if 0 + // add to pushMgr + taosWLockLatch(&pExec->pushHandle.lock); + + pExec->pushHandle.consumerId = consumerId; + pExec->pushHandle.epoch = reqEpoch; + pExec->pushHandle.reqOffset = rsp.reqOffset; + pExec->pushHandle.skipLogNum = rsp.skipLogNum; + pExec->pushHandle.handle = pMsg; + + taosWUnLockLatch(&pExec->pushHandle.lock); + + // TODO add timer + + // TODO: the pointer will always be valid? + taosHashPut(pTq->pushMgr, &consumerId, sizeof(int64_t), &pExec, sizeof(void*)); + taosArrayDestroy(rsp.blockData); + taosArrayDestroy(rsp.blockDataLen); + return 0; +#endif + break; } @@ -325,7 +482,7 @@ int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId) { rsp.blockNum++; } } else if (pExec->subType == TOPIC_SUB_TYPE__DB) { - STqReadHandle* pReader = pExec->pStreamReader[workerId]; + STqReadHandle* pReader = pExec->pExecReader[workerId]; tqReadHandleSetMsg(pReader, pCont, 0); while (tqNextDataBlock(pReader)) { SSDataBlock block = {0}; @@ -635,10 +792,10 @@ int32_t tqProcessVgChangeReq(STQ* pTq, char* msg, int32_t msgLen) { req.qmsg = NULL; pExec->pWalReader = walOpenReadHandle(pTq->pVnode->pWal); - for (int32_t i = 0; i < 4; i++) { - pExec->pStreamReader[i] = tqInitSubmitMsgScanner(pTq->pVnode->pMeta); + for (int32_t i = 0; i < 5; i++) { + pExec->pExecReader[i] = tqInitSubmitMsgScanner(pTq->pVnode->pMeta); SReadHandle handle = { - .reader = pExec->pStreamReader[i], + .reader = pExec->pExecReader[i], .meta = pTq->pVnode->pMeta, }; pExec->task[i] = qCreateStreamExecTaskInfo(pExec->qmsg, &handle); diff --git a/source/os/src/osSemaphore.c b/source/os/src/osSemaphore.c index 1297fdbc27..893cf71a6f 100644 --- a/source/os/src/osSemaphore.c +++ b/source/os/src/osSemaphore.c @@ -85,11 +85,11 @@ int32_t tsem_wait(tsem_t* sem) { #include #include -static TdThread sem_thread; +static TdThread sem_thread; static TdThreadOnce sem_once; -static task_t sem_port; -static volatile int sem_inited = 0; -static semaphore_t sem_exit; +static task_t sem_port; +static volatile int sem_inited = 0; +static semaphore_t sem_exit; static void *sem_thread_routine(void *arg) { (void)arg; @@ -122,12 +122,12 @@ static void once_init(void) { struct tsem_s { #ifdef SEM_USE_PTHREAD - TdThreadMutex lock; - TdThreadCond cond; + TdThreadMutex lock; + TdThreadCond cond; volatile int64_t val; #elif defined(SEM_USE_POSIX) size_t id; - sem_t * sem; + sem_t *sem; #elif defined(SEM_USE_SEM) semaphore_t sem; #else // SEM_USE_PTHREAD @@ -140,7 +140,8 @@ struct tsem_s { int tsem_init(tsem_t *sem, int pshared, unsigned int value) { // fprintf(stderr, "==%s[%d]%s():[%p]==creating\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); if (*sem) { - fprintf(stderr, "==%s[%d]%s():[%p]==already initialized\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); + fprintf(stderr, "==%s[%d]%s():[%p]==already initialized\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, + sem); abort(); } struct tsem_s *p = (struct tsem_s *)taosMemoryCalloc(1, sizeof(*p)); @@ -180,20 +181,22 @@ int tsem_init(tsem_t *sem, int pshared, unsigned int value) { int e = errno; if (e == EEXIST) continue; if (e == EINTR) continue; - fprintf(stderr, "==%s[%d]%s():[%p]==not created[%d]%s\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem, e, - strerror(e)); + fprintf(stderr, "==%s[%d]%s():[%p]==not created[%d]%s\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem, + e, strerror(e)); abort(); } while (p->sem == SEM_FAILED); #elif defined(SEM_USE_SEM) taosThreadOnce(&sem_once, once_init); if (sem_inited != 1) { - fprintf(stderr, "==%s[%d]%s():[%p]==internal resource init failed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); + fprintf(stderr, "==%s[%d]%s():[%p]==internal resource init failed\n", taosDirEntryBaseName(__FILE__), __LINE__, + __func__, sem); errno = ENOMEM; return -1; } kern_return_t ret = semaphore_create(sem_port, &p->sem, SYNC_POLICY_FIFO, value); if (ret != KERN_SUCCESS) { - fprintf(stderr, "==%s[%d]%s():[%p]==semophore_create failed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); + fprintf(stderr, "==%s[%d]%s():[%p]==semophore_create failed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, + sem); // we fail-fast here, because we have less-doc about semaphore_create for the moment abort(); } @@ -224,18 +227,21 @@ int tsem_wait(tsem_t *sem) { } #ifdef SEM_USE_PTHREAD if (taosThreadMutexLock(&p->lock)) { - fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, + sem); abort(); } p->val -= 1; if (p->val < 0) { if (taosThreadCondWait(&p->cond, &p->lock)) { - fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, + sem); abort(); } } if (taosThreadMutexUnlock(&p->lock)) { - fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, + sem); abort(); } return 0; @@ -260,18 +266,21 @@ int tsem_post(tsem_t *sem) { } #ifdef SEM_USE_PTHREAD if (taosThreadMutexLock(&p->lock)) { - fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, + sem); abort(); } p->val += 1; if (p->val <= 0) { if (taosThreadCondSignal(&p->cond)) { - fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, + sem); abort(); } } if (taosThreadMutexUnlock(&p->lock)) { - fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, + sem); abort(); } return 0; @@ -293,26 +302,30 @@ int tsem_destroy(tsem_t *sem) { } struct tsem_s *p = *sem; if (!p->valid) { - // fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); - // abort(); + // fprintf(stderr, "==%s[%d]%s():[%p]==already destroyed\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, + // sem); abort(); return 0; } #ifdef SEM_USE_PTHREAD if (taosThreadMutexLock(&p->lock)) { - fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, + sem); abort(); } p->valid = 0; if (taosThreadCondDestroy(&p->cond)) { - fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, + sem); abort(); } if (taosThreadMutexUnlock(&p->lock)) { - fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, + sem); abort(); } if (taosThreadMutexDestroy(&p->lock)) { - fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem); + fprintf(stderr, "==%s[%d]%s():[%p]==internal logic error\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, + sem); abort(); } #elif defined(SEM_USE_POSIX) @@ -321,8 +334,8 @@ int tsem_destroy(tsem_t *sem) { int r = sem_unlink(name); if (r) { int e = errno; - fprintf(stderr, "==%s[%d]%s():[%p]==unlink failed[%d]%s\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem, e, - strerror(e)); + fprintf(stderr, "==%s[%d]%s():[%p]==unlink failed[%d]%s\n", taosDirEntryBaseName(__FILE__), __LINE__, __func__, sem, + e, strerror(e)); abort(); } #elif defined(SEM_USE_SEM) @@ -424,4 +437,17 @@ int32_t tsem_wait(tsem_t* sem) { return ret; } +int32_t tsem_timewait(tsem_t* sem, int64_t nanosecs) { + int ret = 0; + + struct timespec tv = { + .tv_sec = 0, + .tv_nsec = nanosecs, + }; + + while ((ret = sem_timedwait(sem, &tv)) == -1 && errno == EINTR) continue; + + return ret; +} + #endif diff --git a/source/util/src/tcache.c b/source/util/src/tcache.c index a69f11f285..e2dfde0503 100644 --- a/source/util/src/tcache.c +++ b/source/util/src/tcache.c @@ -19,16 +19,16 @@ #include "tlog.h" #include "tutil.h" -#define CACHE_MAX_CAPACITY 1024*1024*16 -#define CACHE_DEFAULT_CAPACITY 1024*4 +#define CACHE_MAX_CAPACITY 1024 * 1024 * 16 +#define CACHE_DEFAULT_CAPACITY 1024 * 4 -static TdThread cacheRefreshWorker = {0}; +static TdThread cacheRefreshWorker = {0}; static TdThreadOnce cacheThreadInit = PTHREAD_ONCE_INIT; static TdThreadMutex guard = PTHREAD_MUTEX_INITIALIZER; -static SArray *pCacheArrayList = NULL; -static bool stopRefreshWorker = false; -static bool refreshWorkerNormalStopped = false; -static bool refreshWorkerUnexpectedStopped = false; +static SArray *pCacheArrayList = NULL; +static bool stopRefreshWorker = false; +static bool refreshWorkerNormalStopped = false; +static bool refreshWorkerUnexpectedStopped = false; typedef struct SCacheNode { uint64_t addedTime; // the added time when this element is added or updated into cache @@ -36,7 +36,7 @@ typedef struct SCacheNode { int64_t expireTime; // expire time uint64_t signature; struct STrashElem *pTNodeHeader; // point to trash node head - uint16_t keyLen: 15; // max key size: 32kb + uint16_t keyLen : 15; // max key size: 32kb bool inTrashcan : 1; // denote if it is in trash or not uint32_t size; // allocated size for current SCacheNode uint32_t dataLen; @@ -47,8 +47,8 @@ typedef struct SCacheNode { } SCacheNode; typedef struct SCacheEntry { - int32_t num; // number of elements in current entry - SRWLatch latch; // entry latch + int32_t num; // number of elements in current entry + SRWLatch latch; // entry latch SCacheNode *next; } SCacheEntry; @@ -75,24 +75,24 @@ typedef struct SCacheIter { * when the node in pTrash does not be referenced, it will be release at the expired expiredTime */ struct SCacheObj { - int64_t sizeInBytes; // total allocated buffer in this hash table, SCacheObj is not included. - int64_t refreshTime; - char *name; - SCacheStatis statistics; + int64_t sizeInBytes; // total allocated buffer in this hash table, SCacheObj is not included. + int64_t refreshTime; + char *name; + SCacheStatis statistics; - SCacheEntry *pEntryList; - size_t capacity; // number of slots - size_t numOfElems; // number of elements in cache - _hash_fn_t hashFp; // hash function - __cache_free_fn_t freeFp; + SCacheEntry *pEntryList; + size_t capacity; // number of slots + size_t numOfElems; // number of elements in cache + _hash_fn_t hashFp; // hash function + __cache_free_fn_t freeFp; - uint32_t numOfElemsInTrash; // number of element in trash - STrashElem *pTrash; + uint32_t numOfElemsInTrash; // number of element in trash + STrashElem *pTrash; - uint8_t deleting; // set the deleting flag to stop refreshing ASAP. - TdThread refreshWorker; - bool extendLifespan; // auto extend life span when one item is accessed. - int64_t checkTick; // tick used to record the check times of the refresh threads + uint8_t deleting; // set the deleting flag to stop refreshing ASAP. + TdThread refreshWorker; + bool extendLifespan; // auto extend life span when one item is accessed. + int64_t checkTick; // tick used to record the check times of the refresh threads #if defined(LINUX) TdThreadRwlock lock; #else @@ -182,7 +182,7 @@ TdThread doRegisterCacheObj(SCacheObj *pCacheObj) { * @return SCacheNode */ static SCacheNode *taosCreateCacheNode(const char *key, size_t keyLen, const char *pData, size_t size, - uint64_t duration); + uint64_t duration); /** * addedTime object node into trash, and this object is closed for referencing if it is addedTime to trash @@ -267,7 +267,7 @@ static void pushfrontNodeInEntryList(SCacheEntry *pEntry, SCacheNode *pNode) { pEntry->num += 1; } -static void removeNodeInEntryList(SCacheEntry* pe, SCacheNode* prev, SCacheNode* pNode) { +static void removeNodeInEntryList(SCacheEntry *pe, SCacheNode *prev, SCacheNode *pNode) { if (prev == NULL) { ASSERT(pe->next == pNode); pe->next = pNode->pNext; @@ -279,14 +279,14 @@ static void removeNodeInEntryList(SCacheEntry* pe, SCacheNode* prev, SCacheNode* pe->num -= 1; } -static FORCE_INLINE SCacheEntry* doFindEntry(SCacheObj* pCacheObj, const void* key, size_t keyLen) { +static FORCE_INLINE SCacheEntry *doFindEntry(SCacheObj *pCacheObj, const void *key, size_t keyLen) { uint32_t hashVal = (*pCacheObj->hashFp)(key, keyLen); int32_t slot = hashVal % pCacheObj->capacity; return &pCacheObj->pEntryList[slot]; } -static FORCE_INLINE SCacheNode * -doSearchInEntryList(SCacheEntry *pe, const void *key, size_t keyLen, SCacheNode** prev) { +static FORCE_INLINE SCacheNode *doSearchInEntryList(SCacheEntry *pe, const void *key, size_t keyLen, + SCacheNode **prev) { SCacheNode *pNode = pe->next; while (pNode) { if ((pNode->keyLen == keyLen) && memcmp(pNode->key, key, keyLen) == 0) { @@ -299,9 +299,9 @@ doSearchInEntryList(SCacheEntry *pe, const void *key, size_t keyLen, SCacheNode* return pNode; } -static bool doRemoveExpiredFn(void *param, SCacheNode* pNode) { +static bool doRemoveExpiredFn(void *param, SCacheNode *pNode) { SCacheObjTravSup *ps = (SCacheObjTravSup *)param; - SCacheObj *pCacheObj = ps->pCacheObj; + SCacheObj *pCacheObj = ps->pCacheObj; if ((int64_t)pNode->expireTime < ps->time && T_REF_VAL_GET(pNode) <= 0) { taosCacheReleaseNode(pCacheObj, pNode); @@ -320,7 +320,7 @@ static bool doRemoveExpiredFn(void *param, SCacheNode* pNode) { static bool doRemoveNodeFn(void *param, SCacheNode *pNode) { SCacheObjTravSup *ps = (SCacheObjTravSup *)param; - SCacheObj *pCacheObj = ps->pCacheObj; + SCacheObj *pCacheObj = ps->pCacheObj; if (T_REF_VAL_GET(pNode) == 0) { taosCacheReleaseNode(pCacheObj, pNode); @@ -347,14 +347,14 @@ static FORCE_INLINE int32_t getCacheCapacity(int32_t length) { len = (len << 1u); } - return len > CACHE_MAX_CAPACITY? CACHE_MAX_CAPACITY:len; + return len > CACHE_MAX_CAPACITY ? CACHE_MAX_CAPACITY : len; } -SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInSeconds, bool extendLifespan, __cache_free_fn_t fn, +SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInMs, bool extendLifespan, __cache_free_fn_t fn, const char *cacheName) { const int32_t SLEEP_DURATION = 500; // 500 ms - if (refreshTimeInSeconds <= 0) { + if (refreshTimeInMs <= 0) { return NULL; } @@ -374,10 +374,10 @@ SCacheObj *taosCacheInit(int32_t keyType, int64_t refreshTimeInSeconds, bool ext } // set free cache node callback function - pCacheObj->hashFp = taosGetDefaultHashFunction(keyType); - pCacheObj->freeFp = fn; - pCacheObj->refreshTime = refreshTimeInSeconds * 1000; - pCacheObj->checkTick = pCacheObj->refreshTime / SLEEP_DURATION; + pCacheObj->hashFp = taosGetDefaultHashFunction(keyType); + pCacheObj->freeFp = fn; + pCacheObj->refreshTime = refreshTimeInMs; + pCacheObj->checkTick = pCacheObj->refreshTime / SLEEP_DURATION; pCacheObj->extendLifespan = extendLifespan; // the TTL after the last access if (__trashcan_lock_init(pCacheObj) != 0) { @@ -411,8 +411,8 @@ void *taosCachePut(SCacheObj *pCacheObj, const void *key, size_t keyLen, const v taosWLockLatch(&pe->latch); - SCacheNode *prev = NULL; - SCacheNode* pNode = doSearchInEntryList(pe, key, keyLen, &prev); + SCacheNode *prev = NULL; + SCacheNode *pNode = doSearchInEntryList(pe, key, keyLen, &prev); if (pNode == NULL) { pushfrontNodeInEntryList(pe, pNode1); @@ -460,12 +460,12 @@ void *taosCacheAcquireByKey(SCacheObj *pCacheObj, const void *key, size_t keyLen return NULL; } - SCacheNode *prev = NULL; + SCacheNode *prev = NULL; SCacheEntry *pe = doFindEntry(pCacheObj, key, keyLen); taosRLockLatch(&pe->latch); - SCacheNode* pNode = doSearchInEntryList(pe, key, keyLen, &prev); + SCacheNode *pNode = doSearchInEntryList(pe, key, keyLen, &prev); if (pNode != NULL) { int32_t ref = T_REF_INC(pNode); ASSERT(ref > 0); @@ -589,7 +589,7 @@ void taosCacheRelease(SCacheObj *pCacheObj, void **data, bool _remove) { } else { // NOTE: remove it from hash in the first place, otherwise, the pNode may have been released by other thread // when reaches here. - SCacheNode * prev = NULL; + SCacheNode *prev = NULL; SCacheEntry *pe = doFindEntry(pCacheObj, pNode->key, pNode->keyLen); taosWLockLatch(&pe->latch); @@ -646,7 +646,7 @@ void taosCacheRelease(SCacheObj *pCacheObj, void **data, bool _remove) { } } -void doTraverseElems(SCacheObj* pCacheObj, bool (*fp)(void *param, SCacheNode* pNode), SCacheObjTravSup* pSup) { +void doTraverseElems(SCacheObj *pCacheObj, bool (*fp)(void *param, SCacheNode *pNode), SCacheObjTravSup *pSup) { int32_t numOfEntries = (int32_t)pCacheObj->capacity; for (int32_t i = 0; i < numOfEntries; ++i) { SCacheEntry *pEntry = &pCacheObj->pEntryList[i]; @@ -675,7 +675,7 @@ void doTraverseElems(SCacheObj* pCacheObj, bool (*fp)(void *param, SCacheNode* p } } -void taosCacheEmpty(SCacheObj* pCacheObj) { +void taosCacheEmpty(SCacheObj *pCacheObj) { SCacheObjTravSup sup = {.pCacheObj = pCacheObj, .fp = NULL, .time = taosGetTimestampMs()}; doTraverseElems(pCacheObj, doRemoveNodeFn, &sup); taosTrashcanEmpty(pCacheObj, false); @@ -710,20 +710,20 @@ SCacheNode *taosCreateCacheNode(const char *key, size_t keyLen, const char *pDat return NULL; } - pNewNode->data = (char*)pNewNode + sizeof(SCacheNode); + pNewNode->data = (char *)pNewNode + sizeof(SCacheNode); pNewNode->dataLen = size; memcpy(pNewNode->data, pData, size); - pNewNode->key = (char *)pNewNode + sizeof(SCacheNode) + size; + pNewNode->key = (char *)pNewNode + sizeof(SCacheNode) + size; pNewNode->keyLen = (uint16_t)keyLen; memcpy(pNewNode->key, key, keyLen); - pNewNode->addedTime = (uint64_t)taosGetTimestampMs(); - pNewNode->lifespan = duration; + pNewNode->addedTime = (uint64_t)taosGetTimestampMs(); + pNewNode->lifespan = duration; pNewNode->expireTime = pNewNode->addedTime + pNewNode->lifespan; - pNewNode->signature = (uint64_t)pNewNode; - pNewNode->size = (uint32_t)sizeInBytes; + pNewNode->signature = (uint64_t)pNewNode; + pNewNode->size = (uint32_t)sizeInBytes; return pNewNode; } @@ -914,21 +914,19 @@ void taosStopCacheRefreshWorker(void) { taosArrayDestroy(pCacheArrayList); } -size_t taosCacheGetNumOfObj(const SCacheObj* pCacheObj) { - return pCacheObj->numOfElems + pCacheObj->numOfElemsInTrash; -} +size_t taosCacheGetNumOfObj(const SCacheObj *pCacheObj) { return pCacheObj->numOfElems + pCacheObj->numOfElemsInTrash; } -SCacheIter* taosCacheCreateIter(const SCacheObj* pCacheObj) { +SCacheIter *taosCacheCreateIter(const SCacheObj *pCacheObj) { ASSERT(pCacheObj != NULL); - SCacheIter* pIter = taosMemoryCalloc(1, sizeof(SCacheIter)); - pIter->pCacheObj = (SCacheObj*) pCacheObj; + SCacheIter *pIter = taosMemoryCalloc(1, sizeof(SCacheIter)); + pIter->pCacheObj = (SCacheObj *)pCacheObj; pIter->entryIndex = -1; - pIter->index = -1; + pIter->index = -1; return pIter; } -bool taosCacheIterNext(SCacheIter* pIter) { - SCacheObj* pCacheObj = pIter->pCacheObj; +bool taosCacheIterNext(SCacheIter *pIter) { + SCacheObj *pCacheObj = pIter->pCacheObj; if (pIter->index + 1 >= pIter->numOfObj) { if (pIter->entryIndex + 1 >= pCacheObj->capacity) { @@ -936,9 +934,9 @@ bool taosCacheIterNext(SCacheIter* pIter) { } // release the reference for all objects in the snapshot - for(int32_t i = 0; i < pIter->numOfObj; ++i) { - char* p= pIter->pCurrent[i]->data; - taosCacheRelease(pCacheObj, (void**) &p, false); + for (int32_t i = 0; i < pIter->numOfObj; ++i) { + char *p = pIter->pCurrent[i]->data; + taosCacheRelease(pCacheObj, (void **)&p, false); pIter->pCurrent[i] = NULL; } @@ -967,7 +965,7 @@ bool taosCacheIterNext(SCacheIter* pIter) { pIter->pCurrent = (SCacheNode **)tmp; } - SCacheNode* pNode = pEntry->next; + SCacheNode *pNode = pEntry->next; for (int32_t i = 0; i < pEntry->num; ++i) { ASSERT(pNode != NULL); @@ -981,7 +979,7 @@ bool taosCacheIterNext(SCacheIter* pIter) { pIter->numOfObj = pEntry->num; taosRUnLockLatch(&pEntry->latch); - pIter->index = -1; + pIter->index = -1; break; } } @@ -990,19 +988,19 @@ bool taosCacheIterNext(SCacheIter* pIter) { return true; } -void* taosCacheIterGetData(const SCacheIter* pIter, size_t* len) { - SCacheNode* pNode = pIter->pCurrent[pIter->index]; +void *taosCacheIterGetData(const SCacheIter *pIter, size_t *len) { + SCacheNode *pNode = pIter->pCurrent[pIter->index]; *len = pNode->dataLen; return pNode->data; } -void* taosCacheIterGetKey(const SCacheIter* pIter, size_t* len) { - SCacheNode* pNode = pIter->pCurrent[pIter->index]; +void *taosCacheIterGetKey(const SCacheIter *pIter, size_t *len) { + SCacheNode *pNode = pIter->pCurrent[pIter->index]; *len = pNode->keyLen; return pNode->key; } -void taosCacheDestroyIter(SCacheIter* pIter) { +void taosCacheDestroyIter(SCacheIter *pIter) { taosMemoryFreeClear(pIter->pCurrent); taosMemoryFreeClear(pIter); -} \ No newline at end of file +} diff --git a/source/util/test/cacheTest.cpp b/source/util/test/cacheTest.cpp index 748cf31b67..45c887288c 100644 --- a/source/util/test/cacheTest.cpp +++ b/source/util/test/cacheTest.cpp @@ -1,5 +1,5 @@ -#include #include +#include #include "os.h" #include "taos.h" @@ -8,132 +8,134 @@ // test cache TEST(cacheTest, client_cache_test) { const int32_t REFRESH_TIME_IN_SEC = 2; - SCacheObj* tscMetaCache = taosCacheInit(TSDB_DATA_TYPE_BINARY, REFRESH_TIME_IN_SEC, 0, NULL, "test"); + SCacheObj* tscMetaCache = taosCacheInit(TSDB_DATA_TYPE_BINARY, REFRESH_TIME_IN_SEC * 1000, 0, NULL, "test"); const char* key1 = "test1"; - char data1[] = "test11"; + char data1[] = "test11"; - char* cachedObj = (char*) taosCachePut(tscMetaCache, key1, strlen(key1), data1, strlen(data1)+1, 1); - taosSsleep(REFRESH_TIME_IN_SEC+1); + char* cachedObj = (char*)taosCachePut(tscMetaCache, key1, strlen(key1), data1, strlen(data1) + 1, 1); + taosSsleep(REFRESH_TIME_IN_SEC + 1); printf("obj is still valid: %s\n", cachedObj); char data2[] = "test22"; - taosCacheRelease(tscMetaCache, (void**) &cachedObj, false); + taosCacheRelease(tscMetaCache, (void**)&cachedObj, false); /* the object is cleared by cache clean operation */ - cachedObj = (char*) taosCachePut(tscMetaCache, key1, strlen(key1), data2, strlen(data2)+1, 20); + cachedObj = (char*)taosCachePut(tscMetaCache, key1, strlen(key1), data2, strlen(data2) + 1, 20); printf("after updated: %s\n", cachedObj); printf("start to remove data from cache\n"); - taosCacheRelease(tscMetaCache, (void**) &cachedObj, false); + taosCacheRelease(tscMetaCache, (void**)&cachedObj, false); printf("end of removing data from cache\n"); const char* key3 = "test2"; const char* data3 = "kkkkkkk"; - char* cachedObj2 = (char*) taosCachePut(tscMetaCache, key3, strlen(key3), data3, strlen(data3) + 1, 1); + char* cachedObj2 = (char*)taosCachePut(tscMetaCache, key3, strlen(key3), data3, strlen(data3) + 1, 1); printf("%s\n", cachedObj2); - taosCacheRelease(tscMetaCache, (void**) &cachedObj2, false); + taosCacheRelease(tscMetaCache, (void**)&cachedObj2, false); taosSsleep(3); - char* d = (char*) taosCacheAcquireByKey(tscMetaCache, key3, strlen(key3)); + char* d = (char*)taosCacheAcquireByKey(tscMetaCache, key3, strlen(key3)); assert(d == NULL); char key5[] = "test5"; char data5[] = "data5kkkkk"; - cachedObj2 = (char*) taosCachePut(tscMetaCache, key5, strlen(key5), data5, strlen(data5) + 1, 20); + cachedObj2 = (char*)taosCachePut(tscMetaCache, key5, strlen(key5), data5, strlen(data5) + 1, 20); - const char* data6= "new Data after updated"; - taosCacheRelease(tscMetaCache, (void**) &cachedObj2, false); + const char* data6 = "new Data after updated"; + taosCacheRelease(tscMetaCache, (void**)&cachedObj2, false); - cachedObj2 = (char*) taosCachePut(tscMetaCache, key5, strlen(key5), data6, strlen(data6) + 1, 20); + cachedObj2 = (char*)taosCachePut(tscMetaCache, key5, strlen(key5), data6, strlen(data6) + 1, 20); printf("%s\n", cachedObj2); - taosCacheRelease(tscMetaCache, (void**) &cachedObj2, true); + taosCacheRelease(tscMetaCache, (void**)&cachedObj2, true); const char* data7 = "add call update procedure"; - cachedObj2 = (char*) taosCachePut(tscMetaCache, key5, strlen(key5), data7, strlen(data7) + 1, 20); + cachedObj2 = (char*)taosCachePut(tscMetaCache, key5, strlen(key5), data7, strlen(data7) + 1, 20); printf("%s\n=======================================\n\n", cachedObj2); - char* cc = (char*) taosCacheAcquireByKey(tscMetaCache, key5, strlen(key5)); + char* cc = (char*)taosCacheAcquireByKey(tscMetaCache, key5, strlen(key5)); - taosCacheRelease(tscMetaCache, (void**) &cachedObj2, true); - taosCacheRelease(tscMetaCache, (void**) &cc, false); + taosCacheRelease(tscMetaCache, (void**)&cachedObj2, true); + taosCacheRelease(tscMetaCache, (void**)&cc, false); const char* data8 = "ttft"; const char* key6 = "key6"; - char* ft = (char*) taosCachePut(tscMetaCache, key6, strlen(key6), data8, strlen(data8), 20); - taosCacheRelease(tscMetaCache, (void**) &ft, false); + char* ft = (char*)taosCachePut(tscMetaCache, key6, strlen(key6), data8, strlen(data8), 20); + taosCacheRelease(tscMetaCache, (void**)&ft, false); /** * 140ns */ uint64_t startTime = taosGetTimestampUs(); printf("Cache Performance Test\nstart time:%" PRIu64 "\n", startTime); - for(int32_t i=0; i<1000; ++i) { - char* dd = (char*) taosCacheAcquireByKey(tscMetaCache, key6, strlen(key6)); + for (int32_t i = 0; i < 1000; ++i) { + char* dd = (char*)taosCacheAcquireByKey(tscMetaCache, key6, strlen(key6)); if (dd != NULL) { -// printf("get the data\n"); + // printf("get the data\n"); } else { printf("data has been released\n"); } - taosCacheRelease(tscMetaCache, (void**) &dd, false); + taosCacheRelease(tscMetaCache, (void**)&dd, false); } uint64_t endTime = taosGetTimestampUs(); - int64_t el = endTime - startTime; + int64_t el = endTime - startTime; - printf("End of Test, %" PRIu64 "\nTotal Elapsed Time:%" PRIu64 " us.avg:%f us\n", endTime, el, el/1000.0); + printf("End of Test, %" PRIu64 "\nTotal Elapsed Time:%" PRIu64 " us.avg:%f us\n", endTime, el, el / 1000.0); taosCacheCleanup(tscMetaCache); } TEST(cacheTest, cache_iter_test) { const int32_t REFRESH_TIME_IN_SEC = 2; - auto* pCache = taosCacheInit(TSDB_DATA_TYPE_BINARY, REFRESH_TIME_IN_SEC, false, NULL, "test"); + auto* pCache = taosCacheInit(TSDB_DATA_TYPE_BINARY, REFRESH_TIME_IN_SEC * 1000, false, NULL, "test"); char key[256] = {0}; char data[1024] = "abcdefghijk"; -// int32_t len = strlen(data); + // int32_t len = strlen(data); uint64_t startTime = taosGetTimestampUs(); - int32_t num = 10000; + int32_t num = 10000; - for(int32_t i = 0; i < num; ++i) { + for (int32_t i = 0; i < num; ++i) { int32_t len = sprintf(key, "abc_%7d", i); taosCachePut(pCache, key, strlen(key), data, len, 3600); } uint64_t endTime = taosGetTimestampUs(); - printf("add %d object cost:%" PRIu64 " us, avg:%f us\n", num, endTime - startTime, (endTime-startTime)/(double)num); + printf("add %d object cost:%" PRIu64 " us, avg:%f us\n", num, endTime - startTime, + (endTime - startTime) / (double)num); startTime = taosGetTimestampUs(); - for(int32_t i = 0; i < num; ++i) { + for (int32_t i = 0; i < num; ++i) { int32_t len = sprintf(key, "abc_%7d", i); - void* k = taosCacheAcquireByKey(pCache, key, len); + void* k = taosCacheAcquireByKey(pCache, key, len); assert(k != 0); } endTime = taosGetTimestampUs(); - printf("retrieve %d object cost:%" PRIu64 " us,avg:%f\n", num, endTime - startTime, (endTime - startTime)/(double)num); + printf("retrieve %d object cost:%" PRIu64 " us,avg:%f\n", num, endTime - startTime, + (endTime - startTime) / (double)num); - int32_t count = 0; + int32_t count = 0; SCacheIter* pIter = taosCacheCreateIter(pCache); - while(taosCacheIterNext(pIter)) { + while (taosCacheIterNext(pIter)) { size_t keyLen = 0; size_t dataLen = 0; char* key1 = static_cast(taosCacheIterGetKey(pIter, &keyLen)); char* data1 = static_cast(taosCacheIterGetData(pIter, &dataLen)); -// char d[256] = {0}; -// memcpy(d, data1, dataLen); -// char k[256] = {0}; -// memcpy(k, key1, keyLen); + // char d[256] = {0}; + // memcpy(d, data1, dataLen); + // char k[256] = {0}; + // memcpy(k, key1, keyLen); } ASSERT_EQ(count, num); From b816d7091e0894d4ccf11bab45ccd3456967336c Mon Sep 17 00:00:00 2001 From: Liu Jicong Date: Fri, 22 Apr 2022 20:10:44 +0800 Subject: [PATCH 39/51] add multi process case for tmq --- tests/script/jenkins/basic.txt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/script/jenkins/basic.txt b/tests/script/jenkins/basic.txt index 51b188c8db..31b1fff826 100644 --- a/tests/script/jenkins/basic.txt +++ b/tests/script/jenkins/basic.txt @@ -76,7 +76,7 @@ ./test.sh -f tsim/insert/backquote.sim -m ./test.sh -f tsim/parser/fourArithmetic-basic.sim -m ./test.sh -f tsim/query/interval-offset.sim -m -#./test.sh -f tsim/tmq/basic.sim -m +./test.sh -f tsim/tmq/basic1.sim -m ./test.sh -f tsim/stable/vnode3.sim -m ./test.sh -f tsim/qnode/basic1.sim -m ./test.sh -f tsim/mnode/basic1.sim -m From aade540f0514ba9aa489fb040de70c35a01d1a85 Mon Sep 17 00:00:00 2001 From: Liu Jicong Date: Fri, 22 Apr 2022 21:02:21 +0800 Subject: [PATCH 40/51] remove unused msg --- include/common/tmsgdef.h | 4 -- source/dnode/mgmt/mgmt_mnode/src/mmHandle.c | 3 - source/dnode/mgmt/mgmt_vnode/src/vmHandle.c | 4 -- source/dnode/vnode/src/inc/tq.h | 34 +--------- source/dnode/vnode/src/tq/tq.c | 11 ++-- source/dnode/vnode/src/tq/tqPush.c | 70 --------------------- source/dnode/vnode/src/vnd/vnodeSvr.c | 15 ----- 7 files changed, 8 insertions(+), 133 deletions(-) diff --git a/include/common/tmsgdef.h b/include/common/tmsgdef.h index d0f88a660c..b37001bde9 100644 --- a/include/common/tmsgdef.h +++ b/include/common/tmsgdef.h @@ -175,11 +175,7 @@ enum { TD_DEF_MSG_TYPE(TDMT_VND_MQ_QUERY, "vnode-mq-query", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_VND_MQ_CONNECT, "vnode-mq-connect", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_VND_MQ_DISCONNECT, "vnode-mq-disconnect", NULL, NULL) - TD_DEF_MSG_TYPE(TDMT_VND_MQ_SET_CONN, "vnode-mq-set-conn", SMqSetCVgReq, SMqSetCVgRsp) - TD_DEF_MSG_TYPE(TDMT_VND_MQ_REB, "vnode-mq-mv-rebalance", SMqMVRebReq, SMqMVRebRsp) - TD_DEF_MSG_TYPE(TDMT_VND_MQ_CANCEL_CONN, "vnode-mq-mv-cancel-conn", SMqCancelConnReq, SMqCancelConnRsp) TD_DEF_MSG_TYPE(TDMT_VND_MQ_VG_CHANGE, "vnode-mq-vg-change", SMqRebVgReq, SMqRebVgRsp) - TD_DEF_MSG_TYPE(TDMT_VND_MQ_SET_CUR, "vnode-mq-set-cur", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_VND_RES_READY, "vnode-res-ready", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_VND_TASKS_STATUS, "vnode-tasks-status", NULL, NULL) TD_DEF_MSG_TYPE(TDMT_VND_CANCEL_TASK, "vnode-cancel-task", NULL, NULL) diff --git a/source/dnode/mgmt/mgmt_mnode/src/mmHandle.c b/source/dnode/mgmt/mgmt_mnode/src/mmHandle.c index 411eff5374..159e14a3d5 100644 --- a/source/dnode/mgmt/mgmt_mnode/src/mmHandle.c +++ b/source/dnode/mgmt/mgmt_mnode/src/mmHandle.c @@ -218,9 +218,6 @@ void mmInitMsgHandle(SMgmtWrapper *pWrapper) { dmSetMsgHandle(pWrapper, TDMT_MND_GET_INDEX, mmProcessReadMsg, DEFAULT_HANDLE); // Requests handled by VNODE - dmSetMsgHandle(pWrapper, TDMT_VND_MQ_SET_CONN_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_MQ_REB_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); - dmSetMsgHandle(pWrapper, TDMT_VND_MQ_CANCEL_CONN_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); dmSetMsgHandle(pWrapper, TDMT_VND_CREATE_STB_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); dmSetMsgHandle(pWrapper, TDMT_VND_ALTER_STB_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); dmSetMsgHandle(pWrapper, TDMT_VND_DROP_STB_RSP, mmProcessWriteMsg, DEFAULT_HANDLE); diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c b/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c index 65180f84dd..4cc1b8527c 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c @@ -276,10 +276,6 @@ void vmInitMsgHandle(SMgmtWrapper *pWrapper) { dmSetMsgHandle(pWrapper, TDMT_VND_CREATE_SMA, vmProcessWriteMsg, DEFAULT_HANDLE); dmSetMsgHandle(pWrapper, TDMT_VND_CANCEL_SMA, vmProcessWriteMsg, DEFAULT_HANDLE); dmSetMsgHandle(pWrapper, TDMT_VND_DROP_SMA, vmProcessWriteMsg, DEFAULT_HANDLE); - // dmSetMsgHandle(pWrapper, TDMT_VND_MQ_SET_CONN, vmProcessWriteMsg, DEFAULT_HANDLE); - // dmSetMsgHandle(pWrapper, TDMT_VND_MQ_REB, vmProcessWriteMsg, DEFAULT_HANDLE); - // dmSetMsgHandle(pWrapper, TDMT_VND_MQ_CANCEL_CONN, vmProcessWriteMsg, DEFAULT_HANDLE); - // dmSetMsgHandle(pWrapper, TDMT_VND_MQ_SET_CUR, vmProcessFetchMsg, DEFAULT_HANDLE); dmSetMsgHandle(pWrapper, TDMT_VND_MQ_VG_CHANGE, (NodeMsgFp)vmProcessWriteMsg, DEFAULT_HANDLE); dmSetMsgHandle(pWrapper, TDMT_VND_CONSUME, vmProcessFetchMsg, DEFAULT_HANDLE); dmSetMsgHandle(pWrapper, TDMT_VND_TASK_DEPLOY, vmProcessWriteMsg, DEFAULT_HANDLE); diff --git a/source/dnode/vnode/src/inc/tq.h b/source/dnode/vnode/src/inc/tq.h index 2250aab35c..6e004a89fc 100644 --- a/source/dnode/vnode/src/inc/tq.h +++ b/source/dnode/vnode/src/inc/tq.h @@ -83,7 +83,6 @@ typedef struct STqOffsetStore STqOffsetStore; struct STqReadHandle { int64_t ver; - int64_t tbUid; SHashObj* tbIdHash; const SSubmitReq* pMsg; SSubmitBlk* pBlock; @@ -218,20 +217,6 @@ typedef struct { SArray* topics; // SArray } STqConsumer; -enum { - TQ_PUSHER_TYPE__CLIENT = 1, - TQ_PUSHER_TYPE__STREAM, -}; - -typedef struct { - int8_t type; - int8_t reserved[3]; - int32_t ttl; - int64_t consumerId; - SRpcMsg* pMsg; - // SMqPollRsp* rsp; -} STqClientPusher; - typedef struct { int8_t type; int8_t nodeType; @@ -241,10 +226,6 @@ typedef struct { // TODO sync function } STqStreamPusher; -typedef struct { - SHashObj* pHash; // -} STqPushMgr; - typedef struct { int8_t inited; tmr_h timer; @@ -260,14 +241,11 @@ void tqCleanUp(); STQ* tqOpen(const char* path, SVnode* pVnode, SWal* pWal); void tqClose(STQ*); // required by vnode -int tqPushMsg(STQ*, void* msg, int32_t msgLen, tmsg_t msgType, int64_t version); +int tqPushMsg(STQ*, void* msg, int32_t msgLen, tmsg_t msgType, int64_t ver); int tqCommit(STQ*); int32_t tqProcessPollReq(STQ* pTq, SRpcMsg* pMsg, int32_t workerId); int32_t tqProcessVgChangeReq(STQ* pTq, char* msg, int32_t msgLen); -// int32_t tqProcessSetConnReq(STQ* pTq, char* msg); -// int32_t tqProcessRebReq(STQ* pTq, char* msg); -// int32_t tqProcessCancelConnReq(STQ* pTq, char* msg); int32_t tqProcessTaskExec(STQ* pTq, char* msg, int32_t msgLen, int32_t workerId); int32_t tqProcessTaskDeploy(STQ* pTq, char* msg, int32_t msgLen); int32_t tqProcessStreamTrigger(STQ* pTq, void* data, int32_t dataLen, int32_t workerId); @@ -308,16 +286,6 @@ int32_t tqOffsetCommit(STqOffsetStore* pStore, const char* subscribeKey, int64_t int32_t tqOffsetPersist(STqOffsetStore* pStore, const char* subscribeKey); int32_t tqOffsetPersistAll(STqOffsetStore* pStore); -// tqPush -int32_t tqPushMgrInit(); -void tqPushMgrCleanUp(); - -STqPushMgr* tqPushMgrOpen(); -void tqPushMgrClose(STqPushMgr* pushMgr); - -STqClientPusher* tqAddClientPusher(STqPushMgr* pushMgr, SRpcMsg* pMsg, int64_t consumerId, int64_t ttl); -STqStreamPusher* tqAddStreamPusher(STqPushMgr* pushMgr, int64_t streamId, SEpSet* pEpSet); - #ifdef __cplusplus } #endif diff --git a/source/dnode/vnode/src/tq/tq.c b/source/dnode/vnode/src/tq/tq.c index d0018d2cac..0b3feb5010 100644 --- a/source/dnode/vnode/src/tq/tq.c +++ b/source/dnode/vnode/src/tq/tq.c @@ -15,9 +15,12 @@ #include "vnodeInt.h" -int32_t tqInit() { return tqPushMgrInit(); } +int32_t tqInit() { + // + return 0; +} -void tqCleanUp() { tqPushMgrCleanUp(); } +void tqCleanUp() {} STQ* tqOpen(const char* path, SVnode* pVnode, SWal* pWal) { STQ* pTq = taosMemoryMalloc(sizeof(STQ)); @@ -184,7 +187,7 @@ int32_t tqPushMsgNew(STQ* pTq, void* msg, int32_t msgLen, tmsg_t msgType, int64_ return 0; } -int tqPushMsg(STQ* pTq, void* msg, int32_t msgLen, tmsg_t msgType, int64_t version) { +int tqPushMsg(STQ* pTq, void* msg, int32_t msgLen, tmsg_t msgType, int64_t ver) { if (msgType != TDMT_VND_SUBMIT) return 0; void* data = taosMemoryMalloc(msgLen); @@ -194,7 +197,7 @@ int tqPushMsg(STQ* pTq, void* msg, int32_t msgLen, tmsg_t msgType, int64_t versi memcpy(data, msg, msgLen); if (msgType == TDMT_VND_SUBMIT) { - if (tsdbUpdateSmaWindow(pTq->pVnode->pTsdb, msg, version) != 0) { + if (tsdbUpdateSmaWindow(pTq->pVnode->pTsdb, msg, ver) != 0) { return -1; } } diff --git a/source/dnode/vnode/src/tq/tqPush.c b/source/dnode/vnode/src/tq/tqPush.c index 4384255f89..f2f48bbc8a 100644 --- a/source/dnode/vnode/src/tq/tqPush.c +++ b/source/dnode/vnode/src/tq/tqPush.c @@ -12,73 +12,3 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - -#include "vnodeInt.h" - -int32_t tqPushMgrInit() { - // - int8_t old = atomic_val_compare_exchange_8(&tqPushMgmt.inited, 0, 1); - if (old == 1) return 0; - - tqPushMgmt.timer = taosTmrInit(0, 0, 0, "TQ"); - return 0; -} - -void tqPushMgrCleanUp() { - int8_t old = atomic_val_compare_exchange_8(&tqPushMgmt.inited, 1, 0); - if (old == 0) return; - taosTmrStop(tqPushMgmt.timer); - taosTmrCleanUp(tqPushMgmt.timer); -} - -STqPushMgr* tqPushMgrOpen() { - STqPushMgr* mgr = taosMemoryMalloc(sizeof(STqPushMgr)); - if (mgr == NULL) { - return NULL; - } - mgr->pHash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK); - return mgr; -} - -void tqPushMgrClose(STqPushMgr* pushMgr) { - taosHashCleanup(pushMgr->pHash); - taosMemoryFree(pushMgr); -} - -STqClientPusher* tqAddClientPusher(STqPushMgr* pushMgr, SRpcMsg* pMsg, int64_t consumerId, int64_t ttl) { - STqClientPusher* clientPusher = taosMemoryMalloc(sizeof(STqClientPusher)); - if (clientPusher == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; - } - clientPusher->type = TQ_PUSHER_TYPE__CLIENT; - clientPusher->pMsg = pMsg; - clientPusher->consumerId = consumerId; - clientPusher->ttl = ttl; - if (taosHashPut(pushMgr->pHash, &consumerId, sizeof(int64_t), &clientPusher, sizeof(void*)) < 0) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - taosMemoryFree(clientPusher); - // TODO send rsp back - return NULL; - } - return clientPusher; -} - -STqStreamPusher* tqAddStreamPusher(STqPushMgr* pushMgr, int64_t streamId, SEpSet* pEpSet) { - STqStreamPusher* streamPusher = taosMemoryMalloc(sizeof(STqStreamPusher)); - if (streamPusher == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; - } - streamPusher->type = TQ_PUSHER_TYPE__STREAM; - streamPusher->nodeType = 0; - streamPusher->streamId = streamId; - /*memcpy(&streamPusher->epSet, pEpSet, sizeof(SEpSet));*/ - - if (taosHashPut(pushMgr->pHash, &streamId, sizeof(int64_t), &streamPusher, sizeof(void*)) < 0) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - taosMemoryFree(streamPusher); - return NULL; - } - return streamPusher; -} diff --git a/source/dnode/vnode/src/vnd/vnodeSvr.c b/source/dnode/vnode/src/vnd/vnodeSvr.c index e0c37f7eb7..aa90a04478 100644 --- a/source/dnode/vnode/src/vnd/vnodeSvr.c +++ b/source/dnode/vnode/src/vnd/vnodeSvr.c @@ -90,21 +90,6 @@ int vnodeProcessWriteReq(SVnode *pVnode, SRpcMsg *pMsg, int64_t version, SRpcMsg // TODO: handle error } break; -#if 0 - case TDMT_VND_MQ_SET_CONN: { - if (tqProcessSetConnReq(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead))) < 0) { - // TODO: handle error - } - } break; - case TDMT_VND_MQ_REB: { - if (tqProcessRebReq(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead))) < 0) { - } - } break; - case TDMT_VND_MQ_CANCEL_CONN: { - if (tqProcessCancelConnReq(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead))) < 0) { - } - } break; -#endif case TDMT_VND_TASK_DEPLOY: { if (tqProcessTaskDeploy(pVnode->pTq, POINTER_SHIFT(pMsg->pCont, sizeof(SMsgHead)), pMsg->contLen - sizeof(SMsgHead)) < 0) { From 5948ddd6971368d79fc3cd8417fdd6bc1c6528c6 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Sat, 23 Apr 2022 00:13:09 +0800 Subject: [PATCH 41/51] fix: invalid free in unittest --- source/dnode/mgmt/implement/src/dmExec.c | 1 + source/dnode/mgmt/interface/src/dmEnv.c | 1 + source/dnode/mgmt/mgmt_vnode/src/vmInt.c | 2 +- 3 files changed, 3 insertions(+), 1 deletion(-) diff --git a/source/dnode/mgmt/implement/src/dmExec.c b/source/dnode/mgmt/implement/src/dmExec.c index c0b85ed705..06001028b5 100644 --- a/source/dnode/mgmt/implement/src/dmExec.c +++ b/source/dnode/mgmt/implement/src/dmExec.c @@ -174,6 +174,7 @@ void dmStopNode(SMgmtWrapper *pWrapper) { void dmCloseNode(SMgmtWrapper *pWrapper) { dInfo("node:%s, start to close", pWrapper->name); + pWrapper->deployed = false; while (pWrapper->refCount > 0) { taosMsleep(10); diff --git a/source/dnode/mgmt/interface/src/dmEnv.c b/source/dnode/mgmt/interface/src/dmEnv.c index b6f309ec6f..92a6e018cb 100644 --- a/source/dnode/mgmt/interface/src/dmEnv.c +++ b/source/dnode/mgmt/interface/src/dmEnv.c @@ -53,6 +53,7 @@ void dmCleanup() { } monCleanup(); + syncCleanUp(); walCleanUp(); taosStopCacheRefreshWorker(); dInfo("dnode env is cleaned up"); diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c index ffbc6eb3b9..e784272581 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmInt.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmInt.c @@ -274,7 +274,7 @@ static void vmCleanup(SMgmtWrapper *pWrapper) { taosMemoryFree(pMgmt); pWrapper->pMgmt = NULL; - syncCleanUp(); + // syncCleanUp(); dInfo("vnode-mgmt is cleaned up"); } From 5e4737f9a02ca36d90983054ea6fe4a2b6d8d361 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Sat, 23 Apr 2022 00:27:46 +0800 Subject: [PATCH 42/51] fix: sleep 1000ms while unit test creates db so vnode becomes leader --- source/dnode/mnode/impl/test/db/db.cpp | 1 + source/dnode/mnode/impl/test/sma/sma.cpp | 1 + source/dnode/mnode/impl/test/stb/stb.cpp | 8 ++++++++ 3 files changed, 10 insertions(+) diff --git a/source/dnode/mnode/impl/test/db/db.cpp b/source/dnode/mnode/impl/test/db/db.cpp index 6e72ce89d3..1fc1bec650 100644 --- a/source/dnode/mnode/impl/test/db/db.cpp +++ b/source/dnode/mnode/impl/test/db/db.cpp @@ -89,6 +89,7 @@ TEST_F(MndTestDb, 02_Create_Alter_Drop_Db) { void* pReq = rpcMallocCont(contLen); tSerializeSAlterDbReq(pReq, contLen, &alterdbReq); + taosMsleep(1000); // Wait for the vnode to become the leader SRpcMsg* pRsp = test.SendReq(TDMT_MND_ALTER_DB, pReq, contLen); ASSERT_NE(pRsp, nullptr); ASSERT_EQ(pRsp->code, 0); diff --git a/source/dnode/mnode/impl/test/sma/sma.cpp b/source/dnode/mnode/impl/test/sma/sma.cpp index 0d41d5de20..96c0c8e953 100644 --- a/source/dnode/mnode/impl/test/sma/sma.cpp +++ b/source/dnode/mnode/impl/test/sma/sma.cpp @@ -258,6 +258,7 @@ TEST_F(MndTestSma, 02_Create_Show_Meta_Drop_Restart_BSma) { pReq = BuildCreateDbReq(dbname, &contLen); pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen); ASSERT_EQ(pRsp->code, 0); + taosMsleep(1000); // Wait for the vnode to become the leader } { diff --git a/source/dnode/mnode/impl/test/stb/stb.cpp b/source/dnode/mnode/impl/test/stb/stb.cpp index 9270d38e11..0c54091aa9 100644 --- a/source/dnode/mnode/impl/test/stb/stb.cpp +++ b/source/dnode/mnode/impl/test/stb/stb.cpp @@ -304,6 +304,7 @@ TEST_F(MndTestStb, 01_Create_Show_Meta_Drop_Restart_Stb) { SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen); ASSERT_NE(pRsp, nullptr); ASSERT_EQ(pRsp->code, 0); + taosMsleep(2000); // Wait for the vnode to become the leader } { @@ -438,6 +439,7 @@ TEST_F(MndTestStb, 02_Alter_Stb_AddTag) { SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen); ASSERT_NE(pRsp, nullptr); ASSERT_EQ(pRsp->code, 0); + taosMsleep(2000); // Wait for the vnode to become the leader } { @@ -497,6 +499,7 @@ TEST_F(MndTestStb, 03_Alter_Stb_DropTag) { void* pReq = BuildCreateDbReq(dbname, &contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen); ASSERT_EQ(pRsp->code, 0); + taosMsleep(2000); // Wait for the vnode to become the leader } { @@ -538,6 +541,7 @@ TEST_F(MndTestStb, 04_Alter_Stb_AlterTagName) { void* pReq = BuildCreateDbReq(dbname, &contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen); ASSERT_EQ(pRsp->code, 0); + taosMsleep(2000); // Wait for the vnode to become the leader } { @@ -602,6 +606,7 @@ TEST_F(MndTestStb, 05_Alter_Stb_AlterTagBytes) { void* pReq = BuildCreateDbReq(dbname, &contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen); ASSERT_EQ(pRsp->code, 0); + taosMsleep(2000); // Wait for the vnode to become the leader } { @@ -655,6 +660,7 @@ TEST_F(MndTestStb, 06_Alter_Stb_AddColumn) { SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen); ASSERT_NE(pRsp, nullptr); ASSERT_EQ(pRsp->code, 0); + taosMsleep(2000); // Wait for the vnode to become the leader } { @@ -715,6 +721,7 @@ TEST_F(MndTestStb, 07_Alter_Stb_DropColumn) { void* pReq = BuildCreateDbReq(dbname, &contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen); ASSERT_EQ(pRsp->code, 0); + taosMsleep(2000); // Wait for the vnode to become the leader } { @@ -775,6 +782,7 @@ TEST_F(MndTestStb, 08_Alter_Stb_AlterTagBytes) { void* pReq = BuildCreateDbReq(dbname, &contLen); SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen); ASSERT_EQ(pRsp->code, 0); + taosMsleep(2000); // Wait for the vnode to become the leader } { From 43a772e10e4c08fa9f34715f453853047b84290b Mon Sep 17 00:00:00 2001 From: plum-lihui Date: Sun, 24 Apr 2022 13:37:22 +0800 Subject: [PATCH 43/51] [test: add tmq test cases] --- tests/script/tsim/tmq/basic1Of2Cons.sim | 394 ++++++++++++++++++++++++ tests/script/tsim/tmq/basic2Of2Cons.sim | 330 ++++++++++++++++++++ tests/test/c/tmqSim.c | 2 + 3 files changed, 726 insertions(+) create mode 100644 tests/script/tsim/tmq/basic1Of2Cons.sim create mode 100644 tests/script/tsim/tmq/basic2Of2Cons.sim diff --git a/tests/script/tsim/tmq/basic1Of2Cons.sim b/tests/script/tsim/tmq/basic1Of2Cons.sim new file mode 100644 index 0000000000..a5a13f4e8b --- /dev/null +++ b/tests/script/tsim/tmq/basic1Of2Cons.sim @@ -0,0 +1,394 @@ +#### test scenario, please refer to https://jira.taosdata.com:18090/pages/viewpage.action?pageId=135120406 +#basic1Of2Cons.sim: vgroups=1, consumer=2, one topic for one consumer, firstly insert data, then start consume. Include six topics +#basic2Of2Cons.sim: vgroups=1, consumer=2, multi topics for one consumer, firstly insert data, then start consume. Include six topics +#basic3Of2Cons.sim: vgroups=4, consumer=2, one topic for one consumer, firstly insert data, then start consume. Include six topics +#basic4Of2Cons.sim: vgroups=4, consumer=2, multi topics for one consumer, firstly insert data, then start consume. Include six topics + +# notes1: Scalar function: ABS/ACOS/ASIN/ATAN/CEIL/COS/FLOOR/LOG/POW/ROUND/SIN/SQRT/TAN +# The above use cases are combined with where filter conditions, such as: where ts > "2017-08-12 18:25:58.128Z" and sin(a) > 0.5; +# +# notes2: not support aggregate functions(such as sum/count/min/max) and time-windows(interval). +# + +run tsim/tmq/prepareBasicEnv-1vgrp.sim + +#---- global parameters start ----# +$dbName = db +$vgroups = 1 +$stbPrefix = stb +$ctbPrefix = ctb +$ntbPrefix = ntb +$stbNum = 1 +$ctbNum = 10 +$ntbNum = 10 +$rowsPerCtb = 10 +$tstart = 1640966400000 # 2022-01-01 00:00:00.000 +#---- global parameters end ----# + +$pullDelay = 5 +$ifcheckdata = 1 +$showMsg = 1 +$showRow = 0 + +sql connect +sql use $dbName + +print == create topics from super table +sql create topic topic_stb_column as select ts, c3 from stb +sql create topic topic_stb_all as select ts, c1, c2, c3 from stb +sql create topic topic_stb_function as select ts, abs(c1), sin(c2) from stb + +print == create topics from child table +sql create topic topic_ctb_column as select ts, c3 from ctb0 +sql create topic topic_ctb_all as select * from ctb0 +sql create topic topic_ctb_function as select ts, abs(c1), sin(c2) from ctb0 + +print == create topics from normal table +sql create topic topic_ntb_column as select ts, c3 from ntb0 +sql create topic topic_ntb_all as select * from ntb0 +sql create topic topic_ntb_function as select ts, abs(c1), sin(c2) from ntb0 + +#sql show topics +#if $rows != 9 then +# return -1 +#endi + +$keyList = ' . group.id:cgrp1 +$keyList = $keyList . ' + +$cdb_index = 0 +#=============================== start consume =============================# + +print ================ test consume from stb +$loop_cnt = 0 +loop_consume_diff_topic_from_stb: + +####################################################################################### +# clear consume info and consume result +#run tsim/tmq/clearConsume.sim +# because drop table function no stable, so by create new db for consume info and result. Modify it later +$cdb_index = $cdb_index + 1 +$cdbName = cdb . $cdb_index +sql create database $cdbName vgroups 1 +sleep 500 +sql use $cdbName + +print == create consume info table and consume result table for stb +sql create table consumeinfo (ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int) +sql create table consumeresult (ts timestamp, consumerid int, consummsgcnt bigint, consumrowcnt bigint, checkresult int) + +sql show tables +if $rows != 2 then + return -1 +endi +####################################################################################### + +if $loop_cnt == 0 then + print == scenario 1: topic_stb_column + $topicList = ' . topic_stb_column + $topicList = $topicList . ' +elif $loop_cnt == 1 then + print == scenario 2: topic_stb_all + $topicList = ' . topic_stb_all + $topicList = $topicList . ' +elif $loop_cnt == 2 then + print == scenario 3: topic_stb_function + $topicList = ' . topic_stb_function + $topicList = $topicList . ' +else + goto loop_consume_diff_topic_from_stb_end +endi + +$consumerId = 0 +$totalMsgOfStb = $ctbNum * $rowsPerCtb +$expectmsgcnt = $totalMsgOfStb +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + +$consumerId = 1 +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + +print == start consumer to pull msgs from stb +print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start +system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start + +print == check consume result +wait_consumer_end_from_stb: +sql select * from consumeresult +print ==> rows: $rows +print ==> rows[0]: $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6] +print ==> rows[1]: $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6] +if $rows != 2 then + sleep 1000 + goto wait_consumer_end_from_stb +endi +if $data[0][1] == 0 then + if $data[1][1] != 1 then + return -1 + endi +endi +if $data[0][1] == 1 then + if $data[1][1] != 0 then + return -1 + endi +endi + +if $data[0][2] != 0 then + if $data[0][2] != $expectmsgcnt then + return -1 + endi + if $data[1][2] != 0 then + return -1 + endi +endi +if $data[1][2] != 0 then + if $data[1][2] != $expectmsgcnt then + return -1 + endi + if $data[0][2] != 0 then + return -1 + endi +endi + +if $data[0][3] != 0 then + if $data[0][3] != $expectmsgcnt then + return -1 + endi + if $data[1][3] != 0 then + return -1 + endi +endi +if $data[1][3] != 0 then + if $data[1][3] != $expectmsgcnt then + return -1 + endi + if $data[0][3] != 0 then + return -1 + endi +endi +$loop_cnt = $loop_cnt + 1 +goto loop_consume_diff_topic_from_stb +loop_consume_diff_topic_from_stb_end: + +print ================ test consume from ctb +$loop_cnt = 0 +loop_consume_diff_topic_from_ctb: + +####################################################################################### +# clear consume info and consume result +#run tsim/tmq/clearConsume.sim +# because drop table function no stable, so by create new db for consume info and result. Modify it later +$cdb_index = $cdb_index + 1 +$cdbName = cdb . $cdb_index +sql create database $cdbName vgroups 1 +sleep 500 +sql use $cdbName + +print == create consume info table and consume result table for ctb +sql create table consumeinfo (ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int) +sql create table consumeresult (ts timestamp, consumerid int, consummsgcnt bigint, consumrowcnt bigint, checkresult int) + +sql show tables +if $rows != 2 then + return -1 +endi +####################################################################################### + +if $loop_cnt == 0 then + print == scenario 1: topic_ctb_column + $topicList = ' . topic_ctb_column + $topicList = $topicList . ' +elif $loop_cnt == 1 then + print == scenario 2: topic_ctb_all + $topicList = ' . topic_ctb_all + $topicList = $topicList . ' +elif $loop_cnt == 2 then + print == scenario 3: topic_ctb_function + $topicList = ' . topic_ctb_function + $topicList = $topicList . ' +else + goto loop_consume_diff_topic_from_ctb_end +endi + +$consumerId = 0 +$totalMsgOfCtb = $rowsPerCtb +$expectmsgcnt = $totalMsgOfCtb +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) +$consumerId = 1 +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + +print == start consumer to pull msgs from ctb +print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start +system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start + +print == check consume result +wait_consumer_end_from_ctb: +sql select * from consumeresult +print ==> rows: $rows +print ==> rows[0]: $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6] +print ==> rows[1]: $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6] +if $rows != 2 then + sleep 1000 + goto wait_consumer_end_from_ctb +endi +if $data[0][1] == 0 then + if $data[1][1] != 1 then + return -1 + endi +endi +if $data[0][1] == 1 then + if $data[1][1] != 0 then + return -1 + endi +endi + +if $data[0][2] != $totalMsgOfCtb then + if $data[1][2] != $totalMsgOfCtb then + return -1 + endi + if $data[0][2] != 0 then + return -1 + endi +endi +if $data[1][2] != $totalMsgOfCtb then + if $data[0][2] != $totalMsgOfCtb then + return -1 + endi + if $data[1][2] != 0 then + return -1 + endi +endi + +if $data[0][3] != $totalMsgOfCtb then + if $data[1][3] != $totalMsgOfCtb then + return -1 + endi + if $data[0][3] != 0 then + return -1 + endi +endi +if $data[1][3] != $totalMsgOfCtb then + if $data[0][3] != $totalMsgOfCtb then + return -1 + endi + if $data[1][3] != 0 then + return -1 + endi +endi + +$loop_cnt = $loop_cnt + 1 +goto loop_consume_diff_topic_from_ctb +loop_consume_diff_topic_from_ctb_end: + +print ================ test consume from ntb +$loop_cnt = 0 +loop_consume_diff_topic_from_ntb: + +####################################################################################### +# clear consume info and consume result +#run tsim/tmq/clearConsume.sim +# because drop table function no stable, so by create new db for consume info and result. Modify it later +$cdb_index = $cdb_index + 1 +$cdbName = cdb . $cdb_index +sql create database $cdbName vgroups 1 +sleep 500 +sql use $cdbName + +print == create consume info table and consume result table for ntb +sql create table consumeinfo (ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int) +sql create table consumeresult (ts timestamp, consumerid int, consummsgcnt bigint, consumrowcnt bigint, checkresult int) + +sql show tables +if $rows != 2 then + return -1 +endi +####################################################################################### + +if $loop_cnt == 0 then + print == scenario 1: topic_ntb_column + $topicList = ' . topic_ntb_column + $topicList = $topicList . ' +elif $loop_cnt == 1 then + print == scenario 2: topic_ntb_all + $topicList = ' . topic_ntb_all + $topicList = $topicList . ' +elif $loop_cnt == 2 then + print == scenario 3: topic_ntb_function + $topicList = ' . topic_ntb_function + $topicList = $topicList . ' +else + goto loop_consume_diff_topic_from_ntb_end +endi + +$consumerId = 0 +$totalMsgOfNtb = $rowsPerCtb +$expectmsgcnt = $totalMsgOfNtb +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) +$consumerId = 1 +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + +print == start consumer to pull msgs from ntb +print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start +system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start + +print == check consume result from ntb +wait_consumer_end_from_ntb: +sql select * from consumeresult +print ==> rows: $rows +print ==> rows[0]: $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6] +print ==> rows[1]: $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6] +if $rows != 2 then + sleep 1000 + goto wait_consumer_end_from_ntb +endi +if $data[0][1] == 0 then + if $data[1][1] != 1 then + return -1 + endi +endi +if $data[1][1] == 0 then + if $data[0][1] != 1 then + return -1 + endi +endi + +if $data[0][2] != $totalMsgOfNtb then + if $data[1][2] != $totalMsgOfNtb then + return -1 + endi + if $data[0][2] != 0 then + return -1 + endi +endi +if $data[1][2] != $totalMsgOfNtb then + if $data[0][2] != $totalMsgOfNtb then + return -1 + endi + if $data[1][2] != 0 then + return -1 + endi +endi + +if $data[0][3] != $totalMsgOfNtb then + if $data[1][3] != $totalMsgOfNtb then + return -1 + endi + if $data[0][3] != 0 then + return -1 + endi +endi +if $data[1][3] != $totalMsgOfNtb then + if $data[0][3] != $totalMsgOfNtb then + return -1 + endi + if $data[1][3] != 0 then + return -1 + endi +endi +$loop_cnt = $loop_cnt + 1 +goto loop_consume_diff_topic_from_ntb +loop_consume_diff_topic_from_ntb_end: + +#------ not need stop consumer, because it exit after pull msg overthan expect msg +#system tsim/tmq/consume.sh -s stop -x SIGINT + +system sh/exec.sh -n dnode1 -s stop -x SIGINT diff --git a/tests/script/tsim/tmq/basic2Of2Cons.sim b/tests/script/tsim/tmq/basic2Of2Cons.sim new file mode 100644 index 0000000000..89b6114ebe --- /dev/null +++ b/tests/script/tsim/tmq/basic2Of2Cons.sim @@ -0,0 +1,330 @@ +#### test scenario, please refer to https://jira.taosdata.com:18090/pages/viewpage.action?pageId=135120406 +#basic1Of2Cons.sim: vgroups=1, consumer=2, one topic for one consumer, firstly insert data, then start consume. Include six topics +#basic2Of2Cons.sim: vgroups=1, consumer=2, multi topics for one consumer, firstly insert data, then start consume. Include six topics +#basic3Of2Cons.sim: vgroups=4, consumer=2, one topic for one consumer, firstly insert data, then start consume. Include six topics +#basic4Of2Cons.sim: vgroups=4, consumer=2, multi topics for one consumer, firstly insert data, then start consume. Include six topics + +# notes1: Scalar function: ABS/ACOS/ASIN/ATAN/CEIL/COS/FLOOR/LOG/POW/ROUND/SIN/SQRT/TAN +# The above use cases are combined with where filter conditions, such as: where ts > "2017-08-12 18:25:58.128Z" and sin(a) > 0.5; +# +# notes2: not support aggregate functions(such as sum/count/min/max) and time-windows(interval). +# + +run tsim/tmq/prepareBasicEnv-1vgrp.sim + +#---- global parameters start ----# +$dbName = db +$vgroups = 1 +$stbPrefix = stb +$ctbPrefix = ctb +$ntbPrefix = ntb +$stbNum = 1 +$ctbNum = 10 +$ntbNum = 10 +$rowsPerCtb = 10 +$tstart = 1640966400000 # 2022-01-01 00:00:00.000 +#---- global parameters end ----# + +$pullDelay = 5 +$ifcheckdata = 1 +$showMsg = 1 +$showRow = 0 + +sql connect +sql use $dbName + +print == create topics from super table +sql create topic topic_stb_column as select ts, c3 from stb +sql create topic topic_stb_all as select ts, c1, c2, c3 from stb +sql create topic topic_stb_function as select ts, abs(c1), sin(c2) from stb + +print == create topics from child table +sql create topic topic_ctb_column as select ts, c3 from ctb0 +sql create topic topic_ctb_all as select * from ctb0 +sql create topic topic_ctb_function as select ts, abs(c1), sin(c2) from ctb0 + +print == create topics from normal table +sql create topic topic_ntb_column as select ts, c3 from ntb0 +sql create topic topic_ntb_all as select * from ntb0 +sql create topic topic_ntb_function as select ts, abs(c1), sin(c2) from ntb0 + +#sql show topics +#if $rows != 9 then +# return -1 +#endi + +$keyList = ' . group.id:cgrp1 +$keyList = $keyList . ' + +$topicNum = 3 + +#=============================== start consume =============================# + + +print ================ test consume from stb +print == multi toipcs: topic_stb_column + topic_stb_all + topic_stb_function +$topicList = ' . topic_stb_column +$topicList = $topicList . , +$topicList = $topicList . topic_stb_all +$topicList = $topicList . , +$topicList = $topicList . topic_stb_function +$topicList = $topicList . ' + +$consumerId = 0 +$totalMsgOfStb = $ctbNum * $rowsPerCtb +$totalMsgOfStb = $totalMsgOfStb * $topicNum +$expectmsgcnt = $totalMsgOfStb +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) +$consumerId = 1 +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + +print == start consumer to pull msgs from stb +print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $dbName -s start +system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $dbName -s start + +print == check consume result +wait_consumer_end_from_stb: +sql select * from consumeresult +print ==> rows: $rows +print ==> rows[0]: $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6] +if $rows != 2 then + sleep 1000 + goto wait_consumer_end_from_stb +endi +if $data[0][1] == 0 then + if $data[1][1] != 1 then + return -1 + endi +endi +if $data[0][1] == 1 then + if $data[1][1] != 0 then + return -1 + endi +endi + +if $data[0][2] != 0 then + if $data[0][2] != $expectmsgcnt then + return -1 + endi + if $data[1][2] != 0 then + return -1 + endi +endi +if $data[1][2] != 0 then + if $data[1][2] != $expectmsgcnt then + return -1 + endi + if $data[0][2] != 0 then + return -1 + endi +endi + +if $data[0][3] != 0 then + if $data[0][3] != $expectmsgcnt then + return -1 + endi + if $data[1][3] != 0 then + return -1 + endi +endi +if $data[1][3] != 0 then + if $data[1][3] != $expectmsgcnt then + return -1 + endi + if $data[0][3] != 0 then + return -1 + endi +endi + +####################################################################################### +# clear consume info and consume result +#run tsim/tmq/clearConsume.sim +# because drop table function no stable, so by create new db for consume info and result. Modify it later +$cdbName = cdb1 +sql create database $cdbName vgroups 1 +sleep 500 +sql use $cdbName + +print == create consume info table and consume result table for ctb +sql create table consumeinfo (ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int) +sql create table consumeresult (ts timestamp, consumerid int, consummsgcnt bigint, consumrowcnt bigint, checkresult int) + +sql show tables +if $rows != 2 then + return -1 +endi +####################################################################################### + + +print ================ test consume from ctb +print == multi toipcs: topic_ctb_column + topic_ctb_all + topic_ctb_function +$topicList = ' . topic_ctb_column +$topicList = $topicList . , +$topicList = $topicList . topic_ctb_all +$topicList = $topicList . , +$topicList = $topicList . topic_ctb_function +$topicList = $topicList . ' + +$consumerId = 0 +$totalMsgOfCtb = $rowsPerCtb * $topicNum +$expectmsgcnt = $totalMsgOfCtb +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) +$consumerId = 1 +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + +print == start consumer to pull msgs from ctb +print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start +system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start + +print == check consume result +wait_consumer_end_from_ctb: +sql select * from consumeresult +print ==> rows: $rows +print ==> rows[0]: $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6] +if $rows != 2 then + sleep 1000 + goto wait_consumer_end_from_ctb +endi +if $data[0][1] == 0 then + if $data[1][1] != 1 then + return -1 + endi +endi +if $data[0][1] == 1 then + if $data[1][1] != 0 then + return -1 + endi +endi + +if $data[0][2] != $totalMsgOfCtb then + if $data[1][2] != $totalMsgOfCtb then + return -1 + endi + if $data[0][2] != 0 then + return -1 + endi +endi +if $data[1][2] != $totalMsgOfCtb then + if $data[0][2] != $totalMsgOfCtb then + return -1 + endi + if $data[1][2] != 0 then + return -1 + endi +endi + +if $data[0][3] != $totalMsgOfCtb then + if $data[1][3] != $totalMsgOfCtb then + return -1 + endi + if $data[0][3] != 0 then + return -1 + endi +endi +if $data[1][3] != $totalMsgOfCtb then + if $data[0][3] != $totalMsgOfCtb then + return -1 + endi + if $data[1][3] != 0 then + return -1 + endi +endi + +####################################################################################### +# clear consume info and consume result +#run tsim/tmq/clearConsume.sim +# because drop table function no stable, so by create new db for consume info and result. Modify it later +$cdbName = cdb2 +sql create database $cdbName vgroups 1 +sleep 500 +sql use $cdbName + +print == create consume info table and consume result table for ntb +sql create table consumeinfo (ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int) +sql create table consumeresult (ts timestamp, consumerid int, consummsgcnt bigint, consumrowcnt bigint, checkresult int) + +sql show tables +if $rows != 2 then + return -1 +endi +####################################################################################### + + +print ================ test consume from ntb +print == multi toipcs: topic_ntb_column + topic_ntb_all + topic_ntb_function +$topicList = ' . topic_ntb_column +$topicList = $topicList . , +$topicList = $topicList . topic_ntb_all +$topicList = $topicList . , +$topicList = $topicList . topic_ntb_function +$topicList = $topicList . ' + +$consumerId = 0 +$totalMsgOfNtb = $rowsPerCtb * $topicNum +$expectmsgcnt = $totalMsgOfNtb +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) +$consumerId = 1 +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + +print == start consumer to pull msgs from ntb +print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start +system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start + +print == check consume result from ntb +wait_consumer_end_from_ntb: +sql select * from consumeresult +print ==> rows: $rows +print ==> rows[0]: $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6] +if $rows != 2 then + sleep 1000 + goto wait_consumer_end_from_ntb +endi +if $data[0][1] == 0 then + if $data[1][1] != 1 then + return -1 + endi +endi +if $data[1][1] == 0 then + if $data[0][1] != 1 then + return -1 + endi +endi + +if $data[0][2] != $totalMsgOfNtb then + if $data[1][2] != $totalMsgOfNtb then + return -1 + endi + if $data[0][2] != 0 then + return -1 + endi +endi +if $data[1][2] != $totalMsgOfNtb then + if $data[0][2] != $totalMsgOfNtb then + return -1 + endi + if $data[1][2] != 0 then + return -1 + endi +endi + +if $data[0][3] != $totalMsgOfNtb then + if $data[1][3] != $totalMsgOfNtb then + return -1 + endi + if $data[0][3] != 0 then + return -1 + endi +endi +if $data[1][3] != $totalMsgOfNtb then + if $data[0][3] != $totalMsgOfNtb then + return -1 + endi + if $data[1][3] != 0 then + return -1 + endi +endi + +#------ not need stop consumer, because it exit after pull msg overthan expect msg +#system tsim/tmq/consume.sh -s stop -x SIGINT + +system sh/exec.sh -n dnode1 -s stop -x SIGINT diff --git a/tests/test/c/tmqSim.c b/tests/test/c/tmqSim.c index 9f6c1a59eb..cf627fb5fb 100644 --- a/tests/test/c/tmqSim.c +++ b/tests/test/c/tmqSim.c @@ -371,6 +371,8 @@ void *consumeThreadFunc(void *param) { loop_consume(pInfo); + tmq_commit(pInfo->tmq, NULL, 0); + err = tmq_unsubscribe(pInfo->tmq); if (err) { printf("tmq_unsubscribe() fail, reason: %s\n", tmq_err2str(err)); From 9c9d57c961c655599f340a4618a2840883b09a57 Mon Sep 17 00:00:00 2001 From: Li Minghao Date: Sat, 23 Apr 2022 22:38:49 -0700 Subject: [PATCH 44/51] fix bug, syncEnv free NULL --- source/libs/sync/src/syncEnv.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/source/libs/sync/src/syncEnv.c b/source/libs/sync/src/syncEnv.c index 0e04793a83..8f7a717e00 100644 --- a/source/libs/sync/src/syncEnv.c +++ b/source/libs/sync/src/syncEnv.c @@ -91,8 +91,10 @@ static SSyncEnv *doSyncEnvStart() { } static int32_t doSyncEnvStop(SSyncEnv *pSyncEnv) { - taosTmrCleanUp(pSyncEnv->pTimerManager); - taosMemoryFree(pSyncEnv); + if (pSyncEnv != NULL) { + taosTmrCleanUp(pSyncEnv->pTimerManager); + taosMemoryFree(pSyncEnv); + } return 0; } From 3d972f81a2d6fa511712e2d14f7e574ee5ef01dc Mon Sep 17 00:00:00 2001 From: Liu Jicong Date: Sun, 24 Apr 2022 13:42:54 +0800 Subject: [PATCH 45/51] refacor(wal): rename len to bodyLen --- include/common/tmsg.h | 21 ++++++++++----------- include/libs/wal/wal.h | 2 +- source/dnode/mnode/impl/src/mndOffset.c | 2 +- source/libs/sync/src/syncRaftLog.c | 8 ++++---- source/libs/wal/inc/walInt.h | 8 ++++---- source/libs/wal/src/walRead.c | 14 ++++++++------ source/libs/wal/src/walWrite.c | 2 +- source/libs/wal/test/walMetaTest.cpp | 6 +++--- 8 files changed, 32 insertions(+), 31 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index c279836b00..757fa8e74b 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -491,7 +491,7 @@ typedef struct { char intervalUnit; char slidingUnit; char - offsetUnit; // TODO Remove it, the offset is the number of precision tickle, and it must be a immutable duration. + offsetUnit; // TODO Remove it, the offset is the number of precision tickle, and it must be a immutable duration. int8_t precision; int64_t interval; int64_t sliding; @@ -651,7 +651,6 @@ typedef struct SQueryNodeAddr { SEpSet epSet; } SQueryNodeAddr; - typedef struct { SArray* pArray; // Array of SUseDbRsp } SUseDbBatchRsp; @@ -724,7 +723,7 @@ typedef struct { int32_t tSerializeSRetrieveFuncRsp(void* buf, int32_t bufLen, SRetrieveFuncRsp* pRsp); int32_t tDeserializeSRetrieveFuncRsp(void* buf, int32_t bufLen, SRetrieveFuncRsp* pRsp); -void tFreeSFuncInfo(SFuncInfo *pInfo); +void tFreeSFuncInfo(SFuncInfo* pInfo); void tFreeSRetrieveFuncRsp(SRetrieveFuncRsp* pRsp); typedef struct { @@ -1289,14 +1288,14 @@ typedef struct { } SMVCreateStreamRsp, SMSCreateStreamRsp; typedef struct { - char name[TSDB_TOPIC_FNAME_LEN]; // accout.topic - int8_t igExists; - int8_t withTbName; - int8_t withSchema; - int8_t withTag; - char* sql; - char* ast; - char subscribeDbName[TSDB_DB_NAME_LEN]; + char name[TSDB_TOPIC_FNAME_LEN]; // accout.topic + int8_t igExists; + int8_t withTbName; + int8_t withSchema; + int8_t withTag; + char* sql; + char* ast; + char subscribeDbName[TSDB_DB_NAME_LEN]; } SCMCreateTopicReq; int32_t tSerializeSCMCreateTopicReq(void* buf, int32_t bufLen, const SCMCreateTopicReq* pReq); diff --git a/include/libs/wal/wal.h b/include/libs/wal/wal.h index 10d01f12ec..d41e797536 100644 --- a/include/libs/wal/wal.h +++ b/include/libs/wal/wal.h @@ -92,7 +92,7 @@ typedef struct SWalReadHead { int8_t headVer; int8_t reserved; int16_t msgType; - int32_t len; + int32_t bodyLen; int64_t ingestTs; // not implemented int64_t version; diff --git a/source/dnode/mnode/impl/src/mndOffset.c b/source/dnode/mnode/impl/src/mndOffset.c index f5433e8f9e..f075348704 100644 --- a/source/dnode/mnode/impl/src/mndOffset.c +++ b/source/dnode/mnode/impl/src/mndOffset.c @@ -172,7 +172,7 @@ static int32_t mndProcessCommitOffsetReq(SNodeMsg *pMsg) { bool create = false; SMqOffsetObj *pOffsetObj = mndAcquireOffset(pMnode, key); if (pOffsetObj == NULL) { - pOffsetObj = taosMemoryMalloc(sizeof(SMqOffset)); + pOffsetObj = taosMemoryMalloc(sizeof(SMqOffsetObj)); memcpy(pOffsetObj->key, key, TSDB_PARTITION_KEY_LEN); create = true; } diff --git a/source/libs/sync/src/syncRaftLog.c b/source/libs/sync/src/syncRaftLog.c index dc903ad7b2..ae153251c3 100644 --- a/source/libs/sync/src/syncRaftLog.c +++ b/source/libs/sync/src/syncRaftLog.c @@ -71,7 +71,7 @@ SSyncRaftEntry* logStoreGetEntry(SSyncLogStore* pLogStore, SyncIndex index) { SWalReadHandle* pWalHandle = walOpenReadHandle(pWal); assert(walReadWithHandle(pWalHandle, index) == 0); - SSyncRaftEntry* pEntry = syncEntryBuild(pWalHandle->pHead->head.len); + SSyncRaftEntry* pEntry = syncEntryBuild(pWalHandle->pHead->head.bodyLen); assert(pEntry != NULL); pEntry->msgType = TDMT_VND_SYNC_CLIENT_REQUEST; @@ -80,8 +80,8 @@ SSyncRaftEntry* logStoreGetEntry(SSyncLogStore* pLogStore, SyncIndex index) { pEntry->isWeak = pWalHandle->pHead->head.syncMeta.isWeek; pEntry->term = pWalHandle->pHead->head.syncMeta.term; pEntry->index = index; - assert(pEntry->dataLen == pWalHandle->pHead->head.len); - memcpy(pEntry->data, pWalHandle->pHead->head.body, pWalHandle->pHead->head.len); + assert(pEntry->dataLen == pWalHandle->pHead->head.bodyLen); + memcpy(pEntry->data, pWalHandle->pHead->head.body, pWalHandle->pHead->head.bodyLen); // need to hold, do not new every time!! walCloseReadHandle(pWalHandle); @@ -257,4 +257,4 @@ void logStoreSimpleLog2(char* s, SSyncLogStore* pLogStore) { char* serialized = logStoreSimple2Str(pLogStore); sTrace("logStoreSimpleLog2 | len:%lu | %s | %s", strlen(serialized), s, serialized); taosMemoryFree(serialized); -} \ No newline at end of file +} diff --git a/source/libs/wal/inc/walInt.h b/source/libs/wal/inc/walInt.h index ca0c756141..84fe2814ff 100644 --- a/source/libs/wal/inc/walInt.h +++ b/source/libs/wal/inc/walInt.h @@ -16,10 +16,10 @@ #ifndef _TD_WAL_INT_H_ #define _TD_WAL_INT_H_ -#include "tcompare.h" #include "taoserror.h" #include "tchecksum.h" #include "tcoding.h" +#include "tcompare.h" #include "wal.h" #ifdef __cplusplus @@ -41,10 +41,10 @@ typedef struct WalIdxEntry { } SWalIdxEntry; static inline int tSerializeWalIdxEntry(void** buf, SWalIdxEntry* pIdxEntry) { - int tlen; + int tlen = 0; tlen += taosEncodeFixedI64(buf, pIdxEntry->ver); tlen += taosEncodeFixedI64(buf, pIdxEntry->offset); - return 0; + return tlen; } static inline void* tDeserializeWalIdxEntry(void* buf, SWalIdxEntry* pIdxEntry) { @@ -103,7 +103,7 @@ static inline int walValidHeadCksum(SWalHead* pHead) { } static inline int walValidBodyCksum(SWalHead* pHead) { - return taosCheckChecksum((uint8_t*)pHead->head.body, pHead->head.len, pHead->cksumBody); + return taosCheckChecksum((uint8_t*)pHead->head.body, pHead->head.bodyLen, pHead->cksumBody); } static inline int walValidCksum(SWalHead* pHead, void* body, int64_t bodyLen) { diff --git a/source/libs/wal/src/walRead.c b/source/libs/wal/src/walRead.c index 2545eeab4b..6d598699c5 100644 --- a/source/libs/wal/src/walRead.c +++ b/source/libs/wal/src/walRead.c @@ -144,12 +144,12 @@ int32_t walReadWithHandle_s(SWalReadHandle *pRead, int64_t ver, SWalReadHead **p taosThreadMutexUnlock(&pRead->mutex); return -1; } - *ppHead = taosMemoryMalloc(sizeof(SWalReadHead) + pRead->pHead->head.len); + *ppHead = taosMemoryMalloc(sizeof(SWalReadHead) + pRead->pHead->head.bodyLen); if (*ppHead == NULL) { taosThreadMutexUnlock(&pRead->mutex); return -1; } - memcpy(*ppHead, &pRead->pHead->head, sizeof(SWalReadHead) + pRead->pHead->head.len); + memcpy(*ppHead, &pRead->pHead->head, sizeof(SWalReadHead) + pRead->pHead->head.bodyLen); taosThreadMutexUnlock(&pRead->mutex); return 0; } @@ -178,16 +178,18 @@ int32_t walReadWithHandle(SWalReadHandle *pRead, int64_t ver) { terrno = TSDB_CODE_WAL_FILE_CORRUPTED; return -1; } - if (pRead->capacity < pRead->pHead->head.len) { - void *ptr = taosMemoryRealloc(pRead->pHead, sizeof(SWalHead) + pRead->pHead->head.len); + if (pRead->capacity < pRead->pHead->head.bodyLen) { + void *ptr = taosMemoryRealloc(pRead->pHead, sizeof(SWalHead) + pRead->pHead->head.bodyLen); if (ptr == NULL) { terrno = TSDB_CODE_WAL_OUT_OF_MEMORY; return -1; } pRead->pHead = ptr; - pRead->capacity = pRead->pHead->head.len; + pRead->capacity = pRead->pHead->head.bodyLen; } - if (pRead->pHead->head.len != taosReadFile(pRead->pReadLogTFile, pRead->pHead->head.body, pRead->pHead->head.len)) { + + if (pRead->pHead->head.bodyLen != + taosReadFile(pRead->pReadLogTFile, pRead->pHead->head.body, pRead->pHead->head.bodyLen)) { return -1; } diff --git a/source/libs/wal/src/walWrite.c b/source/libs/wal/src/walWrite.c index 81f2d82ea5..530930c261 100644 --- a/source/libs/wal/src/walWrite.c +++ b/source/libs/wal/src/walWrite.c @@ -295,7 +295,7 @@ int64_t walWriteWithSyncInfo(SWal *pWal, int64_t index, tmsg_t msgType, SSyncLog pWal->writeHead.head.version = index; int64_t offset = walGetCurFileOffset(pWal); - pWal->writeHead.head.len = bodyLen; + pWal->writeHead.head.bodyLen = bodyLen; pWal->writeHead.head.msgType = msgType; // sync info diff --git a/source/libs/wal/test/walMetaTest.cpp b/source/libs/wal/test/walMetaTest.cpp index b1de3a1dce..18345699b2 100644 --- a/source/libs/wal/test/walMetaTest.cpp +++ b/source/libs/wal/test/walMetaTest.cpp @@ -320,7 +320,7 @@ TEST_F(WalKeepEnv, readHandleRead) { char newStr[100]; sprintf(newStr, "%s-%d", ranStr, ver); int len = strlen(newStr); - ASSERT_EQ(pRead->pHead->head.len, len); + ASSERT_EQ(pRead->pHead->head.bodyLen, len); for (int j = 0; j < len; j++) { EXPECT_EQ(newStr[j], pRead->pHead->head.body[j]); } @@ -372,7 +372,7 @@ TEST_F(WalRetentionEnv, repairMeta1) { char newStr[100]; sprintf(newStr, "%s-%d", ranStr, ver); int len = strlen(newStr); - ASSERT_EQ(pRead->pHead->head.len, len); + ASSERT_EQ(pRead->pHead->head.bodyLen, len); for (int j = 0; j < len; j++) { EXPECT_EQ(newStr[j], pRead->pHead->head.body[j]); } @@ -402,7 +402,7 @@ TEST_F(WalRetentionEnv, repairMeta1) { char newStr[100]; sprintf(newStr, "%s-%d", ranStr, ver); int len = strlen(newStr); - ASSERT_EQ(pRead->pHead->head.len, len); + ASSERT_EQ(pRead->pHead->head.bodyLen, len); for (int j = 0; j < len; j++) { EXPECT_EQ(newStr[j], pRead->pHead->head.body[j]); } From 43f8b795541175dde52d2eb129ee94c7182ce172 Mon Sep 17 00:00:00 2001 From: plum-lihui Date: Sun, 24 Apr 2022 14:12:55 +0800 Subject: [PATCH 46/51] [test: add tmq test cases] --- tests/script/tsim/tmq/basic1Of2Cons.sim | 8 +- tests/script/tsim/tmq/basic2Of2Cons.sim | 11 +- tests/script/tsim/tmq/basic3Of2Cons.sim | 382 ++++++++++++++++++++++++ tests/script/tsim/tmq/basic4Of2Cons.sim | 324 ++++++++++++++++++++ 4 files changed, 717 insertions(+), 8 deletions(-) create mode 100644 tests/script/tsim/tmq/basic3Of2Cons.sim create mode 100644 tests/script/tsim/tmq/basic4Of2Cons.sim diff --git a/tests/script/tsim/tmq/basic1Of2Cons.sim b/tests/script/tsim/tmq/basic1Of2Cons.sim index a5a13f4e8b..5f54715b36 100644 --- a/tests/script/tsim/tmq/basic1Of2Cons.sim +++ b/tests/script/tsim/tmq/basic1Of2Cons.sim @@ -1,8 +1,8 @@ #### test scenario, please refer to https://jira.taosdata.com:18090/pages/viewpage.action?pageId=135120406 -#basic1Of2Cons.sim: vgroups=1, consumer=2, one topic for one consumer, firstly insert data, then start consume. Include six topics -#basic2Of2Cons.sim: vgroups=1, consumer=2, multi topics for one consumer, firstly insert data, then start consume. Include six topics -#basic3Of2Cons.sim: vgroups=4, consumer=2, one topic for one consumer, firstly insert data, then start consume. Include six topics -#basic4Of2Cons.sim: vgroups=4, consumer=2, multi topics for one consumer, firstly insert data, then start consume. Include six topics +#basic1Of2Cons.sim: vgroups=1, one topic for 2 consumers, firstly insert data, then start consume. Include six topics +#basic2Of2Cons.sim: vgroups=1, multi topics for 2 consumers, firstly insert data, then start consume. Include six topics +#basic3Of2Cons.sim: vgroups=4, one topic for 2 consumers, firstly insert data, then start consume. Include six topics +#basic4Of2Cons.sim: vgroups=4, multi topics for 2 consumers, firstly insert data, then start consume. Include six topics # notes1: Scalar function: ABS/ACOS/ASIN/ATAN/CEIL/COS/FLOOR/LOG/POW/ROUND/SIN/SQRT/TAN # The above use cases are combined with where filter conditions, such as: where ts > "2017-08-12 18:25:58.128Z" and sin(a) > 0.5; diff --git a/tests/script/tsim/tmq/basic2Of2Cons.sim b/tests/script/tsim/tmq/basic2Of2Cons.sim index 89b6114ebe..354260fb47 100644 --- a/tests/script/tsim/tmq/basic2Of2Cons.sim +++ b/tests/script/tsim/tmq/basic2Of2Cons.sim @@ -1,8 +1,8 @@ #### test scenario, please refer to https://jira.taosdata.com:18090/pages/viewpage.action?pageId=135120406 -#basic1Of2Cons.sim: vgroups=1, consumer=2, one topic for one consumer, firstly insert data, then start consume. Include six topics -#basic2Of2Cons.sim: vgroups=1, consumer=2, multi topics for one consumer, firstly insert data, then start consume. Include six topics -#basic3Of2Cons.sim: vgroups=4, consumer=2, one topic for one consumer, firstly insert data, then start consume. Include six topics -#basic4Of2Cons.sim: vgroups=4, consumer=2, multi topics for one consumer, firstly insert data, then start consume. Include six topics +#basic1Of2Cons.sim: vgroups=1, one topic for 2 consumers, firstly insert data, then start consume. Include six topics +#basic2Of2Cons.sim: vgroups=1, multi topics for 2 consumers, firstly insert data, then start consume. Include six topics +#basic3Of2Cons.sim: vgroups=4, one topic for 2 consumers, firstly insert data, then start consume. Include six topics +#basic4Of2Cons.sim: vgroups=4, multi topics for 2 consumers, firstly insert data, then start consume. Include six topics # notes1: Scalar function: ABS/ACOS/ASIN/ATAN/CEIL/COS/FLOOR/LOG/POW/ROUND/SIN/SQRT/TAN # The above use cases are combined with where filter conditions, such as: where ts > "2017-08-12 18:25:58.128Z" and sin(a) > 0.5; @@ -87,6 +87,7 @@ wait_consumer_end_from_stb: sql select * from consumeresult print ==> rows: $rows print ==> rows[0]: $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6] +print ==> rows[1]: $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6] if $rows != 2 then sleep 1000 goto wait_consumer_end_from_stb @@ -181,6 +182,7 @@ wait_consumer_end_from_ctb: sql select * from consumeresult print ==> rows: $rows print ==> rows[0]: $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6] +print ==> rows[1]: $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6] if $rows != 2 then sleep 1000 goto wait_consumer_end_from_ctb @@ -275,6 +277,7 @@ wait_consumer_end_from_ntb: sql select * from consumeresult print ==> rows: $rows print ==> rows[0]: $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6] +print ==> rows[1]: $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6] if $rows != 2 then sleep 1000 goto wait_consumer_end_from_ntb diff --git a/tests/script/tsim/tmq/basic3Of2Cons.sim b/tests/script/tsim/tmq/basic3Of2Cons.sim new file mode 100644 index 0000000000..ba3b54e43b --- /dev/null +++ b/tests/script/tsim/tmq/basic3Of2Cons.sim @@ -0,0 +1,382 @@ +#### test scenario, please refer to https://jira.taosdata.com:18090/pages/viewpage.action?pageId=135120406 +#basic1Of2Cons.sim: vgroups=1, one topic for 2 consumers, firstly insert data, then start consume. Include six topics +#basic2Of2Cons.sim: vgroups=1, multi topics for 2 consumers, firstly insert data, then start consume. Include six topics +#basic3Of2Cons.sim: vgroups=4, one topic for 2 consumers, firstly insert data, then start consume. Include six topics +#basic4Of2Cons.sim: vgroups=4, multi topics for 2 consumers, firstly insert data, then start consume. Include six topics + +# notes1: Scalar function: ABS/ACOS/ASIN/ATAN/CEIL/COS/FLOOR/LOG/POW/ROUND/SIN/SQRT/TAN +# The above use cases are combined with where filter conditions, such as: where ts > "2017-08-12 18:25:58.128Z" and sin(a) > 0.5; +# +# notes2: not support aggregate functions(such as sum/count/min/max) and time-windows(interval). +# + +run tsim/tmq/prepareBasicEnv-4vgrp.sim + +#---- global parameters start ----# +$dbName = db +$vgroups = 4 +$stbPrefix = stb +$ctbPrefix = ctb +$ntbPrefix = ntb +$stbNum = 1 +$ctbNum = 10 +$ntbNum = 10 +$rowsPerCtb = 10 +$tstart = 1640966400000 # 2022-01-01 00:00:00.000 +#---- global parameters end ----# + +$pullDelay = 5 +$ifcheckdata = 1 +$showMsg = 1 +$showRow = 0 + +sql connect +sql use $dbName + +print == create topics from super table +sql create topic topic_stb_column as select ts, c3 from stb +sql create topic topic_stb_all as select ts, c1, c2, c3 from stb +sql create topic topic_stb_function as select ts, abs(c1), sin(c2) from stb + +print == create topics from child table +sql create topic topic_ctb_column as select ts, c3 from ctb0 +sql create topic topic_ctb_all as select * from ctb0 +sql create topic topic_ctb_function as select ts, abs(c1), sin(c2) from ctb0 + +print == create topics from normal table +sql create topic topic_ntb_column as select ts, c3 from ntb0 +sql create topic topic_ntb_all as select * from ntb0 +sql create topic topic_ntb_function as select ts, abs(c1), sin(c2) from ntb0 + +#sql show topics +#if $rows != 9 then +# return -1 +#endi + +$keyList = ' . group.id:cgrp1 +$keyList = $keyList . ' + +$cdb_index = 0 +#=============================== start consume =============================# + +print ================ test consume from stb +$loop_cnt = 0 +loop_consume_diff_topic_from_stb: + +####################################################################################### +# clear consume info and consume result +#run tsim/tmq/clearConsume.sim +# because drop table function no stable, so by create new db for consume info and result. Modify it later +$cdb_index = $cdb_index + 1 +$cdbName = cdb . $cdb_index +sql create database $cdbName vgroups 1 +sleep 500 +sql use $cdbName + +print == create consume info table and consume result table +sql create table consumeinfo (ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int) +sql create table consumeresult (ts timestamp, consumerid int, consummsgcnt bigint, consumrowcnt bigint, checkresult int) + +sql show tables +if $rows != 2 then + return -1 +endi +####################################################################################### + +if $loop_cnt == 0 then + print == scenario 1: topic_stb_column + $topicList = ' . topic_stb_column + $topicList = $topicList . ' +elif $loop_cnt == 1 then + print == scenario 2: topic_stb_all + $topicList = ' . topic_stb_all + $topicList = $topicList . ' +elif $loop_cnt == 2 then + print == scenario 3: topic_stb_function + $topicList = ' . topic_stb_function + $topicList = $topicList . ' +else + goto loop_consume_diff_topic_from_stb_end +endi + +$consumerId = 0 +$totalMsgOfStb = $ctbNum * $rowsPerCtb +$expectmsgcnt = $totalMsgOfStb +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) +$consumerId = 1 +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + +print == start consumer to pull msgs from stb +print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start +system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start + +print == check consume result +wait_consumer_end_from_stb: +sql select * from consumeresult +print ==> rows: $rows +print ==> rows[0]: $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6] +print ==> rows[1]: $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6] +if $rows != 2 then + sleep 1000 + goto wait_consumer_end_from_stb +endi +if $data[0][1] == 0 then + if $data[1][1] != 1 then + return -1 + endi +endi +if $data[0][1] == 1 then + if $data[1][1] != 0 then + return -1 + endi +endi + +if $data[0][2] <= 0 then + return -1 +endi +if $data[0][2] >= $expectmsgcnt then + return -1 +endi + +if $data[1][2] <= 0 then + return -1 +endi +if $data[1][2] >= $expectmsgcnt then + return -1 +endi + +if $data[0][3] <= 0 then + return -1 +endi +if $data[0][3] >= $expectmsgcnt then + return -1 +endi + +if $data[1][3] <= 0 then + return -1 +endi +if $data[1][3] >= $expectmsgcnt then + return -1 +endi +$loop_cnt = $loop_cnt + 1 +goto loop_consume_diff_topic_from_stb +loop_consume_diff_topic_from_stb_end: + +print ================ test consume from ctb +$loop_cnt = 0 +loop_consume_diff_topic_from_ctb: + +####################################################################################### +# clear consume info and consume result +#run tsim/tmq/clearConsume.sim +# because drop table function no stable, so by create new db for consume info and result. Modify it later +$cdb_index = $cdb_index + 1 +$cdbName = cdb . $cdb_index +sql create database $cdbName vgroups 1 +sleep 500 +sql use $cdbName + +print == create consume info table and consume result table +sql create table consumeinfo (ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int) +sql create table consumeresult (ts timestamp, consumerid int, consummsgcnt bigint, consumrowcnt bigint, checkresult int) + +sql show tables +if $rows != 2 then + return -1 +endi +####################################################################################### + +if $loop_cnt == 0 then + print == scenario 1: topic_ctb_column + $topicList = ' . topic_ctb_column + $topicList = $topicList . ' +elif $loop_cnt == 1 then + print == scenario 2: topic_ctb_all + $topicList = ' . topic_ctb_all + $topicList = $topicList . ' +elif $loop_cnt == 2 then + print == scenario 3: topic_ctb_function + $topicList = ' . topic_ctb_function + $topicList = $topicList . ' +else + goto loop_consume_diff_topic_from_ctb_end +endi + +$consumerId = 0 +$totalMsgOfCtb = $rowsPerCtb +$expectmsgcnt = $totalMsgOfCtb +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + +print == start consumer to pull msgs from ctb +print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start +system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start + +print == check consume result +wait_consumer_end_from_ctb: +sql select * from consumeresult +print ==> rows: $rows +print ==> rows[0]: $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6] +print ==> rows[1]: $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6] +if $rows != 2 then + sleep 1000 + goto wait_consumer_end_from_ctb +endi +if $data[0][1] == 0 then + if $data[1][1] != 1 then + return -1 + endi +endi +if $data[0][1] == 1 then + if $data[1][1] != 0 then + return -1 + endi +endi + +if $data[0][2] != $totalMsgOfCtb then + if $data[1][2] != $totalMsgOfCtb then + return -1 + endi + if $data[0][2] != 0 then + return -1 + endi +endi +if $data[1][2] != $totalMsgOfCtb then + if $data[0][2] != $totalMsgOfCtb then + return -1 + endi + if $data[1][2] != 0 then + return -1 + endi +endi + +if $data[0][3] != $totalMsgOfCtb then + if $data[1][3] != $totalMsgOfCtb then + return -1 + endi + if $data[0][3] != 0 then + return -1 + endi +endi +if $data[1][3] != $totalMsgOfCtb then + if $data[0][3] != $totalMsgOfCtb then + return -1 + endi + if $data[1][3] != 0 then + return -1 + endi +endi +$loop_cnt = $loop_cnt + 1 +goto loop_consume_diff_topic_from_ctb +loop_consume_diff_topic_from_ctb_end: + +print ================ test consume from ntb +$loop_cnt = 0 +loop_consume_diff_topic_from_ntb: + +####################################################################################### +# clear consume info and consume result +#run tsim/tmq/clearConsume.sim +# because drop table function no stable, so by create new db for consume info and result. Modify it later +$cdb_index = $cdb_index + 1 +$cdbName = cdb . $cdb_index +sql create database $cdbName vgroups 1 +sleep 500 +sql use $cdbName + +print == create consume info table and consume result table +sql create table consumeinfo (ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int) +sql create table consumeresult (ts timestamp, consumerid int, consummsgcnt bigint, consumrowcnt bigint, checkresult int) + +sql show tables +if $rows != 2 then + return -1 +endi +####################################################################################### + +if $loop_cnt == 0 then + print == scenario 1: topic_ntb_column + $topicList = ' . topic_ntb_column + $topicList = $topicList . ' +elif $loop_cnt == 1 then + print == scenario 2: topic_ntb_all + $topicList = ' . topic_ntb_all + $topicList = $topicList . ' +elif $loop_cnt == 2 then + print == scenario 3: topic_ntb_function + $topicList = ' . topic_ntb_function + $topicList = $topicList . ' +else + goto loop_consume_diff_topic_from_ntb_end +endi + +$consumerId = 0 +$totalMsgOfNtb = $rowsPerCtb +$expectmsgcnt = $totalMsgOfNtb +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + +print == start consumer to pull msgs from ntb +print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start +system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start + +print == check consume result from ntb +wait_consumer_end_from_ntb: +sql select * from consumeresult +print ==> rows: $rows +print ==> rows[0]: $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6] +print ==> rows[1]: $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6] +if $rows != 2 then + sleep 1000 + goto wait_consumer_end_from_ntb +endi +if $data[0][1] == 0 then + if $data[1][1] != 1 then + return -1 + endi +endi +if $data[1][1] == 0 then + if $data[0][1] != 1 then + return -1 + endi +endi + +if $data[0][2] != $totalMsgOfNtb then + if $data[1][2] != $totalMsgOfNtb then + return -1 + endi + if $data[0][2] != 0 then + return -1 + endi +endi +if $data[1][2] != $totalMsgOfNtb then + if $data[0][2] != $totalMsgOfNtb then + return -1 + endi + if $data[1][2] != 0 then + return -1 + endi +endi + +if $data[0][3] != $totalMsgOfNtb then + if $data[1][3] != $totalMsgOfNtb then + return -1 + endi + if $data[0][3] != 0 then + return -1 + endi +endi +if $data[1][3] != $totalMsgOfNtb then + if $data[0][3] != $totalMsgOfNtb then + return -1 + endi + if $data[1][3] != 0 then + return -1 + endi +endi +$loop_cnt = $loop_cnt + 1 +goto loop_consume_diff_topic_from_ntb +loop_consume_diff_topic_from_ntb_end: + +#------ not need stop consumer, because it exit after pull msg overthan expect msg +#system tsim/tmq/consume.sh -s stop -x SIGINT + +system sh/exec.sh -n dnode1 -s stop -x SIGINT diff --git a/tests/script/tsim/tmq/basic4Of2Cons.sim b/tests/script/tsim/tmq/basic4Of2Cons.sim new file mode 100644 index 0000000000..75868bd974 --- /dev/null +++ b/tests/script/tsim/tmq/basic4Of2Cons.sim @@ -0,0 +1,324 @@ +#### test scenario, please refer to https://jira.taosdata.com:18090/pages/viewpage.action?pageId=135120406 +#basic1Of2Cons.sim: vgroups=1, one topic for 2 consumers, firstly insert data, then start consume. Include six topics +#basic2Of2Cons.sim: vgroups=1, multi topics for 2 consumers, firstly insert data, then start consume. Include six topics +#basic3Of2Cons.sim: vgroups=4, one topic for 2 consumers, firstly insert data, then start consume. Include six topics +#basic4Of2Cons.sim: vgroups=4, multi topics for 2 consumers, firstly insert data, then start consume. Include six topics + +# notes1: Scalar function: ABS/ACOS/ASIN/ATAN/CEIL/COS/FLOOR/LOG/POW/ROUND/SIN/SQRT/TAN +# The above use cases are combined with where filter conditions, such as: where ts > "2017-08-12 18:25:58.128Z" and sin(a) > 0.5; +# +# notes2: not support aggregate functions(such as sum/count/min/max) and time-windows(interval). +# + +run tsim/tmq/prepareBasicEnv-4vgrp.sim + +#---- global parameters start ----# +$dbName = db +$vgroups = 4 +$stbPrefix = stb +$ctbPrefix = ctb +$ntbPrefix = ntb +$stbNum = 1 +$ctbNum = 10 +$ntbNum = 10 +$rowsPerCtb = 10 +$tstart = 1640966400000 # 2022-01-01 00:00:00.000 +#---- global parameters end ----# + +$pullDelay = 5 +$ifcheckdata = 1 +$showMsg = 1 +$showRow = 0 + +sql connect +sql use $dbName + +print == create topics from super table +sql create topic topic_stb_column as select ts, c3 from stb +sql create topic topic_stb_all as select ts, c1, c2, c3 from stb +sql create topic topic_stb_function as select ts, abs(c1), sin(c2) from stb + +print == create topics from child table +sql create topic topic_ctb_column as select ts, c3 from ctb0 +sql create topic topic_ctb_all as select * from ctb0 +sql create topic topic_ctb_function as select ts, abs(c1), sin(c2) from ctb0 + +print == create topics from normal table +sql create topic topic_ntb_column as select ts, c3 from ntb0 +sql create topic topic_ntb_all as select * from ntb0 +sql create topic topic_ntb_function as select ts, abs(c1), sin(c2) from ntb0 + +#sql show topics +#if $rows != 9 then +# return -1 +#endi + +$keyList = ' . group.id:cgrp1 +$keyList = $keyList . ' + +$topicNum = 3 + +print ================ test consume from stb +print == multi toipcs: topic_stb_column + topic_stb_all + topic_stb_function +$topicList = ' . topic_stb_column +$topicList = $topicList . , +$topicList = $topicList . topic_stb_all +$topicList = $topicList . , +$topicList = $topicList . topic_stb_function +$topicList = $topicList . ' + +$consumerId = 0 +$totalMsgOfStb = $ctbNum * $rowsPerCtb +$totalMsgOfStb = $totalMsgOfStb * $topicNum +$expectmsgcnt = $totalMsgOfStb +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) +$consumerId = 1 +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + +print == start consumer to pull msgs from stb +print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $dbName -s start +system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $dbName -s start + +print == check consume result +wait_consumer_end_from_stb: +sql select * from consumeresult +print ==> rows: $rows +print ==> rows[0]: $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6] +print ==> rows[1]: $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6] +if $rows != 2 then + sleep 1000 + goto wait_consumer_end_from_stb +endi +if $data[0][1] == 0 then + if $data[1][1] != 1 then + return -1 + endi +endi +if $data[0][1] == 1 then + if $data[1][1] != 0 then + return -1 + endi +endi + +if $data[0][2] <= 0 then + return -1 +endi +if $data[0][2] >= $expectmsgcnt then + return -1 +endi + +if $data[1][2] <= 0 then + return -1 +endi +if $data[1][2] >= $expectmsgcnt then + return -1 +endi + +if $data[0][3] <= 0 then + return -1 +endi +if $data[0][3] >= $expectmsgcnt then + return -1 +endi + +if $data[1][3] <= 0 then + return -1 +endi +if $data[1][3] >= $expectmsgcnt then + return -1 +endi + +####################################################################################### +# clear consume info and consume result +#run tsim/tmq/clearConsume.sim +# because drop table function no stable, so by create new db for consume info and result. Modify it later +$cdbName = cdb1 +sql create database $cdbName vgroups 1 +sleep 500 +sql use $cdbName + +print == create consume info table and consume result table +sql create table consumeinfo (ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int) +sql create table consumeresult (ts timestamp, consumerid int, consummsgcnt bigint, consumrowcnt bigint, checkresult int) + +sql show tables +if $rows != 2 then + return -1 +endi +####################################################################################### + + +print ================ test consume from ctb +print == multi toipcs: topic_ctb_column + topic_ctb_all + topic_ctb_function +$topicList = ' . topic_ctb_column +$topicList = $topicList . , +$topicList = $topicList . topic_ctb_all +$topicList = $topicList . , +$topicList = $topicList . topic_ctb_function +$topicList = $topicList . ' + +$consumerId = 0 +$totalMsgOfCtb = $rowsPerCtb * $topicNum +$expectmsgcnt = $totalMsgOfCtb +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) +$consumerId = 1 +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + +print == start consumer to pull msgs from ctb +print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start +system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start + +print == check consume result +wait_consumer_end_from_ctb: +sql select * from consumeresult +print ==> rows: $rows +print ==> rows[0]: $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6] +print ==> rows[1]: $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6] +if $rows != 2 then + sleep 1000 + goto wait_consumer_end_from_ctb +endi +if $data[0][1] == 0 then + if $data[1][1] != 1 then + return -1 + endi +endi +if $data[0][1] == 1 then + if $data[1][1] != 0 then + return -1 + endi +endi + +if $data[0][2] != $totalMsgOfCtb then + if $data[1][2] != $totalMsgOfCtb then + return -1 + endi + if $data[0][2] != 0 then + return -1 + endi +endi +if $data[1][2] != $totalMsgOfCtb then + if $data[0][2] != $totalMsgOfCtb then + return -1 + endi + if $data[1][2] != 0 then + return -1 + endi +endi + +if $data[0][3] != $totalMsgOfCtb then + if $data[1][3] != $totalMsgOfCtb then + return -1 + endi + if $data[0][3] != 0 then + return -1 + endi +endi +if $data[1][3] != $totalMsgOfCtb then + if $data[0][3] != $totalMsgOfCtb then + return -1 + endi + if $data[1][3] != 0 then + return -1 + endi +endi + +####################################################################################### +# clear consume info and consume result +#run tsim/tmq/clearConsume.sim +# because drop table function no stable, so by create new db for consume info and result. Modify it later +$cdbName = cdb2 +sql create database $cdbName vgroups 1 +sleep 500 +sql use $cdbName + +print == create consume info table and consume result table +sql create table consumeinfo (ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int) +sql create table consumeresult (ts timestamp, consumerid int, consummsgcnt bigint, consumrowcnt bigint, checkresult int) + +sql show tables +if $rows != 2 then + return -1 +endi +####################################################################################### + + +print ================ test consume from ntb +print == multi toipcs: topic_ntb_column + topic_ntb_all + topic_ntb_function +$topicList = ' . topic_ntb_column +$topicList = $topicList . , +$topicList = $topicList . topic_ntb_all +$topicList = $topicList . , +$topicList = $topicList . topic_ntb_function +$topicList = $topicList . ' + +$consumerId = 0 +$totalMsgOfNtb = $rowsPerCtb * $topicNum +$expectmsgcnt = $totalMsgOfNtb +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) +$consumerId = 1 +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + +print == start consumer to pull msgs from ntb +print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start +system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start + +print == check consume result from ntb +wait_consumer_end_from_ntb: +sql select * from consumeresult +print ==> rows: $rows +print ==> rows[0]: $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6] +print ==> rows[1]: $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6] +if $rows != 2 then + sleep 1000 + goto wait_consumer_end_from_ntb +endi +if $data[0][1] == 0 then + if $data[1][1] != 1 then + return -1 + endi +endi +if $data[1][1] == 0 then + if $data[0][1] != 1 then + return -1 + endi +endi + +if $data[0][2] != $totalMsgOfNtb then + if $data[1][2] != $totalMsgOfNtb then + return -1 + endi + if $data[0][2] != 0 then + return -1 + endi +endi +if $data[1][2] != $totalMsgOfNtb then + if $data[0][2] != $totalMsgOfNtb then + return -1 + endi + if $data[1][2] != 0 then + return -1 + endi +endi + +if $data[0][3] != $totalMsgOfNtb then + if $data[1][3] != $totalMsgOfNtb then + return -1 + endi + if $data[0][3] != 0 then + return -1 + endi +endi +if $data[1][3] != $totalMsgOfNtb then + if $data[0][3] != $totalMsgOfNtb then + return -1 + endi + if $data[1][3] != 0 then + return -1 + endi +endi + +#------ not need stop consumer, because it exit after pull msg overthan expect msg +#system tsim/tmq/consume.sh -s stop -x SIGINT + +system sh/exec.sh -n dnode1 -s stop -x SIGINT From e01cb71cea1dc1d2c1c27df325514f2412aa6b19 Mon Sep 17 00:00:00 2001 From: Li Minghao Date: Sat, 23 Apr 2022 23:37:17 -0700 Subject: [PATCH 47/51] comment doSyncEnvStart --- source/libs/sync/src/syncEnv.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/libs/sync/src/syncEnv.c b/source/libs/sync/src/syncEnv.c index 8f7a717e00..1a6b21cc9f 100644 --- a/source/libs/sync/src/syncEnv.c +++ b/source/libs/sync/src/syncEnv.c @@ -29,7 +29,8 @@ static void syncEnvTick(void *param, void *tmrId); int32_t syncEnvStart() { int32_t ret = 0; taosSeedRand(taosGetTimestampSec()); - gSyncEnv = doSyncEnvStart(gSyncEnv); + //gSyncEnv = doSyncEnvStart(gSyncEnv); + gSyncEnv = doSyncEnvStart(); assert(gSyncEnv != NULL); sTrace("syncEnvStart ok!"); return ret; From 1afda11b9c4fde58d780d9504660d828c69bd57d Mon Sep 17 00:00:00 2001 From: Li Minghao Date: Sat, 23 Apr 2022 23:55:01 -0700 Subject: [PATCH 48/51] comment doSyncEnvStart 2 --- source/libs/sync/src/syncEnv.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/source/libs/sync/src/syncEnv.c b/source/libs/sync/src/syncEnv.c index 1a6b21cc9f..ff2d4d4d27 100644 --- a/source/libs/sync/src/syncEnv.c +++ b/source/libs/sync/src/syncEnv.c @@ -92,10 +92,12 @@ static SSyncEnv *doSyncEnvStart() { } static int32_t doSyncEnvStop(SSyncEnv *pSyncEnv) { + assert(pSyncEnv == gSyncEnv); if (pSyncEnv != NULL) { taosTmrCleanUp(pSyncEnv->pTimerManager); taosMemoryFree(pSyncEnv); } + gSyncEnv = NULL; return 0; } From f1a0d736cf33cb8b9e72b09172a48c563b5dfa94 Mon Sep 17 00:00:00 2001 From: plum-lihui Date: Sun, 24 Apr 2022 15:12:33 +0800 Subject: [PATCH 49/51] [test: modify check result for tmq case] --- tests/script/tsim/tmq/basic3Of2Cons.sim | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/tests/script/tsim/tmq/basic3Of2Cons.sim b/tests/script/tsim/tmq/basic3Of2Cons.sim index ba3b54e43b..b097617326 100644 --- a/tests/script/tsim/tmq/basic3Of2Cons.sim +++ b/tests/script/tsim/tmq/basic3Of2Cons.sim @@ -206,6 +206,8 @@ $consumerId = 0 $totalMsgOfCtb = $rowsPerCtb $expectmsgcnt = $totalMsgOfCtb sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) +$consumerId = 1 +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) print == start consumer to pull msgs from ctb print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start @@ -313,6 +315,8 @@ $consumerId = 0 $totalMsgOfNtb = $rowsPerCtb $expectmsgcnt = $totalMsgOfNtb sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) +$consumerId = 1 +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) print == start consumer to pull msgs from ntb print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start From d66d638ce7583942c032f87103cd3929baa08565 Mon Sep 17 00:00:00 2001 From: plum-lihui Date: Sun, 24 Apr 2022 17:45:01 +0800 Subject: [PATCH 50/51] [test: add tmq test cases] --- tests/script/tsim/tmq/basic1.sim | 4 +- tests/script/tsim/tmq/basic1Of2Cons.sim | 134 +++---- tests/script/tsim/tmq/basic2.sim | 4 +- tests/script/tsim/tmq/basic2Of2Cons.sim | 132 +++---- .../script/tsim/tmq/basic2Of2ConsOverlap.sim | 337 ++++++++++++++++++ tests/script/tsim/tmq/basic3.sim | 4 +- tests/script/tsim/tmq/basic3Of2Cons.sim | 102 +++--- tests/script/tsim/tmq/basic4.sim | 4 +- tests/script/tsim/tmq/basic4Of2Cons.sim | 99 +++-- 9 files changed, 552 insertions(+), 268 deletions(-) create mode 100644 tests/script/tsim/tmq/basic2Of2ConsOverlap.sim diff --git a/tests/script/tsim/tmq/basic1.sim b/tests/script/tsim/tmq/basic1.sim index f4e8f08c37..d7534338e1 100644 --- a/tests/script/tsim/tmq/basic1.sim +++ b/tests/script/tsim/tmq/basic1.sim @@ -175,7 +175,7 @@ $totalMsgOfCtb = $rowsPerCtb $expectmsgcnt = $totalMsgOfCtb sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) -print == start consumer to pull msgs from stb +print == start consumer to pull msgs from ctb print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start @@ -246,7 +246,7 @@ $totalMsgOfNtb = $rowsPerCtb $expectmsgcnt = $totalMsgOfNtb sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) -print == start consumer to pull msgs from stb +print == start consumer to pull msgs from ntb print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start diff --git a/tests/script/tsim/tmq/basic1Of2Cons.sim b/tests/script/tsim/tmq/basic1Of2Cons.sim index 5f54715b36..957f1774f9 100644 --- a/tests/script/tsim/tmq/basic1Of2Cons.sim +++ b/tests/script/tsim/tmq/basic1Of2Cons.sim @@ -132,39 +132,32 @@ if $data[0][1] == 1 then endi endi -if $data[0][2] != 0 then - if $data[0][2] != $expectmsgcnt then - return -1 +# either $data[0][2] == $totalMsgOfStb and $data[1][2] == 0 +# or $data[0][2] == 0 and $data[1][2] == $totalMsgOfStb +if $data[0][2] == $totalMsgOfStb then + if $data[1][2] == 0 then + goto check_ok_0 endi - if $data[1][2] != 0 then - return -1 - endi -endi -if $data[1][2] != 0 then - if $data[1][2] != $expectmsgcnt then - return -1 - endi - if $data[0][2] != 0 then - return -1 +elif $data[0][2] == 0 then + if $data[1][2] == $totalMsgOfStb then + goto check_ok_0 endi endi +return -1 +check_ok_0: -if $data[0][3] != 0 then - if $data[0][3] != $expectmsgcnt then - return -1 +if $data[0][3] == $totalMsgOfStb then + if $data[1][3] == 0 then + goto check_ok_1 endi - if $data[1][3] != 0 then - return -1 - endi -endi -if $data[1][3] != 0 then - if $data[1][3] != $expectmsgcnt then - return -1 - endi - if $data[0][3] != 0 then - return -1 +elif $data[0][3] == 0 then + if $data[1][3] == $totalMsgOfStb then + goto check_ok_1 endi endi +return -1 +check_ok_1: + $loop_cnt = $loop_cnt + 1 goto loop_consume_diff_topic_from_stb loop_consume_diff_topic_from_stb_end: @@ -241,39 +234,31 @@ if $data[0][1] == 1 then endi endi -if $data[0][2] != $totalMsgOfCtb then - if $data[1][2] != $totalMsgOfCtb then - return -1 +# either $data[0][2] == $totalMsgOfCtb and $data[1][2] == 0 +# or $data[0][2] == 0 and $data[1][2] == $totalMsgOfCtb +if $data[0][2] == $totalMsgOfCtb then + if $data[1][2] == 0 then + goto check_ok_2 endi - if $data[0][2] != 0 then - return -1 - endi -endi -if $data[1][2] != $totalMsgOfCtb then - if $data[0][2] != $totalMsgOfCtb then - return -1 - endi - if $data[1][2] != 0 then - return -1 +elif $data[0][2] == 0 then + if $data[1][2] == $totalMsgOfCtb then + goto check_ok_2 endi endi +return -1 +check_ok_2: -if $data[0][3] != $totalMsgOfCtb then - if $data[1][3] != $totalMsgOfCtb then - return -1 +if $data[0][3] == $totalMsgOfCtb then + if $data[1][3] == 0 then + goto check_ok_3 endi - if $data[0][3] != 0 then - return -1 - endi -endi -if $data[1][3] != $totalMsgOfCtb then - if $data[0][3] != $totalMsgOfCtb then - return -1 +elif $data[0][3] == 0 then + if $data[1][3] == $totalMsgOfCtb then + goto check_ok_3 endi - if $data[1][3] != 0 then - return -1 - endi endi +return -1 +check_ok_3: $loop_cnt = $loop_cnt + 1 goto loop_consume_diff_topic_from_ctb @@ -351,39 +336,32 @@ if $data[1][1] == 0 then endi endi -if $data[0][2] != $totalMsgOfNtb then - if $data[1][2] != $totalMsgOfNtb then - return -1 +# either $data[0][2] == $totalMsgOfNtb and $data[1][2] == 0 +# or $data[0][2] == 0 and $data[1][2] == $totalMsgOfNtb +if $data[0][2] == $totalMsgOfNtb then + if $data[1][2] == 0 then + goto check_ok_4 endi - if $data[0][2] != 0 then - return -1 - endi -endi -if $data[1][2] != $totalMsgOfNtb then - if $data[0][2] != $totalMsgOfNtb then - return -1 - endi - if $data[1][2] != 0 then - return -1 +elif $data[0][2] == 0 then + if $data[1][2] == $totalMsgOfNtb then + goto check_ok_4 endi endi +return -1 +check_ok_4: -if $data[0][3] != $totalMsgOfNtb then - if $data[1][3] != $totalMsgOfNtb then - return -1 +if $data[0][3] == $totalMsgOfNtb then + if $data[1][3] == 0 then + goto check_ok_5 endi - if $data[0][3] != 0 then - return -1 - endi -endi -if $data[1][3] != $totalMsgOfNtb then - if $data[0][3] != $totalMsgOfNtb then - return -1 +elif $data[0][3] == 0 then + if $data[1][3] == $totalMsgOfNtb then + goto check_ok_5 endi - if $data[1][3] != 0 then - return -1 - endi endi +return -1 +check_ok_5: + $loop_cnt = $loop_cnt + 1 goto loop_consume_diff_topic_from_ntb loop_consume_diff_topic_from_ntb_end: diff --git a/tests/script/tsim/tmq/basic2.sim b/tests/script/tsim/tmq/basic2.sim index b3e9a67c62..ac0d2bb6df 100644 --- a/tests/script/tsim/tmq/basic2.sim +++ b/tests/script/tsim/tmq/basic2.sim @@ -133,7 +133,7 @@ $totalMsgOfCtb = $rowsPerCtb * $topicNum $expectmsgcnt = $totalMsgOfCtb sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) -print == start consumer to pull msgs from stb +print == start consumer to pull msgs from ctb print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start @@ -190,7 +190,7 @@ $totalMsgOfNtb = $rowsPerCtb * $topicNum $expectmsgcnt = $totalMsgOfNtb sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) -print == start consumer to pull msgs from stb +print == start consumer to pull msgs from ntb print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start diff --git a/tests/script/tsim/tmq/basic2Of2Cons.sim b/tests/script/tsim/tmq/basic2Of2Cons.sim index 354260fb47..01ccb2b515 100644 --- a/tests/script/tsim/tmq/basic2Of2Cons.sim +++ b/tests/script/tsim/tmq/basic2Of2Cons.sim @@ -103,39 +103,31 @@ if $data[0][1] == 1 then endi endi -if $data[0][2] != 0 then - if $data[0][2] != $expectmsgcnt then - return -1 +# either $data[0][2] == $totalMsgOfStb and $data[1][2] == 0 +# or $data[0][2] == 0 and $data[1][2] == $totalMsgOfStb +if $data[0][2] == $totalMsgOfStb then + if $data[1][2] == 0 then + goto check_ok_0 endi - if $data[1][2] != 0 then - return -1 - endi -endi -if $data[1][2] != 0 then - if $data[1][2] != $expectmsgcnt then - return -1 - endi - if $data[0][2] != 0 then - return -1 +elif $data[0][2] == 0 then + if $data[1][2] == $totalMsgOfStb then + goto check_ok_0 endi endi +return -1 +check_ok_0: -if $data[0][3] != 0 then - if $data[0][3] != $expectmsgcnt then - return -1 +if $data[0][3] == $totalMsgOfStb then + if $data[1][3] == 0 then + goto check_ok_1 endi - if $data[1][3] != 0 then - return -1 - endi -endi -if $data[1][3] != 0 then - if $data[1][3] != $expectmsgcnt then - return -1 - endi - if $data[0][3] != 0 then - return -1 +elif $data[0][3] == 0 then + if $data[1][3] == $totalMsgOfStb then + goto check_ok_1 endi endi +return -1 +check_ok_1: ####################################################################################### # clear consume info and consume result @@ -198,39 +190,31 @@ if $data[0][1] == 1 then endi endi -if $data[0][2] != $totalMsgOfCtb then - if $data[1][2] != $totalMsgOfCtb then - return -1 +# either $data[0][2] == $totalMsgOfCtb and $data[1][2] == 0 +# or $data[0][2] == 0 and $data[1][2] == $totalMsgOfCtb +if $data[0][2] == $totalMsgOfCtb then + if $data[1][2] == 0 then + goto check_ok_2 endi - if $data[0][2] != 0 then - return -1 - endi -endi -if $data[1][2] != $totalMsgOfCtb then - if $data[0][2] != $totalMsgOfCtb then - return -1 - endi - if $data[1][2] != 0 then - return -1 +elif $data[0][2] == 0 then + if $data[1][2] == $totalMsgOfCtb then + goto check_ok_2 endi endi +return -1 +check_ok_2: -if $data[0][3] != $totalMsgOfCtb then - if $data[1][3] != $totalMsgOfCtb then - return -1 +if $data[0][3] == $totalMsgOfCtb then + if $data[1][3] == 0 then + goto check_ok_3 endi - if $data[0][3] != 0 then - return -1 - endi -endi -if $data[1][3] != $totalMsgOfCtb then - if $data[0][3] != $totalMsgOfCtb then - return -1 +elif $data[0][3] == 0 then + if $data[1][3] == $totalMsgOfCtb then + goto check_ok_3 endi - if $data[1][3] != 0 then - return -1 - endi endi +return -1 +check_ok_3: ####################################################################################### # clear consume info and consume result @@ -293,39 +277,31 @@ if $data[1][1] == 0 then endi endi -if $data[0][2] != $totalMsgOfNtb then - if $data[1][2] != $totalMsgOfNtb then - return -1 +# either $data[0][2] == $totalMsgOfNtb and $data[1][2] == 0 +# or $data[0][2] == 0 and $data[1][2] == $totalMsgOfNtb +if $data[0][2] == $totalMsgOfNtb then + if $data[1][2] == 0 then + goto check_ok_4 endi - if $data[0][2] != 0 then - return -1 - endi -endi -if $data[1][2] != $totalMsgOfNtb then - if $data[0][2] != $totalMsgOfNtb then - return -1 - endi - if $data[1][2] != 0 then - return -1 +elif $data[0][2] == 0 then + if $data[1][2] == $totalMsgOfNtb then + goto check_ok_4 endi endi +return -1 +check_ok_4: -if $data[0][3] != $totalMsgOfNtb then - if $data[1][3] != $totalMsgOfNtb then - return -1 +if $data[0][3] == $totalMsgOfNtb then + if $data[1][3] == 0 then + goto check_ok_5 endi - if $data[0][3] != 0 then - return -1 - endi -endi -if $data[1][3] != $totalMsgOfNtb then - if $data[0][3] != $totalMsgOfNtb then - return -1 +elif $data[0][3] == 0 then + if $data[1][3] == $totalMsgOfNtb then + goto check_ok_5 endi - if $data[1][3] != 0 then - return -1 - endi endi +return -1 +check_ok_5: #------ not need stop consumer, because it exit after pull msg overthan expect msg #system tsim/tmq/consume.sh -s stop -x SIGINT diff --git a/tests/script/tsim/tmq/basic2Of2ConsOverlap.sim b/tests/script/tsim/tmq/basic2Of2ConsOverlap.sim new file mode 100644 index 0000000000..d04338aadd --- /dev/null +++ b/tests/script/tsim/tmq/basic2Of2ConsOverlap.sim @@ -0,0 +1,337 @@ +#### test scenario, please refer to https://jira.taosdata.com:18090/pages/viewpage.action?pageId=135120406 +#basic1Of2Cons.sim: vgroups=1, one topic for 2 consumers, firstly insert data, then start consume. Include six topics +#basic2Of2ConsOverlap.sim: vgroups=1, multi topics for 2 consumers, firstly insert data, then start consume. Include six topics +#basic3Of2Cons.sim: vgroups=4, one topic for 2 consumers, firstly insert data, then start consume. Include six topics +#basic4Of2Cons.sim: vgroups=4, multi topics for 2 consumers, firstly insert data, then start consume. Include six topics + +# notes1: Scalar function: ABS/ACOS/ASIN/ATAN/CEIL/COS/FLOOR/LOG/POW/ROUND/SIN/SQRT/TAN +# The above use cases are combined with where filter conditions, such as: where ts > "2017-08-12 18:25:58.128Z" and sin(a) > 0.5; +# +# notes2: not support aggregate functions(such as sum/count/min/max) and time-windows(interval). +# + +run tsim/tmq/prepareBasicEnv-1vgrp.sim + +#---- global parameters start ----# +$dbName = db +$vgroups = 1 +$stbPrefix = stb +$ctbPrefix = ctb +$ntbPrefix = ntb +$stbNum = 1 +$ctbNum = 10 +$ntbNum = 10 +$rowsPerCtb = 10 +$tstart = 1640966400000 # 2022-01-01 00:00:00.000 +#---- global parameters end ----# + +$pullDelay = 5 +$ifcheckdata = 1 +$showMsg = 1 +$showRow = 0 + +sql connect +sql use $dbName + +print == create topics from super table +sql create topic topic_stb_column as select ts, c3 from stb +sql create topic topic_stb_all as select ts, c1, c2, c3 from stb +sql create topic topic_stb_function as select ts, abs(c1), sin(c2) from stb + +print == create topics from child table +sql create topic topic_ctb_column as select ts, c3 from ctb0 +sql create topic topic_ctb_all as select * from ctb0 +sql create topic topic_ctb_function as select ts, abs(c1), sin(c2) from ctb0 + +print == create topics from normal table +sql create topic topic_ntb_column as select ts, c3 from ntb0 +sql create topic topic_ntb_all as select * from ntb0 +sql create topic topic_ntb_function as select ts, abs(c1), sin(c2) from ntb0 + +#sql show topics +#if $rows != 9 then +# return -1 +#endi + +$keyList = ' . group.id:cgrp1 +$keyList = $keyList . ' + +$topicNum = 2 + +#=============================== start consume =============================# + + +print ================ test consume from stb +print == overlap toipcs: topic_stb_column + topic_stb_all, topic_stb_function + topic_stb_all +$topicList = ' . topic_stb_column +$topicList = $topicList . , +$topicList = $topicList . topic_stb_all +$topicList = $topicList . ' + +$consumerId = 0 +$totalMsgOfOneTopic = $ctbNum * $rowsPerCtb +$totalMsgOfStb = $totalMsgOfOneTopic * $topicNum +$expectmsgcnt = $totalMsgOfStb +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + + +$topicList = ' . topic_stb_all +$topicList = $topicList . , +$topicList = $topicList . topic_stb_function +$topicList = $topicList . ' +$consumerId = 1 +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + +print == start consumer to pull msgs from stb +print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $dbName -s start +system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $dbName -s start + +print == check consume result +wait_consumer_end_from_stb: +sql select * from consumeresult +print ==> rows: $rows +print ==> rows[0]: $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6] +print ==> rows[1]: $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6] +if $rows != 2 then + sleep 1000 + goto wait_consumer_end_from_stb +endi +if $data[0][1] == 0 then + if $data[1][1] != 1 then + return -1 + endi +endi +if $data[0][1] == 1 then + if $data[1][1] != 0 then + return -1 + endi +endi + +# $data[0][2]/$data[1][2] should be between $totalMsgOfOneTopic and $totalMsgOfStb. + +if $data[0][2] < $totalMsgOfOneTopic then + return -1 +endi +if $data[0][2] > $totalMsgOfStb then + return -1 +endi +if $data[1][2] < $totalMsgOfOneTopic then + return -1 +endi +if $data[1][2] > $totalMsgOfStb then + return -1 +endi + +$totalMsgCons = $totalMsgOfOneTopic + $totalMsgOfStb +$sumOfMsgCnt = $data[0][2] + $data[1][2] +if $sumOfMsgCnt != $totalMsgCons then + return -1 +endi + +# $data[0][3]/$data[1][3] should be between $totalMsgOfOneTopic and $totalMsgOfStb. +if $data[0][3] < $totalMsgOfOneTopic then + return -1 +endi +if $data[0][3] > $totalMsgOfStb then + return -1 +endi +if $data[1][3] < $totalMsgOfOneTopic then + return -1 +endi +if $data[1][3] > $totalMsgOfStb then + return -1 +endi + +$totalMsgCons = $totalMsgOfOneTopic + $totalMsgOfStb +$sumOfRows = $data[0][3] + $data[1][3] +if $sumOfRows != $totalMsgCons then + return -1 +endi + +####################################################################################### +# clear consume info and consume result +#run tsim/tmq/clearConsume.sim +# because drop table function no stable, so by create new db for consume info and result. Modify it later +$cdbName = cdb1 +sql create database $cdbName vgroups 1 +sleep 500 +sql use $cdbName + +print == create consume info table and consume result table for ctb +sql create table consumeinfo (ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int) +sql create table consumeresult (ts timestamp, consumerid int, consummsgcnt bigint, consumrowcnt bigint, checkresult int) + +sql show tables +if $rows != 2 then + return -1 +endi +####################################################################################### + + +print ================ test consume from ctb +print == overlap toipcs: topic_ctb_column + topic_ctb_all, topic_ctb_function + topic_ctb_all +$topicList = ' . topic_ctb_column +$topicList = $topicList . , +$topicList = $topicList . topic_ctb_all +$topicList = $topicList . ' +$consumerId = 0 + +$totalMsgOfOneTopic = $rowsPerCtb +$totalMsgOfCtb = $totalMsgOfOneTopic * $topicNum +$expectmsgcnt = $totalMsgOfCtb +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + +$topicList = ' . topic_ctb_function +$topicList = $topicList . , +$topicList = $topicList . topic_ctb_all +$topicList = $topicList . ' +$consumerId = 1 +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + +print == start consumer to pull msgs from ctb +print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start +system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start + +print == check consume result +wait_consumer_end_from_ctb: +sql select * from consumeresult +print ==> rows: $rows +print ==> rows[0]: $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6] +print ==> rows[1]: $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6] +if $rows != 2 then + sleep 1000 + goto wait_consumer_end_from_ctb +endi +if $data[0][1] == 0 then + if $data[1][1] != 1 then + return -1 + endi +endi +if $data[0][1] == 1 then + if $data[1][1] != 0 then + return -1 + endi +endi + +# either $data[0][2] $totalMsgOfOneTopic and $data[1][2] == $totalMsgOfCtb +# or $data[0][2] $totalMsgOfCtb and $data[1][2] == $totalMsgOfOneTopic +if $data[0][2] == $totalMsgOfOneTopic then + if $data[1][2] == $totalMsgOfCtb then + goto check_ok_0 + endi +elif $data[1][2] == $totalMsgOfOneTopic then + if $data[0][2] == $totalMsgOfCtb then + goto check_ok_0 + endi +endi +return -1 +check_ok_0: + +if $data[0][3] == $totalMsgOfOneTopic then + if $data[1][3] == $totalMsgOfCtb then + goto check_ok_1 + endi +elif $data[1][3] == $totalMsgOfOneTopic then + if $data[0][3] == $totalMsgOfCtb then + goto check_ok_1 + endi +endi +return -1 +check_ok_1: + +####################################################################################### +# clear consume info and consume result +#run tsim/tmq/clearConsume.sim +# because drop table function no stable, so by create new db for consume info and result. Modify it later +$cdbName = cdb2 +sql create database $cdbName vgroups 1 +sleep 500 +sql use $cdbName + +print == create consume info table and consume result table for ntb +sql create table consumeinfo (ts timestamp, consumerid int, topiclist binary(1024), keylist binary(1024), expectmsgcnt bigint, ifcheckdata int) +sql create table consumeresult (ts timestamp, consumerid int, consummsgcnt bigint, consumrowcnt bigint, checkresult int) + +sql show tables +if $rows != 2 then + return -1 +endi +####################################################################################### + + +print ================ test consume from ntb +print == overlap toipcs: topic_ntb_column + topic_ntb_all, topic_ntb_function + topic_ntb_all +$topicList = ' . topic_ntb_column +$topicList = $topicList . , +$topicList = $topicList . topic_ntb_all +$topicList = $topicList . ' + +$consumerId = 0 +$totalMsgOfOneTopic = $rowsPerCtb +$totalMsgOfNtb = $totalMsgOfOneTopic * $topicNum +$expectmsgcnt = $totalMsgOfNtb +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + + +$topicList = ' . topic_ntb_function +$topicList = $topicList . , +$topicList = $topicList . topic_ntb_all +$topicList = $topicList . ' +$consumerId = 1 +sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) + +print == start consumer to pull msgs from ntb +print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start +system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start + +print == check consume result from ntb +wait_consumer_end_from_ntb: +sql select * from consumeresult +print ==> rows: $rows +print ==> rows[0]: $data[0][0] $data[0][1] $data[0][2] $data[0][3] $data[0][4] $data[0][5] $data[0][6] +print ==> rows[1]: $data[1][0] $data[1][1] $data[1][2] $data[1][3] $data[1][4] $data[1][5] $data[1][6] +if $rows != 2 then + sleep 1000 + goto wait_consumer_end_from_ctb +endi +if $data[0][1] == 0 then + if $data[1][1] != 1 then + return -1 + endi +endi +if $data[0][1] == 1 then + if $data[1][1] != 0 then + return -1 + endi +endi + +# either $data[0][2] $totalMsgOfOneTopic and $data[1][2] == $totalMsgOfNtb +# or $data[0][2] $totalMsgOfNtb and $data[1][2] == $totalMsgOfOneTopic +if $data[0][2] == $totalMsgOfOneTopic then + if $data[1][2] == $totalMsgOfNtb then + goto check_ok_2 + endi +elif $data[1][2] == $totalMsgOfOneTopic then + if $data[0][2] == $totalMsgOfNtb then + goto check_ok_2 + endi +endi +return -1 +check_ok_2: + +if $data[0][3] == $totalMsgOfOneTopic then + if $data[1][3] == $totalMsgOfNtb then + goto check_ok_3 + endi +elif $data[1][3] == $totalMsgOfOneTopic then + if $data[0][3] == $totalMsgOfNtb then + goto check_ok_3 + endi +endi +return -1 +check_ok_3: + +#------ not need stop consumer, because it exit after pull msg overthan expect msg +#system tsim/tmq/consume.sh -s stop -x SIGINT + +system sh/exec.sh -n dnode1 -s stop -x SIGINT diff --git a/tests/script/tsim/tmq/basic3.sim b/tests/script/tsim/tmq/basic3.sim index 7a0ee6f917..c0ba2c97fb 100644 --- a/tests/script/tsim/tmq/basic3.sim +++ b/tests/script/tsim/tmq/basic3.sim @@ -175,7 +175,7 @@ $totalMsgOfCtb = $rowsPerCtb $expectmsgcnt = $totalMsgOfCtb sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) -print == start consumer to pull msgs from stb +print == start consumer to pull msgs from ctb print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start @@ -246,7 +246,7 @@ $totalMsgOfNtb = $rowsPerCtb $expectmsgcnt = $totalMsgOfNtb sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) -print == start consumer to pull msgs from stb +print == start consumer to pull msgs from ntb print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start diff --git a/tests/script/tsim/tmq/basic3Of2Cons.sim b/tests/script/tsim/tmq/basic3Of2Cons.sim index b097617326..bf640ae1a1 100644 --- a/tests/script/tsim/tmq/basic3Of2Cons.sim +++ b/tests/script/tsim/tmq/basic3Of2Cons.sim @@ -145,6 +145,12 @@ if $data[1][2] >= $expectmsgcnt then return -1 endi +$sumOfMsgCnt = $data[0][2] + $data[1][2] +if $sumOfMsgCnt != $expectmsgcnt then + return -1 +endi + + if $data[0][3] <= 0 then return -1 endi @@ -158,6 +164,12 @@ endi if $data[1][3] >= $expectmsgcnt then return -1 endi + +$sumOfMsgRows = $data[0][3] + $data[1][3] +if $sumOfMsgRows != $expectmsgcnt then + return -1 +endi + $loop_cnt = $loop_cnt + 1 goto loop_consume_diff_topic_from_stb loop_consume_diff_topic_from_stb_end: @@ -234,39 +246,32 @@ if $data[0][1] == 1 then endi endi -if $data[0][2] != $totalMsgOfCtb then - if $data[1][2] != $totalMsgOfCtb then - return -1 +# either $data[0][2] == $totalMsgOfCtb and $data[1][2] == 0 +# or $data[0][2] == 0 and $data[1][2] == $totalMsgOfCtb +if $data[0][2] == $totalMsgOfCtb then + if $data[1][2] == 0 then + goto check_ok_0 endi - if $data[0][2] != 0 then - return -1 - endi -endi -if $data[1][2] != $totalMsgOfCtb then - if $data[0][2] != $totalMsgOfCtb then - return -1 - endi - if $data[1][2] != 0 then - return -1 +elif $data[1][2] == $totalMsgOfCtb then + if $data[0][2] == 0 then + goto check_ok_0 endi endi +return -1 +check_ok_0: -if $data[0][3] != $totalMsgOfCtb then - if $data[1][3] != $totalMsgOfCtb then - return -1 +if $data[0][3] == $totalMsgOfCtb then + if $data[1][3] == 0 then + goto check_ok_1 endi - if $data[0][3] != 0 then - return -1 - endi -endi -if $data[1][3] != $totalMsgOfCtb then - if $data[0][3] != $totalMsgOfCtb then - return -1 +elif $data[1][3] == $totalMsgOfCtb then + if $data[0][3] == 0 then + goto check_ok_1 endi - if $data[1][3] != 0 then - return -1 - endi endi +return -1 +check_ok_1: + $loop_cnt = $loop_cnt + 1 goto loop_consume_diff_topic_from_ctb loop_consume_diff_topic_from_ctb_end: @@ -343,39 +348,32 @@ if $data[1][1] == 0 then endi endi -if $data[0][2] != $totalMsgOfNtb then - if $data[1][2] != $totalMsgOfNtb then - return -1 +# either $data[0][2] == $totalMsgOfNtb and $data[1][2] == 0 +# or $data[0][2] == 0 and $data[1][2] == $totalMsgOfNtb +if $data[0][2] == $totalMsgOfNtb then + if $data[1][2] == 0 then + goto check_ok_2 endi - if $data[0][2] != 0 then - return -1 - endi -endi -if $data[1][2] != $totalMsgOfNtb then - if $data[0][2] != $totalMsgOfNtb then - return -1 - endi - if $data[1][2] != 0 then - return -1 +elif $data[1][2] == $totalMsgOfNtb then + if $data[0][2] == 0 then + goto check_ok_2 endi endi +return -1 +check_ok_2: -if $data[0][3] != $totalMsgOfNtb then - if $data[1][3] != $totalMsgOfNtb then - return -1 +if $data[0][3] == $totalMsgOfNtb then + if $data[1][3] == 0 then + goto check_ok_3 endi - if $data[0][3] != 0 then - return -1 - endi -endi -if $data[1][3] != $totalMsgOfNtb then - if $data[0][3] != $totalMsgOfNtb then - return -1 +elif $data[1][3] == $totalMsgOfNtb then + if $data[0][3] == 0 then + goto check_ok_3 endi - if $data[1][3] != 0 then - return -1 - endi endi +return -1 +check_ok_3: + $loop_cnt = $loop_cnt + 1 goto loop_consume_diff_topic_from_ntb loop_consume_diff_topic_from_ntb_end: diff --git a/tests/script/tsim/tmq/basic4.sim b/tests/script/tsim/tmq/basic4.sim index 1af02751b1..1eed93463c 100644 --- a/tests/script/tsim/tmq/basic4.sim +++ b/tests/script/tsim/tmq/basic4.sim @@ -130,7 +130,7 @@ $totalMsgOfCtb = $rowsPerCtb * $topicNum $expectmsgcnt = $totalMsgOfCtb sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) -print == start consumer to pull msgs from stb +print == start consumer to pull msgs from ctb print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start @@ -187,7 +187,7 @@ $totalMsgOfNtb = $rowsPerCtb * $topicNum $expectmsgcnt = $totalMsgOfNtb sql insert into consumeinfo values (now , $consumerId , $topicList , $keyList , $expectmsgcnt , $ifcheckdata ) -print == start consumer to pull msgs from stb +print == start consumer to pull msgs from ntb print == tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -s start system tsim/tmq/consume.sh -d $dbName -y $pullDelay -g $showMsg -r $showRow -w $cdbName -s start diff --git a/tests/script/tsim/tmq/basic4Of2Cons.sim b/tests/script/tsim/tmq/basic4Of2Cons.sim index 75868bd974..a17d8a2f45 100644 --- a/tests/script/tsim/tmq/basic4Of2Cons.sim +++ b/tests/script/tsim/tmq/basic4Of2Cons.sim @@ -114,6 +114,11 @@ if $data[1][2] >= $expectmsgcnt then return -1 endi +$sumOfConsMsg = $data[0][2] + $data[1][2] +if $sumOfConsMsg != $expectmsgcnt then + return -1 +endi + if $data[0][3] <= 0 then return -1 endi @@ -128,6 +133,11 @@ if $data[1][3] >= $expectmsgcnt then return -1 endi +$sumOfConsRow = $data[0][3] + $data[1][3] +if $sumOfConsRow != $expectmsgcnt then + return -1 +endi + ####################################################################################### # clear consume info and consume result #run tsim/tmq/clearConsume.sim @@ -189,39 +199,32 @@ if $data[0][1] == 1 then endi endi -if $data[0][2] != $totalMsgOfCtb then - if $data[1][2] != $totalMsgOfCtb then - return -1 +# either $data[0][2] == $totalMsgOfCtb and $data[1][2] == 0 +# or $data[0][2] == 0 and $data[1][2] == $totalMsgOfCtb +if $data[0][2] == $totalMsgOfCtb then + if $data[1][2] == 0 then + goto check_ok_0 endi - if $data[0][2] != 0 then - return -1 - endi -endi -if $data[1][2] != $totalMsgOfCtb then - if $data[0][2] != $totalMsgOfCtb then - return -1 - endi - if $data[1][2] != 0 then - return -1 +elif $data[0][2] == 0 then + if $data[1][2] == $totalMsgOfCtb then + goto check_ok_0 endi endi +return -1 +check_ok_0: -if $data[0][3] != $totalMsgOfCtb then - if $data[1][3] != $totalMsgOfCtb then - return -1 +if $data[0][3] == $totalMsgOfCtb then + if $data[1][3] == 0 then + goto check_ok_1 endi - if $data[0][3] != 0 then - return -1 - endi -endi -if $data[1][3] != $totalMsgOfCtb then - if $data[0][3] != $totalMsgOfCtb then - return -1 +elif $data[0][3] == 0 then + if $data[1][3] == $totalMsgOfCtb then + goto check_ok_1 endi - if $data[1][3] != 0 then - return -1 - endi endi +return -1 +check_ok_1: + ####################################################################################### # clear consume info and consume result @@ -284,39 +287,31 @@ if $data[1][1] == 0 then endi endi -if $data[0][2] != $totalMsgOfNtb then - if $data[1][2] != $totalMsgOfNtb then - return -1 +# either $data[0][2] == $totalMsgOfNtb and $data[1][2] == 0 +# or $data[0][2] == 0 and $data[1][2] == $totalMsgOfNtb +if $data[0][2] == $totalMsgOfNtb then + if $data[1][2] == 0 then + goto check_ok_2 endi - if $data[0][2] != 0 then - return -1 - endi -endi -if $data[1][2] != $totalMsgOfNtb then - if $data[0][2] != $totalMsgOfNtb then - return -1 - endi - if $data[1][2] != 0 then - return -1 +elif $data[0][2] == 0 then + if $data[1][2] == $totalMsgOfNtb then + goto check_ok_2 endi endi +return -1 +check_ok_2: -if $data[0][3] != $totalMsgOfNtb then - if $data[1][3] != $totalMsgOfNtb then - return -1 +if $data[0][3] == $totalMsgOfNtb then + if $data[1][3] == 0 then + goto check_ok_3 endi - if $data[0][3] != 0 then - return -1 - endi -endi -if $data[1][3] != $totalMsgOfNtb then - if $data[0][3] != $totalMsgOfNtb then - return -1 +elif $data[0][3] == 0 then + if $data[1][3] == $totalMsgOfNtb then + goto check_ok_3 endi - if $data[1][3] != 0 then - return -1 - endi endi +return -1 +check_ok_3: #------ not need stop consumer, because it exit after pull msg overthan expect msg #system tsim/tmq/consume.sh -s stop -x SIGINT From 645d4829cfd758a9a3b8db313420e50cf97030f7 Mon Sep 17 00:00:00 2001 From: cpwu Date: Sun, 24 Apr 2022 18:03:41 +0800 Subject: [PATCH 51/51] fix case --- tests/system-test/2-query/sum.py | 232 +++++++++++++++++++++++++++++++ 1 file changed, 232 insertions(+) create mode 100644 tests/system-test/2-query/sum.py diff --git a/tests/system-test/2-query/sum.py b/tests/system-test/2-query/sum.py new file mode 100644 index 0000000000..65f5dfd653 --- /dev/null +++ b/tests/system-test/2-query/sum.py @@ -0,0 +1,232 @@ +from util.log import * +from util.sql import * +from util.cases import * +from util.dnodes import * + + +INT_COL = "c1" +BINT_COL = "c2" +SINT_COL = "c3" +TINT_COL = "c4" +FLOAT_COL = "c5" +DOUBLE_COL = "c6" +BOOL_COL = "c7" + +BINARY_COL = "c8" +NCHAR_COL = "c9" +TS_COL = "c10" + +NUM_COL = [INT_COL, BINT_COL, SINT_COL, TINT_COL, FLOAT_COL, DOUBLE_COL, ] +UN_NUM_COL = [BOOL_COL, BINARY_COL, NCHAR_COL, ] +TS_TYPE_COL = [TS_COL] + +class TDTestCase: + + def init(self, conn, logSql): + tdLog.debug(f"start to excute {__file__}") + tdSql.init(conn.cursor()) + + def __sum_condition(self): + sum_condition = [] + for num_col in NUM_COL: + sum_condition.extend( + ( + num_col, + f"ceil( {num_col} )", + ) + ) + sum_condition.extend( f"{num_col} + {num_col_2}" for num_col_2 in NUM_COL ) + sum_condition.extend( f"{num_col} + {un_num_col} " for un_num_col in UN_NUM_COL ) + sum_condition.append(1) + + return sum_condition + + def __where_condition(self, col): + return f" where abs( {col} ) < 1000000 " + + def __group_condition(self, col, having = ""): + return f" group by {col} having {having}" if having else f" group by {col} " + + def __sum_current_check(self, tbname): + sum_condition = self.__sum_condition() + for condition in sum_condition: + where_condition = self.__where_condition(condition) + group_condition = self.__group_condition(condition, having=f"{condition} is not null " ) + + tdSql.query(f"select {condition} from {tbname} {where_condition} ") + datas = [tdSql.getData(i,0) for i in range(tdSql.queryRows)] + sum_data = sum(filter(None, datas)) + tdSql.query(f"select sum( {condition} ) from {tbname} {where_condition} ") + tdSql.checkData(0, 0, sum_data) + + tdSql.query(f"select {condition} from {tbname} {where_condition} {group_condition} ") + + def __sum_err_check(self,tbanme): + sqls = [] + + for un_num_col in UN_NUM_COL: + sqls.extend( + ( + f"select sum( {un_num_col} ) from {tbanme} ", + f"select sum(ceil( {un_num_col} )) from {tbanme} ", + ) + ) + sqls.extend( f"select sum( {un_num_col} + {un_num_col_2} ) from {tbanme} " for un_num_col_2 in UN_NUM_COL ) + sqls.extend( f"select sum( {un_num_col} + {ts_col} ) from {tbanme} " for ts_col in TS_TYPE_COL ) + + sqls.extend( f"select sum( {num_col} + {ts_col} ) from {tbanme} " for num_col in NUM_COL for ts_col in TS_TYPE_COL) + sqls.extend( + ( + f"select sum() from {tbanme} ", + f"select sum(*) from {tbanme} ", + f"select sum(ccccccc) from {tbanme} ", + f"select sum('test') from {tbanme} ", + ) + ) + + return sqls + + def __test_current(self): + tdLog.printNoPrefix("==========current sql condition check , must return query ok==========") + tbname = ["ct1", "ct2", "ct4", "t1"] + for tb in tbname: + self.__sum_current_check(tb) + tdLog.printNoPrefix(f"==========current sql condition check in {tb} over==========") + + def __test_error(self): + tdLog.printNoPrefix("==========err sql condition check , must return error==========") + tbname = ["ct1", "ct2", "ct4", "t1"] + + for tb in tbname: + for errsql in self.__sum_err_check(tb): + tdSql.error(sql=errsql) + tdLog.printNoPrefix(f"==========err sql condition check in {tb} over==========") + + + def all_test(self): + self.__test_current() + self.__test_error() + + + def __create_tb(self): + tdSql.prepare() + + tdLog.printNoPrefix("==========step1:create table") + create_stb_sql = f'''create table stb1( + ts timestamp, {INT_COL} int, {BINT_COL} bigint, {SINT_COL} smallint, {TINT_COL} tinyint, + {FLOAT_COL} float, {DOUBLE_COL} double, {BOOL_COL} bool, + {BINARY_COL} binary(16), {NCHAR_COL} nchar(32), {TS_COL} timestamp + ) tags (t1 int) + ''' + create_ntb_sql = f'''create table t1( + ts timestamp, {INT_COL} int, {BINT_COL} bigint, {SINT_COL} smallint, {TINT_COL} tinyint, + {FLOAT_COL} float, {DOUBLE_COL} double, {BOOL_COL} bool, + {BINARY_COL} binary(16), {NCHAR_COL} nchar(32), {TS_COL} timestamp + ) + ''' + tdSql.execute(create_stb_sql) + tdSql.execute(create_ntb_sql) + + for i in range(4): + tdSql.execute(f'create table ct{i+1} using stb1 tags ( {i+1} )') + + def __insert_data(self, rows): + for i in range(9): + tdSql.execute( + f"insert into ct1 values ( now()-{i*10}s, {1*i}, {11111*i}, {111*i}, {11*i}, {1.11*i}, {11.11*i}, {i%2}, 'binary{i}', 'nchar{i}', now()+{1*i}a )" + ) + tdSql.execute( + f"insert into ct4 values ( now()-{i*90}d, {1*i}, {11111*i}, {111*i}, {11*i}, {1.11*i}, {11.11*i}, {i%2}, 'binary{i}', 'nchar{i}', now()+{1*i}a )" + ) + tdSql.execute( + f"insert into ct2 values ( now()-{i*90}d, {-1*i}, {-11111*i}, {-111*i}, {-11*i}, {-1.11*i}, {-11.11*i}, {i%2}, 'binary{i}', 'nchar{i}', now()+{1*i}a )" + ) + tdSql.execute( + '''insert into ct1 values + ( now()-45s, 0, 0, 0, 0, 0, 0, 0, 'binary0', 'nchar0', now()+8a ) + ( now()+10s, 9, -99999, -999, -99, -9.99, -99.99, 1, 'binary9', 'nchar9', now()+9a ) + ''' + ) + + tdSql.execute( + f'''insert into ct4 values + ( now()-810d, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) + ( now()-400d, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) + ( now()+{rows * 9}d, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) + ( + now()+{rows * 9-10}d, {pow(2,31)-pow(2,15)}, {pow(2,63)-pow(2,30)}, 32767, 127, + { 3.3 * pow(10,38) }, { 1.3 * pow(10,308) }, { rows % 2 }, "binary_limit-1", "nachar_limit-1", now()-1d + ) + ( + now()+{rows * 9-20}d, {pow(2,31)-pow(2,16)}, {pow(2,63)-pow(2,31)}, 32766, 126, + { 3.2 * pow(10,38) }, { 1.2 * pow(10,308) }, { (rows-1) % 2 }, "binary_limit-2", "nachar_limit-2", now()-2d + ) + ''' + ) + + tdSql.execute( + f'''insert into ct2 values + ( now()-810d, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) + ( now()-400d, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) + ( now()+{rows * 9}d, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) + ( + now()+{rows * 9-10}d, { -1 * pow(2,31) + pow(2,15) }, { -1 * pow(2,63) + pow(2,30) }, -32766, -126, + { -1 * 3.2 * pow(10,38) }, { -1.2 * pow(10,308) }, { rows % 2 }, "binary_limit-1", "nachar_limit-1", now()-1d + ) + ( + now()+{rows * 9-20}d, { -1 * pow(2,31) + pow(2,16) }, { -1 * pow(2,63) + pow(2,31) }, -32767, -127, + { - 3.3 * pow(10,38) }, { -1.3 * pow(10,308) }, { (rows-1) % 2 }, "binary_limit-2", "nachar_limit-2", now()-2d + ) + ''' + ) + + for i in range(rows): + insert_data = f'''insert into t1 values + ( now()-{i}h, {i}, {i}, { i % 32767 }, { i % 127}, { i * 1.11111 }, { i * 1000.1111 }, { i % 2}, + "binary_{i}", "nchar_{i}", now()-{i}s ) + ''' + tdSql.execute(insert_data) + tdSql.execute( + f'''insert into t1 values + ( now() + 3h, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) + ( now()-{ ( rows // 2 ) * 60 + 30 }m, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) + ( now()-{rows}h, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL ) + ( now() + 2h, { pow(2,31) - pow(2,15) }, { pow(2,63) - pow(2,30) }, 32767, 127, + { 3.3 * pow(10,38) }, { 1.3 * pow(10,308) }, { rows % 2 }, + "binary_limit-1", "nachar_limit-1", now()-1d + ) + ( + now() + 1h , { pow(2,31) - pow(2,16) }, { pow(2,63) - pow(2,31) }, 32766, 126, + { 3.2 * pow(10,38) }, { 1.2 * pow(10,308) }, { (rows-1) % 2 }, + "binary_limit-2", "nachar_limit-2", now()-2d + ) + ''' + ) + + + def run(self): + tdSql.prepare() + + tdLog.printNoPrefix("==========step1:create table") + self.__create_tb() + + tdLog.printNoPrefix("==========step2:insert data") + self.__insert_data(100) + + tdLog.printNoPrefix("==========step3:all check") + self.all_test() + + # tdDnodes.stop(1) + # tdDnodes.start(1) + + # tdSql.execute("use db") + + # tdLog.printNoPrefix("==========step4:after wal, all check again ") + # self.all_test() + + def stop(self): + tdSql.close() + tdLog.success(f"{__file__} successfully executed") + +tdCases.addLinux(__file__, TDTestCase()) +tdCases.addWindows(__file__, TDTestCase())