• 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/syncElection.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 "syncElection.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
// RequestVote(i, j) ==
26
//    /\ state[i] = Candidate
27
//    /\ j \notin votesResponded[i]
28
//    /\ Send([mtype         |-> RequestVoteRequest,
29
//             mterm         |-> currentTerm[i],
30
//             mlastLogTerm  |-> LastTerm(log[i]),
31
//             mlastLogIndex |-> Len(log[i]),
32
//             msource       |-> i,
33
//             mdest         |-> j])
34
//    /\ UNCHANGED <<serverVars, candidateVars, leaderVars, logVars>>
35

UNCOV
36
static int32_t syncNodeRequestVotePeers(SSyncNode* pNode) {
×
UNCOV
37
  if (pNode->state != TAOS_SYNC_STATE_CANDIDATE) {
×
38
    sNTrace(pNode, "not candidate, stop elect");
×
39
    return 0;
×
40
  }
41

UNCOV
42
  int32_t ret = 0;
×
UNCOV
43
  for (int i = 0; i < pNode->peersNum; ++i) {
×
UNCOV
44
    if(pNode->peersNodeInfo[i].nodeRole == TAOS_SYNC_ROLE_LEARNER) continue;
×
45
    
UNCOV
46
    SRpcMsg rpcMsg = {0};
×
UNCOV
47
    ret = syncBuildRequestVote(&rpcMsg, pNode->vgId);
×
UNCOV
48
    if (ret < 0) {
×
49
      sError("vgId:%d, failed to build request-vote msg since %s", pNode->vgId, terrstr());
×
50
      continue;
×
51
    }
52

UNCOV
53
    SyncRequestVote* pMsg = rpcMsg.pCont;
×
UNCOV
54
    pMsg->srcId = pNode->myRaftId;
×
UNCOV
55
    pMsg->destId = pNode->peersId[i];
×
UNCOV
56
    pMsg->term = raftStoreGetTerm(pNode);
×
57

UNCOV
58
    ret = syncNodeGetLastIndexTerm(pNode, &pMsg->lastLogIndex, &pMsg->lastLogTerm);
×
UNCOV
59
    if (ret < 0) {
×
60
      sError("vgId:%d, failed to get index and term of last log since %s", pNode->vgId, terrstr());
×
61
      continue;
×
62
    }
63

UNCOV
64
    ret = syncNodeSendMsgById(&pNode->peersId[i], pNode, &rpcMsg);
×
UNCOV
65
    if (ret < 0) {
×
66
      sError("vgId:%d, failed to send msg to peerId:%" PRId64, pNode->vgId, pNode->peersId[i].addr);
×
67
      continue;
×
68
    }
69
  }
UNCOV
70
  return 0;
×
71
}
72

UNCOV
73
int32_t syncNodeElect(SSyncNode* pSyncNode) {
×
UNCOV
74
  if (pSyncNode->fsmState == SYNC_FSM_STATE_INCOMPLETE) {
×
75
    sNError(pSyncNode, "skip leader election due to incomplete fsm state");
×
76
    return TSDB_CODE_SYN_WRONG_FSM_STATE;
×
77
  }
78

UNCOV
79
  sNInfo(pSyncNode, "begin election");
×
UNCOV
80
  pSyncNode->electNum++;
×
81

UNCOV
82
  int32_t ret = 0;
×
UNCOV
83
  if (pSyncNode->state == TAOS_SYNC_STATE_FOLLOWER) {
×
UNCOV
84
    syncNodeFollower2Candidate(pSyncNode);
×
85
  }
86

UNCOV
87
  if (pSyncNode->state != TAOS_SYNC_STATE_CANDIDATE) {
×
88
    sNError(pSyncNode, "not candidate, can not elect");
×
89
    return TSDB_CODE_SYN_WRONG_SYNC_STATE;
×
90
  }
91

92
  // start election
UNCOV
93
  raftStoreNextTerm(pSyncNode);
×
UNCOV
94
  raftStoreClearVote(pSyncNode);
×
95

UNCOV
96
  SyncTerm currentTerm = raftStoreGetTerm(pSyncNode);
×
UNCOV
97
  voteGrantedReset(pSyncNode->pVotesGranted, currentTerm);
×
UNCOV
98
  votesRespondReset(pSyncNode->pVotesRespond, currentTerm);
×
UNCOV
99
  syncNodeVoteForSelf(pSyncNode, currentTerm);
×
100

UNCOV
101
  if (voteGrantedMajority(pSyncNode->pVotesGranted)) {
×
102
    // only myself, to leader
103
    if (pSyncNode->pVotesGranted->toLeader) {
×
104
      ret = TSDB_CODE_SYN_INTERNAL_ERROR;
×
105
      sError("vgId:%d, failed to elect since already be to leader", pSyncNode->vgId);
×
106
      return ret;
×
107
    }
108
    syncNodeCandidate2Leader(pSyncNode);
×
109
    pSyncNode->pVotesGranted->toLeader = true;
×
110
    return ret;
×
111
  }
112

UNCOV
113
  if (pSyncNode->replicaNum == 1) {
×
114
    // only myself, to leader
115
    voteGrantedUpdate(pSyncNode->pVotesGranted, pSyncNode);
×
116
    votesRespondUpdate(pSyncNode->pVotesRespond, pSyncNode);
×
117

118
    pSyncNode->quorum = syncUtilQuorum(pSyncNode->raftCfg.cfg.replicaNum);
×
119

120
    syncNodeCandidate2Leader(pSyncNode);
×
121
    pSyncNode->pVotesGranted->toLeader = true;
×
122
    return ret;
×
123
  }
124

UNCOV
125
  ret = syncNodeRequestVotePeers(pSyncNode);
×
UNCOV
126
  if (ret != 0) return ret;
×
127

UNCOV
128
  syncNodeResetElectTimer(pSyncNode);
×
UNCOV
129
  return ret;
×
130
}
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