syncInt.h 7.0 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 23 24 25
/*
 * 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

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
M
Minghao Li 已提交
26
#include "sync.h"
M
Minghao Li 已提交
27
#include "taosdef.h"
M
syncInt  
Minghao Li 已提交
28
#include "tglobal.h"
M
Minghao Li 已提交
29
#include "tlog.h"
M
Minghao Li 已提交
30
#include "ttimer.h"
M
Minghao Li 已提交
31

32 33 34 35 36
#define sFatal(...)                                              \
  {                                                              \
    if (sDebugFlag & DEBUG_FATAL) {                              \
      taosPrintLog("SYN FATAL ", DEBUG_FATAL, 255, __VA_ARGS__); \
    }                                                            \
M
Minghao Li 已提交
37
  }
38 39 40 41 42
#define sError(...)                                              \
  {                                                              \
    if (sDebugFlag & DEBUG_ERROR) {                              \
      taosPrintLog("SYN ERROR ", DEBUG_ERROR, 255, __VA_ARGS__); \
    }                                                            \
M
Minghao Li 已提交
43
  }
44 45 46 47 48
#define sWarn(...)                                             \
  {                                                            \
    if (sDebugFlag & DEBUG_WARN) {                             \
      taosPrintLog("SYN WARN ", DEBUG_WARN, 255, __VA_ARGS__); \
    }                                                          \
M
Minghao Li 已提交
49
  }
50 51 52 53 54
#define sInfo(...)                                             \
  {                                                            \
    if (sDebugFlag & DEBUG_INFO) {                             \
      taosPrintLog("SYN INFO ", DEBUG_INFO, 255, __VA_ARGS__); \
    }                                                          \
M
Minghao Li 已提交
55
  }
56 57 58 59 60
#define sDebug(...)                                                     \
  {                                                                     \
    if (sDebugFlag & DEBUG_DEBUG) {                                     \
      taosPrintLog("SYN DEBUG ", DEBUG_DEBUG, sDebugFlag, __VA_ARGS__); \
    }                                                                   \
M
Minghao Li 已提交
61
  }
62 63 64 65 66
#define sTrace(...)                                                     \
  {                                                                     \
    if (sDebugFlag & DEBUG_TRACE) {                                     \
      taosPrintLog("SYN TRACE ", DEBUG_TRACE, sDebugFlag, __VA_ARGS__); \
    }                                                                   \
M
Minghao Li 已提交
67 68
  }

M
Minghao Li 已提交
69
struct SRaft;
M
Minghao Li 已提交
70 71
typedef struct SRaft SRaft;

M
Minghao Li 已提交
72 73 74
struct SyncTimeout;
typedef struct SyncTimeout SyncTimeout;

M
Minghao Li 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
struct SyncPing;
typedef struct SyncPing SyncPing;

struct SyncPingReply;
typedef struct SyncPingReply SyncPingReply;

struct SyncRequestVote;
typedef struct SyncRequestVote SyncRequestVote;

struct SyncRequestVoteReply;
typedef struct SyncRequestVoteReply SyncRequestVoteReply;

struct SyncAppendEntries;
typedef struct SyncAppendEntries SyncAppendEntries;

struct SyncAppendEntriesReply;
typedef struct SyncAppendEntriesReply SyncAppendEntriesReply;
M
Minghao Li 已提交
92

M
Minghao Li 已提交
93 94 95
struct SSyncEnv;
typedef struct SSyncEnv SSyncEnv;

M
syncInt  
Minghao Li 已提交
96 97 98 99 100 101
struct SRaftStore;
typedef struct SRaftStore SRaftStore;

struct SVotesGranted;
typedef struct SVotesGranted SVotesGranted;

M
Minghao Li 已提交
102 103
struct SVotesRespond;
typedef struct SVotesRespond SVotesRespond;
M
syncInt  
Minghao Li 已提交
104

M
Minghao Li 已提交
105
typedef struct SRaftId {
M
Minghao Li 已提交
106 107
  SyncNodeId  addr;  // typedef uint64_t SyncNodeId;
  SyncGroupId vgId;  // typedef int32_t  SyncGroupId;
M
Minghao Li 已提交
108 109
} SRaftId;

M
Minghao Li 已提交
110
typedef struct SSyncNode {
M
syncInt  
Minghao Li 已提交
111
  // init by SSyncInfo
M
Minghao Li 已提交
112 113 114
  SyncGroupId vgId;
  SSyncCfg    syncCfg;
  char        path[TSDB_FILENAME_LEN];
M
Minghao Li 已提交
115
  char        walPath[TSDB_FILENAME_LEN];
M
syncInt  
Minghao Li 已提交
116
  void*       rpcClient;
M
Minghao Li 已提交
117
  int32_t (*FpSendMsg)(void* rpcClient, const SEpSet* pEpSet, SRpcMsg* pMsg);
M
Minghao Li 已提交
118 119
  void* queue;
  int32_t (*FpEqMsg)(void* queue, SRpcMsg* pMsg);
M
Minghao Li 已提交
120

M
syncInt  
Minghao Li 已提交
121
  // init internal
M
Minghao Li 已提交
122
  SNodeInfo me;
M
Minghao Li 已提交
123 124
  SRaftId   raftId;

M
Minghao Li 已提交
125
  int32_t   peersNum;
M
syncInt  
Minghao Li 已提交
126
  SNodeInfo peers[TSDB_MAX_REPLICA];
M
Minghao Li 已提交
127 128 129 130
  SRaftId   peersId[TSDB_MAX_REPLICA];

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

M
syncInt  
Minghao Li 已提交
132 133 134
  // raft algorithm
  SSyncFSM* pFsm;
  int32_t   quorum;
M
Minghao Li 已提交
135
  SRaftId   leaderCache;
M
syncInt  
Minghao Li 已提交
136 137 138 139 140 141 142 143 144 145

  // life cycle
  int32_t refCount;
  int64_t rid;

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

  // tla+ candidate vars
M
Minghao Li 已提交
146 147
  SVotesGranted* pVotesGranted;
  SVotesRespond* pVotesRespond;
M
Minghao Li 已提交
148

M
syncInt  
Minghao Li 已提交
149 150 151 152 153 154 155 156 157
  // tla+ leader vars
  SHashObj* pNextIndex;
  SHashObj* pMatchIndex;

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

  // timer
M
Minghao Li 已提交
158 159
  tmr_h             pPingTimer;
  int32_t           pingTimerMS;
M
Minghao Li 已提交
160 161
  uint64_t          pingTimerLogicClock;
  uint64_t          pingTimerLogicClockUser;
M
Minghao Li 已提交
162 163 164 165 166
  TAOS_TMR_CALLBACK FpPingTimer;  // Timer Fp
  uint64_t          pingTimerCounter;

  tmr_h             pElectTimer;
  int32_t           electTimerMS;
M
Minghao Li 已提交
167 168
  uint64_t          electTimerLogicClock;
  uint64_t          electTimerLogicClockUser;
M
Minghao Li 已提交
169 170 171 172 173
  TAOS_TMR_CALLBACK FpElectTimer;  // Timer Fp
  uint64_t          electTimerCounter;

  tmr_h             pHeartbeatTimer;
  int32_t           heartbeatTimerMS;
M
Minghao Li 已提交
174 175
  uint64_t          heartbeatTimerLogicClock;
  uint64_t          heartbeatTimerLogicClockUser;
M
Minghao Li 已提交
176 177 178 179
  TAOS_TMR_CALLBACK FpHeartbeatTimer;  // Timer Fp
  uint64_t          heartbeatTimerCounter;

  // callback
M
Minghao Li 已提交
180 181 182 183 184 185
  int32_t (*FpOnPing)(SSyncNode* ths, SyncPing* pMsg);
  int32_t (*FpOnPingReply)(SSyncNode* ths, SyncPingReply* pMsg);
  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 已提交
186
  int32_t (*FpOnTimeout)(SSyncNode* pSyncNode, SyncTimeout* pMsg);
M
Minghao Li 已提交
187

M
Minghao Li 已提交
188 189
} SSyncNode;

M
Minghao Li 已提交
190
SSyncNode* syncNodeOpen(const SSyncInfo* pSyncInfo);
M
syncInt  
Minghao Li 已提交
191
void       syncNodeClose(SSyncNode* pSyncNode);
M
Minghao Li 已提交
192 193 194 195

int32_t syncNodeSendMsgById(const SRaftId* destRaftId, SSyncNode* pSyncNode, SRpcMsg* pMsg);
int32_t syncNodeSendMsgByInfo(const SNodeInfo* nodeInfo, SSyncNode* pSyncNode, SRpcMsg* pMsg);
int32_t syncNodePing(SSyncNode* pSyncNode, const SRaftId* destRaftId, SyncPing* pMsg);
M
Minghao Li 已提交
196 197 198
int32_t syncNodePingAll(SSyncNode* pSyncNode);
int32_t syncNodePingPeers(SSyncNode* pSyncNode);
int32_t syncNodePingSelf(SSyncNode* pSyncNode);
M
Minghao Li 已提交
199 200 201

int32_t syncNodeStartPingTimer(SSyncNode* pSyncNode);
int32_t syncNodeStopPingTimer(SSyncNode* pSyncNode);
M
Minghao Li 已提交
202
int32_t syncNodeStartElectTimer(SSyncNode* pSyncNode, int32_t ms);
M
Minghao Li 已提交
203
int32_t syncNodeStopElectTimer(SSyncNode* pSyncNode);
M
Minghao Li 已提交
204
int32_t syncNodeRestartElectTimer(SSyncNode* pSyncNode, int32_t ms);
M
Minghao Li 已提交
205 206
int32_t syncNodeStartHeartbeatTimer(SSyncNode* pSyncNode);
int32_t syncNodeStopHeartbeatTimer(SSyncNode* pSyncNode);
M
Minghao Li 已提交
207

M
Minghao Li 已提交
208 209 210 211 212
#ifdef __cplusplus
}
#endif

#endif /*_TD_LIBS_SYNC_INT_H*/