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

taosdata / TDengine / #3842

07 Apr 2025 11:21AM UTC coverage: 62.696% (-0.3%) from 63.027%
#3842

push

travis-ci

web-flow
merge: from main to 3.0 branch (#30679)

154855 of 315075 branches covered (49.15%)

Branch coverage included in aggregate %.

6 of 8 new or added lines in 5 files covered. (75.0%)

2309 existing lines in 130 files now uncovered.

240176 of 314995 relevant lines covered (76.25%)

19119980.29 hits per line

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

57.6
/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) {
192,046✔
30
  int32_t len = tsnprintf(buf, bufLen, "{num:%d, as:%d, [", pCfg->replicaNum, pCfg->myIndex);
192,046✔
31
  for (int32_t i = 0; i < pCfg->replicaNum; ++i) {
477,436✔
32
    len += tsnprintf(buf + len, bufLen - len, "%s:%d", pCfg->nodeInfo[i].nodeFqdn, pCfg->nodeInfo[i].nodePort);
285,391✔
33
    if (i < pCfg->replicaNum - 1) {
285,394✔
34
      len += tsnprintf(buf + len, bufLen - len, "%s", ", ");
93,353✔
35
    }
36
  }
37
  len += tsnprintf(buf + len, bufLen - len, "%s", "]}");
192,045✔
38
}
192,043✔
39

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

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

51
  for (int32_t i = 0; i < FQDNRETRYTIMES; i++) {
22,495!
52
    int32_t code = taosGetIpv4FromFqdn(pInfo->nodeFqdn, &ipv4);
22,495✔
53
    if (code) {
22,493!
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;
22,493✔
58
    }
59
  }
60

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

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

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

77
bool syncUtilSameId(const SRaftId* pId1, const SRaftId* pId2) {
28,721,168✔
78
  if (pId1->addr == pId2->addr && pId1->vgId == pId2->vgId) {
28,721,168!
79
    return true;
19,851,237✔
80
  }
81

82
  if ((CID(pId1) == 0 || CID(pId2) == 0) && (DID(pId1) == DID(pId2)) && pId1->vgId == pId2->vgId) {
8,869,931!
83
    return true;
196✔
84
  }
85

86
  return false;
8,869,735✔
87
}
88

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

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

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

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

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

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

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

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

129
// for leader
130
static void syncHearbeatReplyTime2Str(SSyncNode* pSyncNode, char* buf, int32_t bufLen, bool formatTime) {
104,337✔
131
  int32_t len = 0;
104,337✔
132
  len += tsnprintf(buf + len, bufLen - len, "%s", "{");
104,337✔
133
  for (int32_t i = 0; i < pSyncNode->replicaNum; ++i) {
258,438✔
134
    int64_t tsMs = syncIndexMgrGetRecvTime(pSyncNode->pMatchIndex, &(pSyncNode->replicasId[i]));
154,096✔
135
    syncPrintTime(formatTime, &len, tsMs, i, buf, bufLen);
154,097✔
136
    if (i < pSyncNode->replicaNum - 1) {
154,094✔
137
      len += tsnprintf(buf + len, bufLen - len, "%s", ",");
49,755✔
138
    }
139
  }
140
  len += tsnprintf(buf + len, bufLen - len, "%s", "}");
104,342✔
141
}
104,344✔
142

143
static void syncSentHearbeatTime2Str(SSyncNode* pSyncNode, char* buf, int32_t bufLen, bool formatTime) {
104,343✔
144
  int32_t len = 0;
104,343✔
145
  len += tsnprintf(buf + len, bufLen - len, "%s", "{");
104,343✔
146
  for (int32_t i = 0; i < pSyncNode->replicaNum; ++i) {
258,442✔
147
    int64_t tsMs = syncIndexMgrGetSentTime(pSyncNode->pMatchIndex, &(pSyncNode->replicasId[i]));
154,099✔
148
    syncPrintTime(formatTime, &len, tsMs, i, buf, bufLen);
154,100✔
149
    if (i < pSyncNode->replicaNum - 1) {
154,097✔
150
      len += tsnprintf(buf + len, bufLen - len, "%s", ",");
49,755✔
151
    }
152
  }
153
  len += tsnprintf(buf + len, bufLen - len, "%s", "}");
104,343✔
154
}
104,346✔
155

156
// for follower
157
static void syncHearbeatTime2Str(SSyncNode* pSyncNode, char* buf, int32_t bufLen, bool formatTime) {
104,334✔
158
  int32_t len = 0;
104,334✔
159
  len += tsnprintf(buf + len, bufLen - len, "%s", "{");
104,334✔
160
  for (int32_t i = 0; i < pSyncNode->replicaNum; ++i) {
258,440✔
161
    int64_t tsMs = syncIndexMgrGetRecvTime(pSyncNode->pNextIndex, &(pSyncNode->replicasId[i]));
154,086✔
162
    syncPrintTime(formatTime, &len, tsMs, i, buf, bufLen);
154,096✔
163
    if (i < pSyncNode->replicaNum - 1) {
154,102✔
164
      len += tsnprintf(buf + len, bufLen - len, "%s", ",");
49,756✔
165
    }
166
  }
167
  len += tsnprintf(buf + len, bufLen - len, "%s", "}");
104,354✔
168
}
104,343✔
169

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

180
static void syncLogReplStates2Str(SSyncNode* pSyncNode, char* buf, int32_t bufLen) {
104,339✔
181
  int32_t len = 0;
104,339✔
182
  len += tsnprintf(buf + len, bufLen - len, "%s", "{");
104,339✔
183
  for (int32_t i = 0; i < pSyncNode->replicaNum; i++) {
258,443✔
184
    SSyncLogReplMgr* pMgr = pSyncNode->logReplMgrs[i];
154,097✔
185
    if (pMgr == NULL) break;
154,097!
186
    len += tsnprintf(buf + len, bufLen - len, "%d:%d [%" PRId64 ", %" PRId64 ", %" PRId64 "] ", i, pMgr->restored,
154,097✔
187
                     pMgr->startIndex, pMgr->matchIndex, pMgr->endIndex);
188
    len += tsnprintf(buf + len, bufLen - len, "%" PRId64, pMgr->sendCount);
154,095✔
189
    if (i + 1 < pSyncNode->replicaNum) {
154,100✔
190
      len += tsnprintf(buf + len, bufLen - len, "%s", ", ");
49,754✔
191
    }
192
  }
193
  len += tsnprintf(buf + len, bufLen - len, "%s", "}");
104,346✔
194
}
104,344✔
195

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

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

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

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

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

228
  int32_t cacheHit = pNode->pLogStore->cacheHit;
104,341✔
229
  int32_t cacheMiss = pNode->pLogStore->cacheMiss;
104,341✔
230

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

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

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

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

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

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

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

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

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

264
  if (pNode != NULL) {
104,343!
265
    taosPrintLog(
104,332✔
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, "
274
        "msg[sent:%" PRId64 ", recv:%" PRId64 ", slow-recv:%" PRId64 "]",
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,
104,336✔
280
        syncNodeDynamicQuorum(pNode), pNode->electTimerLogicClock, pNode->heartbeatTimerLogicClockUser, bufferStatesStr,
281
        replMgrStatesStr, cfgStr, sentHbTimeStr, hbTimeStr, hbrTimeStr, pNode->arbToken, pNode->sendCount,
104,344✔
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, "
331
        "msg[sent:%" PRId64 ", recv:%" PRId64 ", slow-recv:%" PRId64 "]",
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,
81,951✔
342
                                const char* format, ...) {
343
  SSyncNode* pNode = pSender->pSyncNode;
81,951✔
344
  if (pNode == NULL || pNode->pLogStore == NULL) return;
81,951!
345

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

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

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

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

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

370
  taosPrintLog(flags, level, dflag,
81,969✔
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,
81,969✔
384
               pSender->replicaIndex, DID(&pNode->replicasId[pSender->replicaIndex]), raftStoreGetTerm(pNode),
81,969✔
385
               pNode->commitIndex, logBeginIndex, logLastIndex, pNode->minMatchIndex, snapshot.lastApplyIndex,
386
               snapshot.lastApplyTerm, pNode->raftCfg.isStandBy, pNode->raftCfg.batchSize, pNode->replicaNum,
81,963✔
387
               pNode->raftCfg.lastConfigIndex, pNode->changing, pNode->restoreFinish, pNode->quorum, peerStr, cfgStr);
81,963✔
388
}
389

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

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

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

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

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

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

419
  taosPrintLog(
5,738✔
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,738✔
430
      pReceiver->ack, pReceiver->pRcvBuf->start, pReceiver->pRcvBuf->cursor, pReceiver->pRcvBuf->end,
5,738✔
431
      DID(&pReceiver->fromId), pReceiver->snapshotParam.start, pReceiver->snapshotParam.end,
5,738✔
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,738✔
435
      pNode->replicaNum, pNode->raftCfg.lastConfigIndex, pNode->changing, pNode->restoreFinish, pNode->quorum, peerStr,
5,738✔
436
      cfgStr);
437
}
438

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

442
  int64_t tsNow = taosGetTimestampMs();
6,588✔
443
  int64_t timeDIff = tsNow - pMsg->timeStamp;
6,588✔
444
  sNTrace(pSyncNode,
6,588!
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,539✔
452
  sNTrace(pSyncNode,
34,539!
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,539✔
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,
40,247✔
478
                          int64_t execTime) {
479
  if (sDebugFlag & DEBUG_TRACE) {
40,247!
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
}
40,247✔
499

500
void syncLogRecvHeartbeat(SSyncNode* pSyncNode, const SyncHeartbeat* pMsg, int64_t timeDiff, const STraceId* trace) {
34,724✔
501
  if (timeDiff > SYNC_HEARTBEAT_SLOW_MS) {
34,724!
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) {
34,724!
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
}
34,724✔
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,
33,946✔
542
                               const STraceId* trace) {
543
  if (timeDiff > SYNC_HEARTBEAT_REPLY_SLOW_MS) {
33,946!
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) {
33,946!
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
}
33,946✔
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,
2✔
612
                              const STraceId* trace) {
613
  sNTrace(pSyncNode,
2!
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
}
2✔
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,242✔
630
                            const char* opt, const STraceId* trace) {
631
  char statusMsg[64];
632
  snprintf(statusMsg, sizeof(statusMsg), "granted:%d", voteGranted);
4,242✔
633
  sNInfo(pSyncNode,
4,242!
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,242✔
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,967✔
649
                                 const STraceId* trace) {
650
  sNInfo(pSyncNode,
1,967!
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,967✔
654

655
void syncLogSendRequestVoteReply(SSyncNode* pSyncNode, const SyncRequestVoteReply* pMsg, const char* s,
2,120✔
656
                                 const STraceId* trace) {
657
  sNInfo(pSyncNode,
2,120!
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,120✔
661

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