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

taosdata / TDengine / #3548

04 Dec 2024 01:03PM UTC coverage: 59.846% (-0.8%) from 60.691%
#3548

push

travis-ci

web-flow
Merge pull request #29033 from taosdata/fix/calculate-vnode-memory-used

fix/calculate-vnode-memory-used

118484 of 254183 branches covered (46.61%)

Branch coverage included in aggregate %.

199691 of 277471 relevant lines covered (71.97%)

18794141.86 hits per line

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

75.21
/source/libs/sync/src/syncReplication.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 "syncReplication.h"
18
#include "syncIndexMgr.h"
19
#include "syncPipeline.h"
20
#include "syncRaftEntry.h"
21
#include "syncRaftStore.h"
22
#include "syncUtil.h"
23

24
// TLA+ Spec
25
// AppendEntries(i, j) ==
26
//    /\ i /= j
27
//    /\ state[i] = Leader
28
//    /\ LET prevLogIndex == nextIndex[i][j] - 1
29
//           prevLogTerm == IF prevLogIndex > 0 THEN
30
//                              log[i][prevLogIndex].term
31
//                          ELSE
32
//                              0
33
//           \* Send up to 1 entry, constrained by the end of the log.
34
//           lastEntry == Min({Len(log[i]), nextIndex[i][j]})
35
//           entries == SubSeq(log[i], nextIndex[i][j], lastEntry)
36
//       IN Send([mtype          |-> AppendEntriesRequest,
37
//                mterm          |-> currentTerm[i],
38
//                mprevLogIndex  |-> prevLogIndex,
39
//                mprevLogTerm   |-> prevLogTerm,
40
//                mentries       |-> entries,
41
//                \* mlog is used as a history variable for the proof.
42
//                \* It would not exist in a real implementation.
43
//                mlog           |-> log[i],
44
//                mcommitIndex   |-> Min({commitIndex[i], lastEntry}),
45
//                msource        |-> i,
46
//                mdest          |-> j])
47
//    /\ UNCHANGED <<serverVars, candidateVars, leaderVars, logVars>>
48

49
int32_t syncNodeReplicateReset(SSyncNode* pNode, SRaftId* pDestId) {
69✔
50
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
69✔
51
  (void)taosThreadMutexLock(&pBuf->mutex);
69✔
52
  SSyncLogReplMgr* pMgr = syncNodeGetLogReplMgr(pNode, pDestId);
69✔
53
  syncLogReplReset(pMgr);
69✔
54
  (void)taosThreadMutexUnlock(&pBuf->mutex);
69✔
55

56
  TAOS_RETURN(TSDB_CODE_SUCCESS);
69✔
57
}
58

59
int32_t syncNodeReplicate(SSyncNode* pNode) {
208,910✔
60
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
208,910✔
61
  (void)taosThreadMutexLock(&pBuf->mutex);
208,910✔
62
  int32_t ret = syncNodeReplicateWithoutLock(pNode);
209,298✔
63
  (void)taosThreadMutexUnlock(&pBuf->mutex);
209,028✔
64

65
  TAOS_RETURN(ret);
209,337✔
66
}
67

68
int32_t syncNodeReplicateWithoutLock(SSyncNode* pNode) {
13,368,690✔
69
  if ((pNode->state != TAOS_SYNC_STATE_LEADER && pNode->state != TAOS_SYNC_STATE_ASSIGNED_LEADER) ||
13,368,690!
70
      pNode->raftCfg.cfg.totalReplicaNum == 1) {
10,538,024✔
71
    TAOS_RETURN(TSDB_CODE_SUCCESS);
13,111,172✔
72
  }
73
  for (int32_t i = 0; i < pNode->totalReplicaNum; i++) {
896,568✔
74
    if (syncUtilSameId(&pNode->replicasId[i], &pNode->myRaftId)) {
639,052✔
75
      continue;
257,517✔
76
    }
77
    SSyncLogReplMgr* pMgr = pNode->logReplMgrs[i];
381,532✔
78
    int32_t          ret = 0;
381,532✔
79
    if ((ret = syncLogReplStart(pMgr, pNode)) != 0) {
381,532✔
80
      sWarn("vgId:%d, failed to start log replication to dnode:%d since %s", pNode->vgId, DID(&(pNode->replicasId[i])),
46!
81
            tstrerror(ret));
82
    }
83
  }
84

85
  TAOS_RETURN(TSDB_CODE_SUCCESS);
257,516✔
86
}
87

88
int32_t syncNodeSendAppendEntries(SSyncNode* pSyncNode, const SRaftId* destRaftId, SRpcMsg* pRpcMsg) {
2,851,418✔
89
  SyncAppendEntries* pMsg = pRpcMsg->pCont;
2,851,418✔
90
  pMsg->destId = *destRaftId;
2,851,418✔
91
  TAOS_CHECK_RETURN(syncNodeSendMsgById(destRaftId, pSyncNode, pRpcMsg));
2,851,418!
92

93
  int32_t nRef = 0;
2,851,420✔
94
  if (pSyncNode != NULL) {
2,851,420!
95
    nRef = atomic_add_fetch_32(&pSyncNode->sendCount, 1);
2,851,420✔
96
    if (nRef <= 0) {
2,851,421!
97
      sError("vgId:%d, send count is %d", pSyncNode->vgId, nRef);
×
98
    }
99
  }
100

101
  SSyncLogReplMgr* mgr = syncNodeGetLogReplMgr(pSyncNode, (SRaftId*)destRaftId);
2,851,421✔
102
  if (mgr != NULL) {
2,851,420!
103
    nRef = atomic_add_fetch_32(&mgr->sendCount, 1);
2,851,420✔
104
    if (nRef <= 0) {
2,851,421!
105
      sError("vgId:%d, send count is %d", pSyncNode->vgId, nRef);
×
106
    }
107
  }
108

109
  TAOS_RETURN(TSDB_CODE_SUCCESS);
2,851,421✔
110
}
111

112
int32_t syncNodeSendHeartbeat(SSyncNode* pSyncNode, const SRaftId* destId, SRpcMsg* pMsg) {
36,960✔
113
  SRaftId destIdTmp = *destId;
36,960✔
114
  TAOS_CHECK_RETURN(syncNodeSendMsgById(destId, pSyncNode, pMsg));
36,960✔
115

116
  int64_t tsMs = taosGetTimestampMs();
36,905✔
117
  syncIndexMgrSetSentTime(pSyncNode->pMatchIndex, &destIdTmp, tsMs);
36,905✔
118

119
  return TSDB_CODE_SUCCESS;
36,905✔
120
}
121

122
int32_t syncNodeHeartbeatPeers(SSyncNode* pSyncNode) {
11,202✔
123
  int64_t ts = taosGetTimestampMs();
11,202✔
124
  for (int32_t i = 0; i < pSyncNode->peersNum; ++i) {
13,230✔
125
    SRpcMsg rpcMsg = {0};
2,028✔
126
    if (syncBuildHeartbeat(&rpcMsg, pSyncNode->vgId) != 0) {
2,028!
127
      sError("vgId:%d, build sync-heartbeat error", pSyncNode->vgId);
×
128
      continue;
×
129
    }
130

131
    SyncHeartbeat* pSyncMsg = rpcMsg.pCont;
2,028✔
132
    pSyncMsg->srcId = pSyncNode->myRaftId;
2,028✔
133
    pSyncMsg->destId = pSyncNode->peersId[i];
2,028✔
134
    pSyncMsg->term = raftStoreGetTerm(pSyncNode);
2,028✔
135
    pSyncMsg->commitIndex = pSyncNode->commitIndex;
2,028✔
136
    pSyncMsg->minMatchIndex = syncMinMatchIndex(pSyncNode);
2,028✔
137
    pSyncMsg->privateTerm = 0;
2,028✔
138
    pSyncMsg->timeStamp = ts;
2,028✔
139

140
    // send msg
141
    TRACE_SET_MSGID(&(rpcMsg.info.traceId), tGenIdPI64());
2,028✔
142
    STraceId* trace = &(rpcMsg.info.traceId);
2,028✔
143
    sGTrace("vgId:%d, send sync-heartbeat to dnode:%d", pSyncNode->vgId, DID(&(pSyncMsg->destId)));
2,028!
144
    syncLogSendHeartbeat(pSyncNode, pSyncMsg, true, 0, 0);
2,028✔
145
    int32_t ret = syncNodeSendHeartbeat(pSyncNode, &pSyncMsg->destId, &rpcMsg);
2,028✔
146
    if (ret != 0) {
2,028!
147
      sError("vgId:%d, failed to send sync-heartbeat since %s", pSyncNode->vgId, tstrerror(ret));
×
148
    }
149
  }
150

151
  TAOS_RETURN(TSDB_CODE_SUCCESS);
11,202✔
152
}
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