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

taosdata / TDengine / #3818

01 Apr 2025 07:46AM UTC coverage: 34.065% (-0.02%) from 34.08%
#3818

push

travis-ci

happyguoxy
test:alter gcda dir

148531 of 599532 branches covered (24.77%)

Branch coverage included in aggregate %.

222425 of 489446 relevant lines covered (45.44%)

762721.74 hits per line

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

64.95
/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) {
45✔
48
  SyncTerm  myLastTerm = syncNodeGetLastTerm(ths);
45✔
49
  SyncIndex myLastIndex = syncNodeGetLastIndex(ths);
45✔
50

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

59
  if (pMsg->lastLogTerm > myLastTerm) {
45✔
60
    sNTrace(ths,
7!
61
            "logok:1, {my-lterm:%" PRIu64 ", my-lindex:%" PRId64 ", recv-lterm:%" PRIu64 ", recv-lindex:%" PRId64
62
            ", recv-term:%" PRIu64 "}",
63
            myLastTerm, myLastIndex, pMsg->lastLogTerm, pMsg->lastLogIndex, pMsg->term);
64

65
    if (pMsg->lastLogIndex < ths->commitIndex) {
7!
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;
7✔
72
  }
73

74
  if (pMsg->lastLogTerm == myLastTerm && pMsg->lastLogIndex >= myLastIndex) {
38!
75
    sNTrace(ths,
38!
76
            "logok:1, {my-lterm:%" PRIu64 ", my-lindex:%" PRId64 ", recv-lterm:%" PRIu64 ", recv-lindex:%" PRId64
77
            ", recv-term:%" PRIu64 "}",
78
            myLastTerm, myLastIndex, pMsg->lastLogTerm, pMsg->lastLogIndex, pMsg->term);
79
    return true;
38✔
80
  }
81

82
  sNTrace(ths,
×
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;
×
87
}
88

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

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

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

100
    TAOS_RETURN(TSDB_CODE_SYN_MISMATCHED_SIGNATURE);
×
101
  }
102

103
  bool logOK = syncNodeOnRequestVoteLogOK(ths, pMsg);
45✔
104
  // maybe update term
105
  if (pMsg->term > raftStoreGetTerm(ths)) {
45!
106
    syncNodeStepDown(ths, pMsg->term, pMsg->srcId);
45✔
107
  }
108
  SyncTerm currentTerm = raftStoreGetTerm(ths);
45✔
109
  if (!(pMsg->term <= currentTerm)) return TSDB_CODE_SYN_INTERNAL_ERROR;
45!
110

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

119
    // candidate ?
120
    syncNodeStepDown(ths, currentTerm, pMsg->srcId);
45✔
121

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

126
  // send msg
127
  SRpcMsg rpcMsg = {0};
45✔
128

129
  TAOS_CHECK_RETURN(syncBuildRequestVoteReply(&rpcMsg, ths->vgId));
45!
130

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

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

143
  if (resetElect) syncNodeResetElectTimer(ths);
45!
144

145
  TAOS_RETURN(TSDB_CODE_SUCCESS);
45✔
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