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

taosdata / TDengine / #4720

08 Sep 2025 08:43AM UTC coverage: 58.139% (-0.6%) from 58.762%
#4720

push

travis-ci

web-flow
Merge pull request #32881 from taosdata/enh/add-new-windows-ci

fix(ci): update workflow reference to use new Windows CI YAML

133181 of 292179 branches covered (45.58%)

Branch coverage included in aggregate %.

201691 of 283811 relevant lines covered (71.07%)

5442780.71 hits per line

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

50.96
/source/libs/sync/src/syncElection.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 "syncElection.h"
18
#include "syncMessage.h"
19
#include "syncRaftCfg.h"
20
#include "syncRaftStore.h"
21
#include "syncUtil.h"
22
#include "syncVoteMgr.h"
23

24
// TLA+ Spec
25
// RequestVote(i, j) ==
26
//    /\ state[i] = Candidate
27
//    /\ j \notin votesResponded[i]
28
//    /\ Send([mtype         |-> RequestVoteRequest,
29
//             mterm         |-> currentTerm[i],
30
//             mlastLogTerm  |-> LastTerm(log[i]),
31
//             mlastLogIndex |-> Len(log[i]),
32
//             msource       |-> i,
33
//             mdest         |-> j])
34
//    /\ UNCHANGED <<serverVars, candidateVars, leaderVars, logVars>>
35

36
static int32_t syncNodeRequestVotePeers(SSyncNode* pNode) {
1,361✔
37
  if (pNode->state != TAOS_SYNC_STATE_CANDIDATE) {
1,361!
38
    sNTrace(pNode, "not candidate, stop elect");
×
39
    return 0;
×
40
  }
41

42
  int32_t ret = 0;
1,361✔
43
  for (int i = 0; i < pNode->peersNum; ++i) {
4,018✔
44
    if(pNode->peersNodeInfo[i].nodeRole == TAOS_SYNC_ROLE_LEARNER) continue;
2,657✔
45
    
46
    SRpcMsg rpcMsg = {0};
2,585✔
47
    ret = syncBuildRequestVote(&rpcMsg, pNode->vgId);
2,585✔
48
    if (ret < 0) {
2,585!
49
      sError("vgId:%d, failed to build request-vote msg since %s", pNode->vgId, terrstr());
×
50
      continue;
×
51
    }
52

53
    SyncRequestVote* pMsg = rpcMsg.pCont;
2,585✔
54
    pMsg->srcId = pNode->myRaftId;
2,585✔
55
    pMsg->destId = pNode->peersId[i];
2,585✔
56
    pMsg->term = raftStoreGetTerm(pNode);
2,585✔
57

58
    ret = syncNodeGetLastIndexTerm(pNode, &pMsg->lastLogIndex, &pMsg->lastLogTerm);
2,585✔
59
    if (ret < 0) {
2,585!
60
      sError("vgId:%d, failed to get index and term of last log since %s", pNode->vgId, terrstr());
×
61
      continue;
×
62
    }
63

64
    ret = syncNodeSendMsgById(&pNode->peersId[i], pNode, &rpcMsg);
2,585✔
65
    if (ret < 0) {
2,585!
66
      sError("vgId:%d, failed to send msg to peerId:%" PRId64, pNode->vgId, pNode->peersId[i].addr);
×
67
      continue;
×
68
    }
69
  }
70
  return 0;
1,361✔
71
}
72

73
int32_t syncNodeElect(SSyncNode* pSyncNode) {
1,361✔
74
  if (pSyncNode->fsmState == SYNC_FSM_STATE_INCOMPLETE) {
1,361!
75
    sNError(pSyncNode, "skip leader election due to incomplete fsm state");
×
76
    return TSDB_CODE_SYN_WRONG_FSM_STATE;
×
77
  }
78

79
  sNInfo(pSyncNode, "begin election");
1,361!
80
  pSyncNode->electNum++;
1,361✔
81

82
  int32_t ret = 0;
1,361✔
83
  if (pSyncNode->state == TAOS_SYNC_STATE_FOLLOWER) {
1,361✔
84
    syncNodeFollower2Candidate(pSyncNode);
1,307✔
85
  }
86

87
  if (pSyncNode->state != TAOS_SYNC_STATE_CANDIDATE) {
1,361!
88
    sNError(pSyncNode, "not candidate, can not elect");
×
89
    return TSDB_CODE_SYN_WRONG_SYNC_STATE;
×
90
  }
91

92
  // start election
93
  raftStoreNextTerm(pSyncNode);
1,361✔
94
  raftStoreClearVote(pSyncNode);
1,361✔
95

96
  SyncTerm currentTerm = raftStoreGetTerm(pSyncNode);
1,361✔
97
  voteGrantedReset(pSyncNode->pVotesGranted, currentTerm);
1,361✔
98
  votesRespondReset(pSyncNode->pVotesRespond, currentTerm);
1,361✔
99
  syncNodeVoteForSelf(pSyncNode, currentTerm);
1,361✔
100

101
  if (voteGrantedMajority(pSyncNode->pVotesGranted)) {
1,361!
102
    // only myself, to leader
103
    if (pSyncNode->pVotesGranted->toLeader) {
×
104
      ret = TSDB_CODE_SYN_INTERNAL_ERROR;
×
105
      sError("vgId:%d, failed to elect since already be to leader", pSyncNode->vgId);
×
106
      return ret;
×
107
    }
108
    syncNodeCandidate2Leader(pSyncNode);
×
109
    pSyncNode->pVotesGranted->toLeader = true;
×
110
    return ret;
×
111
  }
112

113
  if (pSyncNode->replicaNum == 1) {
1,361!
114
    // only myself, to leader
115
    voteGrantedUpdate(pSyncNode->pVotesGranted, pSyncNode);
×
116
    votesRespondUpdate(pSyncNode->pVotesRespond, pSyncNode);
×
117

118
    pSyncNode->quorum = syncUtilQuorum(pSyncNode->raftCfg.cfg.replicaNum);
×
119

120
    syncNodeCandidate2Leader(pSyncNode);
×
121
    pSyncNode->pVotesGranted->toLeader = true;
×
122
    return ret;
×
123
  }
124

125
  ret = syncNodeRequestVotePeers(pSyncNode);
1,361✔
126
  if (ret != 0) return ret;
1,361!
127

128
  syncNodeResetElectTimer(pSyncNode);
1,361✔
129
  return ret;
1,361✔
130
}
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

© 2025 Coveralls, Inc