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

taosdata / TDengine / #5139

05 Nov 2026 03:32AM UTC coverage: 74.033% (+0.09%) from 73.942%
#5139

push

travis-ci

jbjia
fix: capture coverage once from per-thread gcda

Per-case lcov walked the shared build tree and kept each case busy for minutes. Pass GCOV_PREFIX into the test container so each thread writes its own counters, then merge and capture after the suite.

373718 of 504798 relevant lines covered (74.03%)

4155647.08 hits per line

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

70.05
/source/dnode/vnode/src/vnd/vnodeSync.c
1
/*
2
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
3
 *
4
 * SPDX-License-Identifier: AGPL-3.0-or-later
5
 *
6
 * This program is free software: you can use, redistribute, and/or modify
7
 * it under the terms of the GNU Affero General Public License, version 3
8
 * or later ("AGPL"), as published by the Free Software Foundation.
9
 *
10
 * This program is distributed in the hope that it will be useful, but WITHOUT
11
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12
 * FITNESS FOR A PARTICULAR PURPOSE.
13
 *
14
 * You should have received a copy of the GNU Affero General Public License
15
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16
 */
17

18
#define _DEFAULT_SOURCE
19
#include "sync.h"
20
#include "tq.h"
21
#include "tsdb.h"
22
#include "vnd.h"
23
#include "stream.h"
24

25
#define BATCH_ENABLE 0
26

27
// static int32_t inline vnodeShouldRewriteSubmitMsg(SVnode *pVnode, SRpcMsg **pMsg);
28
static inline bool vnodeIsMsgWeak(tmsg_t type) { return false; }
1,575,990 ✔
29

30
static inline void vnodeWaitBlockMsg(SVnode *pVnode, const SRpcMsg *pMsg) {
9,151 ✔
31
  vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, wait block, type:%s sec:%d seq:%" PRId64, pVnode->config.vgId, pMsg,
9,151 ✔
32
          TMSG_INFO(pMsg->msgType), pVnode->blockSec, pVnode->blockSeq);
33
  if (tsem_wait(&pVnode->syncSem) != 0) {
9,151 ✔
34
    vError("vgId:%d, failed to wait sem", pVnode->config.vgId);
×
35
  }
36
}
9,151 ✔
37

38
static inline void vnodePostBlockMsg(SVnode *pVnode, const SRpcMsg *pMsg) {
794,028 ✔
39
  if (vnodeIsMsgBlock(pMsg->msgType)) {
794,028 ✔
40
    (void)taosThreadMutexLock(&pVnode->lock);
39,948 ✔
41
    if (pVnode->blocked) {
39,948 ✔
42
      vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, post block, type:%s sec:%d seq:%" PRId64, pVnode->config.vgId, pMsg,
9,130 ✔
43
              TMSG_INFO(pMsg->msgType), pVnode->blockSec, pVnode->blockSeq);
44
      pVnode->blocked = false;
9,130 ✔
45
      pVnode->blockSec = 0;
9,130 ✔
46
      pVnode->blockSeq = 0;
9,130 ✔
47
      if (tsem_post(&pVnode->syncSem) != 0) {
9,130 ✔
48
        vError("vgId:%d, failed to post sem", pVnode->config.vgId);
×
49
      }
50
    }
51
    (void)taosThreadMutexUnlock(&pVnode->lock);
39,950 ✔
52
  }
53
}
794,049 ✔
54

55
void vnodeRedirectRpcMsg(SVnode *pVnode, SRpcMsg *pMsg, int32_t code) {
37,020 ✔
56
  SEpSet newEpSet = {0};
37,020 ✔
57
  syncGetRetryEpSet(pVnode->sync, &newEpSet);
37,020 ✔
58

59
  vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is redirect since not leader, numOfEps:%d inUse:%d",
37,022 ✔
60
          pVnode->config.vgId, pMsg, newEpSet.numOfEps, newEpSet.inUse);
61
  for (int32_t i = 0; i < newEpSet.numOfEps; ++i) {
143,938 ✔
62
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, redirect:%d ep:%s:%u", pVnode->config.vgId, pMsg, i,
106,916 ✔
63
            newEpSet.eps[i].fqdn, newEpSet.eps[i].port);
64
  }
65
  pMsg->info.hasEpSet = 1;
37,022 ✔
66

67
  if (code == 0) code = TSDB_CODE_SYN_NOT_LEADER;
37,022 ✔
68

69
  SRpcMsg rsp = {.code = code, .info = pMsg->info, .msgType = pMsg->msgType + 1};
37,022 ✔
70
  int32_t contLen = tSerializeSEpSet(NULL, 0, &newEpSet);
37,022 ✔
71

72
  rsp.pCont = rpcMallocCont(contLen);
37,021 ✔
73
  if (rsp.pCont == NULL) {
37,022 ✔
74
    pMsg->code = TSDB_CODE_OUT_OF_MEMORY;
×
75
  } else {
76
    if (tSerializeSEpSet(rsp.pCont, contLen, &newEpSet) < 0) {
37,022 ✔
77
      vError("vgId:%d, failed to serialize ep set", pVnode->config.vgId);
×
78
    }
79
    rsp.contLen = contLen;
37,020 ✔
80
  }
81

82
  tmsgSendRsp(&rsp);
37,020 ✔
83
}
37,022 ✔
84

85
static void inline vnodeHandleWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
1,339,742 ✔
86
  int32_t code = 0;
1,339,742 ✔
87
  SRpcMsg rsp = {.code = pMsg->code, .info = pMsg->info};
1,339,742 ✔
88

89
  if (vnodeProcessWriteMsg(pVnode, pMsg, pMsg->info.conn.applyIndex, &rsp) < 0) {
1,339,743 ✔
90
    rsp.code = terrno;
17 ✔
91
    vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to apply right now since %s", pVnode->config.vgId, pMsg,
17 ✔
92
            terrstr());
93
  }
94
  if (rsp.info.handle != NULL) {
1,339,758 ✔
95
    tmsgSendRsp(&rsp);
1,335,580 ✔
96
  } else {
97
    if (rsp.pCont) {
4,178 ✔
98
      rpcFreeCont(rsp.pCont);
546 ✔
99
    }
100
  }
101
}
1,339,741 ✔
102

103
static void vnodeHandleProposeError(SVnode *pVnode, SRpcMsg *pMsg, int32_t code) {
67,593 ✔
104
  if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING) {
67,593 ✔
105
    vnodeRedirectRpcMsg(pVnode, pMsg, code);
31,133 ✔
106
  } else if (code == TSDB_CODE_MSG_PREPROCESSED) {
36,460 ✔
107
    SRpcMsg rsp = {.code = TSDB_CODE_SUCCESS, .info = pMsg->info};
35,879 ✔
108
    if (rsp.info.handle != NULL) {
35,881 ✔
109
      tmsgSendRsp(&rsp);
35,905 ✔
110
    }
111
  } else {
112
    vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to propose since %s, code:0x%x", pVnode->config.vgId, pMsg,
581 ✔
113
            tstrerror(code), code);
114
    SRpcMsg rsp = {.code = code, .info = pMsg->info};
581 ✔
115
    if (rsp.info.handle != NULL) {
580 ✔
116
      tmsgSendRsp(&rsp);
137 ✔
117
    }
118
  }
119
}
67,636 ✔
120

121
static int32_t tEncodeSubSubmitReq2(SEncoder *pEncoder, SSubmitTbData *pSubmitTbData) {
×
122
  int32_t code = 0;
×
123
  int32_t lino = 0;
×
124
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
×
125

126
  TAOS_CHECK_EXIT(tEncodeI32v(pEncoder, pSubmitTbData->flags));
×
127
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pSubmitTbData->suid));
×
128
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pSubmitTbData->uid));
×
129
  TAOS_CHECK_EXIT(tEncodeI32v(pEncoder, pSubmitTbData->sver));
×
130
  TAOS_CHECK_EXIT(tEncodeU64v(pEncoder, taosArrayGetSize(pSubmitTbData->aRowP)));
×
131

132
  int32_t nRow = taosArrayGetSize(pSubmitTbData->aRowP);
×
133
  SRow  **rows = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
×
134
  for (int32_t iRow = 0; iRow < nRow; iRow++) {
×
135
    TAOS_CHECK_EXIT(tEncodeRow(pEncoder, rows[iRow]));
×
136
  }
137

138
  if (pSubmitTbData->ctimeMs > 0) {
×
139
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pSubmitTbData->ctimeMs));
×
140
  }
141
  tEndEncode(pEncoder);
×
142
_exit:
×
143
  return code;
×
144
}
145
static int32_t tEncodeSubmitReq2(SEncoder *pEncoder, SSubmitReq2 *pReq) {
×
146
  int32_t code = 0;
×
147
  int32_t lino = 0;
×
148

149
  if (tStartEncode(pEncoder) < 0) {
×
150
    code = TSDB_CODE_INVALID_MSG;
×
151
    TSDB_CHECK_CODE(code, lino, _exit);
×
152
  }
153

154
  TAOS_CHECK_EXIT(tEncodeU64v(pEncoder, taosArrayGetSize(pReq->aSubmitTbData)));
×
155

156
  for (int32_t i = 0; i < taosArrayGetSize(pReq->aSubmitTbData); i++) {
×
157
    if (tEncodeSubSubmitReq2(pEncoder, taosArrayGet(pReq->aSubmitTbData, i)) < 0) {
×
158
      code = TSDB_CODE_INVALID_MSG;
×
159
      goto _exit;
×
160
    }
161
  }
162
  tEndEncode(pEncoder);
×
163
_exit:
×
164
  if (code != 0) {
×
165
    vDebug("failed to encode submit req since %s", tstrerror(code));
×
166
  }
167
  return code;
×
168
}
169
static int32_t tEncodeSubSubmitAndUpdate(SVnode *pVnode, SEncoder *pEncoder, SSubmitTbData *pSubmitTbData) {
×
170
  uint8_t hasBlob = 0;
×
171
  int32_t code = 0;
×
172
  int32_t lino = 0;
×
173

174
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
×
175

176
  TAOS_CHECK_EXIT(tEncodeI32v(pEncoder, pSubmitTbData->flags));
×
177
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pSubmitTbData->suid));
×
178
  TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pSubmitTbData->uid));
×
179
  TAOS_CHECK_EXIT(tEncodeI32v(pEncoder, pSubmitTbData->sver));
×
180
  TAOS_CHECK_EXIT(tEncodeU64v(pEncoder, taosArrayGetSize(pSubmitTbData->aRowP)));
×
181
  if (pSubmitTbData->flags & SUBMIT_REQ_WITH_BLOB) {
×
182
    hasBlob = 1;
×
183
  }
184

185
  if (hasBlob) {
×
186
    int32_t    nr = 0;
×
187
    uint64_t   seq = 0;
×
188
    SBlobSet  *pBlobSet = pSubmitTbData->pBlobSet;
×
189

190
    SRow  **pRow = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
×
191
    int32_t sz = taosArrayGetSize(pBlobSet->pSeqTable);
×
192
    for (int32_t i = 0; i < sz; i++) {
×
193
      SBlobValue *p = taosArrayGet(pBlobSet->pSeqTable, i);
×
194

195
      // code = bseAppend(pVnode->pBse, &seq, pBlobSet->data + p->offset, p->len);
196
      memcpy(pRow[i]->data + p->dataOffset, (void *)&seq, sizeof(uint64_t));
×
197
    }
198
  }
199
  int32_t nRow = taosArrayGetSize(pSubmitTbData->aRowP);
×
200
  SRow  **rows = (SRow **)TARRAY_DATA(pSubmitTbData->aRowP);
×
201
  for (int32_t iRow = 0; iRow < nRow; iRow++) {
×
202
    TAOS_CHECK_EXIT(tEncodeRow(pEncoder, rows[iRow]));
×
203
  }
204

205
  if (pSubmitTbData->ctimeMs > 0) {
×
206
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pSubmitTbData->ctimeMs));
×
207
  }
208
  tEndEncode(pEncoder);
×
209
_exit:
×
210
  return code;
×
211
}
212
// Exposed (declared in vnode.h) so vnodeSvr.c's vnodePreProcessDeleteMsg can propose the N-1 extra
213
// point-delete sub-messages a column-filter DELETE splits into, in addition to the one pMsg this
214
// same write-queue iteration already proposes via the call below.
215
int32_t vnodeProposeMsg(SVnode *pVnode, SRpcMsg *pMsg, bool isWeak) {
1,516,417 ✔
216
  int32_t code = 0;
1,516,417 ✔
217
  int64_t seq = 0;
1,516,417 ✔
218

219
  (void)taosThreadMutexLock(&pVnode->lock);
1,516,423 ✔
220
  code = syncPropose(pVnode->sync, pMsg, isWeak, &seq);
1,516,487 ✔
221
  bool wait = (code == 0 && vnodeIsMsgBlock(pMsg->msgType));
1,516,483 ✔
222
  if (wait) {
1,515,411 ✔
223
    if (pVnode->blocked) {
9,151 ✔
224
      (void)taosThreadMutexUnlock(&pVnode->lock);
×
225
      return TSDB_CODE_INTERNAL_ERROR;
×
226
    }
227
    pVnode->blocked = true;
9,151 ✔
228
    pVnode->blockSec = taosGetTimestampSec();
9,151 ✔
229
    pVnode->blockSeq = seq;
9,151 ✔
230
  }
231
  (void)taosThreadMutexUnlock(&pVnode->lock);
1,516,483 ✔
232

233
  if (code > 0) {
1,516,490 ✔
234
    // Single-node optimized path: FpCommitCb (vnodeSyncApplyMsg) is bypassed.
235
    // Feed committed entry into the txn WAL cache here before apply.
236
    // applyIndex was set by syncNodeOnClientRequest; txnId was set by PreProcess.
237
    if (pMsg->info.txnId != 0 && pVnode->pTxnWalMgr != NULL) {
1,339,774 ✔
238
      int32_t rc = txnMgrProducerPut(pVnode->pTxnWalMgr, pMsg->info.txnId, pMsg->info.conn.applyIndex,
7,498 ✔
239
                                     pMsg->msgType, pMsg->pCont, pMsg->contLen);
7,498 ✔
240
      if (rc != 0) {
7,498 ✔
241
        vError("vnodeProposeMsg optimized: txnMgrProducerPut failed vgId:%d txnId:%" PRId64 " since %s",
×
242
               TD_VID(pVnode), pMsg->info.txnId, tstrerror(rc));
243
      }
244
    }
245
    vnodeHandleWriteMsg(pVnode, pMsg);
1,339,774 ✔
246
  } else if (code < 0) {
176,716 ✔
247
    if (terrno != 0) code = terrno;
5,809 ✔
248
    vnodeHandleProposeError(pVnode, pMsg, code);
5,809 ✔
249
  }
250

251
  if (wait) vnodeWaitBlockMsg(pVnode, pMsg);
1,516,432 ✔
252

253
  // if (rewrite) {
254
  //   rpcFreeCont(newMsg.pCont);
255
  //   newMsg.pCont = NULL;
256
  // }
257
  return code;
1,515,368 ✔
258
}
259

260
void vnodeProposeCommitOnNeed(SVnode *pVnode, bool atExit) {
1,563,384 ✔
261
  if (!vnodeShouldCommit(pVnode, atExit)) {
1,563,384 ✔
262
    return;
1,554,885 ✔
263
  }
264

265
  int32_t   contLen = sizeof(SMsgHead);
8,531 ✔
266
  SMsgHead *pHead = rpcMallocCont(contLen);
8,531 ✔
267
  pHead->contLen = contLen;
8,531 ✔
268
  pHead->vgId = pVnode->config.vgId;
8,531 ✔
269

270
  SRpcMsg rpcMsg = {0};
8,531 ✔
271
  rpcMsg.msgType = TDMT_VND_COMMIT;
8,531 ✔
272
  rpcMsg.contLen = contLen;
8,531 ✔
273
  rpcMsg.pCont = pHead;
8,531 ✔
274
  rpcMsg.info.noResp = 1;
8,531 ✔
275

276
  vInfo("vgId:%d, propose vnode commit", pVnode->config.vgId);
8,531 ✔
277
  bool isWeak = false;
8,531 ✔
278

279
  if (!atExit) {
8,531 ✔
280
    if (vnodeProposeMsg(pVnode, &rpcMsg, isWeak) < 0) {
185 ✔
281
      vTrace("vgId:%d, failed to propose vnode commit since %s", pVnode->config.vgId, terrstr());
×
282
    }
283
    rpcFreeCont(rpcMsg.pCont);
185 ✔
284
    rpcMsg.pCont = NULL;
185 ✔
285
  } else {
286
    int32_t code = 0;
8,346 ✔
287
    if ((code = tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &rpcMsg)) < 0) {
8,346 ✔
288
      vError("vgId:%d, failed to put vnode commit to write_queue since %s", pVnode->config.vgId, tstrerror(code));
4,389 ✔
289
    }
290
  }
291
}
292

293
#if BATCH_ENABLE
294

295
static void inline vnodeProposeBatchMsg(SVnode *pVnode, SRpcMsg **pMsgArr, bool *pIsWeakArr, int32_t *arrSize) {
296
  if (*arrSize <= 0) return;
297
  SRpcMsg *pLastMsg = pMsgArr[*arrSize - 1];
298

299
  (void)taosThreadMutexLock(&pVnode->lock);
300
  int32_t code = syncProposeBatch(pVnode->sync, pMsgArr, pIsWeakArr, *arrSize);
301
  bool    wait = (code == 0 && vnodeIsBlockMsg(pLastMsg->msgType));
302
  if (wait) {
303
    pVnode->blocked = true;
304
  }
305
  (void)taosThreadMutexUnlock(&pVnode->lock);
306

307
  if (code > 0) {
308
    for (int32_t i = 0; i < *arrSize; ++i) {
309
      vnodeHandleWriteMsg(pVnode, pMsgArr[i]);
310
    }
311
  } else if (code < 0) {
312
    if (terrno != 0) code = terrno;
313
    for (int32_t i = 0; i < *arrSize; ++i) {
314
      vnodeHandleProposeError(pVnode, pMsgArr[i], code);
315
    }
316
  }
317

318
  if (wait) vnodeWaitBlockMsg(pVnode, pLastMsg);
319
  pLastMsg = NULL;
320

321
  for (int32_t i = 0; i < *arrSize; ++i) {
322
    SRpcMsg        *pMsg = pMsgArr[i];
323
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x", pVnode->config.vgId, pMsg, code);
324
    rpcFreeCont(pMsg->pCont);
325
    taosFreeQitem(pMsg);
326
  }
327

328
  *arrSize = 0;
329
}
330

331
void vnodeProposeWriteMsg(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
332
  SVnode   *pVnode = pInfo->ahandle;
333
  int32_t   vgId = pVnode->config.vgId;
334
  int32_t   code = 0;
335
  SRpcMsg  *pMsg = NULL;
336
  int32_t   arrayPos = 0;
337
  SRpcMsg **pMsgArr = taosMemoryCalloc(numOfMsgs, sizeof(SRpcMsg *));
338
  bool     *pIsWeakArr = taosMemoryCalloc(numOfMsgs, sizeof(bool));
339
  vTrace("vgId:%d, get %d msgs from vnode-write queue", vgId, numOfMsgs);
340

341
  for (int32_t msg = 0; msg < numOfMsgs; msg++) {
342
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
343
    bool isWeak = vnodeIsMsgWeak(pMsg->msgType);
344
    bool isBlock = vnodeIsMsgBlock(pMsg->msgType);
345

346
    vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-write queue, weak:%d block:%d msg:%d:%d pos:%d, handle:%p", vgId, pMsg,
347
            isWeak, isBlock, msg, numOfMsgs, arrayPos, pMsg->info.handle);
348

349
    if (!pVnode->restored) {
350
      vGWarn(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to process since restore not finished, type:%s", vgId, pMsg,
351
             TMSG_INFO(pMsg->msgType));
352
      terrno = TSDB_CODE_SYN_RESTORING;
353
      vnodeHandleProposeError(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
354
      rpcFreeCont(pMsg->pCont);
355
      taosFreeQitem(pMsg);
356
      continue;
357
    }
358

359
    if (pMsgArr == NULL || pIsWeakArr == NULL) {
360
      vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to process since out of memory, type:%s", vgId, pMsg, TMSG_INFO(pMsg->msgType));
361
      terrno = TSDB_CODE_OUT_OF_MEMORY;
362
      vnodeHandleProposeError(pVnode, pMsg, terrno);
363
      rpcFreeCont(pMsg->pCont);
364
      taosFreeQitem(pMsg);
365
      continue;
366
    }
367

368
    bool atExit = false;
369
    vnodeProposeCommitOnNeed(pVnode, atExit);
370

371
    code = vnodePreProcessWriteMsg(pVnode, pMsg);
372
    if (code != 0) {
373
      vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to pre-process since %s", vgId, pMsg, terrstr());
374
      rpcFreeCont(pMsg->pCont);
375
      taosFreeQitem(pMsg);
376
      continue;
377
    }
378

379
    if (isBlock) {
380
      vnodeProposeBatchMsg(pVnode, pMsgArr, pIsWeakArr, &arrayPos);
381
    }
382

383
    pMsgArr[arrayPos] = pMsg;
384
    pIsWeakArr[arrayPos] = isWeak;
385
    arrayPos++;
386

387
    if (isBlock || msg == numOfMsgs - 1) {
388
      vnodeProposeBatchMsg(pVnode, pMsgArr, pIsWeakArr, &arrayPos);
389
    }
390
  }
391

392
  taosMemoryFree(pMsgArr);
393
  taosMemoryFree(pIsWeakArr);
394
}
395

396
#else
397

398
void vnodeProposeWriteMsg(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
1,544,406 ✔
399
  SVnode  *pVnode = pInfo->ahandle;
1,544,406 ✔
400
  int32_t  vgId = pVnode->config.vgId;
1,544,406 ✔
401
  int32_t  code = 0;
1,544,406 ✔
402
  SRpcMsg *pMsg = NULL;
1,544,406 ✔
403
  vTrace("vgId:%d, get %d msgs from vnode-write queue", vgId, numOfMsgs);
1,544,447 ✔
404

405
  for (int32_t msg = 0; msg < numOfMsgs; msg++) {
3,121,964 ✔
406
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
1,577,543 ✔
407
    bool isWeak = vnodeIsMsgWeak(pMsg->msgType);
1,577,508 ✔
408

409
    vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-write queue, weak:%d block:%d msg:%d:%d, handle:%p",
1,577,491 ✔
410
            vgId, pMsg, isWeak, vnodeIsMsgBlock(pMsg->msgType), msg, numOfMsgs, pMsg->info.handle);
411

412
    if (!pVnode->restored) {
1,577,587 ✔
413
      vGWarn(&pMsg->info.traceId,
25,771 ✔
414
             "vgId:%d, msg:%p, failed to process since restore not finished, type:%s, seqNum:%" PRId64, vgId, pMsg,
415
             TMSG_INFO(pMsg->msgType), pMsg->info.seqNum);
416
      vnodeHandleProposeError(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
25,772 ✔
417
      rpcFreeCont(pMsg->pCont);
25,772 ✔
418
      taosFreeQitem(pMsg);
25,770 ✔
419
      continue;
25,772 ✔
420
    }
421

422
    bool atExit = false;
1,551,816 ✔
423
    vnodeProposeCommitOnNeed(pVnode, atExit);
1,551,816 ✔
424

425
    code = vnodePreProcessWriteMsg(pVnode, pMsg);
1,551,820 ✔
426
    if (code != 0) {
1,551,742 ✔
427
      if (code != TSDB_CODE_MSG_PREPROCESSED) {
36,022 ✔
428
        vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to pre-process since %s", vgId, pMsg, tstrerror(code));
137 ✔
429
      }
430
      vnodeHandleProposeError(pVnode, pMsg, code);
36,022 ✔
431
      rpcFreeCont(pMsg->pCont);
36,045 ✔
432
      taosFreeQitem(pMsg);
36,054 ✔
433
      continue;
36,042 ✔
434
    }
435

436
    code = vnodeProposeMsg(pVnode, pMsg, isWeak);
1,515,720 ✔
437

438
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x", vgId, pMsg, code);
1,515,708 ✔
439
    rpcFreeCont(pMsg->pCont);
1,515,708 ✔
440
    taosFreeQitem(pMsg);
1,515,720 ✔
441
  }
442
}
1,544,396 ✔
443

444
#endif
445

446
void vnodeApplyWriteMsg(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
690,900 ✔
447
  SVnode  *pVnode = pInfo->ahandle;
690,900 ✔
448
  int32_t  vgId = pVnode->config.vgId;
690,900 ✔
449
  int32_t  code = 0;
690,900 ✔
450
  SRpcMsg *pMsg = NULL;
690,900 ✔
451

452
  for (int32_t i = 0; i < numOfMsgs; ++i) {
1,484,949 ✔
453
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
794,051 ✔
454

455
    if (vnodeIsMsgBlock(pMsg->msgType)) {
794,055 ✔
456
      vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-apply queue, type:%s handle:%p index:%" PRId64
39,950 ✔
457
              ", blocking msg obtained sec:%d seq:%" PRId64,
458
              vgId, pMsg, TMSG_INFO(pMsg->msgType), pMsg->info.handle, pMsg->info.conn.applyIndex, pVnode->blockSec,
459
              pVnode->blockSeq);
460
    } else {
461
      vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-apply queue, type:%s handle:%p index:%" PRId64, vgId, pMsg,
754,108 ✔
462
              TMSG_INFO(pMsg->msgType), pMsg->info.handle, pMsg->info.conn.applyIndex);
463
    }
464

465
    SRpcMsg rsp = {.code = pMsg->code, .info = pMsg->info};
794,065 ✔
466
    if (rsp.code == 0) {
794,058 ✔
467
      int32_t ret = 0;
794,060 ✔
468
      int32_t count = 0;
793,907 ✔
469
      while (1) {
153 ✔
470
        ret = vnodeProcessWriteMsg(pVnode, pMsg, pMsg->info.conn.applyIndex, &rsp);
794,060 ✔
471
        if (ret < 0) {
794,045 ✔
472
          rsp.code = ret;
166 ✔
473
          vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to apply since %s, index:%" PRId64, vgId, pMsg,
166 ✔
474
                  tstrerror(ret), pMsg->info.conn.applyIndex);
475
        }
476
        if (ret == TSDB_CODE_VND_WRITE_DISABLED) {
793,892 ✔
477
          if (count % 100 == 0)
×
478
            vGError(&pMsg->info.traceId,
×
479
                    "vgId:%d, msg:%p, failed to apply since %s, retry after 200ms, retry count:%d index:%" PRId64, vgId,
480
                    pMsg, tstrerror(ret), count, pMsg->info.conn.applyIndex);
481
          count++;
×
482
          taosMsleep(200);  // wait for a while before retrying
×
483
        } else{
484
          break;
793,892 ✔
485
        } 
486
      }
487
    }
488

489
    vnodePostBlockMsg(pVnode, pMsg);
794,043 ✔
490
    if (rsp.info.handle != NULL) {
794,042 ✔
491
      tmsgSendRsp(&rsp);
170,794 ✔
492
    } else {
493
      if (rsp.pCont) {
623,248 ✔
494
        rpcFreeCont(rsp.pCont);
559,525 ✔
495
      }
496
    }
497

498
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x index:%" PRId64, vgId, pMsg, rsp.code,
794,037 ✔
499
            pMsg->info.conn.applyIndex);
500
    rpcFreeCont(pMsg->pCont);
794,037 ✔
501
    taosFreeQitem(pMsg);
794,050 ✔
502
  }
503
}
690,894 ✔
504

505
int32_t vnodeProcessSyncMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) {
1,421,802 ✔
506
  vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-sync queue, type:%s", pVnode->config.vgId, pMsg, TMSG_INFO(pMsg->msgType));
1,421,802 ✔
507

508
  int32_t code = syncProcessMsg(pVnode->sync, pMsg);
1,421,806 ✔
509
  if (code != 0) {
1,421,816 ✔
510
    vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to process since %s, type:%s", pVnode->config.vgId, pMsg, tstrerror(code),
41 ✔
511
            TMSG_INFO(pMsg->msgType));
512
  }
513

514
  return code;
1,421,807 ✔
515
}
516

517
static int32_t vnodeSyncEqCtrlMsg(const SMsgCb *msgcb, SRpcMsg *pMsg) {
×
518
  if (pMsg == NULL || pMsg->pCont == NULL) {
×
519
    return TSDB_CODE_INVALID_PARA;
×
520
  }
521

522
  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
×
523
    rpcFreeCont(pMsg->pCont);
×
524
    pMsg->pCont = NULL;
×
525
    return TSDB_CODE_INVALID_PARA;
×
526
  }
527

528
  int32_t code = tmsgPutToQueue(msgcb, SYNC_RD_QUEUE, pMsg);
×
529
  if (code != 0) {
×
530
    rpcFreeCont(pMsg->pCont);
×
531
    pMsg->pCont = NULL;
×
532
  }
533
  return code;
×
534
}
535

536
static int32_t vnodeSyncEqMsg(const SMsgCb *msgcb, SRpcMsg *pMsg) {
330,059 ✔
537
  if (pMsg == NULL || pMsg->pCont == NULL) {
330,059 ✔
538
    return TSDB_CODE_INVALID_PARA;
×
539
  }
540

541
  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
330,060 ✔
542
    rpcFreeCont(pMsg->pCont);
×
543
    pMsg->pCont = NULL;
×
544
    return TSDB_CODE_INVALID_PARA;
×
545
  }
546

547
  int32_t code = tmsgPutToQueue(msgcb, SYNC_QUEUE, pMsg);
330,060 ✔
548
  if (code != 0) {
330,061 ✔
549
    rpcFreeCont(pMsg->pCont);
1,542 ✔
550
    pMsg->pCont = NULL;
1,542 ✔
551
  }
552
  return code;
329,182 ✔
553
}
554

555
static int32_t vnodeSyncSendMsg(const SEpSet *pEpSet, SRpcMsg *pMsg) {
1,108,375 ✔
556
  int32_t code = tmsgSendSyncReq(pEpSet, pMsg);
1,108,375 ✔
557
  // if (code != 0) {
558
  //   rpcFreeCont(pMsg->pCont);
559
  //   pMsg->pCont = NULL;
560
  // }
561
  return code;
1,108,373 ✔
562
}
563

564
static int32_t vnodeSyncGetSnapshotInfo(const SSyncFSM *pFsm, SSnapshot *pSnapshot) {
565,247 ✔
565
  return vnodeGetSnapshot(pFsm->data, pSnapshot);
565,247 ✔
566
}
567

568
static int32_t vnodeSyncApplyMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, const SFsmCbMeta *pMeta) {
794,139 ✔
569
  SVnode *pVnode = pFsm->data;
794,139 ✔
570
  pMsg->info.conn.applyIndex = pMeta->index;
794,139 ✔
571
  pMsg->info.conn.applyTerm = pMeta->term;
794,139 ✔
572

573
  // txn WAL cache: feed committed entries before pMsg->pCont may be freed.
574
  // FpCommitCb fires after WAL fsync, so pCont is still valid here.
575
  if (pMsg->info.txnId != 0) {
794,139 ✔
576
    int32_t rc = txnMgrProducerPut(pVnode->pTxnWalMgr, pMsg->info.txnId, pMeta->index, pMsg->msgType, pMsg->pCont,
12,090 ✔
577
                                   pMsg->contLen);
578
    if (rc != 0) {
12,090 ✔
579
      vError("applyMsg: txnMgrProducerPut failed vgId:%d txnId:%" PRId64 " since %s",
×
580
             pVnode->config.vgId, pMsg->info.txnId, tstrerror(rc));
581
    }
582
  }
583

584
  vGDebug(&pMsg->info.traceId,
794,140 ✔
585
          "vgId:%d, index:%" PRId64 ", execute commit cb, fsm:%p, term:%" PRIu64 ", msg-index:%" PRId64
586
          ", weak:%d, code:%d, state:%d %s, type:%s code:0x%x",
587
          pVnode->config.vgId, pMeta->index, pFsm, pMeta->term, pMsg->info.conn.applyIndex, pMeta->isWeak, pMeta->code,
588
          pMeta->state, syncStr(pMeta->state), TMSG_INFO(pMsg->msgType), pMsg->code);
589

590
  int32_t code = tmsgPutToQueue(&pVnode->msgCb, APPLY_QUEUE, pMsg);
794,141 ✔
591
  if (code < 0) {
794,140 ✔
592
    if (code == TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE) {
83 ✔
593
      pVnode->applyQueueErrorCount++;
×
594
      if (pVnode->applyQueueErrorCount == APPLY_QUEUE_ERROR_THRESHOLD) {
×
595
        pVnode->applyQueueErrorCount = 0;
×
596
        vWarn("vgId:%d, failed to put into apply_queue since %s", pVnode->config.vgId, tstrerror(code));
×
597
      }
598
    } else {
599
      vError("vgId:%d, failed to put into apply_queue since %s", pVnode->config.vgId, tstrerror(code));
83 ✔
600
    }
601
  }
602
  return code;
794,141 ✔
603
}
604

605
static int32_t vnodeSyncCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
794,138 ✔
606
  if (pMsg->code == 0) {
794,138 ✔
607
    return vnodeSyncApplyMsg(pFsm, pMsg, pMeta);
794,141 ✔
608
  }
609

610
  SVnode *pVnode = pFsm->data;
×
611
  vnodePostBlockMsg(pVnode, pMsg);
×
612

613
  SRpcMsg rsp = {
×
614
      .code = pMsg->code,
×
615
      .info = pMsg->info,
616
  };
617

618
  if (rsp.info.handle != NULL) {
×
619
    tmsgSendRsp(&rsp);
×
620
  }
621

622
  vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x index:%" PRId64, TD_VID(pVnode), pMsg, rsp.code,
×
623
          pMeta->index);
624
  rpcFreeCont(pMsg->pCont);
×
625
  pMsg->pCont = NULL;
×
626
  return 0;
×
627
}
628

629
static int32_t vnodeSyncPreCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
×
630
  if (pMeta->isWeak == 1) {
×
631
    return vnodeSyncApplyMsg(pFsm, pMsg, pMeta);
×
632
  }
633
  return 0;
×
634
}
635

636
static SyncIndex vnodeSyncAppliedIndex(const SSyncFSM *pFSM) {
2,094,356 ✔
637
  SVnode *pVnode = pFSM->data;
2,094,356 ✔
638
  return atomic_load_64(&pVnode->state.applied);
2,094,139 ✔
639
}
640

641
static void vnodeSyncRollBackMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
×
642
  SVnode *pVnode = pFsm->data;
×
643
  vGDebug(&pMsg->info.traceId,
×
644
          "vgId:%d, rollback-cb is executed, fsm:%p, index:%" PRId64 ", weak:%d, code:%d, state:%d %s, type:%s",
645
          pVnode->config.vgId, pFsm, pMeta->index, pMeta->isWeak, pMeta->code, pMeta->state, syncStr(pMeta->state),
646
          TMSG_INFO(pMsg->msgType));
647
}
×
648

649
static int32_t vnodeSnapshotStartRead(const SSyncFSM *pFsm, void *pParam, void **ppReader) {
52 ✔
650
  SVnode *pVnode = pFsm->data;
52 ✔
651
  return vnodeSnapReaderOpen(pVnode, (SSnapshotParam *)pParam, (SVSnapReader **)ppReader);
52 ✔
652
}
653

654
static void vnodeSnapshotStopRead(const SSyncFSM *pFsm, void *pReader) {
52 ✔
655
  SVnode *pVnode = pFsm->data;
52 ✔
656
  vnodeSnapReaderClose(pReader);
52 ✔
657
}
52 ✔
658

659
static int32_t vnodeSnapshotDoRead(const SSyncFSM *pFsm, void *pReader, void **ppBuf, int32_t *len) {
886 ✔
660
  SVnode *pVnode = pFsm->data;
886 ✔
661
  return vnodeSnapRead(pReader, (uint8_t **)ppBuf, len);
886 ✔
662
}
663

664
static int32_t vnodeSnapshotStartWrite(const SSyncFSM *pFsm, void *pParam, void **ppWriter) {
52 ✔
665
  SVnode *pVnode = pFsm->data;
52 ✔
666

667
  do {
×
668
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
52 ✔
669
    if (itemSize == 0) {
52 ✔
670
      vInfo("vgId:%d, start write vnode snapshot since apply queue is empty", pVnode->config.vgId);
52 ✔
671
      break;
52 ✔
672
    } else {
673
      vInfo("vgId:%d, write vnode snapshot later since %d items in apply queue", pVnode->config.vgId, itemSize);
×
674
      taosMsleep(10);
×
675
    }
676
  } while (true);
677

678
  return vnodeSnapWriterOpen(pVnode, (SSnapshotParam *)pParam, (SVSnapWriter **)ppWriter);
52 ✔
679
}
680

681
static int32_t vnodeSnapshotStopWrite(const SSyncFSM *pFsm, void *pWriter, bool isApply, SSnapshot *pSnapshot) {
52 ✔
682
  SVnode *pVnode = pFsm->data;
52 ✔
683
  vInfo("vgId:%d, stop write vnode snapshot, apply:%d, index:%" PRId64 " term:%" PRIu64 " config:%" PRId64,
52 ✔
684
        pVnode->config.vgId, isApply, pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm, pSnapshot->lastConfigIndex);
685

686
  int32_t code = vnodeSnapWriterClose(pWriter, !isApply, pSnapshot);
52 ✔
687
  if (code != 0) {
52 ✔
688
    vError("vgId:%d, failed to finish applying vnode snapshot since %s, code:0x%x", pVnode->config.vgId, terrstr(),
×
689
           code);
690
  }
691
  return code;
52 ✔
692
}
693

694
static int32_t vnodeSnapshotDoWrite(const SSyncFSM *pFsm, void *pWriter, void *pBuf, int32_t len) {
834 ✔
695
  SVnode *pVnode = pFsm->data;
834 ✔
696
  vDebug("vgId:%d, continue write vnode snapshot, blockLen:%d", pVnode->config.vgId, len);
834 ✔
697
  int32_t code = vnodeSnapWrite(pWriter, pBuf, len);
834 ✔
698
  vDebug("vgId:%d, continue write vnode snapshot finished, blockLen:%d", pVnode->config.vgId, len);
834 ✔
699
  return code;
834 ✔
700
}
701

702
static void vnodeRestoreFinish(const SSyncFSM *pFsm, const SyncIndex commitIdx) {
14,366 ✔
703
  SVnode   *pVnode = pFsm->data;
14,366 ✔
704
  int32_t   vgId = pVnode->config.vgId;
14,366 ✔
705
  SyncIndex appliedIdx = -1;
14,366 ✔
706

707
  do {
310 ✔
708
    appliedIdx = vnodeSyncAppliedIndex(pFsm);
30,879 ✔
709
    if (appliedIdx > commitIdx) {
30,893 ✔
710
      vError("vgId:%d, restore failed since applied-index:%" PRId64 " is larger than commit-index:%" PRId64, vgId,
4 ✔
711
             appliedIdx, commitIdx);
712
      break;
×
713
    }
714
    if (appliedIdx == commitIdx) {
30,889 ✔
715
      vInfo("vgId:%d, no items to be applied, restore finish", pVnode->config.vgId);
14,367 ✔
716
      break;
14,246 ✔
717
    } else {
718
      if (appliedIdx % 10 == 0) {
16,522 ✔
719
        vInfo("vgId:%d, restore not finish since %" PRId64 " items to be applied. commit-index:%" PRId64
561 ✔
720
              ", applied-index:%" PRId64,
721
              vgId, commitIdx - appliedIdx, commitIdx, appliedIdx);
722
      } else {
723
        vDebug("vgId:%d, restore not finish since %" PRId64 " items to be applied. commit-index:%" PRId64
15,961 ✔
724
               ", applied-index:%" PRId64,
725
               vgId, commitIdx - appliedIdx, commitIdx, appliedIdx);
726
      }
727
      taosMsleep(10);
16,523 ✔
728
    }
729
  } while (true);
730

731
  walApplyVer(pVnode->pWal, commitIdx);
14,371 ✔
732
  pVnode->restored = true;
14,369 ✔
733
}
14,369 ✔
734

735
static void vnodeBecomeFollower(const SSyncFSM *pFsm) {
5,391 ✔
736
  SVnode *pVnode = pFsm->data;
5,391 ✔
737
  vInfo("vgId:%d, becomefollower callback", pVnode->config.vgId);
5,391 ✔
738

739
  (void)taosThreadMutexLock(&pVnode->lock);
5,391 ✔
740
  if (pVnode->blocked) {
5,391 ✔
741
    pVnode->blocked = false;
×
742
    vDebug("vgId:%d, become follower and post block", pVnode->config.vgId);
×
743
    if (tsem_post(&pVnode->syncSem) != 0) {
×
744
      vError("vgId:%d, failed to post sync semaphore", pVnode->config.vgId);
×
745
    }
746
  }
747
  (void)taosThreadMutexUnlock(&pVnode->lock);
5,391 ✔
748

749
  streamRemoveVnodeLeader(pVnode->config.vgId);
5,391 ✔
750
}
5,390 ✔
751

752
static void vnodeBecomeLearner(const SSyncFSM *pFsm) {
212 ✔
753
  SVnode *pVnode = pFsm->data;
212 ✔
754
  vInfo("vgId:%d, become learner", pVnode->config.vgId);
212 ✔
755

756
  (void)taosThreadMutexLock(&pVnode->lock);
212 ✔
757
  if (pVnode->blocked) {
212 ✔
758
    pVnode->blocked = false;
×
759
    vDebug("vgId:%d, become learner and post block", pVnode->config.vgId);
×
760
    if (tsem_post(&pVnode->syncSem) != 0) {
×
761
      vError("vgId:%d, failed to post sync semaphore", pVnode->config.vgId);
×
762
    }
763
  }
764
  (void)taosThreadMutexUnlock(&pVnode->lock);
212 ✔
765

766
  streamRemoveVnodeLeader(pVnode->config.vgId);  
212 ✔
767
}
212 ✔
768

769
static void vnodeBecomeLeader(const SSyncFSM *pFsm) {
11,654 ✔
770
  SVnode *pVnode = pFsm->data;
11,654 ✔
771
  vInfo("vgId:%d, become leader callback", pVnode->config.vgId);
11,654 ✔
772

773
  streamAddVnodeLeader(pVnode->config.vgId);
11,654 ✔
774
}
11,654 ✔
775

776
static void vnodeBecomeAssignedLeader(const SSyncFSM *pFsm) {
1 ✔
777
  SVnode *pVnode = pFsm->data;
1 ✔
778
  vDebug("vgId:%d, become assigned leader", pVnode->config.vgId);
1 ✔
779

780
  streamAddVnodeLeader(pVnode->config.vgId);
1 ✔
781
}
1 ✔
782

783
static bool vnodeApplyQueueEmpty(const SSyncFSM *pFsm) {
×
784
  SVnode *pVnode = pFsm->data;
×
785

786
  if (pVnode != NULL && pVnode->msgCb.qsizeFp != NULL) {
×
787
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
×
788
    return (itemSize == 0);
×
789
  } else {
790
    return true;
×
791
  }
792
}
793

794
static int32_t vnodeApplyQueueItems(const SSyncFSM *pFsm) {
58,723 ✔
795
  SVnode *pVnode = pFsm->data;
58,723 ✔
796

797
  if (pVnode != NULL && pVnode->msgCb.qsizeFp != NULL) {
58,723 ✔
798
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
58,748 ✔
799
    return itemSize;
58,742 ✔
800
  } else {
801
    return TSDB_CODE_INVALID_PARA;
×
802
  }
803
}
804

805
static SSyncFSM *vnodeSyncMakeFsm(SVnode *pVnode) {
13,856 ✔
806
  SSyncFSM *pFsm = taosMemoryCalloc(1, sizeof(SSyncFSM));
13,856 ✔
807
  if (pFsm == NULL) {
13,843 ✔
808
    terrno = TSDB_CODE_OUT_OF_MEMORY;
1 ✔
809
    return NULL;
×
810
  }
811
  pFsm->data = pVnode;
13,842 ✔
812
  pFsm->FpCommitCb = vnodeSyncCommitMsg;
13,842 ✔
813
  pFsm->FpAppliedIndexCb = vnodeSyncAppliedIndex;
13,842 ✔
814
  pFsm->FpPreCommitCb = vnodeSyncPreCommitMsg;
13,842 ✔
815
  pFsm->FpRollBackCb = vnodeSyncRollBackMsg;
13,842 ✔
816
  pFsm->FpGetSnapshot = NULL;
13,842 ✔
817
  pFsm->FpGetSnapshotInfo = vnodeSyncGetSnapshotInfo;
13,842 ✔
818
  pFsm->FpRestoreFinishCb = vnodeRestoreFinish;
13,842 ✔
819
  pFsm->FpAfterRestoredCb = NULL;
13,842 ✔
820
  pFsm->FpLeaderTransferCb = NULL;
13,842 ✔
821
  pFsm->FpApplyQueueEmptyCb = vnodeApplyQueueEmpty;
13,842 ✔
822
  pFsm->FpApplyQueueItems = vnodeApplyQueueItems;
13,842 ✔
823
  pFsm->FpBecomeLeaderCb = vnodeBecomeLeader;
13,842 ✔
824
  pFsm->FpBecomeAssignedLeaderCb = vnodeBecomeAssignedLeader;
13,842 ✔
825
  pFsm->FpBecomeFollowerCb = vnodeBecomeFollower;
13,842 ✔
826
  pFsm->FpBecomeLearnerCb = vnodeBecomeLearner;
13,842 ✔
827
  pFsm->FpReConfigCb = NULL;
13,842 ✔
828
  pFsm->FpSnapshotStartRead = vnodeSnapshotStartRead;
13,842 ✔
829
  pFsm->FpSnapshotStopRead = vnodeSnapshotStopRead;
13,842 ✔
830
  pFsm->FpSnapshotDoRead = vnodeSnapshotDoRead;
13,842 ✔
831
  pFsm->FpSnapshotStartWrite = vnodeSnapshotStartWrite;
13,842 ✔
832
  pFsm->FpSnapshotStopWrite = vnodeSnapshotStopWrite;
13,842 ✔
833
  pFsm->FpSnapshotDoWrite = vnodeSnapshotDoWrite;
13,842 ✔
834

835
  return pFsm;
13,842 ✔
836
}
837

838
int32_t vnodeSyncOpen(SVnode *pVnode, char *path, int32_t vnodeVersion) {
13,848 ✔
839
  SSyncInfo syncInfo = {
13,848 ✔
840
      .snapshotStrategy = SYNC_STRATEGY_WAL_FIRST,
841
      .batchSize = 1,
842
      .vgId = pVnode->config.vgId,
13,864 ✔
843
      .mountVgId = pVnode->config.mountVgId,
13,864 ✔
844
      .syncCfg = pVnode->config.syncCfg,
845
      .pWal = pVnode->pWal,
13,864 ✔
846
      .msgcb = &pVnode->msgCb,
13,864 ✔
847
      .syncSendMSg = vnodeSyncSendMsg,
848
      .syncEqMsg = vnodeSyncEqMsg,
849
      .syncEqCtrlMsg = vnodeSyncEqCtrlMsg,
850
      .pingMs = 5000,
851
      .electMs = tsVnodeElectIntervalMs,
852
      .heartbeatMs = tsVnodeHeartbeatIntervalMs,
853
  };
854

855
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s%ssync", path, TD_DIRSEP);
13,864 ✔
856
  syncInfo.pFsm = vnodeSyncMakeFsm(pVnode);
13,864 ✔
857

858
  SSyncCfg *pCfg = &syncInfo.syncCfg;
13,844 ✔
859
  vInfo("vgId:%d, start to open sync, replica:%d selfIndex:%d, electMs:%d, heartbeatMs:%d", pVnode->config.vgId,
13,844 ✔
860
        pCfg->replicaNum, pCfg->myIndex, syncInfo.electMs, syncInfo.heartbeatMs);
861
  for (int32_t i = 0; i < pCfg->totalReplicaNum; ++i) {
34,618 ✔
862
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
20,753 ✔
863
    vInfo("vgId:%d, index:%d ep:%s:%u dnode:%d cluster:%" PRId64, pVnode->config.vgId, i, pNode->nodeFqdn,
20,753 ✔
864
          pNode->nodePort, pNode->nodeId, pNode->clusterId);
865
  }
866

867
  pVnode->sync = syncOpen(&syncInfo, vnodeVersion);
13,865 ✔
868
  if (pVnode->sync <= 0) {
13,866 ✔
869
    vError("vgId:%d, failed to open sync since %s", pVnode->config.vgId, terrstr());
1 ✔
870
    return terrno;
1 ✔
871
  }
872

873
  return 0;
13,751 ✔
874
}
875

876
int32_t vnodeSyncStart(SVnode *pVnode) {
13,805 ✔
877
  vInfo("vgId:%d, start sync", pVnode->config.vgId);
13,805 ✔
878
  int32_t code = syncStart(pVnode->sync);
13,807 ✔
879
  if (code) {
13,807 ✔
880
    vError("vgId:%d, failed to start sync subsystem since %s", pVnode->config.vgId, tstrerror(code));
×
881
    return code;
×
882
  }
883
  return 0;
13,693 ✔
884
}
885

886
int32_t vnodeSetSyncTimeout(SVnode *pVnode, int32_t ms) {
×
887
  int32_t code = syncResetTimer(pVnode->sync, tsVnodeElectIntervalMs, tsVnodeHeartbeatIntervalMs);
×
888
  if (code) {
×
889
    vError("vgId:%d, failed to vnode Set SyncTimeout since %s", pVnode->config.vgId, tstrerror(code));
×
890
    return code;
×
891
  }
892
  return 0;
×
893
}
894

895
int32_t vnodeSetElectBaseline(SVnode* pVnode, int32_t ms){
48 ✔
896
  int32_t code = syncSetElectBaseline(pVnode->sync, ms);
48 ✔
897
  if (code) {
48 ✔
898
    vError("vgId:%d, failed to set electBaseline since %s", pVnode->config.vgId, tstrerror(code));
×
899
    return code;
×
900
  }
901
  return 0;
48 ✔
902
}
903

904
void vnodeSyncPreClose(SVnode *pVnode) {
13,866 ✔
905
  vInfo("vgId:%d, vnode sync pre close", pVnode->config.vgId);
13,866 ✔
906
  int32_t code = syncLeaderTransfer(pVnode->sync);
13,867 ✔
907
  if (code) {
13,866 ✔
908
    vError("vgId:%d, failed to transfer leader since %s", pVnode->config.vgId, tstrerror(code));
1,029 ✔
909
  }
910
  syncPreStop(pVnode->sync);
13,866 ✔
911

912
  (void)taosThreadMutexLock(&pVnode->lock);
13,866 ✔
913
  if (pVnode->blocked) {
13,866 ✔
914
    vInfo("vgId:%d, post block after close sync", pVnode->config.vgId);
19 ✔
915
    pVnode->blocked = false;
19 ✔
916
    if (tsem_post(&pVnode->syncSem) != 0) {
19 ✔
917
      vError("vgId:%d, failed to post block", pVnode->config.vgId);
×
918
    }
919
  }
920
  (void)taosThreadMutexUnlock(&pVnode->lock);
13,866 ✔
921
}
13,865 ✔
922

923
void vnodeSyncPostClose(SVnode *pVnode) {
13,866 ✔
924
  vInfo("vgId:%d, vnode sync post close", pVnode->config.vgId);
13,866 ✔
925
  syncPostStop(pVnode->sync);
13,866 ✔
926
}
13,866 ✔
927

928
void vnodeSyncClose(SVnode *pVnode) {
13,866 ✔
929
  vInfo("vgId:%d, vnode close sync", pVnode->config.vgId);
13,866 ✔
930
  syncStop(pVnode->sync);
13,866 ✔
931
}
13,866 ✔
932

933
void vnodeSyncCheckTimeout(SVnode *pVnode) {
14,468 ✔
934
  vTrace("vgId:%d, check sync timeout msg", pVnode->config.vgId);
14,468 ✔
935
  (void)taosThreadMutexLock(&pVnode->lock);
14,468 ✔
936
  if (pVnode->blocked) {
14,468 ✔
937
    int32_t curSec = taosGetTimestampSec();
8 ✔
938
    int32_t delta = curSec - pVnode->blockSec;
8 ✔
939
    if (delta > VNODE_TIMEOUT_SEC) {
8 ✔
940
      vError("vgId:%d, failed to propose since timeout and post block, start:%d cur:%d delta:%d seq:%" PRId64,
×
941
             pVnode->config.vgId, pVnode->blockSec, curSec, delta, pVnode->blockSeq);
942
      if (syncSendTimeoutRsp(pVnode->sync, pVnode->blockSeq) != 0) {
×
943
#if 0
944
        SRpcMsg rpcMsg = {.code = TSDB_CODE_SYN_TIMEOUT, .info = pVnode->blockInfo};
945
        vError("send timeout response since its applied, seq:%" PRId64 " handle:%p ahandle:%p", pVnode->blockSeq,
946
              rpcMsg.info.handle, rpcMsg.info.ahandle);
947
        rpcSendResponse(&rpcMsg);
948
#endif
949
      }
×
950
      pVnode->blocked = false;
×
951
      pVnode->blockSec = 0;
×
952
      pVnode->blockSeq = 0;
×
953
      if (tsem_post(&pVnode->syncSem) != 0) {
×
954
        vError("vgId:%d, failed to post block", pVnode->config.vgId);
×
955
      }
956
    }
957
  }
958
  (void)taosThreadMutexUnlock(&pVnode->lock);
14,468 ✔
959
}
14,468 ✔
960

961
bool vnodeIsRoleLeader(SVnode *pVnode) {
×
962
  SSyncState state = syncGetState(pVnode->sync);
×
963
  return state.state == TAOS_SYNC_STATE_LEADER;
×
964
}
965

966
bool vnodeIsLeader(SVnode *pVnode) {
13,869 ✔
967
  terrno = 0;
13,869 ✔
968
  SSyncState state = syncGetState(pVnode->sync);
13,869 ✔
969

970
  if (terrno != 0) {
13,866 ✔
971
    vInfo("vgId:%d, vnode is stopping", pVnode->config.vgId);
2,279 ✔
972
    return false;
2,280 ✔
973
  }
974

975
  if (state.state != TAOS_SYNC_STATE_LEADER) {
11,588 ✔
976
    terrno = TSDB_CODE_SYN_NOT_LEADER;
1 ✔
977
    vInfo("vgId:%d, vnode not leader, state:%s", pVnode->config.vgId, syncStr(state.state));
1 ✔
978
    return false;
1 ✔
979
  }
980

981
  if (!state.restored || !pVnode->restored) {
11,587 ✔
982
    terrno = TSDB_CODE_SYN_RESTORING;
×
983
    vInfo("vgId:%d, vnode not restored:%d:%d", pVnode->config.vgId, state.restored, pVnode->restored);
×
984
    return false;
×
985
  }
986

987
  return true;
11,479 ✔
988
}
989

990
int64_t vnodeClusterId(SVnode *pVnode) {
×
991
  SSyncCfg *syncCfg = &pVnode->config.syncCfg;
×
992
  return syncCfg->nodeInfo[syncCfg->myIndex].clusterId;
×
993
}
994

995
int32_t vnodeNodeId(SVnode *pVnode) {
146,785 ✔
996
  SSyncCfg *syncCfg = &pVnode->config.syncCfg;
146,785 ✔
997
  return syncCfg->nodeInfo[syncCfg->myIndex].nodeId;
146,785 ✔
998
}
999

1000
int32_t vnodeGetSnapshot(SVnode *pVnode, SSnapshot *pSnap) {
565,524 ✔
1001
  int code = 0;
565,524 ✔
1002
  pSnap->lastApplyIndex = pVnode->state.committed;
565,524 ✔
1003
  pSnap->lastApplyTerm = pVnode->state.commitTerm;
565,524 ✔
1004
  pSnap->lastConfigIndex = -1;
565,524 ✔
1005
  pSnap->state = SYNC_FSM_STATE_COMPLETE;
565,524 ✔
1006

1007
  if (tsdbSnapGetFsState(pVnode) != TSDB_FS_STATE_NORMAL) {
565,524 ✔
1008
    pSnap->state = SYNC_FSM_STATE_INCOMPLETE;
×
1009
  }
1010

1011
  if (pSnap->type == TDMT_SYNC_PREP_SNAPSHOT || pSnap->type == TDMT_SYNC_PREP_SNAPSHOT_REPLY) {
565,540 ✔
1012
    code = tsdbSnapPrepDescription(pVnode, pSnap);
87 ✔
1013
  }
1014
  return code;
565,556 ✔
1015
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc