sync.h 3.3 KB
Newer Older
L
lichuang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <cli@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/>.
 */

S
Shengliang Guan 已提交
16 17
#ifndef _TD_LIBS_SYNC_H
#define _TD_LIBS_SYNC_H
L
lichuang 已提交
18 19 20 21 22 23 24 25 26

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include "taosdef.h"
#include "wal.h"

L
lichuang 已提交
27
typedef int64_t SyncNodeId;
28 29
typedef int32_t SyncGroupId;
typedef int64_t   SyncIndex;
L
lichuang 已提交
30 31 32
typedef uint64_t SSyncTerm;

typedef enum {
H
format  
Hongze Cheng 已提交
33
  TAOS_SYNC_ROLE_FOLLOWER = 0,
L
lichuang 已提交
34
  TAOS_SYNC_ROLE_CANDIDATE = 1,
H
format  
Hongze Cheng 已提交
35
  TAOS_SYNC_ROLE_LEADER = 2,
L
lichuang 已提交
36 37 38
} ESyncRole;

typedef struct {
H
format  
Hongze Cheng 已提交
39
  void*  data;
L
lichuang 已提交
40 41 42 43 44 45 46 47 48
  size_t len;
} SSyncBuffer;

typedef struct {
  uint16_t  nodePort;  // node sync Port
  char      nodeFqdn[TSDB_FQDN_LEN]; // node FQDN  
} SNodeInfo;

typedef struct {
H
format  
Hongze Cheng 已提交
49 50 51
  int        selfIndex;
  int        nNode;
  SNodeInfo* nodeInfo;
L
lichuang 已提交
52 53 54 55 56
} SSyncCluster;

typedef struct {
  int32_t  selfIndex;
  int nNode;
L
lichuang 已提交
57
  SNodeInfo* node;
L
lichuang 已提交
58 59 60 61 62 63 64
  ESyncRole*  role;
} SNodesRole;

typedef struct SSyncFSM {
  void* pData;

  // apply committed log, bufs will be free by raft module
H
format  
Hongze Cheng 已提交
65
  int (*applyLog)(struct SSyncFSM* fsm, SyncIndex index, const SSyncBuffer* buf, void* pData);
L
lichuang 已提交
66

H
format  
Hongze Cheng 已提交
67 68
  // cluster commit callback
  int (*onClusterChanged)(struct SSyncFSM* fsm, const SSyncCluster* cluster, void* pData);
L
lichuang 已提交
69 70 71

  // fsm return snapshot in ppBuf, bufs will be free by raft module
  // TODO: getSnapshot SHOULD be async?
H
format  
Hongze Cheng 已提交
72
  int (*getSnapshot)(struct SSyncFSM* fsm, SSyncBuffer** ppBuf, int* objId, bool* isLast);
L
lichuang 已提交
73 74

  // fsm apply snapshot with pBuf data
H
format  
Hongze Cheng 已提交
75
  int (*applySnapshot)(struct SSyncFSM* fsm, SSyncBuffer* pBuf, int objId, bool isLast);
L
lichuang 已提交
76 77

  // call when restore snapshot and log done
H
format  
Hongze Cheng 已提交
78
  int (*onRestoreDone)(struct SSyncFSM* fsm);
L
lichuang 已提交
79

H
format  
Hongze Cheng 已提交
80
  void (*onRollback)(struct SSyncFSM* fsm, SyncIndex index, const SSyncBuffer* buf);
L
lichuang 已提交
81

H
format  
Hongze Cheng 已提交
82
  void (*onRoleChanged)(struct SSyncFSM* fsm, const SNodesRole* pRole);
L
lichuang 已提交
83 84 85 86

} SSyncFSM;

typedef struct SSyncServerState {
L
lichuang 已提交
87
  SNodeInfo voteFor;
L
lichuang 已提交
88
  SSyncTerm term;
L
lichuang 已提交
89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
} SSyncServerState;

typedef struct SStateManager {
  void* pData;

  void (*saveServerState)(struct SStateManager* stateMng, const SSyncServerState* state);

  const SSyncServerState* (*readServerState)(struct SStateManager* stateMng);

  void (*saveCluster)(struct SStateManager* stateMng, const SSyncCluster* cluster);

  const SSyncCluster* (*readCluster)(struct SStateManager* stateMng);
} SStateManager;

typedef struct {
H
format  
Hongze Cheng 已提交
104
  SyncGroupId vgId;
L
lichuang 已提交
105 106 107

  twalh walHandle;

L
lichuang 已提交
108 109
  SyncIndex snapshotIndex;
  SSyncCluster syncCfg;
L
lichuang 已提交
110 111 112 113 114 115 116 117 118

  SSyncFSM fsm;

  SStateManager stateManager;
} SSyncInfo;

int32_t syncInit();
void    syncCleanUp();

H
format  
Hongze Cheng 已提交
119 120
SyncNodeId syncStart(const SSyncInfo*);
void       syncStop(SyncNodeId);
L
lichuang 已提交
121

H
format  
Hongze Cheng 已提交
122
int32_t syncPropose(SyncNodeId nodeId, SSyncBuffer buffer, void* pData, bool isWeak);
L
lichuang 已提交
123

L
lichuang 已提交
124 125 126 127 128
int32_t syncAddNode(SyncNodeId nodeId, const SNodeInfo *pNode);

int32_t syncRemoveNode(SyncNodeId nodeId, const SNodeInfo *pNode);

extern int32_t  syncDebugFlag;
L
lichuang 已提交
129 130 131 132 133

#ifdef __cplusplus
}
#endif

S
Shengliang Guan 已提交
134
#endif  /*_TD_LIBS_SYNC_H*/