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

taosdata / TDengine / #4991

17 Mar 2026 07:57AM UTC coverage: 69.756% (+0.4%) from 69.348%
#4991

push

travis-ci

web-flow
merge: from main to 3.0 branch #34807

14 of 16 new or added lines in 5 files covered. (87.5%)

3928 existing lines in 138 files now uncovered.

192146 of 275455 relevant lines covered (69.76%)

137208686.18 hits per line

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

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

42
  int64_t rootId = tGenIdPI64();
530,279✔
43

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

55
    SyncRequestVote* pMsg = rpcMsg.pCont;
994,312✔
56
    pMsg->srcId = pNode->myRaftId;
994,312✔
57
    pMsg->destId = pNode->peersId[i];
994,312✔
58
    pMsg->term = raftStoreGetTerm(pNode);
994,312✔
59

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

66
    TRACE_SET_MSGID(&(rpcMsg.info.traceId), tGenIdPI64());
994,312✔
67
    TRACE_SET_ROOTID(&(rpcMsg.info.traceId), rootId);
994,312✔
68
    sInfo("vgId:%d, send request-vote msg to peerId:0x%" PRIx64 ", lastLogIndex:%" PRId64 ", lastLogTerm:%" PRId64
994,312✔
69
          ", QID:0x%" PRIx64 ":0x%" PRIx64,
70
          pNode->vgId, pNode->peersId[i].addr, pMsg->lastLogIndex, pMsg->lastLogTerm,
71
          (uint64_t)rpcMsg.info.traceId.rootId, (uint64_t)rpcMsg.info.traceId.msgId);
72
    ret = syncNodeSendMsgById(&pNode->peersId[i], pNode, &rpcMsg);
994,312✔
73
    if (ret < 0) {
994,312✔
NEW
74
      sError("vgId:%d, failed to send msg to peerId:0x%" PRIx64, pNode->vgId, pNode->peersId[i].addr);
×
75
      continue;
×
76
    }
77
  }
78
  return 0;
530,279✔
79
}
80

81
int32_t syncNodeElect(SSyncNode* pSyncNode) {
531,434✔
82
  if (pSyncNode->fsmState == SYNC_FSM_STATE_INCOMPLETE) {
531,434✔
83
    sNError(pSyncNode, "skip leader election due to incomplete fsm state");
×
84
    return TSDB_CODE_SYN_WRONG_FSM_STATE;
×
85
  }
86

87
  sNInfo(pSyncNode, "begin election");
531,434✔
88
  pSyncNode->electNum++;
531,434✔
89

90
  int32_t ret = 0;
531,434✔
91
  if (pSyncNode->state == TAOS_SYNC_STATE_FOLLOWER) {
531,434✔
92
    syncNodeFollower2Candidate(pSyncNode);
492,898✔
93
  }
94

95
  if (pSyncNode->state != TAOS_SYNC_STATE_CANDIDATE) {
531,434✔
96
    sNError(pSyncNode, "not candidate, can not elect");
1,155✔
97
    return TSDB_CODE_SYN_WRONG_SYNC_STATE;
1,155✔
98
  }
99

100
  // start election
101
  raftStoreNextTerm(pSyncNode);
530,279✔
102
  raftStoreClearVote(pSyncNode);
530,279✔
103

104
  SyncTerm currentTerm = raftStoreGetTerm(pSyncNode);
530,279✔
105
  voteGrantedReset(pSyncNode->pVotesGranted, currentTerm);
530,279✔
106
  votesRespondReset(pSyncNode->pVotesRespond, currentTerm);
530,279✔
107
  syncNodeVoteForSelf(pSyncNode, currentTerm);
530,279✔
108

109
  if (voteGrantedMajority(pSyncNode->pVotesGranted)) {
530,279✔
110
    // only myself, to leader
111
    if (pSyncNode->pVotesGranted->toLeader) {
×
112
      ret = TSDB_CODE_SYN_INTERNAL_ERROR;
×
113
      sError("vgId:%d, failed to elect since already be to leader", pSyncNode->vgId);
×
114
      return ret;
×
115
    }
116
    syncNodeCandidate2Leader(pSyncNode);
×
117
    pSyncNode->pVotesGranted->toLeader = true;
×
118
    return ret;
×
119
  }
120

121
  if (pSyncNode->replicaNum == 1) {
530,279✔
122
    // only myself, to leader
123
    voteGrantedUpdate(pSyncNode->pVotesGranted, pSyncNode);
×
124
    votesRespondUpdate(pSyncNode->pVotesRespond, pSyncNode);
×
125

126
    pSyncNode->quorum = syncUtilQuorum(pSyncNode->raftCfg.cfg.replicaNum);
×
127

128
    syncNodeCandidate2Leader(pSyncNode);
×
129
    pSyncNode->pVotesGranted->toLeader = true;
×
130
    return ret;
×
131
  }
132

133
  ret = syncNodeRequestVotePeers(pSyncNode);
530,279✔
134
  if (ret != 0) return ret;
530,279✔
135

136
  syncNodeResetElectTimer(pSyncNode);
530,279✔
137
  return ret;
530,279✔
138
}
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