sync.h 6.9 KB
Newer Older
S
Shengliang Guan 已提交
1
/*
M
Minghao Li 已提交
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
S
Shengliang Guan 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
 *
 * 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_H
#define _TD_LIBS_SYNC_H

#ifdef __cplusplus
extern "C" {
#endif

M
Minghao Li 已提交
23
#include "cJSON.h"
M
Minghao Li 已提交
24
#include "tdef.h"
S
Shengliang Guan 已提交
25
#include "tmsgcb.h"
S
Shengliang Guan 已提交
26

M
Minghao Li 已提交
27 28
extern bool gRaftDetailLog;

29
#define SYNC_INDEX_BEGIN 0
30 31
#define SYNC_INDEX_INVALID -1

M
Minghao Li 已提交
32
typedef uint64_t SyncNodeId;
S
Shengliang Guan 已提交
33 34 35 36
typedef int32_t  SyncGroupId;
typedef int64_t  SyncIndex;
typedef uint64_t SyncTerm;

37 38 39 40 41
typedef struct SSyncNode      SSyncNode;
typedef struct SSyncBuffer    SSyncBuffer;
typedef struct SWal           SWal;
typedef struct SSyncRaftEntry SSyncRaftEntry;

S
Shengliang Guan 已提交
42
typedef enum {
M
Minghao Li 已提交
43 44 45
  TAOS_SYNC_STATE_FOLLOWER = 100,
  TAOS_SYNC_STATE_CANDIDATE = 101,
  TAOS_SYNC_STATE_LEADER = 102,
M
Minghao Li 已提交
46
  TAOS_SYNC_STATE_ERROR = 103,
M
syncInt  
Minghao Li 已提交
47
} ESyncState;
S
Shengliang Guan 已提交
48

49 50 51 52 53
typedef enum {
  TAOS_SYNC_FSM_CB_SUCCESS = 0,
  TAOS_SYNC_FSM_CB_OTHER_ERROR = 1,
} ESyncFsmCbCode;

M
Minghao Li 已提交
54
typedef struct SNodeInfo {
M
Minghao Li 已提交
55 56
  uint16_t nodePort;
  char     nodeFqdn[TSDB_FQDN_LEN];
S
Shengliang Guan 已提交
57 58
} SNodeInfo;

M
Minghao Li 已提交
59
typedef struct SSyncCfg {
M
Minghao Li 已提交
60
  int32_t   replicaNum;
M
Minghao Li 已提交
61
  int32_t   myIndex;
S
Shengliang Guan 已提交
62
  SNodeInfo nodeInfo[TSDB_MAX_REPLICA];
M
Minghao Li 已提交
63
} SSyncCfg;
S
Shengliang Guan 已提交
64

M
Minghao Li 已提交
65
typedef struct SFsmCbMeta {
M
Minghao Li 已提交
66
  int32_t    code;
M
Minghao Li 已提交
67
  SyncIndex  index;
M
Minghao Li 已提交
68 69
  SyncTerm   term;
  uint64_t   seqNum;
70
  SyncIndex  lastConfigIndex;
M
Minghao Li 已提交
71
  ESyncState state;
72
  SyncTerm   currentTerm;
M
Minghao Li 已提交
73
  bool       isWeak;
74
  uint64_t   flag;
M
Minghao Li 已提交
75 76
} SFsmCbMeta;

77
typedef struct SReConfigCbMeta {
M
Minghao Li 已提交
78 79 80 81 82 83 84 85 86 87 88
  int32_t    code;
  SyncIndex  index;
  SyncTerm   term;
  uint64_t   seqNum;
  SyncIndex  lastConfigIndex;
  ESyncState state;
  SyncTerm   currentTerm;
  bool       isWeak;
  uint64_t   flag;

  // config info
M
Minghao Li 已提交
89
  SSyncCfg  oldCfg;
90
  SSyncCfg  newCfg;
M
Minghao Li 已提交
91 92 93 94
  SyncIndex newCfgIndex;
  SyncTerm  newCfgTerm;
  uint64_t  newCfgSeqNum;

95 96
} SReConfigCbMeta;

97
typedef struct SSnapshot {
M
Minghao Li 已提交
98
  void*     data;
99 100
  SyncIndex lastApplyIndex;
  SyncTerm  lastApplyTerm;
M
Minghao Li 已提交
101
  SyncIndex lastConfigIndex;
102 103
} SSnapshot;

104 105 106 107
typedef struct SSnapshotMeta {
  SyncIndex lastConfigIndex;
} SSnapshotMeta;

M
Minghao Li 已提交
108 109
typedef struct SSyncFSM {
  void* data;
110

M
Minghao Li 已提交
111 112 113
  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);
114

115
  void (*FpRestoreFinishCb)(struct SSyncFSM* pFsm);
116
  void (*FpReConfigCb)(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SReConfigCbMeta cbMeta);
M
Minghao Li 已提交
117
  void (*FpLeaderTransferCb)(struct SSyncFSM* pFsm, const SRpcMsg* pMsg, SFsmCbMeta cbMeta);
M
Minghao Li 已提交
118

M
Minghao Li 已提交
119
  int32_t (*FpGetSnapshot)(struct SSyncFSM* pFsm, SSnapshot* pSnapshot, void* pReaderParam, void** ppReader);
120
  int32_t (*FpGetSnapshotInfo)(struct SSyncFSM* pFsm, SSnapshot* pSnapshot);
121

122 123 124
  int32_t (*FpSnapshotStartRead)(struct SSyncFSM* pFsm, void** ppReader);
  int32_t (*FpSnapshotStopRead)(struct SSyncFSM* pFsm, void* pReader);
  int32_t (*FpSnapshotDoRead)(struct SSyncFSM* pFsm, void* pReader, void** ppBuf, int32_t* len);
125

126 127 128
  int32_t (*FpSnapshotStartWrite)(struct SSyncFSM* pFsm, void** ppWriter);
  int32_t (*FpSnapshotStopWrite)(struct SSyncFSM* pFsm, void* pWriter, bool isApply);
  int32_t (*FpSnapshotDoWrite)(struct SSyncFSM* pFsm, void* pWriter, void* pBuf, int32_t len);
129

S
Shengliang Guan 已提交
130 131
} SSyncFSM;

M
Minghao Li 已提交
132 133
// abstract definition of log store in raft
// SWal implements it
S
Shengliang Guan 已提交
134
typedef struct SSyncLogStore {
M
Minghao Li 已提交
135 136 137
  void* data;

  // append one log entry
M
Minghao Li 已提交
138
  int32_t (*appendEntry)(struct SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry);
S
Shengliang Guan 已提交
139

M
Minghao Li 已提交
140
  // get one log entry, user need to free pEntry->pCont
M
Minghao Li 已提交
141
  SSyncRaftEntry* (*getEntry)(struct SSyncLogStore* pLogStore, SyncIndex index);
S
Shengliang Guan 已提交
142

M
Minghao Li 已提交
143 144
  // truncate log with index, entries after the given index (>=index) will be deleted
  int32_t (*truncate)(struct SSyncLogStore* pLogStore, SyncIndex fromIndex);
S
Shengliang Guan 已提交
145

M
Minghao Li 已提交
146 147 148 149 150
  // return index of last entry
  SyncIndex (*getLastIndex)(struct SSyncLogStore* pLogStore);

  // return term of last entry
  SyncTerm (*getLastTerm)(struct SSyncLogStore* pLogStore);
S
Shengliang Guan 已提交
151

M
Minghao Li 已提交
152 153 154 155 156 157
  // update log store commit index with "index"
  int32_t (*updateCommitIndex)(struct SSyncLogStore* pLogStore, SyncIndex index);

  // return commit index of log
  SyncIndex (*getCommitIndex)(struct SSyncLogStore* pLogStore);

M
Minghao Li 已提交
158 159
  // refactor, log[0 .. n] ==> log[m .. n]
  int32_t (*syncLogSetBeginIndex)(struct SSyncLogStore* pLogStore, SyncIndex beginIndex);
M
Minghao Li 已提交
160
  int32_t (*syncLogResetBeginIndex)(struct SSyncLogStore* pLogStore);
M
Minghao Li 已提交
161 162 163 164 165 166
  SyncIndex (*syncLogBeginIndex)(struct SSyncLogStore* pLogStore);
  SyncIndex (*syncLogEndIndex)(struct SSyncLogStore* pLogStore);
  bool (*syncLogIsEmpty)(struct SSyncLogStore* pLogStore);
  int32_t (*syncLogEntryCount)(struct SSyncLogStore* pLogStore);
  bool (*syncLogInRange)(struct SSyncLogStore* pLogStore, SyncIndex index);

M
Minghao Li 已提交
167
  SyncIndex (*syncLogWriteIndex)(struct SSyncLogStore* pLogStore);
M
Minghao Li 已提交
168 169 170 171 172 173 174
  SyncIndex (*syncLogLastIndex)(struct SSyncLogStore* pLogStore);
  SyncTerm (*syncLogLastTerm)(struct SSyncLogStore* pLogStore);

  int32_t (*syncLogAppendEntry)(struct SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry);
  int32_t (*syncLogGetEntry)(struct SSyncLogStore* pLogStore, SyncIndex index, SSyncRaftEntry** ppEntry);
  int32_t (*syncLogTruncate)(struct SSyncLogStore* pLogStore, SyncIndex fromIndex);

S
Shengliang Guan 已提交
175 176
} SSyncLogStore;

M
Minghao Li 已提交
177
typedef struct SSyncInfo {
M
Minghao Li 已提交
178
  bool        isStandBy;
M
Minghao Li 已提交
179
  bool        snapshotEnable;
M
Minghao Li 已提交
180 181
  SyncGroupId vgId;
  SSyncCfg    syncCfg;
M
Minghao Li 已提交
182
  char        path[TSDB_FILENAME_LEN];
M
Minghao Li 已提交
183
  SWal*       pWal;
M
Minghao Li 已提交
184
  SSyncFSM*   pFsm;
S
Shengliang Guan 已提交
185 186 187
  SMsgCb*     msgcb;
  int32_t (*FpSendMsg)(const SEpSet* pEpSet, SRpcMsg* pMsg);
  int32_t (*FpEqMsg)(const SMsgCb* msgcb, SRpcMsg* pMsg);
S
Shengliang Guan 已提交
188 189
} SSyncInfo;

M
Minghao Li 已提交
190 191 192 193 194
int32_t     syncInit();
void        syncCleanUp();
int64_t     syncOpen(const SSyncInfo* pSyncInfo);
void        syncStart(int64_t rid);
void        syncStop(int64_t rid);
M
Minghao Li 已提交
195
int32_t     syncSetStandby(int64_t rid);
M
Minghao Li 已提交
196
ESyncState  syncGetMyRole(int64_t rid);
M
Minghao Li 已提交
197
bool        syncIsReady(int64_t rid);
M
Minghao Li 已提交
198 199
const char* syncGetMyRoleStr(int64_t rid);
SyncTerm    syncGetMyTerm(int64_t rid);
M
Minghao Li 已提交
200
SyncGroupId syncGetVgId(int64_t rid);
M
Minghao Li 已提交
201
void        syncGetEpSet(int64_t rid, SEpSet* pEpSet);
202 203
int32_t     syncPropose(int64_t rid, const SRpcMsg* pMsg, bool isWeak);
bool        syncEnvIsStart();
M
Minghao Li 已提交
204
const char* syncStr(ESyncState state);
M
Minghao Li 已提交
205
bool        syncIsRestoreFinish(int64_t rid);
206

207
int32_t syncReconfig(int64_t rid, const SSyncCfg* pNewCfg);
M
Minghao Li 已提交
208 209 210

// build SRpcMsg, need to call syncPropose with SRpcMsg
int32_t syncReconfigBuild(int64_t rid, const SSyncCfg* pNewCfg, SRpcMsg* pRpcMsg);
211

212 213 214
int32_t syncLeaderTransfer(int64_t rid);
int32_t syncLeaderTransferTo(int64_t rid, SNodeInfo newLeader);

S
Shengliang Guan 已提交
215 216 217 218 219
#ifdef __cplusplus
}
#endif

#endif /*_TD_LIBS_SYNC_H*/