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

taosdata / TDengine / #3840

04 Apr 2025 03:35PM UTC coverage: 63.027% (+0.6%) from 62.382%
#3840

push

travis-ci

web-flow
Merge pull request #30653 from taosdata/merge/mainto3.0

merge: from main to 3.0 branch

155471 of 315065 branches covered (49.35%)

Branch coverage included in aggregate %.

241637 of 314991 relevant lines covered (76.71%)

18825079.96 hits per line

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

65.92
/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; }
10,470,083✔
26

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

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

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

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

64
  if (code == 0) code = TSDB_CODE_SYN_NOT_LEADER;
35,263!
65

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

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

79
  tmsgSendRsp(&rsp);
35,263✔
80
}
35,263✔
81

82
static void inline vnodeHandleWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
10,200,394✔
83
  SRpcMsg rsp = {.code = pMsg->code, .info = pMsg->info};
10,200,394✔
84
  if (vnodeProcessWriteMsg(pVnode, pMsg, pMsg->info.conn.applyIndex, &rsp) < 0) {
10,200,394!
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) {
10,200,573✔
90
    tmsgSendRsp(&rsp);
10,163,910✔
91
  } else {
92
    if (rsp.pCont) {
36,663✔
93
      rpcFreeCont(rsp.pCont);
26,461✔
94
    }
95
  }
96
}
10,200,711✔
97

98
static void vnodeHandleProposeError(SVnode *pVnode, SRpcMsg *pMsg, int32_t code) {
125,678✔
99
  if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING) {
125,678✔
100
    vnodeRedirectRpcMsg(pVnode, pMsg, code);
25,851✔
101
  } else if (code == TSDB_CODE_MSG_PREPROCESSED) {
99,827✔
102
    SRpcMsg rsp = {.code = TSDB_CODE_SUCCESS, .info = pMsg->info};
98,772✔
103
    if (rsp.info.handle != NULL) {
98,772!
104
      tmsgSendRsp(&rsp);
98,879✔
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,055!
108
            tstrerror(code), code);
109
    SRpcMsg rsp = {.code = code, .info = pMsg->info};
1,072✔
110
    if (rsp.info.handle != NULL) {
1,072✔
111
      tmsgSendRsp(&rsp);
666✔
112
    }
113
  }
114
}
125,888✔
115

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

119
  (void)taosThreadMutexLock(&pVnode->lock);
10,352,502✔
120
  int32_t code = syncPropose(pVnode->sync, pMsg, isWeak, &seq);
10,352,829✔
121
  bool    wait = (code == 0 && vnodeIsMsgBlock(pMsg->msgType));
10,352,449✔
122
  if (wait) {
10,352,449✔
123
    if (pVnode->blocked) {
2,437!
124
      (void)taosThreadMutexUnlock(&pVnode->lock);
×
125
      return TSDB_CODE_INTERNAL_ERROR;
×
126
    }
127
    pVnode->blocked = true;
2,437✔
128
    pVnode->blockSec = taosGetTimestampSec();
2,437✔
129
    pVnode->blockSeq = seq;
2,436✔
130
  }
131
  (void)taosThreadMutexUnlock(&pVnode->lock);
10,352,448✔
132

133
  if (code > 0) {
10,352,813✔
134
    vnodeHandleWriteMsg(pVnode, pMsg);
10,200,791✔
135
  } else if (code < 0) {
152,022✔
136
    if (terrno != 0) code = terrno;
7,251!
137
    vnodeHandleProposeError(pVnode, pMsg, code);
7,251✔
138
  }
139

140
  if (wait) vnodeWaitBlockMsg(pVnode, pMsg);
10,352,709✔
141
  return code;
10,352,747✔
142
}
143

144
void vnodeProposeCommitOnNeed(SVnode *pVnode, bool atExit) {
10,460,691✔
145
  if (!vnodeShouldCommit(pVnode, atExit)) {
10,460,691✔
146
    return;
10,453,321✔
147
  }
148

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

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

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

163
  if (!atExit) {
8,037✔
164
    if (vnodeProposeMsg(pVnode, &rpcMsg, isWeak) < 0) {
885!
165
      vTrace("vgId:%d, failed to propose vnode commit since %s", pVnode->config.vgId, terrstr());
×
166
    }
167
    rpcFreeCont(rpcMsg.pCont);
885✔
168
    rpcMsg.pCont = NULL;
885✔
169
  } else {
170
    int32_t code = 0;
7,152✔
171
    if ((code = tmsgPutToQueue(&pVnode->msgCb, WRITE_QUEUE, &rpcMsg)) < 0) {
7,152✔
172
      vError("vgId:%d, failed to put vnode commit to write_queue since %s", pVnode->config.vgId, tstrerror(code));
3,244✔
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) {
8,694,440✔
283
  SVnode  *pVnode = pInfo->ahandle;
8,694,440✔
284
  int32_t  vgId = pVnode->config.vgId;
8,694,440✔
285
  int32_t  code = 0;
8,694,440✔
286
  SRpcMsg *pMsg = NULL;
8,694,440✔
287
  vTrace("vgId:%d, get %d msgs from vnode-write queue", vgId, numOfMsgs);
8,694,440✔
288

289
  for (int32_t msg = 0; msg < numOfMsgs; msg++) {
19,165,693✔
290
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
10,470,338!
291
    bool isWeak = vnodeIsMsgWeak(pMsg->msgType);
10,470,536✔
292

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

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

305
    bool atExit = false;
10,451,102✔
306
    vnodeProposeCommitOnNeed(pVnode, atExit);
10,451,102✔
307

308
    code = vnodePreProcessWriteMsg(pVnode, pMsg);
10,451,480✔
309
    if (code != 0) {
10,450,794✔
310
      if (code != TSDB_CODE_MSG_PREPROCESSED) {
99,170✔
311
        vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to pre-process since %s", vgId, pMsg, tstrerror(code));
519!
312
      }
313
      vnodeHandleProposeError(pVnode, pMsg, code);
99,170✔
314
      rpcFreeCont(pMsg->pCont);
99,291✔
315
      taosFreeQitem(pMsg);
99,529✔
316
      continue;
99,550✔
317
    }
318

319
    code = vnodeProposeMsg(pVnode, pMsg, isWeak);
10,351,624✔
320

321
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x", vgId, pMsg, code);
10,351,770!
322
    rpcFreeCont(pMsg->pCont);
10,351,770✔
323
    taosFreeQitem(pMsg);
10,351,926✔
324
  }
325
}
8,695,355✔
326

327
#endif
328

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

335
  for (int32_t i = 0; i < numOfMsgs; ++i) {
2,836,688✔
336
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
1,681,396!
337

338
    if (vnodeIsMsgBlock(pMsg->msgType)) {
1,681,388✔
339
      vGDebug(&pMsg->info.traceId, "vgId:%d, msg:%p, get from vnode-apply queue, type:%s handle:%p index:%" PRId64
21,483!
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,
1,659,901!
345
              TMSG_INFO(pMsg->msgType), pMsg->info.handle, pMsg->info.conn.applyIndex);
346
    }
347

348
    SRpcMsg rsp = {.code = pMsg->code, .info = pMsg->info};
1,681,406✔
349
    if (rsp.code == 0) {
1,681,406!
350
      if (vnodeProcessWriteMsg(pVnode, pMsg, pMsg->info.conn.applyIndex, &rsp) < 0) {
1,681,411!
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);
1,681,395✔
358
    if (rsp.info.handle != NULL) {
1,681,396✔
359
      tmsgSendRsp(&rsp);
144,719✔
360
    } else {
361
      if (rsp.pCont) {
1,536,677✔
362
        rpcFreeCont(rsp.pCont);
1,483,427✔
363
      }
364
    }
365

366
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x index:%" PRId64, vgId, pMsg, rsp.code,
1,681,405!
367
            pMsg->info.conn.applyIndex);
368
    rpcFreeCont(pMsg->pCont);
1,681,405✔
369
    taosFreeQitem(pMsg);
1,681,403✔
370
  }
371
}
1,155,292✔
372

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

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

382
  return code;
2,835,849✔
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) {
383,044✔
405
  if (pMsg == NULL || pMsg->pCont == NULL) {
383,044!
406
    return TSDB_CODE_INVALID_PARA;
×
407
  }
408

409
  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
383,051✔
410
    rpcFreeCont(pMsg->pCont);
3✔
411
    pMsg->pCont = NULL;
×
412
    return TSDB_CODE_INVALID_PARA;
×
413
  }
414

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

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

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

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

441
  vGDebug(&pMsg->info.traceId,
1,681,467!
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);
1,681,469✔
448
  if (code < 0) vError("vgId:%d, failed to put into apply_queue since %s", pVnode->config.vgId, tstrerror(code));
1,681,484!
449
  return code;
1,681,483✔
450
}
451

452
static int32_t vnodeSyncCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
1,681,473✔
453
  if (pMsg->code == 0) {
1,681,473!
454
    return vnodeSyncApplyMsg(pFsm, pMsg, pMeta);
1,681,473✔
455
  }
456

457
  SVnode *pVnode = pFsm->data;
×
458
  vnodePostBlockMsg(pVnode, pMsg);
×
459

460
  SRpcMsg rsp = {
×
461
      .code = pMsg->code,
×
462
      .info = pMsg->info,
463
  };
464

465
  if (rsp.info.handle != NULL) {
×
466
    tmsgSendRsp(&rsp);
×
467
  }
468

469
  vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x index:%" PRId64, TD_VID(pVnode), pMsg, rsp.code,
×
470
          pMeta->index);
471
  rpcFreeCont(pMsg->pCont);
×
472
  pMsg->pCont = NULL;
×
473
  return 0;
×
474
}
475

476
static int32_t vnodeSyncPreCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
×
477
  if (pMeta->isWeak == 1) {
×
478
    return vnodeSyncApplyMsg(pFsm, pMsg, pMeta);
×
479
  }
480
  return 0;
×
481
}
482

483
static SyncIndex vnodeSyncAppliedIndex(const SSyncFSM *pFSM) {
10,466,358✔
484
  SVnode *pVnode = pFSM->data;
10,466,358✔
485
  return atomic_load_64(&pVnode->state.applied);
10,466,358✔
486
}
487

488
static void vnodeSyncRollBackMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
×
489
  SVnode *pVnode = pFsm->data;
×
490
  vGDebug(&pMsg->info.traceId,
×
491
          "vgId:%d, rollback-cb is excuted, fsm:%p, index:%" PRId64 ", weak:%d, code:%d, state:%d %s, type:%s",
492
          pVnode->config.vgId, pFsm, pMeta->index, pMeta->isWeak, pMeta->code, pMeta->state, syncStr(pMeta->state),
493
          TMSG_INFO(pMsg->msgType));
494
}
×
495

496
static int32_t vnodeSnapshotStartRead(const SSyncFSM *pFsm, void *pParam, void **ppReader) {
60✔
497
  SVnode *pVnode = pFsm->data;
60✔
498
  return vnodeSnapReaderOpen(pVnode, (SSnapshotParam *)pParam, (SVSnapReader **)ppReader);
60✔
499
}
500

501
static void vnodeSnapshotStopRead(const SSyncFSM *pFsm, void *pReader) {
60✔
502
  SVnode *pVnode = pFsm->data;
60✔
503
  vnodeSnapReaderClose(pReader);
60✔
504
}
60✔
505

506
static int32_t vnodeSnapshotDoRead(const SSyncFSM *pFsm, void *pReader, void **ppBuf, int32_t *len) {
7,031✔
507
  SVnode *pVnode = pFsm->data;
7,031✔
508
  return vnodeSnapRead(pReader, (uint8_t **)ppBuf, len);
7,031✔
509
}
510

511
static int32_t vnodeSnapshotStartWrite(const SSyncFSM *pFsm, void *pParam, void **ppWriter) {
58✔
512
  SVnode *pVnode = pFsm->data;
58✔
513

514
  do {
×
515
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
58✔
516
    if (itemSize == 0) {
58!
517
      vInfo("vgId:%d, start write vnode snapshot since apply queue is empty", pVnode->config.vgId);
58!
518
      break;
58✔
519
    } else {
520
      vInfo("vgId:%d, write vnode snapshot later since %d items in apply queue", pVnode->config.vgId, itemSize);
×
521
      taosMsleep(10);
×
522
    }
523
  } while (true);
524

525
  return vnodeSnapWriterOpen(pVnode, (SSnapshotParam *)pParam, (SVSnapWriter **)ppWriter);
58✔
526
}
527

528
static int32_t vnodeSnapshotStopWrite(const SSyncFSM *pFsm, void *pWriter, bool isApply, SSnapshot *pSnapshot) {
58✔
529
  SVnode *pVnode = pFsm->data;
58✔
530
  vInfo("vgId:%d, stop write vnode snapshot, apply:%d, index:%" PRId64 " term:%" PRIu64 " config:%" PRId64,
58!
531
        pVnode->config.vgId, isApply, pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm, pSnapshot->lastConfigIndex);
532

533
  int32_t code = vnodeSnapWriterClose(pWriter, !isApply, pSnapshot);
58✔
534
  if (code != 0) {
58!
535
    vError("vgId:%d, failed to finish applying vnode snapshot since %s, code:0x%x", pVnode->config.vgId, terrstr(),
×
536
           code);
537
  }
538
  return code;
58✔
539
}
540

541
static int32_t vnodeSnapshotDoWrite(const SSyncFSM *pFsm, void *pWriter, void *pBuf, int32_t len) {
6,863✔
542
  SVnode *pVnode = pFsm->data;
6,863✔
543
  vDebug("vgId:%d, continue write vnode snapshot, blockLen:%d", pVnode->config.vgId, len);
6,863!
544
  int32_t code = vnodeSnapWrite(pWriter, pBuf, len);
6,863✔
545
  vDebug("vgId:%d, continue write vnode snapshot finished, blockLen:%d", pVnode->config.vgId, len);
6,863!
546
  return code;
6,863✔
547
}
548

549
static void vnodeRestoreFinish(const SSyncFSM *pFsm, const SyncIndex commitIdx) {
12,233✔
550
  SVnode   *pVnode = pFsm->data;
12,233✔
551
  int32_t   vgId = pVnode->config.vgId;
12,233✔
552
  SyncIndex appliedIdx = -1;
12,233✔
553

554
  do {
555
    appliedIdx = vnodeSyncAppliedIndex(pFsm);
31,385✔
556
    if (appliedIdx > commitIdx) {
31,387!
557
      vError("vgId:%d, restore failed since applied-index:%" PRId64 " is larger than commit-index:%" PRId64, vgId,
×
558
             appliedIdx, commitIdx);
559
      break;
×
560
    }
561
    if (appliedIdx == commitIdx) {
31,387✔
562
      vInfo("vgId:%d, no items to be applied, restore finish", pVnode->config.vgId);
12,233✔
563
      break;
12,233✔
564
    } else {
565
      vInfo("vgId:%d, restore not finish since %" PRId64 " items to be applied. commit-index:%" PRId64
19,154✔
566
            ", applied-index:%" PRId64,
567
            vgId, commitIdx - appliedIdx, commitIdx, appliedIdx);
568
      taosMsleep(10);
19,154✔
569
    }
570
  } while (true);
571

572
  walApplyVer(pVnode->pWal, commitIdx);
12,233✔
573
  pVnode->restored = true;
12,233✔
574

575
#ifdef USE_STREAM
576
  SStreamMeta *pMeta = pVnode->pTq->pStreamMeta;
12,233✔
577
  streamMetaWLock(pMeta);
12,233✔
578

579
  if (pMeta->startInfo.tasksWillRestart) {
12,233✔
580
    vInfo("vgId:%d, sync restore finished, stream tasks will be launched by other thread", vgId);
927!
581
    streamMetaWUnLock(pMeta);
927✔
582
    return;
927✔
583
  }
584

585
  if (vnodeIsRoleLeader(pVnode)) {
11,306✔
586
    // start to restore all stream tasks
587
    if (tsDisableStream) {
8,910!
588
      vInfo("vgId:%d, sync restore finished, not launch stream tasks, since stream tasks are disabled", vgId);
×
589
    } else {
590
      vInfo("vgId:%d sync restore finished, start to launch stream task(s)", vgId);
8,910✔
591
      if (pMeta->startInfo.startAllTasks == 1) {
8,910!
592
        pMeta->startInfo.restartCount += 1;
×
593
        vDebug("vgId:%d in start tasks procedure, inc restartCounter by 1, remaining restart:%d", vgId,
×
594
               pMeta->startInfo.restartCount);
595
      } else {
596
        pMeta->startInfo.startAllTasks = 1;
8,910✔
597
        streamMetaWUnLock(pMeta);
8,910✔
598

599
        tqInfo("vgId:%d stream task already loaded, start them", vgId);
8,910!
600
        int32_t code = streamTaskSchedTask(&pVnode->msgCb, TD_VID(pVnode), 0, 0, STREAM_EXEC_T_START_ALL_TASKS, false);
8,910✔
601
        if (code != 0) {
8,910!
602
          tqError("vgId:%d failed to sched stream task, code:%s", vgId, tstrerror(code));
×
603
        }
604
        return;
8,910✔
605
      }
606
    }
607
  } else {
608
    vInfo("vgId:%d, sync restore finished, not launch stream tasks since not leader", vgId);
2,396!
609
  }
610

611
  streamMetaWUnLock(pMeta);
2,396✔
612
#endif
613
}
614

615
static void vnodeBecomeFollower(const SSyncFSM *pFsm) {
4,736✔
616
  SVnode *pVnode = pFsm->data;
4,736✔
617
  vInfo("vgId:%d, become follower", pVnode->config.vgId);
4,736!
618

619
  (void)taosThreadMutexLock(&pVnode->lock);
4,736✔
620
  if (pVnode->blocked) {
4,736!
621
    pVnode->blocked = false;
×
622
    vDebug("vgId:%d, become follower and post block", pVnode->config.vgId);
×
623
    if (tsem_post(&pVnode->syncSem) != 0) {
×
624
      vError("vgId:%d, failed to post sync semaphore", pVnode->config.vgId);
×
625
    }
626
  }
627
  (void)taosThreadMutexUnlock(&pVnode->lock);
4,736✔
628

629
#ifdef USE_TQ
630
  if (pVnode->pTq) {
4,736!
631
    tqUpdateNodeStage(pVnode->pTq, false);
4,736✔
632
    if (tqStopStreamAllTasksAsync(pVnode->pTq->pStreamMeta, &pVnode->msgCb) != 0) {
4,736!
633
      vError("vgId:%d, failed to stop stream tasks", pVnode->config.vgId);
×
634
    }
635
  }
636
#endif
637
}
4,736✔
638

639
static void vnodeBecomeLearner(const SSyncFSM *pFsm) {
164✔
640
  SVnode *pVnode = pFsm->data;
164✔
641
  vInfo("vgId:%d, become learner", pVnode->config.vgId);
164!
642

643
  (void)taosThreadMutexLock(&pVnode->lock);
164✔
644
  if (pVnode->blocked) {
164!
645
    pVnode->blocked = false;
×
646
    vDebug("vgId:%d, become learner and post block", pVnode->config.vgId);
×
647
    if (tsem_post(&pVnode->syncSem) != 0) {
×
648
      vError("vgId:%d, failed to post sync semaphore", pVnode->config.vgId);
×
649
    }
650
  }
651
  (void)taosThreadMutexUnlock(&pVnode->lock);
164✔
652
}
164✔
653

654
static void vnodeBecomeLeader(const SSyncFSM *pFsm) {
9,838✔
655
  SVnode *pVnode = pFsm->data;
9,838✔
656
  vDebug("vgId:%d, become leader", pVnode->config.vgId);
9,838✔
657
#ifdef USE_TQ
658
  if (pVnode->pTq) {
9,838!
659
    tqUpdateNodeStage(pVnode->pTq, true);
9,838✔
660
  }
661
#endif
662
}
9,838✔
663

664
static void vnodeBecomeAssignedLeader(const SSyncFSM *pFsm) {
×
665
  SVnode *pVnode = pFsm->data;
×
666
  vDebug("vgId:%d, become assigned leader", pVnode->config.vgId);
×
667
#ifdef USE_TQ
668
  if (pVnode->pTq) {
×
669
    tqUpdateNodeStage(pVnode->pTq, true);
×
670
  }
671
#endif
672
}
×
673

674
static bool vnodeApplyQueueEmpty(const SSyncFSM *pFsm) {
×
675
  SVnode *pVnode = pFsm->data;
×
676

677
  if (pVnode != NULL && pVnode->msgCb.qsizeFp != NULL) {
×
678
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
×
679
    return (itemSize == 0);
×
680
  } else {
681
    return true;
×
682
  }
683
}
684

685
static int32_t vnodeApplyQueueItems(const SSyncFSM *pFsm) {
79,825✔
686
  SVnode *pVnode = pFsm->data;
79,825✔
687

688
  if (pVnode != NULL && pVnode->msgCb.qsizeFp != NULL) {
79,825!
689
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
79,828✔
690
    return itemSize;
79,822✔
691
  } else {
692
    return TSDB_CODE_INVALID_PARA;
×
693
  }
694
}
695

696
static SSyncFSM *vnodeSyncMakeFsm(SVnode *pVnode) {
11,810✔
697
  SSyncFSM *pFsm = taosMemoryCalloc(1, sizeof(SSyncFSM));
11,810!
698
  if (pFsm == NULL) {
11,810!
699
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
700
    return NULL;
×
701
  }
702
  pFsm->data = pVnode;
11,810✔
703
  pFsm->FpCommitCb = vnodeSyncCommitMsg;
11,810✔
704
  pFsm->FpAppliedIndexCb = vnodeSyncAppliedIndex;
11,810✔
705
  pFsm->FpPreCommitCb = vnodeSyncPreCommitMsg;
11,810✔
706
  pFsm->FpRollBackCb = vnodeSyncRollBackMsg;
11,810✔
707
  pFsm->FpGetSnapshot = NULL;
11,810✔
708
  pFsm->FpGetSnapshotInfo = vnodeSyncGetSnapshotInfo;
11,810✔
709
  pFsm->FpRestoreFinishCb = vnodeRestoreFinish;
11,810✔
710
  pFsm->FpAfterRestoredCb = NULL;
11,810✔
711
  pFsm->FpLeaderTransferCb = NULL;
11,810✔
712
  pFsm->FpApplyQueueEmptyCb = vnodeApplyQueueEmpty;
11,810✔
713
  pFsm->FpApplyQueueItems = vnodeApplyQueueItems;
11,810✔
714
  pFsm->FpBecomeLeaderCb = vnodeBecomeLeader;
11,810✔
715
  pFsm->FpBecomeAssignedLeaderCb = vnodeBecomeAssignedLeader;
11,810✔
716
  pFsm->FpBecomeFollowerCb = vnodeBecomeFollower;
11,810✔
717
  pFsm->FpBecomeLearnerCb = vnodeBecomeLearner;
11,810✔
718
  pFsm->FpReConfigCb = NULL;
11,810✔
719
  pFsm->FpSnapshotStartRead = vnodeSnapshotStartRead;
11,810✔
720
  pFsm->FpSnapshotStopRead = vnodeSnapshotStopRead;
11,810✔
721
  pFsm->FpSnapshotDoRead = vnodeSnapshotDoRead;
11,810✔
722
  pFsm->FpSnapshotStartWrite = vnodeSnapshotStartWrite;
11,810✔
723
  pFsm->FpSnapshotStopWrite = vnodeSnapshotStopWrite;
11,810✔
724
  pFsm->FpSnapshotDoWrite = vnodeSnapshotDoWrite;
11,810✔
725

726
  return pFsm;
11,810✔
727
}
728

729
int32_t vnodeSyncOpen(SVnode *pVnode, char *path, int32_t vnodeVersion) {
11,810✔
730
  SSyncInfo syncInfo = {
11,810✔
731
      .snapshotStrategy = SYNC_STRATEGY_WAL_FIRST,
732
      .batchSize = 1,
733
      .vgId = pVnode->config.vgId,
11,810✔
734
      .syncCfg = pVnode->config.syncCfg,
735
      .pWal = pVnode->pWal,
11,810✔
736
      .msgcb = &pVnode->msgCb,
11,810✔
737
      .syncSendMSg = vnodeSyncSendMsg,
738
      .syncEqMsg = vnodeSyncEqMsg,
739
      .syncEqCtrlMsg = vnodeSyncEqCtrlMsg,
740
      .pingMs = 5000,
741
      .electMs = 4000,
742
      .heartbeatMs = 700,
743
  };
744

745
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s%ssync", path, TD_DIRSEP);
11,810✔
746
  syncInfo.pFsm = vnodeSyncMakeFsm(pVnode);
11,810✔
747

748
  SSyncCfg *pCfg = &syncInfo.syncCfg;
11,810✔
749
  vInfo("vgId:%d, start to open sync, replica:%d selfIndex:%d", pVnode->config.vgId, pCfg->replicaNum, pCfg->myIndex);
11,810✔
750
  for (int32_t i = 0; i < pCfg->totalReplicaNum; ++i) {
29,818✔
751
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
18,008✔
752
    vInfo("vgId:%d, index:%d ep:%s:%u dnode:%d cluster:%" PRId64, pVnode->config.vgId, i, pNode->nodeFqdn,
18,008✔
753
          pNode->nodePort, pNode->nodeId, pNode->clusterId);
754
  }
755

756
  pVnode->sync = syncOpen(&syncInfo, vnodeVersion);
11,810✔
757
  if (pVnode->sync <= 0) {
11,810!
758
    vError("vgId:%d, failed to open sync since %s", pVnode->config.vgId, terrstr());
×
759
    return terrno;
×
760
  }
761

762
  return 0;
11,810✔
763
}
764

765
int32_t vnodeSyncStart(SVnode *pVnode) {
11,810✔
766
  vInfo("vgId:%d, start sync", pVnode->config.vgId);
11,810✔
767
  int32_t code = syncStart(pVnode->sync);
11,810✔
768
  if (code) {
11,810!
769
    vError("vgId:%d, failed to start sync subsystem since %s", pVnode->config.vgId, tstrerror(code));
×
770
    return code;
×
771
  }
772
  return 0;
11,810✔
773
}
774

775
void vnodeSyncPreClose(SVnode *pVnode) {
11,810✔
776
  vInfo("vgId:%d, sync pre close", pVnode->config.vgId);
11,810✔
777
  int32_t code = syncLeaderTransfer(pVnode->sync);
11,810✔
778
  if (code) {
11,810✔
779
    vError("vgId:%d, failed to transfer leader since %s", pVnode->config.vgId, tstrerror(code));
899!
780
  }
781
  syncPreStop(pVnode->sync);
11,810✔
782

783
  (void)taosThreadMutexLock(&pVnode->lock);
11,810✔
784
  if (pVnode->blocked) {
11,808✔
785
    vInfo("vgId:%d, post block after close sync", pVnode->config.vgId);
19!
786
    pVnode->blocked = false;
19✔
787
    if (tsem_post(&pVnode->syncSem) != 0) {
19!
788
      vError("vgId:%d, failed to post block", pVnode->config.vgId);
×
789
    }
790
  }
791
  (void)taosThreadMutexUnlock(&pVnode->lock);
11,808✔
792
}
11,810✔
793

794
void vnodeSyncPostClose(SVnode *pVnode) {
11,810✔
795
  vInfo("vgId:%d, sync post close", pVnode->config.vgId);
11,810✔
796
  syncPostStop(pVnode->sync);
11,810✔
797
}
11,810✔
798

799
void vnodeSyncClose(SVnode *pVnode) {
11,810✔
800
  vInfo("vgId:%d, close sync", pVnode->config.vgId);
11,810✔
801
  syncStop(pVnode->sync);
11,810✔
802
}
11,810✔
803

804
void vnodeSyncCheckTimeout(SVnode *pVnode) {
32,371✔
805
  vTrace("vgId:%d, check sync timeout msg", pVnode->config.vgId);
32,371✔
806
  (void)taosThreadMutexLock(&pVnode->lock);
32,371✔
807
  if (pVnode->blocked) {
32,371!
808
    int32_t curSec = taosGetTimestampSec();
×
809
    int32_t delta = curSec - pVnode->blockSec;
×
810
    if (delta > VNODE_TIMEOUT_SEC) {
×
811
      vError("vgId:%d, failed to propose since timeout and post block, start:%d cur:%d delta:%d seq:%" PRId64,
×
812
             pVnode->config.vgId, pVnode->blockSec, curSec, delta, pVnode->blockSeq);
813
      if (syncSendTimeoutRsp(pVnode->sync, pVnode->blockSeq) != 0) {
×
814
#if 0
815
        SRpcMsg rpcMsg = {.code = TSDB_CODE_SYN_TIMEOUT, .info = pVnode->blockInfo};
816
        vError("send timeout response since its applyed, seq:%" PRId64 " handle:%p ahandle:%p", pVnode->blockSeq,
817
              rpcMsg.info.handle, rpcMsg.info.ahandle);
818
        rpcSendResponse(&rpcMsg);
819
#endif
820
      }
821
      pVnode->blocked = false;
×
822
      pVnode->blockSec = 0;
×
823
      pVnode->blockSeq = 0;
×
824
      if (tsem_post(&pVnode->syncSem) != 0) {
×
825
        vError("vgId:%d, failed to post block", pVnode->config.vgId);
×
826
      }
827
    }
828
  }
829
  (void)taosThreadMutexUnlock(&pVnode->lock);
32,371✔
830
}
32,371✔
831

832
bool vnodeIsRoleLeader(SVnode *pVnode) {
152,036✔
833
  SSyncState state = syncGetState(pVnode->sync);
152,036✔
834
  return state.state == TAOS_SYNC_STATE_LEADER;
152,095✔
835
}
836

837
bool vnodeIsLeader(SVnode *pVnode) {
17,806✔
838
  terrno = 0;
17,806✔
839
  SSyncState state = syncGetState(pVnode->sync);
17,834✔
840

841
  if (terrno != 0) {
17,864✔
842
    vInfo("vgId:%d, vnode is stopping", pVnode->config.vgId);
2,131!
843
    return false;
2,131✔
844
  }
845

846
  if (state.state != TAOS_SYNC_STATE_LEADER) {
15,717!
847
    terrno = TSDB_CODE_SYN_NOT_LEADER;
×
848
    vInfo("vgId:%d, vnode not leader, state:%s", pVnode->config.vgId, syncStr(state.state));
×
849
    return false;
×
850
  }
851

852
  if (!state.restored || !pVnode->restored) {
15,717✔
853
    terrno = TSDB_CODE_SYN_RESTORING;
6✔
854
    vInfo("vgId:%d, vnode not restored:%d:%d", pVnode->config.vgId, state.restored, pVnode->restored);
×
855
    return false;
×
856
  }
857

858
  return true;
15,711✔
859
}
860

861
int64_t vnodeClusterId(SVnode *pVnode) {
×
862
  SSyncCfg *syncCfg = &pVnode->config.syncCfg;
×
863
  return syncCfg->nodeInfo[syncCfg->myIndex].clusterId;
×
864
}
865

866
int32_t vnodeNodeId(SVnode *pVnode) {
844,963✔
867
  SSyncCfg *syncCfg = &pVnode->config.syncCfg;
844,963✔
868
  return syncCfg->nodeInfo[syncCfg->myIndex].nodeId;
844,963✔
869
}
870

871
int32_t vnodeGetSnapshot(SVnode *pVnode, SSnapshot *pSnap) {
414,103✔
872
  int code = 0;
414,103✔
873
  pSnap->lastApplyIndex = pVnode->state.committed;
414,103✔
874
  pSnap->lastApplyTerm = pVnode->state.commitTerm;
414,103✔
875
  pSnap->lastConfigIndex = -1;
414,103✔
876
  pSnap->state = SYNC_FSM_STATE_COMPLETE;
414,103✔
877

878
  if (tsdbSnapGetFsState(pVnode) != TSDB_FS_STATE_NORMAL) {
414,103!
879
    pSnap->state = SYNC_FSM_STATE_INCOMPLETE;
×
880
  }
881

882
  if (pSnap->type == TDMT_SYNC_PREP_SNAPSHOT || pSnap->type == TDMT_SYNC_PREP_SNAPSHOT_REPLY) {
414,123✔
883
    code = tsdbSnapPrepDescription(pVnode, pSnap);
116✔
884
  }
885
  return code;
414,126✔
886
}
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