syncRaftCfg.h 3.0 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * 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 _TD_LIBS_SYNC_RAFT_CFG_H
#define _TD_LIBS_SYNC_RAFT_CFG_H

#ifdef __cplusplus
extern "C" {
#endif

#include "syncInt.h"

M
Minghao Li 已提交
25
#define CONFIG_FILE_LEN 2048
M
Minghao Li 已提交
26

27
#define MAX_CONFIG_INDEX_COUNT 256
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42
// SRaftCfgIndex ------------------------------------------
typedef struct SRaftCfgIndex {
  TdFilePtr pFile;
  char      path[TSDB_FILENAME_LEN * 2];

  SyncIndex configIndexArr[MAX_CONFIG_INDEX_COUNT];
  int32_t   configIndexCount;
} SRaftCfgIndex;

SRaftCfgIndex *raftCfgIndexOpen(const char *path);
int32_t        raftCfgIndexClose(SRaftCfgIndex *pRaftCfgIndex);
int32_t        raftCfgIndexPersist(SRaftCfgIndex *pRaftCfgIndex);
int32_t        raftCfgIndexAddConfigIndex(SRaftCfgIndex *pRaftCfgIndex, SyncIndex configIndex);

S
Shengliang Guan 已提交
43 44
cJSON  *raftCfgIndex2Json(SRaftCfgIndex *pRaftCfgIndex);
char   *raftCfgIndex2Str(SRaftCfgIndex *pRaftCfgIndex);
45 46 47 48 49 50 51
int32_t raftCfgIndexFromJson(const cJSON *pRoot, SRaftCfgIndex *pRaftCfgIndex);
int32_t raftCfgIndexFromStr(const char *s, SRaftCfgIndex *pRaftCfgIndex);

int32_t raftCfgIndexCreateFile(const char *path);

// ---------------------------------------------------------

M
Minghao Li 已提交
52 53 54 55
typedef struct SRaftCfg {
  SSyncCfg  cfg;
  TdFilePtr pFile;
  char      path[TSDB_FILENAME_LEN * 2];
M
Minghao Li 已提交
56
  int8_t    isStandBy;
M
Minghao Li 已提交
57
  int32_t   batchSize;
M
Minghao Li 已提交
58
  int8_t    snapshotStrategy;
59
  SyncIndex lastConfigIndex;
60 61 62 63

  SyncIndex configIndexArr[MAX_CONFIG_INDEX_COUNT];
  int32_t   configIndexCount;

M
Minghao Li 已提交
64 65 66 67 68
} SRaftCfg;

SRaftCfg *raftCfgOpen(const char *path);
int32_t   raftCfgClose(SRaftCfg *pRaftCfg);
int32_t   raftCfgPersist(SRaftCfg *pRaftCfg);
69
int32_t   raftCfgAddConfigIndex(SRaftCfg *pRaftCfg, SyncIndex configIndex);
M
Minghao Li 已提交
70

S
Shengliang Guan 已提交
71 72 73
cJSON  *syncCfg2Json(SSyncCfg *pSyncCfg);
char   *syncCfg2Str(SSyncCfg *pSyncCfg);
void    syncCfg2SimpleStr(const SSyncCfg *pCfg, char *str, int32_t bufLen);
M
Minghao Li 已提交
74 75 76
int32_t syncCfgFromJson(const cJSON *pRoot, SSyncCfg *pSyncCfg);
int32_t syncCfgFromStr(const char *s, SSyncCfg *pSyncCfg);

S
Shengliang Guan 已提交
77 78
cJSON  *raftCfg2Json(SRaftCfg *pRaftCfg);
char   *raftCfg2Str(SRaftCfg *pRaftCfg);
M
Minghao Li 已提交
79 80
int32_t raftCfgFromJson(const cJSON *pRoot, SRaftCfg *pRaftCfg);
int32_t raftCfgFromStr(const char *s, SRaftCfg *pRaftCfg);
M
Minghao Li 已提交
81

M
Minghao Li 已提交
82
typedef struct SRaftCfgMeta {
83
  int8_t    isStandBy;
M
Minghao Li 已提交
84
  int32_t   batchSize;
M
Minghao Li 已提交
85
  int8_t    snapshotStrategy;
86
  SyncIndex lastConfigIndex;
M
Minghao Li 已提交
87 88 89
} SRaftCfgMeta;

int32_t raftCfgCreateFile(SSyncCfg *pCfg, SRaftCfgMeta meta, const char *path);
M
Minghao Li 已提交
90 91 92 93 94 95

#ifdef __cplusplus
}
#endif

#endif /*_TD_LIBS_SYNC_RAFT_CFG_H*/