• 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/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) {
×
30
  int32_t len = tsnprintf(buf, bufLen, "{num:%d, as:%d, [", pCfg->replicaNum, pCfg->myIndex);
×
31
  for (int32_t i = 0; i < pCfg->replicaNum; ++i) {
×
32
    len += tsnprintf(buf + len, bufLen - len, "%s:%d", pCfg->nodeInfo[i].nodeFqdn, pCfg->nodeInfo[i].nodePort);
×
33
    if (i < pCfg->replicaNum - 1) {
×
34
      len += tsnprintf(buf + len, bufLen - len, "%s", ", ");
×
35
    }
36
  }
37
  len += tsnprintf(buf + len, bufLen - len, "%s", "]}");
×
38
}
×
39

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

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

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

61
  if (ipv4 == 0xFFFFFFFF || ipv4 == 1) {
×
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};
×
68
  taosInetNtoa(ipbuf, ipv4);
×
69
  raftId->addr = SYNC_ADDR(pInfo);
×
70
  raftId->vgId = vgId;
×
71

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

77
bool syncUtilSameId(const SRaftId* pId1, const SRaftId* pId2) {
×
78
  if (pId1->addr == pId2->addr && pId1->vgId == pId2->vgId) {
×
79
    return true;
×
80
  }
81

82
  if ((CID(pId1) == 0 || CID(pId2) == 0) && (DID(pId1) == DID(pId2)) && pId1->vgId == pId2->vgId) {
×
83
    return true;
×
84
  }
85

86
  return false;
×
87
}
88

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

91
static inline int32_t syncUtilRand(int32_t max) { return taosRand() % max; }
×
92

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

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

100
int32_t syncUtilQuorum(int32_t replicaNum) { return replicaNum / 2 + 1; }
×
101

102
void syncUtilMsgHtoN(void* msg) {
×
103
  SMsgHead* pHead = msg;
×
104
  pHead->contLen = htonl(pHead->contLen);
×
105
  pHead->vgId = htonl(pHead->vgId);
×
106
}
×
107

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

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

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

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

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

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

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

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

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

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

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

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

228
  int32_t cacheHit = pNode->pLogStore->cacheHit;
×
229
  int32_t cacheMiss = pNode->pLogStore->cacheMiss;
×
230

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

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

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

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

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

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

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

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

260
  // restore error code
261
  terrno = errCode;
×
262
  SyncIndex appliedIndex = pNode->pFsm->FpAppliedIndexCb(pNode->pFsm);  // vnodeSyncAppliedIndex
×
263

264
  if (pNode != NULL) {
×
265
    taosPrintLog(
×
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,
×
280
        syncNodeDynamicQuorum(pNode), pNode->electTimerLogicClock, pNode->heartbeatTimerLogicClockUser, bufferStatesStr,
281
        replMgrStatesStr, cfgStr, sentHbTimeStr, hbTimeStr, hbrTimeStr, pNode->arbToken, pNode->sendCount,
×
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,
×
342
                                const char* format, ...) {
343
  SSyncNode* pNode = pSender->pSyncNode;
×
344
  if (pNode == NULL || pNode->pLogStore == NULL) return;
×
345

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

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

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

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

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

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

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

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

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

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

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

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

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

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

442
  int64_t tsNow = taosGetTimestampMs();
×
443
  int64_t timeDIff = tsNow - pMsg->timeStamp;
×
444
  sNTrace(pSyncNode,
×
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) {
×
452
  sNTrace(pSyncNode,
×
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
}
×
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,
×
478
                          int64_t execTime) {
479
  if (sDebugFlag & DEBUG_TRACE) {
×
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
}
×
499

500
void syncLogRecvHeartbeat(SSyncNode* pSyncNode, const SyncHeartbeat* pMsg, int64_t timeDiff, const STraceId* trace) {
×
501
  if (timeDiff > SYNC_HEARTBEAT_SLOW_MS) {
×
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) {
×
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
}
×
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,
×
542
                               const STraceId* trace) {
543
  if (timeDiff > SYNC_HEARTBEAT_REPLY_SLOW_MS) {
×
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) {
×
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
}
×
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,
×
594
                                const STraceId* trace) {
595
  sNDebug(pSyncNode,
×
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
}
×
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,
×
612
                              const STraceId* trace) {
613
  sNTrace(pSyncNode,
×
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
}
×
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,
×
630
                            const char* opt, const STraceId* trace) {
631
  char statusMsg[64];
632
  snprintf(statusMsg, sizeof(statusMsg), "granted:%d", voteGranted);
×
633
  sNInfo(pSyncNode,
×
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
}
×
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,
×
649
                                 const STraceId* trace) {
650
  sNInfo(pSyncNode,
×
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
}
×
654

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

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