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

taosdata / TDengine / #4541

19 Jul 2025 01:13AM UTC coverage: 56.753% (-1.6%) from 58.31%
#4541

push

travis-ci

web-flow
fix: subquery memleak (#32024)

124299 of 282344 branches covered (44.02%)

Branch coverage included in aggregate %.

181106 of 255787 relevant lines covered (70.8%)

24937406.43 hits per line

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

54.66
/source/dnode/vnode/src/vnd/vnodeSync.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 "sync.h"
18
#include "tq.h"
19
#include "tsdb.h"
20
#include "vnd.h"
21
#include "stream.h"
22

23
#define BATCH_ENABLE 0
24

25
// static int32_t inline vnodeShouldRewriteSubmitMsg(SVnode *pVnode, SRpcMsg **pMsg);
26
static inline bool vnodeIsMsgWeak(tmsg_t type) { return false; }
19,399,066✔
27

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

36
static inline void vnodePostBlockMsg(SVnode *pVnode, const SRpcMsg *pMsg) {
7,854,901✔
37
  if (vnodeIsMsgBlock(pMsg->msgType)) {
7,854,901✔
38
    (void)taosThreadMutexLock(&pVnode->lock);
29,744✔
39
    if (pVnode->blocked) {
29,745✔
40
      vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, post block, type:%s sec:%d seq:%" PRId64, pVnode->config.vgId, pMsg,
3,158!
41
              TMSG_INFO(pMsg->msgType), pVnode->blockSec, pVnode->blockSeq);
42
      pVnode->blocked = false;
3,158✔
43
      pVnode->blockSec = 0;
3,158✔
44
      pVnode->blockSeq = 0;
3,158✔
45
      if (tsem_post(&pVnode->syncSem) != 0) {
3,158!
46
        vError("vgId:%d, failed to post sem", pVnode->config.vgId);
×
47
      }
48
    }
49
    (void)taosThreadMutexUnlock(&pVnode->lock);
29,745✔
50
  }
51
}
7,854,908✔
52

53
void vnodeRedirectRpcMsg(SVnode *pVnode, SRpcMsg *pMsg, int32_t code) {
46,136✔
54
  SEpSet newEpSet = {0};
46,136✔
55
  syncGetRetryEpSet(pVnode->sync, &newEpSet);
46,136✔
56

57
  vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is redirect since not leader, numOfEps:%d inUse:%d",
46,135!
58
          pVnode->config.vgId, pMsg, newEpSet.numOfEps, newEpSet.inUse);
59
  for (int32_t i = 0; i < newEpSet.numOfEps; ++i) {
178,159✔
60
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, redirect:%d ep:%s:%u", pVnode->config.vgId, pMsg, i,
132,024!
61
            newEpSet.eps[i].fqdn, newEpSet.eps[i].port);
62
  }
63
  pMsg->info.hasEpSet = 1;
46,135✔
64

65
  if (code == 0) code = TSDB_CODE_SYN_NOT_LEADER;
46,135!
66

67
  SRpcMsg rsp = {.code = code, .info = pMsg->info, .msgType = pMsg->msgType + 1};
46,135✔
68
  int32_t contLen = tSerializeSEpSet(NULL, 0, &newEpSet);
46,135✔
69

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

80
  tmsgSendRsp(&rsp);
46,135✔
81
}
46,135✔
82

83
static void inline vnodeHandleWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
19,066,907✔
84
  int32_t code = 0;
19,066,907✔
85
  SRpcMsg rsp = {.code = pMsg->code, .info = pMsg->info};
19,066,907✔
86

87
  if (vnodeProcessWriteMsg(pVnode, pMsg, pMsg->info.conn.applyIndex, &rsp) < 0) {
19,066,907✔
88
    rsp.code = terrno;
8✔
89
    vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to apply right now since %s", pVnode->config.vgId, pMsg,
8!
90
            terrstr());
91
  }
92
  if (rsp.info.handle != NULL) {
19,067,007✔
93
    tmsgSendRsp(&rsp);
19,061,176✔
94
  } else {
95
    if (rsp.pCont) {
5,831!
96
      rpcFreeCont(rsp.pCont);
×
97
    }
98
  }
99
}
19,067,211✔
100

101
static void vnodeHandleProposeError(SVnode *pVnode, SRpcMsg *pMsg, int32_t code) {
152,541✔
102
  if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING) {
152,541✔
103
    vnodeRedirectRpcMsg(pVnode, pMsg, code);
35,606✔
104
  } else if (code == TSDB_CODE_MSG_PREPROCESSED) {
116,935✔
105
    SRpcMsg rsp = {.code = TSDB_CODE_SUCCESS, .info = pMsg->info};
115,039✔
106
    if (rsp.info.handle != NULL) {
115,039!
107
      tmsgSendRsp(&rsp);
115,205✔
108
    }
109
  } else {
110
    vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to propose since %s, code:0x%x", pVnode->config.vgId, pMsg,
1,896!
111
            tstrerror(code), code);
112
    SRpcMsg rsp = {.code = code, .info = pMsg->info};
1,939✔
113
    if (rsp.info.handle != NULL) {
1,939✔
114
      tmsgSendRsp(&rsp);
1,288✔
115
    }
116
  }
117
}
153,001✔
118

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

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

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

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

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

152
  TAOS_CHECK_EXIT(tEncodeU64v(pEncoder, taosArrayGetSize(pReq->aSubmitTbData)));
×
153

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

172
  TAOS_CHECK_EXIT(tStartEncode(pEncoder));
×
173

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

183
  if (hasBlob) {
×
184
    int32_t    nr = 0;
×
185
    uint64_t   seq = 0;
×
186
    SBlobRow2 *pBlobRow = pSubmitTbData->pBlobRow;
×
187

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

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

203
  if (pSubmitTbData->ctimeMs > 0) {
×
204
    TAOS_CHECK_EXIT(tEncodeI64(pEncoder, pSubmitTbData->ctimeMs));
×
205
  }
206
  tEndEncode(pEncoder);
×
207
_exit:
×
208
  return code;
×
209
}
210
static int32_t inline vnodeProposeMsg(SVnode *pVnode, SRpcMsg *pMsg, bool isWeak) {
19,257,087✔
211
  int32_t code = 0;
19,257,087✔
212
  int64_t seq = 0;
19,257,087✔
213

214
  taosThreadMutexLock(&pVnode->lock);
19,257,087✔
215
  code = syncPropose(pVnode->sync, pMsg, isWeak, &seq);
19,257,441✔
216
  bool wait = (code == 0 && vnodeIsMsgBlock(pMsg->msgType));
19,257,117✔
217
  if (wait) {
19,257,119✔
218
    if (pVnode->blocked) {
3,188!
219
      (void)taosThreadMutexUnlock(&pVnode->lock);
×
220
      return TSDB_CODE_INTERNAL_ERROR;
×
221
    }
222
    pVnode->blocked = true;
3,188✔
223
    pVnode->blockSec = taosGetTimestampSec();
3,188✔
224
    pVnode->blockSeq = seq;
3,188✔
225
  }
226
  (void)taosThreadMutexUnlock(&pVnode->lock);
19,257,119✔
227

228
  if (code > 0) {
19,257,356✔
229
    vnodeHandleWriteMsg(pVnode, pMsg);
19,067,252✔
230
  } else if (code < 0) {
190,104✔
231
    if (terrno != 0) code = terrno;
8,963!
232
    vnodeHandleProposeError(pVnode, pMsg, code);
8,963✔
233
  }
234

235
  if (wait) vnodeWaitBlockMsg(pVnode, pMsg);
19,257,289✔
236

237
  // if (rewrite) {
238
  //   rpcFreeCont(newMsg.pCont);
239
  //   newMsg.pCont = NULL;
240
  // }
241
  return code;
19,257,287✔
242
}
243

244
void vnodeProposeCommitOnNeed(SVnode *pVnode, bool atExit) {
19,383,660✔
245
  if (!vnodeShouldCommit(pVnode, atExit)) {
19,383,660✔
246
    return;
19,374,945✔
247
  }
248

249
  int32_t   contLen = sizeof(SMsgHead);
9,534✔
250
  SMsgHead *pHead = rpcMallocCont(contLen);
9,534✔
251
  pHead->contLen = contLen;
9,545✔
252
  pHead->vgId = pVnode->config.vgId;
9,545✔
253

254
  SRpcMsg rpcMsg = {0};
9,545✔
255
  rpcMsg.msgType = TDMT_VND_COMMIT;
9,545✔
256
  rpcMsg.contLen = contLen;
9,545✔
257
  rpcMsg.pCont = pHead;
9,545✔
258
  rpcMsg.info.noResp = 1;
9,545✔
259

260
  vInfo("vgId:%d, propose vnode commit", pVnode->config.vgId);
9,545✔
261
  bool isWeak = false;
9,546✔
262

263
  if (!atExit) {
9,546✔
264
    if (vnodeProposeMsg(pVnode, &rpcMsg, isWeak) < 0) {
1,611!
265
      vTrace("vgId:%d, failed to propose vnode commit since %s", pVnode->config.vgId, terrstr());
×
266
    }
267
    rpcFreeCont(rpcMsg.pCont);
1,611✔
268
    rpcMsg.pCont = NULL;
1,611✔
269
  } else {
270
    int32_t code = 0;
7,935✔
271
    if ((code = tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &rpcMsg)) < 0) {
7,935✔
272
      vError("vgId:%d, failed to put vnode commit to write_queue since %s", pVnode->config.vgId, tstrerror(code));
3,019✔
273
    }
274
  }
275
}
276

277
#if BATCH_ENABLE
278

279
static void inline vnodeProposeBatchMsg(SVnode *pVnode, SRpcMsg **pMsgArr, bool *pIsWeakArr, int32_t *arrSize) {
280
  if (*arrSize <= 0) return;
281
  SRpcMsg *pLastMsg = pMsgArr[*arrSize - 1];
282

283
  (void)taosThreadMutexLock(&pVnode->lock);
284
  int32_t code = syncProposeBatch(pVnode->sync, pMsgArr, pIsWeakArr, *arrSize);
285
  bool    wait = (code == 0 && vnodeIsBlockMsg(pLastMsg->msgType));
286
  if (wait) {
287
    pVnode->blocked = true;
288
  }
289
  (void)taosThreadMutexUnlock(&pVnode->lock);
290

291
  if (code > 0) {
292
    for (int32_t i = 0; i < *arrSize; ++i) {
293
      vnodeHandleWriteMsg(pVnode, pMsgArr[i]);
294
    }
295
  } else if (code < 0) {
296
    if (terrno != 0) code = terrno;
297
    for (int32_t i = 0; i < *arrSize; ++i) {
298
      vnodeHandleProposeError(pVnode, pMsgArr[i], code);
299
    }
300
  }
301

302
  if (wait) vnodeWaitBlockMsg(pVnode, pLastMsg);
303
  pLastMsg = NULL;
304

305
  for (int32_t i = 0; i < *arrSize; ++i) {
306
    SRpcMsg        *pMsg = pMsgArr[i];
307
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x", pVnode->config.vgId, pMsg, code);
308
    rpcFreeCont(pMsg->pCont);
309
    taosFreeQitem(pMsg);
310
  }
311

312
  *arrSize = 0;
313
}
314

315
void vnodeProposeWriteMsg(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
316
  SVnode   *pVnode = pInfo->ahandle;
317
  int32_t   vgId = pVnode->config.vgId;
318
  int32_t   code = 0;
319
  SRpcMsg  *pMsg = NULL;
320
  int32_t   arrayPos = 0;
321
  SRpcMsg **pMsgArr = taosMemoryCalloc(numOfMsgs, sizeof(SRpcMsg *));
322
  bool     *pIsWeakArr = taosMemoryCalloc(numOfMsgs, sizeof(bool));
323
  vTrace("vgId:%d, get %d msgs from vnode-write queue", vgId, numOfMsgs);
324

325
  for (int32_t msg = 0; msg < numOfMsgs; msg++) {
326
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
327
    bool isWeak = vnodeIsMsgWeak(pMsg->msgType);
328
    bool isBlock = vnodeIsMsgBlock(pMsg->msgType);
329

330
    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,
331
            isWeak, isBlock, msg, numOfMsgs, arrayPos, pMsg->info.handle);
332

333
    if (!pVnode->restored) {
334
      vGWarn(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to process since restore not finished, type:%s", vgId, pMsg,
335
             TMSG_INFO(pMsg->msgType));
336
      terrno = TSDB_CODE_SYN_RESTORING;
337
      vnodeHandleProposeError(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
338
      rpcFreeCont(pMsg->pCont);
339
      taosFreeQitem(pMsg);
340
      continue;
341
    }
342

343
    if (pMsgArr == NULL || pIsWeakArr == NULL) {
344
      vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to process since out of memory, type:%s", vgId, pMsg, TMSG_INFO(pMsg->msgType));
345
      terrno = TSDB_CODE_OUT_OF_MEMORY;
346
      vnodeHandleProposeError(pVnode, pMsg, terrno);
347
      rpcFreeCont(pMsg->pCont);
348
      taosFreeQitem(pMsg);
349
      continue;
350
    }
351

352
    bool atExit = false;
353
    vnodeProposeCommitOnNeed(pVnode, atExit);
354

355
    code = vnodePreProcessWriteMsg(pVnode, pMsg);
356
    if (code != 0) {
357
      vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to pre-process since %s", vgId, pMsg, terrstr());
358
      rpcFreeCont(pMsg->pCont);
359
      taosFreeQitem(pMsg);
360
      continue;
361
    }
362

363
    if (isBlock) {
364
      vnodeProposeBatchMsg(pVnode, pMsgArr, pIsWeakArr, &arrayPos);
365
    }
366

367
    pMsgArr[arrayPos] = pMsg;
368
    pIsWeakArr[arrayPos] = isWeak;
369
    arrayPos++;
370

371
    if (isBlock || msg == numOfMsgs - 1) {
372
      vnodeProposeBatchMsg(pVnode, pMsgArr, pIsWeakArr, &arrayPos);
373
    }
374
  }
375

376
  taosMemoryFree(pMsgArr);
377
  taosMemoryFree(pIsWeakArr);
378
}
379

380
#else
381

382
void vnodeProposeWriteMsg(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
16,397,642✔
383
  SVnode  *pVnode = pInfo->ahandle;
16,397,642✔
384
  int32_t  vgId = pVnode->config.vgId;
16,397,642✔
385
  int32_t  code = 0;
16,397,642✔
386
  SRpcMsg *pMsg = NULL;
16,397,642✔
387
  vTrace("vgId:%d, get %d msgs from vnode-write queue", vgId, numOfMsgs);
16,397,642✔
388

389
  for (int32_t msg = 0; msg < numOfMsgs; msg++) {
35,798,493✔
390
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
19,399,969!
391
    bool isWeak = vnodeIsMsgWeak(pMsg->msgType);
19,399,845✔
392

393
    vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-write queue, weak:%d block:%d msg:%d:%d, handle:%p",
19,399,279!
394
            vgId, pMsg, isWeak, vnodeIsMsgBlock(pMsg->msgType), msg, numOfMsgs, pMsg->info.handle);
395

396
    if (!pVnode->restored) {
19,399,604✔
397
      vGWarn(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to process since restore not finished, type:%s", vgId, pMsg,
27,779!
398
             TMSG_INFO(pMsg->msgType));
399
      vnodeHandleProposeError(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
27,779✔
400
      rpcFreeCont(pMsg->pCont);
27,779✔
401
      taosFreeQitem(pMsg);
27,780✔
402
      continue;
27,780✔
403
    }
404

405
    bool atExit = false;
19,371,825✔
406
    vnodeProposeCommitOnNeed(pVnode, atExit);
19,371,825✔
407

408
    code = vnodePreProcessWriteMsg(pVnode, pMsg);
19,372,234✔
409
    if (code != 0) {
19,371,306✔
410
      if (code != TSDB_CODE_MSG_PREPROCESSED) {
115,838✔
411
        vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to pre-process since %s", vgId, pMsg, tstrerror(code));
1,017!
412
      }
413
      vnodeHandleProposeError(pVnode, pMsg, code);
115,838✔
414
      rpcFreeCont(pMsg->pCont);
116,174✔
415
      taosFreeQitem(pMsg);
116,428✔
416
      continue;
116,414✔
417
    }
418

419
    code = vnodeProposeMsg(pVnode, pMsg, isWeak);
19,255,468✔
420

421
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x", vgId, pMsg, code);
19,255,625!
422
    rpcFreeCont(pMsg->pCont);
19,255,625✔
423
    taosFreeQitem(pMsg);
19,255,816✔
424
  }
425
}
16,398,524✔
426

427
#endif
428

429
void vnodeApplyWriteMsg(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
4,934,286✔
430
  SVnode  *pVnode = pInfo->ahandle;
4,934,286✔
431
  int32_t  vgId = pVnode->config.vgId;
4,934,286✔
432
  int32_t  code = 0;
4,934,286✔
433
  SRpcMsg *pMsg = NULL;
4,934,286✔
434

435
  for (int32_t i = 0; i < numOfMsgs; ++i) {
12,789,200✔
436
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
7,854,884!
437

438
    if (vnodeIsMsgBlock(pMsg->msgType)) {
7,854,882✔
439
      vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-apply queue, type:%s handle:%p index:%" PRId64
29,744!
440
              ", blocking msg obtained sec:%d seq:%" PRId64,
441
              vgId, pMsg, TMSG_INFO(pMsg->msgType), pMsg->info.handle, pMsg->info.conn.applyIndex, pVnode->blockSec,
442
              pVnode->blockSeq);
443
    } else {
444
      vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-apply queue, type:%s handle:%p index:%" PRId64, vgId, pMsg,
7,825,122!
445
              TMSG_INFO(pMsg->msgType), pMsg->info.handle, pMsg->info.conn.applyIndex);
446
    }
447

448
    SRpcMsg rsp = {.code = pMsg->code, .info = pMsg->info};
7,854,917✔
449
    if (rsp.code == 0) {
7,854,917!
450
      int32_t ret = 0;
7,854,926✔
451
      int32_t count = 0;
7,854,926✔
452
      while (1) {
453
        ret = vnodeProcessWriteMsg(pVnode, pMsg, pMsg->info.conn.applyIndex, &rsp);
7,854,926✔
454
        if (ret < 0) {
7,854,904!
455
          rsp.code = ret;
×
456
          vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to apply since %s, index:%" PRId64, vgId, pMsg,
×
457
                  tstrerror(ret), pMsg->info.conn.applyIndex);
458
        }
459
        if (ret == TSDB_CODE_VND_WRITE_DISABLED) {
7,854,902!
460
          if (count % 100 == 0)
×
461
            vGError(&pMsg->info.traceId,
×
462
                    "vgId:%d, msg:%p, failed to apply since %s, retry after 200ms, retry count:%d index:%" PRId64, vgId,
463
                    pMsg, tstrerror(ret), count, pMsg->info.conn.applyIndex);
464
          count++;
×
465
          taosMsleep(200);  // wait for a while before retrying
×
466
        } else{
467
          break;
7,854,902✔
468
        } 
469
      }
470
    }
471

472
    vnodePostBlockMsg(pVnode, pMsg);
7,854,893✔
473
    if (rsp.info.handle != NULL) {
7,854,906✔
474
      tmsgSendRsp(&rsp);
181,117✔
475
    } else {
476
      if (rsp.pCont) {
7,673,789✔
477
        rpcFreeCont(rsp.pCont);
7,607,510✔
478
      }
479
    }
480

481
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x index:%" PRId64, vgId, pMsg, rsp.code,
7,854,923!
482
            pMsg->info.conn.applyIndex);
483
    rpcFreeCont(pMsg->pCont);
7,854,923✔
484
    taosFreeQitem(pMsg);
7,854,916✔
485
  }
486
}
4,934,316✔
487

488
int32_t vnodeProcessSyncMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) {
15,108,304✔
489
  vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-sync queue, type:%s", pVnode->config.vgId, pMsg, TMSG_INFO(pMsg->msgType));
15,108,304!
490

491
  int32_t code = syncProcessMsg(pVnode->sync, pMsg);
15,108,316✔
492
  if (code != 0) {
15,108,435✔
493
    vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to process since %s, type:%s", pVnode->config.vgId, pMsg, tstrerror(code),
155!
494
            TMSG_INFO(pMsg->msgType));
495
  }
496

497
  return code;
15,108,365✔
498
}
499

500
static int32_t vnodeSyncEqCtrlMsg(const SMsgCb *msgcb, SRpcMsg *pMsg) {
×
501
  if (pMsg == NULL || pMsg->pCont == NULL) {
×
502
    return TSDB_CODE_INVALID_PARA;
×
503
  }
504

505
  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
×
506
    rpcFreeCont(pMsg->pCont);
×
507
    pMsg->pCont = NULL;
×
508
    return TSDB_CODE_INVALID_PARA;
×
509
  }
510

511
  int32_t code = tmsgPutToQueue(msgcb, SYNC_RD_QUEUE, pMsg);
×
512
  if (code != 0) {
×
513
    rpcFreeCont(pMsg->pCont);
×
514
    pMsg->pCont = NULL;
×
515
  }
516
  return code;
×
517
}
518

519
static int32_t vnodeSyncEqMsg(const SMsgCb *msgcb, SRpcMsg *pMsg) {
463,441✔
520
  if (pMsg == NULL || pMsg->pCont == NULL) {
463,441!
521
    return TSDB_CODE_INVALID_PARA;
×
522
  }
523

524
  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
463,446!
525
    rpcFreeCont(pMsg->pCont);
×
526
    pMsg->pCont = NULL;
×
527
    return TSDB_CODE_INVALID_PARA;
×
528
  }
529

530
  int32_t code = tmsgPutToQueue(msgcb, SYNC_QUEUE, pMsg);
463,448✔
531
  if (code != 0) {
463,446✔
532
    rpcFreeCont(pMsg->pCont);
1,983✔
533
    pMsg->pCont = NULL;
1,984✔
534
  }
535
  return code;
463,447✔
536
}
537

538
static int32_t vnodeSyncSendMsg(const SEpSet *pEpSet, SRpcMsg *pMsg) {
14,687,299✔
539
  int32_t code = tmsgSendSyncReq(pEpSet, pMsg);
14,687,299✔
540
  if (code != 0) {
14,687,329!
541
    rpcFreeCont(pMsg->pCont);
×
542
    pMsg->pCont = NULL;
×
543
  }
544
  return code;
14,687,329✔
545
}
546

547
static int32_t vnodeSyncGetSnapshotInfo(const SSyncFSM *pFsm, SSnapshot *pSnapshot) {
485,367✔
548
  return vnodeGetSnapshot(pFsm->data, pSnapshot);
485,367✔
549
}
550

551
static int32_t vnodeSyncApplyMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, const SFsmCbMeta *pMeta) {
7,856,421✔
552
  SVnode *pVnode = pFsm->data;
7,856,421✔
553
  pMsg->info.conn.applyIndex = pMeta->index;
7,856,421✔
554
  pMsg->info.conn.applyTerm = pMeta->term;
7,856,421✔
555

556
  vGDebug(&pMsg->info.traceId,
7,856,421!
557
          "vgId:%d, index:%" PRId64 ", execute commit cb, fsm:%p, term:%" PRIu64 ", msg-index:%" PRId64
558
          ", weak:%d, code:%d, state:%d %s, type:%s code:0x%x",
559
          pVnode->config.vgId, pMeta->index, pFsm, pMeta->term, pMsg->info.conn.applyIndex, pMeta->isWeak, pMeta->code,
560
          pMeta->state, syncStr(pMeta->state), TMSG_INFO(pMsg->msgType), pMsg->code);
561

562
  int32_t code = tmsgPutToQueue(&pVnode->msgCb, APPLY_QUEUE, pMsg);
7,856,429✔
563
  if (code < 0) {
7,856,426✔
564
    if (code == TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE) {
1,507!
565
      pVnode->applyQueueErrorCount++;
×
566
      if (pVnode->applyQueueErrorCount == APPLY_QUEUE_ERROR_THRESHOLD) {
×
567
        pVnode->applyQueueErrorCount = 0;
×
568
        vWarn("vgId:%d, failed to put into apply_queue since %s", pVnode->config.vgId, tstrerror(code));
×
569
      }
570
    } else {
571
      vError("vgId:%d, failed to put into apply_queue since %s", pVnode->config.vgId, tstrerror(code));
1,507!
572
    }
573
  }
574
  return code;
7,856,426✔
575
}
576

577
static int32_t vnodeSyncCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
7,856,422✔
578
  if (pMsg->code == 0) {
7,856,422!
579
    return vnodeSyncApplyMsg(pFsm, pMsg, pMeta);
7,856,422✔
580
  }
581

582
  SVnode *pVnode = pFsm->data;
×
583
  vnodePostBlockMsg(pVnode, pMsg);
×
584

585
  SRpcMsg rsp = {
×
586
      .code = pMsg->code,
×
587
      .info = pMsg->info,
588
  };
589

590
  if (rsp.info.handle != NULL) {
×
591
    tmsgSendRsp(&rsp);
×
592
  }
593

594
  vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x index:%" PRId64, TD_VID(pVnode), pMsg, rsp.code,
×
595
          pMeta->index);
596
  rpcFreeCont(pMsg->pCont);
×
597
  pMsg->pCont = NULL;
×
598
  return 0;
×
599
}
600

601
static int32_t vnodeSyncPreCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
×
602
  if (pMeta->isWeak == 1) {
×
603
    return vnodeSyncApplyMsg(pFsm, pMsg, pMeta);
×
604
  }
605
  return 0;
×
606
}
607

608
static SyncIndex vnodeSyncAppliedIndex(const SSyncFSM *pFSM) {
19,383,791✔
609
  SVnode *pVnode = pFSM->data;
19,383,791✔
610
  return atomic_load_64(&pVnode->state.applied);
19,383,791✔
611
}
612

613
static void vnodeSyncRollBackMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
×
614
  SVnode *pVnode = pFsm->data;
×
615
  vGDebug(&pMsg->info.traceId,
×
616
          "vgId:%d, rollback-cb is excuted, fsm:%p, index:%" PRId64 ", weak:%d, code:%d, state:%d %s, type:%s",
617
          pVnode->config.vgId, pFsm, pMeta->index, pMeta->isWeak, pMeta->code, pMeta->state, syncStr(pMeta->state),
618
          TMSG_INFO(pMsg->msgType));
619
}
×
620

621
static int32_t vnodeSnapshotStartRead(const SSyncFSM *pFsm, void *pParam, void **ppReader) {
91✔
622
  SVnode *pVnode = pFsm->data;
91✔
623
  return vnodeSnapReaderOpen(pVnode, (SSnapshotParam *)pParam, (SVSnapReader **)ppReader);
91✔
624
}
625

626
static void vnodeSnapshotStopRead(const SSyncFSM *pFsm, void *pReader) {
91✔
627
  SVnode *pVnode = pFsm->data;
91✔
628
  vnodeSnapReaderClose(pReader);
91✔
629
}
91✔
630

631
static int32_t vnodeSnapshotDoRead(const SSyncFSM *pFsm, void *pReader, void **ppBuf, int32_t *len) {
96,737✔
632
  SVnode *pVnode = pFsm->data;
96,737✔
633
  return vnodeSnapRead(pReader, (uint8_t **)ppBuf, len);
96,737✔
634
}
635

636
static int32_t vnodeSnapshotStartWrite(const SSyncFSM *pFsm, void *pParam, void **ppWriter) {
90✔
637
  SVnode *pVnode = pFsm->data;
90✔
638

639
  do {
×
640
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
90✔
641
    if (itemSize == 0) {
90!
642
      vInfo("vgId:%d, start write vnode snapshot since apply queue is empty", pVnode->config.vgId);
90!
643
      break;
90✔
644
    } else {
645
      vInfo("vgId:%d, write vnode snapshot later since %d items in apply queue", pVnode->config.vgId, itemSize);
×
646
      taosMsleep(10);
×
647
    }
648
  } while (true);
649

650
  return vnodeSnapWriterOpen(pVnode, (SSnapshotParam *)pParam, (SVSnapWriter **)ppWriter);
90✔
651
}
652

653
static int32_t vnodeSnapshotStopWrite(const SSyncFSM *pFsm, void *pWriter, bool isApply, SSnapshot *pSnapshot) {
90✔
654
  SVnode *pVnode = pFsm->data;
90✔
655
  vInfo("vgId:%d, stop write vnode snapshot, apply:%d, index:%" PRId64 " term:%" PRIu64 " config:%" PRId64,
90!
656
        pVnode->config.vgId, isApply, pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm, pSnapshot->lastConfigIndex);
657

658
  int32_t code = vnodeSnapWriterClose(pWriter, !isApply, pSnapshot);
90✔
659
  if (code != 0) {
90!
660
    vError("vgId:%d, failed to finish applying vnode snapshot since %s, code:0x%x", pVnode->config.vgId, terrstr(),
×
661
           code);
662
  }
663
  return code;
90✔
664
}
665

666
static int32_t vnodeSnapshotDoWrite(const SSyncFSM *pFsm, void *pWriter, void *pBuf, int32_t len) {
96,618✔
667
  SVnode *pVnode = pFsm->data;
96,618✔
668
  vDebug("vgId:%d, continue write vnode snapshot, blockLen:%d", pVnode->config.vgId, len);
96,618!
669
  int32_t code = vnodeSnapWrite(pWriter, pBuf, len);
96,618✔
670
  vDebug("vgId:%d, continue write vnode snapshot finished, blockLen:%d", pVnode->config.vgId, len);
96,618!
671
  return code;
96,618✔
672
}
673

674
static void vnodeRestoreFinish(const SSyncFSM *pFsm, const SyncIndex commitIdx) {
15,768✔
675
  SVnode   *pVnode = pFsm->data;
15,768✔
676
  int32_t   vgId = pVnode->config.vgId;
15,768✔
677
  SyncIndex appliedIdx = -1;
15,768✔
678

679
  do {
680
    appliedIdx = vnodeSyncAppliedIndex(pFsm);
32,008✔
681
    if (appliedIdx > commitIdx) {
32,011!
682
      vError("vgId:%d, restore failed since applied-index:%" PRId64 " is larger than commit-index:%" PRId64, vgId,
×
683
             appliedIdx, commitIdx);
684
      break;
×
685
    }
686
    if (appliedIdx == commitIdx) {
32,011✔
687
      vInfo("vgId:%d, no items to be applied, restore finish", pVnode->config.vgId);
15,768✔
688
      break;
15,768✔
689
    } else {
690
      if (appliedIdx % 10 == 0) {
16,243✔
691
        vInfo("vgId:%d, restore not finish since %" PRId64 " items to be applied. commit-index:%" PRId64
521!
692
              ", applied-index:%" PRId64,
693
              vgId, commitIdx - appliedIdx, commitIdx, appliedIdx);
694
      } else {
695
        vDebug("vgId:%d, restore not finish since %" PRId64 " items to be applied. commit-index:%" PRId64
15,722✔
696
               ", applied-index:%" PRId64,
697
               vgId, commitIdx - appliedIdx, commitIdx, appliedIdx);
698
      }
699
      taosMsleep(10);
16,243✔
700
    }
701
  } while (true);
702

703
  walApplyVer(pVnode->pWal, commitIdx);
15,768✔
704
  pVnode->restored = true;
15,768✔
705
}
15,768✔
706

707
static void vnodeBecomeFollower(const SSyncFSM *pFsm) {
6,758✔
708
  SVnode *pVnode = pFsm->data;
6,758✔
709
  vInfo("vgId:%d, become follower", pVnode->config.vgId);
6,758!
710

711
  (void)taosThreadMutexLock(&pVnode->lock);
6,758✔
712
  if (pVnode->blocked) {
6,758!
713
    pVnode->blocked = false;
×
714
    vDebug("vgId:%d, become follower and post block", pVnode->config.vgId);
×
715
    if (tsem_post(&pVnode->syncSem) != 0) {
×
716
      vError("vgId:%d, failed to post sync semaphore", pVnode->config.vgId);
×
717
    }
718
  }
719
  (void)taosThreadMutexUnlock(&pVnode->lock);
6,758✔
720

721
  streamRemoveVnodeLeader(pVnode->config.vgId);
6,758✔
722
}
6,758✔
723

724
static void vnodeBecomeLearner(const SSyncFSM *pFsm) {
281✔
725
  SVnode *pVnode = pFsm->data;
281✔
726
  vInfo("vgId:%d, become learner", pVnode->config.vgId);
281!
727

728
  (void)taosThreadMutexLock(&pVnode->lock);
281✔
729
  if (pVnode->blocked) {
281!
730
    pVnode->blocked = false;
×
731
    vDebug("vgId:%d, become learner and post block", pVnode->config.vgId);
×
732
    if (tsem_post(&pVnode->syncSem) != 0) {
×
733
      vError("vgId:%d, failed to post sync semaphore", pVnode->config.vgId);
×
734
    }
735
  }
736
  (void)taosThreadMutexUnlock(&pVnode->lock);
281✔
737

738
  streamRemoveVnodeLeader(pVnode->config.vgId);  
281✔
739
}
281✔
740

741
static void vnodeBecomeLeader(const SSyncFSM *pFsm) {
12,276✔
742
  SVnode *pVnode = pFsm->data;
12,276✔
743
  vDebug("vgId:%d, become leader", pVnode->config.vgId);
12,276✔
744

745
  streamAddVnodeLeader(pVnode->config.vgId);
12,276✔
746
}
12,277✔
747

748
static void vnodeBecomeAssignedLeader(const SSyncFSM *pFsm) {
×
749
  SVnode *pVnode = pFsm->data;
×
750
  vDebug("vgId:%d, become assigned leader", pVnode->config.vgId);
×
751

752
  streamAddVnodeLeader(pVnode->config.vgId);
×
753
}
×
754

755
static bool vnodeApplyQueueEmpty(const SSyncFSM *pFsm) {
×
756
  SVnode *pVnode = pFsm->data;
×
757

758
  if (pVnode != NULL && pVnode->msgCb.qsizeFp != NULL) {
×
759
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
×
760
    return (itemSize == 0);
×
761
  } else {
762
    return true;
×
763
  }
764
}
765

766
static int32_t vnodeApplyQueueItems(const SSyncFSM *pFsm) {
91,303✔
767
  SVnode *pVnode = pFsm->data;
91,303✔
768

769
  if (pVnode != NULL && pVnode->msgCb.qsizeFp != NULL) {
91,303!
770
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
91,314✔
771
    return itemSize;
91,308✔
772
  } else {
773
    return TSDB_CODE_INVALID_PARA;
×
774
  }
775
}
776

777
static SSyncFSM *vnodeSyncMakeFsm(SVnode *pVnode) {
15,061✔
778
  SSyncFSM *pFsm = taosMemoryCalloc(1, sizeof(SSyncFSM));
15,061!
779
  if (pFsm == NULL) {
15,061!
780
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
781
    return NULL;
×
782
  }
783
  pFsm->data = pVnode;
15,061✔
784
  pFsm->FpCommitCb = vnodeSyncCommitMsg;
15,061✔
785
  pFsm->FpAppliedIndexCb = vnodeSyncAppliedIndex;
15,061✔
786
  pFsm->FpPreCommitCb = vnodeSyncPreCommitMsg;
15,061✔
787
  pFsm->FpRollBackCb = vnodeSyncRollBackMsg;
15,061✔
788
  pFsm->FpGetSnapshot = NULL;
15,061✔
789
  pFsm->FpGetSnapshotInfo = vnodeSyncGetSnapshotInfo;
15,061✔
790
  pFsm->FpRestoreFinishCb = vnodeRestoreFinish;
15,061✔
791
  pFsm->FpAfterRestoredCb = NULL;
15,061✔
792
  pFsm->FpLeaderTransferCb = NULL;
15,061✔
793
  pFsm->FpApplyQueueEmptyCb = vnodeApplyQueueEmpty;
15,061✔
794
  pFsm->FpApplyQueueItems = vnodeApplyQueueItems;
15,061✔
795
  pFsm->FpBecomeLeaderCb = vnodeBecomeLeader;
15,061✔
796
  pFsm->FpBecomeAssignedLeaderCb = vnodeBecomeAssignedLeader;
15,061✔
797
  pFsm->FpBecomeFollowerCb = vnodeBecomeFollower;
15,061✔
798
  pFsm->FpBecomeLearnerCb = vnodeBecomeLearner;
15,061✔
799
  pFsm->FpReConfigCb = NULL;
15,061✔
800
  pFsm->FpSnapshotStartRead = vnodeSnapshotStartRead;
15,061✔
801
  pFsm->FpSnapshotStopRead = vnodeSnapshotStopRead;
15,061✔
802
  pFsm->FpSnapshotDoRead = vnodeSnapshotDoRead;
15,061✔
803
  pFsm->FpSnapshotStartWrite = vnodeSnapshotStartWrite;
15,061✔
804
  pFsm->FpSnapshotStopWrite = vnodeSnapshotStopWrite;
15,061✔
805
  pFsm->FpSnapshotDoWrite = vnodeSnapshotDoWrite;
15,061✔
806

807
  return pFsm;
15,061✔
808
}
809

810
int32_t vnodeSyncOpen(SVnode *pVnode, char *path, int32_t vnodeVersion) {
15,061✔
811
  SSyncInfo syncInfo = {
15,061✔
812
      .snapshotStrategy = SYNC_STRATEGY_WAL_FIRST,
813
      .batchSize = 1,
814
      .vgId = pVnode->config.vgId,
15,061✔
815
      .mountVgId = pVnode->config.mountVgId,
15,061✔
816
      .syncCfg = pVnode->config.syncCfg,
817
      .pWal = pVnode->pWal,
15,061✔
818
      .msgcb = &pVnode->msgCb,
15,061✔
819
      .syncSendMSg = vnodeSyncSendMsg,
820
      .syncEqMsg = vnodeSyncEqMsg,
821
      .syncEqCtrlMsg = vnodeSyncEqCtrlMsg,
822
      .pingMs = 5000,
823
      .electMs = 4000,
824
      .heartbeatMs = 700,
825
  };
826

827
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s%ssync", path, TD_DIRSEP);
15,061✔
828
  syncInfo.pFsm = vnodeSyncMakeFsm(pVnode);
15,061✔
829

830
  SSyncCfg *pCfg = &syncInfo.syncCfg;
15,061✔
831
  vInfo("vgId:%d, start to open sync, replica:%d selfIndex:%d", pVnode->config.vgId, pCfg->replicaNum, pCfg->myIndex);
15,061✔
832
  for (int32_t i = 0; i < pCfg->totalReplicaNum; ++i) {
39,213✔
833
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
24,152✔
834
    vInfo("vgId:%d, index:%d ep:%s:%u dnode:%d cluster:%" PRId64, pVnode->config.vgId, i, pNode->nodeFqdn,
24,152✔
835
          pNode->nodePort, pNode->nodeId, pNode->clusterId);
836
  }
837

838
  pVnode->sync = syncOpen(&syncInfo, vnodeVersion);
15,061✔
839
  if (pVnode->sync <= 0) {
15,061!
840
    vError("vgId:%d, failed to open sync since %s", pVnode->config.vgId, terrstr());
×
841
    return terrno;
×
842
  }
843

844
  return 0;
15,061✔
845
}
846

847
int32_t vnodeSyncStart(SVnode *pVnode) {
15,061✔
848
  vInfo("vgId:%d, start sync", pVnode->config.vgId);
15,061✔
849
  int32_t code = syncStart(pVnode->sync);
15,061✔
850
  if (code) {
15,061!
851
    vError("vgId:%d, failed to start sync subsystem since %s", pVnode->config.vgId, tstrerror(code));
×
852
    return code;
×
853
  }
854
  return 0;
15,061✔
855
}
856

857
void vnodeSyncPreClose(SVnode *pVnode) {
15,061✔
858
  vInfo("vgId:%d, sync pre close", pVnode->config.vgId);
15,061✔
859
  int32_t code = syncLeaderTransfer(pVnode->sync);
15,061✔
860
  if (code) {
15,061✔
861
    vError("vgId:%d, failed to transfer leader since %s", pVnode->config.vgId, tstrerror(code));
1,344!
862
  }
863
  syncPreStop(pVnode->sync);
15,061✔
864

865
  (void)taosThreadMutexLock(&pVnode->lock);
15,060✔
866
  if (pVnode->blocked) {
15,060✔
867
    vInfo("vgId:%d, post block after close sync", pVnode->config.vgId);
30!
868
    pVnode->blocked = false;
30✔
869
    if (tsem_post(&pVnode->syncSem) != 0) {
30!
870
      vError("vgId:%d, failed to post block", pVnode->config.vgId);
×
871
    }
872
  }
873
  (void)taosThreadMutexUnlock(&pVnode->lock);
15,060✔
874
}
15,060✔
875

876
void vnodeSyncPostClose(SVnode *pVnode) {
15,061✔
877
  vInfo("vgId:%d, sync post close", pVnode->config.vgId);
15,061✔
878
  syncPostStop(pVnode->sync);
15,061✔
879
}
15,061✔
880

881
void vnodeSyncClose(SVnode *pVnode) {
15,061✔
882
  vInfo("vgId:%d, close sync", pVnode->config.vgId);
15,061✔
883
  syncStop(pVnode->sync);
15,061✔
884
}
15,060✔
885

886
void vnodeSyncCheckTimeout(SVnode *pVnode) {
38,777✔
887
  vTrace("vgId:%d, check sync timeout msg", pVnode->config.vgId);
38,777✔
888
  (void)taosThreadMutexLock(&pVnode->lock);
38,777✔
889
  if (pVnode->blocked) {
38,777!
890
    int32_t curSec = taosGetTimestampSec();
×
891
    int32_t delta = curSec - pVnode->blockSec;
×
892
    if (delta > VNODE_TIMEOUT_SEC) {
×
893
      vError("vgId:%d, failed to propose since timeout and post block, start:%d cur:%d delta:%d seq:%" PRId64,
×
894
             pVnode->config.vgId, pVnode->blockSec, curSec, delta, pVnode->blockSeq);
895
      if (syncSendTimeoutRsp(pVnode->sync, pVnode->blockSeq) != 0) {
×
896
#if 0
897
        SRpcMsg rpcMsg = {.code = TSDB_CODE_SYN_TIMEOUT, .info = pVnode->blockInfo};
898
        vError("send timeout response since its applyed, seq:%" PRId64 " handle:%p ahandle:%p", pVnode->blockSeq,
899
              rpcMsg.info.handle, rpcMsg.info.ahandle);
900
        rpcSendResponse(&rpcMsg);
901
#endif
902
      }
903
      pVnode->blocked = false;
×
904
      pVnode->blockSec = 0;
×
905
      pVnode->blockSeq = 0;
×
906
      if (tsem_post(&pVnode->syncSem) != 0) {
×
907
        vError("vgId:%d, failed to post block", pVnode->config.vgId);
×
908
      }
909
    }
910
  }
911
  (void)taosThreadMutexUnlock(&pVnode->lock);
38,777✔
912
}
38,777✔
913

914
bool vnodeIsRoleLeader(SVnode *pVnode) {
×
915
  SSyncState state = syncGetState(pVnode->sync);
×
916
  return state.state == TAOS_SYNC_STATE_LEADER;
×
917
}
918

919
bool vnodeIsLeader(SVnode *pVnode) {
15,060✔
920
  terrno = 0;
15,060✔
921
  SSyncState state = syncGetState(pVnode->sync);
15,059✔
922

923
  if (terrno != 0) {
15,061✔
924
    vInfo("vgId:%d, vnode is stopping", pVnode->config.vgId);
2,833!
925
    return false;
2,833✔
926
  }
927

928
  if (state.state != TAOS_SYNC_STATE_LEADER) {
12,228!
929
    terrno = TSDB_CODE_SYN_NOT_LEADER;
×
930
    vInfo("vgId:%d, vnode not leader, state:%s", pVnode->config.vgId, syncStr(state.state));
×
931
    return false;
×
932
  }
933

934
  if (!state.restored || !pVnode->restored) {
12,228!
935
    terrno = TSDB_CODE_SYN_RESTORING;
×
936
    vInfo("vgId:%d, vnode not restored:%d:%d", pVnode->config.vgId, state.restored, pVnode->restored);
×
937
    return false;
×
938
  }
939

940
  return true;
12,228✔
941
}
942

943
int64_t vnodeClusterId(SVnode *pVnode) {
×
944
  SSyncCfg *syncCfg = &pVnode->config.syncCfg;
×
945
  return syncCfg->nodeInfo[syncCfg->myIndex].clusterId;
×
946
}
947

948
int32_t vnodeNodeId(SVnode *pVnode) {
562,566✔
949
  SSyncCfg *syncCfg = &pVnode->config.syncCfg;
562,566✔
950
  return syncCfg->nodeInfo[syncCfg->myIndex].nodeId;
562,566✔
951
}
952

953
int32_t vnodeGetSnapshot(SVnode *pVnode, SSnapshot *pSnap) {
485,448✔
954
  int code = 0;
485,448✔
955
  pSnap->lastApplyIndex = pVnode->state.committed;
485,448✔
956
  pSnap->lastApplyTerm = pVnode->state.commitTerm;
485,448✔
957
  pSnap->lastConfigIndex = -1;
485,448✔
958
  pSnap->state = SYNC_FSM_STATE_COMPLETE;
485,448✔
959

960
  if (tsdbSnapGetFsState(pVnode) != TSDB_FS_STATE_NORMAL) {
485,448!
961
    pSnap->state = SYNC_FSM_STATE_INCOMPLETE;
×
962
  }
963

964
  if (pSnap->type == TDMT_SYNC_PREP_SNAPSHOT || pSnap->type == TDMT_SYNC_PREP_SNAPSHOT_REPLY) {
485,448✔
965
    code = tsdbSnapPrepDescription(pVnode, pSnap);
180✔
966
  }
967
  return code;
485,476✔
968
}
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