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

taosdata / TDengine / #3621

22 Feb 2025 11:44AM UTC coverage: 2.037% (-61.5%) from 63.573%
#3621

push

travis-ci

web-flow
Merge pull request #29874 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

4357 of 287032 branches covered (1.52%)

Branch coverage included in aggregate %.

0 of 174 new or added lines in 18 files covered. (0.0%)

213359 existing lines in 469 files now uncovered.

7260 of 283369 relevant lines covered (2.56%)

23737.72 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

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

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

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

UNCOV
58
    ret = syncNodeGetLastIndexTerm(pNode, &pMsg->lastLogIndex, &pMsg->lastLogTerm);
×
UNCOV
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

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

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

UNCOV
79
  sNInfo(pSyncNode, "begin election");
×
UNCOV
80
  pSyncNode->electNum++;
×
81

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

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

92
  // start election
UNCOV
93
  raftStoreNextTerm(pSyncNode);
×
UNCOV
94
  raftStoreClearVote(pSyncNode);
×
95

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

UNCOV
101
  if (voteGrantedMajority(pSyncNode->pVotesGranted)) {
×
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

UNCOV
113
  if (pSyncNode->replicaNum == 1) {
×
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

UNCOV
125
  ret = syncNodeRequestVotePeers(pSyncNode);
×
UNCOV
126
  if (ret != 0) return ret;
×
127

UNCOV
128
  syncNodeResetElectTimer(pSyncNode);
×
UNCOV
129
  return ret;
×
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

© 2026 Coveralls, Inc