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

taosdata / TDengine / #4142

20 May 2025 07:22AM UTC coverage: 62.238% (-0.9%) from 63.096%
#4142

push

travis-ci

web-flow
docs(datain): add topic meta options docs in tmq (#31147)

155113 of 318088 branches covered (48.76%)

Branch coverage included in aggregate %.

240242 of 317147 relevant lines covered (75.75%)

13602566.83 hits per line

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

66.25
/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 "tqCommon.h"
20
#include "tsdb.h"
21
#include "vnd.h"
22

23
#define BATCH_ENABLE 0
24

25
static inline bool vnodeIsMsgWeak(tmsg_t type) { return false; }
3,037,143✔
26

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

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

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

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

64
  if (code == 0) code = TSDB_CODE_SYN_NOT_LEADER;
37,855!
65

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

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

79
  tmsgSendRsp(&rsp);
37,855✔
80
}
37,855✔
81

82
static void inline vnodeHandleWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,804,615✔
83
  SRpcMsg rsp = {.code = pMsg->code, .info = pMsg->info};
2,804,615✔
84
  if (vnodeProcessWriteMsg(pVnode, pMsg, pMsg->info.conn.applyIndex, &rsp) < 0) {
2,804,615!
85
    rsp.code = terrno;
×
86
    vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to apply right now since %s", pVnode->config.vgId, pMsg,
×
87
            terrstr());
88
  }
89
  if (rsp.info.handle != NULL) {
2,804,754✔
90
    tmsgSendRsp(&rsp);
2,777,928✔
91
  } else {
92
    if (rsp.pCont) {
26,826✔
93
      rpcFreeCont(rsp.pCont);
18,291✔
94
    }
95
  }
96
}
2,804,876✔
97

98
static void vnodeHandleProposeError(SVnode *pVnode, SRpcMsg *pMsg, int32_t code) {
92,542✔
99
  if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING) {
92,542✔
100
    vnodeRedirectRpcMsg(pVnode, pMsg, code);
31,070✔
101
  } else if (code == TSDB_CODE_MSG_PREPROCESSED) {
61,472✔
102
    SRpcMsg rsp = {.code = TSDB_CODE_SUCCESS, .info = pMsg->info};
60,252✔
103
    if (rsp.info.handle != NULL) {
60,252!
104
      tmsgSendRsp(&rsp);
60,318✔
105
    }
106
  } else {
107
    vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to propose since %s, code:0x%x", pVnode->config.vgId, pMsg,
1,220!
108
            tstrerror(code), code);
109
    SRpcMsg rsp = {.code = code, .info = pMsg->info};
1,234✔
110
    if (rsp.info.handle != NULL) {
1,234✔
111
      tmsgSendRsp(&rsp);
687✔
112
    }
113
  }
114
}
92,700✔
115

116
static int32_t inline vnodeProposeMsg(SVnode *pVnode, SRpcMsg *pMsg, bool isWeak) {
2,952,764✔
117
  int64_t seq = 0;
2,952,764✔
118

119
  (void)taosThreadMutexLock(&pVnode->lock);
2,952,764✔
120
  int32_t code = syncPropose(pVnode->sync, pMsg, isWeak, &seq);
2,953,211✔
121
  bool    wait = (code == 0 && vnodeIsMsgBlock(pMsg->msgType));
2,952,649✔
122
  if (wait) {
2,952,650✔
123
    if (pVnode->blocked) {
2,652!
124
      (void)taosThreadMutexUnlock(&pVnode->lock);
×
125
      return TSDB_CODE_INTERNAL_ERROR;
×
126
    }
127
    pVnode->blocked = true;
2,652✔
128
    pVnode->blockSec = taosGetTimestampSec();
2,652✔
129
    pVnode->blockSeq = seq;
2,652✔
130
  }
131
  (void)taosThreadMutexUnlock(&pVnode->lock);
2,952,650✔
132

133
  if (code > 0) {
2,953,210✔
134
    vnodeHandleWriteMsg(pVnode, pMsg);
2,805,141✔
135
  } else if (code < 0) {
148,069✔
136
    if (terrno != 0) code = terrno;
8,033!
137
    vnodeHandleProposeError(pVnode, pMsg, code);
8,033✔
138
  }
139

140
  if (wait) vnodeWaitBlockMsg(pVnode, pMsg);
2,952,897✔
141
  return code;
2,952,983✔
142
}
143

144
void vnodeProposeCommitOnNeed(SVnode *pVnode, bool atExit) {
3,024,676✔
145
  if (!vnodeShouldCommit(pVnode, atExit)) {
3,024,676✔
146
    return;
3,016,641✔
147
  }
148

149
  int32_t   contLen = sizeof(SMsgHead);
8,605✔
150
  SMsgHead *pHead = rpcMallocCont(contLen);
8,605✔
151
  pHead->contLen = contLen;
8,606✔
152
  pHead->vgId = pVnode->config.vgId;
8,606✔
153

154
  SRpcMsg rpcMsg = {0};
8,606✔
155
  rpcMsg.msgType = TDMT_VND_COMMIT;
8,606✔
156
  rpcMsg.contLen = contLen;
8,606✔
157
  rpcMsg.pCont = pHead;
8,606✔
158
  rpcMsg.info.noResp = 1;
8,606✔
159

160
  vInfo("vgId:%d, propose vnode commit", pVnode->config.vgId);
8,606✔
161
  bool isWeak = false;
8,607✔
162

163
  if (!atExit) {
8,607✔
164
    if (vnodeProposeMsg(pVnode, &rpcMsg, isWeak) < 0) {
297!
165
      vTrace("vgId:%d, failed to propose vnode commit since %s", pVnode->config.vgId, terrstr());
×
166
    }
167
    rpcFreeCont(rpcMsg.pCont);
297✔
168
    rpcMsg.pCont = NULL;
297✔
169
  } else {
170
    int32_t code = 0;
8,310✔
171
    if ((code = tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &rpcMsg)) < 0) {
8,310✔
172
      vError("vgId:%d, failed to put vnode commit to write_queue since %s", pVnode->config.vgId, tstrerror(code));
3,494✔
173
    }
174
  }
175
}
176

177
#if BATCH_ENABLE
178

179
static void inline vnodeProposeBatchMsg(SVnode *pVnode, SRpcMsg **pMsgArr, bool *pIsWeakArr, int32_t *arrSize) {
180
  if (*arrSize <= 0) return;
181
  SRpcMsg *pLastMsg = pMsgArr[*arrSize - 1];
182

183
  (void)taosThreadMutexLock(&pVnode->lock);
184
  int32_t code = syncProposeBatch(pVnode->sync, pMsgArr, pIsWeakArr, *arrSize);
185
  bool    wait = (code == 0 && vnodeIsBlockMsg(pLastMsg->msgType));
186
  if (wait) {
187
    pVnode->blocked = true;
188
  }
189
  (void)taosThreadMutexUnlock(&pVnode->lock);
190

191
  if (code > 0) {
192
    for (int32_t i = 0; i < *arrSize; ++i) {
193
      vnodeHandleWriteMsg(pVnode, pMsgArr[i]);
194
    }
195
  } else if (code < 0) {
196
    if (terrno != 0) code = terrno;
197
    for (int32_t i = 0; i < *arrSize; ++i) {
198
      vnodeHandleProposeError(pVnode, pMsgArr[i], code);
199
    }
200
  }
201

202
  if (wait) vnodeWaitBlockMsg(pVnode, pLastMsg);
203
  pLastMsg = NULL;
204

205
  for (int32_t i = 0; i < *arrSize; ++i) {
206
    SRpcMsg        *pMsg = pMsgArr[i];
207
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x", pVnode->config.vgId, pMsg, code);
208
    rpcFreeCont(pMsg->pCont);
209
    taosFreeQitem(pMsg);
210
  }
211

212
  *arrSize = 0;
213
}
214

215
void vnodeProposeWriteMsg(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
216
  SVnode   *pVnode = pInfo->ahandle;
217
  int32_t   vgId = pVnode->config.vgId;
218
  int32_t   code = 0;
219
  SRpcMsg  *pMsg = NULL;
220
  int32_t   arrayPos = 0;
221
  SRpcMsg **pMsgArr = taosMemoryCalloc(numOfMsgs, sizeof(SRpcMsg *));
222
  bool     *pIsWeakArr = taosMemoryCalloc(numOfMsgs, sizeof(bool));
223
  vTrace("vgId:%d, get %d msgs from vnode-write queue", vgId, numOfMsgs);
224

225
  for (int32_t msg = 0; msg < numOfMsgs; msg++) {
226
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
227
    bool isWeak = vnodeIsMsgWeak(pMsg->msgType);
228
    bool isBlock = vnodeIsMsgBlock(pMsg->msgType);
229

230
    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,
231
            isWeak, isBlock, msg, numOfMsgs, arrayPos, pMsg->info.handle);
232

233
    if (!pVnode->restored) {
234
      vGWarn(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to process since restore not finished, type:%s", vgId, pMsg,
235
             TMSG_INFO(pMsg->msgType));
236
      terrno = TSDB_CODE_SYN_RESTORING;
237
      vnodeHandleProposeError(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
238
      rpcFreeCont(pMsg->pCont);
239
      taosFreeQitem(pMsg);
240
      continue;
241
    }
242

243
    if (pMsgArr == NULL || pIsWeakArr == NULL) {
244
      vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to process since out of memory, type:%s", vgId, pMsg, TMSG_INFO(pMsg->msgType));
245
      terrno = TSDB_CODE_OUT_OF_MEMORY;
246
      vnodeHandleProposeError(pVnode, pMsg, terrno);
247
      rpcFreeCont(pMsg->pCont);
248
      taosFreeQitem(pMsg);
249
      continue;
250
    }
251

252
    bool atExit = false;
253
    vnodeProposeCommitOnNeed(pVnode, atExit);
254

255
    code = vnodePreProcessWriteMsg(pVnode, pMsg);
256
    if (code != 0) {
257
      vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to pre-process since %s", vgId, pMsg, terrstr());
258
      rpcFreeCont(pMsg->pCont);
259
      taosFreeQitem(pMsg);
260
      continue;
261
    }
262

263
    if (isBlock) {
264
      vnodeProposeBatchMsg(pVnode, pMsgArr, pIsWeakArr, &arrayPos);
265
    }
266

267
    pMsgArr[arrayPos] = pMsg;
268
    pIsWeakArr[arrayPos] = isWeak;
269
    arrayPos++;
270

271
    if (isBlock || msg == numOfMsgs - 1) {
272
      vnodeProposeBatchMsg(pVnode, pMsgArr, pIsWeakArr, &arrayPos);
273
    }
274
  }
275

276
  taosMemoryFree(pMsgArr);
277
  taosMemoryFree(pIsWeakArr);
278
}
279

280
#else
281

282
void vnodeProposeWriteMsg(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
2,935,406✔
283
  SVnode  *pVnode = pInfo->ahandle;
2,935,406✔
284
  int32_t  vgId = pVnode->config.vgId;
2,935,406✔
285
  int32_t  code = 0;
2,935,406✔
286
  SRpcMsg *pMsg = NULL;
2,935,406✔
287
  vTrace("vgId:%d, get %d msgs from vnode-write queue", vgId, numOfMsgs);
2,935,406✔
288

289
  for (int32_t msg = 0; msg < numOfMsgs; msg++) {
5,973,303✔
290
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
3,037,272!
291
    bool isWeak = vnodeIsMsgWeak(pMsg->msgType);
3,037,463✔
292

293
    vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-write queue, weak:%d block:%d msg:%d:%d, handle:%p",
3,037,174!
294
            vgId, pMsg, isWeak, vnodeIsMsgBlock(pMsg->msgType), msg, numOfMsgs, pMsg->info.handle);
295

296
    if (!pVnode->restored) {
3,037,213✔
297
      vGWarn(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to process since restore not finished, type:%s", vgId, pMsg,
23,814!
298
             TMSG_INFO(pMsg->msgType));
299
      vnodeHandleProposeError(pVnode, pMsg, TSDB_CODE_SYN_RESTORING);
23,814✔
300
      rpcFreeCont(pMsg->pCont);
23,814✔
301
      taosFreeQitem(pMsg);
23,814✔
302
      continue;
23,814✔
303
    }
304

305
    bool atExit = false;
3,013,399✔
306
    vnodeProposeCommitOnNeed(pVnode, atExit);
3,013,399✔
307

308
    code = vnodePreProcessWriteMsg(pVnode, pMsg);
3,013,833✔
309
    if (code != 0) {
3,013,226✔
310
      if (code != TSDB_CODE_MSG_PREPROCESSED) {
60,716✔
311
        vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to pre-process since %s", vgId, pMsg, tstrerror(code));
513!
312
      }
313
      vnodeHandleProposeError(pVnode, pMsg, code);
60,716✔
314
      rpcFreeCont(pMsg->pCont);
60,817✔
315
      taosFreeQitem(pMsg);
60,913✔
316
      continue;
60,919✔
317
    }
318

319
    code = vnodeProposeMsg(pVnode, pMsg, isWeak);
2,952,510✔
320

321
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x", vgId, pMsg, code);
2,952,563!
322
    rpcFreeCont(pMsg->pCont);
2,952,562✔
323
    taosFreeQitem(pMsg);
2,952,788✔
324
  }
325
}
2,936,031✔
326

327
#endif
328

329
void vnodeApplyWriteMsg(SQueueInfo *pInfo, STaosQall *qall, int32_t numOfMsgs) {
583,066✔
330
  SVnode  *pVnode = pInfo->ahandle;
583,066✔
331
  int32_t  vgId = pVnode->config.vgId;
583,066✔
332
  int32_t  code = 0;
583,066✔
333
  SRpcMsg *pMsg = NULL;
583,066✔
334

335
  for (int32_t i = 0; i < numOfMsgs; ++i) {
1,459,653✔
336
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
876,577!
337

338
    if (vnodeIsMsgBlock(pMsg->msgType)) {
876,579✔
339
      vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-apply queue, type:%s handle:%p index:%" PRId64
26,015!
340
              ", blocking msg obtained sec:%d seq:%" PRId64,
341
              vgId, pMsg, TMSG_INFO(pMsg->msgType), pMsg->info.handle, pMsg->info.conn.applyIndex, pVnode->blockSec,
342
              pVnode->blockSeq);
343
    } else {
344
      vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-apply queue, type:%s handle:%p index:%" PRId64, vgId, pMsg,
850,565!
345
              TMSG_INFO(pMsg->msgType), pMsg->info.handle, pMsg->info.conn.applyIndex);
346
    }
347

348
    SRpcMsg rsp = {.code = pMsg->code, .info = pMsg->info};
876,592✔
349
    if (rsp.code == 0) {
876,592!
350
      if (vnodeProcessWriteMsg(pVnode, pMsg, pMsg->info.conn.applyIndex, &rsp) < 0) {
876,597!
351
        rsp.code = terrno;
×
352
        vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to apply since %s, index:%" PRId64, vgId, pMsg, terrstr(),
×
353
                pMsg->info.conn.applyIndex);
354
      }
355
    }
356

357
    vnodePostBlockMsg(pVnode, pMsg);
876,580✔
358
    if (rsp.info.handle != NULL) {
876,584✔
359
      tmsgSendRsp(&rsp);
139,972✔
360
    } else {
361
      if (rsp.pCont) {
736,612✔
362
        rpcFreeCont(rsp.pCont);
678,582✔
363
      }
364
    }
365

366
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x index:%" PRId64, vgId, pMsg, rsp.code,
876,591!
367
            pMsg->info.conn.applyIndex);
368
    rpcFreeCont(pMsg->pCont);
876,591✔
369
    taosFreeQitem(pMsg);
876,590✔
370
  }
371
}
583,076✔
372

373
int32_t vnodeProcessSyncMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) {
1,260,426✔
374
  vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-sync queue, type:%s", pVnode->config.vgId, pMsg, TMSG_INFO(pMsg->msgType));
1,260,426!
375

376
  int32_t code = syncProcessMsg(pVnode->sync, pMsg);
1,260,436✔
377
  if (code != 0) {
1,260,471✔
378
    vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to process since %s, type:%s", pVnode->config.vgId, pMsg, tstrerror(code),
48!
379
            TMSG_INFO(pMsg->msgType));
380
  }
381

382
  return code;
1,260,448✔
383
}
384

385
static int32_t vnodeSyncEqCtrlMsg(const SMsgCb *msgcb, SRpcMsg *pMsg) {
×
386
  if (pMsg == NULL || pMsg->pCont == NULL) {
×
387
    return TSDB_CODE_INVALID_PARA;
×
388
  }
389

390
  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
×
391
    rpcFreeCont(pMsg->pCont);
×
392
    pMsg->pCont = NULL;
×
393
    return TSDB_CODE_INVALID_PARA;
×
394
  }
395

396
  int32_t code = tmsgPutToQueue(msgcb, SYNC_RD_QUEUE, pMsg);
×
397
  if (code != 0) {
×
398
    rpcFreeCont(pMsg->pCont);
×
399
    pMsg->pCont = NULL;
×
400
  }
401
  return code;
×
402
}
403

404
static int32_t vnodeSyncEqMsg(const SMsgCb *msgcb, SRpcMsg *pMsg) {
310,397✔
405
  if (pMsg == NULL || pMsg->pCont == NULL) {
310,397!
406
    return TSDB_CODE_INVALID_PARA;
×
407
  }
408

409
  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
310,398!
410
    rpcFreeCont(pMsg->pCont);
1✔
411
    pMsg->pCont = NULL;
×
412
    return TSDB_CODE_INVALID_PARA;
×
413
  }
414

415
  int32_t code = tmsgPutToQueue(msgcb, SYNC_QUEUE, pMsg);
310,397✔
416
  if (code != 0) {
310,401✔
417
    rpcFreeCont(pMsg->pCont);
1,662✔
418
    pMsg->pCont = NULL;
1,662✔
419
  }
420
  return code;
310,401✔
421
}
422

423
static int32_t vnodeSyncSendMsg(const SEpSet *pEpSet, SRpcMsg *pMsg) {
976,226✔
424
  int32_t code = tmsgSendSyncReq(pEpSet, pMsg);
976,226✔
425
  if (code != 0) {
976,250!
426
    rpcFreeCont(pMsg->pCont);
×
427
    pMsg->pCont = NULL;
×
428
  }
429
  return code;
976,250✔
430
}
431

432
static int32_t vnodeSyncGetSnapshotInfo(const SSyncFSM *pFsm, SSnapshot *pSnapshot) {
464,125✔
433
  return vnodeGetSnapshot(pFsm->data, pSnapshot);
464,125✔
434
}
435

436
static int32_t vnodeSyncApplyMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, const SFsmCbMeta *pMeta) {
876,606✔
437
  SVnode *pVnode = pFsm->data;
876,606✔
438
  pMsg->info.conn.applyIndex = pMeta->index;
876,606✔
439
  pMsg->info.conn.applyTerm = pMeta->term;
876,606✔
440

441
  vGDebug(&pMsg->info.traceId,
876,606!
442
          "vgId:%d, index:%" PRId64 ", execute commit cb, fsm:%p, term:%" PRIu64 ", msg-index:%" PRId64
443
          ", weak:%d, code:%d, state:%d %s, type:%s code:0x%x",
444
          pVnode->config.vgId, pMeta->index, pFsm, pMeta->term, pMsg->info.conn.applyIndex, pMeta->isWeak, pMeta->code,
445
          pMeta->state, syncStr(pMeta->state), TMSG_INFO(pMsg->msgType), pMsg->code);
446

447
  int32_t code = tmsgPutToQueue(&pVnode->msgCb, APPLY_QUEUE, pMsg);
876,607✔
448
  if (code < 0) {
876,607✔
449
    if (code == TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE) {
10!
450
      pVnode->applyQueueErrorCount++;
×
451
      if (pVnode->applyQueueErrorCount == APPLY_QUEUE_ERROR_THRESHOLD) {
×
452
        pVnode->applyQueueErrorCount = 0;
×
453
        vWarn("vgId:%d, failed to put into apply_queue since %s", pVnode->config.vgId, tstrerror(code));
×
454
      }
455
    } else {
456
      vError("vgId:%d, failed to put into apply_queue since %s", pVnode->config.vgId, tstrerror(code));
10!
457
    }
458
  }
459
  return code;
876,607✔
460
}
461

462
static int32_t vnodeSyncCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
876,606✔
463
  if (pMsg->code == 0) {
876,606!
464
    return vnodeSyncApplyMsg(pFsm, pMsg, pMeta);
876,606✔
465
  }
466

467
  SVnode *pVnode = pFsm->data;
×
468
  vnodePostBlockMsg(pVnode, pMsg);
×
469

470
  SRpcMsg rsp = {
×
471
      .code = pMsg->code,
×
472
      .info = pMsg->info,
473
  };
474

475
  if (rsp.info.handle != NULL) {
×
476
    tmsgSendRsp(&rsp);
×
477
  }
478

479
  vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x index:%" PRId64, TD_VID(pVnode), pMsg, rsp.code,
×
480
          pMeta->index);
481
  rpcFreeCont(pMsg->pCont);
×
482
  pMsg->pCont = NULL;
×
483
  return 0;
×
484
}
485

486
static int32_t vnodeSyncPreCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
×
487
  if (pMeta->isWeak == 1) {
×
488
    return vnodeSyncApplyMsg(pFsm, pMsg, pMeta);
×
489
  }
490
  return 0;
×
491
}
492

493
static SyncIndex vnodeSyncAppliedIndex(const SSyncFSM *pFSM) {
3,089,327✔
494
  SVnode *pVnode = pFSM->data;
3,089,327✔
495
  return atomic_load_64(&pVnode->state.applied);
3,089,327✔
496
}
497

498
static void vnodeSyncRollBackMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
×
499
  SVnode *pVnode = pFsm->data;
×
500
  vGDebug(&pMsg->info.traceId,
×
501
          "vgId:%d, rollback-cb is excuted, fsm:%p, index:%" PRId64 ", weak:%d, code:%d, state:%d %s, type:%s",
502
          pVnode->config.vgId, pFsm, pMeta->index, pMeta->isWeak, pMeta->code, pMeta->state, syncStr(pMeta->state),
503
          TMSG_INFO(pMsg->msgType));
504
}
×
505

506
static int32_t vnodeSnapshotStartRead(const SSyncFSM *pFsm, void *pParam, void **ppReader) {
58✔
507
  SVnode *pVnode = pFsm->data;
58✔
508
  return vnodeSnapReaderOpen(pVnode, (SSnapshotParam *)pParam, (SVSnapReader **)ppReader);
58✔
509
}
510

511
static void vnodeSnapshotStopRead(const SSyncFSM *pFsm, void *pReader) {
58✔
512
  SVnode *pVnode = pFsm->data;
58✔
513
  vnodeSnapReaderClose(pReader);
58✔
514
}
58✔
515

516
static int32_t vnodeSnapshotDoRead(const SSyncFSM *pFsm, void *pReader, void **ppBuf, int32_t *len) {
2,058✔
517
  SVnode *pVnode = pFsm->data;
2,058✔
518
  return vnodeSnapRead(pReader, (uint8_t **)ppBuf, len);
2,058✔
519
}
520

521
static int32_t vnodeSnapshotStartWrite(const SSyncFSM *pFsm, void *pParam, void **ppWriter) {
57✔
522
  SVnode *pVnode = pFsm->data;
57✔
523

524
  do {
×
525
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
57✔
526
    if (itemSize == 0) {
57!
527
      vInfo("vgId:%d, start write vnode snapshot since apply queue is empty", pVnode->config.vgId);
57!
528
      break;
57✔
529
    } else {
530
      vInfo("vgId:%d, write vnode snapshot later since %d items in apply queue", pVnode->config.vgId, itemSize);
×
531
      taosMsleep(10);
×
532
    }
533
  } while (true);
534

535
  return vnodeSnapWriterOpen(pVnode, (SSnapshotParam *)pParam, (SVSnapWriter **)ppWriter);
57✔
536
}
537

538
static int32_t vnodeSnapshotStopWrite(const SSyncFSM *pFsm, void *pWriter, bool isApply, SSnapshot *pSnapshot) {
57✔
539
  SVnode *pVnode = pFsm->data;
57✔
540
  vInfo("vgId:%d, stop write vnode snapshot, apply:%d, index:%" PRId64 " term:%" PRIu64 " config:%" PRId64,
57!
541
        pVnode->config.vgId, isApply, pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm, pSnapshot->lastConfigIndex);
542

543
  int32_t code = vnodeSnapWriterClose(pWriter, !isApply, pSnapshot);
57✔
544
  if (code != 0) {
57!
545
    vError("vgId:%d, failed to finish applying vnode snapshot since %s, code:0x%x", pVnode->config.vgId, terrstr(),
×
546
           code);
547
  }
548
  return code;
57✔
549
}
550

551
static int32_t vnodeSnapshotDoWrite(const SSyncFSM *pFsm, void *pWriter, void *pBuf, int32_t len) {
1,972✔
552
  SVnode *pVnode = pFsm->data;
1,972✔
553
  vDebug("vgId:%d, continue write vnode snapshot, blockLen:%d", pVnode->config.vgId, len);
1,972!
554
  int32_t code = vnodeSnapWrite(pWriter, pBuf, len);
1,972✔
555
  vDebug("vgId:%d, continue write vnode snapshot finished, blockLen:%d", pVnode->config.vgId, len);
1,972!
556
  return code;
1,972✔
557
}
558

559
static void vnodeRestoreFinish(const SSyncFSM *pFsm, const SyncIndex commitIdx) {
14,409✔
560
  SVnode   *pVnode = pFsm->data;
14,409✔
561
  int32_t   vgId = pVnode->config.vgId;
14,409✔
562
  SyncIndex appliedIdx = -1;
14,409✔
563

564
  do {
565
    appliedIdx = vnodeSyncAppliedIndex(pFsm);
47,770✔
566
    if (appliedIdx > commitIdx) {
47,813✔
567
      vError("vgId:%d, restore failed since applied-index:%" PRId64 " is larger than commit-index:%" PRId64, vgId,
1!
568
             appliedIdx, commitIdx);
569
      break;
×
570
    }
571
    if (appliedIdx == commitIdx) {
47,812✔
572
      vInfo("vgId:%d, no items to be applied, restore finish", pVnode->config.vgId);
14,409✔
573
      break;
14,409✔
574
    } else {
575
      if (appliedIdx % 10 == 0) {
33,403✔
576
        vInfo("vgId:%d, restore not finish since %" PRId64 " items to be applied. commit-index:%" PRId64
2,182!
577
              ", applied-index:%" PRId64,
578
              vgId, commitIdx - appliedIdx, commitIdx, appliedIdx);
579
      } else {
580
        vDebug("vgId:%d, restore not finish since %" PRId64 " items to be applied. commit-index:%" PRId64
31,221✔
581
               ", applied-index:%" PRId64,
582
               vgId, commitIdx - appliedIdx, commitIdx, appliedIdx);
583
      }
584
      taosMsleep(10);
33,407✔
585
    }
586
  } while (true);
587

588
  walApplyVer(pVnode->pWal, commitIdx);
14,409✔
589
  pVnode->restored = true;
14,409✔
590

591
#ifdef USE_STREAM
592
  SStreamMeta *pMeta = pVnode->pTq->pStreamMeta;
14,409✔
593
  streamMetaWLock(pMeta);
14,409✔
594

595
  if (pMeta->startInfo.tasksWillRestart) {
14,409✔
596
    vInfo("vgId:%d, sync restore finished, stream tasks will be launched by other thread", vgId);
1,143!
597
    streamMetaWUnLock(pMeta);
1,143✔
598
    return;
1,143✔
599
  }
600

601
  if (vnodeIsRoleLeader(pVnode)) {
13,266✔
602
    // start to restore all stream tasks
603
    if (tsDisableStream) {
10,288!
604
      vInfo("vgId:%d, sync restore finished, not launch stream tasks, since stream tasks are disabled", vgId);
×
605
    } else {
606
      vInfo("vgId:%d sync restore finished, start to launch stream task(s)", vgId);
10,288✔
607
      if (pMeta->startInfo.startAllTasks == 1) {
10,288!
608
        pMeta->startInfo.restartCount += 1;
×
609
        vDebug("vgId:%d in start tasks procedure, inc restartCounter by 1, remaining restart:%d", vgId,
×
610
               pMeta->startInfo.restartCount);
611
      } else {
612
        pMeta->startInfo.startAllTasks = 1;
10,288✔
613
        streamMetaWUnLock(pMeta);
10,288✔
614

615
        tqInfo("vgId:%d stream task already loaded, start them", vgId);
10,288!
616
        int32_t code = streamTaskSchedTask(&pVnode->msgCb, TD_VID(pVnode), 0, 0, STREAM_EXEC_T_START_ALL_TASKS, false);
10,288✔
617
        if (code != 0) {
10,288!
618
          tqError("vgId:%d failed to sched stream task, code:%s", vgId, tstrerror(code));
×
619
        }
620
        return;
10,288✔
621
      }
622
    }
623
  } else {
624
    vInfo("vgId:%d, sync restore finished, not launch stream tasks since not leader", vgId);
2,978!
625
  }
626

627
  streamMetaWUnLock(pMeta);
2,978✔
628
#endif
629
}
630

631
static void vnodeBecomeFollower(const SSyncFSM *pFsm) {
5,942✔
632
  SVnode *pVnode = pFsm->data;
5,942✔
633
  vInfo("vgId:%d, become follower", pVnode->config.vgId);
5,942!
634

635
  (void)taosThreadMutexLock(&pVnode->lock);
5,942✔
636
  if (pVnode->blocked) {
5,942!
637
    pVnode->blocked = false;
×
638
    vDebug("vgId:%d, become follower and post block", pVnode->config.vgId);
×
639
    if (tsem_post(&pVnode->syncSem) != 0) {
×
640
      vError("vgId:%d, failed to post sync semaphore", pVnode->config.vgId);
×
641
    }
642
  }
643
  (void)taosThreadMutexUnlock(&pVnode->lock);
5,942✔
644

645
#ifdef USE_TQ
646
  if (pVnode->pTq) {
5,942!
647
    tqUpdateNodeStage(pVnode->pTq, false);
5,942✔
648
    if (tqStopStreamAllTasksAsync(pVnode->pTq->pStreamMeta, &pVnode->msgCb) != 0) {
5,942!
649
      vError("vgId:%d, failed to stop stream tasks", pVnode->config.vgId);
×
650
    }
651
  }
652
#endif
653
}
5,941✔
654

655
static void vnodeBecomeLearner(const SSyncFSM *pFsm) {
208✔
656
  SVnode *pVnode = pFsm->data;
208✔
657
  vInfo("vgId:%d, become learner", pVnode->config.vgId);
208!
658

659
  (void)taosThreadMutexLock(&pVnode->lock);
208✔
660
  if (pVnode->blocked) {
208!
661
    pVnode->blocked = false;
×
662
    vDebug("vgId:%d, become learner and post block", pVnode->config.vgId);
×
663
    if (tsem_post(&pVnode->syncSem) != 0) {
×
664
      vError("vgId:%d, failed to post sync semaphore", pVnode->config.vgId);
×
665
    }
666
  }
667
  (void)taosThreadMutexUnlock(&pVnode->lock);
208✔
668
}
208✔
669

670
static void vnodeBecomeLeader(const SSyncFSM *pFsm) {
11,431✔
671
  SVnode *pVnode = pFsm->data;
11,431✔
672
  vDebug("vgId:%d, become leader", pVnode->config.vgId);
11,431✔
673
#ifdef USE_TQ
674
  if (pVnode->pTq) {
11,431!
675
    tqUpdateNodeStage(pVnode->pTq, true);
11,431✔
676
  }
677
#endif
678
}
11,431✔
679

680
static void vnodeBecomeAssignedLeader(const SSyncFSM *pFsm) {
×
681
  SVnode *pVnode = pFsm->data;
×
682
  vDebug("vgId:%d, become assigned leader", pVnode->config.vgId);
×
683
#ifdef USE_TQ
684
  if (pVnode->pTq) {
×
685
    tqUpdateNodeStage(pVnode->pTq, true);
×
686
  }
687
#endif
688
}
×
689

690
static bool vnodeApplyQueueEmpty(const SSyncFSM *pFsm) {
×
691
  SVnode *pVnode = pFsm->data;
×
692

693
  if (pVnode != NULL && pVnode->msgCb.qsizeFp != NULL) {
×
694
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
×
695
    return (itemSize == 0);
×
696
  } else {
697
    return true;
×
698
  }
699
}
700

701
static int32_t vnodeApplyQueueItems(const SSyncFSM *pFsm) {
85,372✔
702
  SVnode *pVnode = pFsm->data;
85,372✔
703

704
  if (pVnode != NULL && pVnode->msgCb.qsizeFp != NULL) {
85,372!
705
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
85,386✔
706
    return itemSize;
85,382✔
707
  } else {
708
    return TSDB_CODE_INVALID_PARA;
×
709
  }
710
}
711

712
static SSyncFSM *vnodeSyncMakeFsm(SVnode *pVnode) {
13,903✔
713
  SSyncFSM *pFsm = taosMemoryCalloc(1, sizeof(SSyncFSM));
13,903!
714
  if (pFsm == NULL) {
13,903!
715
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
716
    return NULL;
×
717
  }
718
  pFsm->data = pVnode;
13,903✔
719
  pFsm->FpCommitCb = vnodeSyncCommitMsg;
13,903✔
720
  pFsm->FpAppliedIndexCb = vnodeSyncAppliedIndex;
13,903✔
721
  pFsm->FpPreCommitCb = vnodeSyncPreCommitMsg;
13,903✔
722
  pFsm->FpRollBackCb = vnodeSyncRollBackMsg;
13,903✔
723
  pFsm->FpGetSnapshot = NULL;
13,903✔
724
  pFsm->FpGetSnapshotInfo = vnodeSyncGetSnapshotInfo;
13,903✔
725
  pFsm->FpRestoreFinishCb = vnodeRestoreFinish;
13,903✔
726
  pFsm->FpAfterRestoredCb = NULL;
13,903✔
727
  pFsm->FpLeaderTransferCb = NULL;
13,903✔
728
  pFsm->FpApplyQueueEmptyCb = vnodeApplyQueueEmpty;
13,903✔
729
  pFsm->FpApplyQueueItems = vnodeApplyQueueItems;
13,903✔
730
  pFsm->FpBecomeLeaderCb = vnodeBecomeLeader;
13,903✔
731
  pFsm->FpBecomeAssignedLeaderCb = vnodeBecomeAssignedLeader;
13,903✔
732
  pFsm->FpBecomeFollowerCb = vnodeBecomeFollower;
13,903✔
733
  pFsm->FpBecomeLearnerCb = vnodeBecomeLearner;
13,903✔
734
  pFsm->FpReConfigCb = NULL;
13,903✔
735
  pFsm->FpSnapshotStartRead = vnodeSnapshotStartRead;
13,903✔
736
  pFsm->FpSnapshotStopRead = vnodeSnapshotStopRead;
13,903✔
737
  pFsm->FpSnapshotDoRead = vnodeSnapshotDoRead;
13,903✔
738
  pFsm->FpSnapshotStartWrite = vnodeSnapshotStartWrite;
13,903✔
739
  pFsm->FpSnapshotStopWrite = vnodeSnapshotStopWrite;
13,903✔
740
  pFsm->FpSnapshotDoWrite = vnodeSnapshotDoWrite;
13,903✔
741

742
  return pFsm;
13,903✔
743
}
744

745
int32_t vnodeSyncOpen(SVnode *pVnode, char *path, int32_t vnodeVersion) {
13,902✔
746
  SSyncInfo syncInfo = {
13,902✔
747
      .snapshotStrategy = SYNC_STRATEGY_WAL_FIRST,
748
      .batchSize = 1,
749
      .vgId = pVnode->config.vgId,
13,902✔
750
      .syncCfg = pVnode->config.syncCfg,
751
      .pWal = pVnode->pWal,
13,902✔
752
      .msgcb = &pVnode->msgCb,
13,902✔
753
      .syncSendMSg = vnodeSyncSendMsg,
754
      .syncEqMsg = vnodeSyncEqMsg,
755
      .syncEqCtrlMsg = vnodeSyncEqCtrlMsg,
756
      .pingMs = 5000,
757
      .electMs = 4000,
758
      .heartbeatMs = 700,
759
  };
760

761
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s%ssync", path, TD_DIRSEP);
13,902✔
762
  syncInfo.pFsm = vnodeSyncMakeFsm(pVnode);
13,902✔
763

764
  SSyncCfg *pCfg = &syncInfo.syncCfg;
13,903✔
765
  vInfo("vgId:%d, start to open sync, replica:%d selfIndex:%d", pVnode->config.vgId, pCfg->replicaNum, pCfg->myIndex);
13,903✔
766
  for (int32_t i = 0; i < pCfg->totalReplicaNum; ++i) {
35,750✔
767
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
21,847✔
768
    vInfo("vgId:%d, index:%d ep:%s:%u dnode:%d cluster:%" PRId64, pVnode->config.vgId, i, pNode->nodeFqdn,
21,847✔
769
          pNode->nodePort, pNode->nodeId, pNode->clusterId);
770
  }
771

772
  pVnode->sync = syncOpen(&syncInfo, vnodeVersion);
13,903✔
773
  if (pVnode->sync <= 0) {
13,903!
774
    vError("vgId:%d, failed to open sync since %s", pVnode->config.vgId, terrstr());
×
775
    return terrno;
×
776
  }
777

778
  return 0;
13,903✔
779
}
780

781
int32_t vnodeSyncStart(SVnode *pVnode) {
13,903✔
782
  vInfo("vgId:%d, start sync", pVnode->config.vgId);
13,903✔
783
  int32_t code = syncStart(pVnode->sync);
13,903✔
784
  if (code) {
13,903!
785
    vError("vgId:%d, failed to start sync subsystem since %s", pVnode->config.vgId, tstrerror(code));
×
786
    return code;
×
787
  }
788
  return 0;
13,903✔
789
}
790

791
void vnodeSyncPreClose(SVnode *pVnode) {
13,900✔
792
  vInfo("vgId:%d, sync pre close", pVnode->config.vgId);
13,900✔
793
  int32_t code = syncLeaderTransfer(pVnode->sync);
13,903✔
794
  if (code) {
13,903✔
795
    vError("vgId:%d, failed to transfer leader since %s", pVnode->config.vgId, tstrerror(code));
1,111!
796
  }
797
  syncPreStop(pVnode->sync);
13,903✔
798

799
  (void)taosThreadMutexLock(&pVnode->lock);
13,903✔
800
  if (pVnode->blocked) {
13,902✔
801
    vInfo("vgId:%d, post block after close sync", pVnode->config.vgId);
20!
802
    pVnode->blocked = false;
20✔
803
    if (tsem_post(&pVnode->syncSem) != 0) {
20!
804
      vError("vgId:%d, failed to post block", pVnode->config.vgId);
×
805
    }
806
  }
807
  (void)taosThreadMutexUnlock(&pVnode->lock);
13,902✔
808
}
13,901✔
809

810
void vnodeSyncPostClose(SVnode *pVnode) {
13,903✔
811
  vInfo("vgId:%d, sync post close", pVnode->config.vgId);
13,903✔
812
  syncPostStop(pVnode->sync);
13,903✔
813
}
13,903✔
814

815
void vnodeSyncClose(SVnode *pVnode) {
13,902✔
816
  vInfo("vgId:%d, close sync", pVnode->config.vgId);
13,902✔
817
  syncStop(pVnode->sync);
13,903✔
818
}
13,899✔
819

820
void vnodeSyncCheckTimeout(SVnode *pVnode) {
19,971✔
821
  vTrace("vgId:%d, check sync timeout msg", pVnode->config.vgId);
19,971✔
822
  (void)taosThreadMutexLock(&pVnode->lock);
19,971✔
823
  if (pVnode->blocked) {
19,971✔
824
    int32_t curSec = taosGetTimestampSec();
3✔
825
    int32_t delta = curSec - pVnode->blockSec;
3✔
826
    if (delta > VNODE_TIMEOUT_SEC) {
3!
827
      vError("vgId:%d, failed to propose since timeout and post block, start:%d cur:%d delta:%d seq:%" PRId64,
×
828
             pVnode->config.vgId, pVnode->blockSec, curSec, delta, pVnode->blockSeq);
829
      if (syncSendTimeoutRsp(pVnode->sync, pVnode->blockSeq) != 0) {
×
830
#if 0
831
        SRpcMsg rpcMsg = {.code = TSDB_CODE_SYN_TIMEOUT, .info = pVnode->blockInfo};
832
        vError("send timeout response since its applyed, seq:%" PRId64 " handle:%p ahandle:%p", pVnode->blockSeq,
833
              rpcMsg.info.handle, rpcMsg.info.ahandle);
834
        rpcSendResponse(&rpcMsg);
835
#endif
836
      }
837
      pVnode->blocked = false;
×
838
      pVnode->blockSec = 0;
×
839
      pVnode->blockSeq = 0;
×
840
      if (tsem_post(&pVnode->syncSem) != 0) {
×
841
        vError("vgId:%d, failed to post block", pVnode->config.vgId);
×
842
      }
843
    }
844
  }
845
  (void)taosThreadMutexUnlock(&pVnode->lock);
19,971✔
846
}
19,971✔
847

848
bool vnodeIsRoleLeader(SVnode *pVnode) {
121,890✔
849
  SSyncState state = syncGetState(pVnode->sync);
121,890✔
850
  return state.state == TAOS_SYNC_STATE_LEADER;
121,931✔
851
}
852

853
bool vnodeIsLeader(SVnode *pVnode) {
20,247✔
854
  terrno = 0;
20,247✔
855
  SSyncState state = syncGetState(pVnode->sync);
20,247✔
856

857
  if (terrno != 0) {
20,283✔
858
    vInfo("vgId:%d, vnode is stopping", pVnode->config.vgId);
2,642!
859
    return false;
2,642✔
860
  }
861

862
  if (state.state != TAOS_SYNC_STATE_LEADER) {
17,620!
863
    terrno = TSDB_CODE_SYN_NOT_LEADER;
×
864
    vInfo("vgId:%d, vnode not leader, state:%s", pVnode->config.vgId, syncStr(state.state));
×
865
    return false;
×
866
  }
867

868
  if (!state.restored || !pVnode->restored) {
17,620!
869
    terrno = TSDB_CODE_SYN_RESTORING;
4✔
870
    vInfo("vgId:%d, vnode not restored:%d:%d", pVnode->config.vgId, state.restored, pVnode->restored);
×
871
    return false;
×
872
  }
873

874
  return true;
17,616✔
875
}
876

877
int64_t vnodeClusterId(SVnode *pVnode) {
×
878
  SSyncCfg *syncCfg = &pVnode->config.syncCfg;
×
879
  return syncCfg->nodeInfo[syncCfg->myIndex].clusterId;
×
880
}
881

882
int32_t vnodeNodeId(SVnode *pVnode) {
829,771✔
883
  SSyncCfg *syncCfg = &pVnode->config.syncCfg;
829,771✔
884
  return syncCfg->nodeInfo[syncCfg->myIndex].nodeId;
829,771✔
885
}
886

887
int32_t vnodeGetSnapshot(SVnode *pVnode, SSnapshot *pSnap) {
464,178✔
888
  int code = 0;
464,178✔
889
  pSnap->lastApplyIndex = pVnode->state.committed;
464,178✔
890
  pSnap->lastApplyTerm = pVnode->state.commitTerm;
464,178✔
891
  pSnap->lastConfigIndex = -1;
464,178✔
892
  pSnap->state = SYNC_FSM_STATE_COMPLETE;
464,178✔
893

894
  if (tsdbSnapGetFsState(pVnode) != TSDB_FS_STATE_NORMAL) {
464,178!
895
    pSnap->state = SYNC_FSM_STATE_INCOMPLETE;
×
896
  }
897

898
  if (pSnap->type == TDMT_SYNC_PREP_SNAPSHOT || pSnap->type == TDMT_SYNC_PREP_SNAPSHOT_REPLY) {
464,199✔
899
    code = tsdbSnapPrepDescription(pVnode, pSnap);
112✔
900
  }
901
  return code;
464,220✔
902
}
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