syncMessage.h 7.0 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

16 17
#ifndef _TD_LIBS_SYNC_MESSAGE_H
#define _TD_LIBS_SYNC_MESSAGE_H
M
Minghao Li 已提交
18 19 20 21 22

#ifdef __cplusplus
extern "C" {
#endif

23
#include "syncInt.h"
M
Minghao Li 已提交
24

M
Minghao Li 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37
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;
M
Minghao Li 已提交
38
  int64_t          timeStamp;
M
Minghao Li 已提交
39 40 41 42 43 44
  void*            data;  // need optimized
} SyncTimeout;

typedef struct SyncClientRequest {
  uint32_t bytes;
  int32_t  vgId;
45 46
  uint32_t msgType;          // TDMT_SYNC_CLIENT_REQUEST
  uint32_t originalRpcType;  // origin RpcMsg msgType
M
Minghao Li 已提交
47 48
  uint64_t seqNum;
  bool     isWeak;
49
  int16_t  reserved;
50 51
  uint32_t dataLen;  // origin RpcMsg.contLen
  char     data[];   // origin RpcMsg.pCont
M
Minghao Li 已提交
52 53 54 55 56 57 58 59
} SyncClientRequest;

typedef struct SyncClientRequestReply {
  uint32_t bytes;
  int32_t  vgId;
  uint32_t msgType;
  int32_t  errCode;
  SRaftId  leaderHint;
60
  int16_t  reserved;
M
Minghao Li 已提交
61 62 63 64 65 66 67 68 69 70 71 72
} 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;
73
  int16_t   reserved;
M
Minghao Li 已提交
74 75 76 77 78 79 80 81 82 83 84
} SyncRequestVote;

typedef struct SyncRequestVoteReply {
  uint32_t bytes;
  int32_t  vgId;
  uint32_t msgType;
  SRaftId  srcId;
  SRaftId  destId;
  // private data
  SyncTerm term;
  bool     voteGranted;
85
  int16_t  reserved;
M
Minghao Li 已提交
86 87 88 89 90 91 92 93
} SyncRequestVoteReply;

typedef struct SyncAppendEntries {
  uint32_t bytes;
  int32_t  vgId;
  uint32_t msgType;
  SRaftId  srcId;
  SRaftId  destId;
94

M
Minghao Li 已提交
95 96 97 98 99
  // private data
  SyncTerm  term;
  SyncIndex prevLogIndex;
  SyncTerm  prevLogTerm;
  SyncIndex commitIndex;
100
  SyncTerm  privateTerm;
101
  int16_t   reserved;
M
Minghao Li 已提交
102 103 104 105 106 107 108 109 110 111 112 113
  uint32_t  dataLen;
  char      data[];
} SyncAppendEntries;

typedef struct SyncAppendEntriesReply {
  uint32_t bytes;
  int32_t  vgId;
  uint32_t msgType;
  SRaftId  srcId;
  SRaftId  destId;
  // private data
  SyncTerm  term;
114
  SyncTerm  lastMatchTerm;
M
Minghao Li 已提交
115 116
  bool      success;
  SyncIndex matchIndex;
M
Minghao Li 已提交
117
  SyncIndex lastSendIndex;
118
  int64_t   startTime;
119
  int16_t   reserved;
M
Minghao Li 已提交
120 121
} SyncAppendEntriesReply;

M
Minghao Li 已提交
122 123 124 125 126 127 128 129 130 131 132
typedef struct SyncHeartbeat {
  uint32_t bytes;
  int32_t  vgId;
  uint32_t msgType;
  SRaftId  srcId;
  SRaftId  destId;

  // private data
  SyncTerm  term;
  SyncIndex commitIndex;
  SyncTerm  privateTerm;
M
Minghao Li 已提交
133
  SyncTerm  minMatchIndex;
134
  int64_t   timeStamp;
135
  int16_t   reserved;
M
Minghao Li 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148
} SyncHeartbeat;

typedef struct SyncHeartbeatReply {
  uint32_t bytes;
  int32_t  vgId;
  uint32_t msgType;
  SRaftId  srcId;
  SRaftId  destId;

  // private data
  SyncTerm term;
  SyncTerm privateTerm;
  int64_t  startTime;
149
  int64_t  timeStamp;
150
  int16_t  reserved;
M
Minghao Li 已提交
151 152
} SyncHeartbeatReply;

M
Minghao Li 已提交
153 154 155 156 157 158 159 160 161
typedef struct SyncPreSnapshot {
  uint32_t bytes;
  int32_t  vgId;
  uint32_t msgType;
  SRaftId  srcId;
  SRaftId  destId;

  // private data
  SyncTerm term;
162
  int16_t  reserved;
M
Minghao Li 已提交
163 164
} SyncPreSnapshot;

M
Minghao Li 已提交
165 166 167 168 169 170 171 172 173
typedef struct SyncPreSnapshotReply {
  uint32_t bytes;
  int32_t  vgId;
  uint32_t msgType;
  SRaftId  srcId;
  SRaftId  destId;

  // private data
  SyncTerm  term;
174
  SyncIndex snapStart;
175
  int16_t   reserved;
M
Minghao Li 已提交
176 177
} SyncPreSnapshotReply;

M
Minghao Li 已提交
178 179 180 181 182 183 184 185 186 187
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;

188 189 190 191 192 193 194 195
typedef struct SyncSnapshotSend {
  uint32_t bytes;
  int32_t  vgId;
  uint32_t msgType;
  SRaftId  srcId;
  SRaftId  destId;

  SyncTerm  term;
196 197 198 199
  SyncIndex beginIndex;       // snapshot.beginIndex
  SyncIndex lastIndex;        // snapshot.lastIndex
  SyncTerm  lastTerm;         // snapshot.lastTerm
  SyncIndex lastConfigIndex;  // snapshot.lastConfigIndex
200
  SSyncCfg  lastConfig;
M
Minghao Li 已提交
201
  int64_t   startTime;
202
  int32_t   seq;
203
  int16_t   reserved;
204 205 206 207 208 209 210 211 212 213 214 215 216 217
  uint32_t  dataLen;
  char      data[];
} SyncSnapshotSend;

typedef struct SyncSnapshotRsp {
  uint32_t bytes;
  int32_t  vgId;
  uint32_t msgType;
  SRaftId  srcId;
  SRaftId  destId;

  SyncTerm  term;
  SyncIndex lastIndex;
  SyncTerm  lastTerm;
M
Minghao Li 已提交
218
  int64_t   startTime;
219
  int32_t   ack;
220
  int32_t   code;
M
Minghao Li 已提交
221
  SyncIndex snapBeginIndex;  // when ack = SYNC_SNAPSHOT_SEQ_BEGIN, it's valid
222
  int16_t   reserved;
223 224
} SyncSnapshotRsp;

225 226 227 228 229 230 231 232
typedef struct SyncLeaderTransfer {
  uint32_t bytes;
  int32_t  vgId;
  uint32_t msgType;
  /*
   SRaftId  srcId;
   SRaftId  destId;
   */
M
Minghao Li 已提交
233
  SNodeInfo newNodeInfo;
234
  SRaftId   newLeaderId;
235 236
} SyncLeaderTransfer;

M
Minghao Li 已提交
237
typedef enum {
M
Minghao Li 已提交
238
  SYNC_LOCAL_CMD_STEP_DOWN = 100,
239
  SYNC_LOCAL_CMD_FOLLOWER_CMT,
M
Minghao Li 已提交
240 241
} ESyncLocalCmd;

M
Minghao Li 已提交
242 243 244 245 246 247 248
typedef struct SyncLocalCmd {
  uint32_t bytes;
  int32_t  vgId;
  uint32_t msgType;
  SRaftId  srcId;
  SRaftId  destId;

S
Shengliang Guan 已提交
249 250 251
  int32_t   cmd;
  SyncTerm  sdNewTerm;  // step down new term
  SyncIndex fcIndex;    // follower commit index
M
Minghao Li 已提交
252 253
} SyncLocalCmd;

S
Shengliang Guan 已提交
254
int32_t syncBuildTimeout(SRpcMsg* pMsg, ESyncTimeoutType ttype, uint64_t logicClock, int32_t ms, SSyncNode* pNode);
S
Shengliang Guan 已提交
255
int32_t syncBuildClientRequest(SRpcMsg* pMsg, const SRpcMsg* pOriginal, uint64_t seq, bool isWeak, int32_t vgId);
S
Shengliang Guan 已提交
256
int32_t syncBuildClientRequestFromNoopEntry(SRpcMsg* pMsg, const SSyncRaftEntry* pEntry, int32_t vgId);
S
Shengliang Guan 已提交
257
int32_t syncBuildRequestVote(SRpcMsg* pMsg, int32_t vgId);
S
Shengliang Guan 已提交
258
int32_t syncBuildRequestVoteReply(SRpcMsg* pMsg, int32_t vgId);
259
int32_t syncBuildAppendEntries(SRpcMsg* pMsg, int32_t dataLen, int32_t vgId);
260
int32_t syncBuildAppendEntriesReply(SRpcMsg* pMsg, int32_t vgId);
261 262
int32_t syncBuildAppendEntriesFromRaftLog(SSyncNode* pNode, SSyncRaftEntry* pEntry, SyncTerm prevLogTerm,
                                          SRpcMsg* pRpcMsg);
S
Shengliang Guan 已提交
263
int32_t syncBuildHeartbeat(SRpcMsg* pMsg, int32_t vgId);
264
int32_t syncBuildHeartbeatReply(SRpcMsg* pMsg, int32_t vgId);
S
Shengliang Guan 已提交
265 266
int32_t syncBuildPreSnapshot(SRpcMsg* pMsg, int32_t vgId);
int32_t syncBuildPreSnapshotReply(SRpcMsg* pMsg, int32_t vgId);
S
Shengliang Guan 已提交
267
int32_t syncBuildApplyMsg(SRpcMsg* pMsg, const SRpcMsg* pOriginal, int32_t vgId, SFsmCbMeta* pMeta);
268
int32_t syncBuildSnapshotSend(SRpcMsg* pMsg, int32_t dataLen, int32_t vgId);
269
int32_t syncBuildSnapshotSendRsp(SRpcMsg* pMsg, int32_t vgId);
270
int32_t syncBuildLeaderTransfer(SRpcMsg* pMsg, int32_t vgId);
S
Shengliang Guan 已提交
271
int32_t syncBuildLocalCmd(SRpcMsg* pMsg, int32_t vgId);
S
Shengliang Guan 已提交
272

S
Shengliang Guan 已提交
273 274 275
const char* syncTimerTypeStr(ESyncTimeoutType timerType);
const char* syncLocalCmdGetStr(ESyncLocalCmd cmd);

M
Minghao Li 已提交
276 277 278 279
#ifdef __cplusplus
}
#endif

280
#endif /*_TD_LIBS_SYNC_MESSAGE_H*/