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

taosdata / TDengine / #3829

28 Mar 2025 06:51AM UTC coverage: 63.162% (+0.5%) from 62.668%
#3829

push

travis-ci

web-flow
fix(tdb): disable page recycling (#30529)

155078 of 313582 branches covered (49.45%)

Branch coverage included in aggregate %.

240930 of 313390 relevant lines covered (76.88%)

20393545.98 hits per line

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

57.26
/source/libs/sync/src/syncUtil.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 "syncUtil.h"
18
#include "syncIndexMgr.h"
19
#include "syncMessage.h"
20
#include "syncPipeline.h"
21
#include "syncRaftCfg.h"
22
#include "syncRaftStore.h"
23
#include "syncSnapshot.h"
24
#include "tglobal.h"
25
#include "ttime.h"
26

27
#define FQDNRETRYTIMES 100
28

29
static void syncCfg2SimpleStr(const SSyncCfg* pCfg, char* buf, int32_t bufLen) {
182,834✔
30
  int32_t len = tsnprintf(buf, bufLen, "{num:%d, as:%d, [", pCfg->replicaNum, pCfg->myIndex);
182,834✔
31
  for (int32_t i = 0; i < pCfg->replicaNum; ++i) {
461,628✔
32
    len += tsnprintf(buf + len, bufLen - len, "%s:%d", pCfg->nodeInfo[i].nodeFqdn, pCfg->nodeInfo[i].nodePort);
278,789✔
33
    if (i < pCfg->replicaNum - 1) {
278,795✔
34
      len += tsnprintf(buf + len, bufLen - len, "%s", ", ");
95,964✔
35
    }
36
  }
37
  len += tsnprintf(buf + len, bufLen - len, "%s", "]}");
182,839✔
38
}
182,841✔
39

40
void syncUtilNodeInfo2EpSet(const SNodeInfo* pInfo, SEpSet* pEpSet) {
6,945✔
41
  pEpSet->inUse = 0;
6,945✔
42
  pEpSet->numOfEps = 1;
6,945✔
43
  pEpSet->eps[0].port = pInfo->nodePort;
6,945✔
44
  tstrncpy(pEpSet->eps[0].fqdn, pInfo->nodeFqdn, TSDB_FQDN_LEN);
6,945✔
45
}
6,945✔
46

47
bool syncUtilNodeInfo2RaftId(const SNodeInfo* pInfo, SyncGroupId vgId, SRaftId* raftId) {
21,691✔
48
  uint32_t ipv4 = 0xFFFFFFFF;
21,691✔
49
  sDebug("vgId:%d, resolve sync addr from fqdn, ep:%s:%u", vgId, pInfo->nodeFqdn, pInfo->nodePort);
21,691✔
50

51
  for (int32_t i = 0; i < FQDNRETRYTIMES; i++) {
21,697!
52
    int32_t code = taosGetIpv4FromFqdn(pInfo->nodeFqdn, &ipv4);
21,698✔
53
    if (code) {
21,694!
54
      sError("vgId:%d, failed to resolve sync addr, dnode:%d fqdn:%s, retry", vgId, pInfo->nodeId, pInfo->nodeFqdn);
×
55
      taosSsleep(1);
×
56
    } else {
57
      break;
21,694✔
58
    }
59
  }
60

61
  if (ipv4 == 0xFFFFFFFF || ipv4 == 1) {
21,693!
62
    sError("vgId:%d, failed to resolve sync addr, dnode:%d fqdn:%s", vgId, pInfo->nodeId, pInfo->nodeFqdn);
×
63
    terrno = TSDB_CODE_TSC_INVALID_FQDN;
×
64
    return false;
×
65
  }
66

67
  char ipbuf[TD_IP_LEN] = {0};
21,693✔
68
  taosInetNtoa(ipbuf, ipv4);
21,693✔
69
  raftId->addr = SYNC_ADDR(pInfo);
21,695✔
70
  raftId->vgId = vgId;
21,695✔
71

72
  sInfo("vgId:%d, sync addr:0x%" PRIx64 " is resolved, ep:%s:%u ip:%s ipv4:%u dnode:%d cluster:%" PRId64, vgId,
21,695✔
73
        raftId->addr, pInfo->nodeFqdn, pInfo->nodePort, ipbuf, ipv4, pInfo->nodeId, pInfo->clusterId);
74
  return true;
21,698✔
75
}
76

77
bool syncUtilSameId(const SRaftId* pId1, const SRaftId* pId2) {
76,913,493✔
78
  if (pId1->addr == pId2->addr && pId1->vgId == pId2->vgId) {
76,913,493!
79
    return true;
45,825,726✔
80
  }
81

82
  if ((CID(pId1) == 0 || CID(pId2) == 0) && (DID(pId1) == DID(pId2)) && pId1->vgId == pId2->vgId) {
31,087,767!
83
    return true;
172✔
84
  }
85

86
  return false;
31,087,595✔
87
}
88

89
bool syncUtilEmptyId(const SRaftId* pId) { return (pId->addr == 0 && pId->vgId == 0); }
3,223!
90

91
static inline int32_t syncUtilRand(int32_t max) { return taosRand() % max; }
447,944✔
92

93
int32_t syncUtilElectRandomMS(int32_t min, int32_t max) {
447,943✔
94
  int32_t rdm = min + syncUtilRand(max - min);
447,943✔
95

96
  // sDebug("random min:%d, max:%d, rdm:%d", min, max, rdm);
97
  return rdm;
447,948✔
98
}
99

100
int32_t syncUtilQuorum(int32_t replicaNum) { return replicaNum / 2 + 1; }
14,042✔
101

102
void syncUtilMsgHtoN(void* msg) {
10,067,677✔
103
  SMsgHead* pHead = msg;
10,067,677✔
104
  pHead->contLen = htonl(pHead->contLen);
10,067,677✔
105
  pHead->vgId = htonl(pHead->vgId);
10,067,677✔
106
}
10,067,677✔
107

108
void syncUtilGenerateArbToken(int32_t nodeId, int32_t groupId, char* buf) {
13,771✔
109
  (void)memset(buf, 0, TSDB_ARB_TOKEN_SIZE);
13,771✔
110
  int32_t randVal = taosSafeRand() % 1000;
13,771✔
111
  int64_t currentMs = taosGetTimestampMs();
13,771✔
112
  (void)snprintf(buf, TSDB_ARB_TOKEN_SIZE, "d%d#g%d#%" PRId64 "#%d", nodeId, groupId, currentMs, randVal);
13,771✔
113
}
13,771✔
114

115
static void syncPrintTime(bool formatTime, int32_t* len, int64_t tsMs, int32_t i, char* buf, int32_t bufLen) {
441,444✔
116
  if (formatTime) {
441,444!
117
    char pBuf[TD_TIME_STR_LEN] = {0};
441,447✔
118
    if (tsMs > 0) {
441,447✔
119
      if (formatTimestampLocal(pBuf, tsMs, TSDB_TIME_PRECISION_MILLI) == NULL) {
305,026!
120
        pBuf[0] = '\0';
×
121
      }
122
    }
123
    (*len) += tsnprintf(buf + (*len), bufLen - (*len), "%d:%s", i, pBuf);
441,452✔
124
  } else {
125
    (*len) += tsnprintf(buf + (*len), bufLen - (*len), "%d:%" PRId64, i, tsMs);
×
126
  }
127
}
441,463✔
128

129
// for leader
130
static void syncHearbeatReplyTime2Str(SSyncNode* pSyncNode, char* buf, int32_t bufLen, bool formatTime) {
94,754✔
131
  int32_t len = 0;
94,754✔
132
  len += tsnprintf(buf + len, bufLen - len, "%s", "{");
94,754✔
133
  for (int32_t i = 0; i < pSyncNode->replicaNum; ++i) {
241,919✔
134
    int64_t tsMs = syncIndexMgrGetRecvTime(pSyncNode->pMatchIndex, &(pSyncNode->replicasId[i]));
147,153✔
135
    syncPrintTime(formatTime, &len, tsMs, i, buf, bufLen);
147,160✔
136
    if (i < pSyncNode->replicaNum - 1) {
147,157✔
137
      len += tsnprintf(buf + len, bufLen - len, "%s", ",");
52,397✔
138
    }
139
  }
140
  len += tsnprintf(buf + len, bufLen - len, "%s", "}");
94,766✔
141
}
94,762✔
142

143
static void syncSentHearbeatTime2Str(SSyncNode* pSyncNode, char* buf, int32_t bufLen, bool formatTime) {
94,764✔
144
  int32_t len = 0;
94,764✔
145
  len += tsnprintf(buf + len, bufLen - len, "%s", "{");
94,764✔
146
  for (int32_t i = 0; i < pSyncNode->replicaNum; ++i) {
241,922✔
147
    int64_t tsMs = syncIndexMgrGetSentTime(pSyncNode->pMatchIndex, &(pSyncNode->replicasId[i]));
147,162✔
148
    syncPrintTime(formatTime, &len, tsMs, i, buf, bufLen);
147,160✔
149
    if (i < pSyncNode->replicaNum - 1) {
147,158✔
150
      len += tsnprintf(buf + len, bufLen - len, "%s", ",");
52,398✔
151
    }
152
  }
153
  len += tsnprintf(buf + len, bufLen - len, "%s", "}");
94,760✔
154
}
94,762✔
155

156
// for follower
157
static void syncHearbeatTime2Str(SSyncNode* pSyncNode, char* buf, int32_t bufLen, bool formatTime) {
94,752✔
158
  int32_t len = 0;
94,752✔
159
  len += tsnprintf(buf + len, bufLen - len, "%s", "{");
94,752✔
160
  for (int32_t i = 0; i < pSyncNode->replicaNum; ++i) {
241,921✔
161
    int64_t tsMs = syncIndexMgrGetRecvTime(pSyncNode->pNextIndex, &(pSyncNode->replicasId[i]));
147,155✔
162
    syncPrintTime(formatTime, &len, tsMs, i, buf, bufLen);
147,158✔
163
    if (i < pSyncNode->replicaNum - 1) {
147,161✔
164
      len += tsnprintf(buf + len, bufLen - len, "%s", ",");
52,399✔
165
    }
166
  }
167
  len += tsnprintf(buf + len, bufLen - len, "%s", "}");
94,766✔
168
}
94,763✔
169

170
static void syncLogBufferStates2Str(SSyncNode* pSyncNode, char* buf, int32_t bufLen) {
94,755✔
171
  SSyncLogBuffer* pBuf = pSyncNode->pLogBuf;
94,755✔
172
  if (pBuf == NULL) {
94,755!
173
    return;
×
174
  }
175
  int32_t len = 0;
94,755✔
176
  len += tsnprintf(buf + len, bufLen - len, "[%" PRId64 " %" PRId64 " %" PRId64 ", %" PRId64 ")", pBuf->startIndex,
94,755✔
177
                   pBuf->commitIndex, pBuf->matchIndex, pBuf->endIndex);
178
}
179

180
static void syncLogReplStates2Str(SSyncNode* pSyncNode, char* buf, int32_t bufLen) {
94,761✔
181
  int32_t len = 0;
94,761✔
182
  len += tsnprintf(buf + len, bufLen - len, "%s", "{");
94,761✔
183
  for (int32_t i = 0; i < pSyncNode->replicaNum; i++) {
241,923✔
184
    SSyncLogReplMgr* pMgr = pSyncNode->logReplMgrs[i];
147,161✔
185
    if (pMgr == NULL) break;
147,161!
186
    len += tsnprintf(buf + len, bufLen - len, "%d:%d [%" PRId64 ", %" PRId64 ", %" PRId64 "] ", i, pMgr->restored,
147,161✔
187
                     pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex);
188
    len += tsnprintf(buf + len, bufLen - len, "%d", pMgr->sendCount);
147,159✔
189
    if (i + 1 < pSyncNode->replicaNum) {
147,159✔
190
      len += tsnprintf(buf + len, bufLen - len, "%s", ", ");
52,396✔
191
    }
192
  }
193
  len += tsnprintf(buf + len, bufLen - len, "%s", "}");
94,762✔
194
}
94,760✔
195

196
static void syncPeerState2Str(SSyncNode* pSyncNode, char* buf, int32_t bufLen) {
88,080✔
197
  int32_t len = 0;
88,080✔
198
  len += tsnprintf(buf + len, bufLen - len, "%s", "{");
88,080✔
199
  for (int32_t i = 0; i < pSyncNode->replicaNum; ++i) {
219,605✔
200
    SPeerState* pState = syncNodeGetPeerState(pSyncNode, &(pSyncNode->replicasId[i]));
131,526✔
201
    if (pState == NULL) break;
131,516!
202
    len += tsnprintf(buf + len, bufLen - len, "%d:%" PRId64 " %" PRId64 "%s", i, pState->lastSendIndex,
131,516✔
203
                     pState->lastSendTime, (i < pSyncNode->replicaNum - 1) ? ", " : "");
131,516✔
204
  }
205
  len += tsnprintf(buf + len, bufLen - len, "%s", "}");
88,079✔
206
}
88,082✔
207

208
void syncPrintNodeLog(const char* flags, ELogLevel level, int32_t dflag, bool formatTime, SSyncNode* pNode,
94,758✔
209
                      const char* format, ...) {
210
  if (pNode == NULL || pNode->pLogStore == NULL) return;
94,758!
211
  int64_t currentTerm = raftStoreGetTerm(pNode);
94,759✔
212

213
  // save error code, otherwise it will be overwritten
214
  int32_t errCode = terrno;
94,762✔
215

216
  SSnapshot snapshot = {.data = NULL, .lastApplyIndex = -1, .lastApplyTerm = 0};
94,759✔
217
  if (pNode->pFsm != NULL && pNode->pFsm->FpGetSnapshotInfo != NULL) {
94,759!
218
    (void)pNode->pFsm->FpGetSnapshotInfo(pNode->pFsm, &snapshot);  // vnodeSyncGetSnapshotInfo
94,760✔
219
  }
220

221
  SyncIndex logLastIndex = SYNC_INDEX_INVALID;
94,762✔
222
  SyncIndex logBeginIndex = SYNC_INDEX_INVALID;
94,762✔
223
  if (pNode->pLogStore != NULL) {
94,762!
224
    logLastIndex = pNode->pLogStore->syncLogLastIndex(pNode->pLogStore);
94,762✔
225
    logBeginIndex = pNode->pLogStore->syncLogBeginIndex(pNode->pLogStore);
94,760✔
226
  }
227

228
  int32_t cacheHit = pNode->pLogStore->cacheHit;
94,759✔
229
  int32_t cacheMiss = pNode->pLogStore->cacheMiss;
94,759✔
230

231
  char cfgStr[1024] = "";
94,759✔
232
  syncCfg2SimpleStr(&pNode->raftCfg.cfg, cfgStr, sizeof(cfgStr));
94,759✔
233

234
  char replMgrStatesStr[1024] = "";
94,762✔
235
  syncLogReplStates2Str(pNode, replMgrStatesStr, sizeof(replMgrStatesStr));
94,762✔
236

237
  char bufferStatesStr[256] = "";
94,760✔
238
  syncLogBufferStates2Str(pNode, bufferStatesStr, sizeof(bufferStatesStr));
94,760✔
239

240
  char hbrTimeStr[256] = "";
94,762✔
241
  syncHearbeatReplyTime2Str(pNode, hbrTimeStr, sizeof(hbrTimeStr), formatTime);
94,762✔
242

243
  char hbTimeStr[256] = "";
94,760✔
244
  syncHearbeatTime2Str(pNode, hbTimeStr, sizeof(hbTimeStr), formatTime);
94,760✔
245

246
  char sentHbTimeStr[512] = "";
94,762✔
247
  syncSentHearbeatTime2Str(pNode, sentHbTimeStr, sizeof(sentHbTimeStr), formatTime);
94,762✔
248

249
  char    eventLog[512];  // {0};
250
  va_list argpointer;
251
  va_start(argpointer, format);
94,762✔
252
  int32_t writeLen = vsnprintf(eventLog, sizeof(eventLog), format, argpointer);
94,762✔
253
  va_end(argpointer);
94,762✔
254

255
  int32_t aqItems = 0;
94,762✔
256
  if (pNode != NULL && pNode->pFsm != NULL && pNode->pFsm->FpApplyQueueItems != NULL) {
94,762!
257
    aqItems = pNode->pFsm->FpApplyQueueItems(pNode->pFsm);  // vnodeApplyQueueItems
94,762✔
258
  }
259

260
  // restore error code
261
  terrno = errCode;
94,752✔
262
  SyncIndex appliedIndex = pNode->pFsm->FpAppliedIndexCb(pNode->pFsm);  // vnodeSyncAppliedIndex
94,755✔
263

264
  if (pNode != NULL) {
94,757!
265
    taosPrintLog(
94,749✔
266
        flags, level, dflag,
267
        "vgId:%d, %s, sync:%s, term:%" PRIu64 ", commit-index:%" PRId64 ", assigned-index:%" PRId64
268
        ", applied-index:%" PRId64 ", first-ver:%" PRId64 ", last-ver:%" PRId64 ", min:%" PRId64 ", snap:%" PRId64
269
        ", snap-term:%" PRIu64
270
        ", elect-times:%d, as-leader-times:%d, as-assigned-leader-times:%d, cfg-ch-times:%d, hb-slow:%d, hbr-slow:%d, "
271
        "aq-items:%d, snaping:%" PRId64 ", replicas:%d, last-cfg:%" PRId64
272
        ", chging:%d, restore:%d, quorum:%d, elect-lc-timer:%" PRId64 ", hb:%" PRId64
273
        ", buffer:%s, repl-mgrs:%s, members:%s, send hb:%s, recv hb:%s, recv hb-reply:%s, arb-token:%s, msg[sent:%d, "
274
        "recv:%d, slow-recv:%d]",
275
        pNode->vgId, eventLog, syncStr(pNode->state), currentTerm, pNode->commitIndex, pNode->assignedCommitIndex,
276
        appliedIndex, logBeginIndex, logLastIndex, pNode->minMatchIndex, snapshot.lastApplyIndex,
277
        snapshot.lastApplyTerm, pNode->electNum, pNode->becomeLeaderNum, pNode->becomeAssignedLeaderNum,
278
        pNode->configChangeNum, pNode->hbSlowNum, pNode->hbrSlowNum, aqItems, pNode->snapshottingIndex,
279
        pNode->replicaNum, pNode->raftCfg.lastConfigIndex, pNode->changing, pNode->restoreFinish,
94,746✔
280
        syncNodeDynamicQuorum(pNode), pNode->electTimerLogicClock, pNode->heartbeatTimerLogicClockUser, bufferStatesStr,
281
        replMgrStatesStr, cfgStr, sentHbTimeStr, hbTimeStr, hbrTimeStr, pNode->arbToken, pNode->sendCount,
94,758✔
282
        pNode->recvCount, pNode->slowCount);
283
  }
284
}
285

286
void syncPrintHbLog(const char* flags, ELogLevel level, int32_t dflag, bool formatTime, SSyncNode* pNode,
×
287
                    const char* format, ...) {
288
  if (pNode == NULL || pNode->pLogStore == NULL) return;
×
289
  int64_t currentTerm = raftStoreTryGetTerm(pNode);
×
290

291
  // save error code, otherwise it will be overwritten
292
  int32_t errCode = terrno;
×
293

294
  int32_t cacheHit = pNode->pLogStore->cacheHit;
×
295
  int32_t cacheMiss = pNode->pLogStore->cacheMiss;
×
296

297
  char cfgStr[1024] = "";
×
298
  syncCfg2SimpleStr(&pNode->raftCfg.cfg, cfgStr, sizeof(cfgStr));
×
299

300
  char replMgrStatesStr[1024] = "";
×
301
  syncLogReplStates2Str(pNode, replMgrStatesStr, sizeof(replMgrStatesStr));
×
302

303
  char bufferStatesStr[256] = "";
×
304
  syncLogBufferStates2Str(pNode, bufferStatesStr, sizeof(bufferStatesStr));
×
305

306
  char hbrTimeStr[256] = "";
×
307
  syncHearbeatReplyTime2Str(pNode, hbrTimeStr, sizeof(hbrTimeStr), formatTime);
×
308

309
  char hbTimeStr[256] = "";
×
310
  syncHearbeatTime2Str(pNode, hbTimeStr, sizeof(hbTimeStr), formatTime);
×
311

312
  char sentHbTimeStr[512] = "";
×
313
  syncSentHearbeatTime2Str(pNode, sentHbTimeStr, sizeof(sentHbTimeStr), formatTime);
×
314

315
  char    eventLog[512];  // {0};
316
  va_list argpointer;
317
  va_start(argpointer, format);
×
318
  int32_t writeLen = vsnprintf(eventLog, sizeof(eventLog), format, argpointer);
×
319
  va_end(argpointer);
×
320

321
  terrno = errCode;
×
322

323
  if (pNode != NULL) {
×
324
    taosPrintLog(
×
325
        flags, level, dflag,
326
        "vgId:%d, %s, sync:%s, term:%" PRIu64 ", commit-index:%" PRId64 ", assigned-index:%" PRId64 ", min:%" PRId64
327
        ", elect-times:%d, as-leader-times:%d, as-assigned-leader-times:%d, cfg-ch-times:%d, hb-slow:%d, hbr-slow:%d, "
328
        ", snaping:%" PRId64 ", replicas:%d, last-cfg:%" PRId64
329
        ", chging:%d, restore:%d, quorum:%d, elect-lc-timer:%" PRId64 ", hb:%" PRId64
330
        ", buffer:%s, repl-mgrs:%s, members:%s, send hb:%s, recv hb:%s, recv hb-reply:%s, arb-token:%s, msg[sent:%d, "
331
        "recv:%d, slow-recv:%d]",
332
        pNode->vgId, eventLog, syncStr(pNode->state), currentTerm, pNode->commitIndex, pNode->assignedCommitIndex,
333
        pNode->minMatchIndex, pNode->electNum, pNode->becomeLeaderNum, pNode->becomeAssignedLeaderNum,
334
        pNode->configChangeNum, pNode->hbSlowNum, pNode->hbrSlowNum, pNode->snapshottingIndex, pNode->replicaNum,
335
        pNode->raftCfg.lastConfigIndex, pNode->changing, pNode->restoreFinish, syncNodeDynamicQuorum(pNode),
×
336
        pNode->electTimerLogicClock, pNode->heartbeatTimerLogicClockUser, bufferStatesStr, replMgrStatesStr, cfgStr,
337
        sentHbTimeStr, hbTimeStr, hbrTimeStr, pNode->arbToken, pNode->sendCount, pNode->recvCount, pNode->slowCount);
×
338
  }
339
}
340

341
void syncPrintSnapshotSenderLog(const char* flags, ELogLevel level, int32_t dflag, SSyncSnapshotSender* pSender,
82,305✔
342
                                const char* format, ...) {
343
  SSyncNode* pNode = pSender->pSyncNode;
82,305✔
344
  if (pNode == NULL || pNode->pLogStore == NULL) return;
82,305!
345

346
  SSnapshot snapshot = {.data = NULL, .lastApplyIndex = -1, .lastApplyTerm = 0};
82,308✔
347
  if (pNode->pFsm != NULL && pNode->pFsm->FpGetSnapshotInfo != NULL) {
82,308!
348
    (void)pNode->pFsm->FpGetSnapshotInfo(pNode->pFsm, &snapshot);
82,309✔
349
  }
350

351
  SyncIndex logLastIndex = SYNC_INDEX_INVALID;
82,312✔
352
  SyncIndex logBeginIndex = SYNC_INDEX_INVALID;
82,312✔
353
  if (pNode->pLogStore != NULL) {
82,312!
354
    logLastIndex = pNode->pLogStore->syncLogLastIndex(pNode->pLogStore);
82,313✔
355
    logBeginIndex = pNode->pLogStore->syncLogBeginIndex(pNode->pLogStore);
82,312✔
356
  }
357

358
  char cfgStr[1024] = "";
82,311✔
359
  syncCfg2SimpleStr(&pNode->raftCfg.cfg, cfgStr, sizeof(cfgStr));
82,311✔
360

361
  char peerStr[1024] = "";
82,319✔
362
  syncPeerState2Str(pNode, peerStr, sizeof(peerStr));
82,319✔
363

364
  char    eventLog[512];  // {0};
365
  va_list argpointer;
366
  va_start(argpointer, format);
82,320✔
367
  int32_t writeLen = vsnprintf(eventLog, sizeof(eventLog), format, argpointer);
82,320✔
368
  va_end(argpointer);
82,320✔
369

370
  taosPrintLog(flags, level, dflag,
82,321✔
371
               "vgId:%d, %s, sync:%s, snap-sender:%p signature:(%" PRId64 ", %" PRId64 "), {start:%" PRId64
372
               " end:%" PRId64 " last-index:%" PRId64 " last-term:%" PRId64 " last-cfg:%" PRId64
373
               ", seq:%d, ack:%d, "
374
               " buf:[%" PRId64 " %" PRId64 ", %" PRId64
375
               "], finish:%d, as:%d, to-dnode:%d}"
376
               ", term:%" PRIu64 ", commit-index:%" PRId64 ", firstver:%" PRId64 ", lastver:%" PRId64
377
               ", min-match:%" PRId64 ", snap:{last-index:%" PRId64 ", term:%" PRIu64
378
               "}, standby:%d, batch-sz:%d, replicas:%d, last-cfg:%" PRId64
379
               ", chging:%d, restore:%d, quorum:%d, peer:%s, cfg:%s",
380
               pNode->vgId, eventLog, syncStr(pNode->state), pSender, pSender->term, pSender->startTime,
381
               pSender->snapshotParam.start, pSender->snapshotParam.end, pSender->snapshot.lastApplyIndex,
382
               pSender->snapshot.lastApplyTerm, pSender->snapshot.lastConfigIndex, pSender->seq, pSender->ack,
383
               pSender->pSndBuf->start, pSender->pSndBuf->cursor, pSender->pSndBuf->end, pSender->finish,
82,322✔
384
               pSender->replicaIndex, DID(&pNode->replicasId[pSender->replicaIndex]), raftStoreGetTerm(pNode),
82,322✔
385
               pNode->commitIndex, logBeginIndex, logLastIndex, pNode->minMatchIndex, snapshot.lastApplyIndex,
386
               snapshot.lastApplyTerm, pNode->raftCfg.isStandBy, pNode->raftCfg.batchSize, pNode->replicaNum,
82,320✔
387
               pNode->raftCfg.lastConfigIndex, pNode->changing, pNode->restoreFinish, pNode->quorum, peerStr, cfgStr);
82,320✔
388
}
389

390
void syncPrintSnapshotReceiverLog(const char* flags, ELogLevel level, int32_t dflag, SSyncSnapshotReceiver* pReceiver,
5,761✔
391
                                  const char* format, ...) {
392
  SSyncNode* pNode = pReceiver->pSyncNode;
5,761✔
393
  if (pNode == NULL || pNode->pLogStore == NULL) return;
5,761!
394

395
  SSnapshot snapshot = {.data = NULL, .lastApplyIndex = -1, .lastApplyTerm = 0};
5,761✔
396
  if (pNode->pFsm != NULL && pNode->pFsm->FpGetSnapshotInfo != NULL) {
5,761!
397
    (void)pNode->pFsm->FpGetSnapshotInfo(pNode->pFsm, &snapshot);
5,761✔
398
  }
399

400
  SyncIndex logLastIndex = SYNC_INDEX_INVALID;
5,761✔
401
  SyncIndex logBeginIndex = SYNC_INDEX_INVALID;
5,761✔
402
  if (pNode->pLogStore != NULL) {
5,761!
403
    logLastIndex = pNode->pLogStore->syncLogLastIndex(pNode->pLogStore);
5,761✔
404
    logBeginIndex = pNode->pLogStore->syncLogBeginIndex(pNode->pLogStore);
5,761✔
405
  }
406

407
  char cfgStr[1024] = "";
5,761✔
408
  syncCfg2SimpleStr(&pNode->raftCfg.cfg, cfgStr, sizeof(cfgStr));
5,761✔
409

410
  char peerStr[1024] = "";
5,761✔
411
  syncPeerState2Str(pNode, peerStr, sizeof(peerStr));
5,761✔
412

413
  char    eventLog[512];  // {0};
414
  va_list argpointer;
415
  va_start(argpointer, format);
5,761✔
416
  int32_t writeLen = vsnprintf(eventLog, sizeof(eventLog), format, argpointer);
5,761✔
417
  va_end(argpointer);
5,761✔
418

419
  taosPrintLog(
5,761✔
420
      flags, level, dflag,
421
      "vgId:%d, %s, sync:%s,"
422
      " snap-receiver:%p signature:(%" PRId64 ", %" PRId64 "), {start:%d ack:%d buf:[%" PRId64 " %" PRId64 ", %" PRId64
423
      ")"
424
      " from-dnode:%d, start:%" PRId64 " end:%" PRId64 " last-index:%" PRId64 " last-term:%" PRIu64 " last-cfg:%" PRId64
425
      "}"
426
      ", term:%" PRIu64 ", commit-index:%" PRId64 ", firstver:%" PRId64 ", lastver:%" PRId64 ", min-match:%" PRId64
427
      ", snap:{last-index:%" PRId64 ", last-term:%" PRIu64 "}, standby:%d, batch-sz:%d, replicas:%d, last-cfg:%" PRId64
428
      ", chging:%d, restore:%d, quorum:%d, peer:%s, cfg:%s",
429
      pNode->vgId, eventLog, syncStr(pNode->state), pReceiver, pReceiver->term, pReceiver->startTime, pReceiver->start,
5,761✔
430
      pReceiver->ack, pReceiver->pRcvBuf->start, pReceiver->pRcvBuf->cursor, pReceiver->pRcvBuf->end,
5,761✔
431
      DID(&pReceiver->fromId), pReceiver->snapshotParam.start, pReceiver->snapshotParam.end,
5,761✔
432
      pReceiver->snapshot.lastApplyIndex, pReceiver->snapshot.lastApplyTerm, pReceiver->snapshot.lastConfigIndex,
433
      raftStoreGetTerm(pNode), pNode->commitIndex, logBeginIndex, logLastIndex, pNode->minMatchIndex,
434
      snapshot.lastApplyIndex, snapshot.lastApplyTerm, pNode->raftCfg.isStandBy, pNode->raftCfg.batchSize,
5,761✔
435
      pNode->replicaNum, pNode->raftCfg.lastConfigIndex, pNode->changing, pNode->restoreFinish, pNode->quorum, peerStr,
5,761✔
436
      cfgStr);
437
}
438

439
void syncLogRecvTimer(SSyncNode* pSyncNode, const SyncTimeout* pMsg, const STraceId* trace) {
253,644✔
440
  if (!(sDebugFlag & DEBUG_TRACE)) return;
253,644✔
441

442
  int64_t tsNow = taosGetTimestampMs();
5,407✔
443
  int64_t timeDIff = tsNow - pMsg->timeStamp;
5,407✔
444
  sNTrace(pSyncNode,
5,407!
445
          "recv sync-timer {type:%s, lc:%" PRId64 ", ms:%d, ts:%" PRId64 ", elapsed:%" PRId64
446
          ", data:%p}, QID:0x%" PRIx64 ":0x%" PRIx64,
447
          syncTimerTypeStr(pMsg->timeoutType), pMsg->logicClock, pMsg->timerMS, pMsg->timeStamp, timeDIff, pMsg->data,
448
          trace ? trace->rootId : 0, trace ? trace->msgId : 0);
449
}
450

451
void syncLogRecvLocalCmd(SSyncNode* pSyncNode, const SyncLocalCmd* pMsg, const STraceId* trace) {
34,847✔
452
  sNTrace(pSyncNode,
34,847!
453
          "recv sync-local-cmd {cmd:%d-%s, sd-new-term:%" PRId64 ", fc-index:%" PRId64 "}, QID:0x%" PRIx64
454
          ":0x%" PRIx64,
455
          pMsg->cmd, syncLocalCmdGetStr(pMsg->cmd), pMsg->currentTerm, pMsg->commitIndex, trace ? trace->rootId : 0,
456
          trace ? trace->msgId : 0);
457
}
34,847✔
458

459
void syncLogSendAppendEntriesReply(SSyncNode* pSyncNode, const SyncAppendEntriesReply* pMsg, const char* s,
×
460
                                   const STraceId* trace) {
461
  sNTrace(pSyncNode,
×
462
          "send sync-append-entries-reply to dnode:%d, {term:%" PRId64 ", pterm:%" PRId64
463
          ", success:%d, lsend-index:%" PRId64 ", match:%" PRId64 "}, %s, QID:0x%" PRIx64 ":0x%" PRIx64,
464
          DID(&pMsg->destId), pMsg->term, pMsg->lastMatchTerm, pMsg->success, pMsg->lastSendIndex, pMsg->matchIndex, s,
465
          trace ? trace->rootId : 0, trace ? trace->msgId : 0);
466
}
×
467

468
void syncLogRecvAppendEntriesReply(SSyncNode* pSyncNode, const SyncAppendEntriesReply* pMsg, const char* s,
×
469
                                   const STraceId* trace) {
470
  sNTrace(pSyncNode,
×
471
          "recv sync-append-entries-reply from dnode:%d {term:%" PRId64 ", pterm:%" PRId64
472
          ", success:%d, lsend-index:%" PRId64 ", match:%" PRId64 "}, %s, QID:0x%" PRIx64 ":0x%" PRIx64,
473
          DID(&pMsg->srcId), pMsg->term, pMsg->lastMatchTerm, pMsg->success, pMsg->lastSendIndex, pMsg->matchIndex, s,
474
          trace ? trace->rootId : 0, trace ? trace->msgId : 0);
475
}
×
476

477
void syncLogSendHeartbeat(SSyncNode* pSyncNode, const SyncHeartbeat* pMsg, bool printX, int64_t timerElapsed,
41,670✔
478
                          int64_t execTime) {
479
  if (sDebugFlag & DEBUG_TRACE) {
41,670!
480
    char pBuf[TD_TIME_STR_LEN] = {0};
×
481
    if (pMsg->timeStamp > 0) {
×
482
      if (formatTimestampLocal(pBuf, pMsg->timeStamp, TSDB_TIME_PRECISION_MILLI) == NULL) {
×
483
        pBuf[0] = '\0';
×
484
      }
485
    }
486
    if (printX) {
×
487
      sHTrace(pSyncNode,
×
488
              "send sync-heartbeat to dnode:%d {term:%" PRId64 ", commit-index:%" PRId64 ", min-match:%" PRId64
489
              ", ts:%s}, x",
490
              DID(&pMsg->destId), pMsg->term, pMsg->commitIndex, pMsg->minMatchIndex, pBuf);
491
    } else {
492
      sHTrace(pSyncNode,
×
493
              "send sync-heartbeat to dnode:%d {term:%" PRId64 ", commit-index:%" PRId64 ", min-match:%" PRId64
494
              ", ts:%s}, timer-elapsed:%" PRId64 ", next-exec:%" PRId64,
495
              DID(&pMsg->destId), pMsg->term, pMsg->commitIndex, pMsg->minMatchIndex, pBuf, timerElapsed, execTime);
496
    }
497
  }
498
}
41,670✔
499

500
void syncLogRecvHeartbeat(SSyncNode* pSyncNode, const SyncHeartbeat* pMsg, int64_t timeDiff, const STraceId* trace) {
35,044✔
501
  if (timeDiff > SYNC_HEARTBEAT_SLOW_MS) {
35,044!
502
    pSyncNode->hbSlowNum++;
×
503

504
    char pBuf[TD_TIME_STR_LEN] = {0};
×
505
    if (pMsg->timeStamp > 0) {
×
506
      if (formatTimestampLocal(pBuf, pMsg->timeStamp, TSDB_TIME_PRECISION_MILLI) == NULL) {
×
507
        pBuf[0] = '\0';
×
508
      }
509
    }
510

511
    sHError(pSyncNode,
×
512
            "recv sync-heartbeat from dnode:%d slow(%d ms) {term:%" PRId64 ", commit-index:%" PRId64
513
            ", min-match:%" PRId64 ", ts:%s}, net elapsed:%" PRId64 "ms, QID:0x%" PRIx64 ":0x%" PRIx64,
514
            DID(&pMsg->srcId), SYNC_HEARTBEAT_SLOW_MS, pMsg->term, pMsg->commitIndex, pMsg->minMatchIndex, pBuf,
515
            timeDiff, trace ? trace->rootId : 0, trace ? trace->msgId : 0);
516
  } else {
517
    if (sDebugFlag & DEBUG_TRACE) {
35,044!
518
      char pBuf[TD_TIME_STR_LEN] = {0};
×
519
      if (pMsg->timeStamp > 0) {
×
520
        if (formatTimestampLocal(pBuf, pMsg->timeStamp, TSDB_TIME_PRECISION_MILLI) == NULL) {
×
521
          pBuf[0] = '\0';
×
522
        }
523
      }
524
      sHTrace(pSyncNode,
×
525
              "recv sync-heartbeat from dnode:%d {term:%" PRId64 ", commit-index:%" PRId64 ", min-match:%" PRId64
526
              ", ts:%s},  net elapsed:%" PRId64 "ms, QID:0x%" PRIx64 ":0x%" PRIx64,
527
              DID(&pMsg->srcId), pMsg->term, pMsg->commitIndex, pMsg->minMatchIndex, pBuf, timeDiff, trace->rootId,
528
              trace->msgId);
529
    }
530
  }
531
}
35,044✔
532

533
void syncLogSendHeartbeatReply(SSyncNode* pSyncNode, const SyncHeartbeatReply* pMsg, const char* s,
×
534
                               const STraceId* trace) {
535
  sHTrace(pSyncNode,
×
536
          "send sync-heartbeat-reply from dnode:%d {term:%" PRId64 ", ts:%" PRId64 "}, %s, QID:0x%" PRIx64
537
          ":0x%" PRIx64,
538
          DID(&pMsg->destId), pMsg->term, pMsg->timeStamp, s, trace ? trace->rootId : 0, trace ? trace->msgId : 0);
539
}
×
540

541
void syncLogRecvHeartbeatReply(SSyncNode* pSyncNode, const SyncHeartbeatReply* pMsg, int64_t timeDiff,
34,405✔
542
                               const STraceId* trace) {
543
  if (timeDiff > SYNC_HEARTBEAT_REPLY_SLOW_MS) {
34,405!
544
    pSyncNode->hbrSlowNum++;
×
545

546
    char pBuf[TD_TIME_STR_LEN] = {0};
×
547
    if (pMsg->timeStamp > 0) {
×
548
      if (formatTimestampLocal(pBuf, pMsg->timeStamp, TSDB_TIME_PRECISION_MILLI) == NULL) {
×
549
        pBuf[0] = '\0';
×
550
      }
551
    }
552

553
    sHError(pSyncNode,
×
554
            "recv sync-heartbeat-reply from dnode:%d slow(%d ms) {term:%" PRId64 ", ts:%s}, net elapsed:%" PRId64
555
            ", QID:0x%" PRIx64 ":0x%" PRIx64,
556
            DID(&pMsg->srcId), SYNC_HEARTBEAT_REPLY_SLOW_MS, pMsg->term, pBuf, timeDiff, trace->rootId, trace->msgId);
557
  } else {
558
    if (sDebugFlag & DEBUG_TRACE) {
34,405!
559
      char pBuf[TD_TIME_STR_LEN] = {0};
×
560
      if (pMsg->timeStamp > 0) {
×
561
        if (formatTimestampLocal(pBuf, pMsg->timeStamp, TSDB_TIME_PRECISION_MILLI) == NULL) {
×
562
          pBuf[0] = '\0';
×
563
        }
564
      }
565
      sHTrace(pSyncNode,
×
566
              "recv sync-heartbeat-reply from dnode:%d {term:%" PRId64 ", ts:%" PRId64 "}, net elapsed:%" PRId64
567
              ", QID:0x%" PRIx64 ":0x%" PRIx64,
568
              DID(&pMsg->srcId), pMsg->term, pMsg->timeStamp, timeDiff, trace ? trace->rootId : 0,
569
              trace ? trace->msgId : 0);
570
    }
571
  }
572
}
34,405✔
573

574
void syncLogSendSyncSnapshotSend(SSyncNode* pSyncNode, const SyncSnapshotSend* pMsg, const char* s,
×
575
                                 const STraceId* trace) {
576
  sNDebug(pSyncNode,
×
577
          "send sync-snapshot-send to dnode:%d, %s, seq:%d, term:%" PRId64 ", begin-index:%" PRId64
578
          ", last-index:%" PRId64 ", last-term:%" PRId64 ", start-time:%" PRId64 ", QID:0x%" PRIx64 ":0x%" PRIx64,
579
          DID(&pMsg->destId), s, pMsg->seq, pMsg->term, pMsg->beginIndex, pMsg->lastIndex, pMsg->lastTerm,
580
          pMsg->startTime, trace ? trace->rootId : 0, trace ? trace->msgId : 0);
581
}
×
582

583
void syncLogRecvSyncSnapshotSend(SSyncNode* pSyncNode, const SyncSnapshotSend* pMsg, const char* s,
×
584
                                 const STraceId* trace) {
585
  sNDebug(pSyncNode,
×
586
          "recv sync-snapshot-send from dnode:%d, %s, seq:%d, term:%" PRId64 ", begin-index:%" PRId64
587
          ", last-index:%" PRId64 ", last-term:%" PRId64 ", start-time:%" PRId64 ", data-len:%u, QID:0x%" PRIx64
588
          ":0x%" PRIx64,
589
          DID(&pMsg->srcId), s, pMsg->seq, pMsg->term, pMsg->beginIndex, pMsg->lastIndex, pMsg->lastTerm,
590
          pMsg->startTime, pMsg->dataLen, trace ? trace->rootId : 0, trace ? trace->msgId : 0);
591
}
×
592

593
void syncLogSendSyncSnapshotRsp(SSyncNode* pSyncNode, const SyncSnapshotRsp* pMsg, const char* s,
59✔
594
                                const STraceId* trace) {
595
  sNDebug(pSyncNode,
59!
596
          "send sync-snapshot-rsp to dnode:%d, %s, acked:%d, term:%" PRId64 ", begin-index:%" PRId64
597
          ", last-index:%" PRId64 ", last-term:%" PRId64 ", start-time:%" PRId64 ", QID:0x%" PRIx64 ":0x%" PRIx64,
598
          DID(&pMsg->destId), s, pMsg->ack, pMsg->term, pMsg->snapBeginIndex, pMsg->lastIndex, pMsg->lastTerm,
599
          pMsg->startTime, trace ? trace->rootId : 0, trace ? trace->msgId : 0);
600
}
59✔
601

602
void syncLogRecvSyncSnapshotRsp(SSyncNode* pSyncNode, const SyncSnapshotRsp* pMsg, const char* s,
×
603
                                const STraceId* trace) {
604
  sNDebug(pSyncNode,
×
605
          "recv sync-snapshot-rsp from dnode:%d, %s, ack:%d, term:%" PRId64 ", begin-index:%" PRId64
606
          ", last-index:%" PRId64 ", last-term:%" PRId64 ", start-time:%" PRId64 ", QID:0x%" PRIx64 ":0x%" PRIx64,
607
          DID(&pMsg->srcId), s, pMsg->ack, pMsg->term, pMsg->snapBeginIndex, pMsg->lastIndex, pMsg->lastTerm,
608
          pMsg->startTime, trace ? trace->rootId : 0, trace ? trace->msgId : 0);
609
}
×
610

611
void syncLogRecvAppendEntries(SSyncNode* pSyncNode, const SyncAppendEntries* pMsg, const char* s,
1✔
612
                              const STraceId* trace) {
613
  sNTrace(pSyncNode,
1!
614
          "recv sync-append-entries from dnode:%d {term:%" PRId64 ", prev-log:{index:%" PRId64 ", term:%" PRId64
615
          "}, commit-index:%" PRId64 ", datalen:%d}, %s, QID:0x%" PRIx64 ":0x%" PRIx64,
616
          DID(&pMsg->srcId), pMsg->term, pMsg->prevLogIndex, pMsg->prevLogTerm, pMsg->commitIndex, pMsg->dataLen, s,
617
          trace ? trace->rootId : 0, trace ? trace->msgId : 0);
618
}
1✔
619

620
void syncLogSendAppendEntries(SSyncNode* pSyncNode, const SyncAppendEntries* pMsg, const char* s,
×
621
                              const STraceId* trace) {
622
  sNTrace(pSyncNode,
×
623
          "send sync-append-entries to dnode:%d, {term:%" PRId64 ", prev-log:{index:%" PRId64 ", term:%" PRId64
624
          "}, index:%" PRId64 ", commit-index:%" PRId64 ", datalen:%d}, %s, QID:0x%" PRIx64 ":0x%" PRIx64,
625
          DID(&pMsg->destId), pMsg->term, pMsg->prevLogIndex, pMsg->prevLogTerm, (pMsg->prevLogIndex + 1),
626
          pMsg->commitIndex, pMsg->dataLen, s, trace ? trace->rootId : 0, trace ? trace->msgId : 0);
627
}
×
628

629
void syncLogRecvRequestVote(SSyncNode* pSyncNode, const SyncRequestVote* pMsg, int32_t voteGranted, const char* errmsg,
4,186✔
630
                            const char* opt, const STraceId* trace) {
631
  char statusMsg[64];
632
  snprintf(statusMsg, sizeof(statusMsg), "granted:%d", voteGranted);
4,186✔
633
  sNInfo(pSyncNode,
4,186!
634
         "%s sync-request-vote from dnode:%d, {term:%" PRId64 ", last-index:%" PRId64 ", last-term:%" PRId64
635
         "}, %s, QID:0x%" PRIx64 ":0x%" PRIx64,
636
         opt, DID(&pMsg->srcId), pMsg->term, pMsg->lastLogIndex, pMsg->lastLogTerm,
637
         (voteGranted != -1) ? statusMsg : errmsg, trace ? trace->rootId : 0, trace ? trace->msgId : 0);
638
}
4,186✔
639

640
void syncLogSendRequestVote(SSyncNode* pNode, const SyncRequestVote* pMsg, const char* s, const STraceId* trace) {
×
641
  sNInfo(pNode,
×
642
         "send sync-request-vote to dnode:%d {term:%" PRId64 ", last-index:%" PRId64 ", last-term:%" PRId64
643
         "}, %s, QID:0x%" PRIx64 ":0x%" PRIx64,
644
         DID(&pMsg->destId), pMsg->term, pMsg->lastLogIndex, pMsg->lastLogTerm, s, trace ? trace->rootId : 0,
645
         trace ? trace->msgId : 0);
646
}
×
647

648
void syncLogRecvRequestVoteReply(SSyncNode* pSyncNode, const SyncRequestVoteReply* pMsg, const char* s,
1,927✔
649
                                 const STraceId* trace) {
650
  sNInfo(pSyncNode,
1,927!
651
         "recv sync-request-vote-reply from dnode:%d {term:%" PRId64 ", grant:%d}, %s, QID:0x%" PRIx64 ":0x%" PRIx64,
652
         DID(&pMsg->srcId), pMsg->term, pMsg->voteGranted, s, trace ? trace->rootId : 0, trace ? trace->msgId : 0);
653
}
1,927✔
654

655
void syncLogSendRequestVoteReply(SSyncNode* pSyncNode, const SyncRequestVoteReply* pMsg, const char* s,
2,092✔
656
                                 const STraceId* trace) {
657
  sNInfo(pSyncNode,
2,092!
658
         "send sync-request-vote-reply to dnode:%d {term:%" PRId64 ", grant:%d}, %s, QID:0x%" PRIx64 ":0x%" PRIx64,
659
         DID(&pMsg->destId), pMsg->term, pMsg->voteGranted, s, trace ? trace->rootId : 0, trace ? trace->msgId : 0);
660
}
2,092✔
661

662
int32_t syncSnapInfoDataRealloc(SSnapshot* pSnap, int32_t size) {
120✔
663
  void* data = taosMemoryRealloc(pSnap->data, size);
120!
664
  if (data == NULL) {
120!
665
    return terrno;
×
666
  }
667
  pSnap->data = data;
120✔
668
  return 0;
120✔
669
}
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