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

taosdata / TDengine / #4867

26 Nov 2025 05:46AM UTC coverage: 64.473% (-0.03%) from 64.504%
#4867

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

765 of 945 new or added lines in 33 files covered. (80.95%)

3147 existing lines in 126 files now uncovered.

158042 of 245129 relevant lines covered (64.47%)

111495961.54 hits per line

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

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

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

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

58
    ret = syncNodeGetLastIndexTerm(pNode, &pMsg->lastLogIndex, &pMsg->lastLogTerm);
1,408,290✔
59
    if (ret < 0) {
1,408,290✔
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,
1,408,290✔
65
           pNode->vgId, pNode->peersId[i].addr, pMsg->lastLogIndex, pMsg->lastLogTerm);
66
    ret = syncNodeSendMsgById(&pNode->peersId[i], pNode, &rpcMsg);
1,408,290✔
67
    if (ret < 0) {
1,408,290✔
UNCOV
68
      sError("vgId:%d, failed to send msg to peerId:%" PRId64, pNode->vgId, pNode->peersId[i].addr);
×
UNCOV
69
      continue;
×
70
    }
71
  }
72
  return 0;
729,674✔
73
}
74

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

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

84
  int32_t ret = 0;
732,522✔
85
  if (pSyncNode->state == TAOS_SYNC_STATE_FOLLOWER) {
732,522✔
86
    syncNodeFollower2Candidate(pSyncNode);
634,094✔
87
  }
88

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

94
  // start election
95
  raftStoreNextTerm(pSyncNode);
729,674✔
96
  raftStoreClearVote(pSyncNode);
729,674✔
97

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

103
  if (voteGrantedMajority(pSyncNode->pVotesGranted)) {
729,674✔
104
    // only myself, to leader
UNCOV
105
    if (pSyncNode->pVotesGranted->toLeader) {
×
UNCOV
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);
×
UNCOV
111
    pSyncNode->pVotesGranted->toLeader = true;
×
112
    return ret;
×
113
  }
114

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

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

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

127
  ret = syncNodeRequestVotePeers(pSyncNode);
729,674✔
128
  if (ret != 0) return ret;
729,674✔
129

130
  syncNodeResetElectTimer(pSyncNode);
729,674✔
131
  return ret;
729,674✔
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