tsync.h 3.8 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
/*
 * 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

typedef enum _TAOS_SYNC_ROLE {
  TAOS_SYNC_ROLE_OFFLINE,
  TAOS_SYNC_ROLE_UNSYNCED,
  TAOS_SYNC_ROLE_SLAVE,
  TAOS_SYNC_ROLE_MASTER,
} ESyncRole;

typedef enum _TAOS_SYNC_STATUS {
  TAOS_SYNC_STATUS_INIT,
  TAOS_SYNC_STATUS_START,
  TAOS_SYNC_STATUS_FILE,
  TAOS_SYNC_STATUS_CACHE,
} ESyncStatus;

typedef struct {
  uint32_t  nodeId;    // node ID assigned by TDengine
J
jtao1735 已提交
41 42
  uint16_t  nodePort;  // node sync Port
  char      nodeFqdn[TSDB_FQDN_LEN]; // node FQDN  
J
Jeff Tao 已提交
43 44 45 46 47 48 49
} 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 已提交
50 51 52 53 54 55 56

typedef struct {
  int       selfIndex;
  uint32_t  nodeId[TAOS_SYNC_MAX_REPLICA];
  int       role[TAOS_SYNC_MAX_REPLICA];  
} SNodesRole;
 
57 58
// if name is empty(name[0] is zero), get the file from index or after, used by master
// if name is provided(name[0] is not zero), get the named file at the specified index, used by unsynced node
J
Jeff Tao 已提交
59
// it returns the file magic number and size, if file not there, magic shall be 0.
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
60
typedef uint32_t (*FGetFileInfo)(void *ahandle, char *name, uint32_t *index, int32_t *size, uint64_t *fversion); 
J
Jeff Tao 已提交
61 62 63 64 65 66 67 68 69 70 71 72 73 74

// 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
typedef int      (*FGetWalInfo)(void *ahandle, char *name, uint32_t *index); 
 
// when a forward pkt is received, call this to handle data 
typedef int      (*FWriteToCache)(void *ahandle, void *pHead, int type);

// when forward is confirmed by peer, master call this API to notify app
typedef void     (*FConfirmForward)(void *ahandle, void *mhandle, int32_t code);

// when role is changed, call this to notify app
typedef void     (*FNotifyRole)(void *ahandle, int8_t role);

75
// when data file is synced successfully, notity app
陶建辉(Jeff)'s avatar
陶建辉(Jeff) 已提交
76
typedef void     (*FNotifyFileSynced)(void *ahandle, uint64_t fversion);
77

J
Jeff Tao 已提交
78 79 80
typedef struct {
  int32_t    vgId;      // vgroup ID
  uint64_t   version;   // initial version
81 82
  SSyncCfg   syncCfg;   // configuration from mgmt
  char       path[128]; // path to the file
J
Jeff Tao 已提交
83
 
84
  void      *ahandle;   // handle provided by APP 
J
Jeff Tao 已提交
85 86 87 88 89
  FGetFileInfo    getFileInfo;
  FGetWalInfo     getWalInfo;
  FWriteToCache   writeToCache;
  FConfirmForward confirmForward;
  FNotifyRole     notifyRole;
90
  FNotifyFileSynced notifyFileSynced;
J
Jeff Tao 已提交
91 92 93 94
} SSyncInfo;

typedef void* tsync_h;

J
Jeff Tao 已提交
95
tsync_h syncStart(const SSyncInfo *);
J
Jeff Tao 已提交
96
void    syncStop(tsync_h shandle);
J
Jeff Tao 已提交
97
int     syncReconfig(tsync_h shandle, const SSyncCfg *);
98
int     syncForwardToPeer(tsync_h shandle, void *pHead, void *mhandle, int qtype);
J
Jeff Tao 已提交
99 100 101 102 103 104
void    syncConfirmForward(tsync_h shandle, uint64_t version, int32_t code);
void    syncRecover(tsync_h shandle);      // recover from other nodes:
int     syncGetNodesRole(tsync_h shandle, SNodesRole *);

extern  char *syncRole[];

J
Jeff Tao 已提交
105
//global configurable parameters
J
Jeff Tao 已提交
106 107 108
extern  int   tsMaxSyncNum;
extern  int   tsSyncTcpThreads;
extern  int   tsMaxWatchFiles;
J
Jeff Tao 已提交
109
extern  int   tsSyncTimer;
J
Jeff Tao 已提交
110
extern  int   tsMaxFwdInfo; 
J
Jeff Tao 已提交
111
extern  int   sDebugFlag;
J
jtao1735 已提交
112
extern  char  tsArbitrator[];
S
slguan 已提交
113
extern  uint16_t tsSyncPort;
J
Jeff Tao 已提交
114 115 116 117 118 119

#ifdef __cplusplus
}
#endif

#endif  // TDENGINE_SYNC_H