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

taosdata / TDengine / #4868

26 Nov 2025 05:46AM UTC coverage: 64.629% (+0.2%) from 64.473%
#4868

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

770 of 945 new or added lines in 33 files covered. (81.48%)

3010 existing lines in 120 files now uncovered.

158425 of 245129 relevant lines covered (64.63%)

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

42
  int32_t ret = 0;
800,399✔
43
  for (int i = 0; i < pNode->peersNum; ++i) {
2,388,989✔
44
    if(pNode->peersNodeInfo[i].nodeRole == TAOS_SYNC_ROLE_LEARNER) continue;
1,588,590✔
45
    
46
    SRpcMsg rpcMsg = {0};
1,521,452✔
47
    ret = syncBuildRequestVote(&rpcMsg, pNode->vgId);
1,521,452✔
48
    if (ret < 0) {
1,521,452✔
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,521,452✔
54
    pMsg->srcId = pNode->myRaftId;
1,521,452✔
55
    pMsg->destId = pNode->peersId[i];
1,521,452✔
56
    pMsg->term = raftStoreGetTerm(pNode);
1,521,452✔
57

58
    ret = syncNodeGetLastIndexTerm(pNode, &pMsg->lastLogIndex, &pMsg->lastLogTerm);
1,521,452✔
59
    if (ret < 0) {
1,521,452✔
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,521,452✔
65
           pNode->vgId, pNode->peersId[i].addr, pMsg->lastLogIndex, pMsg->lastLogTerm);
66
    ret = syncNodeSendMsgById(&pNode->peersId[i], pNode, &rpcMsg);
1,521,452✔
67
    if (ret < 0) {
1,521,452✔
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;
800,399✔
73
}
74

75
int32_t syncNodeElect(SSyncNode* pSyncNode) {
802,775✔
76
  if (pSyncNode->fsmState == SYNC_FSM_STATE_INCOMPLETE) {
802,775✔
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");
802,775✔
82
  pSyncNode->electNum++;
802,775✔
83

84
  int32_t ret = 0;
802,775✔
85
  if (pSyncNode->state == TAOS_SYNC_STATE_FOLLOWER) {
802,775✔
86
    syncNodeFollower2Candidate(pSyncNode);
671,029✔
87
  }
88

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

94
  // start election
95
  raftStoreNextTerm(pSyncNode);
800,399✔
96
  raftStoreClearVote(pSyncNode);
800,399✔
97

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

103
  if (voteGrantedMajority(pSyncNode->pVotesGranted)) {
800,399✔
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) {
800,399✔
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);
800,399✔
128
  if (ret != 0) return ret;
800,399✔
129

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