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

taosdata / TDengine / #3638

11 Mar 2025 12:59PM UTC coverage: 3.066% (-18.3%) from 21.409%
#3638

push

travis-ci

web-flow
Merge pull request #30118 from taosdata/wl30

udpate ci workflow

5914 of 287117 branches covered (2.06%)

Branch coverage included in aggregate %.

11588 of 283747 relevant lines covered (4.08%)

142.17 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:%" PRIu64 " 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 char* s) {
×
440
  if (!(sDebugFlag & DEBUG_TRACE)) return;
×
441

442
  int64_t tsNow = taosGetTimestampMs();
×
443
  int64_t timeDIff = tsNow - pMsg->timeStamp;
×
444
  sNTrace(
×
445
      pSyncNode, "recv sync-timer {type:%s, lc:%" PRId64 ", ms:%d, ts:%" PRId64 ", elapsed:%" PRId64 ", data:%p}, %s",
446
      syncTimerTypeStr(pMsg->timeoutType), pMsg->logicClock, pMsg->timerMS, pMsg->timeStamp, timeDIff, pMsg->data, s);
447
}
448

449
void syncLogRecvLocalCmd(SSyncNode* pSyncNode, const SyncLocalCmd* pMsg, const char* s) {
×
450
  sNTrace(pSyncNode, "recv sync-local-cmd {cmd:%d-%s, sd-new-term:%" PRId64 ", fc-index:%" PRId64 "}, %s", pMsg->cmd,
×
451
          syncLocalCmdGetStr(pMsg->cmd), pMsg->currentTerm, pMsg->commitIndex, s);
452
}
×
453

454
void syncLogSendAppendEntriesReply(SSyncNode* pSyncNode, const SyncAppendEntriesReply* pMsg, const char* s) {
×
455
  sNTrace(pSyncNode,
×
456
          "send sync-append-entries-reply to dnode:%d, {term:%" PRId64 ", pterm:%" PRId64
457
          ", success:%d, lsend-index:%" PRId64 ", match:%" PRId64 "}, %s",
458
          DID(&pMsg->destId), pMsg->term, pMsg->lastMatchTerm, pMsg->success, pMsg->lastSendIndex, pMsg->matchIndex, s);
459
}
×
460

461
void syncLogRecvAppendEntriesReply(SSyncNode* pSyncNode, const SyncAppendEntriesReply* pMsg, const char* s) {
×
462
  sNTrace(pSyncNode,
×
463
          "recv sync-append-entries-reply from dnode:%d {term:%" PRId64 ", pterm:%" PRId64
464
          ", success:%d, lsend-index:%" PRId64 ", match:%" PRId64 "}, %s",
465
          DID(&pMsg->srcId), pMsg->term, pMsg->lastMatchTerm, pMsg->success, pMsg->lastSendIndex, pMsg->matchIndex, s);
466
}
×
467

468
void syncLogSendHeartbeat(SSyncNode* pSyncNode, const SyncHeartbeat* pMsg, bool printX, int64_t timerElapsed,
×
469
                          int64_t execTime) {
470
  if (sDebugFlag & DEBUG_TRACE) {
×
471
    char pBuf[TD_TIME_STR_LEN] = {0};
×
472
    if (pMsg->timeStamp > 0) {
×
473
      if (formatTimestampLocal(pBuf, pMsg->timeStamp, TSDB_TIME_PRECISION_MILLI) == NULL) {
×
474
        pBuf[0] = '\0';
×
475
      }
476
    }
477
    if (printX) {
×
478
      sHTrace(pSyncNode,
×
479
              "send sync-heartbeat to dnode:%d {term:%" PRId64 ", commit-index:%" PRId64 ", min-match:%" PRId64
480
              ", ts:%s}, x",
481
              DID(&pMsg->destId), pMsg->term, pMsg->commitIndex, pMsg->minMatchIndex, pBuf);
482
    } else {
483
      sHTrace(pSyncNode,
×
484
              "send sync-heartbeat to dnode:%d {term:%" PRId64 ", commit-index:%" PRId64 ", min-match:%" PRId64
485
              ", ts:%s}, timer-elapsed:%" PRId64 ", next-exec:%" PRId64,
486
              DID(&pMsg->destId), pMsg->term, pMsg->commitIndex, pMsg->minMatchIndex, pBuf, timerElapsed, execTime);
487
    }
488
  }
489
}
×
490

491
void syncLogRecvHeartbeat(SSyncNode* pSyncNode, const SyncHeartbeat* pMsg, int64_t timeDiff, const char* s) {
×
492
  if (timeDiff > SYNC_HEARTBEAT_SLOW_MS) {
×
493
    pSyncNode->hbSlowNum++;
×
494

495
    char pBuf[TD_TIME_STR_LEN] = {0};
×
496
    if (pMsg->timeStamp > 0) {
×
497
      if (formatTimestampLocal(pBuf, pMsg->timeStamp, TSDB_TIME_PRECISION_MILLI) == NULL) {
×
498
        pBuf[0] = '\0';
×
499
      }
500
    }
501

502
    sHError(pSyncNode,
×
503
            "recv sync-heartbeat from dnode:%d slow(%d ms) {term:%" PRId64 ", commit-index:%" PRId64
504
            ", min-match:%" PRId64 ", ts:%s}, QID:%s, net elapsed:%" PRId64 "ms",
505
            DID(&pMsg->srcId), SYNC_HEARTBEAT_SLOW_MS, pMsg->term, pMsg->commitIndex, pMsg->minMatchIndex, pBuf, s,
506
            timeDiff);
507
  } else {
508
    if (sDebugFlag & DEBUG_TRACE) {
×
509
      char pBuf[TD_TIME_STR_LEN] = {0};
×
510
      if (pMsg->timeStamp > 0) {
×
511
        if (formatTimestampLocal(pBuf, pMsg->timeStamp, TSDB_TIME_PRECISION_MILLI) == NULL) {
×
512
          pBuf[0] = '\0';
×
513
        }
514
      }
515
      sHTrace(pSyncNode,
×
516
              "recv sync-heartbeat from dnode:%d {term:%" PRId64 ", commit-index:%" PRId64 ", min-match:%" PRId64
517
              ", ts:%s}, QID:%s, net elapsed:%" PRId64 "ms",
518
              DID(&pMsg->srcId), pMsg->term, pMsg->commitIndex, pMsg->minMatchIndex, pBuf, s, timeDiff);
519
    }
520
  }
521
}
×
522

523
void syncLogSendHeartbeatReply(SSyncNode* pSyncNode, const SyncHeartbeatReply* pMsg, const char* s) {
×
524
  sHTrace(pSyncNode, "send sync-heartbeat-reply from dnode:%d {term:%" PRId64 ", ts:%" PRId64 "}, %s",
×
525
          DID(&pMsg->destId), pMsg->term, pMsg->timeStamp, s);
526
}
×
527

528
void syncLogRecvHeartbeatReply(SSyncNode* pSyncNode, const SyncHeartbeatReply* pMsg, int64_t timeDiff, const char* s) {
×
529
  if (timeDiff > SYNC_HEARTBEAT_REPLY_SLOW_MS) {
×
530
    pSyncNode->hbrSlowNum++;
×
531

532
    char pBuf[TD_TIME_STR_LEN] = {0};
×
533
    if (pMsg->timeStamp > 0) {
×
534
      if (formatTimestampLocal(pBuf, pMsg->timeStamp, TSDB_TIME_PRECISION_MILLI) == NULL) {
×
535
        pBuf[0] = '\0';
×
536
      }
537
    }
538

539
    sHError(pSyncNode,
×
540
            "recv sync-heartbeat-reply from dnode:%d slow(%d ms) {term:%" PRId64 ", ts:%s}, %s, net elapsed:%" PRId64,
541
            DID(&pMsg->srcId), SYNC_HEARTBEAT_REPLY_SLOW_MS, pMsg->term, pBuf, s, timeDiff);
542
  } else {
543
    if (sDebugFlag & DEBUG_TRACE) {
×
544
      char pBuf[TD_TIME_STR_LEN] = {0};
×
545
      if (pMsg->timeStamp > 0) {
×
546
        if (formatTimestampLocal(pBuf, pMsg->timeStamp, TSDB_TIME_PRECISION_MILLI) == NULL) {
×
547
          pBuf[0] = '\0';
×
548
        }
549
      }
550
      sHTrace(pSyncNode,
×
551
              "recv sync-heartbeat-reply from dnode:%d {term:%" PRId64 ", ts:%" PRId64 "}, %s, net elapsed:%" PRId64,
552
              DID(&pMsg->srcId), pMsg->term, pMsg->timeStamp, s, timeDiff);
553
    }
554
  }
555
}
×
556

557
void syncLogSendSyncSnapshotSend(SSyncNode* pSyncNode, const SyncSnapshotSend* pMsg, const char* s) {
×
558
  sNDebug(pSyncNode,
×
559
          "send sync-snapshot-send to dnode:%d, %s, seq:%d, term:%" PRId64 ", begin-index:%" PRId64
560
          ", last-index:%" PRId64 ", last-term:%" PRId64 ", start-time:%" PRId64,
561
          DID(&pMsg->destId), s, pMsg->seq, pMsg->term, pMsg->beginIndex, pMsg->lastIndex, pMsg->lastTerm,
562
          pMsg->startTime);
563
}
×
564

565
void syncLogRecvSyncSnapshotSend(SSyncNode* pSyncNode, const SyncSnapshotSend* pMsg, const char* s) {
×
566
  sNDebug(pSyncNode,
×
567
          "recv sync-snapshot-send from dnode:%d, %s, seq:%d, term:%" PRId64 ", begin-index:%" PRId64
568
          ", last-index:%" PRId64 ", last-term:%" PRId64 ", start-time:%" PRId64 ", data-len:%u",
569
          DID(&pMsg->srcId), s, pMsg->seq, pMsg->term, pMsg->beginIndex, pMsg->lastIndex, pMsg->lastTerm,
570
          pMsg->startTime, pMsg->dataLen);
571
}
×
572

573
void syncLogSendSyncSnapshotRsp(SSyncNode* pSyncNode, const SyncSnapshotRsp* pMsg, const char* s) {
×
574
  sNDebug(pSyncNode,
×
575
          "send sync-snapshot-rsp to dnode:%d, %s, acked:%d, term:%" PRId64 ", begin-index:%" PRId64
576
          ", last-index:%" PRId64 ", last-term:%" PRId64 ", start-time:%" PRId64,
577
          DID(&pMsg->destId), s, pMsg->ack, pMsg->term, pMsg->snapBeginIndex, pMsg->lastIndex, pMsg->lastTerm,
578
          pMsg->startTime);
579
}
×
580

581
void syncLogRecvSyncSnapshotRsp(SSyncNode* pSyncNode, const SyncSnapshotRsp* pMsg, const char* s) {
×
582
  sNDebug(pSyncNode,
×
583
          "recv sync-snapshot-rsp from dnode:%d, %s, ack:%d, term:%" PRId64 ", begin-index:%" PRId64
584
          ", last-index:%" PRId64 ", last-term:%" PRId64 ", start-time:%" PRId64,
585
          DID(&pMsg->srcId), s, pMsg->ack, pMsg->term, pMsg->snapBeginIndex, pMsg->lastIndex, pMsg->lastTerm,
586
          pMsg->startTime);
587
}
×
588

589
void syncLogRecvAppendEntries(SSyncNode* pSyncNode, const SyncAppendEntries* pMsg, const char* s) {
×
590
  sNTrace(pSyncNode,
×
591
          "recv sync-append-entries from dnode:%d {term:%" PRId64 ", prev-log:{index:%" PRId64 ", term:%" PRId64
592
          "}, commit-index:%" PRId64 ", datalen:%d}, %s",
593
          DID(&pMsg->srcId), pMsg->term, pMsg->prevLogIndex, pMsg->prevLogTerm, pMsg->commitIndex, pMsg->dataLen, s);
594
}
×
595

596
void syncLogSendAppendEntries(SSyncNode* pSyncNode, const SyncAppendEntries* pMsg, const char* s) {
×
597
  sNTrace(pSyncNode,
×
598
          "send sync-append-entries to dnode:%d, {term:%" PRId64 ", prev-log:{index:%" PRId64 ", term:%" PRId64
599
          "}, index:%" PRId64 ", commit-index:%" PRId64 ", datalen:%d}, %s",
600
          DID(&pMsg->destId), pMsg->term, pMsg->prevLogIndex, pMsg->prevLogTerm, (pMsg->prevLogIndex + 1),
601
          pMsg->commitIndex, pMsg->dataLen, s);
602
}
×
603

604
void syncLogRecvRequestVote(SSyncNode* pSyncNode, const SyncRequestVote* pMsg, int32_t voteGranted, const char* errmsg,
×
605
                            const char* opt) {
606
  char statusMsg[64];
607
  snprintf(statusMsg, sizeof(statusMsg), "granted:%d", voteGranted);
×
608
  sNInfo(pSyncNode,
×
609
         "%s sync-request-vote from dnode:%d, {term:%" PRId64 ", last-index:%" PRId64 ", last-term:%" PRId64 "}, %s",
610
         opt, DID(&pMsg->srcId), pMsg->term, pMsg->lastLogIndex, pMsg->lastLogTerm,
611
         (voteGranted != -1) ? statusMsg : errmsg);
612
}
×
613

614
void syncLogSendRequestVote(SSyncNode* pNode, const SyncRequestVote* pMsg, const char* s) {
×
615
  sNInfo(pNode,
×
616
         "send sync-request-vote to dnode:%d {term:%" PRId64 ", last-index:%" PRId64 ", last-term:%" PRId64 "}, %s",
617
         DID(&pMsg->destId), pMsg->term, pMsg->lastLogIndex, pMsg->lastLogTerm, s);
618
}
×
619

620
void syncLogRecvRequestVoteReply(SSyncNode* pSyncNode, const SyncRequestVoteReply* pMsg, const char* s) {
×
621
  sNInfo(pSyncNode, "recv sync-request-vote-reply from dnode:%d {term:%" PRId64 ", grant:%d}, %s", DID(&pMsg->srcId),
×
622
         pMsg->term, pMsg->voteGranted, s);
623
}
×
624

625
void syncLogSendRequestVoteReply(SSyncNode* pSyncNode, const SyncRequestVoteReply* pMsg, const char* s) {
×
626
  sNInfo(pSyncNode, "send sync-request-vote-reply to dnode:%d {term:%" PRId64 ", grant:%d}, %s", DID(&pMsg->destId),
×
627
         pMsg->term, pMsg->voteGranted, s);
628
}
×
629

630
int32_t syncSnapInfoDataRealloc(SSnapshot* pSnap, int32_t size) {
×
631
  void* data = taosMemoryRealloc(pSnap->data, size);
×
632
  if (data == NULL) {
×
633
    return terrno;
×
634
  }
635
  pSnap->data = data;
×
636
  return 0;
×
637
}
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