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

taosdata / TDengine / #3798

31 Mar 2025 10:39AM UTC coverage: 9.424% (-20.9%) from 30.372%
#3798

push

travis-ci

happyguoxy
test:add test cases

21549 of 307601 branches covered (7.01%)

Branch coverage included in aggregate %.

36084 of 303967 relevant lines covered (11.87%)

58620.7 hits per line

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

0.0
/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) {
×
60
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
×
61
  (void)taosThreadMutexLock(&pBuf->mutex);
×
62
  int32_t ret = syncNodeReplicateWithoutLock(pNode);
×
63
  (void)taosThreadMutexUnlock(&pBuf->mutex);
×
64

65
  TAOS_RETURN(ret);
×
66
}
67

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

85
  TAOS_RETURN(TSDB_CODE_SUCCESS);
×
86
}
87

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

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

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

109
  TAOS_RETURN(TSDB_CODE_SUCCESS);
×
110
}
111

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

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

119
  return TSDB_CODE_SUCCESS;
×
120
}
121

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

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

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

150
  TAOS_RETURN(TSDB_CODE_SUCCESS);
×
151
}
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