syncVoteMgr.h 3.1 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 25 26
/*
 * 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_VOTG_MGR_H
#define _TD_LIBS_SYNC_VOTG_MGR_H

#ifdef __cplusplus
extern "C" {
#endif

#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include "syncInt.h"
M
Minghao Li 已提交
27 28
#include "syncMessage.h"
#include "syncUtil.h"
M
Minghao Li 已提交
29 30
#include "taosdef.h"

M
Minghao Li 已提交
31
// SVotesGranted -----------------------------
M
syncInt  
Minghao Li 已提交
32
typedef struct SVotesGranted {
M
Minghao Li 已提交
33 34
  SRaftId (*replicas)[TSDB_MAX_REPLICA];
  bool       isGranted[TSDB_MAX_REPLICA];
M
Minghao Li 已提交
35
  int32_t    replicaNum;
M
Minghao Li 已提交
36
  int32_t    votes;
M
Minghao Li 已提交
37 38 39 40
  SyncTerm   term;
  int32_t    quorum;
  bool       toLeader;
  SSyncNode *pSyncNode;
M
syncInt  
Minghao Li 已提交
41 42
} SVotesGranted;

M
Minghao Li 已提交
43 44
SVotesGranted *voteGrantedCreate(SSyncNode *pSyncNode);
void           voteGrantedDestroy(SVotesGranted *pVotesGranted);
M
Minghao Li 已提交
45
void           voteGrantedUpdate(SVotesGranted *pVotesGranted, SSyncNode *pSyncNode);
M
Minghao Li 已提交
46 47 48
bool           voteGrantedMajority(SVotesGranted *pVotesGranted);
void           voteGrantedVote(SVotesGranted *pVotesGranted, SyncRequestVoteReply *pMsg);
void           voteGrantedReset(SVotesGranted *pVotesGranted, SyncTerm term);
M
Minghao Li 已提交
49 50
cJSON *        voteGranted2Json(SVotesGranted *pVotesGranted);
char *         voteGranted2Str(SVotesGranted *pVotesGranted);
M
Minghao Li 已提交
51

M
Minghao Li 已提交
52 53 54 55 56 57
// for debug -------------------
void voteGrantedPrint(SVotesGranted *pObj);
void voteGrantedPrint2(char *s, SVotesGranted *pObj);
void voteGrantedLog(SVotesGranted *pObj);
void voteGrantedLog2(char *s, SVotesGranted *pObj);

M
Minghao Li 已提交
58
// SVotesRespond -----------------------------
M
Minghao Li 已提交
59 60 61 62 63 64 65 66 67
typedef struct SVotesRespond {
  SRaftId (*replicas)[TSDB_MAX_REPLICA];
  bool       isRespond[TSDB_MAX_REPLICA];
  int32_t    replicaNum;
  SyncTerm   term;
  SSyncNode *pSyncNode;
} SVotesRespond;

SVotesRespond *votesRespondCreate(SSyncNode *pSyncNode);
M
Minghao Li 已提交
68
void           votesRespondDestory(SVotesRespond *pVotesRespond);
M
Minghao Li 已提交
69
void           votesRespondUpdate(SVotesRespond *pVotesRespond, SSyncNode *pSyncNode);
M
Minghao Li 已提交
70 71
bool           votesResponded(SVotesRespond *pVotesRespond, const SRaftId *pRaftId);
void           votesRespondAdd(SVotesRespond *pVotesRespond, const SyncRequestVoteReply *pMsg);
M
Minghao Li 已提交
72
void           votesRespondReset(SVotesRespond *pVotesRespond, SyncTerm term);
M
Minghao Li 已提交
73 74
cJSON *        votesRespond2Json(SVotesRespond *pVotesRespond);
char *         votesRespond2Str(SVotesRespond *pVotesRespond);
M
syncInt  
Minghao Li 已提交
75

M
Minghao Li 已提交
76 77 78 79 80 81
// for debug -------------------
void votesRespondPrint(SVotesRespond *pObj);
void votesRespondPrint2(char *s, SVotesRespond *pObj);
void votesRespondLog(SVotesRespond *pObj);
void votesRespondLog2(char *s, SVotesRespond *pObj);

M
Minghao Li 已提交
82 83 84 85 86
#ifdef __cplusplus
}
#endif

#endif /*_TD_LIBS_SYNC_VOTG_MGR_H*/