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

taosdata / TDengine / #3543

29 Nov 2024 02:58AM UTC coverage: 60.842% (+0.02%) from 60.819%
#3543

push

travis-ci

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

merge: from main to 3.0

120460 of 253224 branches covered (47.57%)

Branch coverage included in aggregate %.

706 of 908 new or added lines in 18 files covered. (77.75%)

2401 existing lines in 137 files now uncovered.

201633 of 276172 relevant lines covered (73.01%)

19045673.23 hits per line

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

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

51
  if (myLastTerm == SYNC_TERM_INVALID) {
2,244!
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) {
2,244✔
60
    sNTrace(ths,
210!
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) {
210!
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;
210✔
72
  }
73

74
  if (pMsg->lastLogTerm == myLastTerm && pMsg->lastLogIndex >= myLastIndex) {
2,034✔
75
    sNTrace(ths,
1,769!
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;
1,769✔
80
  }
81

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

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

94
  syncLogRecvRequestVote(ths, pMsg, -1, "", "recv");
2,244✔
95

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

UNCOV
100
    TAOS_RETURN(TSDB_CODE_SYN_MISMATCHED_SIGNATURE);
×
101
  }
102

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

111
  bool grant = (pMsg->term == currentTerm) && logOK &&
4,223✔
112
               ((!raftStoreHasVoted(ths)) || (syncUtilSameId(&ths->raftStore.voteFor, &pMsg->srcId)));
1,979!
113
  if (grant) {
2,244✔
114
    // maybe has already voted for pMsg->srcId
115
    // vote again, no harm
116
    raftStoreVote(ths, &(pMsg->srcId));
1,956✔
117

118
    // candidate ?
119
    syncNodeStepDown(ths, currentTerm);
1,956✔
120

121
    // forbid elect for this round
122
    resetElect = true;
1,956✔
123
  }
124

125
  // send msg
126
  SRpcMsg rpcMsg = {0};
2,244✔
127

128
  TAOS_CHECK_RETURN(syncBuildRequestVoteReply(&rpcMsg, ths->vgId));
2,244!
129

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

137
  // trace log
138
  syncLogRecvRequestVote(ths, pMsg, pReply->voteGranted, "", "proceed");
2,244✔
139
  syncLogSendRequestVoteReply(ths, pReply, "");
2,244✔
140
  TAOS_CHECK_RETURN(syncNodeSendMsgById(&pReply->destId, ths, &rpcMsg));
2,244!
141

142
  if (resetElect) syncNodeResetElectTimer(ths);
2,244✔
143

144
  TAOS_RETURN(TSDB_CODE_SUCCESS);
2,244✔
145
}
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