TDengine/source/libs/sync/inc/syncInt.h

266 lines
10 KiB
C
Raw Normal View History

2022-02-22 03:28:15 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* 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 <http://www.gnu.org/licenses/>.
*/
#ifndef _TD_LIBS_SYNC_INT_H
#define _TD_LIBS_SYNC_INT_H
#ifdef __cplusplus
extern "C" {
#endif
2022-02-26 18:24:50 +00:00
#include "sync.h"
2022-04-18 13:50:56 +00:00
#include "syncTools.h"
2022-02-26 18:24:50 +00:00
#include "tlog.h"
2022-02-28 06:10:34 +00:00
#include "ttimer.h"
2022-02-26 18:24:50 +00:00
2022-05-20 02:23:48 +00:00
// clang-format off
#define sFatal(...) do { if (sDebugFlag & DEBUG_FATAL) { taosPrintLog("SYN FATAL ", DEBUG_FATAL, 255, __VA_ARGS__); }} while(0)
#define sError(...) do { if (sDebugFlag & DEBUG_ERROR) { taosPrintLog("SYN ERROR ", DEBUG_ERROR, 255, __VA_ARGS__); }} while(0)
#define sWarn(...) do { if (sDebugFlag & DEBUG_WARN) { taosPrintLog("SYN WARN ", DEBUG_WARN, 255, __VA_ARGS__); }} while(0)
#define sInfo(...) do { if (sDebugFlag & DEBUG_INFO) { taosPrintLog("SYN ", DEBUG_INFO, 255, __VA_ARGS__); }} while(0)
#define sDebug(...) do { if (sDebugFlag & DEBUG_DEBUG) { taosPrintLog("SYN ", DEBUG_DEBUG, sDebugFlag, __VA_ARGS__); }} while(0)
#define sTrace(...) do { if (sDebugFlag & DEBUG_TRACE) { taosPrintLog("SYN ", DEBUG_TRACE, sDebugFlag, __VA_ARGS__); }} while(0)
#define sFatalLong(...) do { if (sDebugFlag & DEBUG_FATAL) { taosPrintLongString("SYN FATAL ", DEBUG_FATAL, 255, __VA_ARGS__); }} while(0)
#define sErrorLong(...) do { if (sDebugFlag & DEBUG_ERROR) { taosPrintLongString("SYN ERROR ", DEBUG_ERROR, 255, __VA_ARGS__); }} while(0)
#define sWarnLong(...) do { if (sDebugFlag & DEBUG_WARN) { taosPrintLongString("SYN WARN ", DEBUG_WARN, 255, __VA_ARGS__); }} while(0)
#define sInfoLong(...) do { if (sDebugFlag & DEBUG_INFO) { taosPrintLongString("SYN ", DEBUG_INFO, 255, __VA_ARGS__); }} while(0)
#define sDebugLong(...) do { if (sDebugFlag & DEBUG_DEBUG) { taosPrintLongString("SYN ", DEBUG_DEBUG, sDebugFlag, __VA_ARGS__); }} while(0)
#define sTraceLong(...) do { if (sDebugFlag & DEBUG_TRACE) { taosPrintLongString("SYN ", DEBUG_TRACE, sDebugFlag, __VA_ARGS__); }} while(0)
// clang-format on
typedef struct SyncTimeout SyncTimeout;
typedef struct SyncClientRequest SyncClientRequest;
typedef struct SyncPing SyncPing;
typedef struct SyncPingReply SyncPingReply;
typedef struct SyncRequestVote SyncRequestVote;
typedef struct SyncRequestVoteReply SyncRequestVoteReply;
typedef struct SyncAppendEntries SyncAppendEntries;
2022-02-26 18:24:50 +00:00
typedef struct SyncAppendEntriesReply SyncAppendEntriesReply;
2022-05-20 02:23:48 +00:00
typedef struct SSyncEnv SSyncEnv;
typedef struct SRaftStore SRaftStore;
typedef struct SVotesGranted SVotesGranted;
typedef struct SVotesRespond SVotesRespond;
typedef struct SSyncIndexMgr SSyncIndexMgr;
typedef struct SRaftCfg SRaftCfg;
typedef struct SSyncRespMgr SSyncRespMgr;
2022-05-27 07:35:26 +00:00
typedef struct SSyncSnapshotSender SSyncSnapshotSender;
typedef struct SSyncSnapshotReceiver SSyncSnapshotReceiver;
2022-02-28 06:10:34 +00:00
extern bool gRaftDetailLog;
2022-02-22 03:28:15 +00:00
typedef struct SSyncNode {
2022-03-03 09:28:00 +00:00
// init by SSyncInfo
2022-02-26 16:02:18 +00:00
SyncGroupId vgId;
2022-04-18 13:50:56 +00:00
SRaftCfg* pRaftCfg;
2022-02-26 16:02:18 +00:00
char path[TSDB_FILENAME_LEN];
2022-03-09 10:33:41 +00:00
char raftStorePath[TSDB_FILENAME_LEN * 2];
2022-04-18 13:50:56 +00:00
char configPath[TSDB_FILENAME_LEN * 2];
2022-03-14 08:27:25 +00:00
// sync io
2022-05-19 11:44:01 +00:00
SWal* pWal;
const SMsgCb* msgcb;
int32_t (*FpSendMsg)(const SEpSet* pEpSet, SRpcMsg* pMsg);
int32_t (*FpEqMsg)(const SMsgCb* msgcb, SRpcMsg* pMsg);
2022-03-01 12:29:49 +00:00
2022-03-03 09:28:00 +00:00
// init internal
2022-03-08 05:55:13 +00:00
SNodeInfo myNodeInfo;
SRaftId myRaftId;
2022-03-05 07:03:49 +00:00
2022-02-28 08:36:57 +00:00
int32_t peersNum;
2022-03-08 05:55:13 +00:00
SNodeInfo peersNodeInfo[TSDB_MAX_REPLICA];
2022-03-05 07:03:49 +00:00
SRaftId peersId[TSDB_MAX_REPLICA];
int32_t replicaNum;
SRaftId replicasId[TSDB_MAX_REPLICA];
2022-02-28 08:36:57 +00:00
2022-03-03 09:28:00 +00:00
// raft algorithm
SSyncFSM* pFsm;
int32_t quorum;
2022-03-06 04:25:12 +00:00
SRaftId leaderCache;
2022-03-03 09:28:00 +00:00
// life cycle
int64_t rid;
// tla+ server vars
ESyncState state;
SRaftStore* pRaftStore;
// tla+ candidate vars
2022-03-08 02:52:18 +00:00
SVotesGranted* pVotesGranted;
SVotesRespond* pVotesRespond;
2022-02-28 06:10:34 +00:00
2022-03-03 09:28:00 +00:00
// tla+ leader vars
2022-03-09 03:27:22 +00:00
SSyncIndexMgr* pNextIndex;
SSyncIndexMgr* pMatchIndex;
2022-03-03 09:28:00 +00:00
// tla+ log vars
SSyncLogStore* pLogStore;
SyncIndex commitIndex;
2022-03-22 02:42:02 +00:00
// timer ms init
int32_t pingBaseLine;
int32_t electBaseLine;
int32_t hbBaseLine;
2022-03-08 06:19:50 +00:00
// ping timer
2022-03-07 06:18:46 +00:00
tmr_h pPingTimer;
int32_t pingTimerMS;
2022-03-06 09:59:24 +00:00
uint64_t pingTimerLogicClock;
uint64_t pingTimerLogicClockUser;
2022-03-14 08:27:25 +00:00
TAOS_TMR_CALLBACK FpPingTimerCB; // Timer Fp
2022-02-28 08:36:57 +00:00
uint64_t pingTimerCounter;
2022-03-08 06:19:50 +00:00
// elect timer
2022-02-28 08:36:57 +00:00
tmr_h pElectTimer;
int32_t electTimerMS;
2022-03-07 06:18:46 +00:00
uint64_t electTimerLogicClock;
uint64_t electTimerLogicClockUser;
2022-03-14 08:27:25 +00:00
TAOS_TMR_CALLBACK FpElectTimerCB; // Timer Fp
2022-02-28 08:36:57 +00:00
uint64_t electTimerCounter;
2022-03-08 06:19:50 +00:00
// heartbeat timer
2022-02-28 08:36:57 +00:00
tmr_h pHeartbeatTimer;
int32_t heartbeatTimerMS;
2022-03-07 06:18:46 +00:00
uint64_t heartbeatTimerLogicClock;
uint64_t heartbeatTimerLogicClockUser;
2022-03-14 08:27:25 +00:00
TAOS_TMR_CALLBACK FpHeartbeatTimerCB; // Timer Fp
2022-02-28 08:36:57 +00:00
uint64_t heartbeatTimerCounter;
// callback
2022-05-30 05:14:48 +00:00
FpOnPingCb FpOnPing;
FpOnPingReplyCb FpOnPingReply;
FpOnClientRequestCb FpOnClientRequest;
FpOnTimeoutCb FpOnTimeout;
FpOnRequestVoteCb FpOnRequestVote;
FpOnRequestVoteReplyCb FpOnRequestVoteReply;
FpOnAppendEntriesCb FpOnAppendEntries;
FpOnAppendEntriesReplyCb FpOnAppendEntriesReply;
2022-06-01 07:03:01 +00:00
FpOnSnapshotSendCb FpOnSnapshotSend;
FpOnSnapshotRspCb FpOnSnapshotRsp;
2022-02-26 16:02:18 +00:00
2022-04-18 13:50:56 +00:00
// tools
SSyncRespMgr* pSyncRespMgr;
// restore state
bool restoreFinish;
// SSnapshot* pSnapshot;
2022-05-30 09:19:31 +00:00
SSyncSnapshotSender* senders[TSDB_MAX_REPLICA];
2022-06-01 13:23:39 +00:00
SSyncSnapshotReceiver* pNewNodeReceiver;
2022-06-20 09:48:56 +00:00
// is config changing
bool changing;
2022-02-22 03:28:15 +00:00
} SSyncNode;
2022-03-14 08:27:25 +00:00
// open/close --------------
2022-02-26 16:02:18 +00:00
SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo);
2022-04-18 13:50:56 +00:00
void syncNodeStart(SSyncNode* pSyncNode);
2022-05-13 04:12:37 +00:00
void syncNodeStartStandBy(SSyncNode* pSyncNode);
2022-03-03 09:28:00 +00:00
void syncNodeClose(SSyncNode* pSyncNode);
2022-06-25 12:31:42 +00:00
int32_t syncNodePropose(SSyncNode* pSyncNode, SRpcMsg* pMsg, bool isWeak);
2022-06-30 08:44:49 +00:00
int32_t syncNodeProposeBatch(SSyncNode* pSyncNode, SRpcMsg* pMsgArr, bool* pIsWeakArr, int32_t arrSize);
2022-03-07 08:06:07 +00:00
2022-05-30 05:14:48 +00:00
// option
2022-07-04 07:57:28 +00:00
bool syncNodeSnapshotEnable(SSyncNode* pSyncNode);
ESyncStrategy syncNodeStrategy(SSyncNode* pSyncNode);
SyncIndex syncNodeGetSnapshotConfigIndex(SSyncNode* pSyncNode, SyncIndex snapshotLastApplyIndex);
2022-05-30 05:14:48 +00:00
2022-03-14 08:27:25 +00:00
// ping --------------
2022-03-07 08:06:07 +00:00
int32_t syncNodePing(SSyncNode* pSyncNode, const SRaftId* destRaftId, SyncPing* pMsg);
2022-03-07 08:46:17 +00:00
int32_t syncNodePingSelf(SSyncNode* pSyncNode);
2022-03-14 08:27:25 +00:00
int32_t syncNodePingPeers(SSyncNode* pSyncNode);
int32_t syncNodePingAll(SSyncNode* pSyncNode);
2022-03-04 07:48:09 +00:00
2022-03-14 08:27:25 +00:00
// timer control --------------
2022-03-04 07:48:09 +00:00
int32_t syncNodeStartPingTimer(SSyncNode* pSyncNode);
int32_t syncNodeStopPingTimer(SSyncNode* pSyncNode);
2022-03-07 06:18:46 +00:00
int32_t syncNodeStartElectTimer(SSyncNode* pSyncNode, int32_t ms);
2022-03-04 07:48:09 +00:00
int32_t syncNodeStopElectTimer(SSyncNode* pSyncNode);
2022-03-07 06:18:46 +00:00
int32_t syncNodeRestartElectTimer(SSyncNode* pSyncNode, int32_t ms);
2022-03-14 10:44:53 +00:00
int32_t syncNodeResetElectTimer(SSyncNode* pSyncNode);
2022-03-04 07:48:09 +00:00
int32_t syncNodeStartHeartbeatTimer(SSyncNode* pSyncNode);
int32_t syncNodeStopHeartbeatTimer(SSyncNode* pSyncNode);
2022-03-14 08:27:25 +00:00
// utils --------------
2022-04-18 13:50:56 +00:00
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);
2022-06-18 07:17:58 +00:00
void syncNodeEventLog(const SSyncNode* pSyncNode, char* str);
2022-06-21 08:02:36 +00:00
void syncNodeErrorLog(const SSyncNode* pSyncNode, char* str);
2022-04-18 13:50:56 +00:00
char* syncNode2SimpleStr(const SSyncNode* pSyncNode);
bool syncNodeInConfig(SSyncNode* pSyncNode, const SSyncCfg* config);
2022-06-20 12:07:36 +00:00
void syncNodeDoConfigChange(SSyncNode* pSyncNode, SSyncCfg* newConfig, SyncIndex lastConfigChangeIndex);
2022-04-18 13:50:56 +00:00
2022-03-21 11:26:48 +00:00
SSyncNode* syncNodeAcquire(int64_t rid);
void syncNodeRelease(SSyncNode* pNode);
2022-03-10 11:21:02 +00:00
2022-03-14 10:44:53 +00:00
// raft state change --------------
void syncNodeUpdateTerm(SSyncNode* pSyncNode, SyncTerm term);
2022-06-10 07:19:11 +00:00
void syncNodeBecomeFollower(SSyncNode* pSyncNode, const char* debugStr);
void syncNodeBecomeLeader(SSyncNode* pSyncNode, const char* debugStr);
2022-03-14 10:44:53 +00:00
void syncNodeCandidate2Leader(SSyncNode* pSyncNode);
void syncNodeFollower2Candidate(SSyncNode* pSyncNode);
void syncNodeLeader2Follower(SSyncNode* pSyncNode);
void syncNodeCandidate2Follower(SSyncNode* pSyncNode);
// raft vote --------------
void syncNodeVoteForTerm(SSyncNode* pSyncNode, SyncTerm term, SRaftId* pRaftId);
void syncNodeVoteForSelf(SSyncNode* pSyncNode);
2022-05-30 09:31:55 +00:00
// snapshot --------------
2022-06-06 03:24:25 +00:00
bool syncNodeHasSnapshot(SSyncNode* pSyncNode);
2022-06-05 11:47:54 +00:00
SyncIndex syncNodeGetLastIndex(SSyncNode* pSyncNode);
SyncTerm syncNodeGetLastTerm(SSyncNode* pSyncNode);
int32_t syncNodeGetLastIndexTerm(SSyncNode* pSyncNode, SyncIndex* pLastIndex, SyncTerm* pLastTerm);
2022-06-06 03:24:25 +00:00
SyncIndex syncNodeSyncStartIndex(SSyncNode* pSyncNode);
2022-06-05 11:47:54 +00:00
SyncIndex syncNodeGetPreIndex(SSyncNode* pSyncNode, SyncIndex index);
2022-06-06 03:24:25 +00:00
SyncTerm syncNodeGetPreTerm(SSyncNode* pSyncNode, SyncIndex index);
2022-06-05 11:47:54 +00:00
int32_t syncNodeGetPreIndexTerm(SSyncNode* pSyncNode, SyncIndex index, SyncIndex* pPreIndex, SyncTerm* pPreTerm);
2022-05-30 09:31:55 +00:00
2022-06-25 12:31:42 +00:00
bool syncNodeIsOptimizedOneReplica(SSyncNode* ths, SRpcMsg* pMsg);
2022-06-06 08:02:25 +00:00
int32_t syncNodeCommit(SSyncNode* ths, SyncIndex beginIndex, SyncIndex endIndex, uint64_t flag);
int32_t syncNodeUpdateNewConfigIndex(SSyncNode* ths, SSyncCfg* pNewCfg);
2022-06-08 03:03:28 +00:00
bool syncNodeInRaftGroup(SSyncNode* ths, SRaftId* pRaftId);
SSyncSnapshotSender* syncNodeGetSnapshotSender(SSyncNode* ths, SRaftId* pDestId);
2022-06-20 09:48:56 +00:00
int32_t syncGetSnapshotMeta(int64_t rid, struct SSnapshotMeta* sMeta);
int32_t syncGetSnapshotMetaByIndex(int64_t rid, SyncIndex snapshotIndex, struct SSnapshotMeta* sMeta);
2022-06-12 07:21:56 +00:00
void syncStartNormal(int64_t rid);
void syncStartStandBy(int64_t rid);
2022-06-20 09:48:56 +00:00
bool syncNodeCanChange(SSyncNode* pSyncNode);
bool syncNodeCheckNewConfig(SSyncNode* pSyncNode, const SSyncCfg* pNewCfg);
2022-03-10 11:21:02 +00:00
// for debug --------------
void syncNodePrint(SSyncNode* pObj);
void syncNodePrint2(char* s, SSyncNode* pObj);
void syncNodeLog(SSyncNode* pObj);
void syncNodeLog2(char* s, SSyncNode* pObj);
2022-06-21 07:09:23 +00:00
void syncNodeLog3(char* s, SSyncNode* pObj);
2022-03-09 10:33:41 +00:00
2022-02-22 03:28:15 +00:00
#ifdef __cplusplus
}
#endif
#endif /*_TD_LIBS_SYNC_INT_H*/