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

taosdata / TDengine / #3621

22 Feb 2025 11:44AM UTC coverage: 2.037% (-61.5%) from 63.573%
#3621

push

travis-ci

web-flow
Merge pull request #29874 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

4357 of 287032 branches covered (1.52%)

Branch coverage included in aggregate %.

0 of 174 new or added lines in 18 files covered. (0.0%)

213359 existing lines in 469 files now uncovered.

7260 of 283369 relevant lines covered (2.56%)

23737.72 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

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

UNCOV
56
  TAOS_RETURN(TSDB_CODE_SUCCESS);
×
57
}
58

UNCOV
59
int32_t syncNodeReplicate(SSyncNode* pNode) {
×
UNCOV
60
  SSyncLogBuffer* pBuf = pNode->pLogBuf;
×
UNCOV
61
  (void)taosThreadMutexLock(&pBuf->mutex);
×
UNCOV
62
  int32_t ret = syncNodeReplicateWithoutLock(pNode);
×
UNCOV
63
  (void)taosThreadMutexUnlock(&pBuf->mutex);
×
64

UNCOV
65
  TAOS_RETURN(ret);
×
66
}
67

UNCOV
68
int32_t syncNodeReplicateWithoutLock(SSyncNode* pNode) {
×
UNCOV
69
  if ((pNode->state != TAOS_SYNC_STATE_LEADER && pNode->state != TAOS_SYNC_STATE_ASSIGNED_LEADER) ||
×
UNCOV
70
      pNode->raftCfg.cfg.totalReplicaNum == 1) {
×
UNCOV
71
    TAOS_RETURN(TSDB_CODE_SUCCESS);
×
72
  }
UNCOV
73
  for (int32_t i = 0; i < pNode->totalReplicaNum; i++) {
×
UNCOV
74
    if (syncUtilSameId(&pNode->replicasId[i], &pNode->myRaftId)) {
×
UNCOV
75
      continue;
×
76
    }
UNCOV
77
    SSyncLogReplMgr* pMgr = pNode->logReplMgrs[i];
×
UNCOV
78
    int32_t          ret = 0;
×
UNCOV
79
    if ((ret = syncLogReplStart(pMgr, pNode)) != 0) {
×
UNCOV
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

UNCOV
85
  TAOS_RETURN(TSDB_CODE_SUCCESS);
×
86
}
87

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

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

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

UNCOV
109
  TAOS_RETURN(TSDB_CODE_SUCCESS);
×
110
}
111

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

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

UNCOV
119
  return TSDB_CODE_SUCCESS;
×
120
}
121

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

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

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

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