syncVoteMgr.c 5.1 KB
Newer Older
M
Minghao Li 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * 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/>.
 */

S
Shengliang Guan 已提交
16
#define _DEFAULT_SOURCE
M
Minghao Li 已提交
17
#include "syncVoteMgr.h"
M
Minghao Li 已提交
18 19 20 21 22 23
#include "syncUtil.h"

static void voteGrantedClearVotes(SVotesGranted *pVotesGranted) {
  memset(pVotesGranted->isGranted, 0, sizeof(pVotesGranted->isGranted));
  pVotesGranted->votes = 0;
}
M
Minghao Li 已提交
24 25

SVotesGranted *voteGrantedCreate(SSyncNode *pSyncNode) {
26
  SVotesGranted *pVotesGranted = taosMemoryCalloc(1, sizeof(SVotesGranted));
27 28 29 30
  if (pVotesGranted == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
M
Minghao Li 已提交
31

M
Minghao Li 已提交
32 33 34 35
  pVotesGranted->replicas = &(pSyncNode->replicasId);
  pVotesGranted->replicaNum = pSyncNode->replicaNum;
  voteGrantedClearVotes(pVotesGranted);

M
Minghao Li 已提交
36
  pVotesGranted->term = 0;
M
Minghao Li 已提交
37
  pVotesGranted->quorum = pSyncNode->quorum;
M
Minghao Li 已提交
38 39 40 41 42 43 44 45
  pVotesGranted->toLeader = false;
  pVotesGranted->pSyncNode = pSyncNode;

  return pVotesGranted;
}

void voteGrantedDestroy(SVotesGranted *pVotesGranted) {
  if (pVotesGranted != NULL) {
wafwerar's avatar
wafwerar 已提交
46
    taosMemoryFree(pVotesGranted);
M
Minghao Li 已提交
47 48 49
  }
}

M
Minghao Li 已提交
50 51 52 53 54 55 56 57 58 59 60
void voteGrantedUpdate(SVotesGranted *pVotesGranted, SSyncNode *pSyncNode) {
  pVotesGranted->replicas = &(pSyncNode->replicasId);
  pVotesGranted->replicaNum = pSyncNode->replicaNum;
  voteGrantedClearVotes(pVotesGranted);

  pVotesGranted->term = 0;
  pVotesGranted->quorum = pSyncNode->quorum;
  pVotesGranted->toLeader = false;
  pVotesGranted->pSyncNode = pSyncNode;
}

61
bool voteGrantedMajority(SVotesGranted *pVotesGranted) { return pVotesGranted->votes >= pVotesGranted->quorum; }
M
Minghao Li 已提交
62 63

void voteGrantedVote(SVotesGranted *pVotesGranted, SyncRequestVoteReply *pMsg) {
64
  if (!pMsg->voteGranted) {
65
    sNFatal(pVotesGranted->pSyncNode, "vote granted should be true");
66 67
    return;
  }
M
Minghao Li 已提交
68 69

  if (pMsg->term != pVotesGranted->term) {
70 71
    sNTrace(pVotesGranted->pSyncNode, "vote grant term:%" PRId64 " not matched with msg term:%" PRId64,
            pVotesGranted->term, pMsg->term);
M
Minghao Li 已提交
72 73 74
    return;
  }

75
  if (!syncUtilSameId(&pVotesGranted->pSyncNode->myRaftId, &pMsg->destId)) {
76
    sNFatal(pVotesGranted->pSyncNode, "vote granted raftId not matched with msg");
77 78
    return;
  }
M
Minghao Li 已提交
79

S
Shengliang Guan 已提交
80 81
  int32_t j = -1;
  for (int32_t i = 0; i < pVotesGranted->replicaNum; ++i) {
M
Minghao Li 已提交
82 83 84 85 86
    if (syncUtilSameId(&((*(pVotesGranted->replicas))[i]), &(pMsg->srcId))) {
      j = i;
      break;
    }
  }
87 88
  if ((j == -1) || !(j >= 0 && j < pVotesGranted->replicaNum)) {
    sNFatal(pVotesGranted->pSyncNode, "invalid msg srcId, index:%d", j);
89 90
    return;
  }
M
Minghao Li 已提交
91 92 93 94 95

  if (pVotesGranted->isGranted[j] != true) {
    ++(pVotesGranted->votes);
    pVotesGranted->isGranted[j] = true;
  }
96

97 98
  if (pVotesGranted->votes > pVotesGranted->replicaNum) {
    sNFatal(pVotesGranted->pSyncNode, "votes:%d not matched with replicaNum:%d", pVotesGranted->votes,
99 100 101
            pVotesGranted->replicaNum);
    return;
  }
M
Minghao Li 已提交
102 103 104 105
}

void voteGrantedReset(SVotesGranted *pVotesGranted, SyncTerm term) {
  pVotesGranted->term = term;
M
Minghao Li 已提交
106
  voteGrantedClearVotes(pVotesGranted);
M
Minghao Li 已提交
107 108 109 110
  pVotesGranted->toLeader = false;
}

SVotesRespond *votesRespondCreate(SSyncNode *pSyncNode) {
111 112 113 114 115
  SVotesRespond *pVotesRespond = taosMemoryCalloc(1, sizeof(SVotesRespond));
  if (pVotesRespond == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
M
Minghao Li 已提交
116 117 118 119 120 121 122 123 124

  pVotesRespond->replicas = &(pSyncNode->replicasId);
  pVotesRespond->replicaNum = pSyncNode->replicaNum;
  pVotesRespond->term = 0;
  pVotesRespond->pSyncNode = pSyncNode;

  return pVotesRespond;
}

M
Minghao Li 已提交
125 126
void votesRespondDestory(SVotesRespond *pVotesRespond) {
  if (pVotesRespond != NULL) {
wafwerar's avatar
wafwerar 已提交
127
    taosMemoryFree(pVotesRespond);
M
Minghao Li 已提交
128 129 130
  }
}

M
Minghao Li 已提交
131 132 133 134 135 136 137
void votesRespondUpdate(SVotesRespond *pVotesRespond, SSyncNode *pSyncNode) {
  pVotesRespond->replicas = &(pSyncNode->replicasId);
  pVotesRespond->replicaNum = pSyncNode->replicaNum;
  pVotesRespond->term = 0;
  pVotesRespond->pSyncNode = pSyncNode;
}

M
Minghao Li 已提交
138 139
bool votesResponded(SVotesRespond *pVotesRespond, const SRaftId *pRaftId) {
  bool ret = false;
S
Shengliang Guan 已提交
140
  for (int32_t i = 0; i < pVotesRespond->replicaNum; ++i) {
M
Minghao Li 已提交
141 142 143 144 145 146 147 148 149
    if (syncUtilSameId(&(*pVotesRespond->replicas)[i], pRaftId) && pVotesRespond->isRespond[i]) {
      ret = true;
      break;
    }
  }
  return ret;
}

void votesRespondAdd(SVotesRespond *pVotesRespond, const SyncRequestVoteReply *pMsg) {
M
Minghao Li 已提交
150
  if (pVotesRespond->term != pMsg->term) {
S
Shengliang Guan 已提交
151
    sNTrace(pVotesRespond->pSyncNode, "vote respond add error");
M
Minghao Li 已提交
152 153 154
    return;
  }

S
Shengliang Guan 已提交
155
  for (int32_t i = 0; i < pVotesRespond->replicaNum; ++i) {
M
Minghao Li 已提交
156
    if (syncUtilSameId(&((*(pVotesRespond->replicas))[i]), &pMsg->srcId)) {
M
Minghao Li 已提交
157 158 159 160
      pVotesRespond->isRespond[i] = true;
      return;
    }
  }
161 162

  sNFatal(pVotesRespond->pSyncNode, "votes respond not found");
M
Minghao Li 已提交
163 164
}

M
Minghao Li 已提交
165
void votesRespondReset(SVotesRespond *pVotesRespond, SyncTerm term) {
M
Minghao Li 已提交
166
  pVotesRespond->term = term;
M
Minghao Li 已提交
167
  memset(pVotesRespond->isRespond, 0, sizeof(pVotesRespond->isRespond));
M
Minghao Li 已提交
168
}