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

taosdata / TDengine / #4143

24 May 2025 03:30AM UTC coverage: 32.868% (-29.4%) from 62.238%
#4143

push

travis-ci

web-flow
test: migrate stream cases (#31164)

76401 of 312956 branches covered (24.41%)

Branch coverage included in aggregate %.

128686 of 311012 relevant lines covered (41.38%)

579734.08 hits per line

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

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

56
  TAOS_RETURN(TSDB_CODE_SUCCESS);
×
57
}
58

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

68
  TAOS_RETURN(ret);
188✔
69
}
70

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

88
  TAOS_RETURN(TSDB_CODE_SUCCESS);
×
89
}
90

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

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

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

112
  TAOS_RETURN(TSDB_CODE_SUCCESS);
×
113
}
114

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

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

122
  return TSDB_CODE_SUCCESS;
×
123
}
124

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

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

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

153
  TAOS_RETURN(TSDB_CODE_SUCCESS);
68✔
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