• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #3798

31 Mar 2025 10:39AM UTC coverage: 9.424% (-20.9%) from 30.372%
#3798

push

travis-ci

happyguoxy
test:add test cases

21549 of 307601 branches covered (7.01%)

Branch coverage included in aggregate %.

36084 of 303967 relevant lines covered (11.87%)

58620.7 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/source/libs/sync/src/syncVoteMgr.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * This program is free software: you can use, redistribute, and/or modify
5
 * it under the terms of the GNU Affero General Public License, version 3
6
 * or later ("AGPL"), as published by the Free Software Foundation.
7
 *
8
 * This program is distributed in the hope that it will be useful, but WITHOUT
9
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
10
 * FITNESS FOR A PARTICULAR PURPOSE.
11
 *
12
 * You should have received a copy of the GNU Affero General Public License
13
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14
 */
15

16
#define _DEFAULT_SOURCE
17
#include "syncVoteMgr.h"
18
#include "syncMessage.h"
19
#include "syncUtil.h"
20

21
static void voteGrantedClearVotes(SVotesGranted *pVotesGranted) {
×
22
  (void)memset(pVotesGranted->isGranted, 0, sizeof(pVotesGranted->isGranted));
×
23
  pVotesGranted->votes = 0;
×
24
}
×
25

26
SVotesGranted *voteGrantedCreate(SSyncNode *pNode) {
×
27
  SVotesGranted *pVotesGranted = taosMemoryCalloc(1, sizeof(SVotesGranted));
×
28
  if (pVotesGranted == NULL) {
×
29
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
30
    return NULL;
×
31
  }
32

33
  pVotesGranted->replicas = (void*)&pNode->replicasId;
×
34
  pVotesGranted->replicaNum = pNode->replicaNum;
×
35
  voteGrantedClearVotes(pVotesGranted);
×
36

37
  pVotesGranted->term = 0;
×
38
  pVotesGranted->quorum = pNode->quorum;
×
39
  pVotesGranted->toLeader = false;
×
40
  pVotesGranted->pNode = pNode;
×
41

42
  return pVotesGranted;
×
43
}
44

45
void voteGrantedDestroy(SVotesGranted *pVotesGranted) {
×
46
  if (pVotesGranted != NULL) {
×
47
    taosMemoryFree(pVotesGranted);
×
48
  }
49
}
×
50

51
void voteGrantedUpdate(SVotesGranted *pVotesGranted, SSyncNode *pNode) {
×
52
  pVotesGranted->replicas = (void*)&pNode->replicasId;
×
53
  pVotesGranted->replicaNum = pNode->replicaNum;
×
54
  voteGrantedClearVotes(pVotesGranted);
×
55

56
  pVotesGranted->term = 0;
×
57
  pVotesGranted->quorum = pNode->quorum;
×
58
  pVotesGranted->toLeader = false;
×
59
  pVotesGranted->pNode = pNode;
×
60
}
×
61

62
bool voteGrantedMajority(SVotesGranted *pVotesGranted) { return pVotesGranted->votes >= pVotesGranted->quorum; }
×
63

64
void voteGrantedVote(SVotesGranted *pVotesGranted, SyncRequestVoteReply *pMsg) {
×
65
  if (!pMsg->voteGranted) {
×
66
    sNFatal(pVotesGranted->pNode, "vote granted should be true");
×
67
    return;
×
68
  }
69

70
  if (pMsg->term != pVotesGranted->term) {
×
71
    sNTrace(pVotesGranted->pNode, "vote grant term:%" PRId64 " not matched with msg term:%" PRId64, pVotesGranted->term,
×
72
            pMsg->term);
73
    return;
×
74
  }
75

76
  if (!syncUtilSameId(&pVotesGranted->pNode->myRaftId, &pMsg->destId)) {
×
77
    sNFatal(pVotesGranted->pNode, "vote granted raftId not matched with msg");
×
78
    return;
×
79
  }
80

81
  int32_t j = -1;
×
82
  for (int32_t i = 0; i < pVotesGranted->replicaNum; ++i) {
×
83
    if (syncUtilSameId(&((*(pVotesGranted->replicas))[i]), &(pMsg->srcId))) {
×
84
      j = i;
×
85
      break;
×
86
    }
87
  }
88
  if ((j == -1) || !(j >= 0 && j < pVotesGranted->replicaNum)) {
×
89
    sNFatal(pVotesGranted->pNode, "invalid msg srcId, index:%d", j);
×
90
    return;
×
91
  }
92

93
  if (pVotesGranted->isGranted[j] != true) {
×
94
    ++(pVotesGranted->votes);
×
95
    pVotesGranted->isGranted[j] = true;
×
96
  }
97

98
  if (pVotesGranted->votes > pVotesGranted->replicaNum) {
×
99
    sNFatal(pVotesGranted->pNode, "votes:%d not matched with replicaNum:%d", pVotesGranted->votes,
×
100
            pVotesGranted->replicaNum);
101
    return;
×
102
  }
103
}
104

105
void voteGrantedReset(SVotesGranted *pVotesGranted, SyncTerm term) {
×
106
  pVotesGranted->term = term;
×
107
  voteGrantedClearVotes(pVotesGranted);
×
108
  pVotesGranted->toLeader = false;
×
109
}
×
110

111
SVotesRespond *votesRespondCreate(SSyncNode *pNode) {
×
112
  SVotesRespond *pVotesRespond = taosMemoryCalloc(1, sizeof(SVotesRespond));
×
113
  if (pVotesRespond == NULL) {
×
114
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
115
    return NULL;
×
116
  }
117

118
  pVotesRespond->replicas = (void*)&pNode->replicasId;
×
119
  pVotesRespond->replicaNum = pNode->replicaNum;
×
120
  pVotesRespond->term = 0;
×
121
  pVotesRespond->pNode = pNode;
×
122

123
  return pVotesRespond;
×
124
}
125

126
void votesRespondDestory(SVotesRespond *pVotesRespond) {
×
127
  if (pVotesRespond != NULL) {
×
128
    taosMemoryFree(pVotesRespond);
×
129
  }
130
}
×
131

132
void votesRespondUpdate(SVotesRespond *pVotesRespond, SSyncNode *pNode) {
×
133
  pVotesRespond->replicas = (void*)&pNode->replicasId;
×
134
  pVotesRespond->replicaNum = pNode->replicaNum;
×
135
  pVotesRespond->term = 0;
×
136
  pVotesRespond->pNode = pNode;
×
137
}
×
138

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

150
void votesRespondAdd(SVotesRespond *pVotesRespond, const SyncRequestVoteReply *pMsg) {
×
151
  if (pVotesRespond->term != pMsg->term) {
×
152
    sNTrace(pVotesRespond->pNode, "vote respond add error");
×
153
    return;
×
154
  }
155

156
  for (int32_t i = 0; i < pVotesRespond->replicaNum; ++i) {
×
157
    if (syncUtilSameId(&((*(pVotesRespond->replicas))[i]), &pMsg->srcId)) {
×
158
      pVotesRespond->isRespond[i] = true;
×
159
      return;
×
160
    }
161
  }
162

163
  sNFatal(pVotesRespond->pNode, "votes respond not found");
×
164
}
165

166
void votesRespondReset(SVotesRespond *pVotesRespond, SyncTerm term) {
×
167
  pVotesRespond->term = term;
×
168
  (void)memset(pVotesRespond->isRespond, 0, sizeof(pVotesRespond->isRespond));
×
169
}
×
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc