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

taosdata / TDengine / #3526

10 Nov 2024 03:50AM UTC coverage: 60.225% (-0.6%) from 60.818%
#3526

push

travis-ci

web-flow
Merge pull request #28709 from taosdata/main

merge: from main to 3.0 branch

117031 of 249004 branches covered (47.0%)

Branch coverage included in aggregate %.

130 of 169 new or added lines in 23 files covered. (76.92%)

4149 existing lines in 176 files now uncovered.

197577 of 273386 relevant lines covered (72.27%)

5840219.36 hits per line

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

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

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

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

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

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

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

79
  sNInfo(pSyncNode, "begin election");
1,268!
80
  pSyncNode->electNum++;
1,268✔
81

82
  int32_t ret = 0;
1,268✔
83
  if (pSyncNode->state == TAOS_SYNC_STATE_FOLLOWER) {
1,268✔
84
    syncNodeFollower2Candidate(pSyncNode);
1,195✔
85
  }
86

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

92
  // start election
93
  raftStoreNextTerm(pSyncNode);
1,268✔
94
  raftStoreClearVote(pSyncNode);
1,268✔
95

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

101
  if (voteGrantedMajority(pSyncNode->pVotesGranted)) {
1,268!
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

113
  if (pSyncNode->replicaNum == 1) {
1,268!
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

125
  ret = syncNodeRequestVotePeers(pSyncNode);
1,268✔
126
  if (ret != 0) return ret;
1,268!
127

128
  syncNodeResetElectTimer(pSyncNode);
1,268✔
129
  return ret;
1,268✔
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