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

taosdata / TDengine / #4469

08 Jul 2025 09:38AM UTC coverage: 62.22% (-1.2%) from 63.381%
#4469

push

travis-ci

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

merge: from main to 3.0 branch

153678 of 316510 branches covered (48.55%)

Branch coverage included in aggregate %.

56 of 60 new or added lines in 13 files covered. (93.33%)

5035 existing lines in 221 files now uncovered.

238955 of 314529 relevant lines covered (75.97%)

6273248.0 hits per line

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

64.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; }
2,501,235✔
26

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

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

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

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

64
  if (code == 0) code = TSDB_CODE_SYN_NOT_LEADER;
41,105!
65

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

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

79
  tmsgSendRsp(&rsp);
41,104✔
80
}
41,105✔
81

82
static void inline vnodeHandleWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,262,108✔
83
  SRpcMsg rsp = {.code = pMsg->code, .info = pMsg->info};
2,262,108✔
84
  if (vnodeProcessWriteMsg(pVnode, pMsg, pMsg->info.conn.applyIndex, &rsp) < 0) {
2,262,108!
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,262,005✔
90
    tmsgSendRsp(&rsp);
2,245,816✔
91
  } else {
92
    if (rsp.pCont) {
16,189✔
93
      rpcFreeCont(rsp.pCont);
9,804✔
94
    }
95
  }
96
}
2,262,095✔
97

98
static void vnodeHandleProposeError(SVnode *pVnode, SRpcMsg *pMsg, int32_t code) {
55,916✔
99
  if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING) {
55,916✔
100
    vnodeRedirectRpcMsg(pVnode, pMsg, code);
35,048✔
101
  } else if (code == TSDB_CODE_MSG_PREPROCESSED) {
20,868✔
102
    SRpcMsg rsp = {.code = TSDB_CODE_SUCCESS, .info = pMsg->info};
19,966✔
103
    if (rsp.info.handle != NULL) {
19,966!
104
      tmsgSendRsp(&rsp);
19,966✔
105
    }
106
  } else {
107
    vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to propose since %s, code:0x%x", pVnode->config.vgId, pMsg,
902!
108
            tstrerror(code), code);
109
    SRpcMsg rsp = {.code = code, .info = pMsg->info};
908✔
110
    if (rsp.info.handle != NULL) {
908✔
111
      tmsgSendRsp(&rsp);
248✔
112
    }
113
  }
114
}
55,955✔
115

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

119
  (void)taosThreadMutexLock(&pVnode->lock);
2,456,101✔
120
  int32_t code = syncPropose(pVnode->sync, pMsg, isWeak, &seq);
2,456,405✔
121
  bool    wait = (code == 0 && vnodeIsMsgBlock(pMsg->msgType));
2,455,905✔
122
  if (wait) {
2,455,904✔
123
    if (pVnode->blocked) {
3,113!
124
      (void)taosThreadMutexUnlock(&pVnode->lock);
×
125
      return TSDB_CODE_INTERNAL_ERROR;
×
126
    }
127
    pVnode->blocked = true;
3,113✔
128
    pVnode->blockSec = taosGetTimestampSec();
3,113✔
129
    pVnode->blockSeq = seq;
3,113✔
130
  }
131
  (void)taosThreadMutexUnlock(&pVnode->lock);
2,455,904✔
132

133
  if (code > 0) {
2,456,412✔
134
    vnodeHandleWriteMsg(pVnode, pMsg);
2,262,332✔
135
  } else if (code < 0) {
194,080✔
136
    if (terrno != 0) code = terrno;
10,601!
137
    vnodeHandleProposeError(pVnode, pMsg, code);
10,601✔
138
  }
139

140
  if (wait) vnodeWaitBlockMsg(pVnode, pMsg);
2,456,071✔
141
  return code;
2,456,237✔
142
}
143

144
void vnodeProposeCommitOnNeed(SVnode *pVnode, bool atExit) {
2,487,716✔
145
  if (!vnodeShouldCommit(pVnode, atExit)) {
2,487,716✔
146
    return;
2,479,691✔
147
  }
148

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

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

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

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

289
  for (int32_t msg = 0; msg < numOfMsgs; msg++) {
4,994,299✔
290
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
2,501,567!
291
    bool isWeak = vnodeIsMsgWeak(pMsg->msgType);
2,501,553✔
292

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

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

305
    bool atExit = false;
2,476,095✔
306
    vnodeProposeCommitOnNeed(pVnode, atExit);
2,476,095✔
307

308
    code = vnodePreProcessWriteMsg(pVnode, pMsg);
2,476,392✔
309
    if (code != 0) {
2,476,021✔
310
      if (code != TSDB_CODE_MSG_PREPROCESSED) {
20,086✔
311
        vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to pre-process since %s", vgId, pMsg, tstrerror(code));
144!
312
      }
313
      vnodeHandleProposeError(pVnode, pMsg, code);
20,086✔
314
      rpcFreeCont(pMsg->pCont);
20,114✔
315
      taosFreeQitem(pMsg);
20,129✔
316
      continue;
20,130✔
317
    }
318

319
    code = vnodeProposeMsg(pVnode, pMsg, isWeak);
2,455,935✔
320

321
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x", vgId, pMsg, code);
2,455,901!
322
    rpcFreeCont(pMsg->pCont);
2,455,901✔
323
    taosFreeQitem(pMsg);
2,456,250✔
324
  }
325
}
2,492,732✔
326

327
#endif
328

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

335
  for (int32_t i = 0; i < numOfMsgs; ++i) {
1,924,513✔
336
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
1,056,374!
337

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

348
    SRpcMsg rsp = {.code = pMsg->code, .info = pMsg->info};
1,056,418✔
349
    if (rsp.code == 0) {
1,056,418!
350
      int32_t ret = 0;
1,056,421✔
351
      int32_t count = 0;
1,056,421✔
352
      while (1) {
353
        ret = vnodeProcessWriteMsg(pVnode, pMsg, pMsg->info.conn.applyIndex, &rsp);
1,056,421✔
354
        if (ret < 0) {
1,056,414!
355
          rsp.code = ret;
×
356
          vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to apply since %s, index:%" PRId64, vgId, pMsg,
×
357
                  tstrerror(ret), pMsg->info.conn.applyIndex);
358
        }
359
        if (ret == TSDB_CODE_VND_WRITE_DISABLED) {
1,056,413!
360
          if (count % 100 == 0)
×
361
            vGError(&pMsg->info.traceId,
×
362
                    "vgId:%d, msg:%p, failed to apply since %s, retry after 200ms, retry count:%d index:%" PRId64, vgId,
363
                    pMsg, tstrerror(ret), count, pMsg->info.conn.applyIndex);
364
          count++;
×
365
          taosMsleep(200);  // wait for a while before retrying
×
366
        } else{
367
          break;
1,056,413✔
368
        } 
369
      }
370
    }
371

372
    vnodePostBlockMsg(pVnode, pMsg);
1,056,410✔
373
    if (rsp.info.handle != NULL) {
1,056,413✔
374
      tmsgSendRsp(&rsp);
183,401✔
375
    } else {
376
      if (rsp.pCont) {
873,012✔
377
        rpcFreeCont(rsp.pCont);
806,100✔
378
      }
379
    }
380

381
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x index:%" PRId64, vgId, pMsg, rsp.code,
1,056,422!
382
            pMsg->info.conn.applyIndex);
383
    rpcFreeCont(pMsg->pCont);
1,056,422✔
384
    taosFreeQitem(pMsg);
1,056,414✔
385
  }
386
}
868,139✔
387

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

391
  int32_t code = syncProcessMsg(pVnode->sync, pMsg);
1,248,368✔
392
  if (code != 0) {
1,248,410✔
393
    vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to process since %s, type:%s", pVnode->config.vgId, pMsg, tstrerror(code),
50!
394
            TMSG_INFO(pMsg->msgType));
395
  }
396

397
  return code;
1,248,402✔
398
}
399

400
static int32_t vnodeSyncEqCtrlMsg(const SMsgCb *msgcb, SRpcMsg *pMsg) {
×
401
  if (pMsg == NULL || pMsg->pCont == NULL) {
×
402
    return TSDB_CODE_INVALID_PARA;
×
403
  }
404

405
  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
×
406
    rpcFreeCont(pMsg->pCont);
×
407
    pMsg->pCont = NULL;
×
408
    return TSDB_CODE_INVALID_PARA;
×
409
  }
410

411
  int32_t code = tmsgPutToQueue(msgcb, SYNC_RD_QUEUE, pMsg);
×
412
  if (code != 0) {
×
413
    rpcFreeCont(pMsg->pCont);
×
414
    pMsg->pCont = NULL;
×
415
  }
416
  return code;
×
417
}
418

419
static int32_t vnodeSyncEqMsg(const SMsgCb *msgcb, SRpcMsg *pMsg) {
282,704✔
420
  if (pMsg == NULL || pMsg->pCont == NULL) {
282,704!
421
    return TSDB_CODE_INVALID_PARA;
×
422
  }
423

424
  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
282,706!
425
    rpcFreeCont(pMsg->pCont);
1✔
426
    pMsg->pCont = NULL;
×
427
    return TSDB_CODE_INVALID_PARA;
×
428
  }
429

430
  int32_t code = tmsgPutToQueue(msgcb, SYNC_QUEUE, pMsg);
282,705✔
431
  if (code != 0) {
282,705✔
432
    rpcFreeCont(pMsg->pCont);
1,959✔
433
    pMsg->pCont = NULL;
1,959✔
434
  }
435
  return code;
282,705✔
436
}
437

438
static int32_t vnodeSyncSendMsg(const SEpSet *pEpSet, SRpcMsg *pMsg) {
995,464✔
439
  int32_t code = tmsgSendSyncReq(pEpSet, pMsg);
995,464✔
440
  if (code != 0) {
995,505!
441
    rpcFreeCont(pMsg->pCont);
×
442
    pMsg->pCont = NULL;
×
443
  }
444
  return code;
995,506✔
445
}
446

447
static int32_t vnodeSyncGetSnapshotInfo(const SSyncFSM *pFsm, SSnapshot *pSnapshot) {
498,274✔
448
  return vnodeGetSnapshot(pFsm->data, pSnapshot);
498,274✔
449
}
450

451
static int32_t vnodeSyncApplyMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, const SFsmCbMeta *pMeta) {
1,056,420✔
452
  SVnode *pVnode = pFsm->data;
1,056,420✔
453
  pMsg->info.conn.applyIndex = pMeta->index;
1,056,420✔
454
  pMsg->info.conn.applyTerm = pMeta->term;
1,056,420✔
455

456
  vGDebug(&pMsg->info.traceId,
1,056,420!
457
          "vgId:%d, index:%" PRId64 ", execute commit cb, fsm:%p, term:%" PRIu64 ", msg-index:%" PRId64
458
          ", weak:%d, code:%d, state:%d %s, type:%s code:0x%x",
459
          pVnode->config.vgId, pMeta->index, pFsm, pMeta->term, pMsg->info.conn.applyIndex, pMeta->isWeak, pMeta->code,
460
          pMeta->state, syncStr(pMeta->state), TMSG_INFO(pMsg->msgType), pMsg->code);
461

462
  int32_t code = tmsgPutToQueue(&pVnode->msgCb, APPLY_QUEUE, pMsg);
1,056,425✔
463
  if (code < 0) {
1,056,423✔
464
    if (code == TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE) {
9!
465
      pVnode->applyQueueErrorCount++;
×
466
      if (pVnode->applyQueueErrorCount == APPLY_QUEUE_ERROR_THRESHOLD) {
×
467
        pVnode->applyQueueErrorCount = 0;
×
468
        vWarn("vgId:%d, failed to put into apply_queue since %s", pVnode->config.vgId, tstrerror(code));
×
469
      }
470
    } else {
471
      vError("vgId:%d, failed to put into apply_queue since %s", pVnode->config.vgId, tstrerror(code));
9!
472
    }
473
  }
474
  return code;
1,056,423✔
475
}
476

477
static int32_t vnodeSyncCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
1,056,419✔
478
  if (pMsg->code == 0) {
1,056,419!
479
    return vnodeSyncApplyMsg(pFsm, pMsg, pMeta);
1,056,421✔
480
  }
481

482
  SVnode *pVnode = pFsm->data;
×
483
  vnodePostBlockMsg(pVnode, pMsg);
×
484

485
  SRpcMsg rsp = {
×
486
      .code = pMsg->code,
×
487
      .info = pMsg->info,
488
  };
489

490
  if (rsp.info.handle != NULL) {
×
491
    tmsgSendRsp(&rsp);
×
492
  }
493

494
  vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x index:%" PRId64, TD_VID(pVnode), pMsg, rsp.code,
×
495
          pMeta->index);
496
  rpcFreeCont(pMsg->pCont);
×
497
  pMsg->pCont = NULL;
×
498
  return 0;
×
499
}
500

501
static int32_t vnodeSyncPreCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
×
502
  if (pMeta->isWeak == 1) {
×
503
    return vnodeSyncApplyMsg(pFsm, pMsg, pMeta);
×
504
  }
505
  return 0;
×
506
}
507

508
static SyncIndex vnodeSyncAppliedIndex(const SSyncFSM *pFSM) {
2,583,966✔
509
  SVnode *pVnode = pFSM->data;
2,583,966✔
510
  return atomic_load_64(&pVnode->state.applied);
2,583,966✔
511
}
512

513
static void vnodeSyncRollBackMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
×
514
  SVnode *pVnode = pFsm->data;
×
515
  vGDebug(&pMsg->info.traceId,
×
516
          "vgId:%d, rollback-cb is excuted, fsm:%p, index:%" PRId64 ", weak:%d, code:%d, state:%d %s, type:%s",
517
          pVnode->config.vgId, pFsm, pMeta->index, pMeta->isWeak, pMeta->code, pMeta->state, syncStr(pMeta->state),
518
          TMSG_INFO(pMsg->msgType));
519
}
×
520

521
static int32_t vnodeSnapshotStartRead(const SSyncFSM *pFsm, void *pParam, void **ppReader) {
55✔
522
  SVnode *pVnode = pFsm->data;
55✔
523
  return vnodeSnapReaderOpen(pVnode, (SSnapshotParam *)pParam, (SVSnapReader **)ppReader);
55✔
524
}
525

526
static void vnodeSnapshotStopRead(const SSyncFSM *pFsm, void *pReader) {
55✔
527
  SVnode *pVnode = pFsm->data;
55✔
528
  vnodeSnapReaderClose(pReader);
55✔
529
}
55✔
530

531
static int32_t vnodeSnapshotDoRead(const SSyncFSM *pFsm, void *pReader, void **ppBuf, int32_t *len) {
913✔
532
  SVnode *pVnode = pFsm->data;
913✔
533
  return vnodeSnapRead(pReader, (uint8_t **)ppBuf, len);
913✔
534
}
535

536
static int32_t vnodeSnapshotStartWrite(const SSyncFSM *pFsm, void *pParam, void **ppWriter) {
53✔
537
  SVnode *pVnode = pFsm->data;
53✔
538

539
  do {
×
540
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
53✔
541
    if (itemSize == 0) {
53!
542
      vInfo("vgId:%d, start write vnode snapshot since apply queue is empty", pVnode->config.vgId);
53!
543
      break;
53✔
544
    } else {
545
      vInfo("vgId:%d, write vnode snapshot later since %d items in apply queue", pVnode->config.vgId, itemSize);
×
546
      taosMsleep(10);
×
547
    }
548
  } while (true);
549

550
  return vnodeSnapWriterOpen(pVnode, (SSnapshotParam *)pParam, (SVSnapWriter **)ppWriter);
53✔
551
}
552

553
static int32_t vnodeSnapshotStopWrite(const SSyncFSM *pFsm, void *pWriter, bool isApply, SSnapshot *pSnapshot) {
53✔
554
  SVnode *pVnode = pFsm->data;
53✔
555
  vInfo("vgId:%d, stop write vnode snapshot, apply:%d, index:%" PRId64 " term:%" PRIu64 " config:%" PRId64,
53!
556
        pVnode->config.vgId, isApply, pSnapshot->lastApplyIndex, pSnapshot->lastApplyTerm, pSnapshot->lastConfigIndex);
557

558
  int32_t code = vnodeSnapWriterClose(pWriter, !isApply, pSnapshot);
53✔
559
  if (code != 0) {
53!
560
    vError("vgId:%d, failed to finish applying vnode snapshot since %s, code:0x%x", pVnode->config.vgId, terrstr(),
×
561
           code);
562
  }
563
  return code;
53✔
564
}
565

566
static int32_t vnodeSnapshotDoWrite(const SSyncFSM *pFsm, void *pWriter, void *pBuf, int32_t len) {
802✔
567
  SVnode *pVnode = pFsm->data;
802✔
568
  vDebug("vgId:%d, continue write vnode snapshot, blockLen:%d", pVnode->config.vgId, len);
802!
569
  int32_t code = vnodeSnapWrite(pWriter, pBuf, len);
802✔
570
  vDebug("vgId:%d, continue write vnode snapshot finished, blockLen:%d", pVnode->config.vgId, len);
802!
571
  return code;
802✔
572
}
573

574
static void vnodeRestoreFinish(const SSyncFSM *pFsm, const SyncIndex commitIdx) {
15,207✔
575
  SVnode   *pVnode = pFsm->data;
15,207✔
576
  int32_t   vgId = pVnode->config.vgId;
15,207✔
577
  SyncIndex appliedIdx = -1;
15,207✔
578

579
  do {
580
    appliedIdx = vnodeSyncAppliedIndex(pFsm);
35,674✔
581
    if (appliedIdx > commitIdx) {
35,739!
UNCOV
582
      vError("vgId:%d, restore failed since applied-index:%" PRId64 " is larger than commit-index:%" PRId64, vgId,
×
583
             appliedIdx, commitIdx);
584
      break;
×
585
    }
586
    if (appliedIdx == commitIdx) {
35,740✔
587
      vInfo("vgId:%d, no items to be applied, restore finish", pVnode->config.vgId);
15,208✔
588
      break;
15,209✔
589
    } else {
590
      if (appliedIdx % 10 == 0) {
20,532✔
591
        vInfo("vgId:%d, restore not finish since %" PRId64 " items to be applied. commit-index:%" PRId64
1,025!
592
              ", applied-index:%" PRId64,
593
              vgId, commitIdx - appliedIdx, commitIdx, appliedIdx);
594
      } else {
595
        vDebug("vgId:%d, restore not finish since %" PRId64 " items to be applied. commit-index:%" PRId64
19,507✔
596
               ", applied-index:%" PRId64,
597
               vgId, commitIdx - appliedIdx, commitIdx, appliedIdx);
598
      }
599
      taosMsleep(10);
20,540✔
600
    }
601
  } while (true);
602

603
  walApplyVer(pVnode->pWal, commitIdx);
15,209✔
604
  pVnode->restored = true;
15,209✔
605

606
#ifdef USE_STREAM
607
  SStreamMeta *pMeta = pVnode->pTq->pStreamMeta;
15,209✔
608
  streamMetaWLock(pMeta);
15,209✔
609

610
  if (pMeta->startInfo.tasksWillRestart) {
15,209✔
611
    vInfo("vgId:%d, sync restore finished, stream tasks will be launched by other thread", vgId);
1,362!
612
    streamMetaWUnLock(pMeta);
1,362✔
613
    return;
1,362✔
614
  }
615

616
  if (vnodeIsRoleLeader(pVnode)) {
13,847✔
617
    // start to restore all stream tasks
618
    if (tsDisableStream) {
10,415!
619
      vInfo("vgId:%d, sync restore finished, not launch stream tasks, since stream tasks are disabled", vgId);
×
620
    } else {
621
      vInfo("vgId:%d sync restore finished, start to launch stream task(s)", vgId);
10,415✔
622
      if (pMeta->startInfo.startAllTasks == 1) {
10,415!
623
        pMeta->startInfo.restartCount += 1;
×
624
        vDebug("vgId:%d in start tasks procedure, inc restartCounter by 1, remaining restart:%d", vgId,
×
625
               pMeta->startInfo.restartCount);
626
      } else {
627
        pMeta->startInfo.startAllTasks = 1;
10,415✔
628
        streamMetaWUnLock(pMeta);
10,415✔
629

630
        tqInfo("vgId:%d stream task already loaded, start them", vgId);
10,415!
631
        int32_t code = streamTaskSchedTask(&pVnode->msgCb, TD_VID(pVnode), 0, 0, STREAM_EXEC_T_START_ALL_TASKS, false);
10,415✔
632
        if (code != 0) {
10,415!
633
          tqError("vgId:%d failed to sched stream task, code:%s", vgId, tstrerror(code));
×
634
        }
635
        return;
10,415✔
636
      }
637
    }
638
  } else {
639
    vInfo("vgId:%d, sync restore finished, not launch stream tasks since not leader", vgId);
3,432!
640
  }
641

642
  streamMetaWUnLock(pMeta);
3,432✔
643
#endif
644
}
645

646
static void vnodeBecomeFollower(const SSyncFSM *pFsm) {
6,828✔
647
  SVnode *pVnode = pFsm->data;
6,828✔
648
  vInfo("vgId:%d, become follower", pVnode->config.vgId);
6,828!
649

650
  (void)taosThreadMutexLock(&pVnode->lock);
6,828✔
651
  if (pVnode->blocked) {
6,828!
652
    pVnode->blocked = false;
×
653
    vDebug("vgId:%d, become follower and post block", pVnode->config.vgId);
×
654
    if (tsem_post(&pVnode->syncSem) != 0) {
×
655
      vError("vgId:%d, failed to post sync semaphore", pVnode->config.vgId);
×
656
    }
657
  }
658
  (void)taosThreadMutexUnlock(&pVnode->lock);
6,828✔
659

660
#ifdef USE_TQ
661
  if (pVnode->pTq) {
6,828!
662
    tqUpdateNodeStage(pVnode->pTq, false);
6,828✔
663
    if (tqStopStreamAllTasksAsync(pVnode->pTq->pStreamMeta, &pVnode->msgCb) != 0) {
6,828!
664
      vError("vgId:%d, failed to stop stream tasks", pVnode->config.vgId);
×
665
    }
666
  }
667
#endif
668
}
6,827✔
669

670
static void vnodeBecomeLearner(const SSyncFSM *pFsm) {
249✔
671
  SVnode *pVnode = pFsm->data;
249✔
672
  vInfo("vgId:%d, become learner", pVnode->config.vgId);
249!
673

674
  (void)taosThreadMutexLock(&pVnode->lock);
249✔
675
  if (pVnode->blocked) {
249!
676
    pVnode->blocked = false;
×
677
    vDebug("vgId:%d, become learner and post block", pVnode->config.vgId);
×
678
    if (tsem_post(&pVnode->syncSem) != 0) {
×
679
      vError("vgId:%d, failed to post sync semaphore", pVnode->config.vgId);
×
680
    }
681
  }
682
  (void)taosThreadMutexUnlock(&pVnode->lock);
249✔
683
}
249✔
684

685
static void vnodeBecomeLeader(const SSyncFSM *pFsm) {
11,779✔
686
  SVnode *pVnode = pFsm->data;
11,779✔
687
  vDebug("vgId:%d, become leader", pVnode->config.vgId);
11,779✔
688
#ifdef USE_TQ
689
  if (pVnode->pTq) {
11,779!
690
    tqUpdateNodeStage(pVnode->pTq, true);
11,779✔
691
  }
692
#endif
693
}
11,779✔
694

695
static void vnodeBecomeAssignedLeader(const SSyncFSM *pFsm) {
×
696
  SVnode *pVnode = pFsm->data;
×
697
  vDebug("vgId:%d, become assigned leader", pVnode->config.vgId);
×
698
#ifdef USE_TQ
699
  if (pVnode->pTq) {
×
700
    tqUpdateNodeStage(pVnode->pTq, true);
×
701
  }
702
#endif
703
}
×
704

705
static bool vnodeApplyQueueEmpty(const SSyncFSM *pFsm) {
×
706
  SVnode *pVnode = pFsm->data;
×
707

708
  if (pVnode != NULL && pVnode->msgCb.qsizeFp != NULL) {
×
709
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
×
710
    return (itemSize == 0);
×
711
  } else {
712
    return true;
×
713
  }
714
}
715

716
static int32_t vnodeApplyQueueItems(const SSyncFSM *pFsm) {
91,043✔
717
  SVnode *pVnode = pFsm->data;
91,043✔
718

719
  if (pVnode != NULL && pVnode->msgCb.qsizeFp != NULL) {
91,043!
720
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
91,048✔
721
    return itemSize;
91,039✔
722
  } else {
723
    return TSDB_CODE_INVALID_PARA;
×
724
  }
725
}
726

727
static SSyncFSM *vnodeSyncMakeFsm(SVnode *pVnode) {
14,519✔
728
  SSyncFSM *pFsm = taosMemoryCalloc(1, sizeof(SSyncFSM));
14,519!
729
  if (pFsm == NULL) {
14,519!
730
    terrno = TSDB_CODE_OUT_OF_MEMORY;
×
731
    return NULL;
×
732
  }
733
  pFsm->data = pVnode;
14,519✔
734
  pFsm->FpCommitCb = vnodeSyncCommitMsg;
14,519✔
735
  pFsm->FpAppliedIndexCb = vnodeSyncAppliedIndex;
14,519✔
736
  pFsm->FpPreCommitCb = vnodeSyncPreCommitMsg;
14,519✔
737
  pFsm->FpRollBackCb = vnodeSyncRollBackMsg;
14,519✔
738
  pFsm->FpGetSnapshot = NULL;
14,519✔
739
  pFsm->FpGetSnapshotInfo = vnodeSyncGetSnapshotInfo;
14,519✔
740
  pFsm->FpRestoreFinishCb = vnodeRestoreFinish;
14,519✔
741
  pFsm->FpAfterRestoredCb = NULL;
14,519✔
742
  pFsm->FpLeaderTransferCb = NULL;
14,519✔
743
  pFsm->FpApplyQueueEmptyCb = vnodeApplyQueueEmpty;
14,519✔
744
  pFsm->FpApplyQueueItems = vnodeApplyQueueItems;
14,519✔
745
  pFsm->FpBecomeLeaderCb = vnodeBecomeLeader;
14,519✔
746
  pFsm->FpBecomeAssignedLeaderCb = vnodeBecomeAssignedLeader;
14,519✔
747
  pFsm->FpBecomeFollowerCb = vnodeBecomeFollower;
14,519✔
748
  pFsm->FpBecomeLearnerCb = vnodeBecomeLearner;
14,519✔
749
  pFsm->FpReConfigCb = NULL;
14,519✔
750
  pFsm->FpSnapshotStartRead = vnodeSnapshotStartRead;
14,519✔
751
  pFsm->FpSnapshotStopRead = vnodeSnapshotStopRead;
14,519✔
752
  pFsm->FpSnapshotDoRead = vnodeSnapshotDoRead;
14,519✔
753
  pFsm->FpSnapshotStartWrite = vnodeSnapshotStartWrite;
14,519✔
754
  pFsm->FpSnapshotStopWrite = vnodeSnapshotStopWrite;
14,519✔
755
  pFsm->FpSnapshotDoWrite = vnodeSnapshotDoWrite;
14,519✔
756

757
  return pFsm;
14,519✔
758
}
759

760
int32_t vnodeSyncOpen(SVnode *pVnode, char *path, int32_t vnodeVersion) {
14,518✔
761
  SSyncInfo syncInfo = {
14,518✔
762
      .snapshotStrategy = SYNC_STRATEGY_WAL_FIRST,
763
      .batchSize = 1,
764
      .vgId = pVnode->config.vgId,
14,518✔
765
      .syncCfg = pVnode->config.syncCfg,
766
      .pWal = pVnode->pWal,
14,518✔
767
      .msgcb = &pVnode->msgCb,
14,518✔
768
      .syncSendMSg = vnodeSyncSendMsg,
769
      .syncEqMsg = vnodeSyncEqMsg,
770
      .syncEqCtrlMsg = vnodeSyncEqCtrlMsg,
771
      .pingMs = 5000,
772
      .electMs = 4000,
773
      .heartbeatMs = 700,
774
  };
775

776
  snprintf(syncInfo.path, sizeof(syncInfo.path), "%s%ssync", path, TD_DIRSEP);
14,518✔
777
  syncInfo.pFsm = vnodeSyncMakeFsm(pVnode);
14,518✔
778

779
  SSyncCfg *pCfg = &syncInfo.syncCfg;
14,519✔
780
  vInfo("vgId:%d, start to open sync, replica:%d selfIndex:%d", pVnode->config.vgId, pCfg->replicaNum, pCfg->myIndex);
14,519✔
781
  for (int32_t i = 0; i < pCfg->totalReplicaNum; ++i) {
38,055✔
782
    SNodeInfo *pNode = &pCfg->nodeInfo[i];
23,535✔
783
    vInfo("vgId:%d, index:%d ep:%s:%u dnode:%d cluster:%" PRId64, pVnode->config.vgId, i, pNode->nodeFqdn,
23,535✔
784
          pNode->nodePort, pNode->nodeId, pNode->clusterId);
785
  }
786

787
  pVnode->sync = syncOpen(&syncInfo, vnodeVersion);
14,520✔
788
  if (pVnode->sync <= 0) {
14,520!
789
    vError("vgId:%d, failed to open sync since %s", pVnode->config.vgId, terrstr());
×
790
    return terrno;
×
791
  }
792

793
  return 0;
14,520✔
794
}
795

796
int32_t vnodeSyncStart(SVnode *pVnode) {
14,519✔
797
  vInfo("vgId:%d, start sync", pVnode->config.vgId);
14,519✔
798
  int32_t code = syncStart(pVnode->sync);
14,520✔
799
  if (code) {
14,520!
800
    vError("vgId:%d, failed to start sync subsystem since %s", pVnode->config.vgId, tstrerror(code));
×
801
    return code;
×
802
  }
803
  return 0;
14,520✔
804
}
805

806
void vnodeSyncPreClose(SVnode *pVnode) {
14,518✔
807
  vInfo("vgId:%d, sync pre close", pVnode->config.vgId);
14,518✔
808
  int32_t code = syncLeaderTransfer(pVnode->sync);
14,519✔
809
  if (code) {
14,519✔
810
    vError("vgId:%d, failed to transfer leader since %s", pVnode->config.vgId, tstrerror(code));
1,299!
811
  }
812
  syncPreStop(pVnode->sync);
14,519✔
813

814
  (void)taosThreadMutexLock(&pVnode->lock);
14,520✔
815
  if (pVnode->blocked) {
14,520✔
816
    vInfo("vgId:%d, post block after close sync", pVnode->config.vgId);
20!
817
    pVnode->blocked = false;
20✔
818
    if (tsem_post(&pVnode->syncSem) != 0) {
20!
819
      vError("vgId:%d, failed to post block", pVnode->config.vgId);
×
820
    }
821
  }
822
  (void)taosThreadMutexUnlock(&pVnode->lock);
14,520✔
823
}
14,520✔
824

825
void vnodeSyncPostClose(SVnode *pVnode) {
14,520✔
826
  vInfo("vgId:%d, sync post close", pVnode->config.vgId);
14,520✔
827
  syncPostStop(pVnode->sync);
14,520✔
828
}
14,520✔
829

830
void vnodeSyncClose(SVnode *pVnode) {
14,520✔
831
  vInfo("vgId:%d, close sync", pVnode->config.vgId);
14,520✔
832
  syncStop(pVnode->sync);
14,520✔
833
}
14,515✔
834

835
void vnodeSyncCheckTimeout(SVnode *pVnode) {
6,545✔
836
  vTrace("vgId:%d, check sync timeout msg", pVnode->config.vgId);
6,545✔
837
  (void)taosThreadMutexLock(&pVnode->lock);
6,545✔
838
  if (pVnode->blocked) {
6,545✔
839
    int32_t curSec = taosGetTimestampSec();
2✔
840
    int32_t delta = curSec - pVnode->blockSec;
2✔
841
    if (delta > VNODE_TIMEOUT_SEC) {
2!
842
      vError("vgId:%d, failed to propose since timeout and post block, start:%d cur:%d delta:%d seq:%" PRId64,
×
843
             pVnode->config.vgId, pVnode->blockSec, curSec, delta, pVnode->blockSeq);
844
      if (syncSendTimeoutRsp(pVnode->sync, pVnode->blockSeq) != 0) {
×
845
#if 0
846
        SRpcMsg rpcMsg = {.code = TSDB_CODE_SYN_TIMEOUT, .info = pVnode->blockInfo};
847
        vError("send timeout response since its applyed, seq:%" PRId64 " handle:%p ahandle:%p", pVnode->blockSeq,
848
              rpcMsg.info.handle, rpcMsg.info.ahandle);
849
        rpcSendResponse(&rpcMsg);
850
#endif
851
      }
852
      pVnode->blocked = false;
×
853
      pVnode->blockSec = 0;
×
854
      pVnode->blockSeq = 0;
×
855
      if (tsem_post(&pVnode->syncSem) != 0) {
×
856
        vError("vgId:%d, failed to post block", pVnode->config.vgId);
×
857
      }
858
    }
859
  }
860
  (void)taosThreadMutexUnlock(&pVnode->lock);
6,545✔
861
}
6,545✔
862

863
bool vnodeIsRoleLeader(SVnode *pVnode) {
81,880✔
864
  SSyncState state = syncGetState(pVnode->sync);
81,880✔
865
  return state.state == TAOS_SYNC_STATE_LEADER;
81,880✔
866
}
867

868
bool vnodeIsLeader(SVnode *pVnode) {
19,013✔
869
  terrno = 0;
19,013✔
870
  SSyncState state = syncGetState(pVnode->sync);
19,020✔
871

872
  if (terrno != 0) {
19,023✔
873
    vInfo("vgId:%d, vnode is stopping", pVnode->config.vgId);
2,943!
874
    return false;
2,943✔
875
  }
876

877
  if (state.state != TAOS_SYNC_STATE_LEADER) {
16,072!
878
    terrno = TSDB_CODE_SYN_NOT_LEADER;
×
879
    vInfo("vgId:%d, vnode not leader, state:%s", pVnode->config.vgId, syncStr(state.state));
×
880
    return false;
×
881
  }
882

883
  if (!state.restored || !pVnode->restored) {
16,072!
UNCOV
884
    terrno = TSDB_CODE_SYN_RESTORING;
×
885
    vInfo("vgId:%d, vnode not restored:%d:%d", pVnode->config.vgId, state.restored, pVnode->restored);
×
886
    return false;
×
887
  }
888

889
  return true;
16,074✔
890
}
891

892
int64_t vnodeClusterId(SVnode *pVnode) {
×
893
  SSyncCfg *syncCfg = &pVnode->config.syncCfg;
×
894
  return syncCfg->nodeInfo[syncCfg->myIndex].clusterId;
×
895
}
896

897
int32_t vnodeNodeId(SVnode *pVnode) {
765,440✔
898
  SSyncCfg *syncCfg = &pVnode->config.syncCfg;
765,440✔
899
  return syncCfg->nodeInfo[syncCfg->myIndex].nodeId;
765,440✔
900
}
901

902
int32_t vnodeGetSnapshot(SVnode *pVnode, SSnapshot *pSnap) {
498,311✔
903
  int code = 0;
498,311✔
904
  pSnap->lastApplyIndex = pVnode->state.committed;
498,311✔
905
  pSnap->lastApplyTerm = pVnode->state.commitTerm;
498,311✔
906
  pSnap->lastConfigIndex = -1;
498,311✔
907
  pSnap->state = SYNC_FSM_STATE_COMPLETE;
498,311✔
908

909
  if (tsdbSnapGetFsState(pVnode) != TSDB_FS_STATE_NORMAL) {
498,311!
910
    pSnap->state = SYNC_FSM_STATE_INCOMPLETE;
×
911
  }
912

913
  if (pSnap->type == TDMT_SYNC_PREP_SNAPSHOT || pSnap->type == TDMT_SYNC_PREP_SNAPSHOT_REPLY) {
498,343✔
914
    code = tsdbSnapPrepDescription(pVnode, pSnap);
105✔
915
  }
916
  return code;
498,364✔
917
}
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