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

taosdata / TDengine / #4870

26 Nov 2025 05:46AM UTC coverage: 64.545% (+0.006%) from 64.539%
#4870

push

travis-ci

guanshengliang
Merge branch '3.0' into cover/3.0

768 of 945 new or added lines in 33 files covered. (81.27%)

2982 existing lines in 119 files now uncovered.

158219 of 245129 relevant lines covered (64.55%)

112474797.36 hits per line

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

89.8
/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,024,347✔
48
  SyncTerm  myLastTerm = syncNodeGetLastTerm(ths);
1,024,347✔
49
  SyncIndex myLastIndex = syncNodeGetLastIndex(ths);
1,024,347✔
50

51
  if (myLastTerm == SYNC_TERM_INVALID) {
1,024,347✔
NEW
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);
UNCOV
56
    return false;
×
57
  }
58

59
  if (pMsg->lastLogTerm > myLastTerm) {
1,024,347✔
60
    sNInfo(ths,
67,202✔
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) {
67,202✔
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;
67,202✔
72
  }
73

74
  if (pMsg->lastLogTerm == myLastTerm && pMsg->lastLogIndex >= myLastIndex) {
957,145✔
75
    sNInfo(ths,
875,060✔
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;
875,060✔
80
  }
81

82
  sNWarn(ths,
82,085✔
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;
82,085✔
87
}
88

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

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

96
  // if already drop replica, do not process
97
  if (!syncNodeInRaftGroup(ths, &pMsg->srcId)) {
1,024,347✔
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
  bool logOK = syncNodeOnRequestVoteLogOK(ths, pMsg);
1,024,347✔
104
  // maybe update term
105
  if (pMsg->term > raftStoreGetTerm(ths)) {
1,024,347✔
106
    syncNodeStepDown(ths, pMsg->term, pMsg->srcId, "requestVote-1");
915,998✔
107
  }
108
  SyncTerm currentTerm = raftStoreGetTerm(ths);
1,024,347✔
109
  if (!(pMsg->term <= currentTerm)) return TSDB_CODE_SYN_INTERNAL_ERROR;
1,024,347✔
110

111
  sTrace("vgId:%d, begin hasVoted", ths->vgId);
1,024,347✔
112
  bool grant = (pMsg->term == currentTerm) && logOK &&
1,965,744✔
113
               ((!raftStoreHasVoted(ths)) || (syncUtilSameId(&ths->raftStore.voteFor, &pMsg->srcId)));
941,397✔
114
  if (grant) {
1,024,347✔
115
    // maybe has already voted for pMsg->srcId
116
    // vote again, no harm
117
    raftStoreVote(ths, &(pMsg->srcId));
872,403✔
118

119
    // candidate ?
120
    syncNodeStepDown(ths, currentTerm, pMsg->srcId, "requestVote-2");
872,403✔
121

122
    // forbid elect for this round
123
    resetElect = true;
872,403✔
124
  }
125

126
  // send msg
127
  SRpcMsg rpcMsg = {0};
1,024,347✔
128

129
  TAOS_CHECK_RETURN(syncBuildRequestVoteReply(&rpcMsg, ths->vgId));
1,024,347✔
130

131
  SyncRequestVoteReply* pReply = rpcMsg.pCont;
1,024,347✔
132
  pReply->srcId = ths->myRaftId;
1,024,347✔
133
  pReply->destId = pMsg->srcId;
1,024,347✔
134
  pReply->term = currentTerm;
1,024,347✔
135
  pReply->voteGranted = grant;
1,024,347✔
136
  if (!(!grant || pMsg->term == pReply->term)) return TSDB_CODE_SYN_INTERNAL_ERROR;
1,024,347✔
137

138
  // trace log
139
  syncLogRecvRequestVote(ths, pMsg, pReply->voteGranted, "", "proceed", &pRpcMsg->info.traceId);
1,024,347✔
140
  syncLogSendRequestVoteReply(ths, pReply, "", &pRpcMsg->info.traceId);
1,024,347✔
141
  TAOS_CHECK_RETURN(syncNodeSendMsgById(&pReply->destId, ths, &rpcMsg));
1,024,347✔
142

143
  if (resetElect) syncNodeResetElectTimer(ths);
1,024,347✔
144

145
  TAOS_RETURN(TSDB_CODE_SUCCESS);
1,024,347✔
146
}
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