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

taosdata / TDengine / #5048

10 May 2026 03:11AM UTC coverage: 73.222% (+0.07%) from 73.152%
#5048

push

travis-ci

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

353 of 452 new or added lines in 9 files covered. (78.1%)

587 existing lines in 140 files now uncovered.

278189 of 379928 relevant lines covered (73.22%)

135206397.85 hits per line

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

87.27
/source/libs/sync/src/syncRequestVote.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 "syncRequestVote.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
// HandleRequestVoteRequest(i, j, m) ==
26
//    LET logOk == \/ m.mlastLogTerm > LastTerm(log[i])
27
//                 \/ /\ m.mlastLogTerm = LastTerm(log[i])
28
//                    /\ m.mlastLogIndex >= Len(log[i])
29
//        grant == /\ m.mterm = currentTerm[i]
30
//                 /\ logOk
31
//                 /\ votedFor[i] \in {Nil, j}
32
//    IN /\ m.mterm <= currentTerm[i]
33
//       /\ \/ grant  /\ votedFor' = [votedFor EXCEPT ![i] = j]
34
//          \/ ~grant /\ UNCHANGED votedFor
35
//       /\ Reply([mtype        |-> RequestVoteResponse,
36
//                 mterm        |-> currentTerm[i],
37
//                 mvoteGranted |-> grant,
38
//                 \* mlog is used just for the `elections' history variable for
39
//                 \* the proof. It would not exist in a real implementation.
40
//                 mlog         |-> log[i],
41
//                 msource      |-> i,
42
//                 mdest        |-> j],
43
//                 m)
44
//       /\ UNCHANGED <<state, currentTerm, candidateVars, leaderVars, logVars>>
45
//
46

47
static bool syncNodeOnRequestVoteLogOK(SSyncNode* ths, SyncRequestVote* pMsg) {
1,021,091✔
48
  SyncTerm  myLastTerm = syncNodeGetLastTerm(ths);
1,021,091✔
49
  SyncIndex myLastIndex = syncNodeGetLastIndex(ths);
1,021,091✔
50

51
  if (myLastTerm == SYNC_TERM_INVALID) {
1,021,091✔
52
    sNWarn(ths,
×
53
           "logok:0, my last term invalid, {my-lterm:%" PRIu64 ", my-lindex:%" PRId64 ", recv-lterm:%" PRIu64
54
           ", recv-lindex:%" PRId64 ", recv-term:%" PRIu64 "}",
55
           myLastTerm, myLastIndex, pMsg->lastLogTerm, pMsg->lastLogIndex, pMsg->term);
56
    return false;
×
57
  }
58

59
  if (pMsg->lastLogTerm > myLastTerm) {
1,021,091✔
60
    sNInfo(ths,
68,283✔
61
           "logok:1, larger log term, {my-lterm:%" PRIu64 ", my-lindex:%" PRId64 ", recv-lterm:%" PRIu64
62
           ", recv-lindex:%" PRId64 ", recv-term:%" PRIu64 "}",
63
           myLastTerm, myLastIndex, pMsg->lastLogTerm, pMsg->lastLogIndex, pMsg->term);
64

65
    if (pMsg->lastLogIndex < ths->commitIndex) {
68,283✔
66
      sNWarn(ths,
×
67
             "logok:1, commit rollback required. {my-lterm:%" PRIu64 ", my-lindex:%" PRId64 ", recv-lterm:%" PRIu64
68
             ", recv-lindex:%" PRId64 ", recv-term:%" PRIu64 "}",
69
             myLastTerm, myLastIndex, pMsg->lastLogTerm, pMsg->lastLogIndex, pMsg->term);
70
    }
71
    return true;
68,283✔
72
  }
73

74
  if (pMsg->lastLogTerm == myLastTerm && pMsg->lastLogIndex >= myLastIndex) {
952,808✔
75
    sNInfo(ths,
859,232✔
76
           "logok:1, larger log index , {my-lterm:%" PRIu64 ", my-lindex:%" PRId64 ", recv-lterm:%" PRIu64
77
           ", recv-lindex:%" PRId64 ", recv-term:%" PRIu64 "}",
78
           myLastTerm, myLastIndex, pMsg->lastLogTerm, pMsg->lastLogIndex, pMsg->term);
79
    return true;
859,232✔
80
  }
81

82
  sNWarn(ths,
93,576✔
83
         "logok:0, {my-lterm:%" PRIu64 ", my-lindex:%" PRId64 ", recv-lterm:%" PRIu64 ", recv-lindex:%" PRId64
84
         ", recv-term:%" PRIu64 "}",
85
         myLastTerm, myLastIndex, pMsg->lastLogTerm, pMsg->lastLogIndex, pMsg->term);
86
  return false;
93,576✔
87
}
88

89
int32_t syncNodeOnRequestVote(SSyncNode* ths, const SRpcMsg* pRpcMsg) {
1,021,091✔
90
  int32_t          ret = 0;
1,021,091✔
91
  SyncRequestVote* pMsg = pRpcMsg->pCont;
1,021,091✔
92
  bool             resetElect = false;
1,021,091✔
93

94
  syncLogRecvRequestVote(ths, pMsg, -1, "", "recv", &pRpcMsg->info.traceId);
1,021,091✔
95

96
  // if already drop replica, do not process
97
  if (!syncNodeInRaftGroup(ths, &pMsg->srcId)) {
1,021,091✔
98
    syncLogRecvRequestVote(ths, pMsg, -1, "not in my config", "process", &pRpcMsg->info.traceId);
×
99

100
    TAOS_RETURN(TSDB_CODE_SYN_NOT_IN_RAFT_GROUP);
×
101
  }
102

103
  if (ths->state == TAOS_SYNC_STATE_LEARNER) {
1,021,091✔
NEW
104
    syncLogRecvRequestVote(ths, pMsg, -1, "I'm learner", "process", &pRpcMsg->info.traceId);
×
105

NEW
106
    TAOS_RETURN(TSDB_CODE_SYN_LEARNER_NO_VOTE);
×
107
  }
108

109
  bool logOK = syncNodeOnRequestVoteLogOK(ths, pMsg);
1,021,091✔
110
  // maybe update term
111
  if (pMsg->term > raftStoreGetTerm(ths)) {
1,021,091✔
112
    syncNodeStepDown(ths, pMsg->term, pMsg->srcId, "requestVote-1");
945,913✔
113
  }
114
  SyncTerm currentTerm = raftStoreGetTerm(ths);
1,021,091✔
115
  if (!(pMsg->term <= currentTerm)) return TSDB_CODE_SYN_INTERNAL_ERROR;
1,021,091✔
116

117
  bool hasVoted = raftStoreHasVoted(ths);
1,021,091✔
118
  bool grant =
1,021,091✔
119
      (pMsg->term == currentTerm) && logOK && ((!hasVoted) || syncUtilSameId(&ths->raftStore.voteFor, &pMsg->srcId));
1,021,091✔
120
  sInfo("vgId:%d, grant:%d, hasVoted:%d, voteFor:0x%" PRIx64 ", srcId:0x%" PRIx64 ", logOK:%d, msg term:%" PRId64
1,021,091✔
121
        ", current term:%" PRId64,
122
        ths->vgId, grant, hasVoted, ths->raftStore.voteFor.addr, pMsg->srcId.addr, logOK, pMsg->term, currentTerm);
123
  if (grant) {
1,021,091✔
124
    // maybe has already voted for pMsg->srcId
125
    // vote again, no harm
126
    raftStoreVote(ths, &(pMsg->srcId));
872,815✔
127

128
    // candidate ?
129
    syncNodeStepDown(ths, currentTerm, pMsg->srcId, "requestVote-2");
872,815✔
130

131
    // forbid elect for this round
132
    resetElect = true;
872,815✔
133
  }
134

135
  // send msg
136
  SRpcMsg rpcMsg = {0};
1,021,091✔
137

138
  TAOS_CHECK_RETURN(syncBuildRequestVoteReply(&rpcMsg, ths->vgId));
1,021,091✔
139

140
  SyncRequestVoteReply* pReply = rpcMsg.pCont;
1,021,091✔
141
  pReply->srcId = ths->myRaftId;
1,021,091✔
142
  pReply->destId = pMsg->srcId;
1,021,091✔
143
  pReply->term = currentTerm;
1,021,091✔
144
  pReply->voteGranted = grant;
1,021,091✔
145
  if (!(!grant || pMsg->term == pReply->term)) return TSDB_CODE_SYN_INTERNAL_ERROR;
1,021,091✔
146

147
  // trace log
148
  syncLogRecvRequestVote(ths, pMsg, pReply->voteGranted, "", "proceed", &pRpcMsg->info.traceId);
1,021,091✔
149

150
  rpcMsg.info.traceId = pRpcMsg->info.traceId;
1,021,091✔
151
  TRACE_SET_MSGID(&(rpcMsg.info.traceId), tGenIdPI64());
1,021,091✔
152
  syncLogSendRequestVoteReply(ths, pReply, "", &rpcMsg.info.traceId);
1,021,091✔
153
  TAOS_CHECK_RETURN(syncNodeSendMsgById(&pReply->destId, ths, &rpcMsg));
1,021,091✔
154

155
  if (resetElect) syncNodeResetElectTimer(ths);
1,021,091✔
156

157
  TAOS_RETURN(TSDB_CODE_SUCCESS);
1,021,091✔
158
}
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