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

taosdata / TDengine / #4041

09 May 2025 07:58AM UTC coverage: 62.508% (-0.3%) from 62.788%
#4041

push

travis-ci

web-flow
enh: update database fetch functions to include status in JSON output (#31005)

155567 of 317611 branches covered (48.98%)

Branch coverage included in aggregate %.

15 of 18 new or added lines in 1 file covered. (83.33%)

3906 existing lines in 185 files now uncovered.

240901 of 316655 relevant lines covered (76.08%)

6304979.72 hits per line

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

78.45
/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) {
56✔
50
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
56✔
51
  (void)taosThreadMutexLock(&pBuf->mutex);
56✔
52
  SSyncLogReplMgr* pMgr = syncNodeGetLogReplMgr(pNode, pDestId);
56✔
53
  syncLogReplReset(pMgr);
56✔
54
  (void)taosThreadMutexUnlock(&pBuf->mutex);
56✔
55

56
  TAOS_RETURN(TSDB_CODE_SUCCESS);
56✔
57
}
58

59
int32_t syncNodeReplicate(SSyncNode* pNode) {
52,038✔
60
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
52,038✔
61
  int32_t         ret = 0;
52,038✔
62
  if ((ret = taosThreadMutexTryLock(&pBuf->mutex)) != 0) {
52,038✔
63
    return ret;
30✔
64
  }
65
  ret = syncNodeReplicateWithoutLock(pNode);
52,095✔
66
  (void)taosThreadMutexUnlock(&pBuf->mutex);
52,109✔
67

68
  TAOS_RETURN(ret);
52,154✔
69
}
70

71
int32_t syncNodeReplicateWithoutLock(SSyncNode* pNode) {
2,347,802✔
72
  if ((pNode->state != TAOS_SYNC_STATE_LEADER && pNode->state != TAOS_SYNC_STATE_ASSIGNED_LEADER) ||
2,347,802!
73
      pNode->raftCfg.cfg.totalReplicaNum == 1) {
2,009,420✔
74
    TAOS_RETURN(TSDB_CODE_SUCCESS);
2,197,235✔
75
  }
76
  for (int32_t i = 0; i < pNode->totalReplicaNum; i++) {
597,648✔
77
    if (syncUtilSameId(&pNode->replicasId[i], &pNode->myRaftId)) {
447,075✔
78
      continue;
150,579✔
79
    }
80
    SSyncLogReplMgr* pMgr = pNode->logReplMgrs[i];
296,500✔
81
    int32_t          ret = 0;
296,500✔
82
    if ((ret = syncLogReplStart(pMgr, pNode)) != 0) {
296,500✔
83
      sWarn("vgId:%d, failed to start log replication to dnode:%d since %s", pNode->vgId, DID(&(pNode->replicasId[i])),
56!
84
            tstrerror(ret));
85
    }
86
  }
87

88
  TAOS_RETURN(TSDB_CODE_SUCCESS);
150,573✔
89
}
90

91
int32_t syncNodeSendAppendEntries(SSyncNode* pSyncNode, const SRaftId* destRaftId, SRpcMsg* pRpcMsg) {
357,083✔
92
  SyncAppendEntries* pMsg = pRpcMsg->pCont;
357,083✔
93
  pMsg->destId = *destRaftId;
357,083✔
94
  TAOS_CHECK_RETURN(syncNodeSendMsgById(destRaftId, pSyncNode, pRpcMsg));
357,083!
95

96
  int64_t nRef = 0;
357,100✔
97
  if (pSyncNode != NULL) {
357,100!
98
    nRef = atomic_add_fetch_64(&pSyncNode->sendCount, 1);
357,100✔
99
    if (nRef <= 0) {
357,101!
UNCOV
100
      sError("vgId:%d, send count is %" PRId64, pSyncNode->vgId, nRef);
×
101
    }
102
  }
103

104
  SSyncLogReplMgr* mgr = syncNodeGetLogReplMgr(pSyncNode, (SRaftId*)destRaftId);
357,101✔
105
  if (mgr != NULL) {
357,100!
106
    nRef = atomic_add_fetch_64(&mgr->sendCount, 1);
357,100✔
107
    if (nRef <= 0) {
357,100!
UNCOV
108
      sError("vgId:%d, send count is %" PRId64, pSyncNode->vgId, nRef);
×
109
    }
110
  }
111

112
  TAOS_RETURN(TSDB_CODE_SUCCESS);
357,100✔
113
}
114

115
int32_t syncNodeSendHeartbeat(SSyncNode* pSyncNode, const SRaftId* destId, SRpcMsg* pMsg) {
44,922✔
116
  SRaftId destIdTmp = *destId;
44,922✔
117
  TAOS_CHECK_RETURN(syncNodeSendMsgById(destId, pSyncNode, pMsg));
44,922✔
118

119
  int64_t tsMs = taosGetTimestampMs();
44,884✔
120
  syncIndexMgrSetSentTime(pSyncNode->pMatchIndex, &destIdTmp, tsMs);
44,884✔
121

122
  return TSDB_CODE_SUCCESS;
44,884✔
123
}
124

125
int32_t syncNodeHeartbeatPeers(SSyncNode* pSyncNode) {
11,505✔
126
  int64_t ts = taosGetTimestampMs();
11,505✔
127
  for (int32_t i = 0; i < pSyncNode->peersNum; ++i) {
13,613✔
128
    SRpcMsg rpcMsg = {0};
2,108✔
129
    if (syncBuildHeartbeat(&rpcMsg, pSyncNode->vgId) != 0) {
2,108!
UNCOV
130
      sError("vgId:%d, build sync-heartbeat error", pSyncNode->vgId);
×
UNCOV
131
      continue;
×
132
    }
133

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

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

153
  TAOS_RETURN(TSDB_CODE_SUCCESS);
11,505✔
154
}
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