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

taosdata / TDengine / #4788

14 Oct 2025 11:21AM UTC coverage: 60.992% (-2.3%) from 63.264%
#4788

push

travis-ci

web-flow
Merge 7ca9b50f9 into 19574fe21

154868 of 324306 branches covered (47.75%)

Branch coverage included in aggregate %.

207304 of 269498 relevant lines covered (76.92%)

125773493.22 hits per line

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

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

56
  TAOS_RETURN(TSDB_CODE_SUCCESS);
11,267✔
57
}
58

59
int32_t syncNodeReplicate(SSyncNode* pNode) {
24,691,161✔
60
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
24,691,161✔
61
  int32_t         ret = 0;
24,691,027✔
62
  if ((ret = taosThreadMutexTryLock(&pBuf->mutex)) != 0) {
24,691,027✔
63
    sWarn("vgId:%d, failed to get log buffer mutex since %d", pNode->vgId, ret);
31,849!
64
    return TSDB_CODE_FAIL_GET_LOCK;
31,849✔
65
  }
66
  ret = syncNodeReplicateWithoutLock(pNode);
24,657,348✔
67
  (void)taosThreadMutexUnlock(&pBuf->mutex);
24,655,348✔
68

69
  TAOS_RETURN(ret);
24,659,654✔
70
}
71

72
int32_t syncNodeReplicateWithoutLock(SSyncNode* pNode) {
694,442,093✔
73
  if ((pNode->state != TAOS_SYNC_STATE_LEADER && pNode->state != TAOS_SYNC_STATE_ASSIGNED_LEADER) ||
694,442,093✔
74
      pNode->raftCfg.cfg.totalReplicaNum == 1) {
660,969,764✔
75
    TAOS_RETURN(TSDB_CODE_SUCCESS);
679,779,881✔
76
  }
77
  for (int32_t i = 0; i < pNode->totalReplicaNum; i++) {
58,598,265✔
78
    if (syncUtilSameId(&pNode->replicasId[i], &pNode->myRaftId)) {
43,915,915✔
79
      continue;
14,683,671✔
80
    }
81
    SSyncLogReplMgr* pMgr = pNode->logReplMgrs[i];
29,231,804✔
82
    int32_t          ret = 0;
29,231,763✔
83
    if ((ret = syncLogReplStart(pMgr, pNode)) != 0) {
29,231,763✔
84
      sWarn("vgId:%d, failed to start log replication to dnode:%d since %s", pNode->vgId, DID(&(pNode->replicasId[i])),
6,977!
85
            tstrerror(ret));
86
    }
87
  }
88

89
  TAOS_RETURN(TSDB_CODE_SUCCESS);
14,683,287✔
90
}
91

92
int32_t syncNodeSendAppendEntries(SSyncNode* pSyncNode, const SRaftId* destRaftId, SRpcMsg* pRpcMsg) {
31,569,856✔
93
  SyncAppendEntries* pMsg = pRpcMsg->pCont;
31,569,856✔
94
  pMsg->destId = *destRaftId;
31,570,373✔
95
  TAOS_CHECK_RETURN(syncNodeSendMsgById(destRaftId, pSyncNode, pRpcMsg));
31,570,480!
96

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

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

113
  TAOS_RETURN(TSDB_CODE_SUCCESS);
31,570,510✔
114
}
115

116
int32_t syncNodeSendHeartbeat(SSyncNode* pSyncNode, const SRaftId* destId, SRpcMsg* pMsg) {
22,203,757✔
117
  SRaftId destIdTmp = *destId;
22,203,757✔
118
  TAOS_CHECK_RETURN(syncNodeSendMsgById(destId, pSyncNode, pMsg));
22,203,757✔
119

120
  int64_t tsMs = taosGetTimestampMs();
22,175,889✔
121
  syncIndexMgrSetSentTime(pSyncNode->pMatchIndex, &destIdTmp, tsMs);
22,175,889✔
122

123
  return TSDB_CODE_SUCCESS;
22,175,889✔
124
}
125

126
int32_t syncNodeHeartbeatPeers(SSyncNode* pSyncNode) {
3,741,941✔
127
  int64_t ts = taosGetTimestampMs();
3,744,557✔
128
  for (int32_t i = 0; i < pSyncNode->peersNum; ++i) {
4,733,452✔
129
    SRpcMsg rpcMsg = {0};
988,895✔
130
    if (syncBuildHeartbeat(&rpcMsg, pSyncNode->vgId) != 0) {
988,895!
131
      sError("vgId:%d, build sync-heartbeat error", pSyncNode->vgId);
×
132
      continue;
×
133
    }
134

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

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

154
  TAOS_RETURN(TSDB_CODE_SUCCESS);
3,744,224✔
155
}
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