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"
18
#include "syncMessage.h"
M
Minghao Li 已提交
19 20 21 22 23 24
#include "syncUtil.h"

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

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

C
cadem 已提交
33
  pVotesGranted->replicas = (void*)&pNode->replicasId;
S
Shengliang Guan 已提交
34
  pVotesGranted->replicaNum = pNode->replicaNum;
M
Minghao Li 已提交
35 36
  voteGrantedClearVotes(pVotesGranted);

M
Minghao Li 已提交
37
  pVotesGranted->term = 0;
S
Shengliang Guan 已提交
38
  pVotesGranted->quorum = pNode->quorum;
M
Minghao Li 已提交
39
  pVotesGranted->toLeader = false;
S
Shengliang Guan 已提交
40
  pVotesGranted->pNode = pNode;
M
Minghao Li 已提交
41 42 43 44 45 46

  return pVotesGranted;
}

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

S
Shengliang Guan 已提交
51
void voteGrantedUpdate(SVotesGranted *pVotesGranted, SSyncNode *pNode) {
C
cadem 已提交
52
  pVotesGranted->replicas = (void*)&pNode->replicasId;
S
Shengliang Guan 已提交
53
  pVotesGranted->replicaNum = pNode->replicaNum;
M
Minghao Li 已提交
54 55 56
  voteGrantedClearVotes(pVotesGranted);

  pVotesGranted->term = 0;
S
Shengliang Guan 已提交
57
  pVotesGranted->quorum = pNode->quorum;
M
Minghao Li 已提交
58
  pVotesGranted->toLeader = false;
S
Shengliang Guan 已提交
59
  pVotesGranted->pNode = pNode;
M
Minghao Li 已提交
60 61
}

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

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

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

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

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

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

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

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

S
Shengliang Guan 已提交
111
SVotesRespond *votesRespondCreate(SSyncNode *pNode) {
112 113 114 115 116
  SVotesRespond *pVotesRespond = taosMemoryCalloc(1, sizeof(SVotesRespond));
  if (pVotesRespond == NULL) {
    terrno = TSDB_CODE_OUT_OF_MEMORY;
    return NULL;
  }
M
Minghao Li 已提交
117

C
cadem 已提交
118
  pVotesRespond->replicas = (void*)&pNode->replicasId;
S
Shengliang Guan 已提交
119
  pVotesRespond->replicaNum = pNode->replicaNum;
M
Minghao Li 已提交
120
  pVotesRespond->term = 0;
S
Shengliang Guan 已提交
121
  pVotesRespond->pNode = pNode;
M
Minghao Li 已提交
122 123 124 125

  return pVotesRespond;
}

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

S
Shengliang Guan 已提交
132
void votesRespondUpdate(SVotesRespond *pVotesRespond, SSyncNode *pNode) {
C
cadem 已提交
133
  pVotesRespond->replicas = (void*)&pNode->replicasId;
S
Shengliang Guan 已提交
134
  pVotesRespond->replicaNum = pNode->replicaNum;
M
Minghao Li 已提交
135
  pVotesRespond->term = 0;
S
Shengliang Guan 已提交
136
  pVotesRespond->pNode = pNode;
M
Minghao Li 已提交
137 138
}

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

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

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

S
Shengliang Guan 已提交
163
  sNFatal(pVotesRespond->pNode, "votes respond not found");
M
Minghao Li 已提交
164 165
}

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