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

taosdata / TDengine / #4452

05 Jul 2025 10:40AM UTC coverage: 60.734% (-2.8%) from 63.58%
#4452

push

travis-ci

web-flow
Merge pull request #31673 from taosdata/fix/huoh/taos_log

test: fix mnode create failure

149869 of 316704 branches covered (47.32%)

Branch coverage included in aggregate %.

233489 of 314508 relevant lines covered (74.24%)

6990477.86 hits per line

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

65.12
/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,905,708✔
26

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

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

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

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

64
  if (code == 0) code = TSDB_CODE_SYN_NOT_LEADER;
44,826!
65

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

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

79
  tmsgSendRsp(&rsp);
44,826✔
80
}
44,826✔
81

82
static void inline vnodeHandleWriteMsg(SVnode *pVnode, SRpcMsg *pMsg) {
2,630,053✔
83
  SRpcMsg rsp = {.code = pMsg->code, .info = pMsg->info};
2,630,053✔
84
  if (vnodeProcessWriteMsg(pVnode, pMsg, pMsg->info.conn.applyIndex, &rsp) < 0) {
2,630,053!
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,629,782✔
90
    tmsgSendRsp(&rsp);
2,612,248✔
91
  } else {
92
    if (rsp.pCont) {
17,534✔
93
      rpcFreeCont(rsp.pCont);
10,715✔
94
    }
95
  }
96
}
2,630,096✔
97

98
static void vnodeHandleProposeError(SVnode *pVnode, SRpcMsg *pMsg, int32_t code) {
64,237✔
99
  if (code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_SYN_RESTORING) {
64,237✔
100
    vnodeRedirectRpcMsg(pVnode, pMsg, code);
38,483✔
101
  } else if (code == TSDB_CODE_MSG_PREPROCESSED) {
25,754✔
102
    SRpcMsg rsp = {.code = TSDB_CODE_SUCCESS, .info = pMsg->info};
24,856✔
103
    if (rsp.info.handle != NULL) {
24,856✔
104
      tmsgSendRsp(&rsp);
24,845✔
105
    }
106
  } else {
107
    vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to propose since %s, code:0x%x", pVnode->config.vgId, pMsg,
898!
108
            tstrerror(code), code);
109
    SRpcMsg rsp = {.code = code, .info = pMsg->info};
891✔
110
    if (rsp.info.handle != NULL) {
891✔
111
      tmsgSendRsp(&rsp);
217✔
112
    }
113
  }
114
}
64,275✔
115

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

119
  (void)taosThreadMutexLock(&pVnode->lock);
2,851,879✔
120
  int32_t code = syncPropose(pVnode->sync, pMsg, isWeak, &seq);
2,852,174✔
121
  bool    wait = (code == 0 && vnodeIsMsgBlock(pMsg->msgType));
2,851,753✔
122
  if (wait) {
2,851,759✔
123
    if (pVnode->blocked) {
3,582!
124
      (void)taosThreadMutexUnlock(&pVnode->lock);
×
125
      return TSDB_CODE_INTERNAL_ERROR;
×
126
    }
127
    pVnode->blocked = true;
3,582✔
128
    pVnode->blockSec = taosGetTimestampSec();
3,582✔
129
    pVnode->blockSeq = seq;
3,581✔
130
  }
131
  (void)taosThreadMutexUnlock(&pVnode->lock);
2,851,758✔
132

133
  if (code > 0) {
2,852,082✔
134
    vnodeHandleWriteMsg(pVnode, pMsg);
2,630,236✔
135
  } else if (code < 0) {
221,846✔
136
    if (terrno != 0) code = terrno;
10,001!
137
    vnodeHandleProposeError(pVnode, pMsg, code);
10,001✔
138
  }
139

140
  if (wait) vnodeWaitBlockMsg(pVnode, pMsg);
2,851,823✔
141
  return code;
2,851,865✔
142
}
143

144
void vnodeProposeCommitOnNeed(SVnode *pVnode, bool atExit) {
2,889,364✔
145
  if (!vnodeShouldCommit(pVnode, atExit)) {
2,889,364✔
146
    return;
2,880,847✔
147
  }
148

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

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

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

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

289
  for (int32_t msg = 0; msg < numOfMsgs; msg++) {
5,801,902✔
290
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
2,906,227!
291
    bool isWeak = vnodeIsMsgWeak(pMsg->msgType);
2,906,198✔
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,905,767!
294
            vgId, pMsg, isWeak, vnodeIsMsgBlock(pMsg->msgType), msg, numOfMsgs, pMsg->info.handle);
295

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

305
    bool atExit = false;
2,876,799✔
306
    vnodeProposeCommitOnNeed(pVnode, atExit);
2,876,799✔
307

308
    code = vnodePreProcessWriteMsg(pVnode, pMsg);
2,877,030✔
309
    if (code != 0) {
2,876,671✔
310
      if (code != TSDB_CODE_MSG_PREPROCESSED) {
24,982✔
311
        vGError(&pMsg->info.traceId, "vgId:%d, msg:%p, failed to pre-process since %s", vgId, pMsg, tstrerror(code));
148!
312
      }
313
      vnodeHandleProposeError(pVnode, pMsg, code);
24,982✔
314
      rpcFreeCont(pMsg->pCont);
25,009✔
315
      taosFreeQitem(pMsg);
25,033✔
316
      continue;
25,034✔
317
    }
318

319
    code = vnodeProposeMsg(pVnode, pMsg, isWeak);
2,851,689✔
320

321
    vGTrace(&pMsg->info.traceId, "vgId:%d, msg:%p, is freed, code:0x%x", vgId, pMsg, code);
2,851,542!
322
    rpcFreeCont(pMsg->pCont);
2,851,541✔
323
    taosFreeQitem(pMsg);
2,851,936✔
324
  }
325
}
2,895,675✔
326

327
#endif
328

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

335
  for (int32_t i = 0; i < numOfMsgs; ++i) {
2,116,208✔
336
    if (taosGetQitem(qall, (void **)&pMsg) == 0) continue;
1,155,387!
337

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

348
    SRpcMsg rsp = {.code = pMsg->code, .info = pMsg->info};
1,155,413✔
349
    if (rsp.code == 0) {
1,155,413!
350
      int32_t ret = 0;
1,155,413✔
351
      int32_t count = 0;
1,155,413✔
352
      while (1) {
353
        ret = vnodeProcessWriteMsg(pVnode, pMsg, pMsg->info.conn.applyIndex, &rsp);
1,155,413✔
354
        if (ret < 0) {
1,155,401!
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,155,401!
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,155,401✔
368
        } 
369
      }
370
    }
371

372
    vnodePostBlockMsg(pVnode, pMsg);
1,155,401✔
373
    if (rsp.info.handle != NULL) {
1,155,401✔
374
      tmsgSendRsp(&rsp);
211,783✔
375
    } else {
376
      if (rsp.pCont) {
943,618✔
377
        rpcFreeCont(rsp.pCont);
869,909✔
378
      }
379
    }
380

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

388
int32_t vnodeProcessSyncMsg(SVnode *pVnode, SRpcMsg *pMsg, SRpcMsg **pRsp) {
1,423,983✔
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,423,983!
390

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

397
  return code;
1,424,059✔
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) {
335,134✔
420
  if (pMsg == NULL || pMsg->pCont == NULL) {
335,134!
421
    return TSDB_CODE_INVALID_PARA;
×
422
  }
423

424
  if (msgcb == NULL || msgcb->putToQueueFp == NULL) {
335,141✔
425
    rpcFreeCont(pMsg->pCont);
3✔
426
    pMsg->pCont = NULL;
×
427
    return TSDB_CODE_INVALID_PARA;
×
428
  }
429

430
  int32_t code = tmsgPutToQueue(msgcb, SYNC_QUEUE, pMsg);
335,138✔
431
  if (code != 0) {
335,133✔
432
    rpcFreeCont(pMsg->pCont);
2,056✔
433
    pMsg->pCont = NULL;
2,056✔
434
  }
435
  return code;
335,133✔
436
}
437

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

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

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

456
  vGDebug(&pMsg->info.traceId,
1,155,426!
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,155,423✔
463
  if (code < 0) {
1,155,425✔
464
    if (code == TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE) {
15!
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));
15!
472
    }
473
  }
474
  return code;
1,155,426✔
475
}
476

477
static int32_t vnodeSyncCommitMsg(const SSyncFSM *pFsm, SRpcMsg *pMsg, SFsmCbMeta *pMeta) {
1,155,426✔
478
  if (pMsg->code == 0) {
1,155,426!
479
    return vnodeSyncApplyMsg(pFsm, pMsg, pMeta);
1,155,428✔
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,996,017✔
509
  SVnode *pVnode = pFSM->data;
2,996,017✔
510
  return atomic_load_64(&pVnode->state.applied);
2,996,017✔
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) {
88✔
522
  SVnode *pVnode = pFsm->data;
88✔
523
  return vnodeSnapReaderOpen(pVnode, (SSnapshotParam *)pParam, (SVSnapReader **)ppReader);
88✔
524
}
525

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

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

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

539
  do {
×
540
    int32_t itemSize = tmsgGetQueueSize(&pVnode->msgCb, pVnode->config.vgId, APPLY_QUEUE);
88✔
541
    if (itemSize == 0) {
88!
542
      vInfo("vgId:%d, start write vnode snapshot since apply queue is empty", pVnode->config.vgId);
88!
543
      break;
88✔
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);
88✔
551
}
552

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

558
  int32_t code = vnodeSnapWriterClose(pWriter, !isApply, pSnapshot);
88✔
559
  if (code != 0) {
88!
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;
88✔
564
}
565

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

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

579
  do {
580
    appliedIdx = vnodeSyncAppliedIndex(pFsm);
39,614✔
581
    if (appliedIdx > commitIdx) {
39,658!
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) {
39,658✔
587
      vInfo("vgId:%d, no items to be applied, restore finish", pVnode->config.vgId);
16,749✔
588
      break;
16,749✔
589
    } else {
590
      if (appliedIdx % 10 == 0) {
22,909✔
591
        vInfo("vgId:%d, restore not finish since %" PRId64 " items to be applied. commit-index:%" PRId64
1,182!
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
21,727✔
596
               ", applied-index:%" PRId64,
597
               vgId, commitIdx - appliedIdx, commitIdx, appliedIdx);
598
      }
599
      taosMsleep(10);
22,912✔
600
    }
601
  } while (true);
602

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

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

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

616
  if (vnodeIsRoleLeader(pVnode)) {
15,145✔
617
    // start to restore all stream tasks
618
    if (tsDisableStream) {
11,272!
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);
11,272✔
622
      if (pMeta->startInfo.startAllTasks == 1) {
11,272!
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;
11,272✔
628
        streamMetaWUnLock(pMeta);
11,272✔
629

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

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

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

650
  (void)taosThreadMutexLock(&pVnode->lock);
8,084✔
651
  if (pVnode->blocked) {
8,084!
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);
8,084✔
659

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

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

674
  (void)taosThreadMutexLock(&pVnode->lock);
282✔
675
  if (pVnode->blocked) {
282!
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);
282✔
683
}
282✔
684

685
static void vnodeBecomeLeader(const SSyncFSM *pFsm) {
12,882✔
686
  SVnode *pVnode = pFsm->data;
12,882✔
687
  vDebug("vgId:%d, become leader", pVnode->config.vgId);
12,882✔
688
#ifdef USE_TQ
689
  if (pVnode->pTq) {
12,882!
690
    tqUpdateNodeStage(pVnode->pTq, true);
12,883✔
691
  }
692
#endif
693
}
12,882✔
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) {
101,638✔
717
  SVnode *pVnode = pFsm->data;
101,638✔
718

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

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

757
  return pFsm;
16,057✔
758
}
759

760
int32_t vnodeSyncOpen(SVnode *pVnode, char *path, int32_t vnodeVersion) {
16,058✔
761
  SSyncInfo syncInfo = {
16,058✔
762
      .snapshotStrategy = SYNC_STRATEGY_WAL_FIRST,
763
      .batchSize = 1,
764
      .vgId = pVnode->config.vgId,
16,058✔
765
      .syncCfg = pVnode->config.syncCfg,
766
      .pWal = pVnode->pWal,
16,058✔
767
      .msgcb = &pVnode->msgCb,
16,058✔
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);
16,058✔
777
  syncInfo.pFsm = vnodeSyncMakeFsm(pVnode);
16,058✔
778

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

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

793
  return 0;
16,058✔
794
}
795

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

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

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

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

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

835
void vnodeSyncCheckTimeout(SVnode *pVnode) {
8,023✔
836
  vTrace("vgId:%d, check sync timeout msg", pVnode->config.vgId);
8,023✔
837
  (void)taosThreadMutexLock(&pVnode->lock);
8,023✔
838
  if (pVnode->blocked) {
8,023!
839
    int32_t curSec = taosGetTimestampSec();
×
840
    int32_t delta = curSec - pVnode->blockSec;
×
841
    if (delta > VNODE_TIMEOUT_SEC) {
×
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);
8,023✔
861
}
8,023✔
862

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

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

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

877
  if (state.state != TAOS_SYNC_STATE_LEADER) {
17,210!
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) {
17,210!
884
    terrno = TSDB_CODE_SYN_RESTORING;
1✔
885
    vInfo("vgId:%d, vnode not restored:%d:%d", pVnode->config.vgId, state.restored, pVnode->restored);
×
886
    return false;
×
887
  }
888

889
  return true;
17,209✔
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) {
811,501✔
898
  SSyncCfg *syncCfg = &pVnode->config.syncCfg;
811,501✔
899
  return syncCfg->nodeInfo[syncCfg->myIndex].nodeId;
811,501✔
900
}
901

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

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

913
  if (pSnap->type == TDMT_SYNC_PREP_SNAPSHOT || pSnap->type == TDMT_SYNC_PREP_SNAPSHOT_REPLY) {
535,790✔
914
    code = tsdbSnapPrepDescription(pVnode, pSnap);
171✔
915
  }
916
  return code;
535,796✔
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