syncInt.h 9.1 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/*
 * 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

M
Minghao Li 已提交
23
#include "sync.h"
M
Minghao Li 已提交
24
#include "syncTools.h"
M
Minghao Li 已提交
25
#include "tlog.h"
M
Minghao Li 已提交
26
#include "ttimer.h"
M
Minghao Li 已提交
27

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
// 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;
M
Minghao Li 已提交
50
typedef struct SyncAppendEntriesReply SyncAppendEntriesReply;
51 52 53 54 55 56 57
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;
M
Minghao Li 已提交
58 59
typedef struct SSyncSnapshotSender    SSyncSnapshotSender;
typedef struct SSyncSnapshotReceiver  SSyncSnapshotReceiver;
M
Minghao Li 已提交
60

M
Minghao Li 已提交
61
typedef struct SSyncNode {
M
syncInt  
Minghao Li 已提交
62
  // init by SSyncInfo
M
Minghao Li 已提交
63
  SyncGroupId vgId;
M
Minghao Li 已提交
64
  SRaftCfg*   pRaftCfg;
M
Minghao Li 已提交
65
  char        path[TSDB_FILENAME_LEN];
M
Minghao Li 已提交
66
  char        raftStorePath[TSDB_FILENAME_LEN * 2];
M
Minghao Li 已提交
67
  char        configPath[TSDB_FILENAME_LEN * 2];
M
Minghao Li 已提交
68 69

  // sync io
S
Shengliang Guan 已提交
70 71 72 73
  SWal*         pWal;
  const SMsgCb* msgcb;
  int32_t (*FpSendMsg)(const SEpSet* pEpSet, SRpcMsg* pMsg);
  int32_t (*FpEqMsg)(const SMsgCb* msgcb, SRpcMsg* pMsg);
M
Minghao Li 已提交
74

M
syncInt  
Minghao Li 已提交
75
  // init internal
M
Minghao Li 已提交
76 77
  SNodeInfo myNodeInfo;
  SRaftId   myRaftId;
M
Minghao Li 已提交
78

M
Minghao Li 已提交
79
  int32_t   peersNum;
M
Minghao Li 已提交
80
  SNodeInfo peersNodeInfo[TSDB_MAX_REPLICA];
M
Minghao Li 已提交
81 82 83 84
  SRaftId   peersId[TSDB_MAX_REPLICA];

  int32_t replicaNum;
  SRaftId replicasId[TSDB_MAX_REPLICA];
M
Minghao Li 已提交
85

M
syncInt  
Minghao Li 已提交
86 87 88
  // raft algorithm
  SSyncFSM* pFsm;
  int32_t   quorum;
M
Minghao Li 已提交
89
  SRaftId   leaderCache;
M
syncInt  
Minghao Li 已提交
90 91 92 93 94 95 96 97 98

  // life cycle
  int64_t rid;

  // tla+ server vars
  ESyncState  state;
  SRaftStore* pRaftStore;

  // tla+ candidate vars
M
Minghao Li 已提交
99 100
  SVotesGranted* pVotesGranted;
  SVotesRespond* pVotesRespond;
M
Minghao Li 已提交
101

M
syncInt  
Minghao Li 已提交
102
  // tla+ leader vars
M
Minghao Li 已提交
103 104
  SSyncIndexMgr* pNextIndex;
  SSyncIndexMgr* pMatchIndex;
M
syncInt  
Minghao Li 已提交
105 106 107 108 109

  // tla+ log vars
  SSyncLogStore* pLogStore;
  SyncIndex      commitIndex;

M
Minghao Li 已提交
110 111 112 113 114
  // timer ms init
  int32_t pingBaseLine;
  int32_t electBaseLine;
  int32_t hbBaseLine;

M
Minghao Li 已提交
115
  // ping timer
M
Minghao Li 已提交
116 117
  tmr_h             pPingTimer;
  int32_t           pingTimerMS;
M
Minghao Li 已提交
118 119
  uint64_t          pingTimerLogicClock;
  uint64_t          pingTimerLogicClockUser;
M
Minghao Li 已提交
120
  TAOS_TMR_CALLBACK FpPingTimerCB;  // Timer Fp
M
Minghao Li 已提交
121 122
  uint64_t          pingTimerCounter;

M
Minghao Li 已提交
123
  // elect timer
M
Minghao Li 已提交
124 125
  tmr_h             pElectTimer;
  int32_t           electTimerMS;
M
Minghao Li 已提交
126 127
  uint64_t          electTimerLogicClock;
  uint64_t          electTimerLogicClockUser;
M
Minghao Li 已提交
128
  TAOS_TMR_CALLBACK FpElectTimerCB;  // Timer Fp
M
Minghao Li 已提交
129 130
  uint64_t          electTimerCounter;

M
Minghao Li 已提交
131
  // heartbeat timer
M
Minghao Li 已提交
132 133
  tmr_h             pHeartbeatTimer;
  int32_t           heartbeatTimerMS;
M
Minghao Li 已提交
134 135
  uint64_t          heartbeatTimerLogicClock;
  uint64_t          heartbeatTimerLogicClockUser;
M
Minghao Li 已提交
136
  TAOS_TMR_CALLBACK FpHeartbeatTimerCB;  // Timer Fp
M
Minghao Li 已提交
137 138 139
  uint64_t          heartbeatTimerCounter;

  // callback
M
Minghao Li 已提交
140 141 142 143 144 145 146 147 148 149
  FpOnPingCb               FpOnPing;
  FpOnPingReplyCb          FpOnPingReply;
  FpOnClientRequestCb      FpOnClientRequest;
  FpOnTimeoutCb            FpOnTimeout;
  FpOnRequestVoteCb        FpOnRequestVote;
  FpOnRequestVoteReplyCb   FpOnRequestVoteReply;
  FpOnAppendEntriesCb      FpOnAppendEntries;
  FpOnAppendEntriesReplyCb FpOnAppendEntriesReply;

  /*
M
Minghao Li 已提交
150 151
  int32_t (*FpOnPing)(SSyncNode* ths, SyncPing* pMsg);
  int32_t (*FpOnPingReply)(SSyncNode* ths, SyncPingReply* pMsg);
M
Minghao Li 已提交
152
  int32_t (*FpOnClientRequest)(SSyncNode* ths, SyncClientRequest* pMsg);
M
Minghao Li 已提交
153
  int32_t (*FpOnTimeout)(SSyncNode* pSyncNode, SyncTimeout* pMsg);
M
Minghao Li 已提交
154 155 156 157
  int32_t (*FpOnRequestVote)(SSyncNode* ths, SyncRequestVote* pMsg);
  int32_t (*FpOnRequestVoteReply)(SSyncNode* ths, SyncRequestVoteReply* pMsg);
  int32_t (*FpOnAppendEntries)(SSyncNode* ths, SyncAppendEntries* pMsg);
  int32_t (*FpOnAppendEntriesReply)(SSyncNode* ths, SyncAppendEntriesReply* pMsg);
M
Minghao Li 已提交
158
  */
M
Minghao Li 已提交
159

M
Minghao Li 已提交
160 161 162
  // tools
  SSyncRespMgr* pSyncRespMgr;

163
  // restore state
164
  // sem_t      restoreSem;
165 166
  bool restoreFinish;
  // SSnapshot*             pSnapshot;
M
Minghao Li 已提交
167 168
  SSyncSnapshotSender*   senders[TSDB_MAX_REPLICA];
  SSyncSnapshotReceiver* receivers[TSDB_MAX_REPLICA];
169

M
Minghao Li 已提交
170 171
} SSyncNode;

M
Minghao Li 已提交
172
// open/close --------------
M
Minghao Li 已提交
173
SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo);
M
Minghao Li 已提交
174
void       syncNodeStart(SSyncNode* pSyncNode);
M
Minghao Li 已提交
175
void       syncNodeStartStandBy(SSyncNode* pSyncNode);
M
syncInt  
Minghao Li 已提交
176
void       syncNodeClose(SSyncNode* pSyncNode);
M
Minghao Li 已提交
177

M
Minghao Li 已提交
178 179 180
// option
bool syncNodeSnapshotEnable(SSyncNode* pSyncNode);

M
Minghao Li 已提交
181
// ping --------------
M
Minghao Li 已提交
182
int32_t syncNodePing(SSyncNode* pSyncNode, const SRaftId* destRaftId, SyncPing* pMsg);
M
Minghao Li 已提交
183
int32_t syncNodePingSelf(SSyncNode* pSyncNode);
M
Minghao Li 已提交
184 185
int32_t syncNodePingPeers(SSyncNode* pSyncNode);
int32_t syncNodePingAll(SSyncNode* pSyncNode);
M
Minghao Li 已提交
186

M
Minghao Li 已提交
187
// timer control --------------
M
Minghao Li 已提交
188 189
int32_t syncNodeStartPingTimer(SSyncNode* pSyncNode);
int32_t syncNodeStopPingTimer(SSyncNode* pSyncNode);
M
Minghao Li 已提交
190
int32_t syncNodeStartElectTimer(SSyncNode* pSyncNode, int32_t ms);
M
Minghao Li 已提交
191
int32_t syncNodeStopElectTimer(SSyncNode* pSyncNode);
M
Minghao Li 已提交
192
int32_t syncNodeRestartElectTimer(SSyncNode* pSyncNode, int32_t ms);
M
Minghao Li 已提交
193
int32_t syncNodeResetElectTimer(SSyncNode* pSyncNode);
M
Minghao Li 已提交
194 195
int32_t syncNodeStartHeartbeatTimer(SSyncNode* pSyncNode);
int32_t syncNodeStopHeartbeatTimer(SSyncNode* pSyncNode);
M
Minghao Li 已提交
196 197

// utils --------------
M
Minghao Li 已提交
198 199 200 201 202
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);
M
Minghao Li 已提交
203
void    syncNodeUpdateConfig(SSyncNode* pSyncNode, SSyncCfg* newConfig, bool* isDrop);
M
Minghao Li 已提交
204

M
Minghao Li 已提交
205 206
SSyncNode* syncNodeAcquire(int64_t rid);
void       syncNodeRelease(SSyncNode* pNode);
M
Minghao Li 已提交
207

M
Minghao Li 已提交
208 209 210 211 212 213 214 215 216 217 218 219 220 221
// raft state change --------------
void syncNodeUpdateTerm(SSyncNode* pSyncNode, SyncTerm term);
void syncNodeBecomeFollower(SSyncNode* pSyncNode);
void syncNodeBecomeLeader(SSyncNode* pSyncNode);

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);

M
Minghao Li 已提交
222
// snapshot --------------
223
bool    syncNodeIsIndexInSnapshot(SSyncNode* pSyncNode, SyncIndex index);
M
Minghao Li 已提交
224 225 226
int32_t syncNodeGetLastIndexTerm(SSyncNode* pSyncNode, SyncIndex* pLastIndex, SyncTerm* pLastTerm);
int32_t syncNodeGetPreIndexTerm(SSyncNode* pSyncNode, SyncIndex index, SyncIndex* pPreIndex, SyncTerm* pPreTerm);

M
Minghao Li 已提交
227 228 229 230 231
// for debug --------------
void syncNodePrint(SSyncNode* pObj);
void syncNodePrint2(char* s, SSyncNode* pObj);
void syncNodeLog(SSyncNode* pObj);
void syncNodeLog2(char* s, SSyncNode* pObj);
M
Minghao Li 已提交
232

M
Minghao Li 已提交
233 234 235 236 237
#ifdef __cplusplus
}
#endif

#endif /*_TD_LIBS_SYNC_INT_H*/