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

taosdata / TDengine / #4986

15 Mar 2026 08:32AM UTC coverage: 37.305% (-31.3%) from 68.601%
#4986

push

travis-ci

tomchon
test: keep docs and unit test

125478 of 336361 relevant lines covered (37.3%)

1134847.06 hits per line

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

0.0
/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) {
×
37
  if (pNode->state != TAOS_SYNC_STATE_CANDIDATE) {
×
38
    sNTrace(pNode, "not candidate, stop elect");
×
39
    return 0;
×
40
  }
41

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

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

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

64
    sInfo("vgId:%d, send request-vote msg to peerId:%" PRId64 ", lastLogIndex:%" PRId64 ", lastLogTerm:%" PRId64,
×
65
           pNode->vgId, pNode->peersId[i].addr, pMsg->lastLogIndex, pMsg->lastLogTerm);
66
    ret = syncNodeSendMsgById(&pNode->peersId[i], pNode, &rpcMsg);
×
67
    if (ret < 0) {
×
68
      sError("vgId:%d, failed to send msg to peerId:%" PRId64, pNode->vgId, pNode->peersId[i].addr);
×
69
      continue;
×
70
    }
71
  }
72
  return 0;
×
73
}
74

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

81
  sNInfo(pSyncNode, "begin election");
×
82
  pSyncNode->electNum++;
×
83

84
  int32_t ret = 0;
×
85
  if (pSyncNode->state == TAOS_SYNC_STATE_FOLLOWER) {
×
86
    syncNodeFollower2Candidate(pSyncNode);
×
87
  }
88

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

94
  // start election
95
  raftStoreNextTerm(pSyncNode);
×
96
  raftStoreClearVote(pSyncNode);
×
97

98
  SyncTerm currentTerm = raftStoreGetTerm(pSyncNode);
×
99
  voteGrantedReset(pSyncNode->pVotesGranted, currentTerm);
×
100
  votesRespondReset(pSyncNode->pVotesRespond, currentTerm);
×
101
  syncNodeVoteForSelf(pSyncNode, currentTerm);
×
102

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

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

120
    pSyncNode->quorum = syncUtilQuorum(pSyncNode->raftCfg.cfg.replicaNum);
×
121

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

127
  ret = syncNodeRequestVotePeers(pSyncNode);
×
128
  if (ret != 0) return ret;
×
129

130
  syncNodeResetElectTimer(pSyncNode);
×
131
  return ret;
×
132
}
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