tsync.h 3.9 KB
Newer Older
J
Jeff Tao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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 TDENGINE_SYNC_H
#define TDENGINE_SYNC_H

#ifdef __cplusplus
extern "C" {
#endif

#define TAOS_SYNC_MAX_REPLICA 5
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
24
#define TAOS_SYNC_MAX_INDEX   0x7FFFFFFF
J
Jeff Tao 已提交
25

S
Shengliang Guan 已提交
26
typedef enum {
S
TD-2157  
Shengliang Guan 已提交
27 28 29 30 31
  TAOS_SYNC_ROLE_OFFLINE  = 0,
  TAOS_SYNC_ROLE_UNSYNCED = 1,
  TAOS_SYNC_ROLE_SYNCING  = 2,
  TAOS_SYNC_ROLE_SLAVE    = 3,
  TAOS_SYNC_ROLE_MASTER   = 4
J
Jeff Tao 已提交
32 33
} ESyncRole;

S
Shengliang Guan 已提交
34
typedef enum {
S
TD-2157  
Shengliang Guan 已提交
35 36 37 38
  TAOS_SYNC_STATUS_INIT  = 0,
  TAOS_SYNC_STATUS_START = 1,
  TAOS_SYNC_STATUS_FILE  = 2,
  TAOS_SYNC_STATUS_CACHE = 3
J
Jeff Tao 已提交
39 40 41 42
} ESyncStatus;

typedef struct {
  uint32_t  nodeId;    // node ID assigned by TDengine
J
jtao1735 已提交
43 44
  uint16_t  nodePort;  // node sync Port
  char      nodeFqdn[TSDB_FQDN_LEN]; // node FQDN  
J
Jeff Tao 已提交
45 46 47 48 49 50 51
} SNodeInfo;

typedef struct {
  int8_t     quorum;    // number of confirms required, >=1 
  int8_t     replica;   // number of replications, >=1
  SNodeInfo  nodeInfo[TAOS_SYNC_MAX_REPLICA];
} SSyncCfg;
J
Jeff Tao 已提交
52 53

typedef struct {
S
Shengliang Guan 已提交
54 55 56
  int32_t  selfIndex;
  uint32_t nodeId[TAOS_SYNC_MAX_REPLICA];
  int32_t  role[TAOS_SYNC_MAX_REPLICA];
J
Jeff Tao 已提交
57
} SNodesRole;
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
58

J
Jeff Tao 已提交
59 60
// get the wal file from index or after
// return value, -1: error, 1:more wal files, 0:last WAL. if name[0]==0, no WAL file
61
typedef int32_t  (*FGetWalInfo)(int32_t vgId, char *fileName, int64_t *fileId); 
J
Jeff Tao 已提交
62
 
S
TD-1918  
Shengliang Guan 已提交
63
// when a forward pkt is received, call this to handle data
64
typedef int32_t  (*FWriteToCache)(int32_t vgId, void *pHead, int32_t qtype, void *pMsg);
J
Jeff Tao 已提交
65 66

// when forward is confirmed by peer, master call this API to notify app
67
typedef void     (*FConfirmForward)(int32_t vgId, void *mhandle, int32_t code);
J
Jeff Tao 已提交
68 69

// when role is changed, call this to notify app
70
typedef void     (*FNotifyRole)(int32_t vgId, int8_t role);
J
Jeff Tao 已提交
71

72
// if a number of retrieving data failed, call this to start flow control 
S
Shengliang Guan 已提交
73
typedef void     (*FNotifyFlowCtrl)(int32_t vgId, int32_t level);
74

75
// when data file is synced successfully, notity app
S
TD-2798  
Shengliang Guan 已提交
76 77
typedef void     (*FStartSyncFile)(int32_t vgId);
typedef void     (*FStopSyncFile)(int32_t vgId, uint64_t fversion);
78

S
TD-1926  
Shengliang Guan 已提交
79
// get file version
S
TD-1926  
Shengliang Guan 已提交
80
typedef int32_t  (*FGetVersion)(int32_t vgId, uint64_t *fver, uint64_t *vver);
S
TD-1926  
Shengliang Guan 已提交
81

S
TD-2798  
Shengliang Guan 已提交
82 83 84
typedef int32_t  (*FSendFile)(void *tsdb, int32_t socketFd);
typedef int32_t  (*FRecvFile)(void *tsdb, int32_t socketFd);

J
Jeff Tao 已提交
85
typedef struct {
S
Shengliang Guan 已提交
86 87 88
  int32_t  vgId;       // vgroup ID
  uint64_t version;    // initial version
  SSyncCfg syncCfg;    // configuration from mgmt
S
TD-2166  
Shengliang Guan 已提交
89
  char     path[TSDB_FILENAME_LEN];  // path to the file
S
TD-2798  
Shengliang Guan 已提交
90 91 92
  void *   pTsdb;
  FGetWalInfo       getWalInfoFp;
  FWriteToCache     writeToCacheFp;
S
Shengliang Guan 已提交
93
  FConfirmForward   confirmForward;
S
TD-2798  
Shengliang Guan 已提交
94 95 96 97 98 99 100
  FNotifyRole       notifyRoleFp;
  FNotifyFlowCtrl   notifyFlowCtrlFp;
  FStartSyncFile    startSyncFileFp;
  FStopSyncFile     stopSyncFileFp;
  FGetVersion       getVersionFp;
  FSendFile         sendFileFp;
  FRecvFile         recvFileFp;
J
Jeff Tao 已提交
101 102
} SSyncInfo;

S
Shengliang Guan 已提交
103
typedef void *tsync_h;
J
Jeff Tao 已提交
104

S
Shengliang Guan 已提交
105 106 107
int32_t syncInit();
void    syncCleanUp();

陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
108 109 110
int64_t syncStart(const SSyncInfo *);
void    syncStop(int64_t rid);
int32_t syncReconfig(int64_t rid, const SSyncCfg *);
S
Shengliang Guan 已提交
111
int32_t syncForwardToPeer(int64_t rid, void *pHead, void *mhandle, int32_t qtype);
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
112
void    syncConfirmForward(int64_t rid, uint64_t version, int32_t code);
S
Shengliang Guan 已提交
113 114
void    syncRecover(int64_t rid);  // recover from other nodes:
int32_t syncGetNodesRole(int64_t rid, SNodesRole *);
J
Jeff Tao 已提交
115

S
Shengliang Guan 已提交
116
extern char *syncRole[];
J
Jeff Tao 已提交
117

J
Jeff Tao 已提交
118
//global configurable parameters
S
Shengliang Guan 已提交
119 120 121
extern int32_t  sDebugFlag;
extern char     tsArbitrator[];
extern uint16_t tsSyncPort;
J
Jeff Tao 已提交
122 123 124 125 126 127

#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_SYNC_H