sync.h 4.6 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 <stdbool.h>
S
Shengliang Guan 已提交
24
#include <stdint.h>
M
Minghao Li 已提交
25
//#include <tdatablock.h>
M
Minghao Li 已提交
26
#include "cJSON.h"
M
Minghao Li 已提交
27 28 29 30
#include "tdef.h"
//#include "taosdef.h"
//#include "trpc.h"
//#include "wal.h"
S
Shengliang Guan 已提交
31

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

typedef enum {
M
Minghao Li 已提交
38 39 40
  TAOS_SYNC_STATE_FOLLOWER = 100,
  TAOS_SYNC_STATE_CANDIDATE = 101,
  TAOS_SYNC_STATE_LEADER = 102,
M
Minghao Li 已提交
41
  TAOS_SYNC_STATE_ERROR = 103,
M
syncInt  
Minghao Li 已提交
42
} ESyncState;
S
Shengliang Guan 已提交
43

M
Minghao Li 已提交
44
typedef struct SNodeInfo {
M
Minghao Li 已提交
45 46
  uint16_t nodePort;
  char     nodeFqdn[TSDB_FQDN_LEN];
S
Shengliang Guan 已提交
47 48
} SNodeInfo;

M
Minghao Li 已提交
49
typedef struct SSyncCfg {
M
Minghao Li 已提交
50
  int32_t   replicaNum;
M
Minghao Li 已提交
51
  int32_t   myIndex;
S
Shengliang Guan 已提交
52
  SNodeInfo nodeInfo[TSDB_MAX_REPLICA];
M
Minghao Li 已提交
53
} SSyncCfg;
S
Shengliang Guan 已提交
54

M
Minghao Li 已提交
55 56 57
typedef struct SSnapshot {
  void*     data;
  SyncIndex lastApplyIndex;
M
Minghao Li 已提交
58
  SyncTerm  lastApplyTerm;
M
Minghao Li 已提交
59
} SSnapshot;
S
Shengliang Guan 已提交
60

M
Minghao Li 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73
typedef enum {
  TAOS_SYNC_FSM_CB_SUCCESS = 0,
  TAOS_SYNC_FSM_CB_OTHER_ERROR,
} ESyncFsmCbCode;

typedef struct SFsmCbMeta {
  SyncIndex  index;
  bool       isWeak;
  int32_t    code;
  ESyncState state;
  uint64_t   seqNum;
} SFsmCbMeta;

M
Minghao Li 已提交
74 75
struct SRpcMsg;
typedef struct SRpcMsg SRpcMsg;
S
Shengliang Guan 已提交
76

M
Minghao Li 已提交
77 78
typedef struct SSyncFSM {
  void* data;
S
Shengliang Guan 已提交
79

M
Minghao Li 已提交
80 81 82
  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);
S
Shengliang Guan 已提交
83

M
Minghao Li 已提交
84 85
  int32_t (*FpGetSnapshot)(struct SSyncFSM* pFsm, SSnapshot* pSnapshot);
  int32_t (*FpRestoreSnapshot)(struct SSyncFSM* pFsm, const SSnapshot* snapshot);
S
Shengliang Guan 已提交
86 87 88

} SSyncFSM;

M
Minghao Li 已提交
89 90 91
struct SSyncRaftEntry;
typedef struct SSyncRaftEntry SSyncRaftEntry;

M
Minghao Li 已提交
92
#define SYNC_INDEX_BEGIN   0
M
Minghao Li 已提交
93 94
#define SYNC_INDEX_INVALID -1

M
Minghao Li 已提交
95 96
// abstract definition of log store in raft
// SWal implements it
S
Shengliang Guan 已提交
97
typedef struct SSyncLogStore {
M
Minghao Li 已提交
98 99 100
  void* data;

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

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

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

M
Minghao Li 已提交
109 110 111 112 113
  // return index of last entry
  SyncIndex (*getLastIndex)(struct SSyncLogStore* pLogStore);

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

M
Minghao Li 已提交
115 116 117 118 119 120
  // 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);

S
Shengliang Guan 已提交
121 122
} SSyncLogStore;

M
Minghao Li 已提交
123 124
struct SWal;
typedef struct SWal SWal;
S
Shengliang Guan 已提交
125

M
Minghao Li 已提交
126 127
struct SEpSet;
typedef struct SEpSet SEpSet;
S
Shengliang Guan 已提交
128

M
Minghao Li 已提交
129 130 131
typedef struct SSyncInfo {
  SyncGroupId vgId;
  SSyncCfg    syncCfg;
M
Minghao Li 已提交
132
  char        path[TSDB_FILENAME_LEN];
M
Minghao Li 已提交
133
  SWal*       pWal;
M
Minghao Li 已提交
134
  SSyncFSM*   pFsm;
M
Minghao Li 已提交
135 136 137

  void* rpcClient;
  int32_t (*FpSendMsg)(void* rpcClient, const SEpSet* pEpSet, SRpcMsg* pMsg);
M
Minghao Li 已提交
138 139
  void* queue;
  int32_t (*FpEqMsg)(void* queue, SRpcMsg* pMsg);
M
Minghao Li 已提交
140

S
Shengliang Guan 已提交
141 142
} SSyncInfo;

M
Minghao Li 已提交
143 144 145 146 147 148 149 150 151
int32_t     syncInit();
void        syncCleanUp();
int64_t     syncOpen(const SSyncInfo* pSyncInfo);
void        syncStart(int64_t rid);
void        syncStop(int64_t rid);
int32_t     syncReconfig(int64_t rid, const SSyncCfg* pSyncCfg);
ESyncState  syncGetMyRole(int64_t rid);
const char* syncGetMyRoleStr(int64_t rid);
SyncTerm    syncGetMyTerm(int64_t rid);
S
Shengliang Guan 已提交
152

M
Minghao Li 已提交
153 154 155 156 157
typedef enum {
  TAOS_SYNC_PROPOSE_SUCCESS = 0,
  TAOS_SYNC_PROPOSE_NOT_LEADER,
  TAOS_SYNC_PROPOSE_OTHER_ERROR,
} ESyncProposeCode;
S
Shengliang Guan 已提交
158

M
Minghao Li 已提交
159
int32_t syncPropose(int64_t rid, const SRpcMsg* pMsg, bool isWeak);
S
Shengliang Guan 已提交
160

161 162
bool syncEnvIsStart();

M
Minghao Li 已提交
163
extern int32_t sDebugFlag;
M
Minghao Li 已提交
164

M
Minghao Li 已提交
165 166 167
//-----------------------------------------
struct SSyncNode;
typedef struct SSyncNode SSyncNode;
M
Minghao Li 已提交
168

M
Minghao Li 已提交
169 170 171
struct SSyncBuffer;
typedef struct SSyncBuffer SSyncBuffer;
//-----------------------------------------
S
Shengliang Guan 已提交
172

M
Minghao Li 已提交
173
const char* syncStr(ESyncState state);
S
Shengliang Guan 已提交
174

S
Shengliang Guan 已提交
175 176 177 178 179
#ifdef __cplusplus
}
#endif

#endif /*_TD_LIBS_SYNC_H*/