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

taosdata / TDengine / #3653

14 Mar 2025 08:10AM UTC coverage: 22.565% (-41.0%) from 63.596%
#3653

push

travis-ci

web-flow
feat(keep): support keep on super table level. (#30097)

* Feat: support use keep while create super table.

* Test(keep): add test for create super table with keep option.

* Feat(keep): Add tmsg for create keep.

* Feat(keep): support alter table option keep.

* Fix(keep): Add baisc test for alter table option.

* Fix(keep): memory leek.

* Feat(keep): add keep to metaEntry&metaCache and fix earliestTs with stn keep.

* Test(keep): add some cases for select with stb keep.

* Fix: fix ci core while alter stb.

* Feat(keep): delete expired data in super table level.

* Feat: remove get stb keep while query.

* Fix : build error.

* Revert "Fix : build error."

This reverts commit 0ed66e4e8.

* Revert "Feat(keep): delete expired data in super table level."

This reverts commit 36330f6b4.

* Fix : build errors.

* Feat : support restart taosd.

* Fix : alter table comment problems.

* Test : add tests for super table keep.

* Fix: change sdb stb reserve size.

* Test: add more tests.

* Feat: Disable normal tables and sub tables from setting the keep parameter

* Fix: add more checks to avoid unknown address.

* Docs: Add docs for stable keep.

* Fix: some review changes.

* Fix: review errors.

49248 of 302527 branches covered (16.28%)

Branch coverage included in aggregate %.

53 of 99 new or added lines in 12 files covered. (53.54%)

155872 existing lines in 443 files now uncovered.

87359 of 302857 relevant lines covered (28.84%)

570004.22 hits per line

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

0.0
/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

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

UNCOV
51
  if (myLastTerm == SYNC_TERM_INVALID) {
×
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

UNCOV
59
  if (pMsg->lastLogTerm > myLastTerm) {
×
UNCOV
60
    sNTrace(ths,
×
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

UNCOV
65
    if (pMsg->lastLogIndex < ths->commitIndex) {
×
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
    }
UNCOV
71
    return true;
×
72
  }
73

UNCOV
74
  if (pMsg->lastLogTerm == myLastTerm && pMsg->lastLogIndex >= myLastIndex) {
×
UNCOV
75
    sNTrace(ths,
×
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);
UNCOV
79
    return true;
×
80
  }
81

UNCOV
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);
UNCOV
86
  return false;
×
87
}
88

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

UNCOV
94
  syncLogRecvRequestVote(ths, pMsg, -1, "", "recv");
×
95

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

100
    TAOS_RETURN(TSDB_CODE_SYN_MISMATCHED_SIGNATURE);
×
101
  }
102

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

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

118
    // candidate ?
UNCOV
119
    syncNodeStepDown(ths, currentTerm, pMsg->srcId);
×
120

121
    // forbid elect for this round
UNCOV
122
    resetElect = true;
×
123
  }
124

125
  // send msg
UNCOV
126
  SRpcMsg rpcMsg = {0};
×
127

UNCOV
128
  TAOS_CHECK_RETURN(syncBuildRequestVoteReply(&rpcMsg, ths->vgId));
×
129

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

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

UNCOV
142
  if (resetElect) syncNodeResetElectTimer(ths);
×
143

UNCOV
144
  TAOS_RETURN(TSDB_CODE_SUCCESS);
×
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